Diff clean-up.
[git/git.git] / diff-stages.c
CommitLineData
22f77b77
JH
1/*
2 * Copyright (c) 2005 Junio C Hamano
3 */
4
5#include "cache.h"
6#include "diff.h"
7
6b5ee137 8static struct diff_options diff_options;
22f77b77 9
4d1f1190 10static const char diff_stages_usage[] =
dda2d79a
JH
11"git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
12COMMON_DIFF_OPTIONS_HELP;
22f77b77 13
9939664a
JH
14static void diff_stages(int stage1, int stage2)
15{
16 int i = 0;
17 while (i < active_nr) {
18 struct cache_entry *ce, *stages[4] = { NULL, };
19 struct cache_entry *one, *two;
20 const char *name;
21 int len;
22 ce = active_cache[i];
23 len = ce_namelen(ce);
24 name = ce->name;
25 for (;;) {
26 int stage = ce_stage(ce);
27 stages[stage] = ce;
28 if (active_nr <= ++i)
29 break;
30 ce = active_cache[i];
31 if (ce_namelen(ce) != len ||
32 memcmp(name, ce->name, len))
33 break;
34 }
35 one = stages[stage1];
36 two = stages[stage2];
37 if (!one && !two)
38 continue;
39 if (!one)
6b5ee137 40 diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
9939664a
JH
41 two->sha1, name, NULL);
42 else if (!two)
6b5ee137 43 diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
9939664a
JH
44 one->sha1, name, NULL);
45 else if (memcmp(one->sha1, two->sha1, 20) ||
4727f640 46 (one->ce_mode != two->ce_mode) ||
6b5ee137
JH
47 diff_options.find_copies_harder)
48 diff_change(&diff_options,
49 ntohl(one->ce_mode), ntohl(two->ce_mode),
4727f640 50 one->sha1, two->sha1, name, NULL);
9939664a
JH
51 }
52}
53
22f77b77
JH
54int main(int ac, const char **av)
55{
9939664a 56 int stage1, stage2;
22f77b77
JH
57
58 read_cache();
6b5ee137 59 diff_setup(&diff_options);
22f77b77
JH
60 while (1 < ac && av[1][0] == '-') {
61 const char *arg = av[1];
62 if (!strcmp(arg, "-r"))
63 ; /* as usual */
6b5ee137
JH
64 else {
65 int diff_opt_cnt;
66 diff_opt_cnt = diff_opt_parse(&diff_options,
67 av+1, ac-1);
68 if (diff_opt_cnt < 0)
22f77b77 69 usage(diff_stages_usage);
6b5ee137
JH
70 else if (diff_opt_cnt) {
71 av += diff_opt_cnt;
72 ac -= diff_opt_cnt;
73 continue;
74 }
75 else
22f77b77
JH
76 usage(diff_stages_usage);
77 }
22f77b77
JH
78 ac--; av++;
79 }
80
81 if (ac < 3 ||
82 sscanf(av[1], "%d", &stage1) != 1 ||
83 ! (0 <= stage1 && stage1 <= 3) ||
84 sscanf(av[2], "%d", &stage2) != 1 ||
6b5ee137 85 ! (0 <= stage2 && stage2 <= 3))
22f77b77
JH
86 usage(diff_stages_usage);
87
88 av += 3; /* The rest from av[0] are for paths restriction. */
6b5ee137 89 diff_options.paths = av;
22f77b77 90
6b5ee137
JH
91 if (diff_setup_done(&diff_options) < 0)
92 usage(diff_stages_usage);
22f77b77 93
6b5ee137
JH
94 diff_stages(stage1, stage2);
95 diffcore_std(&diff_options);
96 diff_flush(&diff_options);
22f77b77
JH
97 return 0;
98}