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>
Linus Torvalds committed
Nov 29, 2005 at 21:06 UTC
10637b84d91cf8870d1db8609a10dc5e58722378
2 files changed
+64
-8
Documentation/git-diff-files.txt
+10
@@ -21,6 +21,16 @@ OPTIONS
21
-------
22
include::diff-options.txt[]
23
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
+
34
-q::
35
Remain silent even on nonexisting files
36
diff-files.c
+54
-8
@@ -7,12 +7,12 @@
7
#include "diff.h"
8
9
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>...]"
11
COMMON_DIFF_OPTIONS_HELP;
12
13
static struct diff_options diff_options;
14
static int silent = 0;
15
+static int diff_unmerged_stage = -1;
16
17
static void show_unmerge(const char *path)
18
{
@@ -46,7 +46,21 @@ int main(int argc, const char **argv)
46
argc--;
47
break;
48
}
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"))
64
silent = 1;
65
else if (!strcmp(argv[1], "-r"))
66
; /* no-op */
@@ -73,6 +87,20 @@ int main(int argc, const char **argv)
87
pathspec = get_pathspec(prefix, argv + 1);
88
entries = read_cache();
89
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
+
104
if (diff_setup_done(&diff_options) < 0)
105
usage(diff_files_usage);
106
@@ -94,13 +122,31 @@ int main(int argc, const char **argv)
122
continue;
123
124
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;
135
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;
147
}
148
+ else if (diff_unmerged_stage)
149
+ continue;
150
151
if (lstat(ce->name, &st) < 0) {
152
if (errno != ENOENT && errno != ENOTDIR) {