2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static char *diff_cmd
= "diff -L 'a/%s' -L 'b/%s' ";
9 static char *diff_opts
= "-p -u";
10 static char *diff_arg_forward
= " - '%s'";
11 static char *diff_arg_reverse
= " '%s' -";
13 static void prepare_diff_cmd(void)
16 * Default values above are meant to match the
17 * Linux kernel development style. Examples of
18 * alternative styles you can specify via environment
21 * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
24 diff_cmd
= getenv("GIT_DIFF_CMD") ? : diff_cmd
;
25 diff_opts
= getenv("GIT_DIFF_OPTS") ? : diff_opts
;
28 /* Help to copy the thing properly quoted for the shell safety.
29 * any single quote is replaced with '\'', and the caller is
30 * expected to enclose the result within a single quote pair.
33 * original sq_expand result
34 * name ==> name ==> 'name'
35 * a b ==> a b ==> 'a b'
36 * a'b ==> a'\''b ==> 'a'\''b'
38 static char *sq_expand(char *src
)
40 static char *buf
= NULL
;
44 /* count bytes needed to store the quoted string. */
45 for (cnt
= 1, cp
= src
; *cp
; cnt
++, cp
++)
49 if (! (buf
= malloc(cnt
)))
52 while ((c
= *src
++)) {
56 cp
= strcpy(cp
, "'\\''");
64 static void show_differences(char *name
, char *label
, void *old_contents
,
65 unsigned long long old_size
, int reverse
)
68 char *name_sq
= sq_expand(name
);
69 char *label_sq
= (name
!= label
) ? sq_expand(label
) : name_sq
;
70 char *diff_arg
= reverse
? diff_arg_reverse
: diff_arg_forward
;
71 int cmd_size
= strlen(name_sq
) + strlen(label_sq
) * 2 +
72 strlen(diff_cmd
) + strlen(diff_opts
) + strlen(diff_arg
);
73 char *cmd
= malloc(cmd_size
);
77 next_at
= snprintf(cmd
, cmd_size
, diff_cmd
, label_sq
, label_sq
);
78 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
, "%s", diff_opts
);
79 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
, diff_arg
, name_sq
);
82 fwrite(old_contents
, old_size
, 1, f
);
84 if (label_sq
!= name_sq
)
90 static void show_diff_empty(struct cache_entry
*ce
, int reverse
)
93 unsigned long int size
;
94 unsigned char type
[20];
96 old
= read_sha1_file(ce
->sha1
, type
, &size
);
98 error("unable to read blob object for %s (%s)", ce
->name
,
99 sha1_to_hex(ce
->sha1
));
102 show_differences("/dev/null", ce
->name
, old
, size
, reverse
);
105 static const char *show_diff_usage
= "show-diff [-q] [-s] [-z] [paths...]";
107 static int matches_pathspec(struct cache_entry
*ce
, char **spec
, int cnt
)
110 int namelen
= ce_namelen(ce
);
111 for (i
= 0; i
< cnt
; i
++) {
112 int speclen
= strlen(spec
[i
]);
113 if (! strncmp(spec
[i
], ce
->name
, speclen
) &&
114 speclen
<= namelen
&&
115 (ce
->name
[speclen
] == 0 ||
116 ce
->name
[speclen
] == '/'))
122 int main(int argc
, char **argv
)
125 int silent_on_nonexisting_files
= 0;
126 int machine_readable
= 0;
128 int entries
= read_cache();
131 while (1 < argc
&& argv
[1][0] == '-') {
132 if (!strcmp(argv
[1], "-R"))
134 else if (!strcmp(argv
[1], "-s"))
135 silent_on_nonexisting_files
= silent
= 1;
136 else if (!strcmp(argv
[1], "-q"))
137 silent_on_nonexisting_files
= 1;
138 else if (!strcmp(argv
[1], "-z"))
139 machine_readable
= 1;
141 usage(show_diff_usage
);
145 /* At this point, if argc == 1, then we are doing everything.
146 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
149 perror("read_cache");
153 for (i
= 0; i
< entries
; i
++) {
155 struct cache_entry
*ce
= active_cache
[i
];
162 ! matches_pathspec(ce
, argv
+1, argc
-1))
166 if (machine_readable
)
167 printf("U %s%c", ce
->name
, 0);
169 printf("%s: Unmerged\n",
171 while (i
< entries
&&
172 !strcmp(ce
->name
, active_cache
[i
]->name
))
174 i
--; /* compensate for loop control increments */
178 if (stat(ce
->name
, &st
) < 0) {
179 if (errno
== ENOENT
&& silent_on_nonexisting_files
)
181 if (machine_readable
)
182 printf("X %s%c", ce
->name
, 0);
184 printf("%s: %s\n", ce
->name
, strerror(errno
));
186 show_diff_empty(ce
, reverse
);
190 changed
= cache_match_stat(ce
, &st
);
193 if (!machine_readable
)
194 printf("%s: %s\n", ce
->name
, sha1_to_hex(ce
->sha1
));
196 printf("%s %s%c", sha1_to_hex(ce
->sha1
), ce
->name
, 0);
202 old
= read_sha1_file(ce
->sha1
, type
, &size
);
204 error("unable to read blob object for %s (%s)",
205 ce
->name
, sha1_to_hex(ce
->sha1
));
207 show_differences(ce
->name
, ce
->name
, old
, size
,