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