Code

059eb6de4932ad7f67a09559da5c08144f14de3f
[git.git] / builtin-diff.c
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"
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 };
25 static const char builtin_diff_usage[] =
26 "diff <options> <rev>{0,2} -- <path>*";
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
43                         usage(builtin_diff_usage);
44                 argv++; argc--;
45         }
46         /*
47          * Make sure there are NO revision (i.e. pending object) parameter,
48          * specified rev.max_count is reasonable (0 <= n <= 3), and
49          * there is no other revision filtering parameter.
50          */
51         if (revs->pending.nr ||
52             revs->min_age != -1 ||
53             revs->max_age != -1 ||
54             3 < revs->max_count)
55                 usage(builtin_diff_usage);
56         if (revs->max_count < 0 &&
57             (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
58                 revs->combine_merges = revs->dense_combined_merges = 1;
59         return run_diff_files(revs, silent);
60 }
62 static void stuff_change(struct diff_options *opt,
63                          unsigned old_mode, unsigned new_mode,
64                          const unsigned char *old_sha1,
65                          const unsigned char *new_sha1,
66                          const char *old_name,
67                          const char *new_name)
68 {
69         struct diff_filespec *one, *two;
71         if (memcmp(null_sha1, old_sha1, 20) &&
72             memcmp(null_sha1, new_sha1, 20) &&
73             !memcmp(old_sha1, new_sha1, 20))
74                 return;
76         if (opt->reverse_diff) {
77                 unsigned tmp;
78                 const unsigned char *tmp_u;
79                 const char *tmp_c;
80                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
81                 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
82                 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
83         }
84         one = alloc_filespec(old_name);
85         two = alloc_filespec(new_name);
86         fill_filespec(one, old_sha1, old_mode);
87         fill_filespec(two, new_sha1, new_mode);
89         /* NEEDSWORK: shouldn't this part of diffopt??? */
90         diff_queue(&diff_queued_diff, one, two);
91 }
93 static int builtin_diff_b_f(struct rev_info *revs,
94                             int argc, const char **argv,
95                             struct blobinfo *blob,
96                             const char *path)
97 {
98         /* Blob vs file in the working tree*/
99         struct stat st;
101         if (argc > 1)
102                 usage(builtin_diff_usage);
104         if (lstat(path, &st))
105                 die("'%s': %s", path, strerror(errno));
106         if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
107                 die("'%s': not a regular file or symlink", path);
108         stuff_change(&revs->diffopt,
109                      canon_mode(st.st_mode), canon_mode(st.st_mode),
110                      blob[0].sha1, null_sha1,
111                      path, path);
112         diffcore_std(&revs->diffopt);
113         diff_flush(&revs->diffopt);
114         return 0;
117 static int builtin_diff_blobs(struct rev_info *revs,
118                               int argc, const char **argv,
119                               struct blobinfo *blob)
121         /* Blobs: the arguments are reversed when setup_revisions()
122          * picked them up.
123          */
124         unsigned mode = canon_mode(S_IFREG | 0644);
126         if (argc > 1)
127                 usage(builtin_diff_usage);
129         stuff_change(&revs->diffopt,
130                      mode, mode,
131                      blob[1].sha1, blob[0].sha1,
132                      blob[0].name, blob[0].name);
133         diffcore_std(&revs->diffopt);
134         diff_flush(&revs->diffopt);
135         return 0;
138 static int builtin_diff_index(struct rev_info *revs,
139                               int argc, const char **argv)
141         int cached = 0;
142         while (1 < argc) {
143                 const char *arg = argv[1];
144                 if (!strcmp(arg, "--cached"))
145                         cached = 1;
146                 else
147                         usage(builtin_diff_usage);
148                 argv++; argc--;
149         }
150         /*
151          * Make sure there is one revision (i.e. pending object),
152          * and there is no revision filtering parameters.
153          */
154         if (revs->pending.nr != 1 ||
155             revs->max_count != -1 || revs->min_age != -1 ||
156             revs->max_age != -1)
157                 usage(builtin_diff_usage);
158         return run_diff_index(revs, cached);
161 static int builtin_diff_tree(struct rev_info *revs,
162                              int argc, const char **argv,
163                              struct object_array_entry *ent)
165         const unsigned char *(sha1[2]);
166         int swap = 0;
168         if (argc > 1)
169                 usage(builtin_diff_usage);
171         /* We saw two trees, ent[0] and ent[1].
172          * if ent[1] is unintesting, they are swapped
173          */
174         if (ent[1].item->flags & UNINTERESTING)
175                 swap = 1;
176         sha1[swap] = ent[0].item->sha1;
177         sha1[1-swap] = ent[1].item->sha1;
178         diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
179         log_tree_diff_flush(revs);
180         return 0;
183 static int builtin_diff_combined(struct rev_info *revs,
184                                  int argc, const char **argv,
185                                  struct object_array_entry *ent,
186                                  int ents)
188         const unsigned char (*parent)[20];
189         int i;
191         if (argc > 1)
192                 usage(builtin_diff_usage);
194         if (!revs->dense_combined_merges && !revs->combine_merges)
195                 revs->dense_combined_merges = revs->combine_merges = 1;
196         parent = xmalloc(ents * sizeof(*parent));
197         /* Again, the revs are all reverse */
198         for (i = 0; i < ents; i++)
199                 memcpy(parent + i, ent[ents - 1 - i].item->sha1, 20);
200         diff_tree_combined(parent[0], parent + 1, ents - 1,
201                            revs->dense_combined_merges, revs);
202         return 0;
205 void add_head(struct rev_info *revs)
207         unsigned char sha1[20];
208         struct object *obj;
209         if (get_sha1("HEAD", sha1))
210                 return;
211         obj = parse_object(sha1);
212         if (!obj)
213                 return;
214         add_pending_object(revs, obj, "HEAD");
217 int cmd_diff(int argc, const char **argv, char **envp)
219         int i;
220         struct rev_info rev;
221         struct object_array_entry ent[100];
222         int ents = 0, blobs = 0, paths = 0;
223         const char *path = NULL;
224         struct blobinfo blob[2];
226         /*
227          * We could get N tree-ish in the rev.pending_objects list.
228          * Also there could be M blobs there, and P pathspecs.
229          *
230          * N=0, M=0:
231          *      cache vs files (diff-files)
232          * N=0, M=2:
233          *      compare two random blobs.  P must be zero.
234          * N=0, M=1, P=1:
235          *      compare a blob with a working tree file.
236          *
237          * N=1, M=0:
238          *      tree vs cache (diff-index --cached)
239          *
240          * N=2, M=0:
241          *      tree vs tree (diff-tree)
242          *
243          * Other cases are errors.
244          */
246         git_config(git_diff_config);
247         init_revisions(&rev);
249         argc = setup_revisions(argc, argv, &rev, NULL);
250         if (!rev.diffopt.output_format) {
251                 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
252                 diff_setup_done(&rev.diffopt);
253         }
255         /* Do we have --cached and not have a pending object, then
256          * default to HEAD by hand.  Eek.
257          */
258         if (!rev.pending.nr) {
259                 int i;
260                 for (i = 1; i < argc; i++) {
261                         const char *arg = argv[i];
262                         if (!strcmp(arg, "--"))
263                                 break;
264                         else if (!strcmp(arg, "--cached")) {
265                                 add_head(&rev);
266                                 break;
267                         }
268                 }
269         }
271         for (i = 0; i < rev.pending.nr; i++) {
272                 struct object_array_entry *list = rev.pending.objects+i;
273                 struct object *obj = list->item;
274                 const char *name = list->name;
275                 int flags = (obj->flags & UNINTERESTING);
276                 if (!obj->parsed)
277                         obj = parse_object(obj->sha1);
278                 obj = deref_tag(obj, NULL, 0);
279                 if (!obj)
280                         die("invalid object '%s' given.", name);
281                 if (obj->type == TYPE_COMMIT)
282                         obj = &((struct commit *)obj)->tree->object;
283                 if (obj->type == TYPE_TREE) {
284                         if (ARRAY_SIZE(ent) <= ents)
285                                 die("more than %d trees given: '%s'",
286                                     (int) ARRAY_SIZE(ent), name);
287                         obj->flags |= flags;
288                         ent[ents].item = obj;
289                         ent[ents].name = name;
290                         ents++;
291                         continue;
292                 }
293                 if (obj->type == TYPE_BLOB) {
294                         if (2 <= blobs)
295                                 die("more than two blobs given: '%s'", name);
296                         memcpy(blob[blobs].sha1, obj->sha1, 20);
297                         blob[blobs].name = name;
298                         blobs++;
299                         continue;
301                 }
302                 die("unhandled object '%s' given.", name);
303         }
304         if (rev.prune_data) {
305                 const char **pathspec = rev.prune_data;
306                 while (*pathspec) {
307                         if (!path)
308                                 path = *pathspec;
309                         paths++;
310                         pathspec++;
311                 }
312         }
314         /*
315          * Now, do the arguments look reasonable?
316          */
317         if (!ents) {
318                 switch (blobs) {
319                 case 0:
320                         return builtin_diff_files(&rev, argc, argv);
321                         break;
322                 case 1:
323                         if (paths != 1)
324                                 usage(builtin_diff_usage);
325                         return builtin_diff_b_f(&rev, argc, argv, blob, path);
326                         break;
327                 case 2:
328                         if (paths)
329                                 usage(builtin_diff_usage);
330                         return builtin_diff_blobs(&rev, argc, argv, blob);
331                         break;
332                 default:
333                         usage(builtin_diff_usage);
334                 }
335         }
336         else if (blobs)
337                 usage(builtin_diff_usage);
338         else if (ents == 1)
339                 return builtin_diff_index(&rev, argc, argv);
340         else if (ents == 2)
341                 return builtin_diff_tree(&rev, argc, argv, ent);
342         else
343                 return builtin_diff_combined(&rev, argc, argv, ent, ents);
344         usage(builtin_diff_usage);