diff-index --quiet: learn the "stop feeding the backend early" logic

A negative return from the unpack callback function usually means unpack failed for the entry and signals the unpack_trees() machinery to fail the entire merge operation, immediately and there is no other way for the callback to tell the machinery to exit early without reporting an error. This is what we usually want to make a merge all-or-nothing operation, but the machinery is also used for diff-index codepath by using a custom unpack callback function. And we do sometimes want to exit early without failing, namely when we are under --quiet and can short-cut the diff upon finding the first difference. Add "exiting_early" field to unpack_trees_options structure, to signal the unpack_trees() machinery that the negative return value is not signaling an error but an early return from the unpack_trees() machinery. As this by definition hasn't unpacked everything, discard the resulting index just like the failure codepath. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 31, 2011 at 10:06 UTC b4194828dc00e97dcd07ecb4bb4f87650a6fca1d
3 files changed +10 -2
diff-lib.c
+6 -1
@@ -433,8 +433,13 @@ static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)
433 if (tree == o->df_conflict_entry)
434 tree = NULL;
435
436 - if (ce_path_match(idx ? idx : tree, &revs->prune_data))
436 + if (ce_path_match(idx ? idx : tree, &revs->prune_data)) {
437 do_oneway_diff(o, idx, tree);
438 + if (diff_can_quit_early(&revs->diffopt)) {
439 + o->exiting_early = 1;
440 + return -1;
441 + }
442 + }
443
444 return 0;
445 }
unpack-trees.c
+3 -1
@@ -593,7 +593,7 @@ static int unpack_nondirectories(int n, unsigned long mask,
593 static int unpack_failed(struct unpack_trees_options *o, const char *message)
594 {
595 discard_index(&o->result);
596 - if (!o->gently) {
596 + if (!o->gently && !o->exiting_early) {
597 if (message)
598 return error("%s", message);
599 return -1;
@@ -1128,6 +1128,8 @@ return_failed:
1128 display_error_msgs(o);
1129 mark_all_ce_unused(o->src_index);
1130 ret = unpack_failed(o, NULL);
1131 + if (o->exiting_early)
1132 + ret = 0;
1133 goto done;
1134 }
1135
unpack-trees.h
+1
@@ -46,6 +46,7 @@ struct unpack_trees_options {
46 debug_unpack,
47 skip_sparse_checkout,
48 gently,
49 + exiting_early,
50 show_all_errors;
51 const char *prefix;
52 int cache_bottom;