2 * Copyright (c) 2005 Junio C Hamano
9 static struct diff_options diff_options;
11 static const char diff_stages_usage[] =
12 "git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
13 COMMON_DIFF_OPTIONS_HELP;
15 static void diff_stages(int stage1, int stage2, const char **pathspec)
18 while (i < active_nr) {
19 struct cache_entry *ce, *stages[4] = { NULL, };
20 struct cache_entry *one, *two;
25 skip = !ce_path_match(ce, pathspec);
29 int stage = ce_stage(ce);
34 if (ce_namelen(ce) != len ||
35 memcmp(name, ce->name, len))
41 if (skip || (!one && !two))
44 diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
45 two->sha1, name, NULL);
47 diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
48 one->sha1, name, NULL);
49 else if (memcmp(one->sha1, two->sha1, 20) ||
50 (one->ce_mode != two->ce_mode) ||
51 diff_options.find_copies_harder)
52 diff_change(&diff_options,
53 ntohl(one->ce_mode), ntohl(two->ce_mode),
54 one->sha1, two->sha1, name, NULL);
58 int cmd_diff_stages(int ac, const char **av, char **envp)
61 const char *prefix = setup_git_directory();
62 const char **pathspec = NULL;
64 git_config(git_default_config); /* no "diff" UI options */
66 diff_setup(&diff_options);
67 while (1 < ac && av[1][0] == '-') {
68 const char *arg = av[1];
69 if (!strcmp(arg, "-r"))
73 diff_opt_cnt = diff_opt_parse(&diff_options,
76 usage(diff_stages_usage);
77 else if (diff_opt_cnt) {
83 usage(diff_stages_usage);
88 if (!diff_options.output_format)
89 diff_options.output_format = DIFF_FORMAT_RAW;
92 sscanf(av[1], "%d", &stage1) != 1 ||
93 ! (0 <= stage1 && stage1 <= 3) ||
94 sscanf(av[2], "%d", &stage2) != 1 ||
95 ! (0 <= stage2 && stage2 <= 3))
96 usage(diff_stages_usage);
98 av += 3; /* The rest from av[0] are for paths restriction. */
99 pathspec = get_pathspec(prefix, av);
101 if (diff_setup_done(&diff_options) < 0)
102 usage(diff_stages_usage);
104 diff_stages(stage1, stage2, pathspec);
105 diffcore_std(&diff_options);
106 diff_flush(&diff_options);