Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 1070 lines (971 sloc) 28.34 kb
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
1 #include "cache.h"
2 #include "commit.h"
8e44025 Use blob_, commit_, tag_, and tree_type throughout.
Peter Eriksen authored
3 #include "blob.h"
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
4 #include "diff.h"
5 #include "diffcore.h"
6 #include "quote.h"
d9ea73e combine-diff: refactor built-in xdiff interface.
Junio C Hamano authored
7 #include "xdiff-interface.h"
9153983 Log message printout cleanups
Linus Torvalds authored
8 #include "log-tree.h"
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
9 #include "refs.h"
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
10
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
11 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
12 {
13 struct diff_queue_struct *q = &diff_queued_diff;
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
14 struct combine_diff_path *p;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
15 int i;
16
17 if (!n) {
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
18 struct combine_diff_path *list = NULL, **tail = &list;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
19 for (i = 0; i < q->nr; i++) {
20 int len;
21 const char *path;
a976b0a Remove combine-diff.c::uninteresting()
Junio C Hamano authored
22 if (diff_unmodified_pair(q->queue[i]))
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
23 continue;
24 path = q->queue[i]->two->path;
25 len = strlen(path);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
26 p = xmalloc(combine_diff_path_size(num_parent, len));
4b25d09 Felipe Contreras Fix a bunch of pointer declarations (codestyle)
felipec authored
27 p->path = (char *) &(p->parent[num_parent]);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
28 memcpy(p->path, path, len);
29 p->path[len] = 0;
30 p->len = len;
31 p->next = NULL;
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
32 memset(p->parent, 0,
33 sizeof(p->parent[0]) * num_parent);
34
e702496 Shawn O. Pearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
35 hashcpy(p->sha1, q->queue[i]->two->sha1);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
36 p->mode = q->queue[i]->two->mode;
e702496 Shawn O. Pearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
37 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
38 p->parent[n].mode = q->queue[i]->one->mode;
d416df8 combine-diff: Record diff status a bit more faithfully
Junio C Hamano authored
39 p->parent[n].status = q->queue[i]->status;
5290a0f combine-diff: fix appending at the tail of a list.
Junio C Hamano authored
40 *tail = p;
41 tail = &p->next;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
42 }
43 return list;
44 }
45
46 for (p = curr; p; p = p->next) {
47 int found = 0;
48 if (!p->len)
49 continue;
50 for (i = 0; i < q->nr; i++) {
51 const char *path;
52 int len;
53
a976b0a Remove combine-diff.c::uninteresting()
Junio C Hamano authored
54 if (diff_unmodified_pair(q->queue[i]))
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
55 continue;
56 path = q->queue[i]->two->path;
57 len = strlen(path);
58 if (len == p->len && !memcmp(path, p->path, len)) {
59 found = 1;
e702496 Shawn O. Pearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
60 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
61 p->parent[n].mode = q->queue[i]->one->mode;
d416df8 combine-diff: Record diff status a bit more faithfully
Junio C Hamano authored
62 p->parent[n].status = q->queue[i]->status;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
63 break;
64 }
65 }
66 if (!found)
67 p->len = 0;
68 }
69 return curr;
70 }
71
b469d8b combine-diff: add a bit more comments.
Junio C Hamano authored
72 /* Lines lost from parent */
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
73 struct lline {
74 struct lline *next;
75 int len;
76 unsigned long parent_map;
77 char line[FLEX_ARRAY];
78 };
79
b469d8b combine-diff: add a bit more comments.
Junio C Hamano authored
80 /* Lines surviving in the merge result */
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
81 struct sline {
82 struct lline *lost_head, **lost_tail;
83 char *bol;
84 int len;
46dc941 combine-diff: cleanup.
Junio C Hamano authored
85 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
86 * we did not change it).
b469d8b combine-diff: add a bit more comments.
Junio C Hamano authored
87 * bit N is used for "interesting" lines, including context.
c86fbe5 Junio C Hamano diff -c/--cc: do not include uninteresting deletion before leading conte...
gitster authored
88 * bit (N+1) is used for "do not show deletion before this".
b469d8b combine-diff: add a bit more comments.
Junio C Hamano authored
89 */
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
90 unsigned long flag;
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
91 unsigned long *p_lno;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
92 };
93
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
94 static char *grab_blob(const unsigned char *sha1, unsigned int mode, unsigned long *size)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
95 {
96 char *blob;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
97 enum object_type type;
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
98
99 if (S_ISGITLINK(mode)) {
100 blob = xmalloc(100);
101 *size = snprintf(blob, 100,
102 "Subproject commit %s\n", sha1_to_hex(sha1));
103 } else if (is_null_sha1(sha1)) {
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
104 /* deleted blob */
105 *size = 0;
106 return xcalloc(1, 1);
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
107 } else {
108 blob = read_sha1_file(sha1, &type, size);
109 if (type != OBJ_BLOB)
110 die("object '%s' is not a blob!", sha1_to_hex(sha1));
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
111 }
112 return blob;
113 }
114
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
115 static void append_lost(struct sline *sline, int n, const char *line, int len)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
116 {
117 struct lline *lline;
118 unsigned long this_mask = (1UL<<n);
119 if (line[len-1] == '\n')
120 len--;
121
122 /* Check to see if we can squash things */
123 if (sline->lost_head) {
124 struct lline *last_one = NULL;
125 /* We cannot squash it with earlier one */
126 for (lline = sline->lost_head;
127 lline;
128 lline = lline->next)
129 if (lline->parent_map & this_mask)
130 last_one = lline;
131 lline = last_one ? last_one->next : sline->lost_head;
132 while (lline) {
133 if (lline->len == len &&
134 !memcmp(lline->line, line, len)) {
135 lline->parent_map |= this_mask;
136 return;
137 }
138 lline = lline->next;
139 }
140 }
141
142 lline = xmalloc(sizeof(*lline) + len + 1);
143 lline->len = len;
144 lline->next = NULL;
145 lline->parent_map = this_mask;
146 memcpy(lline->line, line, len);
147 lline->line[len] = 0;
5290a0f combine-diff: fix appending at the tail of a list.
Junio C Hamano authored
148 *sline->lost_tail = lline;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
149 sline->lost_tail = &lline->next;
150 }
151
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
152 struct combine_diff_state {
a0fd314 Match ofs/cnt types in diff interface.
Junio C Hamano authored
153 unsigned int lno;
154 int ob, on, nb, nn;
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
155 unsigned long nmask;
156 int num_parent;
157 int n;
158 struct sline *sline;
159 struct sline *lost_bucket;
160 };
161
d9ea73e combine-diff: refactor built-in xdiff interface.
Junio C Hamano authored
162 static void consume_line(void *state_, char *line, unsigned long len)
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
163 {
d9ea73e combine-diff: refactor built-in xdiff interface.
Junio C Hamano authored
164 struct combine_diff_state *state = state_;
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
165 if (5 < len && !memcmp("@@ -", line, 4)) {
166 if (parse_hunk_header(line, len,
167 &state->ob, &state->on,
168 &state->nb, &state->nn))
169 return;
170 state->lno = state->nb;
171 if (!state->nb)
172 /* @@ -1,2 +0,0 @@ to remove the
173 * first two lines...
174 */
175 state->nb = 1;
176 if (state->nn == 0)
177 /* @@ -X,Y +N,0 @@ removed Y lines
178 * that would have come *after* line N
179 * in the result. Our lost buckets hang
180 * to the line after the removed lines,
181 */
182 state->lost_bucket = &state->sline[state->nb];
183 else
184 state->lost_bucket = &state->sline[state->nb-1];
185 if (!state->sline[state->nb-1].p_lno)
186 state->sline[state->nb-1].p_lno =
187 xcalloc(state->num_parent,
188 sizeof(unsigned long));
189 state->sline[state->nb-1].p_lno[state->n] = state->ob;
190 return;
191 }
192 if (!state->lost_bucket)
193 return; /* not in any hunk yet */
194 switch (line[0]) {
195 case '-':
196 append_lost(state->lost_bucket, state->n, line+1, len-1);
197 break;
198 case '+':
199 state->sline[state->lno-1].flag |= state->nmask;
200 state->lno++;
201 break;
202 }
203 }
204
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
205 static void combine_diff(const unsigned char *parent, unsigned int mode,
206 mmfile_t *result_file,
2386c29 combine-diff.c: type sanity
Junio C Hamano authored
207 struct sline *sline, unsigned int cnt, int n,
208 int num_parent)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
209 {
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
210 unsigned int p_lno, lno;
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
211 unsigned long nmask = (1UL << n);
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
212 xpparam_t xpp;
213 xdemitconf_t xecfg;
214 mmfile_t parent_file;
215 xdemitcb_t ecb;
216 struct combine_diff_state state;
217 unsigned long sz;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
218
4462731 combine-diff: do not punt on removed or added files.
Junio C Hamano authored
219 if (!cnt)
220 return; /* result deleted */
221
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
222 parent_file.ptr = grab_blob(parent, mode, &sz);
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
223 parent_file.size = sz;
9ccd0a8 Brian Downing Always initialize xpparam_t to 0
bdowning authored
224 memset(&xpp, 0, sizeof(xpp));
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
225 xpp.flags = XDF_NEED_MINIMAL;
30b2501 dscho Future-proof source for changes in xdemitconf_t
dscho authored
226 memset(&xecfg, 0, sizeof(xecfg));
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
227 memset(&state, 0, sizeof(state));
228 state.nmask = nmask;
229 state.sline = sline;
230 state.lno = 1;
231 state.num_parent = num_parent;
232 state.n = n;
233
8a3f524 Junio C Hamano xdiff-interface: hide the whole "xdiff_emit_state" business from the cal...
gitster authored
234 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
235 &xpp, &xecfg, &ecb);
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
236 free(parent_file.ptr);
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
237
238 /* Assign line numbers for this parent.
239 *
240 * sline[lno].p_lno[n] records the first line number
241 * (counting from 1) for parent N if the final hunk display
242 * started by showing sline[lno] (possibly showing the lost
243 * lines attached to it first).
244 */
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
245 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
246 struct lline *ll;
247 sline[lno].p_lno[n] = p_lno;
248
249 /* How many lines would this sline advance the p_lno? */
250 ll = sline[lno].lost_head;
251 while (ll) {
252 if (ll->parent_map & nmask)
253 p_lno++; /* '-' means parent had it */
254 ll = ll->next;
255 }
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
256 if (lno < cnt && !(sline[lno].flag & nmask))
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
257 p_lno++; /* no '+' means parent had it */
258 }
259 sline[lno].p_lno[n] = p_lno; /* trailer */
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
260 }
261
262 static unsigned long context = 3;
263 static char combine_marker = '@';
264
265 static int interesting(struct sline *sline, unsigned long all_mask)
266 {
46dc941 combine-diff: cleanup.
Junio C Hamano authored
267 /* If some parents lost lines here, or if we have added to
268 * some parent, it is interesting.
269 */
270 return ((sline->flag & all_mask) || sline->lost_head);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
271 }
272
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
273 static unsigned long adjust_hunk_tail(struct sline *sline,
274 unsigned long all_mask,
275 unsigned long hunk_begin,
276 unsigned long i)
277 {
46dc941 combine-diff: cleanup.
Junio C Hamano authored
278 /* i points at the first uninteresting line. If the last line
279 * of the hunk was interesting only because it has some
280 * deletion, then it is not all that interesting for the
281 * purpose of giving trailing context lines. This is because
282 * we output '-' line and then unmodified sline[i-1] itself in
283 * that case which gives us one extra context line.
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
284 */
46dc941 combine-diff: cleanup.
Junio C Hamano authored
285 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
286 i--;
287 return i;
288 }
289
46dc941 combine-diff: cleanup.
Junio C Hamano authored
290 static unsigned long find_next(struct sline *sline,
291 unsigned long mark,
292 unsigned long i,
293 unsigned long cnt,
2386c29 combine-diff.c: type sanity
Junio C Hamano authored
294 int look_for_uninteresting)
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
295 {
46dc941 combine-diff: cleanup.
Junio C Hamano authored
296 /* We have examined up to i-1 and are about to look at i.
297 * Find next interesting or uninteresting line. Here,
298 * "interesting" does not mean interesting(), but marked by
299 * the give_context() function below (i.e. it includes context
300 * lines that are not interesting to interesting() function
301 * that are surrounded by interesting() ones.
302 */
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
303 while (i <= cnt)
2386c29 combine-diff.c: type sanity
Junio C Hamano authored
304 if (look_for_uninteresting
46dc941 combine-diff: cleanup.
Junio C Hamano authored
305 ? !(sline[i].flag & mark)
306 : (sline[i].flag & mark))
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
307 return i;
308 else
309 i++;
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
310 return i;
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
311 }
312
313 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
314 {
315 unsigned long all_mask = (1UL<<num_parent) - 1;
316 unsigned long mark = (1UL<<num_parent);
c86fbe5 Junio C Hamano diff -c/--cc: do not include uninteresting deletion before leading conte...
gitster authored
317 unsigned long no_pre_delete = (2UL<<num_parent);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
318 unsigned long i;
319
46dc941 combine-diff: cleanup.
Junio C Hamano authored
320 /* Two groups of interesting lines may have a short gap of
82e5a82 Pavel Roskin Fix more typos, primarily in the code
proski authored
321 * uninteresting lines. Connect such groups to give them a
46dc941 combine-diff: cleanup.
Junio C Hamano authored
322 * bit of context.
323 *
324 * We first start from what the interesting() function says,
325 * and mark them with "mark", and paint context lines with the
326 * mark. So interesting() would still say false for such context
327 * lines but they are treated as "interesting" in the end.
328 */
329 i = find_next(sline, mark, 0, cnt, 0);
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
330 if (cnt < i)
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
331 return 0;
332
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
333 while (i <= cnt) {
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
334 unsigned long j = (context < i) ? (i - context) : 0;
335 unsigned long k;
46dc941 combine-diff: cleanup.
Junio C Hamano authored
336
337 /* Paint a few lines before the first interesting line. */
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
338 while (j < i)
c86fbe5 Junio C Hamano diff -c/--cc: do not include uninteresting deletion before leading conte...
gitster authored
339 sline[j++].flag |= mark | no_pre_delete;
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
340
341 again:
46dc941 combine-diff: cleanup.
Junio C Hamano authored
342 /* we know up to i is to be included. where does the
343 * next uninteresting one start?
344 */
345 j = find_next(sline, mark, i, cnt, 1);
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
346 if (cnt < j)
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
347 break; /* the rest are all interesting */
348
349 /* lookahead context lines */
46dc941 combine-diff: cleanup.
Junio C Hamano authored
350 k = find_next(sline, mark, j, cnt, 0);
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
351 j = adjust_hunk_tail(sline, all_mask, i, j);
352
353 if (k < j + context) {
354 /* k is interesting and [j,k) are not, but
355 * paint them interesting because the gap is small.
356 */
357 while (j < k)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
358 sline[j++].flag |= mark;
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
359 i = k;
360 goto again;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
361 }
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
362
363 /* j is the first uninteresting line and there is
46dc941 combine-diff: cleanup.
Junio C Hamano authored
364 * no overlap beyond it within context lines. Paint
365 * the trailing edge a bit.
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
366 */
367 i = k;
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
368 k = (j + context < cnt+1) ? j + context : cnt+1;
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
369 while (j < k)
370 sline[j++].flag |= mark;
371 }
372 return 1;
373 }
374
375 static int make_hunks(struct sline *sline, unsigned long cnt,
376 int num_parent, int dense)
377 {
378 unsigned long all_mask = (1UL<<num_parent) - 1;
379 unsigned long mark = (1UL<<num_parent);
380 unsigned long i;
381 int has_interesting = 0;
382
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
383 for (i = 0; i <= cnt; i++) {
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
384 if (interesting(&sline[i], all_mask))
385 sline[i].flag |= mark;
386 else
387 sline[i].flag &= ~mark;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
388 }
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
389 if (!dense)
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
390 return give_context(sline, cnt, num_parent);
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
391
263eee2 combine-diff: extend --cc logic to Octopus.
Junio C Hamano authored
392 /* Look at each hunk, and if we have changes from only one
393 * parent, or the changes are the same from all but one
394 * parent, mark that uninteresting.
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
395 */
396 i = 0;
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
397 while (i <= cnt) {
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
398 unsigned long j, hunk_begin, hunk_end;
bf1c32b combine-diff: update --cc "uninteresting hunks" logic.
Junio C Hamano authored
399 unsigned long same_diff;
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
400 while (i <= cnt && !(sline[i].flag & mark))
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
401 i++;
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
402 if (cnt < i)
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
403 break; /* No more interesting hunks */
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
404 hunk_begin = i;
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
405 for (j = i + 1; j <= cnt; j++) {
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
406 if (!(sline[j].flag & mark)) {
407 /* Look beyond the end to see if there
408 * is an interesting line after this
409 * hunk within context span.
410 */
411 unsigned long la; /* lookahead */
412 int contin = 0;
413 la = adjust_hunk_tail(sline, all_mask,
414 hunk_begin, j);
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
415 la = (la + context < cnt + 1) ?
416 (la + context) : cnt + 1;
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
417 while (j <= --la) {
418 if (sline[la].flag & mark) {
419 contin = 1;
420 break;
421 }
422 }
423 if (!contin)
424 break;
425 j = la;
426 }
427 }
428 hunk_end = j;
429
bf1c32b combine-diff: update --cc "uninteresting hunks" logic.
Junio C Hamano authored
430 /* [i..hunk_end) are interesting. Now is it really
fd4b1d2 combine-diff: add safety check to --cc.
Junio C Hamano authored
431 * interesting? We check if there are only two versions
432 * and the result matches one of them. That is, we look
433 * at:
434 * (+) line, which records lines added to which parents;
435 * this line appears in the result.
436 * (-) line, which records from what parents the line
437 * was removed; this line does not appear in the result.
438 * then check the set of parents the result has difference
439 * from, from all lines. If there are lines that has
440 * different set of parents that the result has differences
441 * from, that means we have more than two versions.
442 *
443 * Even when we have only two versions, if the result does
444 * not match any of the parents, the it should be considered
445 * interesting. In such a case, we would have all '+' line.
446 * After passing the above "two versions" test, that would
447 * appear as "the same set of parents" to be "all parents".
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
448 */
bf1c32b combine-diff: update --cc "uninteresting hunks" logic.
Junio C Hamano authored
449 same_diff = 0;
450 has_interesting = 0;
451 for (j = i; j < hunk_end && !has_interesting; j++) {
46dc941 combine-diff: cleanup.
Junio C Hamano authored
452 unsigned long this_diff = sline[j].flag & all_mask;
bf1c32b combine-diff: update --cc "uninteresting hunks" logic.
Junio C Hamano authored
453 struct lline *ll = sline[j].lost_head;
454 if (this_diff) {
455 /* This has some changes. Is it the
456 * same as others?
457 */
458 if (!same_diff)
459 same_diff = this_diff;
460 else if (same_diff != this_diff) {
461 has_interesting = 1;
462 break;
463 }
464 }
465 while (ll && !has_interesting) {
466 /* Lost this line from these parents;
467 * who are they? Are they the same?
468 */
469 this_diff = ll->parent_map;
470 if (!same_diff)
471 same_diff = this_diff;
472 else if (same_diff != this_diff) {
473 has_interesting = 1;
474 }
475 ll = ll->next;
476 }
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
477 }
bf1c32b combine-diff: update --cc "uninteresting hunks" logic.
Junio C Hamano authored
478
fd4b1d2 combine-diff: add safety check to --cc.
Junio C Hamano authored
479 if (!has_interesting && same_diff != all_mask) {
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
480 /* This hunk is not that interesting after all */
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
481 for (j = hunk_begin; j < hunk_end; j++)
d8f4790 diff-tree --cc: denser combined diff output for a merge commit.
Junio C Hamano authored
482 sline[j].flag &= ~mark;
483 }
484 i = hunk_end;
485 }
3ec1909 combine-diff: better hunk splitting.
Junio C Hamano authored
486
487 has_interesting = give_context(sline, cnt, num_parent);
8828cdc diff-tree --cc: squelch header generation on empty patch.
Junio C Hamano authored
488 return has_interesting;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
489 }
490
3b0f5e8 combine-diff: special case --unified=0
Junio C Hamano authored
491 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
492 {
493 l0 = sline[l0].p_lno[n];
494 l1 = sline[l1].p_lno[n];
3b0f5e8 combine-diff: special case --unified=0
Junio C Hamano authored
495 printf(" -%lu,%lu", l0, l1-l0-null_context);
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
496 }
497
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
498 static int hunk_comment_line(const char *bol)
499 {
7a8ac59 combine-diff: fix hunk_comment_line logic.
Junio C Hamano authored
500 int ch;
501
502 if (!bol)
503 return 0;
504 ch = *bol & 0xff;
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
505 return (isalpha(ch) || ch == '_' || ch == '$');
506 }
507
3928097 Junio C Hamano diff: Help "less" hide ^M from the output
gitster authored
508 static void show_line_to_eol(const char *line, int len, const char *reset)
509 {
510 int saw_cr_at_eol = 0;
511 if (len < 0)
512 len = strlen(line);
513 saw_cr_at_eol = (len && line[len-1] == '\r');
514
515 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
516 reset,
517 saw_cr_at_eol ? "\r" : "");
518 }
519
567a03d combine-diff: use color
Junio C Hamano authored
520 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
521 int use_color)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
522 {
523 unsigned long mark = (1UL<<num_parent);
c86fbe5 Junio C Hamano diff -c/--cc: do not include uninteresting deletion before leading conte...
gitster authored
524 unsigned long no_pre_delete = (2UL<<num_parent);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
525 int i;
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
526 unsigned long lno = 0;
567a03d combine-diff: use color
Junio C Hamano authored
527 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
528 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
529 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
530 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
531 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
532
4462731 combine-diff: do not punt on removed or added files.
Junio C Hamano authored
533 if (!cnt)
534 return; /* result deleted */
535
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
536 while (1) {
8bc7574 combine-diff: type fix.
Junio C Hamano authored
537 unsigned long hunk_end;
538 unsigned long rlines;
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
539 const char *hunk_comment = NULL;
3b0f5e8 combine-diff: special case --unified=0
Junio C Hamano authored
540 unsigned long null_context = 0;
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
541
542 while (lno <= cnt && !(sline[lno].flag & mark)) {
543 if (hunk_comment_line(sline[lno].bol))
544 hunk_comment = sline[lno].bol;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
545 lno++;
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
546 }
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
547 if (cnt < lno)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
548 break;
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
549 else {
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
550 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
551 if (!(sline[hunk_end].flag & mark))
552 break;
553 }
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
554 rlines = hunk_end - lno;
555 if (cnt < hunk_end)
556 rlines--; /* pointing at the last delete hunk */
3b0f5e8 combine-diff: special case --unified=0
Junio C Hamano authored
557
558 if (!context) {
559 /*
560 * Even when running with --unified=0, all
561 * lines in the hunk needs to be processed in
562 * the loop below in order to show the
563 * deletion recorded in lost_head. However,
564 * we do not want to show the resulting line
565 * with all blank context markers in such a
566 * case. Compensate.
567 */
568 unsigned long j;
569 for (j = lno; j < hunk_end; j++)
570 if (!(sline[j].flag & (mark-1)))
571 null_context++;
572 rlines -= null_context;
573 }
574
567a03d combine-diff: use color
Junio C Hamano authored
575 fputs(c_frag, stdout);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
576 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
577 for (i = 0; i < num_parent; i++)
3b0f5e8 combine-diff: special case --unified=0
Junio C Hamano authored
578 show_parent_lno(sline, lno, hunk_end, i, null_context);
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
579 printf(" +%lu,%lu ", lno+1, rlines);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
580 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
581
582 if (hunk_comment) {
583 int comment_end = 0;
584 for (i = 0; i < 40; i++) {
585 int ch = hunk_comment[i] & 0xff;
586 if (!ch || ch == '\n')
587 break;
588 if (!isspace(ch))
589 comment_end = i;
590 }
591 if (comment_end)
592 putchar(' ');
593 for (i = 0; i < comment_end; i++)
594 putchar(hunk_comment[i]);
595 }
596
567a03d combine-diff: use color
Junio C Hamano authored
597 printf("%s\n", c_reset);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
598 while (lno < hunk_end) {
599 struct lline *ll;
600 int j;
46dc941 combine-diff: cleanup.
Junio C Hamano authored
601 unsigned long p_mask;
fd13b21 Benjamin Kramer Move local variables to narrower scopes
d0k authored
602 struct sline *sl = &sline[lno++];
c86fbe5 Junio C Hamano diff -c/--cc: do not include uninteresting deletion before leading conte...
gitster authored
603 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
604 while (ll) {
567a03d combine-diff: use color
Junio C Hamano authored
605 fputs(c_old, stdout);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
606 for (j = 0; j < num_parent; j++) {
607 if (ll->parent_map & (1UL<<j))
608 putchar('-');
609 else
610 putchar(' ');
611 }
3928097 Junio C Hamano diff: Help "less" hide ^M from the output
gitster authored
612 show_line_to_eol(ll->line, -1, c_reset);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
613 ll = ll->next;
614 }
7406595 combine-diff: fix hunks at the end (take #2).
Junio C Hamano authored
615 if (cnt < lno)
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
616 break;
46dc941 combine-diff: cleanup.
Junio C Hamano authored
617 p_mask = 1;
3b0f5e8 combine-diff: special case --unified=0
Junio C Hamano authored
618 if (!(sl->flag & (mark-1))) {
619 /*
620 * This sline was here to hang the
621 * lost lines in front of it.
622 */
623 if (!context)
624 continue;
567a03d combine-diff: use color
Junio C Hamano authored
625 fputs(c_plain, stdout);
3b0f5e8 combine-diff: special case --unified=0
Junio C Hamano authored
626 }
567a03d combine-diff: use color
Junio C Hamano authored
627 else
628 fputs(c_new, stdout);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
629 for (j = 0; j < num_parent; j++) {
46dc941 combine-diff: cleanup.
Junio C Hamano authored
630 if (p_mask & sl->flag)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
631 putchar('+');
46dc941 combine-diff: cleanup.
Junio C Hamano authored
632 else
633 putchar(' ');
634 p_mask <<= 1;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
635 }
3928097 Junio C Hamano diff: Help "less" hide ^M from the output
gitster authored
636 show_line_to_eol(sl->bol, sl->len, c_reset);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
637 }
638 }
639 }
640
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
641 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
642 int i, int j)
643 {
644 /* We have already examined parent j and we know parent i
645 * and parent j are the same, so reuse the combined result
646 * of parent j for parent i.
647 */
648 unsigned long lno, imask, jmask;
649 imask = (1UL<<i);
650 jmask = (1UL<<j);
651
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
652 for (lno = 0; lno <= cnt; lno++) {
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
653 struct lline *ll = sline->lost_head;
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
654 sline->p_lno[i] = sline->p_lno[j];
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
655 while (ll) {
656 if (ll->parent_map & jmask)
657 ll->parent_map |= imask;
658 ll = ll->next;
659 }
46dc941 combine-diff: cleanup.
Junio C Hamano authored
660 if (sline->flag & jmask)
661 sline->flag |= imask;
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
662 sline++;
663 }
4462731 combine-diff: do not punt on removed or added files.
Junio C Hamano authored
664 /* the overall size of the file (sline[cnt]) */
665 sline->p_lno[i] = sline->p_lno[j];
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
666 }
667
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
668 static void dump_quoted_path(const char *head,
669 const char *prefix,
670 const char *path,
567a03d combine-diff: use color
Junio C Hamano authored
671 const char *c_meta, const char *c_reset)
eab144a Log message printout cleanups (#2)
Linus Torvalds authored
672 {
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
673 static struct strbuf buf = STRBUF_INIT;
674
675 strbuf_reset(&buf);
676 strbuf_addstr(&buf, c_meta);
677 strbuf_addstr(&buf, head);
d562509 Junio C Hamano Fix rewrite_diff() name quoting.
gitster authored
678 quote_two_c_style(&buf, prefix, path, 0);
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
679 strbuf_addstr(&buf, c_reset);
680 puts(buf.buf);
eab144a Log message printout cleanups (#2)
Linus Torvalds authored
681 }
682
89b0c4b Fix type of combine-diff.c::show_patch_diff()
Junio C Hamano authored
683 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
684 int dense, struct rev_info *rev)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
685 {
9153983 Log message printout cleanups
Linus Torvalds authored
686 struct diff_options *opt = &rev->diffopt;
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
687 unsigned long result_size, cnt, lno;
310f8b5 cleanups: Remove unused vars from combine-diff.c
Serge E. Hallyn authored
688 char *result, *cp;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
689 struct sline *sline; /* survived lines */
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
690 int mode_differs = 0;
89b0c4b Fix type of combine-diff.c::show_patch_diff()
Junio C Hamano authored
691 int i, show_hunks;
0bef57e make inline is_null_sha1 global
David Rientjes authored
692 int working_tree_file = is_null_sha1(elem->sha1);
8f67f8a Pierre Habouzit Make the diff_options bitfields be an unsigned with explicit masks.
MadCoder authored
693 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
a5a818e Junio C Hamano diff: vary default prefix depending on what are compared
gitster authored
694 const char *a_prefix, *b_prefix;
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
695 mmfile_t result_file;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
696
ee1e541 git diff: support "-U" and "--unified" options properly
Linus Torvalds authored
697 context = opt->context;
a5a818e Junio C Hamano diff: vary default prefix depending on what are compared
gitster authored
698 a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
699 b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
700
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
701 /* Read the result of merge first */
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
702 if (!working_tree_file)
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
703 result = grab_blob(elem->sha1, elem->mode, &result_size);
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
704 else {
9843a1f combine-diff: do not send NULL to printf
Junio C Hamano authored
705 /* Used by diff-tree to read from the working tree */
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
706 struct stat st;
4fc970c diff --cc: fix display of symlink conflicts during a merge.
Junio C Hamano authored
707 int fd = -1;
708
709 if (lstat(elem->path, &st) < 0)
710 goto deleted_file;
711
712 if (S_ISLNK(st.st_mode)) {
912342d Junio C Hamano combine-diff.c: use strbuf_readlink()
gitster authored
713 struct strbuf buf = STRBUF_INIT;
714
715 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
4fc970c diff --cc: fix display of symlink conflicts during a merge.
Junio C Hamano authored
716 error("readlink(%s): %s", elem->path,
717 strerror(errno));
718 return;
719 }
912342d Junio C Hamano combine-diff.c: use strbuf_readlink()
gitster authored
720 result_size = buf.len;
721 result = strbuf_detach(&buf, NULL);
4fc970c diff --cc: fix display of symlink conflicts during a merge.
Junio C Hamano authored
722 elem->mode = canon_mode(st.st_mode);
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
723 } else if (S_ISDIR(st.st_mode)) {
724 unsigned char sha1[20];
725 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
726 result = grab_blob(elem->sha1, elem->mode, &result_size);
727 else
728 result = grab_blob(sha1, elem->mode, &result_size);
91fcbcb show_patch_diff(): remove a call to fstat()
Kjetil Barvik authored
729 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
dc49cd7 Shawn O. Pearce Cast 64 bit off_t to 32 bit size_t
spearce authored
730 size_t len = xsize_t(st.st_size);
c697ad1 Heikki Orsila Cleanup xread() loops to use read_in_full()
heikkiorsila authored
731 ssize_t done;
a249a9b Tell multi-parent diff about core.symlinks.
Johannes Sixt authored
732 int is_file, i;
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
733
1b0c717 tree/diff header cleanup.
Junio C Hamano authored
734 elem->mode = canon_mode(st.st_mode);
a249a9b Tell multi-parent diff about core.symlinks.
Johannes Sixt authored
735 /* if symlinks don't work, assume symlink if all parents
736 * are symlinks
737 */
738 is_file = has_symlinks;
739 for (i = 0; !is_file && i < num_parent; i++)
740 is_file = !S_ISLNK(elem->parent[i].mode);
741 if (!is_file)
742 elem->mode = canon_mode(S_IFLNK);
743
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
744 result_size = len;
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
745 result = xmalloc(len + 1);
c697ad1 Heikki Orsila Cleanup xread() loops to use read_in_full()
heikkiorsila authored
746
747 done = read_in_full(fd, result, len);
748 if (done < 0)
0721c31 trast Use die_errno() instead of die() when checking syscalls
trast authored
749 die_errno("read error '%s'", elem->path);
c697ad1 Heikki Orsila Cleanup xread() loops to use read_in_full()
heikkiorsila authored
750 else if (done < len)
751 die("early EOF '%s'", elem->path);
752
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
753 result[len] = 0;
5e568f9 Alexander Gavrilov Respect core.autocrlf in combined diff
angavrilov authored
754
755 /* If not a fake symlink, apply filters, e.g. autocrlf */
756 if (is_file) {
f285a2d Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer
Brandon Casey authored
757 struct strbuf buf = STRBUF_INIT;
5e568f9 Alexander Gavrilov Respect core.autocrlf in combined diff
angavrilov authored
758
759 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
760 free(result);
761 result = strbuf_detach(&buf, &len);
762 result_size = len;
763 }
764 }
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
765 }
766 else {
4fc970c diff --cc: fix display of symlink conflicts during a merge.
Junio C Hamano authored
767 deleted_file:
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
768 result_size = 0;
713a11f combine-diff: diff-files fix.
Junio C Hamano authored
769 elem->mode = 0;
28f7581 Substitute xmalloc()+memset(0) with xcalloc().
Peter Eriksen authored
770 result = xcalloc(1, 1);
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
771 }
4fc970c diff --cc: fix display of symlink conflicts during a merge.
Junio C Hamano authored
772
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
773 if (0 <= fd)
774 close(fd);
775 }
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
776
2386c29 combine-diff.c: type sanity
Junio C Hamano authored
777 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
778 if (*cp == '\n')
779 cnt++;
780 }
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
781 if (result_size && result[result_size-1] != '\n')
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
782 cnt++; /* incomplete line */
783
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
784 sline = xcalloc(cnt+2, sizeof(*sline));
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
785 sline[0].bol = result;
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
786 for (lno = 0; lno <= cnt + 1; lno++) {
4462731 combine-diff: do not punt on removed or added files.
Junio C Hamano authored
787 sline[lno].lost_tail = &sline[lno].lost_head;
788 sline[lno].flag = 0;
789 }
2386c29 combine-diff.c: type sanity
Junio C Hamano authored
790 for (lno = 0, cp = result; cp < result + result_size; cp++) {
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
791 if (*cp == '\n') {
792 sline[lno].len = cp - sline[lno].bol;
793 lno++;
794 if (lno < cnt)
795 sline[lno].bol = cp + 1;
796 }
797 }
f23fc77 combine-diff: use built-in xdiff.
Junio C Hamano authored
798 if (result_size && result[result_size-1] != '\n')
799 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
800
801 result_file.ptr = result;
802 result_file.size = result_size;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
803
8a470eb combine-diff: do not lose hunks with only deletion at end.
Junio C Hamano authored
804 /* Even p_lno[cnt+1] is valid -- that is for the end line number
805 * for deletion hunk at the end.
806 */
807 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
808 for (lno = 0; lno <= cnt; lno++)
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
809 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
810
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
811 for (i = 0; i < num_parent; i++) {
812 int j;
813 for (j = 0; j < i; j++) {
a89fccd Do not use memcmp(sha1_1, sha1_2, 20) with hardcoded length.
David Rientjes authored
814 if (!hashcmp(elem->parent[i].sha1,
815 elem->parent[j].sha1)) {
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
816 reuse_combine_diff(sline, cnt, i, j);
817 break;
818 }
819 }
820 if (i <= j)
7dae8b2 Junio C Hamano diff -c -p: do not die on submodules
gitster authored
821 combine_diff(elem->parent[i].sha1,
822 elem->parent[i].mode,
823 &result_file, sline,
f16706c combine-diff: show parent line numbers as well.
Junio C Hamano authored
824 cnt, i, num_parent);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
825 if (elem->parent[i].mode != elem->mode)
826 mode_differs = 1;
3c39e9b combine-diff: reuse diff from the same blob.
Junio C Hamano authored
827 }
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
828
8828cdc diff-tree --cc: squelch header generation on empty patch.
Junio C Hamano authored
829 show_hunks = make_hunks(sline, cnt, num_parent, dense);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
830
713a11f combine-diff: diff-files fix.
Junio C Hamano authored
831 if (show_hunks || mode_differs || working_tree_file) {
9843a1f combine-diff: do not send NULL to printf
Junio C Hamano authored
832 const char *abb;
8f67f8a Pierre Habouzit Make the diff_options bitfields be an unsigned with explicit masks.
MadCoder authored
833 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
567a03d combine-diff: use color
Junio C Hamano authored
834 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
835 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
836 int added = 0;
837 int deleted = 0;
9843a1f combine-diff: do not send NULL to printf
Junio C Hamano authored
838
4415278 combine-diff: honour --no-commit-id
Junio C Hamano authored
839 if (rev->loginfo && !rev->no_commit_id)
0286565 Adam Simpkins Remove dead code: show_log() sep argument and diff_options.msg_sep
simpkins authored
840 show_log(rev);
567a03d combine-diff: use color
Junio C Hamano authored
841 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
842 "", elem->path, c_meta, c_reset);
567a03d combine-diff: use color
Junio C Hamano authored
843 printf("%sindex ", c_meta);
823bcd6 combine-diff: fix placement of deletion.
Junio C Hamano authored
844 for (i = 0; i < num_parent; i++) {
297a1aa find_unique_abbrev() simplification.
Junio C Hamano authored
845 abb = find_unique_abbrev(elem->parent[i].sha1,
e70c6b3 combine-diff: Honour --full-index.
Mark Wooding authored
846 abbrev);
9843a1f combine-diff: do not send NULL to printf
Junio C Hamano authored
847 printf("%s%s", i ? "," : "", abb);
823bcd6 combine-diff: fix placement of deletion.
Junio C Hamano authored
848 }
e70c6b3 combine-diff: Honour --full-index.
Mark Wooding authored
849 abb = find_unique_abbrev(elem->sha1, abbrev);
567a03d combine-diff: use color
Junio C Hamano authored
850 printf("..%s%s\n", abb, c_reset);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
851
852 if (mode_differs) {
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
853 deleted = !elem->mode;
854
855 /* We say it was added if nobody had it */
856 added = !deleted;
d416df8 combine-diff: Record diff status a bit more faithfully
Junio C Hamano authored
857 for (i = 0; added && i < num_parent; i++)
858 if (elem->parent[i].status !=
859 DIFF_STATUS_ADDED)
860 added = 0;
861 if (added)
567a03d combine-diff: use color
Junio C Hamano authored
862 printf("%snew file mode %06o",
863 c_meta, elem->mode);
d416df8 combine-diff: Record diff status a bit more faithfully
Junio C Hamano authored
864 else {
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
865 if (deleted)
567a03d combine-diff: use color
Junio C Hamano authored
866 printf("%sdeleted file ", c_meta);
d416df8 combine-diff: Record diff status a bit more faithfully
Junio C Hamano authored
867 printf("mode ");
868 for (i = 0; i < num_parent; i++) {
869 printf("%s%06o", i ? "," : "",
870 elem->parent[i].mode);
871 }
872 if (elem->mode)
873 printf("..%06o", elem->mode);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
874 }
567a03d combine-diff: use color
Junio C Hamano authored
875 printf("%s\n", c_reset);
2454c96 combine-diff: show mode changes as well.
Junio C Hamano authored
876 }
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
877 if (added)
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
878 dump_quoted_path("--- ", "", "/dev/null",
879 c_meta, c_reset);
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
880 else
a5a818e Junio C Hamano diff: vary default prefix depending on what are compared
gitster authored
881 dump_quoted_path("--- ", a_prefix, elem->path,
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
882 c_meta, c_reset);
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
883 if (deleted)
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
884 dump_quoted_path("+++ ", "", "/dev/null",
885 c_meta, c_reset);
d5f6a01 combine-diff: a few more finishing touches.
Junio C Hamano authored
886 else
a5a818e Junio C Hamano diff: vary default prefix depending on what are compared
gitster authored
887 dump_quoted_path("+++ ", b_prefix, elem->path,
462a15b Junio C Hamano combine-diff: Fix path quoting
gitster authored
888 c_meta, c_reset);
889 dump_sline(sline, cnt, num_parent,
890 DIFF_OPT_TST(opt, COLOR_DIFF));
8828cdc diff-tree --cc: squelch header generation on empty patch.
Junio C Hamano authored
891 }
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
892 free(result);
893
2386c29 combine-diff.c: type sanity
Junio C Hamano authored
894 for (lno = 0; lno < cnt; lno++) {
895 if (sline[lno].lost_head) {
896 struct lline *ll = sline[lno].lost_head;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
897 while (ll) {
898 struct lline *tmp = ll;
899 ll = ll->next;
900 free(tmp);
901 }
902 }
903 }
46dc941 combine-diff: cleanup.
Junio C Hamano authored
904 free(sline[0].p_lno);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
905 free(sline);
906 }
907
ee63802 diff-tree -c raw output
Linus Torvalds authored
908 #define COLONS "::::::::::::::::::::::::::::::::"
909
9153983 Log message printout cleanups
Linus Torvalds authored
910 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
ee63802 diff-tree -c raw output
Linus Torvalds authored
911 {
9153983 Log message printout cleanups
Linus Torvalds authored
912 struct diff_options *opt = &rev->diffopt;
310f8b5 cleanups: Remove unused vars from combine-diff.c
Serge E. Hallyn authored
913 int i, offset;
ee63802 diff-tree -c raw output
Linus Torvalds authored
914 const char *prefix;
915 int line_termination, inter_name_termination;
916
917 line_termination = opt->line_termination;
918 inter_name_termination = '\t';
919 if (!line_termination)
920 inter_name_termination = 0;
921
4415278 combine-diff: honour --no-commit-id
Junio C Hamano authored
922 if (rev->loginfo && !rev->no_commit_id)
0286565 Adam Simpkins Remove dead code: show_log() sep argument and diff_options.msg_sep
simpkins authored
923 show_log(rev);
ee63802 diff-tree -c raw output
Linus Torvalds authored
924
c674434 tihirvon Merge with_raw, with_stat and summary variables to output_format
tihirvon authored
925 if (opt->output_format & DIFF_FORMAT_RAW) {
0a79807 combine-diff: move formatting logic to show_combined_diff()
Junio C Hamano authored
926 offset = strlen(COLONS) - num_parent;
927 if (offset < 0)
928 offset = 0;
929 prefix = COLONS + offset;
930
931 /* Show the modes */
932 for (i = 0; i < num_parent; i++) {
933 printf("%s%06o", prefix, p->parent[i].mode);
934 prefix = " ";
935 }
936 printf("%s%06o", prefix, p->mode);
937
938 /* Show sha1's */
939 for (i = 0; i < num_parent; i++)
940 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
941 opt->abbrev));
942 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
943 }
944
c674434 tihirvon Merge with_raw, with_stat and summary variables to output_format
tihirvon authored
945 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
d416df8 combine-diff: Record diff status a bit more faithfully
Junio C Hamano authored
946 for (i = 0; i < num_parent; i++)
947 putchar(p->parent[i].status);
948 putchar(inter_name_termination);
949 }
0a79807 combine-diff: move formatting logic to show_combined_diff()
Junio C Hamano authored
950
663af34 Pierre Habouzit Full rework of quote_c_style and write_name_quoted.
MadCoder authored
951 write_name_quoted(p->path, stdout, line_termination);
0a79807 combine-diff: move formatting logic to show_combined_diff()
Junio C Hamano authored
952 }
953
9153983 Log message printout cleanups
Linus Torvalds authored
954 void show_combined_diff(struct combine_diff_path *p,
0a79807 combine-diff: move formatting logic to show_combined_diff()
Junio C Hamano authored
955 int num_parent,
956 int dense,
9153983 Log message printout cleanups
Linus Torvalds authored
957 struct rev_info *rev)
0a79807 combine-diff: move formatting logic to show_combined_diff()
Junio C Hamano authored
958 {
9153983 Log message printout cleanups
Linus Torvalds authored
959 struct diff_options *opt = &rev->diffopt;
0a79807 combine-diff: move formatting logic to show_combined_diff()
Junio C Hamano authored
960 if (!p->len)
9153983 Log message printout cleanups
Linus Torvalds authored
961 return;
c674434 tihirvon Merge with_raw, with_stat and summary variables to output_format
tihirvon authored
962 if (opt->output_format & (DIFF_FORMAT_RAW |
963 DIFF_FORMAT_NAME |
89b0c4b Fix type of combine-diff.c::show_patch_diff()
Junio C Hamano authored
964 DIFF_FORMAT_NAME_STATUS))
9153983 Log message printout cleanups
Linus Torvalds authored
965 show_raw_diff(p, num_parent, rev);
89b0c4b Fix type of combine-diff.c::show_patch_diff()
Junio C Hamano authored
966 else if (opt->output_format & DIFF_FORMAT_PATCH)
9153983 Log message printout cleanups
Linus Torvalds authored
967 show_patch_diff(p, num_parent, dense, rev);
ee63802 diff-tree -c raw output
Linus Torvalds authored
968 }
969
0fe7c1d built-in diff: assorted updates.
Junio C Hamano authored
970 void diff_tree_combined(const unsigned char *sha1,
971 const unsigned char parent[][20],
972 int num_parent,
973 int dense,
974 struct rev_info *rev)
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
975 {
9153983 Log message printout cleanups
Linus Torvalds authored
976 struct diff_options *opt = &rev->diffopt;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
977 struct diff_options diffopts;
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
978 struct combine_diff_path *p, *paths = NULL;
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
979 int i, num_paths, needsep, show_log_first;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
980
5b23683 combined-diff: use diffcore before intersecting paths.
Junio C Hamano authored
981 diffopts = *opt;
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
982 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
8f67f8a Pierre Habouzit Make the diff_options bitfields be an unsigned with explicit masks.
MadCoder authored
983 DIFF_OPT_SET(&diffopts, RECURSIVE);
984 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
985
4415278 combine-diff: honour --no-commit-id
Junio C Hamano authored
986 show_log_first = !!rev->loginfo && !rev->no_commit_id;
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
987 needsep = 0;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
988 /* find set of paths that everybody touches */
0fe7c1d built-in diff: assorted updates.
Junio C Hamano authored
989 for (i = 0; i < num_parent; i++) {
965f803 combine-diff: show diffstat with the first parent.
Junio C Hamano authored
990 /* show stat against the first parent even
991 * when doing combined diff.
992 */
74e2abe diff --numstat
Junio C Hamano authored
993 int stat_opt = (opt->output_format &
994 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
995 if (i == 0 && stat_opt)
996 diffopts.output_format = stat_opt;
965f803 combine-diff: show diffstat with the first parent.
Junio C Hamano authored
997 else
998 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
0fe7c1d built-in diff: assorted updates.
Junio C Hamano authored
999 diff_tree_sha1(parent[i], sha1, "", &diffopts);
5b23683 combined-diff: use diffcore before intersecting paths.
Junio C Hamano authored
1000 diffcore_std(&diffopts);
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
1001 paths = intersect_paths(paths, i, num_parent);
eab144a Log message printout cleanups (#2)
Linus Torvalds authored
1002
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
1003 if (show_log_first && i == 0) {
0286565 Adam Simpkins Remove dead code: show_log() sep argument and diff_options.msg_sep
simpkins authored
1004 show_log(rev);
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
1005 if (rev->verbose_header && opt->output_format)
1006 putchar(opt->line_termination);
1007 }
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
1008 diff_flush(&diffopts);
1009 }
1010
1011 /* find out surviving paths */
1012 for (num_paths = 0, p = paths; p; p = p->next) {
1013 if (p->len)
1014 num_paths++;
1015 }
e3c3a55 combine-diff: remove misguided --show-empty hack.
Junio C Hamano authored
1016 if (num_paths) {
c674434 tihirvon Merge with_raw, with_stat and summary variables to output_format
tihirvon authored
1017 if (opt->output_format & (DIFF_FORMAT_RAW |
1018 DIFF_FORMAT_NAME |
1019 DIFF_FORMAT_NAME_STATUS)) {
86ff1d2 diff-* --patch-with-raw
Junio C Hamano authored
1020 for (p = paths; p; p = p->next) {
c674434 tihirvon Merge with_raw, with_stat and summary variables to output_format
tihirvon authored
1021 if (p->len)
1022 show_raw_diff(p, num_parent, rev);
86ff1d2 diff-* --patch-with-raw
Junio C Hamano authored
1023 }
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
1024 needsep = 1;
86ff1d2 diff-* --patch-with-raw
Junio C Hamano authored
1025 }
74e2abe diff --numstat
Junio C Hamano authored
1026 else if (opt->output_format &
1027 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
1028 needsep = 1;
c674434 tihirvon Merge with_raw, with_stat and summary variables to output_format
tihirvon authored
1029 if (opt->output_format & DIFF_FORMAT_PATCH) {
3969cf7 Fix some more diff options changes.
Junio C Hamano authored
1030 if (needsep)
1031 putchar(opt->line_termination);
c674434 tihirvon Merge with_raw, with_stat and summary variables to output_format
tihirvon authored
1032 for (p = paths; p; p = p->next) {
1033 if (p->len)
1034 show_patch_diff(p, num_parent, dense,
1035 rev);
1036 }
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
1037 }
1038 }
1039
1040 /* Clean things up */
1041 while (paths) {
ea726d0 diff-files: -c and --cc options.
Junio C Hamano authored
1042 struct combine_diff_path *tmp = paths;
af3feef diff-tree -c: show a merge commit a bit more sensibly.
Junio C Hamano authored
1043 paths = paths->next;
1044 free(tmp);
1045 }
1046 }
0fe7c1d built-in diff: assorted updates.
Junio C Hamano authored
1047
1048 void diff_tree_combined_merge(const unsigned char *sha1,
1049 int dense, struct rev_info *rev)
1050 {
1051 int num_parent;
1052 const unsigned char (*parent)[20];
1053 struct commit *commit = lookup_commit(sha1);
1054 struct commit_list *parents;
1055
1056 /* count parents */
1057 for (parents = commit->parents, num_parent = 0;
1058 parents;
1059 parents = parents->next, num_parent++)
1060 ; /* nothing */
1061
1062 parent = xmalloc(num_parent * sizeof(*parent));
1063 for (parents = commit->parents, num_parent = 0;
1064 parents;
1065 parents = parents->next, num_parent++)
4b25d09 Felipe Contreras Fix a bunch of pointer declarations (codestyle)
felipec authored
1066 hashcpy((unsigned char *)(parent + num_parent),
e702496 Shawn O. Pearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
1067 parents->item->object.sha1);
0fe7c1d