blob: dcd20e79e4fb67cdf6ed577a42277ad00b087c68 [file] [log] [blame]
Junio C Hamano22f77b72005-06-09 15:13:13 -07001/*
2 * Copyright (c) 2005 Junio C Hamano
3 */
4
5#include "cache.h"
6#include "diff.h"
7
Junio C Hamano6b5ee132005-09-21 00:00:47 -07008static struct diff_options diff_options;
Junio C Hamano22f77b72005-06-09 15:13:13 -07009
Petr Baudis4d1f1192005-07-29 11:01:26 +020010static const char diff_stages_usage[] =
Junio C Hamanodda2d792005-07-13 12:52:35 -070011"git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
12COMMON_DIFF_OPTIONS_HELP;
Junio C Hamano22f77b72005-06-09 15:13:13 -070013
Junio C Hamano77882f62006-04-10 15:57:24 -070014static void diff_stages(int stage1, int stage2, const char **pathspec)
Junio C Hamano99396642005-06-10 18:44:36 -070015{
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;
Junio C Hamano77882f62006-04-10 15:57:24 -070021 int len, skip;
22
Junio C Hamano99396642005-06-10 18:44:36 -070023 ce = active_cache[i];
Junio C Hamano77882f62006-04-10 15:57:24 -070024 skip = !ce_path_match(ce, pathspec);
Junio C Hamano99396642005-06-10 18:44:36 -070025 len = ce_namelen(ce);
26 name = ce->name;
27 for (;;) {
28 int stage = ce_stage(ce);
29 stages[stage] = ce;
30 if (active_nr <= ++i)
31 break;
32 ce = active_cache[i];
33 if (ce_namelen(ce) != len ||
34 memcmp(name, ce->name, len))
35 break;
36 }
37 one = stages[stage1];
38 two = stages[stage2];
Junio C Hamano77882f62006-04-10 15:57:24 -070039
40 if (skip || (!one && !two))
Junio C Hamano99396642005-06-10 18:44:36 -070041 continue;
42 if (!one)
Junio C Hamano6b5ee132005-09-21 00:00:47 -070043 diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
Junio C Hamano99396642005-06-10 18:44:36 -070044 two->sha1, name, NULL);
45 else if (!two)
Junio C Hamano6b5ee132005-09-21 00:00:47 -070046 diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
Junio C Hamano99396642005-06-10 18:44:36 -070047 one->sha1, name, NULL);
48 else if (memcmp(one->sha1, two->sha1, 20) ||
Junio C Hamano4727f642005-06-19 13:14:05 -070049 (one->ce_mode != two->ce_mode) ||
Junio C Hamano6b5ee132005-09-21 00:00:47 -070050 diff_options.find_copies_harder)
51 diff_change(&diff_options,
52 ntohl(one->ce_mode), ntohl(two->ce_mode),
Junio C Hamano4727f642005-06-19 13:14:05 -070053 one->sha1, two->sha1, name, NULL);
Junio C Hamano99396642005-06-10 18:44:36 -070054 }
55}
56
Junio C Hamano22f77b72005-06-09 15:13:13 -070057int main(int ac, const char **av)
58{
Junio C Hamano99396642005-06-10 18:44:36 -070059 int stage1, stage2;
Junio C Hamano77882f62006-04-10 15:57:24 -070060 const char *prefix = setup_git_directory();
61 const char **pathspec = NULL;
Junio C Hamano9ce392f2005-11-21 22:52:37 -080062
63 git_config(git_diff_config);
Junio C Hamano22f77b72005-06-09 15:13:13 -070064 read_cache();
Junio C Hamano6b5ee132005-09-21 00:00:47 -070065 diff_setup(&diff_options);
Junio C Hamano22f77b72005-06-09 15:13:13 -070066 while (1 < ac && av[1][0] == '-') {
67 const char *arg = av[1];
68 if (!strcmp(arg, "-r"))
69 ; /* as usual */
Junio C Hamano6b5ee132005-09-21 00:00:47 -070070 else {
71 int diff_opt_cnt;
72 diff_opt_cnt = diff_opt_parse(&diff_options,
73 av+1, ac-1);
74 if (diff_opt_cnt < 0)
75 usage(diff_stages_usage);
76 else if (diff_opt_cnt) {
77 av += diff_opt_cnt;
78 ac -= diff_opt_cnt;
79 continue;
80 }
81 else
Junio C Hamano22f77b72005-06-09 15:13:13 -070082 usage(diff_stages_usage);
83 }
Junio C Hamano22f77b72005-06-09 15:13:13 -070084 ac--; av++;
85 }
86
87 if (ac < 3 ||
88 sscanf(av[1], "%d", &stage1) != 1 ||
89 ! (0 <= stage1 && stage1 <= 3) ||
90 sscanf(av[2], "%d", &stage2) != 1 ||
Junio C Hamano6b5ee132005-09-21 00:00:47 -070091 ! (0 <= stage2 && stage2 <= 3))
Junio C Hamano22f77b72005-06-09 15:13:13 -070092 usage(diff_stages_usage);
93
94 av += 3; /* The rest from av[0] are for paths restriction. */
Junio C Hamano77882f62006-04-10 15:57:24 -070095 pathspec = get_pathspec(prefix, av);
Junio C Hamano6b5ee132005-09-21 00:00:47 -070096
97 if (diff_setup_done(&diff_options) < 0)
98 usage(diff_stages_usage);
Junio C Hamano22f77b72005-06-09 15:13:13 -070099
Junio C Hamano77882f62006-04-10 15:57:24 -0700100 diff_stages(stage1, stage2, pathspec);
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700101 diffcore_std(&diff_options);
102 diff_flush(&diff_options);
Junio C Hamano22f77b72005-06-09 15:13:13 -0700103 return 0;
104}