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