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