Skip to content

Commit c9e04d9

Browse files
tgummerergitster
authored andcommitted
fetch --prune: exit with error if pruning fails
When pruning refs fails, we print an error to stderr, but still exit 0 from 'git fetch'. Since this is a genuine error, fetch should be exiting with some non-zero exit code. Make it so. The --prune option was introduced in f360d84 ("builtin-fetch: add --prune option", 2009-11-10). Unfortunately it's unclear from that commit whether ignoring the exit code was an oversight or intentional, but it feels like an oversight. Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c53a8c commit c9e04d9

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

builtin/fetch.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,12 +1609,14 @@ static int do_fetch(struct transport *transport,
16091609
* don't care whether --tags was specified.
16101610
*/
16111611
if (rs->nr) {
1612-
prune_refs(rs, ref_map, transport->url);
1612+
retcode = prune_refs(rs, ref_map, transport->url);
16131613
} else {
1614-
prune_refs(&transport->remote->fetch,
1615-
ref_map,
1616-
transport->url);
1614+
retcode = prune_refs(&transport->remote->fetch,
1615+
ref_map,
1616+
transport->url);
16171617
}
1618+
if (retcode != 0)
1619+
retcode = 1;
16181620
}
16191621
if (fetch_and_consume_refs(transport, ref_map, worktrees)) {
16201622
free_refs(ref_map);

t/t5510-fetch.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
164164
git rev-parse sometag
165165
'
166166

167+
test_expect_success REFFILES 'fetch --prune fails to delete branches' '
168+
cd "$D" &&
169+
git clone . prune-fail &&
170+
cd prune-fail &&
171+
git update-ref refs/remotes/origin/extrabranch main &&
172+
: this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds &&
173+
>.git/packed-refs.new &&
174+
175+
test_must_fail git fetch --prune origin
176+
'
177+
167178
test_expect_success 'fetch --atomic works with a single branch' '
168179
test_when_finished "rm -rf \"$D\"/atomic" &&
169180

0 commit comments

Comments
 (0)