Skip to content

Commit 5733f89

Browse files
thaliaarchigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0df86b6 commit 5733f89

File tree

1 file changed

+27
-37
lines changed

1 file changed

+27
-37
lines changed

builtin/fast-import.c

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,7 @@ static void parse_path_space(struct strbuf *sb, const char *p,
23112311

23122312
static void file_change_m(const char *p, struct branch *b)
23132313
{
2314-
static struct strbuf uq = STRBUF_INIT;
2314+
static struct strbuf path = STRBUF_INIT;
23152315
struct object_entry *oe;
23162316
struct object_id oid;
23172317
uint16_t mode, inline_data = 0;
@@ -2348,13 +2348,12 @@ static void file_change_m(const char *p, struct branch *b)
23482348
die("Missing space after SHA1: %s", command_buf.buf);
23492349
}
23502350

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");
23542353

23552354
/* 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);
23582357
return;
23592358
}
23602359

@@ -2375,10 +2374,6 @@ static void file_change_m(const char *p, struct branch *b)
23752374
if (S_ISDIR(mode))
23762375
die("Directories cannot be specified 'inline': %s",
23772376
command_buf.buf);
2378-
if (p != uq.buf) {
2379-
strbuf_addstr(&uq, p);
2380-
p = uq.buf;
2381-
}
23822377
while (read_next_command() != EOF) {
23832378
const char *v;
23842379
if (skip_prefix(command_buf.buf, "cat-blob ", &v))
@@ -2404,55 +2399,51 @@ static void file_change_m(const char *p, struct branch *b)
24042399
command_buf.buf);
24052400
}
24062401

2407-
if (!*p) {
2402+
if (!*path.buf) {
24082403
tree_content_replace(&b->branch_tree, &oid, mode, NULL);
24092404
return;
24102405
}
2411-
tree_content_set(&b->branch_tree, p, &oid, mode, NULL);
2406+
tree_content_set(&b->branch_tree, path.buf, &oid, mode, NULL);
24122407
}
24132408

24142409
static void file_change_d(const char *p, struct branch *b)
24152410
{
2416-
static struct strbuf uq = STRBUF_INIT;
2411+
static struct strbuf path = STRBUF_INIT;
24172412

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);
24222416
}
24232417

24242418
static void file_change_cr(const char *p, struct branch *b, int rename)
24252419
{
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;
24292422
struct tree_entry leaf;
24302423

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");
24342426

24352427
if (!*p)
24362428
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");
24402431

24412432
memset(&leaf, 0, sizeof(leaf));
24422433
if (rename)
2443-
tree_content_remove(&b->branch_tree, s, &leaf, 1);
2434+
tree_content_remove(&b->branch_tree, source.buf, &leaf, 1);
24442435
else
2445-
tree_content_get(&b->branch_tree, s, &leaf, 1);
2436+
tree_content_get(&b->branch_tree, source.buf, &leaf, 1);
24462437
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" "" */
24492440
tree_content_replace(&b->branch_tree,
24502441
&leaf.versions[1].oid,
24512442
leaf.versions[1].mode,
24522443
leaf.tree);
24532444
return;
24542445
}
2455-
tree_content_set(&b->branch_tree, d,
2446+
tree_content_set(&b->branch_tree, dest.buf,
24562447
&leaf.versions[1].oid,
24572448
leaf.versions[1].mode,
24582449
leaf.tree);
@@ -3180,7 +3171,7 @@ static void print_ls(int mode, const unsigned char *hash, const char *path)
31803171

31813172
static void parse_ls(const char *p, struct branch *b)
31823173
{
3183-
static struct strbuf uq = STRBUF_INIT;
3174+
static struct strbuf path = STRBUF_INIT;
31843175
struct tree_entry *root = NULL;
31853176
struct tree_entry leaf = {NULL};
31863177

@@ -3197,18 +3188,17 @@ static void parse_ls(const char *p, struct branch *b)
31973188
root->versions[1].mode = S_IFDIR;
31983189
load_tree(root);
31993190
}
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);
32043194
/*
32053195
* A directory in preparation would have a sha1 of zero
32063196
* until it is saved. Save, for simplicity.
32073197
*/
32083198
if (S_ISDIR(leaf.versions[1].mode))
32093199
store_tree(&leaf);
32103200

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);
32123202
if (leaf.tree)
32133203
release_tree_content_recursive(leaf.tree);
32143204
if (!b || root != &b->branch_tree)

0 commit comments

Comments
 (0)