{"thread":{"id":"2665","subject":"[RFC/PATCH 1/3] git-find-git: a new helper.","startedAt":"2005-11-24T03:36:22Z","lastAt":"2005-12-01T20:33:52Z","messageCount":5,"participants":["Junio C Hamano","Marco Costalba","Josef Weidendorfer"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"12661","messageId":"7vmzjuzphl.fsf@assigned-by-dhcp.cox.net","threadId":"2665","inReplyTo":null,"subject":"[RFC/PATCH 1/3] git-find-git: a new helper.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-11-24T03:36:22Z","receivedAt":"2005-11-24T03:36:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This is part of rev-format check work, which is currently my top\npriority that is holding up any new 1.0rc updates to maint\nbranch.  It adds a new helper, git-find-git command, to the\nsuite, and rewrites git-sh-setup to use it.\n\nIt adds setup_git_directory_gently(), which is a variant of\nexisting setup_git_directory() but does not die() if we are not\nin a git managed repository.  git-find-git uses it to find the\ntop-level, and if it was run from a subdirectory, outputs shell\nscript fragments that can be evaled to define the two variables:\n\n    GIT_DIR_PREFIX is a string to be prepended to cwd relative\n    paths to make them repository relative.\n\n    GIT_DIR_UPLEVEL is a string (e.g. \"../\") for the command to\n    use to cd to the top-level of repository.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n\n Makefile        |    2 +-\n cache.h         |    1 +\n find-git.c      |   41 +++++++++++++++++++++++++++++++++++++++++\n git-sh-setup.sh |   39 ++++++++++++++++++++++++++++-----------\n setup.c         |   14 ++++++++++++--\n 5 files changed, 83 insertions(+), 14 deletions(-)\n create mode 100644 find-git.c\n\napplies-to: 10227b43e837a38d95d472a0b7c400749fbd46e9\n15d4e27b74f4ff0c9faa5bbbfe8e915398f3be97\ndiff --git a/Makefile b/Makefile\nindex a97a5d9..dda363a 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -110,7 +110,7 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)\n # The ones that do not have to link with lcrypto nor lz.\n SIMPLE_PROGRAMS = \\\n \tgit-get-tar-commit-id$X git-mailinfo$X git-mailsplit$X \\\n-\tgit-stripspace$X git-daemon$X\n+\tgit-stripspace$X git-daemon$X git-find-git$X\n \n # ... and all the rest\n PROGRAMS = \\\ndiff --git a/cache.h b/cache.h\nindex 6ac94c5..3104c59 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -148,6 +148,7 @@ extern char *get_graft_file(void);\n \n extern const char **get_pathspec(const char *prefix, const char **pathspec);\n extern const char *setup_git_directory(void);\n+extern const char *setup_git_directory_gently(int *);\n extern const char *prefix_path(const char *prefix, int len, const char *path);\n \n #define alloc_nr(x) (((x)+16)*3/2)\ndiff --git a/find-git.c b/find-git.c\nnew file mode 100644\nindex 0000000..e063425\n--- /dev/null\n+++ b/find-git.c\n@@ -0,0 +1,41 @@\n+#include \"cache.h\"\n+#include \"quote.h\"\n+\n+int main(int ac, char **av)\n+{\n+\tint not_a_git = 0;\n+\tconst char *prefix = setup_git_directory_gently(&not_a_git);\n+\n+\tif (!prefix && not_a_git)\n+\t\texit(1);\n+\n+\t/* This can die with malformed configuration file --\n+\t * exit code from die() is 128.\n+\t */\n+\tgit_config(git_default_config);\n+\n+\t/* Later we check repository version and exit with non-zero\n+\t * status after issuing an error message here.\n+\t */\n+\n+\tif (!prefix)\n+\t\t/* we are at the project toplevel or GIT_DIR is set.\n+\t\t * either case we do not have to muck with the\n+\t\t * environment further.\n+\t\t */\n+\t\texit(0);\n+\n+\t/* this leaks but we do not care ;-) */\n+\tprintf(\"GIT_DIR_PREFIX=%s\\n\", sq_quote(prefix));\n+\tprintf(\"GIT_DIR_UPLEVEL='\");\n+\twhile (prefix) {\n+\t\tprefix = strchr(prefix, '/');\n+\t\tif (prefix) {\n+\t\t\tprefix++;\n+\t\t\tprintf(\"../\");\n+\t\t}\n+\t}\n+\tprintf(\"'\\n\");\n+\tprintf(\"export GIT_DIR_PREFIX GIT_DIR_UPLEVEL\\n\");\n+\texit(0);\n+}\ndiff --git a/git-sh-setup.sh b/git-sh-setup.sh\nindex dbb9884..8e30bf6 100755\n--- a/git-sh-setup.sh\n+++ b/git-sh-setup.sh\n@@ -3,13 +3,11 @@\n # Set up GIT_DIR and GIT_OBJECT_DIRECTORY\n # and return true if everything looks ok\n #\n-: ${GIT_DIR=.git}\n-: ${GIT_OBJECT_DIRECTORY=\"$GIT_DIR/objects\"}\n \n # Having this variable in your environment would break scripts because\n-# you would cause \"cd\" to be be taken to unexpected places.  If you\n-# like CDPATH, define it for your interactive shell sessions without\n-# exporting it.\n+# you would cause \"cd\" to be taken to unexpected places.  If you line\n+# CDPATH, define it for your interactive shell sessions without exporting\n+# it.\n unset CDPATH\n \n die() {\n@@ -17,9 +15,28 @@ die() {\n \texit 1\n }\n \n-case \"$(GIT_DIR=\"$GIT_DIR\" git-symbolic-ref HEAD 2>/dev/null)\" in\n-refs/*)\t: ;;\n-*)\tfalse ;;\n-esac &&\n-[ -d \"$GIT_DIR/refs\" ] &&\n-[ -d \"$GIT_OBJECT_DIRECTORY/\" ]\n+eval \"`git-find-git`\"\n+case \"$?\" in\n+0)\n+\t: ${GIT_DIR=.git}\n+\t: ${GIT_OBJECT_DIRECTORY=\"$GIT_DIR/objects\"}\n+\n+\tcase \"$(GIT_DIR=\"$GIT_DIR\" git-symbolic-ref HEAD 2>/dev/null)\" in\n+\trefs/*)\t: ;;\n+\t*)\tfalse ;;\n+\tesac &&\n+\t[ -d \"$GIT_DIR/refs\" ] &&\n+\t[ -d \"$GIT_OBJECT_DIRECTORY/\" ]\n+\t;;\n+*)\n+\t# (1) we did not find a git directory.  This is sometimes OK.\n+\t# ls-remote (and parse-remote called from it) uses sh-set to\n+\t# pick up remotes shortcut if available, but outside git the\n+\t# user would want to use the command with explicitly spelled\n+\t# out URL.\n+\n+\t# (128) git-find-git died -- malformed configuration and that\n+\t# is really fatal.  We have already given the error message.\n+\n+\t;;\n+esac\ndiff --git a/setup.c b/setup.c\nindex c487d7e..f64ca23 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -92,7 +92,7 @@ static int is_toplevel_directory(void)\n \treturn 1;\n }\n \n-const char *setup_git_directory(void)\n+const char *setup_git_directory_gently(int *just_fail_on_ungit)\n {\n \tstatic char cwd[PATH_MAX+1];\n \tint len, offset;\n@@ -113,8 +113,13 @@ const char *setup_git_directory(void)\n \t\t\tbreak;\n \t\tchdir(\"..\");\n \t\tdo {\n-\t\t\tif (!offset)\n+\t\t\tif (!offset) {\n+\t\t\t\tif (just_fail_on_ungit) {\n+\t\t\t\t\t*just_fail_on_ungit = 1;\n+\t\t\t\t\treturn NULL;\n+\t\t\t\t}\n \t\t\t\tdie(\"Not a git repository\");\n+\t\t\t}\n \t\t} while (cwd[--offset] != '/');\n \t}\n \n@@ -127,3 +132,8 @@ const char *setup_git_directory(void)\n \tcwd[len] = 0;\n \treturn cwd + offset;\n }\n+\n+const char *setup_git_directory(void)\n+{\n+\treturn setup_git_directory_gently(NULL);\n+}\n---\n0.99.9.GIT\n"},{"id":"12666","messageId":"4385528D.7010203@yahoo.it","threadId":"2665","inReplyTo":"7vmzjuzphl.fsf@assigned-by-dhcp.cox.net","subject":"Re: [RFC/PATCH 1/3] git-find-git: a new helper.","fromName":"Marco Costalba","fromEmail":"mcostalba@yahoo.it","sentAt":"2005-11-24T05:41:33Z","receivedAt":"2005-11-24T05:41:33Z","isPatch":true,"sender":{"key":"mcostalba@gmail.com","avatar":null},"body":"Junio C Hamano wrote:\n\n>  git-find-git uses it to find the\n> top-level, and if it was run from a subdirectory, outputs shell\n> script fragments that can be evaled to define the two variables:\n> \n>     GIT_DIR_PREFIX is a string to be prepended to cwd relative\n>     paths to make them repository relative.\n> \n>     GIT_DIR_UPLEVEL is a string (e.g. \"../\") for the command to\n>     use to cd to the top-level of repository.\n> \n>\nSounds very good.\n\nI did't had the time to test it but I think I can now\neasily update qgit to run from a subdirectory with this new helper.\n\nMarco\n\n\t\n\n\t\n\t\t\n___________________________________ \nYahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB \nhttp://mail.yahoo.it\n"},{"id":"12668","messageId":"7viruiy3ag.fsf@assigned-by-dhcp.cox.net","threadId":"2665","inReplyTo":"4385528D.7010203@yahoo.it","subject":"Re: [RFC/PATCH 1/3] git-find-git: a new helper.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-11-24T06:21:11Z","receivedAt":"2005-11-24T06:21:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Marco Costalba <mcostalba@yahoo.it> writes:\n\n> Junio C Hamano wrote:\n>\n>>  git-find-git uses it to find the\n>> top-level, and if it was run from a subdirectory, outputs shell\n>> script fragments that can be evaled to define the two variables:\n>>     GIT_DIR_PREFIX is a string to be prepended to cwd relative\n>>     paths to make them repository relative.\n>>     GIT_DIR_UPLEVEL is a string (e.g. \"../\") for the command to\n>>     use to cd to the top-level of repository.\n>>\n> Sounds very good.\n>\n> I did't had the time to test it but I think I can now\n> easily update qgit to run from a subdirectory with this new helper.\n\nThe helper might be good, but it turns out that the idea of\nusing it from git-sh-setup was not that great, so please\ndisregard the third part of the patch.  I think I need a bit\nmore thinking about this.\n"},{"id":"13049","messageId":"200512011535.25022.Josef.Weidendorfer@gmx.de","threadId":"2665","inReplyTo":"7vmzjuzphl.fsf@assigned-by-dhcp.cox.net","subject":"Re: [RFC/PATCH 1/3] git-find-git: a new helper.","fromName":"Josef Weidendorfer","fromEmail":"josef.weidendorfer@gmx.de","sentAt":"2005-12-01T14:35:24Z","receivedAt":"2005-12-01T14:35:24Z","isPatch":true,"sender":{"key":"josef.weidendorfer@gmx.de","avatar":null},"body":"On Thursday 24 November 2005 04:36, you wrote:\n>     GIT_DIR_PREFIX is a string to be prepended to cwd relative\n>     paths to make them repository relative.\n> \n>     GIT_DIR_UPLEVEL is a string (e.g. \"../\") for the command to\n>     use to cd to the top-level of repository.\n\nHi,\n\ncan we revive this idea?\nI thought about how to make work\n\n $top/Documentation> git-mv ../file .\n\nand also detection of trying to move to the outside.\nRequirements IMHO:\n(1) helper which is also useful from PERL\n(2) git-mv checks against version controlled files via git-ls-files.\nThus, I need to run git-ls-files from the toplevel, to be able to\nsee $top/file. \n(3) translation of cwd relative into toplevel relative paths.\n\nOr is there already a way to do this with current git?\n\nJosef\n"},{"id":"13059","messageId":"200512012133.53030.Josef.Weidendorfer@gmx.de","threadId":"2665","inReplyTo":"7vr78wirj7.fsf@assigned-by-dhcp.cox.net","subject":"Re: [RFC/PATCH 1/3] git-find-git: a new helper.","fromName":"Josef Weidendorfer","fromEmail":"josef.weidendorfer@gmx.de","sentAt":"2005-12-01T20:33:52Z","receivedAt":"2005-12-01T20:33:52Z","isPatch":true,"sender":{"key":"josef.weidendorfer@gmx.de","avatar":null},"body":"On Thursday 01 December 2005 19:46, you wrote:\n> Josef Weidendorfer <Josef.Weidendorfer@gmx.de> writes:\n> \n> > can we revive this idea?\n> \n> I forgot git-rev-parse had --show-prefix and git-rev-parse --git-dir\n> already when I did that one.\n\nAh, thanks.\nBy the way, the documentation ofr --git-dir is missing.\n\nIt would be nice to have some docu about \"How to write a porcelain\", and\nthis should go under the section \"Making commands work in subdirs of\na working copy\"...\n\nJosef\n"}]}