Skip to content

Commit 7f420a6

Browse files
To1negitster
authored andcommitted
clone: cut down on global variables in clone.c
In clone.c the `struct option` which is used to parse the input options for git-clone(1) is a global variable. Due to this, many variables that are used to parse the value into, are also global. Make `builtin_clone_options` a local variable in cmd_clone() and carry along all variables that are only used in that function. Signed-off-by: Toon Claes <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 58b5801 commit 7f420a6

File tree

1 file changed

+101
-94
lines changed

1 file changed

+101
-94
lines changed

builtin/clone.c

+101-94
Original file line numberDiff line numberDiff line change
@@ -56,42 +56,22 @@
5656
* - dropping use-separate-remote and no-separate-remote compatibility
5757
*
5858
*/
59-
static const char * const builtin_clone_usage[] = {
60-
N_("git clone [<options>] [--] <repo> [<dir>]"),
61-
NULL
62-
};
6359

6460
static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
6561
static int option_local = -1, option_no_hardlinks, option_shared;
6662
static int option_no_tags;
6763
static int option_shallow_submodules;
68-
static int option_reject_shallow = -1; /* unspecified */
6964
static int config_reject_shallow = -1; /* unspecified */
70-
static int deepen;
71-
static char *option_template, *option_depth, *option_since;
72-
static char *option_origin = NULL;
7365
static char *remote_name = NULL;
7466
static char *option_branch = NULL;
75-
static struct string_list option_not = STRING_LIST_INIT_NODUP;
76-
static const char *real_git_dir;
77-
static const char *ref_format;
78-
static const char *option_upload_pack = "git-upload-pack";
7967
static int option_verbosity;
80-
static int option_progress = -1;
81-
static int option_sparse_checkout;
82-
static enum transport_family family;
83-
static struct string_list option_config = STRING_LIST_INIT_NODUP;
8468
static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
8569
static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
86-
static int option_dissociate;
8770
static int max_jobs = -1;
8871
static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
8972
static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
90-
static int option_filter_submodules = -1; /* unspecified */
9173
static int config_filter_submodules = -1; /* unspecified */
92-
static struct string_list server_options = STRING_LIST_INIT_NODUP;
9374
static int option_remote_submodules;
94-
static const char *bundle_uri;
9575

9676
static int recurse_submodules_cb(const struct option *opt,
9777
const char *arg, int unset)
@@ -107,78 +87,6 @@ static int recurse_submodules_cb(const struct option *opt,
10787
return 0;
10888
}
10989

110-
static struct option builtin_clone_options[] = {
111-
OPT__VERBOSITY(&option_verbosity),
112-
OPT_BOOL(0, "progress", &option_progress,
113-
N_("force progress reporting")),
114-
OPT_BOOL(0, "reject-shallow", &option_reject_shallow,
115-
N_("don't clone shallow repository")),
116-
OPT_BOOL('n', "no-checkout", &option_no_checkout,
117-
N_("don't create a checkout")),
118-
OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
119-
OPT_HIDDEN_BOOL(0, "naked", &option_bare,
120-
N_("create a bare repository")),
121-
OPT_BOOL(0, "mirror", &option_mirror,
122-
N_("create a mirror repository (implies --bare)")),
123-
OPT_BOOL('l', "local", &option_local,
124-
N_("to clone from a local repository")),
125-
OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
126-
N_("don't use local hardlinks, always copy")),
127-
OPT_BOOL('s', "shared", &option_shared,
128-
N_("setup as shared repository")),
129-
{ OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
130-
N_("pathspec"), N_("initialize submodules in the clone"),
131-
PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
132-
OPT_ALIAS(0, "recursive", "recurse-submodules"),
133-
OPT_INTEGER('j', "jobs", &max_jobs,
134-
N_("number of submodules cloned in parallel")),
135-
OPT_STRING(0, "template", &option_template, N_("template-directory"),
136-
N_("directory from which templates will be used")),
137-
OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
138-
N_("reference repository")),
139-
OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
140-
N_("repo"), N_("reference repository")),
141-
OPT_BOOL(0, "dissociate", &option_dissociate,
142-
N_("use --reference only while cloning")),
143-
OPT_STRING('o', "origin", &option_origin, N_("name"),
144-
N_("use <name> instead of 'origin' to track upstream")),
145-
OPT_STRING('b', "branch", &option_branch, N_("branch"),
146-
N_("checkout <branch> instead of the remote's HEAD")),
147-
OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
148-
N_("path to git-upload-pack on the remote")),
149-
OPT_STRING(0, "depth", &option_depth, N_("depth"),
150-
N_("create a shallow clone of that depth")),
151-
OPT_STRING(0, "shallow-since", &option_since, N_("time"),
152-
N_("create a shallow clone since a specific time")),
153-
OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("ref"),
154-
N_("deepen history of shallow clone, excluding ref")),
155-
OPT_BOOL(0, "single-branch", &option_single_branch,
156-
N_("clone only one branch, HEAD or --branch")),
157-
OPT_BOOL(0, "no-tags", &option_no_tags,
158-
N_("don't clone any tags, and make later fetches not to follow them")),
159-
OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
160-
N_("any cloned submodules will be shallow")),
161-
OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
162-
N_("separate git dir from working tree")),
163-
OPT_STRING(0, "ref-format", &ref_format, N_("format"),
164-
N_("specify the reference format to use")),
165-
OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
166-
N_("set config inside the new repository")),
167-
OPT_STRING_LIST(0, "server-option", &server_options,
168-
N_("server-specific"), N_("option to transmit")),
169-
OPT_IPVERSION(&family),
170-
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
171-
OPT_BOOL(0, "also-filter-submodules", &option_filter_submodules,
172-
N_("apply partial clone filters to submodules")),
173-
OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
174-
N_("any cloned submodules will use their remote-tracking branch")),
175-
OPT_BOOL(0, "sparse", &option_sparse_checkout,
176-
N_("initialize sparse-checkout file to include only files at root")),
177-
OPT_STRING(0, "bundle-uri", &bundle_uri,
178-
N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")),
179-
OPT_END()
180-
};
181-
18290
static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
18391
{
18492
static const char *suffix[] = { "/.git", "", ".git/.git", ".git" };
@@ -989,10 +897,103 @@ int cmd_clone(int argc,
989897
int hash_algo;
990898
enum ref_storage_format ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN;
991899
const int do_not_override_repo_unix_permissions = -1;
900+
int option_reject_shallow = -1; /* unspecified */
901+
int deepen = 0;
902+
char *option_template = NULL, *option_depth = NULL, *option_since = NULL;
903+
char *option_origin = NULL;
904+
struct string_list option_not = STRING_LIST_INIT_NODUP;
905+
const char *real_git_dir = NULL;
906+
const char *ref_format = NULL;
907+
const char *option_upload_pack = "git-upload-pack";
908+
int option_progress = -1;
909+
int option_sparse_checkout = 0;
910+
enum transport_family family = TRANSPORT_FAMILY_ALL;
911+
struct string_list option_config = STRING_LIST_INIT_DUP;
912+
int option_dissociate = 0;
913+
int option_filter_submodules = -1; /* unspecified */
914+
struct string_list server_options = STRING_LIST_INIT_NODUP;
915+
const char *bundle_uri = NULL;
992916

993917
struct transport_ls_refs_options transport_ls_refs_options =
994918
TRANSPORT_LS_REFS_OPTIONS_INIT;
995919

920+
struct option builtin_clone_options[] = {
921+
OPT__VERBOSITY(&option_verbosity),
922+
OPT_BOOL(0, "progress", &option_progress,
923+
N_("force progress reporting")),
924+
OPT_BOOL(0, "reject-shallow", &option_reject_shallow,
925+
N_("don't clone shallow repository")),
926+
OPT_BOOL('n', "no-checkout", &option_no_checkout,
927+
N_("don't create a checkout")),
928+
OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
929+
OPT_HIDDEN_BOOL(0, "naked", &option_bare,
930+
N_("create a bare repository")),
931+
OPT_BOOL(0, "mirror", &option_mirror,
932+
N_("create a mirror repository (implies --bare)")),
933+
OPT_BOOL('l', "local", &option_local,
934+
N_("to clone from a local repository")),
935+
OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
936+
N_("don't use local hardlinks, always copy")),
937+
OPT_BOOL('s', "shared", &option_shared,
938+
N_("setup as shared repository")),
939+
{ OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
940+
N_("pathspec"), N_("initialize submodules in the clone"),
941+
PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
942+
OPT_ALIAS(0, "recursive", "recurse-submodules"),
943+
OPT_INTEGER('j', "jobs", &max_jobs,
944+
N_("number of submodules cloned in parallel")),
945+
OPT_STRING(0, "template", &option_template, N_("template-directory"),
946+
N_("directory from which templates will be used")),
947+
OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
948+
N_("reference repository")),
949+
OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
950+
N_("repo"), N_("reference repository")),
951+
OPT_BOOL(0, "dissociate", &option_dissociate,
952+
N_("use --reference only while cloning")),
953+
OPT_STRING('o', "origin", &option_origin, N_("name"),
954+
N_("use <name> instead of 'origin' to track upstream")),
955+
OPT_STRING('b', "branch", &option_branch, N_("branch"),
956+
N_("checkout <branch> instead of the remote's HEAD")),
957+
OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
958+
N_("path to git-upload-pack on the remote")),
959+
OPT_STRING(0, "depth", &option_depth, N_("depth"),
960+
N_("create a shallow clone of that depth")),
961+
OPT_STRING(0, "shallow-since", &option_since, N_("time"),
962+
N_("create a shallow clone since a specific time")),
963+
OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("ref"),
964+
N_("deepen history of shallow clone, excluding ref")),
965+
OPT_BOOL(0, "single-branch", &option_single_branch,
966+
N_("clone only one branch, HEAD or --branch")),
967+
OPT_BOOL(0, "no-tags", &option_no_tags,
968+
N_("don't clone any tags, and make later fetches not to follow them")),
969+
OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
970+
N_("any cloned submodules will be shallow")),
971+
OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
972+
N_("separate git dir from working tree")),
973+
OPT_STRING(0, "ref-format", &ref_format, N_("format"),
974+
N_("specify the reference format to use")),
975+
OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
976+
N_("set config inside the new repository")),
977+
OPT_STRING_LIST(0, "server-option", &server_options,
978+
N_("server-specific"), N_("option to transmit")),
979+
OPT_IPVERSION(&family),
980+
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
981+
OPT_BOOL(0, "also-filter-submodules", &option_filter_submodules,
982+
N_("apply partial clone filters to submodules")),
983+
OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
984+
N_("any cloned submodules will use their remote-tracking branch")),
985+
OPT_BOOL(0, "sparse", &option_sparse_checkout,
986+
N_("initialize sparse-checkout file to include only files at root")),
987+
OPT_STRING(0, "bundle-uri", &bundle_uri,
988+
N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")),
989+
OPT_END()
990+
};
991+
992+
const char * const builtin_clone_usage[] = {
993+
N_("git clone [<options>] [--] <repo> [<dir>]"),
994+
NULL
995+
};
996+
996997
packet_trace_identity("clone");
997998

998999
git_config(git_clone_config, NULL);
@@ -1138,8 +1139,8 @@ int cmd_clone(int argc,
11381139
for_each_string_list_item(item, &option_recurse_submodules) {
11391140
strbuf_addf(&sb, "submodule.active=%s",
11401141
item->string);
1141-
string_list_append(&option_config,
1142-
strbuf_detach(&sb, NULL));
1142+
string_list_append(&option_config, sb.buf);
1143+
strbuf_reset(&sb);
11431144
}
11441145

11451146
if (!git_config_get_bool("submodule.stickyRecursiveClone", &val) &&
@@ -1161,6 +1162,8 @@ int cmd_clone(int argc,
11611162
string_list_append(&option_config,
11621163
"submodule.alternateErrorStrategy=info");
11631164
}
1165+
1166+
strbuf_release(&sb);
11641167
}
11651168

11661169
/*
@@ -1578,6 +1581,10 @@ int cmd_clone(int argc,
15781581
err = checkout(submodule_progress, filter_submodules,
15791582
ref_storage_format);
15801583

1584+
string_list_clear(&option_not, 0);
1585+
string_list_clear(&option_config, 0);
1586+
string_list_clear(&server_options, 0);
1587+
15811588
free(remote_name);
15821589
strbuf_release(&reflog_msg);
15831590
strbuf_release(&branch_top);

0 commit comments

Comments
 (0)