Git development
 help / color / mirror / Atom feed
* [PATCH] added -C option to chdir() into another directory first
@ 2009-02-26 20:11 kevin brintnall
  2009-02-26 20:35 ` Junio C Hamano
  2009-02-27 14:21 ` Michael J Gruber
  0 siblings, 2 replies; 5+ messages in thread
From: kevin brintnall @ 2009-02-26 20:11 UTC (permalink / raw)
  To: git; +Cc: kevin brintnall

This allows things like 'git -C /somewhere pull' without specifying both
--work-tree and --git-dir.

Signed-off-by: kevin brintnall <kbrint@rufus.net>
---
 git.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)

diff --git a/git.c b/git.c
index c2b181e..b218bfe 100644
--- a/git.c
+++ b/git.c
@@ -5,7 +5,7 @@
 #include "run-command.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 BASE_DIR] [--help] COMMAND [ARGS]";
 
 const char git_more_info_string[] =
 	"See 'git help COMMAND' for more information on a specific command.";
@@ -116,6 +116,16 @@ 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")) {
+			char *dir = (*argv)[1];
+			if (*argc < 2) {
+				fprintf(stderr, "No directory given for -C");
+				usage(git_usage_string);
+			}
+			if (chdir(dir))
+				die("Cannot change to %s: %s", dir, strerror(errno));
+			(*argv)++;
+			(*argc)--;
 		} else {
 			fprintf(stderr, "Unknown option: %s\n", cmd);
 			usage(git_usage_string);
-- 
1.6.1.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] added -C option to chdir() into another directory first
  2009-02-26 20:11 [PATCH] added -C option to chdir() into another directory first kevin brintnall
@ 2009-02-26 20:35 ` Junio C Hamano
  2009-02-26 20:44   ` kevin brintnall
  2009-02-27 14:21 ` Michael J Gruber
  1 sibling, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2009-02-26 20:35 UTC (permalink / raw)
  To: kevin brintnall; +Cc: git

kevin brintnall <kbrint@rufus.net> writes:

> This allows things like 'git -C /somewhere pull' without specifying both
> --work-tree and --git-dir.

Where should "git -C sub/dir apply this.patch" find the file "this.patch"?

More generally, when "git -C there cmd arg1 arg2 arg3..." is run, how
should the implementation of cmd learn what to prefix arg$N with?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] added -C option to chdir() into another directory first
  2009-02-26 20:35 ` Junio C Hamano
@ 2009-02-26 20:44   ` kevin brintnall
  2009-02-26 21:50     ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: kevin brintnall @ 2009-02-26 20:44 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Thu, Feb 26, 2009 at 12:35:57PM -0800, Junio C Hamano wrote:
> kevin brintnall <kbrint@rufus.net> writes:
> 
> > This allows things like 'git -C /somewhere pull' without specifying both
> > --work-tree and --git-dir.
> 
> Where should "git -C sub/dir apply this.patch" find the file "this.patch"?

Good question..  It should probably come from the original $PWD.  Maybe we
should have "-C $DIR" simulate "--work-tree=$DIR --git=dir=$DIR/.git" ?

> More generally, when "git -C there cmd arg1 arg2 arg3..." is run, how
> should the implementation of cmd learn what to prefix arg$N with?

I envisioned these two as equivalent:

	git -C $DIR something
	cd $DIR ; git something

-- 
 kevin brintnall =~ /kbrint@rufus.net/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] added -C option to chdir() into another directory first
  2009-02-26 20:44   ` kevin brintnall
@ 2009-02-26 21:50     ` Junio C Hamano
  0 siblings, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2009-02-26 21:50 UTC (permalink / raw)
  To: kevin brintnall; +Cc: git

kevin brintnall <kbrint@rufus.net> writes:

> On Thu, Feb 26, 2009 at 12:35:57PM -0800, Junio C Hamano wrote:
>> kevin brintnall <kbrint@rufus.net> writes:
>> 
>> > This allows things like 'git -C /somewhere pull' without specifying both
>> > --work-tree and --git-dir.
>> 
>> Where should "git -C sub/dir apply this.patch" find the file "this.patch"?
>
> Good question..  It should probably come from the original $PWD.  Maybe we
> should have "-C $DIR" simulate "--work-tree=$DIR --git=dir=$DIR/.git" ?
>
>> More generally, when "git -C there cmd arg1 arg2 arg3..." is run, how
>> should the implementation of cmd learn what to prefix arg$N with?
>
> I envisioned these two as equivalent:
>
> 	git -C $DIR something
> 	cd $DIR ; git something

If that is the case then I really do not see the point, other than "there
are _some_ tools like 'tar' that do it".

Sure, there are some tools that do many other things.  So what?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] added -C option to chdir() into another directory first
  2009-02-26 20:11 [PATCH] added -C option to chdir() into another directory first kevin brintnall
  2009-02-26 20:35 ` Junio C Hamano
@ 2009-02-27 14:21 ` Michael J Gruber
  1 sibling, 0 replies; 5+ messages in thread
From: Michael J Gruber @ 2009-02-27 14:21 UTC (permalink / raw)
  To: kevin brintnall; +Cc: git

kevin brintnall venit, vidit, dixit 26.02.2009 21:11:
> This allows things like 'git -C /somewhere pull' without specifying both
> --work-tree and --git-dir.
> 

I'm afraid you hit a somewhat reoccurring issue here. chdir'ing looks
simple, but it has many side effects, such as on relative paths (as
Junio pointed out) but also on the order of parsing config. Having -C as
a global git option would require dealing with all of these effects.

On the other hand, a shell function can achieve the same very easily.
The side effects are left to be dealt with by the user then ;)

Michael

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-02-27 14:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-26 20:11 [PATCH] added -C option to chdir() into another directory first kevin brintnall
2009-02-26 20:35 ` Junio C Hamano
2009-02-26 20:44   ` kevin brintnall
2009-02-26 21:50     ` Junio C Hamano
2009-02-27 14:21 ` Michael J Gruber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox