Skip to content

Commit 10637b8

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
diff-files: -1/-2/-3 to diff against unmerged stage.
While resolving conflicted merge, it was not easy to compare the working tree file with unmerged index entries. This commit introduces new options -1/-2/-3 (with synonyms --base, --ours, and --theirs) to compare working tree files with specified stages. When none of these options are given, the command defaults to -2 if the index file is unmerged, otherwise it acts as before. [jc: majorly butchered from the version Linus originally posted.] Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 5620518 commit 10637b8

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

Documentation/git-diff-files.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ OPTIONS
2121
-------
2222
include::diff-options.txt[]
2323

24+
-1 -2 -3 or --base --ours --theirs, and -0::
25+
Diff against the "base" version, "our branch" or "their
26+
branch" respectively. With these options, diffs for
27+
merged entries are not shown.
28+
+
29+
The default is to diff against our branch (-2) if there
30+
is an unmerged path, and show diff for unmerged entries
31+
otherwise. The option -0 can be given to force diff for
32+
unmerged entries even when the index is unmerged.
33+
2434
-q::
2535
Remain silent even on nonexisting files
2636

diff-files.c

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include "diff.h"
88

99
static const char diff_files_usage[] =
10-
"git-diff-files [-q] "
11-
"[<common diff options>] [<path>...]"
10+
"git-diff-files [-q] [-0/-1/2/3] [<common diff options>] [<path>...]"
1211
COMMON_DIFF_OPTIONS_HELP;
1312

1413
static struct diff_options diff_options;
1514
static int silent = 0;
15+
static int diff_unmerged_stage = -1;
1616

1717
static void show_unmerge(const char *path)
1818
{
@@ -46,7 +46,21 @@ int main(int argc, const char **argv)
4646
argc--;
4747
break;
4848
}
49-
if (!strcmp(argv[1], "-q"))
49+
if (!strcmp(argv[1], "-0"))
50+
diff_unmerged_stage = 0;
51+
else if (!strcmp(argv[1], "-1"))
52+
diff_unmerged_stage = 1;
53+
else if (!strcmp(argv[1], "-2"))
54+
diff_unmerged_stage = 2;
55+
else if (!strcmp(argv[1], "-3"))
56+
diff_unmerged_stage = 3;
57+
else if (!strcmp(argv[1], "--base"))
58+
diff_unmerged_stage = 1;
59+
else if (!strcmp(argv[1], "--ours"))
60+
diff_unmerged_stage = 2;
61+
else if (!strcmp(argv[1], "--theirs"))
62+
diff_unmerged_stage = 3;
63+
else if (!strcmp(argv[1], "-q"))
5064
silent = 1;
5165
else if (!strcmp(argv[1], "-r"))
5266
; /* no-op */
@@ -73,6 +87,20 @@ int main(int argc, const char **argv)
7387
pathspec = get_pathspec(prefix, argv + 1);
7488
entries = read_cache();
7589

90+
if (diff_unmerged_stage < 0) {
91+
/* default to "ours" if unmerged index, otherwise 0 */
92+
for (i = 0; i < entries; i++) {
93+
struct cache_entry *ce = active_cache[i];
94+
if (ce_stage(ce)) {
95+
diff_unmerged_stage = 2;
96+
break;
97+
}
98+
}
99+
if (diff_unmerged_stage < 0)
100+
diff_unmerged_stage = 0;
101+
}
102+
103+
76104
if (diff_setup_done(&diff_options) < 0)
77105
usage(diff_files_usage);
78106

@@ -94,13 +122,31 @@ int main(int argc, const char **argv)
94122
continue;
95123

96124
if (ce_stage(ce)) {
97-
show_unmerge(ce->name);
98-
while (i < entries &&
99-
!strcmp(ce->name, active_cache[i]->name))
125+
if (!diff_unmerged_stage)
126+
show_unmerge(ce->name);
127+
while (i < entries) {
128+
struct cache_entry *nce = active_cache[i];
129+
130+
if (strcmp(ce->name, nce->name))
131+
break;
132+
/* diff against the proper unmerged stage */
133+
if (ce_stage(nce) == diff_unmerged_stage)
134+
ce = nce;
100135
i++;
101-
i--; /* compensate for loop control increments */
102-
continue;
136+
}
137+
/*
138+
* Compensate for loop update
139+
*/
140+
i--;
141+
/*
142+
* Show the diff for the 'ce' if we found the one
143+
* from the desired stage.
144+
*/
145+
if (ce_stage(ce) != diff_unmerged_stage)
146+
continue;
103147
}
148+
else if (diff_unmerged_stage)
149+
continue;
104150

105151
if (lstat(ce->name, &st) < 0) {
106152
if (errno != ENOENT && errno != ENOTDIR) {

0 commit comments

Comments
 (0)