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

[PATCH v2] Add command line option --chdir/-C to allow setting git process work directory.

From
Maciej Pasternacki <maciej@pasternacki.net>
Date
Oct 19, 2008, 16:18 UTC
Message-ID
<20081019161819.GA12495@charybdis.dreamhost.com>

When "git pull" is ran outside of work tree, it is unable to update work tree with pulled changes; specifying --git-dir and --work-tree does not help here because "cd_to_toplevel" call in git-pull occurs after "require_work_tree"; changing order may break the commend if there is no working tree. Some more commands behave in a similar way.

In my project, I need to call "git pull" from outside of work tree, but I need it to update the work tree. It seems to require doing chdir() before running git, but this is problematic thing to do in the calling program because of portability issues. Proper solution seems to be providing -C / --chdir command line option, similar to this of Make, that would make main git program perform chdir() to specified directory before running the specific command. This would make using git from outside programs much more straightforward.

This patch provides -C and --chdir command line options that behave as described above.

Signed-off-by: Maciej Pasternacki <maciej@pasternacki.net>
---
 Documentation/git.txt |    6 +++++-
 git.c                 |   19 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/Documentation/git.txt b/Documentation/git.txt
index df420ae..6676d68 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -12,7 +12,7 @@ SYNOPSIS
 'git' [--version] [--exec-path[=GIT_EXEC_PATH]]
     [-p|--paginate|--no-pager]
     [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
-    [--help] COMMAND [ARGS]
+    [-C DIRECTORY|--chdir=DIRECTORY] [--help] COMMAND [ARGS]
 
 DESCRIPTION
 -----------
@@ -185,6 +185,10 @@ help ...`.
 	environment is not set, it is set to the current working
 	directory.
 
+-C <directory>::
+--chdir=<directory>::
+	Change working directory before doing anything.
+
 
 FURTHER DOCUMENTATION
 ---------------------
diff --git a/git.c b/git.c
index 89feb0b..c63d754 100644
--- a/git.c
+++ b/git.c
@@ -4,7 +4,7 @@
 #include "quote.h"
 
 const char git_usage_string[] =
-	"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]";
+	"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [-C DIR|--chdir=DIR] [--help] COMMAND [ARGS]";
 
 const char git_more_info_string[] =
 	"See 'git help COMMAND' for more information on a specific command.";
@@ -115,6 +115,23 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)
 			setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
 			if (envchanged)
 				*envchanged = 1;
+		} else if (!strcmp(cmd, "-C") || !strcmp(cmd, "--chdir")) {
+			if (*argc < 2) {
+				fprintf(stderr, "No directory given for --chdir or -C.\n" );
+				usage(git_usage_string);
+			}
+			if (chdir((*argv)[1])) {
+				fprintf(stderr, "Cannot change directory to %s: %s\n", (*argv)[1], strerror(errno));
+				usage(git_usage_string);
+			}
+			(*argv)++;
+			(*argc)--;
+			handled++;
+		} else if (!prefixcmp(cmd,"--chdir=")) {
+			if (chdir(cmd+8)) {
+				fprintf(stderr, "Cannot change directory to %s: %s\n", cmd+8, strerror(errno));				     
+				usage(git_usage_string);
+			}
 		} else {
 			fprintf(stderr, "Unknown option: %s\n", cmd);
 			usage(git_usage_string);
-- 
1.6.0.1
Next: Jeff King
Message 1 of 2 in “Add command line option --chdir/-C to allow setting git process work directory.”
  1. Add command line option --chdir/-C to allow setting git process work directory.Maciej Pasternacki, Oct 19, 2008
  2. Jeff KingOct 19, 2008

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.