built-in diff.
This starts to replace the shell script version of "git diff". Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 28, 2006 at 23:20 UTC
65056021f2d2dcb7a72f05c5d6cbbd79a79b9d40
4 files changed
+335
-1
Makefile
+1
-1
@@ -214,7 +214,7 @@ LIB_OBJS = \
214
$(DIFF_OBJS)
215
216
BUILTIN_OBJS = \
217
- builtin-log.o builtin-help.o
217
+ builtin-log.o builtin-help.o builtin-diff.o
218
219
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
220
LIBS = $(GITLIBS) -lz
builtin-diff.c
new
+332
@@ -0,0 +1,332 @@
1
+/*
2
+ * Builtin "git diff"
3
+ *
4
+ * Copyright (c) 2006 Junio C Hamano
5
+ */
6
+#include "cache.h"
7
+#include "commit.h"
8
+#include "blob.h"
9
+#include "tag.h"
10
+#include "diff.h"
11
+#include "diffcore.h"
12
+#include "revision.h"
13
+#include "log-tree.h"
14
+#include "builtin.h"
15
+
16
+/* NEEDSWORK: struct object has place for name but we _do_
17
+ * know mode when we extracted the blob out of a tree, which
18
+ * we currently lose.
19
+ */
20
+struct blobinfo {
21
+ unsigned char sha1[20];
22
+ const char *name;
23
+};
24
+
25
+static const char builtin_diff_usage[] =
26
+"diff <options> <rev>{0,2} -- <path>*";
27
+
28
+static int builtin_diff_files(struct rev_info *revs,
29
+ int argc, const char **argv)
30
+{
31
+ int silent = 0;
32
+ while (1 < argc) {
33
+ const char *arg = argv[1];
34
+ if (!strcmp(arg, "--base"))
35
+ revs->max_count = 1;
36
+ else if (!strcmp(arg, "--ours"))
37
+ revs->max_count = 2;
38
+ else if (!strcmp(arg, "--theirs"))
39
+ revs->max_count = 3;
40
+ else if (!strcmp(arg, "-q"))
41
+ silent = 1;
42
+ else if (!strcmp(arg, "--raw"))
43
+ revs->diffopt.output_format = DIFF_FORMAT_RAW;
44
+ else
45
+ usage(builtin_diff_usage);
46
+ argv++; argc--;
47
+ }
48
+ /*
49
+ * Make sure there are NO revision (i.e. pending object) parameter,
50
+ * rev.max_count is reasonable (0 <= n <= 3),
51
+ * there is no other revision filtering parameters.
52
+ */
53
+ if (revs->pending_objects ||
54
+ revs->min_age != -1 ||
55
+ revs->max_age != -1)
56
+ usage(builtin_diff_usage);
57
+ /*
58
+ * Backward compatibility wart - "diff-files -s" used to
59
+ * defeat the common diff option "-s" which asked for
60
+ * DIFF_FORMAT_NO_OUTPUT.
61
+ */
62
+ if (revs->diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
63
+ revs->diffopt.output_format = DIFF_FORMAT_RAW;
64
+ return run_diff_files(revs, silent);
65
+}
66
+
67
+static void stuff_change(struct diff_options *opt,
68
+ unsigned old_mode, unsigned new_mode,
69
+ const unsigned char *old_sha1,
70
+ const unsigned char *new_sha1,
71
+ const char *old_name,
72
+ const char *new_name)
73
+{
74
+ struct diff_filespec *one, *two;
75
+
76
+ if (memcmp(null_sha1, old_sha1, 20) &&
77
+ memcmp(null_sha1, new_sha1, 20) &&
78
+ !memcmp(old_sha1, new_sha1, 20))
79
+ return;
80
+
81
+ if (opt->reverse_diff) {
82
+ unsigned tmp;
83
+ const
84
+ const unsigned char *tmp_u;
85
+ const char *tmp_c;
86
+ tmp = old_mode; old_mode = new_mode; new_mode = tmp;
87
+ tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
88
+ tmp_c = old_name; old_name = new_name; new_name = tmp_c;
89
+ }
90
+ one = alloc_filespec(old_name);
91
+ two = alloc_filespec(new_name);
92
+ fill_filespec(one, old_sha1, old_mode);
93
+ fill_filespec(two, new_sha1, new_mode);
94
+
95
+ /* NEEDSWORK: shouldn't this part of diffopt??? */
96
+ diff_queue(&diff_queued_diff, one, two);
97
+}
98
+
99
+static int builtin_diff_b_f(struct rev_info *revs,
100
+ int argc, const char **argv,
101
+ struct blobinfo *blob,
102
+ const char *path)
103
+{
104
+ /* Blob vs file in the working tree*/
105
+ struct stat st;
106
+
107
+ while (1 < argc) {
108
+ const char *arg = argv[1];
109
+ if (!strcmp(arg, "--raw"))
110
+ revs->diffopt.output_format = DIFF_FORMAT_RAW;
111
+ else
112
+ usage(builtin_diff_usage);
113
+ argv++; argc--;
114
+ }
115
+ if (lstat(path, &st))
116
+ die("'%s': %s", path, strerror(errno));
117
+ if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
118
+ die("'%s': not a regular file or symlink", path);
119
+ stuff_change(&revs->diffopt,
120
+ canon_mode(st.st_mode), canon_mode(st.st_mode),
121
+ blob[0].sha1, null_sha1,
122
+ blob[0].name, path);
123
+ diffcore_std(&revs->diffopt);
124
+ diff_flush(&revs->diffopt);
125
+ return 0;
126
+}
127
+
128
+static int builtin_diff_blobs(struct rev_info *revs,
129
+ int argc, const char **argv,
130
+ struct blobinfo *blob)
131
+{
132
+ /* Blobs */
133
+ unsigned mode = canon_mode(S_IFREG | 0644);
134
+
135
+ while (1 < argc) {
136
+ const char *arg = argv[1];
137
+ if (!strcmp(arg, "--raw"))
138
+ revs->diffopt.output_format = DIFF_FORMAT_RAW;
139
+ else
140
+ usage(builtin_diff_usage);
141
+ argv++; argc--;
142
+ }
143
+ stuff_change(&revs->diffopt,
144
+ mode, mode,
145
+ blob[0].sha1, blob[1].sha1,
146
+ blob[1].name, blob[1].name);
147
+ diffcore_std(&revs->diffopt);
148
+ diff_flush(&revs->diffopt);
149
+ return 0;
150
+}
151
+
152
+static int builtin_diff_index(struct rev_info *revs,
153
+ int argc, const char **argv)
154
+{
155
+ int cached = 0;
156
+ while (1 < argc) {
157
+ const char *arg = argv[1];
158
+ if (!strcmp(arg, "--cached"))
159
+ cached = 1;
160
+ else if (!strcmp(arg, "--raw"))
161
+ revs->diffopt.output_format = DIFF_FORMAT_RAW;
162
+ else
163
+ usage(builtin_diff_usage);
164
+ argv++; argc--;
165
+ }
166
+ /*
167
+ * Make sure there is one revision (i.e. pending object),
168
+ * and there is no revision filtering parameters.
169
+ */
170
+ if (!revs->pending_objects || revs->pending_objects->next ||
171
+ revs->max_count != -1 || revs->min_age != -1 ||
172
+ revs->max_age != -1)
173
+ usage(builtin_diff_usage);
174
+ return run_diff_index(revs, cached);
175
+}
176
+
177
+static int builtin_diff_tree(struct rev_info *revs,
178
+ int argc, const char **argv,
179
+ struct object_list *ent)
180
+{
181
+ /* We saw two trees, ent[0] and ent[1].
182
+ * unless ent[0] is unintesting, they are swapped
183
+ */
184
+ const unsigned char *(sha1[2]);
185
+ int swap = 1;
186
+ while (1 < argc) {
187
+ const char *arg = argv[1];
188
+ if (!strcmp(arg, "--raw"))
189
+ revs->diffopt.output_format = DIFF_FORMAT_RAW;
190
+ else
191
+ usage(builtin_diff_usage);
192
+ argv++; argc--;
193
+ }
194
+ if (ent[0].item->flags & UNINTERESTING)
195
+ swap = 0;
196
+ sha1[swap] = ent[0].item->sha1;
197
+ sha1[1-swap] = ent[1].item->sha1;
198
+ diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
199
+ log_tree_diff_flush(revs);
200
+ return 0;
201
+}
202
+
203
+static void add_head(struct rev_info *revs)
204
+{
205
+ unsigned char sha1[20];
206
+ struct object *obj;
207
+ if (get_sha1("HEAD", sha1))
208
+ return;
209
+ obj = parse_object(sha1);
210
+ if (!obj)
211
+ return;
212
+ add_object(obj, &revs->pending_objects, NULL, "HEAD");
213
+}
214
+
215
+int cmd_diff(int argc, const char **argv, char **envp)
216
+{
217
+ struct rev_info rev;
218
+ struct object_list *list, ent[2];
219
+ int ents = 0, blobs = 0, paths = 0;
220
+ const char *path = NULL;
221
+ struct blobinfo blob[2];
222
+
223
+ /*
224
+ * We could get N tree-ish in the rev.pending_objects list.
225
+ * Also there could be M blobs there, and P pathspecs.
226
+ *
227
+ * N=0, M=0:
228
+ * cache vs files (diff-files)
229
+ * N=0, M=2:
230
+ * compare two random blobs. P must be zero.
231
+ * N=0, M=1, P=1:
232
+ * compare a blob with a working tree file.
233
+ *
234
+ * N=1, M=0:
235
+ * tree vs cache (diff-index --cached)
236
+ *
237
+ * N=2, M=0:
238
+ * tree vs tree (diff-tree)
239
+ *
240
+ * Other cases are errors.
241
+ */
242
+
243
+ git_config(git_diff_config);
244
+ init_revisions(&rev);
245
+ rev.diffopt.output_format = DIFF_FORMAT_PATCH;
246
+
247
+ argc = setup_revisions(argc, argv, &rev, NULL);
248
+ /* Do we have --cached and not have a pending object, then
249
+ * default to HEAD by hand. Eek.
250
+ */
251
+ if (!rev.pending_objects) {
252
+ int i;
253
+ for (i = 1; i < argc; i++) {
254
+ const char *arg = argv[i];
255
+ if (!strcmp(arg, "--"))
256
+ break;
257
+ else if (!strcmp(arg, "--cached")) {
258
+ add_head(&rev);
259
+ break;
260
+ }
261
+ }
262
+ }
263
+
264
+ for (list = rev.pending_objects; list; list = list->next) {
265
+ struct object *obj = list->item;
266
+ const char *name = list->name;
267
+ int flags = (obj->flags & UNINTERESTING);
268
+ if (!obj->parsed)
269
+ obj = parse_object(obj->sha1);
270
+ obj = deref_tag(obj, NULL, 0);
271
+ if (!obj)
272
+ die("invalid object '%s' given.", name);
273
+ if (!strcmp(obj->type, commit_type))
274
+ obj = &((struct commit *)obj)->tree->object;
275
+ if (!strcmp(obj->type, tree_type)) {
276
+ if (2 <= ents)
277
+ die("more than two trees given: '%s'", name);
278
+ obj->flags |= flags;
279
+ ent[ents].item = obj;
280
+ ent[ents].name = name;
281
+ ents++;
282
+ continue;
283
+ }
284
+ if (!strcmp(obj->type, blob_type)) {
285
+ if (2 <= blobs)
286
+ die("more than two blobs given: '%s'", name);
287
+ memcpy(blob[blobs].sha1, obj->sha1, 20);
288
+ blob[blobs].name = name;
289
+ blobs++;
290
+ continue;
291
+
292
+ }
293
+ die("unhandled object '%s' given.", name);
294
+ }
295
+ if (rev.prune_data) {
296
+ const char **pathspec = rev.prune_data;
297
+ while (*pathspec) {
298
+ if (!path)
299
+ path = *pathspec;
300
+ paths++;
301
+ pathspec++;
302
+ }
303
+ }
304
+
305
+ /*
306
+ * Now, do the arguments look reasonable?
307
+ */
308
+ if (!ents) {
309
+ switch (blobs) {
310
+ case 0:
311
+ return builtin_diff_files(&rev, argc, argv);
312
+ break;
313
+ case 1:
314
+ if (paths != 1)
315
+ usage(builtin_diff_usage);
316
+ return builtin_diff_b_f(&rev, argc, argv, blob, path);
317
+ break;
318
+ case 2:
319
+ return builtin_diff_blobs(&rev, argc, argv, blob);
320
+ break;
321
+ default:
322
+ usage(builtin_diff_usage);
323
+ }
324
+ }
325
+ else if (blobs)
326
+ usage(builtin_diff_usage);
327
+ else if (ents == 1)
328
+ return builtin_diff_index(&rev, argc, argv);
329
+ else if (ents == 2)
330
+ return builtin_diff_tree(&rev, argc, argv, ent);
331
+ usage(builtin_diff_usage);
332
+}
builtin.h
+1
@@ -19,5 +19,6 @@ extern int cmd_version(int argc, const char **argv, char **envp);
19
extern int cmd_whatchanged(int argc, const char **argv, char **envp);
20
extern int cmd_show(int argc, const char **argv, char **envp);
21
extern int cmd_log(int argc, const char **argv, char **envp);
22
+extern int cmd_diff(int argc, const char **argv, char **envp);
23
24
#endif
git.c
+1
@@ -46,6 +46,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
46
{ "log", cmd_log },
47
{ "whatchanged", cmd_whatchanged },
48
{ "show", cmd_show },
49
+ { "diffn", cmd_diff },
50
};
51
int i;
52