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

[PATCH v2 3/3] remote-helpers: export GIT_DIR variable to helpers

From
Dmitry Ivankov <divanorama@gmail.com>
Date
Jul 13, 2011, 17:10 UTC
Message-ID
<1310577055-6347-3-git-send-email-divanorama@gmail.com>
In-Reply-To
<1310483428-29833-1-git-send-email-divanorama@gmail.com>

The gitdir capability is recognized by git and can be used to tell the helper where the .git directory is. But it is not mentioned in the documentation and considered worse than if gitdir was passed via GIT_DIR environment variable.

Do export GIT_DIR for a remote helper. Switch testgit to use env instead of less favoured capability gitdir.

Mention it's possible uses in documentation: data storage, auxiliary git commands needed by a helper.

Signed-off-by: Dmitry Ivankov <divanorama@gmail.com>
---
Instead of documenting capability gitdir export GIT_DIR. Turned out
to be quite straightforward.
 Documentation/git-remote-helpers.txt |   18 ++++++++++++++++++
 git-remote-testgit.py                |   14 +-------------
 transport-helper.c                   |   10 ++++++++++
 3 files changed, 29 insertions(+), 13 deletions(-)
diff --git a/Documentation/git-remote-helpers.txt b/Documentation/git-remote-helpers.txt
index 58f6ad4..394fc8e 100644
--- a/Documentation/git-remote-helpers.txt
+++ b/Documentation/git-remote-helpers.txt
@@ -47,6 +47,9 @@ arguments. The first argument specifies a remote repository as in git;
 it is either the name of a configured remote or a URL. The second
 argument specifies a URL; it is usually of the form
 '<transport>://<address>', but any arbitrary string is possible.
+'GIT_DIR' environment variable is set up for the remote helper and
+can be used to store some additional data or to invoke auxiliary git
+commands.
 
 When git encounters a URL of the form '<transport>://<address>', where
 '<transport>' is a protocol that it cannot handle natively, it
@@ -159,6 +162,14 @@ Supported if the helper has the "import" capability.
 +
 Supported if the helper has the "connect" capability.
 
+'gitdir' <path>::
+	Tells helper the location of current repository .git
+	directory. The path is absolute. The command is issued
+	immediately after "gitdir" capability line is read by
+	the caller.
++
+Supported if the helper has the "gitdir" capability.
+
 If a fatal error occurs, the program writes the error message to
 stderr and exits. The caller should expect that a suitable error
 message has been printed if the child closes the connection without
@@ -175,6 +186,7 @@ CAPABILITIES
 'push'::
 'import'::
 'connect'::
+'gitdir'::
 	This helper supports the corresponding command with the same name.
 
 'refspec' 'spec'::
@@ -187,6 +199,12 @@ CAPABILITIES
 	all, it must cover all refs reported by the list command; if
 	it is not used, it is effectively "{asterisk}:{asterisk}"
 
+'gitdir'::
+	The helper wants to know .git directory location. The gitdir
+	command is sent immediately after reading this capability.
+	The helper can read it immediately too or it can proceed with
+	reporting other capabilities and read it as a next command.
+
 REF LIST ATTRIBUTES
 -------------------
 
diff --git a/git-remote-testgit.py b/git-remote-testgit.py
index df9d512..df3e37f 100644
--- a/git-remote-testgit.py
+++ b/git-remote-testgit.py
@@ -35,7 +35,7 @@ def get_repo(alias, url):
     prefix = 'refs/testgit/%s/' % alias
     debug("prefix: '%s'", prefix)
 
-    repo.gitdir = ""
+    repo.gitdir = os.environ["GIT_DIR"]
     repo.alias = alias
     repo.prefix = prefix
 
@@ -70,7 +70,6 @@ def do_capabilities(repo, args):
 
     print "import"
     print "export"
-    print "gitdir"
     print "refspec refs/heads/*:%s*" % repo.prefix
 
     print # end capabilities
@@ -150,22 +149,11 @@ def do_export(repo, args):
     repo.non_local.push(repo.gitdir)
 
 
-def do_gitdir(repo, args):
-    """Stores the location of the gitdir.
-    """
-
-    if not args:
-        die("gitdir needs an argument")
-
-    repo.gitdir = ' '.join(args)
-
-
 COMMANDS = {
     'capabilities': do_capabilities,
     'list': do_list,
     'import': do_import,
     'export': do_export,
-    'gitdir': do_gitdir,
 }
 
 
diff --git a/transport-helper.c b/transport-helper.c
index 660147f..3282693 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -105,6 +105,12 @@ static struct child_process *get_helper(struct transport *transport)
 	int refspec_alloc = 0;
 	int duped;
 	int code;
+	char git_dir_buf[sizeof(GIT_DIR_ENVIRONMENT) + PATH_MAX + 1];
+	const char *helper_env[] = {
+		git_dir_buf,
+		NULL
+	};
+
 
 	if (data->helper)
 		return data->helper;
@@ -120,6 +126,10 @@ static struct child_process *get_helper(struct transport *transport)
 	helper->argv[2] = remove_ext_force(transport->url);
 	helper->git_cmd = 0;
 	helper->silent_exec_failure = 1;
+
+	snprintf(git_dir_buf, sizeof(git_dir_buf), "%s=%s", GIT_DIR_ENVIRONMENT, get_git_dir());
+	helper->env = helper_env;
+
 	code = start_command(helper);
 	if (code < 0 && errno == ENOENT)
 		die("Unable to find remote helper for '%s'", data->name);
-- 
1.7.3.4
Previous: Dmitry IvankovNext: Dmitry Ivankov
Message 11 of 14 in “doc/{fast-import,remote-helpers}: few clarifications”
  1. 0/3 doc/{fast-import,remote-helpers}: few clarificationsDmitry Ivankov, Jul 12, 2011
  2. 1/3 doc/fast-import: clarify notemodify commandDmitry Ivankov, Jul 12, 2011
  3. 2/3 doc/fast-import: document feature import-marks-if-existsDmitry Ivankov, Jul 12, 2011
  4. Junio C HamanoJul 12, 2011
  5. Ramkumar RamachandraJul 13, 2011
  6. 3/3 doc/remote-helpers: document the gitdir featureDmitry Ivankov, Jul 12, 2011
  7. Sverre RabbelierJul 13, 2011
  8. 1/3 doc/fast-import: clarify notemodify commandDmitry Ivankov, Jul 13, 2011
  9. Johan HerlandJul 22, 2011
  10. 2/3 doc/fast-import: document feature import-marks-if-existsDmitry Ivankov, Jul 13, 2011
  11. 3/3 remote-helpers: export GIT_DIR variable to helpersDmitry Ivankov, Jul 13, 2011
  12. Dmitry IvankovJul 13, 2011
  13. Sverre RabbelierJul 13, 2011
  14. Dmitry IvankovJul 13, 2011

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.