builtin rebase: support `-C` and `--whitespace=<type>`
[gitweb.git] / builtin / rebase.c
index 0f3e156daca6a5634e52dbdbbd480738bb353dad..c2f28cdd9e32ca54dd96c76dc281e30e45e4324b 100644 (file)
@@ -95,6 +95,9 @@ struct rebase_options {
        const char *action;
        int signoff;
        int allow_rerere_autoupdate;
+       int keep_empty;
+       int autosquash;
+       char *gpg_sign_opt;
 };
 
 static int is_interactive(struct rebase_options *opts)
@@ -103,6 +106,23 @@ static int is_interactive(struct rebase_options *opts)
                opts->type == REBASE_PRESERVE_MERGES;
 }
 
+static void imply_interactive(struct rebase_options *opts, const char *option)
+{
+       switch (opts->type) {
+       case REBASE_AM:
+               die(_("%s requires an interactive rebase"), option);
+               break;
+       case REBASE_INTERACTIVE:
+       case REBASE_PRESERVE_MERGES:
+               break;
+       case REBASE_MERGE:
+               /* we silently *upgrade* --merge to --interactive if needed */
+       default:
+               opts->type = REBASE_INTERACTIVE; /* implied */
+               break;
+       }
+}
+
 /* Returns the filename prefixed by the state_dir */
 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
 {
@@ -190,6 +210,15 @@ static int read_basic_state(struct rebase_options *opts)
        } else
                opts->allow_rerere_autoupdate = -1;
 
+       if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
+               strbuf_reset(&buf);
+               if (read_one(state_dir_path("gpg_sign_opt", opts),
+                           &buf))
+                       return -1;
+               free(opts->gpg_sign_opt);
+               opts->gpg_sign_opt = xstrdup(buf.buf);
+       }
+
        strbuf_release(&buf);
 
        return 0;
@@ -276,6 +305,9 @@ static int run_specific_rebase(struct rebase_options *opts)
                opts->allow_rerere_autoupdate < 0 ? "" :
                opts->allow_rerere_autoupdate ?
                "--rerere-autoupdate" : "--no-rerere-autoupdate");
+       add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
+       add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
+       add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
 
        switch (opts->type) {
        case REBASE_AM:
@@ -436,6 +468,18 @@ static int rebase_config(const char *var, const char *value, void *data)
                return 0;
        }
 
+       if (!strcmp(var, "rebase.autosquash")) {
+               opts->autosquash = git_config_bool(var, value);
+               return 0;
+       }
+
+       if (!strcmp(var, "commit.gpgsign")) {
+               free(opts->gpg_sign_opt);
+               opts->gpg_sign_opt = git_config_bool(var, value) ?
+                       xstrdup("-S") : NULL;
+               return 0;
+       }
+
        return git_default_config(var, value, data);
 }
 
@@ -529,6 +573,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
        int committer_date_is_author_date = 0;
        int ignore_date = 0;
        int ignore_whitespace = 0;
+       const char *gpg_sign = NULL;
+       int opt_c = -1;
+       struct string_list whitespace = STRING_LIST_INIT_NODUP;
        struct option builtin_rebase_options[] = {
                OPT_STRING(0, "onto", &options.onto_name,
                           N_("revision"),
@@ -588,6 +635,18 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
                         &options.allow_rerere_autoupdate,
                         N_("allow rerere to update index  with resolved "
                            "conflict")),
+               OPT_BOOL('k', "keep-empty", &options.keep_empty,
+                        N_("preserve empty commits during rebase")),
+               OPT_BOOL(0, "autosquash", &options.autosquash,
+                        N_("move commits that begin with "
+                           "squash!/fixup! under -i")),
+               { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
+                       N_("GPG-sign commits"),
+                       PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
+               OPT_STRING_LIST(0, "whitespace", &whitespace,
+                               N_("whitespace"), N_("passed to 'git apply'")),
+               OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
+                           REBASE_AM),
                OPT_END(),
        };
 
@@ -787,6 +846,31 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
                options.flags |= REBASE_FORCE;
        }
 
+       if (options.keep_empty)
+               imply_interactive(&options, "--keep-empty");
+
+       if (gpg_sign) {
+               free(options.gpg_sign_opt);
+               options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
+       }
+
+       if (opt_c >= 0)
+               strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
+
+       if (whitespace.nr) {
+               int i;
+
+               for (i = 0; i < whitespace.nr; i++) {
+                       const char *item = whitespace.items[i].string;
+
+                       strbuf_addf(&options.git_am_opt, " --whitespace=%s",
+                                   item);
+
+                       if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
+                               options.flags |= REBASE_FORCE;
+               }
+       }
+
        switch (options.type) {
        case REBASE_MERGE:
        case REBASE_INTERACTIVE:
@@ -1012,5 +1096,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 cleanup:
        strbuf_release(&revisions);
        free(options.head_name);
+       free(options.gpg_sign_opt);
        return ret;
 }