@matheusbsbs / matheus-bagliano / commits / d23748a6af
Fork 0

update-index: allow --index-info to add higher stages.

The new merge world order tells the merge strategies to leave the cache unmerged and store the automerge result in the working tree if automerge is not clean. This was done for the resolve strategy and recursive strategy when no rename is involved, but recording a conflicting merge in the rename case could not easily be done by the recursive strategy. This commit adds a new input format, in addition to the exsting two, to "update-index --index-info". (1) mode SP sha1 TAB path The first format is what "git-apply --index-info" reports, and used to reconstruct a partial tree that is used for phony merge base tree when falling back on 3-way merge. (2) mode SP type SP sha1 TAB path The second format is to stuff git-ls-tree output into the index file. (3) mode SP sha1 SP stage TAB path This format is to put higher order stages into the index file and matches git-ls-files --stage output. To place a higher stage entry to the index, the path should first be removed by feeding a mode=0 entry for the path, and then feeding necessary input lines in the (3) format. For example, starting with this index: $ git ls-files -s 100644 8a1218a1024a212bb3db30becd860315f9f3ac52 0 frotz $ git update-index --index-info ;# interactive session -- input follows... 0 0000000000000000000000000000000000000000 frotz 100644 8a1218a1024a212bb3db30becd860315f9f3ac52 1 frotz 100755 8a1218a1024a212bb3db30becd860315f9f3ac52 2 frotz The first line of the input feeds 0 as the mode to remove the path; the SHA1 does not matter as long as it is well formatted. Then the second and third line feeds stage 1 and stage 2 entries for that path. After the above, we would end up with this: $ git ls-files -s 100644 8a1218a1024a212bb3db30becd860315f9f3ac52 1 frotz 100755 8a1218a1024a212bb3db30becd860315f9f3ac52 2 frotz This completes the groundwork for the new merge world order. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 7, 2005 at 01:45 UTC d23748a6af3e3db2b239090553f92c3a779b3578
2 files changed +104 -18
Documentation/git-update-index.txt
+57
@@ -136,6 +136,63 @@ in the database but the file isn't available locally. '--info-only' is
136 useful when the file is available, but you do not wish to update the
137 object database.
138
139 +
140 +Using --index-info
141 +------------------
142 +
143 +`--index-info` is a more powerful mechanism that lets you feed
144 +multiple entry definitions from the standard input, and designed
145 +specifically for scripts. It can take inputs of three formats:
146 +
147 + . mode SP sha1 TAB path
148 ++
149 +The first format is what "git-apply --index-info"
150 +reports, and used to reconstruct a partial tree
151 +that is used for phony merge base tree when falling
152 +back on 3-way merge.
153 +
154 + . mode SP type SP sha1 TAB path
155 ++
156 +The second format is to stuff git-ls-tree output
157 +into the index file.
158 +
159 + . mode SP sha1 SP stage TAB path
160 ++
161 +This format is to put higher order stages into the
162 +index file and matches git-ls-files --stage output.
163 +
164 +To place a higher stage entry to the index, the path should
165 +first be removed by feeding a mode=0 entry for the path, and
166 +then feeding necessary input lines in the third format.
167 +
168 +For example, starting with this index:
169 +
170 +------------
171 +$ git ls-files -s
172 +100644 8a1218a1024a212bb3db30becd860315f9f3ac52 0 frotz
173 +------------
174 +
175 +you can feed the following input to `--index-info`:
176 +
177 +------------
178 +$ git update-index --index-info
179 +0 0000000000000000000000000000000000000000 frotz
180 +100644 8a1218a1024a212bb3db30becd860315f9f3ac52 1 frotz
181 +100755 8a1218a1024a212bb3db30becd860315f9f3ac52 2 frotz
182 +------------
183 +
184 +The first line of the input feeds 0 as the mode to remove the
185 +path; the SHA1 does not matter as long as it is well formatted.
186 +Then the second and third line feeds stage 1 and stage 2 entries
187 +for that path. After the above, we would end up with this:
188 +
189 +------------
190 +$ git ls-files -s
191 +100644 8a1218a1024a212bb3db30becd860315f9f3ac52 1 frotz
192 +100755 8a1218a1024a212bb3db30becd860315f9f3ac52 2 frotz
193 +------------
194 +
195 +
196 Examples
197 --------
198 To update and refresh only the files already checked out:
update-index.c
+47 -18
@@ -256,35 +256,30 @@ inside:
256 }
257 }
258
259 -static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
259 +static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
260 + const char *path, int stage)
261 {
262 int size, len, option;
262 - unsigned int mode;
263 - unsigned char sha1[20];
263 struct cache_entry *ce;
264
266 - if (sscanf(arg1, "%o", &mode) != 1)
267 - return -1;
268 - if (get_sha1_hex(arg2, sha1))
269 - return -1;
270 - if (!verify_path(arg3))
265 + if (!verify_path(path))
266 return -1;
267
273 - len = strlen(arg3);
268 + len = strlen(path);
269 size = cache_entry_size(len);
270 ce = xmalloc(size);
271 memset(ce, 0, size);
272
273 memcpy(ce->sha1, sha1, 20);
279 - memcpy(ce->name, arg3, len);
280 - ce->ce_flags = htons(len);
274 + memcpy(ce->name, path, len);
275 + ce->ce_flags = create_ce_flags(len, stage);
276 ce->ce_mode = create_ce_mode(mode);
277 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
278 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
279 if (add_cache_entry(ce, option))
280 return error("%s: cannot add to the index - missing --add option?",
286 - arg3);
287 - report("add '%s'", arg3);
281 + path);
282 + report("add '%s'", path);
283 return 0;
284 }
285
@@ -342,7 +337,24 @@ static void read_index_info(int line_termination)
337 char *path_name;
338 unsigned char sha1[20];
339 unsigned int mode;
345 -
340 + int stage;
341 +
342 + /* This reads lines formatted in one of three formats:
343 + *
344 + * (1) mode SP sha1 TAB path
345 + * The first format is what "git-apply --index-info"
346 + * reports, and used to reconstruct a partial tree
347 + * that is used for phony merge base tree when falling
348 + * back on 3-way merge.
349 + *
350 + * (2) mode SP type SP sha1 TAB path
351 + * The second format is to stuff git-ls-tree output
352 + * into the index file.
353 + *
354 + * (3) mode SP sha1 SP stage TAB path
355 + * This format is to put higher order stages into the
356 + * index file and matches git-ls-files --stage output.
357 + */
358 read_line(&buf, stdin, line_termination);
359 if (buf.eof)
360 break;
@@ -354,9 +366,19 @@ static void read_index_info(int line_termination)
366 tab = strchr(ptr, '\t');
367 if (!tab || tab - ptr < 41)
368 goto bad_line;
369 +
370 + if (tab[-2] == ' ' && '1' <= tab[-1] && tab[-1] <= '3') {
371 + stage = tab[-1] - '0';
372 + ptr = tab + 1; /* point at the head of path */
373 + tab = tab - 2; /* point at tail of sha1 */
374 + }
375 + else {
376 + stage = 0;
377 + ptr = tab + 1; /* point at the head of path */
378 + }
379 +
380 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
381 goto bad_line;
359 - ptr = tab + 1;
382
383 if (line_termination && ptr[0] == '"')
384 path_name = unquote_c_style(ptr, NULL);
@@ -382,7 +404,7 @@ static void read_index_info(int line_termination)
404 * ptr[-41] is at the beginning of sha1
405 */
406 ptr[-42] = ptr[-1] = 0;
385 - if (add_cacheinfo(buf.buf, ptr-41, path_name))
407 + if (add_cacheinfo(mode, sha1, path_name, stage))
408 die("git-update-index: unable to update %s",
409 path_name);
410 }
@@ -449,10 +471,17 @@ int main(int argc, const char **argv)
471 continue;
472 }
473 if (!strcmp(path, "--cacheinfo")) {
474 + unsigned char sha1[20];
475 + unsigned int mode;
476 +
477 if (i+3 >= argc)
478 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
454 - if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
455 - die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
479 +
480 + if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
481 + get_sha1_hex(argv[i+2], sha1) ||
482 + add_cacheinfo(mode, sha1, argv[i+3], 0))
483 + die("git-update-index: --cacheinfo"
484 + " cannot add %s", argv[i+3]);
485 i += 3;
486 continue;
487 }