2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static char *diff_cmd
= "diff -L '%s' -u -N - '%s'";
10 /* Help to copy the thing properly quoted for the shell safety.
11 * any single quote is replaced with '\'', and the caller is
12 * expected to enclose the result within a single quote pair.
15 * original sq_expand result
16 * name ==> name ==> 'name'
17 * a b ==> a b ==> 'a b'
18 * a'b ==> a'\''b ==> 'a'\''b'
20 * NOTE! The returned memory belongs to this function so
23 static char *sq_expand(char *src
)
25 static char *buf
= NULL
;
26 static int buf_size
= -1;
30 /* count single quote characters */
31 for (cnt
= 0, cp
= src
; *cp
; cnt
++, cp
++)
42 while ((c
= *src
++)) {
46 cp
= strcpy(cp
, "'\\''");
54 static void show_differences(char *name
, void *old_contents
,
55 unsigned long long old_size
)
58 static char *cmd
= NULL
;
59 static int cmd_size
= -1;
61 char *name_sq
= sq_expand(name
);
62 int cmd_required_length
= strlen(name_sq
) * 2 + strlen(diff_cmd
);
64 if (cmd_size
< cmd_required_length
) {
66 cmd_size
= cmd_required_length
;
67 cmd
= malloc(cmd_required_length
);
69 snprintf(cmd
, cmd_size
, diff_cmd
, name_sq
, name_sq
);
72 fwrite(old_contents
, old_size
, 1, f
);
76 static void show_diff_empty(struct cache_entry
*ce
)
79 unsigned long int size
;
81 unsigned char type
[20], *p
, *end
;
83 old
= read_sha1_file(ce
->sha1
, type
, &size
);
88 printf("--- %s\n", ce
->name
);
89 printf("+++ /dev/null\n");
95 printf("@@ -1,%d +0,0 @@\n", lines
);
113 static const char *show_diff_usage
= "show-diff [-q] [-s] [-z] [paths...]";
115 static int matches_pathspec(struct cache_entry
*ce
, char **spec
, int cnt
)
118 int namelen
= ce_namelen(ce
);
119 for (i
= 0; i
< cnt
; i
++) {
120 int speclen
= strlen(spec
[i
]);
121 if (! strncmp(spec
[i
], ce
->name
, speclen
) &&
122 speclen
<= namelen
&&
123 (ce
->name
[speclen
] == 0 ||
124 ce
->name
[speclen
] == '/'))
130 int main(int argc
, char **argv
)
133 int silent_on_nonexisting_files
= 0;
134 int machine_readable
= 0;
135 int entries
= read_cache();
138 while (1 < argc
&& argv
[1][0] == '-') {
139 if (!strcmp(argv
[1], "-s"))
140 silent_on_nonexisting_files
= silent
= 1;
141 else if (!strcmp(argv
[1], "-q"))
142 silent_on_nonexisting_files
= 1;
143 else if (!strcmp(argv
[1], "-z"))
144 machine_readable
= 1;
146 usage(show_diff_usage
);
150 /* At this point, if argc == 1, then we are doing everything.
151 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
154 perror("read_cache");
157 for (i
= 0; i
< entries
; i
++) {
159 struct cache_entry
*ce
= active_cache
[i
];
166 ! matches_pathspec(ce
, argv
+1, argc
-1))
170 if (machine_readable
)
171 printf("U %s%c", ce
->name
, 0);
173 printf("%s: Unmerged\n",
175 while (i
< entries
&&
176 !strcmp(ce
->name
, active_cache
[i
]->name
))
178 i
--; /* compensate for loop control increments */
182 if (stat(ce
->name
, &st
) < 0) {
183 if (errno
== ENOENT
&& silent_on_nonexisting_files
)
185 if (machine_readable
)
186 printf("X %s%c", ce
->name
, 0);
188 printf("%s: %s\n", ce
->name
, strerror(errno
));
194 changed
= cache_match_stat(ce
, &st
);
197 if (!machine_readable
)
198 printf("%s: %s\n", ce
->name
, sha1_to_hex(ce
->sha1
));
200 printf("%s %s%c", sha1_to_hex(ce
->sha1
), ce
->name
, 0);
207 old
= read_sha1_file(ce
->sha1
, type
, &size
);
208 show_differences(ce
->name
, old
, size
);