mergetool: fix running mergetool in sub-directories
[gitweb.git] / git.c
diff --git a/git.c b/git.c
index 5582c515ac04609a338de1d2d5e510e7e7c4914d..ecc8fad09aebd16410f2736458d60e04f9dfb296 100644 (file)
--- a/git.c
+++ b/git.c
@@ -158,7 +158,7 @@ static int handle_alias(int *argcp, const char ***argv)
                        if (ret >= 0 && WIFEXITED(ret) &&
                            WEXITSTATUS(ret) != 127)
                                exit(WEXITSTATUS(ret));
-                       die("Failed to run '%s' when expanding alias '%s'\n",
+                       die("Failed to run '%s' when expanding alias '%s'",
                            alias_string + 1, alias_command);
                }
                count = split_cmdline(alias_string, &new_argv);
@@ -195,8 +195,8 @@ static int handle_alias(int *argcp, const char ***argv)
                ret = 1;
        }
 
-       if (subdir)
-               chdir(subdir);
+       if (subdir && chdir(subdir))
+               die("Cannot change to %s: %s", subdir, strerror(errno));
 
        errno = saved_errno;
 
@@ -266,6 +266,7 @@ static void handle_internal_command(int argc, const char **argv)
        const char *cmd = argv[0];
        static struct cmd_struct commands[] = {
                { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+               { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
                { "annotate", cmd_annotate, RUN_SETUP },
                { "apply", cmd_apply },
                { "archive", cmd_archive },
@@ -330,6 +331,7 @@ static void handle_internal_command(int argc, const char **argv)
                { "prune-packed", cmd_prune_packed, RUN_SETUP },
                { "push", cmd_push, RUN_SETUP },
                { "read-tree", cmd_read_tree, RUN_SETUP },
+               { "receive-pack", cmd_receive_pack },
                { "reflog", cmd_reflog, RUN_SETUP },
                { "remote", cmd_remote, RUN_SETUP },
                { "repo-config", cmd_config },
@@ -366,7 +368,7 @@ static void handle_internal_command(int argc, const char **argv)
        if (sizeof(ext) > 1) {
                i = strlen(argv[0]) - strlen(ext);
                if (i > 0 && !strcmp(argv[0] + i, ext)) {
-                       char *argv0 = strdup(argv[0]);
+                       char *argv0 = xstrdup(argv[0]);
                        argv[0] = cmd = argv0;
                        argv0[i] = '\0';
                }
@@ -388,10 +390,9 @@ static void handle_internal_command(int argc, const char **argv)
 
 static void execv_dashed_external(const char **argv)
 {
-       struct strbuf cmd;
+       struct strbuf cmd = STRBUF_INIT;
        const char *tmp;
 
-       strbuf_init(&cmd, 0);
        strbuf_addf(&cmd, "git-%s", argv[0]);
 
        /*
@@ -415,21 +416,42 @@ static void execv_dashed_external(const char **argv)
        strbuf_release(&cmd);
 }
 
+static int run_argv(int *argcp, const char ***argv)
+{
+       int done_alias = 0;
+
+       while (1) {
+               /* See if it's an internal command */
+               handle_internal_command(*argcp, *argv);
+
+               /* .. then try the external ones */
+               execv_dashed_external(*argv);
+
+               /* It could be an alias -- this works around the insanity
+                * of overriding "git log" with "git show" by having
+                * alias.log = show
+                */
+               if (done_alias || !handle_alias(argcp, argv))
+                       break;
+               done_alias = 1;
+       }
+
+       return done_alias;
+}
+
 
 int main(int argc, const char **argv)
 {
        const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help";
        char *slash = (char *)cmd + strlen(cmd);
-       int done_alias = 0;
 
        /*
         * Take the basename of argv[0] as the command
         * name, and the dirname as the default exec_path
         * if we don't have anything better.
         */
-       do
-               --slash;
-       while (cmd <= slash && !is_dir_sep(*slash));
+       while (cmd <= slash && !is_dir_sep(*slash))
+               slash--;
        if (cmd <= slash) {
                *slash++ = 0;
                git_set_argv0_path(cmd);
@@ -479,29 +501,22 @@ int main(int argc, const char **argv)
        setup_path();
 
        while (1) {
-               /* See if it's an internal command */
-               handle_internal_command(argc, argv);
-
-               /* .. then try the external ones */
-               execv_dashed_external(argv);
-
-               /* It could be an alias -- this works around the insanity
-                * of overriding "git log" with "git show" by having
-                * alias.log = show
-                */
-               if (done_alias || !handle_alias(&argc, &argv))
+               static int done_help = 0;
+               static int was_alias = 0;
+               was_alias = run_argv(&argc, &argv);
+               if (errno != ENOENT)
                        break;
-               done_alias = 1;
-       }
-
-       if (errno == ENOENT) {
-               if (done_alias) {
+               if (was_alias) {
                        fprintf(stderr, "Expansion of alias '%s' failed; "
                                "'%s' is not a git-command\n",
                                cmd, argv[0]);
                        exit(1);
                }
-               help_unknown_cmd(cmd);
+               if (!done_help) {
+                       cmd = argv[0] = help_unknown_cmd(cmd);
+                       done_help = 1;
+               } else
+                       break;
        }
 
        fprintf(stderr, "Failed to run command '%s': %s\n",