* [PATCH v2] Add command line option --chdir/-C to allow setting git process work directory.
@ 2008-10-19 16:18 Maciej Pasternacki
2008-10-19 21:02 ` Jeff King
0 siblings, 1 reply; 4+ messages in thread
From: Maciej Pasternacki @ 2008-10-19 16:18 UTC (permalink / raw)
To: git; +Cc: Jeff King
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] Add command line option --chdir/-C to allow setting git process work directory.
2008-10-19 16:18 Maciej Pasternacki
@ 2008-10-19 21:02 ` Jeff King
0 siblings, 0 replies; 4+ messages in thread
From: Jeff King @ 2008-10-19 21:02 UTC (permalink / raw)
To: Maciej Pasternacki; +Cc: git
On Sun, Oct 19, 2008 at 09:18:19AM -0700, Maciej Pasternacki wrote:
> 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.
I took a closer look at this. I'm not sure that there is actually a
problem with moving the cd_to_toplevel before require_work_tree.
cd_to_toplevel makes sure there is actually something to cdup to. In my
test without a work-tree, "git rev-parse --show-cdup" simply printed
nothing, causing no "cd" to occur.
Moreover, git-pull is not the only script with this problem. There are
quite a few with
require_work_tree
cd_to_toplevel
which will break in the same way with --work-tree. And there are even
more with just require_work_tree that don't cd_to_toplevel at all. So I
suspect that --work-tree is largely broken in our scripts and nobody has
actually tried it.
So:
1. I think we can fix this breakage by swapping the two.
2. There is still breakage in other scripts, some of which may need
quite in-depth fixes (e.g., git-bisect requires a work tree but
does not chdir at all. For the require_work_tree test to work, it
needs to be inside the work tree, and the script will need a
careful looking over to see what ramifications that has).
3. I think your -C option has some merit independent of this, since
it allows you to chdir and still use the usual lookup rules (e.g.,
see if it is bare vs a regular repository). But I don't feel
strongly about it one way or the other.
-Peff
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] Add command line option --chdir/-C to allow setting git process work directory.
[not found] <0B4A1BD5-5FC3-4B29-B0BB-77A85AEDF5B2@pasternacki.net>
@ 2008-10-19 23:13 ` Maciej Pasternacki
2011-09-24 6:57 ` kimi
0 siblings, 1 reply; 4+ messages in thread
From: Maciej Pasternacki @ 2008-10-19 23:13 UTC (permalink / raw)
To: git
On 2008-10-19, at 23:02, Jeff King wrote:
> I took a closer look at this. I'm not sure that there is actually a
> problem with moving the cd_to_toplevel before require_work_tree.
> cd_to_toplevel makes sure there is actually something to cdup to. In
> my
> test without a work-tree, "git rev-parse --show-cdup" simply printed
> nothing, causing no "cd" to occur.
I won't speculate on whether moving cd_to_toplevel up will introduce
problem or not. I am not familiar with git internals, and I just
repeat an opinion gathered on #git. However, -C seem to be better
suited for simple cases, conceptually cleaner, and may make the
learning curve a bit less steep. There is no need for consdering all
possible --work-tree/--git-dir combinations and their implications, it
is possible just to say "cd there, and act as usual".
> So:
>
> 1. I think we can fix this breakage by swapping the two.
I can prepare a patch if that's expected, but probably I won't
understand what it exactly does and what are implications of this
change.
> 2. There is still breakage in other scripts, some of which may need
> quite in-depth fixes (e.g., git-bisect requires a work tree but
> does not chdir at all. For the require_work_tree test to work, it
> needs to be inside the work tree, and the script will need a
> careful looking over to see what ramifications that has).
I really don't think I'm competent enough to fix more complex cases
without breaking twice as many things; I don't feel confident even
with a simple swap in git-pull at the moment
> 3. I think your -C option has some merit independent of this, since
> it allows you to chdir and still use the usual lookup rules (e.g.,
> see if it is bare vs a regular repository). But I don't feel
> strongly about it one way or the other.
In my use case, it definitely has merits as it makes git behave
exactly as if user himself wrote "git pull" inside the work tree, and
I am still not sure wheter expected --work-tree/--git-dir combinations
would behave the same. Probably I'll need to add more commands and
more complex use cases as I develop cl-librarian, and -C would make
this job way easier.
Regards,
Maciej.
--
-><- Maciej Pasternacki -><- http://www.pasternacki.net/ -><-
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] Add command line option --chdir/-C to allow setting git process work directory.
2008-10-19 23:13 ` [PATCH v2] Add command line option --chdir/-C to allow setting git process work directory Maciej Pasternacki
@ 2011-09-24 6:57 ` kimi
0 siblings, 0 replies; 4+ messages in thread
From: kimi @ 2011-09-24 6:57 UTC (permalink / raw)
To: git
I like http://www.monclershare.com/ <strong>Moncler</strong>
I like http://www.monclershare.com/ <strong>Buy Moncler</strong>
I like http://www.monclershare.com/ <strong>Moncler Online</strong>
I like http://www.monclershare.com/ <strong>Moncler Jackets</strong>
I like http://www.monclershare.com/ <strong>Discount Moncler
Jackets</strong>
--
View this message in context: http://git.661346.n2.nabble.com/PATCH-v2-Add-command-line-option-chdir-C-to-allow-setting-git-process-work-directory-tp1352126p6826652.html
Sent from the git mailing list archive at Nabble.com.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-09-24 6:57 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <0B4A1BD5-5FC3-4B29-B0BB-77A85AEDF5B2@pasternacki.net>
2008-10-19 23:13 ` [PATCH v2] Add command line option --chdir/-C to allow setting git process work directory Maciej Pasternacki
2011-09-24 6:57 ` kimi
2008-10-19 16:18 Maciej Pasternacki
2008-10-19 21:02 ` Jeff King
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).