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_recursive(void *buffer
, const char *type
,
28 const char *base
, int baselen
)
30 if (!buffer
|| strcmp(type
, "tree"))
33 int len
= strlen(buffer
)+1;
34 unsigned char *sha1
= buffer
+ len
;
35 char *path
= strchr(buffer
, ' ')+1;
38 if (size
< len
+ 20 || sscanf(buffer
, "%o", &mode
) != 1)
46 int pathlen
= strlen(path
);
47 char *newbase
= malloc(baselen
+ 1 + pathlen
);
50 unsigned long eltsize
;
52 eltbuf
= read_sha1_file(sha1
, elttype
, &eltsize
);
55 memcpy(newbase
, base
, baselen
);
56 memcpy(newbase
+ baselen
, path
, pathlen
);
57 newbase
[baselen
+ pathlen
] = '/';
58 retval
= read_tree_recursive(eltbuf
, elttype
, eltsize
,
60 baselen
+ pathlen
+ 1);
67 if (read_one_entry(sha1
, base
, baselen
, path
, mode
) < 0)
73 static int read_tree(unsigned char *sha1
, const char *base
, int baselen
)
78 buffer
= read_tree_with_tree_or_commit_sha1(sha1
, &size
, 0);
79 return read_tree_recursive(buffer
, "tree", size
, base
, baselen
);
82 static int remove_lock
= 0;
84 static void remove_lock_file(void)
87 unlink(".git/index.lock");
90 static int path_matches(struct cache_entry
*a
, struct cache_entry
*b
)
92 int len
= ce_namelen(a
);
93 return ce_namelen(b
) == len
&&
94 !memcmp(a
->name
, b
->name
, len
);
97 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
99 return a
->ce_mode
== b
->ce_mode
&&
100 !memcmp(a
->sha1
, b
->sha1
, 20);
105 * This removes all trivial merges that don't change the tree
106 * and collapses them to state 0.
108 * _Any_ other merge is left to user policy. That includes "both
109 * created the same file", and "both removed the same file" - which are
110 * trivial, but the user might still want to _note_ it.
112 static struct cache_entry
*merge_entries(struct cache_entry
*a
,
113 struct cache_entry
*b
,
114 struct cache_entry
*c
)
116 int len
= ce_namelen(a
);
119 * Are they all the same filename? We won't do
122 if (ce_namelen(b
) != len
||
123 ce_namelen(c
) != len
||
124 memcmp(a
->name
, b
->name
, len
) ||
125 memcmp(a
->name
, c
->name
, len
))
129 * Ok, all three entries describe the same
130 * filename, but maybe the contents or file
133 * The trivial cases end up being the ones where two
134 * out of three files are the same:
135 * - both destinations the same, trivially take either
136 * - one of the destination versions hasn't changed,
139 * The "all entries exactly the same" case falls out as
140 * a special case of any of the "two same" cases.
142 * Here "a" is "original", and "b" and "c" are the two
143 * trees we are merging.
154 static void trivially_merge_cache(struct cache_entry
**src
, int nr
)
156 static struct cache_entry null_entry
;
157 struct cache_entry
**dst
= src
;
158 struct cache_entry
*old
= &null_entry
;
161 struct cache_entry
*ce
, *result
;
165 /* We throw away original cache entries except for the stat information */
173 if (nr
> 2 && (result
= merge_entries(ce
, src
[1], src
[2])) != NULL
) {
175 * See if we can re-use the old CE directly?
176 * That way we get the uptodate stat info.
178 if (path_matches(result
, old
) && same(result
, old
))
181 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
192 static void merge_stat_info(struct cache_entry
**src
, int nr
)
194 static struct cache_entry null_entry
;
195 struct cache_entry
**dst
= src
;
196 struct cache_entry
*old
= &null_entry
;
199 struct cache_entry
*ce
;
203 /* We throw away original cache entries except for the stat information */
211 if (path_matches(ce
, old
) && same(ce
, old
))
213 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
220 int main(int argc
, char **argv
)
223 unsigned char sha1
[20];
225 newfd
= open(".git/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
227 die("unable to create new cachefile");
228 atexit(remove_lock_file
);
232 for (i
= 1; i
< argc
; i
++) {
233 const char *arg
= argv
[i
];
235 /* "-m" stands for "merge", meaning we start in stage 1 */
236 if (!strcmp(arg
, "-m")) {
239 usage("-m needs to come first");
241 for (i
= 0; i
< active_nr
; i
++) {
242 if (ce_stage(active_cache
[i
]))
243 usage("you need to resolve your current index first");
249 if (get_sha1_hex(arg
, sha1
) < 0)
250 usage("read-tree [-m] <sha1>");
252 usage("can't merge more than two trees");
253 if (read_tree(sha1
, "", 0) < 0)
254 die("failed to unpack tree object %s", arg
);
259 case 4: /* Three-way merge */
260 trivially_merge_cache(active_cache
, active_nr
);
262 case 2: /* Just read a tree, merge with old cache contents */
263 merge_stat_info(active_cache
, active_nr
);
266 die("just how do you expect me to merge %d trees?", stage
-1);
269 if (write_cache(newfd
, active_cache
, active_nr
) ||
270 rename(".git/index.lock", ".git/index"))
271 die("unable to write new index file");