2005-04-07 22:16:10 +00:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-04-07 22:13:13 +00:00
|
|
|
#include "cache.h"
|
2005-04-27 16:21:00 +00:00
|
|
|
#include "diff.h"
|
2005-04-12 09:04:44 +00:00
|
|
|
|
2005-07-29 09:01:26 +00:00
|
|
|
static const char diff_files_usage[] =
|
2006-01-28 08:03:38 +00:00
|
|
|
"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
|
2005-07-13 19:52:35 +00:00
|
|
|
COMMON_DIFF_OPTIONS_HELP;
|
2005-04-17 04:29:45 +00:00
|
|
|
|
2005-09-21 07:00:47 +00:00
|
|
|
static struct diff_options diff_options;
|
2005-04-27 00:17:36 +00: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 10:16:36 +00:00
|
|
|
static int diff_unmerged_stage = 2;
|
2006-01-28 08:03:38 +00:00
|
|
|
static int combine_merges = 0;
|
|
|
|
static int dense_combined_merges = 0;
|
2005-04-27 00:17:36 +00:00
|
|
|
|
2005-04-27 16:21:00 +00:00
|
|
|
static void show_unmerge(const char *path)
|
2005-04-27 00:17:36 +00:00
|
|
|
{
|
2005-09-21 07:00:47 +00:00
|
|
|
diff_unmerge(&diff_options, path);
|
2005-04-27 16:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_file(int pfx, struct cache_entry *ce)
|
|
|
|
{
|
2005-09-21 07:00:47 +00:00
|
|
|
diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
|
|
|
|
ce->sha1, ce->name, NULL);
|
2005-04-27 16:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_modified(int oldmode, int mode,
|
2005-05-18 12:14:09 +00:00
|
|
|
const unsigned char *old_sha1, const unsigned char *sha1,
|
2005-04-27 16:21:00 +00:00
|
|
|
char *path)
|
|
|
|
{
|
2005-09-21 07:00:47 +00:00
|
|
|
diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
|
2005-04-27 00:17:36 +00:00
|
|
|
}
|
|
|
|
|
2005-09-21 07:00:47 +00:00
|
|
|
int main(int argc, const char **argv)
|
2005-04-07 22:13:13 +00:00
|
|
|
{
|
2005-07-14 23:55:06 +00:00
|
|
|
const char **pathspec;
|
2005-08-17 01:06:34 +00:00
|
|
|
const char *prefix = setup_git_directory();
|
|
|
|
int entries, i;
|
2005-04-07 22:13:13 +00:00
|
|
|
|
2005-11-22 06:52:37 +00:00
|
|
|
git_config(git_diff_config);
|
2005-09-21 07:00:47 +00:00
|
|
|
diff_setup(&diff_options);
|
2005-04-17 04:29:45 +00:00
|
|
|
while (1 < argc && argv[1][0] == '-') {
|
2005-10-18 07:16:45 +00:00
|
|
|
if (!strcmp(argv[1], "--")) {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
break;
|
|
|
|
}
|
2005-11-30 05:06:10 +00: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-27 22:22:02 +00:00
|
|
|
silent = 1;
|
2005-04-27 00:17:36 +00:00
|
|
|
else if (!strcmp(argv[1], "-r"))
|
2005-04-27 16:21:00 +00:00
|
|
|
; /* no-op */
|
2005-04-27 22:22:02 +00:00
|
|
|
else if (!strcmp(argv[1], "-s"))
|
|
|
|
; /* no-op */
|
2006-01-28 08:03:38 +00: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 07:00:47 +00: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 08:37:54 +00:00
|
|
|
usage(diff_files_usage);
|
2005-09-21 07:00:47 +00:00
|
|
|
else if (diff_opt_cnt) {
|
|
|
|
argv += diff_opt_cnt;
|
|
|
|
argc -= diff_opt_cnt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2005-06-03 08:37:54 +00:00
|
|
|
usage(diff_files_usage);
|
2005-05-21 09:39:09 +00:00
|
|
|
}
|
2005-04-17 04:29:45 +00:00
|
|
|
argv++; argc--;
|
2005-04-13 08:40:09 +00:00
|
|
|
}
|
2006-01-28 08:03:38 +00:00
|
|
|
if (combine_merges) {
|
|
|
|
diff_options.output_format = DIFF_FORMAT_PATCH;
|
|
|
|
}
|
2005-04-13 08:40:09 +00:00
|
|
|
|
2005-08-17 01:06:34 +00:00
|
|
|
/* Find the directory, and set up the pathspec */
|
|
|
|
pathspec = get_pathspec(prefix, argv + 1);
|
|
|
|
entries = read_cache();
|
2005-07-14 23:55:06 +00:00
|
|
|
|
2005-09-21 07:00:47 +00:00
|
|
|
if (diff_setup_done(&diff_options) < 0)
|
2005-06-19 20:14:05 +00:00
|
|
|
usage(diff_files_usage);
|
|
|
|
|
2005-04-17 04:29:45 +00:00
|
|
|
/* At this point, if argc == 1, then we are doing everything.
|
|
|
|
* Otherwise argv[1] .. argv[argc-1] have the explicit paths.
|
|
|
|
*/
|
2005-04-07 22:13:13 +00:00
|
|
|
if (entries < 0) {
|
|
|
|
perror("read_cache");
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-04-26 16:25:05 +00:00
|
|
|
|
2005-04-07 22:13:13 +00:00
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
struct stat st;
|
2005-10-12 01:45:33 +00:00
|
|
|
unsigned int oldmode, newmode;
|
2005-04-07 22:13:13 +00:00
|
|
|
struct cache_entry *ce = active_cache[i];
|
2005-04-17 04:29:45 +00:00
|
|
|
int changed;
|
2005-04-07 22:13:13 +00:00
|
|
|
|
2005-07-14 23:55:06 +00:00
|
|
|
if (!ce_path_match(ce, pathspec))
|
|
|
|
continue;
|
|
|
|
|
2005-04-17 04:29:45 +00:00
|
|
|
if (ce_stage(ce)) {
|
2006-01-28 08:03:38 +00:00
|
|
|
struct {
|
|
|
|
struct combine_diff_path p;
|
|
|
|
unsigned char fill[4][20];
|
|
|
|
} combine;
|
2006-01-28 10:09:08 +00:00
|
|
|
int num_compare_stages = 0;
|
2006-01-28 08:03:38 +00: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;
|
|
|
|
memset(combine.p.sha1, 0, 100);
|
|
|
|
|
2005-11-30 05:06:10 +00:00
|
|
|
while (i < entries) {
|
|
|
|
struct cache_entry *nce = active_cache[i];
|
2006-01-28 08:03:38 +00:00
|
|
|
int stage;
|
2005-11-30 05:06:10 +00:00
|
|
|
|
|
|
|
if (strcmp(ce->name, nce->name))
|
|
|
|
break;
|
2006-01-28 08:03:38 +00:00
|
|
|
|
|
|
|
/* Stage #2 (ours) is the first parent,
|
|
|
|
* stage #3 (theirs) is the second.
|
|
|
|
*/
|
|
|
|
stage = ce_stage(nce);
|
2006-01-28 10:09:08 +00:00
|
|
|
if (2 <= stage) {
|
|
|
|
num_compare_stages++;
|
2006-01-28 08:03:38 +00:00
|
|
|
memcpy(combine.p.parent_sha1[stage-2],
|
|
|
|
nce->sha1, 20);
|
2006-01-28 10:09:08 +00:00
|
|
|
}
|
2006-01-28 08:03:38 +00:00
|
|
|
|
2005-11-30 05:06:10 +00:00
|
|
|
/* diff against the proper unmerged stage */
|
2006-01-28 08:03:38 +00:00
|
|
|
if (stage == diff_unmerged_stage)
|
2005-11-30 05:06:10 +00:00
|
|
|
ce = nce;
|
2005-04-17 04:29:45 +00:00
|
|
|
i++;
|
2005-11-30 05:06:10 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Compensate for loop update
|
|
|
|
*/
|
|
|
|
i--;
|
2006-01-28 08:03:38 +00:00
|
|
|
|
2006-01-28 10:09:08 +00:00
|
|
|
if (combine_merges && num_compare_stages == 2) {
|
2006-01-28 08:03:38 +00:00
|
|
|
show_combined_diff(&combine.p, 2,
|
|
|
|
dense_combined_merges,
|
2006-02-06 06:25:00 +00:00
|
|
|
NULL);
|
2006-01-28 10:09:08 +00:00
|
|
|
free(combine.p.path);
|
2006-01-28 08:03:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-01-28 10:09:08 +00:00
|
|
|
free(combine.p.path);
|
2006-01-28 08:03:38 +00:00
|
|
|
|
2005-11-30 05:06:10 +00:00
|
|
|
/*
|
|
|
|
* Show the diff for the 'ce' if we found the one
|
|
|
|
* from the desired stage.
|
|
|
|
*/
|
2006-01-28 08:03:38 +00:00
|
|
|
show_unmerge(ce->name);
|
2005-11-30 05:06:10 +00:00
|
|
|
if (ce_stage(ce) != diff_unmerged_stage)
|
|
|
|
continue;
|
2005-04-17 04:29:45 +00:00
|
|
|
}
|
2005-05-20 02:00:36 +00:00
|
|
|
|
2005-05-06 13:45:01 +00:00
|
|
|
if (lstat(ce->name, &st) < 0) {
|
2005-05-20 16:48:38 +00:00
|
|
|
if (errno != ENOENT && errno != ENOTDIR) {
|
2005-04-27 00:17:36 +00:00
|
|
|
perror(ce->name);
|
2005-04-15 22:08:09 +00:00
|
|
|
continue;
|
2005-05-20 02:00:36 +00:00
|
|
|
}
|
2005-04-27 22:22:02 +00:00
|
|
|
if (silent)
|
2005-04-27 00:17:36 +00:00
|
|
|
continue;
|
2005-04-27 16:21:00 +00:00
|
|
|
show_file('-', ce);
|
2005-04-07 22:13:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-05-15 02:04:25 +00:00
|
|
|
changed = ce_match_stat(ce, &st);
|
2005-09-21 07:00:47 +00:00
|
|
|
if (!changed && !diff_options.find_copies_harder)
|
2005-04-07 22:13:13 +00:00
|
|
|
continue;
|
2005-04-27 00:17:36 +00:00
|
|
|
oldmode = ntohl(ce->ce_mode);
|
2005-10-12 01:45:33 +00:00
|
|
|
|
|
|
|
newmode = DIFF_FILE_CANON_MODE(st.st_mode);
|
|
|
|
if (!trust_executable_bit &&
|
|
|
|
S_ISREG(newmode) && S_ISREG(oldmode) &&
|
|
|
|
((newmode ^ oldmode) == 0111))
|
|
|
|
newmode = oldmode;
|
|
|
|
show_modified(oldmode, newmode,
|
2005-06-19 20:14:05 +00:00
|
|
|
ce->sha1, (changed ? null_sha1 : ce->sha1),
|
2005-04-27 16:21:00 +00:00
|
|
|
ce->name);
|
2005-04-07 22:13:13 +00:00
|
|
|
}
|
2005-09-21 07:00:47 +00:00
|
|
|
diffcore_std(&diff_options);
|
|
|
|
diff_flush(&diff_options);
|
2005-04-07 22:13:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|