git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
@ 2005-12-24 12:20 Eric Wong
  2005-12-24 18:08 ` Junio C Hamano
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Wong @ 2005-12-24 12:20 UTC (permalink / raw)
  To: git list

strcpy_user_path(dest,src) acts just like strcpy() except tildes
in src are expanded appropriately to point to a user's home
directory.

The following expansions are supported:

~user/path	reads home directory from getpwnam()
~/path	 	reads home directory from $HOME env,
		or getpwuid() if $HOME is not set

This patch makes it easier for an ordinary user to share compiled
binaries across different machines, especially if they have different
account names and home directories on those machines.

Hint: build git with: make 'prefix=~'

Signed-off-by: Eric Wong <normalperson@yhbt.net>

---

 cache.h   |    1 +
 git.c     |    6 ++++++
 init-db.c |    3 ++-
 path.c    |   41 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 1 deletions(-)

c46a17b746572e6d16aa026de50d7911eddf8664
diff --git a/cache.h b/cache.h
index c5ff4b7..041bf94 100644
--- a/cache.h
+++ b/cache.h
@@ -184,6 +184,7 @@ extern const unsigned char null_sha1[20]
 
 int git_mkstemp(char *path, size_t n, const char *template);
 
+char * strcpy_user_path(char *dest, const char *path);
 int safe_create_leading_directories(char *path);
 char *safe_strncpy(char *, const char *, size_t);
 char *enter_repo(char *path, int strict);
diff --git a/git.c b/git.c
index 434a3d9..15f4c69 100644
--- a/git.c
+++ b/git.c
@@ -12,6 +12,8 @@
 #include <termios.h>
 #include "git-compat-util.h"
 
+extern char * strcpy_user_path(char *dest, const char *path);
+
 #ifndef PATH_MAX
 # define PATH_MAX 4096
 #endif
@@ -234,6 +236,7 @@ int main(int argc, char **argv, char **e
 {
 	char git_command[PATH_MAX + 1];
 	char wd[PATH_MAX + 1];
+	char tmp[PATH_MAX + 1];
 	int i, len, show_help = 0;
 	char *exec_path = getenv("GIT_EXEC_PATH");
 
@@ -269,6 +272,9 @@ int main(int argc, char **argv, char **e
 			cmd_usage(NULL, NULL);
 	}
 
+	strcpy_user_path(tmp, exec_path);
+	exec_path = tmp;
+
 	if (i >= argc || show_help) {
 		if (i >= argc)
 			cmd_usage(exec_path, NULL);
diff --git a/init-db.c b/init-db.c
index ead37b5..468c93a 100644
--- a/init-db.c
+++ b/init-db.c
@@ -119,7 +119,8 @@ static void copy_templates(const char *g
 
 	if (!template_dir)
 		template_dir = DEFAULT_GIT_TEMPLATE_DIR;
-	strcpy(template_path, template_dir);
+	strcpy_user_path(template_path, template_dir);
+	
 	template_len = strlen(template_path);
 	if (template_path[template_len-1] != '/') {
 		template_path[template_len++] = '/';
diff --git a/path.c b/path.c
index 334b2bd..3caa188 100644
--- a/path.c
+++ b/path.c
@@ -131,6 +131,47 @@ int validate_symref(const char *path)
 	return -1;
 }
 
+char * strcpy_user_path(char *dest, const char *path)
+{
+	if (path[0] == '~') {
+		struct passwd *pw;
+
+		if (path[1] == '/' || path[1] == '\0') {
+			char *home;
+			
+			if (!(home = getenv("HOME"))) {
+				pw = getpwuid(getuid());
+				if (pw && pw->pw_dir)
+					home = pw->pw_dir;
+			}
+				
+			strcpy(dest, home);
+			
+			if (path[1] == '/')
+				strcat(dest, path + 1);
+		} else {
+			char *slash;
+			
+			if ((slash = strchr(path,'/'))) {
+				memcpy(pathname, path + 1, slash - path - 1);
+				pathname[slash - path] = '\0';
+				pw = getpwnam(pathname);
+			} else
+				pw = getpwnam(path + 1);
+
+			if (pw && pw->pw_dir) {
+				strcpy(dest, pw->pw_dir);
+				if (slash)
+					strcat(dest,slash);
+			} else
+				strcpy(dest, path);
+		}
+	} else
+		strcpy(dest, path);
+
+	return dest;
+}
+
 static char *user_path(char *buf, char *path, int sz)
 {
 	struct passwd *pw;
-- 
1.0.GIT

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

* Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
  2005-12-24 12:20 [PATCH] add strcpy_user_path() and use it in init-db.c and git.c Eric Wong
@ 2005-12-24 18:08 ` Junio C Hamano
  2005-12-24 19:50   ` Eric Wong
  0 siblings, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2005-12-24 18:08 UTC (permalink / raw)
  To: Eric Wong; +Cc: git list

Eric Wong <normalperson@yhbt.net> writes:

> Hint: build git with: make 'prefix=~'

Sorry, I do not see why you would want to do this.  I understand
"make prefix=~" or "make prefix=$HOME", but "make prefix='~'"
and expanding tilde and friends at runtime you need to justify
why it helps in which situation.

We are not DOS and do not do argument expansion shell should
have done for us ourselves.

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

* Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
  2005-12-24 18:08 ` Junio C Hamano
@ 2005-12-24 19:50   ` Eric Wong
  2005-12-24 21:07     ` Junio C Hamano
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Wong @ 2005-12-24 19:50 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git list

Junio C Hamano <junkio@cox.net> wrote:
> Eric Wong <normalperson@yhbt.net> writes:
> 
> > Hint: build git with: make 'prefix=~'
> 
> Sorry, I do not see why you would want to do this.  I understand
> "make prefix=~" or "make prefix=$HOME", but "make prefix='~'"
> and expanding tilde and friends at runtime you need to justify
> why it helps in which situation.
> 
> We are not DOS and do not do argument expansion shell should
> have done for us ourselves.

My home directories have different names on different machines I'm
on, and I want to avoid having to recompile git for each one.
I don't have root access to some of them, so installing globally in /usr
or /usr/local isn't an option, either.

-- 
Eric Wong

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

* Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
  2005-12-24 19:50   ` Eric Wong
@ 2005-12-24 21:07     ` Junio C Hamano
  2005-12-24 21:19       ` Eric Wong
  0 siblings, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2005-12-24 21:07 UTC (permalink / raw)
  To: Eric Wong; +Cc: git list

Eric Wong <normalperson@yhbt.net> writes:

> My home directories have different names on different machines I'm
> on, and I want to avoid having to recompile git for each one.
> I don't have root access to some of them, so installing globally in /usr
> or /usr/local isn't an option, either.

Then you probably need to use GIT_EXEC_PATH environment
variable.

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

* Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
  2005-12-24 21:07     ` Junio C Hamano
@ 2005-12-24 21:19       ` Eric Wong
  2005-12-26 16:40         ` Johannes Schindelin
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Wong @ 2005-12-24 21:19 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git list

Junio C Hamano <junkio@cox.net> wrote:
> Eric Wong <normalperson@yhbt.net> writes:
> 
> > My home directories have different names on different machines I'm
> > on, and I want to avoid having to recompile git for each one.
> > I don't have root access to some of them, so installing globally in /usr
> > or /usr/local isn't an option, either.
> 
> Then you probably need to use GIT_EXEC_PATH environment
> variable.

That works with git.c but not init-db.  But then again I don't use
git-init-db that often.  I'll just write a shell script wrapper for the
latter if I do.

No need for this patch anymore :)

-- 
Eric Wong

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

* Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
  2005-12-24 21:19       ` Eric Wong
@ 2005-12-26 16:40         ` Johannes Schindelin
  2005-12-30 23:10           ` Eric Wong
  0 siblings, 1 reply; 8+ messages in thread
From: Johannes Schindelin @ 2005-12-26 16:40 UTC (permalink / raw)
  To: Eric Wong; +Cc: Junio C Hamano, git list

Hi,

On Sat, 24 Dec 2005, Eric Wong wrote:

> Junio C Hamano <junkio@cox.net> wrote:
> > Eric Wong <normalperson@yhbt.net> writes:
> > 
> > > My home directories have different names on different machines I'm
> > > on, and I want to avoid having to recompile git for each one.
> > > I don't have root access to some of them, so installing globally in /usr
> > > or /usr/local isn't an option, either.
> > 
> > Then you probably need to use GIT_EXEC_PATH environment
> > variable.
> 
> That works with git.c but not init-db.  But then again I don't use
> git-init-db that often.  I'll just write a shell script wrapper for the
> latter if I do.

How about something like this?

---
[PATCH] Introduce environment variable for the path to the templates

The environment variable GIT_TEMPLATE_PATH can override the compiled-in
setting, and can be overridden with the '--template=' argument to init-db.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

	Eric said that GIT_EXEC_PATH is enough for most things, but not for
	init-db. I guess he wanted something like this.

 init-db.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

5d95ce750b09a14bcb86e07ba23077ab0825089c
diff --git a/init-db.c b/init-db.c
index 863ec1a..774a91f 100644
--- a/init-db.c
+++ b/init-db.c
@@ -238,7 +238,7 @@ int main(int argc, char **argv)
 {
 	const char *git_dir;
 	const char *sha1_dir;
-	char *path, *template_dir = NULL;
+	char *path, *template_dir = getenv("GIT_TEMPLATE_PATH");
 	int len, i;
 
 	for (i = 1; i < argc; i++, argv++) {
-- 
1.0.GIT

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

* Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
@ 2005-12-26 17:42 Johannes Schindelin
  0 siblings, 0 replies; 8+ messages in thread
From: Johannes Schindelin @ 2005-12-26 17:42 UTC (permalink / raw)
  To: Eric Wong; +Cc: Junio C Hamano, git list

[Sorry, this mail will probably be sent to you twice. The mailserver I 
use normally is kinda broken. It sends out the mails one or two days late, 
so paradoxically, the resent mail will arrive earlier than the original.]

Hi,

On Sat, 24 Dec 2005, Eric Wong wrote:

> Junio C Hamano <junkio@cox.net> wrote:
> > Eric Wong <normalperson@yhbt.net> writes:
> > 
> > > My home directories have different names on different machines I'm
> > > on, and I want to avoid having to recompile git for each one.
> > > I don't have root access to some of them, so installing globally in /usr
> > > or /usr/local isn't an option, either.
> > 
> > Then you probably need to use GIT_EXEC_PATH environment
> > variable.
> 
> That works with git.c but not init-db.  But then again I don't use
> git-init-db that often.  I'll just write a shell script wrapper for the
> latter if I do.

How about something like this?

---
[PATCH] Introduce environment variable for the path to the templates

The environment variable GIT_TEMPLATE_PATH can override the compiled-in
setting, and can be overridden with the '--template=' argument to init-db.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

	Eric said that GIT_EXEC_PATH is enough for most things, but not for
	init-db. I guess he wanted something like this.

 init-db.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

5d95ce750b09a14bcb86e07ba23077ab0825089c
diff --git a/init-db.c b/init-db.c
index 863ec1a..774a91f 100644
--- a/init-db.c
+++ b/init-db.c
@@ -238,7 +238,7 @@ int main(int argc, char **argv)
 {
 	const char *git_dir;
 	const char *sha1_dir;
-	char *path, *template_dir = NULL;
+	char *path, *template_dir = getenv("GIT_TEMPLATE_PATH");
 	int len, i;
 
 	for (i = 1; i < argc; i++, argv++) {
-- 
1.0.GIT

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

* Re: [PATCH] add strcpy_user_path() and use it in init-db.c and git.c
  2005-12-26 16:40         ` Johannes Schindelin
@ 2005-12-30 23:10           ` Eric Wong
  0 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2005-12-30 23:10 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Junio C Hamano, git list

Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> Hi,
> 
> On Sat, 24 Dec 2005, Eric Wong wrote:
> 
> > Junio C Hamano <junkio@cox.net> wrote:
> > > Eric Wong <normalperson@yhbt.net> writes:
> > > 
> > > > My home directories have different names on different machines I'm
> > > > on, and I want to avoid having to recompile git for each one.
> > > > I don't have root access to some of them, so installing globally in /usr
> > > > or /usr/local isn't an option, either.
> > > 
> > > Then you probably need to use GIT_EXEC_PATH environment
> > > variable.
> > 
> > That works with git.c but not init-db.  But then again I don't use
> > git-init-db that often.  I'll just write a shell script wrapper for the
> > latter if I do.
> 
> How about something like this?

Sure, seems reasonable, but I don't feel that strongly towards
making my env getting bigger and bigger all the time.

I've also been comtemplating just using my own wrapper instead of the
default 'git' wrapper (which being in C, is more difficult to customize
on-the-fly than sh or perl).  Or have git read a config file from a
user's home directory.

> ---
> [PATCH] Introduce environment variable for the path to the templates
> 
> The environment variable GIT_TEMPLATE_PATH can override the compiled-in
> setting, and can be overridden with the '--template=' argument to init-db.
> 
> Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> 
> ---
> 
> 	Eric said that GIT_EXEC_PATH is enough for most things, but not for
> 	init-db. I guess he wanted something like this.
> 
>  init-db.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> 5d95ce750b09a14bcb86e07ba23077ab0825089c
> diff --git a/init-db.c b/init-db.c
> index 863ec1a..774a91f 100644
> --- a/init-db.c
> +++ b/init-db.c
> @@ -238,7 +238,7 @@ int main(int argc, char **argv)
>  {
>  	const char *git_dir;
>  	const char *sha1_dir;
> -	char *path, *template_dir = NULL;
> +	char *path, *template_dir = getenv("GIT_TEMPLATE_PATH");
>  	int len, i;
>  
>  	for (i = 1; i < argc; i++, argv++) {

-- 
Eric Wong

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

end of thread, other threads:[~2005-12-30 23:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-12-24 12:20 [PATCH] add strcpy_user_path() and use it in init-db.c and git.c Eric Wong
2005-12-24 18:08 ` Junio C Hamano
2005-12-24 19:50   ` Eric Wong
2005-12-24 21:07     ` Junio C Hamano
2005-12-24 21:19       ` Eric Wong
2005-12-26 16:40         ` Johannes Schindelin
2005-12-30 23:10           ` Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2005-12-26 17:42 Johannes Schindelin

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).