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