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 same(struct cache_entry
*a
, struct cache_entry
*b
)
78 return a
->ce_mode
== b
->ce_mode
&&
79 !memcmp(a
->sha1
, b
->sha1
, 20);
84 * This removes all trivial merges that don't change the tree
85 * and collapses them to state 0.
87 * _Any_ other merge is left to user policy. That includes "both
88 * created the same file", and "both removed the same file" - which are
89 * trivial, but the user might still want to _note_ it.
91 static struct cache_entry
*merge_entries(struct cache_entry
*a
,
92 struct cache_entry
*b
,
93 struct cache_entry
*c
)
95 int len
= ce_namelen(a
);
98 * Are they all the same filename? We won't do
101 if (ce_namelen(b
) != len
||
102 ce_namelen(c
) != len
||
103 memcmp(a
->name
, b
->name
, len
) ||
104 memcmp(a
->name
, c
->name
, len
))
108 * Ok, all three entries describe the same
109 * filename, but maybe the contents or file
112 * The trivial cases end up being the ones where two
113 * out of three files are the same:
114 * - both destinations the same, trivially take either
115 * - one of the destination versions hasn't changed,
118 * The "all entries exactly the same" case falls out as
119 * a special case of any of the "two same" cases.
121 * Here "a" is "original", and "b" and "c" are the two
122 * trees we are merging.
133 static void trivially_merge_cache(struct cache_entry
**src
, int nr
)
135 struct cache_entry
**dst
= src
;
138 struct cache_entry
*ce
, *result
;
141 if (nr
> 2 && (result
= merge_entries(ce
, src
[1], src
[2])) != NULL
) {
143 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
155 int main(int argc
, char **argv
)
158 unsigned char sha1
[20];
160 newfd
= open(".git/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
162 die("unable to create new cachefile");
163 atexit(remove_lock_file
);
166 for (i
= 1; i
< argc
; i
++) {
167 const char *arg
= argv
[i
];
169 /* "-m" stands for "merge", meaning we start in stage 1 */
170 if (!strcmp(arg
, "-m")) {
174 if (get_sha1_hex(arg
, sha1
) < 0)
175 usage("read-tree [-m] <sha1>");
177 usage("can't merge more than two trees");
178 if (read_tree(sha1
, "", 0) < 0)
179 die("failed to unpack tree object %s", arg
);
183 trivially_merge_cache(active_cache
, active_nr
);
184 if (write_cache(newfd
, active_cache
, active_nr
) ||
185 rename(".git/index.lock", ".git/index"))
186 die("unable to write new index file");