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