Merge branch 'rs/cleanup-strbuf-users'
authorJunio C Hamano <gitster@pobox.com>
Thu, 5 Oct 2017 04:48:19 +0000 (13:48 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Oct 2017 04:48:19 +0000 (13:48 +0900)
Code clean-up.

* rs/cleanup-strbuf-users:
graph: use strbuf_addchars() to add spaces
use strbuf_addstr() for adding strings to strbufs
path: use strbuf_add_real_path()

73 files changed:
.clang-format
Documentation/RelNotes/2.15.0.txt
Documentation/git-add.txt
Documentation/git-branch.txt
Documentation/git-grep.txt
Documentation/git-rebase.txt
Documentation/git-status.txt
Documentation/git.txt
Documentation/glossary-content.txt
Documentation/technical/api-string-list.txt [deleted file]
Makefile
abspath.c
builtin/branch.c
builtin/checkout.c
builtin/commit.c
builtin/get-tar-commit-id.c
builtin/rebase--helper.c
builtin/receive-pack.c
builtin/rev-parse.c
builtin/worktree.c
bulk-checkin.c
cache.h
config.c
config.h
config.mak.uname
csum-file.c
diff.c
environment.c
fast-import.c
git-rebase--interactive.sh
git-rebase.sh
git.c
notes-merge.c
pack-write.c
packfile.c
parse-options.c
path.c
pkt-line.c
ref-filter.c
reflog-walk.c
refs.c
refs.h
refs/files-backend.c
refs/iterator.c
refs/packed-backend.c
refs/ref-cache.c
refs/ref-cache.h
refs/refs-internal.h
sequencer.c
sequencer.h
setup.c
sha1_file.c
string-list.h
submodule.c
t/helper/test-parse-options.c
t/t0040-parse-options.sh
t/t1502-rev-parse-parseopt.sh
t/t3200-branch.sh
t/t3404-rebase-interactive.sh
t/t3415-rebase-autosquash.sh
t/t4013-diff-various.sh
t/t4013/diff.diff-tree_--stat_initial_mode [new file with mode: 0644]
t/t4013/diff.diff-tree_--summary_initial_mode [new file with mode: 0644]
t/t4013/diff.diff-tree_initial_mode [new file with mode: 0644]
t/t4013/diff.log_--decorate=full_--all
t/t4013/diff.log_--decorate_--all
t/t4059-diff-submodule-not-initialized.sh
t/t6132-pathspec-exclude.sh
t/t7406-submodule-update.sh
t/t7508-status.sh
t/t9300-fast-import.sh
transport.c
wt-status.c
index 3ede2628d2d1b6060c50cd1ed026be2dbdd4dd6d..56822c116b13c6782d4488fed6d3e21d01c18f35 100644 (file)
@@ -153,13 +153,13 @@ KeepEmptyLinesAtTheStartOfBlocks: false
 
 # Penalties
 # This decides what order things should be done if a line is too long
-PenaltyBreakAssignment: 100
-PenaltyBreakBeforeFirstCallParameter: 100
-PenaltyBreakComment: 100
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
 PenaltyBreakFirstLessLess: 0
-PenaltyBreakString: 100
-PenaltyExcessCharacter: 5
-PenaltyReturnTypeOnItsOwnLine: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 5
 
 # Don't sort #include's
 SortIncludes: false
index 8a869e4ef1d6a620bbb76e85540f9e36e6b95fb2..face5e0b0236c6f28ffecee61f92693ddc11d7a4 100644 (file)
@@ -87,6 +87,14 @@ UI, Workflows & Features
  * "git describe --match <pattern>" has been taught to play well with
    the "--all" option.
 
+ * "git branch" learned "-c/-C" to create a new branch by copying an
+   existing one.
+
+ * Some commands (most notably "git status") makes an opportunistic
+   update when performing a read-only operation to help optimize later
+   operations in the same repository.  The new "--no-optional-locks"
+   option can be passed to Git to disable them.
+
 
 Performance, Internal Implementation, Development Support etc.
 
@@ -203,6 +211,14 @@ Performance, Internal Implementation, Development Support etc.
    the directory, which is unnecessary.  The codepath has been
    optimized to avoid this overhead.
 
+ * The final batch to "git rebase -i" updates to move more code from
+   the shell script to C has been merged.
+
+ * Operations that do not touch (majority of) packed refs have been
+   optimized by making accesses to packed-refs file lazy; we no longer
+   pre-parse everything, and an access to a single ref in the
+   packed-refs does not touch majority of irrelevant refs, either.
+
 Also contains various documentation updates and code clean-ups.
 
 
@@ -421,6 +437,11 @@ Fixes since v2.14
  * Memory leaks in various codepaths have been plugged.
    (merge 4d01a7fa65 ma/leakplugs later to maint).
 
+ * Recent versions of "git rev-parse --parseopt" did not parse the
+   option specification that does not have the optional flags (*=?!)
+   correctly, which has been corrected.
+   (merge a6304fa4c2 bc/rev-parse-parseopt-fix later to maint).
+
  * Other minor doc, test and build updates and code cleanups.
    (merge f094b89a4d ma/parse-maybe-bool later to maint).
    (merge 39b00fa4d4 jk/drop-sha1-entry-pos later to maint).
@@ -441,3 +462,6 @@ Fixes since v2.14
    (merge 217bb56d4f hn/typofix later to maint).
    (merge c08fd6388c jk/doc-read-tree-table-asciidoctor-fix later to maint).
    (merge c3342b362e ks/doc-use-camelcase-for-config-name later to maint).
+   (merge 0bca165fdb jk/validate-headref-fix later to maint).
+   (merge 93dbefb389 mr/doc-negative-pathspec later to maint).
+   (merge 5e633326e4 ad/doc-markup-fix later to maint).
index f4169fb1ec4c61068cc70756c31a5abcc31496f6..b700beaff5ad2eaa407bfb3818e016a5bb27d87c 100644 (file)
@@ -61,6 +61,9 @@ OPTIONS
        the working tree.  Note that older versions of Git used
        to ignore removed files; use `--no-all` option if you want
        to add modified or new files but ignore removed ones.
++
+For more details about the <pathspec> syntax, see the 'pathspec' entry
+in linkgit:gitglossary[7].
 
 -n::
 --dry-run::
index 58f1e5c9c74e187449d6721b4d4322e9cb4733eb..fe029ac6fc1a2280893c8ad7c36582c3f891a79e 100644 (file)
@@ -18,6 +18,7 @@ SYNOPSIS
 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
 'git branch' --unset-upstream [<branchname>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
+'git branch' (-c | -C) [<oldbranch>] <newbranch>
 'git branch' (-d | -D) [-r] <branchname>...
 'git branch' --edit-description [<branchname>]
 
@@ -64,6 +65,10 @@ If <oldbranch> had a corresponding reflog, it is renamed to match
 renaming. If <newbranch> exists, -M must be used to force the rename
 to happen.
 
+The `-c` and `-C` options have the exact same semantics as `-m` and
+`-M`, except instead of the branch being renamed it along with its
+config and reflog will be copied to a new name.
+
 With a `-d` or `-D` option, `<branchname>` will be deleted.  You may
 specify more than one branch for deletion.  If the branch currently
 has a reflog then the reflog will also be deleted.
@@ -104,7 +109,7 @@ OPTIONS
        In combination with `-d` (or `--delete`), allow deleting the
        branch irrespective of its merged status. In combination with
        `-m` (or `--move`), allow renaming the branch even if the new
-       branch name already exists.
+       branch name already exists, the same applies for `-c` (or `--copy`).
 
 -m::
 --move::
@@ -113,6 +118,13 @@ OPTIONS
 -M::
        Shortcut for `--move --force`.
 
+-c::
+--copy::
+       Copy a branch and the corresponding reflog.
+
+-C::
+       Shortcut for `--copy --force`.
+
 --color[=<when>]::
        Color branches to highlight current, local, and
        remote-tracking branches.
index 720c7850e2790bf18a3c493977a2703cbeab048a..18b494731f51145e9d4a4d078264bedfd3beb00f 100644 (file)
@@ -289,6 +289,9 @@ providing this option will cause it to die.
 <pathspec>...::
        If given, limit the search to paths matching at least one pattern.
        Both leading paths match and glob(7) patterns are supported.
++
+For more details about the <pathspec> syntax, see the 'pathspec' entry
+in linkgit:gitglossary[7].
 
 Examples
 --------
@@ -305,6 +308,9 @@ Examples
        Looks for a line that has `NODE` or `Unexpected` in
        files that have lines that match both.
 
+`git grep solution -- :^Documentation`::
+       Looks for `solution`, excluding files in `Documentation`.
+
 GIT
 ---
 Part of the linkgit:git[1] suite
index 6805a74aec20cacd7b9320136b8648ad5b40ef5b..3cedfb0fd22be1c060ab2e7b9a141a0d1deb03af 100644 (file)
@@ -430,13 +430,15 @@ without an explicit `--interactive`.
 --autosquash::
 --no-autosquash::
        When the commit log message begins with "squash! ..." (or
-       "fixup! ..."), and there is a commit whose title begins with
-       the same ..., automatically modify the todo list of rebase -i
-       so that the commit marked for squashing comes right after the
-       commit to be modified, and change the action of the moved
-       commit from `pick` to `squash` (or `fixup`).  Ignores subsequent
-       "fixup! " or "squash! " after the first, in case you referred to an
-       earlier fixup/squash with `git commit --fixup/--squash`.
+       "fixup! ..."), and there is already a commit in the todo list that
+       matches the same `...`, automatically modify the todo list of rebase
+       -i so that the commit marked for squashing comes right after the
+       commit to be modified, and change the action of the moved commit
+       from `pick` to `squash` (or `fixup`).  A commit matches the `...` if
+       the commit subject matches, or if the `...` refers to the commit's
+       hash. As a fall-back, partial matches of the commit subject work,
+       too.  The recommended way to create fixup/squash commits is by using
+       the `--fixup`/`--squash` options of linkgit:git-commit[1].
 +
 This option is only valid when the `--interactive` option is used.
 +
index d47f198f15cd4c767b03c14580b816bf750e9ee1..9f3a78a36c48c55318ee0eea8e96a64ccce5bfa2 100644 (file)
@@ -111,6 +111,8 @@ configuration variable documented in linkgit:git-config[1].
        without options are equivalent to 'always' and 'never'
        respectively.
 
+<pathspec>...::
+       See the 'pathspec' entry in linkgit:gitglossary[7].
 
 OUTPUT
 ------
index 6e3a6767e5f0ce347b2363cc7829d8eab042ae0a..7a1d629ca068059d274da66728eb7f886b43081d 100644 (file)
@@ -75,7 +75,7 @@ example the following invocations are equivalent:
 Note that omitting the `=` in `git -c foo.bar ...` is allowed and sets
 `foo.bar` to the boolean true value (just like `[foo]bar` would in a
 config file). Including the equals but with an empty value (like `git -c
-foo.bar= ...`) sets `foo.bar` to the empty string which ` git config
+foo.bar= ...`) sets `foo.bar` to the empty string which `git config
 --bool` will convert to `false`.
 
 --exec-path[=<path>]::
@@ -159,6 +159,10 @@ foo.bar= ...`) sets `foo.bar` to the empty string which ` git config
        Add "icase" magic to all pathspec. This is equivalent to setting
        the `GIT_ICASE_PATHSPECS` environment variable to `1`.
 
+--no-optional-locks::
+       Do not perform optional operations that require locks. This is
+       equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`.
+
 GIT COMMANDS
 ------------
 
@@ -697,6 +701,14 @@ of clones and fetches.
        which feed potentially-untrusted URLS to git commands.  See
        linkgit:git-config[1] for more details.
 
+`GIT_OPTIONAL_LOCKS`::
+       If set to `0`, Git will complete any requested operation without
+       performing any optional sub-operations that require taking a lock.
+       For example, this will prevent `git status` from refreshing the
+       index as a side effect. This is useful for processes running in
+       the background which do not want to cause lock contention with
+       other operations on the repository.  Defaults to `1`.
+
 Discussion[[Discussion]]
 ------------------------
 
index b71b943b12eda2eab1792dfc910e405ad2ad540c..6b8888d123826179ace38660f5043d897eb5ce70 100644 (file)
@@ -407,7 +407,7 @@ these forms:
 
 exclude;;
        After a path matches any non-exclude pathspec, it will be run
-       through all exclude pathspec (magic signature: `!` or its
+       through all exclude pathspecs (magic signature: `!` or its
        synonym `^`). If it matches, the path is ignored.  When there
        is no non-exclude pathspec, the exclusion is applied to the
        result set as if invoked without any pathspec.
diff --git a/Documentation/technical/api-string-list.txt b/Documentation/technical/api-string-list.txt
deleted file mode 100644 (file)
index c08402b..0000000
+++ /dev/null
@@ -1,209 +0,0 @@
-string-list API
-===============
-
-The string_list API offers a data structure and functions to handle
-sorted and unsorted string lists.  A "sorted" list is one whose
-entries are sorted by string value in `strcmp()` order.
-
-The 'string_list' struct used to be called 'path_list', but was renamed
-because it is not specific to paths.
-
-The caller:
-
-. Allocates and clears a `struct string_list` variable.
-
-. Initializes the members. You might want to set the flag `strdup_strings`
-  if the strings should be strdup()ed. For example, this is necessary
-  when you add something like git_path("..."), since that function returns
-  a static buffer that will change with the next call to git_path().
-+
-If you need something advanced, you can manually malloc() the `items`
-member (you need this if you add things later) and you should set the
-`nr` and `alloc` members in that case, too.
-
-. Adds new items to the list, using `string_list_append`,
-  `string_list_append_nodup`, `string_list_insert`,
-  `string_list_split`, and/or `string_list_split_in_place`.
-
-. Can check if a string is in the list using `string_list_has_string` or
-  `unsorted_string_list_has_string` and get it from the list using
-  `string_list_lookup` for sorted lists.
-
-. Can sort an unsorted list using `string_list_sort`.
-
-. Can remove duplicate items from a sorted list using
-  `string_list_remove_duplicates`.
-
-. Can remove individual items of an unsorted list using
-  `unsorted_string_list_delete_item`.
-
-. Can remove items not matching a criterion from a sorted or unsorted
-  list using `filter_string_list`, or remove empty strings using
-  `string_list_remove_empty_items`.
-
-. Finally it should free the list using `string_list_clear`.
-
-Example:
-
-----
-struct string_list list = STRING_LIST_INIT_NODUP;
-int i;
-
-string_list_append(&list, "foo");
-string_list_append(&list, "bar");
-for (i = 0; i < list.nr; i++)
-       printf("%s\n", list.items[i].string)
-----
-
-NOTE: It is more efficient to build an unsorted list and sort it
-afterwards, instead of building a sorted list (`O(n log n)` instead of
-`O(n^2)`).
-+
-However, if you use the list to check if a certain string was added
-already, you should not do that (using unsorted_string_list_has_string()),
-because the complexity would be quadratic again (but with a worse factor).
-
-Functions
----------
-
-* General ones (works with sorted and unsorted lists as well)
-
-`string_list_init`::
-
-       Initialize the members of the string_list, set `strdup_strings`
-       member according to the value of the second parameter.
-
-`filter_string_list`::
-
-       Apply a function to each item in a list, retaining only the
-       items for which the function returns true.  If free_util is
-       true, call free() on the util members of any items that have
-       to be deleted.  Preserve the order of the items that are
-       retained.
-
-`string_list_remove_empty_items`::
-
-       Remove any empty strings from the list.  If free_util is true,
-       call free() on the util members of any items that have to be
-       deleted.  Preserve the order of the items that are retained.
-
-`print_string_list`::
-
-       Dump a string_list to stdout, useful mainly for debugging purposes. It
-       can take an optional header argument and it writes out the
-       string-pointer pairs of the string_list, each one in its own line.
-
-`string_list_clear`::
-
-       Free a string_list. The `string` pointer of the items will be freed in
-       case the `strdup_strings` member of the string_list is set. The second
-       parameter controls if the `util` pointer of the items should be freed
-       or not.
-
-* Functions for sorted lists only
-
-`string_list_has_string`::
-
-       Determine if the string_list has a given string or not.
-
-`string_list_insert`::
-
-       Insert a new element to the string_list. The returned pointer can be
-       handy if you want to write something to the `util` pointer of the
-       string_list_item containing the just added string. If the given
-       string already exists the insertion will be skipped and the
-       pointer to the existing item returned.
-+
-Since this function uses xrealloc() (which die()s if it fails) if the
-list needs to grow, it is safe not to check the pointer. I.e. you may
-write `string_list_insert(...)->util = ...;`.
-
-`string_list_lookup`::
-
-       Look up a given string in the string_list, returning the containing
-       string_list_item. If the string is not found, NULL is returned.
-
-`string_list_remove_duplicates`::
-
-       Remove all but the first of consecutive entries that have the
-       same string value.  If free_util is true, call free() on the
-       util members of any items that have to be deleted.
-
-* Functions for unsorted lists only
-
-`string_list_append`::
-
-       Append a new string to the end of the string_list.  If
-       `strdup_string` is set, then the string argument is copied;
-       otherwise the new `string_list_entry` refers to the input
-       string.
-
-`string_list_append_nodup`::
-
-       Append a new string to the end of the string_list.  The new
-       `string_list_entry` always refers to the input string, even if
-       `strdup_string` is set.  This function can be used to hand
-       ownership of a malloc()ed string to a `string_list` that has
-       `strdup_string` set.
-
-`string_list_sort`::
-
-       Sort the list's entries by string value in `strcmp()` order.
-
-`unsorted_string_list_has_string`::
-
-       It's like `string_list_has_string()` but for unsorted lists.
-
-`unsorted_string_list_lookup`::
-
-       It's like `string_list_lookup()` but for unsorted lists.
-+
-The above two functions need to look through all items, as opposed to their
-counterpart for sorted lists, which performs a binary search.
-
-`unsorted_string_list_delete_item`::
-
-       Remove an item from a string_list. The `string` pointer of the items
-       will be freed in case the `strdup_strings` member of the string_list
-       is set. The third parameter controls if the `util` pointer of the
-       items should be freed or not.
-
-`string_list_split`::
-`string_list_split_in_place`::
-
-       Split a string into substrings on a delimiter character and
-       append the substrings to a `string_list`.  If `maxsplit` is
-       non-negative, then split at most `maxsplit` times.  Return the
-       number of substrings appended to the list.
-+
-`string_list_split` requires a `string_list` that has `strdup_strings`
-set to true; it leaves the input string untouched and makes copies of
-the substrings in newly-allocated memory.
-`string_list_split_in_place` requires a `string_list` that has
-`strdup_strings` set to false; it splits the input string in place,
-overwriting the delimiter characters with NULs and creating new
-string_list_items that point into the original string (the original
-string must therefore not be modified or freed while the `string_list`
-is in use).
-
-
-Data structures
----------------
-
-* `struct string_list_item`
-
-Represents an item of the list. The `string` member is a pointer to the
-string, and you may use the `util` member for any purpose, if you want.
-
-* `struct string_list`
-
-Represents the list itself.
-
-. The array of items are available via the `items` member.
-. The `nr` member contains the number of items stored in the list.
-. The `alloc` member is used to avoid reallocating at every insertion.
-  You should not tamper with it.
-. Setting the `strdup_strings` member to 1 will strdup() the strings
-  before adding them, see above.
-. The `compare_strings_fn` member is used to specify a custom compare
-  function, otherwise `strcmp()` is used as the default function.
index ed4ca438bd9c6ddab51f78cb4b620f02a7e12eed..b143e4eea3fa75eebd883361c40c3faa9e6c8060 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -205,6 +205,9 @@ all::
 #
 # Define NO_MMAP if you want to avoid mmap.
 #
+# Define MMAP_PREVENTS_DELETE if a file that is currently mmapped cannot be
+# deleted or cannot be replaced using rename().
+#
 # Define NO_SYS_POLL_H if you don't have sys/poll.h.
 #
 # Define NO_POLL if you do not have or don't want to use poll().
@@ -1391,6 +1394,9 @@ else
                COMPAT_OBJS += compat/win32mmap.o
        endif
 endif
+ifdef MMAP_PREVENTS_DELETE
+       BASIC_CFLAGS += -DMMAP_PREVENTS_DELETE
+endif
 ifdef OBJECT_CREATION_USES_RENAMES
        COMPAT_CFLAGS += -DOBJECT_CREATION_MODE=1
 endif
index 708aff8d42c562112fe4076635af903c7f41fbaa..98579853299427ff906434fe56288a73b9d19b21 100644 (file)
--- a/abspath.c
+++ b/abspath.c
@@ -202,6 +202,10 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,
        return retval;
 }
 
+/*
+ * Resolve `path` into an absolute, cleaned-up path. The return value
+ * comes from a shared buffer.
+ */
 const char *real_path(const char *path)
 {
        static struct strbuf realpath = STRBUF_INIT;
index a2b7c554275e871b2d175353459bbfa0744327f2..89e34e9ceb171412b9d36c6d2e01b929f1518e29 100644 (file)
@@ -28,6 +28,7 @@ static const char * const builtin_branch_usage[] = {
        N_("git branch [<options>] [-l] [-f] <branch-name> [<start-point>]"),
        N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
        N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
+       N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
        N_("git branch [<options>] [-r | -a] [--points-at]"),
        N_("git branch [<options>] [-r | -a] [--format]"),
        NULL
@@ -456,15 +457,19 @@ static void reject_rebase_or_bisect_branch(const char *target)
        free_worktrees(worktrees);
 }
 
-static void rename_branch(const char *oldname, const char *newname, int force)
+static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
 {
        struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
        struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
        int recovery = 0;
        int clobber_head_ok;
 
-       if (!oldname)
-               die(_("cannot rename the current branch while not on any."));
+       if (!oldname) {
+               if (copy)
+                       die(_("cannot copy the current branch while not on any."));
+               else
+                       die(_("cannot rename the current branch while not on any."));
+       }
 
        if (strbuf_check_branch_ref(&oldref, oldname)) {
                /*
@@ -487,16 +492,29 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 
        reject_rebase_or_bisect_branch(oldref.buf);
 
-       strbuf_addf(&logmsg, "Branch: renamed %s to %s",
-                oldref.buf, newref.buf);
+       if (copy)
+               strbuf_addf(&logmsg, "Branch: copied %s to %s",
+                           oldref.buf, newref.buf);
+       else
+               strbuf_addf(&logmsg, "Branch: renamed %s to %s",
+                           oldref.buf, newref.buf);
 
-       if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
+       if (!copy && rename_ref(oldref.buf, newref.buf, logmsg.buf))
                die(_("Branch rename failed"));
+       if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
+               die(_("Branch copy failed"));
 
-       if (recovery)
-               warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
+       if (recovery) {
+               if (copy)
+                       warning(_("Copied a misnamed branch '%s' away"),
+                               oldref.buf + 11);
+               else
+                       warning(_("Renamed a misnamed branch '%s' away"),
+                               oldref.buf + 11);
+       }
 
-       if (replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
+       if (!copy &&
+           replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
                die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
 
        strbuf_release(&logmsg);
@@ -505,8 +523,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
        strbuf_release(&oldref);
        strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
        strbuf_release(&newref);
-       if (git_config_rename_section(oldsection.buf, newsection.buf) < 0)
+       if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
                die(_("Branch is renamed, but update of config-file failed"));
+       if (copy && strcmp(oldname, newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0)
+               die(_("Branch is copied, but update of config-file failed"));
        strbuf_release(&oldsection);
        strbuf_release(&newsection);
 }
@@ -544,7 +564,7 @@ static int edit_branch_description(const char *branch_name)
 
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
-       int delete = 0, rename = 0, force = 0, list = 0;
+       int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
        int reflog = 0, edit_description = 0;
        int quiet = 0, unset_upstream = 0;
        const char *new_upstream = NULL;
@@ -581,6 +601,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
                OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
                OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
+               OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
+               OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
                OPT_BOOL(0, "list", &list, N_("list branch names")),
                OPT_BOOL('l', "create-reflog", &reflog, N_("create the branch's reflog")),
                OPT_BOOL(0, "edit-description", &edit_description,
@@ -624,14 +646,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
                             0);
 
-       if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
+       if (!delete && !rename && !copy && !edit_description && !new_upstream && !unset_upstream && argc == 0)
                list = 1;
 
        if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
            filter.no_commit)
                list = 1;
 
-       if (!!delete + !!rename + !!new_upstream +
+       if (!!delete + !!rename + !!copy + !!new_upstream +
            list + unset_upstream > 1)
                usage_with_options(builtin_branch_usage, options);
 
@@ -649,6 +671,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        if (force) {
                delete *= 2;
                rename *= 2;
+               copy *= 2;
        }
 
        if (delete) {
@@ -703,13 +726,22 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
                if (edit_branch_description(branch_name))
                        return 1;
+       } else if (copy) {
+               if (!argc)
+                       die(_("branch name required"));
+               else if (argc == 1)
+                       copy_or_rename_branch(head, argv[0], 1, copy > 1);
+               else if (argc == 2)
+                       copy_or_rename_branch(argv[0], argv[1], 1, copy > 1);
+               else
+                       die(_("too many branches for a copy operation"));
        } else if (rename) {
                if (!argc)
                        die(_("branch name required"));
                else if (argc == 1)
-                       rename_branch(head, argv[0], rename > 1);
+                       copy_or_rename_branch(head, argv[0], 0, rename > 1);
                else if (argc == 2)
-                       rename_branch(argv[0], argv[1], rename > 1);
+                       copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
                else
                        die(_("too many branches for a rename operation"));
        } else if (new_upstream) {
index 3345a0d16f21c38fe5fe6f32fa5b0a139e21c4a3..10751585ea38c282d439e4128c895c01a2598985 100644 (file)
@@ -1124,9 +1124,8 @@ static int checkout_branch(struct checkout_opts *opts,
 
        if (new->path && !opts->force_detach && !opts->new_branch &&
            !opts->ignore_other_worktrees) {
-               struct object_id oid;
                int flag;
-               char *head_ref = resolve_refdup("HEAD", 0, oid.hash, &flag);
+               char *head_ref = resolve_refdup("HEAD", 0, NULL, &flag);
                if (head_ref &&
                    (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)))
                        die_if_checked_out(new->path, 1);
index 0f8ddb6866b3d14c4e6b84f019110387c7f77319..d75b3805ea7fe3475564f337bf660d8155909445 100644 (file)
@@ -1392,7 +1392,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)
        read_cache_preload(&s.pathspec);
        refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL);
 
-       fd = hold_locked_index(&index_lock, 0);
+       if (use_optional_locks())
+               fd = hold_locked_index(&index_lock, 0);
+       else
+               fd = -1;
 
        s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
        if (!s.is_initial)
index 6d9a79f9b39834d953685b2641354915df3cfcbe..2706fcfaf2261e2ac2eaff5a054d0dd7a9291c0b 100644 (file)
@@ -26,8 +26,10 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
                usage(builtin_get_tar_commit_id_usage);
 
        n = read_in_full(0, buffer, HEADERSIZE);
-       if (n < HEADERSIZE)
-               die("git get-tar-commit-id: read error");
+       if (n < 0)
+               die_errno("git get-tar-commit-id: read error");
+       if (n != HEADERSIZE)
+               die_errno("git get-tar-commit-id: EOF before reading tar header");
        if (header->typeflag[0] != 'g')
                return 1;
        if (!skip_prefix(content, "52 comment=", &comment))
index c82b4dce6838fa039e9c2cfc769c7cd17a8ef562..f8519363a393862b6857acab037e74367c7f2134 100644 (file)
@@ -12,15 +12,30 @@ static const char * const builtin_rebase_helper_usage[] = {
 int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
 {
        struct replay_opts opts = REPLAY_OPTS_INIT;
+       int keep_empty = 0;
        enum {
-               CONTINUE = 1, ABORT
+               CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S,
+               CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH
        } command = 0;
        struct option options[] = {
                OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
+               OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
                OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
                                CONTINUE),
                OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
                                ABORT),
+               OPT_CMDMODE(0, "make-script", &command,
+                       N_("make rebase script"), MAKE_SCRIPT),
+               OPT_CMDMODE(0, "shorten-ids", &command,
+                       N_("shorten SHA-1s in the todo list"), SHORTEN_SHA1S),
+               OPT_CMDMODE(0, "expand-ids", &command,
+                       N_("expand SHA-1s in the todo list"), EXPAND_SHA1S),
+               OPT_CMDMODE(0, "check-todo-list", &command,
+                       N_("check the todo list"), CHECK_TODO_LIST),
+               OPT_CMDMODE(0, "skip-unnecessary-picks", &command,
+                       N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS),
+               OPT_CMDMODE(0, "rearrange-squash", &command,
+                       N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
                OPT_END()
        };
 
@@ -37,5 +52,17 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
                return !!sequencer_continue(&opts);
        if (command == ABORT && argc == 1)
                return !!sequencer_remove_state(&opts);
+       if (command == MAKE_SCRIPT && argc > 1)
+               return !!sequencer_make_script(keep_empty, stdout, argc, argv);
+       if (command == SHORTEN_SHA1S && argc == 1)
+               return !!transform_todo_ids(1);
+       if (command == EXPAND_SHA1S && argc == 1)
+               return !!transform_todo_ids(0);
+       if (command == CHECK_TODO_LIST && argc == 1)
+               return !!check_todo_list();
+       if (command == SKIP_UNNECESSARY_PICKS && argc == 1)
+               return !!skip_unnecessary_picks();
+       if (command == REARRANGE_SQUASH && argc == 1)
+               return !!rearrange_squash();
        usage_with_options(builtin_rebase_helper_usage, options);
 }
index 29a0f3b75fb02dfa2a02c325816453e36baaa55a..cc4876740569c6e495b7cb32161d019619171d79 100644 (file)
@@ -1458,7 +1458,6 @@ static void execute_commands(struct command *commands,
 {
        struct check_connected_options opt = CHECK_CONNECTED_INIT;
        struct command *cmd;
-       struct object_id oid;
        struct iterate_data data;
        struct async muxer;
        int err_fd = 0;
@@ -1515,7 +1514,7 @@ static void execute_commands(struct command *commands,
        check_aliased_updates(commands);
 
        free(head_name_to_free);
-       head_name = head_name_to_free = resolve_refdup("HEAD", 0, oid.hash, NULL);
+       head_name = head_name_to_free = resolve_refdup("HEAD", 0, NULL, NULL);
 
        if (use_atomic)
                execute_commands_atomic(commands, si);
index b9c13d3d9df6cf5f90e990d181e268f698ac93e4..a8d7e6f7aecc2e4179547bde3fd6d07231764b9f 100644 (file)
@@ -387,6 +387,14 @@ static const char *skipspaces(const char *s)
        return s;
 }
 
+static char *findspace(const char *s)
+{
+       for (; *s; s++)
+               if (isspace(*s))
+                       return (char*)s;
+       return NULL;
+}
+
 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 {
        static int keep_dashdash = 0, stop_at_non_option = 0;
@@ -434,7 +442,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
        /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
        while (strbuf_getline(&sb, stdin) != EOF) {
                const char *s;
-               const char *help;
+               char *help;
                struct option *o;
 
                if (!sb.len)
@@ -444,15 +452,17 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
                memset(opts + onb, 0, sizeof(opts[onb]));
 
                o = &opts[onb++];
-               help = strchr(sb.buf, ' ');
-               if (!help || *sb.buf == ' ') {
+               help = findspace(sb.buf);
+               if (!help || sb.buf == help) {
                        o->type = OPTION_GROUP;
                        o->help = xstrdup(skipspaces(sb.buf));
                        continue;
                }
 
+               *help = '\0';
+
                o->type = OPTION_CALLBACK;
-               o->help = xstrdup(skipspaces(help));
+               o->help = xstrdup(skipspaces(help+1));
                o->value = &parsed;
                o->flags = PARSE_OPT_NOARG;
                o->callback = &parseopt_dump;
index de26849f5551a12aac16e4121c7024ebb40502d2..7b9307aa588a70a47de4c53a380e165f90924ab9 100644 (file)
@@ -38,7 +38,9 @@ static int prune_worktree(const char *id, struct strbuf *reason)
 {
        struct stat st;
        char *path;
-       int fd, len;
+       int fd;
+       size_t len;
+       ssize_t read_result;
 
        if (!is_directory(git_path("worktrees/%s", id))) {
                strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
@@ -56,10 +58,26 @@ static int prune_worktree(const char *id, struct strbuf *reason)
                            id, strerror(errno));
                return 1;
        }
-       len = st.st_size;
+       len = xsize_t(st.st_size);
        path = xmallocz(len);
-       read_in_full(fd, path, len);
+
+       read_result = read_in_full(fd, path, len);
+       if (read_result < 0) {
+               strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
+                           id, strerror(errno));
+               close(fd);
+               free(path);
+               return 1;
+       }
        close(fd);
+
+       if (read_result != len) {
+               strbuf_addf(reason,
+                           _("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
+                           id, (uintmax_t)len, (uintmax_t)read_result);
+               free(path);
+               return 1;
+       }
        while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
                len--;
        if (!len) {
index 9a1f6c49aba4ddd11284723fe781d7fbaa419f95..3310fd210a151545076169b45f5555b52acbbf9d 100644 (file)
@@ -115,7 +115,10 @@ static int stream_to_pack(struct bulk_checkin_state *state,
 
                if (size && !s.avail_in) {
                        ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
-                       if (read_in_full(fd, ibuf, rsize) != rsize)
+                       ssize_t read_result = read_in_full(fd, ibuf, rsize);
+                       if (read_result < 0)
+                               die_errno("failed to read from '%s'", path);
+                       if (read_result != rsize)
                                die("failed to read %d bytes from '%s'",
                                    (int)rsize, path);
                        offset += rsize;
diff --git a/cache.h b/cache.h
index ea6c236e0fa5e895d89a0029349ea54f9afabd07..6440e2bf21f5800db98f82fcedff46478188674e 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -444,6 +444,7 @@ static inline enum object_type object_type(unsigned int mode)
 #define GIT_NOGLOB_PATHSPECS_ENVIRONMENT "GIT_NOGLOB_PATHSPECS"
 #define GIT_ICASE_PATHSPECS_ENVIRONMENT "GIT_ICASE_PATHSPECS"
 #define GIT_QUARANTINE_ENVIRONMENT "GIT_QUARANTINE_PATH"
+#define GIT_OPTIONAL_LOCKS_ENVIRONMENT "GIT_OPTIONAL_LOCKS"
 
 /*
  * This environment variable is expected to contain a boolean indicating
@@ -783,6 +784,11 @@ extern int protect_ntfs;
  */
 extern int ref_paranoia;
 
+/*
+ * Returns the boolean value of $GIT_OPTIONAL_LOCKS (or the default value).
+ */
+int use_optional_locks(void);
+
 /*
  * The character that begins a commented line in user-editable file
  * that is subject to stripspace.
index 345d78c2ba80bbb44c1b700605391ef09d2b3c42..4831c1273542706437139ccc379afb38e7ca3d3b 100644 (file)
--- a/config.c
+++ b/config.c
@@ -2292,11 +2292,10 @@ static int write_error(const char *filename)
        return 4;
 }
 
-static ssize_t write_section(int fd, const char *key)
+static struct strbuf store_create_section(const char *key)
 {
        const char *dot;
        int i;
-       ssize_t ret;
        struct strbuf sb = STRBUF_INIT;
 
        dot = memchr(key, '.', store.baselen);
@@ -2312,7 +2311,15 @@ static ssize_t write_section(int fd, const char *key)
                strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
        }
 
-       ret = write_in_full(fd, sb.buf, sb.len);
+       return sb;
+}
+
+static ssize_t write_section(int fd, const char *key)
+{
+       struct strbuf sb = store_create_section(key);
+       ssize_t ret;
+
+       ret = write_in_full(fd, sb.buf, sb.len) == sb.len;
        strbuf_release(&sb);
 
        return ret;
@@ -2743,8 +2750,8 @@ static int section_name_is_ok(const char *name)
 }
 
 /* if new_name == NULL, the section is removed instead */
-int git_config_rename_section_in_file(const char *config_filename,
-                                     const char *old_name, const char *new_name)
+static int git_config_copy_or_rename_section_in_file(const char *config_filename,
+                                     const char *old_name, const char *new_name, int copy)
 {
        int ret = 0, remove = 0;
        char *filename_buf = NULL;
@@ -2753,6 +2760,7 @@ int git_config_rename_section_in_file(const char *config_filename,
        char buf[1024];
        FILE *config_file = NULL;
        struct stat st;
+       struct strbuf copystr = STRBUF_INIT;
 
        if (new_name && !section_name_is_ok(new_name)) {
                ret = error("invalid section name: %s", new_name);
@@ -2791,12 +2799,30 @@ int git_config_rename_section_in_file(const char *config_filename,
        while (fgets(buf, sizeof(buf), config_file)) {
                int i;
                int length;
+               int is_section = 0;
                char *output = buf;
                for (i = 0; buf[i] && isspace(buf[i]); i++)
                        ; /* do nothing */
                if (buf[i] == '[') {
                        /* it's a section */
-                       int offset = section_name_match(&buf[i], old_name);
+                       int offset;
+                       is_section = 1;
+
+                       /*
+                        * When encountering a new section under -c we
+                        * need to flush out any section we're already
+                        * coping and begin anew. There might be
+                        * multiple [branch "$name"] sections.
+                        */
+                       if (copystr.len > 0) {
+                               if (write_in_full(out_fd, copystr.buf, copystr.len) != copystr.len) {
+                                       ret = write_error(get_lock_file_path(lock));
+                                       goto out;
+                               }
+                               strbuf_reset(&copystr);
+                       }
+
+                       offset = section_name_match(&buf[i], old_name);
                        if (offset > 0) {
                                ret++;
                                if (new_name == NULL) {
@@ -2804,25 +2830,29 @@ int git_config_rename_section_in_file(const char *config_filename,
                                        continue;
                                }
                                store.baselen = strlen(new_name);
-                               if (write_section(out_fd, new_name) < 0) {
-                                       ret = write_error(get_lock_file_path(lock));
-                                       goto out;
-                               }
-                               /*
-                                * We wrote out the new section, with
-                                * a newline, now skip the old
-                                * section's length
-                                */
-                               output += offset + i;
-                               if (strlen(output) > 0) {
+                               if (!copy) {
+                                       if (write_section(out_fd, new_name) < 0) {
+                                               ret = write_error(get_lock_file_path(lock));
+                                               goto out;
+                                       }
                                        /*
-                                        * More content means there's
-                                        * a declaration to put on the
-                                        * next line; indent with a
-                                        * tab
+                                        * We wrote out the new section, with
+                                        * a newline, now skip the old
+                                        * section's length
                                         */
-                                       output -= 1;
-                                       output[0] = '\t';
+                                       output += offset + i;
+                                       if (strlen(output) > 0) {
+                                               /*
+                                                * More content means there's
+                                                * a declaration to put on the
+                                                * next line; indent with a
+                                                * tab
+                                                */
+                                               output -= 1;
+                                               output[0] = '\t';
+                                       }
+                               } else {
+                                       copystr = store_create_section(new_name);
                                }
                        }
                        remove = 0;
@@ -2830,11 +2860,30 @@ int git_config_rename_section_in_file(const char *config_filename,
                if (remove)
                        continue;
                length = strlen(output);
+
+               if (!is_section && copystr.len > 0) {
+                       strbuf_add(&copystr, output, length);
+               }
+
                if (write_in_full(out_fd, output, length) < 0) {
                        ret = write_error(get_lock_file_path(lock));
                        goto out;
                }
        }
+
+       /*
+        * Copy a trailing section at the end of the config, won't be
+        * flushed by the usual "flush because we have a new section
+        * logic in the loop above.
+        */
+       if (copystr.len > 0) {
+               if (write_in_full(out_fd, copystr.buf, copystr.len) != copystr.len) {
+                       ret = write_error(get_lock_file_path(lock));
+                       goto out;
+               }
+               strbuf_reset(&copystr);
+       }
+
        fclose(config_file);
        config_file = NULL;
 commit_and_out:
@@ -2850,11 +2899,30 @@ int git_config_rename_section_in_file(const char *config_filename,
        return ret;
 }
 
+int git_config_rename_section_in_file(const char *config_filename,
+                                     const char *old_name, const char *new_name)
+{
+       return git_config_copy_or_rename_section_in_file(config_filename,
+                                        old_name, new_name, 0);
+}
+
 int git_config_rename_section(const char *old_name, const char *new_name)
 {
        return git_config_rename_section_in_file(NULL, old_name, new_name);
 }
 
+int git_config_copy_section_in_file(const char *config_filename,
+                                     const char *old_name, const char *new_name)
+{
+       return git_config_copy_or_rename_section_in_file(config_filename,
+                                        old_name, new_name, 1);
+}
+
+int git_config_copy_section(const char *old_name, const char *new_name)
+{
+       return git_config_copy_section_in_file(NULL, old_name, new_name);
+}
+
 /*
  * Call this to report error for your variable that should not
  * get a boolean value (i.e. "[my] var" means "true").
index 456b3d134d3ed187b80b5941c20690763f3eab35..a49d26441622508fd3c6560ac9784e84541c10dc 100644 (file)
--- a/config.h
+++ b/config.h
@@ -70,6 +70,8 @@ extern int git_config_set_multivar_in_file_gently(const char *, const char *, co
 extern void git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
 extern int git_config_rename_section_in_file(const char *, const char *, const char *);
+extern int git_config_copy_section(const char *, const char *);
+extern int git_config_copy_section_in_file(const char *, const char *, const char *);
 extern const char *git_etc_gitconfig(void);
 extern int git_env_bool(const char *, int);
 extern unsigned long git_env_ulong(const char *, unsigned long);
index 6604b130f8c5e5f9095f18f11eee2e1185373532..685a80d13843800be3c633539b0a22e88652444a 100644 (file)
@@ -184,6 +184,7 @@ ifeq ($(uname_O),Cygwin)
        UNRELIABLE_FSTAT = UnfortunatelyYes
        SPARSE_FLAGS = -isystem /usr/include/w32api -Wno-one-bit-signed-bitfield
        OBJECT_CREATION_USES_RENAMES = UnfortunatelyNeedsTo
+       MMAP_PREVENTS_DELETE = UnfortunatelyYes
        COMPAT_OBJS += compat/cygwin.o
        FREAD_READS_DIRECTORIES = UnfortunatelyYes
 endif
@@ -353,6 +354,7 @@ ifeq ($(uname_S),Windows)
        NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
        NO_NSEC = YesPlease
        USE_WIN32_MMAP = YesPlease
+       MMAP_PREVENTS_DELETE = UnfortunatelyYes
        # USE_NED_ALLOCATOR = YesPlease
        UNRELIABLE_FSTAT = UnfortunatelyYes
        OBJECT_CREATION_USES_RENAMES = UnfortunatelyNeedsTo
@@ -501,6 +503,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
        NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
        NO_NSEC = YesPlease
        USE_WIN32_MMAP = YesPlease
+       MMAP_PREVENTS_DELETE = UnfortunatelyYes
        USE_NED_ALLOCATOR = YesPlease
        UNRELIABLE_FSTAT = UnfortunatelyYes
        OBJECT_CREATION_USES_RENAMES = UnfortunatelyNeedsTo
index a172199e44bf370cd66ee7073f5a2228fd9679ef..2adae04073816a781d01d85433d8d8922baafd7f 100644 (file)
@@ -19,7 +19,7 @@ static void flush(struct sha1file *f, const void *buf, unsigned int count)
 
                if (ret < 0)
                        die_errno("%s: sha1 file read error", f->name);
-               if (ret < count)
+               if (ret != count)
                        die("%s: sha1 file truncated", f->name);
                if (memcmp(buf, check_buffer, count))
                        die("sha1 file '%s' validation error", f->name);
diff --git a/diff.c b/diff.c
index 4da0714cb5c4147defe9bb59685cf0f68c63bdc0..69f03570adf6cf2c3642aea1fb0dba5011ef966f 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -5272,6 +5272,7 @@ static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
                        strbuf_addch(&sb, ' ');
                        quote_c_style(p->two->path, &sb, NULL, 0);
                }
+               strbuf_addch(&sb, '\n');
                emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
                                 sb.buf, sb.len, 0);
                strbuf_release(&sb);
index f1f934b6fddd101191a17b1bc1883da287793319..8289c25b44d74a0f054f9fe3eaf44c5fdcf0b3d2 100644 (file)
@@ -338,3 +338,8 @@ void reset_shared_repository(void)
 {
        need_shared_repository_from_config = 1;
 }
+
+int use_optional_locks(void)
+{
+       return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
+}
index 35bf671f12c41ce5a2a9f9224babee792bf71209..d5e4cf0bad411dad859dbe421ab7583cfb0f1561 100644 (file)
@@ -3189,10 +3189,10 @@ static void checkpoint(void)
        checkpoint_requested = 0;
        if (object_count) {
                cycle_packfile();
-               dump_branches();
-               dump_tags();
-               dump_marks();
        }
+       dump_branches();
+       dump_tags();
+       dump_marks();
 }
 
 static void parse_checkpoint(void)
index 29b7e8824b53abeaa68780b95d5954f67f734098..2563dc52daaf7acbbdba803360f94bfa5040c5b6 100644 (file)
@@ -155,13 +155,13 @@ reschedule_last_action () {
 append_todo_help () {
        gettext "
 Commands:
- p, pick = use commit
- r, reword = use commit, but edit the commit message
- e, edit = use commit, but stop for amending
- s, squash = use commit, but meld into previous commit
- f, fixup = like \"squash\", but discard this commit's log message
- x, exec = run command (the rest of the line) using shell
- d, drop = remove commit
+p, pick = use commit
+r, reword = use commit, but edit the commit message
+e, edit = use commit, but stop for amending
+s, squash = use commit, but meld into previous commit
+f, fixup = like \"squash\", but discard this commit's log message
+x, exec = run command (the rest of the line) using shell
+d, drop = remove commit
 
 These lines can be re-ordered; they are executed from top to bottom.
 " | git stripspace --comment-lines >>"$todo"
@@ -714,154 +714,12 @@ do_rest () {
        done
 }
 
-# skip picking commits whose parents are unchanged
-skip_unnecessary_picks () {
-       fd=3
-       while read -r command rest
-       do
-               # fd=3 means we skip the command
-               case "$fd,$command" in
-               3,pick|3,p)
-                       # pick a commit whose parent is current $onto -> skip
-                       sha1=${rest%% *}
-                       case "$(git rev-parse --verify --quiet "$sha1"^)" in
-                       "$onto"*)
-                               onto=$sha1
-                               ;;
-                       *)
-                               fd=1
-                               ;;
-                       esac
-                       ;;
-               3,"$comment_char"*|3,)
-                       # copy comments
-                       ;;
-               *)
-                       fd=1
-                       ;;
-               esac
-               printf '%s\n' "$command${rest:+ }$rest" >&$fd
-       done <"$todo" >"$todo.new" 3>>"$done" &&
-       mv -f "$todo".new "$todo" &&
-       case "$(peek_next_command)" in
-       squash|s|fixup|f)
-               record_in_rewritten "$onto"
-               ;;
-       esac ||
-               die "$(gettext "Could not skip unnecessary pick commands")"
-}
-
-transform_todo_ids () {
-       while read -r command rest
-       do
-               case "$command" in
-               "$comment_char"* | exec)
-                       # Be careful for oddball commands like 'exec'
-                       # that do not have a SHA-1 at the beginning of $rest.
-                       ;;
-               *)
-                       sha1=$(git rev-parse --verify --quiet "$@" ${rest%%[     ]*}) &&
-                       rest="$sha1 ${rest#*[    ]}"
-                       ;;
-               esac
-               printf '%s\n' "$command${rest:+ }$rest"
-       done <"$todo" >"$todo.new" &&
-       mv -f "$todo.new" "$todo"
-}
-
 expand_todo_ids() {
-       transform_todo_ids
+       git rebase--helper --expand-ids
 }
 
 collapse_todo_ids() {
-       transform_todo_ids --short
-}
-
-# Rearrange the todo list that has both "pick sha1 msg" and
-# "pick sha1 fixup!/squash! msg" appears in it so that the latter
-# comes immediately after the former, and change "pick" to
-# "fixup"/"squash".
-#
-# Note that if the config has specified a custom instruction format
-# each log message will be re-retrieved in order to normalize the
-# autosquash arrangement
-rearrange_squash () {
-       # extract fixup!/squash! lines and resolve any referenced sha1's
-       while read -r pick sha1 message
-       do
-               test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
-               case "$message" in
-               "squash! "*|"fixup! "*)
-                       action="${message%%!*}"
-                       rest=$message
-                       prefix=
-                       # skip all squash! or fixup! (but save for later)
-                       while :
-                       do
-                               case "$rest" in
-                               "squash! "*|"fixup! "*)
-                                       prefix="$prefix${rest%%!*},"
-                                       rest="${rest#*! }"
-                                       ;;
-                               *)
-                                       break
-                                       ;;
-                               esac
-                       done
-                       printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
-                       # if it's a single word, try to resolve to a full sha1 and
-                       # emit a second copy. This allows us to match on both message
-                       # and on sha1 prefix
-                       if test "${rest#* }" = "$rest"; then
-                               fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
-                               if test -n "$fullsha"; then
-                                       # prefix the action to uniquely identify this line as
-                                       # intended for full sha1 match
-                                       echo "$sha1 +$action $prefix $fullsha"
-                               fi
-                       fi
-               esac
-       done >"$1.sq" <"$1"
-       test -s "$1.sq" || return
-
-       used=
-       while read -r pick sha1 message
-       do
-               case " $used" in
-               *" $sha1 "*) continue ;;
-               esac
-               printf '%s\n' "$pick $sha1 $message"
-               test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
-               used="$used$sha1 "
-               while read -r squash action msg_prefix msg_content
-               do
-                       case " $used" in
-                       *" $squash "*) continue ;;
-                       esac
-                       emit=0
-                       case "$action" in
-                       +*)
-                               action="${action#+}"
-                               # full sha1 prefix test
-                               case "$msg_content" in "$sha1"*) emit=1;; esac ;;
-                       *)
-                               # message prefix test
-                               case "$message" in "$msg_content"*) emit=1;; esac ;;
-                       esac
-                       if test $emit = 1; then
-                               if test -n "${format}"
-                               then
-                                       msg_content=$(git log -n 1 --format="${format}" ${squash})
-                               else
-                                       msg_content="$(echo "$msg_prefix" | sed "s/,/! /g")$msg_content"
-                               fi
-                               printf '%s\n' "$action $squash $msg_content"
-                               used="$used$squash "
-                       fi
-               done <"$1.sq"
-       done >"$1.rearranged" <"$1"
-       cat "$1.rearranged" >"$1"
-       rm -f "$1.sq" "$1.rearranged"
+       git rebase--helper --shorten-ids
 }
 
 # Add commands after a pick or after a squash/fixup serie
@@ -885,96 +743,6 @@ add_exec_commands () {
        mv "$1.new" "$1"
 }
 
-# Check if the SHA-1 passed as an argument is a
-# correct one, if not then print $2 in "$todo".badsha
-# $1: the SHA-1 to test
-# $2: the line number of the input
-# $3: the input filename
-check_commit_sha () {
-       badsha=0
-       if test -z "$1"
-       then
-               badsha=1
-       else
-               sha1_verif="$(git rev-parse --verify --quiet $1^{commit})"
-               if test -z "$sha1_verif"
-               then
-                       badsha=1
-               fi
-       fi
-
-       if test $badsha -ne 0
-       then
-               line="$(sed -n -e "${2}p" "$3")"
-               warn "$(eval_gettext "\
-Warning: the SHA-1 is missing or isn't a commit in the following line:
- - \$line")"
-               warn
-       fi
-
-       return $badsha
-}
-
-# prints the bad commits and bad commands
-# from the todolist in stdin
-check_bad_cmd_and_sha () {
-       retval=0
-       lineno=0
-       while read -r command rest
-       do
-               lineno=$(( $lineno + 1 ))
-               case $command in
-               "$comment_char"*|''|noop|x|exec)
-                       # Doesn't expect a SHA-1
-                       ;;
-               "$cr")
-                       # Work around CR left by "read" (e.g. with Git for
-                       # Windows' Bash).
-                       ;;
-               pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
-                       if ! check_commit_sha "${rest%%[        ]*}" "$lineno" "$1"
-                       then
-                               retval=1
-                       fi
-                       ;;
-               *)
-                       line="$(sed -n -e "${lineno}p" "$1")"
-                       warn "$(eval_gettext "\
-Warning: the command isn't recognized in the following line:
- - \$line")"
-                       warn
-                       retval=1
-                       ;;
-               esac
-       done <"$1"
-       return $retval
-}
-
-# Print the list of the SHA-1 of the commits
-# from stdin to stdout
-todo_list_to_sha_list () {
-       git stripspace --strip-comments |
-       while read -r command sha1 rest
-       do
-               case $command in
-               "$comment_char"*|''|noop|x|"exec")
-                       ;;
-               *)
-                       long_sha=$(git rev-list --no-walk "$sha1" 2>/dev/null)
-                       printf "%s\n" "$long_sha"
-                       ;;
-               esac
-       done
-}
-
-# Use warn for each line in stdin
-warn_lines () {
-       while read -r line
-       do
-               warn " - $line"
-       done
-}
-
 # Switch to the branch in $into and notify it in the reflog
 checkout_onto () {
        GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
@@ -989,74 +757,6 @@ get_missing_commit_check_level () {
        printf '%s' "$check_level" | tr 'A-Z' 'a-z'
 }
 
-# Check if the user dropped some commits by mistake
-# Behaviour determined by rebase.missingCommitsCheck.
-# Check if there is an unrecognized command or a
-# bad SHA-1 in a command.
-check_todo_list () {
-       raise_error=f
-
-       check_level=$(get_missing_commit_check_level)
-
-       case "$check_level" in
-       warn|error)
-               # Get the SHA-1 of the commits
-               todo_list_to_sha_list <"$todo".backup >"$todo".oldsha1
-               todo_list_to_sha_list <"$todo" >"$todo".newsha1
-
-               # Sort the SHA-1 and compare them
-               sort -u "$todo".oldsha1 >"$todo".oldsha1+
-               mv "$todo".oldsha1+ "$todo".oldsha1
-               sort -u "$todo".newsha1 >"$todo".newsha1+
-               mv "$todo".newsha1+ "$todo".newsha1
-               comm -2 -3 "$todo".oldsha1 "$todo".newsha1 >"$todo".miss
-
-               # Warn about missing commits
-               if test -s "$todo".miss
-               then
-                       test "$check_level" = error && raise_error=t
-
-                       warn "$(gettext "\
-Warning: some commits may have been dropped accidentally.
-Dropped commits (newer to older):")"
-
-                       # Make the list user-friendly and display
-                       opt="--no-walk=sorted --format=oneline --abbrev-commit --stdin"
-                       git rev-list $opt <"$todo".miss | warn_lines
-
-                       warn "$(gettext "\
-To avoid this message, use \"drop\" to explicitly remove a commit.
-
-Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
-The possible behaviours are: ignore, warn, error.")"
-                       warn
-               fi
-               ;;
-       ignore)
-               ;;
-       *)
-               warn "$(eval_gettext "Unrecognized setting \$check_level for option rebase.missingCommitsCheck. Ignoring.")"
-               ;;
-       esac
-
-       if ! check_bad_cmd_and_sha "$todo"
-       then
-               raise_error=t
-       fi
-
-       if test $raise_error = t
-       then
-               # Checkout before the first commit of the
-               # rebase: this way git rebase --continue
-               # will work correctly as it expects HEAD to be
-               # placed before the commit of the next action
-               checkout_onto
-
-               warn "$(gettext "You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.")"
-               die "$(gettext "Or you can abort the rebase with 'git rebase --abort'.")"
-       fi
-}
-
 # The whole contents of this file is run by dot-sourcing it from
 # inside a shell function.  It used to be that "return"s we see
 # below were not inside any function, and expected to return
@@ -1211,26 +911,27 @@ else
        revisions=$onto...$orig_head
        shortrevisions=$shorthead
 fi
-format=$(git config --get rebase.instructionFormat)
-# the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
-git rev-list $merges_option --format="%m%H ${format:-%s}" \
-       --reverse --left-right --topo-order \
-       $revisions ${restrict_revision+^$restrict_revision} | \
-       sed -n "s/^>//p" |
-while read -r sha1 rest
-do
-
-       if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
-       then
-               comment_out="$comment_char "
-       else
-               comment_out=
-       fi
+if test t != "$preserve_merges"
+then
+       git rebase--helper --make-script ${keep_empty:+--keep-empty} \
+               $revisions ${restrict_revision+^$restrict_revision} >"$todo"
+else
+       format=$(git config --get rebase.instructionFormat)
+       # the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
+       git rev-list $merges_option --format="%m%H ${format:-%s}" \
+               --reverse --left-right --topo-order \
+               $revisions ${restrict_revision+^$restrict_revision} | \
+               sed -n "s/^>//p" |
+       while read -r sha1 rest
+       do
+
+               if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
+               then
+                       comment_out="$comment_char "
+               else
+                       comment_out=
+               fi
 
-       if test t != "$preserve_merges"
-       then
-               printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
-       else
                if test -z "$rebase_root"
                then
                        preserve=t
@@ -1249,8 +950,8 @@ do
                        touch "$rewritten"/$sha1
                        printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
                fi
-       fi
-done
+       done
+fi
 
 # Watch for commits that been dropped by --cherry-pick
 if test t = "$preserve_merges"
@@ -1280,7 +981,7 @@ then
 fi
 
 test -s "$todo" || echo noop >> "$todo"
-test -n "$autosquash" && rearrange_squash "$todo"
+test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
 test -n "$cmd" && add_exec_commands "$todo"
 
 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
@@ -1316,11 +1017,17 @@ git_sequence_editor "$todo" ||
 has_action "$todo" ||
        return 2
 
-check_todo_list
+git rebase--helper --check-todo-list || {
+       ret=$?
+       checkout_onto
+       exit $ret
+}
 
 expand_todo_ids
 
-test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
+test -d "$rewritten" || test -n "$force_rebase" ||
+onto="$(git rebase--helper --skip-unnecessary-picks)" ||
+die "Could not skip unnecessary pick commands"
 
 checkout_onto
 if test -z "$rebase_root" && test ! -d "$rewritten"
index ad8415e3cf6907044b0730b59fc2e6ff30ab7174..6344e8d5e38ca237dc5b5b67844f87f48aaa74ca 100755 (executable)
@@ -350,6 +350,9 @@ do
                shift
                break
                ;;
+       *)
+               usage
+               ;;
        esac
        shift
 done
diff --git a/git.c b/git.c
index f31dca69620ac244e87e04f1d508ee23cf2f92cf..9e96dd409017c643e8a9bebbc41281728978b0b8 100644 (file)
--- a/git.c
+++ b/git.c
@@ -182,6 +182,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                        setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1);
                        if (envchanged)
                                *envchanged = 1;
+               } else if (!strcmp(cmd, "--no-optional-locks")) {
+                       setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1);
+                       if (envchanged)
+                               *envchanged = 1;
                } else if (!strcmp(cmd, "--shallow-file")) {
                        (*argv)++;
                        (*argc)--;
index 597d43f65c664c04dc3045c3d913146321f9e97b..4352c34a6ee50f907f2d03e7d0ed1a596345a5d3 100644 (file)
@@ -308,8 +308,6 @@ static void write_buf_to_worktree(const struct object_id *obj,
                        if (errno == EPIPE)
                                break;
                        die_errno("notes-merge");
-               } else if (!ret) {
-                       die("notes-merge: disk full?");
                }
                size -= ret;
                buf += ret;
index a333ec675476b4f10a5e12fe3303333ea200faea..fea62841920c9647edfcfba249a59bfb91170d8e 100644 (file)
@@ -213,14 +213,19 @@ void fixup_pack_header_footer(int pack_fd,
        git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
        struct pack_header hdr;
        char *buf;
+       ssize_t read_result;
 
        git_SHA1_Init(&old_sha1_ctx);
        git_SHA1_Init(&new_sha1_ctx);
 
        if (lseek(pack_fd, 0, SEEK_SET) != 0)
                die_errno("Failed seeking to start of '%s'", pack_name);
-       if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
+       read_result = read_in_full(pack_fd, &hdr, sizeof(hdr));
+       if (read_result < 0)
                die_errno("Unable to reread header of '%s'", pack_name);
+       else if (read_result != sizeof(hdr))
+               die_errno("Unexpected short read for header of '%s'",
+                         pack_name);
        if (lseek(pack_fd, 0, SEEK_SET) != 0)
                die_errno("Failed seeking to start of '%s'", pack_name);
        git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
index f69a5c8d607af191fa8ba04e84da8f02c40823ef..eab7542487a194ecd25708f906e66c40e1bed336 100644 (file)
@@ -442,6 +442,7 @@ static int open_packed_git_1(struct packed_git *p)
        unsigned char sha1[20];
        unsigned char *idx_sha1;
        long fd_flag;
+       ssize_t read_result;
 
        if (!p->index_data && open_pack_index(p))
                return error("packfile %s index unavailable", p->pack_name);
@@ -483,7 +484,10 @@ static int open_packed_git_1(struct packed_git *p)
                return error("cannot set FD_CLOEXEC");
 
        /* Verify we recognize this pack file format. */
-       if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
+       read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr));
+       if (read_result < 0)
+               return error_errno("error reading from %s", p->pack_name);
+       if (read_result != sizeof(hdr))
                return error("file %s is far too short to be a packfile", p->pack_name);
        if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
                return error("file %s is not a GIT packfile", p->pack_name);
@@ -500,7 +504,10 @@ static int open_packed_git_1(struct packed_git *p)
                             p->num_objects);
        if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
                return error("end of packfile %s is unavailable", p->pack_name);
-       if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
+       read_result = read_in_full(p->pack_fd, sha1, sizeof(sha1));
+       if (read_result < 0)
+               return error_errno("error reading from %s", p->pack_name);
+       if (read_result != sizeof(sha1))
                return error("packfile %s signature is unavailable", p->pack_name);
        idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40;
        if (hashcmp(sha1, idx_sha1))
index 0dd9fc6a0dd0a518200d9bbd834decb3c3ee22c6..fca7159646c82cb9213e72afd94d8debc6bd4c51 100644 (file)
@@ -581,6 +581,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
                                       const struct option *opts, int full, int err)
 {
        FILE *outfile = err ? stderr : stdout;
+       int need_newline;
 
        if (!usagestr)
                return PARSE_OPT_HELP;
@@ -599,12 +600,11 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
                if (**usagestr)
                        fprintf_ln(outfile, _("    %s"), _(*usagestr));
                else
-                       putchar('\n');
+                       fputc('\n', outfile);
                usagestr++;
        }
 
-       if (opts->type != OPTION_GROUP)
-               fputc('\n', outfile);
+       need_newline = 1;
 
        for (; opts->type != OPTION_END; opts++) {
                size_t pos;
@@ -612,6 +612,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 
                if (opts->type == OPTION_GROUP) {
                        fputc('\n', outfile);
+                       need_newline = 0;
                        if (*opts->help)
                                fprintf(outfile, "%s\n", _(opts->help));
                        continue;
@@ -619,6 +620,11 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
                if (!full && (opts->flags & PARSE_OPT_HIDDEN))
                        continue;
 
+               if (need_newline) {
+                       fputc('\n', outfile);
+                       need_newline = 0;
+               }
+
                pos = fprintf(outfile, "    ");
                if (opts->short_name) {
                        if (opts->flags & PARSE_OPT_NODASH)
diff --git a/path.c b/path.c
index bf096c411c300273f3490949179c893b993c67da..00ec04e7a5f9e90b758855a16f420324ee158739 100644 (file)
--- a/path.c
+++ b/path.c
@@ -637,8 +637,9 @@ void strbuf_git_common_path(struct strbuf *sb,
 int validate_headref(const char *path)
 {
        struct stat st;
-       char *buf, buffer[256];
-       unsigned char sha1[20];
+       char buffer[256];
+       const char *refname;
+       struct object_id oid;
        int fd;
        ssize_t len;
 
@@ -662,24 +663,24 @@ int validate_headref(const char *path)
        len = read_in_full(fd, buffer, sizeof(buffer)-1);
        close(fd);
 
+       if (len < 0)
+               return -1;
+       buffer[len] = '\0';
+
        /*
         * Is it a symbolic ref?
         */
-       if (len < 4)
-               return -1;
-       if (!memcmp("ref:", buffer, 4)) {
-               buf = buffer + 4;
-               len -= 4;
-               while (len && isspace(*buf))
-                       buf++, len--;
-               if (len >= 5 && !memcmp("refs/", buf, 5))
+       if (skip_prefix(buffer, "ref:", &refname)) {
+               while (isspace(*refname))
+                       refname++;
+               if (starts_with(refname, "refs/"))
                        return 0;
        }
 
        /*
         * Is this a detached HEAD?
         */
-       if (!get_sha1_hex(buffer, sha1))
+       if (!get_oid_hex(buffer, &oid))
                return 0;
 
        return -1;
index 647bbd3bceda71f15fdf137a37f3fa53e6fa6d86..93ea311443a37b81b85a72dc569c715417d7ef29 100644 (file)
@@ -258,7 +258,7 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size,
        }
 
        /* And complain if we didn't get enough bytes to satisfy the read. */
-       if (ret < size) {
+       if (ret != size) {
                if (options & PACKET_READ_GENTLE_ON_EOF)
                        return -1;
 
index bc591f4f3de0c07b0cfe3813d5d9daaf1ad44b63..55323620ab2aedc443989a876b4f4c43df6115cf 100644 (file)
@@ -295,9 +295,7 @@ static void if_atom_parser(const struct ref_format *format, struct used_atom *at
 
 static void head_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
 {
-       struct object_id unused;
-
-       atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, unused.hash, NULL);
+       atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
 }
 
 static struct {
@@ -1317,9 +1315,8 @@ static void populate_value(struct ref_array_item *ref)
        ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
 
        if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
-               struct object_id unused1;
                ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
-                                            unused1.hash, NULL);
+                                            NULL, NULL);
                if (!ref->symref)
                        ref->symref = "";
        }
index 74ebe5148f6623f0bffd612262519266a8ab2cc4..842b2f77dc3e40a2eb296bfade00d9d2104ff9e1 100644 (file)
@@ -61,11 +61,10 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
        reflogs->ref = xstrdup(ref);
        for_each_reflog_ent(ref, read_one_reflog, reflogs);
        if (reflogs->nr == 0) {
-               struct object_id oid;
                const char *name;
                void *name_to_free;
                name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
-                                                    oid.hash, NULL);
+                                                    NULL, NULL);
                if (name) {
                        for_each_reflog_ent(name, read_one_reflog, reflogs);
                        free(name_to_free);
@@ -151,9 +150,8 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
                reflogs = item->util;
        else {
                if (*branch == '\0') {
-                       struct object_id oid;
                        free(branch);
-                       branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
+                       branch = resolve_refdup("HEAD", 0, NULL, NULL);
                        if (!branch)
                                die ("No current branch");
 
diff --git a/refs.c b/refs.c
index 6042645c40614992bbd8ea0b06c53a350449f413..df075fcd06b0826982c94f27ab38395ac7ce30a6 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1285,6 +1285,10 @@ struct ref_iterator *refs_ref_iterator_begin(
        if (trim)
                iter = prefix_ref_iterator_begin(iter, "", trim);
 
+       /* Sanity check for subclasses: */
+       if (!iter->ordered)
+               BUG("reference iterator is not ordered");
+
        return iter;
 }
 
@@ -1686,7 +1690,23 @@ int refs_pack_refs(struct ref_store *refs, unsigned int flags)
 int refs_peel_ref(struct ref_store *refs, const char *refname,
                  unsigned char *sha1)
 {
-       return refs->be->peel_ref(refs, refname, sha1);
+       int flag;
+       unsigned char base[20];
+
+       if (current_ref_iter && current_ref_iter->refname == refname) {
+               struct object_id peeled;
+
+               if (ref_iterator_peel(current_ref_iter, &peeled))
+                       return -1;
+               hashcpy(sha1, peeled.hash);
+               return 0;
+       }
+
+       if (refs_read_ref_full(refs, refname,
+                              RESOLVE_REF_READING, base, &flag))
+               return -1;
+
+       return peel_object(base, sha1);
 }
 
 int peel_ref(const char *refname, unsigned char *sha1)
@@ -2036,3 +2056,14 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
 {
        return refs_rename_ref(get_main_ref_store(), oldref, newref, logmsg);
 }
+
+int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+                   const char *newref, const char *logmsg)
+{
+       return refs->be->copy_ref(refs, oldref, newref, logmsg);
+}
+
+int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg)
+{
+       return refs_copy_existing_ref(get_main_ref_store(), oldref, newref, logmsg);
+}
diff --git a/refs.h b/refs.h
index e1c5803f9a5aba0b6ba18f5a9bd5192cba661542..a02b628c8fe81b648f13c66b7e618d8660151e77 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -442,7 +442,14 @@ char *shorten_unambiguous_ref(const char *refname, int strict);
 /** rename ref, return 0 on success **/
 int refs_rename_ref(struct ref_store *refs, const char *oldref,
                    const char *newref, const char *logmsg);
-int rename_ref(const char *oldref, const char *newref, const char *logmsg);
+int rename_ref(const char *oldref, const char *newref,
+                       const char *logmsg);
+
+/** copy ref, return 0 on success **/
+int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+                   const char *newref, const char *logmsg);
+int copy_existing_ref(const char *oldref, const char *newref,
+                       const char *logmsg);
 
 int refs_create_symref(struct ref_store *refs, const char *refname,
                       const char *target, const char *logmsg);
index fec77744b40179b1454093bd37cf0815f3b755ae..014dabb0bf3647b9392d6087ebf6c40e1bc68108 100644 (file)
@@ -641,43 +641,6 @@ static int lock_raw_ref(struct files_ref_store *refs,
        return ret;
 }
 
-static int files_peel_ref(struct ref_store *ref_store,
-                         const char *refname, unsigned char *sha1)
-{
-       struct files_ref_store *refs =
-               files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
-                              "peel_ref");
-       int flag;
-       unsigned char base[20];
-
-       if (current_ref_iter && current_ref_iter->refname == refname) {
-               struct object_id peeled;
-
-               if (ref_iterator_peel(current_ref_iter, &peeled))
-                       return -1;
-               hashcpy(sha1, peeled.hash);
-               return 0;
-       }
-
-       if (refs_read_ref_full(ref_store, refname,
-                              RESOLVE_REF_READING, base, &flag))
-               return -1;
-
-       /*
-        * If the reference is packed, read its ref_entry from the
-        * cache in the hope that we already know its peeled value.
-        * We only try this optimization on packed references because
-        * (a) forcing the filling of the loose reference cache could
-        * be expensive and (b) loose references anyway usually do not
-        * have REF_KNOWS_PEELED.
-        */
-       if (flag & REF_ISPACKED &&
-           !refs_peel_ref(refs->packed_ref_store, refname, sha1))
-               return 0;
-
-       return peel_object(base, sha1);
-}
-
 struct files_ref_iterator {
        struct ref_iterator base;
 
@@ -748,7 +711,7 @@ static struct ref_iterator *files_ref_iterator_begin(
                const char *prefix, unsigned int flags)
 {
        struct files_ref_store *refs;
-       struct ref_iterator *loose_iter, *packed_iter;
+       struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
        struct files_ref_iterator *iter;
        struct ref_iterator *ref_iterator;
        unsigned int required_flags = REF_STORE_READ;
@@ -758,10 +721,6 @@ static struct ref_iterator *files_ref_iterator_begin(
 
        refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
 
-       iter = xcalloc(1, sizeof(*iter));
-       ref_iterator = &iter->base;
-       base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
-
        /*
         * We must make sure that all loose refs are read before
         * accessing the packed-refs file; this avoids a race
@@ -797,7 +756,13 @@ static struct ref_iterator *files_ref_iterator_begin(
                        refs->packed_ref_store, prefix, 0,
                        DO_FOR_EACH_INCLUDE_BROKEN);
 
-       iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
+       overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter);
+
+       iter = xcalloc(1, sizeof(*iter));
+       ref_iterator = &iter->base;
+       base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable,
+                              overlay_iter->ordered);
+       iter->iter0 = overlay_iter;
        iter->flags = flags;
 
        return ref_iterator;
@@ -1258,9 +1223,9 @@ static int commit_ref_update(struct files_ref_store *refs,
                             const struct object_id *oid, const char *logmsg,
                             struct strbuf *err);
 
-static int files_rename_ref(struct ref_store *ref_store,
+static int files_copy_or_rename_ref(struct ref_store *ref_store,
                            const char *oldrefname, const char *newrefname,
-                           const char *logmsg)
+                           const char *logmsg, int copy)
 {
        struct files_ref_store *refs =
                files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
@@ -1292,8 +1257,12 @@ static int files_rename_ref(struct ref_store *ref_store,
        }
 
        if (flag & REF_ISSYMREF) {
-               ret = error("refname %s is a symbolic ref, renaming it is not supported",
-                           oldrefname);
+               if (copy)
+                       ret = error("refname %s is a symbolic ref, copying it is not supported",
+                                   oldrefname);
+               else
+                       ret = error("refname %s is a symbolic ref, renaming it is not supported",
+                                   oldrefname);
                goto out;
        }
        if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
@@ -1301,13 +1270,19 @@ static int files_rename_ref(struct ref_store *ref_store,
                goto out;
        }
 
-       if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
+       if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
                ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
                            oldrefname, strerror(errno));
                goto out;
        }
 
-       if (refs_delete_ref(&refs->base, logmsg, oldrefname,
+       if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
+               ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
+                           oldrefname, strerror(errno));
+               goto out;
+       }
+
+       if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,
                            orig_oid.hash, REF_NODEREF)) {
                error("unable to delete old %s", oldrefname);
                goto rollback;
@@ -1320,7 +1295,7 @@ static int files_rename_ref(struct ref_store *ref_store,
         * the safety anyway; we want to delete the reference whatever
         * its current value.
         */
-       if (!refs_read_ref_full(&refs->base, newrefname,
+       if (!copy && !refs_read_ref_full(&refs->base, newrefname,
                                RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
                                oid.hash, NULL) &&
            refs_delete_ref(&refs->base, NULL, newrefname,
@@ -1351,7 +1326,10 @@ static int files_rename_ref(struct ref_store *ref_store,
        lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
                                   REF_NODEREF, NULL, &err);
        if (!lock) {
-               error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
+               if (copy)
+                       error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);
+               else
+                       error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
                strbuf_release(&err);
                goto rollback;
        }
@@ -1402,6 +1380,22 @@ static int files_rename_ref(struct ref_store *ref_store,
        return ret;
 }
 
+static int files_rename_ref(struct ref_store *ref_store,
+                           const char *oldrefname, const char *newrefname,
+                           const char *logmsg)
+{
+       return files_copy_or_rename_ref(ref_store, oldrefname,
+                                newrefname, logmsg, 0);
+}
+
+static int files_copy_ref(struct ref_store *ref_store,
+                           const char *oldrefname, const char *newrefname,
+                           const char *logmsg)
+{
+       return files_copy_or_rename_ref(ref_store, oldrefname,
+                                newrefname, logmsg, 1);
+}
+
 static int close_ref_gently(struct ref_lock *lock)
 {
        if (close_lock_file_gently(&lock->lk))
@@ -2065,7 +2059,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
        struct ref_iterator *ref_iterator = &iter->base;
        struct strbuf sb = STRBUF_INIT;
 
-       base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
+       base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
        strbuf_addf(&sb, "%s/logs", gitdir);
        iter->dir_iterator = dir_iterator_begin(sb.buf);
        iter->ref_store = ref_store;
@@ -2109,6 +2103,7 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st
                return reflog_iterator_begin(ref_store, refs->gitcommondir);
        } else {
                return merge_ref_iterator_begin(
+                       0,
                        reflog_iterator_begin(ref_store, refs->gitdir),
                        reflog_iterator_begin(ref_store, refs->gitcommondir),
                        reflog_iterator_select, refs);
@@ -2499,7 +2494,6 @@ static int files_transaction_prepare(struct ref_store *ref_store,
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
        char *head_ref = NULL;
        int head_type;
-       struct object_id head_oid;
        struct files_transaction_backend_data *backend_data;
        struct ref_transaction *packed_transaction = NULL;
 
@@ -2556,7 +2550,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
         */
        head_ref = refs_resolve_refdup(ref_store, "HEAD",
                                       RESOLVE_REF_NO_RECURSE,
-                                      head_oid.hash, &head_type);
+                                      NULL, &head_type);
 
        if (head_ref && !(head_type & REF_ISSYMREF)) {
                FREE_AND_NULL(head_ref);
@@ -3006,7 +3000,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
                } else if (update &&
                           (write_in_full(get_lock_file_fd(&lock->lk),
                                oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 ||
-                           write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 1 ||
+                           write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
                            close_ref_gently(lock) < 0)) {
                        status |= error("couldn't write %s",
                                        get_lock_file_path(&lock->lk));
@@ -3060,10 +3054,10 @@ struct ref_storage_be refs_be_files = {
        files_initial_transaction_commit,
 
        files_pack_refs,
-       files_peel_ref,
        files_create_symref,
        files_delete_refs,
        files_rename_ref,
+       files_copy_ref,
 
        files_ref_iterator_begin,
        files_read_raw_ref,
index 4cf449ef660e2bc9ee5b6ddb4efd8662d27e12b3..bd35da4e622ca4016194177ce97594b46c5aafe7 100644 (file)
@@ -25,9 +25,11 @@ int ref_iterator_abort(struct ref_iterator *ref_iterator)
 }
 
 void base_ref_iterator_init(struct ref_iterator *iter,
-                           struct ref_iterator_vtable *vtable)
+                           struct ref_iterator_vtable *vtable,
+                           int ordered)
 {
        iter->vtable = vtable;
+       iter->ordered = !!ordered;
        iter->refname = NULL;
        iter->oid = NULL;
        iter->flags = 0;
@@ -72,7 +74,7 @@ struct ref_iterator *empty_ref_iterator_begin(void)
        struct empty_ref_iterator *iter = xcalloc(1, sizeof(*iter));
        struct ref_iterator *ref_iterator = &iter->base;
 
-       base_ref_iterator_init(ref_iterator, &empty_ref_iterator_vtable);
+       base_ref_iterator_init(ref_iterator, &empty_ref_iterator_vtable, 1);
        return ref_iterator;
 }
 
@@ -205,6 +207,7 @@ static struct ref_iterator_vtable merge_ref_iterator_vtable = {
 };
 
 struct ref_iterator *merge_ref_iterator_begin(
+               int ordered,
                struct ref_iterator *iter0, struct ref_iterator *iter1,
                ref_iterator_select_fn *select, void *cb_data)
 {
@@ -219,7 +222,7 @@ struct ref_iterator *merge_ref_iterator_begin(
         * references through only if they exist in both iterators.
         */
 
-       base_ref_iterator_init(ref_iterator, &merge_ref_iterator_vtable);
+       base_ref_iterator_init(ref_iterator, &merge_ref_iterator_vtable, ordered);
        iter->iter0 = iter0;
        iter->iter1 = iter1;
        iter->select = select;
@@ -268,9 +271,11 @@ struct ref_iterator *overlay_ref_iterator_begin(
        } else if (is_empty_ref_iterator(back)) {
                ref_iterator_abort(back);
                return front;
+       } else if (!front->ordered || !back->ordered) {
+               BUG("overlay_ref_iterator requires ordered inputs");
        }
 
-       return merge_ref_iterator_begin(front, back,
+       return merge_ref_iterator_begin(1, front, back,
                                        overlay_iterator_select, NULL);
 }
 
@@ -282,6 +287,20 @@ struct prefix_ref_iterator {
        int trim;
 };
 
+/* Return -1, 0, 1 if refname is before, inside, or after the prefix. */
+static int compare_prefix(const char *refname, const char *prefix)
+{
+       while (*prefix) {
+               if (*refname != *prefix)
+                       return ((unsigned char)*refname < (unsigned char)*prefix) ? -1 : +1;
+
+               refname++;
+               prefix++;
+       }
+
+       return 0;
+}
+
 static int prefix_ref_iterator_advance(struct ref_iterator *ref_iterator)
 {
        struct prefix_ref_iterator *iter =
@@ -289,9 +308,25 @@ static int prefix_ref_iterator_advance(struct ref_iterator *ref_iterator)
        int ok;
 
        while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
-               if (!starts_with(iter->iter0->refname, iter->prefix))
+               int cmp = compare_prefix(iter->iter0->refname, iter->prefix);
+
+               if (cmp < 0)
                        continue;
 
+               if (cmp > 0) {
+                       /*
+                        * If the source iterator is ordered, then we
+                        * can stop the iteration as soon as we see a
+                        * refname that comes after the prefix:
+                        */
+                       if (iter->iter0->ordered) {
+                               ok = ref_iterator_abort(iter->iter0);
+                               break;
+                       } else {
+                               continue;
+                       }
+               }
+
                if (iter->trim) {
                        /*
                         * It is nonsense to trim off characters that
@@ -361,7 +396,7 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
        iter = xcalloc(1, sizeof(*iter));
        ref_iterator = &iter->base;
 
-       base_ref_iterator_init(ref_iterator, &prefix_ref_iterator_vtable);
+       base_ref_iterator_init(ref_iterator, &prefix_ref_iterator_vtable, iter0->ordered);
 
        iter->iter0 = iter0;
        iter->prefix = xstrdup(prefix);
index 2951514f251bcb9274932789a0535caefbf03c2d..3279d42c5a696434bbcdb85d6244cded1ddbc758 100644 (file)
 #include "../config.h"
 #include "../refs.h"
 #include "refs-internal.h"
-#include "ref-cache.h"
 #include "packed-backend.h"
 #include "../iterator.h"
 #include "../lockfile.h"
 
-struct packed_ref_cache {
-       struct ref_cache *cache;
+enum mmap_strategy {
+       /*
+        * Don't use mmap() at all for reading `packed-refs`.
+        */
+       MMAP_NONE,
 
        /*
-        * Count of references to the data structure in this instance,
-        * including the pointer from files_ref_store::packed if any.
-        * The data will not be freed as long as the reference count
-        * is nonzero.
+        * Can use mmap() for reading `packed-refs`, but the file must
+        * not remain mmapped. This is the usual option on Windows,
+        * where you cannot rename a new version of a file onto a file
+        * that is currently mmapped.
         */
-       unsigned int referrers;
+       MMAP_TEMPORARY,
 
-       /* The metadata from when this packed-refs cache was read */
-       struct stat_validity validity;
+       /*
+        * It is OK to leave the `packed-refs` file mmapped while
+        * arbitrary other code is running.
+        */
+       MMAP_OK
 };
 
-/*
- * Increment the reference count of *packed_refs.
- */
-static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
-{
-       packed_refs->referrers++;
-}
+#if defined(NO_MMAP)
+static enum mmap_strategy mmap_strategy = MMAP_NONE;
+#elif defined(MMAP_PREVENTS_DELETE)
+static enum mmap_strategy mmap_strategy = MMAP_TEMPORARY;
+#else
+static enum mmap_strategy mmap_strategy = MMAP_OK;
+#endif
+
+struct packed_ref_store;
 
 /*
- * Decrease the reference count of *packed_refs.  If it goes to zero,
- * free *packed_refs and return true; otherwise return false.
+ * A `snapshot` represents one snapshot of a `packed-refs` file.
+ *
+ * Normally, this will be a mmapped view of the contents of the
+ * `packed-refs` file at the time the snapshot was created. However,
+ * if the `packed-refs` file was not sorted, this might point at heap
+ * memory holding the contents of the `packed-refs` file with its
+ * records sorted by refname.
+ *
+ * `snapshot` instances are reference counted (via
+ * `acquire_snapshot()` and `release_snapshot()`). This is to prevent
+ * an instance from disappearing while an iterator is still iterating
+ * over it. Instances are garbage collected when their `referrers`
+ * count goes to zero.
+ *
+ * The most recent `snapshot`, if available, is referenced by the
+ * `packed_ref_store`. Its freshness is checked whenever
+ * `get_snapshot()` is called; if the existing snapshot is obsolete, a
+ * new snapshot is taken.
  */
-static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
-{
-       if (!--packed_refs->referrers) {
-               free_ref_cache(packed_refs->cache);
-               stat_validity_clear(&packed_refs->validity);
-               free(packed_refs);
-               return 1;
-       } else {
-               return 0;
-       }
-}
+struct snapshot {
+       /*
+        * A back-pointer to the packed_ref_store with which this
+        * snapshot is associated:
+        */
+       struct packed_ref_store *refs;
+
+       /* Is the `packed-refs` file currently mmapped? */
+       int mmapped;
+
+       /*
+        * The contents of the `packed-refs` file. If the file was
+        * already sorted, this points at the mmapped contents of the
+        * file. If not, this points at heap-allocated memory
+        * containing the contents, sorted. If there were no contents
+        * (e.g., because the file didn't exist), `buf` and `eof` are
+        * both NULL.
+        */
+       char *buf, *eof;
+
+       /* The size of the header line, if any; otherwise, 0: */
+       size_t header_len;
+
+       /*
+        * What is the peeled state of the `packed-refs` file that
+        * this snapshot represents? (This is usually determined from
+        * the file's header.)
+        */
+       enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled;
+
+       /*
+        * Count of references to this instance, including the pointer
+        * from `packed_ref_store::snapshot`, if any. The instance
+        * will not be freed as long as the reference count is
+        * nonzero.
+        */
+       unsigned int referrers;
+
+       /*
+        * The metadata of the `packed-refs` file from which this
+        * snapshot was created, used to tell if the file has been
+        * replaced since we read it.
+        */
+       struct stat_validity validity;
+};
 
 /*
- * A container for `packed-refs`-related data. It is not (yet) a
- * `ref_store`.
+ * A `ref_store` representing references stored in a `packed-refs`
+ * file. It implements the `ref_store` interface, though it has some
+ * limitations:
+ *
+ * - It cannot store symbolic references.
+ *
+ * - It cannot store reflogs.
+ *
+ * - It does not support reference renaming (though it could).
+ *
+ * On the other hand, it can be locked outside of a reference
+ * transaction. In that case, it remains locked even after the
+ * transaction is done and the new `packed-refs` file is activated.
  */
 struct packed_ref_store {
        struct ref_store base;
@@ -59,10 +127,10 @@ struct packed_ref_store {
        char *path;
 
        /*
-        * A cache of the values read from the `packed-refs` file, if
-        * it might still be current; otherwise, NULL.
+        * A snapshot of the values read from the `packed-refs` file,
+        * if it might still be current; otherwise, NULL.
         */
-       struct packed_ref_cache *cache;
+       struct snapshot *snapshot;
 
        /*
         * Lock used for the "packed-refs" file. Note that this (and
@@ -78,6 +146,49 @@ struct packed_ref_store {
        struct tempfile *tempfile;
 };
 
+/*
+ * Increment the reference count of `*snapshot`.
+ */
+static void acquire_snapshot(struct snapshot *snapshot)
+{
+       snapshot->referrers++;
+}
+
+/*
+ * If the buffer in `snapshot` is active, then either munmap the
+ * memory and close the file, or free the memory. Then set the buffer
+ * pointers to NULL.
+ */
+static void clear_snapshot_buffer(struct snapshot *snapshot)
+{
+       if (snapshot->mmapped) {
+               if (munmap(snapshot->buf, snapshot->eof - snapshot->buf))
+                       die_errno("error ummapping packed-refs file %s",
+                                 snapshot->refs->path);
+               snapshot->mmapped = 0;
+       } else {
+               free(snapshot->buf);
+       }
+       snapshot->buf = snapshot->eof = NULL;
+       snapshot->header_len = 0;
+}
+
+/*
+ * Decrease the reference count of `*snapshot`. If it goes to zero,
+ * free `*snapshot` and return true; otherwise return false.
+ */
+static int release_snapshot(struct snapshot *snapshot)
+{
+       if (!--snapshot->referrers) {
+               stat_validity_clear(&snapshot->validity);
+               clear_snapshot_buffer(snapshot);
+               free(snapshot);
+               return 1;
+       } else {
+               return 0;
+       }
+}
+
 struct ref_store *packed_ref_store_create(const char *path,
                                          unsigned int store_flags)
 {
@@ -116,64 +227,369 @@ static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
        return refs;
 }
 
-static void clear_packed_ref_cache(struct packed_ref_store *refs)
+static void clear_snapshot(struct packed_ref_store *refs)
 {
-       if (refs->cache) {
-               struct packed_ref_cache *cache = refs->cache;
+       if (refs->snapshot) {
+               struct snapshot *snapshot = refs->snapshot;
 
-               refs->cache = NULL;
-               release_packed_ref_cache(cache);
+               refs->snapshot = NULL;
+               release_snapshot(snapshot);
        }
 }
 
-/* The length of a peeled reference line in packed-refs, including EOL: */
-#define PEELED_LINE_LENGTH 42
+static NORETURN void die_unterminated_line(const char *path,
+                                          const char *p, size_t len)
+{
+       if (len < 80)
+               die("unterminated line in %s: %.*s", path, (int)len, p);
+       else
+               die("unterminated line in %s: %.75s...", path, p);
+}
+
+static NORETURN void die_invalid_line(const char *path,
+                                     const char *p, size_t len)
+{
+       const char *eol = memchr(p, '\n', len);
+
+       if (!eol)
+               die_unterminated_line(path, p, len);
+       else if (eol - p < 80)
+               die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
+       else
+               die("unexpected line in %s: %.75s...", path, p);
+
+}
+
+struct snapshot_record {
+       const char *start;
+       size_t len;
+};
+
+static int cmp_packed_ref_records(const void *v1, const void *v2)
+{
+       const struct snapshot_record *e1 = v1, *e2 = v2;
+       const char *r1 = e1->start + GIT_SHA1_HEXSZ + 1;
+       const char *r2 = e2->start + GIT_SHA1_HEXSZ + 1;
+
+       while (1) {
+               if (*r1 == '\n')
+                       return *r2 == '\n' ? 0 : -1;
+               if (*r1 != *r2) {
+                       if (*r2 == '\n')
+                               return 1;
+                       else
+                               return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
+               }
+               r1++;
+               r2++;
+       }
+}
 
 /*
- * Parse one line from a packed-refs file.  Write the SHA1 to sha1.
- * Return a pointer to the refname within the line (null-terminated),
- * or NULL if there was a problem.
+ * Compare a snapshot record at `rec` to the specified NUL-terminated
+ * refname.
  */
-static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
+static int cmp_record_to_refname(const char *rec, const char *refname)
 {
-       const char *ref;
+       const char *r1 = rec + GIT_SHA1_HEXSZ + 1;
+       const char *r2 = refname;
+
+       while (1) {
+               if (*r1 == '\n')
+                       return *r2 ? -1 : 0;
+               if (!*r2)
+                       return 1;
+               if (*r1 != *r2)
+                       return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1;
+               r1++;
+               r2++;
+       }
+}
 
-       if (parse_oid_hex(line->buf, oid, &ref) < 0)
-               return NULL;
-       if (!isspace(*ref++))
-               return NULL;
+/*
+ * `snapshot->buf` is not known to be sorted. Check whether it is, and
+ * if not, sort it into new memory and munmap/free the old storage.
+ */
+static void sort_snapshot(struct snapshot *snapshot)
+{
+       struct snapshot_record *records = NULL;
+       size_t alloc = 0, nr = 0;
+       int sorted = 1;
+       const char *pos, *eof, *eol;
+       size_t len, i;
+       char *new_buffer, *dst;
 
-       if (isspace(*ref))
-               return NULL;
+       pos = snapshot->buf + snapshot->header_len;
+       eof = snapshot->eof;
+       len = eof - pos;
 
-       if (line->buf[line->len - 1] != '\n')
-               return NULL;
-       line->buf[--line->len] = 0;
+       if (!len)
+               return;
+
+       /*
+        * Initialize records based on a crude estimate of the number
+        * of references in the file (we'll grow it below if needed):
+        */
+       ALLOC_GROW(records, len / 80 + 20, alloc);
+
+       while (pos < eof) {
+               eol = memchr(pos, '\n', eof - pos);
+               if (!eol)
+                       /* The safety check should prevent this. */
+                       BUG("unterminated line found in packed-refs");
+               if (eol - pos < GIT_SHA1_HEXSZ + 2)
+                       die_invalid_line(snapshot->refs->path,
+                                        pos, eof - pos);
+               eol++;
+               if (eol < eof && *eol == '^') {
+                       /*
+                        * Keep any peeled line together with its
+                        * reference:
+                        */
+                       const char *peeled_start = eol;
+
+                       eol = memchr(peeled_start, '\n', eof - peeled_start);
+                       if (!eol)
+                               /* The safety check should prevent this. */
+                               BUG("unterminated peeled line found in packed-refs");
+                       eol++;
+               }
+
+               ALLOC_GROW(records, nr + 1, alloc);
+               records[nr].start = pos;
+               records[nr].len = eol - pos;
+               nr++;
+
+               if (sorted &&
+                   nr > 1 &&
+                   cmp_packed_ref_records(&records[nr - 2],
+                                          &records[nr - 1]) >= 0)
+                       sorted = 0;
+
+               pos = eol;
+       }
+
+       if (sorted)
+               goto cleanup;
+
+       /* We need to sort the memory. First we sort the records array: */
+       QSORT(records, nr, cmp_packed_ref_records);
+
+       /*
+        * Allocate a new chunk of memory, and copy the old memory to
+        * the new in the order indicated by `records` (not bothering
+        * with the header line):
+        */
+       new_buffer = xmalloc(len);
+       for (dst = new_buffer, i = 0; i < nr; i++) {
+               memcpy(dst, records[i].start, records[i].len);
+               dst += records[i].len;
+       }
+
+       /*
+        * Now munmap the old buffer and use the sorted buffer in its
+        * place:
+        */
+       clear_snapshot_buffer(snapshot);
+       snapshot->buf = new_buffer;
+       snapshot->eof = new_buffer + len;
+       snapshot->header_len = 0;
+
+cleanup:
+       free(records);
+}
+
+/*
+ * Return a pointer to the start of the record that contains the
+ * character `*p` (which must be within the buffer). If no other
+ * record start is found, return `buf`.
+ */
+static const char *find_start_of_record(const char *buf, const char *p)
+{
+       while (p > buf && (p[-1] != '\n' || p[0] == '^'))
+               p--;
+       return p;
+}
+
+/*
+ * Return a pointer to the start of the record following the record
+ * that contains `*p`. If none is found before `end`, return `end`.
+ */
+static const char *find_end_of_record(const char *p, const char *end)
+{
+       while (++p < end && (p[-1] != '\n' || p[0] == '^'))
+               ;
+       return p;
+}
+
+/*
+ * We want to be able to compare mmapped reference records quickly,
+ * without totally parsing them. We can do so because the records are
+ * LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ
+ * + 1) bytes past the beginning of the record.
+ *
+ * But what if the `packed-refs` file contains garbage? We're willing
+ * to tolerate not detecting the problem, as long as we don't produce
+ * totally garbled output (we can't afford to check the integrity of
+ * the whole file during every Git invocation). But we do want to be
+ * sure that we never read past the end of the buffer in memory and
+ * perform an illegal memory access.
+ *
+ * Guarantee that minimum level of safety by verifying that the last
+ * record in the file is LF-terminated, and that it has at least
+ * (GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of
+ * these checks fails.
+ */
+static void verify_buffer_safe(struct snapshot *snapshot)
+{
+       const char *buf = snapshot->buf + snapshot->header_len;
+       const char *eof = snapshot->eof;
+       const char *last_line;
+
+       if (buf == eof)
+               return;
+
+       last_line = find_start_of_record(buf, eof - 1);
+       if (*(eof - 1) != '\n' || eof - last_line < GIT_SHA1_HEXSZ + 2)
+               die_invalid_line(snapshot->refs->path,
+                                last_line, eof - last_line);
+}
 
-       return ref;
+/*
+ * Depending on `mmap_strategy`, either mmap or read the contents of
+ * the `packed-refs` file into the snapshot. Return 1 if the file
+ * existed and was read, or 0 if the file was absent. Die on errors.
+ */
+static int load_contents(struct snapshot *snapshot)
+{
+       int fd;
+       struct stat st;
+       size_t size;
+       ssize_t bytes_read;
+
+       fd = open(snapshot->refs->path, O_RDONLY);
+       if (fd < 0) {
+               if (errno == ENOENT) {
+                       /*
+                        * This is OK; it just means that no
+                        * "packed-refs" file has been written yet,
+                        * which is equivalent to it being empty,
+                        * which is its state when initialized with
+                        * zeros.
+                        */
+                       return 0;
+               } else {
+                       die_errno("couldn't read %s", snapshot->refs->path);
+               }
+       }
+
+       stat_validity_update(&snapshot->validity, fd);
+
+       if (fstat(fd, &st) < 0)
+               die_errno("couldn't stat %s", snapshot->refs->path);
+       size = xsize_t(st.st_size);
+
+       switch (mmap_strategy) {
+       case MMAP_NONE:
+               snapshot->buf = xmalloc(size);
+               bytes_read = read_in_full(fd, snapshot->buf, size);
+               if (bytes_read < 0 || bytes_read != size)
+                       die_errno("couldn't read %s", snapshot->refs->path);
+               snapshot->eof = snapshot->buf + size;
+               snapshot->mmapped = 0;
+               break;
+       case MMAP_TEMPORARY:
+       case MMAP_OK:
+               snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
+               snapshot->eof = snapshot->buf + size;
+               snapshot->mmapped = 1;
+               break;
+       }
+       close(fd);
+
+       return 1;
 }
 
 /*
- * Read from `packed_refs_file` into a newly-allocated
- * `packed_ref_cache` and return it. The return value will already
- * have its reference count incremented.
+ * Find the place in `snapshot->buf` where the start of the record for
+ * `refname` starts. If `mustexist` is true and the reference doesn't
+ * exist, then return NULL. If `mustexist` is false and the reference
+ * doesn't exist, then return the point where that reference would be
+ * inserted. In the latter mode, `refname` doesn't have to be a proper
+ * reference name; for example, one could search for "refs/replace/"
+ * to find the start of any replace references.
+ *
+ * The record is sought using a binary search, so `snapshot->buf` must
+ * be sorted.
+ */
+static const char *find_reference_location(struct snapshot *snapshot,
+                                          const char *refname, int mustexist)
+{
+       /*
+        * This is not *quite* a garden-variety binary search, because
+        * the data we're searching is made up of records, and we
+        * always need to find the beginning of a record to do a
+        * comparison. A "record" here is one line for the reference
+        * itself and zero or one peel lines that start with '^'. Our
+        * loop invariant is described in the next two comments.
+        */
+
+       /*
+        * A pointer to the character at the start of a record whose
+        * preceding records all have reference names that come
+        * *before* `refname`.
+        */
+       const char *lo = snapshot->buf + snapshot->header_len;
+
+       /*
+        * A pointer to a the first character of a record whose
+        * reference name comes *after* `refname`.
+        */
+       const char *hi = snapshot->eof;
+
+       while (lo < hi) {
+               const char *mid, *rec;
+               int cmp;
+
+               mid = lo + (hi - lo) / 2;
+               rec = find_start_of_record(lo, mid);
+               cmp = cmp_record_to_refname(rec, refname);
+               if (cmp < 0) {
+                       lo = find_end_of_record(mid, hi);
+               } else if (cmp > 0) {
+                       hi = rec;
+               } else {
+                       return rec;
+               }
+       }
+
+       if (mustexist)
+               return NULL;
+       else
+               return lo;
+}
+
+/*
+ * Create a newly-allocated `snapshot` of the `packed-refs` file in
+ * its current state and return it. The return value will already have
+ * its reference count incremented.
  *
  * A comment line of the form "# pack-refs with: " may contain zero or
  * more traits. We interpret the traits as follows:
  *
- *   No traits:
+ *   Neither `peeled` nor `fully-peeled`:
  *
  *      Probably no references are peeled. But if the file contains a
  *      peeled value for a reference, we will use it.
  *
- *   peeled:
+ *   `peeled`:
  *
  *      References under "refs/tags/", if they *can* be peeled, *are*
  *      peeled in this file. References outside of "refs/tags/" are
  *      probably not peeled even if they could have been, but if we find
  *      a peeled value for such a reference we will use it.
  *
- *   fully-peeled:
+ *   `fully-peeled`:
  *
  *      All references in the file that can be peeled are peeled.
  *      Inversely (and this is more important), any references in the
@@ -181,141 +597,122 @@ static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
  *      trait should typically be written alongside "peeled" for
  *      compatibility with older clients, but we do not require it
  *      (i.e., "peeled" is a no-op if "fully-peeled" is set).
+ *
+ *   `sorted`:
+ *
+ *      The references in this file are known to be sorted by refname.
  */
-static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
+static struct snapshot *create_snapshot(struct packed_ref_store *refs)
 {
-       FILE *f;
-       struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
-       struct ref_entry *last = NULL;
-       struct strbuf line = STRBUF_INIT;
-       enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
-       struct ref_dir *dir;
-
-       acquire_packed_ref_cache(packed_refs);
-       packed_refs->cache = create_ref_cache(NULL, NULL);
-       packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
-
-       f = fopen(packed_refs_file, "r");
-       if (!f) {
-               if (errno == ENOENT) {
-                       /*
-                        * This is OK; it just means that no
-                        * "packed-refs" file has been written yet,
-                        * which is equivalent to it being empty.
-                        */
-                       return packed_refs;
-               } else {
-                       die_errno("couldn't read %s", packed_refs_file);
-               }
-       }
+       struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot));
+       int sorted = 0;
 
-       stat_validity_update(&packed_refs->validity, fileno(f));
+       snapshot->refs = refs;
+       acquire_snapshot(snapshot);
+       snapshot->peeled = PEELED_NONE;
 
-       dir = get_ref_dir(packed_refs->cache->root);
-       while (strbuf_getwholeline(&line, f, '\n') != EOF) {
-               struct object_id oid;
-               const char *refname;
-               const char *traits;
+       if (!load_contents(snapshot))
+               return snapshot;
 
-               if (!line.len || line.buf[line.len - 1] != '\n')
-                       die("unterminated line in %s: %s", packed_refs_file, line.buf);
+       /* If the file has a header line, process it: */
+       if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
+               struct strbuf tmp = STRBUF_INIT;
+               char *p;
+               const char *eol;
+               struct string_list traits = STRING_LIST_INIT_NODUP;
 
-               if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
-                       if (strstr(traits, " fully-peeled "))
-                               peeled = PEELED_FULLY;
-                       else if (strstr(traits, " peeled "))
-                               peeled = PEELED_TAGS;
-                       /* perhaps other traits later as well */
-                       continue;
-               }
+               eol = memchr(snapshot->buf, '\n',
+                            snapshot->eof - snapshot->buf);
+               if (!eol)
+                       die_unterminated_line(refs->path,
+                                             snapshot->buf,
+                                             snapshot->eof - snapshot->buf);
 
-               refname = parse_ref_line(&line, &oid);
-               if (refname) {
-                       int flag = REF_ISPACKED;
+               strbuf_add(&tmp, snapshot->buf, eol - snapshot->buf);
 
-                       if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
-                               if (!refname_is_safe(refname))
-                                       die("packed refname is dangerous: %s", refname);
-                               oidclr(&oid);
-                               flag |= REF_BAD_NAME | REF_ISBROKEN;
-                       }
-                       last = create_ref_entry(refname, &oid, flag);
-                       if (peeled == PEELED_FULLY ||
-                           (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
-                               last->flag |= REF_KNOWS_PEELED;
-                       add_ref_entry(dir, last);
-               } else if (last &&
-                   line.buf[0] == '^' &&
-                   line.len == PEELED_LINE_LENGTH &&
-                   line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
-                   !get_oid_hex(line.buf + 1, &oid)) {
-                       oidcpy(&last->u.value.peeled, &oid);
-                       /*
-                        * Regardless of what the file header said,
-                        * we definitely know the value of *this*
-                        * reference:
-                        */
-                       last->flag |= REF_KNOWS_PEELED;
-               } else {
-                       strbuf_setlen(&line, line.len - 1);
-                       die("unexpected line in %s: %s", packed_refs_file, line.buf);
-               }
+               if (!skip_prefix(tmp.buf, "# pack-refs with:", (const char **)&p))
+                       die_invalid_line(refs->path,
+                                        snapshot->buf,
+                                        snapshot->eof - snapshot->buf);
+
+               string_list_split_in_place(&traits, p, ' ', -1);
+
+               if (unsorted_string_list_has_string(&traits, "fully-peeled"))
+                       snapshot->peeled = PEELED_FULLY;
+               else if (unsorted_string_list_has_string(&traits, "peeled"))
+                       snapshot->peeled = PEELED_TAGS;
+
+               sorted = unsorted_string_list_has_string(&traits, "sorted");
+
+               /* perhaps other traits later as well */
+
+               /* The "+ 1" is for the LF character. */
+               snapshot->header_len = eol + 1 - snapshot->buf;
+
+               string_list_clear(&traits, 0);
+               strbuf_release(&tmp);
        }
 
-       fclose(f);
-       strbuf_release(&line);
+       verify_buffer_safe(snapshot);
 
-       return packed_refs;
+       if (!sorted) {
+               sort_snapshot(snapshot);
+
+               /*
+                * Reordering the records might have moved a short one
+                * to the end of the buffer, so verify the buffer's
+                * safety again:
+                */
+               verify_buffer_safe(snapshot);
+       }
+
+       if (mmap_strategy != MMAP_OK && snapshot->mmapped) {
+               /*
+                * We don't want to leave the file mmapped, so we are
+                * forced to make a copy now:
+                */
+               size_t size = snapshot->eof -
+                       (snapshot->buf + snapshot->header_len);
+               char *buf_copy = xmalloc(size);
+
+               memcpy(buf_copy, snapshot->buf + snapshot->header_len, size);
+               clear_snapshot_buffer(snapshot);
+               snapshot->buf = buf_copy;
+               snapshot->eof = buf_copy + size;
+       }
+
+       return snapshot;
 }
 
 /*
- * Check that the packed refs cache (if any) still reflects the
- * contents of the file. If not, clear the cache.
+ * Check that `refs->snapshot` (if present) still reflects the
+ * contents of the `packed-refs` file. If not, clear the snapshot.
  */
-static void validate_packed_ref_cache(struct packed_ref_store *refs)
+static void validate_snapshot(struct packed_ref_store *refs)
 {
-       if (refs->cache &&
-           !stat_validity_check(&refs->cache->validity, refs->path))
-               clear_packed_ref_cache(refs);
+       if (refs->snapshot &&
+           !stat_validity_check(&refs->snapshot->validity, refs->path))
+               clear_snapshot(refs);
 }
 
 /*
- * Get the packed_ref_cache for the specified packed_ref_store,
- * creating and populating it if it hasn't been read before or if the
- * file has been changed (according to its `validity` field) since it
- * was last read. On the other hand, if we hold the lock, then assume
- * that the file hasn't been changed out from under us, so skip the
- * extra `stat()` call in `stat_validity_check()`.
+ * Get the `snapshot` for the specified packed_ref_store, creating and
+ * populating it if it hasn't been read before or if the file has been
+ * changed (according to its `validity` field) since it was last read.
+ * On the other hand, if we hold the lock, then assume that the file
+ * hasn't been changed out from under us, so skip the extra `stat()`
+ * call in `stat_validity_check()`. This function does *not* increase
+ * the snapshot's reference count on behalf of the caller.
  */
-static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
+static struct snapshot *get_snapshot(struct packed_ref_store *refs)
 {
        if (!is_lock_file_locked(&refs->lock))
-               validate_packed_ref_cache(refs);
+               validate_snapshot(refs);
 
-       if (!refs->cache)
-               refs->cache = read_packed_refs(refs->path);
+       if (!refs->snapshot)
+               refs->snapshot = create_snapshot(refs);
 
-       return refs->cache;
-}
-
-static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
-{
-       return get_ref_dir(packed_ref_cache->cache->root);
-}
-
-static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
-{
-       return get_packed_ref_dir(get_packed_ref_cache(refs));
-}
-
-/*
- * Return the ref_entry for the given refname from the packed
- * references.  If it does not exist, return NULL.
- */
-static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
-                                       const char *refname)
-{
-       return find_ref_entry(get_packed_refs(refs), refname);
+       return refs->snapshot;
 }
 
 static int packed_read_raw_ref(struct ref_store *ref_store,
@@ -324,69 +721,147 @@ static int packed_read_raw_ref(struct ref_store *ref_store,
 {
        struct packed_ref_store *refs =
                packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
-
-       struct ref_entry *entry;
+       struct snapshot *snapshot = get_snapshot(refs);
+       const char *rec;
 
        *type = 0;
 
-       entry = get_packed_ref(refs, refname);
-       if (!entry) {
+       rec = find_reference_location(snapshot, refname, 1);
+
+       if (!rec) {
+               /* refname is not a packed reference. */
                errno = ENOENT;
                return -1;
        }
 
-       hashcpy(sha1, entry->u.value.oid.hash);
+       if (get_sha1_hex(rec, sha1))
+               die_invalid_line(refs->path, rec, snapshot->eof - rec);
+
        *type = REF_ISPACKED;
        return 0;
 }
 
-static int packed_peel_ref(struct ref_store *ref_store,
-                          const char *refname, unsigned char *sha1)
-{
-       struct packed_ref_store *refs =
-               packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
-                               "peel_ref");
-       struct ref_entry *r = get_packed_ref(refs, refname);
-
-       if (!r || peel_entry(r, 0))
-               return -1;
-
-       hashcpy(sha1, r->u.value.peeled.hash);
-       return 0;
-}
+/*
+ * This value is set in `base.flags` if the peeled value of the
+ * current reference is known. In that case, `peeled` contains the
+ * correct peeled value for the reference, which might be `null_sha1`
+ * if the reference is not a tag or if it is broken.
+ */
+#define REF_KNOWS_PEELED 0x40
 
+/*
+ * An iterator over a snapshot of a `packed-refs` file.
+ */
 struct packed_ref_iterator {
        struct ref_iterator base;
 
-       struct packed_ref_cache *cache;
-       struct ref_iterator *iter0;
+       struct snapshot *snapshot;
+
+       /* The current position in the snapshot's buffer: */
+       const char *pos;
+
+       /* The end of the part of the buffer that will be iterated over: */
+       const char *eof;
+
+       /* Scratch space for current values: */
+       struct object_id oid, peeled;
+       struct strbuf refname_buf;
+
        unsigned int flags;
 };
 
+/*
+ * Move the iterator to the next record in the snapshot, without
+ * respect for whether the record is actually required by the current
+ * iteration. Adjust the fields in `iter` and return `ITER_OK` or
+ * `ITER_DONE`. This function does not free the iterator in the case
+ * of `ITER_DONE`.
+ */
+static int next_record(struct packed_ref_iterator *iter)
+{
+       const char *p = iter->pos, *eol;
+
+       strbuf_reset(&iter->refname_buf);
+
+       if (iter->pos == iter->eof)
+               return ITER_DONE;
+
+       iter->base.flags = REF_ISPACKED;
+
+       if (iter->eof - p < GIT_SHA1_HEXSZ + 2 ||
+           parse_oid_hex(p, &iter->oid, &p) ||
+           !isspace(*p++))
+               die_invalid_line(iter->snapshot->refs->path,
+                                iter->pos, iter->eof - iter->pos);
+
+       eol = memchr(p, '\n', iter->eof - p);
+       if (!eol)
+               die_unterminated_line(iter->snapshot->refs->path,
+                                     iter->pos, iter->eof - iter->pos);
+
+       strbuf_add(&iter->refname_buf, p, eol - p);
+       iter->base.refname = iter->refname_buf.buf;
+
+       if (check_refname_format(iter->base.refname, REFNAME_ALLOW_ONELEVEL)) {
+               if (!refname_is_safe(iter->base.refname))
+                       die("packed refname is dangerous: %s",
+                           iter->base.refname);
+               oidclr(&iter->oid);
+               iter->base.flags |= REF_BAD_NAME | REF_ISBROKEN;
+       }
+       if (iter->snapshot->peeled == PEELED_FULLY ||
+           (iter->snapshot->peeled == PEELED_TAGS &&
+            starts_with(iter->base.refname, "refs/tags/")))
+               iter->base.flags |= REF_KNOWS_PEELED;
+
+       iter->pos = eol + 1;
+
+       if (iter->pos < iter->eof && *iter->pos == '^') {
+               p = iter->pos + 1;
+               if (iter->eof - p < GIT_SHA1_HEXSZ + 1 ||
+                   parse_oid_hex(p, &iter->peeled, &p) ||
+                   *p++ != '\n')
+                       die_invalid_line(iter->snapshot->refs->path,
+                                        iter->pos, iter->eof - iter->pos);
+               iter->pos = p;
+
+               /*
+                * Regardless of what the file header said, we
+                * definitely know the value of *this* reference. But
+                * we suppress it if the reference is broken:
+                */
+               if ((iter->base.flags & REF_ISBROKEN)) {
+                       oidclr(&iter->peeled);
+                       iter->base.flags &= ~REF_KNOWS_PEELED;
+               } else {
+                       iter->base.flags |= REF_KNOWS_PEELED;
+               }
+       } else {
+               oidclr(&iter->peeled);
+       }
+
+       return ITER_OK;
+}
+
 static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
 {
        struct packed_ref_iterator *iter =
                (struct packed_ref_iterator *)ref_iterator;
        int ok;
 
-       while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
+       while ((ok = next_record(iter)) == ITER_OK) {
                if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
-                   ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
+                   ref_type(iter->base.refname) != REF_TYPE_PER_WORKTREE)
                        continue;
 
                if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
-                   !ref_resolves_to_object(iter->iter0->refname,
-                                           iter->iter0->oid,
-                                           iter->iter0->flags))
+                   !ref_resolves_to_object(iter->base.refname, &iter->oid,
+                                           iter->flags))
                        continue;
 
-               iter->base.refname = iter->iter0->refname;
-               iter->base.oid = iter->iter0->oid;
-               iter->base.flags = iter->iter0->flags;
                return ITER_OK;
        }
 
-       iter->iter0 = NULL;
        if (ref_iterator_abort(ref_iterator) != ITER_DONE)
                ok = ITER_ERROR;
 
@@ -399,7 +874,14 @@ static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
        struct packed_ref_iterator *iter =
                (struct packed_ref_iterator *)ref_iterator;
 
-       return ref_iterator_peel(iter->iter0, peeled);
+       if ((iter->base.flags & REF_KNOWS_PEELED)) {
+               oidcpy(peeled, &iter->peeled);
+               return is_null_oid(&iter->peeled) ? -1 : 0;
+       } else if ((iter->base.flags & (REF_ISBROKEN | REF_ISSYMREF))) {
+               return -1;
+       } else {
+               return !!peel_object(iter->oid.hash, peeled->hash);
+       }
 }
 
 static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
@@ -408,10 +890,8 @@ static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
                (struct packed_ref_iterator *)ref_iterator;
        int ok = ITER_DONE;
 
-       if (iter->iter0)
-               ok = ref_iterator_abort(iter->iter0);
-
-       release_packed_ref_cache(iter->cache);
+       strbuf_release(&iter->refname_buf);
+       release_snapshot(iter->snapshot);
        base_ref_iterator_free(ref_iterator);
        return ok;
 }
@@ -427,6 +907,8 @@ static struct ref_iterator *packed_ref_iterator_begin(
                const char *prefix, unsigned int flags)
 {
        struct packed_ref_store *refs;
+       struct snapshot *snapshot;
+       const char *start;
        struct packed_ref_iterator *iter;
        struct ref_iterator *ref_iterator;
        unsigned int required_flags = REF_STORE_READ;
@@ -435,22 +917,40 @@ static struct ref_iterator *packed_ref_iterator_begin(
                required_flags |= REF_STORE_ODB;
        refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
 
+       /*
+        * Note that `get_snapshot()` internally checks whether the
+        * snapshot is up to date with what is on disk, and re-reads
+        * it if not.
+        */
+       snapshot = get_snapshot(refs);
+
+       if (!snapshot->buf)
+               return empty_ref_iterator_begin();
+
        iter = xcalloc(1, sizeof(*iter));
        ref_iterator = &iter->base;
-       base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);
+       base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
 
-       /*
-        * Note that get_packed_ref_cache() internally checks whether
-        * the packed-ref cache is up to date with what is on disk,
-        * and re-reads it if not.
-        */
+       iter->snapshot = snapshot;
+       acquire_snapshot(snapshot);
 
-       iter->cache = get_packed_ref_cache(refs);
-       acquire_packed_ref_cache(iter->cache);
-       iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
+       if (prefix && *prefix)
+               start = find_reference_location(snapshot, prefix, 0);
+       else
+               start = snapshot->buf + snapshot->header_len;
+
+       iter->pos = start;
+       iter->eof = snapshot->eof;
+       strbuf_init(&iter->refname_buf, 0);
+
+       iter->base.oid = &iter->oid;
 
        iter->flags = flags;
 
+       if (prefix && *prefix)
+               /* Stop iteration after we've gone *past* prefix: */
+               ref_iterator = prefix_ref_iterator_begin(ref_iterator, prefix, 0);
+
        return ref_iterator;
 }
 
@@ -505,19 +1005,19 @@ int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
 
        /*
         * Now that we hold the `packed-refs` lock, make sure that our
-        * cache matches the current version of the file. Normally
-        * `get_packed_ref_cache()` does that for us, but that
-        * function assumes that when the file is locked, any existing
-        * cache is still valid. We've just locked the file, but it
-        * might have changed the moment *before* we locked it.
+        * snapshot matches the current version of the file. Normally
+        * `get_snapshot()` does that for us, but that function
+        * assumes that when the file is locked, any existing snapshot
+        * is still valid. We've just locked the file, but it might
+        * have changed the moment *before* we locked it.
         */
-       validate_packed_ref_cache(refs);
+       validate_snapshot(refs);
 
        /*
         * Now make sure that the packed-refs file as it exists in the
-        * locked state is loaded into the cache:
+        * locked state is loaded into the snapshot:
         */
-       get_packed_ref_cache(refs);
+       get_snapshot(refs);
        return 0;
 }
 
@@ -544,11 +1044,15 @@ int packed_refs_is_locked(struct ref_store *ref_store)
 }
 
 /*
- * The packed-refs header line that we write out.  Perhaps other
- * traits will be added later.  The trailing space is required.
+ * The packed-refs header line that we write out. Perhaps other traits
+ * will be added later.
+ *
+ * Note that earlier versions of Git used to parse these traits by
+ * looking for " trait " in the line. For this reason, the space after
+ * the colon and the trailing space are required.
  */
 static const char PACKED_REFS_HEADER[] =
-       "# pack-refs with: peeled fully-peeled \n";
+       "# pack-refs with: peeled fully-peeled sorted \n";
 
 static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
 {
@@ -557,9 +1061,9 @@ static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
 }
 
 /*
- * Write the packed-refs from the cache to the packed-refs tempfile,
- * incorporating any changes from `updates`. `updates` must be a
- * sorted string list whose keys are the refnames and whose util
+ * Write the packed refs from the current snapshot to the packed-refs
+ * tempfile, incorporating any changes from `updates`. `updates` must
+ * be a sorted string list whose keys are the refnames and whose util
  * values are `struct ref_update *`. On error, rollback the tempfile,
  * write an error message to `err`, and return a nonzero value.
  *
@@ -802,9 +1306,10 @@ static int packed_transaction_prepare(struct ref_store *ref_store,
        /*
         * Note that we *don't* skip transactions with zero updates,
         * because such a transaction might be executed for the side
-        * effect of ensuring that all of the references are peeled.
-        * If the caller wants to optimize away empty transactions, it
-        * should do so itself.
+        * effect of ensuring that all of the references are peeled or
+        * ensuring that the `packed-refs` file is sorted. If the
+        * caller wants to optimize away empty transactions, it should
+        * do so itself.
         */
 
        data = xcalloc(1, sizeof(*data));
@@ -870,6 +1375,8 @@ static int packed_transaction_finish(struct ref_store *ref_store,
        int ret = TRANSACTION_GENERIC_ERROR;
        char *packed_refs_path;
 
+       clear_snapshot(refs);
+
        packed_refs_path = get_locked_file_path(&refs->lock);
        if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
                strbuf_addf(err, "error replacing %s: %s",
@@ -877,7 +1384,6 @@ static int packed_transaction_finish(struct ref_store *ref_store,
                goto cleanup;
        }
 
-       clear_packed_ref_cache(refs);
        ret = 0;
 
 cleanup:
@@ -966,6 +1472,13 @@ static int packed_rename_ref(struct ref_store *ref_store,
        die("BUG: packed reference store does not support renaming references");
 }
 
+static int packed_copy_ref(struct ref_store *ref_store,
+                          const char *oldrefname, const char *newrefname,
+                          const char *logmsg)
+{
+       die("BUG: packed reference store does not support copying references");
+}
+
 static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
 {
        return empty_ref_iterator_begin();
@@ -1027,10 +1540,10 @@ struct ref_storage_be refs_be_packed = {
        packed_initial_transaction_commit,
 
        packed_pack_refs,
-       packed_peel_ref,
        packed_create_symref,
        packed_delete_refs,
        packed_rename_ref,
+       packed_copy_ref,
 
        packed_ref_iterator_begin,
        packed_read_raw_ref,
index 76bb723c8674c3022b271c93b587d941c8e6d397..4f850e1b5c9ed1fae746a804ceceee42e2f54b86 100644 (file)
@@ -38,7 +38,6 @@ struct ref_entry *create_ref_entry(const char *refname,
 
        FLEX_ALLOC_STR(ref, name, refname);
        oidcpy(&ref->u.value.oid, oid);
-       oidclr(&ref->u.value.peeled);
        ref->flag = flag;
        return ref;
 }
@@ -491,49 +490,10 @@ static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator)
        }
 }
 
-enum peel_status peel_entry(struct ref_entry *entry, int repeel)
-{
-       enum peel_status status;
-
-       if (entry->flag & REF_KNOWS_PEELED) {
-               if (repeel) {
-                       entry->flag &= ~REF_KNOWS_PEELED;
-                       oidclr(&entry->u.value.peeled);
-               } else {
-                       return is_null_oid(&entry->u.value.peeled) ?
-                               PEEL_NON_TAG : PEEL_PEELED;
-               }
-       }
-       if (entry->flag & REF_ISBROKEN)
-               return PEEL_BROKEN;
-       if (entry->flag & REF_ISSYMREF)
-               return PEEL_IS_SYMREF;
-
-       status = peel_object(entry->u.value.oid.hash, entry->u.value.peeled.hash);
-       if (status == PEEL_PEELED || status == PEEL_NON_TAG)
-               entry->flag |= REF_KNOWS_PEELED;
-       return status;
-}
-
 static int cache_ref_iterator_peel(struct ref_iterator *ref_iterator,
                                   struct object_id *peeled)
 {
-       struct cache_ref_iterator *iter =
-               (struct cache_ref_iterator *)ref_iterator;
-       struct cache_ref_iterator_level *level;
-       struct ref_entry *entry;
-
-       level = &iter->levels[iter->levels_nr - 1];
-
-       if (level->index == -1)
-               die("BUG: peel called before advance for cache iterator");
-
-       entry = level->dir->entries[level->index];
-
-       if (peel_entry(entry, 0))
-               return -1;
-       oidcpy(peeled, &entry->u.value.peeled);
-       return 0;
+       return peel_object(ref_iterator->oid->hash, peeled->hash);
 }
 
 static int cache_ref_iterator_abort(struct ref_iterator *ref_iterator)
@@ -574,7 +534,7 @@ struct ref_iterator *cache_ref_iterator_begin(struct ref_cache *cache,
 
        iter = xcalloc(1, sizeof(*iter));
        ref_iterator = &iter->base;
-       base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable);
+       base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable, 1);
        ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
 
        iter->levels_nr = 1;
index 794f000fd31ebad3ce340ffd9af3fc843b089479..eda65e73edd2d978e7c07065b888219f42348d87 100644 (file)
@@ -38,14 +38,6 @@ struct ref_value {
         * referred to by the last reference in the symlink chain.
         */
        struct object_id oid;
-
-       /*
-        * If REF_KNOWS_PEELED, then this field holds the peeled value
-        * of this reference, or null if the reference is known not to
-        * be peelable.  See the documentation for peel_ref() for an
-        * exact definition of "peelable".
-        */
-       struct object_id peeled;
 };
 
 /*
@@ -97,21 +89,14 @@ struct ref_dir {
  * public values; see refs.h.
  */
 
-/*
- * The field ref_entry->u.value.peeled of this value entry contains
- * the correct peeled value for the reference, which might be
- * null_sha1 if the reference is not a tag or if it is broken.
- */
-#define REF_KNOWS_PEELED 0x10
-
 /* ref_entry represents a directory of references */
-#define REF_DIR 0x20
+#define REF_DIR 0x10
 
 /*
  * Entry has not yet been read from disk (used only for REF_DIR
  * entries representing loose references)
  */
-#define REF_INCOMPLETE 0x40
+#define REF_INCOMPLETE 0x20
 
 /*
  * A ref_entry represents either a reference or a "subdirectory" of
@@ -245,23 +230,11 @@ struct ref_entry *find_ref_entry(struct ref_dir *dir, const char *refname);
  * Start iterating over references in `cache`. If `prefix` is
  * specified, only include references whose names start with that
  * prefix. If `prime_dir` is true, then fill any incomplete
- * directories before beginning the iteration.
+ * directories before beginning the iteration. The output is ordered
+ * by refname.
  */
 struct ref_iterator *cache_ref_iterator_begin(struct ref_cache *cache,
                                              const char *prefix,
                                              int prime_dir);
 
-/*
- * Peel the entry (if possible) and return its new peel_status.  If
- * repeel is true, re-peel the entry even if there is an old peeled
- * value that is already stored in it.
- *
- * It is OK to call this function with a packed reference entry that
- * might be stale and might even refer to an object that has since
- * been garbage-collected.  In such a case, if the entry has
- * REF_KNOWS_PEELED then leave the status unchanged and return
- * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID.
- */
-enum peel_status peel_entry(struct ref_entry *entry, int repeel);
-
 #endif /* REFS_REF_CACHE_H */
index d7d344de73e6f85fed1e8460bce246a894eab6da..448de4bccb8f3d9531b50d5489af001ec7ce4732 100644 (file)
@@ -329,6 +329,13 @@ int refs_rename_ref_available(struct ref_store *refs,
  */
 struct ref_iterator {
        struct ref_iterator_vtable *vtable;
+
+       /*
+        * Does this `ref_iterator` iterate over references in order
+        * by refname?
+        */
+       unsigned int ordered : 1;
+
        const char *refname;
        const struct object_id *oid;
        unsigned int flags;
@@ -374,7 +381,7 @@ int is_empty_ref_iterator(struct ref_iterator *ref_iterator);
  * which the refname begins with prefix. If trim is non-zero, then
  * trim that many characters off the beginning of each refname. flags
  * can be DO_FOR_EACH_INCLUDE_BROKEN to include broken references in
- * the iteration.
+ * the iteration. The output is ordered by refname.
  */
 struct ref_iterator *refs_ref_iterator_begin(
                struct ref_store *refs,
@@ -400,9 +407,11 @@ typedef enum iterator_selection ref_iterator_select_fn(
  * Iterate over the entries from iter0 and iter1, with the values
  * interleaved as directed by the select function. The iterator takes
  * ownership of iter0 and iter1 and frees them when the iteration is
- * over.
+ * over. A derived class should set `ordered` to 1 or 0 based on
+ * whether it generates its output in order by reference name.
  */
 struct ref_iterator *merge_ref_iterator_begin(
+               int ordered,
                struct ref_iterator *iter0, struct ref_iterator *iter1,
                ref_iterator_select_fn *select, void *cb_data);
 
@@ -431,6 +440,8 @@ struct ref_iterator *overlay_ref_iterator_begin(
  * As an convenience to callers, if prefix is the empty string and
  * trim is zero, this function returns iter0 directly, without
  * wrapping it.
+ *
+ * The resulting ref_iterator is ordered if iter0 is.
  */
 struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
                                               const char *prefix,
@@ -441,11 +452,14 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
 /*
  * Base class constructor for ref_iterators. Initialize the
  * ref_iterator part of iter, setting its vtable pointer as specified.
+ * `ordered` should be set to 1 if the iterator will iterate over
+ * references in order by refname; otherwise it should be set to 0.
  * This is meant to be called only by the initializers of derived
  * classes.
  */
 void base_ref_iterator_init(struct ref_iterator *iter,
-                           struct ref_iterator_vtable *vtable);
+                           struct ref_iterator_vtable *vtable,
+                           int ordered);
 
 /*
  * Base class destructor for ref_iterators. Destroy the ref_iterator
@@ -548,8 +562,6 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,
                                      struct strbuf *err);
 
 typedef int pack_refs_fn(struct ref_store *ref_store, unsigned int flags);
-typedef int peel_ref_fn(struct ref_store *ref_store,
-                       const char *refname, unsigned char *sha1);
 typedef int create_symref_fn(struct ref_store *ref_store,
                             const char *ref_target,
                             const char *refs_heads_master,
@@ -559,12 +571,16 @@ typedef int delete_refs_fn(struct ref_store *ref_store, const char *msg,
 typedef int rename_ref_fn(struct ref_store *ref_store,
                          const char *oldref, const char *newref,
                          const char *logmsg);
+typedef int copy_ref_fn(struct ref_store *ref_store,
+                         const char *oldref, const char *newref,
+                         const char *logmsg);
 
 /*
  * Iterate over the references in `ref_store` whose names start with
  * `prefix`. `prefix` is matched as a literal string, without regard
  * for path separators. If prefix is NULL or the empty string, iterate
- * over all references in `ref_store`.
+ * over all references in `ref_store`. The output is ordered by
+ * refname.
  */
 typedef struct ref_iterator *ref_iterator_begin_fn(
                struct ref_store *ref_store,
@@ -653,10 +669,10 @@ struct ref_storage_be {
        ref_transaction_commit_fn *initial_transaction_commit;
 
        pack_refs_fn *pack_refs;
-       peel_ref_fn *peel_ref;
        create_symref_fn *create_symref;
        delete_refs_fn *delete_refs;
        rename_ref_fn *rename_ref;
+       copy_ref_fn *copy_ref;
 
        ref_iterator_begin_fn *iterator_begin;
        read_raw_ref_fn *read_raw_ref;
index ba95cefc7d22d849163d28795d644995641606ef..7886e2269eb7813cdf9f908002ec70a9561a153c 100644 (file)
@@ -20,6 +20,7 @@
 #include "trailer.h"
 #include "log-tree.h"
 #include "wt-status.h"
+#include "hashmap.h"
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
@@ -2435,3 +2436,533 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
 
        strbuf_release(&sob);
 }
+
+int sequencer_make_script(int keep_empty, FILE *out,
+               int argc, const char **argv)
+{
+       char *format = NULL;
+       struct pretty_print_context pp = {0};
+       struct strbuf buf = STRBUF_INIT;
+       struct rev_info revs;
+       struct commit *commit;
+
+       init_revisions(&revs, NULL);
+       revs.verbose_header = 1;
+       revs.max_parents = 1;
+       revs.cherry_pick = 1;
+       revs.limited = 1;
+       revs.reverse = 1;
+       revs.right_only = 1;
+       revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
+       revs.topo_order = 1;
+
+       revs.pretty_given = 1;
+       git_config_get_string("rebase.instructionFormat", &format);
+       if (!format || !*format) {
+               free(format);
+               format = xstrdup("%s");
+       }
+       get_commit_format(format, &revs);
+       free(format);
+       pp.fmt = revs.commit_format;
+       pp.output_encoding = get_log_output_encoding();
+
+       if (setup_revisions(argc, argv, &revs, NULL) > 1)
+               return error(_("make_script: unhandled options"));
+
+       if (prepare_revision_walk(&revs) < 0)
+               return error(_("make_script: error preparing revisions"));
+
+       while ((commit = get_revision(&revs))) {
+               strbuf_reset(&buf);
+               if (!keep_empty && is_original_commit_empty(commit))
+                       strbuf_addf(&buf, "%c ", comment_line_char);
+               strbuf_addf(&buf, "pick %s ", oid_to_hex(&commit->object.oid));
+               pretty_print_commit(&pp, commit, &buf);
+               strbuf_addch(&buf, '\n');
+               fputs(buf.buf, out);
+       }
+       strbuf_release(&buf);
+       return 0;
+}
+
+
+int transform_todo_ids(int shorten_ids)
+{
+       const char *todo_file = rebase_path_todo();
+       struct todo_list todo_list = TODO_LIST_INIT;
+       int fd, res, i;
+       FILE *out;
+
+       strbuf_reset(&todo_list.buf);
+       fd = open(todo_file, O_RDONLY);
+       if (fd < 0)
+               return error_errno(_("could not open '%s'"), todo_file);
+       if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
+               close(fd);
+               return error(_("could not read '%s'."), todo_file);
+       }
+       close(fd);
+
+       res = parse_insn_buffer(todo_list.buf.buf, &todo_list);
+       if (res) {
+               todo_list_release(&todo_list);
+               return error(_("unusable todo list: '%s'"), todo_file);
+       }
+
+       out = fopen(todo_file, "w");
+       if (!out) {
+               todo_list_release(&todo_list);
+               return error(_("unable to open '%s' for writing"), todo_file);
+       }
+       for (i = 0; i < todo_list.nr; i++) {
+               struct todo_item *item = todo_list.items + i;
+               int bol = item->offset_in_buf;
+               const char *p = todo_list.buf.buf + bol;
+               int eol = i + 1 < todo_list.nr ?
+                       todo_list.items[i + 1].offset_in_buf :
+                       todo_list.buf.len;
+
+               if (item->command >= TODO_EXEC && item->command != TODO_DROP)
+                       fwrite(p, eol - bol, 1, out);
+               else {
+                       const char *id = shorten_ids ?
+                               short_commit_name(item->commit) :
+                               oid_to_hex(&item->commit->object.oid);
+                       int len;
+
+                       p += strspn(p, " \t"); /* left-trim command */
+                       len = strcspn(p, " \t"); /* length of command */
+
+                       fprintf(out, "%.*s %s %.*s\n",
+                               len, p, id, item->arg_len, item->arg);
+               }
+       }
+       fclose(out);
+       todo_list_release(&todo_list);
+       return 0;
+}
+
+enum check_level {
+       CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR
+};
+
+static enum check_level get_missing_commit_check_level(void)
+{
+       const char *value;
+
+       if (git_config_get_value("rebase.missingcommitscheck", &value) ||
+                       !strcasecmp("ignore", value))
+               return CHECK_IGNORE;
+       if (!strcasecmp("warn", value))
+               return CHECK_WARN;
+       if (!strcasecmp("error", value))
+               return CHECK_ERROR;
+       warning(_("unrecognized setting %s for option"
+                 "rebase.missingCommitsCheck. Ignoring."), value);
+       return CHECK_IGNORE;
+}
+
+/*
+ * Check if the user dropped some commits by mistake
+ * Behaviour determined by rebase.missingCommitsCheck.
+ * Check if there is an unrecognized command or a
+ * bad SHA-1 in a command.
+ */
+int check_todo_list(void)
+{
+       enum check_level check_level = get_missing_commit_check_level();
+       struct strbuf todo_file = STRBUF_INIT;
+       struct todo_list todo_list = TODO_LIST_INIT;
+       struct strbuf missing = STRBUF_INIT;
+       int advise_to_edit_todo = 0, res = 0, fd, i;
+
+       strbuf_addstr(&todo_file, rebase_path_todo());
+       fd = open(todo_file.buf, O_RDONLY);
+       if (fd < 0) {
+               res = error_errno(_("could not open '%s'"), todo_file.buf);
+               goto leave_check;
+       }
+       if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
+               close(fd);
+               res = error(_("could not read '%s'."), todo_file.buf);
+               goto leave_check;
+       }
+       close(fd);
+       advise_to_edit_todo = res =
+               parse_insn_buffer(todo_list.buf.buf, &todo_list);
+
+       if (res || check_level == CHECK_IGNORE)
+               goto leave_check;
+
+       /* Mark the commits in git-rebase-todo as seen */
+       for (i = 0; i < todo_list.nr; i++) {
+               struct commit *commit = todo_list.items[i].commit;
+               if (commit)
+                       commit->util = (void *)1;
+       }
+
+       todo_list_release(&todo_list);
+       strbuf_addstr(&todo_file, ".backup");
+       fd = open(todo_file.buf, O_RDONLY);
+       if (fd < 0) {
+               res = error_errno(_("could not open '%s'"), todo_file.buf);
+               goto leave_check;
+       }
+       if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
+               close(fd);
+               res = error(_("could not read '%s'."), todo_file.buf);
+               goto leave_check;
+       }
+       close(fd);
+       strbuf_release(&todo_file);
+       res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
+
+       /* Find commits in git-rebase-todo.backup yet unseen */
+       for (i = todo_list.nr - 1; i >= 0; i--) {
+               struct todo_item *item = todo_list.items + i;
+               struct commit *commit = item->commit;
+               if (commit && !commit->util) {
+                       strbuf_addf(&missing, " - %s %.*s\n",
+                                   short_commit_name(commit),
+                                   item->arg_len, item->arg);
+                       commit->util = (void *)1;
+               }
+       }
+
+       /* Warn about missing commits */
+       if (!missing.len)
+               goto leave_check;
+
+       if (check_level == CHECK_ERROR)
+               advise_to_edit_todo = res = 1;
+
+       fprintf(stderr,
+               _("Warning: some commits may have been dropped accidentally.\n"
+               "Dropped commits (newer to older):\n"));
+
+       /* Make the list user-friendly and display */
+       fputs(missing.buf, stderr);
+       strbuf_release(&missing);
+
+       fprintf(stderr, _("To avoid this message, use \"drop\" to "
+               "explicitly remove a commit.\n\n"
+               "Use 'git config rebase.missingCommitsCheck' to change "
+               "the level of warnings.\n"
+               "The possible behaviours are: ignore, warn, error.\n\n"));
+
+leave_check:
+       strbuf_release(&todo_file);
+       todo_list_release(&todo_list);
+
+       if (advise_to_edit_todo)
+               fprintf(stderr,
+                       _("You can fix this with 'git rebase --edit-todo' "
+                         "and then run 'git rebase --continue'.\n"
+                         "Or you can abort the rebase with 'git rebase"
+                         " --abort'.\n"));
+
+       return res;
+}
+
+/* skip picking commits whose parents are unchanged */
+int skip_unnecessary_picks(void)
+{
+       const char *todo_file = rebase_path_todo();
+       struct strbuf buf = STRBUF_INIT;
+       struct todo_list todo_list = TODO_LIST_INIT;
+       struct object_id onto_oid, *oid = &onto_oid, *parent_oid;
+       int fd, i;
+
+       if (!read_oneliner(&buf, rebase_path_onto(), 0))
+               return error(_("could not read 'onto'"));
+       if (get_oid(buf.buf, &onto_oid)) {
+               strbuf_release(&buf);
+               return error(_("need a HEAD to fixup"));
+       }
+       strbuf_release(&buf);
+
+       fd = open(todo_file, O_RDONLY);
+       if (fd < 0) {
+               return error_errno(_("could not open '%s'"), todo_file);
+       }
+       if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
+               close(fd);
+               return error(_("could not read '%s'."), todo_file);
+       }
+       close(fd);
+       if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
+               todo_list_release(&todo_list);
+               return -1;
+       }
+
+       for (i = 0; i < todo_list.nr; i++) {
+               struct todo_item *item = todo_list.items + i;
+
+               if (item->command >= TODO_NOOP)
+                       continue;
+               if (item->command != TODO_PICK)
+                       break;
+               if (parse_commit(item->commit)) {
+                       todo_list_release(&todo_list);
+                       return error(_("could not parse commit '%s'"),
+                               oid_to_hex(&item->commit->object.oid));
+               }
+               if (!item->commit->parents)
+                       break; /* root commit */
+               if (item->commit->parents->next)
+                       break; /* merge commit */
+               parent_oid = &item->commit->parents->item->object.oid;
+               if (hashcmp(parent_oid->hash, oid->hash))
+                       break;
+               oid = &item->commit->object.oid;
+       }
+       if (i > 0) {
+               int offset = i < todo_list.nr ?
+                       todo_list.items[i].offset_in_buf : todo_list.buf.len;
+               const char *done_path = rebase_path_done();
+
+               fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
+               if (fd < 0) {
+                       error_errno(_("could not open '%s' for writing"),
+                                   done_path);
+                       todo_list_release(&todo_list);
+                       return -1;
+               }
+               if (write_in_full(fd, todo_list.buf.buf, offset) < 0) {
+                       error_errno(_("could not write to '%s'"), done_path);
+                       todo_list_release(&todo_list);
+                       close(fd);
+                       return -1;
+               }
+               close(fd);
+
+               fd = open(rebase_path_todo(), O_WRONLY, 0666);
+               if (fd < 0) {
+                       error_errno(_("could not open '%s' for writing"),
+                                   rebase_path_todo());
+                       todo_list_release(&todo_list);
+                       return -1;
+               }
+               if (write_in_full(fd, todo_list.buf.buf + offset,
+                               todo_list.buf.len - offset) < 0) {
+                       error_errno(_("could not write to '%s'"),
+                                   rebase_path_todo());
+                       close(fd);
+                       todo_list_release(&todo_list);
+                       return -1;
+               }
+               if (ftruncate(fd, todo_list.buf.len - offset) < 0) {
+                       error_errno(_("could not truncate '%s'"),
+                                   rebase_path_todo());
+                       todo_list_release(&todo_list);
+                       close(fd);
+                       return -1;
+               }
+               close(fd);
+
+               todo_list.current = i;
+               if (is_fixup(peek_command(&todo_list, 0)))
+                       record_in_rewritten(oid, peek_command(&todo_list, 0));
+       }
+
+       todo_list_release(&todo_list);
+       printf("%s\n", oid_to_hex(oid));
+
+       return 0;
+}
+
+struct subject2item_entry {
+       struct hashmap_entry entry;
+       int i;
+       char subject[FLEX_ARRAY];
+};
+
+static int subject2item_cmp(const void *fndata,
+                           const struct subject2item_entry *a,
+                           const struct subject2item_entry *b, const void *key)
+{
+       return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject);
+}
+
+/*
+ * Rearrange the todo list that has both "pick commit-id msg" and "pick
+ * commit-id fixup!/squash! msg" in it so that the latter is put immediately
+ * after the former, and change "pick" to "fixup"/"squash".
+ *
+ * Note that if the config has specified a custom instruction format, each log
+ * message will have to be retrieved from the commit (as the oneline in the
+ * script cannot be trusted) in order to normalize the autosquash arrangement.
+ */
+int rearrange_squash(void)
+{
+       const char *todo_file = rebase_path_todo();
+       struct todo_list todo_list = TODO_LIST_INIT;
+       struct hashmap subject2item;
+       int res = 0, rearranged = 0, *next, *tail, fd, i;
+       char **subjects;
+
+       fd = open(todo_file, O_RDONLY);
+       if (fd < 0)
+               return error_errno(_("could not open '%s'"), todo_file);
+       if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
+               close(fd);
+               return error(_("could not read '%s'."), todo_file);
+       }
+       close(fd);
+       if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
+               todo_list_release(&todo_list);
+               return -1;
+       }
+
+       /*
+        * The hashmap maps onelines to the respective todo list index.
+        *
+        * If any items need to be rearranged, the next[i] value will indicate
+        * which item was moved directly after the i'th.
+        *
+        * In that case, last[i] will indicate the index of the latest item to
+        * be moved to appear after the i'th.
+        */
+       hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp,
+                    NULL, todo_list.nr);
+       ALLOC_ARRAY(next, todo_list.nr);
+       ALLOC_ARRAY(tail, todo_list.nr);
+       ALLOC_ARRAY(subjects, todo_list.nr);
+       for (i = 0; i < todo_list.nr; i++) {
+               struct strbuf buf = STRBUF_INIT;
+               struct todo_item *item = todo_list.items + i;
+               const char *commit_buffer, *subject, *p;
+               size_t subject_len;
+               int i2 = -1;
+               struct subject2item_entry *entry;
+
+               next[i] = tail[i] = -1;
+               if (item->command >= TODO_EXEC) {
+                       subjects[i] = NULL;
+                       continue;
+               }
+
+               if (is_fixup(item->command)) {
+                       todo_list_release(&todo_list);
+                       return error(_("the script was already rearranged."));
+               }
+
+               item->commit->util = item;
+
+               parse_commit(item->commit);
+               commit_buffer = get_commit_buffer(item->commit, NULL);
+               find_commit_subject(commit_buffer, &subject);
+               format_subject(&buf, subject, " ");
+               subject = subjects[i] = strbuf_detach(&buf, &subject_len);
+               unuse_commit_buffer(item->commit, commit_buffer);
+               if ((skip_prefix(subject, "fixup! ", &p) ||
+                    skip_prefix(subject, "squash! ", &p))) {
+                       struct commit *commit2;
+
+                       for (;;) {
+                               while (isspace(*p))
+                                       p++;
+                               if (!skip_prefix(p, "fixup! ", &p) &&
+                                   !skip_prefix(p, "squash! ", &p))
+                                       break;
+                       }
+
+                       if ((entry = hashmap_get_from_hash(&subject2item,
+                                                          strhash(p), p)))
+                               /* found by title */
+                               i2 = entry->i;
+                       else if (!strchr(p, ' ') &&
+                                (commit2 =
+                                 lookup_commit_reference_by_name(p)) &&
+                                commit2->util)
+                               /* found by commit name */
+                               i2 = (struct todo_item *)commit2->util
+                                       - todo_list.items;
+                       else {
+                               /* copy can be a prefix of the commit subject */
+                               for (i2 = 0; i2 < i; i2++)
+                                       if (subjects[i2] &&
+                                           starts_with(subjects[i2], p))
+                                               break;
+                               if (i2 == i)
+                                       i2 = -1;
+                       }
+               }
+               if (i2 >= 0) {
+                       rearranged = 1;
+                       todo_list.items[i].command =
+                               starts_with(subject, "fixup!") ?
+                               TODO_FIXUP : TODO_SQUASH;
+                       if (next[i2] < 0)
+                               next[i2] = i;
+                       else
+                               next[tail[i2]] = i;
+                       tail[i2] = i;
+               } else if (!hashmap_get_from_hash(&subject2item,
+                                               strhash(subject), subject)) {
+                       FLEX_ALLOC_MEM(entry, subject, subject, subject_len);
+                       entry->i = i;
+                       hashmap_entry_init(entry, strhash(entry->subject));
+                       hashmap_put(&subject2item, entry);
+               }
+       }
+
+       if (rearranged) {
+               struct strbuf buf = STRBUF_INIT;
+
+               for (i = 0; i < todo_list.nr; i++) {
+                       enum todo_command command = todo_list.items[i].command;
+                       int cur = i;
+
+                       /*
+                        * Initially, all commands are 'pick's. If it is a
+                        * fixup or a squash now, we have rearranged it.
+                        */
+                       if (is_fixup(command))
+                               continue;
+
+                       while (cur >= 0) {
+                               int offset = todo_list.items[cur].offset_in_buf;
+                               int end_offset = cur + 1 < todo_list.nr ?
+                                       todo_list.items[cur + 1].offset_in_buf :
+                                       todo_list.buf.len;
+                               char *bol = todo_list.buf.buf + offset;
+                               char *eol = todo_list.buf.buf + end_offset;
+
+                               /* replace 'pick', by 'fixup' or 'squash' */
+                               command = todo_list.items[cur].command;
+                               if (is_fixup(command)) {
+                                       strbuf_addstr(&buf,
+                                               todo_command_info[command].str);
+                                       bol += strcspn(bol, " \t");
+                               }
+
+                               strbuf_add(&buf, bol, eol - bol);
+
+                               cur = next[cur];
+                       }
+               }
+
+               fd = open(todo_file, O_WRONLY);
+               if (fd < 0)
+                       res = error_errno(_("could not open '%s'"), todo_file);
+               else if (write(fd, buf.buf, buf.len) < 0)
+                       res = error_errno(_("could not read '%s'."), todo_file);
+               else if (ftruncate(fd, buf.len) < 0)
+                       res = error_errno(_("could not finish '%s'"),
+                                          todo_file);
+               close(fd);
+               strbuf_release(&buf);
+       }
+
+       free(next);
+       free(tail);
+       for (i = 0; i < todo_list.nr; i++)
+               free(subjects[i]);
+       free(subjects);
+       hashmap_free(&subject2item, 1);
+       todo_list_release(&todo_list);
+
+       return res;
+}
index f885b68395f4bff1ded96c0ab84ed87d164f0c7d..6f3d3df82c0ade64b7b125acd49bf3f5e15c53af 100644 (file)
@@ -45,6 +45,14 @@ int sequencer_continue(struct replay_opts *opts);
 int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
+int sequencer_make_script(int keep_empty, FILE *out,
+               int argc, const char **argv);
+
+int transform_todo_ids(int shorten_ids);
+int check_todo_list(void);
+int skip_unnecessary_picks(void);
+int rearrange_squash(void);
+
 extern const char sign_off_header[];
 
 void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);
diff --git a/setup.c b/setup.c
index 6d8380acd2b66ee7d8206639d4b03933afb1816e..d777ff34b7daf50fb24a8653a7e26e3e95b8c0c7 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -541,7 +541,8 @@ void read_gitfile_error_die(int error_code, const char *path, const char *dir)
 
 /*
  * Try to read the location of the git directory from the .git file,
- * return path to git directory if found.
+ * return path to git directory if found. The return value comes from
+ * a shared buffer.
  *
  * On failure, if return_error_code is not NULL, return_error_code
  * will be set to an error code and NULL will be returned. If
index 5a2014811fd0a42aa74b36bf3a470e471968be6f..09ad64ce555e0b908ce0d9f22219026a2d37ae0c 100644 (file)
@@ -1748,10 +1748,15 @@ static int index_core(unsigned char *sha1, int fd, size_t size,
                ret = index_mem(sha1, "", size, type, path, flags);
        } else if (size <= SMALL_FILE_SIZE) {
                char *buf = xmalloc(size);
-               if (size == read_in_full(fd, buf, size))
-                       ret = index_mem(sha1, buf, size, type, path, flags);
+               ssize_t read_result = read_in_full(fd, buf, size);
+               if (read_result < 0)
+                       ret = error_errno("read error while indexing %s",
+                                         path ? path : "<unknown>");
+               else if (read_result != size)
+                       ret = error("short read while indexing %s",
+                                   path ? path : "<unknown>");
                else
-                       ret = error_errno("short read");
+                       ret = index_mem(sha1, buf, size, type, path, flags);
                free(buf);
        } else {
                void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
index 79ae567cbc3bfbd7574b8156aa9fcf56581cad83..ff8f6094a3300d7bbdbb1a1ed58c8037daff5c7a 100644 (file)
@@ -1,6 +1,69 @@
 #ifndef STRING_LIST_H
 #define STRING_LIST_H
 
+/**
+ * The string_list API offers a data structure and functions to handle
+ * sorted and unsorted arrays of strings.  A "sorted" list is one whose
+ * entries are sorted by string value in `strcmp()` order.
+ *
+ * The caller:
+ *
+ * . Allocates and clears a `struct string_list` variable.
+ *
+ * . Initializes the members. You might want to set the flag `strdup_strings`
+ *   if the strings should be strdup()ed. For example, this is necessary
+ *   when you add something like git_path("..."), since that function returns
+ *   a static buffer that will change with the next call to git_path().
+ *
+ * If you need something advanced, you can manually malloc() the `items`
+ * member (you need this if you add things later) and you should set the
+ * `nr` and `alloc` members in that case, too.
+ *
+ * . Adds new items to the list, using `string_list_append`,
+ *   `string_list_append_nodup`, `string_list_insert`,
+ *   `string_list_split`, and/or `string_list_split_in_place`.
+ *
+ * . Can check if a string is in the list using `string_list_has_string` or
+ *   `unsorted_string_list_has_string` and get it from the list using
+ *   `string_list_lookup` for sorted lists.
+ *
+ * . Can sort an unsorted list using `string_list_sort`.
+ *
+ * . Can remove duplicate items from a sorted list using
+ *   `string_list_remove_duplicates`.
+ *
+ * . Can remove individual items of an unsorted list using
+ *   `unsorted_string_list_delete_item`.
+ *
+ * . Can remove items not matching a criterion from a sorted or unsorted
+ *   list using `filter_string_list`, or remove empty strings using
+ *   `string_list_remove_empty_items`.
+ *
+ * . Finally it should free the list using `string_list_clear`.
+ *
+ * Example:
+ *
+ *     struct string_list list = STRING_LIST_INIT_NODUP;
+ *     int i;
+ *
+ *     string_list_append(&list, "foo");
+ *     string_list_append(&list, "bar");
+ *     for (i = 0; i < list.nr; i++)
+ *             printf("%s\n", list.items[i].string)
+ *
+ * NOTE: It is more efficient to build an unsorted list and sort it
+ * afterwards, instead of building a sorted list (`O(n log n)` instead of
+ * `O(n^2)`).
+ *
+ * However, if you use the list to check if a certain string was added
+ * already, you should not do that (using unsorted_string_list_has_string()),
+ * because the complexity would be quadratic again (but with a worse factor).
+ */
+
+/**
+ * Represents an item of the list. The `string` member is a pointer to the
+ * string, and you may use the `util` member for any purpose, if you want.
+ */
 struct string_list_item {
        char *string;
        void *util;
@@ -8,6 +71,18 @@ struct string_list_item {
 
 typedef int (*compare_strings_fn)(const char *, const char *);
 
+/**
+ * Represents the list itself.
+ *
+ * . The array of items are available via the `items` member.
+ * . The `nr` member contains the number of items stored in the list.
+ * . The `alloc` member is used to avoid reallocating at every insertion.
+ *   You should not tamper with it.
+ * . Setting the `strdup_strings` member to 1 will strdup() the strings
+ *   before adding them, see above.
+ * . The `compare_strings_fn` member is used to specify a custom compare
+ *   function, otherwise `strcmp()` is used as the default function.
+ */
 struct string_list {
        struct string_list_item *items;
        unsigned int nr, alloc;
@@ -18,35 +93,65 @@ struct string_list {
 #define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
 #define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1, NULL }
 
+/* General functions which work with both sorted and unsorted lists. */
+
+/**
+ * Initialize the members of the string_list, set `strdup_strings`
+ * member according to the value of the second parameter.
+ */
 void string_list_init(struct string_list *list, int strdup_strings);
 
+/** Callback function type for for_each_string_list */
+typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
+
+/**
+ * Apply `want` to each item in `list`, retaining only the ones for which
+ * the function returns true.  If `free_util` is true, call free() on
+ * the util members of any items that have to be deleted.  Preserve
+ * the order of the items that are retained.
+ */
+void filter_string_list(struct string_list *list, int free_util,
+                       string_list_each_func_t want, void *cb_data);
+
+/**
+ * Dump a string_list to stdout, useful mainly for debugging
+ * purposes. It can take an optional header argument and it writes out
+ * the string-pointer pairs of the string_list, each one in its own
+ * line.
+ */
 void print_string_list(const struct string_list *p, const char *text);
+
+/**
+ * Free a string_list. The `string` pointer of the items will be freed
+ * in case the `strdup_strings` member of the string_list is set. The
+ * second parameter controls if the `util` pointer of the items should
+ * be freed or not.
+ */
 void string_list_clear(struct string_list *list, int free_util);
 
-/* Use this function to call a custom clear function on each util pointer */
-/* The string associated with the util pointer is passed as the second argument */
+/**
+ * Callback type for `string_list_clear_func`.  The string associated
+ * with the util pointer is passed as the second argument
+ */
 typedef void (*string_list_clear_func_t)(void *p, const char *str);
+
+/** Call a custom clear function on each util pointer */
 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
 
-/* Use this function or the macro below to iterate over each item */
-typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
+/**
+ * Apply `func` to each item. If `func` returns nonzero, the
+ * iteration aborts and the return value is propagated.
+ */
 int for_each_string_list(struct string_list *list,
-                        string_list_each_func_t, void *cb_data);
+                        string_list_each_func_t func, void *cb_data);
+
+/** Iterate over each item, as a macro. */
 #define for_each_string_list_item(item,list)            \
        for (item = (list)->items;                      \
             item && item < (list)->items + (list)->nr; \
             ++item)
 
-/*
- * Apply want to each item in list, retaining only the ones for which
- * the function returns true.  If free_util is true, call free() on
- * the util members of any items that have to be deleted.  Preserve
- * the order of the items that are retained.
- */
-void filter_string_list(struct string_list *list, int free_util,
-                       string_list_each_func_t want, void *cb_data);
-
-/*
+/**
  * Remove any empty strings from the list.  If free_util is true, call
  * free() on the util members of any items that have to be deleted.
  * Preserve the order of the items that are retained.
@@ -54,25 +159,34 @@ void filter_string_list(struct string_list *list, int free_util,
 void string_list_remove_empty_items(struct string_list *list, int free_util);
 
 /* Use these functions only on sorted lists: */
+
+/** Determine if the string_list has a given string or not. */
 int string_list_has_string(const struct string_list *list, const char *string);
 int string_list_find_insert_index(const struct string_list *list, const char *string,
                                  int negative_existing_index);
-/*
- * Inserts the given string into the sorted list.
- * If the string already exists, the list is not altered.
- * Returns the string_list_item, the string is part of.
+
+/**
+ * Insert a new element to the string_list. The returned pointer can
+ * be handy if you want to write something to the `util` pointer of
+ * the string_list_item containing the just added string. If the given
+ * string already exists the insertion will be skipped and the pointer
+ * to the existing item returned.
+ *
+ * Since this function uses xrealloc() (which die()s if it fails) if the
+ * list needs to grow, it is safe not to check the pointer. I.e. you may
+ * write `string_list_insert(...)->util = ...;`.
  */
 struct string_list_item *string_list_insert(struct string_list *list, const char *string);
 
-/*
- * Removes the given string from the sorted list.
- * If the string doesn't exist, the list is not altered.
+/**
+ * Remove the given string from the sorted list.  If the string
+ * doesn't exist, the list is not altered.
  */
 extern void string_list_remove(struct string_list *list, const char *string,
                               int free_util);
 
-/*
- * Checks if the given string is part of a sorted list. If it is part of the list,
+/**
+ * Check if the given string is part of a sorted list. If it is part of the list,
  * return the coresponding string_list_item, NULL otherwise.
  */
 struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
@@ -87,14 +201,14 @@ void string_list_remove_duplicates(struct string_list *sorted_list, int free_uti
 
 /* Use these functions only on unsorted lists: */
 
-/*
+/**
  * Add string to the end of list.  If list->strdup_string is set, then
  * string is copied; otherwise the new string_list_entry refers to the
  * input string.
  */
 struct string_list_item *string_list_append(struct string_list *list, const char *string);
 
-/*
+/**
  * Like string_list_append(), except string is never copied.  When
  * list->strdup_strings is set, this function can be used to hand
  * ownership of a malloc()ed string to list without making an extra
@@ -102,16 +216,34 @@ struct string_list_item *string_list_append(struct string_list *list, const char
  */
 struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
 
+/**
+ * Sort the list's entries by string value in `strcmp()` order.
+ */
 void string_list_sort(struct string_list *list);
+
+/**
+ * Like `string_list_has_string()` but for unsorted lists. Linear in
+ * size of the list.
+ */
 int unsorted_string_list_has_string(struct string_list *list, const char *string);
+
+/**
+ * Like `string_list_lookup()` but for unsorted lists. Linear in size
+ * of the list.
+ */
 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
                                                     const char *string);
-
+/**
+ * Remove an item from a string_list. The `string` pointer of the
+ * items will be freed in case the `strdup_strings` member of the
+ * string_list is set. The third parameter controls if the `util`
+ * pointer of the items should be freed or not.
+ */
 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
 
-/*
- * Split string into substrings on character delim and append the
- * substrings to list.  The input string is not modified.
+/**
+ * Split string into substrings on character `delim` and append the
+ * substrings to `list`.  The input string is not modified.
  * list->strdup_strings must be set, as new memory needs to be
  * allocated to hold the substrings.  If maxsplit is non-negative,
  * then split at most maxsplit times.  Return the number of substrings
index f2f30bb488c05d9002d755feed65e9ca781a440c..63e7094e1620c2c3eb7dda0a8c38a833ae2d1c5a 100644 (file)
@@ -503,7 +503,7 @@ static void show_submodule_header(struct diff_options *o, const char *path,
 
        if (add_submodule_odb(path)) {
                if (!message)
-                       message = "(not initialized)";
+                       message = "(commits not present)";
                goto output_header;
        }
 
@@ -1997,6 +1997,10 @@ const char *get_superproject_working_tree(void)
        return ret;
 }
 
+/*
+ * Put the gitdir for a submodule (given relative to the main
+ * repository worktree) into `buf`, or return -1 on error.
+ */
 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
 {
        const struct submodule *sub;
index 75fe883aac1ee17b9dda40dd88b7a4a4fd328318..630c76d1275485d2d460896407e645116c997916 100644 (file)
@@ -99,6 +99,8 @@ int cmd_main(int argc, const char **argv)
        const char *prefix = "prefix/";
        const char *usage[] = {
                "test-parse-options <options>",
+               "",
+               "A helper function for the parse-options API.",
                NULL
        };
        struct string_list expect = STRING_LIST_INIT_NODUP;
index 74d2cd76fe56bf6d6fab5d44834999b55d3220de..0c2fc81d7b0fa401db58c41cd2fed4469e80b058 100755 (executable)
@@ -10,6 +10,8 @@ test_description='our own option parser'
 cat >expect <<\EOF
 usage: test-parse-options <options>
 
+    A helper function for the parse-options API.
+
     --yes                 get a boolean
     -D, --no-doubt        begins with 'no-'
     -B, --no-fear         be brave
index 310f93fd30ea51f94eaa2b4dd2e8d0e398783cba..a859abedf5820da7fd2a2457c6d19b213ff78784 100755 (executable)
@@ -28,6 +28,9 @@ test_expect_success 'setup optionspec' '
 |g,fluf?path     short and long option optional argument
 |longest=very-long-argument-hint  a very long argument hint
 |pair=key=value  with an equals sign in the hint
+|aswitch help te=t contains? fl*g characters!`
+|bswitch=hint   hint has trailing tab character
+|cswitch        switch has trailing tab character
 |short-hint=a    with a one symbol hint
 |
 |Extras
@@ -35,6 +38,25 @@ test_expect_success 'setup optionspec' '
 EOF
 '
 
+test_expect_success 'setup optionspec-no-switches' '
+       sed -e "s/^|//" >optionspec_no_switches <<\EOF
+|some-command [options] <args>...
+|
+|some-command does foo and bar!
+|--
+EOF
+'
+
+test_expect_success 'setup optionspec-only-hidden-switches' '
+       sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF
+|some-command [options] <args>...
+|
+|some-command does foo and bar!
+|--
+|hidden1* A hidden switch
+EOF
+'
+
 test_expect_success 'test --parseopt help output' '
        sed -e "s/^|//" >expect <<\END_EXPECT &&
 |cat <<\EOF
@@ -62,6 +84,9 @@ test_expect_success 'test --parseopt help output' '
 |    --longest <very-long-argument-hint>
 |                          a very long argument hint
 |    --pair <key=value>    with an equals sign in the hint
+|    --aswitch             help te=t contains? fl*g characters!`
+|    --bswitch <hint>      hint has trailing tab character
+|    --cswitch             switch has trailing tab character
 |    --short-hint <a>      with a one symbol hint
 |
 |Extras
@@ -73,19 +98,100 @@ END_EXPECT
        test_i18ncmp expect output
 '
 
+test_expect_success 'test --parseopt help output no switches' '
+       sed -e "s/^|//" >expect <<\END_EXPECT &&
+|cat <<\EOF
+|usage: some-command [options] <args>...
+|
+|    some-command does foo and bar!
+|
+|EOF
+END_EXPECT
+       test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches &&
+       test_i18ncmp expect output
+'
+
+test_expect_success 'test --parseopt help output hidden switches' '
+       sed -e "s/^|//" >expect <<\END_EXPECT &&
+|cat <<\EOF
+|usage: some-command [options] <args>...
+|
+|    some-command does foo and bar!
+|
+|EOF
+END_EXPECT
+       test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches &&
+       test_i18ncmp expect output
+'
+
+test_expect_success 'test --parseopt help-all output hidden switches' '
+       sed -e "s/^|//" >expect <<\END_EXPECT &&
+|cat <<\EOF
+|usage: some-command [options] <args>...
+|
+|    some-command does foo and bar!
+|
+|    --hidden1             A hidden switch
+|
+|EOF
+END_EXPECT
+       test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches &&
+       test_i18ncmp expect output
+'
+
+test_expect_success 'test --parseopt invalid switch help output' '
+       sed -e "s/^|//" >expect <<\END_EXPECT &&
+|error: unknown option `does-not-exist'\''
+|usage: some-command [options] <args>...
+|
+|    some-command does foo and bar!
+|
+|    -h, --help            show the help
+|    --foo                 some nifty option --foo
+|    --bar ...             some cool option --bar with an argument
+|    -b, --baz             a short and long option
+|
+|An option group Header
+|    -C[...]               option C with an optional argument
+|    -d, --data[=...]      short and long option with an optional argument
+|
+|Argument hints
+|    -B <arg>              short option required argument
+|    --bar2 <arg>          long option required argument
+|    -e, --fuz <with-space>
+|                          short and long option required argument
+|    -s[<some>]            short option optional argument
+|    --long[=<data>]       long option optional argument
+|    -g, --fluf[=<path>]   short and long option optional argument
+|    --longest <very-long-argument-hint>
+|                          a very long argument hint
+|    --pair <key=value>    with an equals sign in the hint
+|    --aswitch             help te=t contains? fl*g characters!`
+|    --bswitch <hint>      hint has trailing tab character
+|    --cswitch             switch has trailing tab character
+|    --short-hint <a>      with a one symbol hint
+|
+|Extras
+|    --extra1              line above used to cause a segfault but no longer does
+|
+END_EXPECT
+       test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
+       test_i18ncmp expect output
+'
+
 test_expect_success 'setup expect.1' "
        cat > expect <<EOF
-set -- --foo --bar 'ham' -b -- 'arg'
+set -- --foo --bar 'ham' -b --aswitch -- 'arg'
 EOF
 "
 
 test_expect_success 'test --parseopt' '
-       git rev-parse --parseopt -- --foo --bar=ham --baz arg < optionspec > output &&
+       git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
        test_cmp expect output
 '
 
 test_expect_success 'test --parseopt with mixed options and arguments' '
-       git rev-parse --parseopt -- --foo arg --bar=ham --baz < optionspec > output &&
+       git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
        test_cmp expect output
 '
 
index d971649979fa2661923d9df797125cd4d997cf97..3ac7ebf85f3198cde56f784456e0024205aec853 100755 (executable)
@@ -381,6 +381,262 @@ test_expect_success 'config information was renamed, too' '
        test_must_fail git config branch.s/s.dummy
 '
 
+test_expect_success 'git branch -m correctly renames multiple config sections' '
+       test_when_finished "git checkout master" &&
+       git checkout -b source master &&
+
+       # Assert that a config file with multiple config sections has
+       # those sections preserved...
+       cat >expect <<-\EOF &&
+       branch.dest.key1=value1
+       some.gar.b=age
+       branch.dest.key2=value2
+       EOF
+       cat >config.branch <<\EOF &&
+;; Note the lack of -\EOF above & mixed indenting here. This is
+;; intentional, we are also testing that the formatting of copied
+;; sections is preserved.
+
+;; Comment for source. Tabs
+[branch "source"]
+       ;; Comment for the source value
+       key1 = value1
+;; Comment for some.gar. Spaces
+[some "gar"]
+    ;; Comment for the some.gar value
+    b = age
+;; Comment for source, again. Mixed tabs/spaces.
+[branch "source"]
+    ;; Comment for the source value, again
+       key2 = value2
+EOF
+       cat config.branch >>.git/config &&
+       git branch -m source dest &&
+       git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
+       test_cmp expect actual &&
+
+       # ...and that the comments for those sections are also
+       # preserved.
+       cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
+       sed -n -e "/Note the lack/,\$p" .git/config >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'git branch -c dumps usage' '
+       test_expect_code 128 git branch -c 2>err &&
+       test_i18ngrep "branch name required" err
+'
+
+test_expect_success 'git branch --copy dumps usage' '
+       test_expect_code 128 git branch --copy 2>err &&
+       test_i18ngrep "branch name required" err
+'
+
+test_expect_success 'git branch -c d e should work' '
+       git branch -l d &&
+       git reflog exists refs/heads/d &&
+       git config branch.d.dummy Hello &&
+       git branch -c d e &&
+       git reflog exists refs/heads/d &&
+       git reflog exists refs/heads/e &&
+       echo Hello >expect &&
+       git config branch.e.dummy >actual &&
+       test_cmp expect actual &&
+       echo Hello >expect &&
+       git config branch.d.dummy >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'git branch --copy is a synonym for -c' '
+       git branch -l copy &&
+       git reflog exists refs/heads/copy &&
+       git config branch.copy.dummy Hello &&
+       git branch --copy copy copy-to &&
+       git reflog exists refs/heads/copy &&
+       git reflog exists refs/heads/copy-to &&
+       echo Hello >expect &&
+       git config branch.copy.dummy >actual &&
+       test_cmp expect actual &&
+       echo Hello >expect &&
+       git config branch.copy-to.dummy >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
+       git checkout -b ee &&
+       git reflog exists refs/heads/ee &&
+       git config branch.ee.dummy Hello &&
+       git branch -c ee ef &&
+       git reflog exists refs/heads/ee &&
+       git reflog exists refs/heads/ef &&
+       test $(git config branch.ee.dummy) = Hello &&
+       test $(git config branch.ef.dummy) = Hello &&
+       test $(git rev-parse --abbrev-ref HEAD) = ee
+'
+
+test_expect_success 'git branch -c f/f g/g should work' '
+       git branch -l f/f &&
+       git reflog exists refs/heads/f/f &&
+       git config branch.f/f.dummy Hello &&
+       git branch -c f/f g/g &&
+       git reflog exists refs/heads/f/f &&
+       git reflog exists refs/heads/g/g &&
+       test $(git config branch.f/f.dummy) = Hello &&
+       test $(git config branch.g/g.dummy) = Hello
+'
+
+test_expect_success 'git branch -c m2 m2 should work' '
+       git branch -l m2 &&
+       git reflog exists refs/heads/m2 &&
+       git config branch.m2.dummy Hello &&
+       git branch -c m2 m2 &&
+       git reflog exists refs/heads/m2 &&
+       test $(git config branch.m2.dummy) = Hello
+'
+
+test_expect_success 'git branch -c zz zz/zz should fail' '
+       git branch -l zz &&
+       git reflog exists refs/heads/zz &&
+       test_must_fail git branch -c zz zz/zz
+'
+
+test_expect_success 'git branch -c b/b b should fail' '
+       git branch -l b/b &&
+       test_must_fail git branch -c b/b b
+'
+
+test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
+       git branch -l o/q &&
+       git reflog exists refs/heads/o/q &&
+       git reflog exists refs/heads/o/p &&
+       git branch -C o/q o/p
+'
+
+test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
+       git reflog exists refs/heads/o/q &&
+       git reflog exists refs/heads/o/p &&
+       git branch -c -f o/q o/p
+'
+
+test_expect_success 'git branch -c qq rr/qq should fail when r exists' '
+       git branch qq &&
+       git branch rr &&
+       test_must_fail git branch -c qq rr/qq
+'
+
+test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
+       git branch b1 &&
+       git checkout -b b2 &&
+       test_must_fail git branch -C b1 b2
+'
+
+test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
+       git checkout -b c1 &&
+       git branch c2 &&
+       git branch -C c1 c2 &&
+       test $(git rev-parse --abbrev-ref HEAD) = c1
+'
+
+test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
+       msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
+       ! grep "$msg$" .git/logs/HEAD
+'
+
+test_expect_success 'git branch -C master should work when master is checked out' '
+       git checkout master &&
+       git branch -C master
+'
+
+test_expect_success 'git branch -C master master should work when master is checked out' '
+       git checkout master &&
+       git branch -C master master
+'
+
+test_expect_success 'git branch -C master5 master5 should work when master is checked out' '
+       git checkout master &&
+       git branch master5 &&
+       git branch -C master5 master5
+'
+
+test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
+       git branch -l cd &&
+       git reflog exists refs/heads/cd &&
+       git config branch.cd.dummy CD &&
+       git branch -l ab &&
+       git reflog exists refs/heads/ab &&
+       git config branch.ab.dummy AB &&
+       git branch -C ab cd &&
+       git reflog exists refs/heads/ab &&
+       git reflog exists refs/heads/cd &&
+       test $(git config branch.ab.dummy) = AB &&
+       test $(git config branch.cd.dummy) = AB
+'
+
+test_expect_success 'git branch -c correctly copies multiple config sections' '
+       FOO=1 &&
+       export FOO &&
+       test_when_finished "git checkout master" &&
+       git checkout -b source2 master &&
+
+       # Assert that a config file with multiple config sections has
+       # those sections preserved...
+       cat >expect <<-\EOF &&
+       branch.source2.key1=value1
+       branch.dest2.key1=value1
+       more.gar.b=age
+       branch.source2.key2=value2
+       branch.dest2.key2=value2
+       EOF
+       cat >config.branch <<\EOF &&
+;; Note the lack of -\EOF above & mixed indenting here. This is
+;; intentional, we are also testing that the formatting of copied
+;; sections is preserved.
+
+;; Comment for source2. Tabs
+[branch "source2"]
+       ;; Comment for the source2 value
+       key1 = value1
+;; Comment for more.gar. Spaces
+[more "gar"]
+    ;; Comment for the more.gar value
+    b = age
+;; Comment for source2, again. Mixed tabs/spaces.
+[branch "source2"]
+    ;; Comment for the source2 value, again
+       key2 = value2
+EOF
+       cat config.branch >>.git/config &&
+       git branch -c source2 dest2 &&
+       git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
+       test_cmp expect actual &&
+
+       # ...and that the comments and formatting for those sections
+       # is also preserved.
+       cat >expect <<\EOF &&
+;; Comment for source2. Tabs
+[branch "source2"]
+       ;; Comment for the source2 value
+       key1 = value1
+;; Comment for more.gar. Spaces
+[branch "dest2"]
+       ;; Comment for the source2 value
+       key1 = value1
+;; Comment for more.gar. Spaces
+[more "gar"]
+    ;; Comment for the more.gar value
+    b = age
+;; Comment for source2, again. Mixed tabs/spaces.
+[branch "source2"]
+    ;; Comment for the source2 value, again
+       key2 = value2
+[branch "dest2"]
+    ;; Comment for the source2 value, again
+       key2 = value2
+EOF
+       sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'deleting a symref' '
        git branch target &&
        git symbolic-ref refs/heads/symref refs/heads/target &&
index 37821d245433f757fa13f0a3e27da0312bebb7db..3704dbb2ecf6046e09adb03b451e4e531509b2f4 100755 (executable)
@@ -1249,20 +1249,13 @@ test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
        test B = $(git cat-file commit HEAD^ | sed -ne \$p)
 '
 
-cat >expect <<EOF
-Warning: the command isn't recognized in the following line:
- - badcmd $(git rev-list --oneline -1 master~1)
-
-You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
-Or you can abort the rebase with 'git rebase --abort'.
-EOF
-
 test_expect_success 'static check of bad command' '
        rebase_setup_and_clean bad-cmd &&
        set_fake_editor &&
        test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
                git rebase -i --root 2>actual &&
-       test_i18ncmp expect actual &&
+       test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
+       test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
        FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
        git rebase --continue &&
        test E = $(git cat-file commit HEAD | sed -ne \$p) &&
@@ -1284,20 +1277,13 @@ test_expect_success 'tabs and spaces are accepted in the todolist' '
        test E = $(git cat-file commit HEAD | sed -ne \$p)
 '
 
-cat >expect <<EOF
-Warning: the SHA-1 is missing or isn't a commit in the following line:
- - edit XXXXXXX False commit
-
-You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
-Or you can abort the rebase with 'git rebase --abort'.
-EOF
-
 test_expect_success 'static check of bad SHA-1' '
        rebase_setup_and_clean bad-sha &&
        set_fake_editor &&
        test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
                git rebase -i --root 2>actual &&
-       test_i18ncmp expect actual &&
+       test_i18ngrep "edit XXXXXXX False commit" actual &&
+       test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
        FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
        git rebase --continue &&
        test E = $(git cat-file commit HEAD | sed -ne \$p)
index 5848949ec3700c2188d581500c19eaffa1392c3d..e364c12622f8566c3c99370de25ec5659a1e1546 100755 (executable)
@@ -271,6 +271,18 @@ test_expect_success C_LOCALE_OUTPUT 'autosquash with custom inst format' '
        test 2 = $(git cat-file commit HEAD^ | grep squash | wc -l)
 '
 
+test_expect_success 'autosquash with empty custom instructionFormat' '
+       git reset --hard base &&
+       test_commit empty-instructionFormat-test &&
+       (
+               set_cat_todo_editor &&
+               test_must_fail git -c rebase.instructionFormat= \
+                       rebase --autosquash  --force -i HEAD^ >actual &&
+               git log -1 --format="pick %h %s" >expect &&
+               test_cmp expect actual
+       )
+'
+
 set_backup_editor () {
        write_script backup-editor.sh <<-\EOF
        cp "$1" .git/backup-"$(basename "$1")"
@@ -278,7 +290,7 @@ set_backup_editor () {
        test_set_editor "$PWD/backup-editor.sh"
 }
 
-test_expect_failure 'autosquash with multiple empty patches' '
+test_expect_success 'autosquash with multiple empty patches' '
        test_tick &&
        git commit --allow-empty -m "empty" &&
        test_tick &&
@@ -304,4 +316,18 @@ test_expect_success 'extra spaces after fixup!' '
        test $base = $parent
 '
 
+test_expect_success 'wrapped original subject' '
+       if test -d .git/rebase-merge; then git rebase --abort; fi &&
+       base=$(git rev-parse HEAD) &&
+       echo "wrapped subject" >wrapped &&
+       git add wrapped &&
+       test_tick &&
+       git commit --allow-empty -m "$(printf "To\nfixup")" &&
+       test_tick &&
+       git commit --allow-empty -m "fixup! To fixup" &&
+       git rebase -i --autosquash --keep-empty HEAD~2 &&
+       parent=$(git rev-parse HEAD^) &&
+       test $base = $parent
+'
+
 test_done
index d09acfe48e81b2567fd7408d7cb6345c73d99754..c515e3e53feef29fd6c7434e13d52b3ea913e167 100755 (executable)
@@ -90,6 +90,14 @@ test_expect_success setup '
        git commit -m "Rearranged lines in dir/sub" &&
        git checkout master &&
 
+       GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
+       GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
+       export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+       git checkout -b mode initial &&
+       git update-index --chmod=+x file0 &&
+       git commit -m "update mode" &&
+       git checkout -f master &&
+
        git config diff.renames false &&
 
        git show-branch
@@ -192,6 +200,10 @@ diff-tree --pretty side
 diff-tree --pretty -p side
 diff-tree --pretty --patch-with-stat side
 
+diff-tree initial mode
+diff-tree --stat initial mode
+diff-tree --summary initial mode
+
 diff-tree master
 diff-tree -p master
 diff-tree -p -m master
diff --git a/t/t4013/diff.diff-tree_--stat_initial_mode b/t/t4013/diff.diff-tree_--stat_initial_mode
new file mode 100644 (file)
index 0000000..0e5943c
--- /dev/null
@@ -0,0 +1,4 @@
+$ git diff-tree --stat initial mode
+ file0 | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+$
diff --git a/t/t4013/diff.diff-tree_--summary_initial_mode b/t/t4013/diff.diff-tree_--summary_initial_mode
new file mode 100644 (file)
index 0000000..25846b6
--- /dev/null
@@ -0,0 +1,3 @@
+$ git diff-tree --summary initial mode
+ mode change 100644 => 100755 file0
+$
diff --git a/t/t4013/diff.diff-tree_initial_mode b/t/t4013/diff.diff-tree_initial_mode
new file mode 100644 (file)
index 0000000..c47c094
--- /dev/null
@@ -0,0 +1,3 @@
+$ git diff-tree initial mode
+:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M     file0
+$
index b345b2ebfa53e51cb0ab32635a9123abdf55f2b1..2afe91f1167b14329108d438fa9cf9ed7963acad 100644 (file)
@@ -1,4 +1,10 @@
 $ git log --decorate=full --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (refs/heads/mode)
+Author: A U Thor <author@example.com>
+Date:   Mon Jun 26 00:06:00 2006 +0000
+
+    update mode
+
 commit cd4e72fd96faed3f0ba949dc42967430374e2290 (refs/heads/rearrange)
 Author: A U Thor <author@example.com>
 Date:   Mon Jun 26 00:06:00 2006 +0000
index 3aa16a9e423bae96815718d0aef235060659a7c1..d0f308ab2bb73048cf153e209d7046e47682a524 100644 (file)
@@ -1,4 +1,10 @@
 $ git log --decorate --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (mode)
+Author: A U Thor <author@example.com>
+Date:   Mon Jun 26 00:06:00 2006 +0000
+
+    update mode
+
 commit cd4e72fd96faed3f0ba949dc42967430374e2290 (rearrange)
 Author: A U Thor <author@example.com>
 Date:   Mon Jun 26 00:06:00 2006 +0000
index cd70fd5192ea88b9ccba4680f578d8c01be64945..49bca7b48d910fa1128435f121c849e45a7eefa6 100755 (executable)
@@ -95,7 +95,7 @@ test_expect_success 'submodule not initialized in new clone' '
        git clone . sm3 &&
        git -C sm3 diff-tree -p --no-commit-id --submodule=log HEAD >actual &&
        cat >expected <<-EOF &&
-       Submodule sm1 $smhead1...$smhead2 (not initialized)
+       Submodule sm1 $smhead1...$smhead2 (commits not present)
        EOF
        test_cmp expected actual
 '
index 9dd5cde5fc53deb365234e815d7142de9a975005..eb829fce97dc7067cbb502b9f5fd2b14c4e74e10 100755 (executable)
@@ -25,7 +25,7 @@ EOF
        test_cmp expect actual
 '
 
-test_expect_success 'exclude only no longer errors out' '
+test_expect_success 'exclude only pathspec uses default implicit pathspec' '
        git log --oneline --format=%s -- . ":(exclude)sub" >expect &&
        git log --oneline --format=%s -- ":(exclude)sub" >actual &&
        test_cmp expect actual
@@ -183,4 +183,15 @@ EOF
        test_cmp expect actual
 '
 
+test_expect_success 'multiple exclusions' '
+       git ls-files -- ":^*/file2" ":^sub2" >actual &&
+       cat <<-\EOF >expect &&
+       file
+       sub/file
+       sub/sub/file
+       sub/sub/sub/file
+       EOF
+       test_cmp expect actual
+'
+
 test_done
index 034914a14fd49fea07e8bff4802994fcd38807a9..6f083c4d68b677ff4058961311d6f753d3953a54 100755 (executable)
@@ -406,6 +406,14 @@ test_expect_success 'submodule update - command in .git/config' '
        )
 '
 
+test_expect_success 'submodule update - command in .gitmodules is ignored' '
+       test_when_finished "git -C super reset --hard HEAD^" &&
+       git -C super config -f .gitmodules submodule.submodule.update "!false" &&
+       git -C super commit -a -m "add command to .gitmodules file" &&
+       git -C super/submodule reset --hard $submodulesha1^ &&
+       git -C super submodule update submodule
+'
+
 cat << EOF >expect
 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
 EOF
index 43d19a9b2292033fd0297afb7df162efa2eb5621..93f162a4f78cf9c54b12309a62f6a89b2ce4ddbb 100755 (executable)
@@ -1670,4 +1670,14 @@ test_expect_success '"Initial commit" should not be noted in commit template' '
        test_i18ngrep ! "Initial commit" output
 '
 
+test_expect_success '--no-optional-locks prevents index update' '
+       test-chmtime =1234567890 .git/index &&
+       git --no-optional-locks status &&
+       test-chmtime -v +0 .git/index >out &&
+       grep ^1234567890 out &&
+       git status &&
+       test-chmtime -v +0 .git/index >out &&
+       ! grep ^1234567890 out
+'
+
 test_done
index 67b8c50a5ab431345de2909209811fc8e4f12548..d47560b6343db7006461259e4808837f3d35eecb 100755 (executable)
@@ -3120,4 +3120,146 @@ test_expect_success 'U: validate root delete result' '
        compare_diff_raw expect actual
 '
 
+###
+### series V (checkpoint)
+###
+
+# The commands in input_file should not produce any output on the file
+# descriptor set with --cat-blob-fd (or stdout if unspecified).
+#
+# To make sure you're observing the side effects of checkpoint *before*
+# fast-import terminates (and thus writes out its state), check that the
+# fast-import process is still running using background_import_still_running
+# *after* evaluating the test conditions.
+background_import_then_checkpoint () {
+       options=$1
+       input_file=$2
+
+       mkfifo V.input
+       exec 8<>V.input
+       rm V.input
+
+       mkfifo V.output
+       exec 9<>V.output
+       rm V.output
+
+       git fast-import $options <&8 >&9 &
+       echo $! >V.pid
+       # We don't mind if fast-import has already died by the time the test
+       # ends.
+       test_when_finished "exec 8>&-; exec 9>&-; kill $(cat V.pid) || true"
+
+       # Start in the background to ensure we adhere strictly to (blocking)
+       # pipes writing sequence. We want to assume that the write below could
+       # block, e.g. if fast-import blocks writing its own output to &9
+       # because there is no reader on &9 yet.
+       (
+               cat "$input_file"
+               echo "checkpoint"
+               echo "progress checkpoint"
+       ) >&8 &
+
+       error=1 ;# assume the worst
+       while read output <&9
+       do
+               if test "$output" = "progress checkpoint"
+               then
+                       error=0
+                       break
+               fi
+               # otherwise ignore cruft
+               echo >&2 "cruft: $output"
+       done
+
+       if test $error -eq 1
+       then
+               false
+       fi
+}
+
+background_import_still_running () {
+       if ! kill -0 "$(cat V.pid)"
+       then
+               echo >&2 "background fast-import terminated too early"
+               false
+       fi
+}
+
+test_expect_success PIPE 'V: checkpoint helper does not get stuck with extra output' '
+       cat >input <<-INPUT_END &&
+       progress foo
+       progress bar
+
+       INPUT_END
+
+       background_import_then_checkpoint "" input &&
+       background_import_still_running
+'
+
+test_expect_success PIPE 'V: checkpoint updates refs after reset' '
+       cat >input <<-\INPUT_END &&
+       reset refs/heads/V
+       from refs/heads/U
+
+       INPUT_END
+
+       background_import_then_checkpoint "" input &&
+       test "$(git rev-parse --verify V)" = "$(git rev-parse --verify U)" &&
+       background_import_still_running
+'
+
+test_expect_success PIPE 'V: checkpoint updates refs and marks after commit' '
+       cat >input <<-INPUT_END &&
+       commit refs/heads/V
+       mark :1
+       committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+       data 0
+       from refs/heads/U
+
+       INPUT_END
+
+       background_import_then_checkpoint "--export-marks=marks.actual" input &&
+
+       echo ":1 $(git rev-parse --verify V)" >marks.expected &&
+
+       test "$(git rev-parse --verify V^)" = "$(git rev-parse --verify U)" &&
+       test_cmp marks.expected marks.actual &&
+       background_import_still_running
+'
+
+# Re-create the exact same commit, but on a different branch: no new object is
+# created in the database, but the refs and marks still need to be updated.
+test_expect_success PIPE 'V: checkpoint updates refs and marks after commit (no new objects)' '
+       cat >input <<-INPUT_END &&
+       commit refs/heads/V2
+       mark :2
+       committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+       data 0
+       from refs/heads/U
+
+       INPUT_END
+
+       background_import_then_checkpoint "--export-marks=marks.actual" input &&
+
+       echo ":2 $(git rev-parse --verify V2)" >marks.expected &&
+
+       test "$(git rev-parse --verify V2)" = "$(git rev-parse --verify V)" &&
+       test_cmp marks.expected marks.actual &&
+       background_import_still_running
+'
+
+test_expect_success PIPE 'V: checkpoint updates tags after tag' '
+       cat >input <<-INPUT_END &&
+       tag Vtag
+       from refs/heads/V
+       tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+       data 0
+
+       INPUT_END
+
+       background_import_then_checkpoint "" input &&
+       git show-ref -d Vtag &&
+       background_import_still_running
+'
+
 test_done
index fb8c01e57a99f35f63690c536e3f623d3695b1de..f1e2f61991424f3314158676754f6b8d305a31f2 100644 (file)
@@ -471,11 +471,10 @@ void transport_print_push_status(const char *dest, struct ref *refs,
 {
        struct ref *ref;
        int n = 0;
-       struct object_id head_oid;
        char *head;
        int summary_width = transport_summary_width(refs);
 
-       head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL);
+       head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
 
        if (verbose) {
                for (ref = refs; ref; ref = ref->next)
index 6f730ee8f22b958c7502656d2e1d01ea1f932396..29bc64cc0280675065142bac257930904216d415 100644 (file)
@@ -121,15 +121,13 @@ static void status_printf_more(struct wt_status *s, const char *color,
 
 void wt_status_prepare(struct wt_status *s)
 {
-       struct object_id oid;
-
        memset(s, 0, sizeof(*s));
        memcpy(s->color_palette, default_wt_status_colors,
               sizeof(default_wt_status_colors));
        s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
        s->use_color = -1;
        s->relative_paths = 1;
-       s->branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
+       s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
        s->reference = "HEAD";
        s->fp = stdout;
        s->index_file = get_index_file();