]> git.ipfire.org Git - thirdparty/git.git/blame - show-diff.c
[PATCH] Un unoptimize ls-tree behaviour
[thirdparty/git.git] / show-diff.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
aebb2679 8static void show_differences(char *name,
e83c5163
LT
9 void *old_contents, unsigned long long old_size)
10{
11 static char cmd[1000];
12 FILE *f;
13
c0fb976a 14 snprintf(cmd, sizeof(cmd), "diff -L %s -u -N - %s", name, name);
e83c5163 15 f = popen(cmd, "w");
c0fb976a
CL
16 if (old_size)
17 fwrite(old_contents, old_size, 1, f);
e83c5163
LT
18 pclose(f);
19}
20
c0fb976a
CL
21static void show_diff_empty(struct cache_entry *ce)
22{
23 char *old;
24 unsigned long int size;
25 int lines=0;
26 unsigned char type[20], *p, *end;
27
28 old = read_sha1_file(ce->sha1, type, &size);
29 if (size > 0) {
30 int startline = 1;
31 int c = 0;
32
33 printf("--- %s\n", ce->name);
c57a3a91 34 printf("+++ /dev/null\n");
c0fb976a 35 p = old;
aebb2679 36 end = old + size;
c0fb976a
CL
37 while (p < end)
38 if (*p++ == '\n')
39 lines ++;
40 printf("@@ -1,%d +0,0 @@\n", lines);
41 p = old;
42 while (p < end) {
43 c = *p++;
44 if (startline) {
45 putchar('-');
46 startline = 0;
aebb2679 47 }
c0fb976a
CL
48 putchar(c);
49 if (c == '\n')
50 startline = 1;
51 }
52 if (c!='\n')
53 printf("\n");
54 fflush(stdout);
55 }
56}
57
e83c5163
LT
58int main(int argc, char **argv)
59{
e2e5e98a 60 int silent = 0;
ca2a0798 61 int silent_on_nonexisting_files = 0;
e83c5163
LT
62 int entries = read_cache();
63 int i;
64
ca2a0798
JH
65 for (i = 1; i < argc; i++) {
66 if (!strcmp(argv[i], "-s")) {
67 silent_on_nonexisting_files = silent = 1;
e2e5e98a
PB
68 continue;
69 }
ca2a0798
JH
70 if (!strcmp(argv[i], "-q")) {
71 silent_on_nonexisting_files = 1;
72 continue;
73 }
74 usage("show-diff [-s] [-q]");
e2e5e98a
PB
75 }
76
e83c5163
LT
77 if (entries < 0) {
78 perror("read_cache");
79 exit(1);
80 }
81 for (i = 0; i < entries; i++) {
82 struct stat st;
83 struct cache_entry *ce = active_cache[i];
84 int n, changed;
e83c5163
LT
85 unsigned long size;
86 char type[20];
87 void *new;
88
89 if (stat(ce->name, &st) < 0) {
ca2a0798
JH
90 if (errno == ENOENT && silent_on_nonexisting_files)
91 continue;
e83c5163 92 printf("%s: %s\n", ce->name, strerror(errno));
ca2a0798 93 if (errno == ENOENT)
c0fb976a 94 show_diff_empty(ce);
e83c5163
LT
95 continue;
96 }
734aab75 97 changed = cache_match_stat(ce, &st);
5e76011c 98 if (!changed)
e83c5163 99 continue;
13e897e5 100 printf("%s: ", ce->name);
e83c5163
LT
101 for (n = 0; n < 20; n++)
102 printf("%02x", ce->sha1[n]);
103 printf("\n");
c0fb976a 104 fflush(stdout);
e2e5e98a
PB
105 if (silent)
106 continue;
107
e83c5163 108 new = read_sha1_file(ce->sha1, type, &size);
c0fb976a 109 show_differences(ce->name, new, size);
e83c5163
LT
110 free(new);
111 }
112 return 0;
113}