1/*
2* Copyright (c) 2005 Junio C Hamano
3*/
45
#include "cache.h"
6#include "diff.h"
78
static int diff_output_format = DIFF_FORMAT_HUMAN;
9static int detect_rename = 0;
10static int find_copies_harder = 0;
11static int diff_setup_opt = 0;
12static int diff_score_opt = 0;
13static const char *pickaxe = NULL;
14static int pickaxe_opts = 0;
15static int diff_break_opt = -1;
16static const char *orderfile = NULL;
17static const char *diff_filter = NULL;
1819
static char *diff_stages_usage =
20"git-diff-stages [-p] [-r] [-z] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] <stage1> <stage2> [<path>...]";
2122
static void diff_stages(int stage1, int stage2)
23{
24int i = 0;
25while (i < active_nr) {
26struct cache_entry *ce, *stages[4] = { NULL, };
27struct cache_entry *one, *two;
28const char *name;
29int len;
30ce = active_cache[i];
31len = ce_namelen(ce);
32name = ce->name;
33for (;;) {
34int stage = ce_stage(ce);
35stages[stage] = ce;
36if (active_nr <= ++i)
37break;
38ce = active_cache[i];
39if (ce_namelen(ce) != len ||
40memcmp(name, ce->name, len))
41break;
42}
43one = stages[stage1];
44two = stages[stage2];
45if (!one && !two)
46continue;
47if (!one)
48diff_addremove('+', ntohl(two->ce_mode),
49two->sha1, name, NULL);
50else if (!two)
51diff_addremove('-', ntohl(one->ce_mode),
52one->sha1, name, NULL);
53else if (memcmp(one->sha1, two->sha1, 20) ||
54(one->ce_mode != two->ce_mode) ||
55find_copies_harder)
56diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
57one->sha1, two->sha1, name, NULL);
58}
59}
6061
int main(int ac, const char **av)
62{
63int stage1, stage2;
6465
read_cache();
66while (1 < ac && av[1][0] == '-') {
67const char *arg = av[1];
68if (!strcmp(arg, "-r"))
69; /* as usual */
70else if (!strcmp(arg, "-p"))
71diff_output_format = DIFF_FORMAT_PATCH;
72else if (!strncmp(arg, "-B", 2)) {
73if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
74usage(diff_stages_usage);
75}
76else if (!strncmp(arg, "-M", 2)) {
77detect_rename = DIFF_DETECT_RENAME;
78if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
79usage(diff_stages_usage);
80}
81else if (!strncmp(arg, "-C", 2)) {
82detect_rename = DIFF_DETECT_COPY;
83if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
84usage(diff_stages_usage);
85}
86else if (!strcmp(arg, "--find-copies-harder"))
87find_copies_harder = 1;
88else if (!strcmp(arg, "-z"))
89diff_output_format = DIFF_FORMAT_MACHINE;
90else if (!strcmp(arg, "-R"))
91diff_setup_opt |= DIFF_SETUP_REVERSE;
92else if (!strncmp(arg, "-S", 2))
93pickaxe = arg + 2;
94else if (!strncmp(arg, "-O", 2))
95orderfile = arg + 2;
96else if (!strncmp(arg, "--diff-filter=", 14))
97diff_filter = arg + 14;
98else if (!strcmp(arg, "--pickaxe-all"))
99pickaxe_opts = DIFF_PICKAXE_ALL;
100else
101usage(diff_stages_usage);
102ac--; av++;
103}
104105
if (ac < 3 ||
106sscanf(av[1], "%d", &stage1) != 1 ||
107! (0 <= stage1 && stage1 <= 3) ||
108sscanf(av[2], "%d", &stage2) != 1 ||
109! (0 <= stage2 && stage2 <= 3) ||
110(find_copies_harder && detect_rename != DIFF_DETECT_COPY))
111usage(diff_stages_usage);
112113
av += 3; /* The rest from av[0] are for paths restriction. */
114diff_setup(diff_setup_opt);
115116
diff_stages(stage1, stage2);
117118
diffcore_std(av,
119detect_rename, diff_score_opt,
120pickaxe, pickaxe_opts,
121diff_break_opt,
122orderfile,
123diff_filter);
124diff_flush(diff_output_format);
125return 0;
126}