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