2005-04-08 02:16:10 +04:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-04-08 02:13:13 +04:00
|
|
|
#include "cache.h"
|
2005-04-27 20:21:00 +04:00
|
|
|
#include "diff.h"
|
2005-04-12 13:04:44 +04:00
|
|
|
|
2005-07-29 13:01:26 +04:00
|
|
|
static const char diff_files_usage[] =
|
2006-01-28 11:03:38 +03:00
|
|
|
"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
|
2005-07-13 23:52:35 +04:00
|
|
|
COMMON_DIFF_OPTIONS_HELP;
|
2005-04-17 08:29:45 +04:00
|
|
|
|
2005-09-21 11:00:47 +04:00
|
|
|
static struct diff_options diff_options;
|
2005-04-27 04:17:36 +04:00
|
|
|
static int silent = 0;
|
diff-files: show diffs with stage0 and unmerged stage at the same time.
After thinking about it more, I realized that much of the change
I did on top of Linus' version does not make much sense. This
commit reverts it so that it by default shows diffs with stage0
paths or stage2 paths with working tree; the unmerged stage to
use can be overridden with -1/-2/-3 option (-2 is the default so
essentially is a no-op).
When the index file is unmerged, we are by definition in the
middle of a conflicting merge, and we should show the diff with
stage 2 by default. More importantly, paths without conflicts
are updated in the working tree and collapsed to stage0 in the
index, so showing diff with stage0 at the same time does not
hurt. In normal cases, stage0 entries should be in sync with
the working tree files and does not clutter the output. It even
helps the user to realize that the working tree has local
changes unrelated to the merge and remember to be careful not to
do a "git-commit -a" after resolving the conflicts.
When there is no unmerged entries, giving diff_unmerged_stage a
default value of 2 does not cause any harm, because it would not
be used anyway. So in all, always showing diff between stage0
paths and unmerged entries from a stage (defaulting to 2) is the
right thing to do, as Linus originally did.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-11-30 13:16:36 +03:00
|
|
|
static int diff_unmerged_stage = 2;
|
2006-01-28 11:03:38 +03:00
|
|
|
static int combine_merges = 0;
|
|
|
|
static int dense_combined_merges = 0;
|
2005-04-27 04:17:36 +04:00
|
|
|
|
2005-04-27 20:21:00 +04:00
|
|
|
static void show_unmerge(const char *path)
|
2005-04-27 04:17:36 +04:00
|
|
|
{
|
2005-09-21 11:00:47 +04:00
|
|
|
diff_unmerge(&diff_options, path);
|
2005-04-27 20:21:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_file(int pfx, struct cache_entry *ce)
|
|
|
|
{
|
2005-09-21 11:00:47 +04:00
|
|
|
diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
|
|
|
|
ce->sha1, ce->name, NULL);
|
2005-04-27 20:21:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_modified(int oldmode, int mode,
|
2005-05-18 16:14:09 +04:00
|
|
|
const unsigned char *old_sha1, const unsigned char *sha1,
|
2005-04-27 20:21:00 +04:00
|
|
|
char *path)
|
|
|
|
{
|
2005-09-21 11:00:47 +04:00
|
|
|
diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
|
2005-04-27 04:17:36 +04:00
|
|
|
}
|
|
|
|
|
2005-09-21 11:00:47 +04:00
|
|
|
int main(int argc, const char **argv)
|
2005-04-08 02:13:13 +04:00
|
|
|
{
|
2005-07-15 03:55:06 +04:00
|
|
|
const char **pathspec;
|
2005-08-17 05:06:34 +04:00
|
|
|
const char *prefix = setup_git_directory();
|
|
|
|
int entries, i;
|
2005-04-08 02:13:13 +04:00
|
|
|
|
2005-11-22 09:52:37 +03:00
|
|
|
git_config(git_diff_config);
|
2005-09-21 11:00:47 +04:00
|
|
|
diff_setup(&diff_options);
|
2005-04-17 08:29:45 +04:00
|
|
|
while (1 < argc && argv[1][0] == '-') {
|
2005-10-18 11:16:45 +04:00
|
|
|
if (!strcmp(argv[1], "--")) {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
break;
|
|
|
|
}
|
2005-11-30 08:06:10 +03:00
|
|
|
if (!strcmp(argv[1], "-0"))
|
|
|
|
diff_unmerged_stage = 0;
|
|
|
|
else if (!strcmp(argv[1], "-1"))
|
|
|
|
diff_unmerged_stage = 1;
|
|
|
|
else if (!strcmp(argv[1], "-2"))
|
|
|
|
diff_unmerged_stage = 2;
|
|
|
|
else if (!strcmp(argv[1], "-3"))
|
|
|
|
diff_unmerged_stage = 3;
|
|
|
|
else if (!strcmp(argv[1], "--base"))
|
|
|
|
diff_unmerged_stage = 1;
|
|
|
|
else if (!strcmp(argv[1], "--ours"))
|
|
|
|
diff_unmerged_stage = 2;
|
|
|
|
else if (!strcmp(argv[1], "--theirs"))
|
|
|
|
diff_unmerged_stage = 3;
|
|
|
|
else if (!strcmp(argv[1], "-q"))
|
2005-04-28 02:22:02 +04:00
|
|
|
silent = 1;
|
2005-04-27 04:17:36 +04:00
|
|
|
else if (!strcmp(argv[1], "-r"))
|
2005-04-27 20:21:00 +04:00
|
|
|
; /* no-op */
|
2005-04-28 02:22:02 +04:00
|
|
|
else if (!strcmp(argv[1], "-s"))
|
|
|
|
; /* no-op */
|
2006-01-28 11:03:38 +03:00
|
|
|
else if (!strcmp(argv[1], "-c"))
|
|
|
|
combine_merges = 1;
|
|
|
|
else if (!strcmp(argv[1], "--cc"))
|
|
|
|
dense_combined_merges = combine_merges = 1;
|
2005-09-21 11:00:47 +04:00
|
|
|
else {
|
|
|
|
int diff_opt_cnt;
|
|
|
|
diff_opt_cnt = diff_opt_parse(&diff_options,
|
|
|
|
argv+1, argc-1);
|
|
|
|
if (diff_opt_cnt < 0)
|
2005-06-03 12:37:54 +04:00
|
|
|
usage(diff_files_usage);
|
2005-09-21 11:00:47 +04:00
|
|
|
else if (diff_opt_cnt) {
|
|
|
|
argv += diff_opt_cnt;
|
|
|
|
argc -= diff_opt_cnt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2005-06-03 12:37:54 +04:00
|
|
|
usage(diff_files_usage);
|
2005-05-21 13:39:09 +04:00
|
|
|
}
|
2005-04-17 08:29:45 +04:00
|
|
|
argv++; argc--;
|
2005-04-13 12:40:09 +04:00
|
|
|
}
|
2006-02-10 02:23:06 +03:00
|
|
|
if (dense_combined_merges)
|
2006-01-28 11:03:38 +03:00
|
|
|
diff_options.output_format = DIFF_FORMAT_PATCH;
|
2005-04-13 12:40:09 +04:00
|
|
|
|
2005-08-17 05:06:34 +04:00
|
|
|
/* Find the directory, and set up the pathspec */
|
|
|
|
pathspec = get_pathspec(prefix, argv + 1);
|
|
|
|
entries = read_cache();
|
2005-07-15 03:55:06 +04:00
|
|
|
|
2005-09-21 11:00:47 +04:00
|
|
|
if (diff_setup_done(&diff_options) < 0)
|
2005-06-20 00:14:05 +04:00
|
|
|
usage(diff_files_usage);
|
|
|
|
|
2005-04-17 08:29:45 +04:00
|
|
|
/* At this point, if argc == 1, then we are doing everything.
|
|
|
|
* Otherwise argv[1] .. argv[argc-1] have the explicit paths.
|
|
|
|
*/
|
2005-04-08 02:13:13 +04:00
|
|
|
if (entries < 0) {
|
|
|
|
perror("read_cache");
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-04-26 20:25:05 +04:00
|
|
|
|
2005-04-08 02:13:13 +04:00
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
struct stat st;
|
2005-10-12 05:45:33 +04:00
|
|
|
unsigned int oldmode, newmode;
|
2005-04-08 02:13:13 +04:00
|
|
|
struct cache_entry *ce = active_cache[i];
|
2005-04-17 08:29:45 +04:00
|
|
|
int changed;
|
2005-04-08 02:13:13 +04:00
|
|
|
|
2005-07-15 03:55:06 +04:00
|
|
|
if (!ce_path_match(ce, pathspec))
|
|
|
|
continue;
|
|
|
|
|
2005-04-17 08:29:45 +04:00
|
|
|
if (ce_stage(ce)) {
|
2006-01-28 11:03:38 +03:00
|
|
|
struct {
|
|
|
|
struct combine_diff_path p;
|
2006-02-06 23:53:07 +03:00
|
|
|
struct combine_diff_parent filler[5];
|
2006-01-28 11:03:38 +03:00
|
|
|
} combine;
|
2006-01-28 13:09:08 +03:00
|
|
|
int num_compare_stages = 0;
|
2006-01-28 11:03:38 +03:00
|
|
|
|
|
|
|
combine.p.next = NULL;
|
|
|
|
combine.p.len = ce_namelen(ce);
|
|
|
|
combine.p.path = xmalloc(combine.p.len + 1);
|
|
|
|
memcpy(combine.p.path, ce->name, combine.p.len);
|
|
|
|
combine.p.path[combine.p.len] = 0;
|
2006-02-06 23:53:07 +03:00
|
|
|
combine.p.mode = 0;
|
|
|
|
memset(combine.p.sha1, 0, 20);
|
|
|
|
memset(&combine.p.parent[0], 0,
|
|
|
|
sizeof(combine.filler));
|
2006-01-28 11:03:38 +03:00
|
|
|
|
2005-11-30 08:06:10 +03:00
|
|
|
while (i < entries) {
|
|
|
|
struct cache_entry *nce = active_cache[i];
|
2006-01-28 11:03:38 +03:00
|
|
|
int stage;
|
2005-11-30 08:06:10 +03:00
|
|
|
|
|
|
|
if (strcmp(ce->name, nce->name))
|
|
|
|
break;
|
2006-01-28 11:03:38 +03:00
|
|
|
|
|
|
|
/* Stage #2 (ours) is the first parent,
|
|
|
|
* stage #3 (theirs) is the second.
|
|
|
|
*/
|
|
|
|
stage = ce_stage(nce);
|
2006-01-28 13:09:08 +03:00
|
|
|
if (2 <= stage) {
|
2006-02-06 23:53:07 +03:00
|
|
|
int mode = ntohl(nce->ce_mode);
|
2006-01-28 13:09:08 +03:00
|
|
|
num_compare_stages++;
|
2006-02-06 23:53:07 +03:00
|
|
|
memcpy(combine.p.parent[stage-2].sha1,
|
2006-01-28 11:03:38 +03:00
|
|
|
nce->sha1, 20);
|
2006-02-06 23:53:07 +03:00
|
|
|
combine.p.parent[stage-2].mode =
|
2006-03-30 10:55:43 +04:00
|
|
|
canon_mode(mode);
|
2006-02-14 12:11:42 +03:00
|
|
|
combine.p.parent[stage-2].status =
|
|
|
|
DIFF_STATUS_MODIFIED;
|
2006-01-28 13:09:08 +03:00
|
|
|
}
|
2006-01-28 11:03:38 +03:00
|
|
|
|
2005-11-30 08:06:10 +03:00
|
|
|
/* diff against the proper unmerged stage */
|
2006-01-28 11:03:38 +03:00
|
|
|
if (stage == diff_unmerged_stage)
|
2005-11-30 08:06:10 +03:00
|
|
|
ce = nce;
|
2005-04-17 08:29:45 +04:00
|
|
|
i++;
|
2005-11-30 08:06:10 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Compensate for loop update
|
|
|
|
*/
|
|
|
|
i--;
|
2006-01-28 11:03:38 +03:00
|
|
|
|
2006-01-28 13:09:08 +03:00
|
|
|
if (combine_merges && num_compare_stages == 2) {
|
2006-01-28 11:03:38 +03:00
|
|
|
show_combined_diff(&combine.p, 2,
|
|
|
|
dense_combined_merges,
|
2006-02-10 02:23:06 +03:00
|
|
|
NULL,
|
|
|
|
&diff_options);
|
2006-01-28 13:09:08 +03:00
|
|
|
free(combine.p.path);
|
2006-01-28 11:03:38 +03:00
|
|
|
continue;
|
|
|
|
}
|
2006-01-28 13:09:08 +03:00
|
|
|
free(combine.p.path);
|
2006-01-28 11:03:38 +03:00
|
|
|
|
2005-11-30 08:06:10 +03:00
|
|
|
/*
|
|
|
|
* Show the diff for the 'ce' if we found the one
|
|
|
|
* from the desired stage.
|
|
|
|
*/
|
2006-01-28 11:03:38 +03:00
|
|
|
show_unmerge(ce->name);
|
2005-11-30 08:06:10 +03:00
|
|
|
if (ce_stage(ce) != diff_unmerged_stage)
|
|
|
|
continue;
|
2005-04-17 08:29:45 +04:00
|
|
|
}
|
2005-05-20 06:00:36 +04:00
|
|
|
|
2005-05-06 17:45:01 +04:00
|
|
|
if (lstat(ce->name, &st) < 0) {
|
2005-05-20 20:48:38 +04:00
|
|
|
if (errno != ENOENT && errno != ENOTDIR) {
|
2005-04-27 04:17:36 +04:00
|
|
|
perror(ce->name);
|
2005-04-16 02:08:09 +04:00
|
|
|
continue;
|
2005-05-20 06:00:36 +04:00
|
|
|
}
|
2005-04-28 02:22:02 +04:00
|
|
|
if (silent)
|
2005-04-27 04:17:36 +04:00
|
|
|
continue;
|
2005-04-27 20:21:00 +04:00
|
|
|
show_file('-', ce);
|
2005-04-08 02:13:13 +04:00
|
|
|
continue;
|
|
|
|
}
|
2006-02-09 08:15:24 +03:00
|
|
|
changed = ce_match_stat(ce, &st, 0);
|
2005-09-21 11:00:47 +04:00
|
|
|
if (!changed && !diff_options.find_copies_harder)
|
2005-04-08 02:13:13 +04:00
|
|
|
continue;
|
2005-04-27 04:17:36 +04:00
|
|
|
oldmode = ntohl(ce->ce_mode);
|
2005-10-12 05:45:33 +04:00
|
|
|
|
2006-03-30 10:55:43 +04:00
|
|
|
newmode = canon_mode(st.st_mode);
|
2005-10-12 05:45:33 +04:00
|
|
|
if (!trust_executable_bit &&
|
|
|
|
S_ISREG(newmode) && S_ISREG(oldmode) &&
|
|
|
|
((newmode ^ oldmode) == 0111))
|
|
|
|
newmode = oldmode;
|
|
|
|
show_modified(oldmode, newmode,
|
2005-06-20 00:14:05 +04:00
|
|
|
ce->sha1, (changed ? null_sha1 : ce->sha1),
|
2005-04-27 20:21:00 +04:00
|
|
|
ce->name);
|
2005-04-08 02:13:13 +04:00
|
|
|
}
|
2005-09-21 11:00:47 +04:00
|
|
|
diffcore_std(&diff_options);
|
|
|
|
diff_flush(&diff_options);
|
2005-04-08 02:13:13 +04:00
|
|
|
return 0;
|
|
|
|
}
|