{"thread":{"id":"18037","subject":"[PATCH] added -C option to chdir() into another directory first","startedAt":"2009-02-26T20:11:39Z","lastAt":"2009-02-27T14:21:15Z","messageCount":5,"participants":["kevin brintnall","Junio C Hamano","Michael J Gruber"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"106355","messageId":"1235679099-33994-1-git-send-email-kbrint@rufus.net","threadId":"18037","inReplyTo":null,"subject":"[PATCH] added -C option to chdir() into another directory first","fromName":"kevin brintnall","fromEmail":"kbrint@rufus.net","sentAt":"2009-02-26T20:11:39Z","receivedAt":"2009-02-26T20:11:39Z","isPatch":true,"sender":{"key":"kbrint@rufus.net","avatar":null},"body":"This allows things like 'git -C /somewhere pull' without specifying both\n--work-tree and --git-dir.\n\nSigned-off-by: kevin brintnall <kbrint@rufus.net>\n---\n git.c |   12 +++++++++++-\n 1 files changed, 11 insertions(+), 1 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex c2b181e..b218bfe 100644\n--- a/git.c\n+++ b/git.c\n@@ -5,7 +5,7 @@\n #include \"run-command.h\"\n \n const char git_usage_string[] =\n-\t\"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]\";\n+\t\"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]\";\n \n const char git_more_info_string[] =\n \t\"See 'git help COMMAND' for more information on a specific command.\";\n@@ -116,6 +116,16 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)\n \t\t\tsetenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);\n \t\t\tif (envchanged)\n \t\t\t\t*envchanged = 1;\n+\t\t} else if (!strcmp(cmd, \"-C\")) {\n+\t\t\tchar *dir = (*argv)[1];\n+\t\t\tif (*argc < 2) {\n+\t\t\t\tfprintf(stderr, \"No directory given for -C\");\n+\t\t\t\tusage(git_usage_string);\n+\t\t\t}\n+\t\t\tif (chdir(dir))\n+\t\t\t\tdie(\"Cannot change to %s: %s\", dir, strerror(errno));\n+\t\t\t(*argv)++;\n+\t\t\t(*argc)--;\n \t\t} else {\n \t\t\tfprintf(stderr, \"Unknown option: %s\\n\", cmd);\n \t\t\tusage(git_usage_string);\n-- \n1.6.1.3\n"},{"id":"106358","messageId":"7vr61lq6ky.fsf@gitster.siamese.dyndns.org","threadId":"18037","inReplyTo":"1235679099-33994-1-git-send-email-kbrint@rufus.net","subject":"Re: [PATCH] added -C option to chdir() into another directory first","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-02-26T20:35:57Z","receivedAt":"2009-02-26T20:35:57Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"kevin brintnall <kbrint@rufus.net> writes:\n\n> This allows things like 'git -C /somewhere pull' without specifying both\n> --work-tree and --git-dir.\n\nWhere should \"git -C sub/dir apply this.patch\" find the file \"this.patch\"?\n\nMore generally, when \"git -C there cmd arg1 arg2 arg3...\" is run, how\nshould the implementation of cmd learn what to prefix arg$N with?\n"},{"id":"106360","messageId":"20090226204423.GA34377@rufus.net","threadId":"18037","inReplyTo":"7vr61lq6ky.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] added -C option to chdir() into another directory first","fromName":"kevin brintnall","fromEmail":"kbrint@rufus.net","sentAt":"2009-02-26T20:44:23Z","receivedAt":"2009-02-26T20:44:23Z","isPatch":true,"sender":{"key":"kbrint@rufus.net","avatar":null},"body":"On Thu, Feb 26, 2009 at 12:35:57PM -0800, Junio C Hamano wrote:\n> kevin brintnall <kbrint@rufus.net> writes:\n> \n> > This allows things like 'git -C /somewhere pull' without specifying both\n> > --work-tree and --git-dir.\n> \n> Where should \"git -C sub/dir apply this.patch\" find the file \"this.patch\"?\n\nGood question..  It should probably come from the original $PWD.  Maybe we\nshould have \"-C $DIR\" simulate \"--work-tree=$DIR --git=dir=$DIR/.git\" ?\n\n> More generally, when \"git -C there cmd arg1 arg2 arg3...\" is run, how\n> should the implementation of cmd learn what to prefix arg$N with?\n\nI envisioned these two as equivalent:\n\n\tgit -C $DIR something\n\tcd $DIR ; git something\n\n-- \n kevin brintnall =~ /kbrint@rufus.net/\n"},{"id":"106365","messageId":"7viqmwrhoy.fsf@gitster.siamese.dyndns.org","threadId":"18037","inReplyTo":"20090226204423.GA34377@rufus.net","subject":"Re: [PATCH] added -C option to chdir() into another directory first","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-02-26T21:50:37Z","receivedAt":"2009-02-26T21:50:37Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"kevin brintnall <kbrint@rufus.net> writes:\n\n> On Thu, Feb 26, 2009 at 12:35:57PM -0800, Junio C Hamano wrote:\n>> kevin brintnall <kbrint@rufus.net> writes:\n>> \n>> > This allows things like 'git -C /somewhere pull' without specifying both\n>> > --work-tree and --git-dir.\n>> \n>> Where should \"git -C sub/dir apply this.patch\" find the file \"this.patch\"?\n>\n> Good question..  It should probably come from the original $PWD.  Maybe we\n> should have \"-C $DIR\" simulate \"--work-tree=$DIR --git=dir=$DIR/.git\" ?\n>\n>> More generally, when \"git -C there cmd arg1 arg2 arg3...\" is run, how\n>> should the implementation of cmd learn what to prefix arg$N with?\n>\n> I envisioned these two as equivalent:\n>\n> \tgit -C $DIR something\n> \tcd $DIR ; git something\n\nIf that is the case then I really do not see the point, other than \"there\nare _some_ tools like 'tar' that do it\".\n\nSure, there are some tools that do many other things.  So what?\n"},{"id":"106435","messageId":"49A7F6DB.7040403@drmicha.warpmail.net","threadId":"18037","inReplyTo":"1235679099-33994-1-git-send-email-kbrint@rufus.net","subject":"Re: [PATCH] added -C option to chdir() into another directory first","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2009-02-27T14:21:15Z","receivedAt":"2009-02-27T14:21:15Z","isPatch":true,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"kevin brintnall venit, vidit, dixit 26.02.2009 21:11:\n> This allows things like 'git -C /somewhere pull' without specifying both\n> --work-tree and --git-dir.\n> \n\nI'm afraid you hit a somewhat reoccurring issue here. chdir'ing looks\nsimple, but it has many side effects, such as on relative paths (as\nJunio pointed out) but also on the order of parsing config. Having -C as\na global git option would require dealing with all of these effects.\n\nOn the other hand, a shell function can achieve the same very easily.\nThe side effects are left to be dealt with by the user then ;)\n\nMichael\n"}]}