builtin-remote: make rm operation safer in mirrored repository
"git remote rm <repo>" happily removes non-remote refs and their reflogs. This may be okay if the repository truely is a mirror, but if the user had done "git remote add --mirror <repo>" by accident and was just undoing their mistake, then they are left in a situation that is difficult to recover from. After this commit, "git remote rm" skips over non-remote refs. The user is advised on how remove branches using "git branch -d", which itself has nice safety checks wrt to branch removal lacking from "git remote rm". Non-remote non-branch refs are skipped silently. Signed-off-by: Jay Soffian <jaysoffian@gmail.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jay Soffian committed
Feb 4, 2009 at 11:06 UTC
441adf0ccf571a9fe15658fdfcd856d2aabc01cb
2 files changed
+53
-2
builtin-remote.c
+27
-2
@@ -296,7 +296,7 @@ static int add_known_remote(struct remote *remote, void *cb_data)
296
297
struct branches_for_remote {
298
struct remote *remote;
299
- struct string_list *branches;
299
+ struct string_list *branches, *skipped;
300
struct known_remotes *keep;
301
};
302
@@ -321,6 +321,16 @@ static int add_branch_for_removal(const char *refname,
321
return 0;
322
}
323
324
+ /* don't delete non-remote refs */
325
+ if (prefixcmp(refname, "refs/remotes")) {
326
+ /* advise user how to delete local branches */
327
+ if (!prefixcmp(refname, "refs/heads/"))
328
+ string_list_append(abbrev_branch(refname),
329
+ branches->skipped);
330
+ /* silently skip over other non-remote refs */
331
+ return 0;
332
+ }
333
+
334
/* make sure that symrefs are deleted */
335
if (flags & REF_ISSYMREF)
336
return unlink(git_path("%s", refname));
@@ -355,7 +365,10 @@ static int rm(int argc, const char **argv)
365
struct strbuf buf;
366
struct known_remotes known_remotes = { NULL, NULL };
367
struct string_list branches = { NULL, 0, 0, 1 };
358
- struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
368
+ struct string_list skipped = { NULL, 0, 0, 1 };
369
+ struct branches_for_remote cb_data = {
370
+ NULL, &branches, &skipped, &known_remotes
371
+ };
372
int i, result;
373
374
if (argc != 2)
@@ -404,6 +417,18 @@ static int rm(int argc, const char **argv)
417
result = remove_branches(&branches);
418
string_list_clear(&branches, 1);
419
420
+ if (skipped.nr) {
421
+ fprintf(stderr, skipped.nr == 1 ?
422
+ "Note: A non-remote branch was not removed; "
423
+ "to delete it, use:\n" :
424
+ "Note: Non-remote branches were not removed; "
425
+ "to delete them, use:\n");
426
+ for (i = 0; i < skipped.nr; i++)
427
+ fprintf(stderr, " git branch -d %s\n",
428
+ skipped.items[i].string);
429
+ }
430
+ string_list_clear(&skipped, 0);
431
+
432
return result;
433
}
434
t/t5505-remote.sh
+26
@@ -107,6 +107,32 @@ test_expect_success 'remove remote' '
107
)
108
'
109
110
+test_expect_success 'remove remote protects non-remote branches' '
111
+(
112
+ cd test &&
113
+ (cat >expect1 <<EOF
114
+Note: A non-remote branch was not removed; to delete it, use:
115
+ git branch -d master
116
+EOF
117
+ cat >expect2 <<EOF
118
+Note: Non-remote branches were not removed; to delete them, use:
119
+ git branch -d foobranch
120
+ git branch -d master
121
+EOF
122
+) &&
123
+ git tag footag
124
+ git config --add remote.oops.fetch "+refs/*:refs/*" &&
125
+ git remote rm oops 2>actual1 &&
126
+ git branch foobranch &&
127
+ git config --add remote.oops.fetch "+refs/*:refs/*" &&
128
+ git remote rm oops 2>actual2 &&
129
+ git branch -d foobranch &&
130
+ git tag -d footag &&
131
+ test_cmp expect1 actual1 &&
132
+ test_cmp expect2 actual2
133
+)
134
+'
135
+
136
cat > test/expect << EOF
137
* remote origin
138
URL: $(pwd)/one