{"thread":{"id":"17235","subject":"[PATCH 3/7 v2] git_extract_argv0_path(): Move check for valid argv0 from caller to callee","startedAt":"2009-01-18T12:00:08Z","lastAt":"2009-01-19T17:41:05Z","messageCount":12,"participants":["Steffen Prohaska","Johannes Sixt","Junio C Hamano","Johannes Schindelin"],"isPatch":true,"patchVersion":2,"patchTotal":7},"messages":[{"id":"100952","messageId":"1232280015-8144-1-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":null,"subject":"[PATCH 0/7 v2] RUNTIME_PREFIX","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:08Z","receivedAt":"2009-01-18T12:00:08Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"This series is a replacement for\n\n   http://article.gmane.org/gmane.comp.version-control.git/105109\n\nimproved according to the comments by Dscho and Hannes.\n\n        Steffen\n\n\nSteffen Prohaska (6):\n  Move computation of absolute paths from Makefile to runtime (in\n    preparation for RUNTIME_PREFIX)\n  git_extract_argv0_path(): Move check for valid argv0 from caller to\n    callee\n  Add calls to git_extract_argv0_path() in programs that call\n    git_config_*\n  Modify setup_path() to only add git_exec_path() to PATH\n  Compute prefix at runtime if RUNTIME_PREFIX is set\n  Windows: Revert to default paths and convert them by RUNTIME_PREFIX\n\nSteve Haslam (1):\n  Refactor git_set_argv0_path() to git_extract_argv0_path()\n\n Makefile             |   49 +++++++++++++++++++------------\n builtin-help.c       |    4 +-\n daemon.c             |    2 +\n exec_cmd.c           |   77 ++++++++++++++++++++++++++++++++++++++++++++------\n exec_cmd.h           |    2 +-\n fast-import.c        |    3 ++\n git.c                |   18 ++---------\n hash-object.c        |    3 ++\n http-push.c          |    2 +\n imap-send.c          |    3 ++\n index-pack.c         |    3 ++\n merge-index.c        |    3 ++\n merge-tree.c         |    3 ++\n mktag.c              |    3 ++\n mktree.c             |    3 ++\n pack-redundant.c     |    3 ++\n patch-id.c           |    3 ++\n unpack-file.c        |    3 ++\n update-server-info.c |    3 ++\n upload-pack.c        |    2 +\n var.c                |    3 ++\n 21 files changed, 150 insertions(+), 45 deletions(-)\n"},{"id":"100954","messageId":"1232280015-8144-2-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":"1232280015-8144-1-git-send-email-prohaska@zib.de","subject":"[PATCH 1/7 v2] Move computation of absolute paths from Makefile to runtime (in preparation for RUNTIME_PREFIX)","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:09Z","receivedAt":"2009-01-18T12:00:09Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"This commit prepares the Makefile for relocatable binaries (called\nRUNTIME_PREFIX).  Such binaries will be able to be moved together\nwith the system configuration files to a different directory,\nrequiring to compute the prefix at runtime.\n\nIn a first step, we make all paths relative in the Makefile and\nteach system_path() to add the prefix instead.  We used to compute\nabsolute paths in the Makefile and passed them to C as defines.  We\nnow pass relative paths to C and call system_path() to add the\nprefix at runtime.\n\nSigned-off-by: Steffen Prohaska <prohaska@zib.de>\n---\n Makefile       |   42 ++++++++++++++++++++++++++----------------\n builtin-help.c |    4 ++--\n exec_cmd.c     |   12 ++++++++----\n 3 files changed, 36 insertions(+), 22 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex 2b873fa..1d2060a 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -179,28 +179,32 @@ STRIP ?= strip\n # Among the variables below, these:\n #   gitexecdir\n #   template_dir\n+#   mandir\n+#   infodir\n #   htmldir\n #   ETC_GITCONFIG (but not sysconfdir)\n-# can be specified as a relative path ../some/where/else (which must begin\n-# with ../); this is interpreted as relative to $(bindir) and \"git\" at\n+# can be specified as a relative path some/where/else;\n+# this is interpreted as relative to $(prefix) and \"git\" at\n # runtime figures out where they are based on the path to the executable.\n # This can help installing the suite in a relocatable way.\n \n prefix = $(HOME)\n-bindir = $(prefix)/bin\n-mandir = $(prefix)/share/man\n-infodir = $(prefix)/share/info\n-gitexecdir = $(prefix)/libexec/git-core\n+bindir_relative = bin\n+bindir = $(prefix)/$(bindir_relative)\n+mandir = share/man\n+infodir = share/info\n+gitexecdir = libexec/git-core\n sharedir = $(prefix)/share\n-template_dir = $(sharedir)/git-core/templates\n-htmldir=$(sharedir)/doc/git-doc\n+template_dir = share/git-core/templates\n+htmldir = share/doc/git-doc\n ifeq ($(prefix),/usr)\n sysconfdir = /etc\n+ETC_GITCONFIG = $(sysconfdir)/gitconfig\n else\n sysconfdir = $(prefix)/etc\n+ETC_GITCONFIG = etc/gitconfig\n endif\n lib = lib\n-ETC_GITCONFIG = $(sysconfdir)/gitconfig\n # DESTDIR=\n \n # default configuration for gitweb\n@@ -1086,6 +1090,7 @@ ETC_GITCONFIG_SQ = $(subst ','\\'',$(ETC_GITCONFIG))\n \n DESTDIR_SQ = $(subst ','\\'',$(DESTDIR))\n bindir_SQ = $(subst ','\\'',$(bindir))\n+bindir_relative_SQ = $(subst ','\\'',$(bindir_relative))\n mandir_SQ = $(subst ','\\'',$(mandir))\n infodir_SQ = $(subst ','\\'',$(infodir))\n gitexecdir_SQ = $(subst ','\\'',$(gitexecdir))\n@@ -1251,7 +1256,12 @@ git.o git.spec \\\n \t$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $<\n \n exec_cmd.o: exec_cmd.c GIT-CFLAGS\n-\t$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) '-DGIT_EXEC_PATH=\"$(gitexecdir_SQ)\"' $<\n+\t$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) \\\n+\t\t'-DGIT_EXEC_PATH=\"$(gitexecdir_SQ)\"' \\\n+\t\t'-DBINDIR=\"$(bindir_relative_SQ)\"' \\\n+\t\t'-DPREFIX=\"$(prefix_SQ)\"' \\\n+\t\t$<\n+\n builtin-init-db.o: builtin-init-db.c GIT-CFLAGS\n \t$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DDEFAULT_GIT_TEMPLATE_DIR='\"$(template_dir_SQ)\"' $<\n \n@@ -1407,17 +1417,17 @@ remove-dashes:\n \n ### Installation rules\n \n-ifeq ($(firstword $(subst /, ,$(template_dir))),..)\n-template_instdir = $(bindir)/$(template_dir)\n-else\n+ifeq ($(abspath $(template_dir)),$(template_dir))\n template_instdir = $(template_dir)\n+else\n+template_instdir = $(prefix)/$(template_dir)\n endif\n export template_instdir\n \n-ifeq ($(firstword $(subst /, ,$(gitexecdir))),..)\n-gitexec_instdir = $(bindir)/$(gitexecdir)\n-else\n+ifeq ($(abspath $(gitexecdir)),$(gitexecdir))\n gitexec_instdir = $(gitexecdir)\n+else\n+gitexec_instdir = $(prefix)/$(gitexecdir)\n endif\n gitexec_instdir_SQ = $(subst ','\\'',$(gitexec_instdir))\n export gitexec_instdir\ndiff --git a/builtin-help.c b/builtin-help.c\nindex f076efa..9b57a74 100644\n--- a/builtin-help.c\n+++ b/builtin-help.c\n@@ -329,7 +329,7 @@ static void setup_man_path(void)\n \t * old_path, the ':' at the end will let 'man' to try\n \t * system-wide paths after ours to find the manual page. If\n \t * there is old_path, we need ':' as delimiter. */\n-\tstrbuf_addstr(&new_path, GIT_MAN_PATH);\n+\tstrbuf_addstr(&new_path, system_path(GIT_MAN_PATH));\n \tstrbuf_addch(&new_path, ':');\n \tif (old_path)\n \t\tstrbuf_addstr(&new_path, old_path);\n@@ -375,7 +375,7 @@ static void show_man_page(const char *git_cmd)\n static void show_info_page(const char *git_cmd)\n {\n \tconst char *page = cmd_to_page(git_cmd);\n-\tsetenv(\"INFOPATH\", GIT_INFO_PATH, 1);\n+\tsetenv(\"INFOPATH\", system_path(GIT_INFO_PATH), 1);\n \texeclp(\"info\", \"info\", \"gitman\", page, NULL);\n }\n \ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex cdd35f9..b7e7b60 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -9,11 +9,15 @@ static const char *argv0_path;\n \n const char *system_path(const char *path)\n {\n-\tif (!is_absolute_path(path) && argv0_path) {\n-\t\tstruct strbuf d = STRBUF_INIT;\n-\t\tstrbuf_addf(&d, \"%s/%s\", argv0_path, path);\n-\t\tpath = strbuf_detach(&d, NULL);\n+\tstatic const char *prefix = PREFIX;\n+\n+\tif (is_absolute_path(path)) {\n+\t\treturn path;\n \t}\n+\n+\tstruct strbuf d = STRBUF_INIT;\n+\tstrbuf_addf(&d, \"%s/%s\", prefix, path);\n+\tpath = strbuf_detach(&d, NULL);\n \treturn path;\n }\n \n-- \n1.6.1.87.g15624\n"},{"id":"100956","messageId":"1232280015-8144-3-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":"1232280015-8144-2-git-send-email-prohaska@zib.de","subject":"[PATCH 2/7 v2] Refactor git_set_argv0_path() to git_extract_argv0_path()","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:10Z","receivedAt":"2009-01-18T12:00:10Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"From: Steve Haslam <shaslam@lastminute.com>\n\nThis commit moves the code that computes the dirname of argv[0]\nfrom git.c's main() to git_set_argv0_path() and renames the function\nto git_extract_argv0_path().  This makes the code in git.c's main\nless cluttered, and we can use the dirname computation from other\nmain() functions too.\n\n[ spr:\n - split Steve's original commit and wrote new commit message.\n - Integrated Johannes Schindelin's\n   cca1704897e7fdb182f68d4c48a437c5d7bc5203 while rebasing onto master.\n]\n\nSigned-off-by: Steve Haslam <shaslam@lastminute.com>\nSigned-off-by: Steffen Prohaska <prohaska@zib.de>\n---\n exec_cmd.c |   14 ++++++++++++--\n exec_cmd.h |    2 +-\n git.c      |   19 +++++--------------\n 3 files changed, 18 insertions(+), 17 deletions(-)\n\ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex b7e7b60..23a52cf 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -21,9 +21,19 @@ const char *system_path(const char *path)\n \treturn path;\n }\n \n-void git_set_argv0_path(const char *path)\n+const char *git_extract_argv0_path(const char *argv0)\n {\n-\targv0_path = path;\n+\tconst char *slash = argv0 + strlen(argv0);\n+\n+\twhile (argv0 <= slash && !is_dir_sep(*slash))\n+\t\tslash--;\n+\n+\tif (slash >= argv0) {\n+\t\targv0_path = xstrndup(argv0, slash - argv0);\n+\t\treturn slash + 1;\n+\t}\n+\n+\treturn argv0;\n }\n \n void git_set_argv_exec_path(const char *exec_path)\ndiff --git a/exec_cmd.h b/exec_cmd.h\nindex 594f961..392e903 100644\n--- a/exec_cmd.h\n+++ b/exec_cmd.h\n@@ -2,7 +2,7 @@\n #define GIT_EXEC_CMD_H\n \n extern void git_set_argv_exec_path(const char *exec_path);\n-extern void git_set_argv0_path(const char *path);\n+extern const char* git_extract_argv0_path(const char *path);\n extern const char* git_exec_path(void);\n extern void setup_path(void);\n extern const char **prepare_git_cmd(const char **argv);\ndiff --git a/git.c b/git.c\nindex a53e24f..b99b1b2 100644\n--- a/git.c\n+++ b/git.c\n@@ -419,22 +419,13 @@ static void execv_dashed_external(const char **argv)\n \n int main(int argc, const char **argv)\n {\n-\tconst char *cmd = argv[0] && *argv[0] ? argv[0] : \"git-help\";\n-\tchar *slash = (char *)cmd + strlen(cmd);\n+\tconst char *cmd;\n \tint done_alias = 0;\n \n-\t/*\n-\t * Take the basename of argv[0] as the command\n-\t * name, and the dirname as the default exec_path\n-\t * if we don't have anything better.\n-\t */\n-\twhile (cmd <= slash && !is_dir_sep(*slash))\n-\t\tslash--;\n-\tif (cmd <= slash) {\n-\t\t*slash++ = 0;\n-\t\tgit_set_argv0_path(cmd);\n-\t\tcmd = slash;\n-\t}\n+\tif (argv[0] && *argv[0])\n+\t\tcmd = git_extract_argv0_path(argv[0]);\n+\telse\n+\t\tcmd = \"git-help\";\n \n \t/*\n \t * \"git-xxxx\" is the same as \"git xxxx\", but we obviously:\n-- \n1.6.1.87.g15624\n"},{"id":"100951","messageId":"1232280015-8144-4-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":"1232280015-8144-3-git-send-email-prohaska@zib.de","subject":"[PATCH 3/7 v2] git_extract_argv0_path(): Move check for valid argv0 from caller to callee","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:11Z","receivedAt":"2009-01-18T12:00:11Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"This simplifies the calling code.\n\nSigned-off-by: Steffen Prohaska <prohaska@zib.de>\n---\n exec_cmd.c |    3 +++\n git.c      |    5 ++---\n 2 files changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex 23a52cf..89931e4 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -23,6 +23,9 @@ const char *system_path(const char *path)\n \n const char *git_extract_argv0_path(const char *argv0)\n {\n+\tif (!argv0 || !*argv0)\n+\t\treturn 0;\n+\n \tconst char *slash = argv0 + strlen(argv0);\n \n \twhile (argv0 <= slash && !is_dir_sep(*slash))\ndiff --git a/git.c b/git.c\nindex b99b1b2..9c8da93 100644\n--- a/git.c\n+++ b/git.c\n@@ -422,9 +422,8 @@ int main(int argc, const char **argv)\n \tconst char *cmd;\n \tint done_alias = 0;\n \n-\tif (argv[0] && *argv[0])\n-\t\tcmd = git_extract_argv0_path(argv[0]);\n-\telse\n+\tcmd = git_extract_argv0_path(argv[0]);\n+\tif (!cmd)\n \t\tcmd = \"git-help\";\n \n \t/*\n-- \n1.6.1.87.g15624\n"},{"id":"100955","messageId":"1232280015-8144-5-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":"1232280015-8144-4-git-send-email-prohaska@zib.de","subject":"[PATCH 4/7 v2] Add calls to git_extract_argv0_path() in programs that call git_config_*","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:12Z","receivedAt":"2009-01-18T12:00:12Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"Programs that use git_config need to find the global configuration.\nWhen runtime prefix computation is enabled, this requires that\ngit_extract_argv0_path() is called early in the program's main().\n\nThis commit adds the necessary calls.\n\nSigned-off-by: Steffen Prohaska <prohaska@zib.de>\n---\n daemon.c             |    2 ++\n fast-import.c        |    3 +++\n hash-object.c        |    3 +++\n http-push.c          |    2 ++\n imap-send.c          |    3 +++\n index-pack.c         |    3 +++\n merge-index.c        |    3 +++\n merge-tree.c         |    3 +++\n mktag.c              |    3 +++\n mktree.c             |    3 +++\n pack-redundant.c     |    3 +++\n patch-id.c           |    3 +++\n unpack-file.c        |    3 +++\n update-server-info.c |    3 +++\n upload-pack.c        |    2 ++\n var.c                |    3 +++\n 16 files changed, 45 insertions(+), 0 deletions(-)\n\ndiff --git a/daemon.c b/daemon.c\nindex 540700e..d93cf96 100644\n--- a/daemon.c\n+++ b/daemon.c\n@@ -937,6 +937,8 @@ int main(int argc, char **argv)\n \tgid_t gid = 0;\n \tint i;\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tfor (i = 1; i < argc; i++) {\n \t\tchar *arg = argv[i];\n \ndiff --git a/fast-import.c b/fast-import.c\nindex f0e08ac..1935206 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -150,6 +150,7 @@ Format of STDIN stream:\n #include \"refs.h\"\n #include \"csum-file.h\"\n #include \"quote.h\"\n+#include \"exec_cmd.h\"\n \n #define PACK_ID_BITS 16\n #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)\n@@ -2406,6 +2407,8 @@ int main(int argc, const char **argv)\n {\n \tunsigned int i, show_stats = 1;\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tsetup_git_directory();\n \tgit_config(git_pack_config, NULL);\n \tif (!pack_compression_seen && core_compression_seen)\ndiff --git a/hash-object.c b/hash-object.c\nindex 846e91a..37e6677 100644\n--- a/hash-object.c\n+++ b/hash-object.c\n@@ -8,6 +8,7 @@\n #include \"blob.h\"\n #include \"quote.h\"\n #include \"parse-options.h\"\n+#include \"exec_cmd.h\"\n \n static void hash_fd(int fd, const char *type, int write_object, const char *path)\n {\n@@ -81,6 +82,8 @@ int main(int argc, const char **argv)\n \n \ttype = blob_type;\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tgit_config(git_default_config, NULL);\n \n \targc = parse_options(argc, argv, hash_object_options, hash_object_usage, 0);\ndiff --git a/http-push.c b/http-push.c\nindex a4b7d08..09caede 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -2179,6 +2179,8 @@ int main(int argc, char **argv)\n \tstruct ref *ref;\n \tchar *rewritten_url = NULL;\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tsetup_git_directory();\n \n \tremote = xcalloc(sizeof(*remote), 1);\ndiff --git a/imap-send.c b/imap-send.c\nindex c3fa0df..f91293c 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -23,6 +23,7 @@\n  */\n \n #include \"cache.h\"\n+#include \"exec_cmd.h\"\n #ifdef NO_OPENSSL\n typedef void *SSL;\n #endif\n@@ -1389,6 +1390,8 @@ int main(int argc, char **argv)\n \tint total, n = 0;\n \tint nongit_ok;\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \t/* init the random number generator */\n \tarc4_init();\n \ndiff --git a/index-pack.c b/index-pack.c\nindex 2931511..72c41fd 100644\n--- a/index-pack.c\n+++ b/index-pack.c\n@@ -8,6 +8,7 @@\n #include \"tree.h\"\n #include \"progress.h\"\n #include \"fsck.h\"\n+#include \"exec_cmd.h\"\n \n static const char index_pack_usage[] =\n \"git index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] [--strict] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }\";\n@@ -880,6 +881,8 @@ int main(int argc, char **argv)\n \tstruct pack_idx_entry **idx_objects;\n \tunsigned char pack_sha1[20];\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \t/*\n \t * We wish to read the repository's config file if any, and\n \t * for that it is necessary to call setup_git_directory_gently().\ndiff --git a/merge-index.c b/merge-index.c\nindex 7827e87..c00a2b3 100644\n--- a/merge-index.c\n+++ b/merge-index.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"run-command.h\"\n+#include \"exec_cmd.h\"\n \n static const char *pgm;\n static const char *arguments[9];\n@@ -93,6 +94,8 @@ int main(int argc, char **argv)\n \tif (argc < 3)\n \t\tusage(\"git-merge-index [-o] [-q] <merge-program> (-a | [--] <filename>*)\");\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tsetup_git_directory();\n \tread_cache();\n \ndiff --git a/merge-tree.c b/merge-tree.c\nindex 2d1413e..f18201a 100644\n--- a/merge-tree.c\n+++ b/merge-tree.c\n@@ -2,6 +2,7 @@\n #include \"tree-walk.h\"\n #include \"xdiff-interface.h\"\n #include \"blob.h\"\n+#include \"exec_cmd.h\"\n \n static const char merge_tree_usage[] = \"git-merge-tree <base-tree> <branch1> <branch2>\";\n static int resolve_directories = 1;\n@@ -344,6 +345,8 @@ int main(int argc, char **argv)\n \tif (argc != 4)\n \t\tusage(merge_tree_usage);\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tsetup_git_directory();\n \n \tbuf1 = get_tree_descriptor(t+0, argv[1]);\ndiff --git a/mktag.c b/mktag.c\nindex ba3d495..6d5083e 100644\n--- a/mktag.c\n+++ b/mktag.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"tag.h\"\n+#include \"exec_cmd.h\"\n \n /*\n  * A signature file has a very simple fixed format: four lines\n@@ -159,6 +160,8 @@ int main(int argc, char **argv)\n \tif (argc != 1)\n \t\tusage(\"git-mktag < signaturefile\");\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tsetup_git_directory();\n \n \tif (strbuf_read(&buf, 0, 4096) < 0) {\ndiff --git a/mktree.c b/mktree.c\nindex 514fd9b..6283bc3 100644\n--- a/mktree.c\n+++ b/mktree.c\n@@ -6,6 +6,7 @@\n #include \"cache.h\"\n #include \"quote.h\"\n #include \"tree.h\"\n+#include \"exec_cmd.h\"\n \n static struct treeent {\n \tunsigned mode;\n@@ -70,6 +71,8 @@ int main(int ac, char **av)\n \tunsigned char sha1[20];\n \tint line_termination = '\\n';\n \n+\tgit_extract_argv0_path(av[0]);\n+\n \tsetup_git_directory();\n \n \twhile ((1 < ac) && av[1][0] == '-') {\ndiff --git a/pack-redundant.c b/pack-redundant.c\nindex e93eb96..48a12bc 100644\n--- a/pack-redundant.c\n+++ b/pack-redundant.c\n@@ -7,6 +7,7 @@\n */\n \n #include \"cache.h\"\n+#include \"exec_cmd.h\"\n \n #define BLKSIZE 512\n \n@@ -601,6 +602,8 @@ int main(int argc, char **argv)\n \tunsigned char *sha1;\n \tchar buf[42]; /* 40 byte sha1 + \\n + \\0 */\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tsetup_git_directory();\n \n \tfor (i = 1; i < argc; i++) {\ndiff --git a/patch-id.c b/patch-id.c\nindex 871f1d2..3660ad4 100644\n--- a/patch-id.c\n+++ b/patch-id.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"exec_cmd.h\"\n \n static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)\n {\n@@ -79,6 +80,8 @@ int main(int argc, char **argv)\n \tif (argc != 1)\n \t\tusage(patch_id_usage);\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tgenerate_id_list();\n \treturn 0;\n }\ndiff --git a/unpack-file.c b/unpack-file.c\nindex bcdc8bb..6dd8ad0 100644\n--- a/unpack-file.c\n+++ b/unpack-file.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"blob.h\"\n+#include \"exec_cmd.h\"\n \n static char *create_temp_file(unsigned char *sha1)\n {\n@@ -25,6 +26,8 @@ int main(int argc, char **argv)\n {\n \tunsigned char sha1[20];\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tif (argc != 2)\n \t\tusage(\"git-unpack-file <sha1>\");\n \tif (get_sha1(argv[1], sha1))\ndiff --git a/update-server-info.c b/update-server-info.c\nindex 7e8209e..7b38fd8 100644\n--- a/update-server-info.c\n+++ b/update-server-info.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"exec_cmd.h\"\n \n static const char update_server_info_usage[] =\n \"git update-server-info [--force]\";\n@@ -19,6 +20,8 @@ int main(int ac, char **av)\n \tif (i != ac)\n \t\tusage(update_server_info_usage);\n \n+\tgit_extract_argv0_path(av[0]);\n+\n \tsetup_git_directory();\n \n \treturn !!update_server_info(force);\ndiff --git a/upload-pack.c b/upload-pack.c\nindex e5adbc0..5db6f93 100644\n--- a/upload-pack.c\n+++ b/upload-pack.c\n@@ -616,6 +616,8 @@ int main(int argc, char **argv)\n \tint i;\n \tint strict = 0;\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tfor (i = 1; i < argc; i++) {\n \t\tchar *arg = argv[i];\n \ndiff --git a/var.c b/var.c\nindex f1eb314..7362ed8 100644\n--- a/var.c\n+++ b/var.c\n@@ -4,6 +4,7 @@\n  * Copyright (C) Eric Biederman, 2005\n  */\n #include \"cache.h\"\n+#include \"exec_cmd.h\"\n \n static const char var_usage[] = \"git var [-l | <variable>]\";\n \n@@ -56,6 +57,8 @@ int main(int argc, char **argv)\n \t\tusage(var_usage);\n \t}\n \n+\tgit_extract_argv0_path(argv[0]);\n+\n \tsetup_git_directory_gently(&nongit);\n \tval = NULL;\n \n-- \n1.6.1.87.g15624\n"},{"id":"100957","messageId":"1232280015-8144-6-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":"1232280015-8144-5-git-send-email-prohaska@zib.de","subject":"[PATCH 5/7 v2] Modify setup_path() to only add git_exec_path() to PATH","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:13Z","receivedAt":"2009-01-18T12:00:13Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"Searching git programs only in the highest priority location is\nsufficient.  It does not make sense that some of the required\nprograms are located at the highest priority location but other\nprograms are picked up from a lower priority exec-path.  If\nexec-path is overridden a complete set of commands should be\nprovided, otherwise several different versions could get mixed,\nwhich is likely to cause confusion.\n\nIf a user explicitly overrides the default location (by --exec-path\nor GIT_EXEC_PATH), we now expect that all the required programs are\nfound there.  Instead of adding the directories \"argv_exec_path\",\n\"getenv(EXEC_PATH_ENVIRONMENT)\", and \"system_path(GIT_EXEC_PATH)\"\nto PATH, we now rely on git_exec_path(), which implements the same\norder, but only returns the highest priority location to search for\nexecutables.\n\nAccessing only the location with highest priority is also required\nfor testing executables built with RUNTIME_PREFIX.  The call to\nsystem_path() should be avoided if RUNTIME_PREFIX is set and the\nexecutable is not installed at its final destination.  Because we\ntest before installing, we want to avoid calling system_path()\nduring tests.  The modifications in this commit avoid calling\nsystem_path(GIT_EXEC_PATH) if a higher-priority location is\nprovided, which is the case when running the tests.\n\nSigned-off-by: Steffen Prohaska <prohaska@zib.de>\n---\n exec_cmd.c |    4 +---\n 1 files changed, 1 insertions(+), 3 deletions(-)\n\ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex 89931e4..d9408db 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -78,9 +78,7 @@ void setup_path(void)\n \tconst char *old_path = getenv(\"PATH\");\n \tstruct strbuf new_path = STRBUF_INIT;\n \n-\tadd_path(&new_path, argv_exec_path);\n-\tadd_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));\n-\tadd_path(&new_path, system_path(GIT_EXEC_PATH));\n+\tadd_path(&new_path, git_exec_path());\n \tadd_path(&new_path, argv0_path);\n \n \tif (old_path)\n-- \n1.6.1.87.g15624\n"},{"id":"100953","messageId":"1232280015-8144-7-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":"1232280015-8144-6-git-send-email-prohaska@zib.de","subject":"[PATCH 6/7 v2] Compute prefix at runtime if RUNTIME_PREFIX is set","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:14Z","receivedAt":"2009-01-18T12:00:14Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"This commit adds support for relocatable binaries (called\nRUNTIME_PREFIX).  Such binaries can be moved together with the\nsystem configuration files to a different directory, as long as the\nrelative paths from the binary to the configuration files is\npreserved.  This functionality is essential on Windows where we\ndeliver git binaries with an installer that allows to freely choose\nthe installation location.\n\nIf RUNTIME_PREFIX is unset we use the static prefix.  This will be\nthe default on Unix.  Thus, the behavior on Unix will remain\nidentical to the old implementation, which used to add the prefix\nin the Makefile.\n\nIf RUNTIME_PREFIX is set the prefix is computed from the location\nof the executable.  In this case, system_path() tries to strip\nknown directories that executables can be located in from the path\nof the executable.  If the path is successfully stripped it is used\nas the prefix.  For example, if the executable is\n\"/msysgit/bin/git\" and BINDIR is \"bin\", then the prefix computed is\n\"/msysgit\".\n\nIf the runtime prefix computation fails, we fall back to the static\nprefix specified in the makefile.  This can be the case if the\nexecutable is not installed at a known location.  Note that our\ntest system sets GIT_CONFIG_NOSYSTEM to tell git to ignore global\nconfiguration files during testing.  Hence testing does not trigger\nthe fall back.\n\nNote that RUNTIME_PREFIX only works on Windows, though adding\nsupport on Unix should not be too hard.  The implementation\nrequires argv0_path to be set to an absolute path.  argv0_path must\npoint to the directory of the executable.  We use assert() to\nverify this in debug builds.  On Windows, the wrapper for main()\n(see compat/mingw.h) guarantees that argv0_path is correctly\ninitialized.  On Unix, further work is required before\nRUNTIME_PREFIX can be enabled.\n\nSigned-off-by: Steffen Prohaska <prohaska@zib.de>\n---\n Makefile   |    3 +++\n exec_cmd.c |   44 ++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 47 insertions(+), 0 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex 1d2060a..fbe52c9 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1031,6 +1031,9 @@ ifdef INTERNAL_QSORT\n \tCOMPAT_CFLAGS += -DINTERNAL_QSORT\n \tCOMPAT_OBJS += compat/qsort.o\n endif\n+ifdef RUNTIME_PREFIX\n+\tCOMPAT_CFLAGS += -DRUNTIME_PREFIX\n+endif\n \n ifdef NO_PTHREADS\n \tTHREADED_DELTA_SEARCH =\ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex d9408db..5f59880 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -9,12 +9,56 @@ static const char *argv0_path;\n \n const char *system_path(const char *path)\n {\n+#ifdef RUNTIME_PREFIX\n+\tstatic const char *prefix = 0;\n+#else\n \tstatic const char *prefix = PREFIX;\n+#endif\n \n \tif (is_absolute_path(path)) {\n \t\treturn path;\n \t}\n \n+#ifdef RUNTIME_PREFIX\n+\tassert(argv0_path);\n+\tassert(is_absolute_path(argv0_path));\n+\n+\tif (!prefix) {\n+\t\tconst char *strip[] = {\n+\t\t\tGIT_EXEC_PATH,\n+\t\t\tBINDIR,\n+\t\t\t0\n+\t\t};\n+\t\tconst char **s;\n+\n+\t\tfor (s = strip; *s; s++) {\n+\t\t\tconst char *sargv = argv0_path + strlen(argv0_path);\n+\t\t\tconst char *ss = *s + strlen(*s);\n+\t\t\twhile (argv0_path < sargv && *s < ss\n+\t\t\t\t&& (*sargv == *ss ||\n+\t\t\t\t    (is_dir_sep(*sargv) && is_dir_sep(*ss)))) {\n+\t\t\t\tsargv--;\n+\t\t\t\tss--;\n+\t\t\t}\n+\t\t\tif (*s == ss) {\n+\t\t\t\tstruct strbuf d = STRBUF_INIT;\n+\t\t\t\t/* We also skip the trailing directory separator. */\n+\t\t\t\tassert(sargv - argv0_path - 1 >= 0);\n+\t\t\t\tstrbuf_add(&d, argv0_path, sargv - argv0_path - 1);\n+\t\t\t\tprefix = strbuf_detach(&d, NULL);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tif (!prefix) {\n+\t\tprefix = PREFIX;\n+\t\tfprintf(stderr, \"RUNTIME_PREFIX requested, \"\n+\t\t\t\t\"but prefix computation failed.  \"\n+\t\t\t\t\"Using static fallback '%s'.\\n\", prefix);\n+\t}\n+#endif\n+\n \tstruct strbuf d = STRBUF_INIT;\n \tstrbuf_addf(&d, \"%s/%s\", prefix, path);\n \tpath = strbuf_detach(&d, NULL);\n-- \n1.6.1.87.g15624\n"},{"id":"100958","messageId":"1232280015-8144-8-git-send-email-prohaska@zib.de","threadId":"17235","inReplyTo":"1232280015-8144-7-git-send-email-prohaska@zib.de","subject":"[PATCH 7/7 v2] Windows: Revert to default paths and convert them by RUNTIME_PREFIX","fromName":"Steffen Prohaska","fromEmail":"prohaska@zib.de","sentAt":"2009-01-18T12:00:15Z","receivedAt":"2009-01-18T12:00:15Z","isPatch":true,"sender":{"key":"prohaska@zib.de","avatar":"https://avatars.githubusercontent.com/u/217580?v=4"},"body":"The RUNTIME_PREFIX mechanism allows us to use the default paths on\nWindows too.  Defining RUNTIME_PREFIX explicitly requests for\ntranslation of paths relative to the executable at runtime.\n\nSigned-off-by: Steffen Prohaska <prohaska@zib.de>\n---\n Makefile |    4 +---\n 1 files changed, 1 insertions(+), 3 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex fbe52c9..74ecd77 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -789,6 +789,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))\n \tSNPRINTF_RETURNS_BOGUS = YesPlease\n \tNO_SVN_TESTS = YesPlease\n \tNO_PERL_MAKEMAKER = YesPlease\n+\tRUNTIME_PREFIX = YesPlease\n \tNO_POSIX_ONLY_PROGRAMS = YesPlease\n \tNO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease\n \tCOMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch\n@@ -797,9 +798,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))\n \tCOMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o\n \tEXTLIBS += -lws2_32\n \tX = .exe\n-\tgitexecdir = ../libexec/git-core\n-\ttemplate_dir = ../share/git-core/templates/\n-\tETC_GITCONFIG = ../etc/gitconfig\n endif\n ifneq (,$(findstring arm,$(uname_M)))\n \tARM_SHA1 = YesPlease\n-- \n1.6.1.87.g15624\n"},{"id":"101038","messageId":"200901182016.56395.j6t@kdbg.org","threadId":"17235","inReplyTo":"1232280015-8144-4-git-send-email-prohaska@zib.de","subject":"Re: [PATCH 3/7 v2] git_extract_argv0_path(): Move check for valid argv0 from caller to callee","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2009-01-18T19:16:54Z","receivedAt":"2009-01-18T19:16:54Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"On Sonntag, 18. Januar 2009, Steffen Prohaska wrote:\n> This simplifies the calling code.\n\nBut it could really be squashed into the previous patch, after fixing...\n\n> @@ -23,6 +23,9 @@ const char *system_path(const char *path)\n>\n>  const char *git_extract_argv0_path(const char *argv0)\n>  {\n> +\tif (!argv0 || !*argv0)\n> +\t\treturn 0;\n> +\n>  \tconst char *slash = argv0 + strlen(argv0);\n\n... this declaration after statement.\n\n-- Hannes\n"},{"id":"101043","messageId":"200901182028.11345.j6t@kdbg.org","threadId":"17235","inReplyTo":"200901182016.56395.j6t@kdbg.org","subject":"Re: [PATCH 3/7 v2] git_extract_argv0_path(): Move check for valid argv0 from caller to callee","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2009-01-18T19:28:10Z","receivedAt":"2009-01-18T19:28:10Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"On Sonntag, 18. Januar 2009, Johannes Sixt wrote:\n> On Sonntag, 18. Januar 2009, Steffen Prohaska wrote:\n> > This simplifies the calling code.\n>\n> But it could really be squashed into the previous patch, after fixing...\n>\n> > @@ -23,6 +23,9 @@ const char *system_path(const char *path)\n> >\n> >  const char *git_extract_argv0_path(const char *argv0)\n> >  {\n> > +\tif (!argv0 || !*argv0)\n> > +\t\treturn 0;\n> > +\n> >  \tconst char *slash = argv0 + strlen(argv0);\n>\n> ... this declaration after statement.\n\nAnd we prefer NULL over 0 for the null pointer.\n\nThe series is nicely done, thank you! I am using it (the previous round) \nwithout problems so far. I hope we can get this in RSN.\n\n-- Hannes\n"},{"id":"101064","messageId":"7vzlhotic6.fsf@gitster.siamese.dyndns.org","threadId":"17235","inReplyTo":"200901182028.11345.j6t@kdbg.org","subject":"Re: [PATCH 3/7 v2] git_extract_argv0_path(): Move check for valid argv0 from caller to callee","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-01-18T21:28:09Z","receivedAt":"2009-01-18T21:28:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Sixt <j6t@kdbg.org> writes:\n\n> On Sonntag, 18. Januar 2009, Johannes Sixt wrote:\n>> On Sonntag, 18. Januar 2009, Steffen Prohaska wrote:\n>> > This simplifies the calling code.\n>>\n>> But it could really be squashed into the previous patch, after fixing...\n>>\n>> > @@ -23,6 +23,9 @@ const char *system_path(const char *path)\n>> >\n>> >  const char *git_extract_argv0_path(const char *argv0)\n>> >  {\n>> > +\tif (!argv0 || !*argv0)\n>> > +\t\treturn 0;\n>> > +\n>> >  \tconst char *slash = argv0 + strlen(argv0);\n>>\n>> ... this declaration after statement.\n>\n> And we prefer NULL over 0 for the null pointer.\n>\n> The series is nicely done, thank you! I am using it (the previous round) \n> without problems so far. I hope we can get this in RSN.\n\nThanks, both.  I take that as your Ack to the whole series.\n\nI've rebased the series to master, squashed in fixes like the above (there\nwere others) so that each step can compile.  Will queue the result.\n"},{"id":"101149","messageId":"alpine.DEB.1.00.0901191825040.3586@pacific.mpi-cbg.de","threadId":"17235","inReplyTo":"1232280015-8144-7-git-send-email-prohaska@zib.de","subject":"Re: [PATCH 6/7 v2] Compute prefix at runtime if RUNTIME_PREFIX is set","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-01-19T17:41:05Z","receivedAt":"2009-01-19T17:41:05Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 18 Jan 2009, Steffen Prohaska wrote:\n\n> +\tif (!prefix) {\n> +\t\tconst char *strip[] = {\n> +\t\t\tGIT_EXEC_PATH,\n> +\t\t\tBINDIR,\n> +\t\t\t0\n> +\t\t};\n> +\t\tconst char **s;\n> +\n> +\t\tfor (s = strip; *s; s++) {\n> +\t\t\tconst char *sargv = argv0_path + strlen(argv0_path);\n\nThis does not need to be recomputed all the time, right?\n\n> +\t\t\tconst char *ss = *s + strlen(*s);\n> +\t\t\twhile (argv0_path < sargv && *s < ss\n> +\t\t\t\t&& (*sargv == *ss ||\n> +\t\t\t\t    (is_dir_sep(*sargv) && is_dir_sep(*ss)))) {\n> +\t\t\t\tsargv--;\n> +\t\t\t\tss--;\n> +\t\t\t}\n> +\t\t\tif (*s == ss) {\n> +\t\t\t\tstruct strbuf d = STRBUF_INIT;\n> +\t\t\t\t/* We also skip the trailing directory separator. */\n> +\t\t\t\tassert(sargv - argv0_path - 1 >= 0);\n> +\t\t\t\tstrbuf_add(&d, argv0_path, sargv - argv0_path - 1);\n> +\t\t\t\tprefix = strbuf_detach(&d, NULL);\n> +\t\t\t\tbreak;\n> +\t\t\t}\n> +\t\t}\n> +\t}\n\nI have a definite feeling that this code would be easier to read if it \nresembled this:\n\n/* sets prefix if the suffix matches (modulo separator changes) */\nstatic int strip_path_suffix(const char *path, const char *suffix,\n\tchar **prefix)\n{\n\tint path_len = strlen(path), suffix_len = strlen(suffix);\n\n\twhile (suffix_len) {\n\t\tif (!path_len)\n\t\t\treturn 1;\n\n\t\tpath_len--; suffix_len--;\n\t\t/* strip arbitrary amount of directory separators */\n\t\tif (is_dir_sep(path[path_len]) && \n\t\t\t\tis_dir_sep(suffix[suffix_len])) {\n\t\t\twhile (path_len && is_dir_sep(path[path_len]))\n\t\t\t\tpath_len--;\n\t\t\twhile (suffix_len && is_dir_sep(suffix[suffix_len]))\n\t\t\t\tsuffix_len--;\n\t\t}\n\t\telse if (path[path_len] != suffix[suffix_len])\n\t\t\treturn 1;\n\t}\n\n\twhile (path_len && is_dir_sep(path[path_len - 1]))\n\t\tpath_len--;\n\n\tprefix = xstrndup(path, path_len);\n\treturn 0;\n}\n\n...\n\n\tif (!prefix && strip_path_suffix(argv0_path, GIT_EXEC_PATH) &&\n\t\t\tstrip_path_suffix(argv0_path, BINDIR)) {\n\t\tprefix = PREFIX;\n\t\t...\n\nNote: this function could be exported in path.c, as it also handles the \n\"a//bin\" vs \"a/bin\" case.\n\nCiao,\nDscho\n"}]}