+ if (gitenv("GIT_EXTERNAL_DIFF"))
+ external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
+
+ /* In case external diff fails... */
+ diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
+
+ done_preparing = 1;
+ return external_diff_cmd;
+}
+
+static struct diff_tempfile {
+ const char *name; /* filename external diff should read from */
+ char hex[41];
+ char mode[10];
+ char tmp_path[50];
+} diff_temp[2];
+
+static int count_lines(const char *filename)
+{
+ FILE *in;
+ int count, ch, completely_empty = 1, nl_just_seen = 0;
+ in = fopen(filename, "r");
+ count = 0;
+ while ((ch = fgetc(in)) != EOF)
+ if (ch == '\n') {
+ count++;
+ nl_just_seen = 1;
+ completely_empty = 0;
+ }
+ else {
+ nl_just_seen = 0;
+ completely_empty = 0;
+ }
+ fclose(in);
+ if (completely_empty)
+ return 0;
+ if (!nl_just_seen)
+ count++; /* no trailing newline */
+ return count;
+}
+
+static void print_line_count(int count)
+{
+ switch (count) {
+ case 0:
+ printf("0,0");
+ break;
+ case 1:
+ printf("1");
+ break;
+ default:
+ printf("1,%d", count);
+ break;
+ }
+}
+
+static void copy_file(int prefix, const char *filename)
+{
+ FILE *in;
+ int ch, nl_just_seen = 1;
+ in = fopen(filename, "r");
+ while ((ch = fgetc(in)) != EOF) {
+ if (nl_just_seen)
+ putchar(prefix);
+ putchar(ch);
+ if (ch == '\n')
+ nl_just_seen = 1;
+ else
+ nl_just_seen = 0;
+ }
+ fclose(in);
+ if (!nl_just_seen)
+ printf("\n\\ No newline at end of file\n");
+}
+
+static void emit_rewrite_diff(const char *name_a,
+ const char *name_b,
+ struct diff_tempfile *temp)
+{
+ /* Use temp[i].name as input, name_a and name_b as labels */
+ int lc_a, lc_b;
+ lc_a = count_lines(temp[0].name);
+ lc_b = count_lines(temp[1].name);
+ printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
+ print_line_count(lc_a);
+ printf(" +");
+ print_line_count(lc_b);
+ printf(" @@\n");
+ if (lc_a)
+ copy_file('-', temp[0].name);
+ if (lc_b)
+ copy_file('+', temp[1].name);
+}
+
+static void builtin_diff(const char *name_a,
+ const char *name_b,
+ struct diff_tempfile *temp,
+ const char *xfrm_msg,
+ int complete_rewrite)
+{
+ int i, next_at, cmd_size;
+ const char *diff_cmd = "diff -L%s%s -L%s%s";
+ const char *diff_arg = "%s %s||:"; /* "||:" is to return 0 */
+ const char *input_name_sq[2];
+ const char *path0[2];
+ const char *path1[2];
+ const char *name_sq[2];
+ char *cmd;
+
+ name_sq[0] = sq_quote(name_a);
+ name_sq[1] = sq_quote(name_b);
+
+ /* diff_cmd and diff_arg have 6 %s in total which makes
+ * the sum of these strings 12 bytes larger than required.
+ * we use 2 spaces around diff-opts, and we need to count
+ * terminating NUL, so we subtract 9 here.
+ */
+ cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
+ strlen(diff_arg) - 9);
+ for (i = 0; i < 2; i++) {
+ input_name_sq[i] = sq_quote(temp[i].name);
+ if (!strcmp(temp[i].name, "/dev/null")) {
+ path0[i] = "/dev/null";
+ path1[i] = "";
+ } else {
+ path0[i] = i ? "b/" : "a/";
+ path1[i] = name_sq[i];
+ }
+ cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
+ strlen(input_name_sq[i]));
+ }
+
+ cmd = xmalloc(cmd_size);
+
+ next_at = 0;
+ next_at += snprintf(cmd+next_at, cmd_size-next_at,
+ diff_cmd,
+ path0[0], path1[0], path0[1], path1[1]);
+ next_at += snprintf(cmd+next_at, cmd_size-next_at,
+ " %s ", diff_opts);
+ next_at += snprintf(cmd+next_at, cmd_size-next_at,
+ diff_arg, input_name_sq[0], input_name_sq[1]);
+
+ printf("diff --git a/%s b/%s\n", name_a, name_b);
+ if (!path1[0][0]) {
+ printf("new file mode %s\n", temp[1].mode);
+ if (xfrm_msg && xfrm_msg[0])
+ puts(xfrm_msg);
+ }
+ else if (!path1[1][0]) {
+ printf("deleted file mode %s\n", temp[0].mode);
+ if (xfrm_msg && xfrm_msg[0])
+ puts(xfrm_msg);
+ }
+ else {
+ if (strcmp(temp[0].mode, temp[1].mode)) {
+ printf("old mode %s\n", temp[0].mode);
+ printf("new mode %s\n", temp[1].mode);
+ }
+ if (xfrm_msg && xfrm_msg[0])
+ puts(xfrm_msg);
+ if (strncmp(temp[0].mode, temp[1].mode, 3))
+ /* we do not run diff between different kind
+ * of objects.
+ */
+ exit(0);
+ if (complete_rewrite) {
+ fflush(NULL);
+ emit_rewrite_diff(name_a, name_b, temp);
+ exit(0);
+ }
+ }
+ fflush(NULL);
+ execlp("/bin/sh","sh", "-c", cmd, NULL);
+}
+
+struct diff_filespec *alloc_filespec(const char *path)
+{
+ int namelen = strlen(path);
+ struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
+ spec->path = (char *)(spec + 1);
+ strcpy(spec->path, path);
+ spec->should_free = spec->should_munmap = 0;
+ spec->xfrm_flags = 0;
+ spec->size = 0;
+ spec->data = NULL;
+ spec->mode = 0;
+ memset(spec->sha1, 0, 20);
+ return spec;
+}
+
+void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
+ unsigned short mode)
+{
+ if (mode) {
+ spec->mode = DIFF_FILE_CANON_MODE(mode);
+ memcpy(spec->sha1, sha1, 20);
+ spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
+ }
+}
+
+/*
+ * Given a name and sha1 pair, if the dircache tells us the file in
+ * the work tree has that object contents, return true, so that
+ * prepare_temp_file() does not have to inflate and extract.
+ */
+static int work_tree_matches(const char *name, const unsigned char *sha1)
+{
+ struct cache_entry *ce;
+ struct stat st;
+ int pos, len;
+
+ /* We do not read the cache ourselves here, because the
+ * benchmark with my previous version that always reads cache
+ * shows that it makes things worse for diff-tree comparing
+ * two linux-2.6 kernel trees in an already checked out work
+ * tree. This is because most diff-tree comparisons deal with
+ * only a small number of files, while reading the cache is
+ * expensive for a large project, and its cost outweighs the
+ * savings we get by not inflating the object to a temporary
+ * file. Practically, this code only helps when we are used
+ * by diff-cache --cached, which does read the cache before
+ * calling us.
+ */
+ if (!active_cache)
+ return 0;
+
+ len = strlen(name);
+ pos = cache_name_pos(name, len);
+ if (pos < 0)
+ return 0;
+ ce = active_cache[pos];
+ if ((lstat(name, &st) < 0) ||
+ !S_ISREG(st.st_mode) || /* careful! */
+ ce_match_stat(ce, &st) ||
+ memcmp(sha1, ce->sha1, 20))
+ return 0;
+ /* we return 1 only when we can stat, it is a regular file,
+ * stat information matches, and sha1 recorded in the cache
+ * matches. I.e. we know the file in the work tree really is
+ * the same as the <name, sha1> pair.
+ */
+ return 1;
+}
+
+static struct sha1_size_cache {
+ unsigned char sha1[20];
+ unsigned long size;
+} **sha1_size_cache;
+static int sha1_size_cache_nr, sha1_size_cache_alloc;
+
+static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
+ int find_only,
+ unsigned long size)
+{
+ int first, last;
+ struct sha1_size_cache *e;
+
+ first = 0;
+ last = sha1_size_cache_nr;
+ while (last > first) {
+ int cmp, next = (last + first) >> 1;
+ e = sha1_size_cache[next];
+ cmp = memcmp(e->sha1, sha1, 20);
+ if (!cmp)
+ return e;
+ if (cmp < 0) {
+ last = next;
+ continue;
+ }
+ first = next+1;
+ }
+ /* not found */
+ if (find_only)
+ return NULL;
+ /* insert to make it at "first" */
+ if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
+ sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
+ sha1_size_cache = xrealloc(sha1_size_cache,
+ sha1_size_cache_alloc *
+ sizeof(*sha1_size_cache));
+ }
+ sha1_size_cache_nr++;
+ if (first < sha1_size_cache_nr)
+ memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
+ (sha1_size_cache_nr - first - 1) *
+ sizeof(*sha1_size_cache));
+ e = xmalloc(sizeof(struct sha1_size_cache));
+ sha1_size_cache[first] = e;
+ memcpy(e->sha1, sha1, 20);
+ e->size = size;
+ return e;