Skip to content

Commit 95d0367

Browse files
committedDec 27, 2016
Merge branch 'sb/submodule-config-cleanup'
Minor code clean-up. * sb/submodule-config-cleanup: submodule-config: clarify parsing of null_sha1 element submodule-config: rename commit_sha1 to treeish_name submodule config: inline config_from_{name, path}
2 parents 6c18dd4 + f2627d9 commit 95d0367

File tree

4 files changed

+48
-42
lines changed

4 files changed

+48
-42
lines changed
 

‎Documentation/technical/api-submodule-config.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,20 @@ Functions
4747
Can be passed to the config parsing infrastructure to parse
4848
local (worktree) submodule configurations.
4949

50-
`const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const char *path)`::
50+
`const struct submodule *submodule_from_path(const unsigned char *treeish_name, const char *path)`::
5151

52-
Lookup values for one submodule by its commit_sha1 and path.
52+
Given a tree-ish in the superproject and a path, return the
53+
submodule that is bound at the path in the named tree.
5354

54-
`const struct submodule *submodule_from_name(const unsigned char *commit_sha1, const char *name)`::
55+
`const struct submodule *submodule_from_name(const unsigned char *treeish_name, const char *name)`::
5556

5657
The same as above but lookup by name.
5758

58-
If given the null_sha1 as commit_sha1 the local configuration of a
59-
submodule will be returned (e.g. consolidated values from local git
59+
Whenever a submodule configuration is parsed in `parse_submodule_config_option`
60+
via e.g. `gitmodules_config()`, it will overwrite the null_sha1 entry.
61+
So in the normal case, when HEAD:.gitmodules is parsed first and then overlayed
62+
with the repository configuration, the null_sha1 entry contains the local
63+
configuration of a submodule (e.g. consolidated values from local git
6064
configuration and the .gitmodules file in the worktree).
6165

6266
For an example usage see test-submodule-config.c.

‎submodule-config.c

+23-35
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,20 @@ int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
263263
return parse_push_recurse(opt, arg, 1);
264264
}
265265

266-
static void warn_multiple_config(const unsigned char *commit_sha1,
266+
static void warn_multiple_config(const unsigned char *treeish_name,
267267
const char *name, const char *option)
268268
{
269269
const char *commit_string = "WORKTREE";
270-
if (commit_sha1)
271-
commit_string = sha1_to_hex(commit_sha1);
270+
if (treeish_name)
271+
commit_string = sha1_to_hex(treeish_name);
272272
warning("%s:.gitmodules, multiple configurations found for "
273273
"'submodule.%s.%s'. Skipping second one!",
274274
commit_string, name, option);
275275
}
276276

277277
struct parse_config_parameter {
278278
struct submodule_cache *cache;
279-
const unsigned char *commit_sha1;
279+
const unsigned char *treeish_name;
280280
const unsigned char *gitmodules_sha1;
281281
int overwrite;
282282
};
@@ -300,7 +300,7 @@ static int parse_config(const char *var, const char *value, void *data)
300300
if (!value)
301301
ret = config_error_nonbool(var);
302302
else if (!me->overwrite && submodule->path)
303-
warn_multiple_config(me->commit_sha1, submodule->name,
303+
warn_multiple_config(me->treeish_name, submodule->name,
304304
"path");
305305
else {
306306
if (submodule->path)
@@ -314,7 +314,7 @@ static int parse_config(const char *var, const char *value, void *data)
314314
int die_on_error = is_null_sha1(me->gitmodules_sha1);
315315
if (!me->overwrite &&
316316
submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
317-
warn_multiple_config(me->commit_sha1, submodule->name,
317+
warn_multiple_config(me->treeish_name, submodule->name,
318318
"fetchrecursesubmodules");
319319
else
320320
submodule->fetch_recurse = parse_fetch_recurse(
@@ -324,7 +324,7 @@ static int parse_config(const char *var, const char *value, void *data)
324324
if (!value)
325325
ret = config_error_nonbool(var);
326326
else if (!me->overwrite && submodule->ignore)
327-
warn_multiple_config(me->commit_sha1, submodule->name,
327+
warn_multiple_config(me->treeish_name, submodule->name,
328328
"ignore");
329329
else if (strcmp(value, "untracked") &&
330330
strcmp(value, "dirty") &&
@@ -340,7 +340,7 @@ static int parse_config(const char *var, const char *value, void *data)
340340
if (!value) {
341341
ret = config_error_nonbool(var);
342342
} else if (!me->overwrite && submodule->url) {
343-
warn_multiple_config(me->commit_sha1, submodule->name,
343+
warn_multiple_config(me->treeish_name, submodule->name,
344344
"url");
345345
} else {
346346
free((void *) submodule->url);
@@ -351,21 +351,21 @@ static int parse_config(const char *var, const char *value, void *data)
351351
ret = config_error_nonbool(var);
352352
else if (!me->overwrite &&
353353
submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
354-
warn_multiple_config(me->commit_sha1, submodule->name,
354+
warn_multiple_config(me->treeish_name, submodule->name,
355355
"update");
356356
else if (parse_submodule_update_strategy(value,
357357
&submodule->update_strategy) < 0)
358358
die(_("invalid value for %s"), var);
359359
} else if (!strcmp(item.buf, "shallow")) {
360360
if (!me->overwrite && submodule->recommend_shallow != -1)
361-
warn_multiple_config(me->commit_sha1, submodule->name,
361+
warn_multiple_config(me->treeish_name, submodule->name,
362362
"shallow");
363363
else
364364
submodule->recommend_shallow =
365365
git_config_bool(var, value);
366366
} else if (!strcmp(item.buf, "branch")) {
367367
if (!me->overwrite && submodule->branch)
368-
warn_multiple_config(me->commit_sha1, submodule->name,
368+
warn_multiple_config(me->treeish_name, submodule->name,
369369
"branch");
370370
else {
371371
free((void *)submodule->branch);
@@ -379,18 +379,18 @@ static int parse_config(const char *var, const char *value, void *data)
379379
return ret;
380380
}
381381

382-
static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
382+
static int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
383383
unsigned char *gitmodules_sha1,
384384
struct strbuf *rev)
385385
{
386386
int ret = 0;
387387

388-
if (is_null_sha1(commit_sha1)) {
388+
if (is_null_sha1(treeish_name)) {
389389
hashclr(gitmodules_sha1);
390390
return 1;
391391
}
392392

393-
strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
393+
strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
394394
if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
395395
ret = 1;
396396

@@ -402,7 +402,7 @@ static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
402402
* revisions.
403403
*/
404404
static const struct submodule *config_from(struct submodule_cache *cache,
405-
const unsigned char *commit_sha1, const char *key,
405+
const unsigned char *treeish_name, const char *key,
406406
enum lookup_type lookup_type)
407407
{
408408
struct strbuf rev = STRBUF_INIT;
@@ -418,7 +418,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
418418
* return the first submodule. Can be used to check whether
419419
* there are any submodules parsed.
420420
*/
421-
if (!commit_sha1 || !key) {
421+
if (!treeish_name || !key) {
422422
struct hashmap_iter iter;
423423
struct submodule_entry *entry;
424424

@@ -428,7 +428,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
428428
return entry->config;
429429
}
430430

431-
if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev))
431+
if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
432432
goto out;
433433

434434
switch (lookup_type) {
@@ -448,7 +448,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
448448

449449
/* fill the submodule config into the cache */
450450
parameter.cache = cache;
451-
parameter.commit_sha1 = commit_sha1;
451+
parameter.treeish_name = treeish_name;
452452
parameter.gitmodules_sha1 = sha1;
453453
parameter.overwrite = 0;
454454
git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
@@ -471,18 +471,6 @@ static const struct submodule *config_from(struct submodule_cache *cache,
471471
return submodule;
472472
}
473473

474-
static const struct submodule *config_from_path(struct submodule_cache *cache,
475-
const unsigned char *commit_sha1, const char *path)
476-
{
477-
return config_from(cache, commit_sha1, path, lookup_path);
478-
}
479-
480-
static const struct submodule *config_from_name(struct submodule_cache *cache,
481-
const unsigned char *commit_sha1, const char *name)
482-
{
483-
return config_from(cache, commit_sha1, name, lookup_name);
484-
}
485-
486474
static void ensure_cache_init(void)
487475
{
488476
if (is_cache_init)
@@ -496,26 +484,26 @@ int parse_submodule_config_option(const char *var, const char *value)
496484
{
497485
struct parse_config_parameter parameter;
498486
parameter.cache = &the_submodule_cache;
499-
parameter.commit_sha1 = NULL;
487+
parameter.treeish_name = NULL;
500488
parameter.gitmodules_sha1 = null_sha1;
501489
parameter.overwrite = 1;
502490

503491
ensure_cache_init();
504492
return parse_config(var, value, &parameter);
505493
}
506494

507-
const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
495+
const struct submodule *submodule_from_name(const unsigned char *treeish_name,
508496
const char *name)
509497
{
510498
ensure_cache_init();
511-
return config_from_name(&the_submodule_cache, commit_sha1, name);
499+
return config_from(&the_submodule_cache, treeish_name, name, lookup_name);
512500
}
513501

514-
const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
502+
const struct submodule *submodule_from_path(const unsigned char *treeish_name,
515503
const char *path)
516504
{
517505
ensure_cache_init();
518-
return config_from_path(&the_submodule_cache, commit_sha1, path);
506+
return config_from(&the_submodule_cache, treeish_name, path, lookup_path);
519507
}
520508

521509
void submodule_free(void)

‎submodule-config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ struct submodule {
2525
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
2626
int parse_push_recurse_submodules_arg(const char *opt, const char *arg);
2727
int parse_submodule_config_option(const char *var, const char *value);
28-
const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
28+
const struct submodule *submodule_from_name(const unsigned char *commit_or_tree,
2929
const char *name);
30-
const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
30+
const struct submodule *submodule_from_path(const unsigned char *commit_or_tree,
3131
const char *path);
3232
void submodule_free(void);
3333

‎t/t7411-submodule-config.sh

+14
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ test_expect_success 'error message contains blob reference' '
9393
)
9494
'
9595

96+
test_expect_success 'using different treeishs works' '
97+
(
98+
cd super &&
99+
git tag new_tag &&
100+
tree=$(git rev-parse HEAD^{tree}) &&
101+
commit=$(git rev-parse HEAD^{commit}) &&
102+
test-submodule-config $commit b >expect &&
103+
test-submodule-config $tree b >actual.1 &&
104+
test-submodule-config new_tag b >actual.2 &&
105+
test_cmp expect actual.1 &&
106+
test_cmp expect actual.2
107+
)
108+
'
109+
96110
cat >super/expect_url <<EOF
97111
Submodule url: 'git@somewhere.else.net:a.git' for path 'b'
98112
Submodule url: 'git@somewhere.else.net:submodule.git' for path 'submodule'

0 commit comments

Comments
 (0)
Please sign in to comment.