{"thread":{"id":"2925","subject":"[PATCH] add strcpy_user_path() and use it in init-db.c and git.c","startedAt":"2005-12-24T12:20:16Z","lastAt":"2005-12-30T23:10:17Z","messageCount":7,"participants":["Eric Wong","Junio C Hamano","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"14011","messageId":"20051224122016.GD3963@mail.yhbt.net","threadId":"2925","inReplyTo":null,"subject":"[PATCH] add strcpy_user_path() and use it in init-db.c and git.c","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2005-12-24T12:20:16Z","receivedAt":"2005-12-24T12:20:16Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"strcpy_user_path(dest,src) acts just like strcpy() except tildes\nin src are expanded appropriately to point to a user's home\ndirectory.\n\nThe following expansions are supported:\n\n~user/path\treads home directory from getpwnam()\n~/path\t \treads home directory from $HOME env,\n\t\tor getpwuid() if $HOME is not set\n\nThis patch makes it easier for an ordinary user to share compiled\nbinaries across different machines, especially if they have different\naccount names and home directories on those machines.\n\nHint: build git with: make 'prefix=~'\n\nSigned-off-by: Eric Wong <normalperson@yhbt.net>\n\n---\n\n cache.h   |    1 +\n git.c     |    6 ++++++\n init-db.c |    3 ++-\n path.c    |   41 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 50 insertions(+), 1 deletions(-)\n\nc46a17b746572e6d16aa026de50d7911eddf8664\ndiff --git a/cache.h b/cache.h\nindex c5ff4b7..041bf94 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -184,6 +184,7 @@ extern const unsigned char null_sha1[20]\n \n int git_mkstemp(char *path, size_t n, const char *template);\n \n+char * strcpy_user_path(char *dest, const char *path);\n int safe_create_leading_directories(char *path);\n char *safe_strncpy(char *, const char *, size_t);\n char *enter_repo(char *path, int strict);\ndiff --git a/git.c b/git.c\nindex 434a3d9..15f4c69 100644\n--- a/git.c\n+++ b/git.c\n@@ -12,6 +12,8 @@\n #include <termios.h>\n #include \"git-compat-util.h\"\n \n+extern char * strcpy_user_path(char *dest, const char *path);\n+\n #ifndef PATH_MAX\n # define PATH_MAX 4096\n #endif\n@@ -234,6 +236,7 @@ int main(int argc, char **argv, char **e\n {\n \tchar git_command[PATH_MAX + 1];\n \tchar wd[PATH_MAX + 1];\n+\tchar tmp[PATH_MAX + 1];\n \tint i, len, show_help = 0;\n \tchar *exec_path = getenv(\"GIT_EXEC_PATH\");\n \n@@ -269,6 +272,9 @@ int main(int argc, char **argv, char **e\n \t\t\tcmd_usage(NULL, NULL);\n \t}\n \n+\tstrcpy_user_path(tmp, exec_path);\n+\texec_path = tmp;\n+\n \tif (i >= argc || show_help) {\n \t\tif (i >= argc)\n \t\t\tcmd_usage(exec_path, NULL);\ndiff --git a/init-db.c b/init-db.c\nindex ead37b5..468c93a 100644\n--- a/init-db.c\n+++ b/init-db.c\n@@ -119,7 +119,8 @@ static void copy_templates(const char *g\n \n \tif (!template_dir)\n \t\ttemplate_dir = DEFAULT_GIT_TEMPLATE_DIR;\n-\tstrcpy(template_path, template_dir);\n+\tstrcpy_user_path(template_path, template_dir);\n+\t\n \ttemplate_len = strlen(template_path);\n \tif (template_path[template_len-1] != '/') {\n \t\ttemplate_path[template_len++] = '/';\ndiff --git a/path.c b/path.c\nindex 334b2bd..3caa188 100644\n--- a/path.c\n+++ b/path.c\n@@ -131,6 +131,47 @@ int validate_symref(const char *path)\n \treturn -1;\n }\n \n+char * strcpy_user_path(char *dest, const char *path)\n+{\n+\tif (path[0] == '~') {\n+\t\tstruct passwd *pw;\n+\n+\t\tif (path[1] == '/' || path[1] == '\\0') {\n+\t\t\tchar *home;\n+\t\t\t\n+\t\t\tif (!(home = getenv(\"HOME\"))) {\n+\t\t\t\tpw = getpwuid(getuid());\n+\t\t\t\tif (pw && pw->pw_dir)\n+\t\t\t\t\thome = pw->pw_dir;\n+\t\t\t}\n+\t\t\t\t\n+\t\t\tstrcpy(dest, home);\n+\t\t\t\n+\t\t\tif (path[1] == '/')\n+\t\t\t\tstrcat(dest, path + 1);\n+\t\t} else {\n+\t\t\tchar *slash;\n+\t\t\t\n+\t\t\tif ((slash = strchr(path,'/'))) {\n+\t\t\t\tmemcpy(pathname, path + 1, slash - path - 1);\n+\t\t\t\tpathname[slash - path] = '\\0';\n+\t\t\t\tpw = getpwnam(pathname);\n+\t\t\t} else\n+\t\t\t\tpw = getpwnam(path + 1);\n+\n+\t\t\tif (pw && pw->pw_dir) {\n+\t\t\t\tstrcpy(dest, pw->pw_dir);\n+\t\t\t\tif (slash)\n+\t\t\t\t\tstrcat(dest,slash);\n+\t\t\t} else\n+\t\t\t\tstrcpy(dest, path);\n+\t\t}\n+\t} else\n+\t\tstrcpy(dest, path);\n+\n+\treturn dest;\n+}\n+\n static char *user_path(char *buf, char *path, int sz)\n {\n \tstruct passwd *pw;\n-- \n1.0.GIT\n"},{"id":"14017","messageId":"7virtes6zd.fsf@assigned-by-dhcp.cox.net","threadId":"2925","inReplyTo":"20051224122016.GD3963@mail.yhbt.net","subject":"Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-12-24T18:08:38Z","receivedAt":"2005-12-24T18:08:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Wong <normalperson@yhbt.net> writes:\n\n> Hint: build git with: make 'prefix=~'\n\nSorry, I do not see why you would want to do this.  I understand\n\"make prefix=~\" or \"make prefix=$HOME\", but \"make prefix='~'\"\nand expanding tilde and friends at runtime you need to justify\nwhy it helps in which situation.\n\nWe are not DOS and do not do argument expansion shell should\nhave done for us ourselves.\n"},{"id":"14020","messageId":"20051224195033.GE3963@mail.yhbt.net","threadId":"2925","inReplyTo":"7virtes6zd.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2005-12-24T19:50:33Z","receivedAt":"2005-12-24T19:50:33Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"Junio C Hamano <junkio@cox.net> wrote:\n> Eric Wong <normalperson@yhbt.net> writes:\n> \n> > Hint: build git with: make 'prefix=~'\n> \n> Sorry, I do not see why you would want to do this.  I understand\n> \"make prefix=~\" or \"make prefix=$HOME\", but \"make prefix='~'\"\n> and expanding tilde and friends at runtime you need to justify\n> why it helps in which situation.\n> \n> We are not DOS and do not do argument expansion shell should\n> have done for us ourselves.\n\nMy home directories have different names on different machines I'm\non, and I want to avoid having to recompile git for each one.\nI don't have root access to some of them, so installing globally in /usr\nor /usr/local isn't an option, either.\n\n-- \nEric Wong\n"},{"id":"14022","messageId":"7vu0cyqk5d.fsf@assigned-by-dhcp.cox.net","threadId":"2925","inReplyTo":"20051224195033.GE3963@mail.yhbt.net","subject":"Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-12-24T21:07:10Z","receivedAt":"2005-12-24T21:07:10Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Wong <normalperson@yhbt.net> writes:\n\n> My home directories have different names on different machines I'm\n> on, and I want to avoid having to recompile git for each one.\n> I don't have root access to some of them, so installing globally in /usr\n> or /usr/local isn't an option, either.\n\nThen you probably need to use GIT_EXEC_PATH environment\nvariable.\n"},{"id":"14025","messageId":"20051224211925.GH3963@mail.yhbt.net","threadId":"2925","inReplyTo":"7vu0cyqk5d.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2005-12-24T21:19:25Z","receivedAt":"2005-12-24T21:19:25Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"Junio C Hamano <junkio@cox.net> wrote:\n> Eric Wong <normalperson@yhbt.net> writes:\n> \n> > My home directories have different names on different machines I'm\n> > on, and I want to avoid having to recompile git for each one.\n> > I don't have root access to some of them, so installing globally in /usr\n> > or /usr/local isn't an option, either.\n> \n> Then you probably need to use GIT_EXEC_PATH environment\n> variable.\n\nThat works with git.c but not init-db.  But then again I don't use\ngit-init-db that often.  I'll just write a shell script wrapper for the\nlatter if I do.\n\nNo need for this patch anymore :)\n\n-- \nEric Wong\n"},{"id":"14042","messageId":"Pine.LNX.4.63.0512261730170.7716@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"2925","inReplyTo":"20051224211925.GH3963@mail.yhbt.net","subject":"Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2005-12-26T16:40:09Z","receivedAt":"2005-12-26T16:40:09Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sat, 24 Dec 2005, Eric Wong wrote:\n\n> Junio C Hamano <junkio@cox.net> wrote:\n> > Eric Wong <normalperson@yhbt.net> writes:\n> > \n> > > My home directories have different names on different machines I'm\n> > > on, and I want to avoid having to recompile git for each one.\n> > > I don't have root access to some of them, so installing globally in /usr\n> > > or /usr/local isn't an option, either.\n> > \n> > Then you probably need to use GIT_EXEC_PATH environment\n> > variable.\n> \n> That works with git.c but not init-db.  But then again I don't use\n> git-init-db that often.  I'll just write a shell script wrapper for the\n> latter if I do.\n\nHow about something like this?\n\n---\n[PATCH] Introduce environment variable for the path to the templates\n\nThe environment variable GIT_TEMPLATE_PATH can override the compiled-in\nsetting, and can be overridden with the '--template=' argument to init-db.\n\nSigned-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n\n---\n\n\tEric said that GIT_EXEC_PATH is enough for most things, but not for\n\tinit-db. I guess he wanted something like this.\n\n init-db.c |    2 +-\n 1 files changed, 1 insertions(+), 1 deletions(-)\n\n5d95ce750b09a14bcb86e07ba23077ab0825089c\ndiff --git a/init-db.c b/init-db.c\nindex 863ec1a..774a91f 100644\n--- a/init-db.c\n+++ b/init-db.c\n@@ -238,7 +238,7 @@ int main(int argc, char **argv)\n {\n \tconst char *git_dir;\n \tconst char *sha1_dir;\n-\tchar *path, *template_dir = NULL;\n+\tchar *path, *template_dir = getenv(\"GIT_TEMPLATE_PATH\");\n \tint len, i;\n \n \tfor (i = 1; i < argc; i++, argv++) {\n-- \n1.0.GIT\n"},{"id":"14108","messageId":"20051230231017.GB7165@mail.yhbt.net","threadId":"2925","inReplyTo":"Pine.LNX.4.63.0512261730170.7716@wbgn013.biozentrum.uni-wuerzburg.de","subject":"Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2005-12-30T23:10:17Z","receivedAt":"2005-12-30T23:10:17Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:\n> Hi,\n> \n> On Sat, 24 Dec 2005, Eric Wong wrote:\n> \n> > Junio C Hamano <junkio@cox.net> wrote:\n> > > Eric Wong <normalperson@yhbt.net> writes:\n> > > \n> > > > My home directories have different names on different machines I'm\n> > > > on, and I want to avoid having to recompile git for each one.\n> > > > I don't have root access to some of them, so installing globally in /usr\n> > > > or /usr/local isn't an option, either.\n> > > \n> > > Then you probably need to use GIT_EXEC_PATH environment\n> > > variable.\n> > \n> > That works with git.c but not init-db.  But then again I don't use\n> > git-init-db that often.  I'll just write a shell script wrapper for the\n> > latter if I do.\n> \n> How about something like this?\n\nSure, seems reasonable, but I don't feel that strongly towards\nmaking my env getting bigger and bigger all the time.\n\nI've also been comtemplating just using my own wrapper instead of the\ndefault 'git' wrapper (which being in C, is more difficult to customize\non-the-fly than sh or perl).  Or have git read a config file from a\nuser's home directory.\n\n> ---\n> [PATCH] Introduce environment variable for the path to the templates\n> \n> The environment variable GIT_TEMPLATE_PATH can override the compiled-in\n> setting, and can be overridden with the '--template=' argument to init-db.\n> \n> Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n> \n> ---\n> \n> \tEric said that GIT_EXEC_PATH is enough for most things, but not for\n> \tinit-db. I guess he wanted something like this.\n> \n>  init-db.c |    2 +-\n>  1 files changed, 1 insertions(+), 1 deletions(-)\n> \n> 5d95ce750b09a14bcb86e07ba23077ab0825089c\n> diff --git a/init-db.c b/init-db.c\n> index 863ec1a..774a91f 100644\n> --- a/init-db.c\n> +++ b/init-db.c\n> @@ -238,7 +238,7 @@ int main(int argc, char **argv)\n>  {\n>  \tconst char *git_dir;\n>  \tconst char *sha1_dir;\n> -\tchar *path, *template_dir = NULL;\n> +\tchar *path, *template_dir = getenv(\"GIT_TEMPLATE_PATH\");\n>  \tint len, i;\n>  \n>  \tfor (i = 1; i < argc; i++, argv++) {\n\n-- \nEric Wong\n"}]}