2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 static int read_one_entry(unsigned char *sha1
, const char *base
, int baselen
, const char *pathname
, unsigned mode
)
12 int len
= strlen(pathname
);
13 unsigned int size
= cache_entry_size(baselen
+ len
);
14 struct cache_entry
*ce
= malloc(size
);
18 ce
->ce_mode
= create_ce_mode(mode
);
19 ce
->ce_flags
= create_ce_flags(baselen
+ len
, stage
);
20 memcpy(ce
->name
, base
, baselen
);
21 memcpy(ce
->name
+ baselen
, pathname
, len
+1);
22 memcpy(ce
->sha1
, sha1
, 20);
23 return add_cache_entry(ce
, 1);
26 static int read_tree(unsigned char *sha1
, const char *base
, int baselen
)
32 buffer
= read_sha1_file(sha1
, type
, &size
);
35 if (strcmp(type
, "tree"))
38 int len
= strlen(buffer
)+1;
39 unsigned char *sha1
= buffer
+ len
;
40 char *path
= strchr(buffer
, ' ')+1;
43 if (size
< len
+ 20 || sscanf(buffer
, "%o", &mode
) != 1)
51 int pathlen
= strlen(path
);
52 char *newbase
= malloc(baselen
+ 1 + pathlen
);
53 memcpy(newbase
, base
, baselen
);
54 memcpy(newbase
+ baselen
, path
, pathlen
);
55 newbase
[baselen
+ pathlen
] = '/';
56 retval
= read_tree(sha1
, newbase
, baselen
+ pathlen
+ 1);
62 if (read_one_entry(sha1
, base
, baselen
, path
, mode
) < 0)
68 static int remove_lock
= 0;
70 static void remove_lock_file(void)
73 unlink(".git/index.lock");
76 static int path_matches(struct cache_entry
*a
, struct cache_entry
*b
)
78 int len
= ce_namelen(a
);
79 return ce_namelen(b
) == len
&&
80 !memcmp(a
->name
, b
->name
, len
);
83 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
85 return a
->ce_mode
== b
->ce_mode
&&
86 !memcmp(a
->sha1
, b
->sha1
, 20);
91 * This removes all trivial merges that don't change the tree
92 * and collapses them to state 0.
94 * _Any_ other merge is left to user policy. That includes "both
95 * created the same file", and "both removed the same file" - which are
96 * trivial, but the user might still want to _note_ it.
98 static struct cache_entry
*merge_entries(struct cache_entry
*a
,
99 struct cache_entry
*b
,
100 struct cache_entry
*c
)
102 int len
= ce_namelen(a
);
105 * Are they all the same filename? We won't do
108 if (ce_namelen(b
) != len
||
109 ce_namelen(c
) != len
||
110 memcmp(a
->name
, b
->name
, len
) ||
111 memcmp(a
->name
, c
->name
, len
))
115 * Ok, all three entries describe the same
116 * filename, but maybe the contents or file
119 * The trivial cases end up being the ones where two
120 * out of three files are the same:
121 * - both destinations the same, trivially take either
122 * - one of the destination versions hasn't changed,
125 * The "all entries exactly the same" case falls out as
126 * a special case of any of the "two same" cases.
128 * Here "a" is "original", and "b" and "c" are the two
129 * trees we are merging.
140 static void trivially_merge_cache(struct cache_entry
**src
, int nr
)
142 static struct cache_entry null_entry
;
143 struct cache_entry
**dst
= src
;
144 struct cache_entry
*old
= &null_entry
;
147 struct cache_entry
*ce
, *result
;
151 /* We throw away original cache entries except for the stat information */
159 if (nr
> 2 && (result
= merge_entries(ce
, src
[1], src
[2])) != NULL
) {
161 * See if we can re-use the old CE directly?
162 * That way we get the uptodate stat info.
164 if (path_matches(result
, old
) && same(result
, old
))
167 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
178 static void merge_stat_info(struct cache_entry
**src
, int nr
)
180 static struct cache_entry null_entry
;
181 struct cache_entry
**dst
= src
;
182 struct cache_entry
*old
= &null_entry
;
185 struct cache_entry
*ce
;
189 /* We throw away original cache entries except for the stat information */
197 if (path_matches(ce
, old
) && same(ce
, old
))
199 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
206 int main(int argc
, char **argv
)
209 unsigned char sha1
[20];
211 newfd
= open(".git/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
213 die("unable to create new cachefile");
214 atexit(remove_lock_file
);
218 for (i
= 1; i
< argc
; i
++) {
219 const char *arg
= argv
[i
];
221 /* "-m" stands for "merge", meaning we start in stage 1 */
222 if (!strcmp(arg
, "-m")) {
225 usage("-m needs to come first");
227 for (i
= 0; i
< active_nr
; i
++) {
228 if (ce_stage(active_cache
[i
]))
229 usage("you need to resolve your current index first");
235 if (get_sha1_hex(arg
, sha1
) < 0)
236 usage("read-tree [-m] <sha1>");
238 usage("can't merge more than two trees");
239 if (read_tree(sha1
, "", 0) < 0)
240 die("failed to unpack tree object %s", arg
);
245 case 4: /* Three-way merge */
246 trivially_merge_cache(active_cache
, active_nr
);
248 case 2: /* Just read a tree, merge with old cache contents */
249 merge_stat_info(active_cache
, active_nr
);
252 die("just how do you expect me to merge %d trees?", stage
-1);
255 if (write_cache(newfd
, active_cache
, active_nr
) ||
256 rename(".git/index.lock", ".git/index"))
257 die("unable to write new index file");