Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4 | #include "cache.h" |
| 5 | #include "quote.h" |
| 6 | #include "diff.h" |
| 7 | #include "diffcore.h" |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 8 | #include "delta.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 9 | #include "xdiff-interface.h" |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 10 | #include "color.h" |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 11 | #include "attr.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 12 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 13 | #ifdef NO_FAST_WORKING_DIRECTORY |
| 14 | #define FAST_WORKING_DIRECTORY 0 |
| 15 | #else |
| 16 | #define FAST_WORKING_DIRECTORY 1 |
| 17 | #endif |
| 18 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 19 | static int diff_detect_rename_default; |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 20 | static int diff_rename_limit_default = -1; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 21 | static int diff_use_color_default; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 22 | |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 23 | static char diff_colors[][COLOR_MAXLEN] = { |
Timo Hirvonen | f37399e | 2006-07-13 19:06:12 +0300 | [diff] [blame] | 24 | "\033[m", /* reset */ |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 25 | "", /* PLAIN (normal) */ |
| 26 | "\033[1m", /* METAINFO (bold) */ |
| 27 | "\033[36m", /* FRAGINFO (cyan) */ |
| 28 | "\033[31m", /* OLD (red) */ |
| 29 | "\033[32m", /* NEW (green) */ |
| 30 | "\033[33m", /* COMMIT (yellow) */ |
| 31 | "\033[41m", /* WHITESPACE (red background) */ |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 34 | static int parse_diff_color_slot(const char *var, int ofs) |
| 35 | { |
| 36 | if (!strcasecmp(var+ofs, "plain")) |
| 37 | return DIFF_PLAIN; |
| 38 | if (!strcasecmp(var+ofs, "meta")) |
| 39 | return DIFF_METAINFO; |
| 40 | if (!strcasecmp(var+ofs, "frag")) |
| 41 | return DIFF_FRAGINFO; |
| 42 | if (!strcasecmp(var+ofs, "old")) |
| 43 | return DIFF_FILE_OLD; |
| 44 | if (!strcasecmp(var+ofs, "new")) |
| 45 | return DIFF_FILE_NEW; |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 46 | if (!strcasecmp(var+ofs, "commit")) |
| 47 | return DIFF_COMMIT; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 48 | if (!strcasecmp(var+ofs, "whitespace")) |
| 49 | return DIFF_WHITESPACE; |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 50 | die("bad config variable '%s'", var); |
| 51 | } |
| 52 | |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 53 | static struct ll_diff_driver { |
| 54 | const char *name; |
| 55 | struct ll_diff_driver *next; |
| 56 | char *cmd; |
| 57 | } *user_diff, **user_diff_tail; |
| 58 | |
| 59 | /* |
| 60 | * Currently there is only "diff.<drivername>.command" variable; |
| 61 | * because there are "diff.color.<slot>" variables, we are parsing |
| 62 | * this in a bit convoluted way to allow low level diff driver |
| 63 | * called "color". |
| 64 | */ |
| 65 | static int parse_lldiff_command(const char *var, const char *ep, const char *value) |
| 66 | { |
| 67 | const char *name; |
| 68 | int namelen; |
| 69 | struct ll_diff_driver *drv; |
| 70 | |
| 71 | name = var + 5; |
| 72 | namelen = ep - name; |
| 73 | for (drv = user_diff; drv; drv = drv->next) |
| 74 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) |
| 75 | break; |
| 76 | if (!drv) { |
| 77 | char *namebuf; |
| 78 | drv = xcalloc(1, sizeof(struct ll_diff_driver)); |
| 79 | namebuf = xmalloc(namelen + 1); |
| 80 | memcpy(namebuf, name, namelen); |
| 81 | namebuf[namelen] = 0; |
| 82 | drv->name = namebuf; |
| 83 | drv->next = NULL; |
| 84 | if (!user_diff_tail) |
| 85 | user_diff_tail = &user_diff; |
| 86 | *user_diff_tail = drv; |
| 87 | user_diff_tail = &(drv->next); |
| 88 | } |
| 89 | |
| 90 | if (!value) |
| 91 | return error("%s: lacks value", var); |
| 92 | drv->cmd = strdup(value); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Junio C Hamano | 83ad63c | 2006-07-08 01:05:16 -0700 | [diff] [blame] | 96 | /* |
| 97 | * These are to give UI layer defaults. |
| 98 | * The core-level commands such as git-diff-files should |
| 99 | * never be affected by the setting of diff.renames |
| 100 | * the user happens to have in the configuration file. |
| 101 | */ |
| 102 | int git_diff_ui_config(const char *var, const char *value) |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 103 | { |
| 104 | if (!strcmp(var, "diff.renamelimit")) { |
| 105 | diff_rename_limit_default = git_config_int(var, value); |
| 106 | return 0; |
| 107 | } |
Andy Parkins | a159ca0 | 2006-12-13 09:13:28 +0000 | [diff] [blame] | 108 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 109 | diff_use_color_default = git_config_colorbool(var, value); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 112 | if (!strcmp(var, "diff.renames")) { |
| 113 | if (!value) |
| 114 | diff_detect_rename_default = DIFF_DETECT_RENAME; |
| 115 | else if (!strcasecmp(value, "copies") || |
| 116 | !strcasecmp(value, "copy")) |
| 117 | diff_detect_rename_default = DIFF_DETECT_COPY; |
| 118 | else if (git_config_bool(var,value)) |
| 119 | diff_detect_rename_default = DIFF_DETECT_RENAME; |
| 120 | return 0; |
| 121 | } |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 122 | if (!prefixcmp(var, "diff.")) { |
| 123 | const char *ep = strrchr(var, '.'); |
| 124 | |
| 125 | if (ep != var + 4 && !strcmp(ep, ".command")) |
| 126 | return parse_lldiff_command(var, ep, value); |
| 127 | } |
Junio C Hamano | 1968d77 | 2007-02-20 01:55:07 -0800 | [diff] [blame] | 128 | if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) { |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 129 | int slot = parse_diff_color_slot(var, 11); |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 130 | color_parse(value, var, diff_colors[slot]); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 131 | return 0; |
| 132 | } |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 133 | |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 134 | return git_default_config(var, value); |
| 135 | } |
| 136 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 137 | static char *quote_one(const char *str) |
| 138 | { |
| 139 | int needlen; |
| 140 | char *xp; |
| 141 | |
| 142 | if (!str) |
| 143 | return NULL; |
| 144 | needlen = quote_c_style(str, NULL, NULL, 0); |
| 145 | if (!needlen) |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 146 | return xstrdup(str); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 147 | xp = xmalloc(needlen + 1); |
| 148 | quote_c_style(str, xp, NULL, 0); |
| 149 | return xp; |
| 150 | } |
| 151 | |
| 152 | static char *quote_two(const char *one, const char *two) |
| 153 | { |
| 154 | int need_one = quote_c_style(one, NULL, NULL, 1); |
| 155 | int need_two = quote_c_style(two, NULL, NULL, 1); |
| 156 | char *xp; |
| 157 | |
| 158 | if (need_one + need_two) { |
| 159 | if (!need_one) need_one = strlen(one); |
| 160 | if (!need_two) need_one = strlen(two); |
| 161 | |
| 162 | xp = xmalloc(need_one + need_two + 3); |
| 163 | xp[0] = '"'; |
| 164 | quote_c_style(one, xp + 1, NULL, 1); |
| 165 | quote_c_style(two, xp + need_one + 1, NULL, 1); |
| 166 | strcpy(xp + need_one + need_two + 1, "\""); |
| 167 | return xp; |
| 168 | } |
| 169 | need_one = strlen(one); |
| 170 | need_two = strlen(two); |
| 171 | xp = xmalloc(need_one + need_two + 1); |
| 172 | strcpy(xp, one); |
| 173 | strcpy(xp + need_one, two); |
| 174 | return xp; |
| 175 | } |
| 176 | |
| 177 | static const char *external_diff(void) |
| 178 | { |
| 179 | static const char *external_diff_cmd = NULL; |
| 180 | static int done_preparing = 0; |
| 181 | |
| 182 | if (done_preparing) |
| 183 | return external_diff_cmd; |
| 184 | external_diff_cmd = getenv("GIT_EXTERNAL_DIFF"); |
| 185 | done_preparing = 1; |
| 186 | return external_diff_cmd; |
| 187 | } |
| 188 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 189 | static struct diff_tempfile { |
| 190 | const char *name; /* filename external diff should read from */ |
| 191 | char hex[41]; |
| 192 | char mode[10]; |
Fernando J. Pereda | 1472966 | 2007-05-20 15:35:46 +0200 | [diff] [blame] | 193 | char tmp_path[PATH_MAX]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 194 | } diff_temp[2]; |
| 195 | |
| 196 | static int count_lines(const char *data, int size) |
| 197 | { |
| 198 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
| 199 | count = 0; |
| 200 | while (0 < size--) { |
| 201 | ch = *data++; |
| 202 | if (ch == '\n') { |
| 203 | count++; |
| 204 | nl_just_seen = 1; |
| 205 | completely_empty = 0; |
| 206 | } |
| 207 | else { |
| 208 | nl_just_seen = 0; |
| 209 | completely_empty = 0; |
| 210 | } |
| 211 | } |
| 212 | if (completely_empty) |
| 213 | return 0; |
| 214 | if (!nl_just_seen) |
| 215 | count++; /* no trailing newline */ |
| 216 | return count; |
| 217 | } |
| 218 | |
| 219 | static void print_line_count(int count) |
| 220 | { |
| 221 | switch (count) { |
| 222 | case 0: |
| 223 | printf("0,0"); |
| 224 | break; |
| 225 | case 1: |
| 226 | printf("1"); |
| 227 | break; |
| 228 | default: |
| 229 | printf("1,%d", count); |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 234 | static void copy_file(int prefix, const char *data, int size, |
| 235 | const char *set, const char *reset) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 236 | { |
| 237 | int ch, nl_just_seen = 1; |
| 238 | while (0 < size--) { |
| 239 | ch = *data++; |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 240 | if (nl_just_seen) { |
| 241 | fputs(set, stdout); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 242 | putchar(prefix); |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 243 | } |
| 244 | if (ch == '\n') { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 245 | nl_just_seen = 1; |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 246 | fputs(reset, stdout); |
| 247 | } else |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 248 | nl_just_seen = 0; |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 249 | putchar(ch); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 250 | } |
| 251 | if (!nl_just_seen) |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 252 | printf("%s\n\\ No newline at end of file\n", reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void emit_rewrite_diff(const char *name_a, |
| 256 | const char *name_b, |
| 257 | struct diff_filespec *one, |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 258 | struct diff_filespec *two, |
| 259 | int color_diff) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 260 | { |
| 261 | int lc_a, lc_b; |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 262 | const char *name_a_tab, *name_b_tab; |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 263 | const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO); |
| 264 | const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO); |
| 265 | const char *old = diff_get_color(color_diff, DIFF_FILE_OLD); |
| 266 | const char *new = diff_get_color(color_diff, DIFF_FILE_NEW); |
| 267 | const char *reset = diff_get_color(color_diff, DIFF_RESET); |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 268 | |
Junio C Hamano | 8a13bec | 2007-02-24 01:42:06 -0800 | [diff] [blame] | 269 | name_a += (*name_a == '/'); |
| 270 | name_b += (*name_b == '/'); |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 271 | name_a_tab = strchr(name_a, ' ') ? "\t" : ""; |
| 272 | name_b_tab = strchr(name_b, ' ') ? "\t" : ""; |
| 273 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 274 | diff_populate_filespec(one, 0); |
| 275 | diff_populate_filespec(two, 0); |
| 276 | lc_a = count_lines(one->data, one->size); |
| 277 | lc_b = count_lines(two->data, two->size); |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 278 | printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -", |
| 279 | metainfo, name_a, name_a_tab, reset, |
| 280 | metainfo, name_b, name_b_tab, reset, fraginfo); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 281 | print_line_count(lc_a); |
| 282 | printf(" +"); |
| 283 | print_line_count(lc_b); |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 284 | printf(" @@%s\n", reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 285 | if (lc_a) |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 286 | copy_file('-', one->data, one->size, old, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 287 | if (lc_b) |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 288 | copy_file('+', two->data, two->size, new, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one) |
| 292 | { |
| 293 | if (!DIFF_FILE_VALID(one)) { |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 294 | mf->ptr = (char *)""; /* does not matter */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 295 | mf->size = 0; |
| 296 | return 0; |
| 297 | } |
| 298 | else if (diff_populate_filespec(one, 0)) |
| 299 | return -1; |
| 300 | mf->ptr = one->data; |
| 301 | mf->size = one->size; |
| 302 | return 0; |
| 303 | } |
| 304 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 305 | struct diff_words_buffer { |
| 306 | mmfile_t text; |
| 307 | long alloc; |
| 308 | long current; /* output pointer */ |
| 309 | int suppressed_newline; |
| 310 | }; |
| 311 | |
| 312 | static void diff_words_append(char *line, unsigned long len, |
| 313 | struct diff_words_buffer *buffer) |
| 314 | { |
| 315 | if (buffer->text.size + len > buffer->alloc) { |
| 316 | buffer->alloc = (buffer->text.size + len) * 3 / 2; |
| 317 | buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc); |
| 318 | } |
| 319 | line++; |
| 320 | len--; |
| 321 | memcpy(buffer->text.ptr + buffer->text.size, line, len); |
| 322 | buffer->text.size += len; |
| 323 | } |
| 324 | |
| 325 | struct diff_words_data { |
| 326 | struct xdiff_emit_state xm; |
| 327 | struct diff_words_buffer minus, plus; |
| 328 | }; |
| 329 | |
| 330 | static void print_word(struct diff_words_buffer *buffer, int len, int color, |
| 331 | int suppress_newline) |
| 332 | { |
| 333 | const char *ptr; |
| 334 | int eol = 0; |
| 335 | |
| 336 | if (len == 0) |
| 337 | return; |
| 338 | |
| 339 | ptr = buffer->text.ptr + buffer->current; |
| 340 | buffer->current += len; |
| 341 | |
| 342 | if (ptr[len - 1] == '\n') { |
| 343 | eol = 1; |
| 344 | len--; |
| 345 | } |
| 346 | |
| 347 | fputs(diff_get_color(1, color), stdout); |
| 348 | fwrite(ptr, len, 1, stdout); |
| 349 | fputs(diff_get_color(1, DIFF_RESET), stdout); |
| 350 | |
| 351 | if (eol) { |
| 352 | if (suppress_newline) |
| 353 | buffer->suppressed_newline = 1; |
| 354 | else |
| 355 | putchar('\n'); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len) |
| 360 | { |
| 361 | struct diff_words_data *diff_words = priv; |
| 362 | |
| 363 | if (diff_words->minus.suppressed_newline) { |
| 364 | if (line[0] != '+') |
| 365 | putchar('\n'); |
| 366 | diff_words->minus.suppressed_newline = 0; |
| 367 | } |
| 368 | |
| 369 | len--; |
| 370 | switch (line[0]) { |
| 371 | case '-': |
| 372 | print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1); |
| 373 | break; |
| 374 | case '+': |
| 375 | print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0); |
| 376 | break; |
| 377 | case ' ': |
| 378 | print_word(&diff_words->plus, len, DIFF_PLAIN, 0); |
| 379 | diff_words->minus.current += len; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* this executes the word diff on the accumulated buffers */ |
| 385 | static void diff_words_show(struct diff_words_data *diff_words) |
| 386 | { |
| 387 | xpparam_t xpp; |
| 388 | xdemitconf_t xecfg; |
| 389 | xdemitcb_t ecb; |
| 390 | mmfile_t minus, plus; |
| 391 | int i; |
| 392 | |
| 393 | minus.size = diff_words->minus.text.size; |
| 394 | minus.ptr = xmalloc(minus.size); |
| 395 | memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size); |
| 396 | for (i = 0; i < minus.size; i++) |
| 397 | if (isspace(minus.ptr[i])) |
| 398 | minus.ptr[i] = '\n'; |
| 399 | diff_words->minus.current = 0; |
| 400 | |
| 401 | plus.size = diff_words->plus.text.size; |
| 402 | plus.ptr = xmalloc(plus.size); |
| 403 | memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size); |
| 404 | for (i = 0; i < plus.size; i++) |
| 405 | if (isspace(plus.ptr[i])) |
| 406 | plus.ptr[i] = '\n'; |
| 407 | diff_words->plus.current = 0; |
| 408 | |
| 409 | xpp.flags = XDF_NEED_MINIMAL; |
| 410 | xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc; |
| 411 | xecfg.flags = 0; |
| 412 | ecb.outf = xdiff_outf; |
| 413 | ecb.priv = diff_words; |
| 414 | diff_words->xm.consume = fn_out_diff_words_aux; |
| 415 | xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb); |
| 416 | |
| 417 | free(minus.ptr); |
| 418 | free(plus.ptr); |
| 419 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
| 420 | |
| 421 | if (diff_words->minus.suppressed_newline) { |
| 422 | putchar('\n'); |
| 423 | diff_words->minus.suppressed_newline = 0; |
| 424 | } |
| 425 | } |
| 426 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 427 | struct emit_callback { |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 428 | struct xdiff_emit_state xm; |
| 429 | int nparents, color_diff; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 430 | const char **label_path; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 431 | struct diff_words_data *diff_words; |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 432 | int *found_changesp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 435 | static void free_diff_words_data(struct emit_callback *ecbdata) |
| 436 | { |
| 437 | if (ecbdata->diff_words) { |
| 438 | /* flush buffers */ |
| 439 | if (ecbdata->diff_words->minus.text.size || |
| 440 | ecbdata->diff_words->plus.text.size) |
| 441 | diff_words_show(ecbdata->diff_words); |
| 442 | |
| 443 | if (ecbdata->diff_words->minus.text.ptr) |
| 444 | free (ecbdata->diff_words->minus.text.ptr); |
| 445 | if (ecbdata->diff_words->plus.text.ptr) |
| 446 | free (ecbdata->diff_words->plus.text.ptr); |
| 447 | free(ecbdata->diff_words); |
| 448 | ecbdata->diff_words = NULL; |
| 449 | } |
| 450 | } |
| 451 | |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 452 | const char *diff_get_color(int diff_use_color, enum color_diff ix) |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 453 | { |
| 454 | if (diff_use_color) |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 455 | return diff_colors[ix]; |
| 456 | return ""; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 459 | static void emit_line(const char *set, const char *reset, const char *line, int len) |
| 460 | { |
| 461 | if (len > 0 && line[len-1] == '\n') |
| 462 | len--; |
| 463 | fputs(set, stdout); |
| 464 | fwrite(line, len, 1, stdout); |
| 465 | puts(reset); |
| 466 | } |
| 467 | |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 468 | static void emit_line_with_ws(int nparents, |
| 469 | const char *set, const char *reset, const char *ws, |
| 470 | const char *line, int len) |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 471 | { |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 472 | int col0 = nparents; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 473 | int last_tab_in_indent = -1; |
| 474 | int last_space_in_indent = -1; |
| 475 | int i; |
| 476 | int tail = len; |
| 477 | int need_highlight_leading_space = 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 478 | /* The line is a newly added line. Does it have funny leading |
| 479 | * whitespaces? In indent, SP should never precede a TAB. |
| 480 | */ |
| 481 | for (i = col0; i < len; i++) { |
| 482 | if (line[i] == '\t') { |
| 483 | last_tab_in_indent = i; |
| 484 | if (0 <= last_space_in_indent) |
| 485 | need_highlight_leading_space = 1; |
| 486 | } |
| 487 | else if (line[i] == ' ') |
| 488 | last_space_in_indent = i; |
| 489 | else |
| 490 | break; |
| 491 | } |
| 492 | fputs(set, stdout); |
| 493 | fwrite(line, col0, 1, stdout); |
| 494 | fputs(reset, stdout); |
| 495 | if (((i == len) || line[i] == '\n') && i != col0) { |
| 496 | /* The whole line was indent */ |
| 497 | emit_line(ws, reset, line + col0, len - col0); |
| 498 | return; |
| 499 | } |
| 500 | i = col0; |
| 501 | if (need_highlight_leading_space) { |
| 502 | while (i < last_tab_in_indent) { |
| 503 | if (line[i] == ' ') { |
| 504 | fputs(ws, stdout); |
| 505 | putchar(' '); |
| 506 | fputs(reset, stdout); |
| 507 | } |
| 508 | else |
| 509 | putchar(line[i]); |
| 510 | i++; |
| 511 | } |
| 512 | } |
| 513 | tail = len - 1; |
| 514 | if (line[tail] == '\n' && i < tail) |
| 515 | tail--; |
| 516 | while (i < tail) { |
| 517 | if (!isspace(line[tail])) |
| 518 | break; |
| 519 | tail--; |
| 520 | } |
| 521 | if ((i < tail && line[tail + 1] != '\n')) { |
| 522 | /* This has whitespace between tail+1..len */ |
| 523 | fputs(set, stdout); |
| 524 | fwrite(line + i, tail - i + 1, 1, stdout); |
| 525 | fputs(reset, stdout); |
| 526 | emit_line(ws, reset, line + tail + 1, len - tail - 1); |
| 527 | } |
| 528 | else |
| 529 | emit_line(set, reset, line + i, len - i); |
| 530 | } |
| 531 | |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 532 | static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len) |
| 533 | { |
| 534 | const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE); |
| 535 | const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW); |
| 536 | |
| 537 | if (!*ws) |
| 538 | emit_line(set, reset, line, len); |
| 539 | else |
| 540 | emit_line_with_ws(ecbdata->nparents, set, reset, ws, |
| 541 | line, len); |
| 542 | } |
| 543 | |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 544 | static void fn_out_consume(void *priv, char *line, unsigned long len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 545 | { |
| 546 | int i; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 547 | int color; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 548 | struct emit_callback *ecbdata = priv; |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 549 | const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO); |
| 550 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 551 | |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 552 | *(ecbdata->found_changesp) = 1; |
| 553 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 554 | if (ecbdata->label_path[0]) { |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 555 | const char *name_a_tab, *name_b_tab; |
| 556 | |
| 557 | name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : ""; |
| 558 | name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : ""; |
| 559 | |
| 560 | printf("%s--- %s%s%s\n", |
| 561 | set, ecbdata->label_path[0], reset, name_a_tab); |
| 562 | printf("%s+++ %s%s%s\n", |
| 563 | set, ecbdata->label_path[1], reset, name_b_tab); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 564 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
| 565 | } |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 566 | |
| 567 | /* This is not really necessary for now because |
| 568 | * this codepath only deals with two-way diffs. |
| 569 | */ |
| 570 | for (i = 0; i < len && line[i] == '@'; i++) |
| 571 | ; |
| 572 | if (2 <= i && i < len && line[i] == ' ') { |
| 573 | ecbdata->nparents = i - 1; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 574 | emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO), |
| 575 | reset, line, len); |
| 576 | return; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 577 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 578 | |
| 579 | if (len < ecbdata->nparents) { |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 580 | set = reset; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 581 | emit_line(reset, reset, line, len); |
| 582 | return; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 583 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 584 | |
| 585 | color = DIFF_PLAIN; |
| 586 | if (ecbdata->diff_words && ecbdata->nparents != 1) |
| 587 | /* fall back to normal diff */ |
| 588 | free_diff_words_data(ecbdata); |
| 589 | if (ecbdata->diff_words) { |
| 590 | if (line[0] == '-') { |
| 591 | diff_words_append(line, len, |
| 592 | &ecbdata->diff_words->minus); |
| 593 | return; |
| 594 | } else if (line[0] == '+') { |
| 595 | diff_words_append(line, len, |
| 596 | &ecbdata->diff_words->plus); |
| 597 | return; |
| 598 | } |
| 599 | if (ecbdata->diff_words->minus.text.size || |
| 600 | ecbdata->diff_words->plus.text.size) |
| 601 | diff_words_show(ecbdata->diff_words); |
| 602 | line++; |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 603 | len--; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 604 | emit_line(set, reset, line, len); |
| 605 | return; |
| 606 | } |
| 607 | for (i = 0; i < ecbdata->nparents && len; i++) { |
| 608 | if (line[i] == '-') |
| 609 | color = DIFF_FILE_OLD; |
| 610 | else if (line[i] == '+') |
| 611 | color = DIFF_FILE_NEW; |
| 612 | } |
| 613 | |
| 614 | if (color != DIFF_FILE_NEW) { |
| 615 | emit_line(diff_get_color(ecbdata->color_diff, color), |
| 616 | reset, line, len); |
| 617 | return; |
| 618 | } |
| 619 | emit_add_line(reset, ecbdata, line, len); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static char *pprint_rename(const char *a, const char *b) |
| 623 | { |
| 624 | const char *old = a; |
| 625 | const char *new = b; |
| 626 | char *name = NULL; |
| 627 | int pfx_length, sfx_length; |
| 628 | int len_a = strlen(a); |
| 629 | int len_b = strlen(b); |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 630 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
| 631 | int qlen_b = quote_c_style(b, NULL, NULL, 0); |
| 632 | |
| 633 | if (qlen_a || qlen_b) { |
| 634 | if (qlen_a) len_a = qlen_a; |
| 635 | if (qlen_b) len_b = qlen_b; |
| 636 | name = xmalloc( len_a + len_b + 5 ); |
| 637 | if (qlen_a) |
| 638 | quote_c_style(a, name, NULL, 0); |
| 639 | else |
| 640 | memcpy(name, a, len_a); |
| 641 | memcpy(name + len_a, " => ", 4); |
| 642 | if (qlen_b) |
| 643 | quote_c_style(b, name + len_a + 4, NULL, 0); |
| 644 | else |
| 645 | memcpy(name + len_a + 4, b, len_b + 1); |
| 646 | return name; |
| 647 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 648 | |
| 649 | /* Find common prefix */ |
| 650 | pfx_length = 0; |
| 651 | while (*old && *new && *old == *new) { |
| 652 | if (*old == '/') |
| 653 | pfx_length = old - a + 1; |
| 654 | old++; |
| 655 | new++; |
| 656 | } |
| 657 | |
| 658 | /* Find common suffix */ |
| 659 | old = a + len_a; |
| 660 | new = b + len_b; |
| 661 | sfx_length = 0; |
| 662 | while (a <= old && b <= new && *old == *new) { |
| 663 | if (*old == '/') |
| 664 | sfx_length = len_a - (old - a); |
| 665 | old--; |
| 666 | new--; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * pfx{mid-a => mid-b}sfx |
| 671 | * {pfx-a => pfx-b}sfx |
| 672 | * pfx{sfx-a => sfx-b} |
| 673 | * name-a => name-b |
| 674 | */ |
| 675 | if (pfx_length + sfx_length) { |
Junio C Hamano | cc908b8 | 2006-05-14 22:07:28 -0700 | [diff] [blame] | 676 | int a_midlen = len_a - pfx_length - sfx_length; |
| 677 | int b_midlen = len_b - pfx_length - sfx_length; |
| 678 | if (a_midlen < 0) a_midlen = 0; |
| 679 | if (b_midlen < 0) b_midlen = 0; |
| 680 | |
Sean | e300846 | 2006-05-22 20:36:34 -0400 | [diff] [blame] | 681 | name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 682 | sprintf(name, "%.*s{%.*s => %.*s}%s", |
| 683 | pfx_length, a, |
Junio C Hamano | cc908b8 | 2006-05-14 22:07:28 -0700 | [diff] [blame] | 684 | a_midlen, a + pfx_length, |
| 685 | b_midlen, b + pfx_length, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 686 | a + len_a - sfx_length); |
| 687 | } |
| 688 | else { |
| 689 | name = xmalloc(len_a + len_b + 5); |
| 690 | sprintf(name, "%s => %s", a, b); |
| 691 | } |
| 692 | return name; |
| 693 | } |
| 694 | |
| 695 | struct diffstat_t { |
| 696 | struct xdiff_emit_state xm; |
| 697 | |
| 698 | int nr; |
| 699 | int alloc; |
| 700 | struct diffstat_file { |
| 701 | char *name; |
| 702 | unsigned is_unmerged:1; |
| 703 | unsigned is_binary:1; |
| 704 | unsigned is_renamed:1; |
| 705 | unsigned int added, deleted; |
| 706 | } **files; |
| 707 | }; |
| 708 | |
| 709 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, |
| 710 | const char *name_a, |
| 711 | const char *name_b) |
| 712 | { |
| 713 | struct diffstat_file *x; |
| 714 | x = xcalloc(sizeof (*x), 1); |
| 715 | if (diffstat->nr == diffstat->alloc) { |
| 716 | diffstat->alloc = alloc_nr(diffstat->alloc); |
| 717 | diffstat->files = xrealloc(diffstat->files, |
| 718 | diffstat->alloc * sizeof(x)); |
| 719 | } |
| 720 | diffstat->files[diffstat->nr++] = x; |
| 721 | if (name_b) { |
| 722 | x->name = pprint_rename(name_a, name_b); |
| 723 | x->is_renamed = 1; |
| 724 | } |
| 725 | else |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 726 | x->name = xstrdup(name_a); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 727 | return x; |
| 728 | } |
| 729 | |
| 730 | static void diffstat_consume(void *priv, char *line, unsigned long len) |
| 731 | { |
| 732 | struct diffstat_t *diffstat = priv; |
| 733 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; |
| 734 | |
| 735 | if (line[0] == '+') |
| 736 | x->added++; |
| 737 | else if (line[0] == '-') |
| 738 | x->deleted++; |
| 739 | } |
| 740 | |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 741 | const char mime_boundary_leader[] = "------------"; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 742 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 743 | static int scale_linear(int it, int width, int max_change) |
| 744 | { |
| 745 | /* |
Johannes Schindelin | 3ed74e6 | 2006-09-28 17:37:39 +0200 | [diff] [blame] | 746 | * make sure that at least one '-' is printed if there were deletions, |
| 747 | * and likewise for '+'. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 748 | */ |
Johannes Schindelin | 3ed74e6 | 2006-09-28 17:37:39 +0200 | [diff] [blame] | 749 | if (max_change < 2) |
| 750 | return it; |
| 751 | return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 754 | static void show_name(const char *prefix, const char *name, int len, |
| 755 | const char *reset, const char *set) |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 756 | { |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 757 | printf(" %s%s%-*s%s |", set, prefix, len, name, reset); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 760 | static void show_graph(char ch, int cnt, const char *set, const char *reset) |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 761 | { |
| 762 | if (cnt <= 0) |
| 763 | return; |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 764 | printf("%s", set); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 765 | while (cnt--) |
| 766 | putchar(ch); |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 767 | printf("%s", reset); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void show_stats(struct diffstat_t* data, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 771 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 772 | int i, len, add, del, total, adds = 0, dels = 0; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 773 | int max_change = 0, max_len = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 774 | int total_files = data->nr; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 775 | int width, name_width; |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 776 | const char *reset, *set, *add_c, *del_c; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 777 | |
| 778 | if (data->nr == 0) |
| 779 | return; |
| 780 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 781 | width = options->stat_width ? options->stat_width : 80; |
| 782 | name_width = options->stat_name_width ? options->stat_name_width : 50; |
| 783 | |
| 784 | /* Sanity: give at least 5 columns to the graph, |
| 785 | * but leave at least 10 columns for the name. |
| 786 | */ |
| 787 | if (width < name_width + 15) { |
| 788 | if (name_width <= 25) |
| 789 | width = name_width + 15; |
| 790 | else |
| 791 | name_width = width - 15; |
| 792 | } |
| 793 | |
| 794 | /* Find the longest filename and max number of changes */ |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 795 | reset = diff_get_color(options->color_diff, DIFF_RESET); |
| 796 | set = diff_get_color(options->color_diff, DIFF_PLAIN); |
| 797 | add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW); |
| 798 | del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD); |
| 799 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 800 | for (i = 0; i < data->nr; i++) { |
| 801 | struct diffstat_file *file = data->files[i]; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 802 | int change = file->added + file->deleted; |
| 803 | |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 804 | if (!file->is_renamed) { /* renames are already quoted by pprint_rename */ |
| 805 | len = quote_c_style(file->name, NULL, NULL, 0); |
| 806 | if (len) { |
| 807 | char *qname = xmalloc(len + 1); |
| 808 | quote_c_style(file->name, qname, NULL, 0); |
| 809 | free(file->name); |
| 810 | file->name = qname; |
| 811 | } |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 812 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 813 | |
| 814 | len = strlen(file->name); |
| 815 | if (max_len < len) |
| 816 | max_len = len; |
| 817 | |
| 818 | if (file->is_binary || file->is_unmerged) |
| 819 | continue; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 820 | if (max_change < change) |
| 821 | max_change = change; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 824 | /* Compute the width of the graph part; |
| 825 | * 10 is for one blank at the beginning of the line plus |
| 826 | * " | count " between the name and the graph. |
| 827 | * |
| 828 | * From here on, name_width is the width of the name area, |
| 829 | * and width is the width of the graph area. |
| 830 | */ |
| 831 | name_width = (name_width < max_len) ? name_width : max_len; |
| 832 | if (width < (name_width + 10) + max_change) |
| 833 | width = width - (name_width + 10); |
| 834 | else |
| 835 | width = max_change; |
| 836 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 837 | for (i = 0; i < data->nr; i++) { |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 838 | const char *prefix = ""; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 839 | char *name = data->files[i]->name; |
| 840 | int added = data->files[i]->added; |
| 841 | int deleted = data->files[i]->deleted; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 842 | int name_len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 843 | |
| 844 | /* |
| 845 | * "scale" the filename |
| 846 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 847 | len = name_width; |
| 848 | name_len = strlen(name); |
| 849 | if (name_width < name_len) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 850 | char *slash; |
| 851 | prefix = "..."; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 852 | len -= 3; |
| 853 | name += name_len - len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 854 | slash = strchr(name, '/'); |
| 855 | if (slash) |
| 856 | name = slash; |
| 857 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 858 | |
| 859 | if (data->files[i]->is_binary) { |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 860 | show_name(prefix, name, len, reset, set); |
Andy Parkins | b188258 | 2007-04-04 13:14:14 +0000 | [diff] [blame] | 861 | printf(" Bin "); |
| 862 | printf("%s%d%s", del_c, deleted, reset); |
| 863 | printf(" -> "); |
| 864 | printf("%s%d%s", add_c, added, reset); |
| 865 | printf(" bytes"); |
| 866 | printf("\n"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 867 | goto free_diffstat_file; |
| 868 | } |
| 869 | else if (data->files[i]->is_unmerged) { |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 870 | show_name(prefix, name, len, reset, set); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 871 | printf(" Unmerged\n"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 872 | goto free_diffstat_file; |
| 873 | } |
| 874 | else if (!data->files[i]->is_renamed && |
| 875 | (added + deleted == 0)) { |
| 876 | total_files--; |
| 877 | goto free_diffstat_file; |
| 878 | } |
| 879 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 880 | /* |
| 881 | * scale the add/delete |
| 882 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 883 | add = added; |
| 884 | del = deleted; |
| 885 | total = add + del; |
| 886 | adds += add; |
| 887 | dels += del; |
| 888 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 889 | if (width <= max_change) { |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 890 | add = scale_linear(add, width, max_change); |
Johannes Schindelin | 3ed74e6 | 2006-09-28 17:37:39 +0200 | [diff] [blame] | 891 | del = scale_linear(del, width, max_change); |
| 892 | total = add + del; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 893 | } |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 894 | show_name(prefix, name, len, reset, set); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 895 | printf("%5d ", added + deleted); |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 896 | show_graph('+', add, add_c, reset); |
| 897 | show_graph('-', del, del_c, reset); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 898 | putchar('\n'); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 899 | free_diffstat_file: |
| 900 | free(data->files[i]->name); |
| 901 | free(data->files[i]); |
| 902 | } |
| 903 | free(data->files); |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 904 | printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n", |
| 905 | set, total_files, adds, dels, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 908 | static void show_shortstats(struct diffstat_t* data) |
| 909 | { |
| 910 | int i, adds = 0, dels = 0, total_files = data->nr; |
| 911 | |
| 912 | if (data->nr == 0) |
| 913 | return; |
| 914 | |
| 915 | for (i = 0; i < data->nr; i++) { |
| 916 | if (!data->files[i]->is_binary && |
| 917 | !data->files[i]->is_unmerged) { |
| 918 | int added = data->files[i]->added; |
| 919 | int deleted= data->files[i]->deleted; |
| 920 | if (!data->files[i]->is_renamed && |
| 921 | (added + deleted == 0)) { |
| 922 | total_files--; |
| 923 | } else { |
| 924 | adds += added; |
| 925 | dels += deleted; |
| 926 | } |
| 927 | } |
| 928 | free(data->files[i]->name); |
| 929 | free(data->files[i]); |
| 930 | } |
| 931 | free(data->files); |
| 932 | |
| 933 | printf(" %d files changed, %d insertions(+), %d deletions(-)\n", |
| 934 | total_files, adds, dels); |
| 935 | } |
| 936 | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 937 | static void show_numstat(struct diffstat_t* data, struct diff_options *options) |
| 938 | { |
| 939 | int i; |
| 940 | |
| 941 | for (i = 0; i < data->nr; i++) { |
| 942 | struct diffstat_file *file = data->files[i]; |
| 943 | |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 944 | if (file->is_binary) |
| 945 | printf("-\t-\t"); |
| 946 | else |
| 947 | printf("%d\t%d\t", file->added, file->deleted); |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 948 | if (options->line_termination && !file->is_renamed && |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 949 | quote_c_style(file->name, NULL, NULL, 0)) |
| 950 | quote_c_style(file->name, NULL, stdout, 0); |
| 951 | else |
| 952 | fputs(file->name, stdout); |
| 953 | putchar(options->line_termination); |
| 954 | } |
| 955 | } |
| 956 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 957 | struct checkdiff_t { |
| 958 | struct xdiff_emit_state xm; |
| 959 | const char *filename; |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 960 | int lineno, color_diff; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 961 | }; |
| 962 | |
| 963 | static void checkdiff_consume(void *priv, char *line, unsigned long len) |
| 964 | { |
| 965 | struct checkdiff_t *data = priv; |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 966 | const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE); |
| 967 | const char *reset = diff_get_color(data->color_diff, DIFF_RESET); |
| 968 | const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 969 | |
| 970 | if (line[0] == '+') { |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 971 | int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 972 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 973 | /* check space before tab */ |
| 974 | for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++) |
| 975 | if (line[i] == ' ') |
| 976 | spaces++; |
| 977 | if (line[i - 1] == '\t' && spaces) |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 978 | space_before_tab = 1; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 979 | |
| 980 | /* check white space at line end */ |
| 981 | if (line[len - 1] == '\n') |
| 982 | len--; |
| 983 | if (isspace(line[len - 1])) |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 984 | white_space_at_end = 1; |
| 985 | |
| 986 | if (space_before_tab || white_space_at_end) { |
| 987 | printf("%s:%d: %s", data->filename, data->lineno, ws); |
| 988 | if (space_before_tab) { |
| 989 | printf("space before tab"); |
| 990 | if (white_space_at_end) |
| 991 | putchar(','); |
| 992 | } |
| 993 | if (white_space_at_end) |
| 994 | printf("white space at end"); |
| 995 | printf(":%s ", reset); |
| 996 | emit_line_with_ws(1, set, reset, ws, line, len); |
| 997 | } |
Johannes Schindelin | e6d40d6 | 2006-12-22 03:20:11 +0100 | [diff] [blame] | 998 | |
| 999 | data->lineno++; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1000 | } else if (line[0] == ' ') |
| 1001 | data->lineno++; |
| 1002 | else if (line[0] == '@') { |
| 1003 | char *plus = strchr(line, '+'); |
| 1004 | if (plus) |
Junio C Hamano | 9e84816 | 2006-05-21 03:01:59 -0700 | [diff] [blame] | 1005 | data->lineno = strtol(plus, NULL, 10); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1006 | else |
| 1007 | die("invalid diff"); |
| 1008 | } |
| 1009 | } |
| 1010 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1011 | static unsigned char *deflate_it(char *data, |
| 1012 | unsigned long size, |
| 1013 | unsigned long *result_size) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1014 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1015 | int bound; |
| 1016 | unsigned char *deflated; |
| 1017 | z_stream stream; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1018 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1019 | memset(&stream, 0, sizeof(stream)); |
Joachim B Haga | 12f6c30 | 2006-07-03 22:11:47 +0200 | [diff] [blame] | 1020 | deflateInit(&stream, zlib_compression_level); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1021 | bound = deflateBound(&stream, size); |
| 1022 | deflated = xmalloc(bound); |
| 1023 | stream.next_out = deflated; |
| 1024 | stream.avail_out = bound; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1025 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1026 | stream.next_in = (unsigned char *)data; |
| 1027 | stream.avail_in = size; |
| 1028 | while (deflate(&stream, Z_FINISH) == Z_OK) |
| 1029 | ; /* nothing */ |
| 1030 | deflateEnd(&stream); |
| 1031 | *result_size = stream.total_out; |
| 1032 | return deflated; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 1035 | static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1036 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1037 | void *cp; |
| 1038 | void *delta; |
| 1039 | void *deflated; |
| 1040 | void *data; |
| 1041 | unsigned long orig_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1042 | unsigned long delta_size; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1043 | unsigned long deflate_size; |
| 1044 | unsigned long data_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1045 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1046 | /* We could do deflated delta, or we could do just deflated two, |
| 1047 | * whichever is smaller. |
| 1048 | */ |
| 1049 | delta = NULL; |
| 1050 | deflated = deflate_it(two->ptr, two->size, &deflate_size); |
| 1051 | if (one->size && two->size) { |
| 1052 | delta = diff_delta(one->ptr, one->size, |
| 1053 | two->ptr, two->size, |
| 1054 | &delta_size, deflate_size); |
| 1055 | if (delta) { |
| 1056 | void *to_free = delta; |
| 1057 | orig_size = delta_size; |
| 1058 | delta = deflate_it(delta, delta_size, &delta_size); |
| 1059 | free(to_free); |
| 1060 | } |
| 1061 | } |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1062 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1063 | if (delta && delta_size < deflate_size) { |
| 1064 | printf("delta %lu\n", orig_size); |
| 1065 | free(deflated); |
| 1066 | data = delta; |
| 1067 | data_size = delta_size; |
| 1068 | } |
| 1069 | else { |
| 1070 | printf("literal %lu\n", two->size); |
| 1071 | free(delta); |
| 1072 | data = deflated; |
| 1073 | data_size = deflate_size; |
| 1074 | } |
| 1075 | |
| 1076 | /* emit data encoded in base85 */ |
| 1077 | cp = data; |
| 1078 | while (data_size) { |
| 1079 | int bytes = (52 < data_size) ? 52 : data_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1080 | char line[70]; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1081 | data_size -= bytes; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1082 | if (bytes <= 26) |
| 1083 | line[0] = bytes + 'A' - 1; |
| 1084 | else |
| 1085 | line[0] = bytes - 26 + 'a' - 1; |
| 1086 | encode_85(line + 1, cp, bytes); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 1087 | cp = (char *) cp + bytes; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1088 | puts(line); |
| 1089 | } |
| 1090 | printf("\n"); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1091 | free(data); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 1094 | static void emit_binary_diff(mmfile_t *one, mmfile_t *two) |
| 1095 | { |
| 1096 | printf("GIT binary patch\n"); |
| 1097 | emit_binary_diff_body(one, two); |
| 1098 | emit_binary_diff_body(two, one); |
| 1099 | } |
| 1100 | |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1101 | static void setup_diff_attr_check(struct git_attr_check *check) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1102 | { |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1103 | static struct git_attr *attr_diff; |
| 1104 | |
| 1105 | if (!attr_diff) |
| 1106 | attr_diff = git_attr("diff", 4); |
| 1107 | check->attr = attr_diff; |
| 1108 | } |
| 1109 | |
| 1110 | #define FIRST_FEW_BYTES 8000 |
| 1111 | static int file_is_binary(struct diff_filespec *one) |
| 1112 | { |
| 1113 | unsigned long sz; |
| 1114 | struct git_attr_check attr_diff_check; |
| 1115 | |
| 1116 | setup_diff_attr_check(&attr_diff_check); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1117 | if (!git_checkattr(one->path, 1, &attr_diff_check)) { |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 1118 | const char *value = attr_diff_check.value; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1119 | if (ATTR_TRUE(value)) |
| 1120 | return 0; |
| 1121 | else if (ATTR_FALSE(value)) |
| 1122 | return 1; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1123 | } |
Junio C Hamano | 40250af | 2007-04-15 14:35:11 -0700 | [diff] [blame] | 1124 | |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1125 | if (!one->data) { |
| 1126 | if (!DIFF_FILE_VALID(one)) |
| 1127 | return 0; |
| 1128 | diff_populate_filespec(one, 0); |
| 1129 | } |
| 1130 | sz = one->size; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1131 | if (FIRST_FEW_BYTES < sz) |
| 1132 | sz = FIRST_FEW_BYTES; |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1133 | return !!memchr(one->data, 0, sz); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static void builtin_diff(const char *name_a, |
| 1137 | const char *name_b, |
| 1138 | struct diff_filespec *one, |
| 1139 | struct diff_filespec *two, |
| 1140 | const char *xfrm_msg, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1141 | struct diff_options *o, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1142 | int complete_rewrite) |
| 1143 | { |
| 1144 | mmfile_t mf1, mf2; |
| 1145 | const char *lbl[2]; |
| 1146 | char *a_one, *b_two; |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 1147 | const char *set = diff_get_color(o->color_diff, DIFF_METAINFO); |
| 1148 | const char *reset = diff_get_color(o->color_diff, DIFF_RESET); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1149 | |
Junio C Hamano | 5089277 | 2007-02-23 03:44:30 -0800 | [diff] [blame] | 1150 | a_one = quote_two("a/", name_a + (*name_a == '/')); |
| 1151 | b_two = quote_two("b/", name_b + (*name_b == '/')); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1152 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
| 1153 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 1154 | printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1155 | if (lbl[0][0] == '/') { |
| 1156 | /* /dev/null */ |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 1157 | printf("%snew file mode %06o%s\n", set, two->mode, reset); |
| 1158 | if (xfrm_msg && xfrm_msg[0]) |
| 1159 | printf("%s%s%s\n", set, xfrm_msg, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1160 | } |
| 1161 | else if (lbl[1][0] == '/') { |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 1162 | printf("%sdeleted file mode %06o%s\n", set, one->mode, reset); |
| 1163 | if (xfrm_msg && xfrm_msg[0]) |
| 1164 | printf("%s%s%s\n", set, xfrm_msg, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1165 | } |
| 1166 | else { |
| 1167 | if (one->mode != two->mode) { |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 1168 | printf("%sold mode %06o%s\n", set, one->mode, reset); |
| 1169 | printf("%snew mode %06o%s\n", set, two->mode, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1170 | } |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 1171 | if (xfrm_msg && xfrm_msg[0]) |
| 1172 | printf("%s%s%s\n", set, xfrm_msg, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * we do not run diff between different kind |
| 1175 | * of objects. |
| 1176 | */ |
| 1177 | if ((one->mode ^ two->mode) & S_IFMT) |
| 1178 | goto free_ab_and_return; |
| 1179 | if (complete_rewrite) { |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1180 | emit_rewrite_diff(name_a, name_b, one, two, |
| 1181 | o->color_diff); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 1182 | o->found_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1183 | goto free_ab_and_return; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
| 1188 | die("unable to read files to diff"); |
| 1189 | |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1190 | if (!o->text && (file_is_binary(one) || file_is_binary(two))) { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 1191 | /* Quite common confusing case */ |
| 1192 | if (mf1.size == mf2.size && |
| 1193 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) |
| 1194 | goto free_ab_and_return; |
| 1195 | if (o->binary) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1196 | emit_binary_diff(&mf1, &mf2); |
| 1197 | else |
| 1198 | printf("Binary files %s and %s differ\n", |
| 1199 | lbl[0], lbl[1]); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 1200 | o->found_changes = 1; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1201 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1202 | else { |
| 1203 | /* Crazy xdl interfaces.. */ |
| 1204 | const char *diffopts = getenv("GIT_DIFF_OPTS"); |
| 1205 | xpparam_t xpp; |
| 1206 | xdemitconf_t xecfg; |
| 1207 | xdemitcb_t ecb; |
| 1208 | struct emit_callback ecbdata; |
| 1209 | |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 1210 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1211 | ecbdata.label_path = lbl; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 1212 | ecbdata.color_diff = o->color_diff; |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 1213 | ecbdata.found_changesp = &o->found_changes; |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 1214 | xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts; |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 1215 | xecfg.ctxlen = o->context; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1216 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
| 1217 | if (!diffopts) |
| 1218 | ; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1219 | else if (!prefixcmp(diffopts, "--unified=")) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1220 | xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10); |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1221 | else if (!prefixcmp(diffopts, "-u")) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1222 | xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10); |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 1223 | ecb.outf = xdiff_outf; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1224 | ecb.priv = &ecbdata; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 1225 | ecbdata.xm.consume = fn_out_consume; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1226 | if (o->color_diff_words) |
| 1227 | ecbdata.diff_words = |
| 1228 | xcalloc(1, sizeof(struct diff_words_data)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1229 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1230 | if (o->color_diff_words) |
| 1231 | free_diff_words_data(&ecbdata); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | free_ab_and_return: |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 1235 | diff_free_filespec_data(one); |
| 1236 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1237 | free(a_one); |
| 1238 | free(b_two); |
| 1239 | return; |
| 1240 | } |
| 1241 | |
| 1242 | static void builtin_diffstat(const char *name_a, const char *name_b, |
| 1243 | struct diff_filespec *one, |
| 1244 | struct diff_filespec *two, |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 1245 | struct diffstat_t *diffstat, |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 1246 | struct diff_options *o, |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 1247 | int complete_rewrite) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1248 | { |
| 1249 | mmfile_t mf1, mf2; |
| 1250 | struct diffstat_file *data; |
| 1251 | |
| 1252 | data = diffstat_add(diffstat, name_a, name_b); |
| 1253 | |
| 1254 | if (!one || !two) { |
| 1255 | data->is_unmerged = 1; |
| 1256 | return; |
| 1257 | } |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 1258 | if (complete_rewrite) { |
| 1259 | diff_populate_filespec(one, 0); |
| 1260 | diff_populate_filespec(two, 0); |
| 1261 | data->deleted = count_lines(one->data, one->size); |
| 1262 | data->added = count_lines(two->data, two->size); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 1263 | goto free_and_return; |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 1264 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1265 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
| 1266 | die("unable to read files to diff"); |
| 1267 | |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1268 | if (file_is_binary(one) || file_is_binary(two)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1269 | data->is_binary = 1; |
Andy Parkins | b188258 | 2007-04-04 13:14:14 +0000 | [diff] [blame] | 1270 | data->added = mf2.size; |
| 1271 | data->deleted = mf1.size; |
| 1272 | } else { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1273 | /* Crazy xdl interfaces.. */ |
| 1274 | xpparam_t xpp; |
| 1275 | xdemitconf_t xecfg; |
| 1276 | xdemitcb_t ecb; |
| 1277 | |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 1278 | xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1279 | xecfg.ctxlen = 0; |
| 1280 | xecfg.flags = 0; |
| 1281 | ecb.outf = xdiff_outf; |
| 1282 | ecb.priv = diffstat; |
| 1283 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); |
| 1284 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 1285 | |
| 1286 | free_and_return: |
| 1287 | diff_free_filespec_data(one); |
| 1288 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1291 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
| 1292 | struct diff_filespec *one, |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 1293 | struct diff_filespec *two, struct diff_options *o) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1294 | { |
| 1295 | mmfile_t mf1, mf2; |
| 1296 | struct checkdiff_t data; |
| 1297 | |
| 1298 | if (!two) |
| 1299 | return; |
| 1300 | |
| 1301 | memset(&data, 0, sizeof(data)); |
| 1302 | data.xm.consume = checkdiff_consume; |
| 1303 | data.filename = name_b ? name_b : name_a; |
| 1304 | data.lineno = 0; |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 1305 | data.color_diff = o->color_diff; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1306 | |
| 1307 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
| 1308 | die("unable to read files to diff"); |
| 1309 | |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1310 | if (file_is_binary(two)) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 1311 | goto free_and_return; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1312 | else { |
| 1313 | /* Crazy xdl interfaces.. */ |
| 1314 | xpparam_t xpp; |
| 1315 | xdemitconf_t xecfg; |
| 1316 | xdemitcb_t ecb; |
| 1317 | |
| 1318 | xpp.flags = XDF_NEED_MINIMAL; |
| 1319 | xecfg.ctxlen = 0; |
| 1320 | xecfg.flags = 0; |
| 1321 | ecb.outf = xdiff_outf; |
| 1322 | ecb.priv = &data; |
| 1323 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); |
| 1324 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 1325 | free_and_return: |
| 1326 | diff_free_filespec_data(one); |
| 1327 | diff_free_filespec_data(two); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1328 | } |
| 1329 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1330 | struct diff_filespec *alloc_filespec(const char *path) |
| 1331 | { |
| 1332 | int namelen = strlen(path); |
| 1333 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); |
| 1334 | |
| 1335 | memset(spec, 0, sizeof(*spec)); |
| 1336 | spec->path = (char *)(spec + 1); |
| 1337 | memcpy(spec->path, path, namelen+1); |
| 1338 | return spec; |
| 1339 | } |
| 1340 | |
| 1341 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, |
| 1342 | unsigned short mode) |
| 1343 | { |
| 1344 | if (mode) { |
| 1345 | spec->mode = canon_mode(mode); |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1346 | hashcpy(spec->sha1, sha1); |
David Rientjes | 0bef57e | 2006-08-15 13:37:19 -0700 | [diff] [blame] | 1347 | spec->sha1_valid = !is_null_sha1(sha1); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /* |
Jakub Narebski | 5adf317 | 2007-05-26 00:37:40 +0200 | [diff] [blame] | 1352 | * Given a name and sha1 pair, if the index tells us the file in |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1353 | * the work tree has that object contents, return true, so that |
| 1354 | * prepare_temp_file() does not have to inflate and extract. |
| 1355 | */ |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 1356 | static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1357 | { |
| 1358 | struct cache_entry *ce; |
| 1359 | struct stat st; |
| 1360 | int pos, len; |
| 1361 | |
| 1362 | /* We do not read the cache ourselves here, because the |
| 1363 | * benchmark with my previous version that always reads cache |
| 1364 | * shows that it makes things worse for diff-tree comparing |
| 1365 | * two linux-2.6 kernel trees in an already checked out work |
| 1366 | * tree. This is because most diff-tree comparisons deal with |
| 1367 | * only a small number of files, while reading the cache is |
| 1368 | * expensive for a large project, and its cost outweighs the |
| 1369 | * savings we get by not inflating the object to a temporary |
| 1370 | * file. Practically, this code only helps when we are used |
| 1371 | * by diff-cache --cached, which does read the cache before |
| 1372 | * calling us. |
| 1373 | */ |
| 1374 | if (!active_cache) |
| 1375 | return 0; |
| 1376 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 1377 | /* We want to avoid the working directory if our caller |
| 1378 | * doesn't need the data in a normal file, this system |
| 1379 | * is rather slow with its stat/open/mmap/close syscalls, |
| 1380 | * and the object is contained in a pack file. The pack |
| 1381 | * is probably already open and will be faster to obtain |
| 1382 | * the data through than the working directory. Loose |
| 1383 | * objects however would tend to be slower as they need |
| 1384 | * to be individually opened and inflated. |
| 1385 | */ |
Junio C Hamano | 5caf923 | 2006-12-19 18:26:48 -0800 | [diff] [blame] | 1386 | if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL)) |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 1387 | return 0; |
| 1388 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1389 | len = strlen(name); |
| 1390 | pos = cache_name_pos(name, len); |
| 1391 | if (pos < 0) |
| 1392 | return 0; |
| 1393 | ce = active_cache[pos]; |
| 1394 | if ((lstat(name, &st) < 0) || |
| 1395 | !S_ISREG(st.st_mode) || /* careful! */ |
| 1396 | ce_match_stat(ce, &st, 0) || |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 1397 | hashcmp(sha1, ce->sha1)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1398 | return 0; |
| 1399 | /* we return 1 only when we can stat, it is a regular file, |
| 1400 | * stat information matches, and sha1 recorded in the cache |
| 1401 | * matches. I.e. we know the file in the work tree really is |
| 1402 | * the same as the <name, sha1> pair. |
| 1403 | */ |
| 1404 | return 1; |
| 1405 | } |
| 1406 | |
Junio C Hamano | 3afaa72 | 2007-03-04 00:17:27 -0800 | [diff] [blame] | 1407 | static int populate_from_stdin(struct diff_filespec *s) |
| 1408 | { |
| 1409 | #define INCREMENT 1024 |
| 1410 | char *buf; |
| 1411 | unsigned long size; |
Johan Herland | 8a912bc | 2007-05-15 14:49:22 +0200 | [diff] [blame] | 1412 | ssize_t got; |
Junio C Hamano | 3afaa72 | 2007-03-04 00:17:27 -0800 | [diff] [blame] | 1413 | |
| 1414 | size = 0; |
| 1415 | buf = NULL; |
| 1416 | while (1) { |
| 1417 | buf = xrealloc(buf, size + INCREMENT); |
| 1418 | got = xread(0, buf + size, INCREMENT); |
| 1419 | if (!got) |
| 1420 | break; /* EOF */ |
| 1421 | if (got < 0) |
| 1422 | return error("error while reading from stdin %s", |
| 1423 | strerror(errno)); |
| 1424 | size += got; |
| 1425 | } |
| 1426 | s->should_munmap = 0; |
| 1427 | s->data = buf; |
| 1428 | s->size = size; |
| 1429 | s->should_free = 1; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 1433 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
| 1434 | { |
| 1435 | int len; |
| 1436 | char *data = xmalloc(100); |
| 1437 | len = snprintf(data, 100, |
| 1438 | "Subproject commit %s\n", sha1_to_hex(s->sha1)); |
| 1439 | s->data = data; |
| 1440 | s->size = len; |
| 1441 | s->should_free = 1; |
| 1442 | if (size_only) { |
| 1443 | s->data = NULL; |
| 1444 | free(data); |
| 1445 | } |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1449 | /* |
| 1450 | * While doing rename detection and pickaxe operation, we may need to |
| 1451 | * grab the data for the blob (or file) for our own in-core comparison. |
| 1452 | * diff_filespec has data and size fields for this purpose. |
| 1453 | */ |
| 1454 | int diff_populate_filespec(struct diff_filespec *s, int size_only) |
| 1455 | { |
| 1456 | int err = 0; |
| 1457 | if (!DIFF_FILE_VALID(s)) |
| 1458 | die("internal error: asking to populate invalid file."); |
| 1459 | if (S_ISDIR(s->mode)) |
| 1460 | return -1; |
| 1461 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1462 | if (s->data) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 1463 | return 0; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 1464 | |
Junio C Hamano | 6e0b8ed | 2007-05-07 01:14:21 -0700 | [diff] [blame] | 1465 | if (size_only && 0 < s->size) |
| 1466 | return 0; |
| 1467 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 1468 | if (S_ISGITLINK(s->mode)) |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 1469 | return diff_populate_gitlink(s, size_only); |
| 1470 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1471 | if (!s->sha1_valid || |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 1472 | reuse_worktree_file(s->path, s->sha1, 0)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1473 | struct stat st; |
| 1474 | int fd; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 1475 | char *buf; |
| 1476 | unsigned long size; |
| 1477 | |
Junio C Hamano | 3afaa72 | 2007-03-04 00:17:27 -0800 | [diff] [blame] | 1478 | if (!strcmp(s->path, "-")) |
| 1479 | return populate_from_stdin(s); |
| 1480 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1481 | if (lstat(s->path, &st) < 0) { |
| 1482 | if (errno == ENOENT) { |
| 1483 | err_empty: |
| 1484 | err = -1; |
| 1485 | empty: |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 1486 | s->data = (char *)""; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1487 | s->size = 0; |
| 1488 | return err; |
| 1489 | } |
| 1490 | } |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1491 | s->size = xsize_t(st.st_size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1492 | if (!s->size) |
| 1493 | goto empty; |
| 1494 | if (size_only) |
| 1495 | return 0; |
| 1496 | if (S_ISLNK(st.st_mode)) { |
| 1497 | int ret; |
| 1498 | s->data = xmalloc(s->size); |
| 1499 | s->should_free = 1; |
| 1500 | ret = readlink(s->path, s->data, s->size); |
| 1501 | if (ret < 0) { |
| 1502 | free(s->data); |
| 1503 | goto err_empty; |
| 1504 | } |
| 1505 | return 0; |
| 1506 | } |
| 1507 | fd = open(s->path, O_RDONLY); |
| 1508 | if (fd < 0) |
| 1509 | goto err_empty; |
Shawn O. Pearce | c4712e4 | 2006-12-24 00:47:23 -0500 | [diff] [blame] | 1510 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1511 | close(fd); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1512 | s->should_munmap = 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 1513 | |
| 1514 | /* |
| 1515 | * Convert from working tree format to canonical git format |
| 1516 | */ |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 1517 | size = s->size; |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 1518 | buf = convert_to_git(s->path, s->data, &size); |
| 1519 | if (buf) { |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 1520 | munmap(s->data, s->size); |
| 1521 | s->should_munmap = 0; |
| 1522 | s->data = buf; |
| 1523 | s->size = size; |
| 1524 | s->should_free = 1; |
| 1525 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1526 | } |
| 1527 | else { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1528 | enum object_type type; |
Junio C Hamano | 6e0b8ed | 2007-05-07 01:14:21 -0700 | [diff] [blame] | 1529 | if (size_only) |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1530 | type = sha1_object_info(s->sha1, &s->size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1531 | else { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1532 | s->data = read_sha1_file(s->sha1, &type, &s->size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1533 | s->should_free = 1; |
| 1534 | } |
| 1535 | } |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | void diff_free_filespec_data(struct diff_filespec *s) |
| 1540 | { |
| 1541 | if (s->should_free) |
| 1542 | free(s->data); |
| 1543 | else if (s->should_munmap) |
| 1544 | munmap(s->data, s->size); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 1545 | |
| 1546 | if (s->should_free || s->should_munmap) { |
| 1547 | s->should_free = s->should_munmap = 0; |
| 1548 | s->data = NULL; |
| 1549 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1550 | free(s->cnt_data); |
| 1551 | s->cnt_data = NULL; |
| 1552 | } |
| 1553 | |
| 1554 | static void prep_temp_blob(struct diff_tempfile *temp, |
| 1555 | void *blob, |
| 1556 | unsigned long size, |
| 1557 | const unsigned char *sha1, |
| 1558 | int mode) |
| 1559 | { |
| 1560 | int fd; |
| 1561 | |
Fernando J. Pereda | 1472966 | 2007-05-20 15:35:46 +0200 | [diff] [blame] | 1562 | fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1563 | if (fd < 0) |
| 1564 | die("unable to create temp-file"); |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 1565 | if (write_in_full(fd, blob, size) != size) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1566 | die("unable to write temp-file"); |
| 1567 | close(fd); |
| 1568 | temp->name = temp->tmp_path; |
| 1569 | strcpy(temp->hex, sha1_to_hex(sha1)); |
| 1570 | temp->hex[40] = 0; |
| 1571 | sprintf(temp->mode, "%06o", mode); |
| 1572 | } |
| 1573 | |
| 1574 | static void prepare_temp_file(const char *name, |
| 1575 | struct diff_tempfile *temp, |
| 1576 | struct diff_filespec *one) |
| 1577 | { |
| 1578 | if (!DIFF_FILE_VALID(one)) { |
| 1579 | not_a_valid_file: |
| 1580 | /* A '-' entry produces this for file-2, and |
| 1581 | * a '+' entry produces this for file-1. |
| 1582 | */ |
| 1583 | temp->name = "/dev/null"; |
| 1584 | strcpy(temp->hex, "."); |
| 1585 | strcpy(temp->mode, "."); |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | if (!one->sha1_valid || |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 1590 | reuse_worktree_file(name, one->sha1, 1)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1591 | struct stat st; |
| 1592 | if (lstat(name, &st) < 0) { |
| 1593 | if (errno == ENOENT) |
| 1594 | goto not_a_valid_file; |
| 1595 | die("stat(%s): %s", name, strerror(errno)); |
| 1596 | } |
| 1597 | if (S_ISLNK(st.st_mode)) { |
| 1598 | int ret; |
| 1599 | char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1600 | size_t sz = xsize_t(st.st_size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1601 | if (sizeof(buf) <= st.st_size) |
| 1602 | die("symlink too long: %s", name); |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1603 | ret = readlink(name, buf, sz); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1604 | if (ret < 0) |
| 1605 | die("readlink(%s)", name); |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1606 | prep_temp_blob(temp, buf, sz, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1607 | (one->sha1_valid ? |
| 1608 | one->sha1 : null_sha1), |
| 1609 | (one->sha1_valid ? |
| 1610 | one->mode : S_IFLNK)); |
| 1611 | } |
| 1612 | else { |
| 1613 | /* we can borrow from the file in the work tree */ |
| 1614 | temp->name = name; |
| 1615 | if (!one->sha1_valid) |
| 1616 | strcpy(temp->hex, sha1_to_hex(null_sha1)); |
| 1617 | else |
| 1618 | strcpy(temp->hex, sha1_to_hex(one->sha1)); |
| 1619 | /* Even though we may sometimes borrow the |
| 1620 | * contents from the work tree, we always want |
| 1621 | * one->mode. mode is trustworthy even when |
| 1622 | * !(one->sha1_valid), as long as |
| 1623 | * DIFF_FILE_VALID(one). |
| 1624 | */ |
| 1625 | sprintf(temp->mode, "%06o", one->mode); |
| 1626 | } |
| 1627 | return; |
| 1628 | } |
| 1629 | else { |
| 1630 | if (diff_populate_filespec(one, 0)) |
| 1631 | die("cannot read data blob for %s", one->path); |
| 1632 | prep_temp_blob(temp, one->data, one->size, |
| 1633 | one->sha1, one->mode); |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | static void remove_tempfile(void) |
| 1638 | { |
| 1639 | int i; |
| 1640 | |
| 1641 | for (i = 0; i < 2; i++) |
| 1642 | if (diff_temp[i].name == diff_temp[i].tmp_path) { |
| 1643 | unlink(diff_temp[i].name); |
| 1644 | diff_temp[i].name = NULL; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | static void remove_tempfile_on_signal(int signo) |
| 1649 | { |
| 1650 | remove_tempfile(); |
| 1651 | signal(SIGINT, SIG_DFL); |
| 1652 | raise(signo); |
| 1653 | } |
| 1654 | |
| 1655 | static int spawn_prog(const char *pgm, const char **arg) |
| 1656 | { |
| 1657 | pid_t pid; |
| 1658 | int status; |
| 1659 | |
| 1660 | fflush(NULL); |
| 1661 | pid = fork(); |
| 1662 | if (pid < 0) |
| 1663 | die("unable to fork"); |
| 1664 | if (!pid) { |
| 1665 | execvp(pgm, (char *const*) arg); |
| 1666 | exit(255); |
| 1667 | } |
| 1668 | |
| 1669 | while (waitpid(pid, &status, 0) < 0) { |
| 1670 | if (errno == EINTR) |
| 1671 | continue; |
| 1672 | return -1; |
| 1673 | } |
| 1674 | |
| 1675 | /* Earlier we did not check the exit status because |
| 1676 | * diff exits non-zero if files are different, and |
| 1677 | * we are not interested in knowing that. It was a |
| 1678 | * mistake which made it harder to quit a diff-* |
| 1679 | * session that uses the git-apply-patch-script as |
| 1680 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF |
| 1681 | * should also exit non-zero only when it wants to |
| 1682 | * abort the entire diff-* session. |
| 1683 | */ |
| 1684 | if (WIFEXITED(status) && !WEXITSTATUS(status)) |
| 1685 | return 0; |
| 1686 | return -1; |
| 1687 | } |
| 1688 | |
| 1689 | /* An external diff command takes: |
| 1690 | * |
| 1691 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ |
| 1692 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
| 1693 | * |
| 1694 | */ |
| 1695 | static void run_external_diff(const char *pgm, |
| 1696 | const char *name, |
| 1697 | const char *other, |
| 1698 | struct diff_filespec *one, |
| 1699 | struct diff_filespec *two, |
| 1700 | const char *xfrm_msg, |
| 1701 | int complete_rewrite) |
| 1702 | { |
| 1703 | const char *spawn_arg[10]; |
| 1704 | struct diff_tempfile *temp = diff_temp; |
| 1705 | int retval; |
| 1706 | static int atexit_asked = 0; |
| 1707 | const char *othername; |
| 1708 | const char **arg = &spawn_arg[0]; |
| 1709 | |
| 1710 | othername = (other? other : name); |
| 1711 | if (one && two) { |
| 1712 | prepare_temp_file(name, &temp[0], one); |
| 1713 | prepare_temp_file(othername, &temp[1], two); |
| 1714 | if (! atexit_asked && |
| 1715 | (temp[0].name == temp[0].tmp_path || |
| 1716 | temp[1].name == temp[1].tmp_path)) { |
| 1717 | atexit_asked = 1; |
| 1718 | atexit(remove_tempfile); |
| 1719 | } |
| 1720 | signal(SIGINT, remove_tempfile_on_signal); |
| 1721 | } |
| 1722 | |
| 1723 | if (one && two) { |
| 1724 | *arg++ = pgm; |
| 1725 | *arg++ = name; |
| 1726 | *arg++ = temp[0].name; |
| 1727 | *arg++ = temp[0].hex; |
| 1728 | *arg++ = temp[0].mode; |
| 1729 | *arg++ = temp[1].name; |
| 1730 | *arg++ = temp[1].hex; |
| 1731 | *arg++ = temp[1].mode; |
| 1732 | if (other) { |
| 1733 | *arg++ = other; |
| 1734 | *arg++ = xfrm_msg; |
| 1735 | } |
| 1736 | } else { |
| 1737 | *arg++ = pgm; |
| 1738 | *arg++ = name; |
| 1739 | } |
| 1740 | *arg = NULL; |
| 1741 | retval = spawn_prog(pgm, spawn_arg); |
| 1742 | remove_tempfile(); |
| 1743 | if (retval) { |
| 1744 | fprintf(stderr, "external diff died, stopping at %s.\n", name); |
| 1745 | exit(1); |
| 1746 | } |
| 1747 | } |
| 1748 | |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 1749 | static const char *external_diff_attr(const char *name) |
| 1750 | { |
| 1751 | struct git_attr_check attr_diff_check; |
| 1752 | |
| 1753 | setup_diff_attr_check(&attr_diff_check); |
| 1754 | if (!git_checkattr(name, 1, &attr_diff_check)) { |
| 1755 | const char *value = attr_diff_check.value; |
| 1756 | if (!ATTR_TRUE(value) && |
| 1757 | !ATTR_FALSE(value) && |
| 1758 | !ATTR_UNSET(value)) { |
| 1759 | struct ll_diff_driver *drv; |
| 1760 | |
| 1761 | if (!user_diff_tail) { |
| 1762 | user_diff_tail = &user_diff; |
| 1763 | git_config(git_diff_ui_config); |
| 1764 | } |
| 1765 | for (drv = user_diff; drv; drv = drv->next) |
| 1766 | if (!strcmp(drv->name, value)) |
| 1767 | return drv->cmd; |
| 1768 | } |
| 1769 | } |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1773 | static void run_diff_cmd(const char *pgm, |
| 1774 | const char *name, |
| 1775 | const char *other, |
| 1776 | struct diff_filespec *one, |
| 1777 | struct diff_filespec *two, |
| 1778 | const char *xfrm_msg, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1779 | struct diff_options *o, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1780 | int complete_rewrite) |
| 1781 | { |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 1782 | if (!o->allow_external) |
| 1783 | pgm = NULL; |
| 1784 | else { |
| 1785 | const char *cmd = external_diff_attr(name); |
| 1786 | if (cmd) |
| 1787 | pgm = cmd; |
| 1788 | } |
| 1789 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1790 | if (pgm) { |
| 1791 | run_external_diff(pgm, name, other, one, two, xfrm_msg, |
| 1792 | complete_rewrite); |
| 1793 | return; |
| 1794 | } |
| 1795 | if (one && two) |
| 1796 | builtin_diff(name, other ? other : name, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1797 | one, two, xfrm_msg, o, complete_rewrite); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1798 | else |
| 1799 | printf("* Unmerged path %s\n", name); |
| 1800 | } |
| 1801 | |
| 1802 | static void diff_fill_sha1_info(struct diff_filespec *one) |
| 1803 | { |
| 1804 | if (DIFF_FILE_VALID(one)) { |
| 1805 | if (!one->sha1_valid) { |
| 1806 | struct stat st; |
Johannes Schindelin | 5332b2a | 2007-02-25 23:36:10 +0100 | [diff] [blame] | 1807 | if (!strcmp(one->path, "-")) { |
| 1808 | hashcpy(one->sha1, null_sha1); |
| 1809 | return; |
| 1810 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1811 | if (lstat(one->path, &st) < 0) |
| 1812 | die("stat %s", one->path); |
| 1813 | if (index_path(one->sha1, one->path, &st, 0)) |
| 1814 | die("cannot hash %s\n", one->path); |
| 1815 | } |
| 1816 | } |
| 1817 | else |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1818 | hashclr(one->sha1); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
| 1822 | { |
| 1823 | const char *pgm = external_diff(); |
| 1824 | char msg[PATH_MAX*2+300], *xfrm_msg; |
| 1825 | struct diff_filespec *one; |
| 1826 | struct diff_filespec *two; |
| 1827 | const char *name; |
| 1828 | const char *other; |
| 1829 | char *name_munged, *other_munged; |
| 1830 | int complete_rewrite = 0; |
| 1831 | int len; |
| 1832 | |
| 1833 | if (DIFF_PAIR_UNMERGED(p)) { |
| 1834 | /* unmerged */ |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1835 | run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1836 | return; |
| 1837 | } |
| 1838 | |
| 1839 | name = p->one->path; |
| 1840 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
| 1841 | name_munged = quote_one(name); |
| 1842 | other_munged = quote_one(other); |
| 1843 | one = p->one; two = p->two; |
| 1844 | |
| 1845 | diff_fill_sha1_info(one); |
| 1846 | diff_fill_sha1_info(two); |
| 1847 | |
| 1848 | len = 0; |
| 1849 | switch (p->status) { |
| 1850 | case DIFF_STATUS_COPIED: |
| 1851 | len += snprintf(msg + len, sizeof(msg) - len, |
| 1852 | "similarity index %d%%\n" |
| 1853 | "copy from %s\n" |
| 1854 | "copy to %s\n", |
| 1855 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
| 1856 | name_munged, other_munged); |
| 1857 | break; |
| 1858 | case DIFF_STATUS_RENAMED: |
| 1859 | len += snprintf(msg + len, sizeof(msg) - len, |
| 1860 | "similarity index %d%%\n" |
| 1861 | "rename from %s\n" |
| 1862 | "rename to %s\n", |
| 1863 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
| 1864 | name_munged, other_munged); |
| 1865 | break; |
| 1866 | case DIFF_STATUS_MODIFIED: |
| 1867 | if (p->score) { |
| 1868 | len += snprintf(msg + len, sizeof(msg) - len, |
| 1869 | "dissimilarity index %d%%\n", |
| 1870 | (int)(0.5 + p->score * |
| 1871 | 100.0/MAX_SCORE)); |
| 1872 | complete_rewrite = 1; |
| 1873 | break; |
| 1874 | } |
| 1875 | /* fallthru */ |
| 1876 | default: |
| 1877 | /* nothing */ |
| 1878 | ; |
| 1879 | } |
| 1880 | |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 1881 | if (hashcmp(one->sha1, two->sha1)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1882 | int abbrev = o->full_index ? 40 : DEFAULT_ABBREV; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1883 | |
Junio C Hamano | 82793c5 | 2006-09-07 00:54:22 -0700 | [diff] [blame] | 1884 | if (o->binary) { |
| 1885 | mmfile_t mf; |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 1886 | if ((!fill_mmfile(&mf, one) && file_is_binary(one)) || |
| 1887 | (!fill_mmfile(&mf, two) && file_is_binary(two))) |
Junio C Hamano | 82793c5 | 2006-09-07 00:54:22 -0700 | [diff] [blame] | 1888 | abbrev = 40; |
| 1889 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1890 | len += snprintf(msg + len, sizeof(msg) - len, |
| 1891 | "index %.*s..%.*s", |
Linus Torvalds | dcb3450 | 2006-05-03 17:21:08 -0700 | [diff] [blame] | 1892 | abbrev, sha1_to_hex(one->sha1), |
| 1893 | abbrev, sha1_to_hex(two->sha1)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1894 | if (one->mode == two->mode) |
| 1895 | len += snprintf(msg + len, sizeof(msg) - len, |
| 1896 | " %06o", one->mode); |
| 1897 | len += snprintf(msg + len, sizeof(msg) - len, "\n"); |
| 1898 | } |
| 1899 | |
| 1900 | if (len) |
| 1901 | msg[--len] = 0; |
| 1902 | xfrm_msg = len ? msg : NULL; |
| 1903 | |
| 1904 | if (!pgm && |
| 1905 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && |
| 1906 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { |
| 1907 | /* a filepair that changes between file and symlink |
| 1908 | * needs to be split into deletion and creation. |
| 1909 | */ |
| 1910 | struct diff_filespec *null = alloc_filespec(two->path); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1911 | run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1912 | free(null); |
| 1913 | null = alloc_filespec(one->path); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1914 | run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1915 | free(null); |
| 1916 | } |
| 1917 | else |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 1918 | run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1919 | complete_rewrite); |
| 1920 | |
| 1921 | free(name_munged); |
| 1922 | free(other_munged); |
| 1923 | } |
| 1924 | |
| 1925 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, |
| 1926 | struct diffstat_t *diffstat) |
| 1927 | { |
| 1928 | const char *name; |
| 1929 | const char *other; |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 1930 | int complete_rewrite = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1931 | |
| 1932 | if (DIFF_PAIR_UNMERGED(p)) { |
| 1933 | /* unmerged */ |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 1934 | builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1935 | return; |
| 1936 | } |
| 1937 | |
| 1938 | name = p->one->path; |
| 1939 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
| 1940 | |
| 1941 | diff_fill_sha1_info(p->one); |
| 1942 | diff_fill_sha1_info(p->two); |
| 1943 | |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 1944 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
| 1945 | complete_rewrite = 1; |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 1946 | builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1949 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
| 1950 | { |
| 1951 | const char *name; |
| 1952 | const char *other; |
| 1953 | |
| 1954 | if (DIFF_PAIR_UNMERGED(p)) { |
| 1955 | /* unmerged */ |
| 1956 | return; |
| 1957 | } |
| 1958 | |
| 1959 | name = p->one->path; |
| 1960 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
| 1961 | |
| 1962 | diff_fill_sha1_info(p->one); |
| 1963 | diff_fill_sha1_info(p->two); |
| 1964 | |
Johannes Schindelin | c5a8c3e | 2007-02-18 17:27:24 +0100 | [diff] [blame] | 1965 | builtin_checkdiff(name, other, p->one, p->two, o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 1966 | } |
| 1967 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1968 | void diff_setup(struct diff_options *options) |
| 1969 | { |
| 1970 | memset(options, 0, sizeof(*options)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1971 | options->line_termination = '\n'; |
| 1972 | options->break_opt = -1; |
| 1973 | options->rename_limit = -1; |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 1974 | options->context = 3; |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 1975 | options->msg_sep = ""; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1976 | |
| 1977 | options->change = diff_change; |
| 1978 | options->add_remove = diff_addremove; |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 1979 | options->color_diff = diff_use_color_default; |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 1980 | options->detect_rename = diff_detect_rename_default; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | int diff_setup_done(struct diff_options *options) |
| 1984 | { |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 1985 | int count = 0; |
| 1986 | |
| 1987 | if (options->output_format & DIFF_FORMAT_NAME) |
| 1988 | count++; |
| 1989 | if (options->output_format & DIFF_FORMAT_NAME_STATUS) |
| 1990 | count++; |
| 1991 | if (options->output_format & DIFF_FORMAT_CHECKDIFF) |
| 1992 | count++; |
| 1993 | if (options->output_format & DIFF_FORMAT_NO_OUTPUT) |
| 1994 | count++; |
| 1995 | if (count > 1) |
| 1996 | die("--name-only, --name-status, --check and -s are mutually exclusive"); |
| 1997 | |
Junio C Hamano | 03b9d56 | 2006-08-09 13:17:19 -0700 | [diff] [blame] | 1998 | if (options->find_copies_harder) |
| 1999 | options->detect_rename = DIFF_DETECT_COPY; |
| 2000 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2001 | if (options->output_format & (DIFF_FORMAT_NAME | |
| 2002 | DIFF_FORMAT_NAME_STATUS | |
| 2003 | DIFF_FORMAT_CHECKDIFF | |
| 2004 | DIFF_FORMAT_NO_OUTPUT)) |
| 2005 | options->output_format &= ~(DIFF_FORMAT_RAW | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2006 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2007 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2008 | DIFF_FORMAT_SHORTSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2009 | DIFF_FORMAT_SUMMARY | |
| 2010 | DIFF_FORMAT_PATCH); |
| 2011 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2012 | /* |
| 2013 | * These cases always need recursive; we do not drop caller-supplied |
| 2014 | * recursive bits for other formats here. |
| 2015 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2016 | if (options->output_format & (DIFF_FORMAT_PATCH | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2017 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2018 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2019 | DIFF_FORMAT_SHORTSTAT | |
Johannes Schindelin | d7014dc | 2006-10-03 23:09:56 +0200 | [diff] [blame] | 2020 | DIFF_FORMAT_SUMMARY | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2021 | DIFF_FORMAT_CHECKDIFF)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2022 | options->recursive = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 2023 | /* |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 2024 | * Also pickaxe would not work very well if you do not say recursive |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 2025 | */ |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 2026 | if (options->pickaxe) |
| 2027 | options->recursive = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 2028 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2029 | if (options->detect_rename && options->rename_limit < 0) |
| 2030 | options->rename_limit = diff_rename_limit_default; |
| 2031 | if (options->setup & DIFF_SETUP_USE_CACHE) { |
| 2032 | if (!active_cache) |
| 2033 | /* read-cache does not die even when it fails |
| 2034 | * so it is safe for us to do this here. Also |
| 2035 | * it does not smudge active_cache or active_nr |
| 2036 | * when it fails, so we do not have to worry about |
| 2037 | * cleaning it up ourselves either. |
| 2038 | */ |
| 2039 | read_cache(); |
| 2040 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2041 | if (options->abbrev <= 0 || 40 < options->abbrev) |
| 2042 | options->abbrev = 40; /* full */ |
| 2043 | |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 2044 | /* |
| 2045 | * It does not make sense to show the first hit we happened |
| 2046 | * to have found. It does not make sense not to return with |
| 2047 | * exit code in such a case either. |
| 2048 | */ |
| 2049 | if (options->quiet) { |
| 2050 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
| 2051 | options->exit_with_status = 1; |
| 2052 | } |
| 2053 | |
| 2054 | /* |
| 2055 | * If we postprocess in diffcore, we cannot simply return |
| 2056 | * upon the first hit. We need to run diff as usual. |
| 2057 | */ |
| 2058 | if (options->pickaxe || options->filter) |
| 2059 | options->quiet = 0; |
| 2060 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2061 | return 0; |
| 2062 | } |
| 2063 | |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 2064 | static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val) |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 2065 | { |
| 2066 | char c, *eq; |
| 2067 | int len; |
| 2068 | |
| 2069 | if (*arg != '-') |
| 2070 | return 0; |
| 2071 | c = *++arg; |
| 2072 | if (!c) |
| 2073 | return 0; |
| 2074 | if (c == arg_short) { |
| 2075 | c = *++arg; |
| 2076 | if (!c) |
| 2077 | return 1; |
| 2078 | if (val && isdigit(c)) { |
| 2079 | char *end; |
| 2080 | int n = strtoul(arg, &end, 10); |
| 2081 | if (*end) |
| 2082 | return 0; |
| 2083 | *val = n; |
| 2084 | return 1; |
| 2085 | } |
| 2086 | return 0; |
| 2087 | } |
| 2088 | if (c != '-') |
| 2089 | return 0; |
| 2090 | arg++; |
| 2091 | eq = strchr(arg, '='); |
| 2092 | if (eq) |
| 2093 | len = eq - arg; |
| 2094 | else |
| 2095 | len = strlen(arg); |
| 2096 | if (!len || strncmp(arg, arg_long, len)) |
| 2097 | return 0; |
| 2098 | if (eq) { |
| 2099 | int n; |
| 2100 | char *end; |
| 2101 | if (!isdigit(*++eq)) |
| 2102 | return 0; |
| 2103 | n = strtoul(eq, &end, 10); |
| 2104 | if (*end) |
| 2105 | return 0; |
| 2106 | *val = n; |
| 2107 | } |
| 2108 | return 1; |
| 2109 | } |
| 2110 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2111 | int diff_opt_parse(struct diff_options *options, const char **av, int ac) |
| 2112 | { |
| 2113 | const char *arg = av[0]; |
| 2114 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2115 | options->output_format |= DIFF_FORMAT_PATCH; |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 2116 | else if (opt_arg(arg, 'U', "unified", &options->context)) |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2117 | options->output_format |= DIFF_FORMAT_PATCH; |
Timo Hirvonen | a610786 | 2006-06-24 20:23:06 +0300 | [diff] [blame] | 2118 | else if (!strcmp(arg, "--raw")) |
| 2119 | options->output_format |= DIFF_FORMAT_RAW; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2120 | else if (!strcmp(arg, "--patch-with-raw")) { |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2121 | options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2122 | } |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2123 | else if (!strcmp(arg, "--numstat")) { |
| 2124 | options->output_format |= DIFF_FORMAT_NUMSTAT; |
| 2125 | } |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2126 | else if (!strcmp(arg, "--shortstat")) { |
| 2127 | options->output_format |= DIFF_FORMAT_SHORTSTAT; |
| 2128 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2129 | else if (!prefixcmp(arg, "--stat")) { |
Linus Torvalds | 5c5b2ea | 2006-09-28 15:07:16 -0700 | [diff] [blame] | 2130 | char *end; |
| 2131 | int width = options->stat_width; |
| 2132 | int name_width = options->stat_name_width; |
| 2133 | arg += 6; |
| 2134 | end = (char *)arg; |
| 2135 | |
| 2136 | switch (*arg) { |
| 2137 | case '-': |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2138 | if (!prefixcmp(arg, "-width=")) |
Linus Torvalds | 5c5b2ea | 2006-09-28 15:07:16 -0700 | [diff] [blame] | 2139 | width = strtoul(arg + 7, &end, 10); |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2140 | else if (!prefixcmp(arg, "-name-width=")) |
Linus Torvalds | 5c5b2ea | 2006-09-28 15:07:16 -0700 | [diff] [blame] | 2141 | name_width = strtoul(arg + 12, &end, 10); |
| 2142 | break; |
| 2143 | case '=': |
| 2144 | width = strtoul(arg+1, &end, 10); |
| 2145 | if (*end == ',') |
| 2146 | name_width = strtoul(end+1, &end, 10); |
| 2147 | } |
| 2148 | |
| 2149 | /* Important! This checks all the error cases! */ |
| 2150 | if (*end) |
| 2151 | return 0; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2152 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
Linus Torvalds | 5c5b2ea | 2006-09-28 15:07:16 -0700 | [diff] [blame] | 2153 | options->stat_name_width = name_width; |
| 2154 | options->stat_width = width; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2155 | } |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 2156 | else if (!strcmp(arg, "--check")) |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2157 | options->output_format |= DIFF_FORMAT_CHECKDIFF; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2158 | else if (!strcmp(arg, "--summary")) |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2159 | options->output_format |= DIFF_FORMAT_SUMMARY; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2160 | else if (!strcmp(arg, "--patch-with-stat")) { |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2161 | options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2162 | } |
| 2163 | else if (!strcmp(arg, "-z")) |
| 2164 | options->line_termination = 0; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2165 | else if (!prefixcmp(arg, "-l")) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2166 | options->rename_limit = strtoul(arg+2, NULL, 10); |
| 2167 | else if (!strcmp(arg, "--full-index")) |
| 2168 | options->full_index = 1; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 2169 | else if (!strcmp(arg, "--binary")) { |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2170 | options->output_format |= DIFF_FORMAT_PATCH; |
Junio C Hamano | 82793c5 | 2006-09-07 00:54:22 -0700 | [diff] [blame] | 2171 | options->binary = 1; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 2172 | } |
Stephan Feder | 63ac450 | 2006-07-07 15:57:07 +0200 | [diff] [blame] | 2173 | else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) { |
Stephan Feder | 6d64ea9 | 2006-07-07 12:33:57 +0200 | [diff] [blame] | 2174 | options->text = 1; |
| 2175 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2176 | else if (!strcmp(arg, "--name-only")) |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2177 | options->output_format |= DIFF_FORMAT_NAME; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2178 | else if (!strcmp(arg, "--name-status")) |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2179 | options->output_format |= DIFF_FORMAT_NAME_STATUS; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2180 | else if (!strcmp(arg, "-R")) |
| 2181 | options->reverse_diff = 1; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2182 | else if (!prefixcmp(arg, "-S")) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2183 | options->pickaxe = arg + 2; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2184 | else if (!strcmp(arg, "-s")) { |
| 2185 | options->output_format |= DIFF_FORMAT_NO_OUTPUT; |
| 2186 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2187 | else if (!prefixcmp(arg, "-O")) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2188 | options->orderfile = arg + 2; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2189 | else if (!prefixcmp(arg, "--diff-filter=")) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2190 | options->filter = arg + 14; |
| 2191 | else if (!strcmp(arg, "--pickaxe-all")) |
| 2192 | options->pickaxe_opts = DIFF_PICKAXE_ALL; |
| 2193 | else if (!strcmp(arg, "--pickaxe-regex")) |
| 2194 | options->pickaxe_opts = DIFF_PICKAXE_REGEX; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2195 | else if (!prefixcmp(arg, "-B")) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2196 | if ((options->break_opt = |
| 2197 | diff_scoreopt_parse(arg)) == -1) |
| 2198 | return -1; |
| 2199 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2200 | else if (!prefixcmp(arg, "-M")) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2201 | if ((options->rename_score = |
| 2202 | diff_scoreopt_parse(arg)) == -1) |
| 2203 | return -1; |
| 2204 | options->detect_rename = DIFF_DETECT_RENAME; |
| 2205 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2206 | else if (!prefixcmp(arg, "-C")) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2207 | if ((options->rename_score = |
| 2208 | diff_scoreopt_parse(arg)) == -1) |
| 2209 | return -1; |
| 2210 | options->detect_rename = DIFF_DETECT_COPY; |
| 2211 | } |
| 2212 | else if (!strcmp(arg, "--find-copies-harder")) |
| 2213 | options->find_copies_harder = 1; |
| 2214 | else if (!strcmp(arg, "--abbrev")) |
| 2215 | options->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2216 | else if (!prefixcmp(arg, "--abbrev=")) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2217 | options->abbrev = strtoul(arg + 9, NULL, 10); |
| 2218 | if (options->abbrev < MINIMUM_ABBREV) |
| 2219 | options->abbrev = MINIMUM_ABBREV; |
| 2220 | else if (40 < options->abbrev) |
| 2221 | options->abbrev = 40; |
| 2222 | } |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2223 | else if (!strcmp(arg, "--color")) |
| 2224 | options->color_diff = 1; |
Junio C Hamano | fef88bb | 2006-07-07 05:27:24 -0700 | [diff] [blame] | 2225 | else if (!strcmp(arg, "--no-color")) |
| 2226 | options->color_diff = 0; |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 2227 | else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space")) |
| 2228 | options->xdl_opts |= XDF_IGNORE_WHITESPACE; |
| 2229 | else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change")) |
| 2230 | options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE; |
Johannes Schindelin | 859f9c4 | 2007-02-14 01:30:29 +0100 | [diff] [blame] | 2231 | else if (!strcmp(arg, "--ignore-space-at-eol")) |
| 2232 | options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2233 | else if (!strcmp(arg, "--color-words")) |
| 2234 | options->color_diff = options->color_diff_words = 1; |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 2235 | else if (!strcmp(arg, "--no-renames")) |
| 2236 | options->detect_rename = 0; |
Alex Riesen | 41bbf9d | 2007-03-14 01:17:04 +0100 | [diff] [blame] | 2237 | else if (!strcmp(arg, "--exit-code")) |
| 2238 | options->exit_with_status = 1; |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 2239 | else if (!strcmp(arg, "--quiet")) |
| 2240 | options->quiet = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2241 | else |
| 2242 | return 0; |
| 2243 | return 1; |
| 2244 | } |
| 2245 | |
| 2246 | static int parse_num(const char **cp_p) |
| 2247 | { |
| 2248 | unsigned long num, scale; |
| 2249 | int ch, dot; |
| 2250 | const char *cp = *cp_p; |
| 2251 | |
| 2252 | num = 0; |
| 2253 | scale = 1; |
| 2254 | dot = 0; |
| 2255 | for(;;) { |
| 2256 | ch = *cp; |
| 2257 | if ( !dot && ch == '.' ) { |
| 2258 | scale = 1; |
| 2259 | dot = 1; |
| 2260 | } else if ( ch == '%' ) { |
| 2261 | scale = dot ? scale*100 : 100; |
| 2262 | cp++; /* % is always at the end */ |
| 2263 | break; |
| 2264 | } else if ( ch >= '0' && ch <= '9' ) { |
| 2265 | if ( scale < 100000 ) { |
| 2266 | scale *= 10; |
| 2267 | num = (num*10) + (ch-'0'); |
| 2268 | } |
| 2269 | } else { |
| 2270 | break; |
| 2271 | } |
| 2272 | cp++; |
| 2273 | } |
| 2274 | *cp_p = cp; |
| 2275 | |
| 2276 | /* user says num divided by scale and we say internally that |
| 2277 | * is MAX_SCORE * num / scale. |
| 2278 | */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 2279 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | int diff_scoreopt_parse(const char *opt) |
| 2283 | { |
| 2284 | int opt1, opt2, cmd; |
| 2285 | |
| 2286 | if (*opt++ != '-') |
| 2287 | return -1; |
| 2288 | cmd = *opt++; |
| 2289 | if (cmd != 'M' && cmd != 'C' && cmd != 'B') |
| 2290 | return -1; /* that is not a -M, -C nor -B option */ |
| 2291 | |
| 2292 | opt1 = parse_num(&opt); |
| 2293 | if (cmd != 'B') |
| 2294 | opt2 = 0; |
| 2295 | else { |
| 2296 | if (*opt == 0) |
| 2297 | opt2 = 0; |
| 2298 | else if (*opt != '/') |
| 2299 | return -1; /* we expect -B80/99 or -B80 */ |
| 2300 | else { |
| 2301 | opt++; |
| 2302 | opt2 = parse_num(&opt); |
| 2303 | } |
| 2304 | } |
| 2305 | if (*opt != 0) |
| 2306 | return -1; |
| 2307 | return opt1 | (opt2 << 16); |
| 2308 | } |
| 2309 | |
| 2310 | struct diff_queue_struct diff_queued_diff; |
| 2311 | |
| 2312 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) |
| 2313 | { |
| 2314 | if (queue->alloc <= queue->nr) { |
| 2315 | queue->alloc = alloc_nr(queue->alloc); |
| 2316 | queue->queue = xrealloc(queue->queue, |
| 2317 | sizeof(dp) * queue->alloc); |
| 2318 | } |
| 2319 | queue->queue[queue->nr++] = dp; |
| 2320 | } |
| 2321 | |
| 2322 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
| 2323 | struct diff_filespec *one, |
| 2324 | struct diff_filespec *two) |
| 2325 | { |
Junio C Hamano | ef67768 | 2006-08-03 12:01:01 -0700 | [diff] [blame] | 2326 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2327 | dp->one = one; |
| 2328 | dp->two = two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2329 | if (queue) |
| 2330 | diff_q(queue, dp); |
| 2331 | return dp; |
| 2332 | } |
| 2333 | |
| 2334 | void diff_free_filepair(struct diff_filepair *p) |
| 2335 | { |
| 2336 | diff_free_filespec_data(p->one); |
| 2337 | diff_free_filespec_data(p->two); |
| 2338 | free(p->one); |
| 2339 | free(p->two); |
| 2340 | free(p); |
| 2341 | } |
| 2342 | |
| 2343 | /* This is different from find_unique_abbrev() in that |
| 2344 | * it stuffs the result with dots for alignment. |
| 2345 | */ |
| 2346 | const char *diff_unique_abbrev(const unsigned char *sha1, int len) |
| 2347 | { |
| 2348 | int abblen; |
| 2349 | const char *abbrev; |
| 2350 | if (len == 40) |
| 2351 | return sha1_to_hex(sha1); |
| 2352 | |
| 2353 | abbrev = find_unique_abbrev(sha1, len); |
| 2354 | if (!abbrev) |
| 2355 | return sha1_to_hex(sha1); |
| 2356 | abblen = strlen(abbrev); |
| 2357 | if (abblen < 37) { |
| 2358 | static char hex[41]; |
| 2359 | if (len < abblen && abblen <= len + 2) |
| 2360 | sprintf(hex, "%s%.*s", abbrev, len+3-abblen, ".."); |
| 2361 | else |
| 2362 | sprintf(hex, "%s...", abbrev); |
| 2363 | return hex; |
| 2364 | } |
| 2365 | return sha1_to_hex(sha1); |
| 2366 | } |
| 2367 | |
| 2368 | static void diff_flush_raw(struct diff_filepair *p, |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2369 | struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2370 | { |
| 2371 | int two_paths; |
| 2372 | char status[10]; |
| 2373 | int abbrev = options->abbrev; |
| 2374 | const char *path_one, *path_two; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2375 | int inter_name_termination = '\t'; |
| 2376 | int line_termination = options->line_termination; |
| 2377 | |
| 2378 | if (!line_termination) |
| 2379 | inter_name_termination = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2380 | |
| 2381 | path_one = p->one->path; |
| 2382 | path_two = p->two->path; |
| 2383 | if (line_termination) { |
| 2384 | path_one = quote_one(path_one); |
| 2385 | path_two = quote_one(path_two); |
| 2386 | } |
| 2387 | |
| 2388 | if (p->score) |
| 2389 | sprintf(status, "%c%03d", p->status, |
| 2390 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); |
| 2391 | else { |
| 2392 | status[0] = p->status; |
| 2393 | status[1] = 0; |
| 2394 | } |
| 2395 | switch (p->status) { |
| 2396 | case DIFF_STATUS_COPIED: |
| 2397 | case DIFF_STATUS_RENAMED: |
| 2398 | two_paths = 1; |
| 2399 | break; |
| 2400 | case DIFF_STATUS_ADDED: |
| 2401 | case DIFF_STATUS_DELETED: |
| 2402 | two_paths = 0; |
| 2403 | break; |
| 2404 | default: |
| 2405 | two_paths = 0; |
| 2406 | break; |
| 2407 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2408 | if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2409 | printf(":%06o %06o %s ", |
| 2410 | p->one->mode, p->two->mode, |
| 2411 | diff_unique_abbrev(p->one->sha1, abbrev)); |
| 2412 | printf("%s ", |
| 2413 | diff_unique_abbrev(p->two->sha1, abbrev)); |
| 2414 | } |
| 2415 | printf("%s%c%s", status, inter_name_termination, path_one); |
| 2416 | if (two_paths) |
| 2417 | printf("%c%s", inter_name_termination, path_two); |
| 2418 | putchar(line_termination); |
| 2419 | if (path_one != p->one->path) |
| 2420 | free((void*)path_one); |
| 2421 | if (path_two != p->two->path) |
| 2422 | free((void*)path_two); |
| 2423 | } |
| 2424 | |
Junio C Hamano | 471efb09 | 2007-02-09 22:18:19 -0800 | [diff] [blame] | 2425 | static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2426 | { |
| 2427 | char *path = p->two->path; |
| 2428 | |
Junio C Hamano | 471efb09 | 2007-02-09 22:18:19 -0800 | [diff] [blame] | 2429 | if (opt->line_termination) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2430 | path = quote_one(p->two->path); |
Junio C Hamano | 471efb09 | 2007-02-09 22:18:19 -0800 | [diff] [blame] | 2431 | printf("%s%c", path, opt->line_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2432 | if (p->two->path != path) |
| 2433 | free(path); |
| 2434 | } |
| 2435 | |
| 2436 | int diff_unmodified_pair(struct diff_filepair *p) |
| 2437 | { |
| 2438 | /* This function is written stricter than necessary to support |
| 2439 | * the currently implemented transformers, but the idea is to |
| 2440 | * let transformers to produce diff_filepairs any way they want, |
| 2441 | * and filter and clean them up here before producing the output. |
| 2442 | */ |
| 2443 | struct diff_filespec *one, *two; |
| 2444 | |
| 2445 | if (DIFF_PAIR_UNMERGED(p)) |
| 2446 | return 0; /* unmerged is interesting */ |
| 2447 | |
| 2448 | one = p->one; |
| 2449 | two = p->two; |
| 2450 | |
| 2451 | /* deletion, addition, mode or type change |
| 2452 | * and rename are all interesting. |
| 2453 | */ |
| 2454 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
| 2455 | DIFF_PAIR_MODE_CHANGED(p) || |
| 2456 | strcmp(one->path, two->path)) |
| 2457 | return 0; |
| 2458 | |
| 2459 | /* both are valid and point at the same path. that is, we are |
| 2460 | * dealing with a change. |
| 2461 | */ |
| 2462 | if (one->sha1_valid && two->sha1_valid && |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 2463 | !hashcmp(one->sha1, two->sha1)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2464 | return 1; /* no change */ |
| 2465 | if (!one->sha1_valid && !two->sha1_valid) |
| 2466 | return 1; /* both look at the same file on the filesystem. */ |
| 2467 | return 0; |
| 2468 | } |
| 2469 | |
| 2470 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) |
| 2471 | { |
| 2472 | if (diff_unmodified_pair(p)) |
| 2473 | return; |
| 2474 | |
| 2475 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 2476 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 2477 | return; /* no tree diffs in patch format */ |
| 2478 | |
| 2479 | run_diff(p, o); |
| 2480 | } |
| 2481 | |
| 2482 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, |
| 2483 | struct diffstat_t *diffstat) |
| 2484 | { |
| 2485 | if (diff_unmodified_pair(p)) |
| 2486 | return; |
| 2487 | |
| 2488 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 2489 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 2490 | return; /* no tree diffs in patch format */ |
| 2491 | |
| 2492 | run_diffstat(p, o, diffstat); |
| 2493 | } |
| 2494 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 2495 | static void diff_flush_checkdiff(struct diff_filepair *p, |
| 2496 | struct diff_options *o) |
| 2497 | { |
| 2498 | if (diff_unmodified_pair(p)) |
| 2499 | return; |
| 2500 | |
| 2501 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 2502 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 2503 | return; /* no tree diffs in patch format */ |
| 2504 | |
| 2505 | run_checkdiff(p, o); |
| 2506 | } |
| 2507 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2508 | int diff_queue_is_empty(void) |
| 2509 | { |
| 2510 | struct diff_queue_struct *q = &diff_queued_diff; |
| 2511 | int i; |
| 2512 | for (i = 0; i < q->nr; i++) |
| 2513 | if (!diff_unmodified_pair(q->queue[i])) |
| 2514 | return 0; |
| 2515 | return 1; |
| 2516 | } |
| 2517 | |
| 2518 | #if DIFF_DEBUG |
| 2519 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) |
| 2520 | { |
| 2521 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", |
| 2522 | x, one ? one : "", |
| 2523 | s->path, |
| 2524 | DIFF_FILE_VALID(s) ? "valid" : "invalid", |
| 2525 | s->mode, |
| 2526 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); |
| 2527 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", |
| 2528 | x, one ? one : "", |
| 2529 | s->size, s->xfrm_flags); |
| 2530 | } |
| 2531 | |
| 2532 | void diff_debug_filepair(const struct diff_filepair *p, int i) |
| 2533 | { |
| 2534 | diff_debug_filespec(p->one, i, "one"); |
| 2535 | diff_debug_filespec(p->two, i, "two"); |
| 2536 | fprintf(stderr, "score %d, status %c stays %d broken %d\n", |
| 2537 | p->score, p->status ? p->status : '?', |
| 2538 | p->source_stays, p->broken_pair); |
| 2539 | } |
| 2540 | |
| 2541 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) |
| 2542 | { |
| 2543 | int i; |
| 2544 | if (msg) |
| 2545 | fprintf(stderr, "%s\n", msg); |
| 2546 | fprintf(stderr, "q->nr = %d\n", q->nr); |
| 2547 | for (i = 0; i < q->nr; i++) { |
| 2548 | struct diff_filepair *p = q->queue[i]; |
| 2549 | diff_debug_filepair(p, i); |
| 2550 | } |
| 2551 | } |
| 2552 | #endif |
| 2553 | |
| 2554 | static void diff_resolve_rename_copy(void) |
| 2555 | { |
| 2556 | int i, j; |
| 2557 | struct diff_filepair *p, *pp; |
| 2558 | struct diff_queue_struct *q = &diff_queued_diff; |
| 2559 | |
| 2560 | diff_debug_queue("resolve-rename-copy", q); |
| 2561 | |
| 2562 | for (i = 0; i < q->nr; i++) { |
| 2563 | p = q->queue[i]; |
| 2564 | p->status = 0; /* undecided */ |
| 2565 | if (DIFF_PAIR_UNMERGED(p)) |
| 2566 | p->status = DIFF_STATUS_UNMERGED; |
| 2567 | else if (!DIFF_FILE_VALID(p->one)) |
| 2568 | p->status = DIFF_STATUS_ADDED; |
| 2569 | else if (!DIFF_FILE_VALID(p->two)) |
| 2570 | p->status = DIFF_STATUS_DELETED; |
| 2571 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
| 2572 | p->status = DIFF_STATUS_TYPE_CHANGED; |
| 2573 | |
| 2574 | /* from this point on, we are dealing with a pair |
| 2575 | * whose both sides are valid and of the same type, i.e. |
| 2576 | * either in-place edit or rename/copy edit. |
| 2577 | */ |
| 2578 | else if (DIFF_PAIR_RENAME(p)) { |
| 2579 | if (p->source_stays) { |
| 2580 | p->status = DIFF_STATUS_COPIED; |
| 2581 | continue; |
| 2582 | } |
| 2583 | /* See if there is some other filepair that |
| 2584 | * copies from the same source as us. If so |
| 2585 | * we are a copy. Otherwise we are either a |
| 2586 | * copy if the path stays, or a rename if it |
| 2587 | * does not, but we already handled "stays" case. |
| 2588 | */ |
| 2589 | for (j = i + 1; j < q->nr; j++) { |
| 2590 | pp = q->queue[j]; |
| 2591 | if (strcmp(pp->one->path, p->one->path)) |
| 2592 | continue; /* not us */ |
| 2593 | if (!DIFF_PAIR_RENAME(pp)) |
| 2594 | continue; /* not a rename/copy */ |
| 2595 | /* pp is a rename/copy from the same source */ |
| 2596 | p->status = DIFF_STATUS_COPIED; |
| 2597 | break; |
| 2598 | } |
| 2599 | if (!p->status) |
| 2600 | p->status = DIFF_STATUS_RENAMED; |
| 2601 | } |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 2602 | else if (hashcmp(p->one->sha1, p->two->sha1) || |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 2603 | p->one->mode != p->two->mode || |
| 2604 | is_null_sha1(p->one->sha1)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2605 | p->status = DIFF_STATUS_MODIFIED; |
| 2606 | else { |
| 2607 | /* This is a "no-change" entry and should not |
| 2608 | * happen anymore, but prepare for broken callers. |
| 2609 | */ |
| 2610 | error("feeding unmodified %s to diffcore", |
| 2611 | p->one->path); |
| 2612 | p->status = DIFF_STATUS_UNKNOWN; |
| 2613 | } |
| 2614 | } |
| 2615 | diff_debug_queue("resolve-rename-copy done", q); |
| 2616 | } |
| 2617 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2618 | static int check_pair_status(struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2619 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2620 | switch (p->status) { |
| 2621 | case DIFF_STATUS_UNKNOWN: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2622 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2623 | case 0: |
| 2624 | die("internal error in diff-resolve-rename-copy"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2625 | default: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2626 | return 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2630 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
| 2631 | { |
| 2632 | int fmt = opt->output_format; |
| 2633 | |
| 2634 | if (fmt & DIFF_FORMAT_CHECKDIFF) |
| 2635 | diff_flush_checkdiff(p, opt); |
| 2636 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) |
| 2637 | diff_flush_raw(p, opt); |
| 2638 | else if (fmt & DIFF_FORMAT_NAME) |
Junio C Hamano | 471efb09 | 2007-02-09 22:18:19 -0800 | [diff] [blame] | 2639 | diff_flush_name(p, opt); |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2640 | } |
| 2641 | |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2642 | static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs) |
| 2643 | { |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 2644 | char *name = quote_one(fs->path); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2645 | if (fs->mode) |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 2646 | printf(" %s mode %06o %s\n", newdelete, fs->mode, name); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2647 | else |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 2648 | printf(" %s %s\n", newdelete, name); |
| 2649 | free(name); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | |
| 2653 | static void show_mode_change(struct diff_filepair *p, int show_name) |
| 2654 | { |
| 2655 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 2656 | if (show_name) { |
| 2657 | char *name = quote_one(p->two->path); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2658 | printf(" mode change %06o => %06o %s\n", |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 2659 | p->one->mode, p->two->mode, name); |
| 2660 | free(name); |
| 2661 | } |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2662 | else |
| 2663 | printf(" mode change %06o => %06o\n", |
| 2664 | p->one->mode, p->two->mode); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | static void show_rename_copy(const char *renamecopy, struct diff_filepair *p) |
| 2669 | { |
Alexandre Julliard | b9f4416 | 2007-02-10 15:36:47 +0100 | [diff] [blame] | 2670 | char *names = pprint_rename(p->one->path, p->two->path); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2671 | |
Alexandre Julliard | b9f4416 | 2007-02-10 15:36:47 +0100 | [diff] [blame] | 2672 | printf(" %s %s (%d%%)\n", renamecopy, names, |
| 2673 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); |
| 2674 | free(names); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2675 | show_mode_change(p, 0); |
| 2676 | } |
| 2677 | |
| 2678 | static void diff_summary(struct diff_filepair *p) |
| 2679 | { |
| 2680 | switch(p->status) { |
| 2681 | case DIFF_STATUS_DELETED: |
| 2682 | show_file_mode_name("delete", p->one); |
| 2683 | break; |
| 2684 | case DIFF_STATUS_ADDED: |
| 2685 | show_file_mode_name("create", p->two); |
| 2686 | break; |
| 2687 | case DIFF_STATUS_COPIED: |
| 2688 | show_rename_copy("copy", p); |
| 2689 | break; |
| 2690 | case DIFF_STATUS_RENAMED: |
| 2691 | show_rename_copy("rename", p); |
| 2692 | break; |
| 2693 | default: |
| 2694 | if (p->score) { |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 2695 | char *name = quote_one(p->two->path); |
| 2696 | printf(" rewrite %s (%d%%)\n", name, |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2697 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 2698 | free(name); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2699 | show_mode_change(p, 0); |
| 2700 | } else show_mode_change(p, 1); |
| 2701 | break; |
| 2702 | } |
| 2703 | } |
| 2704 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 2705 | struct patch_id_t { |
| 2706 | struct xdiff_emit_state xm; |
| 2707 | SHA_CTX *ctx; |
| 2708 | int patchlen; |
| 2709 | }; |
| 2710 | |
| 2711 | static int remove_space(char *line, int len) |
| 2712 | { |
| 2713 | int i; |
| 2714 | char *dst = line; |
| 2715 | unsigned char c; |
| 2716 | |
| 2717 | for (i = 0; i < len; i++) |
| 2718 | if (!isspace((c = line[i]))) |
| 2719 | *dst++ = c; |
| 2720 | |
| 2721 | return dst - line; |
| 2722 | } |
| 2723 | |
| 2724 | static void patch_id_consume(void *priv, char *line, unsigned long len) |
| 2725 | { |
| 2726 | struct patch_id_t *data = priv; |
| 2727 | int new_len; |
| 2728 | |
| 2729 | /* Ignore line numbers when computing the SHA1 of the patch */ |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 2730 | if (!prefixcmp(line, "@@ -")) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 2731 | return; |
| 2732 | |
| 2733 | new_len = remove_space(line, len); |
| 2734 | |
| 2735 | SHA1_Update(data->ctx, line, new_len); |
| 2736 | data->patchlen += new_len; |
| 2737 | } |
| 2738 | |
| 2739 | /* returns 0 upon success, and writes result into sha1 */ |
| 2740 | static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1) |
| 2741 | { |
| 2742 | struct diff_queue_struct *q = &diff_queued_diff; |
| 2743 | int i; |
| 2744 | SHA_CTX ctx; |
| 2745 | struct patch_id_t data; |
| 2746 | char buffer[PATH_MAX * 4 + 20]; |
| 2747 | |
| 2748 | SHA1_Init(&ctx); |
| 2749 | memset(&data, 0, sizeof(struct patch_id_t)); |
| 2750 | data.ctx = &ctx; |
| 2751 | data.xm.consume = patch_id_consume; |
| 2752 | |
| 2753 | for (i = 0; i < q->nr; i++) { |
| 2754 | xpparam_t xpp; |
| 2755 | xdemitconf_t xecfg; |
| 2756 | xdemitcb_t ecb; |
| 2757 | mmfile_t mf1, mf2; |
| 2758 | struct diff_filepair *p = q->queue[i]; |
| 2759 | int len1, len2; |
| 2760 | |
| 2761 | if (p->status == 0) |
| 2762 | return error("internal diff status error"); |
| 2763 | if (p->status == DIFF_STATUS_UNKNOWN) |
| 2764 | continue; |
| 2765 | if (diff_unmodified_pair(p)) |
| 2766 | continue; |
| 2767 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 2768 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 2769 | continue; |
| 2770 | if (DIFF_PAIR_UNMERGED(p)) |
| 2771 | continue; |
| 2772 | |
| 2773 | diff_fill_sha1_info(p->one); |
| 2774 | diff_fill_sha1_info(p->two); |
| 2775 | if (fill_mmfile(&mf1, p->one) < 0 || |
| 2776 | fill_mmfile(&mf2, p->two) < 0) |
| 2777 | return error("unable to read files to diff"); |
| 2778 | |
| 2779 | /* Maybe hash p->two? into the patch id? */ |
Junio C Hamano | 8c70124 | 2007-04-12 23:05:29 -0700 | [diff] [blame] | 2780 | if (file_is_binary(p->two)) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 2781 | continue; |
| 2782 | |
| 2783 | len1 = remove_space(p->one->path, strlen(p->one->path)); |
| 2784 | len2 = remove_space(p->two->path, strlen(p->two->path)); |
| 2785 | if (p->one->mode == 0) |
| 2786 | len1 = snprintf(buffer, sizeof(buffer), |
| 2787 | "diff--gita/%.*sb/%.*s" |
| 2788 | "newfilemode%06o" |
| 2789 | "---/dev/null" |
| 2790 | "+++b/%.*s", |
| 2791 | len1, p->one->path, |
| 2792 | len2, p->two->path, |
| 2793 | p->two->mode, |
| 2794 | len2, p->two->path); |
| 2795 | else if (p->two->mode == 0) |
| 2796 | len1 = snprintf(buffer, sizeof(buffer), |
| 2797 | "diff--gita/%.*sb/%.*s" |
| 2798 | "deletedfilemode%06o" |
| 2799 | "---a/%.*s" |
| 2800 | "+++/dev/null", |
| 2801 | len1, p->one->path, |
| 2802 | len2, p->two->path, |
| 2803 | p->one->mode, |
| 2804 | len1, p->one->path); |
| 2805 | else |
| 2806 | len1 = snprintf(buffer, sizeof(buffer), |
| 2807 | "diff--gita/%.*sb/%.*s" |
| 2808 | "---a/%.*s" |
| 2809 | "+++b/%.*s", |
| 2810 | len1, p->one->path, |
| 2811 | len2, p->two->path, |
| 2812 | len1, p->one->path, |
| 2813 | len2, p->two->path); |
| 2814 | SHA1_Update(&ctx, buffer, len1); |
| 2815 | |
| 2816 | xpp.flags = XDF_NEED_MINIMAL; |
| 2817 | xecfg.ctxlen = 3; |
Junio C Hamano | 9fdc3bb | 2006-06-28 22:49:42 -0700 | [diff] [blame] | 2818 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 2819 | ecb.outf = xdiff_outf; |
| 2820 | ecb.priv = &data; |
| 2821 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); |
| 2822 | } |
| 2823 | |
| 2824 | SHA1_Final(sha1, &ctx); |
| 2825 | return 0; |
| 2826 | } |
| 2827 | |
| 2828 | int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1) |
| 2829 | { |
| 2830 | struct diff_queue_struct *q = &diff_queued_diff; |
| 2831 | int i; |
| 2832 | int result = diff_get_patch_id(options, sha1); |
| 2833 | |
| 2834 | for (i = 0; i < q->nr; i++) |
| 2835 | diff_free_filepair(q->queue[i]); |
| 2836 | |
| 2837 | free(q->queue); |
| 2838 | q->queue = NULL; |
| 2839 | q->nr = q->alloc = 0; |
| 2840 | |
| 2841 | return result; |
| 2842 | } |
| 2843 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2844 | static int is_summary_empty(const struct diff_queue_struct *q) |
| 2845 | { |
| 2846 | int i; |
| 2847 | |
| 2848 | for (i = 0; i < q->nr; i++) { |
| 2849 | const struct diff_filepair *p = q->queue[i]; |
| 2850 | |
| 2851 | switch (p->status) { |
| 2852 | case DIFF_STATUS_DELETED: |
| 2853 | case DIFF_STATUS_ADDED: |
| 2854 | case DIFF_STATUS_COPIED: |
| 2855 | case DIFF_STATUS_RENAMED: |
| 2856 | return 0; |
| 2857 | default: |
| 2858 | if (p->score) |
| 2859 | return 0; |
| 2860 | if (p->one->mode && p->two->mode && |
| 2861 | p->one->mode != p->two->mode) |
| 2862 | return 0; |
| 2863 | break; |
| 2864 | } |
| 2865 | } |
| 2866 | return 1; |
| 2867 | } |
| 2868 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2869 | void diff_flush(struct diff_options *options) |
| 2870 | { |
| 2871 | struct diff_queue_struct *q = &diff_queued_diff; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2872 | int i, output_format = options->output_format; |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2873 | int separator = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2874 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2875 | /* |
| 2876 | * Order: raw, stat, summary, patch |
| 2877 | * or: name/name-status/checkdiff (other bits clear) |
| 2878 | */ |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2879 | if (!q->nr) |
| 2880 | goto free_queue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2881 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2882 | if (output_format & (DIFF_FORMAT_RAW | |
| 2883 | DIFF_FORMAT_NAME | |
| 2884 | DIFF_FORMAT_NAME_STATUS | |
| 2885 | DIFF_FORMAT_CHECKDIFF)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2886 | for (i = 0; i < q->nr; i++) { |
| 2887 | struct diff_filepair *p = q->queue[i]; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2888 | if (check_pair_status(p)) |
| 2889 | flush_one_pair(p, options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2890 | } |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2891 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2892 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2893 | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2894 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) { |
Timo Hirvonen | 5e2b063 | 2006-06-25 14:28:19 +0300 | [diff] [blame] | 2895 | struct diffstat_t diffstat; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2896 | |
Timo Hirvonen | 5e2b063 | 2006-06-25 14:28:19 +0300 | [diff] [blame] | 2897 | memset(&diffstat, 0, sizeof(struct diffstat_t)); |
| 2898 | diffstat.xm.consume = diffstat_consume; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2899 | for (i = 0; i < q->nr; i++) { |
| 2900 | struct diff_filepair *p = q->queue[i]; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2901 | if (check_pair_status(p)) |
Timo Hirvonen | 5e2b063 | 2006-06-25 14:28:19 +0300 | [diff] [blame] | 2902 | diff_flush_stat(p, options, &diffstat); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2903 | } |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2904 | if (output_format & DIFF_FORMAT_NUMSTAT) |
| 2905 | show_numstat(&diffstat, options); |
| 2906 | if (output_format & DIFF_FORMAT_DIFFSTAT) |
| 2907 | show_stats(&diffstat, options); |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2908 | else if (output_format & DIFF_FORMAT_SHORTSTAT) |
| 2909 | show_shortstats(&diffstat); |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 2910 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2913 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2914 | for (i = 0; i < q->nr; i++) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2915 | diff_summary(q->queue[i]); |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 2916 | separator++; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 2917 | } |
| 2918 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2919 | if (output_format & DIFF_FORMAT_PATCH) { |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2920 | if (separator) { |
| 2921 | if (options->stat_sep) { |
| 2922 | /* attach patch instead of inline */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2923 | fputs(options->stat_sep, stdout); |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2924 | } else { |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2925 | putchar(options->line_termination); |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2926 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | for (i = 0; i < q->nr; i++) { |
| 2930 | struct diff_filepair *p = q->queue[i]; |
| 2931 | if (check_pair_status(p)) |
| 2932 | diff_flush_patch(p, options); |
| 2933 | } |
| 2934 | } |
| 2935 | |
Jeff King | 0424558 | 2006-09-07 02:35:42 -0400 | [diff] [blame] | 2936 | if (output_format & DIFF_FORMAT_CALLBACK) |
| 2937 | options->format_callback(q, options, options->format_callback_data); |
| 2938 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 2939 | for (i = 0; i < q->nr; i++) |
| 2940 | diff_free_filepair(q->queue[i]); |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 2941 | free_queue: |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2942 | free(q->queue); |
| 2943 | q->queue = NULL; |
| 2944 | q->nr = q->alloc = 0; |
| 2945 | } |
| 2946 | |
| 2947 | static void diffcore_apply_filter(const char *filter) |
| 2948 | { |
| 2949 | int i; |
| 2950 | struct diff_queue_struct *q = &diff_queued_diff; |
| 2951 | struct diff_queue_struct outq; |
| 2952 | outq.queue = NULL; |
| 2953 | outq.nr = outq.alloc = 0; |
| 2954 | |
| 2955 | if (!filter) |
| 2956 | return; |
| 2957 | |
| 2958 | if (strchr(filter, DIFF_STATUS_FILTER_AON)) { |
| 2959 | int found; |
| 2960 | for (i = found = 0; !found && i < q->nr; i++) { |
| 2961 | struct diff_filepair *p = q->queue[i]; |
| 2962 | if (((p->status == DIFF_STATUS_MODIFIED) && |
| 2963 | ((p->score && |
| 2964 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || |
| 2965 | (!p->score && |
| 2966 | strchr(filter, DIFF_STATUS_MODIFIED)))) || |
| 2967 | ((p->status != DIFF_STATUS_MODIFIED) && |
| 2968 | strchr(filter, p->status))) |
| 2969 | found++; |
| 2970 | } |
| 2971 | if (found) |
| 2972 | return; |
| 2973 | |
| 2974 | /* otherwise we will clear the whole queue |
| 2975 | * by copying the empty outq at the end of this |
| 2976 | * function, but first clear the current entries |
| 2977 | * in the queue. |
| 2978 | */ |
| 2979 | for (i = 0; i < q->nr; i++) |
| 2980 | diff_free_filepair(q->queue[i]); |
| 2981 | } |
| 2982 | else { |
| 2983 | /* Only the matching ones */ |
| 2984 | for (i = 0; i < q->nr; i++) { |
| 2985 | struct diff_filepair *p = q->queue[i]; |
| 2986 | |
| 2987 | if (((p->status == DIFF_STATUS_MODIFIED) && |
| 2988 | ((p->score && |
| 2989 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || |
| 2990 | (!p->score && |
| 2991 | strchr(filter, DIFF_STATUS_MODIFIED)))) || |
| 2992 | ((p->status != DIFF_STATUS_MODIFIED) && |
| 2993 | strchr(filter, p->status))) |
| 2994 | diff_q(&outq, p); |
| 2995 | else |
| 2996 | diff_free_filepair(p); |
| 2997 | } |
| 2998 | } |
| 2999 | free(q->queue); |
| 3000 | *q = outq; |
| 3001 | } |
| 3002 | |
| 3003 | void diffcore_std(struct diff_options *options) |
| 3004 | { |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 3005 | if (options->quiet) |
| 3006 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3007 | if (options->break_opt != -1) |
| 3008 | diffcore_break(options->break_opt); |
| 3009 | if (options->detect_rename) |
| 3010 | diffcore_rename(options); |
| 3011 | if (options->break_opt != -1) |
| 3012 | diffcore_merge_broken(); |
| 3013 | if (options->pickaxe) |
| 3014 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); |
| 3015 | if (options->orderfile) |
| 3016 | diffcore_order(options->orderfile); |
| 3017 | diff_resolve_rename_copy(); |
| 3018 | diffcore_apply_filter(options->filter); |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 3019 | |
| 3020 | options->has_changes = !!diff_queued_diff.nr; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3024 | void diff_addremove(struct diff_options *options, |
| 3025 | int addremove, unsigned mode, |
| 3026 | const unsigned char *sha1, |
| 3027 | const char *base, const char *path) |
| 3028 | { |
| 3029 | char concatpath[PATH_MAX]; |
| 3030 | struct diff_filespec *one, *two; |
| 3031 | |
| 3032 | /* This may look odd, but it is a preparation for |
| 3033 | * feeding "there are unchanged files which should |
| 3034 | * not produce diffs, but when you are doing copy |
| 3035 | * detection you would need them, so here they are" |
| 3036 | * entries to the diff-core. They will be prefixed |
| 3037 | * with something like '=' or '*' (I haven't decided |
| 3038 | * which but should not make any difference). |
| 3039 | * Feeding the same new and old to diff_change() |
| 3040 | * also has the same effect. |
| 3041 | * Before the final output happens, they are pruned after |
| 3042 | * merged into rename/copy pairs as appropriate. |
| 3043 | */ |
| 3044 | if (options->reverse_diff) |
| 3045 | addremove = (addremove == '+' ? '-' : |
| 3046 | addremove == '-' ? '+' : addremove); |
| 3047 | |
| 3048 | if (!path) path = ""; |
| 3049 | sprintf(concatpath, "%s%s", base, path); |
| 3050 | one = alloc_filespec(concatpath); |
| 3051 | two = alloc_filespec(concatpath); |
| 3052 | |
| 3053 | if (addremove != '+') |
| 3054 | fill_filespec(one, sha1, mode); |
| 3055 | if (addremove != '-') |
| 3056 | fill_filespec(two, sha1, mode); |
| 3057 | |
| 3058 | diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 3059 | options->has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | void diff_change(struct diff_options *options, |
| 3063 | unsigned old_mode, unsigned new_mode, |
| 3064 | const unsigned char *old_sha1, |
| 3065 | const unsigned char *new_sha1, |
| 3066 | const char *base, const char *path) |
| 3067 | { |
| 3068 | char concatpath[PATH_MAX]; |
| 3069 | struct diff_filespec *one, *two; |
| 3070 | |
| 3071 | if (options->reverse_diff) { |
| 3072 | unsigned tmp; |
| 3073 | const unsigned char *tmp_c; |
| 3074 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; |
| 3075 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; |
| 3076 | } |
| 3077 | if (!path) path = ""; |
| 3078 | sprintf(concatpath, "%s%s", base, path); |
| 3079 | one = alloc_filespec(concatpath); |
| 3080 | two = alloc_filespec(concatpath); |
| 3081 | fill_filespec(one, old_sha1, old_mode); |
| 3082 | fill_filespec(two, new_sha1, new_mode); |
| 3083 | |
| 3084 | diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 3085 | options->has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | void diff_unmerge(struct diff_options *options, |
Junio C Hamano | e9c8409 | 2007-01-05 01:25:18 -0800 | [diff] [blame] | 3089 | const char *path, |
| 3090 | unsigned mode, const unsigned char *sha1) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3091 | { |
| 3092 | struct diff_filespec *one, *two; |
| 3093 | one = alloc_filespec(path); |
| 3094 | two = alloc_filespec(path); |
Junio C Hamano | e9c8409 | 2007-01-05 01:25:18 -0800 | [diff] [blame] | 3095 | fill_filespec(one, sha1, mode); |
| 3096 | diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3097 | } |