fast-import: directly use strbufs for paths

Previously, one case would not write the path to the strbuf: when the path is unquoted and at the end of the string. It was essentially copy-on-write. However, with the logic simplification of the previous commit, this case was eliminated and the strbuf is always populated. Directly use the strbufs now instead of an alias. Since this already changes all the lines that use the strbufs, rename them from `uq` to be more descriptive. That they are unquoted is not their most important property, so name them after what they carry. Additionally, `file_change_m` no longer needs to copy the path before reading inline data. Signed-off-by: Thalia Archibald <thalia@archibald.dev> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thalia Archibald committed Apr 14, 2024 at 01:11 UTC 5733f894d7e66d0f0fbd29675d8420825ce0c29f
1 file changed +27 -37
builtin/fast-import.c
+27 -37
@@ -2311,7 +2311,7 @@ static void parse_path_space(struct strbuf *sb, const char *p,
2311
2312 static void file_change_m(const char *p, struct branch *b)
2313 {
2314 - static struct strbuf uq = STRBUF_INIT;
2314 + static struct strbuf path = STRBUF_INIT;
2315 struct object_entry *oe;
2316 struct object_id oid;
2317 uint16_t mode, inline_data = 0;
@@ -2348,13 +2348,12 @@ static void file_change_m(const char *p, struct branch *b)
2348 die("Missing space after SHA1: %s", command_buf.buf);
2349 }
2350
2351 - strbuf_reset(&uq);
2352 - parse_path_eol(&uq, p, "path");
2353 - p = uq.buf;
2351 + strbuf_reset(&path);
2352 + parse_path_eol(&path, p, "path");
2353
2354 /* Git does not track empty, non-toplevel directories. */
2356 - if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *p) {
2357 - tree_content_remove(&b->branch_tree, p, NULL, 0);
2355 + if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *path.buf) {
2356 + tree_content_remove(&b->branch_tree, path.buf, NULL, 0);
2357 return;
2358 }
2359
@@ -2375,10 +2374,6 @@ static void file_change_m(const char *p, struct branch *b)
2374 if (S_ISDIR(mode))
2375 die("Directories cannot be specified 'inline': %s",
2376 command_buf.buf);
2378 - if (p != uq.buf) {
2379 - strbuf_addstr(&uq, p);
2380 - p = uq.buf;
2381 - }
2377 while (read_next_command() != EOF) {
2378 const char *v;
2379 if (skip_prefix(command_buf.buf, "cat-blob ", &v))
@@ -2404,55 +2399,51 @@ static void file_change_m(const char *p, struct branch *b)
2399 command_buf.buf);
2400 }
2401
2407 - if (!*p) {
2402 + if (!*path.buf) {
2403 tree_content_replace(&b->branch_tree, &oid, mode, NULL);
2404 return;
2405 }
2411 - tree_content_set(&b->branch_tree, p, &oid, mode, NULL);
2406 + tree_content_set(&b->branch_tree, path.buf, &oid, mode, NULL);
2407 }
2408
2409 static void file_change_d(const char *p, struct branch *b)
2410 {
2416 - static struct strbuf uq = STRBUF_INIT;
2411 + static struct strbuf path = STRBUF_INIT;
2412
2418 - strbuf_reset(&uq);
2419 - parse_path_eol(&uq, p, "path");
2420 - p = uq.buf;
2421 - tree_content_remove(&b->branch_tree, p, NULL, 1);
2413 + strbuf_reset(&path);
2414 + parse_path_eol(&path, p, "path");
2415 + tree_content_remove(&b->branch_tree, path.buf, NULL, 1);
2416 }
2417
2418 static void file_change_cr(const char *p, struct branch *b, int rename)
2419 {
2426 - const char *s, *d;
2427 - static struct strbuf s_uq = STRBUF_INIT;
2428 - static struct strbuf d_uq = STRBUF_INIT;
2420 + static struct strbuf source = STRBUF_INIT;
2421 + static struct strbuf dest = STRBUF_INIT;
2422 struct tree_entry leaf;
2423
2431 - strbuf_reset(&s_uq);
2432 - parse_path_space(&s_uq, p, &p, "source");
2433 - s = s_uq.buf;
2424 + strbuf_reset(&source);
2425 + parse_path_space(&source, p, &p, "source");
2426
2427 if (!*p)
2428 die("Missing dest: %s", command_buf.buf);
2437 - strbuf_reset(&d_uq);
2438 - parse_path_eol(&d_uq, p, "dest");
2439 - d = d_uq.buf;
2429 + strbuf_reset(&dest);
2430 + parse_path_eol(&dest, p, "dest");
2431
2432 memset(&leaf, 0, sizeof(leaf));
2433 if (rename)
2443 - tree_content_remove(&b->branch_tree, s, &leaf, 1);
2434 + tree_content_remove(&b->branch_tree, source.buf, &leaf, 1);
2435 else
2445 - tree_content_get(&b->branch_tree, s, &leaf, 1);
2436 + tree_content_get(&b->branch_tree, source.buf, &leaf, 1);
2437 if (!leaf.versions[1].mode)
2447 - die("Path %s not in branch", s);
2448 - if (!*d) { /* C "path/to/subdir" "" */
2438 + die("Path %s not in branch", source.buf);
2439 + if (!*dest.buf) { /* C "path/to/subdir" "" */
2440 tree_content_replace(&b->branch_tree,
2441 &leaf.versions[1].oid,
2442 leaf.versions[1].mode,
2443 leaf.tree);
2444 return;
2445 }
2455 - tree_content_set(&b->branch_tree, d,
2446 + tree_content_set(&b->branch_tree, dest.buf,
2447 &leaf.versions[1].oid,
2448 leaf.versions[1].mode,
2449 leaf.tree);
@@ -3180,7 +3171,7 @@ static void print_ls(int mode, const unsigned char *hash, const char *path)
3171
3172 static void parse_ls(const char *p, struct branch *b)
3173 {
3183 - static struct strbuf uq = STRBUF_INIT;
3174 + static struct strbuf path = STRBUF_INIT;
3175 struct tree_entry *root = NULL;
3176 struct tree_entry leaf = {NULL};
3177
@@ -3197,10 +3188,9 @@ static void parse_ls(const char *p, struct branch *b)
3188 root->versions[1].mode = S_IFDIR;
3189 load_tree(root);
3190 }
3200 - strbuf_reset(&uq);
3201 - parse_path_eol(&uq, p, "path");
3202 - p = uq.buf;
3203 - tree_content_get(root, p, &leaf, 1);
3191 + strbuf_reset(&path);
3192 + parse_path_eol(&path, p, "path");
3193 + tree_content_get(root, path.buf, &leaf, 1);
3194 /*
3195 * A directory in preparation would have a sha1 of zero
3196 * until it is saved. Save, for simplicity.
@@ -3208,7 +3198,7 @@ static void parse_ls(const char *p, struct branch *b)
3198 if (S_ISDIR(leaf.versions[1].mode))
3199 store_tree(&leaf);
3200
3211 - print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, p);
3201 + print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, path.buf);
3202 if (leaf.tree)
3203 release_tree_content_recursive(leaf.tree);
3204 if (!b || root != &b->branch_tree)