git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v6 1/4] git: make super-prefix option

From
BWBrandon Williams <bmwill@google.com>
Date
Sep 29, 2016, 21:48 UTC
Message-ID
<1475185723-36871-2-git-send-email-bmwill@google.com>
In-Reply-To
<1475185723-36871-1-git-send-email-bmwill@google.com>

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 <bmwill@google.com>
---
 Documentation/git.txt |  6 ++++++
 cache.h               |  2 ++
 environment.c         | 10 ++++++++++
 git.c                 | 26 ++++++++++++++++++++++++++
 4 files changed, 44 insertions(+)
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 7913fc2..2188ae6 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -13,6 +13,7 @@ SYNOPSIS
     [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
     [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
     [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
+    [--super-prefix=<path>]
     <command> [<args>]
 
 DESCRIPTION
@@ -601,6 +602,11 @@ foo.bar= ...`) sets `foo.bar` to the empty string.
 	details.  Equivalent to setting the `GIT_NAMESPACE` environment
 	variable.
 
+--super-prefix=<path>::
+	Currently for internal use only.  Set a prefix which gives a path from
+	above a repository down to its root.  One use is to give submodules
+	context about the superproject that invoked it.
+
 --bare::
 	Treat the repository as a bare repository.  If GIT_DIR
 	environment is not set, it is set to the current working
diff --git a/cache.h b/cache.h
index 3556326..8cf495d 100644
--- a/cache.h
+++ b/cache.h
@@ -408,6 +408,7 @@ static inline enum object_type object_type(unsigned int mode)
 #define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
 #define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
 #define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
+#define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
 #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
 #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
 #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@ -468,6 +469,7 @@ extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
 extern int get_common_dir(struct strbuf *sb, const char *gitdir);
 extern const char *get_git_namespace(void);
 extern const char *strip_namespace(const char *namespaced_ref);
+extern const char *get_super_prefix(void);
 extern const char *get_git_work_tree(void);
 
 /*
diff --git a/environment.c b/environment.c
index ca72464..13f3d70 100644
--- a/environment.c
+++ b/environment.c
@@ -100,6 +100,8 @@ static char *work_tree;
 static const char *namespace;
 static size_t namespace_len;
 
+static const char *super_prefix;
+
 static const char *git_dir, *git_common_dir;
 static char *git_object_dir, *git_index_file, *git_graft_file;
 int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
@@ -120,6 +122,7 @@ const char * const local_repo_env[] = {
 	NO_REPLACE_OBJECTS_ENVIRONMENT,
 	GIT_REPLACE_REF_BASE_ENVIRONMENT,
 	GIT_PREFIX_ENVIRONMENT,
+	GIT_SUPER_PREFIX_ENVIRONMENT,
 	GIT_SHALLOW_FILE_ENVIRONMENT,
 	GIT_COMMON_DIR_ENVIRONMENT,
 	NULL
@@ -222,6 +225,13 @@ const char *strip_namespace(const char *namespaced_ref)
 	return namespaced_ref + namespace_len;
 }
 
+const char *get_super_prefix(void)
+{
+	if (!super_prefix)
+		super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
+	return super_prefix;
+}
+
 static int git_work_tree_initialized;
 
 /*
diff --git a/git.c b/git.c
index 1c61151..f756b62 100644
--- a/git.c
+++ b/git.c
@@ -164,6 +164,20 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
 			setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
 			if (envchanged)
 				*envchanged = 1;
+		} else if (!strcmp(cmd, "--super-prefix")) {
+			if (*argc < 2) {
+				fprintf(stderr, "No prefix given for --super-prefix.\n" );
+				usage(git_usage_string);
+			}
+			setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1);
+			if (envchanged)
+				*envchanged = 1;
+			(*argv)++;
+			(*argc)--;
+		} else if (skip_prefix(cmd, "--super-prefix=", &cmd)) {
+			setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1);
+			if (envchanged)
+				*envchanged = 1;
 		} else if (!strcmp(cmd, "--bare")) {
 			char *cwd = xgetcwd();
 			is_bare_repository_cfg = 1;
@@ -310,6 +324,7 @@ static int handle_alias(int *argcp, const char ***argv)
  * RUN_SETUP for reading from the configuration file.
  */
 #define NEED_WORK_TREE		(1<<3)
+#define SUPPORT_SUPER_PREFIX	(1<<4)
 
 struct cmd_struct {
 	const char *cmd;
@@ -344,6 +359,13 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 	}
 	commit_pager_choice();
 
+	if (!help && get_super_prefix()) {
+		if (!(p->option & SUPPORT_SUPER_PREFIX))
+			die("%s doesn't support --super-prefix", p->cmd);
+		if (prefix)
+			die("can't use --super-prefix from a subdirectory");
+	}
+
 	if (!help && p->option & NEED_WORK_TREE)
 		setup_work_tree();
 
@@ -558,6 +580,10 @@ static void execv_dashed_external(const char **argv)
 	const char *tmp;
 	int status;
 
+	if (get_super_prefix()) {
+		die("%s doesn't support --super-prefix", argv[0]);
+	}
+
 	if (use_pager == -1)
 		use_pager = check_pager_config(argv[0]);
 	commit_pager_choice();
-- 
2.10.0
Previous: Brandon WilliamsNext: Stefan Beller
Message 49 of 67 in “recursive support for ls-files”
  1. 0/3 recursive support for ls-filesBrandon Williams, Sep 24, 2016
  2. 1/3 submodules: make submodule-prefix option an envvarBrandon Williams, Sep 24, 2016
  3. Junio C HamanoSep 25, 2016
  4. 2/3 ls-files: optionally recurse into submodulesBrandon Williams, Sep 24, 2016
  5. 3/3 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 24, 2016
  6. Jeff KingSep 25, 2016
  7. Brandon WilliamsSep 25, 2016
  8. Junio C HamanoSep 25, 2016
  9. Brandon WilliamsSep 26, 2016
  10. Junio C HamanoSep 26, 2016
  11. Brandon WilliamsSep 26, 2016
  12. Junio C HamanoSep 26, 2016
  13. 0/4 recursive support for ls-filesBrandon Williams, Sep 26, 2016
  14. 1/4 submodules: make submodule-prefix optionBrandon Williams, Sep 26, 2016
  15. Junio C HamanoSep 27, 2016
  16. Brandon WilliamsSep 27, 2016
  17. Junio C HamanoSep 27, 2016
  18. Brandon WilliamsSep 27, 2016
  19. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Sep 26, 2016
  20. Junio C HamanoSep 27, 2016
  21. Brandon WilliamsSep 27, 2016
  22. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 26, 2016
  23. Junio C HamanoSep 27, 2016
  24. Brandon WilliamsSep 27, 2016
  25. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Sep 26, 2016
  26. Junio C HamanoSep 27, 2016
  27. Junio C HamanoSep 27, 2016
  28. Brandon WilliamsSep 27, 2016
  29. Junio C HamanoSep 27, 2016
  30. Stefan BellerSep 27, 2016
  31. Brandon WilliamsSep 28, 2016
  32. Junio C HamanoSep 28, 2016
  33. Brandon WilliamsSep 27, 2016
  34. Junio C HamanoSep 27, 2016
  35. Brandon WilliamsSep 27, 2016
  36. Junio C HamanoSep 27, 2016
  37. 0/4 recursive support for ls-filesBrandon Williams, Sep 28, 2016
  38. 1/4 git: make super-prefix optionBrandon Williams, Sep 28, 2016
  39. Stefan BellerSep 28, 2016
  40. Junio C HamanoSep 28, 2016
  41. Jeff KingSep 29, 2016
  42. Brandon WilliamsSep 29, 2016
  43. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Sep 28, 2016
  44. Stefan BellerSep 28, 2016
  45. Junio C HamanoSep 28, 2016
  46. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Sep 28, 2016
  47. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 28, 2016
  48. 0/4 recursive support for ls-filesBrandon Williams, Sep 29, 2016
  49. 1/4 git: make super-prefix optionBrandon Williams, Sep 29, 2016
  50. Stefan BellerOct 4, 2016
  51. Junio C HamanoOct 4, 2016
  52. Jeff KingOct 4, 2016
  53. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Sep 29, 2016
  54. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Sep 29, 2016
  55. Junio C HamanoSep 30, 2016
  56. Brandon WilliamsSep 30, 2016
  57. Brandon WilliamsSep 30, 2016
  58. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 29, 2016
  59. Stefan BellerOct 4, 2016
  60. 0/4 recursive support for ls-filesBrandon Williams, Oct 7, 2016
  61. 1/4 git: make super-prefix optionBrandon Williams, Oct 7, 2016
  62. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Oct 7, 2016
  63. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Oct 7, 2016
  64. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Oct 7, 2016
  65. Stefan BellerOct 7, 2016
  66. Stefan BellerOct 7, 2016
  67. Brandon WilliamsOct 7, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.