Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/wait.h> |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 6 | #include <signal.h> |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 7 | #include "cache.h" |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 8 | #include "quote.h" |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 9 | #include "diff.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 10 | #include "diffcore.h" |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 11 | |
Junio C Hamano | d19938a | 2005-05-09 17:57:56 -0700 | [diff] [blame] | 12 | static const char *diff_opts = "-pu"; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 13 | |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 14 | static int use_size_cache; |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 15 | |
Junio C Hamano | 3299c6f | 2005-11-15 12:48:08 -0800 | [diff] [blame] | 16 | int diff_rename_limit_default = -1; |
| 17 | |
Junio C Hamano | 9ce392f | 2005-11-21 22:52:37 -0800 | [diff] [blame] | 18 | int git_diff_config(const char *var, const char *value) |
| 19 | { |
| 20 | if (!strcmp(var, "diff.renamelimit")) { |
| 21 | diff_rename_limit_default = git_config_int(var, value); |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | return git_default_config(var, value); |
| 26 | } |
| 27 | |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 28 | static char *quote_one(const char *str) |
| 29 | { |
| 30 | int needlen; |
| 31 | char *xp; |
| 32 | |
| 33 | if (!str) |
| 34 | return NULL; |
| 35 | needlen = quote_c_style(str, NULL, NULL, 0); |
| 36 | if (!needlen) |
| 37 | return strdup(str); |
| 38 | xp = xmalloc(needlen + 1); |
| 39 | quote_c_style(str, xp, NULL, 0); |
| 40 | return xp; |
| 41 | } |
| 42 | |
| 43 | static char *quote_two(const char *one, const char *two) |
| 44 | { |
| 45 | int need_one = quote_c_style(one, NULL, NULL, 1); |
| 46 | int need_two = quote_c_style(two, NULL, NULL, 1); |
| 47 | char *xp; |
| 48 | |
| 49 | if (need_one + need_two) { |
| 50 | if (!need_one) need_one = strlen(one); |
| 51 | if (!need_two) need_one = strlen(two); |
| 52 | |
| 53 | xp = xmalloc(need_one + need_two + 3); |
| 54 | xp[0] = '"'; |
| 55 | quote_c_style(one, xp + 1, NULL, 1); |
| 56 | quote_c_style(two, xp + need_one + 1, NULL, 1); |
| 57 | strcpy(xp + need_one + need_two + 1, "\""); |
| 58 | return xp; |
| 59 | } |
| 60 | need_one = strlen(one); |
| 61 | need_two = strlen(two); |
| 62 | xp = xmalloc(need_one + need_two + 1); |
| 63 | strcpy(xp, one); |
| 64 | strcpy(xp + need_one, two); |
| 65 | return xp; |
| 66 | } |
| 67 | |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 68 | static const char *external_diff(void) |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 69 | { |
Junio C Hamano | d19938a | 2005-05-09 17:57:56 -0700 | [diff] [blame] | 70 | static const char *external_diff_cmd = NULL; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 71 | static int done_preparing = 0; |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 72 | const char *env_diff_opts; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 73 | |
| 74 | if (done_preparing) |
| 75 | return external_diff_cmd; |
| 76 | |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Default values above are meant to match the |
| 79 | * Linux kernel development style. Examples of |
| 80 | * alternative styles you can specify via environment |
| 81 | * variables are: |
| 82 | * |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 83 | * GIT_DIFF_OPTS="-c"; |
| 84 | */ |
Junio C Hamano | a9ab586 | 2005-09-09 14:48:54 -0700 | [diff] [blame] | 85 | external_diff_cmd = getenv("GIT_EXTERNAL_DIFF"); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 86 | |
| 87 | /* In case external diff fails... */ |
Junio C Hamano | a9ab586 | 2005-09-09 14:48:54 -0700 | [diff] [blame] | 88 | env_diff_opts = getenv("GIT_DIFF_OPTS"); |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 89 | if (env_diff_opts) diff_opts = env_diff_opts; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 90 | |
| 91 | done_preparing = 1; |
| 92 | return external_diff_cmd; |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Holger Eitzenberger | 64f8a63 | 2005-08-04 22:49:49 +0200 | [diff] [blame] | 95 | #define TEMPFILE_PATH_LEN 50 |
| 96 | |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 97 | static struct diff_tempfile { |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 98 | const char *name; /* filename external diff should read from */ |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 99 | char hex[41]; |
| 100 | char mode[10]; |
Holger Eitzenberger | 64f8a63 | 2005-08-04 22:49:49 +0200 | [diff] [blame] | 101 | char tmp_path[TEMPFILE_PATH_LEN]; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 102 | } diff_temp[2]; |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 103 | |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 104 | static int count_lines(const char *filename) |
| 105 | { |
| 106 | FILE *in; |
| 107 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
| 108 | in = fopen(filename, "r"); |
| 109 | count = 0; |
| 110 | while ((ch = fgetc(in)) != EOF) |
| 111 | if (ch == '\n') { |
| 112 | count++; |
| 113 | nl_just_seen = 1; |
| 114 | completely_empty = 0; |
| 115 | } |
| 116 | else { |
| 117 | nl_just_seen = 0; |
| 118 | completely_empty = 0; |
| 119 | } |
| 120 | fclose(in); |
| 121 | if (completely_empty) |
| 122 | return 0; |
| 123 | if (!nl_just_seen) |
| 124 | count++; /* no trailing newline */ |
| 125 | return count; |
| 126 | } |
| 127 | |
| 128 | static void print_line_count(int count) |
| 129 | { |
| 130 | switch (count) { |
| 131 | case 0: |
| 132 | printf("0,0"); |
| 133 | break; |
| 134 | case 1: |
| 135 | printf("1"); |
| 136 | break; |
| 137 | default: |
| 138 | printf("1,%d", count); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static void copy_file(int prefix, const char *filename) |
| 144 | { |
| 145 | FILE *in; |
| 146 | int ch, nl_just_seen = 1; |
| 147 | in = fopen(filename, "r"); |
| 148 | while ((ch = fgetc(in)) != EOF) { |
| 149 | if (nl_just_seen) |
| 150 | putchar(prefix); |
| 151 | putchar(ch); |
| 152 | if (ch == '\n') |
| 153 | nl_just_seen = 1; |
| 154 | else |
| 155 | nl_just_seen = 0; |
| 156 | } |
| 157 | fclose(in); |
| 158 | if (!nl_just_seen) |
| 159 | printf("\n\\ No newline at end of file\n"); |
| 160 | } |
| 161 | |
| 162 | static void emit_rewrite_diff(const char *name_a, |
| 163 | const char *name_b, |
| 164 | struct diff_tempfile *temp) |
| 165 | { |
| 166 | /* Use temp[i].name as input, name_a and name_b as labels */ |
| 167 | int lc_a, lc_b; |
| 168 | lc_a = count_lines(temp[0].name); |
| 169 | lc_b = count_lines(temp[1].name); |
| 170 | printf("--- %s\n+++ %s\n@@ -", name_a, name_b); |
| 171 | print_line_count(lc_a); |
| 172 | printf(" +"); |
| 173 | print_line_count(lc_b); |
| 174 | printf(" @@\n"); |
| 175 | if (lc_a) |
| 176 | copy_file('-', temp[0].name); |
| 177 | if (lc_b) |
| 178 | copy_file('+', temp[1].name); |
| 179 | } |
| 180 | |
Junio C Hamano | 915838c | 2005-05-17 23:29:49 -0700 | [diff] [blame] | 181 | static void builtin_diff(const char *name_a, |
| 182 | const char *name_b, |
Junio C Hamano | 57fe64a | 2005-05-19 19:00:36 -0700 | [diff] [blame] | 183 | struct diff_tempfile *temp, |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 184 | const char *xfrm_msg, |
| 185 | int complete_rewrite) |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 186 | { |
Thomas Glanzmann | 9669e17 | 2005-05-19 15:23:18 +0200 | [diff] [blame] | 187 | int i, next_at, cmd_size; |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 188 | const char *const diff_cmd = "diff -L%s -L%s"; |
Linus Torvalds | 694a764 | 2005-10-18 00:16:45 -0700 | [diff] [blame] | 189 | const char *const diff_arg = "-- %s %s||:"; /* "||:" is to return 0 */ |
Junio C Hamano | 2f97813 | 2005-04-28 08:04:39 -0700 | [diff] [blame] | 190 | const char *input_name_sq[2]; |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 191 | const char *label_path[2]; |
Junio C Hamano | 2f97813 | 2005-04-28 08:04:39 -0700 | [diff] [blame] | 192 | char *cmd; |
Junio C Hamano | 915838c | 2005-05-17 23:29:49 -0700 | [diff] [blame] | 193 | |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 194 | /* diff_cmd and diff_arg have 4 %s in total which makes |
| 195 | * the sum of these strings 8 bytes larger than required. |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 196 | * we use 2 spaces around diff-opts, and we need to count |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 197 | * terminating NUL; we used to subtract 5 here, but we do not |
| 198 | * care about small leaks in this subprocess that is about |
| 199 | * to exec "diff" anymore. |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 200 | */ |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 201 | cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg) |
| 202 | + 128); |
| 203 | |
Junio C Hamano | 2f97813 | 2005-04-28 08:04:39 -0700 | [diff] [blame] | 204 | for (i = 0; i < 2; i++) { |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 205 | input_name_sq[i] = sq_quote(temp[i].name); |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 206 | if (!strcmp(temp[i].name, "/dev/null")) |
| 207 | label_path[i] = "/dev/null"; |
| 208 | else if (!i) |
| 209 | label_path[i] = sq_quote(quote_two("a/", name_a)); |
| 210 | else |
| 211 | label_path[i] = sq_quote(quote_two("b/", name_b)); |
| 212 | cmd_size += (strlen(label_path[i]) + strlen(input_name_sq[i])); |
Junio C Hamano | 2f97813 | 2005-04-28 08:04:39 -0700 | [diff] [blame] | 213 | } |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 214 | |
Junio C Hamano | 2f97813 | 2005-04-28 08:04:39 -0700 | [diff] [blame] | 215 | cmd = xmalloc(cmd_size); |
| 216 | |
| 217 | next_at = 0; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 218 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 219 | diff_cmd, label_path[0], label_path[1]); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 220 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
| 221 | " %s ", diff_opts); |
| 222 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
Junio C Hamano | 2f97813 | 2005-04-28 08:04:39 -0700 | [diff] [blame] | 223 | diff_arg, input_name_sq[0], input_name_sq[1]); |
| 224 | |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 225 | printf("diff --git %s %s\n", |
| 226 | quote_two("a/", name_a), quote_two("b/", name_b)); |
| 227 | if (label_path[0][0] == '/') { |
| 228 | /* dev/null */ |
Junio C Hamano | b58f23b | 2005-05-18 09:10:47 -0700 | [diff] [blame] | 229 | printf("new file mode %s\n", temp[1].mode); |
Junio C Hamano | 70aadac | 2005-05-30 16:40:16 -0700 | [diff] [blame] | 230 | if (xfrm_msg && xfrm_msg[0]) |
| 231 | puts(xfrm_msg); |
| 232 | } |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 233 | else if (label_path[1][0] == '/') { |
Junio C Hamano | b58f23b | 2005-05-18 09:10:47 -0700 | [diff] [blame] | 234 | printf("deleted file mode %s\n", temp[0].mode); |
Junio C Hamano | 70aadac | 2005-05-30 16:40:16 -0700 | [diff] [blame] | 235 | if (xfrm_msg && xfrm_msg[0]) |
| 236 | puts(xfrm_msg); |
| 237 | } |
Junio C Hamano | 273b983 | 2005-05-13 18:40:14 -0700 | [diff] [blame] | 238 | else { |
Junio C Hamano | b58f23b | 2005-05-18 09:10:47 -0700 | [diff] [blame] | 239 | if (strcmp(temp[0].mode, temp[1].mode)) { |
| 240 | printf("old mode %s\n", temp[0].mode); |
| 241 | printf("new mode %s\n", temp[1].mode); |
| 242 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 243 | if (xfrm_msg && xfrm_msg[0]) |
Junio C Hamano | 09d9d1a | 2005-05-27 15:54:06 -0700 | [diff] [blame] | 244 | puts(xfrm_msg); |
Junio C Hamano | 273b983 | 2005-05-13 18:40:14 -0700 | [diff] [blame] | 245 | if (strncmp(temp[0].mode, temp[1].mode, 3)) |
| 246 | /* we do not run diff between different kind |
| 247 | * of objects. |
| 248 | */ |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 249 | exit(0); |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 250 | if (complete_rewrite) { |
| 251 | fflush(NULL); |
| 252 | emit_rewrite_diff(name_a, name_b, temp); |
| 253 | exit(0); |
| 254 | } |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 255 | } |
Junio C Hamano | c983370 | 2005-05-01 09:33:12 -0700 | [diff] [blame] | 256 | fflush(NULL); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 257 | execlp("/bin/sh","sh", "-c", cmd, NULL); |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 260 | struct diff_filespec *alloc_filespec(const char *path) |
| 261 | { |
| 262 | int namelen = strlen(path); |
| 263 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); |
Linus Torvalds | 705a714 | 2005-09-14 13:41:24 -0700 | [diff] [blame] | 264 | |
| 265 | memset(spec, 0, sizeof(*spec)); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 266 | spec->path = (char *)(spec + 1); |
Linus Torvalds | 705a714 | 2005-09-14 13:41:24 -0700 | [diff] [blame] | 267 | memcpy(spec->path, path, namelen+1); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 268 | return spec; |
| 269 | } |
| 270 | |
| 271 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, |
| 272 | unsigned short mode) |
| 273 | { |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 274 | if (mode) { |
| 275 | spec->mode = DIFF_FILE_CANON_MODE(mode); |
Junio C Hamano | 81e50ea | 2005-05-21 19:42:18 -0700 | [diff] [blame] | 276 | memcpy(spec->sha1, sha1, 20); |
| 277 | spec->sha1_valid = !!memcmp(sha1, null_sha1, 20); |
| 278 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 281 | /* |
| 282 | * Given a name and sha1 pair, if the dircache tells us the file in |
| 283 | * the work tree has that object contents, return true, so that |
| 284 | * prepare_temp_file() does not have to inflate and extract. |
| 285 | */ |
| 286 | static int work_tree_matches(const char *name, const unsigned char *sha1) |
| 287 | { |
| 288 | struct cache_entry *ce; |
| 289 | struct stat st; |
| 290 | int pos, len; |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 291 | |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 292 | /* We do not read the cache ourselves here, because the |
| 293 | * benchmark with my previous version that always reads cache |
| 294 | * shows that it makes things worse for diff-tree comparing |
| 295 | * two linux-2.6 kernel trees in an already checked out work |
Junio C Hamano | 915838c | 2005-05-17 23:29:49 -0700 | [diff] [blame] | 296 | * tree. This is because most diff-tree comparisons deal with |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 297 | * only a small number of files, while reading the cache is |
| 298 | * expensive for a large project, and its cost outweighs the |
| 299 | * savings we get by not inflating the object to a temporary |
| 300 | * file. Practically, this code only helps when we are used |
| 301 | * by diff-cache --cached, which does read the cache before |
| 302 | * calling us. |
Junio C Hamano | 57fe64a | 2005-05-19 19:00:36 -0700 | [diff] [blame] | 303 | */ |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 304 | if (!active_cache) |
| 305 | return 0; |
| 306 | |
| 307 | len = strlen(name); |
| 308 | pos = cache_name_pos(name, len); |
| 309 | if (pos < 0) |
| 310 | return 0; |
| 311 | ce = active_cache[pos]; |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 312 | if ((lstat(name, &st) < 0) || |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 313 | !S_ISREG(st.st_mode) || /* careful! */ |
Brad Roberts | 5d728c8 | 2005-05-14 19:04:25 -0700 | [diff] [blame] | 314 | ce_match_stat(ce, &st) || |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 315 | memcmp(sha1, ce->sha1, 20)) |
| 316 | return 0; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 317 | /* we return 1 only when we can stat, it is a regular file, |
| 318 | * stat information matches, and sha1 recorded in the cache |
| 319 | * matches. I.e. we know the file in the work tree really is |
| 320 | * the same as the <name, sha1> pair. |
| 321 | */ |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 322 | return 1; |
| 323 | } |
| 324 | |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 325 | static struct sha1_size_cache { |
| 326 | unsigned char sha1[20]; |
| 327 | unsigned long size; |
| 328 | } **sha1_size_cache; |
| 329 | static int sha1_size_cache_nr, sha1_size_cache_alloc; |
| 330 | |
| 331 | static struct sha1_size_cache *locate_size_cache(unsigned char *sha1, |
Junio C Hamano | 0601e13 | 2005-06-03 23:02:23 -0700 | [diff] [blame] | 332 | int find_only, |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 333 | unsigned long size) |
| 334 | { |
| 335 | int first, last; |
| 336 | struct sha1_size_cache *e; |
| 337 | |
| 338 | first = 0; |
| 339 | last = sha1_size_cache_nr; |
| 340 | while (last > first) { |
Junio C Hamano | 0601e13 | 2005-06-03 23:02:23 -0700 | [diff] [blame] | 341 | int cmp, next = (last + first) >> 1; |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 342 | e = sha1_size_cache[next]; |
Junio C Hamano | 0601e13 | 2005-06-03 23:02:23 -0700 | [diff] [blame] | 343 | cmp = memcmp(e->sha1, sha1, 20); |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 344 | if (!cmp) |
| 345 | return e; |
| 346 | if (cmp < 0) { |
| 347 | last = next; |
| 348 | continue; |
| 349 | } |
| 350 | first = next+1; |
| 351 | } |
| 352 | /* not found */ |
Junio C Hamano | 0601e13 | 2005-06-03 23:02:23 -0700 | [diff] [blame] | 353 | if (find_only) |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 354 | return NULL; |
| 355 | /* insert to make it at "first" */ |
| 356 | if (sha1_size_cache_alloc <= sha1_size_cache_nr) { |
| 357 | sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc); |
| 358 | sha1_size_cache = xrealloc(sha1_size_cache, |
| 359 | sha1_size_cache_alloc * |
| 360 | sizeof(*sha1_size_cache)); |
| 361 | } |
| 362 | sha1_size_cache_nr++; |
| 363 | if (first < sha1_size_cache_nr) |
| 364 | memmove(sha1_size_cache + first + 1, sha1_size_cache + first, |
| 365 | (sha1_size_cache_nr - first - 1) * |
| 366 | sizeof(*sha1_size_cache)); |
| 367 | e = xmalloc(sizeof(struct sha1_size_cache)); |
| 368 | sha1_size_cache[first] = e; |
| 369 | memcpy(e->sha1, sha1, 20); |
| 370 | e->size = size; |
| 371 | return e; |
| 372 | } |
| 373 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 374 | /* |
| 375 | * While doing rename detection and pickaxe operation, we may need to |
| 376 | * grab the data for the blob (or file) for our own in-core comparison. |
| 377 | * diff_filespec has data and size fields for this purpose. |
| 378 | */ |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 379 | int diff_populate_filespec(struct diff_filespec *s, int size_only) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 380 | { |
| 381 | int err = 0; |
Junio C Hamano | 81e50ea | 2005-05-21 19:42:18 -0700 | [diff] [blame] | 382 | if (!DIFF_FILE_VALID(s)) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 383 | die("internal error: asking to populate invalid file."); |
| 384 | if (S_ISDIR(s->mode)) |
| 385 | return -1; |
| 386 | |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 387 | if (!use_size_cache) |
| 388 | size_only = 0; |
| 389 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 390 | if (s->data) |
| 391 | return err; |
| 392 | if (!s->sha1_valid || |
| 393 | work_tree_matches(s->path, s->sha1)) { |
| 394 | struct stat st; |
| 395 | int fd; |
| 396 | if (lstat(s->path, &st) < 0) { |
| 397 | if (errno == ENOENT) { |
| 398 | err_empty: |
| 399 | err = -1; |
| 400 | empty: |
| 401 | s->data = ""; |
| 402 | s->size = 0; |
| 403 | return err; |
| 404 | } |
| 405 | } |
| 406 | s->size = st.st_size; |
| 407 | if (!s->size) |
| 408 | goto empty; |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 409 | if (size_only) |
| 410 | return 0; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 411 | if (S_ISLNK(st.st_mode)) { |
| 412 | int ret; |
| 413 | s->data = xmalloc(s->size); |
| 414 | s->should_free = 1; |
| 415 | ret = readlink(s->path, s->data, s->size); |
| 416 | if (ret < 0) { |
| 417 | free(s->data); |
| 418 | goto err_empty; |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | fd = open(s->path, O_RDONLY); |
| 423 | if (fd < 0) |
| 424 | goto err_empty; |
| 425 | s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 426 | close(fd); |
Pavel Roskin | e35f982 | 2005-07-29 10:49:14 -0400 | [diff] [blame] | 427 | if (s->data == MAP_FAILED) |
| 428 | goto err_empty; |
| 429 | s->should_munmap = 1; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 430 | } |
| 431 | else { |
| 432 | char type[20]; |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 433 | struct sha1_size_cache *e; |
| 434 | |
| 435 | if (size_only) { |
Junio C Hamano | 0601e13 | 2005-06-03 23:02:23 -0700 | [diff] [blame] | 436 | e = locate_size_cache(s->sha1, 1, 0); |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 437 | if (e) { |
| 438 | s->size = e->size; |
| 439 | return 0; |
| 440 | } |
Junio C Hamano | 36e4d74 | 2005-06-27 03:34:06 -0700 | [diff] [blame] | 441 | if (!sha1_object_info(s->sha1, type, &s->size)) |
Junio C Hamano | 0601e13 | 2005-06-03 23:02:23 -0700 | [diff] [blame] | 442 | locate_size_cache(s->sha1, 0, s->size); |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 443 | } |
Junio C Hamano | 65c2e0c | 2005-06-02 15:20:54 -0700 | [diff] [blame] | 444 | else { |
| 445 | s->data = read_sha1_file(s->sha1, type, &s->size); |
| 446 | s->should_free = 1; |
| 447 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
Junio C Hamano | 19397b4 | 2005-09-14 14:06:50 -0700 | [diff] [blame] | 452 | void diff_free_filespec_data(struct diff_filespec *s) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 453 | { |
| 454 | if (s->should_free) |
| 455 | free(s->data); |
| 456 | else if (s->should_munmap) |
| 457 | munmap(s->data, s->size); |
Junio C Hamano | 19397b4 | 2005-09-14 14:06:50 -0700 | [diff] [blame] | 458 | s->should_free = s->should_munmap = 0; |
| 459 | s->data = NULL; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 462 | static void prep_temp_blob(struct diff_tempfile *temp, |
| 463 | void *blob, |
| 464 | unsigned long size, |
Junio C Hamano | 88cd621 | 2005-09-30 14:02:47 -0700 | [diff] [blame] | 465 | const unsigned char *sha1, |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 466 | int mode) |
| 467 | { |
| 468 | int fd; |
| 469 | |
Holger Eitzenberger | 64f8a63 | 2005-08-04 22:49:49 +0200 | [diff] [blame] | 470 | fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX"); |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 471 | if (fd < 0) |
| 472 | die("unable to create temp-file"); |
| 473 | if (write(fd, blob, size) != size) |
| 474 | die("unable to write temp-file"); |
| 475 | close(fd); |
| 476 | temp->name = temp->tmp_path; |
| 477 | strcpy(temp->hex, sha1_to_hex(sha1)); |
| 478 | temp->hex[40] = 0; |
| 479 | sprintf(temp->mode, "%06o", mode); |
| 480 | } |
| 481 | |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 482 | static void prepare_temp_file(const char *name, |
| 483 | struct diff_tempfile *temp, |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 484 | struct diff_filespec *one) |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 485 | { |
Junio C Hamano | 81e50ea | 2005-05-21 19:42:18 -0700 | [diff] [blame] | 486 | if (!DIFF_FILE_VALID(one)) { |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 487 | not_a_valid_file: |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 488 | /* A '-' entry produces this for file-2, and |
| 489 | * a '+' entry produces this for file-1. |
| 490 | */ |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 491 | temp->name = "/dev/null"; |
| 492 | strcpy(temp->hex, "."); |
| 493 | strcpy(temp->mode, "."); |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 494 | return; |
| 495 | } |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 496 | |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 497 | if (!one->sha1_valid || |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 498 | work_tree_matches(name, one->sha1)) { |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 499 | struct stat st; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 500 | if (lstat(name, &st) < 0) { |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 501 | if (errno == ENOENT) |
| 502 | goto not_a_valid_file; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 503 | die("stat(%s): %s", name, strerror(errno)); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 504 | } |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 505 | if (S_ISLNK(st.st_mode)) { |
| 506 | int ret; |
| 507 | char *buf, buf_[1024]; |
| 508 | buf = ((sizeof(buf_) < st.st_size) ? |
| 509 | xmalloc(st.st_size) : buf_); |
| 510 | ret = readlink(name, buf, st.st_size); |
| 511 | if (ret < 0) |
| 512 | die("readlink(%s)", name); |
| 513 | prep_temp_blob(temp, buf, st.st_size, |
| 514 | (one->sha1_valid ? |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 515 | one->sha1 : null_sha1), |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 516 | (one->sha1_valid ? |
| 517 | one->mode : S_IFLNK)); |
| 518 | } |
| 519 | else { |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 520 | /* we can borrow from the file in the work tree */ |
| 521 | temp->name = name; |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 522 | if (!one->sha1_valid) |
| 523 | strcpy(temp->hex, sha1_to_hex(null_sha1)); |
| 524 | else |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 525 | strcpy(temp->hex, sha1_to_hex(one->sha1)); |
Junio C Hamano | 9d429ff | 2005-05-30 00:07:39 -0700 | [diff] [blame] | 526 | /* Even though we may sometimes borrow the |
| 527 | * contents from the work tree, we always want |
| 528 | * one->mode. mode is trustworthy even when |
| 529 | * !(one->sha1_valid), as long as |
| 530 | * DIFF_FILE_VALID(one). |
| 531 | */ |
| 532 | sprintf(temp->mode, "%06o", one->mode); |
Junio C Hamano | b28858b | 2005-05-05 16:10:21 -0700 | [diff] [blame] | 533 | } |
| 534 | return; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 535 | } |
| 536 | else { |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 537 | if (diff_populate_filespec(one, 0)) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 538 | die("cannot read data blob for %s", one->path); |
| 539 | prep_temp_blob(temp, one->data, one->size, |
| 540 | one->sha1, one->mode); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | static void remove_tempfile(void) |
| 545 | { |
| 546 | int i; |
| 547 | |
| 548 | for (i = 0; i < 2; i++) |
| 549 | if (diff_temp[i].name == diff_temp[i].tmp_path) { |
| 550 | unlink(diff_temp[i].name); |
| 551 | diff_temp[i].name = NULL; |
| 552 | } |
| 553 | } |
| 554 | |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 555 | static void remove_tempfile_on_signal(int signo) |
| 556 | { |
| 557 | remove_tempfile(); |
| 558 | } |
| 559 | |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 560 | /* An external diff command takes: |
| 561 | * |
| 562 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 563 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 564 | * |
| 565 | */ |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 566 | static void run_external_diff(const char *pgm, |
| 567 | const char *name, |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 568 | const char *other, |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 569 | struct diff_filespec *one, |
| 570 | struct diff_filespec *two, |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 571 | const char *xfrm_msg, |
| 572 | int complete_rewrite) |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 573 | { |
| 574 | struct diff_tempfile *temp = diff_temp; |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 575 | pid_t pid; |
| 576 | int status; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 577 | static int atexit_asked = 0; |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 578 | const char *othername; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 579 | |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 580 | othername = (other? other : name); |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 581 | if (one && two) { |
| 582 | prepare_temp_file(name, &temp[0], one); |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 583 | prepare_temp_file(othername, &temp[1], two); |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 584 | if (! atexit_asked && |
| 585 | (temp[0].name == temp[0].tmp_path || |
| 586 | temp[1].name == temp[1].tmp_path)) { |
| 587 | atexit_asked = 1; |
| 588 | atexit(remove_tempfile); |
| 589 | } |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 590 | signal(SIGINT, remove_tempfile_on_signal); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | fflush(NULL); |
| 594 | pid = fork(); |
| 595 | if (pid < 0) |
| 596 | die("unable to fork"); |
| 597 | if (!pid) { |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 598 | if (pgm) { |
| 599 | if (one && two) { |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 600 | const char *exec_arg[10]; |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 601 | const char **arg = &exec_arg[0]; |
| 602 | *arg++ = pgm; |
| 603 | *arg++ = name; |
| 604 | *arg++ = temp[0].name; |
| 605 | *arg++ = temp[0].hex; |
| 606 | *arg++ = temp[0].mode; |
| 607 | *arg++ = temp[1].name; |
| 608 | *arg++ = temp[1].hex; |
| 609 | *arg++ = temp[1].mode; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 610 | if (other) { |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 611 | *arg++ = other; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 612 | *arg++ = xfrm_msg; |
| 613 | } |
Linus Torvalds | 09d74b3 | 2005-05-22 14:33:43 -0700 | [diff] [blame] | 614 | *arg = NULL; |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 615 | execvp(pgm, (char *const*) exec_arg); |
| 616 | } |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 617 | else |
| 618 | execlp(pgm, pgm, name, NULL); |
| 619 | } |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 620 | /* |
| 621 | * otherwise we use the built-in one. |
| 622 | */ |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 623 | if (one && two) |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 624 | builtin_diff(name, othername, temp, xfrm_msg, |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 625 | complete_rewrite); |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 626 | else |
| 627 | printf("* Unmerged path %s\n", name); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 628 | exit(0); |
| 629 | } |
Junio C Hamano | 6fa2806 | 2005-05-04 01:38:06 -0700 | [diff] [blame] | 630 | if (waitpid(pid, &status, 0) < 0 || |
| 631 | !WIFEXITED(status) || WEXITSTATUS(status)) { |
| 632 | /* Earlier we did not check the exit status because |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 633 | * diff exits non-zero if files are different, and |
Junio C Hamano | 6fa2806 | 2005-05-04 01:38:06 -0700 | [diff] [blame] | 634 | * we are not interested in knowing that. It was a |
| 635 | * mistake which made it harder to quit a diff-* |
| 636 | * session that uses the git-apply-patch-script as |
| 637 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF |
| 638 | * should also exit non-zero only when it wants to |
| 639 | * abort the entire diff-* session. |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 640 | */ |
| 641 | remove_tempfile(); |
Junio C Hamano | 6fa2806 | 2005-05-04 01:38:06 -0700 | [diff] [blame] | 642 | fprintf(stderr, "external diff died, stopping at %s.\n", name); |
| 643 | exit(1); |
Junio C Hamano | 532149d | 2005-04-28 10:13:01 -0700 | [diff] [blame] | 644 | } |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 645 | remove_tempfile(); |
| 646 | } |
| 647 | |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 648 | static void diff_fill_sha1_info(struct diff_filespec *one) |
| 649 | { |
| 650 | if (DIFF_FILE_VALID(one)) { |
| 651 | if (!one->sha1_valid) { |
| 652 | struct stat st; |
| 653 | if (stat(one->path, &st) < 0) |
| 654 | die("stat %s", one->path); |
| 655 | if (index_path(one->sha1, one->path, &st, 0)) |
| 656 | die("cannot hash %s\n", one->path); |
| 657 | } |
| 658 | } |
| 659 | else |
| 660 | memset(one->sha1, 0, 20); |
| 661 | } |
| 662 | |
Junio C Hamano | 80b1e51 | 2005-11-14 17:53:22 -0800 | [diff] [blame] | 663 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 664 | { |
| 665 | const char *pgm = external_diff(); |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 666 | char msg[PATH_MAX*2+300], *xfrm_msg; |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 667 | struct diff_filespec *one; |
| 668 | struct diff_filespec *two; |
| 669 | const char *name; |
| 670 | const char *other; |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 671 | char *name_munged, *other_munged; |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 672 | int complete_rewrite = 0; |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 673 | int len; |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 674 | |
| 675 | if (DIFF_PAIR_UNMERGED(p)) { |
| 676 | /* unmerged */ |
| 677 | run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL, |
| 678 | 0); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | name = p->one->path; |
| 683 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 684 | name_munged = quote_one(name); |
| 685 | other_munged = quote_one(other); |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 686 | one = p->one; two = p->two; |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 687 | |
| 688 | diff_fill_sha1_info(one); |
| 689 | diff_fill_sha1_info(two); |
| 690 | |
| 691 | len = 0; |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 692 | switch (p->status) { |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 693 | case DIFF_STATUS_COPIED: |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 694 | len += snprintf(msg + len, sizeof(msg) - len, |
| 695 | "similarity index %d%%\n" |
| 696 | "copy from %s\n" |
| 697 | "copy to %s\n", |
| 698 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 699 | name_munged, other_munged); |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 700 | break; |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 701 | case DIFF_STATUS_RENAMED: |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 702 | len += snprintf(msg + len, sizeof(msg) - len, |
| 703 | "similarity index %d%%\n" |
| 704 | "rename from %s\n" |
| 705 | "rename to %s\n", |
| 706 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 707 | name_munged, other_munged); |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 708 | break; |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 709 | case DIFF_STATUS_MODIFIED: |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 710 | if (p->score) { |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 711 | len += snprintf(msg + len, sizeof(msg) - len, |
| 712 | "dissimilarity index %d%%\n", |
| 713 | (int)(0.5 + p->score * |
| 714 | 100.0/MAX_SCORE)); |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 715 | complete_rewrite = 1; |
| 716 | break; |
| 717 | } |
| 718 | /* fallthru */ |
| 719 | default: |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 720 | /* nothing */ |
| 721 | ; |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 724 | if (memcmp(one->sha1, two->sha1, 20)) { |
| 725 | char one_sha1[41]; |
Junio C Hamano | 80b1e51 | 2005-11-14 17:53:22 -0800 | [diff] [blame] | 726 | const char *index_fmt = o->full_index ? "index %s..%s" : "index %.7s..%.7s"; |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 727 | memcpy(one_sha1, sha1_to_hex(one->sha1), 41); |
| 728 | |
| 729 | len += snprintf(msg + len, sizeof(msg) - len, |
Junio C Hamano | 80b1e51 | 2005-11-14 17:53:22 -0800 | [diff] [blame] | 730 | index_fmt, one_sha1, sha1_to_hex(two->sha1)); |
Junio C Hamano | ec1fcc1 | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 731 | if (one->mode == two->mode) |
| 732 | len += snprintf(msg + len, sizeof(msg) - len, |
| 733 | " %06o", one->mode); |
| 734 | len += snprintf(msg + len, sizeof(msg) - len, "\n"); |
| 735 | } |
| 736 | |
| 737 | if (len) |
| 738 | msg[--len] = 0; |
| 739 | xfrm_msg = len ? msg : NULL; |
| 740 | |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 741 | if (!pgm && |
| 742 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && |
| 743 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { |
| 744 | /* a filepair that changes between file and symlink |
| 745 | * needs to be split into deletion and creation. |
| 746 | */ |
| 747 | struct diff_filespec *null = alloc_filespec(two->path); |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 748 | run_external_diff(NULL, name, other, one, null, xfrm_msg, 0); |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 749 | free(null); |
| 750 | null = alloc_filespec(one->path); |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 751 | run_external_diff(NULL, name, other, null, two, xfrm_msg, 0); |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 752 | free(null); |
| 753 | } |
| 754 | else |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 755 | run_external_diff(pgm, name, other, one, two, xfrm_msg, |
| 756 | complete_rewrite); |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 757 | |
| 758 | free(name_munged); |
| 759 | free(other_munged); |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 762 | void diff_setup(struct diff_options *options) |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 763 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 764 | memset(options, 0, sizeof(*options)); |
| 765 | options->output_format = DIFF_FORMAT_RAW; |
| 766 | options->line_termination = '\n'; |
| 767 | options->break_opt = -1; |
Junio C Hamano | 8082d8d | 2005-09-21 00:18:27 -0700 | [diff] [blame] | 768 | options->rename_limit = -1; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 769 | |
| 770 | options->change = diff_change; |
| 771 | options->add_remove = diff_addremove; |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | int diff_setup_done(struct diff_options *options) |
| 775 | { |
Junio C Hamano | 3299c6f | 2005-11-15 12:48:08 -0800 | [diff] [blame] | 776 | if ((options->find_copies_harder && |
| 777 | options->detect_rename != DIFF_DETECT_COPY) || |
| 778 | (0 <= options->rename_limit && !options->detect_rename)) |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 779 | return -1; |
Junio C Hamano | 3299c6f | 2005-11-15 12:48:08 -0800 | [diff] [blame] | 780 | if (options->detect_rename && options->rename_limit < 0) |
| 781 | options->rename_limit = diff_rename_limit_default; |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 782 | if (options->setup & DIFF_SETUP_USE_CACHE) { |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 783 | if (!active_cache) |
| 784 | /* read-cache does not die even when it fails |
| 785 | * so it is safe for us to do this here. Also |
| 786 | * it does not smudge active_cache or active_nr |
| 787 | * when it fails, so we do not have to worry about |
| 788 | * cleaning it up oufselves either. |
| 789 | */ |
| 790 | read_cache(); |
| 791 | } |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 792 | if (options->setup & DIFF_SETUP_USE_SIZE_CACHE) |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 793 | use_size_cache = 1; |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | int diff_opt_parse(struct diff_options *options, const char **av, int ac) |
| 799 | { |
| 800 | const char *arg = av[0]; |
| 801 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) |
| 802 | options->output_format = DIFF_FORMAT_PATCH; |
| 803 | else if (!strcmp(arg, "-z")) |
| 804 | options->line_termination = 0; |
Junio C Hamano | 8082d8d | 2005-09-21 00:18:27 -0700 | [diff] [blame] | 805 | else if (!strncmp(arg, "-l", 2)) |
| 806 | options->rename_limit = strtoul(arg+2, NULL, 10); |
Junio C Hamano | 80b1e51 | 2005-11-14 17:53:22 -0800 | [diff] [blame] | 807 | else if (!strcmp(arg, "--full-index")) |
| 808 | options->full_index = 1; |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 809 | else if (!strcmp(arg, "--name-only")) |
| 810 | options->output_format = DIFF_FORMAT_NAME; |
Junio C Hamano | 946f5f7 | 2005-09-21 00:20:06 -0700 | [diff] [blame] | 811 | else if (!strcmp(arg, "--name-status")) |
| 812 | options->output_format = DIFF_FORMAT_NAME_STATUS; |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 813 | else if (!strcmp(arg, "-R")) |
| 814 | options->reverse_diff = 1; |
| 815 | else if (!strncmp(arg, "-S", 2)) |
| 816 | options->pickaxe = arg + 2; |
| 817 | else if (!strcmp(arg, "-s")) |
| 818 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
| 819 | else if (!strncmp(arg, "-O", 2)) |
| 820 | options->orderfile = arg + 2; |
| 821 | else if (!strncmp(arg, "--diff-filter=", 14)) |
| 822 | options->filter = arg + 14; |
| 823 | else if (!strcmp(arg, "--pickaxe-all")) |
| 824 | options->pickaxe_opts = DIFF_PICKAXE_ALL; |
| 825 | else if (!strncmp(arg, "-B", 2)) { |
| 826 | if ((options->break_opt = |
| 827 | diff_scoreopt_parse(arg)) == -1) |
| 828 | return -1; |
| 829 | } |
| 830 | else if (!strncmp(arg, "-M", 2)) { |
| 831 | if ((options->rename_score = |
| 832 | diff_scoreopt_parse(arg)) == -1) |
| 833 | return -1; |
| 834 | options->detect_rename = DIFF_DETECT_RENAME; |
| 835 | } |
| 836 | else if (!strncmp(arg, "-C", 2)) { |
| 837 | if ((options->rename_score = |
| 838 | diff_scoreopt_parse(arg)) == -1) |
| 839 | return -1; |
| 840 | options->detect_rename = DIFF_DETECT_COPY; |
| 841 | } |
| 842 | else if (!strcmp(arg, "--find-copies-harder")) |
| 843 | options->find_copies_harder = 1; |
| 844 | else |
| 845 | return 0; |
| 846 | return 1; |
Junio C Hamano | 57fe64a | 2005-05-19 19:00:36 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 849 | static int parse_num(const char **cp_p) |
| 850 | { |
H. Peter Anvin | 1b1480f | 2005-11-21 14:17:12 -0800 | [diff] [blame] | 851 | unsigned long num, scale; |
| 852 | int ch, dot; |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 853 | const char *cp = *cp_p; |
| 854 | |
H. Peter Anvin | 1b1480f | 2005-11-21 14:17:12 -0800 | [diff] [blame] | 855 | num = 0; |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 856 | scale = 1; |
H. Peter Anvin | 1b1480f | 2005-11-21 14:17:12 -0800 | [diff] [blame] | 857 | dot = 0; |
| 858 | for(;;) { |
| 859 | ch = *cp; |
| 860 | if ( !dot && ch == '.' ) { |
| 861 | scale = 1; |
| 862 | dot = 1; |
| 863 | } else if ( ch == '%' ) { |
| 864 | scale = dot ? scale*100 : 100; |
| 865 | cp++; /* % is always at the end */ |
| 866 | break; |
| 867 | } else if ( ch >= '0' && ch <= '9' ) { |
| 868 | if ( scale < 100000 ) { |
| 869 | scale *= 10; |
| 870 | num = (num*10) + (ch-'0'); |
| 871 | } |
| 872 | } else { |
| 873 | break; |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 874 | } |
Junio C Hamano | 5de36bf | 2005-08-29 21:17:21 -0700 | [diff] [blame] | 875 | cp++; |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 876 | } |
| 877 | *cp_p = cp; |
| 878 | |
| 879 | /* user says num divided by scale and we say internally that |
| 880 | * is MAX_SCORE * num / scale. |
| 881 | */ |
H. Peter Anvin | 1b1480f | 2005-11-21 14:17:12 -0800 | [diff] [blame] | 882 | return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale); |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | int diff_scoreopt_parse(const char *opt) |
| 886 | { |
Junio C Hamano | eeaa460 | 2005-06-03 01:40:28 -0700 | [diff] [blame] | 887 | int opt1, opt2, cmd; |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 888 | |
| 889 | if (*opt++ != '-') |
| 890 | return -1; |
| 891 | cmd = *opt++; |
| 892 | if (cmd != 'M' && cmd != 'C' && cmd != 'B') |
| 893 | return -1; /* that is not a -M, -C nor -B option */ |
| 894 | |
| 895 | opt1 = parse_num(&opt); |
Junio C Hamano | eeaa460 | 2005-06-03 01:40:28 -0700 | [diff] [blame] | 896 | if (cmd != 'B') |
| 897 | opt2 = 0; |
| 898 | else { |
| 899 | if (*opt == 0) |
| 900 | opt2 = 0; |
| 901 | else if (*opt != '/') |
| 902 | return -1; /* we expect -B80/99 or -B80 */ |
| 903 | else { |
| 904 | opt++; |
| 905 | opt2 = parse_num(&opt); |
| 906 | } |
| 907 | } |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 908 | if (*opt != 0) |
| 909 | return -1; |
Junio C Hamano | eeaa460 | 2005-06-03 01:40:28 -0700 | [diff] [blame] | 910 | return opt1 | (opt2 << 16); |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 913 | struct diff_queue_struct diff_queued_diff; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 914 | |
Junio C Hamano | 6b14d7f | 2005-05-22 10:04:37 -0700 | [diff] [blame] | 915 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) |
| 916 | { |
| 917 | if (queue->alloc <= queue->nr) { |
| 918 | queue->alloc = alloc_nr(queue->alloc); |
| 919 | queue->queue = xrealloc(queue->queue, |
| 920 | sizeof(dp) * queue->alloc); |
| 921 | } |
| 922 | queue->queue[queue->nr++] = dp; |
| 923 | } |
| 924 | |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 925 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
Junio C Hamano | 6b14d7f | 2005-05-22 10:04:37 -0700 | [diff] [blame] | 926 | struct diff_filespec *one, |
| 927 | struct diff_filespec *two) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 928 | { |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 929 | struct diff_filepair *dp = xmalloc(sizeof(*dp)); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 930 | dp->one = one; |
| 931 | dp->two = two; |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 932 | dp->score = 0; |
Junio C Hamano | 2210100 | 2005-06-11 20:55:20 -0700 | [diff] [blame] | 933 | dp->status = 0; |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 934 | dp->source_stays = 0; |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 935 | dp->broken_pair = 0; |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 936 | if (queue) |
| 937 | diff_q(queue, dp); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 938 | return dp; |
| 939 | } |
| 940 | |
Junio C Hamano | 226406f | 2005-05-27 15:50:30 -0700 | [diff] [blame] | 941 | void diff_free_filepair(struct diff_filepair *p) |
| 942 | { |
Junio C Hamano | 19397b4 | 2005-09-14 14:06:50 -0700 | [diff] [blame] | 943 | diff_free_filespec_data(p->one); |
| 944 | diff_free_filespec_data(p->two); |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 945 | free(p->one); |
| 946 | free(p->two); |
Junio C Hamano | 226406f | 2005-05-27 15:50:30 -0700 | [diff] [blame] | 947 | free(p); |
| 948 | } |
| 949 | |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 950 | static void diff_flush_raw(struct diff_filepair *p, |
| 951 | int line_termination, |
Junio C Hamano | 946f5f7 | 2005-09-21 00:20:06 -0700 | [diff] [blame] | 952 | int inter_name_termination, |
| 953 | int output_format) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 954 | { |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 955 | int two_paths; |
| 956 | char status[10]; |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 957 | const char *path_one, *path_two; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 958 | |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 959 | path_one = p->one->path; |
| 960 | path_two = p->two->path; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 961 | if (line_termination) { |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 962 | path_one = quote_one(path_one); |
| 963 | path_two = quote_one(path_two); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Junio C Hamano | 366175e | 2005-06-19 13:17:50 -0700 | [diff] [blame] | 966 | if (p->score) |
| 967 | sprintf(status, "%c%03d", p->status, |
| 968 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); |
| 969 | else { |
| 970 | status[0] = p->status; |
| 971 | status[1] = 0; |
| 972 | } |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 973 | switch (p->status) { |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 974 | case DIFF_STATUS_COPIED: |
| 975 | case DIFF_STATUS_RENAMED: |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 976 | two_paths = 1; |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 977 | break; |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 978 | case DIFF_STATUS_ADDED: |
| 979 | case DIFF_STATUS_DELETED: |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 980 | two_paths = 0; |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 981 | break; |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 982 | default: |
| 983 | two_paths = 0; |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 984 | break; |
Junio C Hamano | 6b14d7f | 2005-05-22 10:04:37 -0700 | [diff] [blame] | 985 | } |
Junio C Hamano | 946f5f7 | 2005-09-21 00:20:06 -0700 | [diff] [blame] | 986 | if (output_format != DIFF_FORMAT_NAME_STATUS) { |
| 987 | printf(":%06o %06o %s ", |
| 988 | p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1)); |
| 989 | printf("%s ", sha1_to_hex(p->two->sha1)); |
| 990 | } |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 991 | printf("%s%c%s", status, inter_name_termination, path_one); |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 992 | if (two_paths) |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 993 | printf("%c%s", inter_name_termination, path_two); |
Junio C Hamano | b6d8f30 | 2005-05-23 14:55:33 -0700 | [diff] [blame] | 994 | putchar(line_termination); |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 995 | if (path_one != p->one->path) |
| 996 | free((void*)path_one); |
| 997 | if (path_two != p->two->path) |
| 998 | free((void*)path_two); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Junio C Hamano | 52f2852 | 2005-07-13 12:45:51 -0700 | [diff] [blame] | 1001 | static void diff_flush_name(struct diff_filepair *p, |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 1002 | int inter_name_termination, |
Junio C Hamano | 52f2852 | 2005-07-13 12:45:51 -0700 | [diff] [blame] | 1003 | int line_termination) |
| 1004 | { |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 1005 | char *path = p->two->path; |
| 1006 | |
| 1007 | if (line_termination) |
| 1008 | path = quote_one(p->two->path); |
| 1009 | else |
| 1010 | path = p->two->path; |
| 1011 | printf("%s%c", path, line_termination); |
| 1012 | if (p->two->path != path) |
| 1013 | free(path); |
Junio C Hamano | 52f2852 | 2005-07-13 12:45:51 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1016 | int diff_unmodified_pair(struct diff_filepair *p) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1017 | { |
| 1018 | /* This function is written stricter than necessary to support |
| 1019 | * the currently implemented transformers, but the idea is to |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 1020 | * let transformers to produce diff_filepairs any way they want, |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1021 | * and filter and clean them up here before producing the output. |
| 1022 | */ |
Junio C Hamano | 6b14d7f | 2005-05-22 10:04:37 -0700 | [diff] [blame] | 1023 | struct diff_filespec *one, *two; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1024 | |
Junio C Hamano | 6b14d7f | 2005-05-22 10:04:37 -0700 | [diff] [blame] | 1025 | if (DIFF_PAIR_UNMERGED(p)) |
| 1026 | return 0; /* unmerged is interesting */ |
| 1027 | |
| 1028 | one = p->one; |
| 1029 | two = p->two; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1030 | |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 1031 | /* deletion, addition, mode or type change |
| 1032 | * and rename are all interesting. |
| 1033 | */ |
Junio C Hamano | 81e50ea | 2005-05-21 19:42:18 -0700 | [diff] [blame] | 1034 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
Junio C Hamano | 4130b99 | 2005-05-26 02:24:30 -0700 | [diff] [blame] | 1035 | DIFF_PAIR_MODE_CHANGED(p) || |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1036 | strcmp(one->path, two->path)) |
| 1037 | return 0; |
| 1038 | |
| 1039 | /* both are valid and point at the same path. that is, we are |
| 1040 | * dealing with a change. |
| 1041 | */ |
| 1042 | if (one->sha1_valid && two->sha1_valid && |
| 1043 | !memcmp(one->sha1, two->sha1, sizeof(one->sha1))) |
| 1044 | return 1; /* no change */ |
| 1045 | if (!one->sha1_valid && !two->sha1_valid) |
| 1046 | return 1; /* both look at the same file on the filesystem. */ |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Junio C Hamano | 80b1e51 | 2005-11-14 17:53:22 -0800 | [diff] [blame] | 1050 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1051 | { |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1052 | if (diff_unmodified_pair(p)) |
| 1053 | return; |
| 1054 | |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1055 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 1056 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 1057 | return; /* no tree diffs in patch format */ |
| 1058 | |
Junio C Hamano | 80b1e51 | 2005-11-14 17:53:22 -0800 | [diff] [blame] | 1059 | run_diff(p, o); |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1062 | int diff_queue_is_empty(void) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1063 | { |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1064 | struct diff_queue_struct *q = &diff_queued_diff; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1065 | int i; |
| 1066 | for (i = 0; i < q->nr; i++) |
| 1067 | if (!diff_unmodified_pair(q->queue[i])) |
| 1068 | return 0; |
| 1069 | return 1; |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1072 | #if DIFF_DEBUG |
| 1073 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) |
| 1074 | { |
| 1075 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 1076 | x, one ? one : "", |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1077 | s->path, |
| 1078 | DIFF_FILE_VALID(s) ? "valid" : "invalid", |
| 1079 | s->mode, |
| 1080 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); |
| 1081 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 1082 | x, one ? one : "", |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1083 | s->size, s->xfrm_flags); |
| 1084 | } |
| 1085 | |
| 1086 | void diff_debug_filepair(const struct diff_filepair *p, int i) |
| 1087 | { |
| 1088 | diff_debug_filespec(p->one, i, "one"); |
| 1089 | diff_debug_filespec(p->two, i, "two"); |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1090 | fprintf(stderr, "score %d, status %c stays %d broken %d\n", |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 1091 | p->score, p->status ? p->status : '?', |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1092 | p->source_stays, p->broken_pair); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) |
| 1096 | { |
| 1097 | int i; |
| 1098 | if (msg) |
| 1099 | fprintf(stderr, "%s\n", msg); |
| 1100 | fprintf(stderr, "q->nr = %d\n", q->nr); |
| 1101 | for (i = 0; i < q->nr; i++) { |
| 1102 | struct diff_filepair *p = q->queue[i]; |
| 1103 | diff_debug_filepair(p, i); |
| 1104 | } |
| 1105 | } |
| 1106 | #endif |
| 1107 | |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1108 | static void diff_resolve_rename_copy(void) |
| 1109 | { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1110 | int i, j; |
| 1111 | struct diff_filepair *p, *pp; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1112 | struct diff_queue_struct *q = &diff_queued_diff; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1113 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1114 | diff_debug_queue("resolve-rename-copy", q); |
| 1115 | |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1116 | for (i = 0; i < q->nr; i++) { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1117 | p = q->queue[i]; |
Junio C Hamano | 96716a1 | 2005-05-25 15:07:08 -0700 | [diff] [blame] | 1118 | p->status = 0; /* undecided */ |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1119 | if (DIFF_PAIR_UNMERGED(p)) |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1120 | p->status = DIFF_STATUS_UNMERGED; |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1121 | else if (!DIFF_FILE_VALID(p->one)) |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1122 | p->status = DIFF_STATUS_ADDED; |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 1123 | else if (!DIFF_FILE_VALID(p->two)) |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1124 | p->status = DIFF_STATUS_DELETED; |
Junio C Hamano | 96716a1 | 2005-05-25 15:07:08 -0700 | [diff] [blame] | 1125 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1126 | p->status = DIFF_STATUS_TYPE_CHANGED; |
Junio C Hamano | 96716a1 | 2005-05-25 15:07:08 -0700 | [diff] [blame] | 1127 | |
| 1128 | /* from this point on, we are dealing with a pair |
| 1129 | * whose both sides are valid and of the same type, i.e. |
| 1130 | * either in-place edit or rename/copy edit. |
| 1131 | */ |
Junio C Hamano | 01c4e70 | 2005-05-29 16:56:48 -0700 | [diff] [blame] | 1132 | else if (DIFF_PAIR_RENAME(p)) { |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1133 | if (p->source_stays) { |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1134 | p->status = DIFF_STATUS_COPIED; |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1135 | continue; |
| 1136 | } |
| 1137 | /* See if there is some other filepair that |
| 1138 | * copies from the same source as us. If so |
Junio C Hamano | 6bac10d | 2005-09-10 12:42:32 -0700 | [diff] [blame] | 1139 | * we are a copy. Otherwise we are either a |
| 1140 | * copy if the path stays, or a rename if it |
| 1141 | * does not, but we already handled "stays" case. |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1142 | */ |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1143 | for (j = i + 1; j < q->nr; j++) { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1144 | pp = q->queue[j]; |
| 1145 | if (strcmp(pp->one->path, p->one->path)) |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1146 | continue; /* not us */ |
Junio C Hamano | 01c4e70 | 2005-05-29 16:56:48 -0700 | [diff] [blame] | 1147 | if (!DIFF_PAIR_RENAME(pp)) |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1148 | continue; /* not a rename/copy */ |
| 1149 | /* pp is a rename/copy from the same source */ |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1150 | p->status = DIFF_STATUS_COPIED; |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1151 | break; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1152 | } |
| 1153 | if (!p->status) |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1154 | p->status = DIFF_STATUS_RENAMED; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1155 | } |
Junio C Hamano | 9fdade0 | 2005-05-25 16:00:04 -0700 | [diff] [blame] | 1156 | else if (memcmp(p->one->sha1, p->two->sha1, 20) || |
| 1157 | p->one->mode != p->two->mode) |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1158 | p->status = DIFF_STATUS_MODIFIED; |
Junio C Hamano | 67574c4 | 2005-06-01 11:38:07 -0700 | [diff] [blame] | 1159 | else { |
| 1160 | /* This is a "no-change" entry and should not |
| 1161 | * happen anymore, but prepare for broken callers. |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1162 | */ |
Junio C Hamano | 67574c4 | 2005-06-01 11:38:07 -0700 | [diff] [blame] | 1163 | error("feeding unmodified %s to diffcore", |
| 1164 | p->one->path); |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1165 | p->status = DIFF_STATUS_UNKNOWN; |
Junio C Hamano | 67574c4 | 2005-06-01 11:38:07 -0700 | [diff] [blame] | 1166 | } |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1167 | } |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1168 | diff_debug_queue("resolve-rename-copy done", q); |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1171 | void diff_flush(struct diff_options *options) |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1172 | { |
| 1173 | struct diff_queue_struct *q = &diff_queued_diff; |
| 1174 | int i; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1175 | int inter_name_termination = '\t'; |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1176 | int diff_output_format = options->output_format; |
| 1177 | int line_termination = options->line_termination; |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1178 | |
Linus Torvalds | e68b6f1 | 2005-07-14 17:59:17 -0700 | [diff] [blame] | 1179 | if (!line_termination) |
| 1180 | inter_name_termination = 0; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1181 | |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1182 | for (i = 0; i < q->nr; i++) { |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1183 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1184 | if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) || |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1185 | (p->status == DIFF_STATUS_UNKNOWN)) |
Junio C Hamano | 96716a1 | 2005-05-25 15:07:08 -0700 | [diff] [blame] | 1186 | continue; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1187 | if (p->status == 0) |
Junio C Hamano | 96716a1 | 2005-05-25 15:07:08 -0700 | [diff] [blame] | 1188 | die("internal error in diff-resolve-rename-copy"); |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1189 | switch (diff_output_format) { |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1190 | case DIFF_FORMAT_PATCH: |
Junio C Hamano | 80b1e51 | 2005-11-14 17:53:22 -0800 | [diff] [blame] | 1191 | diff_flush_patch(p, options); |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1192 | break; |
Linus Torvalds | e68b6f1 | 2005-07-14 17:59:17 -0700 | [diff] [blame] | 1193 | case DIFF_FORMAT_RAW: |
Junio C Hamano | 946f5f7 | 2005-09-21 00:20:06 -0700 | [diff] [blame] | 1194 | case DIFF_FORMAT_NAME_STATUS: |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1195 | diff_flush_raw(p, line_termination, |
Junio C Hamano | 946f5f7 | 2005-09-21 00:20:06 -0700 | [diff] [blame] | 1196 | inter_name_termination, |
| 1197 | diff_output_format); |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1198 | break; |
Junio C Hamano | 52f2852 | 2005-07-13 12:45:51 -0700 | [diff] [blame] | 1199 | case DIFF_FORMAT_NAME: |
Junio C Hamano | cf9dfc6 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 1200 | diff_flush_name(p, |
| 1201 | inter_name_termination, |
| 1202 | line_termination); |
Junio C Hamano | 52f2852 | 2005-07-13 12:45:51 -0700 | [diff] [blame] | 1203 | break; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1204 | } |
Junio C Hamano | 226406f | 2005-05-27 15:50:30 -0700 | [diff] [blame] | 1205 | diff_free_filepair(q->queue[i]); |
Yasushi SHOJI | 90a734d | 2005-08-21 16:14:16 +0900 | [diff] [blame] | 1206 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1207 | free(q->queue); |
| 1208 | q->queue = NULL; |
| 1209 | q->nr = q->alloc = 0; |
| 1210 | } |
| 1211 | |
Junio C Hamano | f2ce9fd | 2005-06-11 20:57:13 -0700 | [diff] [blame] | 1212 | static void diffcore_apply_filter(const char *filter) |
| 1213 | { |
| 1214 | int i; |
| 1215 | struct diff_queue_struct *q = &diff_queued_diff; |
| 1216 | struct diff_queue_struct outq; |
| 1217 | outq.queue = NULL; |
| 1218 | outq.nr = outq.alloc = 0; |
| 1219 | |
| 1220 | if (!filter) |
| 1221 | return; |
| 1222 | |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1223 | if (strchr(filter, DIFF_STATUS_FILTER_AON)) { |
Junio C Hamano | f2ce9fd | 2005-06-11 20:57:13 -0700 | [diff] [blame] | 1224 | int found; |
| 1225 | for (i = found = 0; !found && i < q->nr; i++) { |
| 1226 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1227 | if (((p->status == DIFF_STATUS_MODIFIED) && |
| 1228 | ((p->score && |
| 1229 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || |
| 1230 | (!p->score && |
| 1231 | strchr(filter, DIFF_STATUS_MODIFIED)))) || |
| 1232 | ((p->status != DIFF_STATUS_MODIFIED) && |
| 1233 | strchr(filter, p->status))) |
Junio C Hamano | f2ce9fd | 2005-06-11 20:57:13 -0700 | [diff] [blame] | 1234 | found++; |
| 1235 | } |
| 1236 | if (found) |
| 1237 | return; |
| 1238 | |
| 1239 | /* otherwise we will clear the whole queue |
| 1240 | * by copying the empty outq at the end of this |
| 1241 | * function, but first clear the current entries |
| 1242 | * in the queue. |
| 1243 | */ |
| 1244 | for (i = 0; i < q->nr; i++) |
| 1245 | diff_free_filepair(q->queue[i]); |
| 1246 | } |
| 1247 | else { |
| 1248 | /* Only the matching ones */ |
| 1249 | for (i = 0; i < q->nr; i++) { |
| 1250 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | e7baa4f | 2005-07-25 13:05:44 -0700 | [diff] [blame] | 1251 | |
| 1252 | if (((p->status == DIFF_STATUS_MODIFIED) && |
| 1253 | ((p->score && |
| 1254 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || |
| 1255 | (!p->score && |
| 1256 | strchr(filter, DIFF_STATUS_MODIFIED)))) || |
| 1257 | ((p->status != DIFF_STATUS_MODIFIED) && |
| 1258 | strchr(filter, p->status))) |
Junio C Hamano | f2ce9fd | 2005-06-11 20:57:13 -0700 | [diff] [blame] | 1259 | diff_q(&outq, p); |
| 1260 | else |
| 1261 | diff_free_filepair(p); |
| 1262 | } |
| 1263 | } |
| 1264 | free(q->queue); |
| 1265 | *q = outq; |
| 1266 | } |
| 1267 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1268 | void diffcore_std(struct diff_options *options) |
Junio C Hamano | befe863 | 2005-05-29 16:56:13 -0700 | [diff] [blame] | 1269 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1270 | if (options->paths && options->paths[0]) |
| 1271 | diffcore_pathspec(options->paths); |
| 1272 | if (options->break_opt != -1) |
| 1273 | diffcore_break(options->break_opt); |
| 1274 | if (options->detect_rename) |
Junio C Hamano | 8082d8d | 2005-09-21 00:18:27 -0700 | [diff] [blame] | 1275 | diffcore_rename(options); |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1276 | if (options->break_opt != -1) |
Junio C Hamano | eeaa460 | 2005-06-03 01:40:28 -0700 | [diff] [blame] | 1277 | diffcore_merge_broken(); |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1278 | if (options->pickaxe) |
| 1279 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); |
| 1280 | if (options->orderfile) |
| 1281 | diffcore_order(options->orderfile); |
Junio C Hamano | f2ce9fd | 2005-06-11 20:57:13 -0700 | [diff] [blame] | 1282 | diff_resolve_rename_copy(); |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1283 | diffcore_apply_filter(options->filter); |
Junio C Hamano | f2ce9fd | 2005-06-11 20:57:13 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1287 | void diffcore_std_no_resolve(struct diff_options *options) |
Junio C Hamano | f2ce9fd | 2005-06-11 20:57:13 -0700 | [diff] [blame] | 1288 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1289 | if (options->pickaxe) |
| 1290 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); |
| 1291 | if (options->orderfile) |
| 1292 | diffcore_order(options->orderfile); |
| 1293 | diffcore_apply_filter(options->filter); |
Junio C Hamano | befe863 | 2005-05-29 16:56:13 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1296 | void diff_addremove(struct diff_options *options, |
| 1297 | int addremove, unsigned mode, |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 1298 | const unsigned char *sha1, |
| 1299 | const char *base, const char *path) |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1300 | { |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 1301 | char concatpath[PATH_MAX]; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1302 | struct diff_filespec *one, *two; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1303 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1304 | /* This may look odd, but it is a preparation for |
| 1305 | * feeding "there are unchanged files which should |
| 1306 | * not produce diffs, but when you are doing copy |
| 1307 | * detection you would need them, so here they are" |
| 1308 | * entries to the diff-core. They will be prefixed |
| 1309 | * with something like '=' or '*' (I haven't decided |
| 1310 | * which but should not make any difference). |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 1311 | * Feeding the same new and old to diff_change() |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 1312 | * also has the same effect. |
| 1313 | * Before the final output happens, they are pruned after |
| 1314 | * merged into rename/copy pairs as appropriate. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1315 | */ |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1316 | if (options->reverse_diff) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1317 | addremove = (addremove == '+' ? '-' : |
| 1318 | addremove == '-' ? '+' : addremove); |
Junio C Hamano | 7ca4525 | 2005-05-20 09:54:07 -0700 | [diff] [blame] | 1319 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1320 | if (!path) path = ""; |
| 1321 | sprintf(concatpath, "%s%s", base, path); |
| 1322 | one = alloc_filespec(concatpath); |
| 1323 | two = alloc_filespec(concatpath); |
Junio C Hamano | 57fe64a | 2005-05-19 19:00:36 -0700 | [diff] [blame] | 1324 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1325 | if (addremove != '+') |
| 1326 | fill_filespec(one, sha1, mode); |
| 1327 | if (addremove != '-') |
| 1328 | fill_filespec(two, sha1, mode); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1329 | |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1330 | diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1333 | void diff_change(struct diff_options *options, |
| 1334 | unsigned old_mode, unsigned new_mode, |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 1335 | const unsigned char *old_sha1, |
| 1336 | const unsigned char *new_sha1, |
Junio C Hamano | 81e50ea | 2005-05-21 19:42:18 -0700 | [diff] [blame] | 1337 | const char *base, const char *path) |
| 1338 | { |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 1339 | char concatpath[PATH_MAX]; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1340 | struct diff_filespec *one, *two; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1341 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1342 | if (options->reverse_diff) { |
Junio C Hamano | 7ca4525 | 2005-05-20 09:54:07 -0700 | [diff] [blame] | 1343 | unsigned tmp; |
| 1344 | const unsigned char *tmp_c; |
| 1345 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; |
| 1346 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; |
| 1347 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1348 | if (!path) path = ""; |
| 1349 | sprintf(concatpath, "%s%s", base, path); |
| 1350 | one = alloc_filespec(concatpath); |
| 1351 | two = alloc_filespec(concatpath); |
| 1352 | fill_filespec(one, old_sha1, old_mode); |
| 1353 | fill_filespec(two, new_sha1, new_mode); |
Junio C Hamano | 7ca4525 | 2005-05-20 09:54:07 -0700 | [diff] [blame] | 1354 | |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1355 | diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 1356 | } |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1357 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 1358 | void diff_unmerge(struct diff_options *options, |
| 1359 | const char *path) |
Junio C Hamano | 77eb272 | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 1360 | { |
Junio C Hamano | 6b14d7f | 2005-05-22 10:04:37 -0700 | [diff] [blame] | 1361 | struct diff_filespec *one, *two; |
| 1362 | one = alloc_filespec(path); |
| 1363 | two = alloc_filespec(path); |
| 1364 | diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 1365 | } |