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 * NOTE! The returned memory belongs to this function so
41 static char *sq_expand(char *src
)
43 static char *buf
= NULL
;
47 /* count bytes needed to store the quoted string. */
48 for (cnt
= 1, cp
= src
; *cp
; cnt
++, cp
++)
52 if (! (buf
= malloc(cnt
)))
55 while ((c
= *src
++)) {
59 cp
= strcpy(cp
, "'\\''");
67 static void show_differences(char *name
, char *label
, void *old_contents
,
68 unsigned long long old_size
, int reverse
)
71 char *name_sq
= sq_expand(name
);
72 char *label_sq
= (name
!= label
) ? sq_expand(label
) : name_sq
;
73 char *diff_arg
= reverse
? diff_arg_reverse
: diff_arg_forward
;
74 int cmd_size
= strlen(name_sq
) + strlen(label_sq
) * 2 +
75 strlen(diff_cmd
) + strlen(diff_opts
) + strlen(diff_arg
);
76 char *cmd
= malloc(cmd_size
);
80 next_at
= snprintf(cmd
, cmd_size
, diff_cmd
, label_sq
, label_sq
);
81 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
, "%s", diff_opts
);
82 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
, diff_arg
, name_sq
);
85 fwrite(old_contents
, old_size
, 1, f
);
87 if (label_sq
!= name_sq
)
93 static void show_diff_empty(struct cache_entry
*ce
, int reverse
)
96 unsigned long int size
;
97 unsigned char type
[20];
99 old
= read_sha1_file(ce
->sha1
, type
, &size
);
101 error("unable to read blob object for %s (%s)", ce
->name
,
102 sha1_to_hex(ce
->sha1
));
105 show_differences("/dev/null", ce
->name
, old
, size
, reverse
);
108 static const char *show_diff_usage
= "show-diff [-q] [-s] [-z] [paths...]";
110 static int matches_pathspec(struct cache_entry
*ce
, char **spec
, int cnt
)
113 int namelen
= ce_namelen(ce
);
114 for (i
= 0; i
< cnt
; i
++) {
115 int speclen
= strlen(spec
[i
]);
116 if (! strncmp(spec
[i
], ce
->name
, speclen
) &&
117 speclen
<= namelen
&&
118 (ce
->name
[speclen
] == 0 ||
119 ce
->name
[speclen
] == '/'))
125 int main(int argc
, char **argv
)
128 int silent_on_nonexisting_files
= 0;
129 int machine_readable
= 0;
131 int entries
= read_cache();
134 while (1 < argc
&& argv
[1][0] == '-') {
135 if (!strcmp(argv
[1], "-R"))
137 else if (!strcmp(argv
[1], "-s"))
138 silent_on_nonexisting_files
= silent
= 1;
139 else if (!strcmp(argv
[1], "-q"))
140 silent_on_nonexisting_files
= 1;
141 else if (!strcmp(argv
[1], "-z"))
142 machine_readable
= 1;
144 usage(show_diff_usage
);
148 /* At this point, if argc == 1, then we are doing everything.
149 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
152 perror("read_cache");
156 for (i
= 0; i
< entries
; i
++) {
158 struct cache_entry
*ce
= active_cache
[i
];
165 ! matches_pathspec(ce
, argv
+1, argc
-1))
169 if (machine_readable
)
170 printf("U %s%c", ce
->name
, 0);
172 printf("%s: Unmerged\n",
174 while (i
< entries
&&
175 !strcmp(ce
->name
, active_cache
[i
]->name
))
177 i
--; /* compensate for loop control increments */
181 if (stat(ce
->name
, &st
) < 0) {
182 if (errno
== ENOENT
&& silent_on_nonexisting_files
)
184 if (machine_readable
)
185 printf("X %s%c", ce
->name
, 0);
187 printf("%s: %s\n", ce
->name
, strerror(errno
));
189 show_diff_empty(ce
, reverse
);
193 changed
= cache_match_stat(ce
, &st
);
196 if (!machine_readable
)
197 printf("%s: %s\n", ce
->name
, sha1_to_hex(ce
->sha1
));
199 printf("%s %s%c", sha1_to_hex(ce
->sha1
), ce
->name
, 0);
205 old
= read_sha1_file(ce
->sha1
, type
, &size
);
207 error("unable to read blob object for %s (%s)",
208 ce
->name
, sha1_to_hex(ce
->sha1
));
210 show_differences(ce
->name
, ce
->name
, old
, size
,