From: Maciej Pasternacki <maciej@pasternacki.net>
To: git@vger.kernel.org
Subject: [PATCH] -C/--chdir command line option
Date: Sat, 18 Oct 2008 17:02:27 -0700 [thread overview]
Message-ID: <20081019000227.GA9423@charybdis.dreamhost.com> (raw)
In my project cl-librarian, which is a layer built upon different
version control systems, I need to run git pull, but start it from
outside of work tree; however, pull needs to be in work tree (as in
getcwd()) in order to update said work tree. --git-dir and
--work-tree options don't work; I discussed it on #git yesterday,
and it turned out that this issue is nontrivial:
231701 <doener> hm, require_work_tree checks if you are _in_ the work tree,
and pull only switches to the work tree after the require_work_tree
call...
231710 <doener> looks like a chicken-egg-problem
231715 <shd> japhy`: spawning commands requires forking anyway, so do chdir in
forked process
231739 <doener> if you move the "cd_to_toplevel" call up in git-push, so that
it happens before require_work_tree, then it works
231747 <japhy`> shd: yup, that would be a workaround, but I tried to avoid
shell-quoting ;)
231749 <japhy`> doener: push?
231756 <doener> ehrm, pull
channel #git
231820 <doener> but I guess doing the cd_to_toplevel first might break somehow
if there is no working tree...
231843 <japhy`> Oh.
231922 <doener> I'd just ask on the list about that.
I don't like the idea of doing chdir() myself -- this would expose me to a
whole world of portability issues, and I don't feel like spending time to
figure out how to do a fork on windows ;) the best workaround that occured to
me was adding -C/--chdir option to main git binary, like one that Make accepts.
This fixed my problem, I paste the resulting patch below:
>From 4461cd1be99772c93a83bcdfe6e6a86e71abaa35 Mon Sep 17 00:00:00 2001
From: Maciej Pasternacki <maciej@pasternacki.net>
Date: Sun, 19 Oct 2008 01:33:49 +0200
Subject: [PATCH] Add command line option --chdir/-C to allow setting git process work directory.
Signed-off-by: Maciej Pasternacki <maciej@pasternacki.net>
---
Documentation/git.txt | 6 +++++-
git.c | 9 ++++++++-
2 files changed, 13 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..8c97671 100644
--- a/git.c
+++ b/git.c
@@ -115,7 +115,14 @@ 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 {
+ } else if (!strcmp(cmd, "-C") || !strcmp(cmd, "--chdir")) {
+ chdir((*argv)[1]);
+ (*argv)++;
+ (*argc)--;
+ handled++;
+ } else if (!prefixcmp(cmd,"--chdir=")) {
+ chdir(cmd+8);
+ } else {
fprintf(stderr, "Unknown option: %s\n", cmd);
usage(git_usage_string);
}
--
1.6.0.1
next reply other threads:[~2008-10-19 0:39 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-19 0:02 Maciej Pasternacki [this message]
2008-10-19 13:17 ` [PATCH] -C/--chdir command line option Jeff King
2008-10-19 13:47 ` Maciej Pasternacki
2008-10-19 14:16 ` Jeff King
2008-10-19 15:24 ` Maciej Pasternacki
2008-10-20 4:55 ` Junio C Hamano
2008-10-20 5:41 ` Junio C Hamano
2008-10-20 6:26 ` Alex Riesen
2008-10-20 12:57 ` Michael J Gruber
2008-10-20 13:59 ` Nguyen Thai Ngoc Duy
[not found] <DDFCD680-C477-4BE5-AB71-3F26048E26D1@pasternacki.net>
2008-10-20 7:28 ` Maciej Pasternacki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20081019000227.GA9423@charybdis.dreamhost.com \
--to=maciej@pasternacki.net \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).