Skip to content

Commit a92b109

Browse files
committed
merge-ours: do not use cmd_*() as a subroutine
The call to cmd_diff_index() "git merge-ours" makes has been working by accident that the function did not call exit(3), and the caller exited almost immediately after making a call, but it sets a bad precedent for people to cut and paste. For finding out if the index exactly matches the HEAD (or a given tree-ish), there is index_differs_from() which is exactly written for that purpose. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent be26d2b commit a92b109

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

builtin/merge-ours.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@
99
*/
1010
#include "git-compat-util.h"
1111
#include "builtin.h"
12+
#include "diff.h"
1213

1314
static const char builtin_merge_ours_usage[] =
1415
"git merge-ours <base>... -- HEAD <remote>...";
1516

16-
static const char *diff_index_args[] = {
17-
"diff-index", "--quiet", "--cached", "HEAD", "--", NULL
18-
};
19-
#define NARGS (ARRAY_SIZE(diff_index_args) - 1)
20-
2117
int cmd_merge_ours(int argc, const char **argv, const char *prefix)
2218
{
2319
if (argc == 2 && !strcmp(argv[1], "-h"))
2420
usage(builtin_merge_ours_usage);
2521

2622
/*
27-
* We need to exit with 2 if the index does not match our HEAD tree,
28-
* because the current index is what we will be committing as the
29-
* merge result.
23+
* The contents of the current index becomes the tree we
24+
* commit. The index must match HEAD, or this merge cannot go
25+
* through.
3026
*/
31-
if (cmd_diff_index(NARGS, diff_index_args, prefix))
27+
if (read_cache() < 0)
28+
die_errno("read_cache failed");
29+
if (index_differs_from("HEAD", 0, 0))
3230
exit(2);
3331
exit(0);
3432
}

0 commit comments

Comments
 (0)