Skip to content

Commit 74866d7

Browse files
bmwillgitster
authored andcommitted
git: make super-prefix option
Add a super-prefix environment variable 'GIT_INTERNAL_SUPER_PREFIX' which can be used to specify a path from above a repository down to its root. When such a super-prefix is specified, the paths reported by Git are prefixed with it to make them relative to that directory "above". The paths given by the user on the command line (e.g. "git subcmd --output-file=path/to/a/file" and pathspecs) are taken relative to the directory "above" to match. The immediate use of this option is by commands which have a --recurse-submodule option in order to give context to submodules about how they were invoked. This option is currently only allowed for builtins which support a super-prefix. Signed-off-by: Brandon Williams <[email protected]> Reviewed-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 35f6318 commit 74866d7

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

Documentation/git.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SYNOPSIS
1313
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
1414
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
1515
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
16+
[--super-prefix=<path>]
1617
<command> [<args>]
1718

1819
DESCRIPTION
@@ -601,6 +602,11 @@ foo.bar= ...`) sets `foo.bar` to the empty string.
601602
details. Equivalent to setting the `GIT_NAMESPACE` environment
602603
variable.
603604

605+
--super-prefix=<path>::
606+
Currently for internal use only. Set a prefix which gives a path from
607+
above a repository down to its root. One use is to give submodules
608+
context about the superproject that invoked it.
609+
604610
--bare::
605611
Treat the repository as a bare repository. If GIT_DIR
606612
environment is not set, it is set to the current working

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ static inline enum object_type object_type(unsigned int mode)
408408
#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
409409
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
410410
#define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
411+
#define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
411412
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
412413
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
413414
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@ -468,6 +469,7 @@ extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
468469
extern int get_common_dir(struct strbuf *sb, const char *gitdir);
469470
extern const char *get_git_namespace(void);
470471
extern const char *strip_namespace(const char *namespaced_ref);
472+
extern const char *get_super_prefix(void);
471473
extern const char *get_git_work_tree(void);
472474

473475
/*

environment.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ static char *work_tree;
100100
static const char *namespace;
101101
static size_t namespace_len;
102102

103+
static const char *super_prefix;
104+
103105
static const char *git_dir, *git_common_dir;
104106
static char *git_object_dir, *git_index_file, *git_graft_file;
105107
int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
@@ -120,6 +122,7 @@ const char * const local_repo_env[] = {
120122
NO_REPLACE_OBJECTS_ENVIRONMENT,
121123
GIT_REPLACE_REF_BASE_ENVIRONMENT,
122124
GIT_PREFIX_ENVIRONMENT,
125+
GIT_SUPER_PREFIX_ENVIRONMENT,
123126
GIT_SHALLOW_FILE_ENVIRONMENT,
124127
GIT_COMMON_DIR_ENVIRONMENT,
125128
NULL
@@ -222,6 +225,16 @@ const char *strip_namespace(const char *namespaced_ref)
222225
return namespaced_ref + namespace_len;
223226
}
224227

228+
const char *get_super_prefix(void)
229+
{
230+
static int initialized;
231+
if (!initialized) {
232+
super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
233+
initialized = 1;
234+
}
235+
return super_prefix;
236+
}
237+
225238
static int git_work_tree_initialized;
226239

227240
/*

git.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
164164
setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
165165
if (envchanged)
166166
*envchanged = 1;
167+
} else if (!strcmp(cmd, "--super-prefix")) {
168+
if (*argc < 2) {
169+
fprintf(stderr, "No prefix given for --super-prefix.\n" );
170+
usage(git_usage_string);
171+
}
172+
setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1);
173+
if (envchanged)
174+
*envchanged = 1;
175+
(*argv)++;
176+
(*argc)--;
177+
} else if (skip_prefix(cmd, "--super-prefix=", &cmd)) {
178+
setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1);
179+
if (envchanged)
180+
*envchanged = 1;
167181
} else if (!strcmp(cmd, "--bare")) {
168182
char *cwd = xgetcwd();
169183
is_bare_repository_cfg = 1;
@@ -310,6 +324,7 @@ static int handle_alias(int *argcp, const char ***argv)
310324
* RUN_SETUP for reading from the configuration file.
311325
*/
312326
#define NEED_WORK_TREE (1<<3)
327+
#define SUPPORT_SUPER_PREFIX (1<<4)
313328

314329
struct cmd_struct {
315330
const char *cmd;
@@ -344,6 +359,13 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
344359
}
345360
commit_pager_choice();
346361

362+
if (!help && get_super_prefix()) {
363+
if (!(p->option & SUPPORT_SUPER_PREFIX))
364+
die("%s doesn't support --super-prefix", p->cmd);
365+
if (prefix)
366+
die("can't use --super-prefix from a subdirectory");
367+
}
368+
347369
if (!help && p->option & NEED_WORK_TREE)
348370
setup_work_tree();
349371

@@ -558,6 +580,9 @@ static void execv_dashed_external(const char **argv)
558580
const char *tmp;
559581
int status;
560582

583+
if (get_super_prefix())
584+
die("%s doesn't support --super-prefix", argv[0]);
585+
561586
if (use_pager == -1)
562587
use_pager = check_pager_config(argv[0]);
563588
commit_pager_choice();

0 commit comments

Comments
 (0)