Skip to content

Commit 441adf0

Browse files
jaysoffiangitster
authored andcommitted
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 <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e02f176 commit 441adf0

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

builtin-remote.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static int add_known_remote(struct remote *remote, void *cb_data)
296296

297297
struct branches_for_remote {
298298
struct remote *remote;
299-
struct string_list *branches;
299+
struct string_list *branches, *skipped;
300300
struct known_remotes *keep;
301301
};
302302

@@ -321,6 +321,16 @@ static int add_branch_for_removal(const char *refname,
321321
return 0;
322322
}
323323

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+
324334
/* make sure that symrefs are deleted */
325335
if (flags & REF_ISSYMREF)
326336
return unlink(git_path("%s", refname));
@@ -355,7 +365,10 @@ static int rm(int argc, const char **argv)
355365
struct strbuf buf;
356366
struct known_remotes known_remotes = { NULL, NULL };
357367
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+
};
359372
int i, result;
360373

361374
if (argc != 2)
@@ -404,6 +417,18 @@ static int rm(int argc, const char **argv)
404417
result = remove_branches(&branches);
405418
string_list_clear(&branches, 1);
406419

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+
407432
return result;
408433
}
409434

t/t5505-remote.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ test_expect_success 'remove remote' '
107107
)
108108
'
109109

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+
110136
cat > test/expect << EOF
111137
* remote origin
112138
URL: $(pwd)/one

0 commit comments

Comments
 (0)