diff.c: --ws-error-highlight=<kind> option
[gitweb.git] / git.c
diff --git a/git.c b/git.c
index cb5208de6a5aab7c222a1d1655fe37089a119fe0..18fbf79430687a473e9306f2bb65abbaec161bb4 100644 (file)
--- a/git.c
+++ b/git.c
@@ -14,12 +14,49 @@ const char git_usage_string[] =
        "           <command> [<args>]";
 
 const char git_more_info_string[] =
-       N_("'git help -a' and 'git help -g' lists available subcommands and some\n"
+       N_("'git help -a' and 'git help -g' list available subcommands and some\n"
           "concept guides. See 'git help <command>' or 'git help <concept>'\n"
           "to read about a specific subcommand or concept.");
 
 static struct startup_info git_startup_info;
 static int use_pager = -1;
+static char *orig_cwd;
+static const char *env_names[] = {
+       GIT_DIR_ENVIRONMENT,
+       GIT_WORK_TREE_ENVIRONMENT,
+       GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
+       GIT_PREFIX_ENVIRONMENT
+};
+static char *orig_env[4];
+static int saved_environment;
+
+static void save_env(void)
+{
+       int i;
+       if (saved_environment)
+               return;
+       saved_environment = 1;
+       orig_cwd = xgetcwd();
+       for (i = 0; i < ARRAY_SIZE(env_names); i++) {
+               orig_env[i] = getenv(env_names[i]);
+               if (orig_env[i])
+                       orig_env[i] = xstrdup(orig_env[i]);
+       }
+}
+
+static void restore_env(void)
+{
+       int i;
+       if (orig_cwd && chdir(orig_cwd))
+               die_errno("could not move to %s", orig_cwd);
+       free(orig_cwd);
+       for (i = 0; i < ARRAY_SIZE(env_names); i++) {
+               if (orig_env[i])
+                       setenv(env_names[i], orig_env[i], 1);
+               else
+                       unsetenv(env_names[i]);
+       }
+}
 
 static void commit_pager_choice(void) {
        switch (use_pager) {
@@ -54,8 +91,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                /*
                 * Check remaining flags.
                 */
-               if (!prefixcmp(cmd, "--exec-path")) {
-                       cmd += 11;
+               if (skip_prefix(cmd, "--exec-path", &cmd)) {
                        if (*cmd == '=')
                                git_set_argv_exec_path(cmd + 1);
                        else {
@@ -78,7 +114,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                        if (envchanged)
                                *envchanged = 1;
                } else if (!strcmp(cmd, "--no-replace-objects")) {
-                       read_replace_refs = 0;
+                       check_replace_refs = 0;
                        setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
                        if (envchanged)
                                *envchanged = 1;
@@ -92,8 +128,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                                *envchanged = 1;
                        (*argv)++;
                        (*argc)--;
-               } else if (!prefixcmp(cmd, "--git-dir=")) {
-                       setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
+               } else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
+                       setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
                        if (envchanged)
                                *envchanged = 1;
                } else if (!strcmp(cmd, "--namespace")) {
@@ -106,8 +142,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                                *envchanged = 1;
                        (*argv)++;
                        (*argc)--;
-               } else if (!prefixcmp(cmd, "--namespace=")) {
-                       setenv(GIT_NAMESPACE_ENVIRONMENT, cmd + 12, 1);
+               } else if (skip_prefix(cmd, "--namespace=", &cmd)) {
+                       setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
                        if (envchanged)
                                *envchanged = 1;
                } else if (!strcmp(cmd, "--work-tree")) {
@@ -120,14 +156,15 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                                *envchanged = 1;
                        (*argv)++;
                        (*argc)--;
-               } else if (!prefixcmp(cmd, "--work-tree=")) {
-                       setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
+               } else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
+                       setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
                        if (envchanged)
                                *envchanged = 1;
                } else if (!strcmp(cmd, "--bare")) {
-                       static char git_dir[PATH_MAX+1];
+                       char *cwd = xgetcwd();
                        is_bare_repository_cfg = 1;
-                       setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
+                       setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
+                       free(cwd);
                        setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
                        if (envchanged)
                                *envchanged = 1;
@@ -162,7 +199,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                } else if (!strcmp(cmd, "--shallow-file")) {
                        (*argv)++;
                        (*argc)--;
-                       set_alternate_shallow_file((*argv)[0]);
+                       set_alternate_shallow_file((*argv)[0], 1);
                        if (envchanged)
                                *envchanged = 1;
                } else if (!strcmp(cmd, "-C")) {
@@ -245,8 +282,7 @@ static int handle_alias(int *argcp, const char ***argv)
                                  "trace: alias expansion: %s =>",
                                  alias_command);
 
-               new_argv = xrealloc(new_argv, sizeof(char *) *
-                                   (count + *argcp));
+               REALLOC_ARRAY(new_argv, count + *argcp);
                /* insert after command name */
                memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
 
@@ -272,6 +308,7 @@ static int handle_alias(int *argcp, const char ***argv)
  * RUN_SETUP for reading from the configuration file.
  */
 #define NEED_WORK_TREE         (1<<3)
+#define NO_SETUP               (1<<4)
 
 struct cmd_struct {
        const char *cmd;
@@ -290,7 +327,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
        if (!help) {
                if (p->option & RUN_SETUP)
                        prefix = setup_git_directory();
-               if (p->option & RUN_SETUP_GENTLY) {
+               else if (p->option & RUN_SETUP_GENTLY) {
                        int nongit_ok;
                        prefix = setup_git_directory_gently(&nongit_ok);
                }
@@ -332,127 +369,138 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
        return 0;
 }
 
-static void handle_internal_command(int argc, const char **argv)
+static struct cmd_struct commands[] = {
+       { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+       { "annotate", cmd_annotate, RUN_SETUP },
+       { "apply", cmd_apply, RUN_SETUP_GENTLY },
+       { "archive", cmd_archive },
+       { "bisect--helper", cmd_bisect__helper, RUN_SETUP },
+       { "blame", cmd_blame, RUN_SETUP },
+       { "branch", cmd_branch, RUN_SETUP },
+       { "bundle", cmd_bundle, RUN_SETUP_GENTLY },
+       { "cat-file", cmd_cat_file, RUN_SETUP },
+       { "check-attr", cmd_check_attr, RUN_SETUP },
+       { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
+       { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
+       { "check-ref-format", cmd_check_ref_format },
+       { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
+       { "checkout-index", cmd_checkout_index,
+               RUN_SETUP | NEED_WORK_TREE},
+       { "cherry", cmd_cherry, RUN_SETUP },
+       { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+       { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
+       { "clone", cmd_clone, NO_SETUP },
+       { "column", cmd_column, RUN_SETUP_GENTLY },
+       { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
+       { "commit-tree", cmd_commit_tree, RUN_SETUP },
+       { "config", cmd_config, RUN_SETUP_GENTLY },
+       { "count-objects", cmd_count_objects, RUN_SETUP },
+       { "credential", cmd_credential, RUN_SETUP_GENTLY },
+       { "describe", cmd_describe, RUN_SETUP },
+       { "diff", cmd_diff },
+       { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
+       { "diff-index", cmd_diff_index, RUN_SETUP },
+       { "diff-tree", cmd_diff_tree, RUN_SETUP },
+       { "fast-export", cmd_fast_export, RUN_SETUP },
+       { "fetch", cmd_fetch, RUN_SETUP },
+       { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
+       { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
+       { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
+       { "format-patch", cmd_format_patch, RUN_SETUP },
+       { "fsck", cmd_fsck, RUN_SETUP },
+       { "fsck-objects", cmd_fsck, RUN_SETUP },
+       { "gc", cmd_gc, RUN_SETUP },
+       { "get-tar-commit-id", cmd_get_tar_commit_id },
+       { "grep", cmd_grep, RUN_SETUP_GENTLY },
+       { "hash-object", cmd_hash_object },
+       { "help", cmd_help },
+       { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
+       { "init", cmd_init_db, NO_SETUP },
+       { "init-db", cmd_init_db, NO_SETUP },
+       { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP },
+       { "log", cmd_log, RUN_SETUP },
+       { "ls-files", cmd_ls_files, RUN_SETUP },
+       { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
+       { "ls-tree", cmd_ls_tree, RUN_SETUP },
+       { "mailinfo", cmd_mailinfo },
+       { "mailsplit", cmd_mailsplit },
+       { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
+       { "merge-base", cmd_merge_base, RUN_SETUP },
+       { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
+       { "merge-index", cmd_merge_index, RUN_SETUP },
+       { "merge-ours", cmd_merge_ours, RUN_SETUP },
+       { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+       { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+       { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+       { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+       { "merge-tree", cmd_merge_tree, RUN_SETUP },
+       { "mktag", cmd_mktag, RUN_SETUP },
+       { "mktree", cmd_mktree, RUN_SETUP },
+       { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
+       { "name-rev", cmd_name_rev, RUN_SETUP },
+       { "notes", cmd_notes, RUN_SETUP },
+       { "pack-objects", cmd_pack_objects, RUN_SETUP },
+       { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
+       { "pack-refs", cmd_pack_refs, RUN_SETUP },
+       { "patch-id", cmd_patch_id },
+       { "pickaxe", cmd_blame, RUN_SETUP },
+       { "prune", cmd_prune, RUN_SETUP },
+       { "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 },
+       { "remote-ext", cmd_remote_ext },
+       { "remote-fd", cmd_remote_fd },
+       { "repack", cmd_repack, RUN_SETUP },
+       { "replace", cmd_replace, RUN_SETUP },
+       { "rerere", cmd_rerere, RUN_SETUP },
+       { "reset", cmd_reset, RUN_SETUP },
+       { "rev-list", cmd_rev_list, RUN_SETUP },
+       { "rev-parse", cmd_rev_parse },
+       { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
+       { "rm", cmd_rm, RUN_SETUP },
+       { "send-pack", cmd_send_pack, RUN_SETUP },
+       { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
+       { "show", cmd_show, RUN_SETUP },
+       { "show-branch", cmd_show_branch, RUN_SETUP },
+       { "show-ref", cmd_show_ref, RUN_SETUP },
+       { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+       { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
+       { "stripspace", cmd_stripspace },
+       { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
+       { "tag", cmd_tag, RUN_SETUP },
+       { "unpack-file", cmd_unpack_file, RUN_SETUP },
+       { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
+       { "update-index", cmd_update_index, RUN_SETUP },
+       { "update-ref", cmd_update_ref, RUN_SETUP },
+       { "update-server-info", cmd_update_server_info, RUN_SETUP },
+       { "upload-archive", cmd_upload_archive },
+       { "upload-archive--writer", cmd_upload_archive_writer },
+       { "var", cmd_var, RUN_SETUP_GENTLY },
+       { "verify-commit", cmd_verify_commit, RUN_SETUP },
+       { "verify-pack", cmd_verify_pack },
+       { "verify-tag", cmd_verify_tag, RUN_SETUP },
+       { "version", cmd_version },
+       { "whatchanged", cmd_whatchanged, RUN_SETUP },
+       { "write-tree", cmd_write_tree, RUN_SETUP },
+};
+
+int is_builtin(const char *s)
+{
+       int i;
+       for (i = 0; i < ARRAY_SIZE(commands); i++) {
+               struct cmd_struct *p = commands+i;
+               if (!strcmp(s, p->cmd))
+                       return 1;
+       }
+       return 0;
+}
+
+static void handle_builtin(int argc, const char **argv)
 {
        const char *cmd = argv[0];
-       static struct cmd_struct commands[] = {
-               { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
-               { "annotate", cmd_annotate, RUN_SETUP },
-               { "apply", cmd_apply, RUN_SETUP_GENTLY },
-               { "archive", cmd_archive },
-               { "bisect--helper", cmd_bisect__helper, RUN_SETUP },
-               { "blame", cmd_blame, RUN_SETUP },
-               { "branch", cmd_branch, RUN_SETUP },
-               { "bundle", cmd_bundle, RUN_SETUP_GENTLY },
-               { "cat-file", cmd_cat_file, RUN_SETUP },
-               { "check-attr", cmd_check_attr, RUN_SETUP },
-               { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
-               { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
-               { "check-ref-format", cmd_check_ref_format },
-               { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
-               { "checkout-index", cmd_checkout_index,
-                       RUN_SETUP | NEED_WORK_TREE},
-               { "cherry", cmd_cherry, RUN_SETUP },
-               { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
-               { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
-               { "clone", cmd_clone },
-               { "column", cmd_column, RUN_SETUP_GENTLY },
-               { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
-               { "commit-tree", cmd_commit_tree, RUN_SETUP },
-               { "config", cmd_config, RUN_SETUP_GENTLY },
-               { "count-objects", cmd_count_objects, RUN_SETUP },
-               { "credential", cmd_credential, RUN_SETUP_GENTLY },
-               { "describe", cmd_describe, RUN_SETUP },
-               { "diff", cmd_diff },
-               { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
-               { "diff-index", cmd_diff_index, RUN_SETUP },
-               { "diff-tree", cmd_diff_tree, RUN_SETUP },
-               { "fast-export", cmd_fast_export, RUN_SETUP },
-               { "fetch", cmd_fetch, RUN_SETUP },
-               { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
-               { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
-               { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
-               { "format-patch", cmd_format_patch, RUN_SETUP },
-               { "fsck", cmd_fsck, RUN_SETUP },
-               { "fsck-objects", cmd_fsck, RUN_SETUP },
-               { "gc", cmd_gc, RUN_SETUP },
-               { "get-tar-commit-id", cmd_get_tar_commit_id },
-               { "grep", cmd_grep, RUN_SETUP_GENTLY },
-               { "hash-object", cmd_hash_object },
-               { "help", cmd_help },
-               { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
-               { "init", cmd_init_db },
-               { "init-db", cmd_init_db },
-               { "log", cmd_log, RUN_SETUP },
-               { "ls-files", cmd_ls_files, RUN_SETUP },
-               { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
-               { "ls-tree", cmd_ls_tree, RUN_SETUP },
-               { "mailinfo", cmd_mailinfo },
-               { "mailsplit", cmd_mailsplit },
-               { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
-               { "merge-base", cmd_merge_base, RUN_SETUP },
-               { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
-               { "merge-index", cmd_merge_index, RUN_SETUP },
-               { "merge-ours", cmd_merge_ours, RUN_SETUP },
-               { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-               { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-               { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-               { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
-               { "merge-tree", cmd_merge_tree, RUN_SETUP },
-               { "mktag", cmd_mktag, RUN_SETUP },
-               { "mktree", cmd_mktree, RUN_SETUP },
-               { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
-               { "name-rev", cmd_name_rev, RUN_SETUP },
-               { "notes", cmd_notes, RUN_SETUP },
-               { "pack-objects", cmd_pack_objects, RUN_SETUP },
-               { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
-               { "pack-refs", cmd_pack_refs, RUN_SETUP },
-               { "patch-id", cmd_patch_id },
-               { "peek-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
-               { "pickaxe", cmd_blame, RUN_SETUP },
-               { "prune", cmd_prune, RUN_SETUP },
-               { "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 },
-               { "remote-ext", cmd_remote_ext },
-               { "remote-fd", cmd_remote_fd },
-               { "repack", cmd_repack, RUN_SETUP },
-               { "replace", cmd_replace, RUN_SETUP },
-               { "repo-config", cmd_repo_config, RUN_SETUP_GENTLY },
-               { "rerere", cmd_rerere, RUN_SETUP },
-               { "reset", cmd_reset, RUN_SETUP },
-               { "rev-list", cmd_rev_list, RUN_SETUP },
-               { "rev-parse", cmd_rev_parse },
-               { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
-               { "rm", cmd_rm, RUN_SETUP },
-               { "send-pack", cmd_send_pack, RUN_SETUP },
-               { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
-               { "show", cmd_show, RUN_SETUP },
-               { "show-branch", cmd_show_branch, RUN_SETUP },
-               { "show-ref", cmd_show_ref, RUN_SETUP },
-               { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
-               { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
-               { "stripspace", cmd_stripspace },
-               { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
-               { "tag", cmd_tag, RUN_SETUP },
-               { "tar-tree", cmd_tar_tree },
-               { "unpack-file", cmd_unpack_file, RUN_SETUP },
-               { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
-               { "update-index", cmd_update_index, RUN_SETUP },
-               { "update-ref", cmd_update_ref, RUN_SETUP },
-               { "update-server-info", cmd_update_server_info, RUN_SETUP },
-               { "upload-archive", cmd_upload_archive },
-               { "upload-archive--writer", cmd_upload_archive_writer },
-               { "var", cmd_var, RUN_SETUP_GENTLY },
-               { "verify-pack", cmd_verify_pack },
-               { "verify-tag", cmd_verify_tag, RUN_SETUP },
-               { "version", cmd_version },
-               { "whatchanged", cmd_whatchanged, RUN_SETUP },
-               { "write-tree", cmd_write_tree, RUN_SETUP },
-       };
        int i;
        static const char ext[] = STRIP_EXTENSION;
 
@@ -475,6 +523,10 @@ static void handle_internal_command(int argc, const char **argv)
                struct cmd_struct *p = commands+i;
                if (strcmp(p->cmd, cmd))
                        continue;
+               if (saved_environment && (p->option & NO_SETUP)) {
+                       restore_env();
+                       break;
+               }
                exit(run_builtin(p, argc, argv));
        }
 }
@@ -520,8 +572,8 @@ 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);
+               /* See if it's a builtin */
+               handle_builtin(*argcp, *argv);
 
                /* .. then try the external ones */
                execv_dashed_external(*argv);
@@ -530,7 +582,10 @@ static int run_argv(int *argcp, const char ***argv)
                 * of overriding "git log" with "git show" by having
                 * alias.log = show
                 */
-               if (done_alias || !handle_alias(argcp, argv))
+               if (done_alias)
+                       break;
+               save_env();
+               if (!handle_alias(argcp, argv))
                        break;
                done_alias = 1;
        }
@@ -538,6 +593,26 @@ static int run_argv(int *argcp, const char ***argv)
        return done_alias;
 }
 
+/*
+ * Many parts of Git have subprograms communicate via pipe, expect the
+ * upstream of a pipe to die with SIGPIPE when the downstream of a
+ * pipe does not need to read all that is written.  Some third-party
+ * programs that ignore or block SIGPIPE for their own reason forget
+ * to restore SIGPIPE handling to the default before spawning Git and
+ * break this carefully orchestrated machinery.
+ *
+ * Restore the way SIGPIPE is handled to default, which is what we
+ * expect.
+ */
+static void restore_sigpipe_to_default(void)
+{
+       sigset_t unblock;
+
+       sigemptyset(&unblock);
+       sigaddset(&unblock, SIGPIPE);
+       sigprocmask(SIG_UNBLOCK, &unblock, NULL);
+       signal(SIGPIPE, SIG_DFL);
+}
 
 int main(int argc, char **av)
 {
@@ -557,8 +632,12 @@ int main(int argc, char **av)
         */
        sanitize_stdfds();
 
+       restore_sigpipe_to_default();
+
        git_setup_gettext();
 
+       trace_command_performance(argv);
+
        /*
         * "git-xxxx" is the same as "git xxxx", but we obviously:
         *
@@ -566,14 +645,13 @@ int main(int argc, char **av)
         *  - cannot execute it externally (since it would just do
         *    the same thing over again)
         *
-        * So we just directly call the internal command handler, and
-        * die if that one cannot handle it.
+        * So we just directly call the builtin handler, and die if
+        * that one cannot handle it.
         */
-       if (!prefixcmp(cmd, "git-")) {
-               cmd += 4;
+       if (skip_prefix(cmd, "git-", &cmd)) {
                argv[0] = cmd;
-               handle_internal_command(argc, argv);
-               die("cannot handle %s internally", cmd);
+               handle_builtin(argc, argv);
+               die("cannot handle %s as a builtin", cmd);
        }
 
        /* Look for flags.. */
@@ -581,8 +659,8 @@ int main(int argc, char **av)
        argc--;
        handle_options(&argv, &argc, NULL);
        if (argc > 0) {
-               if (!prefixcmp(argv[0], "--"))
-                       argv[0] += 2;
+               /* translate --help and --version into commands */
+               skip_prefix(argv[0], "--", &argv[0]);
        } else {
                /* The user didn't specify a command; give them help */
                commit_pager_choice();