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