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