Skip to content

Commit 5a1a1e8

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23366d2 commit 5a1a1e8

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

merge-ort.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,34 @@ static void path_msg(struct merge_options *opt,
343343
strbuf_addch(sb, '\n');
344344
}
345345

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+
346356
static char *unique_path(struct strmap *existing_paths,
347357
const char *path,
348358
const char *branch)
349359
{
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);
351374
}
352375

353376
/*** Function Grouping: functions related to collect_merge_info() ***/

0 commit comments

Comments
 (0)