[PATCH] Teach diff-tree-helper to handle unmerged paths.
[git/git.git] / diff-cache.c
CommitLineData
e74f8f6a 1#include "cache.h"
f92a4465 2#include "diff.h"
e74f8f6a
LT
3
4static int cached_only = 0;
f92a4465 5static int generate_patch = 0;
e74f8f6a
LT
6static int line_termination = '\n';
7
e74f8f6a 8/* A file entry went away or appeared */
b5af9107 9static void show_file(const char *prefix, struct cache_entry *ce)
e74f8f6a 10{
f92a4465
JH
11 if (generate_patch)
12 diff_addremove(prefix[0], ntohl(ce->ce_mode),
13 ce->sha1, ce->name, NULL);
14 else
15 printf("%s%06o\tblob\t%s\t%s%c", prefix, ntohl(ce->ce_mode),
16 sha1_to_hex(ce->sha1), ce->name,
17 line_termination);
e74f8f6a
LT
18}
19
b5af9107 20static int show_modified(struct cache_entry *old, struct cache_entry *new)
e74f8f6a 21{
b5af9107
LT
22 unsigned int mode = ntohl(new->ce_mode), oldmode;
23 unsigned char *sha1 = new->sha1;
24 unsigned char old_sha1_hex[60];
e74f8f6a
LT
25
26 if (!cached_only) {
27 static unsigned char no_sha1[20];
b5af9107 28 int changed;
e74f8f6a 29 struct stat st;
b5af9107
LT
30 if (stat(new->name, &st) < 0) {
31 show_file("-", old);
e74f8f6a
LT
32 return -1;
33 }
b5af9107 34 changed = cache_match_stat(new, &st);
e74f8f6a 35 if (changed) {
b5af9107
LT
36 mode = st.st_mode;
37 sha1 = no_sha1;
e74f8f6a
LT
38 }
39 }
40
b5af9107
LT
41 oldmode = ntohl(old->ce_mode);
42 if (mode == oldmode && !memcmp(sha1, old->sha1, 20))
e74f8f6a
LT
43 return 0;
44
f92a4465
JH
45 if (generate_patch)
46 diff_change(oldmode, mode,
47 old->sha1, sha1, old->name, NULL);
48 else {
49 strcpy(old_sha1_hex, sha1_to_hex(old->sha1));
50 printf("*%06o->%06o\tblob\t%s->%s\t%s%c", oldmode, mode,
51 old_sha1_hex, sha1_to_hex(sha1),
52 old->name, line_termination);
53 }
e74f8f6a
LT
54 return 0;
55}
56
b5af9107 57static int diff_cache(struct cache_entry **ac, int entries)
e74f8f6a 58{
b5af9107
LT
59 while (entries) {
60 struct cache_entry *ce = *ac;
b0fe89ca 61 int same = (entries > 1) && same_name(ce, ac[1]);
e74f8f6a 62
b0fe89ca
LT
63 switch (ce_stage(ce)) {
64 case 0:
65 /* No stage 1 entry? That means it's a new file */
66 if (!same) {
67 show_file("+", ce);
68 break;
69 }
70 /* Show difference between old and new */
71 show_modified(ac[1], ce);
72 break;
73 case 1:
74 /* No stage 3 (merge) entry? That means it's been deleted */
75 if (!same) {
76 show_file("-", ce);
77 break;
78 }
79 /* Otherwise we fall through to the "unmerged" case */
80 case 3:
f92a4465
JH
81 if (generate_patch)
82 diff_unmerge(ce->name);
83 else
84 printf("U %s%c", ce->name, line_termination);
b0fe89ca
LT
85 break;
86
87 default:
88 die("impossible cache entry stage");
e74f8f6a 89 }
b0fe89ca
LT
90
91 /*
92 * Ignore all the different stages for this file,
93 * we've handled the relevant cases now.
94 */
95 do {
e74f8f6a
LT
96 ac++;
97 entries--;
b0fe89ca 98 } while (entries && same_name(ce, ac[0]));
e74f8f6a
LT
99 }
100 return 0;
101}
102
b0fe89ca
LT
103/*
104 * This turns all merge entries into "stage 3". That guarantees that
105 * when we read in the new tree (into "stage 1"), we won't lose sight
106 * of the fact that we had unmerged entries.
107 */
108static void mark_merge_entries(void)
b5af9107
LT
109{
110 int i;
111 for (i = 0; i < active_nr; i++) {
112 struct cache_entry *ce = active_cache[i];
113 if (!ce_stage(ce))
5697ecc7 114 continue;
b0fe89ca 115 ce->ce_flags |= htons(CE_STAGEMASK);
b5af9107
LT
116 }
117}
118
f92a4465
JH
119static char *diff_cache_usage =
120"diff-cache [-r] [-z] [-p] [--cached] <tree sha1>";
c5bac17a 121
e74f8f6a
LT
122int main(int argc, char **argv)
123{
124 unsigned char tree_sha1[20];
125 void *tree;
126 unsigned long size;
e74f8f6a
LT
127
128 read_cache();
129 while (argc > 2) {
130 char *arg = argv[1];
131 argv++;
132 argc--;
133 if (!strcmp(arg, "-r")) {
b5af9107 134 /* We accept the -r flag just to look like diff-tree */
e74f8f6a
LT
135 continue;
136 }
f92a4465
JH
137 if (!strcmp(arg, "-p")) {
138 generate_patch = 1;
139 continue;
140 }
e74f8f6a
LT
141 if (!strcmp(arg, "-z")) {
142 line_termination = '\0';
143 continue;
144 }
145 if (!strcmp(arg, "--cached")) {
146 cached_only = 1;
147 continue;
148 }
c5bac17a 149 usage(diff_cache_usage);
e74f8f6a
LT
150 }
151
152 if (argc != 2 || get_sha1_hex(argv[1], tree_sha1))
c5bac17a 153 usage(diff_cache_usage);
e74f8f6a 154
b0fe89ca 155 mark_merge_entries();
b5af9107 156
38357e67 157 tree = read_tree_with_tree_or_commit_sha1(tree_sha1, &size, 0);
e74f8f6a
LT
158 if (!tree)
159 die("bad tree object %s", argv[1]);
b5af9107
LT
160 if (read_tree(tree, size, 1))
161 die("unable to read tree object %s", argv[1]);
e74f8f6a 162
b5af9107 163 return diff_cache(active_cache, active_nr);
e74f8f6a 164}