4 * Copyright (c) 2006 Junio C Hamano
16 /* NEEDSWORK: struct object has place for name but we _do_
17 * know mode when we extracted the blob out of a tree, which
21 unsigned char sha1[20];
25 static const char builtin_diff_usage[] =
26 "git-diff <options> <rev>{0,2} -- <path>*";
28 static void stuff_change(struct diff_options *opt,
29 unsigned old_mode, unsigned new_mode,
30 const unsigned char *old_sha1,
31 const unsigned char *new_sha1,
35 struct diff_filespec *one, *two;
37 if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
38 !hashcmp(old_sha1, new_sha1))
41 if (opt->reverse_diff) {
43 const unsigned char *tmp_u;
45 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
46 tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
47 tmp_c = old_name; old_name = new_name; new_name = tmp_c;
49 one = alloc_filespec(old_name);
50 two = alloc_filespec(new_name);
51 fill_filespec(one, old_sha1, old_mode);
52 fill_filespec(two, new_sha1, new_mode);
54 /* NEEDSWORK: shouldn't this part of diffopt??? */
55 diff_queue(&diff_queued_diff, one, two);
58 static int builtin_diff_b_f(struct rev_info *revs,
59 int argc, const char **argv,
60 struct blobinfo *blob,
63 /* Blob vs file in the working tree*/
67 usage(builtin_diff_usage);
70 die("'%s': %s", path, strerror(errno));
71 if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
72 die("'%s': not a regular file or symlink", path);
73 stuff_change(&revs->diffopt,
74 canon_mode(st.st_mode), canon_mode(st.st_mode),
75 blob[0].sha1, null_sha1,
77 diffcore_std(&revs->diffopt);
78 diff_flush(&revs->diffopt);
82 static int builtin_diff_blobs(struct rev_info *revs,
83 int argc, const char **argv,
84 struct blobinfo *blob)
86 unsigned mode = canon_mode(S_IFREG | 0644);
89 usage(builtin_diff_usage);
91 stuff_change(&revs->diffopt,
93 blob[0].sha1, blob[1].sha1,
94 blob[0].name, blob[1].name);
95 diffcore_std(&revs->diffopt);
96 diff_flush(&revs->diffopt);
100 static int builtin_diff_index(struct rev_info *revs,
101 int argc, const char **argv)
105 const char *arg = argv[1];
106 if (!strcmp(arg, "--cached"))
109 usage(builtin_diff_usage);
113 * Make sure there is one revision (i.e. pending object),
114 * and there is no revision filtering parameters.
116 if (revs->pending.nr != 1 ||
117 revs->max_count != -1 || revs->min_age != -1 ||
119 usage(builtin_diff_usage);
120 return run_diff_index(revs, cached);
123 static int builtin_diff_tree(struct rev_info *revs,
124 int argc, const char **argv,
125 struct object_array_entry *ent)
127 const unsigned char *(sha1[2]);
131 usage(builtin_diff_usage);
133 /* We saw two trees, ent[0] and ent[1].
134 * if ent[1] is uninteresting, they are swapped
136 if (ent[1].item->flags & UNINTERESTING)
138 sha1[swap] = ent[0].item->sha1;
139 sha1[1-swap] = ent[1].item->sha1;
140 diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
141 log_tree_diff_flush(revs);
145 static int builtin_diff_combined(struct rev_info *revs,
146 int argc, const char **argv,
147 struct object_array_entry *ent,
150 const unsigned char (*parent)[20];
154 usage(builtin_diff_usage);
156 if (!revs->dense_combined_merges && !revs->combine_merges)
157 revs->dense_combined_merges = revs->combine_merges = 1;
158 parent = xmalloc(ents * sizeof(*parent));
159 /* Again, the revs are all reverse */
160 for (i = 0; i < ents; i++)
161 hashcpy((unsigned char*)parent + i, ent[ents - 1 - i].item->sha1);
162 diff_tree_combined(parent[0], parent + 1, ents - 1,
163 revs->dense_combined_merges, revs);
167 void add_head(struct rev_info *revs)
169 unsigned char sha1[20];
171 if (get_sha1("HEAD", sha1))
173 obj = parse_object(sha1);
176 add_pending_object(revs, obj, "HEAD");
179 int cmd_diff(int argc, const char **argv, const char *prefix)
183 struct object_array_entry ent[100];
184 int ents = 0, blobs = 0, paths = 0;
185 const char *path = NULL;
186 struct blobinfo blob[2];
190 * We could get N tree-ish in the rev.pending_objects list.
191 * Also there could be M blobs there, and P pathspecs.
194 * cache vs files (diff-files)
196 * compare two random blobs. P must be zero.
198 * compare a blob with a working tree file.
201 * tree vs cache (diff-index --cached)
204 * tree vs tree (diff-tree)
206 * Other cases are errors.
209 prefix = setup_git_directory_gently(&nongit);
210 git_config(git_diff_ui_config);
211 init_revisions(&rev, prefix);
213 argc = setup_revisions(argc, argv, &rev, NULL);
214 if (!rev.diffopt.output_format) {
215 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
216 if (diff_setup_done(&rev.diffopt) < 0)
217 die("diff_setup_done failed");
220 /* Do we have --cached and not have a pending object, then
221 * default to HEAD by hand. Eek.
223 if (!rev.pending.nr) {
225 for (i = 1; i < argc; i++) {
226 const char *arg = argv[i];
227 if (!strcmp(arg, "--"))
229 else if (!strcmp(arg, "--cached")) {
236 for (i = 0; i < rev.pending.nr; i++) {
237 struct object_array_entry *list = rev.pending.objects+i;
238 struct object *obj = list->item;
239 const char *name = list->name;
240 int flags = (obj->flags & UNINTERESTING);
242 obj = parse_object(obj->sha1);
243 obj = deref_tag(obj, NULL, 0);
245 die("invalid object '%s' given.", name);
246 if (obj->type == OBJ_COMMIT)
247 obj = &((struct commit *)obj)->tree->object;
248 if (obj->type == OBJ_TREE) {
249 if (ARRAY_SIZE(ent) <= ents)
250 die("more than %d trees given: '%s'",
251 (int) ARRAY_SIZE(ent), name);
253 ent[ents].item = obj;
254 ent[ents].name = name;
258 if (obj->type == OBJ_BLOB) {
260 die("more than two blobs given: '%s'", name);
261 hashcpy(blob[blobs].sha1, obj->sha1);
262 blob[blobs].name = name;
267 die("unhandled object '%s' given.", name);
269 if (rev.prune_data) {
270 const char **pathspec = rev.prune_data;
280 * Now, do the arguments look reasonable?
285 return run_diff_files_cmd(&rev, argc, argv);
289 usage(builtin_diff_usage);
290 return builtin_diff_b_f(&rev, argc, argv, blob, path);
294 usage(builtin_diff_usage);
295 return builtin_diff_blobs(&rev, argc, argv, blob);
298 usage(builtin_diff_usage);
302 usage(builtin_diff_usage);
304 return builtin_diff_index(&rev, argc, argv);
306 return builtin_diff_tree(&rev, argc, argv, ent);
307 else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
308 /* diff A...B where there is one sane merge base between
309 * A and B. We have ent[0] == merge-base, ent[1] == A,
310 * and ent[2] == B. Show diff between the base and B.
313 return builtin_diff_tree(&rev, argc, argv, ent);
316 return builtin_diff_combined(&rev, argc, argv,
318 usage(builtin_diff_usage);