Skip to content

Commit f4fa8a9

Browse files
committed
Merge branch 'rs/submodule-config-code-cleanup'
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 a58a8e3 + 55cbe18 commit f4fa8a9

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
@@ -371,21 +371,20 @@ static int parse_config(const char *var, const char *value, void *data)
371371
}
372372

373373
static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
374-
unsigned char *gitmodules_sha1)
374+
unsigned char *gitmodules_sha1,
375+
struct strbuf *rev)
375376
{
376-
struct strbuf rev = STRBUF_INIT;
377377
int ret = 0;
378378

379379
if (is_null_sha1(commit_sha1)) {
380380
hashclr(gitmodules_sha1);
381381
return 1;
382382
}
383383

384-
strbuf_addf(&rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
385-
if (get_sha1(rev.buf, gitmodules_sha1) >= 0)
384+
strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
385+
if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
386386
ret = 1;
387387

388-
strbuf_release(&rev);
389388
return ret;
390389
}
391390

@@ -399,7 +398,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
399398
{
400399
struct strbuf rev = STRBUF_INIT;
401400
unsigned long config_size;
402-
char *config;
401+
char *config = NULL;
403402
unsigned char sha1[20];
404403
enum object_type type;
405404
const struct submodule *submodule = NULL;
@@ -420,8 +419,8 @@ static const struct submodule *config_from(struct submodule_cache *cache,
420419
return entry->config;
421420
}
422421

423-
if (!gitmodule_sha1_from_commit(commit_sha1, sha1))
424-
return NULL;
422+
if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev))
423+
goto out;
425424

426425
switch (lookup_type) {
427426
case lookup_name:
@@ -432,16 +431,11 @@ static const struct submodule *config_from(struct submodule_cache *cache,
432431
break;
433432
}
434433
if (submodule)
435-
return submodule;
434+
goto out;
436435

437436
config = read_sha1_file(sha1, &type, &config_size);
438-
if (!config)
439-
return NULL;
440-
441-
if (type != OBJ_BLOB) {
442-
free(config);
443-
return NULL;
444-
}
437+
if (!config || type != OBJ_BLOB)
438+
goto out;
445439

446440
/* fill the submodule config into the cache */
447441
parameter.cache = cache;
@@ -450,6 +444,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
450444
parameter.overwrite = 0;
451445
git_config_from_mem(parse_config, "submodule-blob", rev.buf,
452446
config, config_size, &parameter);
447+
strbuf_release(&rev);
453448
free(config);
454449

455450
switch (lookup_type) {
@@ -460,6 +455,11 @@ static const struct submodule *config_from(struct submodule_cache *cache,
460455
default:
461456
return NULL;
462457
}
458+
459+
out:
460+
strbuf_release(&rev);
461+
free(config);
462+
return submodule;
463463
}
464464

465465
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 cmd_main(int argc, const 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: 'git@somewhere.else.net:a.git' for path 'b'
8798
Submodule url: 'git@somewhere.else.net:submodule.git' for path 'submodule'

0 commit comments

Comments
 (0)