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 f360d844de ("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 <johannes.schindelin@gmx.de> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Thomas Gummerer committed
Jan 31, 2022 at 13:30 UTC
c9e04d905edb5487c43b03304704e8d1248f9ac0
2 files changed
+17
-4
builtin/fetch.c
+6
-4
@@ -1609,12 +1609,14 @@ static int do_fetch(struct transport *transport,
1609
* don't care whether --tags was specified.
1610
*/
1611
if (rs->nr) {
1612
- prune_refs(rs, ref_map, transport->url);
1612
+ retcode = prune_refs(rs, ref_map, transport->url);
1613
} 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);
1617
}
1618
+ if (retcode != 0)
1619
+ retcode = 1;
1620
}
1621
if (fetch_and_consume_refs(transport, ref_map, worktrees)) {
1622
free_refs(ref_map);
t/t5510-fetch.sh
+11
@@ -164,6 +164,17 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
164
git rev-parse sometag
165
'
166
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
+
178
test_expect_success 'fetch --atomic works with a single branch' '
179
test_when_finished "rm -rf \"$D\"/atomic" &&
180