Skip to content

Commit 4002ec3

Browse files
avargitster
authored andcommitted
read-tree: add "--super-prefix" option, eliminate global
The "--super-prefix" option to "git" was initially added in [1] for use with "ls-files"[2], and shortly thereafter "submodule--helper"[3] and "grep"[4]. It wasn't until [5] that "read-tree" made use of it. At the time [5] made sense, but since then we've made "ls-files" recurse in-process in [6], "grep" in [7], and finally "submodule--helper" in the preceding commits. Let's also remove it from "read-tree", which allows us to remove the option to "git" itself. We can do this because the only remaining user of it is the submodule API, which will now invoke "read-tree" with its new "--super-prefix" option. It will only do so when the "submodule_move_head()" function is called. That "submodule_move_head()" function was then only invoked by "read-tree" itself, but now rather than setting an environment variable to pass "--super-prefix" between cmd_read_tree() we: - Set a new "super_prefix" in "struct unpack_trees_options". The "super_prefixed()" function in "unpack-trees.c" added in [5] will now use this, rather than get_super_prefix() looking up the environment variable we set earlier in the same process. - Add the same field to the "struct checkout", which is only needed to ferry the "super_prefix" in the "struct unpack_trees_options" all the way down to the "entry.c" callers of "submodule_move_head()". Those calls which used the super prefix all originated in "cmd_read_tree()". The only other caller is the "unlink_entry()" caller in "builtin/checkout.c", which now passes a "NULL". 1. 74866d7 (git: make super-prefix option, 2016-10-07) 2. e77aa33 (ls-files: optionally recurse into submodules, 2016-10-07) 3. 89c8626 (submodule helper: support super prefix, 2016-12-08) 4. 0281e48 (grep: optionally recurse into submodules, 2016-12-16) 5. 3d41542 (unpack-trees: support super-prefix option, 2017-01-17) 6. 188dce1 (ls-files: use repository object, 2017-06-22) 7. f9ee2fc (grep: recurse in-process using 'struct repository', 2017-08-02) Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f5a6be9 commit 4002ec3

15 files changed

+48
-100
lines changed

Documentation/git.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ SYNOPSIS
1313
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
1414
[-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
1515
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
16-
[--super-prefix=<path>] [--config-env=<name>=<envvar>]
17-
<command> [<args>]
16+
[--config-env=<name>=<envvar>] <command> [<args>]
1817

1918
DESCRIPTION
2019
-----------
@@ -169,11 +168,6 @@ If you just want to run git as if it was started in `<path>` then use
169168
details. Equivalent to setting the `GIT_NAMESPACE` environment
170169
variable.
171170

172-
--super-prefix=<path>::
173-
Currently for internal use only. Set a prefix which gives a path from
174-
above a repository down to its root. One use is to give submodules
175-
context about the superproject that invoked it.
176-
177171
--bare::
178172
Treat the repository as a bare repository. If GIT_DIR
179173
environment is not set, it is set to the current working

builtin.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@
5151
* on bare repositories.
5252
* This only makes sense when `RUN_SETUP` is also set.
5353
*
54-
* `SUPPORT_SUPER_PREFIX`:
55-
*
56-
* The built-in supports `--super-prefix`.
57-
*
5854
* `DELAY_PAGER_CONFIG`:
5955
*
6056
* If RUN_SETUP or RUN_SETUP_GENTLY is set, git.c normally handles

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
232232
pos++;
233233
}
234234
if (!overlay_mode) {
235-
unlink_entry(ce);
235+
unlink_entry(ce, NULL);
236236
return 0;
237237
}
238238
if (stage == 2)

builtin/read-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
114114
int prefix_set = 0;
115115
struct lock_file lock_file = LOCK_INIT;
116116
const struct option read_tree_options[] = {
117+
OPT__SUPER_PREFIX(&opts.super_prefix),
117118
OPT_CALLBACK_F(0, "index-output", NULL, N_("file"),
118119
N_("write resulting index to <file>"),
119120
PARSE_OPT_NONEG, index_output_cb),

cache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ static inline enum object_type object_type(unsigned int mode)
480480
#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
481481
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
482482
#define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
483-
#define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
484483
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
485484
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
486485
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@ -566,7 +565,6 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
566565
int get_common_dir(struct strbuf *sb, const char *gitdir);
567566
const char *get_git_namespace(void);
568567
const char *strip_namespace(const char *namespaced_ref);
569-
const char *get_super_prefix(void);
570568
const char *get_git_work_tree(void);
571569

572570
/*

entry.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
383383
return error("cannot create submodule directory %s", path);
384384
sub = submodule_from_ce(ce);
385385
if (sub)
386-
return submodule_move_head(ce->name,
386+
return submodule_move_head(ce->name, state->super_prefix,
387387
NULL, oid_to_hex(&ce->oid),
388388
state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
389389
break;
@@ -476,7 +476,7 @@ int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
476476
* no pathname to return.
477477
*/
478478
BUG("Can't remove entry to a path");
479-
unlink_entry(ce);
479+
unlink_entry(ce, state->super_prefix);
480480
return 0;
481481
}
482482

@@ -510,10 +510,10 @@ int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
510510
if (!(st.st_mode & S_IFDIR))
511511
unlink_or_warn(ce->name);
512512

513-
return submodule_move_head(ce->name,
513+
return submodule_move_head(ce->name, state->super_prefix,
514514
NULL, oid_to_hex(&ce->oid), 0);
515515
} else
516-
return submodule_move_head(ce->name,
516+
return submodule_move_head(ce->name, state->super_prefix,
517517
"HEAD", oid_to_hex(&ce->oid),
518518
state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
519519
}
@@ -560,12 +560,12 @@ int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
560560
return write_entry(ce, path.buf, ca, state, 0, nr_checkouts);
561561
}
562562

563-
void unlink_entry(const struct cache_entry *ce)
563+
void unlink_entry(const struct cache_entry *ce, const char *super_prefix)
564564
{
565565
const struct submodule *sub = submodule_from_ce(ce);
566566
if (sub) {
567567
/* state.force is set at the caller. */
568-
submodule_move_head(ce->name, "HEAD", NULL,
568+
submodule_move_head(ce->name, super_prefix, "HEAD", NULL,
569569
SUBMODULE_MOVE_HEAD_FORCE);
570570
}
571571
if (check_leading_path(ce->name, ce_namelen(ce), 1) >= 0)

entry.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct checkout {
88
struct index_state *istate;
99
const char *base_dir;
1010
int base_dir_len;
11+
const char *super_prefix;
1112
struct delayed_checkout *delayed_checkout;
1213
struct checkout_metadata meta;
1314
unsigned force:1,
@@ -48,8 +49,11 @@ int finish_delayed_checkout(struct checkout *state, int show_progress);
4849
/*
4950
* Unlink the last component and schedule the leading directories for
5051
* removal, such that empty directories get removed.
52+
*
53+
* The "super_prefix" is either NULL, or the "--super-prefix" passed
54+
* down from "read-tree" et al.
5155
*/
52-
void unlink_entry(const struct cache_entry *ce);
56+
void unlink_entry(const struct cache_entry *ce, const char *super_prefix);
5357

5458
void *read_blob_entry(const struct cache_entry *ce, size_t *size);
5559
int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st);

environment.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ char *git_work_tree_cfg;
102102

103103
static char *git_namespace;
104104

105-
static char *super_prefix;
106-
107105
/*
108106
* Repository-local GIT_* environment variables; see cache.h for details.
109107
*/
@@ -121,7 +119,6 @@ const char * const local_repo_env[] = {
121119
NO_REPLACE_OBJECTS_ENVIRONMENT,
122120
GIT_REPLACE_REF_BASE_ENVIRONMENT,
123121
GIT_PREFIX_ENVIRONMENT,
124-
GIT_SUPER_PREFIX_ENVIRONMENT,
125122
GIT_SHALLOW_FILE_ENVIRONMENT,
126123
GIT_COMMON_DIR_ENVIRONMENT,
127124
NULL
@@ -234,16 +231,6 @@ const char *strip_namespace(const char *namespaced_ref)
234231
return NULL;
235232
}
236233

237-
const char *get_super_prefix(void)
238-
{
239-
static int initialized;
240-
if (!initialized) {
241-
super_prefix = xstrdup_or_null(getenv(GIT_SUPER_PREFIX_ENVIRONMENT));
242-
initialized = 1;
243-
}
244-
return super_prefix;
245-
}
246-
247234
static int git_work_tree_initialized;
248235

249236
/*

git.c

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* RUN_SETUP for reading from the configuration file.
1515
*/
1616
#define NEED_WORK_TREE (1<<3)
17-
#define SUPPORT_SUPER_PREFIX (1<<4)
18-
#define DELAY_PAGER_CONFIG (1<<5)
19-
#define NO_PARSEOPT (1<<6) /* parse-options is not used */
17+
#define DELAY_PAGER_CONFIG (1<<4)
18+
#define NO_PARSEOPT (1<<5) /* parse-options is not used */
2019

2120
struct cmd_struct {
2221
const char *cmd;
@@ -29,8 +28,7 @@ const char git_usage_string[] =
2928
" [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
3029
" [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
3130
" [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
32-
" [--super-prefix=<path>] [--config-env=<name>=<envvar>]\n"
33-
" <command> [<args>]");
31+
" [--config-env=<name>=<envvar>] <command> [<args>]");
3432

3533
const char git_more_info_string[] =
3634
N_("'git help -a' and 'git help -g' list available subcommands and some\n"
@@ -226,20 +224,6 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
226224
setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
227225
if (envchanged)
228226
*envchanged = 1;
229-
} else if (!strcmp(cmd, "--super-prefix")) {
230-
if (*argc < 2) {
231-
fprintf(stderr, _("no prefix given for --super-prefix\n" ));
232-
usage(git_usage_string);
233-
}
234-
setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1);
235-
if (envchanged)
236-
*envchanged = 1;
237-
(*argv)++;
238-
(*argc)--;
239-
} else if (skip_prefix(cmd, "--super-prefix=", &cmd)) {
240-
setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1);
241-
if (envchanged)
242-
*envchanged = 1;
243227
} else if (!strcmp(cmd, "--bare")) {
244228
char *cwd = xgetcwd();
245229
is_bare_repository_cfg = 1;
@@ -449,11 +433,6 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
449433
trace_repo_setup(prefix);
450434
commit_pager_choice();
451435

452-
if (!help && get_super_prefix()) {
453-
if (!(p->option & SUPPORT_SUPER_PREFIX))
454-
die(_("%s doesn't support --super-prefix"), p->cmd);
455-
}
456-
457436
if (!help && p->option & NEED_WORK_TREE)
458437
setup_work_tree();
459438

@@ -504,7 +483,7 @@ static struct cmd_struct commands[] = {
504483
{ "check-ref-format", cmd_check_ref_format, NO_PARSEOPT },
505484
{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
506485
{ "checkout--worker", cmd_checkout__worker,
507-
RUN_SETUP | NEED_WORK_TREE | SUPPORT_SUPER_PREFIX },
486+
RUN_SETUP | NEED_WORK_TREE },
508487
{ "checkout-index", cmd_checkout_index,
509488
RUN_SETUP | NEED_WORK_TREE},
510489
{ "cherry", cmd_cherry, RUN_SETUP },
@@ -583,7 +562,7 @@ static struct cmd_struct commands[] = {
583562
{ "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE },
584563
{ "push", cmd_push, RUN_SETUP },
585564
{ "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER },
586-
{ "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX},
565+
{ "read-tree", cmd_read_tree, RUN_SETUP },
587566
{ "rebase", cmd_rebase, RUN_SETUP | NEED_WORK_TREE },
588567
{ "receive-pack", cmd_receive_pack },
589568
{ "reflog", cmd_reflog, RUN_SETUP },
@@ -727,9 +706,6 @@ static void execv_dashed_external(const char **argv)
727706
struct child_process cmd = CHILD_PROCESS_INIT;
728707
int status;
729708

730-
if (get_super_prefix())
731-
die(_("%s doesn't support --super-prefix"), argv[0]);
732-
733709
if (use_pager == -1 && !is_builtin(argv[0]))
734710
use_pager = check_pager_config(argv[0]);
735711
commit_pager_choice();
@@ -799,9 +775,6 @@ static int run_argv(int *argcp, const char ***argv)
799775
*/
800776
trace2_cmd_name("_run_git_alias_");
801777

802-
if (get_super_prefix())
803-
die("%s doesn't support --super-prefix", **argv);
804-
805778
commit_pager_choice();
806779

807780
strvec_push(&cmd.args, "git");

submodule.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,14 +2054,6 @@ void submodule_unset_core_worktree(const struct submodule *sub)
20542054
strbuf_release(&config_path);
20552055
}
20562056

2057-
static const char *get_super_prefix_or_empty(void)
2058-
{
2059-
const char *s = get_super_prefix();
2060-
if (!s)
2061-
s = "";
2062-
return s;
2063-
}
2064-
20652057
static int submodule_has_dirty_index(const struct submodule *sub)
20662058
{
20672059
struct child_process cp = CHILD_PROCESS_INIT;
@@ -2080,7 +2072,7 @@ static int submodule_has_dirty_index(const struct submodule *sub)
20802072
return finish_command(&cp);
20812073
}
20822074

2083-
static void submodule_reset_index(const char *path)
2075+
static void submodule_reset_index(const char *path, const char *super_prefix)
20842076
{
20852077
struct child_process cp = CHILD_PROCESS_INIT;
20862078
prepare_submodule_repo_env(&cp.env);
@@ -2089,10 +2081,10 @@ static void submodule_reset_index(const char *path)
20892081
cp.no_stdin = 1;
20902082
cp.dir = path;
20912083

2092-
strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2093-
get_super_prefix_or_empty(), path);
20942084
/* TODO: determine if this might overwright untracked files */
20952085
strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
2086+
strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2087+
(super_prefix ? super_prefix : ""), path);
20962088

20972089
strvec_push(&cp.args, empty_tree_oid_hex());
20982090

@@ -2105,10 +2097,9 @@ static void submodule_reset_index(const char *path)
21052097
* For edge cases (a submodule coming into existence or removing a submodule)
21062098
* pass NULL for old or new respectively.
21072099
*/
2108-
int submodule_move_head(const char *path,
2109-
const char *old_head,
2110-
const char *new_head,
2111-
unsigned flags)
2100+
int submodule_move_head(const char *path, const char *super_prefix,
2101+
const char *old_head, const char *new_head,
2102+
unsigned flags)
21122103
{
21132104
int ret = 0;
21142105
struct child_process cp = CHILD_PROCESS_INIT;
@@ -2146,7 +2137,7 @@ int submodule_move_head(const char *path,
21462137
if (old_head) {
21472138
if (!submodule_uses_gitfile(path))
21482139
absorb_git_dir_into_superproject(path,
2149-
get_super_prefix());
2140+
super_prefix);
21502141
} else {
21512142
struct strbuf gitdir = STRBUF_INIT;
21522143
submodule_name_to_gitdir(&gitdir, the_repository,
@@ -2155,7 +2146,7 @@ int submodule_move_head(const char *path,
21552146
strbuf_release(&gitdir);
21562147

21572148
/* make sure the index is clean as well */
2158-
submodule_reset_index(path);
2149+
submodule_reset_index(path, super_prefix);
21592150
}
21602151

21612152
if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
@@ -2173,9 +2164,9 @@ int submodule_move_head(const char *path,
21732164
cp.no_stdin = 1;
21742165
cp.dir = path;
21752166

2176-
strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2177-
get_super_prefix_or_empty(), path);
21782167
strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
2168+
strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2169+
(super_prefix ? super_prefix : ""), path);
21792170

21802171
if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
21812172
strvec_push(&cp.args, "-n");

0 commit comments

Comments
 (0)