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