Merge branch 'xy/format-patch-base'
authorJunio C Hamano <gitster@pobox.com>
Mon, 23 May 2016 21:54:31 +0000 (14:54 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 May 2016 21:54:31 +0000 (14:54 -0700)
"git format-patch" learned a new "--base" option to record what
(public, well-known) commit the original series was built on in
its output.

* xy/format-patch-base:
format-patch: introduce format.useAutoBase configuration
format-patch: introduce --base=auto option
format-patch: add '--base' option to record base tree info
patch-ids: make commit_patch_id() a public helper function

1  2 
Documentation/config.txt
builtin/log.c
t/t4014-format-patch.sh
diff --combined Documentation/config.txt
index 54253d163590091cba2d1f3ed5215c59b65a81ae,fb31ae5b3ccf8139e510247b9af6adcde51a38eb..ea928a729fc780e58a6621f5982684751e2a2087
@@@ -81,16 -81,13 +81,16 @@@ Include
  
  You can include one config file from another by setting the special
  `include.path` variable to the name of the file to be included. The
 +variable takes a pathname as its value, and is subject to tilde
 +expansion.
 +
 +The
  included file is expanded immediately, as if its contents had been
  found at the location of the include directive. If the value of the
  `include.path` variable is a relative path, the path is considered to be
  relative to the configuration file in which the include directive was
 -found. The value of `include.path` is subject to tilde expansion: `~/`
 -is expanded to the value of `$HOME`, and `~user/` to the specified
 -user's home directory. See below for examples.
 +found.  See below for examples.
 +
  
  Example
  ~~~~~~~
        [include]
                path = /path/to/foo.inc ; include by absolute path
                path = foo ; expand "foo" relative to the current file
 -              path = ~/foo ; expand "foo" in your $HOME directory
 +              path = ~/foo ; expand "foo" in your `$HOME` directory
  
  
  Values
@@@ -172,13 -169,6 +172,13 @@@ thing on the same output line (e.g. ope
  list of branch names in `log --decorate` output) is set to be
  painted with `bold` or some other attribute.
  
 +pathname::
 +      A variable that takes a pathname value can be given a
 +      string that begins with "`~/`" or "`~user/`", and the usual
 +      tilde expansion happens to such a string: `~/`
 +      is expanded to the value of `$HOME`, and `~user/` to the
 +      specified user's home directory.
 +
  
  Variables
  ~~~~~~~~~
@@@ -279,12 -269,6 +279,12 @@@ See linkgit:git-update-index[1]
  +
  The default is true (when core.filemode is not specified in the config file).
  
 +core.hideDotFiles::
 +      (Windows-only) If true, mark newly-created directories and files whose
 +      name starts with a dot as hidden.  If 'dotGitOnly', only the `.git/`
 +      directory is hidden, but no other files starting with a dot.  The
 +      default mode is 'dotGitOnly'.
 +
  core.ignoreCase::
        If true, this option enables various workarounds to enable
        Git to work better on filesystems that are not case sensitive,
@@@ -353,9 -337,9 +353,9 @@@ core.quotePath:
  
  core.eol::
        Sets the line ending type to use in the working directory for
 -      files that have the `text` property set.  Alternatives are
 -      'lf', 'crlf' and 'native', which uses the platform's native
 -      line ending.  The default value is `native`.  See
 +      files that have the `text` property set when core.autocrlf is false.
 +      Alternatives are 'lf', 'crlf' and 'native', which uses the platform's
 +      native line ending.  The default value is `native`.  See
        linkgit:gitattributes[5] for more information on end-of-line
        conversion.
  
@@@ -502,10 -486,10 +502,10 @@@ repository's usual working tree)
  
  core.logAllRefUpdates::
        Enable the reflog. Updates to a ref <ref> is logged to the file
 -      "$GIT_DIR/logs/<ref>", by appending the new and old
 +      "`$GIT_DIR/logs/<ref>`", by appending the new and old
        SHA-1, the date/time and the reason of the update, but
        only when the file exists.  If this configuration
 -      variable is set to true, missing "$GIT_DIR/logs/<ref>"
 +      variable is set to true, missing "`$GIT_DIR/logs/<ref>`"
        file is automatically created for branch heads (i.e. under
        refs/heads/), remote refs (i.e. under refs/remotes/),
        note refs (i.e. under refs/notes/), and the symbolic ref HEAD.
@@@ -609,11 -593,12 +609,11 @@@ be delta compressed, but larger binary 
  Common unit suffixes of 'k', 'm', or 'g' are supported.
  
  core.excludesFile::
 -      In addition to '.gitignore' (per-directory) and
 -      '.git/info/exclude', Git looks into this file for patterns
 -      of files which are not meant to be tracked.  "`~/`" is expanded
 -      to the value of `$HOME` and "`~user/`" to the specified user's
 -      home directory. Its default value is $XDG_CONFIG_HOME/git/ignore.
 -      If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore
 +      Specifies the pathname to the file that contains patterns to
 +      describe paths that are not meant to be tracked, in addition
 +      to '.gitignore' (per-directory) and '.git/info/exclude'.
 +      Defaults to `$XDG_CONFIG_HOME/git/ignore`.
 +      If `$XDG_CONFIG_HOME` is either not set or empty, `$HOME/.config/git/ignore`
        is used instead. See linkgit:gitignore[5].
  
  core.askPass::
@@@ -630,25 -615,8 +630,25 @@@ core.attributesFile:
        '.git/info/attributes', Git looks into this file for attributes
        (see linkgit:gitattributes[5]). Path expansions are made the same
        way as for `core.excludesFile`. Its default value is
 -      $XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME is either not
 -      set or empty, $HOME/.config/git/attributes is used instead.
 +      `$XDG_CONFIG_HOME/git/attributes`. If `$XDG_CONFIG_HOME` is either not
 +      set or empty, `$HOME/.config/git/attributes` is used instead.
 +
 +core.hooksPath::
 +      By default Git will look for your hooks in the
 +      '$GIT_DIR/hooks' directory. Set this to different path,
 +      e.g. '/etc/git/hooks', and Git will try to find your hooks in
 +      that directory, e.g. '/etc/git/hooks/pre-receive' instead of
 +      in '$GIT_DIR/hooks/pre-receive'.
 ++
 +The path can be either absolute or relative. A relative path is
 +taken as relative to the directory where the hooks are run (see
 +the "DESCRIPTION" section of linkgit:githooks[5]).
 ++
 +This configuration variable is useful in cases where you'd like to
 +centrally configure your Git hooks instead of configuring them on a
 +per-repository basis, or as a more flexible and centralized
 +alternative to having an `init.templateDir` where you've changed
 +default hooks.
  
  core.editor::
        Commands such as `commit` and `tag` that lets you edit
@@@ -1138,15 -1106,15 +1138,15 @@@ commit.status:
        message.  Defaults to true.
  
  commit.template::
 -      Specify a file to use as the template for new commit messages.
 -      "`~/`" is expanded to the value of `$HOME` and "`~user/`" to the
 -      specified user's home directory.
 +      Specify the pathname of a file to use as the template for
 +      new commit messages.
  
  credential.helper::
        Specify an external helper to be called when a username or
        password credential is needed; the helper may consult external
 -      storage to avoid prompting the user for the credentials. See
 -      linkgit:gitcredentials[7] for details.
 +      storage to avoid prompting the user for the credentials. Note
 +      that multiple helpers may be defined. See linkgit:gitcredentials[7]
 +      for details.
  
  credential.useHttpPath::
        When acquiring credentials, consider the "path" component of an http
@@@ -1290,6 -1258,10 +1290,10 @@@ format.outputDirectory:
        Set a custom directory to store the resulting files instead of the
        current working directory.
  
+ format.useAutoBase::
+       A boolean value which lets you enable the `--base=auto` option of
+       format-patch by default.
  filter.<driver>.clean::
        The command which is used to convert the content of a worktree
        file to a blob upon checkin.  See linkgit:gitattributes[5] for
@@@ -1366,7 -1338,7 +1370,7 @@@ gc.worktreePruneExpire:
        'git worktree prune --expire 3.months.ago'.
        This config variable can be used to set a different grace
        period. The value "now" may be used to disable the grace
 -      period and prune $GIT_DIR/worktrees immediately, or "never"
 +      period and prune `$GIT_DIR/worktrees` immediately, or "never"
        may be used to suppress pruning.
  
  gc.reflogExpire::
@@@ -1506,13 -1478,13 +1510,13 @@@ grep.fallbackToNoIndex:
        is executed outside of a git repository.  Defaults to false.
  
  gpg.program::
 -      Use this custom program instead of "gpg" found on $PATH when
 +      Use this custom program instead of "`gpg`" found on `$PATH` when
        making or verifying a PGP signature. The program must support the
        same command-line interface as GPG, namely, to verify a detached
 -      signature, "gpg --verify $file - <$signature" is run, and the
 +      signature, "`gpg --verify $file - <$signature`" is run, and the
        program is expected to signal a good signature by exiting with
        code 0, and to generate an ASCII-armored detached signature, the
 -      standard input of "gpg -bsau $key" is fed with the contents to be
 +      standard input of "`gpg -bsau $key`" is fed with the contents to be
        signed, and the program is expected to send the result to its
        standard output.
  
@@@ -1525,7 -1497,7 +1529,7 @@@ gui.diffContext:
        made by the linkgit:git-gui[1]. The default is "5".
  
  gui.displayUntracked::
 -      Determines if linkgit::git-gui[1] shows untracked files
 +      Determines if linkgit:git-gui[1] shows untracked files
        in the file list. The default is "true".
  
  gui.encoding::
@@@ -1686,19 -1658,12 +1690,19 @@@ http.emptyAuth:
        a username in the URL, as libcurl normally requires a username for
        authentication.
  
 +http.extraHeader::
 +      Pass an additional HTTP header when communicating with a server.  If
 +      more than one such entry exists, all of them are added as extra
 +      headers.  To allow overriding the settings inherited from the system
 +      config, an empty value will reset the extra headers to the empty list.
 +
  http.cookieFile::
 -      File containing previously stored cookie lines which should be used
 +      The pathname of a file containing previously stored cookie lines,
 +      which should be used
        in the Git http session, if they match the server. The file format
        of the file to read cookies from should be plain HTTP headers or
 -      the Netscape/Mozilla cookie file format (see linkgit:curl[1]).
 -      NOTE that the file specified with http.cookieFile is only used as
 +      the Netscape/Mozilla cookie file format (see `curl(1)`).
 +      NOTE that the file specified with http.cookieFile is used only as
        input unless http.saveCookies is set.
  
  http.saveCookies::
@@@ -1925,14 -1890,6 +1929,14 @@@ interactive.singleKey:
        setting is silently ignored if portable keystroke input
        is not available; requires the Perl module Term::ReadKey.
  
 +interactive.diffFilter::
 +      When an interactive command (such as `git add --patch`) shows
 +      a colorized diff, git will pipe the diff through the shell
 +      command defined by this configuration variable. The command may
 +      mark up the diff further for human consumption, provided that it
 +      retains a one-to-one correspondence with the lines in the
 +      original diff. Defaults to disabled (no filtering).
 +
  log.abbrevCommit::
        If true, makes linkgit:git-log[1], linkgit:git-show[1], and
        linkgit:git-whatchanged[1] assume `--abbrev-commit`. You may
@@@ -2194,11 -2151,8 +2198,11 @@@ pack.packSizeLimit:
        The maximum size of a pack.  This setting only affects
        packing to a file when repacking, i.e. the git:// protocol
        is unaffected.  It can be overridden by the `--max-pack-size`
 -      option of linkgit:git-repack[1]. The minimum size allowed is
 -      limited to 1 MiB. The default is unlimited.
 +      option of linkgit:git-repack[1].  Reaching this limit results
 +      in the creation of multiple packfiles; which in turn prevents
 +      bitmaps from being created.
 +      The minimum size allowed is limited to 1 MiB.
 +      The default is unlimited.
        Common unit suffixes of 'k', 'm', or 'g' are
        supported.
  
@@@ -2598,9 -2552,8 +2602,9 @@@ repack.writeBitmaps:
        objects to disk (e.g., when `git repack -a` is run).  This
        index can speed up the "counting objects" phase of subsequent
        packs created for clones and fetches, at the cost of some disk
 -      space and extra time spent on the initial repack.  Defaults to
 -      false.
 +      space and extra time spent on the initial repack.  This has
 +      no effect if multiple packfiles are created.
 +      Defaults to false.
  
  rerere.autoUpdate::
        When set to true, `git-rerere` updates the index with the
@@@ -2780,17 -2733,6 +2784,17 @@@ submodule.<name>.ignore:
        "--ignore-submodules" option. The 'git submodule' commands are not
        affected by this setting.
  
 +submodule.fetchJobs::
 +      Specifies how many submodules are fetched/cloned at the same time.
 +      A positive integer allows up to that number of submodules fetched
 +      in parallel. A value of 0 will give some reasonable default.
 +      If unset, it defaults to 1.
 +
 +tag.forceSignAnnotated::
 +      A boolean to specify whether annotated tags created should be GPG signed.
 +      If `--annotate` is specified on the command line, it takes
 +      precedence over this option.
 +
  tag.sort::
        This variable controls the sort ordering of tags when displayed by
        linkgit:git-tag[1]. Without the "--sort=<value>" option provided, the
diff --combined builtin/log.c
index dff3fbbb437c462e51544dbf1d4781c550a14d1a,acfcc05b77d56e84f176d8be8d79984a66a93255..099f4f7be92c6d4371dcc8cba1322351c6840439
@@@ -100,12 -100,6 +100,12 @@@ static int log_line_range_callback(cons
        return 0;
  }
  
 +static void init_log_defaults(void)
 +{
 +      init_grep_defaults();
 +      init_diff_ui_defaults();
 +}
 +
  static void cmd_log_init_defaults(struct rev_info *rev)
  {
        if (fmt_pretty)
@@@ -422,7 -416,7 +422,7 @@@ int cmd_whatchanged(int argc, const cha
        struct rev_info rev;
        struct setup_revision_opt opt;
  
 -      init_grep_defaults();
 +      init_log_defaults();
        git_config(git_log_config, NULL);
  
        init_revisions(&rev, prefix);
@@@ -533,7 -527,7 +533,7 @@@ int cmd_show(int argc, const char **arg
        struct pathspec match_all;
        int i, count, ret = 0;
  
 -      init_grep_defaults();
 +      init_log_defaults();
        git_config(git_log_config, NULL);
  
        memset(&match_all, 0, sizeof(match_all));
@@@ -614,7 -608,7 +614,7 @@@ int cmd_log_reflog(int argc, const cha
        struct rev_info rev;
        struct setup_revision_opt opt;
  
 -      init_grep_defaults();
 +      init_log_defaults();
        git_config(git_log_config, NULL);
  
        init_revisions(&rev, prefix);
@@@ -653,7 -647,7 +653,7 @@@ int cmd_log(int argc, const char **argv
        struct rev_info rev;
        struct setup_revision_opt opt;
  
 -      init_grep_defaults();
 +      init_log_defaults();
        git_config(git_log_config, NULL);
  
        init_revisions(&rev, prefix);
@@@ -702,6 -696,7 +702,7 @@@ static void add_header(const char *valu
  #define THREAD_DEEP 2
  static int thread;
  static int do_signoff;
+ static int base_auto;
  static const char *signature = git_version_string;
  static const char *signature_file;
  static int config_cover_letter;
@@@ -786,6 -781,10 +787,10 @@@ static int git_format_config(const cha
        }
        if (!strcmp(var, "format.outputdirectory"))
                return git_config_string(&config_output_directory, var, value);
+       if (!strcmp(var, "format.useautobase")) {
+               base_auto = git_config_bool(var, value);
+               return 0;
+       }
  
        return git_log_config(var, value, cb);
  }
@@@ -1191,6 -1190,155 +1196,155 @@@ static int from_callback(const struct o
        return 0;
  }
  
+ struct base_tree_info {
+       struct object_id base_commit;
+       int nr_patch_id, alloc_patch_id;
+       struct object_id *patch_id;
+ };
+ static struct commit *get_base_commit(const char *base_commit,
+                                     struct commit **list,
+                                     int total)
+ {
+       struct commit *base = NULL;
+       struct commit **rev;
+       int i = 0, rev_nr = 0;
+       if (base_commit && strcmp(base_commit, "auto")) {
+               base = lookup_commit_reference_by_name(base_commit);
+               if (!base)
+                       die(_("Unknown commit %s"), base_commit);
+       } else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) {
+               struct branch *curr_branch = branch_get(NULL);
+               const char *upstream = branch_get_upstream(curr_branch, NULL);
+               if (upstream) {
+                       struct commit_list *base_list;
+                       struct commit *commit;
+                       unsigned char sha1[20];
+                       if (get_sha1(upstream, sha1))
+                               die(_("Failed to resolve '%s' as a valid ref."), upstream);
+                       commit = lookup_commit_or_die(sha1, "upstream base");
+                       base_list = get_merge_bases_many(commit, total, list);
+                       /* There should be one and only one merge base. */
+                       if (!base_list || base_list->next)
+                               die(_("Could not find exact merge base."));
+                       base = base_list->item;
+                       free_commit_list(base_list);
+               } else {
+                       die(_("Failed to get upstream, if you want to record base commit automatically,\n"
+                             "please use git branch --set-upstream-to to track a remote branch.\n"
+                             "Or you could specify base commit by --base=<base-commit-id> manually."));
+               }
+       }
+       ALLOC_ARRAY(rev, total);
+       for (i = 0; i < total; i++)
+               rev[i] = list[i];
+       rev_nr = total;
+       /*
+        * Get merge base through pair-wise computations
+        * and store it in rev[0].
+        */
+       while (rev_nr > 1) {
+               for (i = 0; i < rev_nr / 2; i++) {
+                       struct commit_list *merge_base;
+                       merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
+                       if (!merge_base || merge_base->next)
+                               die(_("Failed to find exact merge base"));
+                       rev[i] = merge_base->item;
+               }
+               if (rev_nr % 2)
+                       rev[i] = rev[2 * i];
+               rev_nr = (rev_nr + 1) / 2;
+       }
+       if (!in_merge_bases(base, rev[0]))
+               die(_("base commit should be the ancestor of revision list"));
+       for (i = 0; i < total; i++) {
+               if (base == list[i])
+                       die(_("base commit shouldn't be in revision list"));
+       }
+       free(rev);
+       return base;
+ }
+ static void prepare_bases(struct base_tree_info *bases,
+                         struct commit *base,
+                         struct commit **list,
+                         int total)
+ {
+       struct commit *commit;
+       struct rev_info revs;
+       struct diff_options diffopt;
+       int i;
+       if (!base)
+               return;
+       diff_setup(&diffopt);
+       DIFF_OPT_SET(&diffopt, RECURSIVE);
+       diff_setup_done(&diffopt);
+       oidcpy(&bases->base_commit, &base->object.oid);
+       init_revisions(&revs, NULL);
+       revs.max_parents = 1;
+       revs.topo_order = 1;
+       for (i = 0; i < total; i++) {
+               list[i]->object.flags &= ~UNINTERESTING;
+               add_pending_object(&revs, &list[i]->object, "rev_list");
+               list[i]->util = (void *)1;
+       }
+       base->object.flags |= UNINTERESTING;
+       add_pending_object(&revs, &base->object, "base");
+       if (prepare_revision_walk(&revs))
+               die(_("revision walk setup failed"));
+       /*
+        * Traverse the commits list, get prerequisite patch ids
+        * and stuff them in bases structure.
+        */
+       while ((commit = get_revision(&revs)) != NULL) {
+               unsigned char sha1[20];
+               struct object_id *patch_id;
+               if (commit->util)
+                       continue;
+               if (commit_patch_id(commit, &diffopt, sha1))
+                       die(_("cannot get patch id"));
+               ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
+               patch_id = bases->patch_id + bases->nr_patch_id;
+               hashcpy(patch_id->hash, sha1);
+               bases->nr_patch_id++;
+       }
+ }
+ static void print_bases(struct base_tree_info *bases)
+ {
+       int i;
+       /* Only do this once, either for the cover or for the first one */
+       if (is_null_oid(&bases->base_commit))
+               return;
+       /* Show the base commit */
+       printf("base-commit: %s\n", oid_to_hex(&bases->base_commit));
+       /* Show the prerequisite patches */
+       for (i = bases->nr_patch_id - 1; i >= 0; i--)
+               printf("prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
+       free(bases->patch_id);
+       bases->nr_patch_id = 0;
+       bases->alloc_patch_id = 0;
+       oidclr(&bases->base_commit);
+ }
  int cmd_format_patch(int argc, const char **argv, const char *prefix)
  {
        struct commit *commit;
        int reroll_count = -1;
        char *branch_name = NULL;
        char *from = NULL;
+       char *base_commit = NULL;
+       struct base_tree_info bases;
        const struct option builtin_format_patch_options[] = {
                { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
                            N_("use [PATCH n/m] even with a single patch"),
                            PARSE_OPT_OPTARG, thread_callback },
                OPT_STRING(0, "signature", &signature, N_("signature"),
                            N_("add a signature")),
+               OPT_STRING(0, "base", &base_commit, N_("base-commit"),
+                          N_("add prerequisite tree info to the patch series")),
                OPT_FILENAME(0, "signature-file", &signature_file,
                                N_("add a signature from a file")),
                OPT__QUIET(&quiet, N_("don't print the patch filenames")),
        extra_hdr.strdup_strings = 1;
        extra_to.strdup_strings = 1;
        extra_cc.strdup_strings = 1;
 -      init_grep_defaults();
 +      init_log_defaults();
        git_config(git_format_config, NULL);
        init_revisions(&rev, prefix);
        rev.commit_format = CMIT_FMT_EMAIL;
 +      rev.expand_tabs_in_log_default = 0;
        rev.verbose_header = 1;
        rev.diff = 1;
        rev.max_parents = 1;
                signature = strbuf_detach(&buf, NULL);
        }
  
+       memset(&bases, 0, sizeof(bases));
+       if (base_commit || base_auto) {
+               struct commit *base = get_base_commit(base_commit, list, nr);
+               reset_revision_walk();
+               prepare_bases(&bases, base, list, nr);
+       }
        if (in_reply_to || thread || cover_letter)
                rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
        if (in_reply_to) {
                        gen_message_id(&rev, "cover");
                make_cover_letter(&rev, use_stdout,
                                  origin, nr, list, branch_name, quiet);
+               print_bases(&bases);
                total++;
                start_number--;
        }
                                       rev.mime_boundary);
                        else
                                print_signature();
+                       print_bases(&bases);
                }
                if (!use_stdout)
                        fclose(stdout);
diff --combined t/t4014-format-patch.sh
index eed2981b96df71c6acdc0f90f8b477561241a9a3,ab98cefe09078d91814c85074e4162bfb53b01ad..8049cad374827d15ec098218b01acb59b65383ee
@@@ -549,7 -549,7 +549,7 @@@ test_expect_success 'cover-letter inher
  
        git mv file foo &&
        git commit -m foo &&
 -      git format-patch --cover-letter -1 &&
 +      git format-patch --no-renames --cover-letter -1 &&
        check_patch 0000-cover-letter.patch &&
        ! grep "file => foo .* 0 *\$" 0000-cover-letter.patch &&
        git format-patch --cover-letter -1 -M &&
@@@ -703,7 -703,7 +703,7 @@@ test_expect_success 'options no longer 
  
  test_expect_success 'format-patch --numstat should produce a patch' '
        git format-patch --numstat --stdout master..side > output &&
 -      test 6 = $(grep "^diff --git a/" output | wc -l)'
 +      test 5 = $(grep "^diff --git a/" output | wc -l)'
  
  test_expect_success 'format-patch -- <path>' '
        git format-patch master..side -- file 2>error &&
@@@ -1460,4 -1460,109 +1460,109 @@@ test_expect_success 'format-patch -o ov
        test_path_is_dir patchset
  '
  
+ test_expect_success 'format-patch --base' '
+       git checkout side &&
+       git format-patch --stdout --base=HEAD~3 -1 >patch &&
+       grep "^base-commit:" patch >actual &&
+       grep "^prerequisite-patch-id:" patch >>actual &&
+       echo "base-commit: $(git rev-parse HEAD~3)" >expected &&
+       echo "prerequisite-patch-id: $(git show --patch HEAD~2 | git patch-id --stable | awk "{print \$1}")" >>expected &&
+       echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --stable | awk "{print \$1}")" >>expected &&
+       test_cmp expected actual
+ '
+ test_expect_success 'format-patch --base errors out when base commit is in revision list' '
+       test_must_fail git format-patch --base=HEAD -2 &&
+       test_must_fail git format-patch --base=HEAD~1 -2 &&
+       git format-patch --stdout --base=HEAD~2 -2 >patch &&
+       grep "^base-commit:" patch >actual &&
+       echo "base-commit: $(git rev-parse HEAD~2)" >expected &&
+       test_cmp expected actual
+ '
+ test_expect_success 'format-patch --base errors out when base commit is not ancestor of revision list' '
+       # For history as below:
+       #
+       #    ---Q---P---Z---Y---*---X
+       #        \             /
+       #         ------------W
+       #
+       # If "format-patch Z..X" is given, P and Z can not be specified as the base commit
+       git checkout -b topic1 master &&
+       git rev-parse HEAD >commit-id-base &&
+       test_commit P &&
+       git rev-parse HEAD >commit-id-P &&
+       test_commit Z &&
+       git rev-parse HEAD >commit-id-Z &&
+       test_commit Y &&
+       git checkout -b topic2 master &&
+       test_commit W &&
+       git merge topic1 &&
+       test_commit X &&
+       test_must_fail git format-patch --base=$(cat commit-id-P) -3 &&
+       test_must_fail git format-patch --base=$(cat commit-id-Z) -3 &&
+       git format-patch --stdout --base=$(cat commit-id-base) -3 >patch &&
+       grep "^base-commit:" patch >actual &&
+       echo "base-commit: $(cat commit-id-base)" >expected &&
+       test_cmp expected actual
+ '
+ test_expect_success 'format-patch --base=auto' '
+       git checkout -b upstream master &&
+       git checkout -b local upstream &&
+       git branch --set-upstream-to=upstream &&
+       test_commit N1 &&
+       test_commit N2 &&
+       git format-patch --stdout --base=auto -2 >patch &&
+       grep "^base-commit:" patch >actual &&
+       echo "base-commit: $(git rev-parse upstream)" >expected &&
+       test_cmp expected actual
+ '
+ test_expect_success 'format-patch errors out when history involves criss-cross' '
+       # setup criss-cross history
+       #
+       #   B---M1---D
+       #  / \ /
+       # A   X
+       #  \ / \
+       #   C---M2---E
+       #
+       git checkout master &&
+       test_commit A &&
+       git checkout -b xb master &&
+       test_commit B &&
+       git checkout -b xc master &&
+       test_commit C &&
+       git checkout -b xbc xb -- &&
+       git merge xc &&
+       git checkout -b xcb xc -- &&
+       git branch --set-upstream-to=xbc &&
+       git merge xb &&
+       git checkout xbc &&
+       test_commit D &&
+       git checkout xcb &&
+       test_commit E &&
+       test_must_fail  git format-patch --base=auto -1
+ '
+ test_expect_success 'format-patch format.useAutoBaseoption' '
+       test_when_finished "git config --unset format.useAutoBase" &&
+       git checkout local &&
+       git config format.useAutoBase true &&
+       git format-patch --stdout -1 >patch &&
+       grep "^base-commit:" patch >actual &&
+       echo "base-commit: $(git rev-parse upstream)" >expected &&
+       test_cmp expected actual
+ '
+ test_expect_success 'format-patch --base overrides format.useAutoBase' '
+       test_when_finished "git config --unset format.useAutoBase" &&
+       git config format.useAutoBase true &&
+       git format-patch --stdout --base=HEAD~1 -1 >patch &&
+       grep "^base-commit:" patch >actual &&
+       echo "base-commit: $(git rev-parse HEAD~1)" >expected &&
+       test_cmp expected actual
+ '
  test_done