2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 static int unpack_tree(unsigned char *sha1
)
15 buffer
= read_object_with_reference(sha1
, "tree", &size
, 0);
18 return read_tree(buffer
, size
, stage
);
21 static char *lockfile_name
;
23 static void remove_lock_file(void)
26 unlink(lockfile_name
);
29 static int path_matches(struct cache_entry
*a
, struct cache_entry
*b
)
31 int len
= ce_namelen(a
);
32 return ce_namelen(b
) == len
&&
33 !memcmp(a
->name
, b
->name
, len
);
36 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
38 return a
->ce_mode
== b
->ce_mode
&&
39 !memcmp(a
->sha1
, b
->sha1
, 20);
44 * This removes all trivial merges that don't change the tree
45 * and collapses them to state 0.
47 * _Any_ other merge is left to user policy. That includes "both
48 * created the same file", and "both removed the same file" - which are
49 * trivial, but the user might still want to _note_ it.
51 static struct cache_entry
*merge_entries(struct cache_entry
*a
,
52 struct cache_entry
*b
,
53 struct cache_entry
*c
)
55 int len
= ce_namelen(a
);
58 * Are they all the same filename? We won't do
61 if (ce_namelen(b
) != len
||
62 ce_namelen(c
) != len
||
63 memcmp(a
->name
, b
->name
, len
) ||
64 memcmp(a
->name
, c
->name
, len
))
68 * Ok, all three entries describe the same
69 * filename, but maybe the contents or file
72 * The trivial cases end up being the ones where two
73 * out of three files are the same:
74 * - both destinations the same, trivially take either
75 * - one of the destination versions hasn't changed,
78 * The "all entries exactly the same" case falls out as
79 * a special case of any of the "two same" cases.
81 * Here "a" is "original", and "b" and "c" are the two
82 * trees we are merging.
93 static void trivially_merge_cache(struct cache_entry
**src
, int nr
)
95 static struct cache_entry null_entry
;
96 struct cache_entry
**dst
= src
;
97 struct cache_entry
*old
= &null_entry
;
100 struct cache_entry
*ce
, *result
;
104 /* We throw away original cache entries except for the stat information */
112 if (nr
> 2 && (result
= merge_entries(ce
, src
[1], src
[2])) != NULL
) {
114 * See if we can re-use the old CE directly?
115 * That way we get the uptodate stat info.
117 if (path_matches(result
, old
) && same(result
, old
))
120 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
131 static void merge_stat_info(struct cache_entry
**src
, int nr
)
133 static struct cache_entry null_entry
;
134 struct cache_entry
**dst
= src
;
135 struct cache_entry
*old
= &null_entry
;
138 struct cache_entry
*ce
;
142 /* We throw away original cache entries except for the stat information */
150 if (path_matches(ce
, old
) && same(ce
, old
))
152 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
159 static char *read_tree_usage
= "read-tree (<sha> | -m <sha1> [<sha2> <sha3>])";
161 int main(int argc
, char **argv
)
164 unsigned char sha1
[20];
165 static char lockfile
[MAXPATHLEN
+1];
166 const char *indexfile
= get_index_file();
168 snprintf(lockfile
, sizeof(lockfile
), "%s.lock", indexfile
);
170 newfd
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
172 die("unable to create new cachefile");
173 atexit(remove_lock_file
);
174 lockfile_name
= lockfile
;
177 for (i
= 1; i
< argc
; i
++) {
178 const char *arg
= argv
[i
];
180 /* "-m" stands for "merge", meaning we start in stage 1 */
181 if (!strcmp(arg
, "-m")) {
184 die("-m needs to come first");
186 for (i
= 0; i
< active_nr
; i
++) {
187 if (ce_stage(active_cache
[i
]))
188 die("you need to resolve your current index first");
194 if (get_sha1_hex(arg
, sha1
) < 0)
195 usage(read_tree_usage
);
197 usage(read_tree_usage
);
198 if (unpack_tree(sha1
) < 0)
199 die("failed to unpack tree object %s", arg
);
204 case 4: /* Three-way merge */
205 trivially_merge_cache(active_cache
, active_nr
);
207 case 2: /* Just read a tree, merge with old cache contents */
208 merge_stat_info(active_cache
, active_nr
);
211 die("just how do you expect me to merge %d trees?", stage
-1);
214 if (write_cache(newfd
, active_cache
, active_nr
) || rename(lockfile
, indexfile
))
215 die("unable to write new index file");
216 lockfile_name
= NULL
;