merge-ort: implement unique_path() helper

Implement unique_path(), based on the one from merge-recursive.c. It is simplified, however, due to: (1) using strmaps, and (2) the fact that merge-ort lets the checkout codepath handle possible collisions with the working tree means that other code locations don't have to. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Jan 1, 2021 at 02:34 UTC 5a1a1e8ea9fdd4a575c711b91f176838ea502e44
1 file changed +24 -1
merge-ort.c
+24 -1
@@ -343,11 +343,34 @@ static void path_msg(struct merge_options *opt,
343 strbuf_addch(sb, '\n');
344 }
345
346 +/* add a string to a strbuf, but converting "/" to "_" */
347 +static void add_flattened_path(struct strbuf *out, const char *s)
348 +{
349 + size_t i = out->len;
350 + strbuf_addstr(out, s);
351 + for (; i < out->len; i++)
352 + if (out->buf[i] == '/')
353 + out->buf[i] = '_';
354 +}
355 +
356 static char *unique_path(struct strmap *existing_paths,
357 const char *path,
358 const char *branch)
359 {
350 - die("Not yet implemented.");
360 + struct strbuf newpath = STRBUF_INIT;
361 + int suffix = 0;
362 + size_t base_len;
363 +
364 + strbuf_addf(&newpath, "%s~", path);
365 + add_flattened_path(&newpath, branch);
366 +
367 + base_len = newpath.len;
368 + while (strmap_contains(existing_paths, newpath.buf)) {
369 + strbuf_setlen(&newpath, base_len);
370 + strbuf_addf(&newpath, "_%d", suffix++);
371 + }
372 +
373 + return strbuf_detach(&newpath, NULL);
374 }
375
376 /*** Function Grouping: functions related to collect_merge_info() ***/