diff-files: -c and --cc options.
This ports the "combined diff" to diff-files so that differences to the working tree files since stage 2 and stage 3 are shown the same way as combined diff output from diff-tree for the merge commit would be shown if the current working tree files are committed. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 28, 2006 at 00:03 UTC
ea726d02e9677a66586d7ffebe97f112ab6dab33
3 files changed
+99
-23
combine-diff.c
+46
-20
@@ -4,14 +4,6 @@
4
#include "diffcore.h"
5
#include "quote.h"
6
7
-struct path_list {
8
- struct path_list *next;
9
- int len;
10
- char *path;
11
- unsigned char sha1[20];
12
- unsigned char parent_sha1[FLEX_ARRAY][20];
13
-};
14
-
7
static int uninteresting(struct diff_filepair *p)
8
{
9
if (diff_unmodified_pair(p))
@@ -21,15 +13,14 @@ static int uninteresting(struct diff_filepair *p)
13
return 0;
14
}
15
24
-static struct path_list *intersect_paths(struct path_list *curr,
25
- int n, int num_parent)
16
+static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
17
{
18
struct diff_queue_struct *q = &diff_queued_diff;
28
- struct path_list *p;
19
+ struct combine_diff_path *p;
20
int i;
21
22
if (!n) {
32
- struct path_list *list = NULL, **tail = &list;
23
+ struct combine_diff_path *list = NULL, **tail = &list;
24
for (i = 0; i < q->nr; i++) {
25
int len;
26
const char *path;
@@ -532,18 +523,52 @@ static void dump_sline(struct sline *sline, int cnt, int num_parent)
523
}
524
}
525
535
-static int show_combined_diff(struct path_list *elem, int num_parent,
536
- int dense, const char *header, int show_empty)
526
+int show_combined_diff(struct combine_diff_path *elem, int num_parent,
527
+ int dense, const char *header, int show_empty)
528
{
529
unsigned long size, cnt, lno;
530
char *result, *cp, *ep;
531
struct sline *sline; /* survived lines */
532
int i, show_hunks, shown_header = 0;
542
- char ourtmp[TMPPATHLEN];
533
+ char ourtmp_buf[TMPPATHLEN];
534
+ char *ourtmp = ourtmp_buf;
535
536
/* Read the result of merge first */
545
- result = grab_blob(elem->sha1, &size);
546
- write_to_temp_file(ourtmp, result, size);
537
+ if (memcmp(elem->sha1, null_sha1, 20)) {
538
+ result = grab_blob(elem->sha1, &size);
539
+ write_to_temp_file(ourtmp, result, size);
540
+ }
541
+ else {
542
+ struct stat st;
543
+ int fd;
544
+ ourtmp = elem->path;
545
+ if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
546
+ !fstat(fd, &st)) {
547
+ int len = st.st_size;
548
+ int cnt = 0;
549
+
550
+ size = len;
551
+ result = xmalloc(len + 1);
552
+ while (cnt < len) {
553
+ int done = xread(fd, result+cnt, len-cnt);
554
+ if (done == 0)
555
+ break;
556
+ if (done < 0)
557
+ die("read error '%s'", ourtmp);
558
+ cnt += done;
559
+ }
560
+ result[len] = 0;
561
+ }
562
+ else {
563
+ /* deleted file */
564
+ size = 0;
565
+ result = xmalloc(1);
566
+ result[0] = 0;
567
+ ourtmp = "/dev/null";
568
+ }
569
+ if (0 <= fd)
570
+ close(fd);
571
+ }
572
573
for (cnt = 0, cp = result; cp - result < size; cp++) {
574
if (*cp == '\n')
@@ -589,7 +614,8 @@ static int show_combined_diff(struct path_list *elem, int num_parent,
614
putchar('\n');
615
dump_sline(sline, cnt, num_parent);
616
}
592
- unlink(ourtmp);
617
+ if (ourtmp == ourtmp_buf)
618
+ unlink(ourtmp);
619
free(result);
620
621
for (i = 0; i < cnt; i++) {
@@ -613,7 +639,7 @@ int diff_tree_combined_merge(const unsigned char *sha1,
639
struct commit *commit = lookup_commit(sha1);
640
struct diff_options diffopts;
641
struct commit_list *parents;
616
- struct path_list *p, *paths = NULL;
642
+ struct combine_diff_path *p, *paths = NULL;
643
int num_parent, i, num_paths;
644
645
diff_setup(&diffopts);
@@ -654,7 +680,7 @@ int diff_tree_combined_merge(const unsigned char *sha1,
680
681
/* Clean things up */
682
while (paths) {
657
- struct path_list *tmp = paths;
683
+ struct combine_diff_path *tmp = paths;
684
paths = paths->next;
685
free(tmp);
686
}
diff-files.c
+42
-3
@@ -7,12 +7,14 @@
7
#include "diff.h"
8
9
static const char diff_files_usage[] =
10
-"git-diff-files [-q] [-0/-1/2/3] [<common diff options>] [<path>...]"
10
+"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<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 = 2;
16
+static int combine_merges = 0;
17
+static int dense_combined_merges = 0;
18
19
static void show_unmerge(const char *path)
20
{
@@ -66,6 +68,10 @@ int main(int argc, const char **argv)
68
; /* no-op */
69
else if (!strcmp(argv[1], "-s"))
70
; /* no-op */
71
+ else if (!strcmp(argv[1], "-c"))
72
+ combine_merges = 1;
73
+ else if (!strcmp(argv[1], "--cc"))
74
+ dense_combined_merges = combine_merges = 1;
75
else {
76
int diff_opt_cnt;
77
diff_opt_cnt = diff_opt_parse(&diff_options,
@@ -82,6 +88,9 @@ int main(int argc, const char **argv)
88
}
89
argv++; argc--;
90
}
91
+ if (combine_merges) {
92
+ diff_options.output_format = DIFF_FORMAT_PATCH;
93
+ }
94
95
/* Find the directory, and set up the pathspec */
96
pathspec = get_pathspec(prefix, argv + 1);
@@ -108,14 +117,35 @@ int main(int argc, const char **argv)
117
continue;
118
119
if (ce_stage(ce)) {
111
- show_unmerge(ce->name);
120
+ struct {
121
+ struct combine_diff_path p;
122
+ unsigned char fill[4][20];
123
+ } combine;
124
+
125
+ combine.p.next = NULL;
126
+ combine.p.len = ce_namelen(ce);
127
+ combine.p.path = xmalloc(combine.p.len + 1);
128
+ memcpy(combine.p.path, ce->name, combine.p.len);
129
+ combine.p.path[combine.p.len] = 0;
130
+ memset(combine.p.sha1, 0, 100);
131
+
132
while (i < entries) {
133
struct cache_entry *nce = active_cache[i];
134
+ int stage;
135
136
if (strcmp(ce->name, nce->name))
137
break;
138
+
139
+ /* Stage #2 (ours) is the first parent,
140
+ * stage #3 (theirs) is the second.
141
+ */
142
+ stage = ce_stage(nce);
143
+ if (2 <= stage)
144
+ memcpy(combine.p.parent_sha1[stage-2],
145
+ nce->sha1, 20);
146
+
147
/* diff against the proper unmerged stage */
118
- if (ce_stage(nce) == diff_unmerged_stage)
148
+ if (stage == diff_unmerged_stage)
149
ce = nce;
150
i++;
151
}
@@ -123,10 +153,19 @@ int main(int argc, const char **argv)
153
* Compensate for loop update
154
*/
155
i--;
156
+
157
+ if (combine_merges) {
158
+ show_combined_diff(&combine.p, 2,
159
+ dense_combined_merges,
160
+ NULL, 0);
161
+ continue;
162
+ }
163
+
164
/*
165
* Show the diff for the 'ce' if we found the one
166
* from the desired stage.
167
*/
168
+ show_unmerge(ce->name);
169
if (ce_stage(ce) != diff_unmerged_stage)
170
continue;
171
}
diff.h
+11
@@ -56,6 +56,17 @@ extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
56
extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
57
const char *base, struct diff_options *opt);
58
59
+struct combine_diff_path {
60
+ struct combine_diff_path *next;
61
+ int len;
62
+ char *path;
63
+ unsigned char sha1[20];
64
+ unsigned char parent_sha1[FLEX_ARRAY][20];
65
+};
66
+
67
+int show_combined_diff(struct combine_diff_path *elem, int num_parent,
68
+ int dense, const char *header, int show_empty);
69
+
70
extern int diff_tree_combined_merge(const unsigned char *sha1, const char *, int, int);
71
72
extern void diff_addremove(struct diff_options *,