Skip to content

Commit f7b01d3

Browse files
committed
Merge branch 'rs/submodule-config-code-cleanup' into maint
Code cleanup. * rs/submodule-config-code-cleanup: submodule-config: fix test binary crashing when no arguments given submodule-config: combine early return code into one goto submodule-config: passing name reference for .gitmodule blobs submodule-config: use explicit empty string instead of strbuf in config_from()
2 parents 6a024a2 + 55cbe18 commit f7b01d3

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

submodule-config.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -362,21 +362,20 @@ static int parse_config(const char *var, const char *value, void *data)
362362
}
363363

364364
static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
365-
unsigned char *gitmodules_sha1)
365+
unsigned char *gitmodules_sha1,
366+
struct strbuf *rev)
366367
{
367-
struct strbuf rev = STRBUF_INIT;
368368
int ret = 0;
369369

370370
if (is_null_sha1(commit_sha1)) {
371371
hashcpy(gitmodules_sha1, null_sha1);
372372
return 1;
373373
}
374374

375-
strbuf_addf(&rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
376-
if (get_sha1(rev.buf, gitmodules_sha1) >= 0)
375+
strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
376+
if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
377377
ret = 1;
378378

379-
strbuf_release(&rev);
380379
return ret;
381380
}
382381

@@ -390,7 +389,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
390389
{
391390
struct strbuf rev = STRBUF_INIT;
392391
unsigned long config_size;
393-
char *config;
392+
char *config = NULL;
394393
unsigned char sha1[20];
395394
enum object_type type;
396395
const struct submodule *submodule = NULL;
@@ -411,8 +410,8 @@ static const struct submodule *config_from(struct submodule_cache *cache,
411410
return entry->config;
412411
}
413412

414-
if (!gitmodule_sha1_from_commit(commit_sha1, sha1))
415-
return NULL;
413+
if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev))
414+
goto out;
416415

417416
switch (lookup_type) {
418417
case lookup_name:
@@ -423,16 +422,11 @@ static const struct submodule *config_from(struct submodule_cache *cache,
423422
break;
424423
}
425424
if (submodule)
426-
return submodule;
425+
goto out;
427426

428427
config = read_sha1_file(sha1, &type, &config_size);
429-
if (!config)
430-
return NULL;
431-
432-
if (type != OBJ_BLOB) {
433-
free(config);
434-
return NULL;
435-
}
428+
if (!config || type != OBJ_BLOB)
429+
goto out;
436430

437431
/* fill the submodule config into the cache */
438432
parameter.cache = cache;
@@ -441,6 +435,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
441435
parameter.overwrite = 0;
442436
git_config_from_mem(parse_config, "submodule-blob", rev.buf,
443437
config, config_size, &parameter);
438+
strbuf_release(&rev);
444439
free(config);
445440

446441
switch (lookup_type) {
@@ -451,6 +446,11 @@ static const struct submodule *config_from(struct submodule_cache *cache,
451446
default:
452447
return NULL;
453448
}
449+
450+
out:
451+
strbuf_release(&rev);
452+
free(config);
453+
return submodule;
454454
}
455455

456456
static const struct submodule *config_from_path(struct submodule_cache *cache,

t/helper/test-submodule-config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int main(int argc, char **argv)
2323

2424
arg++;
2525
my_argc--;
26-
while (starts_with(arg[0], "--")) {
26+
while (arg[0] && starts_with(arg[0], "--")) {
2727
if (!strcmp(arg[0], "--url"))
2828
output_url = 1;
2929
if (!strcmp(arg[0], "--name"))

t/t7411-submodule-config.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ test_expect_success 'error in one submodule config lets continue' '
8282
)
8383
'
8484

85+
test_expect_success 'error message contains blob reference' '
86+
(cd super &&
87+
sha1=$(git rev-parse HEAD) &&
88+
test-submodule-config \
89+
HEAD b \
90+
HEAD submodule \
91+
2>actual_err &&
92+
grep "submodule-blob $sha1:.gitmodules" actual_err >/dev/null
93+
)
94+
'
95+
8596
cat >super/expect_url <<EOF
8697
Submodule url: '[email protected]:a.git' for path 'b'
8798
Submodule url: '[email protected]:submodule.git' for path 'submodule'

0 commit comments

Comments
 (0)