Skip to content

Commit c369fc4

Browse files
pks-tgitster
authored andcommitted
builtin/submodule: allow "add" to use different ref storage format
Same as with "clone", users may want to add a submodule to a repository with a non-default ref storage format. Wire up a new `--ref-format=` option that works the same as for `git submodule clone`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb99dde commit c369fc4

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

Documentation/git-submodule.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ COMMANDS
3434
With no arguments, shows the status of existing submodules. Several
3535
subcommands are available to perform operations on the submodules.
3636

37-
add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--depth <depth>] [--] <repository> [<path>]::
37+
add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--ref-format <format>] [--depth <depth>] [--] <repository> [<path>]::
3838
Add the given repository as a submodule at the given path
3939
to the changeset to be committed next to the current
4040
project: the current project is termed the "superproject".
@@ -71,6 +71,9 @@ submodule repositories will be kept together in the same relative
7171
location, and only the superproject's URL needs to be provided.
7272
git-submodule will correctly locate the submodule using the relative
7373
URL in `.gitmodules`.
74+
+
75+
If `--ref-format <format>` is specified, the ref storage format of newly
76+
cloned submodules will be set accordingly.
7477

7578
status [--cached] [--recursive] [--] [<path>...]::
7679
Show the status of the submodules. This will print the SHA-1 of the

builtin/submodule--helper.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3128,13 +3128,17 @@ struct add_data {
31283128
const char *sm_name;
31293129
const char *repo;
31303130
const char *realrepo;
3131+
enum ref_storage_format ref_storage_format;
31313132
int depth;
31323133
unsigned int force: 1;
31333134
unsigned int quiet: 1;
31343135
unsigned int progress: 1;
31353136
unsigned int dissociate: 1;
31363137
};
3137-
#define ADD_DATA_INIT { .depth = -1 }
3138+
#define ADD_DATA_INIT { \
3139+
.depth = -1, \
3140+
.ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
3141+
}
31383142

31393143
static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
31403144
{
@@ -3228,6 +3232,7 @@ static int add_submodule(const struct add_data *add_data)
32283232

32293233
string_list_append(&reference, p)->util = p;
32303234
}
3235+
clone_data.ref_storage_format = add_data->ref_storage_format;
32313236
clone_data.dissociate = add_data->dissociate;
32323237
if (add_data->depth >= 0)
32333238
clone_data.depth = xstrfmt("%d", add_data->depth);
@@ -3392,6 +3397,7 @@ static int module_add(int argc, const char **argv, const char *prefix)
33923397
{
33933398
int force = 0, quiet = 0, progress = 0, dissociate = 0;
33943399
struct add_data add_data = ADD_DATA_INIT;
3400+
const char *ref_storage_format = NULL;
33953401
char *to_free = NULL;
33963402
struct option options[] = {
33973403
OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
@@ -3402,6 +3408,8 @@ static int module_add(int argc, const char **argv, const char *prefix)
34023408
OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
34033409
OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
34043410
N_("reference repository")),
3411+
OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
3412+
N_("specify the reference format to use")),
34053413
OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
34063414
OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
34073415
N_("sets the submodule's name to the given string "
@@ -3428,6 +3436,12 @@ static int module_add(int argc, const char **argv, const char *prefix)
34283436
if (argc == 0 || argc > 2)
34293437
usage_with_options(usage, options);
34303438

3439+
if (ref_storage_format) {
3440+
add_data.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
3441+
if (add_data.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
3442+
die(_("unknown ref storage format '%s'"), ref_storage_format);
3443+
}
3444+
34313445
add_data.repo = argv[0];
34323446
if (argc == 1)
34333447
add_data.sm_path = git_url_basename(add_data.repo, 0, 0);

git-submodule.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ cmd_add()
9494
--reference=*)
9595
reference_path="${1#--reference=}"
9696
;;
97+
--ref-format)
98+
case "$2" in '') usage ;; esac
99+
ref_format="--ref-format=$2"
100+
shift
101+
;;
102+
--ref-format=*)
103+
ref_format="$1"
104+
;;
97105
--dissociate)
98106
dissociate=1
99107
;;
@@ -135,6 +143,7 @@ cmd_add()
135143
${progress:+"--progress"} \
136144
${branch:+--branch "$branch"} \
137145
${reference_path:+--reference "$reference_path"} \
146+
${ref_format:+"$ref_format"} \
138147
${dissociate:+--dissociate} \
139148
${custom_name:+--name "$custom_name"} \
140149
${depth:+"$depth"} \

t/t7424-submodule-mixed-ref-formats.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ test_expect_success 'add existing repository with different ref storage format'
3737
)
3838
'
3939

40+
test_expect_success 'add submodules with different ref storage format' '
41+
test_when_finished "rm -rf submodule upstream" &&
42+
43+
git init submodule &&
44+
test_commit -C submodule submodule-initial &&
45+
git init upstream &&
46+
test_ref_format upstream "$GIT_DEFAULT_REF_FORMAT" &&
47+
git -C upstream submodule add --ref-format="$OTHER_FORMAT" "file://$(pwd)/submodule" &&
48+
test_ref_format upstream/submodule "$OTHER_FORMAT"
49+
'
50+
4051
test_expect_success 'recursive clone propagates ref storage format' '
4152
test_when_finished "rm -rf submodule upstream downstream" &&
4253

0 commit comments

Comments
 (0)