Merge branch 'rs/fsck-null-return-from-lookup'
authorJunio C Hamano <gitster@pobox.com>
Wed, 11 Oct 2017 05:52:23 +0000 (14:52 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 11 Oct 2017 05:52:23 +0000 (14:52 +0900)
Improve behaviour of "git fsck" upon finding a missing object.

* rs/fsck-null-return-from-lookup:
fsck: handle NULL return of lookup_blob() and lookup_tree()

379 files changed:
.clang-format [new file with mode: 0644]
.travis.yml
.tsan-suppressions [new file with mode: 0644]
Documentation/Makefile
Documentation/RelNotes/2.15.0.txt [new file with mode: 0644]
Documentation/config.txt
Documentation/diff-options.txt
Documentation/git-add.txt
Documentation/git-apply.txt
Documentation/git-branch.txt
Documentation/git-cat-file.txt
Documentation/git-checkout.txt
Documentation/git-cvsserver.txt
Documentation/git-describe.txt
Documentation/git-diff-index.txt
Documentation/git-filter-branch.txt
Documentation/git-for-each-ref.txt
Documentation/git-format-patch.txt
Documentation/git-grep.txt
Documentation/git-interpret-trailers.txt
Documentation/git-merge.txt
Documentation/git-notes.txt
Documentation/git-push.txt
Documentation/git-read-tree.txt
Documentation/git-rebase.txt
Documentation/git-rerere.txt
Documentation/git-rev-parse.txt
Documentation/git-rm.txt
Documentation/git-send-pack.txt
Documentation/git-status.txt
Documentation/git-svn.txt
Documentation/git-tag.txt
Documentation/git-update-index.txt
Documentation/git.txt
Documentation/gitattributes.txt
Documentation/gitcore-tutorial.txt
Documentation/githooks.txt
Documentation/gitrepository-layout.txt
Documentation/gittutorial.txt
Documentation/glossary-content.txt
Documentation/merge-options.txt
Documentation/merge-strategies.txt
Documentation/pretty-formats.txt
Documentation/rev-list-options.txt
Documentation/technical/api-argv-array.txt
Documentation/technical/api-config.txt
Documentation/technical/api-ref-iteration.txt
Documentation/technical/api-string-list.txt [deleted file]
Documentation/technical/api-tree-walking.txt
Documentation/technical/hash-function-transition.txt [new file with mode: 0644]
Documentation/technical/pack-protocol.txt
Documentation/technical/trivial-merge.txt
Documentation/user-manual.txt
GIT-VERSION-GEN
Makefile
RelNotes
abspath.c
apply.c
archive.c
attr.c
bisect.c
branch.c
builtin/add.c
builtin/am.c
builtin/blame.c
builtin/branch.c
builtin/cat-file.c
builtin/check-ref-format.c
builtin/checkout.c
builtin/clean.c
builtin/clone.c
builtin/commit-tree.c
builtin/commit.c
builtin/config.c
builtin/count-objects.c
builtin/describe.c
builtin/diff-files.c
builtin/diff-index.c
builtin/diff-tree.c
builtin/diff.c
builtin/difftool.c
builtin/fast-export.c
builtin/fetch.c
builtin/for-each-ref.c
builtin/fsck.c
builtin/gc.c
builtin/get-tar-commit-id.c
builtin/grep.c
builtin/hash-object.c
builtin/help.c
builtin/index-pack.c
builtin/init-db.c
builtin/interpret-trailers.c
builtin/log.c
builtin/ls-files.c
builtin/merge-tree.c
builtin/merge.c
builtin/mv.c
builtin/name-rev.c
builtin/notes.c
builtin/pack-objects.c
builtin/pack-redundant.c
builtin/prune-packed.c
builtin/prune.c
builtin/pull.c
builtin/push.c
builtin/read-tree.c
builtin/rebase--helper.c
builtin/receive-pack.c
builtin/reflog.c
builtin/remote-ext.c
builtin/remote.c
builtin/replace.c
builtin/rerere.c
builtin/reset.c
builtin/rev-list.c
builtin/rev-parse.c
builtin/rm.c
builtin/send-pack.c
builtin/shortlog.c
builtin/show-branch.c
builtin/submodule--helper.c
builtin/symbolic-ref.c
builtin/tag.c
builtin/unpack-file.c
builtin/update-index.c
builtin/update-ref.c
builtin/verify-tag.c
builtin/worktree.c
bulk-checkin.c
bundle.c
cache-tree.c
cache.h
ci/install-dependencies.sh [new file with mode: 0755]
ci/lib-travisci.sh [new file with mode: 0755]
ci/print-test-failures.sh [new file with mode: 0755]
ci/run-build.sh [new file with mode: 0755]
ci/run-linux32-docker.sh [new file with mode: 0755]
ci/run-static-analysis.sh [new file with mode: 0755]
ci/run-tests.sh [new file with mode: 0755]
ci/run-windows-build.sh
ci/test-documentation.sh
color.c
color.h
commit-slab.h
commit.c
commit.h
compat/poll/poll.c
compat/win32/lazyload.h [new file with mode: 0644]
config.c
config.h
config.mak.uname
connect.c
connected.c
contrib/coccinelle/array.cocci
contrib/contacts/git-contacts
contrib/diff-highlight/Makefile
contrib/examples/git-merge.sh
contrib/examples/git-resolve.sh
contrib/rerere-train.sh
contrib/subtree/t/t7900-subtree.sh
convert.c
credential-cache--daemon.c
csum-file.c
diff-delta.c
diff-lib.c
diff.c
diff.h
diffcore-rename.c
dir.c
entry.c
environment.c
exec_cmd.c
fast-import.c
fetch-pack.c
fsck.c
git-compat-util.h
git-filter-branch.sh
git-gui/po/README
git-merge-octopus.sh
git-p4.py
git-rebase--am.sh
git-rebase--interactive.sh
git-rebase.sh
git-request-pull.sh
git-send-email.perl
git-stash.sh
git.c
gitweb/gitweb.perl
gpg-interface.c
graph.c
grep.c
grep.h
hash.h
hashmap.c
hashmap.h
http-backend.c
http-push.c
http-walker.c
http.c
imap-send.c
line-log.c
line-log.h
list.h
ll-merge.c
lockfile.c
lockfile.h
log-tree.c
mailinfo.c
mailmap.c
merge-recursive.c
merge-recursive.h
name-hash.c
notes-merge.c
notes.c
object.c
object.h
oidmap.c [new file with mode: 0644]
oidmap.h [new file with mode: 0644]
oidset.c
oidset.h
pack-bitmap-write.c
pack-bitmap.c
pack-check.c
pack-write.c
packfile.c [new file with mode: 0644]
packfile.h [new file with mode: 0644]
pager.c
parse-options.c
patch-ids.c
path.c
pathspec.c
pathspec.h
pkt-line.c
pretty.c
progress.c
progress.h
quote.c
reachable.c
read-cache.c
ref-filter.c
reflog-walk.c
refs.c
refs.h
refs/files-backend.c
refs/iterator.c
refs/packed-backend.c [new file with mode: 0644]
refs/packed-backend.h [new file with mode: 0644]
refs/ref-cache.c
refs/ref-cache.h
refs/refs-internal.h
remote.c
remote.h
repository.c
repository.h
rerere.c
revision.c
revision.h
run-command.c
send-pack.c
sequencer.c
sequencer.h
server-info.c
setup.c
sha1-lookup.c
sha1-lookup.h
sha1_file.c
sha1_name.c
sha1dc_git.c
sha1dc_git.h
shallow.c
strbuf.c
strbuf.h
streaming.c
string-list.h
sub-process.c
sub-process.h
submodule-config.c
submodule-config.h
submodule.c
submodule.h
t/README
t/check-non-portable-shell.pl
t/helper/.gitignore
t/helper/test-delta.c
t/helper/test-hashmap.c
t/helper/test-line-buffer.c
t/helper/test-parse-options.c
t/helper/test-string-list.c
t/helper/test-submodule-config.c
t/helper/test-write-cache.c [new file with mode: 0644]
t/perf/p0007-write-cache.sh [new file with mode: 0755]
t/t0040-parse-options.sh
t/t1200-tutorial.sh [deleted file]
t/t1400-update-ref.sh
t/t1404-update-ref-errors.sh
t/t1407-worktree-ref-store.sh
t/t1408-packed-refs.sh [new file with mode: 0755]
t/t1500-rev-parse.sh
t/t1502-rev-parse-parseopt.sh
t/t3200-branch.sh
t/t3203-branch-output.sh
t/t3205-branch-color.sh
t/t3210-pack-refs.sh
t/t3404-rebase-interactive.sh
t/t3415-rebase-autosquash.sh
t/t3701-add-interactive.sh
t/t3903-stash.sh
t/t3905-stash-include-untracked.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/t4015-diff-whitespace.sh
t/t4027-diff-submodule.sh
t/t4059-diff-submodule-not-initialized.sh
t/t4200-rerere.sh
t/t4202-log.sh
t/t4205-log-pretty-formats.sh
t/t5001-archive-attr.sh
t/t5002-archive-attr-pattern.sh
t/t5004-archive-corner-cases.sh
t/t5150-request-pull.sh
t/t5304-prune.sh
t/t5308-pack-detect-duplicates.sh
t/t5520-pull.sh
t/t5531-deep-submodule-push.sh
t/t5534-push-signed.sh
t/t5572-pull-submodule.sh
t/t6002-rev-list-bisect.sh
t/t6006-rev-list-format.sh
t/t6040-tracking-info.sh
t/t6120-describe.sh
t/t6132-pathspec-exclude.sh
t/t6300-for-each-ref.sh
t/t7001-mv.sh
t/t7004-tag.sh
t/t7006-pager.sh
t/t7301-clean-interactive.sh
t/t7400-submodule-basic.sh
t/t7406-submodule-update.sh
t/t7411-submodule-config.sh
t/t7502-commit.sh
t/t7504-commit-msg-hook.sh
t/t7508-status.sh
t/t7513-interpret-trailers.sh
t/t7614-merge-signoff.sh [new file with mode: 0755]
t/t8010-cat-file-filters.sh
t/t9001-send-email.sh
t/t9010-svn-fe.sh
t/t9300-fast-import.sh
t/t9350-fast-export.sh
t/test-lib.sh
t/test-terminal.perl
tag.c
tag.h
tempfile.c
tempfile.h
templates/hooks--pre-rebase.sample
templates/hooks--prepare-commit-msg.sample
trailer.c
trailer.h
transport-helper.c
transport.c
tree-diff.c
tree-walk.c
tree-walk.h
unpack-trees.c
upload-pack.c
usage.c
userdiff.c
utf8.c
vcs-svn/svndump.c
worktree.c
worktree.h
wrapper.c
wt-status.c
diff --git a/.clang-format b/.clang-format
new file mode 100644 (file)
index 0000000..611ab47
--- /dev/null
@@ -0,0 +1,169 @@
+# This file is an example configuration for clang-format 5.0.
+#
+# Note that this style definition should only be understood as a hint
+# for writing new code. The rules are still work-in-progress and does
+# not yet exactly match the style we have in the existing code.
+
+# Use tabs whenever we need to fill whitespace that spans at least from one tab
+# stop to the next one.
+UseTab: Always
+TabWidth: 8
+IndentWidth: 8
+ContinuationIndentWidth: 8
+ColumnLimit: 80
+
+# C Language specifics
+Language: Cpp
+
+# Align parameters on the open bracket
+# someLongFunction(argument1,
+#                  argument2);
+AlignAfterOpenBracket: Align
+
+# Don't align consecutive assignments
+# int aaaa = 12;
+# int b = 14;
+AlignConsecutiveAssignments: false
+
+# Don't align consecutive declarations
+# int aaaa = 12;
+# double b = 3.14;
+AlignConsecutiveDeclarations: false
+
+# Align escaped newlines as far left as possible
+# #define A   \
+#   int aaaa; \
+#   int b;    \
+#   int cccccccc;
+AlignEscapedNewlines: Left
+
+# Align operands of binary and ternary expressions
+# int aaa = bbbbbbbbbbb +
+#           cccccc;
+AlignOperands: true
+
+# Don't align trailing comments
+# int a; // Comment a
+# int b = 2; // Comment b
+AlignTrailingComments: false
+
+# By default don't allow putting parameters onto the next line
+# myFunction(foo, bar, baz);
+AllowAllParametersOfDeclarationOnNextLine: false
+
+# Don't allow short braced statements to be on a single line
+# if (a)           not       if (a) return;
+#   return;
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: false
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+
+# By default don't add a line break after the return type of top-level functions
+# int foo();
+AlwaysBreakAfterReturnType: None
+
+# Pack as many parameters or arguments onto the same line as possible
+# int myFunction(int aaaaaaaaaaaa, int bbbbbbbb,
+#                int cccc);
+BinPackArguments: true
+BinPackParameters: true
+
+# Attach braces to surrounding context except break before braces on function
+# definitions.
+# void foo()
+# {
+#    if (true) {
+#    } else {
+#    }
+# };
+BreakBeforeBraces: Linux
+
+# Break after operators
+# int valuve = aaaaaaaaaaaaa +
+#              bbbbbb -
+#              ccccccccccc;
+BreakBeforeBinaryOperators: None
+BreakBeforeTernaryOperators: false
+
+# Don't break string literals
+BreakStringLiterals: false
+
+# Use the same indentation level as for the switch statement.
+# Switch statement body is always indented one level more than case labels.
+IndentCaseLabels: false
+
+# Don't indent a function definition or declaration if it is wrapped after the
+# type
+IndentWrappedFunctionNames: false
+
+# Align pointer to the right
+# int *a;
+PointerAlignment: Right
+
+# Don't insert a space after a cast
+# x = (int32)y;    not    x = (int32) y;
+SpaceAfterCStyleCast: false
+
+# Insert spaces before and after assignment operators
+# int a = 5;    not    int a=5;
+# a += 42;             a+=42;
+SpaceBeforeAssignmentOperators: true
+
+# Put a space before opening parentheses only after control statement keywords.
+# void f() {
+#   if (true) {
+#     f();
+#   }
+# }
+SpaceBeforeParens: ControlStatements
+
+# Don't insert spaces inside empty '()'
+SpaceInEmptyParentheses: false
+
+# The number of spaces before trailing line comments (// - comments).
+# This does not affect trailing block comments (/* - comments).
+SpacesBeforeTrailingComments: 1
+
+# Don't insert spaces in casts
+# x = (int32) y;    not    x = ( int32 ) y;
+SpacesInCStyleCastParentheses: false
+
+# Don't insert spaces inside container literals
+# var arr = [1, 2, 3];    not    var arr = [ 1, 2, 3 ];
+SpacesInContainerLiterals: false
+
+# Don't insert spaces after '(' or before ')'
+# f(arg);    not    f( arg );
+SpacesInParentheses: false
+
+# Don't insert spaces after '[' or before ']'
+# int a[5];    not    int a[ 5 ];
+SpacesInSquareBrackets: false
+
+# Insert a space after '{' and before '}' in struct initializers
+Cpp11BracedListStyle: false
+
+# A list of macros that should be interpreted as foreach loops instead of as
+# function calls.
+ForEachMacros: ['for_each_string_list_item']
+
+# The maximum number of consecutive empty lines to keep.
+MaxEmptyLinesToKeep: 1
+
+# No empty line at the start of a block.
+KeepEmptyLinesAtTheStartOfBlocks: false
+
+# Penalties
+# This decides what order things should be done if a line is too long
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 5
+
+# Don't sort #include's
+SortIncludes: false
index 278943d14a244b6b748078ff9ee18a17f58e7b45..fead995eddd15460b6be81e6a5f7c8f0648368ca 100644 (file)
@@ -61,23 +61,8 @@ matrix:
       services:
         - docker
       before_install:
-        - docker pull daald/ubuntu32:xenial
       before_script:
-      script:
-        - >
-          docker run
-          --interactive
-          --env DEVELOPER
-          --env DEFAULT_TEST_TARGET
-          --env GIT_PROVE_OPTS
-          --env GIT_TEST_OPTS
-          --env GIT_TEST_CLONE_2GB
-          --volume "${PWD}:/usr/src/git"
-          daald/ubuntu32:xenial
-          /usr/src/git/ci/run-linux32-build.sh $(id -u $USER)
-        # Use the following command to debug the docker build locally:
-        # $ docker run -itv "${PWD}:/usr/src/git" --entrypoint /bin/bash daald/ubuntu32:xenial
-        # root@container:/# /usr/src/git/ci/run-linux32-build.sh
+      script: ci/run-linux32-docker.sh
     - env: Static Analysis
       os: linux
       compiler:
@@ -86,9 +71,8 @@ matrix:
           packages:
           - coccinelle
       before_install:
-      script:
-        # "before_script" that builds Git is inherited from base job
-        - make coccicheck
+      # "before_script" that builds Git is inherited from base job
+      script: ci/run-static-analysis.sh
       after_failure:
     - env: Documentation
       os: linux
@@ -99,70 +83,14 @@ matrix:
           - asciidoc
           - xmlto
       before_install:
-      before_script: gem install asciidoctor
+      before_script:
       script: ci/test-documentation.sh
       after_failure:
 
-before_install:
-  - >
-    case "${TRAVIS_OS_NAME:-linux}" in
-    linux)
-      export GIT_TEST_HTTPD=YesPlease
-
-      mkdir --parents custom/p4
-      pushd custom/p4
-        wget --quiet http://filehost.perforce.com/perforce/r$LINUX_P4_VERSION/bin.linux26x86_64/p4d
-        wget --quiet http://filehost.perforce.com/perforce/r$LINUX_P4_VERSION/bin.linux26x86_64/p4
-        chmod u+x p4d
-        chmod u+x p4
-        export PATH="$(pwd):$PATH"
-      popd
-      mkdir --parents custom/git-lfs
-      pushd custom/git-lfs
-        wget --quiet https://github.com/github/git-lfs/releases/download/v$LINUX_GIT_LFS_VERSION/git-lfs-linux-amd64-$LINUX_GIT_LFS_VERSION.tar.gz
-        tar --extract --gunzip --file "git-lfs-linux-amd64-$LINUX_GIT_LFS_VERSION.tar.gz"
-        cp git-lfs-$LINUX_GIT_LFS_VERSION/git-lfs .
-        export PATH="$(pwd):$PATH"
-      popd
-      ;;
-    osx)
-      brew update --quiet
-      # Uncomment this if you want to run perf tests:
-      # brew install gnu-time
-      brew install git-lfs gettext
-      brew link --force gettext
-      brew install caskroom/cask/perforce
-      ;;
-    esac;
-    echo "$(tput setaf 6)Perforce Server Version$(tput sgr0)";
-    p4d -V | grep Rev.;
-    echo "$(tput setaf 6)Perforce Client Version$(tput sgr0)";
-    p4 -V | grep Rev.;
-    echo "$(tput setaf 6)Git-LFS Version$(tput sgr0)";
-    git-lfs version;
-
-before_script: make --jobs=2
-
-script:
-  - >
-    mkdir -p $HOME/travis-cache;
-    ln -s $HOME/travis-cache/.prove t/.prove;
-    make --quiet test;
-
-after_failure:
-  - >
-    : '<-- Click here to see detailed test output!                                                        ';
-    for TEST_EXIT in t/test-results/*.exit;
-    do
-      if [ "$(cat "$TEST_EXIT")" != "0" ];
-      then
-        TEST_OUT="${TEST_EXIT%exit}out";
-        echo "------------------------------------------------------------------------";
-        echo "$(tput setaf 1)${TEST_OUT}...$(tput sgr0)";
-        echo "------------------------------------------------------------------------";
-        cat "${TEST_OUT}";
-      fi;
-    done;
+before_install: ci/install-dependencies.sh
+before_script: ci/run-build.sh
+script: ci/run-tests.sh
+after_failure: ci/print-test-failures.sh
 
 notifications:
   email: false
diff --git a/.tsan-suppressions b/.tsan-suppressions
new file mode 100644 (file)
index 0000000..8c85014
--- /dev/null
@@ -0,0 +1,10 @@
+# Suppressions for ThreadSanitizer (tsan).
+#
+# This file is used by setting the environment variable TSAN_OPTIONS to, e.g.,
+# "suppressions=$(pwd)/.tsan-suppressions". Observe that relative paths such as
+# ".tsan-suppressions" might not work.
+
+# A static variable is written to racily, but we always write the same value, so
+# in practice it (hopefully!) doesn't matter.
+race:^want_color$
+race:^transfer_debug$
index 2415e0d6573e0dd9c0802708df1b5699d71dd09f..471bb29725cdc47b4e56bc85e2af635f7b836776 100644 (file)
@@ -67,6 +67,7 @@ SP_ARTICLES += howto/maintain-git
 API_DOCS = $(patsubst %.txt,%,$(filter-out technical/api-index-skel.txt technical/api-index.txt, $(wildcard technical/api-*.txt)))
 SP_ARTICLES += $(API_DOCS)
 
+TECH_DOCS += technical/hash-function-transition
 TECH_DOCS += technical/http-protocol
 TECH_DOCS += technical/index-format
 TECH_DOCS += technical/pack-format
diff --git a/Documentation/RelNotes/2.15.0.txt b/Documentation/RelNotes/2.15.0.txt
new file mode 100644 (file)
index 0000000..7b8eeb5
--- /dev/null
@@ -0,0 +1,518 @@
+Git 2.15 Release Notes
+======================
+
+Backward compatibility notes and other notable changes.
+
+ * Use of an empty string as a pathspec element that is used for
+   'everything matches' is still warned and Git asks users to use a
+   more explicit '.' for that instead.  The hope is that existing
+   users will not mind this change, and eventually the warning can be
+   turned into a hard error, upgrading the deprecation into removal of
+   this (mis)feature.  That is now scheduled to happen in Git v2.16,
+   the next major release after this one.
+
+ * Git now avoids blindly falling back to ".git" when the setup
+   sequence said we are _not_ in Git repository.  A corner case that
+   happens to work right now may be broken by a call to BUG().
+   We've tried hard to locate such cases and fixed them, but there
+   might still be cases that need to be addressed--bug reports are
+   greatly appreciated.
+
+ * "branch --set-upstream" that has been deprecated in Git 1.8 has
+   finally been retired.
+
+
+Updates since v2.14
+-------------------
+
+UI, Workflows & Features
+
+ * An example that is now obsolete has been removed from a sample hook,
+   and an old example in it that added a sign-off manually has been
+   improved to use the interpret-trailers command.
+
+ * The advice message given when "git rebase" stops for conflicting
+   changes has been improved.
+
+ * The "rerere-train" script (in contrib/) learned the "--overwrite"
+   option to allow overwriting existing recorded resolutions.
+
+ * "git contacts" (in contrib/) now lists the address on the
+   "Reported-by:" trailer to its output, in addition to those on
+   S-o-b: and other trailers, to make it easier to notify (and thank)
+   the original bug reporter.
+
+ * "git rebase", especially when it is run by mistake and ends up
+   trying to replay many changes, spent long time in silence.  The
+   command has been taught to show progress report when it spends
+   long time preparing these many changes to replay (which would give
+   the user a chance to abort with ^C).
+
+ * "git merge" learned a "--signoff" option to add the Signed-off-by:
+   trailer with the committer's name.
+
+ * "git diff" learned to optionally paint new lines that are the same
+   as deleted lines elsewhere differently from genuinely new lines.
+
+ * "git interpret-trailers" learned to take the trailer specifications
+   from the command line that overrides the configured values.
+
+ * "git interpret-trailers" has been taught a "--parse" and a few
+   other options to make it easier for scripts to grab existing
+   trailer lines from a commit log message.
+
+ * The "--format=%(trailers)" option "git log" and its friends take
+   learned to take the 'unfold' and 'only' modifiers to normalize its
+   output, e.g. "git log --format=%(trailers:only,unfold)".
+
+ * "gitweb" shows a link to visit the 'raw' contents of blbos in the
+   history overview page.
+
+ * "[gc] rerereResolved = 5.days" used to be invalid, as the variable
+   is defined to take an integer counting the number of days.  It now
+   is allowed.
+
+ * The code to acquire a lock on a reference (e.g. while accepting a
+   push from a client) used to immediately fail when the reference is
+   already locked---now it waits for a very short while and retries,
+   which can make it succeed if the lock holder was holding it during
+   a read-only operation.
+
+ * "branch --set-upstream" that has been deprecated in Git 1.8 has
+   finally been retired.
+
+ * The codepath to call external process filter for smudge/clean
+   operation learned to show the progress meter.
+
+ * "git rev-parse" learned "--is-shallow-repository", that is to be
+   used in a way similar to existing "--is-bare-repository" and
+   friends.
+
+ * "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.
+
+ * Conversion from uchar[20] to struct object_id continues.
+
+ * Start using selected c99 constructs in small, stable and
+   essentialpart of the system to catch people who care about
+   older compilers that do not grok them.
+
+ * The filter-process interface learned to allow a process with long
+   latency give a "delayed" response.
+
+ * Many uses of comparision callback function the hashmap API uses
+   cast the callback function type when registering it to
+   hashmap_init(), which defeats the compile time type checking when
+   the callback interface changes (e.g. gaining more parameters).
+   The callback implementations have been updated to take "void *"
+   pointers and cast them to the type they expect instead.
+
+ * Because recent Git for Windows do come with a real msgfmt, the
+   build procedure for git-gui has been updated to use it instead of a
+   hand-rolled substitute.
+
+ * "git grep --recurse-submodules" has been reworked to give a more
+   consistent output across submodule boundary (and do its thing
+   without having to fork a separate process).
+
+ * A helper function to read a single whole line into strbuf
+   mistakenly triggered OOM error at EOF under certain conditions,
+   which has been fixed.
+   (merge 642956cf45 rs/strbuf-getwholeline-fix later to maint).
+
+ * The "ref-store" code reorganization continues.
+
+ * "git commit" used to discard the index and re-read from the filesystem
+   just in case the pre-commit hook has updated it in the middle; this
+   has been optimized out when we know we do not run the pre-commit hook.
+   (merge 680ee550d7 kw/commit-keep-index-when-pre-commit-is-not-run later to maint).
+
+ * Updates to the HTTP layer we made recently unconditionally used
+   features of libCurl without checking the existence of them, causing
+   compilation errors, which has been fixed.  Also migrate the code to
+   check feature macros, not version numbers, to cope better with
+   libCurl that vendor ships with backported features.
+
+ * The API to start showing progress meter after a short delay has
+   been simplified.
+   (merge 8aade107dd jc/simplify-progress later to maint).
+
+ * Code clean-up to avoid mixing values read from the .gitmodules file
+   and values read from the .git/config file.
+
+ * We used to spend more than necessary cycles allocating and freeing
+   piece of memory while writing each index entry out.  This has been
+   optimized.
+
+ * Platforms that ship with a separate sha1 with collision detection
+   library can link to it instead of using the copy we ship as part of
+   our source tree.
+
+ * Code around "notes" have been cleaned up.
+   (merge 3964281524 mh/notes-cleanup later to maint).
+
+ * The long-standing rule that an in-core lockfile instance, once it
+   is used, must not be freed, has been lifted and the lockfile and
+   tempfile APIs have been updated to reduce the chance of programming
+   errors.
+
+ * Our hashmap implementation in hashmap.[ch] is not thread-safe when
+   adding a new item needs to expand the hashtable by rehashing; add
+   an API to disable the automatic rehashing to work it around.
+
+ * Many of our programs consider that it is OK to release dynamic
+   storage that is used throughout the life of the program by simply
+   exiting, but this makes it harder to leak detection tools to avoid
+   reporting false positives.  Plug many existing leaks and introduce
+   a mechanism for developers to mark that the region of memory
+   pointed by a pointer is not lost/leaking to help these tools.
+
+ * As "git commit" to conclude a conflicted "git merge" honors the
+   commit-msg hook, "git merge" that records a merge commit that
+   cleanly auto-merges should, but it didn't.
+
+ * The codepath for "git merge-recursive" has been cleaned up.
+
+ * Many leaks of strbuf have been fixed.
+
+ * "git imap-send" has our own implementation of the protocol and also
+   can use more recent libCurl with the imap protocol support.  Update
+   the latter so that it can use the credential subsystem, and then
+   make it the default option to use, so that we can eventually
+   deprecate and remove the former.
+
+ * "make style" runs git-clang-format to help developers by pointing
+   out coding style issues.
+
+ * A test to demonstrate "git mv" failing to adjust nested submodules
+   has been added.
+   (merge c514167df2 hv/mv-nested-submodules-test later to maint).
+
+ * On Cygwin, "ulimit -s" does not report failure but it does not work
+   at all, which causes an unexpected success of some tests that
+   expect failures under a limited stack situation.  This has been
+   fixed.
+
+ * Many codepaths have been updated to squelch -Wimplicit-fallthrough
+   warnings from Gcc 7 (which is a good code hygiene).
+
+ * Add a helper for DLL loading in anticipation for its need in a
+   future topic RSN.
+
+ * "git status --ignored", when noticing that a directory without any
+   tracked path is ignored, still enumerated all the ignored paths in
+   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.
+
+ * Add comment to clarify that the style file is meant to be used with
+   clang-5 and the rules are still work in progress.
+
+ * Many variables that points at a region of memory that will live
+   throughout the life of the program have been marked with UNLEAK
+   marker to help the leak checkers concentrate on real leaks..
+
+
+Also contains various documentation updates and code clean-ups.
+
+
+Fixes since v2.14
+-----------------
+
+ * "%C(color name)" in the pretty print format always produced ANSI
+   color escape codes, which was an early design mistake.  They now
+   honor the configuration (e.g. "color.ui = never") and also tty-ness
+   of the output medium.
+
+ * The http.{sslkey,sslCert} configuration variables are to be
+   interpreted as a pathname that honors "~[username]/" prefix, but
+   weren't, which has been fixed.
+
+ * Numerous bugs in walking of reflogs via "log -g" and friends have
+   been fixed.
+
+ * "git commit" when seeing an totally empty message said "you did not
+   edit the message", which is clearly wrong.  The message has been
+   corrected.
+
+ * When a directory is not readable, "gitweb" fails to build the
+   project list.  Work this around by skipping such a directory.
+
+ * Some versions of GnuPG fails to kill gpg-agent it auto-spawned
+   and such a left-over agent can interfere with a test.  Work it
+   around by attempting to kill one before starting a new test.
+
+ * A recently added test for the "credential-cache" helper revealed
+   that EOF detection done around the time the connection to the cache
+   daemon is torn down were flaky.  This was fixed by reacting to
+   ECONNRESET and behaving as if we got an EOF.
+
+ * "git log --tag=no-such-tag" showed log starting from HEAD, which
+   has been fixed---it now shows nothing.
+
+ * The "tag.pager" configuration variable was useless for those who
+   actually create tag objects, as it interfered with the use of an
+   editor.  A new mechanism has been introduced for commands to enable
+   pager depending on what operation is being carried out to fix this,
+   and then "git tag -l" is made to run pager by default.
+
+ * "git push --recurse-submodules $there HEAD:$target" was not
+   propagated down to the submodules, but now it is.
+
+ * Commands like "git rebase" accepted the --rerere-autoupdate option
+   from the command line, but did not always use it.  This has been
+   fixed.
+
+ * "git clone --recurse-submodules --quiet" did not pass the quiet
+   option down to submodules.
+
+ * Test portability fix for OBSD.
+
+ * Portability fix for OBSD.
+
+ * "git am -s" has been taught that some input may end with a trailer
+   block that is not Signed-off-by: and it should refrain from adding
+   an extra blank line before adding a new sign-off in such a case.
+
+ * "git svn" used with "--localtime" option did not compute the tz
+   offset for the timestamp in question and instead always used the
+   current time, which has been corrected.
+
+ * Memory leak in an error codepath has been plugged.
+
+ * "git stash -u" used the contents of the committed version of the
+   ".gitignore" file to decide which paths are ignored, even when the
+   file has local changes.  The command has been taught to instead use
+   the locally modified contents.
+
+ * bash 4.4 or newer gave a warning on NUL byte in command
+   substitution done in "git stash"; this has been squelched.
+
+ * "git grep -L" and "git grep --quiet -L" reported different exit
+   codes; this has been corrected.
+
+ * When handshake with a subprocess filter notices that the process
+   asked for an unknown capability, Git did not report what program
+   the offending subprocess was running.  This has been corrected.
+
+ * "git apply" that is used as a better "patch -p1" failed to apply a
+   taken from a file with CRLF line endings to a file with CRLF line
+   endings.  The root cause was because it misused convert_to_git()
+   that tried to do "safe-crlf" processing by looking at the index
+   entry at the same path, which is a nonsense---in that mode, "apply"
+   is not working on the data in (or derived from) the index at all.
+   This has been fixed.
+
+ * Killing "git merge --edit" before the editor returns control left
+   the repository in a state with MERGE_MSG but without MERGE_HEAD,
+   which incorrectly tells the subsequent "git commit" that there was
+   a squash merge in progress.  This has been fixed.
+
+ * "git archive" did not work well with pathspecs and the
+   export-ignore attribute.
+
+ * In addition to "cc: <a@dd.re.ss> # cruft", "cc: a@dd.re.ss # cruft"
+   was taught to "git send-email" as a valid way to tell it that it
+   needs to also send a carbon copy to <a@dd.re.ss> in the trailer
+   section.
+   (merge cc90750677 mm/send-email-cc-cruft later to maint).
+
+ * "git branch -M a b" while on a branch that is completely unrelated
+   to either branch a or branch b misbehaved when multiple worktree
+   was in use.  This has been fixed.
+   (merge 31824d180d nd/worktree-kill-parse-ref later to maint).
+
+ * "git gc" and friends when multiple worktrees are used off of a
+   single repository did not consider the index and per-worktree refs
+   of other worktrees as the root for reachability traversal, making
+   objects that are in use only in other worktrees to be subject to
+   garbage collection.
+
+ * A regression to "gitk --bisect" by a recent update has been fixed.
+   (merge 1d0538e486 mh/packed-ref-store-prep later to maint).
+
+ * "git -c submodule.recurse=yes pull" did not work as if the
+   "--recurse-submodules" option was given from the command line.
+   This has been corrected.
+
+ * Unlike "git commit-tree < file", "git commit-tree -F file" did not
+   pass the contents of the file verbatim and instead completed an
+   incomplete line at the end, if exists.  The latter has been updated
+   to match the behaviour of the former.
+   (merge c818e74332 rk/commit-tree-make-F-verbatim later to maint).
+
+ * Many codepaths did not diagnose write failures correctly when disks
+   go full, due to their misuse of write_in_full() helper function,
+   which have been corrected.
+   (merge f48ecd38cb jk/write-in-full-fix later to maint).
+
+ * "git help co" now says "co is aliased to ...", not "git co is".
+   (merge b3a8076e0d ks/help-alias-label later to maint).
+
+ * "git archive", especially when used with pathspec, stored an empty
+   directory in its output, even though Git itself never does so.
+   This has been fixed.
+   (merge 4318094047 rs/archive-excluded-directory later to maint).
+
+ * API error-proofing which happens to also squelch warnings from GCC.
+   (merge c788c54cde tg/refs-allowed-flags later to maint).
+
+ * The explanation of the cut-line in the commit log editor has been
+   slightly tweaked.
+   (merge 8c4b1a3593 ks/commit-do-not-touch-cut-line later to maint).
+
+ * "git gc" tries to avoid running two instances at the same time by
+   reading and writing pid/host from and to a lock file; it used to
+   use an incorrect fscanf() format when reading, which has been
+   corrected.
+   (merge afe2fab72c aw/gc-lockfile-fscanf-fix later to maint).
+
+ * The scripts to drive TravisCI has been reorganized and then an
+   optimization to avoid spending cycles on a branch whose tip is
+   tagged has been implemented.
+   (merge 8376eb4a8f ls/travis-scriptify later to maint).
+
+ * The test linter has been taught that we do not like "echo -e".
+   (merge 1a6d46895d tb/test-lint-echo-e later to maint).
+
+ * Code cmp.std.c nitpick.
+   (merge ac7da78ede mh/for-each-string-list-item-empty-fix later to maint).
+
+ * A regression fix for 2.11 that made the code to read the list of
+   alternate object stores overrun the end of the string.
+   (merge f0f7bebef7 jk/info-alternates-fix later to maint).
+
+ * "git describe --match" learned to take multiple patterns in v2.13
+   series, but the feature ignored the patterns after the first one
+   and did not work at all.  This has been fixed.
+   (merge da769d2986 jk/describe-omit-some-refs later to maint).
+
+ * "git filter-branch" cannot reproduce a history with a tag without
+   the tagger field, which only ancient versions of Git allowed to be
+   created.  This has been corrected.
+   (merge b2c1ca6b4b ic/fix-filter-branch-to-handle-tag-without-tagger later to maint).
+
+ * "git cat-file --textconv" started segfaulting recently, which
+   has been corrected.
+   (merge cc0ea7c9e5 jk/diff-blob later to maint).
+
+ * The built-in pattern to detect the "function header" for HTML did
+   not match <H1>..<H6> elements without any attributes, which has
+   been fixed.
+   (merge 9c03caca2c ik/userdiff-html-h-element-fix later to maint).
+
+ * "git mailinfo" was loose in decoding quoted printable and produced
+   garbage when the two letters after the equal sign are not
+   hexadecimal.  This has been fixed.
+   (merge c8cf423eab rs/mailinfo-qp-decode-fix later to maint).
+
+ * The machinery to create xdelta used in pack files received the
+   sizes of the data in size_t, but lost the higher bits of them by
+   storing them in "unsigned int" during the computation, which is
+   fixed.
+
+ * The delta format used in the packfile cannot reference data at
+   offset larger than what can be expressed in 4-byte, but the
+   generator for the data failed to make sure the offset does not
+   overflow.  This has been corrected.
+
+ * The documentation for '-X<option>' for merges was misleadingly
+   written to suggest that "-s theirs" exists, which is not the case.
+   (merge c25d98b2a7 jc/merge-x-theirs-docfix later to maint).
+
+ * "git fast-export" with -M/-C option issued "copy" instruction on a
+   path that is simultaneously modified, which was incorrect.
+   (merge b3e8ca89cf jt/fast-export-copy-modify-fix later to maint).
+
+ * Many codepaths have been updated to squelch -Wsign-compare
+   warnings.
+   (merge 071bcaab64 rj/no-sign-compare later to maint).
+
+ * 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).
+
+ * The checkpoint command "git fast-import" did not flush updates to
+   refs and marks unless at least one object was created since the
+   last checkpoint, which has been corrected, as these things can
+   happen without any new object getting created.
+   (merge 30e215a65c er/fast-import-dump-refs-on-checkpoint later to maint).
+
+ * Spell the name of our system as "Git" in the output from
+   request-pull script.
+   (merge e66d7c37a5 ar/request-pull-phrasofix later to maint).
+
+ * Fixes for a handful memory access issues identified by valgrind.
+   (merge 2944a94c6b tg/memfixes later to maint).
+
+ * Backports a moral equivalent of 2015 fix to the poll() emulation
+   from the upstream gnulib to fix occasional breakages on HPE NonStop.
+   (merge 61b2a1acaa rb/compat-poll-fix later to maint).
+
+ * Users with "color.ui = always" in their configuration were broken
+   by a recent change that made plumbing commands to pay attention to
+   them as the patch created internally by "git add -p" were colored
+   (heh) and made unusable.  Fix this regression by redefining
+   'always' to mean the same thing as 'auto'.
+   (merge 6be4595edb jk/ui-color-always-to-auto-maint later to maint).
+
+ * In the "--format=..." option of the "git for-each-ref" command (and
+   its friends, i.e. the listing mode of "git branch/tag"), "%(atom:)"
+   (e.g. "%(refname:)", "%(body:)" used to error out.  Instead, treat
+   them as if the colon and an empty string that follows it were not
+   there.
+   (merge bea4dbeafd tb/ref-filter-empty-modifier 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).
+   (merge 6cdf8a7929 ma/ts-cleanups later to maint).
+   (merge 7560f547e6 ma/up-to-date later to maint).
+   (merge 0db3dc75f3 rs/apply-epoch later to maint).
+   (merge 74f1bd912b dw/diff-highlight-makefile-fix later to maint).
+   (merge f991761eb8 jk/config-lockfile-leak-fix later to maint).
+   (merge 150efef1e7 ma/pkt-line-leakfix later to maint).
+   (merge 5554451de6 mg/timestamp-t-fix later to maint).
+   (merge 276d0e35c0 ma/split-symref-update-fix later to maint).
+   (merge 3bc4b8f7c7 bb/doc-eol-dirty later to maint).
+   (merge c1bb33c99c jk/system-path-cleanup later to maint).
+   (merge ab46e6fc72 cc/subprocess-handshake-missing-capabilities later to maint).
+   (merge f7a32dd97f kd/doc-for-each-ref later to maint).
+   (merge be94568bc7 ez/doc-duplicated-words-fix later to maint).
+   (merge 01e4be6c3d ks/test-readme-phrasofix later to maint).
+   (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).
+   (merge 9ca356fa8b rs/cocci-de-paren-call-params later to maint).
+   (merge 7099153e8d rs/tag-null-pointer-arith-fix later to maint).
+   (merge 0e187d758c rs/run-command-use-alloc-array later to maint).
+   (merge e0222159fa jn/strbuf-doc-re-reuse later to maint).
+   (merge 97487ea11a rs/qsort-s later to maint).
+   (merge a9155c50bd sb/branch-avoid-repeated-strbuf-release later to maint).
+   (merge f777623514 ks/branch-tweak-error-message-for-extra-args later to maint).
+   (merge 33f3c683ec ks/verify-filename-non-option-error-message-tweak later to maint).
index 2271809d90a5855c40de64bf1952c58a48b0d386..b53c994d0a9f41da483aa22f66c7e308044eda1e 100644 (file)
@@ -776,6 +776,12 @@ core.commentChar::
 If set to "auto", `git-commit` would select a character that is not
 the beginning character of any line in existing commit messages.
 
+core.filesRefLockTimeout::
+       The length of time, in milliseconds, to retry when trying to
+       lock an individual reference. Value 0 means not to retry at
+       all; -1 means to try indefinitely. Default is 100 (i.e.,
+       retry for 100ms).
+
 core.packedRefsTimeout::
        The length of time, in milliseconds, to retry when trying to
        lock the `packed-refs` file. Value 0 means not to retry at
@@ -1052,10 +1058,10 @@ clean.requireForce::
 
 color.branch::
        A boolean to enable/disable color in the output of
-       linkgit:git-branch[1]. May be set to `always`,
-       `false` (or `never`) or `auto` (or `true`), in which case colors are used
-       only when the output is to a terminal. If unset, then the
-       value of `color.ui` is used (`auto` by default).
+       linkgit:git-branch[1]. May be set to `false` (or `never`) to
+       disable color entirely, `auto` (or `true` or `always`) in which
+       case colors are used only when the output is to a terminal.  If
+       unset, then the value of `color.ui` is used (`auto` by default).
 
 color.branch.<slot>::
        Use customized color for branch coloration. `<slot>` is one of
@@ -1066,25 +1072,35 @@ color.branch.<slot>::
 
 color.diff::
        Whether to use ANSI escape sequences to add color to patches.
-       If this is set to `always`, linkgit:git-diff[1],
+       If this is set to `true` or `auto`, linkgit:git-diff[1],
        linkgit:git-log[1], and linkgit:git-show[1] will use color
-       for all patches.  If it is set to `true` or `auto`, those
-       commands will only use color when output is to the terminal.
-       If unset, then the value of `color.ui` is used (`auto` by
-       default).
+       when output is to the terminal. The value `always` is a
+       historical synonym for `auto`.  If unset, then the value of
+       `color.ui` is used (`auto` by default).
 +
 This does not affect linkgit:git-format-patch[1] or the
 'git-diff-{asterisk}' plumbing commands.  Can be overridden on the
 command line with the `--color[=<when>]` option.
 
+diff.colorMoved::
+       If set to either a valid `<mode>` or a true value, moved lines
+       in a diff are colored differently, for details of valid modes
+       see '--color-moved' in linkgit:git-diff[1]. If simply set to
+       true the default color mode will be used. When set to false,
+       moved lines are not colored.
+
 color.diff.<slot>::
        Use customized color for diff colorization.  `<slot>` specifies
        which part of the patch to use the specified color, and is one
        of `context` (context text - `plain` is a historical synonym),
        `meta` (metainformation), `frag`
        (hunk header), 'func' (function in hunk header), `old` (removed lines),
-       `new` (added lines), `commit` (commit headers), or `whitespace`
-       (highlighting whitespace errors).
+       `new` (added lines), `commit` (commit headers), `whitespace`
+       (highlighting whitespace errors), `oldMoved` (deleted lines),
+       `newMoved` (added lines), `oldMovedDimmed`, `oldMovedAlternative`,
+       `oldMovedAlternativeDimmed`, `newMovedDimmed`, `newMovedAlternative`
+       and `newMovedAlternativeDimmed` (See the '<mode>'
+       setting of '--color-moved' in linkgit:git-diff[1] for details).
 
 color.decorate.<slot>::
        Use customized color for 'git log --decorate' output.  `<slot>` is one
@@ -1124,12 +1140,12 @@ color.grep.<slot>::
 --
 
 color.interactive::
-       When set to `always`, always use colors for interactive prompts
+       When set to `true` or `auto`, use colors for interactive prompts
        and displays (such as those used by "git-add --interactive" and
-       "git-clean --interactive"). When false (or `never`), never.
-       When set to `true` or `auto`, use colors only when the output is
-       to the terminal. If unset, then the value of `color.ui` is
-       used (`auto` by default).
+       "git-clean --interactive") when the output is to the terminal.
+       When false (or `never`), never show colors. The value `always`
+       is a historical synonym for `auto`.  If unset, then the value of
+       `color.ui` is used (`auto` by default).
 
 color.interactive.<slot>::
        Use customized color for 'git add --interactive' and 'git clean
@@ -1176,10 +1192,10 @@ color.ui::
        configuration to set a default for the `--color` option.  Set it
        to `false` or `never` if you prefer Git commands not to use
        color unless enabled explicitly with some other configuration
-       or the `--color` option. Set it to `always` if you want all
-       output not intended for machine consumption to use color, to
-       `true` or `auto` (this is the default since Git 1.8.4) if you
-       want such output to use color when written to the terminal.
+       or the `--color` option. Set it to `true` or `auto` to enable
+       color when output is written to the terminal (this is also the
+       default since Git 1.8.4). The value `always` is a historical
+       synonym for `auto`.
 
 column.ui::
        Specify whether supported commands should output in columns.
@@ -1553,11 +1569,13 @@ gc.<pattern>.reflogExpireUnreachable::
 gc.rerereResolved::
        Records of conflicted merge you resolved earlier are
        kept for this many days when 'git rerere gc' is run.
+       You can also use more human-readable "1.month.ago", etc.
        The default is 60 days.  See linkgit:git-rerere[1].
 
 gc.rerereUnresolved::
        Records of conflicted merge you have not resolved are
        kept for this many days when 'git rerere gc' is run.
+       You can also use more human-readable "1.month.ago", etc.
        The default is 15 days.  See linkgit:git-rerere[1].
 
 gitcvs.commitMsgAnnotation::
@@ -3066,10 +3084,14 @@ submodule.<name>.url::
        See linkgit:git-submodule[1] and linkgit:gitmodules[5] for details.
 
 submodule.<name>.update::
-       The default update procedure for a submodule. This variable
-       is populated by `git submodule init` from the
-       linkgit:gitmodules[5] file. See description of 'update'
-       command in linkgit:git-submodule[1].
+       The method by which a submodule is updated by 'git submodule update',
+       which is the only affected command, others such as
+       'git checkout --recurse-submodules' are unaffected. It exists for
+       historical reasons, when 'git submodule' was the only command to
+       interact with submodules; settings like `submodule.active`
+       and `pull.rebase` are more specific. It is populated by
+       `git submodule init` from the linkgit:gitmodules[5] file.
+       See description of 'update' command in linkgit:git-submodule[1].
 
 submodule.<name>.branch::
        The remote branch name for a submodule, used by `git submodule
index 56dedafcd4bd68c4192fbe22469428ed274a46a0..a88c76741e781f5888fd467e17d02c633c87f15d 100644 (file)
@@ -231,6 +231,40 @@ ifdef::git-diff[]
 endif::git-diff[]
        It is the same as `--color=never`.
 
+--color-moved[=<mode>]::
+       Moved lines of code are colored differently.
+ifdef::git-diff[]
+       It can be changed by the `diff.colorMoved` configuration setting.
+endif::git-diff[]
+       The <mode> defaults to 'no' if the option is not given
+       and to 'zebra' if the option with no mode is given.
+       The mode must be one of:
++
+--
+no::
+       Moved lines are not highlighted.
+default::
+       Is a synonym for `zebra`. This may change to a more sensible mode
+       in the future.
+plain::
+       Any line that is added in one location and was removed
+       in another location will be colored with 'color.diff.newMoved'.
+       Similarly 'color.diff.oldMoved' will be used for removed lines
+       that are added somewhere else in the diff. This mode picks up any
+       moved line, but it is not very useful in a review to determine
+       if a block of code was moved without permutation.
+zebra::
+       Blocks of moved text of at least 20 alphanumeric characters
+       are detected greedily. The detected blocks are
+       painted using either the 'color.diff.{old,new}Moved' color or
+       'color.diff.{old,new}MovedAlternative'. The change between
+       the two colors indicates that a new block was detected.
+dimmed_zebra::
+       Similar to 'zebra', but additional dimming of uninteresting parts
+       of moved code is performed. The bordering lines of two adjacent
+       blocks are considered interesting, the rest is uninteresting.
+--
+
 --word-diff[=<mode>]::
        Show a word diff, using the <mode> to delimit changed words.
        By default, words are delimited by whitespace; see
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 631cbd840a08eb29add1e900ac9272992669a75c..4ebc3d32719dfefa988d34b41871f0e9fb969471 100644 (file)
@@ -66,7 +66,7 @@ OPTIONS
        disables it is in effect), make sure the patch is
        applicable to what the current index file records.  If
        the file to be patched in the working tree is not
-       up-to-date, it is flagged as an error.  This flag also
+       up to date, it is flagged as an error.  This flag also
        causes the index file to be updated.
 
 --cached::
@@ -259,7 +259,7 @@ treats these changes as follows.
 If `--index` is specified (explicitly or implicitly), then the submodule
 commits must match the index exactly for the patch to apply.  If any
 of the submodules are checked-out, then these check-outs are completely
-ignored, i.e., they are not required to be up-to-date or clean and they
+ignored, i.e., they are not required to be up to date or clean and they
 are not updated.
 
 If `--index` is not specified, then the submodule commits in the patch
index d0b33587717a198655164f5dd5e35e116a8a30e0..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.
@@ -92,10 +97,10 @@ OPTIONS
        all changes made to the branch ref, enabling use of date
        based sha1 expressions such as "<branchname>@\{yesterday}".
        Note that in non-bare repositories, reflogs are usually
-       enabled by default by the `core.logallrefupdates` config option.
+       enabled by default by the `core.logAllRefUpdates` config option.
        The negated form `--no-create-reflog` only overrides an earlier
        `--create-reflog`, but currently does not negate the setting of
-       `core.logallrefupdates`.
+       `core.logAllRefUpdates`.
 
 -f::
 --force::
@@ -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.
@@ -195,10 +207,8 @@ start-point is either a local or remote-tracking branch.
        branch.autoSetupMerge configuration variable is true.
 
 --set-upstream::
-       If specified branch does not exist yet or if `--force` has been
-       given, acts exactly like `--track`. Otherwise sets up configuration
-       like `--track` would when creating the branch, except that where
-       branch points to is not changed.
+       As this option had confusing syntax, it is no longer supported.
+       Please use `--track` or `--set-upstream-to` instead.
 
 -u <upstream>::
 --set-upstream-to=<upstream>::
index 204541c690ce24bdf5b6e20baa0ddca928920f71..fb09cd69d63fec03ad1987970fdbdffe8d8a4339 100644 (file)
@@ -192,7 +192,7 @@ newline. The available atoms are:
        The 40-hex object name of the object.
 
 `objecttype`::
-       The type of of the object (the same as `cat-file -t` reports).
+       The type of the object (the same as `cat-file -t` reports).
 
 `objectsize`::
        The size, in bytes, of the object (the same as `cat-file -s`
index d6399c0af86bb84cd86b82500ce706cbcd93cd93..bd268a8fccc32a1d218b30d58a688353a0fe236f 100644 (file)
@@ -38,7 +38,7 @@ $ git checkout -b <branch> --track <remote>/<branch>
 ------------
 +
 You could omit <branch>, in which case the command degenerates to
-"check out the current branch", which is a glorified no-op with a
+"check out the current branch", which is a glorified no-op with
 rather expensive side-effects to show only the tracking information,
 if exists, for the current branch.
 
index a336ae5f6fd5587aa8f778d2378783d0fe2e6dc4..ba90066f108cf525eb7781c1607d65358c617b0e 100644 (file)
@@ -223,7 +223,7 @@ access method and requested operation.
 That means that even if you offer only read access (e.g. by using
 the pserver method), 'git-cvsserver' should have write access to
 the database to work reliably (otherwise you need to make sure
-that the database is up-to-date any time 'git-cvsserver' is executed).
+that the database is up to date any time 'git-cvsserver' is executed).
 
 By default it uses SQLite databases in the Git directory, named
 `gitcvs.<module_name>.sqlite`. Note that the SQLite backend creates
index 26f19d3b072aa358043fbf79f3057a11aee2fdb2..c924c945ba8b79e578ddbb7598aa29fabcdf5887 100644 (file)
@@ -87,19 +87,23 @@ OPTIONS
 
 --match <pattern>::
        Only consider tags matching the given `glob(7)` pattern,
-       excluding the "refs/tags/" prefix.  This can be used to avoid
-       leaking private tags from the repository. If given multiple times, a
-       list of patterns will be accumulated, and tags matching any of the
-       patterns will be considered. Use `--no-match` to clear and reset the
-       list of patterns.
+       excluding the "refs/tags/" prefix. If used with `--all`, it also
+       considers local branches and remote-tracking references matching the
+       pattern, excluding respectively "refs/heads/" and "refs/remotes/"
+       prefix; references of other types are never considered. If given
+       multiple times, a list of patterns will be accumulated, and tags
+       matching any of the patterns will be considered.  Use `--no-match` to
+       clear and reset the list of patterns.
 
 --exclude <pattern>::
        Do not consider tags matching the given `glob(7)` pattern, excluding
-       the "refs/tags/" prefix. This can be used to narrow the tag space and
-       find only tags matching some meaningful criteria. If given multiple
-       times, a list of patterns will be accumulated and tags matching any
-       of the patterns will be excluded. When combined with --match a tag will
-       be considered when it matches at least one --match pattern and does not
+       the "refs/tags/" prefix. If used with `--all`, it also does not consider
+       local branches and remote-tracking references matching the pattern,
+       excluding respectively "refs/heads/" and "refs/remotes/" prefix;
+       references of other types are never considered. If given multiple times,
+       a list of patterns will be accumulated and tags matching any of the
+       patterns will be excluded. When combined with --match a tag will be
+       considered when it matches at least one --match pattern and does not
        match any of the --exclude patterns. Use `--no-exclude` to clear and
        reset the list of patterns.
 
index a1715069529eba9d143d527c058e962381efd743..b380677718ae129b4643814d7c1a16351ce4d779 100644 (file)
@@ -85,7 +85,7 @@ a 'git write-tree' + 'git diff-tree'. Thus that's the default mode.
 The non-cached version asks the question:
 
   show me the differences between HEAD and the currently checked out
-  tree - index contents _and_ files that aren't up-to-date
+  tree - index contents _and_ files that aren't up to date
 
 which is obviously a very useful question too, since that tells you what
 you *could* commit. Again, the output matches the 'git diff-tree -r'
@@ -100,8 +100,8 @@ have not actually done a 'git update-index' on it yet - there is no
   torvalds@ppc970:~/v2.6/linux> git diff-index --abbrev HEAD
   :100644 100664 7476bb... 000000...      kernel/sched.c
 
-i.e., it shows that the tree has changed, and that `kernel/sched.c` has is
-not up-to-date and may contain new stuff. The all-zero sha1 means that to
+i.e., it shows that the tree has changed, and that `kernel/sched.c` is
+not up to date and may contain new stuff. The all-zero sha1 means that to
 get the real diff, you need to look at the object in the working directory
 directly rather than do an object-to-object diff.
 
index 9e5169aa64f4ffd5ea42f85b2dbe812086e9652c..bebdcdec5ad4775588abe8814c59a269c7c1a832 100644 (file)
@@ -14,7 +14,7 @@ SYNOPSIS
        [--commit-filter <command>] [--tag-name-filter <command>]
        [--subdirectory-filter <directory>] [--prune-empty]
        [--original <namespace>] [-d <directory>] [-f | --force]
-       [--] [<rev-list options>...]
+       [--state-branch <branch>] [--] [<rev-list options>...]
 
 DESCRIPTION
 -----------
@@ -198,6 +198,12 @@ to other tags will be rewritten to point to the underlying commit.
        directory or when there are already refs starting with
        'refs/original/', unless forced.
 
+--state-branch <branch>::
+       This option will cause the mapping from old to new objects to
+       be loaded from named branch upon startup and saved as a new
+       commit to that branch upon exit, enabling incremental of large
+       trees. If '<branch>' does not exist it will be created.
+
 <rev-list options>...::
        Arguments for 'git rev-list'.  All positive refs included by
        these options are rewritten.  You may also specify options
index bb370c9c7b91425ad939383d8b28c5fbbb73cbac..1d420e4cde8230de00aae583a296128fdd59140f 100644 (file)
@@ -10,8 +10,9 @@ SYNOPSIS
 [verse]
 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
                   [(--sort=<key>)...] [--format=<format>] [<pattern>...]
-                  [--points-at <object>] [(--merged | --no-merged) [<object>]]
-                  [--contains [<object>]] [--no-contains [<object>]]
+                  [--points-at=<object>]
+                  (--merged[=<object>] | --no-merged[=<object>])
+                  [--contains[=<object>]] [--no-contains[=<object>]]
 
 DESCRIPTION
 -----------
@@ -25,19 +26,25 @@ host language allowing their direct evaluation in that language.
 
 OPTIONS
 -------
-<count>::
+<pattern>...::
+       If one or more patterns are given, only refs are shown that
+       match against at least one pattern, either using fnmatch(3) or
+       literally, in the latter case matching completely or from the
+       beginning up to a slash.
+
+--count=<count>::
        By default the command shows all refs that match
        `<pattern>`.  This option makes it stop after showing
        that many refs.
 
-<key>::
+--sort=<key>::
        A field name to sort on.  Prefix `-` to sort in
        descending order of the value.  When unspecified,
        `refname` is used.  You may use the --sort=<key> option
        multiple times, in which case the last key becomes the primary
        key.
 
-<format>::
+--format=<format>::
        A string that interpolates `%(fieldname)` from a ref being shown
        and the object it points at.  If `fieldname`
        is prefixed with an asterisk (`*`) and the ref points
@@ -50,11 +57,10 @@ OPTIONS
        `xx`; for example `%00` interpolates to `\0` (NUL),
        `%09` to `\t` (TAB) and `%0a` to `\n` (LF).
 
-<pattern>...::
-       If one or more patterns are given, only refs are shown that
-       match against at least one pattern, either using fnmatch(3) or
-       literally, in the latter case matching completely or from the
-       beginning up to a slash.
+--color[=<when>]:
+       Respect any colors specified in the `--format` option. The
+       `<when>` field must be one of `always`, `never`, or `auto` (if
+       `<when>` is absent, behave as if `always` was given).
 
 --shell::
 --perl::
@@ -65,24 +71,24 @@ OPTIONS
        the specified host language.  This is meant to produce
        a scriptlet that can directly be `eval`ed.
 
---points-at <object>::
+--points-at=<object>::
        Only list refs which points at the given object.
 
---merged [<object>]::
+--merged[=<object>]::
        Only list refs whose tips are reachable from the
        specified commit (HEAD if not specified),
        incompatible with `--no-merged`.
 
---no-merged [<object>]::
+--no-merged[=<object>]::
        Only list refs whose tips are not reachable from the
        specified commit (HEAD if not specified),
        incompatible with `--merged`.
 
---contains [<object>]::
+--contains[=<object>]::
        Only list refs which contain the specified commit (HEAD if not
        specified).
 
---no-contains [<object>]::
+--no-contains[=<object>]::
        Only list refs which don't contain the specified commit (HEAD
        if not specified).
 
@@ -212,11 +218,15 @@ and `date` to extract the named component.
 The complete message in a commit and tag object is `contents`.
 Its first line is `contents:subject`, where subject is the concatenation
 of all lines of the commit message up to the first blank line.  The next
-line is 'contents:body', where body is all of the lines after the first
+line is `contents:body`, where body is all of the lines after the first
 blank line.  The optional GPG signature is `contents:signature`.  The
 first `N` lines of the message is obtained using `contents:lines=N`.
 Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
-are obtained as 'contents:trailers'.
+are obtained as `trailers` (or by using the historical alias
+`contents:trailers`).  Non-trailer lines from the trailer block can be omitted
+with `trailers:only`. Whitespace-continuations can be removed from trailers so
+that each trailer appears on a line by itself with its full content with
+`trailers:unfold`. Both can be used together as `trailers:unfold,only`.
 
 For sorting purposes, fields with numeric values sort in numeric order
 (`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
index c890328b02ec4c3a28aaeb8aa057d327c75b9967..6cbe462a77467b05561938ff9cf8e9dcebd42efe 100644 (file)
@@ -23,6 +23,7 @@ SYNOPSIS
                   [(--reroll-count|-v) <n>]
                   [--to=<email>] [--cc=<email>]
                   [--[no-]cover-letter] [--quiet] [--notes[=<ref>]]
+                  [--progress]
                   [<common diff options>]
                   [ <since> | <revision range> ]
 
@@ -283,6 +284,9 @@ you can use `--suffix=-patch` to get `0001-description-of-my-change-patch`.
        range are always formatted as creation patches, independently
        of this flag.
 
+--progress::
+       Show progress reports on stderr as patches are generated.
+
 CONFIGURATION
 -------------
 You can specify extra mail header lines to be added to each message,
index 5033483db496286910dd24507f3374e18ff6c2b5..18b494731f51145e9d4a4d078264bedfd3beb00f 100644 (file)
@@ -95,13 +95,6 @@ OPTIONS
        <tree> option the prefix of all submodule output will be the name of
        the parent project's <tree> object.
 
---parent-basename <basename>::
-       For internal use only.  In order to produce uniform output with the
-       --recurse-submodules option, this option can be used to provide the
-       basename of a parent's <tree> object to a submodule so the submodule
-       can prefix its output with the parent's name rather than the SHA1 of
-       the submodule.
-
 -a::
 --text::
        Process binary files as if they were text.
@@ -296,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
 --------
@@ -312,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 31cdeaecdfde8fb358c35c3a9a2e3c4279d440d0..9dd19a1dd9f126fbf54f1dc628b30fbe05445cf3 100644 (file)
@@ -3,24 +3,27 @@ git-interpret-trailers(1)
 
 NAME
 ----
-git-interpret-trailers - help add structured information into commit messages
+git-interpret-trailers - add or parse structured information in commit messages
 
 SYNOPSIS
 --------
 [verse]
-'git interpret-trailers' [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]
+'git interpret-trailers' [options] [(--trailer <token>[(=|:)<value>])...] [<file>...]
+'git interpret-trailers' [options] [--parse] [<file>...]
 
 DESCRIPTION
 -----------
-Help adding 'trailers' lines, that look similar to RFC 822 e-mail
+Help parsing or adding 'trailers' lines, that look similar to RFC 822 e-mail
 headers, at the end of the otherwise free-form part of a commit
 message.
 
 This command reads some patches or commit messages from either the
-<file> arguments or the standard input if no <file> is specified. Then
-this command applies the arguments passed using the `--trailer`
-option, if any, to the commit message part of each input file. The
-result is emitted on the standard output.
+<file> arguments or the standard input if no <file> is specified. If
+`--parse` is specified, the output consists of the parsed trailers.
+
+Otherwise, this command applies the arguments passed using the
+`--trailer` option, if any, to the commit message part of each input
+file. The result is emitted on the standard output.
 
 Some configuration variables control the way the `--trailer` arguments
 are applied to each commit message and the way any existing trailer in
@@ -80,6 +83,45 @@ OPTIONS
        trailer to the input messages. See the description of this
        command.
 
+--where <placement>::
+--no-where::
+       Specify where all new trailers will be added.  A setting
+       provided with '--where' overrides all configuration variables
+       and applies to all '--trailer' options until the next occurrence of
+       '--where' or '--no-where'.
+
+--if-exists <action>::
+--no-if-exists::
+       Specify what action will be performed when there is already at
+       least one trailer with the same <token> in the message.  A setting
+       provided with '--if-exists' overrides all configuration variables
+       and applies to all '--trailer' options until the next occurrence of
+       '--if-exists' or '--no-if-exists'.
+
+--if-missing <action>::
+--no-if-missing::
+       Specify what action will be performed when there is no other
+       trailer with the same <token> in the message.  A setting
+       provided with '--if-missing' overrides all configuration variables
+       and applies to all '--trailer' options until the next occurrence of
+       '--if-missing' or '--no-if-missing'.
+
+--only-trailers::
+       Output only the trailers, not any other parts of the input.
+
+--only-input::
+       Output only trailers that exist in the input; do not add any
+       from the command-line or by following configured `trailer.*`
+       rules.
+
+--unfold::
+       Remove any whitespace-continuation in trailers, so that each
+       trailer appears on a line by itself with its full content.
+
+--parse::
+       A convenience alias for `--only-trailers --only-input
+       --unfold`.
+
 CONFIGURATION VARIABLES
 -----------------------
 
@@ -170,8 +212,8 @@ trailer.<token>.where::
        configuration variable and it overrides what is specified by
        that option for trailers with the specified <token>.
 
-trailer.<token>.ifexist::
-       This option takes the same values as the 'trailer.ifexist'
+trailer.<token>.ifexists::
+       This option takes the same values as the 'trailer.ifexists'
        configuration variable and it overrides what is specified by
        that option for trailers with the specified <token>.
 
index f90faf7aaa250b2fea09d611e4efac31968ce6db..4df6431c341a466f2cb9fef094607751b58c008d 100644 (file)
@@ -64,6 +64,14 @@ OPTIONS
 -------
 include::merge-options.txt[]
 
+--signoff::
+       Add Signed-off-by line by the committer at the end of the commit
+       log message.  The meaning of a signoff depends on the project,
+       but it typically certifies that committer has
+       the rights to submit this work under the same license and
+       agrees to a Developer Certificate of Origin
+       (see http://developercertificate.org/ for more information).
+
 -S[<keyid>]::
 --gpg-sign[=<keyid>]::
        GPG-sign the resulting merge commit. The `keyid` argument is
@@ -133,7 +141,7 @@ exception is when the changed index entries are in the state that
 would result from the merge already.)
 
 If all named commits are already ancestors of `HEAD`, 'git merge'
-will exit early with the message "Already up-to-date."
+will exit early with the message "Already up to date."
 
 FAST-FORWARD MERGE
 ------------------
index be7db3048d4776200c6ed8781b25ffccb68a41eb..43677297f3b85814c18c972a159b81c8076bc445 100644 (file)
@@ -171,7 +171,7 @@ OPTIONS
        object that does not have notes attached to it.
 
 --stdin::
-       Also read the object names to remove notes from from the standard
+       Also read the object names to remove notes from the standard
        input (there is no reason you cannot combine this with object
        names from the command line).
 
index 0a639664fd67f497b1597cf903015a284947509d..3e76e99f38f67a530d43e2da3b3129c2a99e3366 100644 (file)
@@ -12,7 +12,7 @@ SYNOPSIS
 'git push' [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
           [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
           [-u | --set-upstream] [--push-option=<string>]
-          [--[no-]signed|--sign=(true|false|if-asked)]
+          [--[no-]signed|--signed=(true|false|if-asked)]
           [--force-with-lease[=<refname>[:<expect>]]]
           [--no-verify] [<repository> [<refspec>...]]
 
@@ -141,7 +141,7 @@ already exists on the remote side.
        information, see `push.followTags` in linkgit:git-config[1].
 
 --[no-]signed::
---sign=(true|false|if-asked)::
+--signed=(true|false|if-asked)::
        GPG-sign the push request to update refs on the receiving
        side, to allow it to be checked by the hooks and/or be
        logged.  If `false` or `--no-signed`, no signing will be
index 02576d8c0ae6008fc22d7c7c706909efde5856da..72bd809fb8d59812d79e402f6b9608d09f0d52ec 100644 (file)
@@ -179,6 +179,7 @@ Here are the "carry forward" rules, where "I" denotes the index,
 "clean" means that index and work tree coincide, and "exists"/"nothing"
 refer to the presence of a path in the specified commit:
 
+....
        I                   H        M        Result
        -------------------------------------------------------
      0  nothing             nothing  nothing  (does not happen)
@@ -217,6 +218,7 @@ refer to the presence of a path in the specified commit:
      19 no    no    yes     exists   exists   keep index
      20 yes   yes   no      exists   exists   use M
      21 no    yes   no      exists   exists   fail
+....
 
 In all "keep index" cases, the index entry stays as in the
 original index file.  If the entry is not up to date,
index 4f6bed61a92b74e4540b71e0e0cac7cba60302ca..3cedfb0fd22be1c060ab2e7b9a141a0d1deb03af 100644 (file)
@@ -334,7 +334,7 @@ which makes little sense.
 
 -f::
 --force-rebase::
-       Force a rebase even if the current branch is up-to-date and
+       Force a rebase even if the current branch is up to date and
        the command without `--force` would return without doing anything.
 +
 You may find this (or --no-ff with an interactive rebase) helpful after
@@ -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 9ee083c415a27dc499b75031d49df9e3f3856ec0..031f31fa471154f474b35448564aff6c8c2b3f51 100644 (file)
@@ -205,7 +205,7 @@ development on the topic branch:
 ------------
 
 you could run `git rebase master topic`, to bring yourself
-up-to-date before your topic is ready to be sent upstream.
+up to date before your topic is ready to be sent upstream.
 This would result in falling back to a three-way merge, and it
 would conflict the same way as the test merge you resolved earlier.
 'git rerere' will be run by 'git rebase' to help you resolve this
index b1293f24bb46f71238f3dee4700340a79b90fca8..0917b8207b9d6e6c88dc611cf40a81ab17481a2e 100644 (file)
@@ -235,6 +235,9 @@ print a message to stderr and exit with nonzero status.
 --is-bare-repository::
        When the repository is bare print "true", otherwise "false".
 
+--is-shallow-repository::
+       When the repository is shallow print "true", otherwise "false".
+
 --resolve-git-dir <path>::
        Check if <path> is a valid repository or a gitfile that
        points at a valid repository, and print the location of the
index 683e59133003536c776c476304e369bdd80e27a2..b5c46223c44b1b17673ae4782f74528b3d151bed 100644 (file)
@@ -146,7 +146,7 @@ the submodule's history. If it exists the submodule.<name> section
 in the linkgit:gitmodules[5] file will also be removed and that file
 will be staged (unless --cached or -n are used).
 
-A submodule is considered up-to-date when the HEAD is the same as
+A submodule is considered up to date when the HEAD is the same as
 recorded in the index, no tracked files are modified and no untracked
 files that aren't ignored are present in the submodules work tree.
 Ignored files are deemed expendable and won't stop a submodule's work
index 966abb0df807c79714a18d8b106390caae45cf89..f51c64939b48b7b082752a294b17aee6d92c4fa0 100644 (file)
@@ -11,7 +11,7 @@ SYNOPSIS
 [verse]
 'git send-pack' [--all] [--dry-run] [--force] [--receive-pack=<git-receive-pack>]
                [--verbose] [--thin] [--atomic]
-               [--[no-]signed|--sign=(true|false|if-asked)]
+               [--[no-]signed|--signed=(true|false|if-asked)]
                [<host>:]<directory> [<ref>...]
 
 DESCRIPTION
@@ -71,7 +71,7 @@ be in a separate packet, and the list must end with a flush packet.
        refs.
 
 --[no-]signed::
---sign=(true|false|if-asked)::
+--signed=(true|false|if-asked)::
        GPG-sign the push request to update refs on the receiving
        side, to allow it to be checked by the hooks and/or be
        logged.  If `false` or `--no-signed`, no signing will be
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 aa2aeabb60209fabd772907a91c2812b2a305d7d..636e09048e8846b813166737ef58eef96eb3a3c4 100644 (file)
@@ -424,7 +424,7 @@ Any other arguments are passed directly to 'git log'
 'set-tree'::
        You should consider using 'dcommit' instead of this command.
        Commit specified commit or tree objects to SVN.  This relies on
-       your imported fetch data being up-to-date.  This makes
+       your imported fetch data being up to date.  This makes
        absolutely no attempts to do patching when committing to SVN, it
        simply overwrites files with those specified in the tree or
        commit.  All merging is assumed to have taken place
index 543fb425ee7c1d4c09a5ad4afbd00b9b812e4871..956fc019f984bca1754a72dc0d7308b39a28445d 100644 (file)
@@ -115,6 +115,11 @@ options for details.
        variable if it exists, or lexicographic order otherwise. See
        linkgit:git-config[1].
 
+--color[=<when>]:
+       Respect any colors specified in the `--format` option. The
+       `<when>` field must be one of `always`, `never`, or `auto` (if
+       `<when>` is absent, behave as if `always` was given).
+
 -i::
 --ignore-case::
        Sorting and filtering tags are case insensitive.
@@ -174,7 +179,7 @@ This option is only applicable when listing tags without annotation lines.
        `core.logAllRefUpdates` in linkgit:git-config[1].
        The negated form `--no-create-reflog` only overrides an earlier
        `--create-reflog`, but currently does not negate the setting of
-       `core.logallrefupdates`.
+       `core.logAllRefUpdates`.
 
 <tagname>::
        The name of the tag to create, delete, or describe.
index 1579abf3c3b45f4e63f8f17746dde31a06a3a564..75c7dd9dea4d2b6c26b84b99fb21ba75cf1cdcf3 100644 (file)
@@ -153,7 +153,7 @@ you will need to handle the situation manually.
 +
 Version 4 performs a simple pathname compression that reduces index
 size by 30%-50% on large repositories, which results in faster load
-time. Version 4 is relatively young (first released in in 1.8.0 in
+time. Version 4 is relatively young (first released in 1.8.0 in
 October 2012). Other Git implementations such as JGit and libgit2
 may not support it yet.
 
@@ -214,7 +214,7 @@ will remove the intended effect of the option.
 Using --refresh
 ---------------
 `--refresh` does not calculate a new sha1 file or bring the index
-up-to-date for mode/content changes. But what it *does* do is to
+up to date for mode/content changes. But what it *does* do is to
 "re-match" the stat information of a file with the index, so that you
 can refresh the index for a file that hasn't been changed but where
 the stat entry is out of date.
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 c4f2be2542d1fcd417c69596dafdf89bc3a93f00..4c68bc19d5108d1ba0c91b05d35acf8060eb2c9d 100644 (file)
@@ -151,7 +151,10 @@ unspecified.
 
 This attribute sets a specific line-ending style to be used in the
 working directory.  It enables end-of-line conversion without any
-content checks, effectively setting the `text` attribute.
+content checks, effectively setting the `text` attribute.  Note that
+setting this attribute on paths which are in the index with CRLF line
+endings may make the paths to be considered dirty.  Adding the path to
+the index again will normalize the line endings in the index.
 
 Set to string value "crlf"::
 
index 7577f27ec20c90eabea2652a438ca09961559f6a..e29a9effccbcf439fec8a8fe9bfd1927f58439d7 100644 (file)
@@ -631,7 +631,7 @@ So after you do a `cp -a` to create a new copy, you'll want to do
 $ git update-index --refresh
 ----------------
 +
-in the new repository to make sure that the index file is up-to-date.
+in the new repository to make sure that the index file is up to date.
 
 Note that the second point is true even across machines. You can
 duplicate a remote Git repository with *any* regular copy mechanism, be it
@@ -701,7 +701,7 @@ $ git checkout-index -u -a
 ----------------
 
 where the `-u` flag means that you want the checkout to keep the index
-up-to-date (so that you don't have to refresh it afterward), and the
+up to date (so that you don't have to refresh it afterward), and the
 `-a` flag means "check out all files" (if you have a stale copy or an
 older version of a checked out tree you may also need to add the `-f`
 flag first, to tell 'git checkout-index' to *force* overwriting of any old
@@ -1283,7 +1283,7 @@ run a single command, 'git-receive-pack'.
 
 First, you need to create an empty repository on the remote
 machine that will house your public repository. This empty
-repository will be populated and be kept up-to-date by pushing
+repository will be populated and be kept up to date by pushing
 into it later. Obviously, this repository creation needs to be
 done only once.
 
@@ -1450,7 +1450,7 @@ transport protocols (HTTP), you need to keep this repository
 would contain a call to 'git update-server-info'
 but you need to manually enable the hook with
 `mv post-update.sample post-update`.  This makes sure
-'git update-server-info' keeps the necessary files up-to-date.
+'git update-server-info' keeps the necessary files up to date.
 
 3. Push into the public repository from your primary
    repository.
index b2514f4d4426a730e65bcae264a6364eb50bcd60..5d3f45560ea9c29ec382c8772522f06ab5f81f73 100644 (file)
@@ -121,17 +121,16 @@ it is not suppressed by the `--no-verify` option.  A non-zero exit
 means a failure of the hook and aborts the commit.  It should not
 be used as replacement for pre-commit hook.
 
-The sample `prepare-commit-msg` hook that comes with Git comments
-out the `Conflicts:` part of a merge's commit message.
+The sample `prepare-commit-msg` hook that comes with Git removes the
+help message found in the commented portion of the commit template.
 
 commit-msg
 ~~~~~~~~~~
 
-This hook is invoked by 'git commit', and can be bypassed
-with the `--no-verify` option.  It takes a single parameter, the
-name of the file that holds the proposed commit log message.
-Exiting with a non-zero status causes the 'git commit' to
-abort.
+This hook is invoked by 'git commit' and 'git merge', and can be
+bypassed with the `--no-verify` option.  It takes a single parameter,
+the name of the file that holds the proposed commit log message.
+Exiting with a non-zero status causes the command to abort.
 
 The hook is allowed to edit the message file in place, and can be used
 to normalize the message into some project standard format. It
@@ -369,7 +368,7 @@ them.
 
 When enabled, the default 'post-update' hook runs
 'git update-server-info' to keep the information used by dumb
-transports (e.g., HTTP) up-to-date.  If you are publishing
+transports (e.g., HTTP) up to date.  If you are publishing
 a Git repository that is accessible via HTTP, you should
 probably enable this hook.
 
index f51ed4e37cd3fad80bf855e6217906f094f894fb..adf9554ad2f84cb69a6b0a940e4d3e0e336df9ff 100644 (file)
@@ -71,7 +71,7 @@ objects/info/packs::
        This file is to help dumb transports discover what packs
        are available in this object store.  Whenever a pack is
        added or removed, `git update-server-info` should be run
-       to keep this file up-to-date if the repository is
+       to keep this file up to date if the repository is
        published for dumb transports.  'git repack' does this
        by default.
 
index 794b83393ea21a8d569c34791ee4af1dfb99a57a..242de31cb6ccc6600b94a68a9efd215df2cb71f7 100644 (file)
@@ -109,7 +109,7 @@ summary of the situation with 'git status':
 $ git status
 On branch master
 Changes to be committed:
-Your branch is up-to-date with 'origin/master'.
+Your branch is up to date with 'origin/master'.
   (use "git reset HEAD <file>..." to unstage)
 
        modified:   file1
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.
index 5b4a62e93624bc8289c835631aab5d5f13dff708..4e323043017deee83d547e52e7797dd61a02cd5d 100644 (file)
@@ -39,7 +39,7 @@ set to `no` at the beginning of them.
 
 --ff-only::
        Refuse to merge and exit with a non-zero status unless the
-       current `HEAD` is already up-to-date or the merge can be
+       current `HEAD` is already up to date or the merge can be
        resolved as a fast-forward.
 
 --log[=<n>]::
index 2eb92b93274df9fb5002336114654fe869903808..a09d5974639fa772ba86c5547fe70716fd044f73 100644 (file)
@@ -39,7 +39,8 @@ even look at what the other tree contains at all.  It discards everything
 the other tree did, declaring 'our' history contains all that happened in it.
 
 theirs;;
-       This is the opposite of 'ours'.
+       This is the opposite of 'ours'; note that, unlike 'ours', there is
+       no 'theirs' merge stragegy to confuse this merge option with.
 
 patience;;
        With this option, 'merge-recursive' spends a little extra time
index 973d19606b63c314786de4965160b2c285987947..d433d50f8104dd5c86e49c844047208fe455b6a0 100644 (file)
@@ -205,7 +205,10 @@ endif::git-rev-list[]
 - '%><(<N>)', '%><|(<N>)': similar to '% <(<N>)', '%<|(<N>)'
   respectively, but padding both sides (i.e. the text is centered)
 - %(trailers): display the trailers of the body as interpreted by
-  linkgit:git-interpret-trailers[1]
+  linkgit:git-interpret-trailers[1]. If the `:only` option is given,
+  omit non-trailer lines from the trailer block.  If the `:unfold`
+  option is given, behave as if interpret-trailer's `--unfold` option
+  was given. E.g., `%(trailers:only:unfold)` to do both.
 
 NOTE: Some placeholders may depend on other options given to the
 revision traversal engine. For example, the `%g*` reflog options will
index a6cf9eb380fabedd0e89b84d182e6e2c4798e503..7d860bfca1442e7957c41b7a7a5cd6f82141ed7e 100644 (file)
@@ -184,6 +184,14 @@ explicitly.
        Pretend as if all objects mentioned by reflogs are listed on the
        command line as `<commit>`.
 
+--single-worktree::
+       By default, all working trees will be examined by the
+       following options when there are more than one (see
+       linkgit:git-worktree[1]): `--all`, `--reflog` and
+       `--indexed-objects`.
+       This option forces them to examine the current working tree
+       only.
+
 --ignore-missing::
        Upon seeing an invalid object name in the input, pretend as if
        the bad input was not given.
index cfc063018c996a61b333b38d7b387f40b21e7a00..870c8edbfb1d8ac538efee52dd321bdc48937477 100644 (file)
@@ -8,7 +8,7 @@ always NULL-terminated at the element pointed to by `argv[argc]`. This
 makes the result suitable for passing to functions expecting to receive
 argv from main(), or the link:api-run-command.html[run-command API].
 
-The link:api-string-list.html[string-list API] is similar, but cannot be
+The string-list API (documented in string-list.h) is similar, but cannot be
 used for these purposes; instead of storing a straight string pointer,
 it contains an item structure with a `util` field that is not compatible
 with the traditional argv interface.
index 20741f345e459f41d357039496f7d6be30edab06..9a778b0cad02faab3ce6bd7f89c24a17de45b777 100644 (file)
@@ -186,7 +186,7 @@ parsing is successful, the return value is the result.
 Same as `git_config_bool`, except that integers are returned as-is, and
 an `is_bool` flag is unset.
 
-`git_config_maybe_bool`::
+`git_parse_maybe_bool`::
 Same as `git_config_bool`, except that it returns -1 on error rather
 than dying.
 
index 37379d83376fc2342d1a81570b3de86b8e7dcd49..46c3d5c3550a1ba7c83b6937fe68538c3c12074e 100644 (file)
@@ -32,11 +32,8 @@ Iteration functions
 
 * `for_each_glob_ref_in()` the previous and `for_each_ref_in()` combined.
 
-* `head_ref_submodule()`, `for_each_ref_submodule()`,
-  `for_each_ref_in_submodule()`, `for_each_tag_ref_submodule()`,
-  `for_each_branch_ref_submodule()`, `for_each_remote_ref_submodule()`
-  do the same as the functions described above but for a specified
-  submodule.
+* Use `refs_` API for accessing submodules. The submodule ref store could
+  be obtained with `get_submodule_ref_store()`.
 
 * `for_each_rawref()` can be used to learn about broken ref and symref.
 
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 14af37c3f14854e87d9d28f60b5ff72eb189c37f..bde18622a87404fc258c60ecb87c43bfeb047f0c 100644 (file)
@@ -55,9 +55,9 @@ Initializing
 
 `fill_tree_descriptor`::
 
-       Initialize a `tree_desc` and decode its first entry given the sha1 of
-       a tree. Returns the `buffer` member if the sha1 is a valid tree
-       identifier and NULL otherwise.
+       Initialize a `tree_desc` and decode its first entry given the
+       object ID of a tree. Returns the `buffer` member if the latter
+       is a valid tree identifier and NULL otherwise.
 
 `setup_traverse_info`::
 
diff --git a/Documentation/technical/hash-function-transition.txt b/Documentation/technical/hash-function-transition.txt
new file mode 100644 (file)
index 0000000..417ba49
--- /dev/null
@@ -0,0 +1,797 @@
+Git hash function transition
+============================
+
+Objective
+---------
+Migrate Git from SHA-1 to a stronger hash function.
+
+Background
+----------
+At its core, the Git version control system is a content addressable
+filesystem. It uses the SHA-1 hash function to name content. For
+example, files, directories, and revisions are referred to by hash
+values unlike in other traditional version control systems where files
+or versions are referred to via sequential numbers. The use of a hash
+function to address its content delivers a few advantages:
+
+* Integrity checking is easy. Bit flips, for example, are easily
+  detected, as the hash of corrupted content does not match its name.
+* Lookup of objects is fast.
+
+Using a cryptographically secure hash function brings additional
+advantages:
+
+* Object names can be signed and third parties can trust the hash to
+  address the signed object and all objects it references.
+* Communication using Git protocol and out of band communication
+  methods have a short reliable string that can be used to reliably
+  address stored content.
+
+Over time some flaws in SHA-1 have been discovered by security
+researchers. https://shattered.io demonstrated a practical SHA-1 hash
+collision. As a result, SHA-1 cannot be considered cryptographically
+secure any more. This impacts the communication of hash values because
+we cannot trust that a given hash value represents the known good
+version of content that the speaker intended.
+
+SHA-1 still possesses the other properties such as fast object lookup
+and safe error checking, but other hash functions are equally suitable
+that are believed to be cryptographically secure.
+
+Goals
+-----
+Where NewHash is a strong 256-bit hash function to replace SHA-1 (see
+"Selection of a New Hash", below):
+
+1. The transition to NewHash can be done one local repository at a time.
+   a. Requiring no action by any other party.
+   b. A NewHash repository can communicate with SHA-1 Git servers
+      (push/fetch).
+   c. Users can use SHA-1 and NewHash identifiers for objects
+      interchangeably (see "Object names on the command line", below).
+   d. New signed objects make use of a stronger hash function than
+      SHA-1 for their security guarantees.
+2. Allow a complete transition away from SHA-1.
+   a. Local metadata for SHA-1 compatibility can be removed from a
+      repository if compatibility with SHA-1 is no longer needed.
+3. Maintainability throughout the process.
+   a. The object format is kept simple and consistent.
+   b. Creation of a generalized repository conversion tool.
+
+Non-Goals
+---------
+1. Add NewHash support to Git protocol. This is valuable and the
+   logical next step but it is out of scope for this initial design.
+2. Transparently improving the security of existing SHA-1 signed
+   objects.
+3. Intermixing objects using multiple hash functions in a single
+   repository.
+4. Taking the opportunity to fix other bugs in Git's formats and
+   protocols.
+5. Shallow clones and fetches into a NewHash repository. (This will
+   change when we add NewHash support to Git protocol.)
+6. Skip fetching some submodules of a project into a NewHash
+   repository. (This also depends on NewHash support in Git
+   protocol.)
+
+Overview
+--------
+We introduce a new repository format extension. Repositories with this
+extension enabled use NewHash instead of SHA-1 to name their objects.
+This affects both object names and object content --- both the names
+of objects and all references to other objects within an object are
+switched to the new hash function.
+
+NewHash repositories cannot be read by older versions of Git.
+
+Alongside the packfile, a NewHash repository stores a bidirectional
+mapping between NewHash and SHA-1 object names. The mapping is generated
+locally and can be verified using "git fsck". Object lookups use this
+mapping to allow naming objects using either their SHA-1 and NewHash names
+interchangeably.
+
+"git cat-file" and "git hash-object" gain options to display an object
+in its sha1 form and write an object given its sha1 form. This
+requires all objects referenced by that object to be present in the
+object database so that they can be named using the appropriate name
+(using the bidirectional hash mapping).
+
+Fetches from a SHA-1 based server convert the fetched objects into
+NewHash form and record the mapping in the bidirectional mapping table
+(see below for details). Pushes to a SHA-1 based server convert the
+objects being pushed into sha1 form so the server does not have to be
+aware of the hash function the client is using.
+
+Detailed Design
+---------------
+Repository format extension
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+A NewHash repository uses repository format version `1` (see
+Documentation/technical/repository-version.txt) with extensions
+`objectFormat` and `compatObjectFormat`:
+
+       [core]
+               repositoryFormatVersion = 1
+       [extensions]
+               objectFormat = newhash
+               compatObjectFormat = sha1
+
+Specifying a repository format extension ensures that versions of Git
+not aware of NewHash do not try to operate on these repositories,
+instead producing an error message:
+
+       $ git status
+       fatal: unknown repository extensions found:
+               objectformat
+               compatobjectformat
+
+See the "Transition plan" section below for more details on these
+repository extensions.
+
+Object names
+~~~~~~~~~~~~
+Objects can be named by their 40 hexadecimal digit sha1-name or 64
+hexadecimal digit newhash-name, plus names derived from those (see
+gitrevisions(7)).
+
+The sha1-name of an object is the SHA-1 of the concatenation of its
+type, length, a nul byte, and the object's sha1-content. This is the
+traditional <sha1> used in Git to name objects.
+
+The newhash-name of an object is the NewHash of the concatenation of its
+type, length, a nul byte, and the object's newhash-content.
+
+Object format
+~~~~~~~~~~~~~
+The content as a byte sequence of a tag, commit, or tree object named
+by sha1 and newhash differ because an object named by newhash-name refers to
+other objects by their newhash-names and an object named by sha1-name
+refers to other objects by their sha1-names.
+
+The newhash-content of an object is the same as its sha1-content, except
+that objects referenced by the object are named using their newhash-names
+instead of sha1-names. Because a blob object does not refer to any
+other object, its sha1-content and newhash-content are the same.
+
+The format allows round-trip conversion between newhash-content and
+sha1-content.
+
+Object storage
+~~~~~~~~~~~~~~
+Loose objects use zlib compression and packed objects use the packed
+format described in Documentation/technical/pack-format.txt, just like
+today. The content that is compressed and stored uses newhash-content
+instead of sha1-content.
+
+Pack index
+~~~~~~~~~~
+Pack index (.idx) files use a new v3 format that supports multiple
+hash functions. They have the following format (all integers are in
+network byte order):
+
+- A header appears at the beginning and consists of the following:
+  - The 4-byte pack index signature: '\377t0c'
+  - 4-byte version number: 3
+  - 4-byte length of the header section, including the signature and
+    version number
+  - 4-byte number of objects contained in the pack
+  - 4-byte number of object formats in this pack index: 2
+  - For each object format:
+    - 4-byte format identifier (e.g., 'sha1' for SHA-1)
+    - 4-byte length in bytes of shortened object names. This is the
+      shortest possible length needed to make names in the shortened
+      object name table unambiguous.
+    - 4-byte integer, recording where tables relating to this format
+      are stored in this index file, as an offset from the beginning.
+  - 4-byte offset to the trailer from the beginning of this file.
+  - Zero or more additional key/value pairs (4-byte key, 4-byte
+    value). Only one key is supported: 'PSRC'. See the "Loose objects
+    and unreachable objects" section for supported values and how this
+    is used.  All other keys are reserved. Readers must ignore
+    unrecognized keys.
+- Zero or more NUL bytes. This can optionally be used to improve the
+  alignment of the full object name table below.
+- Tables for the first object format:
+  - A sorted table of shortened object names.  These are prefixes of
+    the names of all objects in this pack file, packed together
+    without offset values to reduce the cache footprint of the binary
+    search for a specific object name.
+
+  - A table of full object names in pack order. This allows resolving
+    a reference to "the nth object in the pack file" (from a
+    reachability bitmap or from the next table of another object
+    format) to its object name.
+
+  - A table of 4-byte values mapping object name order to pack order.
+    For an object in the table of sorted shortened object names, the
+    value at the corresponding index in this table is the index in the
+    previous table for that same object.
+
+    This can be used to look up the object in reachability bitmaps or
+    to look up its name in another object format.
+
+  - A table of 4-byte CRC32 values of the packed object data, in the
+    order that the objects appear in the pack file. This is to allow
+    compressed data to be copied directly from pack to pack during
+    repacking without undetected data corruption.
+
+  - A table of 4-byte offset values. For an object in the table of
+    sorted shortened object names, the value at the corresponding
+    index in this table indicates where that object can be found in
+    the pack file. These are usually 31-bit pack file offsets, but
+    large offsets are encoded as an index into the next table with the
+    most significant bit set.
+
+  - A table of 8-byte offset entries (empty for pack files less than
+    2 GiB). Pack files are organized with heavily used objects toward
+    the front, so most object references should not need to refer to
+    this table.
+- Zero or more NUL bytes.
+- Tables for the second object format, with the same layout as above,
+  up to and not including the table of CRC32 values.
+- Zero or more NUL bytes.
+- The trailer consists of the following:
+  - A copy of the 20-byte NewHash checksum at the end of the
+    corresponding packfile.
+
+  - 20-byte NewHash checksum of all of the above.
+
+Loose object index
+~~~~~~~~~~~~~~~~~~
+A new file $GIT_OBJECT_DIR/loose-object-idx contains information about
+all loose objects. Its format is
+
+  # loose-object-idx
+  (newhash-name SP sha1-name LF)*
+
+where the object names are in hexadecimal format. The file is not
+sorted.
+
+The loose object index is protected against concurrent writes by a
+lock file $GIT_OBJECT_DIR/loose-object-idx.lock. To add a new loose
+object:
+
+1. Write the loose object to a temporary file, like today.
+2. Open loose-object-idx.lock with O_CREAT | O_EXCL to acquire the lock.
+3. Rename the loose object into place.
+4. Open loose-object-idx with O_APPEND and write the new object
+5. Unlink loose-object-idx.lock to release the lock.
+
+To remove entries (e.g. in "git pack-refs" or "git-prune"):
+
+1. Open loose-object-idx.lock with O_CREAT | O_EXCL to acquire the
+   lock.
+2. Write the new content to loose-object-idx.lock.
+3. Unlink any loose objects being removed.
+4. Rename to replace loose-object-idx, releasing the lock.
+
+Translation table
+~~~~~~~~~~~~~~~~~
+The index files support a bidirectional mapping between sha1-names
+and newhash-names. The lookup proceeds similarly to ordinary object
+lookups. For example, to convert a sha1-name to a newhash-name:
+
+ 1. Look for the object in idx files. If a match is present in the
+    idx's sorted list of truncated sha1-names, then:
+    a. Read the corresponding entry in the sha1-name order to pack
+       name order mapping.
+    b. Read the corresponding entry in the full sha1-name table to
+       verify we found the right object. If it is, then
+    c. Read the corresponding entry in the full newhash-name table.
+       That is the object's newhash-name.
+ 2. Check for a loose object. Read lines from loose-object-idx until
+    we find a match.
+
+Step (1) takes the same amount of time as an ordinary object lookup:
+O(number of packs * log(objects per pack)). Step (2) takes O(number of
+loose objects) time. To maintain good performance it will be necessary
+to keep the number of loose objects low. See the "Loose objects and
+unreachable objects" section below for more details.
+
+Since all operations that make new objects (e.g., "git commit") add
+the new objects to the corresponding index, this mapping is possible
+for all objects in the object store.
+
+Reading an object's sha1-content
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The sha1-content of an object can be read by converting all newhash-names
+its newhash-content references to sha1-names using the translation table.
+
+Fetch
+~~~~~
+Fetching from a SHA-1 based server requires translating between SHA-1
+and NewHash based representations on the fly.
+
+SHA-1s named in the ref advertisement that are present on the client
+can be translated to NewHash and looked up as local objects using the
+translation table.
+
+Negotiation proceeds as today. Any "have"s generated locally are
+converted to SHA-1 before being sent to the server, and SHA-1s
+mentioned by the server are converted to NewHash when looking them up
+locally.
+
+After negotiation, the server sends a packfile containing the
+requested objects. We convert the packfile to NewHash format using
+the following steps:
+
+1. index-pack: inflate each object in the packfile and compute its
+   SHA-1. Objects can contain deltas in OBJ_REF_DELTA format against
+   objects the client has locally. These objects can be looked up
+   using the translation table and their sha1-content read as
+   described above to resolve the deltas.
+2. topological sort: starting at the "want"s from the negotiation
+   phase, walk through objects in the pack and emit a list of them,
+   excluding blobs, in reverse topologically sorted order, with each
+   object coming later in the list than all objects it references.
+   (This list only contains objects reachable from the "wants". If the
+   pack from the server contained additional extraneous objects, then
+   they will be discarded.)
+3. convert to newhash: open a new (newhash) packfile. Read the topologically
+   sorted list just generated. For each object, inflate its
+   sha1-content, convert to newhash-content, and write it to the newhash
+   pack. Record the new sha1<->newhash mapping entry for use in the idx.
+4. sort: reorder entries in the new pack to match the order of objects
+   in the pack the server generated and include blobs. Write a newhash idx
+   file
+5. clean up: remove the SHA-1 based pack file, index, and
+   topologically sorted list obtained from the server in steps 1
+   and 2.
+
+Step 3 requires every object referenced by the new object to be in the
+translation table. This is why the topological sort step is necessary.
+
+As an optimization, step 1 could write a file describing what non-blob
+objects each object it has inflated from the packfile references. This
+makes the topological sort in step 2 possible without inflating the
+objects in the packfile for a second time. The objects need to be
+inflated again in step 3, for a total of two inflations.
+
+Step 4 is probably necessary for good read-time performance. "git
+pack-objects" on the server optimizes the pack file for good data
+locality (see Documentation/technical/pack-heuristics.txt).
+
+Details of this process are likely to change. It will take some
+experimenting to get this to perform well.
+
+Push
+~~~~
+Push is simpler than fetch because the objects referenced by the
+pushed objects are already in the translation table. The sha1-content
+of each object being pushed can be read as described in the "Reading
+an object's sha1-content" section to generate the pack written by git
+send-pack.
+
+Signed Commits
+~~~~~~~~~~~~~~
+We add a new field "gpgsig-newhash" to the commit object format to allow
+signing commits without relying on SHA-1. It is similar to the
+existing "gpgsig" field. Its signed payload is the newhash-content of the
+commit object with any "gpgsig" and "gpgsig-newhash" fields removed.
+
+This means commits can be signed
+1. using SHA-1 only, as in existing signed commit objects
+2. using both SHA-1 and NewHash, by using both gpgsig-newhash and gpgsig
+   fields.
+3. using only NewHash, by only using the gpgsig-newhash field.
+
+Old versions of "git verify-commit" can verify the gpgsig signature in
+cases (1) and (2) without modifications and view case (3) as an
+ordinary unsigned commit.
+
+Signed Tags
+~~~~~~~~~~~
+We add a new field "gpgsig-newhash" to the tag object format to allow
+signing tags without relying on SHA-1. Its signed payload is the
+newhash-content of the tag with its gpgsig-newhash field and "-----BEGIN PGP
+SIGNATURE-----" delimited in-body signature removed.
+
+This means tags can be signed
+1. using SHA-1 only, as in existing signed tag objects
+2. using both SHA-1 and NewHash, by using gpgsig-newhash and an in-body
+   signature.
+3. using only NewHash, by only using the gpgsig-newhash field.
+
+Mergetag embedding
+~~~~~~~~~~~~~~~~~~
+The mergetag field in the sha1-content of a commit contains the
+sha1-content of a tag that was merged by that commit.
+
+The mergetag field in the newhash-content of the same commit contains the
+newhash-content of the same tag.
+
+Submodules
+~~~~~~~~~~
+To convert recorded submodule pointers, you need to have the converted
+submodule repository in place. The translation table of the submodule
+can be used to look up the new hash.
+
+Loose objects and unreachable objects
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Fast lookups in the loose-object-idx require that the number of loose
+objects not grow too high.
+
+"git gc --auto" currently waits for there to be 6700 loose objects
+present before consolidating them into a packfile. We will need to
+measure to find a more appropriate threshold for it to use.
+
+"git gc --auto" currently waits for there to be 50 packs present
+before combining packfiles. Packing loose objects more aggressively
+may cause the number of pack files to grow too quickly. This can be
+mitigated by using a strategy similar to Martin Fick's exponential
+rolling garbage collection script:
+https://gerrit-review.googlesource.com/c/gerrit/+/35215
+
+"git gc" currently expels any unreachable objects it encounters in
+pack files to loose objects in an attempt to prevent a race when
+pruning them (in case another process is simultaneously writing a new
+object that refers to the about-to-be-deleted object). This leads to
+an explosion in the number of loose objects present and disk space
+usage due to the objects in delta form being replaced with independent
+loose objects.  Worse, the race is still present for loose objects.
+
+Instead, "git gc" will need to move unreachable objects to a new
+packfile marked as UNREACHABLE_GARBAGE (using the PSRC field; see
+below). To avoid the race when writing new objects referring to an
+about-to-be-deleted object, code paths that write new objects will
+need to copy any objects from UNREACHABLE_GARBAGE packs that they
+refer to to new, non-UNREACHABLE_GARBAGE packs (or loose objects).
+UNREACHABLE_GARBAGE are then safe to delete if their creation time (as
+indicated by the file's mtime) is long enough ago.
+
+To avoid a proliferation of UNREACHABLE_GARBAGE packs, they can be
+combined under certain circumstances. If "gc.garbageTtl" is set to
+greater than one day, then packs created within a single calendar day,
+UTC, can be coalesced together. The resulting packfile would have an
+mtime before midnight on that day, so this makes the effective maximum
+ttl the garbageTtl + 1 day. If "gc.garbageTtl" is less than one day,
+then we divide the calendar day into intervals one-third of that ttl
+in duration. Packs created within the same interval can be coalesced
+together. The resulting packfile would have an mtime before the end of
+the interval, so this makes the effective maximum ttl equal to the
+garbageTtl * 4/3.
+
+This rule comes from Thirumala Reddy Mutchukota's JGit change
+https://git.eclipse.org/r/90465.
+
+The UNREACHABLE_GARBAGE setting goes in the PSRC field of the pack
+index. More generally, that field indicates where a pack came from:
+
+ - 1 (PACK_SOURCE_RECEIVE) for a pack received over the network
+ - 2 (PACK_SOURCE_AUTO) for a pack created by a lightweight
+   "gc --auto" operation
+ - 3 (PACK_SOURCE_GC) for a pack created by a full gc
+ - 4 (PACK_SOURCE_UNREACHABLE_GARBAGE) for potential garbage
+   discovered by gc
+ - 5 (PACK_SOURCE_INSERT) for locally created objects that were
+   written directly to a pack file, e.g. from "git add ."
+
+This information can be useful for debugging and for "gc --auto" to
+make appropriate choices about which packs to coalesce.
+
+Caveats
+-------
+Invalid objects
+~~~~~~~~~~~~~~~
+The conversion from sha1-content to newhash-content retains any
+brokenness in the original object (e.g., tree entry modes encoded with
+leading 0, tree objects whose paths are not sorted correctly, and
+commit objects without an author or committer). This is a deliberate
+feature of the design to allow the conversion to round-trip.
+
+More profoundly broken objects (e.g., a commit with a truncated "tree"
+header line) cannot be converted but were not usable by current Git
+anyway.
+
+Shallow clone and submodules
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Because it requires all referenced objects to be available in the
+locally generated translation table, this design does not support
+shallow clone or unfetched submodules. Protocol improvements might
+allow lifting this restriction.
+
+Alternates
+~~~~~~~~~~
+For the same reason, a newhash repository cannot borrow objects from a
+sha1 repository using objects/info/alternates or
+$GIT_ALTERNATE_OBJECT_REPOSITORIES.
+
+git notes
+~~~~~~~~~
+The "git notes" tool annotates objects using their sha1-name as key.
+This design does not describe a way to migrate notes trees to use
+newhash-names. That migration is expected to happen separately (for
+example using a file at the root of the notes tree to describe which
+hash it uses).
+
+Server-side cost
+~~~~~~~~~~~~~~~~
+Until Git protocol gains NewHash support, using NewHash based storage
+on public-facing Git servers is strongly discouraged. Once Git
+protocol gains NewHash support, NewHash based servers are likely not
+to support SHA-1 compatibility, to avoid what may be a very expensive
+hash reencode during clone and to encourage peers to modernize.
+
+The design described here allows fetches by SHA-1 clients of a
+personal NewHash repository because it's not much more difficult than
+allowing pushes from that repository. This support needs to be guarded
+by a configuration option --- servers like git.kernel.org that serve a
+large number of clients would not be expected to bear that cost.
+
+Meaning of signatures
+~~~~~~~~~~~~~~~~~~~~~
+The signed payload for signed commits and tags does not explicitly
+name the hash used to identify objects. If some day Git adopts a new
+hash function with the same length as the current SHA-1 (40
+hexadecimal digit) or NewHash (64 hexadecimal digit) objects then the
+intent behind the PGP signed payload in an object signature is
+unclear:
+
+       object e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7
+       type commit
+       tag v2.12.0
+       tagger Junio C Hamano <gitster@pobox.com> 1487962205 -0800
+
+       Git 2.12
+
+Does this mean Git v2.12.0 is the commit with sha1-name
+e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7 or the commit with
+new-40-digit-hash-name e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7?
+
+Fortunately NewHash and SHA-1 have different lengths. If Git starts
+using another hash with the same length to name objects, then it will
+need to change the format of signed payloads using that hash to
+address this issue.
+
+Object names on the command line
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+To support the transition (see Transition plan below), this design
+supports four different modes of operation:
+
+ 1. ("dark launch") Treat object names input by the user as SHA-1 and
+    convert any object names written to output to SHA-1, but store
+    objects using NewHash.  This allows users to test the code with no
+    visible behavior change except for performance.  This allows
+    allows running even tests that assume the SHA-1 hash function, to
+    sanity-check the behavior of the new mode.
+
+ 2. ("early transition") Allow both SHA-1 and NewHash object names in
+    input. Any object names written to output use SHA-1. This allows
+    users to continue to make use of SHA-1 to communicate with peers
+    (e.g. by email) that have not migrated yet and prepares for mode 3.
+
+ 3. ("late transition") Allow both SHA-1 and NewHash object names in
+    input. Any object names written to output use NewHash. In this
+    mode, users are using a more secure object naming method by
+    default.  The disruption is minimal as long as most of their peers
+    are in mode 2 or mode 3.
+
+ 4. ("post-transition") Treat object names input by the user as
+    NewHash and write output using NewHash. This is safer than mode 3
+    because there is less risk that input is incorrectly interpreted
+    using the wrong hash function.
+
+The mode is specified in configuration.
+
+The user can also explicitly specify which format to use for a
+particular revision specifier and for output, overriding the mode. For
+example:
+
+git --output-format=sha1 log abac87a^{sha1}..f787cac^{newhash}
+
+Selection of a New Hash
+-----------------------
+In early 2005, around the time that Git was written,  Xiaoyun Wang,
+Yiqun Lisa Yin, and Hongbo Yu announced an attack finding SHA-1
+collisions in 2^69 operations. In August they published details.
+Luckily, no practical demonstrations of a collision in full SHA-1 were
+published until 10 years later, in 2017.
+
+The hash function NewHash to replace SHA-1 should be stronger than
+SHA-1 was: we would like it to be trustworthy and useful in practice
+for at least 10 years.
+
+Some other relevant properties:
+
+1. A 256-bit hash (long enough to match common security practice; not
+   excessively long to hurt performance and disk usage).
+
+2. High quality implementations should be widely available (e.g. in
+   OpenSSL).
+
+3. The hash function's properties should match Git's needs (e.g. Git
+   requires collision and 2nd preimage resistance and does not require
+   length extension resistance).
+
+4. As a tiebreaker, the hash should be fast to compute (fortunately
+   many contenders are faster than SHA-1).
+
+Some hashes under consideration are SHA-256, SHA-512/256, SHA-256x16,
+K12, and BLAKE2bp-256.
+
+Transition plan
+---------------
+Some initial steps can be implemented independently of one another:
+- adding a hash function API (vtable)
+- teaching fsck to tolerate the gpgsig-newhash field
+- excluding gpgsig-* from the fields copied by "git commit --amend"
+- annotating tests that depend on SHA-1 values with a SHA1 test
+  prerequisite
+- using "struct object_id", GIT_MAX_RAWSZ, and GIT_MAX_HEXSZ
+  consistently instead of "unsigned char *" and the hardcoded
+  constants 20 and 40.
+- introducing index v3
+- adding support for the PSRC field and safer object pruning
+
+
+The first user-visible change is the introduction of the objectFormat
+extension (without compatObjectFormat). This requires:
+- implementing the loose-object-idx
+- teaching fsck about this mode of operation
+- using the hash function API (vtable) when computing object names
+- signing objects and verifying signatures
+- rejecting attempts to fetch from or push to an incompatible
+  repository
+
+Next comes introduction of compatObjectFormat:
+- translating object names between object formats
+- translating object content between object formats
+- generating and verifying signatures in the compat format
+- adding appropriate index entries when adding a new object to the
+  object store
+- --output-format option
+- ^{sha1} and ^{newhash} revision notation
+- configuration to specify default input and output format (see
+  "Object names on the command line" above)
+
+The next step is supporting fetches and pushes to SHA-1 repositories:
+- allow pushes to a repository using the compat format
+- generate a topologically sorted list of the SHA-1 names of fetched
+  objects
+- convert the fetched packfile to newhash format and generate an idx
+  file
+- re-sort to match the order of objects in the fetched packfile
+
+The infrastructure supporting fetch also allows converting an existing
+repository. In converted repositories and new clones, end users can
+gain support for the new hash function without any visible change in
+behavior (see "dark launch" in the "Object names on the command line"
+section). In particular this allows users to verify NewHash signatures
+on objects in the repository, and it should ensure the transition code
+is stable in production in preparation for using it more widely.
+
+Over time projects would encourage their users to adopt the "early
+transition" and then "late transition" modes to take advantage of the
+new, more futureproof NewHash object names.
+
+When objectFormat and compatObjectFormat are both set, commands
+generating signatures would generate both SHA-1 and NewHash signatures
+by default to support both new and old users.
+
+In projects using NewHash heavily, users could be encouraged to adopt
+the "post-transition" mode to avoid accidentally making implicit use
+of SHA-1 object names.
+
+Once a critical mass of users have upgraded to a version of Git that
+can verify NewHash signatures and have converted their existing
+repositories to support verifying them, we can add support for a
+setting to generate only NewHash signatures. This is expected to be at
+least a year later.
+
+That is also a good moment to advertise the ability to convert
+repositories to use NewHash only, stripping out all SHA-1 related
+metadata. This improves performance by eliminating translation
+overhead and security by avoiding the possibility of accidentally
+relying on the safety of SHA-1.
+
+Updating Git's protocols to allow a server to specify which hash
+functions it supports is also an important part of this transition. It
+is not discussed in detail in this document but this transition plan
+assumes it happens. :)
+
+Alternatives considered
+-----------------------
+Upgrading everyone working on a particular project on a flag day
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Projects like the Linux kernel are large and complex enough that
+flipping the switch for all projects based on the repository at once
+is infeasible.
+
+Not only would all developers and server operators supporting
+developers have to switch on the same flag day, but supporting tooling
+(continuous integration, code review, bug trackers, etc) would have to
+be adapted as well. This also makes it difficult to get early feedback
+from some project participants testing before it is time for mass
+adoption.
+
+Using hash functions in parallel
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+(e.g. https://public-inbox.org/git/22708.8913.864049.452252@chiark.greenend.org.uk/ )
+Objects newly created would be addressed by the new hash, but inside
+such an object (e.g. commit) it is still possible to address objects
+using the old hash function.
+* You cannot trust its history (needed for bisectability) in the
+  future without further work
+* Maintenance burden as the number of supported hash functions grows
+  (they will never go away, so they accumulate). In this proposal, by
+  comparison, converted objects lose all references to SHA-1.
+
+Signed objects with multiple hashes
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Instead of introducing the gpgsig-newhash field in commit and tag objects
+for newhash-content based signatures, an earlier version of this design
+added "hash newhash <newhash-name>" fields to strengthen the existing
+sha1-content based signatures.
+
+In other words, a single signature was used to attest to the object
+content using both hash functions. This had some advantages:
+* Using one signature instead of two speeds up the signing process.
+* Having one signed payload with both hashes allows the signer to
+  attest to the sha1-name and newhash-name referring to the same object.
+* All users consume the same signature. Broken signatures are likely
+  to be detected quickly using current versions of git.
+
+However, it also came with disadvantages:
+* Verifying a signed object requires access to the sha1-names of all
+  objects it references, even after the transition is complete and
+  translation table is no longer needed for anything else. To support
+  this, the design added fields such as "hash sha1 tree <sha1-name>"
+  and "hash sha1 parent <sha1-name>" to the newhash-content of a signed
+  commit, complicating the conversion process.
+* Allowing signed objects without a sha1 (for after the transition is
+  complete) complicated the design further, requiring a "nohash sha1"
+  field to suppress including "hash sha1" fields in the newhash-content
+  and signed payload.
+
+Lazily populated translation table
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Some of the work of building the translation table could be deferred to
+push time, but that would significantly complicate and slow down pushes.
+Calculating the sha1-name at object creation time at the same time it is
+being streamed to disk and having its newhash-name calculated should be
+an acceptable cost.
+
+Document History
+----------------
+
+2017-03-03
+bmwill@google.com, jonathantanmy@google.com, jrnieder@gmail.com,
+sbeller@google.com
+
+Initial version sent to
+http://public-inbox.org/git/20170304011251.GA26789@aiede.mtv.corp.google.com
+
+2017-03-03 jrnieder@gmail.com
+Incorporated suggestions from jonathantanmy and sbeller:
+* describe purpose of signed objects with each hash type
+* redefine signed object verification using object content under the
+  first hash function
+
+2017-03-06 jrnieder@gmail.com
+* Use SHA3-256 instead of SHA2 (thanks, Linus and brian m. carlson).[1][2]
+* Make sha3-based signatures a separate field, avoiding the need for
+  "hash" and "nohash" fields (thanks to peff[3]).
+* Add a sorting phase to fetch (thanks to Junio for noticing the need
+  for this).
+* Omit blobs from the topological sort during fetch (thanks to peff).
+* Discuss alternates, git notes, and git servers in the caveats
+  section (thanks to Junio Hamano, brian m. carlson[4], and Shawn
+  Pearce).
+* Clarify language throughout (thanks to various commenters,
+  especially Junio).
+
+2017-09-27 jrnieder@gmail.com, sbeller@google.com
+* use placeholder NewHash instead of SHA3-256
+* describe criteria for picking a hash function.
+* include a transition plan (thanks especially to Brandon Williams
+  for fleshing these ideas out)
+* define the translation table (thanks, Shawn Pearce[5], Jonathan
+  Tan, and Masaya Suzuki)
+* avoid loose object overhead by packing more aggressively in
+  "git gc --auto"
+
+[1] http://public-inbox.org/git/CA+55aFzJtejiCjV0e43+9oR3QuJK2PiFiLQemytoLpyJWe6P9w@mail.gmail.com/
+[2] http://public-inbox.org/git/CA+55aFz+gkAsDZ24zmePQuEs1XPS9BP_s8O7Q4wQ7LV7X5-oDA@mail.gmail.com/
+[3] http://public-inbox.org/git/20170306084353.nrns455dvkdsfgo5@sigill.intra.peff.net/
+[4] http://public-inbox.org/git/20170304224936.rqqtkdvfjgyezsht@genre.crustytoothpaste.net
+[5] https://public-inbox.org/git/CAJo=hJtoX9=AyLHHpUJS7fueV9ciZ_MNpnEPHUz8Whui6g9F0A@mail.gmail.com/
index a34917153fd3dc10fefe71a1ba05e5dacb9af7f9..ed1eae8b83a651d34f9ce2488f58d02a763fbfd4 100644 (file)
@@ -199,7 +199,7 @@ After reference and capabilities discovery, the client can decide to
 terminate the connection by sending a flush-pkt, telling the server it can
 now gracefully terminate, and disconnect, when it does not need any pack
 data. This can happen with the ls-remote command, and also can happen when
-the client already is up-to-date.
+the client already is up to date.
 
 Otherwise, it enters the negotiation phase, where the client and
 server determine what the minimal packfile necessary for transport is,
index c79d4a7c476da2c57c117529150de77646ed116e..1f1c33d0da30dbdf90c37bd808eb52059b598c84 100644 (file)
@@ -32,7 +32,7 @@ or the result.
 If multiple cases apply, the one used is listed first.
 
 A result which changes the index is an error if the index is not empty
-and not up-to-date.
+and not up to date.
 
 Entries marked '+' have stat information. Spaces marked '*' don't
 affect the result.
@@ -65,7 +65,7 @@ empty, no entry is left for that stage). Otherwise, the given entry is
 left in stage 0, and there are no other entries.
 
 A result of "no merge" is an error if the index is not empty and not
-up-to-date.
+up to date.
 
 *empty* means that the tree must not have a directory-file conflict
  with the entry.
index bc29298678a6d2d9f6b2927f97c40ba8978bd93b..b4d88af133e8eafd92f86443bd74de063bad4835 100644 (file)
@@ -2044,10 +2044,12 @@ If a push would not result in a <<fast-forwards,fast-forward>> of the
 remote branch, then it will fail with an error like:
 
 -------------------------------------------------
-error: remote 'refs/heads/master' is not an ancestor of
- local  'refs/heads/master'.
- Maybe you are not up-to-date and need to pull first?
-error: failed to push to 'ssh://yourserver.com/~you/proj.git'
+ ! [rejected]        master -> master (non-fast-forward)
+error: failed to push some refs to '...'
+hint: Updates were rejected because the tip of your current branch is behind
+hint: its remote counterpart. Integrate the remote changes (e.g.
+hint: 'git pull ...') before pushing again.
+hint: See the 'Note about fast-forwards' in 'git push --help' for details.
 -------------------------------------------------
 
 This can happen, for example, if you:
@@ -2193,7 +2195,7 @@ $ cd work
 Linus's tree will be stored in the remote-tracking branch named origin/master,
 and can be updated using linkgit:git-fetch[1]; you can track other
 public trees using linkgit:git-remote[1] to set up a "remote" and
-linkgit:git-fetch[1] to keep them up-to-date; see
+linkgit:git-fetch[1] to keep them up to date; see
 <<repositories-and-branches>>.
 
 Now create the branches in which you are going to work; these start out
index 1ab52e381e2f4e6bfb0e4d08fd339aee9b891c8d..ab04c977be0cfdb6f282b7911d3fe630d5f70c65 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 GVF=GIT-VERSION-FILE
-DEF_VER=v2.14.2
+DEF_VER=v2.15.0-rc0
 
 LF='
 '
index 043ec8377b72ff3d86193982eb866a2f055f752c..cd75985991f4535c45e2589222a9e6a38fb1d613 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -162,6 +162,11 @@ all::
 # algorithm. This is slower, but may detect attempted collision attacks.
 # Takes priority over other *_SHA1 knobs.
 #
+# Define DC_SHA1_EXTERNAL in addition to DC_SHA1 if you want to build / link
+# git with the external SHA1 collision-detect library.
+# Without this option, i.e. the default behavior is to build git with its
+# own built-in code (or submodule).
+#
 # Define DC_SHA1_SUBMODULE in addition to DC_SHA1 to use the
 # sha1collisiondetection shipped as a submodule instead of the
 # non-submodule copy in sha1dc/. This is an experimental option used
@@ -200,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().
@@ -655,6 +663,7 @@ TEST_PROGRAMS_NEED_X += test-parse-options
 TEST_PROGRAMS_NEED_X += test-path-utils
 TEST_PROGRAMS_NEED_X += test-prio-queue
 TEST_PROGRAMS_NEED_X += test-read-cache
+TEST_PROGRAMS_NEED_X += test-write-cache
 TEST_PROGRAMS_NEED_X += test-ref-store
 TEST_PROGRAMS_NEED_X += test-regex
 TEST_PROGRAMS_NEED_X += test-revision-walking
@@ -815,7 +824,9 @@ LIB_OBJS += notes-cache.o
 LIB_OBJS += notes-merge.o
 LIB_OBJS += notes-utils.o
 LIB_OBJS += object.o
+LIB_OBJS += oidmap.o
 LIB_OBJS += oidset.o
+LIB_OBJS += packfile.o
 LIB_OBJS += pack-bitmap.o
 LIB_OBJS += pack-bitmap-write.o
 LIB_OBJS += pack-check.o
@@ -842,6 +853,7 @@ LIB_OBJS += reflog-walk.o
 LIB_OBJS += refs.o
 LIB_OBJS += refs/files-backend.o
 LIB_OBJS += refs/iterator.o
+LIB_OBJS += refs/packed-backend.o
 LIB_OBJS += refs/ref-cache.o
 LIB_OBJS += ref-filter.o
 LIB_OBJS += remote.o
@@ -1033,6 +1045,9 @@ BASIC_CFLAGS += -fno-omit-frame-pointer
 ifneq ($(filter undefined,$(SANITIZERS)),)
 BASIC_CFLAGS += -DNO_UNALIGNED_LOADS
 endif
+ifneq ($(filter leak,$(SANITIZERS)),)
+BASIC_CFLAGS += -DSUPPRESS_ANNOTATED_LEAKS
+endif
 endif
 
 ifndef sysconfdir
@@ -1380,6 +1395,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
@@ -1472,6 +1490,15 @@ ifdef APPLE_COMMON_CRYPTO
        BASIC_CFLAGS += -DSHA1_APPLE
 else
        DC_SHA1 := YesPlease
+       BASIC_CFLAGS += -DSHA1_DC
+       LIB_OBJS += sha1dc_git.o
+ifdef DC_SHA1_EXTERNAL
+       ifdef DC_SHA1_SUBMODULE
+$(error Only set DC_SHA1_EXTERNAL or DC_SHA1_SUBMODULE, not both)
+       endif
+       BASIC_CFLAGS += -DDC_SHA1_EXTERNAL
+       EXTLIBS += -lsha1detectcoll
+else
 ifdef DC_SHA1_SUBMODULE
        LIB_OBJS += sha1collisiondetection/lib/sha1.o
        LIB_OBJS += sha1collisiondetection/lib/ubc_check.o
@@ -1481,17 +1508,15 @@ else
        LIB_OBJS += sha1dc/ubc_check.o
 endif
        BASIC_CFLAGS += \
-               -DSHA1_DC \
                -DSHA1DC_NO_STANDARD_INCLUDES \
                -DSHA1DC_INIT_SAFE_HASH_DEFAULT=0 \
                -DSHA1DC_CUSTOM_INCLUDE_SHA1_C="\"cache.h\"" \
-               -DSHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C="\"sha1dc_git.c\"" \
-               -DSHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H="\"sha1dc_git.h\"" \
                -DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C="\"git-compat-util.h\""
 endif
 endif
 endif
 endif
+endif
 
 ifdef SHA1_MAX_BLOCK_SIZE
        LIB_OBJS += compat/sha1-chunked.o
@@ -2439,6 +2464,10 @@ $(SP_OBJ): %.sp: %.c GIT-CFLAGS FORCE
 .PHONY: sparse $(SP_OBJ)
 sparse: $(SP_OBJ)
 
+.PHONY: style
+style:
+       git clang-format --style file --diff --extensions c,h
+
 check: common-cmds.h
        @if sparse; \
        then \
index 29ef98331028ae5b1cdc10be75626e48b173a961..80ae7a3110de6bd298cfa9d8481808d0af5e7d2d 120000 (symlink)
--- a/RelNotes
+++ b/RelNotes
@@ -1 +1 @@
-Documentation/RelNotes/2.14.2.txt
\ No newline at end of file
+Documentation/RelNotes/2.15.0.txt
\ No newline at end of file
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;
diff --git a/apply.c b/apply.c
index 0c7b25989cdf58b411190744c97d93450be9b53e..c022af53a24bf56556d136a3456862cf6a2ca7cc 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -812,16 +812,13 @@ static int has_epoch_timestamp(const char *nameline)
         * 1970-01-01, and the seconds part must be "00".
         */
        const char stamp_regexp[] =
-               "^(1969-12-31|1970-01-01)"
-               " "
-               "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
+               "^[0-2][0-9]:([0-5][0-9]):00(\\.0+)?"
                " "
                "([-+][0-2][0-9]:?[0-5][0-9])\n";
        const char *timestamp = NULL, *cp, *colon;
        static regex_t *stamp;
        regmatch_t m[10];
-       int zoneoffset;
-       int hourminute;
+       int zoneoffset, epoch_hour, hour, minute;
        int status;
 
        for (cp = nameline; *cp != '\n'; cp++) {
@@ -830,6 +827,18 @@ static int has_epoch_timestamp(const char *nameline)
        }
        if (!timestamp)
                return 0;
+
+       /*
+        * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
+        * (west of GMT) or 1970-01-01 (east of GMT)
+        */
+       if (skip_prefix(timestamp, "1969-12-31 ", &timestamp))
+               epoch_hour = 24;
+       else if (skip_prefix(timestamp, "1970-01-01 ", &timestamp))
+               epoch_hour = 0;
+       else
+               return 0;
+
        if (!stamp) {
                stamp = xmalloc(sizeof(*stamp));
                if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
@@ -847,6 +856,9 @@ static int has_epoch_timestamp(const char *nameline)
                return 0;
        }
 
+       hour = strtol(timestamp, NULL, 10);
+       minute = strtol(timestamp + m[1].rm_so, NULL, 10);
+
        zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
        if (*colon == ':')
                zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
@@ -855,20 +867,7 @@ static int has_epoch_timestamp(const char *nameline)
        if (timestamp[m[3].rm_so] == '-')
                zoneoffset = -zoneoffset;
 
-       /*
-        * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
-        * (west of GMT) or 1970-01-01 (east of GMT)
-        */
-       if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
-           (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
-               return 0;
-
-       hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
-                     strtol(timestamp + 14, NULL, 10) -
-                     zoneoffset);
-
-       return ((zoneoffset < 0 && hourminute == 1440) ||
-               (0 <= zoneoffset && !hourminute));
+       return hour * 60 + minute - zoneoffset == epoch_hour * 60;
 }
 
 /*
@@ -2921,6 +2920,7 @@ static int apply_one_fragment(struct apply_state *state,
                        if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
                            ws_blank_line(patch + 1, plen, ws_rule))
                                is_blank_context = 1;
+                       /* fallthrough */
                case '-':
                        memcpy(old, patch + 1, plen);
                        add_line_info(&preimage, old, plen,
@@ -2928,7 +2928,7 @@ static int apply_one_fragment(struct apply_state *state,
                        old += plen;
                        if (first == '-')
                                break;
-               /* Fall-through for ' ' */
+                       /* fallthrough */
                case '+':
                        /* --no-add does not add new lines */
                        if (first == '+' && state->no_add)
@@ -3577,7 +3577,7 @@ static int try_threeway(struct apply_state *state,
        /* Preimage the patch was prepared for */
        if (patch->is_new)
                write_sha1_file("", 0, blob_type, pre_oid.hash);
-       else if (get_sha1(patch->old_sha1_prefix, pre_oid.hash) ||
+       else if (get_oid(patch->old_sha1_prefix, &pre_oid) ||
                 read_blob_object(&buf, &pre_oid, patch->old_mode))
                return error(_("repository lacks the necessary blob to fall back on 3-way merge."));
 
@@ -4101,7 +4101,7 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)
                        else
                                return error(_("sha1 information is lacking or "
                                               "useless for submodule %s"), name);
-               } else if (!get_sha1_blob(patch->old_sha1_prefix, oid.hash)) {
+               } else if (!get_oid_blob(patch->old_sha1_prefix, &oid)) {
                        ; /* ok */
                } else if (!patch->lines_added && !patch->lines_deleted) {
                        /* mode-only change: update the current */
index 2ad7e6cb9071e42faa59da18aeacc9bcc7549da5..1e41f4bbeb1d8730da83aad12bc8987abedcad8a 100644 (file)
--- a/archive.c
+++ b/archive.c
@@ -121,11 +121,6 @@ static int check_attr_export_subst(const struct attr_check *check)
        return check && ATTR_TRUE(check->items[1].value);
 }
 
-static int should_queue_directories(const struct archiver_args *args)
-{
-       return args->pathspec.has_wildcard;
-}
-
 static int write_archive_entry(const unsigned char *sha1, const char *base,
                int baselen, const char *filename, unsigned mode, int stage,
                void *context)
@@ -147,7 +142,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
                strbuf_addch(&path, '/');
        path_without_prefix = path.buf + args->baselen;
 
-       if (!S_ISDIR(mode) || !should_queue_directories(args)) {
+       if (!S_ISDIR(mode)) {
                const struct attr_check *check;
                check = get_archive_attrs(path_without_prefix);
                if (check_attr_export_ignore(check))
@@ -169,14 +164,6 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
        return write_entry(args, sha1, path.buf, path.len, mode);
 }
 
-static int write_archive_entry_buf(const unsigned char *sha1, struct strbuf *base,
-               const char *filename, unsigned mode, int stage,
-               void *context)
-{
-       return write_archive_entry(sha1, base->buf, base->len,
-                                    filename, mode, stage, context);
-}
-
 static void queue_directory(const unsigned char *sha1,
                struct strbuf *base, const char *filename,
                unsigned mode, int stage, struct archiver_context *c)
@@ -290,9 +277,7 @@ int write_archive_entries(struct archiver_args *args,
        }
 
        err = read_tree_recursive(args->tree, "", 0, 0, &args->pathspec,
-                                 should_queue_directories(args) ?
-                                 queue_or_write_archive_entry :
-                                 write_archive_entry_buf,
+                                 queue_or_write_archive_entry,
                                  &context);
        if (err == READ_TREE_RECURSIVE)
                err = 0;
@@ -391,7 +376,7 @@ static void parse_treeish_arg(const char **argv,
                free(ref);
        }
 
-       if (get_sha1(name, oid.hash))
+       if (get_oid(name, &oid))
                die("Not a valid object name");
 
        commit = lookup_commit_reference_gently(&oid, 1);
diff --git a/attr.c b/attr.c
index 56961f02361129b5cb49e6ffb7ab185655f09c41..dfc3a558d83737921e1f97c96886539ef5fc3acb 100644 (file)
--- a/attr.c
+++ b/attr.c
@@ -76,18 +76,20 @@ struct attr_hash_entry {
 };
 
 /* attr_hashmap comparison function */
-static int attr_hash_entry_cmp(void *unused_cmp_data,
-                              const struct attr_hash_entry *a,
-                              const struct attr_hash_entry *b,
-                              void *unused_keydata)
+static int attr_hash_entry_cmp(const void *unused_cmp_data,
+                              const void *entry,
+                              const void *entry_or_key,
+                              const void *unused_keydata)
 {
+       const struct attr_hash_entry *a = entry;
+       const struct attr_hash_entry *b = entry_or_key;
        return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
 }
 
 /* Initialize an 'attr_hashmap' object */
 static void attr_hashmap_init(struct attr_hashmap *map)
 {
-       hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
+       hashmap_init(&map->map, attr_hash_entry_cmp, NULL, 0);
 }
 
 /*
@@ -149,10 +151,12 @@ struct all_attrs_item {
 static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check)
 {
        int i;
+       unsigned int size;
 
        hashmap_lock(map);
 
-       if (map->map.size < check->all_attrs_nr)
+       size = hashmap_get_size(&map->map);
+       if (size < check->all_attrs_nr)
                die("BUG: interned attributes shouldn't be deleted");
 
        /*
@@ -161,13 +165,13 @@ static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check)
         * field), reallocate the provided attr_check instance's all_attrs
         * field and fill each entry with its corresponding git_attr.
         */
-       if (map->map.size != check->all_attrs_nr) {
+       if (size != check->all_attrs_nr) {
                struct attr_hash_entry *e;
                struct hashmap_iter iter;
                hashmap_iter_init(&map->map, &iter);
 
-               REALLOC_ARRAY(check->all_attrs, map->map.size);
-               check->all_attrs_nr = map->map.size;
+               REALLOC_ARRAY(check->all_attrs, size);
+               check->all_attrs_nr = size;
 
                while ((e = hashmap_iter_next(&iter))) {
                        const struct git_attr *a = e->value;
@@ -235,10 +239,11 @@ static const struct git_attr *git_attr_internal(const char *name, int namelen)
 
        if (!a) {
                FLEX_ALLOC_MEM(a, name, name, namelen);
-               a->attr_nr = g_attr_hashmap.map.size;
+               a->attr_nr = hashmap_get_size(&g_attr_hashmap.map);
 
                attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a);
-               assert(a->attr_nr == (g_attr_hashmap.map.size - 1));
+               assert(a->attr_nr ==
+                      (hashmap_get_size(&g_attr_hashmap.map) - 1));
        }
 
        hashmap_unlock(&g_attr_hashmap);
index a9fd9fbc61a661ab19e18713b115f868daab2f98..96beeb5d13630672fba3021468670a1bcfc72158 100644 (file)
--- a/bisect.c
+++ b/bisect.c
@@ -680,16 +680,16 @@ static int is_expected_rev(const struct object_id *oid)
        return res;
 }
 
-static int bisect_checkout(const unsigned char *bisect_rev, int no_checkout)
+static int bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
 {
        char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
 
-       memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1);
-       update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+       memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1);
+       update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev->hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
 
        argv_checkout[2] = bisect_rev_hex;
        if (no_checkout) {
-               update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+               update_ref(NULL, "BISECT_HEAD", bisect_rev->hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
        } else {
                int res;
                res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
@@ -796,7 +796,7 @@ static void check_merge_bases(int no_checkout)
                        handle_skipped_merge_base(mb);
                } else {
                        printf(_("Bisecting: a merge base must be tested\n"));
-                       exit(bisect_checkout(mb->hash, no_checkout));
+                       exit(bisect_checkout(mb, no_checkout));
                }
        }
 
@@ -826,7 +826,8 @@ static int check_ancestors(const char *prefix)
 
        /* Clean up objects used, as they will be reused. */
        clear_commit_marks_for_object_array(&pending_copy, ALL_REV_FLAGS);
-       free(pending_copy.objects);
+
+       object_array_clear(&pending_copy);
 
        return res;
 }
@@ -939,7 +940,7 @@ int bisect_next_all(const char *prefix, int no_checkout)
        struct rev_info revs;
        struct commit_list *tried;
        int reaches = 0, all = 0, nr, steps;
-       const unsigned char *bisect_rev;
+       struct object_id *bisect_rev;
        char *steps_msg;
 
        read_bisect_terms(&term_bad, &term_good);
@@ -977,11 +978,11 @@ int bisect_next_all(const char *prefix, int no_checkout)
                exit(4);
        }
 
-       bisect_rev = revs.commits->item->object.oid.hash;
+       bisect_rev = &revs.commits->item->object.oid;
 
-       if (!hashcmp(bisect_rev, current_bad_oid->hash)) {
+       if (!oidcmp(bisect_rev, current_bad_oid)) {
                exit_if_skipped_commits(tried, current_bad_oid);
-               printf("%s is the first %s commit\n", sha1_to_hex(bisect_rev),
+               printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
                        term_bad);
                show_diff_tree(prefix, revs.commits->item);
                /* This means the bisection process succeeded. */
index 36541d05cd7b934bceba1af4fd4c121e51cd862b..4377ce2fb17d7467ee92d4db02cb4859f6397505 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -90,24 +90,24 @@ int install_branch_config(int flag, const char *local, const char *origin, const
                if (shortname) {
                        if (origin)
                                printf_ln(rebasing ?
-                                         _("Branch %s set up to track remote branch %s from %s by rebasing.") :
-                                         _("Branch %s set up to track remote branch %s from %s."),
+                                         _("Branch '%s' set up to track remote branch '%s' from '%s' by rebasing.") :
+                                         _("Branch '%s' set up to track remote branch '%s' from '%s'."),
                                          local, shortname, origin);
                        else
                                printf_ln(rebasing ?
-                                         _("Branch %s set up to track local branch %s by rebasing.") :
-                                         _("Branch %s set up to track local branch %s."),
+                                         _("Branch '%s' set up to track local branch '%s' by rebasing.") :
+                                         _("Branch '%s' set up to track local branch '%s'."),
                                          local, shortname);
                } else {
                        if (origin)
                                printf_ln(rebasing ?
-                                         _("Branch %s set up to track remote ref %s by rebasing.") :
-                                         _("Branch %s set up to track remote ref %s."),
+                                         _("Branch '%s' set up to track remote ref '%s' by rebasing.") :
+                                         _("Branch '%s' set up to track remote ref '%s'."),
                                          local, remote);
                        else
                                printf_ln(rebasing ?
-                                         _("Branch %s set up to track local ref %s by rebasing.") :
-                                         _("Branch %s set up to track local ref %s."),
+                                         _("Branch '%s' set up to track local ref '%s' by rebasing.") :
+                                         _("Branch '%s' set up to track local ref '%s'."),
                                          local, remote);
                }
        }
@@ -191,9 +191,8 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
 
        if (!attr_only) {
                const char *head;
-               struct object_id oid;
 
-               head = resolve_ref_unsafe("HEAD", 0, oid.hash, NULL);
+               head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
                if (!is_bare_repository() && head && !strcmp(head, ref->buf))
                        die(_("Cannot force update the current branch."));
        }
@@ -357,8 +356,9 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
 
                if (worktrees[i]->is_detached)
                        continue;
-               if (worktrees[i]->head_ref &&
-                   strcmp(oldref, worktrees[i]->head_ref))
+               if (!worktrees[i]->head_ref)
+                       continue;
+               if (strcmp(oldref, worktrees[i]->head_ref))
                        continue;
 
                refs = get_worktree_ref_store(worktrees[i]);
index 5d5773d5cd2fc9e713498a72da7b57b84fd25d19..a648cf4c56c9f56eb60a97ca50ccf0b191dc50ff 100644 (file)
@@ -116,8 +116,10 @@ int add_files_to_cache(const char *prefix,
        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
        rev.diffopt.format_callback = update_callback;
        rev.diffopt.format_callback_data = &data;
+       rev.diffopt.flags |= DIFF_OPT_OVERRIDE_SUBMODULE_CONFIG;
        rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
        run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
+       clear_pathspec(&rev.prune_data);
        return !!data.add_errors;
 }
 
@@ -513,5 +515,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                        die(_("Unable to write new index file"));
        }
 
+       UNLEAK(pathspec);
+       UNLEAK(dir);
        return exit_status;
 }
index 73f542bec51d77b92d39ced0dfefcc5b6b649db8..d7513f53759bc5834aa02bd904432f64e036ccb6 100644 (file)
@@ -31,6 +31,7 @@
 #include "mailinfo.h"
 #include "apply.h"
 #include "string-list.h"
+#include "packfile.h"
 
 /**
  * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -670,9 +671,7 @@ static int detect_patch_format(const char **paths)
                goto done;
        }
 
-       strbuf_reset(&l2);
        strbuf_getline(&l2, fp);
-       strbuf_reset(&l3);
        strbuf_getline(&l3, fp);
 
        /*
@@ -695,6 +694,8 @@ static int detect_patch_format(const char **paths)
 done:
        fclose(fp);
        strbuf_release(&l1);
+       strbuf_release(&l2);
+       strbuf_release(&l3);
        return ret;
 }
 
@@ -880,6 +881,7 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
 static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
 {
        struct strbuf sb = STRBUF_INIT;
+       int rc = 0;
 
        while (!strbuf_getline_lf(&sb, in)) {
                const char *str;
@@ -893,19 +895,27 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
 
                        errno = 0;
                        timestamp = parse_timestamp(str, &end, 10);
-                       if (errno)
-                               return error(_("invalid timestamp"));
+                       if (errno) {
+                               rc = error(_("invalid timestamp"));
+                               goto exit;
+                       }
 
-                       if (!skip_prefix(end, " ", &str))
-                               return error(_("invalid Date line"));
+                       if (!skip_prefix(end, " ", &str)) {
+                               rc = error(_("invalid Date line"));
+                               goto exit;
+                       }
 
                        errno = 0;
                        tz = strtol(str, &end, 10);
-                       if (errno)
-                               return error(_("invalid timezone offset"));
+                       if (errno) {
+                               rc = error(_("invalid timezone offset"));
+                               goto exit;
+                       }
 
-                       if (*end)
-                               return error(_("invalid Date line"));
+                       if (*end) {
+                               rc = error(_("invalid Date line"));
+                               goto exit;
+                       }
 
                        /*
                         * mercurial's timezone is in seconds west of UTC,
@@ -930,9 +940,9 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
                fwrite(sb.buf, 1, sb.len, out);
                strbuf_reset(&sb);
        }
-
+exit:
        strbuf_release(&sb);
-       return 0;
+       return rc;
 }
 
 /**
@@ -1143,7 +1153,7 @@ static int index_has_changes(struct strbuf *sb)
        struct object_id head;
        int i;
 
-       if (!get_sha1_tree("HEAD", head.hash)) {
+       if (!get_oid_tree("HEAD", &head)) {
                struct diff_options opt;
 
                diff_setup(&opt);
@@ -1420,7 +1430,7 @@ static void write_index_patch(const struct am_state *state)
        struct rev_info rev_info;
        FILE *fp;
 
-       if (!get_sha1_tree("HEAD", head.hash))
+       if (!get_oid_tree("HEAD", &head))
                tree = lookup_tree(&head);
        else
                tree = lookup_tree(&empty_tree_oid);
@@ -1649,7 +1659,7 @@ static void do_commit(const struct am_state *state)
        if (write_cache_as_tree(tree.hash, 0, NULL))
                die(_("git write-tree failed to write a tree"));
 
-       if (!get_sha1_commit("HEAD", parent.hash)) {
+       if (!get_oid_commit("HEAD", &parent)) {
                old_oid = &parent;
                commit_list_insert(lookup_commit(&parent), &parents);
        } else {
@@ -2095,6 +2105,7 @@ static int safe_to_abort(const struct am_state *state)
                        die(_("could not parse %s"), am_path(state, "abort-safety"));
        } else
                oidclr(&abort_safety);
+       strbuf_release(&sb);
 
        if (get_oid("HEAD", &head))
                oidclr(&head);
index bda1a787265e6d44d2ec0bec1e4dee5bf8de9c3b..67adaef4d80ee4e2fcd28755255935f0fcae6c03 100644 (file)
@@ -488,7 +488,7 @@ static int read_ancestry(const char *graft_file)
                return -1;
        while (!strbuf_getwholeline(&buf, fp, '\n')) {
                /* The format is just "Commit Parent1 Parent2 ...\n" */
-               struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
+               struct commit_graft *graft = read_graft_line(&buf);
                if (graft)
                        register_commit_graft(graft, 0);
        }
@@ -925,8 +925,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        sb.found_guilty_entry = &found_guilty_entry;
        sb.found_guilty_entry_data = &pi;
        if (show_progress)
-               pi.progress = start_progress_delay(_("Blaming lines"),
-                                                  sb.num_lines, 50, 1);
+               pi.progress = start_delayed_progress(_("Blaming lines"), sb.num_lines);
 
        assign_blame(&sb, opt);
 
index 16d391b407c9fa688b48a3e28f77800dcc1e2e0c..b67593288cf54084b569763ba2ef8e3b2cf9aa97 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
@@ -216,7 +217,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
                if (!head_rev)
                        die(_("Couldn't look up commit object for HEAD"));
        }
-       for (i = 0; i < argc; i++, strbuf_release(&bname)) {
+       for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
                char *target = NULL;
                int flags = 0;
 
@@ -281,8 +282,9 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
        }
 
        free(name);
+       strbuf_release(&bname);
 
-       return(ret);
+       return ret;
 }
 
 static int calc_maxwidth(struct ref_array *refs, int remote_bonus)
@@ -352,7 +354,7 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
                        strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
 
                strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
-               strbuf_addf(&local, "%s", branch_get_color(BRANCH_COLOR_RESET));
+               strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET));
                strbuf_addf(&local, " %s ", obname.buf);
 
                if (filter->verbose > 1)
@@ -456,15 +458,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 +493,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 +524,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 +565,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;
@@ -561,8 +582,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                OPT__QUIET(&quiet, N_("suppress informational messages")),
                OPT_SET_INT('t', "track",  &track, N_("set up tracking mode (see git-pull(1))"),
                        BRANCH_TRACK_EXPLICIT),
-               OPT_SET_INT( 0, "set-upstream",  &track, N_("change upstream info"),
-                       BRANCH_TRACK_OVERRIDE),
+               { OPTION_SET_INT, 0, "set-upstream", &track, NULL, N_("do not use"),
+                       PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, BRANCH_TRACK_OVERRIDE },
                OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
                OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("Unset the upstream info")),
                OPT__COLOR(&branch_use_color, N_("use colored output")),
@@ -581,6 +602,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 +647,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 +672,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        if (force) {
                delete *= 2;
                rename *= 2;
+               copy *= 2;
        }
 
        if (delete) {
@@ -703,20 +727,29 @@ 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"));
+                       die(_("too many arguments for a rename operation"));
        } else if (new_upstream) {
                struct branch *branch = branch_get(argv[0]);
 
                if (argc > 1)
-                       die(_("too many branches to set new upstream"));
+                       die(_("too many arguments to set new upstream"));
 
                if (!branch) {
                        if (!argc || !strcmp(argv[0], "HEAD"))
@@ -739,7 +772,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                struct strbuf buf = STRBUF_INIT;
 
                if (argc > 1)
-                       die(_("too many branches to unset upstream"));
+                       die(_("too many arguments to unset upstream"));
 
                if (!branch) {
                        if (!argc || !strcmp(argv[0], "HEAD"))
@@ -759,8 +792,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                strbuf_release(&buf);
        } else if (argc > 0 && argc <= 2) {
                struct branch *branch = branch_get(argv[0]);
-               int branch_existed = 0, remote_tracking = 0;
-               struct strbuf buf = STRBUF_INIT;
 
                if (!strcmp(argv[0], "HEAD"))
                        die(_("it does not make sense to create 'HEAD' manually"));
@@ -772,28 +803,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                        die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
 
                if (track == BRANCH_TRACK_OVERRIDE)
-                       fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
-
-               strbuf_addf(&buf, "refs/remotes/%s", branch->name);
-               remote_tracking = ref_exists(buf.buf);
-               strbuf_release(&buf);
+                       die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
 
-               branch_existed = ref_exists(branch->refname);
                create_branch(argv[0], (argc == 2) ? argv[1] : head,
                              force, reflog, 0, quiet, track);
 
-               /*
-                * We only show the instructions if the user gave us
-                * one branch which doesn't exist locally, but is the
-                * name of a remote-tracking branch.
-                */
-               if (argc == 1 && track == BRANCH_TRACK_OVERRIDE &&
-                   !branch_existed && remote_tracking) {
-                       fprintf(stderr, _("\nIf you wanted to make '%s' track '%s', do this:\n\n"), head, branch->name);
-                       fprintf(stderr, "    git branch -d %s\n", branch->name);
-                       fprintf(stderr, "    git branch --set-upstream-to %s\n", branch->name);
-               }
-
        } else
                usage_with_options(builtin_branch_usage, options);
 
index 96b786e4892aec0ed7e5d07ac8d3261c7b765f33..f5fa4fd75af26a66cd1b2c0d493116704bc16dd5 100644 (file)
@@ -12,6 +12,7 @@
 #include "streaming.h"
 #include "tree-walk.h"
 #include "sha1-array.h"
+#include "packfile.h"
 
 struct batch_options {
        int enabled;
@@ -63,8 +64,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
        if (unknown_type)
                flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
 
-       if (get_sha1_with_context(obj_name, GET_SHA1_RECORD_PATH,
-                                 oid.hash, &obj_context))
+       if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
+                                &oid, &obj_context))
                die("Not a valid object name %s", obj_name);
 
        if (!path)
@@ -96,7 +97,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
                return !has_object_file(&oid);
 
        case 'w':
-               if (!path[0])
+               if (!path)
                        die("git cat-file --filters %s: <object> must be "
                            "<sha1:path>", obj_name);
 
@@ -106,12 +107,13 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
                break;
 
        case 'c':
-               if (!path[0])
+               if (!path)
                        die("git cat-file --textconv %s: <object> must be <sha1:path>",
                            obj_name);
 
                if (textconv_object(path, obj_context.mode, &oid, 1, &buf, &size))
                        break;
+               /* else fallthrough */
 
        case 'p':
                type = sha1_object_info(oid.hash, NULL);
@@ -361,10 +363,10 @@ static void batch_one_object(const char *obj_name, struct batch_options *opt,
                             struct expand_data *data)
 {
        struct object_context ctx;
-       int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
+       int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
        enum follow_symlinks_result result;
 
-       result = get_sha1_with_context(obj_name, flags, data->oid.hash, &ctx);
+       result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
        if (result != FOUND) {
                switch (result) {
                case MISSING_OBJECT:
index eac499450f63554387fb8b32ef5780c32ded4a21..6c40ff110bee7387d4eb4e9c7c04a21e8b12ca44 100644 (file)
@@ -45,6 +45,7 @@ static int check_ref_format_branch(const char *arg)
        if (strbuf_check_branch_ref(&sb, arg))
                die("'%s' is not a valid branch name", arg);
        printf("%s\n", sb.buf + 11);
+       strbuf_release(&sb);
        return 0;
 }
 
index 2d75ac66c7f8447e846e0bc38072b76bddc3672b..fc4f8fd2ea29c7c23d3b1c7ca0ba78824c255a91 100644 (file)
@@ -436,6 +436,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o,
                 * update paths in the work tree, and we cannot revert
                 * them.
                 */
+               /* fallthrough */
        case 0:
                return 0;
        default:
@@ -796,9 +797,14 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
        for_each_ref(add_pending_uninteresting_ref, &revs);
        add_pending_oid(&revs, "HEAD", &new->object.oid, UNINTERESTING);
 
+       /* Save pending objects, so they can be cleaned up later. */
        refs = revs.pending;
        revs.leak_pending = 1;
 
+       /*
+        * prepare_revision_walk (together with .leak_pending = 1) makes us
+        * the sole owner of the list of pending objects.
+        */
        if (prepare_revision_walk(&revs))
                die(_("internal error in revision walk"));
        if (!(old->object.flags & UNINTERESTING))
@@ -806,8 +812,10 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
        else
                describe_detached_head(_("Previous HEAD position was"), old);
 
+       /* Clean up objects used, as they will be reused. */
        clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
-       free(refs.objects);
+
+       object_array_clear(&refs);
 }
 
 static int switch_branches(const struct checkout_opts *opts,
@@ -861,7 +869,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
        }
 
        if (starts_with(var, "submodule."))
-               return submodule_config(var, value, NULL);
+               return git_default_submodule_config(var, value, NULL);
 
        return git_xmerge_config(var, value, NULL);
 }
@@ -1116,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);
@@ -1182,7 +1189,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        opts.prefix = prefix;
        opts.show_progress = -1;
 
-       gitmodules_config();
        git_config(git_checkout_config, &opts);
 
        opts.track = BRANCH_TRACK_UNSPECIFIED;
@@ -1291,6 +1297,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                strbuf_release(&buf);
        }
 
+       UNLEAK(opts);
        if (opts.patch_mode || opts.pathspec.nr)
                return checkout_paths(&opts, new.name);
        else
index c1bafda5b63324b0aba7400535cc0ba745e46004..733b6d3745ee5780c697323372f1e78e28b8a117 100644 (file)
@@ -33,15 +33,6 @@ static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
 static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
 static const char *msg_warn_remove_failed = N_("failed to remove %s");
 
-static int clean_use_color = -1;
-static char clean_colors[][COLOR_MAXLEN] = {
-       GIT_COLOR_RESET,
-       GIT_COLOR_NORMAL,       /* PLAIN */
-       GIT_COLOR_BOLD_BLUE,    /* PROMPT */
-       GIT_COLOR_BOLD,         /* HEADER */
-       GIT_COLOR_BOLD_RED,     /* HELP */
-       GIT_COLOR_BOLD_RED,     /* ERROR */
-};
 enum color_clean {
        CLEAN_COLOR_RESET = 0,
        CLEAN_COLOR_PLAIN = 1,
@@ -51,6 +42,16 @@ enum color_clean {
        CLEAN_COLOR_ERROR = 5
 };
 
+static int clean_use_color = -1;
+static char clean_colors[][COLOR_MAXLEN] = {
+       [CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
+       [CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,
+       [CLEAN_COLOR_HELP] = GIT_COLOR_BOLD_RED,
+       [CLEAN_COLOR_PLAIN] = GIT_COLOR_NORMAL,
+       [CLEAN_COLOR_PROMPT] = GIT_COLOR_BOLD_BLUE,
+       [CLEAN_COLOR_RESET] = GIT_COLOR_RESET,
+};
+
 #define MENU_OPTS_SINGLETON            01
 #define MENU_OPTS_IMMEDIATE            02
 #define MENU_OPTS_LIST_ONLY            04
@@ -166,7 +167,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                }
 
                *dir_gone = 0;
-               return 0;
+               goto out;
        }
 
        dir = opendir(path->buf);
@@ -180,7 +181,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                        warning_errno(_(msg_warn_remove_failed), quoted.buf);
                        *dir_gone = 0;
                }
-               return res;
+               ret = res;
+               goto out;
        }
 
        strbuf_complete(path, '/');
@@ -248,6 +250,8 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                for (i = 0; i < dels.nr; i++)
                        printf(dry_run ?  _(msg_would_remove) : _(msg_remove), dels.items[i].string);
        }
+out:
+       strbuf_release(&quoted);
        string_list_clear(&dels, 0);
        return ret;
 }
index f7e17d22951cfd8e498143c009fa0303d0ff8319..dbddd98f80d6660f2cde190a0e121022f079071d 100644 (file)
@@ -25,6 +25,7 @@
 #include "remote.h"
 #include "run-command.h"
 #include "connected.h"
+#include "packfile.h"
 
 /*
  * Overall FIXMEs:
@@ -506,8 +507,8 @@ static void remove_junk(void)
        if (junk_work_tree) {
                strbuf_addstr(&sb, junk_work_tree);
                remove_dir_recursively(&sb, 0);
-               strbuf_reset(&sb);
        }
+       strbuf_release(&sb);
 }
 
 static void remove_junk_on_signal(int signo)
index a4a923d7c0b688e162c8e4d0411ff9b72748fb5c..2177251e247cc144d6ac8619fa5940262430d50a 100644 (file)
@@ -56,7 +56,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
                        struct object_id oid;
                        if (argc <= ++i)
                                usage(commit_tree_usage);
-                       if (get_sha1_commit(argv[i], oid.hash))
+                       if (get_oid_commit(argv[i], &oid))
                                die("Not a valid object name %s", argv[i]);
                        assert_sha1_type(oid.hash, OBJ_COMMIT);
                        new_parent(lookup_commit(&oid), &parents);
@@ -102,11 +102,10 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
                        if (fd && close(fd))
                                die_errno("git commit-tree: failed to close '%s'",
                                          argv[i]);
-                       strbuf_complete_line(&buffer);
                        continue;
                }
 
-               if (get_sha1_tree(arg, tree_oid.hash))
+               if (get_oid_tree(arg, &tree_oid))
                        die("Not a valid object name %s", arg);
                if (got_tree)
                        die("Cannot give more than one trees");
index 1a0da71a43cfef8818b52a3d27131e340725cef2..d75b3805ea7fe3475564f337bf660d8155909445 100644 (file)
@@ -195,7 +195,6 @@ static void determine_whence(struct wt_status *s)
 static void status_init_config(struct wt_status *s, config_fn_t fn)
 {
        wt_status_prepare(s);
-       gitmodules_config();
        git_config(fn, s);
        determine_whence(s);
        init_diff_ui_defaults();
@@ -336,7 +335,7 @@ static void refresh_cache_or_die(int refresh_flags)
 static const char *prepare_index(int argc, const char **argv, const char *prefix,
                                 const struct commit *current_head, int is_status)
 {
-       struct string_list partial;
+       struct string_list partial = STRING_LIST_INIT_DUP;
        struct pathspec pathspec;
        int refresh_flags = REFRESH_QUIET;
        const char *ret;
@@ -381,7 +380,8 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
                        warning(_("Failed to update main cache tree"));
 
                commit_style = COMMIT_NORMAL;
-               return get_lock_file_path(&index_lock);
+               ret = get_lock_file_path(&index_lock);
+               goto out;
        }
 
        /*
@@ -404,7 +404,8 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
                if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
                        die(_("unable to write new_index file"));
                commit_style = COMMIT_NORMAL;
-               return get_lock_file_path(&index_lock);
+               ret = get_lock_file_path(&index_lock);
+               goto out;
        }
 
        /*
@@ -430,7 +431,8 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
                        rollback_lock_file(&index_lock);
                }
                commit_style = COMMIT_AS_IS;
-               return get_index_file();
+               ret = get_index_file();
+               goto out;
        }
 
        /*
@@ -461,7 +463,6 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
                        die(_("cannot do a partial commit during a cherry-pick."));
        }
 
-       string_list_init(&partial, 1);
        if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, &pathspec))
                exit(1);
 
@@ -491,6 +492,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
        discard_cache();
        ret = get_lock_file_path(&false_lock);
        read_cache_from(ret);
+out:
+       string_list_clear(&partial, 0);
+       clear_pathspec(&pathspec);
        return ret;
 }
 
@@ -510,7 +514,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
        s->index_file = index_file;
        s->fp = fp;
        s->nowarn = nowarn;
-       s->is_initial = get_sha1(s->reference, oid.hash) ? 1 : 0;
+       s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
        if (!s->is_initial)
                hashcpy(s->sha1_commit, oid.hash);
        s->status_format = status_format;
@@ -891,7 +895,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                if (amend)
                        parent = "HEAD^1";
 
-               if (get_sha1(parent, oid.hash)) {
+               if (get_oid(parent, &oid)) {
                        int i, ita_nr = 0;
 
                        for (i = 0; i < active_nr; i++)
@@ -940,13 +944,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                return 0;
        }
 
-       /*
-        * Re-read the index as pre-commit hook could have updated it,
-        * and write it out as a tree.  We must do this before we invoke
-        * the editor and after we invoke run_status above.
-        */
-       discard_cache();
+       if (!no_verify && find_hook("pre-commit")) {
+               /*
+                * Re-read the index as pre-commit hook could have updated it,
+                * and write it out as a tree.  We must do this before we invoke
+                * the editor and after we invoke run_status above.
+                */
+               discard_cache();
+       }
        read_cache_from(index_file);
+
        if (update_main_cache_tree(0)) {
                error(_("Error building trees"));
                return 0;
@@ -1385,9 +1392,12 @@ 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_sha1(s.reference, oid.hash) ? 1 : 0;
+       s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
        if (!s.is_initial)
                hashcpy(s.sha1_commit, oid.hash);
 
@@ -1429,7 +1439,6 @@ static void print_summary(const char *prefix, const struct object_id *oid,
        struct rev_info rev;
        struct commit *commit;
        struct strbuf format = STRBUF_INIT;
-       struct object_id junk_oid;
        const char *head;
        struct pretty_print_context pctx = {0};
        struct strbuf author_ident = STRBUF_INIT;
@@ -1482,7 +1491,7 @@ static void print_summary(const char *prefix, const struct object_id *oid,
        rev.diffopt.break_opt = 0;
        diff_setup_done(&rev.diffopt);
 
-       head = resolve_ref_unsafe("HEAD", 0, junk_oid.hash, NULL);
+       head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
        if (!strcmp(head, "HEAD"))
                head = _("detached HEAD");
        else
@@ -1657,7 +1666,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
        s.colopts = 0;
 
-       if (get_sha1("HEAD", oid.hash))
+       if (get_oid("HEAD", &oid))
                current_head = NULL;
        else {
                current_head = lookup_commit_or_die(&oid, "HEAD");
@@ -1816,6 +1825,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        if (!quiet)
                print_summary(prefix, &oid, !current_head);
 
-       strbuf_release(&err);
+       UNLEAK(err);
+       UNLEAK(sb);
        return 0;
 }
index 70ff231e9c5bce550e7af20ddfe91b8b68ee32ae..d13daeeb55927758ceec816f39412123d2ce5846 100644 (file)
@@ -518,10 +518,13 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                        die("$HOME not set");
 
                if (access_or_warn(user_config, R_OK, 0) &&
-                   xdg_config && !access_or_warn(xdg_config, R_OK, 0))
+                   xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
                        given_config_source.file = xdg_config;
-               else
+                       free(user_config);
+               } else {
                        given_config_source.file = user_config;
+                       free(xdg_config);
+               }
        }
        else if (use_system_config)
                given_config_source.file = git_etc_gitconfig();
@@ -628,6 +631,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_write();
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
+               UNLEAK(value);
                ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
                if (ret == CONFIG_NOTHING_SET)
                        error(_("cannot overwrite multiple values with a single value\n"
@@ -638,6 +642,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_write();
                check_argc(argc, 2, 3);
                value = normalize_value(argv[0], argv[1]);
+               UNLEAK(value);
                return git_config_set_multivar_in_file_gently(given_config_source.file,
                                                              argv[0], value, argv[2], 0);
        }
@@ -645,6 +650,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_write();
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
+               UNLEAK(value);
                return git_config_set_multivar_in_file_gently(given_config_source.file,
                                                              argv[0], value,
                                                              CONFIG_REGEX_NONE, 0);
@@ -653,6 +659,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_write();
                check_argc(argc, 2, 3);
                value = normalize_value(argv[0], argv[1]);
+               UNLEAK(value);
                return git_config_set_multivar_in_file_gently(given_config_source.file,
                                                              argv[0], value, argv[2], 1);
        }
index 1d82e61f2a6391bd93a6c3dd91a2515189ff79cc..33343818c830bf64452640b14b2ce7b876221022 100644 (file)
@@ -10,6 +10,7 @@
 #include "builtin.h"
 #include "parse-options.h"
 #include "quote.h"
+#include "packfile.h"
 
 static unsigned long garbage;
 static off_t size_garbage;
index 89ea1cdd60a215473ecb52ff2a4ce51932e1cf6d..29075dbd0f884513420b646cdb06d7c0b3d33643 100644 (file)
@@ -55,10 +55,13 @@ static const char *prio_names[] = {
 };
 
 static int commit_name_cmp(const void *unused_cmp_data,
-                          const struct commit_name *cn1,
-                          const struct commit_name *cn2,
+                          const void *entry,
+                          const void *entry_or_key,
                           const void *peeled)
 {
+       const struct commit_name *cn1 = entry;
+       const struct commit_name *cn2 = entry_or_key;
+
        return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
 }
 
@@ -126,13 +129,24 @@ static void add_to_known_names(const char *path,
 
 static int get_name(const char *path, const struct object_id *oid, int flag, void *cb_data)
 {
-       int is_tag = starts_with(path, "refs/tags/");
+       int is_tag = 0;
        struct object_id peeled;
        int is_annotated, prio;
-
-       /* Reject anything outside refs/tags/ unless --all */
-       if (!all && !is_tag)
+       const char *path_to_match = NULL;
+
+       if (skip_prefix(path, "refs/tags/", &path_to_match)) {
+               is_tag = 1;
+       } else if (all) {
+               if ((exclude_patterns.nr || patterns.nr) &&
+                   !skip_prefix(path, "refs/heads/", &path_to_match) &&
+                   !skip_prefix(path, "refs/remotes/", &path_to_match)) {
+                       /* Only accept reference of known type if there are match/exclude patterns */
+                       return 0;
+               }
+       } else {
+               /* Reject anything outside refs/tags/ unless --all */
                return 0;
+       }
 
        /*
         * If we're given exclude patterns, first exclude any tag which match
@@ -141,11 +155,8 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
        if (exclude_patterns.nr) {
                struct string_list_item *item;
 
-               if (!is_tag)
-                       return 0;
-
                for_each_string_list_item(item, &exclude_patterns) {
-                       if (!wildmatch(item->string, path + 10, 0))
+                       if (!wildmatch(item->string, path_to_match, 0))
                                return 0;
                }
        }
@@ -155,18 +166,18 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
         * pattern.
         */
        if (patterns.nr) {
+               int found = 0;
                struct string_list_item *item;
 
-               if (!is_tag)
-                       return 0;
-
                for_each_string_list_item(item, &patterns) {
-                       if (!wildmatch(item->string, path + 10, 0))
+                       if (!wildmatch(item->string, path_to_match, 0)) {
+                               found = 1;
                                break;
+                       }
+               }
 
-                       /* If we get here, no pattern matched. */
+               if (!found)
                        return 0;
-               }
        }
 
        /* Is it annotated? */
@@ -503,9 +514,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                return cmd_name_rev(args.argc, args.argv, prefix);
        }
 
-       hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
+       hashmap_init(&names, commit_name_cmp, NULL, 0);
        for_each_rawref(get_name, NULL);
-       if (!names.size && !always)
+       if (!hashmap_get_size(&names) && !always)
                die(_("No names found, cannot describe anything."));
 
        if (argc == 0) {
index 17bf84d18f802d3f223e7408fee644b94878b35f..e88493ffe5d5d66461edb71832b58e8b3b4b2bc8 100644 (file)
@@ -26,7 +26,6 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
 
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
        init_revisions(&rev, prefix);
-       gitmodules_config();
        rev.abbrev = 0;
        precompose_argv(argc, argv);
 
index 185e6f9b582fdcf15072038b570463a0cfb1bbf2..522f4fdffd064de2e50232934cb7d616246c92e4 100644 (file)
@@ -23,7 +23,6 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
 
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
        init_revisions(&rev, prefix);
-       gitmodules_config();
        rev.abbrev = 0;
        precompose_argv(argc, argv);
 
@@ -57,5 +56,6 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
                return -1;
        }
        result = run_diff_index(&rev, cached);
+       UNLEAK(rev);
        return diff_result_code(&rev.diffopt, result);
 }
index 31d2cb410738d335d9f6431d6901ea1c0f8b67ff..d66499909e82e255a8d1356b606f9b901935317f 100644 (file)
@@ -110,7 +110,6 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
 
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
        init_revisions(opt, prefix);
-       gitmodules_config();
        opt->abbrev = 0;
        opt->diff = 1;
        opt->disable_stdin = 1;
index 7cde6abbcf7651af8313bd3d70eb1944e72c9cb3..f5bbd4d757e12e6e34a86fac006298acb771909f 100644 (file)
@@ -315,8 +315,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
                        no_index = DIFF_NO_INDEX_IMPLICIT;
        }
 
-       if (!no_index)
-               gitmodules_config();
        init_diff_ui_defaults();
        git_config(git_diff_ui_config, NULL);
        precompose_argv(argc, argv);
@@ -466,5 +464,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
        result = diff_result_code(&rev.diffopt, result);
        if (1 < rev.diffopt.skip_stat_unmatch)
                refresh_index_quietly();
+       UNLEAK(rev);
+       UNLEAK(ent);
+       UNLEAK(blob);
        return result;
 }
index a1a26ba8912f4ee49527b287657826440fd92e9e..b2d3ba7539d5b9cdc7ec6f910ee24510e709f76c 100644 (file)
@@ -111,7 +111,7 @@ static int use_wt_file(const char *workdir, const char *name,
                int fd = open(buf.buf, O_RDONLY);
 
                if (fd >= 0 &&
-                   !index_fd(wt_oid.hash, fd, &st, OBJ_BLOB, name, 0)) {
+                   !index_fd(&wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
                        if (is_null_oid(oid)) {
                                oidcpy(oid, &wt_oid);
                                use = 1;
@@ -131,10 +131,12 @@ struct working_tree_entry {
 };
 
 static int working_tree_entry_cmp(const void *unused_cmp_data,
-                                 struct working_tree_entry *a,
-                                 struct working_tree_entry *b,
-                                 void *unused_keydata)
+                                 const void *entry,
+                                 const void *entry_or_key,
+                                 const void *unused_keydata)
 {
+       const struct working_tree_entry *a = entry;
+       const struct working_tree_entry *b = entry_or_key;
        return strcmp(a->path, b->path);
 }
 
@@ -149,9 +151,13 @@ struct pair_entry {
 };
 
 static int pair_cmp(const void *unused_cmp_data,
-                   struct pair_entry *a, struct pair_entry *b,
-                   void *unused_keydata)
+                   const void *entry,
+                   const void *entry_or_key,
+                   const void *unused_keydata)
 {
+       const struct pair_entry *a = entry;
+       const struct pair_entry *b = entry_or_key;
+
        return strcmp(a->path, b->path);
 }
 
@@ -179,9 +185,13 @@ struct path_entry {
 };
 
 static int path_entry_cmp(const void *unused_cmp_data,
-                         struct path_entry *a, struct path_entry *b,
-                         void *key)
+                         const void *entry,
+                         const void *entry_or_key,
+                         const void *key)
 {
+       const struct path_entry *a = entry;
+       const struct path_entry *b = entry_or_key;
+
        return strcmp(a->path, key ? key : b->path);
 }
 
@@ -372,10 +382,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
        rdir_len = rdir.len;
        wtdir_len = wtdir.len;
 
-       hashmap_init(&working_tree_dups,
-                    (hashmap_cmp_fn)working_tree_entry_cmp, NULL, 0);
-       hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, NULL, 0);
-       hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, NULL, 0);
+       hashmap_init(&working_tree_dups, working_tree_entry_cmp, NULL, 0);
+       hashmap_init(&submodules, pair_cmp, NULL, 0);
+       hashmap_init(&symlinks2, pair_cmp, NULL, 0);
 
        child.no_stdin = 1;
        child.git_cmd = 1;
@@ -585,10 +594,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
         * in the common case of --symlinks and the difftool updating
         * files through the symlink.
         */
-       hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
-                    NULL, wtindex.cache_nr);
-       hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
-                    NULL, wtindex.cache_nr);
+       hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
+       hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
 
        for (i = 0; i < wtindex.cache_nr; i++) {
                struct hashmap_entry dummy;
index d412c0a8f354659acade8ea6271f5c60826a0b3f..2fb60d6d48eae68affdcf64f6dbab0282408f393 100644 (file)
@@ -344,6 +344,7 @@ static void show_filemodify(struct diff_queue_struct *q,
                            struct diff_options *options, void *data)
 {
        int i;
+       struct string_list *changed = data;
 
        /*
         * Handle files below a directory first, in case they are all deleted
@@ -359,20 +360,31 @@ static void show_filemodify(struct diff_queue_struct *q,
                case DIFF_STATUS_DELETED:
                        printf("D ");
                        print_path(spec->path);
+                       string_list_insert(changed, spec->path);
                        putchar('\n');
                        break;
 
                case DIFF_STATUS_COPIED:
                case DIFF_STATUS_RENAMED:
-                       printf("%c ", q->queue[i]->status);
-                       print_path(ospec->path);
-                       putchar(' ');
-                       print_path(spec->path);
-                       putchar('\n');
-
-                       if (!oidcmp(&ospec->oid, &spec->oid) &&
-                           ospec->mode == spec->mode)
-                               break;
+                       /*
+                        * If a change in the file corresponding to ospec->path
+                        * has been observed, we cannot trust its contents
+                        * because the diff is calculated based on the prior
+                        * contents, not the current contents.  So, declare a
+                        * copy or rename only if there was no change observed.
+                        */
+                       if (!string_list_has_string(changed, ospec->path)) {
+                               printf("%c ", q->queue[i]->status);
+                               print_path(ospec->path);
+                               putchar(' ');
+                               print_path(spec->path);
+                               string_list_insert(changed, spec->path);
+                               putchar('\n');
+
+                               if (!oidcmp(&ospec->oid, &spec->oid) &&
+                                   ospec->mode == spec->mode)
+                                       break;
+                       }
                        /* fallthrough */
 
                case DIFF_STATUS_TYPE_CHANGED:
@@ -393,6 +405,7 @@ static void show_filemodify(struct diff_queue_struct *q,
                                       get_object_mark(object));
                        }
                        print_path(spec->path);
+                       string_list_insert(changed, spec->path);
                        putchar('\n');
                        break;
 
@@ -528,7 +541,8 @@ static void anonymize_ident_line(const char **beg, const char **end)
        *end = out->buf + out->len;
 }
 
-static void handle_commit(struct commit *commit, struct rev_info *rev)
+static void handle_commit(struct commit *commit, struct rev_info *rev,
+                         struct string_list *paths_of_changed_objects)
 {
        int saved_output_format = rev->diffopt.output_format;
        const char *commit_buffer;
@@ -615,6 +629,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
        if (full_tree)
                printf("deleteall\n");
        log_tree_diff_flush(rev);
+       string_list_clear(paths_of_changed_objects, 0);
        rev->diffopt.output_format = saved_output_format;
 
        printf("\n");
@@ -630,15 +645,15 @@ static void *anonymize_tag(const void *old, size_t *len)
        return strbuf_detach(&out, len);
 }
 
-static void handle_tail(struct object_array *commits, struct rev_info *revs)
+static void handle_tail(struct object_array *commits, struct rev_info *revs,
+                       struct string_list *paths_of_changed_objects)
 {
        struct commit *commit;
        while (commits->nr) {
-               commit = (struct commit *)commits->objects[commits->nr - 1].item;
+               commit = (struct commit *)object_array_pop(commits);
                if (has_unshown_parent(commit))
                        return;
-               handle_commit(commit, revs);
-               commits->nr--;
+               handle_commit(commit, revs, paths_of_changed_objects);
        }
 }
 
@@ -977,6 +992,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
        char *export_filename = NULL, *import_filename = NULL;
        uint32_t lastimportid;
        struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
+       struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
        struct option options[] = {
                OPT_INTEGER(0, "progress", &progress,
                            N_("show progress after <n> objects")),
@@ -1049,14 +1065,15 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
        if (prepare_revision_walk(&revs))
                die("revision walk setup failed");
        revs.diffopt.format_callback = show_filemodify;
+       revs.diffopt.format_callback_data = &paths_of_changed_objects;
        DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
        while ((commit = get_revision(&revs))) {
                if (has_unshown_parent(commit)) {
                        add_object_array(&commit->object, NULL, &commits);
                }
                else {
-                       handle_commit(commit, &revs);
-                       handle_tail(&commits, &revs);
+                       handle_commit(commit, &revs, &paths_of_changed_objects);
+                       handle_tail(&commits, &revs, &paths_of_changed_objects);
                }
        }
 
index c87e59f3b1def1f064e0dae54a14e310c06df1fa..225c734924f14854784cbc1e310f2fb817f1f38b 100644 (file)
@@ -17,6 +17,7 @@
 #include "connected.h"
 #include "argv-array.h"
 #include "utf8.h"
+#include "packfile.h"
 
 static const char * const builtin_fetch_usage[] = {
        N_("git fetch [<options>] [<repository> [<refspec>...]]"),
@@ -39,7 +40,7 @@ static int prune = -1; /* unspecified */
 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
 static int progress = -1;
 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
-static int max_children = -1;
+static int max_children = 1;
 static enum transport_family family;
 static const char *depth;
 static const char *deepen_since;
@@ -68,9 +69,30 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
                recurse_submodules = r;
        }
 
+       if (!strcmp(k, "submodule.fetchjobs")) {
+               max_children = parse_submodule_fetchjobs(k, v);
+               return 0;
+       } else if (!strcmp(k, "fetch.recursesubmodules")) {
+               recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
+               return 0;
+       }
+
        return git_default_config(k, v, cb);
 }
 
+static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
+{
+       if (!strcmp(var, "submodule.fetchjobs")) {
+               max_children = parse_submodule_fetchjobs(var, value);
+               return 0;
+       } else if (!strcmp(var, "fetch.recursesubmodules")) {
+               recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
+               return 0;
+       }
+
+       return 0;
+}
+
 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
 {
        ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
@@ -1311,6 +1333,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
        for (i = 1; i < argc; i++)
                strbuf_addf(&default_rla, " %s", argv[i]);
 
+       config_from_gitmodules(gitmodules_fetch_config, NULL);
        git_config(git_fetch_config, NULL);
 
        argc = parse_options(argc, argv, prefix,
@@ -1338,12 +1361,6 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
        if (depth || deepen_since || deepen_not.nr)
                deepen = 1;
 
-       if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
-               set_config_fetch_recurse_submodules(recurse_submodules_default);
-               gitmodules_config();
-               git_config(submodule_config, NULL);
-       }
-
        if (all) {
                if (argc == 1)
                        die(_("fetch --all does not take a repository argument"));
@@ -1383,6 +1400,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
                result = fetch_populated_submodules(&options,
                                                    submodule_prefix,
                                                    recurse_submodules,
+                                                   recurse_submodules_default,
                                                    verbosity < 0,
                                                    max_children);
                argv_array_clear(&options);
index 5d7c921a773718d6bee7f85b8d868fc22602377d..e931be9ce4d9f180de23d09d227e5a2f571f4f1a 100644 (file)
@@ -36,6 +36,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
                OPT_GROUP(""),
                OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
                OPT_STRING(  0 , "format", &format.format, N_("format"), N_("format to use for the output")),
+               OPT__COLOR(&format.use_color, N_("respect format colors")),
                OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
                            N_("field name to sort on"), &parse_opt_ref_sorting),
                OPT_CALLBACK(0, "points-at", &filter.points_at,
index d18244ab546bae863e6ef81d58663dc5c27c49c0..56afe405b8072b3099a23661ac586f673e54ff0e 100644 (file)
@@ -15,6 +15,7 @@
 #include "progress.h"
 #include "streaming.h"
 #include "decorate.h"
+#include "packfile.h"
 
 #define REACHABLE 0x0001
 #define SEEN      0x0002
@@ -179,14 +180,9 @@ static int traverse_reachable(void)
        unsigned int nr = 0;
        int result = 0;
        if (show_progress)
-               progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
+               progress = start_delayed_progress(_("Checking connectivity"), 0);
        while (pending.nr) {
-               struct object_array_entry *entry;
-               struct object *obj;
-
-               entry = pending.objects + --pending.nr;
-               obj = entry->item;
-               result |= traverse_one_object(obj);
+               result |= traverse_one_object(object_array_pop(&pending));
                display_progress(progress, ++nr);
        }
        stop_progress(&progress);
@@ -730,12 +726,12 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 
        for (i = 0; i < argc; i++) {
                const char *arg = argv[i];
-               unsigned char sha1[20];
-               if (!get_sha1(arg, sha1)) {
-                       struct object *obj = lookup_object(sha1);
+               struct object_id oid;
+               if (!get_oid(arg, &oid)) {
+                       struct object *obj = lookup_object(oid.hash);
 
                        if (!obj || !(obj->flags & HAS_OBJ)) {
-                               error("%s: object missing", sha1_to_hex(sha1));
+                               error("%s: object missing", oid_to_hex(&oid));
                                errors_found |= ERROR_OBJECT;
                                continue;
                        }
index e6b84475aef6aeab05beda137fb2882f663fac0f..3c5eae0edf12e461e84c71c0e2988fcfa58d3d44 100644 (file)
@@ -19,6 +19,7 @@
 #include "sigchain.h"
 #include "argv-array.h"
 #include "commit.h"
+#include "packfile.h"
 
 #define FAILED_RUN "failed to run %s"
 
@@ -46,7 +47,7 @@ static struct argv_array prune = ARGV_ARRAY_INIT;
 static struct argv_array prune_worktrees = ARGV_ARRAY_INIT;
 static struct argv_array rerere = ARGV_ARRAY_INIT;
 
-static struct tempfile pidfile;
+static struct tempfile *pidfile;
 static struct lock_file log_lock;
 
 static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
@@ -77,7 +78,7 @@ static void process_log_file(void)
                 */
                int saved_errno = errno;
                fprintf(stderr, _("Failed to fstat %s: %s"),
-                       get_tempfile_path(&log_lock.tempfile),
+                       get_tempfile_path(log_lock.tempfile),
                        strerror(saved_errno));
                fflush(stderr);
                commit_lock_file(&log_lock);
@@ -241,7 +242,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        int fd;
        char *pidfile_path;
 
-       if (is_tempfile_active(&pidfile))
+       if (is_tempfile_active(pidfile))
                /* already locked */
                return NULL;
 
@@ -257,7 +258,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
                int should_exit;
 
                if (!scan_fmt)
-                       scan_fmt = xstrfmt("%s %%%dc", "%"SCNuMAX, HOST_NAME_MAX);
+                       scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
                fp = fopen(pidfile_path, "r");
                memset(locking_host, 0, sizeof(locking_host));
                should_exit =
@@ -292,7 +293,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        write_in_full(fd, sb.buf, sb.len);
        strbuf_release(&sb);
        commit_lock_file(&lock);
-       register_tempfile(&pidfile, pidfile_path);
+       pidfile = register_tempfile(pidfile_path);
        free(pidfile_path);
        return NULL;
 }
index e21c5416cd478eab3b0dddcba5ca9eeefad83d34..2706fcfaf2261e2ac2eaff5a054d0dd7a9291c0b 100644 (file)
@@ -26,15 +26,16 @@ 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))
                return 1;
 
-       n = write_in_full(1, comment, 41);
-       if (n < 41)
+       if (write_in_full(1, comment, 41) < 0)
                die_errno("git get-tar-commit-id: write error");
 
        return 0;
index 42ff87065a0e72c999488d2844463f901fd73a5c..19e23946ac4bfe53dd0f590fe26250b1f789778b 100644 (file)
@@ -28,13 +28,7 @@ static char const * const grep_usage[] = {
        NULL
 };
 
-static const char *super_prefix;
 static int recurse_submodules;
-static struct argv_array submodule_options = ARGV_ARRAY_INIT;
-static const char *parent_basename;
-
-static int grep_submodule_launch(struct grep_opt *opt,
-                                const struct grep_source *gs);
 
 #define GREP_NUM_THREADS_DEFAULT 8
 static int num_threads;
@@ -186,10 +180,7 @@ static void *run(void *arg)
                        break;
 
                opt->output_priv = w;
-               if (w->source.type == GREP_SOURCE_SUBMODULE)
-                       hit |= grep_submodule_launch(opt, &w->source);
-               else
-                       hit |= grep_source(opt, &w->source);
+               hit |= grep_source(opt, &w->source);
                grep_source_clear_data(&w->source);
                work_done(w);
        }
@@ -327,21 +318,13 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
 {
        struct strbuf pathbuf = STRBUF_INIT;
 
-       if (super_prefix) {
-               strbuf_add(&pathbuf, filename, tree_name_len);
-               strbuf_addstr(&pathbuf, super_prefix);
-               strbuf_addstr(&pathbuf, filename + tree_name_len);
+       if (opt->relative && opt->prefix_length) {
+               quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
+               strbuf_insert(&pathbuf, 0, filename, tree_name_len);
        } else {
                strbuf_addstr(&pathbuf, filename);
        }
 
-       if (opt->relative && opt->prefix_length) {
-               char *name = strbuf_detach(&pathbuf, NULL);
-               quote_path_relative(name + tree_name_len, opt->prefix, &pathbuf);
-               strbuf_insert(&pathbuf, 0, name, tree_name_len);
-               free(name);
-       }
-
 #ifndef NO_PTHREADS
        if (num_threads) {
                add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
@@ -366,15 +349,10 @@ static int grep_file(struct grep_opt *opt, const char *filename)
 {
        struct strbuf buf = STRBUF_INIT;
 
-       if (super_prefix)
-               strbuf_addstr(&buf, super_prefix);
-       strbuf_addstr(&buf, filename);
-
-       if (opt->relative && opt->prefix_length) {
-               char *name = strbuf_detach(&buf, NULL);
-               quote_path_relative(name, opt->prefix, &buf);
-               free(name);
-       }
+       if (opt->relative && opt->prefix_length)
+               quote_path_relative(filename, opt->prefix, &buf);
+       else
+               strbuf_addstr(&buf, filename);
 
 #ifndef NO_PTHREADS
        if (num_threads) {
@@ -421,284 +399,89 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
                exit(status);
 }
 
-static void compile_submodule_options(const struct grep_opt *opt,
-                                     const char **argv,
-                                     int cached, int untracked,
-                                     int opt_exclude, int use_index,
-                                     int pattern_type_arg)
-{
-       struct grep_pat *pattern;
-
-       if (recurse_submodules)
-               argv_array_push(&submodule_options, "--recurse-submodules");
-
-       if (cached)
-               argv_array_push(&submodule_options, "--cached");
-       if (!use_index)
-               argv_array_push(&submodule_options, "--no-index");
-       if (untracked)
-               argv_array_push(&submodule_options, "--untracked");
-       if (opt_exclude > 0)
-               argv_array_push(&submodule_options, "--exclude-standard");
-
-       if (opt->invert)
-               argv_array_push(&submodule_options, "-v");
-       if (opt->ignore_case)
-               argv_array_push(&submodule_options, "-i");
-       if (opt->word_regexp)
-               argv_array_push(&submodule_options, "-w");
-       switch (opt->binary) {
-       case GREP_BINARY_NOMATCH:
-               argv_array_push(&submodule_options, "-I");
-               break;
-       case GREP_BINARY_TEXT:
-               argv_array_push(&submodule_options, "-a");
-               break;
-       default:
-               break;
-       }
-       if (opt->allow_textconv)
-               argv_array_push(&submodule_options, "--textconv");
-       if (opt->max_depth != -1)
-               argv_array_pushf(&submodule_options, "--max-depth=%d",
-                                opt->max_depth);
-       if (opt->linenum)
-               argv_array_push(&submodule_options, "-n");
-       if (!opt->pathname)
-               argv_array_push(&submodule_options, "-h");
-       if (!opt->relative)
-               argv_array_push(&submodule_options, "--full-name");
-       if (opt->name_only)
-               argv_array_push(&submodule_options, "-l");
-       if (opt->unmatch_name_only)
-               argv_array_push(&submodule_options, "-L");
-       if (opt->null_following_name)
-               argv_array_push(&submodule_options, "-z");
-       if (opt->count)
-               argv_array_push(&submodule_options, "-c");
-       if (opt->file_break)
-               argv_array_push(&submodule_options, "--break");
-       if (opt->heading)
-               argv_array_push(&submodule_options, "--heading");
-       if (opt->pre_context)
-               argv_array_pushf(&submodule_options, "--before-context=%d",
-                                opt->pre_context);
-       if (opt->post_context)
-               argv_array_pushf(&submodule_options, "--after-context=%d",
-                                opt->post_context);
-       if (opt->funcname)
-               argv_array_push(&submodule_options, "-p");
-       if (opt->funcbody)
-               argv_array_push(&submodule_options, "-W");
-       if (opt->all_match)
-               argv_array_push(&submodule_options, "--all-match");
-       if (opt->debug)
-               argv_array_push(&submodule_options, "--debug");
-       if (opt->status_only)
-               argv_array_push(&submodule_options, "-q");
-
-       switch (pattern_type_arg) {
-       case GREP_PATTERN_TYPE_BRE:
-               argv_array_push(&submodule_options, "-G");
-               break;
-       case GREP_PATTERN_TYPE_ERE:
-               argv_array_push(&submodule_options, "-E");
-               break;
-       case GREP_PATTERN_TYPE_FIXED:
-               argv_array_push(&submodule_options, "-F");
-               break;
-       case GREP_PATTERN_TYPE_PCRE:
-               argv_array_push(&submodule_options, "-P");
-               break;
-       case GREP_PATTERN_TYPE_UNSPECIFIED:
-               break;
-       default:
-               die("BUG: Added a new grep pattern type without updating switch statement");
-       }
-
-       for (pattern = opt->pattern_list; pattern != NULL;
-            pattern = pattern->next) {
-               switch (pattern->token) {
-               case GREP_PATTERN:
-                       argv_array_pushf(&submodule_options, "-e%s",
-                                        pattern->pattern);
-                       break;
-               case GREP_AND:
-               case GREP_OPEN_PAREN:
-               case GREP_CLOSE_PAREN:
-               case GREP_NOT:
-               case GREP_OR:
-                       argv_array_push(&submodule_options, pattern->pattern);
-                       break;
-               /* BODY and HEAD are not used by git-grep */
-               case GREP_PATTERN_BODY:
-               case GREP_PATTERN_HEAD:
-                       break;
-               }
-       }
-
-       /*
-        * Limit number of threads for child process to use.
-        * This is to prevent potential fork-bomb behavior of git-grep as each
-        * submodule process has its own thread pool.
-        */
-       argv_array_pushf(&submodule_options, "--threads=%d",
-                        DIV_ROUND_UP(num_threads, 2));
-
-       /* Add Pathspecs */
-       argv_array_push(&submodule_options, "--");
-       for (; *argv; argv++)
-               argv_array_push(&submodule_options, *argv);
-}
+static int grep_cache(struct grep_opt *opt, struct repository *repo,
+                     const struct pathspec *pathspec, int cached);
+static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
+                    struct tree_desc *tree, struct strbuf *base, int tn_len,
+                    int check_attr, struct repository *repo);
 
-/*
- * Launch child process to grep contents of a submodule
- */
-static int grep_submodule_launch(struct grep_opt *opt,
-                                const struct grep_source *gs)
+static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
+                         const struct pathspec *pathspec,
+                         const struct object_id *oid,
+                         const char *filename, const char *path)
 {
-       struct child_process cp = CHILD_PROCESS_INIT;
-       int status, i;
-       const char *end_of_base;
-       const char *name;
-       struct strbuf child_output = STRBUF_INIT;
-
-       end_of_base = strchr(gs->name, ':');
-       if (gs->identifier && end_of_base)
-               name = end_of_base + 1;
-       else
-               name = gs->name;
+       struct repository submodule;
+       int hit;
 
-       prepare_submodule_repo_env(&cp.env_array);
-       argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
+       if (!is_submodule_active(superproject, path))
+               return 0;
 
-       if (opt->relative && opt->prefix_length)
-               argv_array_pushf(&cp.env_array, "%s=%s",
-                                GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
-                                opt->prefix);
+       if (repo_submodule_init(&submodule, superproject, path))
+               return 0;
 
-       /* Add super prefix */
-       argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
-                        super_prefix ? super_prefix : "",
-                        name);
-       argv_array_push(&cp.args, "grep");
+       repo_read_gitmodules(&submodule);
 
        /*
-        * Add basename of parent project
-        * When performing grep on a tree object the filename is prefixed
-        * with the object's name: 'tree-name:filename'.  In order to
-        * provide uniformity of output we want to pass the name of the
-        * parent project's object name to the submodule so the submodule can
-        * prefix its output with the parent's name and not its own OID.
+        * NEEDSWORK: This adds the submodule's object directory to the list of
+        * alternates for the single in-memory object store.  This has some bad
+        * consequences for memory (processed objects will never be freed) and
+        * performance (this increases the number of pack files git has to pay
+        * attention to, to the sum of the number of pack files in all the
+        * repositories processed so far).  This can be removed once the object
+        * store is no longer global and instead is a member of the repository
+        * object.
         */
-       if (gs->identifier && end_of_base)
-               argv_array_pushf(&cp.args, "--parent-basename=%.*s",
-                                (int) (end_of_base - gs->name),
-                                gs->name);
+       add_to_alternates_memory(submodule.objectdir);
 
-       /* Add options */
-       for (i = 0; i < submodule_options.argc; i++) {
-               /*
-                * If there is a tree identifier for the submodule, add the
-                * rev after adding the submodule options but before the
-                * pathspecs.  To do this we listen for the '--' and insert the
-                * oid before pushing the '--' onto the child process argv
-                * array.
-                */
-               if (gs->identifier &&
-                   !strcmp("--", submodule_options.argv[i])) {
-                       argv_array_push(&cp.args, oid_to_hex(gs->identifier));
-               }
+       if (oid) {
+               struct object *object;
+               struct tree_desc tree;
+               void *data;
+               unsigned long size;
+               struct strbuf base = STRBUF_INIT;
 
-               argv_array_push(&cp.args, submodule_options.argv[i]);
-       }
+               object = parse_object_or_die(oid, oid_to_hex(oid));
 
-       cp.git_cmd = 1;
-       cp.dir = gs->path;
+               grep_read_lock();
+               data = read_object_with_reference(object->oid.hash, tree_type,
+                                                 &size, NULL);
+               grep_read_unlock();
 
-       /*
-        * Capture output to output buffer and check the return code from the
-        * child process.  A '0' indicates a hit, a '1' indicates no hit and
-        * anything else is an error.
-        */
-       status = capture_command(&cp, &child_output, 0);
-       if (status && (status != 1)) {
-               /* flush the buffer */
-               write_or_die(1, child_output.buf, child_output.len);
-               die("process for submodule '%s' failed with exit code: %d",
-                   gs->name, status);
-       }
+               if (!data)
+                       die(_("unable to read tree (%s)"), oid_to_hex(&object->oid));
 
-       opt->output(opt, child_output.buf, child_output.len);
-       strbuf_release(&child_output);
-       /* invert the return code to make a hit equal to 1 */
-       return !status;
-}
+               strbuf_addstr(&base, filename);
+               strbuf_addch(&base, '/');
 
-/*
- * Prep grep structures for a submodule grep
- * oid: the oid of the submodule or NULL if using the working tree
- * filename: name of the submodule including tree name of parent
- * path: location of the submodule
- */
-static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
-                         const char *filename, const char *path)
-{
-       if (!is_submodule_active(the_repository, path))
-               return 0;
-       if (!is_submodule_populated_gently(path, NULL)) {
-               /*
-                * If searching history, check for the presence of the
-                * submodule's gitdir before skipping the submodule.
-                */
-               if (oid) {
-                       const struct submodule *sub =
-                                       submodule_from_path(null_sha1, path);
-                       if (sub)
-                               path = git_path("modules/%s", sub->name);
-
-                       if (!(is_directory(path) && is_git_directory(path)))
-                               return 0;
-               } else {
-                       return 0;
-               }
+               init_tree_desc(&tree, data, size);
+               hit = grep_tree(opt, pathspec, &tree, &base, base.len,
+                               object->type == OBJ_COMMIT, &submodule);
+               strbuf_release(&base);
+               free(data);
+       } else {
+               hit = grep_cache(opt, &submodule, pathspec, 1);
        }
 
-#ifndef NO_PTHREADS
-       if (num_threads) {
-               add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, oid);
-               return 0;
-       } else
-#endif
-       {
-               struct grep_source gs;
-               int hit;
-
-               grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
-                                filename, path, oid);
-               hit = grep_submodule_launch(opt, &gs);
-
-               grep_source_clear(&gs);
-               return hit;
-       }
+       repo_clear(&submodule);
+       return hit;
 }
 
-static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec,
-                     int cached)
+static int grep_cache(struct grep_opt *opt, struct repository *repo,
+                     const struct pathspec *pathspec, int cached)
 {
        int hit = 0;
        int nr;
        struct strbuf name = STRBUF_INIT;
        int name_base_len = 0;
-       if (super_prefix) {
-               name_base_len = strlen(super_prefix);
-               strbuf_addstr(&name, super_prefix);
+       if (repo->submodule_prefix) {
+               name_base_len = strlen(repo->submodule_prefix);
+               strbuf_addstr(&name, repo->submodule_prefix);
        }
 
-       read_cache();
+       repo_read_index(repo);
 
-       for (nr = 0; nr < active_nr; nr++) {
-               const struct cache_entry *ce = active_cache[nr];
+       for (nr = 0; nr < repo->index->cache_nr; nr++) {
+               const struct cache_entry *ce = repo->index->cache[nr];
                strbuf_setlen(&name, name_base_len);
                strbuf_addstr(&name, ce->name);
 
@@ -715,14 +498,14 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec,
                            ce_skip_worktree(ce)) {
                                if (ce_stage(ce) || ce_intent_to_add(ce))
                                        continue;
-                               hit |= grep_oid(opt, &ce->oid, ce->name,
-                                                0, ce->name);
+                               hit |= grep_oid(opt, &ce->oid, name.buf,
+                                                0, name.buf);
                        } else {
-                               hit |= grep_file(opt, ce->name);
+                               hit |= grep_file(opt, name.buf);
                        }
                } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
                           submodule_path_match(pathspec, name.buf, NULL)) {
-                       hit |= grep_submodule(opt, NULL, ce->name, ce->name);
+                       hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name);
                } else {
                        continue;
                }
@@ -730,8 +513,8 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec,
                if (ce_stage(ce)) {
                        do {
                                nr++;
-                       } while (nr < active_nr &&
-                                !strcmp(ce->name, active_cache[nr]->name));
+                       } while (nr < repo->index->cache_nr &&
+                                !strcmp(ce->name, repo->index->cache[nr]->name));
                        nr--; /* compensate for loop control */
                }
                if (hit && opt->status_only)
@@ -744,7 +527,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec,
 
 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                     struct tree_desc *tree, struct strbuf *base, int tn_len,
-                    int check_attr)
+                    int check_attr, struct repository *repo)
 {
        int hit = 0;
        enum interesting match = entry_not_interesting;
@@ -752,8 +535,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
        int old_baselen = base->len;
        struct strbuf name = STRBUF_INIT;
        int name_base_len = 0;
-       if (super_prefix) {
-               strbuf_addstr(&name, super_prefix);
+       if (repo->submodule_prefix) {
+               strbuf_addstr(&name, repo->submodule_prefix);
                name_base_len = name.len;
        }
 
@@ -791,11 +574,11 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                        strbuf_addch(base, '/');
                        init_tree_desc(&sub, data, size);
                        hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
-                                        check_attr);
+                                        check_attr, repo);
                        free(data);
                } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
-                       hit |= grep_submodule(opt, entry.oid, base->buf,
-                                             base->buf + tn_len);
+                       hit |= grep_submodule(opt, repo, pathspec, entry.oid,
+                                             base->buf, base->buf + tn_len);
                }
 
                strbuf_setlen(base, old_baselen);
@@ -809,7 +592,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 }
 
 static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
-                      struct object *obj, const char *name, const char *path)
+                      struct object *obj, const char *name, const char *path,
+                      struct repository *repo)
 {
        if (obj->type == OBJ_BLOB)
                return grep_oid(opt, &obj->oid, name, 0, path);
@@ -828,10 +612,6 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
                if (!data)
                        die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
 
-               /* Use parent's name as base when recursing submodules */
-               if (recurse_submodules && parent_basename)
-                       name = parent_basename;
-
                len = name ? strlen(name) : 0;
                strbuf_init(&base, PATH_MAX + len + 1);
                if (len) {
@@ -840,7 +620,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
                }
                init_tree_desc(&tree, data, size);
                hit = grep_tree(opt, pathspec, &tree, &base, base.len,
-                               obj->type == OBJ_COMMIT);
+                               obj->type == OBJ_COMMIT, repo);
                strbuf_release(&base);
                free(data);
                return hit;
@@ -849,6 +629,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
 }
 
 static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
+                       struct repository *repo,
                        const struct object_array *list)
 {
        unsigned int i;
@@ -862,9 +643,10 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
                /* load the gitmodules file for this rev */
                if (recurse_submodules) {
                        submodule_free();
-                       gitmodules_config_sha1(real_obj->oid.hash);
+                       gitmodules_config_oid(&real_obj->oid);
                }
-               if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path)) {
+               if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path,
+                               repo)) {
                        hit = 1;
                        if (opt->status_only)
                                break;
@@ -1005,9 +787,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                            N_("ignore files specified via '.gitignore'"), 1),
                OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
                         N_("recursively search in each submodule")),
-               OPT_STRING(0, "parent-basename", &parent_basename,
-                          N_("basename"),
-                          N_("prepend parent project's basename to output")),
                OPT_GROUP(""),
                OPT_BOOL('v', "invert-match", &opt.invert,
                        N_("show non-matching lines")),
@@ -1112,7 +891,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        init_grep_defaults();
        git_config(grep_cmd_config, NULL);
        grep_init(&opt, prefix);
-       super_prefix = get_super_prefix();
 
        /*
         * If there is no -- then the paths must exist in the working
@@ -1205,8 +983,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        break;
                }
 
-               if (get_sha1_with_context(arg, GET_SHA1_RECORD_PATH,
-                                         oid.hash, &oc)) {
+               if (get_oid_with_context(arg, GET_OID_RECORD_PATH,
+                                        &oid, &oc)) {
                        if (seen_dashdash)
                                die(_("unable to resolve revision: %s"), arg);
                        break;
@@ -1270,13 +1048,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        }
 #endif
 
-       if (recurse_submodules) {
-               gitmodules_config();
-               compile_submodule_options(&opt, argv + i, cached, untracked,
-                                         opt_exclude, use_index,
-                                         pattern_type_arg);
-       }
-
        if (show_in_pager && (cached || list.nr))
                die(_("--open-files-in-pager only works on the worktree"));
 
@@ -1318,11 +1089,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                if (!cached)
                        setup_work_tree();
 
-               hit = grep_cache(&opt, &pathspec, cached);
+               hit = grep_cache(&opt, the_repository, &pathspec, cached);
        } else {
                if (cached)
                        die(_("both --cached and trees are given."));
-               hit = grep_objects(&opt, &pathspec, &list);
+
+               hit = grep_objects(&opt, &pathspec, the_repository, &list);
        }
 
        if (num_threads)
index d04baf999a94cfa6a07e74861876d6a9f1c88a6d..c532ff9320c751d1db5475add51f2c3c6a8c7146 100644 (file)
@@ -16,7 +16,7 @@
  * needs to bypass the data conversion performed by, and the type
  * limitation imposed by, index_fd() and its callees.
  */
-static int hash_literally(unsigned char *sha1, int fd, const char *type, unsigned flags)
+static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags)
 {
        struct strbuf buf = STRBUF_INIT;
        int ret;
@@ -24,7 +24,7 @@ static int hash_literally(unsigned char *sha1, int fd, const char *type, unsigne
        if (strbuf_read(&buf, fd, 4096) < 0)
                ret = -1;
        else
-               ret = hash_sha1_file_literally(buf.buf, buf.len, type, sha1, flags);
+               ret = hash_sha1_file_literally(buf.buf, buf.len, type, oid, flags);
        strbuf_release(&buf);
        return ret;
 }
@@ -33,16 +33,16 @@ static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
                    int literally)
 {
        struct stat st;
-       unsigned char sha1[20];
+       struct object_id oid;
 
        if (fstat(fd, &st) < 0 ||
            (literally
-            ? hash_literally(sha1, fd, type, flags)
-            : index_fd(sha1, fd, &st, type_from_string(type), path, flags)))
+            ? hash_literally(&oid, fd, type, flags)
+            : index_fd(&oid, fd, &st, type_from_string(type), path, flags)))
                die((flags & HASH_WRITE_OBJECT)
                    ? "Unable to add %s to database"
                    : "Unable to hash %s", path);
-       printf("%s\n", sha1_to_hex(sha1));
+       printf("%s\n", oid_to_hex(&oid));
        maybe_flush_or_die(stdout, "hash to stdout");
 }
 
index 334a8494abcc4ac52babf468d8a1ef415aa7f297..d3c8fc40820faa10f7c9004560159986b2900e9e 100644 (file)
@@ -131,6 +131,7 @@ static void exec_woman_emacs(const char *path, const char *page)
                strbuf_addf(&man_page, "(woman \"%s\")", page);
                execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
                warning_errno(_("failed to exec '%s'"), path);
+               strbuf_release(&man_page);
        }
 }
 
@@ -152,6 +153,7 @@ static void exec_man_konqueror(const char *path, const char *page)
                strbuf_addf(&man_page, "man:%s(1)", page);
                execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
                warning_errno(_("failed to exec '%s'"), path);
+               strbuf_release(&man_page);
        }
 }
 
@@ -169,6 +171,7 @@ static void exec_man_cmd(const char *cmd, const char *page)
        strbuf_addf(&shell_cmd, "%s %s", cmd, page);
        execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
        warning(_("failed to exec '%s'"), cmd);
+       strbuf_release(&shell_cmd);
 }
 
 static void add_man_viewer(const char *name)
@@ -438,7 +441,7 @@ static const char *check_git_cmd(const char* cmd)
 
        alias = alias_lookup(cmd);
        if (alias) {
-               printf_ln(_("`git %s' is aliased to `%s'"), cmd, alias);
+               printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
                free(alias);
                exit(0);
        }
index 26828c1d82bc9a997041cb464d4a3fde38e2d19d..f2be145e128d819da3adeaed0d0a05218fbe1411 100644 (file)
@@ -12,6 +12,7 @@
 #include "exec_cmd.h"
 #include "streaming.h"
 #include "thread-utils.h"
+#include "packfile.h"
 
 static const char index_pack_usage[] =
 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
index 47823f9aa4452edfa684b042dd5fbaa935df1d39..c9b7946bade9e10f799942137480e71ee3233701 100644 (file)
@@ -579,6 +579,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
                        set_git_work_tree(work_tree);
        }
 
+       UNLEAK(real_git_dir);
+
        flags |= INIT_DB_EXIST_OK;
        return init_db(git_dir, real_git_dir, template_dir, flags);
 }
index 175f14797b101d22ead9d1008744440da66a7c1c..b742539d4de20361bb43bcb4dc33cfc9165c42fb 100644 (file)
@@ -16,34 +16,119 @@ static const char * const git_interpret_trailers_usage[] = {
        NULL
 };
 
+static enum trailer_where where;
+static enum trailer_if_exists if_exists;
+static enum trailer_if_missing if_missing;
+
+static int option_parse_where(const struct option *opt,
+                             const char *arg, int unset)
+{
+       return trailer_set_where(&where, arg);
+}
+
+static int option_parse_if_exists(const struct option *opt,
+                                 const char *arg, int unset)
+{
+       return trailer_set_if_exists(&if_exists, arg);
+}
+
+static int option_parse_if_missing(const struct option *opt,
+                                  const char *arg, int unset)
+{
+       return trailer_set_if_missing(&if_missing, arg);
+}
+
+static void new_trailers_clear(struct list_head *trailers)
+{
+       struct list_head *pos, *tmp;
+       struct new_trailer_item *item;
+
+       list_for_each_safe(pos, tmp, trailers) {
+               item = list_entry(pos, struct new_trailer_item, list);
+               list_del(pos);
+               free(item);
+       }
+}
+
+static int option_parse_trailer(const struct option *opt,
+                                  const char *arg, int unset)
+{
+       struct list_head *trailers = opt->value;
+       struct new_trailer_item *item;
+
+       if (unset) {
+               new_trailers_clear(trailers);
+               return 0;
+       }
+
+       if (!arg)
+               return -1;
+
+       item = xmalloc(sizeof(*item));
+       item->text = arg;
+       item->where = where;
+       item->if_exists = if_exists;
+       item->if_missing = if_missing;
+       list_add_tail(&item->list, trailers);
+       return 0;
+}
+
+static int parse_opt_parse(const struct option *opt, const char *arg,
+                          int unset)
+{
+       struct process_trailer_options *v = opt->value;
+       v->only_trailers = 1;
+       v->only_input = 1;
+       v->unfold = 1;
+       return 0;
+}
+
 int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 {
-       int in_place = 0;
-       int trim_empty = 0;
-       struct string_list trailers = STRING_LIST_INIT_NODUP;
+       struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
+       LIST_HEAD(trailers);
 
        struct option options[] = {
-               OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
-               OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
-               OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
-                               N_("trailer(s) to add")),
+               OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
+               OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
+
+               OPT_CALLBACK(0, "where", NULL, N_("action"),
+                            N_("where to place the new trailer"), option_parse_where),
+               OPT_CALLBACK(0, "if-exists", NULL, N_("action"),
+                            N_("action if trailer already exists"), option_parse_if_exists),
+               OPT_CALLBACK(0, "if-missing", NULL, N_("action"),
+                            N_("action if trailer is missing"), option_parse_if_missing),
+
+               OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")),
+               OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply config rules")),
+               OPT_BOOL(0, "unfold", &opts.unfold, N_("join whitespace-continued values")),
+               { OPTION_CALLBACK, 0, "parse", &opts, NULL, N_("set parsing options"),
+                       PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse },
+               OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
+                               N_("trailer(s) to add"), option_parse_trailer),
                OPT_END()
        };
 
        argc = parse_options(argc, argv, prefix, options,
                             git_interpret_trailers_usage, 0);
 
+       if (opts.only_input && !list_empty(&trailers))
+               usage_msg_opt(
+                       _("--trailer with --only-input does not make sense"),
+                       git_interpret_trailers_usage,
+                       options);
+
        if (argc) {
                int i;
                for (i = 0; i < argc; i++)
-                       process_trailers(argv[i], in_place, trim_empty, &trailers);
+                       process_trailers(argv[i], &opts, &trailers);
        } else {
-               if (in_place)
+               if (opts.in_place)
                        die(_("no input file given for in-place editing"));
-               process_trailers(NULL, in_place, trim_empty, &trailers);
+               process_trailers(NULL, &opts, &trailers);
        }
 
-       string_list_clear(&trailers, 0);
+       new_trailers_clear(&trailers);
 
        return 0;
 }
index 5ffc380bd71f0ab8b5bec06f864e9cdcffda1e2f..d81a09051ead0bb14161c25ef30bf8b0348b3198 100644 (file)
@@ -27,6 +27,7 @@
 #include "version.h"
 #include "mailmap.h"
 #include "gpg-interface.h"
+#include "progress.h"
 
 /* Set a default date-time format for git log ("log.date" config variable) */
 static const char *default_date_mode = NULL;
@@ -58,9 +59,9 @@ static int auto_decoration_style(void)
        return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS : 0;
 }
 
-static int parse_decoration_style(const char *var, const char *value)
+static int parse_decoration_style(const char *value)
 {
-       switch (git_config_maybe_bool(var, value)) {
+       switch (git_parse_maybe_bool(value)) {
        case 1:
                return DECORATE_SHORT_REFS;
        case 0:
@@ -82,7 +83,7 @@ static int decorate_callback(const struct option *opt, const char *arg, int unse
        if (unset)
                decoration_style = 0;
        else if (arg)
-               decoration_style = parse_decoration_style("command line", arg);
+               decoration_style = parse_decoration_style(arg);
        else
                decoration_style = DECORATE_SHORT_REFS;
 
@@ -412,7 +413,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
        if (!strcmp(var, "log.date"))
                return git_config_string(&default_date_mode, var, value);
        if (!strcmp(var, "log.decorate")) {
-               decoration_style = parse_decoration_style(var, value);
+               decoration_style = parse_decoration_style(value);
                if (decoration_style < 0)
                        decoration_style = 0; /* maybe warn? */
                return 0;
@@ -487,8 +488,8 @@ static int show_blob_object(const struct object_id *oid, struct rev_info *rev, c
            !DIFF_OPT_TST(&rev->diffopt, ALLOW_TEXTCONV))
                return stream_blob_to_fd(1, oid, NULL, 0);
 
-       if (get_sha1_with_context(obj_name, GET_SHA1_RECORD_PATH,
-                                 oidc.hash, &obj_context))
+       if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH,
+                                &oidc, &obj_context))
                die(_("Not a valid object name %s"), obj_name);
        if (!obj_context.path ||
            !textconv_object(obj_context.path, obj_context.mode, &oidc, 1, &buf, &size)) {
@@ -824,7 +825,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
                return 0;
        }
        if (!strcmp(var, "format.from")) {
-               int b = git_config_maybe_bool(var, value);
+               int b = git_parse_maybe_bool(value);
                free(from);
                if (b < 0)
                        from = xstrdup(value);
@@ -1422,6 +1423,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        char *branch_name = NULL;
        char *base_commit = NULL;
        struct base_tree_info bases;
+       int show_progress = 0;
+       struct progress *progress = NULL;
 
        const struct option builtin_format_patch_options[] = {
                { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
@@ -1493,6 +1496,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                OPT_FILENAME(0, "signature-file", &signature_file,
                                N_("add a signature from a file")),
                OPT__QUIET(&quiet, N_("don't print the patch filenames")),
+               OPT_BOOL(0, "progress", &show_progress,
+                        N_("show progress while generating patches")),
                OPT_END()
        };
 
@@ -1655,10 +1660,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                        check_head = 1;
 
                if (check_head) {
-                       struct object_id oid;
                        const char *ref, *v;
                        ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
-                                                oid.hash, NULL);
+                                                NULL, NULL);
                        if (ref && skip_prefix(ref, "refs/heads/", &v))
                                branch_name = xstrdup(v);
                        else
@@ -1752,8 +1756,12 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                start_number--;
        }
        rev.add_signoff = do_signoff;
+
+       if (show_progress)
+               progress = start_delayed_progress(_("Generating patches"), total);
        while (0 <= --nr) {
                int shown;
+               display_progress(progress, total - nr);
                commit = list[nr];
                rev.nr = total - nr + (start_number - 1);
                /* Make the second and subsequent mails replies to the first */
@@ -1818,6 +1826,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                if (!use_stdout)
                        fclose(rev.diffopt.file);
        }
+       stop_progress(&progress);
        free(list);
        free(branch_name);
        string_list_clear(&extra_to, 0);
index c6126eae550beb75e7c90094be337efbb48cd3bc..8c713c47acccf0a3a50ec8a0b822aafae8d6bada 100644 (file)
@@ -19,6 +19,7 @@
 #include "pathspec.h"
 #include "run-command.h"
 #include "submodule.h"
+#include "submodule-config.h"
 
 static int abbrev;
 static int show_deleted;
@@ -210,8 +211,6 @@ static void show_submodule(struct repository *superproject,
        if (repo_read_index(&submodule) < 0)
                die("index file corrupt");
 
-       repo_read_gitmodules(&submodule);
-
        show_files(&submodule, dir);
 
        repo_clear(&submodule);
@@ -609,9 +608,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
        if (require_work_tree && !is_inside_work_tree())
                setup_work_tree();
 
-       if (recurse_submodules)
-               repo_read_gitmodules(the_repository);
-
        if (recurse_submodules &&
            (show_stage || show_deleted || show_others || show_unmerged ||
             show_killed || show_modified || show_resolve_undo || with_tree))
@@ -677,5 +673,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
                return bad ? 1 : 0;
        }
 
+       UNLEAK(dir);
        return 0;
 }
index bad6735c76fd0647547edfd1201d91c3c86dfc8f..d01ddecf6602eabdca97a175e5c2a57bf1257865 100644 (file)
@@ -213,11 +213,11 @@ static void unresolved_directory(const struct traverse_info *info,
 
        newbase = traverse_path(info, p);
 
-#define ENTRY_SHA1(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid->hash : NULL)
-       buf0 = fill_tree_descriptor(t+0, ENTRY_SHA1(n + 0));
-       buf1 = fill_tree_descriptor(t+1, ENTRY_SHA1(n + 1));
-       buf2 = fill_tree_descriptor(t+2, ENTRY_SHA1(n + 2));
-#undef ENTRY_SHA1
+#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
+       buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
+       buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
+       buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
+#undef ENTRY_OID
 
        merge_trees(t, newbase);
 
@@ -347,12 +347,12 @@ static void merge_trees(struct tree_desc t[3], const char *base)
 
 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
        void *buf;
 
-       if (get_sha1(rev, sha1))
+       if (get_oid(rev, &oid))
                die("unknown rev %s", rev);
-       buf = fill_tree_descriptor(desc, sha1);
+       buf = fill_tree_descriptor(desc, &oid);
        if (!buf)
                die("%s is not a tree", rev);
        return buf;
index 23c53a3082b26e8f89f8be1f14985acc491d9907..ab5ffe85e8f5d7a359004f2bbb52a5778d0b91b4 100644 (file)
@@ -32,6 +32,7 @@
 #include "gpg-interface.h"
 #include "sequencer.h"
 #include "string-list.h"
+#include "packfile.h"
 
 #define DEFAULT_TWOHEAD (1<<0)
 #define DEFAULT_OCTOPUS (1<<1)
@@ -70,7 +71,9 @@ static int continue_current_merge;
 static int allow_unrelated_histories;
 static int show_progress = -1;
 static int default_to_upstream = 1;
+static int signoff;
 static const char *sign_commit;
+static int verify_msg = 1;
 
 static struct strategy all_strategy[] = {
        { "recursive",  DEFAULT_TWOHEAD | NO_TRIVIAL },
@@ -233,6 +236,8 @@ static struct option builtin_merge_options[] = {
        { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
          N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
        OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
+       OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")),
+       OPT_BOOL(0, "verify", &verify_msg, N_("verify commit-msg hook")),
        OPT_END()
 };
 
@@ -250,6 +255,7 @@ static int save_state(struct object_id *stash)
        struct child_process cp = CHILD_PROCESS_INIT;
        struct strbuf buffer = STRBUF_INIT;
        const char *argv[] = {"stash", "create", NULL};
+       int rc = -1;
 
        cp.argv = argv;
        cp.out = -1;
@@ -263,11 +269,14 @@ static int save_state(struct object_id *stash)
        if (finish_command(&cp) || len < 0)
                die(_("stash failed"));
        else if (!len)          /* no changes */
-               return -1;
+               goto out;
        strbuf_setlen(&buffer, buffer.len-1);
        if (get_oid(buffer.buf, stash))
                die(_("not a valid object: %s"), buffer.buf);
-       return 0;
+       rc = 0;
+out:
+       strbuf_release(&buffer);
+       return rc;
 }
 
 static void read_empty(unsigned const char *sha1, int verbose)
@@ -566,7 +575,7 @@ static int git_merge_config(const char *k, const char *v, void *cb)
        else if (!strcmp(k, "merge.renormalize"))
                option_renormalize = git_config_bool(k, v);
        else if (!strcmp(k, "merge.ff")) {
-               int boolval = git_config_maybe_bool(k, v);
+               int boolval = git_parse_maybe_bool(v);
                if (0 <= boolval) {
                        fast_forward = boolval ? FF_ALLOW : FF_NO;
                } else if (v && !strcmp(v, "only")) {
@@ -766,6 +775,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
                BUG("the control must not reach here under --squash");
        if (0 < option_edit)
                strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
+       if (signoff)
+               append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
        write_merge_heads(remoteheads);
        write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
        if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
@@ -775,6 +786,12 @@ static void prepare_to_commit(struct commit_list *remoteheads)
                if (launch_editor(git_path_merge_msg(), NULL, NULL))
                        abort_commit(remoteheads, NULL);
        }
+
+       if (verify_msg && run_commit_hook(0 < option_edit, get_index_file(),
+                                         "commit-msg",
+                                         git_path_merge_msg(), NULL))
+               abort_commit(remoteheads, NULL);
+
        read_merge_msg(&msg);
        strbuf_stripspace(&msg, 0 < option_edit);
        if (!msg.len)
@@ -929,6 +946,7 @@ static void write_merge_heads(struct commit_list *remoteheads)
        if (fast_forward == FF_NO)
                strbuf_addstr(&buf, "no-ff");
        write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
+       strbuf_release(&buf);
 }
 
 static void write_merge_state(struct commit_list *remoteheads)
@@ -949,7 +967,7 @@ static int default_edit_option(void)
                return 0;
 
        if (e) {
-               int v = git_config_maybe_bool(name, e);
+               int v = git_parse_maybe_bool(e);
                if (v < 0)
                        die(_("Bad value '%s' in environment '%s'"), e, name);
                return v;
@@ -1354,7 +1372,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                 * If head can reach all the merge then we are up to date.
                 * but first the most common case of merging one remote.
                 */
-               finish_up_to_date(_("Already up-to-date."));
+               finish_up_to_date(_("Already up to date."));
                goto done;
        } else if (fast_forward != FF_NO && !remoteheads->next &&
                        !common->next &&
@@ -1437,7 +1455,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                        }
                }
                if (up_to_date) {
-                       finish_up_to_date(_("Already up-to-date. Yeeah!"));
+                       finish_up_to_date(_("Already up to date. Yeeah!"));
                        goto done;
                }
        }
index dcf6736b5b4c53989ee1fb3c5240e04f5dbb337d..ffdd5f01a190b99407d1019972e3198c42b7194c 100644 (file)
@@ -81,7 +81,7 @@ static void prepare_move_submodule(const char *src, int first,
        struct strbuf submodule_dotgit = STRBUF_INIT;
        if (!S_ISGITLINK(active_cache[first]->ce_mode))
                die(_("Directory %s is in index and no submodule?"), src);
-       if (!is_staging_gitmodules_ok())
+       if (!is_staging_gitmodules_ok(&the_index))
                die(_("Please stage your changes to .gitmodules or stash them to proceed"));
        strbuf_addf(&submodule_dotgit, "%s/.git", src);
        *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
@@ -131,7 +131,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
        struct stat st;
        struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
 
-       gitmodules_config();
        git_config(git_default_config, NULL);
 
        argc = parse_options(argc, argv, prefix, builtin_mv_options,
index c41ea7c2a62ba868061884e511256c24f4d020d9..9e088ebd11dced248640df9e17adbbd8b9a73ffb 100644 (file)
@@ -253,7 +253,7 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
                struct commit *commit = (struct commit *)o;
                int from_tag = starts_with(path, "refs/tags/");
 
-               if (taggerdate == ULONG_MAX)
+               if (taggerdate == TIME_MAX)
                        taggerdate = ((struct commit *)o)->date;
                path = name_ref_abbrev(path, can_abbreviate_output);
                name_rev(commit, xstrdup(path), taggerdate, 0, 0,
@@ -494,5 +494,6 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
                                  always, allow_undefined, data.name_only);
        }
 
+       UNLEAK(revs);
        return 0;
 }
index 77573cf1ea8cb4d998597e265263487e22fde592..8e54f2d14648bcf41369e71c2f7f586322c702ef 100644 (file)
@@ -328,6 +328,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
        } else {
                finish_copy_notes_for_rewrite(c, msg);
        }
+       strbuf_release(&buf);
        return ret;
 }
 
@@ -456,7 +457,7 @@ static int add(int argc, const char **argv, const char *prefix)
                        oid_to_hex(&object));
        }
 
-       prepare_note_data(&object, &d, note->hash);
+       prepare_note_data(&object, &d, note ? note->hash : NULL);
        if (d.buf.len || allow_empty) {
                write_note_data(&d, new_note.hash);
                if (add_note(t, &object, &new_note, combine_notes_overwrite))
index c753e9237a8d5981a17e872db33d5326bd8d7eab..5ee2c48ffb82e4adde3fe25b4661f3624314f238 100644 (file)
@@ -25,6 +25,7 @@
 #include "sha1-array.h"
 #include "argv-array.h"
 #include "mru.h"
+#include "packfile.h"
 
 static const char *pack_usage[] = {
        N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
@@ -1011,7 +1012,7 @@ static int want_object_in_pack(const unsigned char *sha1,
                        return want;
        }
 
-       for (entry = packed_git_mru->head; entry; entry = entry->next) {
+       for (entry = packed_git_mru.head; entry; entry = entry->next) {
                struct packed_git *p = entry->item;
                off_t offset;
 
@@ -1029,7 +1030,7 @@ static int want_object_in_pack(const unsigned char *sha1,
                        }
                        want = want_found_object(exclude, p);
                        if (!exclude && want > 0)
-                               mru_mark(packed_git_mru, entry);
+                               mru_mark(&packed_git_mru, entry);
                        if (want != -1)
                                return want;
                }
@@ -2170,7 +2171,10 @@ static void *threaded_find_deltas(void *arg)
 {
        struct thread_params *me = arg;
 
+       progress_lock();
        while (me->remaining) {
+               progress_unlock();
+
                find_deltas(me->list, &me->remaining,
                            me->window, me->depth, me->processed);
 
@@ -2192,7 +2196,10 @@ static void *threaded_find_deltas(void *arg)
                        pthread_cond_wait(&me->cond, &me->mutex);
                me->data_ready = 0;
                pthread_mutex_unlock(&me->mutex);
+
+               progress_lock();
        }
+       progress_unlock();
        /* leave ->working 1 so that this doesn't get more work assigned */
        return NULL;
 }
@@ -2556,8 +2563,8 @@ struct in_pack_object {
 };
 
 struct in_pack {
-       int alloc;
-       int nr;
+       unsigned int alloc;
+       unsigned int nr;
        struct in_pack_object *array;
 };
 
index cb1df1c7614b4a91f2ed39ffe1f31dfdbcbe793e..aaa8136322244ccde5b7d6804a3bc09b0c053568 100644 (file)
@@ -7,6 +7,7 @@
 */
 
 #include "builtin.h"
+#include "packfile.h"
 
 #define BLKSIZE 512
 
index ac978ad401c01c4f44d3134b95a2bcb8b29973f9..419238171d7a2159f5e533bab84408aacb4d35e4 100644 (file)
@@ -2,6 +2,7 @@
 #include "cache.h"
 #include "progress.h"
 #include "parse-options.h"
+#include "packfile.h"
 
 static const char * const prune_packed_usage[] = {
        N_("git prune-packed [-n | --dry-run] [-q | --quiet]"),
@@ -37,8 +38,7 @@ static int prune_object(const struct object_id *oid, const char *path,
 void prune_packed_objects(int opts)
 {
        if (opts & PRUNE_PACKED_VERBOSE)
-               progress = start_progress_delay(_("Removing duplicate objects"),
-                       256, 95, 2);
+               progress = start_delayed_progress(_("Removing duplicate objects"), 256);
 
        for_each_loose_file_in_objdir(get_object_directory(),
                                      prune_object, NULL, prune_subdir, &opts);
index c378690545b27b7e4753e0f919f3ea6626b0eae9..cddabf26a95cc22dfcad9843554dc8af26c858bc 100644 (file)
@@ -138,7 +138,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
        if (show_progress == -1)
                show_progress = isatty(2);
        if (show_progress)
-               progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
+               progress = start_delayed_progress(_("Checking connectivity"), 0);
 
        mark_reachable_objects(&revs, 1, expire, progress);
        stop_progress(&progress);
index 9b86e519b19a6180e52fda3342dec4f080e1f9e9..6f772e8a224c18238cf9a75436e8ea192765e7fe 100644 (file)
@@ -39,7 +39,7 @@ enum rebase_type {
 static enum rebase_type parse_config_rebase(const char *key, const char *value,
                int fatal)
 {
-       int v = git_config_maybe_bool("pull.rebase", value);
+       int v = git_parse_maybe_bool(value);
 
        if (!v)
                return REBASE_FALSE;
@@ -274,7 +274,7 @@ static const char *config_get_ff(void)
        if (git_config_get_value("pull.ff", &value))
                return NULL;
 
-       switch (git_config_maybe_bool("pull.ff", value)) {
+       switch (git_parse_maybe_bool(value)) {
        case 0:
                return "--no-ff";
        case 1:
@@ -325,6 +325,10 @@ static int git_pull_config(const char *var, const char *value, void *cb)
        if (!strcmp(var, "rebase.autostash")) {
                config_autostash = git_config_bool(var, value);
                return 0;
+       } else if (!strcmp(var, "submodule.recurse")) {
+               recurse_submodules = git_config_bool(var, value) ?
+                       RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
+               return 0;
        }
        return git_default_config(var, value, cb);
 }
@@ -815,6 +819,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
        if (!getenv("GIT_REFLOG_ACTION"))
                set_reflog_message(argc, argv);
 
+       git_config(git_pull_config, NULL);
+
        argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
 
        parse_repo_refspecs(argc, argv, &repo, &refspecs);
@@ -825,8 +831,6 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
        if (opt_rebase < 0)
                opt_rebase = config_get_rebase();
 
-       git_config(git_pull_config, NULL);
-
        if (read_cache_unmerged())
                die_resolve_conflict("pull");
 
index 03846e83795c477c8e802d078c5a5ed77140d550..2ac81042292ef1852ec9a31dea34ec91a1b796e3 100644 (file)
@@ -481,7 +481,7 @@ static int git_push_config(const char *k, const char *v, void *cb)
        } else if (!strcmp(k, "push.gpgsign")) {
                const char *value;
                if (!git_config_get_value("push.gpgsign", &value)) {
-                       switch (git_config_maybe_bool("push.gpgsign", value)) {
+                       switch (git_parse_maybe_bool(value)) {
                        case 0:
                                set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER);
                                break;
index d5f618d086365520fcf36ed8db110ba701ba37d3..bf87a2710b31fa49b1802ed297c8ae52eede6354 100644 (file)
@@ -164,8 +164,6 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
        argc = parse_options(argc, argv, unused_prefix, read_tree_options,
                             read_tree_usage, 0);
 
-       load_submodule_cache();
-
        hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
 
        prefix_set = opts.prefix ? 1 : 0;
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 cabdc55e0933adb09d459a5a0747c718fd2c7d1e..cc4876740569c6e495b7cb32161d019619171d79 100644 (file)
@@ -23,6 +23,7 @@
 #include "fsck.h"
 #include "tmp-objdir.h"
 #include "oidset.h"
+#include "packfile.h"
 
 static const char * const receive_pack_usage[] = {
        N_("git receive-pack <git-dir>"),
@@ -742,7 +743,7 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,
                size_t n;
                if (feed(feed_state, &buf, &n))
                        break;
-               if (write_in_full(proc.in, buf, n) != n)
+               if (write_in_full(proc.in, buf, n) < 0)
                        break;
        }
        close(proc.in);
@@ -919,9 +920,9 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
  */
 static int head_has_history(void)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
 
-       return !get_sha1("HEAD", sha1);
+       return !get_oid("HEAD", &oid);
 }
 
 static const char *push_to_deploy(unsigned char *sha1,
@@ -1138,7 +1139,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
                }
                if (ref_transaction_delete(transaction,
                                           namespaced_name,
-                                          old_oid->hash,
+                                          old_oid ? old_oid->hash : NULL,
                                           0, "push", &err)) {
                        rp_error("%s", err.buf);
                        strbuf_release(&err);
@@ -1206,11 +1207,10 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
        const char *dst_name;
        struct string_list_item *item;
        struct command *dst_cmd;
-       unsigned char sha1[GIT_MAX_RAWSZ];
        int flag;
 
        strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
-       dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
+       dst_name = resolve_ref_unsafe(buf.buf, 0, NULL, &flag);
        strbuf_release(&buf);
 
        if (!(flag & REF_ISSYMREF))
@@ -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 e237d927a0881b56135a21a1b0041e7de6ff39ab..2067cca5b1ec4ed141a3e404cb7738c9b0ebc5d9 100644 (file)
@@ -126,7 +126,7 @@ static int commit_is_complete(struct commit *commit)
                struct commit *c;
                struct commit_list *parent;
 
-               c = (struct commit *)study.objects[--study.nr].item;
+               c = (struct commit *)object_array_pop(&study);
                if (!c->object.parsed && !parse_object(&c->object.oid))
                        c->object.flags |= INCOMPLETE;
 
@@ -182,8 +182,8 @@ static int commit_is_complete(struct commit *commit)
                        found.objects[i].item->flags |= SEEN;
        }
        /* free object arrays */
-       free(study.objects);
-       free(found.objects);
+       object_array_clear(&study);
+       object_array_clear(&found);
        return !is_incomplete;
 }
 
index bfb21ba7d217bafaf3527af007cd4ac4658d36c5..6a9127a33c019c5e6336075b5f68c7514d720b04 100644 (file)
@@ -57,7 +57,7 @@ static char *strip_escapes(const char *str, const char *service,
                                special = str[rpos];
                                if (rpos == 1)
                                        break;
-                               /* Fall-through to error. */
+                               /* fallthrough */
                        default:
                                die("Bad remote-ext placeholder '%%%c'.",
                                        str[rpos]);
index 6273c0c23c904d5f789ff794458cf0c3f2661d94..4f5cac96b0f5e0044ddf0fd1e2ea686f694d62ca 100644 (file)
@@ -301,7 +301,7 @@ static int config_read_branches(const char *key, const char *value, void *cb)
                        }
                        string_list_append(&info->merge, xstrdup(value));
                } else {
-                       int v = git_config_maybe_bool(orig_key, value);
+                       int v = git_parse_maybe_bool(value);
                        if (v >= 0)
                                info->rebase = v;
                        else if (!strcmp(value, "preserve"))
@@ -558,19 +558,19 @@ static int read_remote_branches(const char *refname,
        struct strbuf buf = STRBUF_INIT;
        struct string_list_item *item;
        int flag;
-       struct object_id orig_oid;
        const char *symref;
 
        strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
        if (starts_with(refname, buf.buf)) {
                item = string_list_append(rename->remote_branches, xstrdup(refname));
                symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
-                                           orig_oid.hash, &flag);
+                                           NULL, &flag);
                if (flag & REF_ISSYMREF)
                        item->util = xstrdup(symref);
                else
                        item->util = NULL;
        }
+       strbuf_release(&buf);
 
        return 0;
 }
@@ -595,6 +595,7 @@ static int migrate_file(struct remote *remote)
                unlink_or_warn(git_path("remotes/%s", remote->name));
        else if (remote->origin == REMOTE_BRANCHES)
                unlink_or_warn(git_path("branches/%s", remote->name));
+       strbuf_release(&buf);
 
        return 0;
 }
@@ -1563,9 +1564,7 @@ static int set_url(int argc, const char **argv)
                                                       "^$", 0);
                else
                        git_config_set(name_buf.buf, newurl);
-               strbuf_release(&name_buf);
-
-               return 0;
+               goto out;
        }
 
        /* Old URL specified. Demand that one matches. */
@@ -1588,6 +1587,8 @@ static int set_url(int argc, const char **argv)
                git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
        else
                git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
+out:
+       strbuf_release(&name_buf);
        return 0;
 }
 
index fba336a68a375e20e9b40d332eb58a3f302249fa..3e71a771523d8566590bfbd5de71b08acf79e3a4 100644 (file)
@@ -50,7 +50,7 @@ static int show_reference(const char *refname, const struct object_id *oid,
                        struct object_id object;
                        enum object_type obj_type, repl_type;
 
-                       if (get_sha1(refname, object.hash))
+                       if (get_oid(refname, &object))
                                return error("Failed to resolve '%s' as a valid ref.", refname);
 
                        obj_type = sha1_object_info(object.hash, NULL);
@@ -269,7 +269,7 @@ static void import_object(struct object_id *oid, enum object_type type,
 
                if (fstat(fd, &st) < 0)
                        die_errno("unable to fstat %s", filename);
-               if (index_fd(oid->hash, fd, &st, type, NULL, flags) < 0)
+               if (index_fd(oid, fd, &st, type, NULL, flags) < 0)
                        die("unable to write object to database");
                /* index_fd close()s fd for us */
        }
@@ -365,7 +365,7 @@ static void check_one_mergetag(struct commit *commit,
        /* iterate over new parents */
        for (i = 1; i < mergetag_data->argc; i++) {
                struct object_id oid;
-               if (get_sha1(mergetag_data->argv[i], oid.hash) < 0)
+               if (get_oid(mergetag_data->argv[i], &oid) < 0)
                        die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]);
                if (!oidcmp(&tag->tagged->oid, &oid))
                        return; /* found */
index ffb66e29073c07c43197ae34f2c31d3b9b610903..0bc40298c2417a6d1f25aafb4b3577243e985c57 100644 (file)
@@ -18,7 +18,7 @@ static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
 {
        int i;
        for (i = 0; i < nbuf; i++)
-               if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
+               if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0)
                        return -1;
        return 0;
 }
index 7aeaea2737991f021eb788708c1710305abb4b08..9cd89b23059f768c4651725f2c26c5b08c35f650 100644 (file)
@@ -44,10 +44,11 @@ static inline int is_merge(void)
 
 static int reset_index(const struct object_id *oid, int reset_type, int quiet)
 {
-       int nr = 1;
+       int i, nr = 0;
        struct tree_desc desc[2];
        struct tree *tree;
        struct unpack_trees_options opts;
+       int ret = -1;
 
        memset(&opts, 0, sizeof(opts));
        opts.head_idx = 1;
@@ -75,23 +76,32 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet)
                struct object_id head_oid;
                if (get_oid("HEAD", &head_oid))
                        return error(_("You do not have a valid HEAD."));
-               if (!fill_tree_descriptor(desc, head_oid.hash))
+               if (!fill_tree_descriptor(desc + nr, &head_oid))
                        return error(_("Failed to find tree of HEAD."));
                nr++;
                opts.fn = twoway_merge;
        }
 
-       if (!fill_tree_descriptor(desc + nr - 1, oid->hash))
-               return error(_("Failed to find tree of %s."), oid_to_hex(oid));
+       if (!fill_tree_descriptor(desc + nr, oid)) {
+               error(_("Failed to find tree of %s."), oid_to_hex(oid));
+               goto out;
+       }
+       nr++;
+
        if (unpack_trees(nr, desc, &opts))
-               return -1;
+               goto out;
 
        if (reset_type == MIXED || reset_type == HARD) {
                tree = parse_tree_indirect(oid);
                prime_cache_tree(&the_index, tree);
        }
 
-       return 0;
+       ret = 0;
+
+out:
+       for (i = 0; i < nr; i++)
+               free((void *)desc[i].buffer);
+       return ret;
 }
 
 static void print_new_head_line(struct commit *commit)
@@ -156,6 +166,7 @@ static int read_from_tree(const struct pathspec *pathspec,
        opt.output_format = DIFF_FORMAT_CALLBACK;
        opt.format_callback = update_index_from_diff;
        opt.format_callback_data = &intent_to_add;
+       opt.flags |= DIFF_OPT_OVERRIDE_SUBMODULE_CONFIG;
 
        if (do_diff_cache(tree_oid, &opt))
                return 1;
@@ -219,8 +230,8 @@ static void parse_args(struct pathspec *pathspec,
                 * has to be unambiguous. If there is a single argument, it
                 * can not be a tree
                 */
-               else if ((!argv[1] && !get_sha1_committish(argv[0], unused.hash)) ||
-                        (argv[1] && !get_sha1_treeish(argv[0], unused.hash))) {
+               else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
+                        (argv[1] && !get_oid_treeish(argv[0], &unused))) {
                        /*
                         * Ok, argv[0] looks like a commit/tree; it should not
                         * be a filename.
@@ -308,15 +319,13 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                                                PARSE_OPT_KEEP_DASHDASH);
        parse_args(&pathspec, argv, prefix, patch_mode, &rev);
 
-       load_submodule_cache();
-
-       unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", oid.hash);
+       unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
        if (unborn) {
                /* reset on unborn branch: treat as reset to empty tree */
                hashcpy(oid.hash, EMPTY_TREE_SHA1_BIN);
        } else if (!pathspec.nr) {
                struct commit *commit;
-               if (get_sha1_committish(rev, oid.hash))
+               if (get_oid_committish(rev, &oid))
                        die(_("Failed to resolve '%s' as a valid revision."), rev);
                commit = lookup_commit_reference(&oid);
                if (!commit)
@@ -324,7 +333,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                oidcpy(&oid, &commit->object.oid);
        } else {
                struct tree *tree;
-               if (get_sha1_treeish(rev, oid.hash))
+               if (get_oid_treeish(rev, &oid))
                        die(_("Failed to resolve '%s' as a valid tree."), rev);
                tree = parse_tree_indirect(&oid);
                if (!tree)
@@ -368,8 +377,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                die_if_unmerged_cache(reset_type);
 
        if (reset_type != SOFT) {
-               struct lock_file *lock = xcalloc(1, sizeof(*lock));
-               hold_locked_index(lock, LOCK_DIE_ON_ERROR);
+               struct lock_file lock = LOCK_INIT;
+               hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
                if (reset_type == MIXED) {
                        int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
                        if (read_from_tree(&pathspec, &oid, intent_to_add))
@@ -385,7 +394,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                                die(_("Could not reset index file to revision '%s'."), rev);
                }
 
-               if (write_locked_index(&the_index, lock, COMMIT_LOCK))
+               if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
                        die(_("Could not write new index file."));
        }
 
index 95b4128250c850eb130fbdfea2407bad819a85b8..c1c74d4a7956430fca46fd743946280daf2f0f3f 100644 (file)
@@ -367,7 +367,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
                revs.limited = 1;
 
        if (show_progress)
-               progress = start_progress_delay(show_progress, 0, 0, 2);
+               progress = start_delayed_progress(show_progress, 0);
 
        if (use_bitmap_index && !revs.prune) {
                if (revs.count && !revs.left_right && !revs.cherry_mark) {
index c78b7b33d6604bb38a16e30d64cfabee0fd67f40..a8d7e6f7aecc2e4179547bde3fd6d07231764b9f 100644 (file)
@@ -274,7 +274,7 @@ static int try_difference(const char *arg)
                return 0;
        }
 
-       if (!get_sha1_committish(this, oid.hash) && !get_sha1_committish(next, end.hash)) {
+       if (!get_oid_committish(this, &oid) && !get_oid_committish(next, &end)) {
                show_rev(NORMAL, &end, next);
                show_rev(symmetric ? NORMAL : REVERSED, &oid, this);
                if (symmetric) {
@@ -328,7 +328,7 @@ static int try_parent_shorthands(const char *arg)
                return 0;
 
        *dotdot = 0;
-       if (get_sha1_committish(arg, oid.hash)) {
+       if (get_oid_committish(arg, &oid)) {
                *dotdot = '^';
                return 0;
        }
@@ -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;
@@ -702,7 +712,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        }
                        if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
                                quiet = 1;
-                               flags |= GET_SHA1_QUIETLY;
+                               flags |= GET_OID_QUIETLY;
                                continue;
                        }
                        if (opt_with_value(arg, "--short", &arg)) {
@@ -757,8 +767,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--bisect")) {
-                               for_each_ref_in("refs/bisect/bad", show_reference, NULL);
-                               for_each_ref_in("refs/bisect/good", anti_reference, NULL);
+                               for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
+                               for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
                                continue;
                        }
                        if (opt_with_value(arg, "--branches", &arg)) {
@@ -868,6 +878,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                                : "false");
                                continue;
                        }
+                       if (!strcmp(arg, "--is-shallow-repository")) {
+                               printf("%s\n", is_repository_shallow() ? "true"
+                                               : "false");
+                               continue;
+                       }
                        if (!strcmp(arg, "--shared-index-path")) {
                                if (read_cache() < 0)
                                        die(_("Could not read the index"));
@@ -911,7 +926,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        name++;
                        type = REVERSED;
                }
-               if (!get_sha1_with_context(name, flags, oid.hash, &unused)) {
+               if (!get_oid_with_context(name, flags, &oid, &unused)) {
                        if (verify)
                                revs_count++;
                        else
index 52826d137935ca6698006258ebdb8b207f7161df..d91451fea122cec3923543ef78acf0c5ef4c5fa2 100644 (file)
@@ -255,7 +255,6 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
        struct pathspec pathspec;
        char *seen;
 
-       gitmodules_config();
        git_config(git_default_config, NULL);
 
        argc = parse_options(argc, argv, prefix, builtin_rm_options,
@@ -286,7 +285,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
                list.entry[list.nr].name = xstrdup(ce->name);
                list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
                if (list.entry[list.nr++].is_submodule &&
-                   !is_staging_gitmodules_ok())
+                   !is_staging_gitmodules_ok(&the_index))
                        die (_("Please stage your changes to .gitmodules or stash them to proceed"));
        }
 
index 633e0c3cdd3171e6e51944c5b68e4a4afa48862e..fc4f0bb5fbc033604a13a147094c0d1bc661db17 100644 (file)
@@ -105,7 +105,7 @@ static int send_pack_config(const char *k, const char *v, void *cb)
        if (!strcmp(k, "push.gpgsign")) {
                const char *value;
                if (!git_config_get_value("push.gpgsign", &value)) {
-                       switch (git_config_maybe_bool("push.gpgsign", value)) {
+                       switch (git_parse_maybe_bool(value)) {
                        case 0:
                                args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
                                break;
index 43c4799ea9a9d963f9a44be5cd0ee5bc511e73df..e29875b84389b25237e39e0112eafa8ac34599ee 100644 (file)
@@ -52,26 +52,8 @@ static void insert_one_record(struct shortlog *log,
                              const char *oneline)
 {
        struct string_list_item *item;
-       const char *mailbuf, *namebuf;
-       size_t namelen, maillen;
-       struct strbuf namemailbuf = STRBUF_INIT;
-       struct ident_split ident;
 
-       if (split_ident_line(&ident, author, strlen(author)))
-               return;
-
-       namebuf = ident.name_begin;
-       mailbuf = ident.mail_begin;
-       namelen = ident.name_end - ident.name_begin;
-       maillen = ident.mail_end - ident.mail_begin;
-
-       map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
-       strbuf_add(&namemailbuf, namebuf, namelen);
-
-       if (log->email)
-               strbuf_addf(&namemailbuf, " <%.*s>", (int)maillen, mailbuf);
-
-       item = string_list_insert(&log->list, namemailbuf.buf);
+       item = string_list_insert(&log->list, author);
 
        if (log->summary)
                item->util = (void *)(UTIL_TO_INT(item) + 1);
@@ -114,9 +96,33 @@ static void insert_one_record(struct shortlog *log,
        }
 }
 
+static int parse_stdin_author(struct shortlog *log,
+                              struct strbuf *out, const char *in)
+{
+       const char *mailbuf, *namebuf;
+       size_t namelen, maillen;
+       struct ident_split ident;
+
+       if (split_ident_line(&ident, in, strlen(in)))
+               return -1;
+
+       namebuf = ident.name_begin;
+       mailbuf = ident.mail_begin;
+       namelen = ident.name_end - ident.name_begin;
+       maillen = ident.mail_end - ident.mail_begin;
+
+       map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
+       strbuf_add(out, namebuf, namelen);
+       if (log->email)
+               strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
+
+       return 0;
+}
+
 static void read_from_stdin(struct shortlog *log)
 {
        struct strbuf author = STRBUF_INIT;
+       struct strbuf mapped_author = STRBUF_INIT;
        struct strbuf oneline = STRBUF_INIT;
        static const char *author_match[2] = { "Author: ", "author " };
        static const char *committer_match[2] = { "Commit: ", "committer " };
@@ -134,9 +140,15 @@ static void read_from_stdin(struct shortlog *log)
                while (strbuf_getline_lf(&oneline, stdin) != EOF &&
                       !oneline.len)
                        ; /* discard blanks */
-               insert_one_record(log, v, oneline.buf);
+
+               strbuf_reset(&mapped_author);
+               if (parse_stdin_author(log, &mapped_author, v) < 0)
+                       continue;
+
+               insert_one_record(log, mapped_author.buf, oneline.buf);
        }
        strbuf_release(&author);
+       strbuf_release(&mapped_author);
        strbuf_release(&oneline);
 }
 
@@ -153,7 +165,9 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
        ctx.date_mode.type = DATE_NORMAL;
        ctx.output_encoding = get_log_output_encoding();
 
-       fmt = log->committer ? "%cn <%ce>" : "%an <%ae>";
+       fmt = log->committer ?
+               (log->email ? "%cN <%cE>" : "%cN") :
+               (log->email ? "%aN <%aE>" : "%aN");
 
        format_commit_message(commit, fmt, &author, &ctx);
        if (!log->summary) {
index 28f245c8cccbc20c96a189516e9df77d238c7e96..84547d6fba07cb7770d94e80cda170c391d04a21 100644 (file)
@@ -393,7 +393,7 @@ static int append_head_ref(const char *refname, const struct object_id *oid,
        /* If both heads/foo and tags/foo exists, get_sha1 would
         * get confused.
         */
-       if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
+       if (get_oid(refname + ofs, &tmp) || oidcmp(&tmp, oid))
                ofs = 5;
        return append_ref(refname + ofs, oid, 0);
 }
@@ -408,7 +408,7 @@ static int append_remote_ref(const char *refname, const struct object_id *oid,
        /* If both heads/foo and tags/foo exists, get_sha1 would
         * get confused.
         */
-       if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
+       if (get_oid(refname + ofs, &tmp) || oidcmp(&tmp, oid))
                ofs = 5;
        return append_ref(refname + ofs, oid, 0);
 }
@@ -514,7 +514,7 @@ static int show_independent(struct commit **rev,
 static void append_one_rev(const char *av)
 {
        struct object_id revkey;
-       if (!get_sha1(av, revkey.hash)) {
+       if (!get_oid(av, &revkey)) {
                append_ref(av, &revkey, 0);
                return;
        }
@@ -808,7 +808,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
                        die(Q_("cannot handle more than %d rev.",
                               "cannot handle more than %d revs.",
                               MAX_REVS), MAX_REVS);
-               if (get_sha1(ref_name[num_rev], revkey.hash))
+               if (get_oid(ref_name[num_rev], &revkey))
                        die(_("'%s' is not a valid ref."), ref_name[num_rev]);
                commit = lookup_commit_reference(&revkey);
                if (!commit)
index 895555c93a3bb86ad5511a2ded2350614732d618..06ed02f99402be6a7edb36ff0eb08b903c513c6c 100644 (file)
@@ -17,9 +17,8 @@
 static char *get_default_remote(void)
 {
        char *dest = NULL, *ret;
-       unsigned char sha1[20];
        struct strbuf sb = STRBUF_INIT;
-       const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
+       const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
 
        if (!refname)
                die(_("No such ref: %s"), "HEAD");
@@ -275,8 +274,6 @@ static void module_list_active(struct module_list *list)
        int i;
        struct module_list active_modules = MODULE_LIST_INIT;
 
-       gitmodules_config();
-
        for (i = 0; i < list->nr; i++) {
                const struct cache_entry *ce = list->entries[i];
 
@@ -337,9 +334,6 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
        struct strbuf sb = STRBUF_INIT;
        char *upd = NULL, *url = NULL, *displaypath;
 
-       /* Only loads from .gitmodules, no overlay with .git/config */
-       gitmodules_config();
-
        if (prefix && get_super_prefix())
                die("BUG: cannot have prefix and superprefix");
        else if (prefix)
@@ -350,7 +344,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
        } else
                displaypath = xstrdup(path);
 
-       sub = submodule_from_path(null_sha1, path);
+       sub = submodule_from_path(&null_oid, path);
 
        if (!sub)
                die(_("No url found for submodule path '%s' in .gitmodules"),
@@ -475,8 +469,7 @@ static int module_name(int argc, const char **argv, const char *prefix)
        if (argc != 2)
                usage(_("git submodule--helper name <path>"));
 
-       gitmodules_config();
-       sub = submodule_from_path(null_sha1, argv[1]);
+       sub = submodule_from_path(&null_oid, argv[1]);
 
        if (!sub)
                die(_("no submodule mapping found in .gitmodules for path '%s'"),
@@ -780,6 +773,10 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
                                           struct strbuf *out)
 {
        const struct submodule *sub = NULL;
+       const char *url = NULL;
+       const char *update_string;
+       enum submodule_update_type update_type;
+       char *key;
        struct strbuf displaypath_sb = STRBUF_INIT;
        struct strbuf sb = STRBUF_INIT;
        const char *displaypath = NULL;
@@ -795,7 +792,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
                goto cleanup;
        }
 
-       sub = submodule_from_path(null_sha1, ce->name);
+       sub = submodule_from_path(&null_oid, ce->name);
 
        if (suc->recursive_prefix)
                displaypath = relative_path(suc->recursive_prefix,
@@ -808,9 +805,17 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
                goto cleanup;
        }
 
+       key = xstrfmt("submodule.%s.update", sub->name);
+       if (!repo_config_get_string_const(the_repository, key, &update_string)) {
+               update_type = parse_submodule_update_type(update_string);
+       } else {
+               update_type = sub->update_strategy.type;
+       }
+       free(key);
+
        if (suc->update.type == SM_UPDATE_NONE
            || (suc->update.type == SM_UPDATE_UNSPECIFIED
-               && sub->update_strategy.type == SM_UPDATE_NONE)) {
+               && update_type == SM_UPDATE_NONE)) {
                strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
                strbuf_addch(out, '\n');
                goto cleanup;
@@ -822,6 +827,11 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
                goto cleanup;
        }
 
+       strbuf_reset(&sb);
+       strbuf_addf(&sb, "submodule.%s.url", sub->name);
+       if (repo_config_get_string_const(the_repository, sb.buf, &url))
+               url = sub->url;
+
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/.git", ce->name);
        needs_cloning = !file_exists(sb.buf);
@@ -851,7 +861,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
                argv_array_push(&child->args, "--depth=1");
        argv_array_pushl(&child->args, "--path", sub->path, NULL);
        argv_array_pushl(&child->args, "--name", sub->name, NULL);
-       argv_array_pushl(&child->args, "--url", sub->url, NULL);
+       argv_array_pushl(&child->args, "--url", url, NULL);
        if (suc->references.nr) {
                struct string_list_item *item;
                for_each_string_list_item(item, &suc->references)
@@ -960,10 +970,19 @@ static int update_clone_task_finished(int result,
        return 0;
 }
 
+static int gitmodules_update_clone_config(const char *var, const char *value,
+                                         void *cb)
+{
+       int *max_jobs = cb;
+       if (!strcmp(var, "submodule.fetchjobs"))
+               *max_jobs = parse_submodule_fetchjobs(var, value);
+       return 0;
+}
+
 static int update_clone(int argc, const char **argv, const char *prefix)
 {
        const char *update = NULL;
-       int max_jobs = -1;
+       int max_jobs = 1;
        struct string_list_item *item;
        struct pathspec pathspec;
        struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
@@ -1000,6 +1019,9 @@ static int update_clone(int argc, const char **argv, const char *prefix)
        };
        suc.prefix = prefix;
 
+       config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
+       git_config(gitmodules_update_clone_config, &max_jobs);
+
        argc = parse_options(argc, argv, prefix, module_update_clone_options,
                             git_submodule_helper_usage, 0);
 
@@ -1013,13 +1035,6 @@ static int update_clone(int argc, const char **argv, const char *prefix)
        if (pathspec.nr)
                suc.warn_if_uninitialized = 1;
 
-       /* Overlay the parsed .gitmodules file with .git/config */
-       gitmodules_config();
-       git_config(submodule_config, NULL);
-
-       if (max_jobs < 0)
-               max_jobs = parallel_submodules();
-
        run_processes_parallel(max_jobs,
                               update_clone_get_next_task,
                               update_clone_start_failure,
@@ -1057,19 +1072,23 @@ static int resolve_relative_path(int argc, const char **argv, const char *prefix
 static const char *remote_submodule_branch(const char *path)
 {
        const struct submodule *sub;
-       gitmodules_config();
-       git_config(submodule_config, NULL);
+       const char *branch = NULL;
+       char *key;
 
-       sub = submodule_from_path(null_sha1, path);
+       sub = submodule_from_path(&null_oid, path);
        if (!sub)
                return NULL;
 
-       if (!sub->branch)
+       key = xstrfmt("submodule.%s.branch", sub->name);
+       if (repo_config_get_string_const(the_repository, key, &branch))
+               branch = sub->branch;
+       free(key);
+
+       if (!branch)
                return "master";
 
-       if (!strcmp(sub->branch, ".")) {
-               unsigned char sha1[20];
-               const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
+       if (!strcmp(branch, ".")) {
+               const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
 
                if (!refname)
                        die(_("No such ref: %s"), "HEAD");
@@ -1085,7 +1104,7 @@ static const char *remote_submodule_branch(const char *path)
                return refname;
        }
 
-       return sub->branch;
+       return branch;
 }
 
 static int resolve_remote_submodule_branch(int argc, const char **argv,
@@ -1168,6 +1187,7 @@ static int push_check(int argc, const char **argv, const char *prefix)
                                                break;
                                        die("HEAD does not match the named branch in the superproject");
                                }
+                               /* fallthrough */
                        default:
                                die("src refspec '%s' must name a ref",
                                    rs->src);
@@ -1204,9 +1224,6 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, embed_gitdir_options,
                             git_submodule_helper_usage, 0);
 
-       gitmodules_config();
-       git_config(submodule_config, NULL);
-
        if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
                return 1;
 
@@ -1222,8 +1239,6 @@ static int is_active(int argc, const char **argv, const char *prefix)
        if (argc != 2)
                die("submodule--helper is-active takes exactly 1 argument");
 
-       gitmodules_config();
-
        return !is_submodule_active(the_repository, argv[1]);
 }
 
index df75cb9d4a21a76df3451f925d674b475dc26753..17aabaa679d599090ef5eb01d0c3650108c4f79c 100644 (file)
@@ -12,9 +12,8 @@ static const char * const git_symbolic_ref_usage[] = {
 
 static int check_symref(const char *HEAD, int quiet, int shorten, int print)
 {
-       unsigned char sha1[20];
        int flag;
-       const char *refname = resolve_ref_unsafe(HEAD, 0, sha1, &flag);
+       const char *refname = resolve_ref_unsafe(HEAD, 0, NULL, &flag);
 
        if (!refname)
                die("No such ref: %s", HEAD);
index 7a70d5a9bb5d456f032fa35dbef9a2fbf4c24124..695cb0778e2cad52d829030b00f91a4b230224e1 100644 (file)
@@ -113,7 +113,7 @@ static int verify_tag(const char *name, const char *ref,
        if (format->format)
                flags = GPG_VERIFY_OMIT_STATUS;
 
-       if (gpg_verify_tag(oid->hash, name, flags))
+       if (gpg_verify_tag(oid, name, flags))
                return -1;
 
        if (format->format)
@@ -411,6 +411,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
                },
                OPT_STRING(  0 , "format", &format.format, N_("format"),
                           N_("format to use for the output")),
+               OPT__COLOR(&format.use_color, N_("respect format colors")),
                OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
                OPT_END()
        };
@@ -552,9 +553,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
        if (force && !is_null_oid(&prev) && oidcmp(&prev, &object))
                printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev.hash, DEFAULT_ABBREV));
 
-       strbuf_release(&err);
-       strbuf_release(&buf);
-       strbuf_release(&ref);
-       strbuf_release(&reflog_msg);
+       UNLEAK(buf);
+       UNLEAK(ref);
+       UNLEAK(reflog_msg);
+       UNLEAK(msg);
+       UNLEAK(err);
        return 0;
 }
index 73f133419167840d150300faa696bc32027cd02f..32e01555774c838e489fd33c675488e754c3e8e2 100644 (file)
@@ -1,7 +1,7 @@
 #include "builtin.h"
 #include "config.h"
 
-static char *create_temp_file(unsigned char *sha1)
+static char *create_temp_file(struct object_id *oid)
 {
        static char path[50];
        void *buf;
@@ -9,13 +9,13 @@ static char *create_temp_file(unsigned char *sha1)
        unsigned long size;
        int fd;
 
-       buf = read_sha1_file(sha1, &type, &size);
+       buf = read_sha1_file(oid->hash, &type, &size);
        if (!buf || type != OBJ_BLOB)
-               die("unable to read blob object %s", sha1_to_hex(sha1));
+               die("unable to read blob object %s", oid_to_hex(oid));
 
        xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
        fd = xmkstemp(path);
-       if (write_in_full(fd, buf, size) != size)
+       if (write_in_full(fd, buf, size) < 0)
                die_errno("unable to write temp-file");
        close(fd);
        return path;
@@ -23,15 +23,15 @@ static char *create_temp_file(unsigned char *sha1)
 
 int cmd_unpack_file(int argc, const char **argv, const char *prefix)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
 
        if (argc != 2 || !strcmp(argv[1], "-h"))
                usage("git unpack-file <sha1>");
-       if (get_sha1(argv[1], sha1))
+       if (get_oid(argv[1], &oid))
                die("Not a valid object name %s", argv[1]);
 
        git_config(git_default_config, NULL);
 
-       puts(create_temp_file(sha1));
+       puts(create_temp_file(&oid));
        return 0;
 }
index 56721cf03db23a2f5a1b8e9419422df916d7b00f..bf7420b808fd1e0314d981bf65073df62f7578a4 100644 (file)
@@ -280,15 +280,17 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
        fill_stat_cache_info(ce, st);
        ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
 
-       if (index_path(ce->oid.hash, path, st,
+       if (index_path(&ce->oid, path, st,
                       info_only ? 0 : HASH_WRITE_OBJECT)) {
                free(ce);
                return -1;
        }
        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
-       if (add_cache_entry(ce, option))
+       if (add_cache_entry(ce, option)) {
+               free(ce);
                return error("%s: cannot add to the index - missing --add option?", path);
+       }
        return 0;
 }
 
@@ -915,7 +917,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
        struct refresh_params refresh_args = {0, &has_errors};
        int lock_error = 0;
        int split_index = -1;
-       struct lock_file *lock_file;
+       struct lock_file lock_file = LOCK_INIT;
        struct parse_opt_ctx_t ctx;
        strbuf_getline_fn getline_fn;
        int parseopt_state = PARSE_OPT_UNKNOWN;
@@ -1014,11 +1016,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
        git_config(git_default_config, NULL);
 
-       /* We can't free this memory, it becomes part of a linked list parsed atexit() */
-       lock_file = xcalloc(1, sizeof(struct lock_file));
-
        /* we will diagnose later if it turns out that we need to update it */
-       newfd = hold_locked_index(lock_file, 0);
+       newfd = hold_locked_index(&lock_file, 0);
        if (newfd < 0)
                lock_error = errno;
 
@@ -1153,11 +1152,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                                exit(128);
                        unable_to_lock_die(get_index_file(), lock_error);
                }
-               if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
+               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                        die("Unable to write new index file");
        }
 
-       rollback_lock_file(lock_file);
+       rollback_lock_file(&lock_file);
 
        return has_errors ? 1 : 0;
 }
index 40ccfc193bf785cfae0f83526c561eedf0d310bc..6b90c5deadb62600aa08166f500bde24cb800fbf 100644 (file)
@@ -94,10 +94,10 @@ static char *parse_refname(struct strbuf *input, const char **next)
  * provided but cannot be converted to a SHA-1, die.  flags can
  * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY.
  */
-static int parse_next_sha1(struct strbuf *input, const char **next,
-                          unsigned char *sha1,
-                          const char *command, const char *refname,
-                          int flags)
+static int parse_next_oid(struct strbuf *input, const char **next,
+                         struct object_id *oid,
+                         const char *command, const char *refname,
+                         int flags)
 {
        struct strbuf arg = STRBUF_INIT;
        int ret = 0;
@@ -115,11 +115,11 @@ static int parse_next_sha1(struct strbuf *input, const char **next,
                (*next)++;
                *next = parse_arg(*next, &arg);
                if (arg.len) {
-                       if (get_sha1(arg.buf, sha1))
+                       if (get_oid(arg.buf, oid))
                                goto invalid;
                } else {
                        /* Without -z, an empty value means all zeros: */
-                       hashclr(sha1);
+                       oidclr(oid);
                }
        } else {
                /* With -z, read the next NUL-terminated line */
@@ -133,13 +133,13 @@ static int parse_next_sha1(struct strbuf *input, const char **next,
                *next += arg.len;
 
                if (arg.len) {
-                       if (get_sha1(arg.buf, sha1))
+                       if (get_oid(arg.buf, oid))
                                goto invalid;
                } else if (flags & PARSE_SHA1_ALLOW_EMPTY) {
                        /* With -z, treat an empty value as all zeros: */
                        warning("%s %s: missing <newvalue>, treating as zero",
                                command, refname);
-                       hashclr(sha1);
+                       oidclr(oid);
                } else {
                        /*
                         * With -z, an empty non-required value means
@@ -182,26 +182,25 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
 {
        struct strbuf err = STRBUF_INIT;
        char *refname;
-       unsigned char new_sha1[20];
-       unsigned char old_sha1[20];
+       struct object_id new_oid, old_oid;
        int have_old;
 
        refname = parse_refname(input, &next);
        if (!refname)
                die("update: missing <ref>");
 
-       if (parse_next_sha1(input, &next, new_sha1, "update", refname,
-                           PARSE_SHA1_ALLOW_EMPTY))
+       if (parse_next_oid(input, &next, &new_oid, "update", refname,
+                          PARSE_SHA1_ALLOW_EMPTY))
                die("update %s: missing <newvalue>", refname);
 
-       have_old = !parse_next_sha1(input, &next, old_sha1, "update", refname,
-                                   PARSE_SHA1_OLD);
+       have_old = !parse_next_oid(input, &next, &old_oid, "update", refname,
+                                  PARSE_SHA1_OLD);
 
        if (*next != line_termination)
                die("update %s: extra input: %s", refname, next);
 
        if (ref_transaction_update(transaction, refname,
-                                  new_sha1, have_old ? old_sha1 : NULL,
+                                  new_oid.hash, have_old ? old_oid.hash : NULL,
                                   update_flags | create_reflog_flag,
                                   msg, &err))
                die("%s", err.buf);
@@ -218,22 +217,22 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
 {
        struct strbuf err = STRBUF_INIT;
        char *refname;
-       unsigned char new_sha1[20];
+       struct object_id new_oid;
 
        refname = parse_refname(input, &next);
        if (!refname)
                die("create: missing <ref>");
 
-       if (parse_next_sha1(input, &next, new_sha1, "create", refname, 0))
+       if (parse_next_oid(input, &next, &new_oid, "create", refname, 0))
                die("create %s: missing <newvalue>", refname);
 
-       if (is_null_sha1(new_sha1))
+       if (is_null_oid(&new_oid))
                die("create %s: zero <newvalue>", refname);
 
        if (*next != line_termination)
                die("create %s: extra input: %s", refname, next);
 
-       if (ref_transaction_create(transaction, refname, new_sha1,
+       if (ref_transaction_create(transaction, refname, new_oid.hash,
                                   update_flags | create_reflog_flag,
                                   msg, &err))
                die("%s", err.buf);
@@ -250,18 +249,18 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
 {
        struct strbuf err = STRBUF_INIT;
        char *refname;
-       unsigned char old_sha1[20];
+       struct object_id old_oid;
        int have_old;
 
        refname = parse_refname(input, &next);
        if (!refname)
                die("delete: missing <ref>");
 
-       if (parse_next_sha1(input, &next, old_sha1, "delete", refname,
-                           PARSE_SHA1_OLD)) {
+       if (parse_next_oid(input, &next, &old_oid, "delete", refname,
+                          PARSE_SHA1_OLD)) {
                have_old = 0;
        } else {
-               if (is_null_sha1(old_sha1))
+               if (is_null_oid(&old_oid))
                        die("delete %s: zero <oldvalue>", refname);
                have_old = 1;
        }
@@ -270,7 +269,7 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
                die("delete %s: extra input: %s", refname, next);
 
        if (ref_transaction_delete(transaction, refname,
-                                  have_old ? old_sha1 : NULL,
+                                  have_old ? old_oid.hash : NULL,
                                   update_flags, msg, &err))
                die("%s", err.buf);
 
@@ -286,20 +285,20 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
 {
        struct strbuf err = STRBUF_INIT;
        char *refname;
-       unsigned char old_sha1[20];
+       struct object_id old_oid;
 
        refname = parse_refname(input, &next);
        if (!refname)
                die("verify: missing <ref>");
 
-       if (parse_next_sha1(input, &next, old_sha1, "verify", refname,
-                           PARSE_SHA1_OLD))
-               hashclr(old_sha1);
+       if (parse_next_oid(input, &next, &old_oid, "verify", refname,
+                          PARSE_SHA1_OLD))
+               oidclr(&old_oid);
 
        if (*next != line_termination)
                die("verify %s: extra input: %s", refname, next);
 
-       if (ref_transaction_verify(transaction, refname, old_sha1,
+       if (ref_transaction_verify(transaction, refname, old_oid.hash,
                                   update_flags, &err))
                die("%s", err.buf);
 
@@ -355,7 +354,7 @@ static void update_refs_stdin(struct ref_transaction *transaction)
 int cmd_update_ref(int argc, const char **argv, const char *prefix)
 {
        const char *refname, *oldval;
-       unsigned char sha1[20], oldsha1[20];
+       struct object_id oid, oldoid;
        int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
        unsigned int flags = 0;
        int create_reflog = 0;
@@ -412,7 +411,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
                refname = argv[0];
                value = argv[1];
                oldval = argv[2];
-               if (get_sha1(value, sha1))
+               if (get_oid(value, &oid))
                        die("%s: not a valid SHA1", value);
        }
 
@@ -422,8 +421,8 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
                         * The empty string implies that the reference
                         * must not already exist:
                         */
-                       hashclr(oldsha1);
-               else if (get_sha1(oldval, oldsha1))
+                       oidclr(&oldoid);
+               else if (get_oid(oldval, &oldoid))
                        die("%s: not a valid old SHA1", oldval);
        }
 
@@ -435,10 +434,10 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
                 * NULL_SHA1 as "don't care" here:
                 */
                return delete_ref(msg, refname,
-                                 (oldval && !is_null_sha1(oldsha1)) ? oldsha1 : NULL,
+                                 (oldval && !is_null_oid(&oldoid)) ? oldoid.hash : NULL,
                                  flags);
        else
-               return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
+               return update_ref(msg, refname, oid.hash, oldval ? oldoid.hash : NULL,
                                  flags | create_reflog_flag,
                                  UPDATE_REFS_DIE_ON_ERR);
 }
index 87d73e856a13e4abaa67ee8cad3e520f1bd9f779..ad7b79fa5cd718daf3be5f1a46a1fbb2bed41ad5 100644 (file)
@@ -58,20 +58,21 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
        }
 
        while (i < argc) {
-               unsigned char sha1[20];
+               struct object_id oid;
                const char *name = argv[i++];
-               if (get_sha1(name, sha1)) {
+
+               if (get_oid(name, &oid)) {
                        had_error = !!error("tag '%s' not found.", name);
                        continue;
                }
 
-               if (gpg_verify_tag(sha1, name, flags)) {
+               if (gpg_verify_tag(&oid, name, flags)) {
                        had_error = 1;
                        continue;
                }
 
                if (format.format)
-                       pretty_print_ref(name, sha1, &format);
+                       pretty_print_ref(name, oid.hash, &format);
        }
        return had_error;
 }
index c98e2ce5f57c1f41ccaf90041a1a22233ae75551..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) {
@@ -381,6 +399,8 @@ static int add(int ac, const char **av, const char *prefix)
                branch = opts.new_branch;
        }
 
+       UNLEAK(path);
+       UNLEAK(opts);
        return add_worktree(path, branch, &opts);
 }
 
index 5be7ce5c730f128903ed226c45834dcb2c5c3214..3310fd210a151545076169b45f5555b52acbbf9d 100644 (file)
@@ -6,6 +6,7 @@
 #include "csum-file.h"
 #include "pack.h"
 #include "strbuf.h"
+#include "packfile.h"
 
 static struct bulk_checkin_state {
        unsigned plugged:1;
@@ -114,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;
index d15db03c84556af8a47783f4d4ee76379d721020..c092d5d68f32652c229fe232af72d02111f51de7 100644 (file)
--- a/bundle.c
+++ b/bundle.c
@@ -157,9 +157,14 @@ int verify_bundle(struct bundle_header *header, int verbose)
        req_nr = revs.pending.nr;
        setup_revisions(2, argv, &revs, NULL);
 
+       /* Save pending objects, so they can be cleaned up later. */
        refs = revs.pending;
        revs.leak_pending = 1;
 
+       /*
+        * prepare_revision_walk (together with .leak_pending = 1) makes us
+        * the sole owner of the list of pending objects.
+        */
        if (prepare_revision_walk(&revs))
                die(_("revision walk setup failed"));
 
@@ -176,8 +181,10 @@ int verify_bundle(struct bundle_header *header, int verbose)
                                refs.objects[i].name);
                }
 
+       /* Clean up objects used, as they will be reused. */
        clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
-       free(refs.objects);
+
+       object_array_clear(&refs);
 
        if (verbose) {
                struct ref_list *r;
index 2440d1dc89175efaf74286da4b2c8f97fc52fbd9..71d092ed514549f03407aef4fd17ab1467191c68 100644 (file)
@@ -603,19 +603,16 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
 int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
 {
        int entries, was_valid, newfd;
-       struct lock_file *lock_file;
+       struct lock_file lock_file = LOCK_INIT;
+       int ret = 0;
 
-       /*
-        * We can't free this memory, it becomes part of a linked list
-        * parsed atexit()
-        */
-       lock_file = xcalloc(1, sizeof(struct lock_file));
-
-       newfd = hold_lock_file_for_update(lock_file, index_path, LOCK_DIE_ON_ERROR);
+       newfd = hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
 
        entries = read_index_from(index_state, index_path);
-       if (entries < 0)
-               return WRITE_TREE_UNREADABLE_INDEX;
+       if (entries < 0) {
+               ret = WRITE_TREE_UNREADABLE_INDEX;
+               goto out;
+       }
        if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
                cache_tree_free(&index_state->cache_tree);
 
@@ -624,10 +621,12 @@ int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, co
 
        was_valid = cache_tree_fully_valid(index_state->cache_tree);
        if (!was_valid) {
-               if (cache_tree_update(index_state, flags) < 0)
-                       return WRITE_TREE_UNMERGED_INDEX;
+               if (cache_tree_update(index_state, flags) < 0) {
+                       ret = WRITE_TREE_UNMERGED_INDEX;
+                       goto out;
+               }
                if (0 <= newfd) {
-                       if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
+                       if (!write_locked_index(index_state, &lock_file, COMMIT_LOCK))
                                newfd = -1;
                }
                /* Not being able to write is fine -- we are only interested
@@ -641,17 +640,19 @@ int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, co
        if (prefix) {
                struct cache_tree *subtree;
                subtree = cache_tree_find(index_state->cache_tree, prefix);
-               if (!subtree)
-                       return WRITE_TREE_PREFIX_ERROR;
+               if (!subtree) {
+                       ret = WRITE_TREE_PREFIX_ERROR;
+                       goto out;
+               }
                hashcpy(sha1, subtree->oid.hash);
        }
        else
                hashcpy(sha1, index_state->cache_tree->oid.hash);
 
+out:
        if (0 <= newfd)
-               rollback_lock_file(lock_file);
-
-       return 0;
+               rollback_lock_file(&lock_file);
+       return ret;
 }
 
 int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
diff --git a/cache.h b/cache.h
index 849bc0dcdd0d6bc572d3524f2646ea7f719eb7a9..6440e2bf21f5800db98f82fcedff46478188674e 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -4,6 +4,7 @@
 #include "git-compat-util.h"
 #include "strbuf.h"
 #include "hashmap.h"
+#include "mru.h"
 #include "advice.h"
 #include "gettext.h"
 #include "convert.h"
@@ -417,7 +418,6 @@ static inline enum object_type object_type(unsigned int mode)
 #define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
 #define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
 #define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
-#define GIT_TOPLEVEL_PREFIX_ENVIRONMENT "GIT_INTERNAL_TOPLEVEL_PREFIX"
 #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
 #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
 #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@ -433,6 +433,7 @@ static inline enum object_type object_type(unsigned int mode)
 #define GITATTRIBUTES_FILE ".gitattributes"
 #define INFOATTRIBUTES_FILE "info/attributes"
 #define ATTRIBUTE_MACRO_PREFIX "[attr]"
+#define GITMODULES_FILE ".gitmodules"
 #define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
 #define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
 #define GIT_NOTES_DISPLAY_REF_ENVIRONMENT "GIT_NOTES_DISPLAY_REF"
@@ -443,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
@@ -684,8 +686,8 @@ extern int ie_modified(const struct index_state *, const struct cache_entry *, s
 
 #define HASH_WRITE_OBJECT 1
 #define HASH_FORMAT_CHECK 2
-extern int index_fd(unsigned char *sha1, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
-extern int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags);
+extern int index_fd(struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
+extern int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags);
 
 /*
  * Record to sd the data from st that we use to check whether a file
@@ -782,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.
@@ -902,20 +909,6 @@ extern void check_repository_format(void);
  */
 extern const char *sha1_file_name(const unsigned char *sha1);
 
-/*
- * Return the name of the (local) packfile with the specified sha1 in
- * its name.  The return value is a pointer to memory that is
- * overwritten each time this function is called.
- */
-extern char *sha1_pack_name(const unsigned char *sha1);
-
-/*
- * Return the name of the (local) pack index file with the specified
- * sha1 in its name.  The return value is a pointer to memory that is
- * overwritten each time this function is called.
- */
-extern char *sha1_pack_index_name(const unsigned char *sha1);
-
 /*
  * Return an abbreviated sha1 unique within this repository's object database.
  * The result will be at least `len` characters long, and will be NUL
@@ -1192,7 +1185,7 @@ static inline const unsigned char *lookup_replace_object(const unsigned char *sh
 extern int sha1_object_info(const unsigned char *, unsigned long *);
 extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
 extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
-extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, unsigned char *sha1, unsigned flags);
+extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, struct object_id *oid, unsigned flags);
 extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
 extern int force_object_loose(const unsigned char *sha1, time_t mtime);
 extern int git_open_cloexec(const char *name, int flags);
@@ -1201,15 +1194,10 @@ extern void *map_sha1_file(const unsigned char *sha1, unsigned long *size);
 extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
 extern int parse_sha1_header(const char *hdr, unsigned long *sizep);
 
-/* global flag to enable extra checks when accessing packed objects */
-extern int do_check_packed_object_crc;
-
 extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
 
 extern int finalize_object_file(const char *tmpfile, const char *filename);
 
-extern int has_sha1_pack(const unsigned char *sha1);
-
 /*
  * Open the loose object at path, check its sha1, and return the contents,
  * type, and size. If the object is a blob, then "contents" may return NULL,
@@ -1245,8 +1233,6 @@ extern int has_object_file_with_flags(const struct object_id *oid, int flags);
  */
 extern int has_loose_object_nonlocal(const unsigned char *sha1);
 
-extern int has_pack_index(const unsigned char *sha1);
-
 extern void assert_sha1_type(const unsigned char *sha1, enum object_type expect);
 
 /* Helper to check and "touch" a file */
@@ -1264,8 +1250,8 @@ static inline unsigned int hexval(unsigned char c)
  */
 static inline int hex2chr(const char *s)
 {
-       int val = hexval(s[0]);
-       return (val < 0) ? val : (val << 4) | hexval(s[1]);
+       unsigned int val = hexval(s[0]);
+       return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
 }
 
 /* Convert to/from hex/sha1 representation */
@@ -1284,38 +1270,37 @@ struct object_context {
         */
        struct strbuf symlink_path;
        /*
-        * If GET_SHA1_RECORD_PATH is set, this will record path (if any)
+        * If GET_OID_RECORD_PATH is set, this will record path (if any)
         * found when resolving the name. The caller is responsible for
         * releasing the memory.
         */
        char *path;
 };
 
-#define GET_SHA1_QUIETLY           01
-#define GET_SHA1_COMMIT            02
-#define GET_SHA1_COMMITTISH        04
-#define GET_SHA1_TREE             010
-#define GET_SHA1_TREEISH          020
-#define GET_SHA1_BLOB             040
-#define GET_SHA1_FOLLOW_SYMLINKS 0100
-#define GET_SHA1_RECORD_PATH     0200
-#define GET_SHA1_ONLY_TO_DIE    04000
-
-#define GET_SHA1_DISAMBIGUATORS \
-       (GET_SHA1_COMMIT | GET_SHA1_COMMITTISH | \
-       GET_SHA1_TREE | GET_SHA1_TREEISH | \
-       GET_SHA1_BLOB)
-
-extern int get_sha1(const char *str, unsigned char *sha1);
-extern int get_sha1_commit(const char *str, unsigned char *sha1);
-extern int get_sha1_committish(const char *str, unsigned char *sha1);
-extern int get_sha1_tree(const char *str, unsigned char *sha1);
-extern int get_sha1_treeish(const char *str, unsigned char *sha1);
-extern int get_sha1_blob(const char *str, unsigned char *sha1);
-extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
-extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc);
+#define GET_OID_QUIETLY           01
+#define GET_OID_COMMIT            02
+#define GET_OID_COMMITTISH        04
+#define GET_OID_TREE             010
+#define GET_OID_TREEISH          020
+#define GET_OID_BLOB             040
+#define GET_OID_FOLLOW_SYMLINKS 0100
+#define GET_OID_RECORD_PATH     0200
+#define GET_OID_ONLY_TO_DIE    04000
+
+#define GET_OID_DISAMBIGUATORS \
+       (GET_OID_COMMIT | GET_OID_COMMITTISH | \
+       GET_OID_TREE | GET_OID_TREEISH | \
+       GET_OID_BLOB)
 
 extern int get_oid(const char *str, struct object_id *oid);
+extern int get_oid_commit(const char *str, struct object_id *oid);
+extern int get_oid_committish(const char *str, struct object_id *oid);
+extern int get_oid_tree(const char *str, struct object_id *oid);
+extern int get_oid_treeish(const char *str, struct object_id *oid);
+extern int get_oid_blob(const char *str, struct object_id *oid);
+extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
+extern int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc);
+
 
 typedef int each_abbrev_fn(const struct object_id *oid, void *);
 extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
@@ -1611,8 +1596,7 @@ extern struct packed_git {
  * A most-recently-used ordered version of the packed_git list, which can
  * be iterated instead of packed_git (and marked via mru_mark).
  */
-struct mru;
-extern struct mru *packed_git_mru;
+extern struct mru packed_git_mru;
 
 struct pack_entry {
        off_t offset;
@@ -1620,29 +1604,6 @@ struct pack_entry {
        struct packed_git *p;
 };
 
-extern struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path);
-
-/* A hook to report invalid files in pack directory */
-#define PACKDIR_FILE_PACK 1
-#define PACKDIR_FILE_IDX 2
-#define PACKDIR_FILE_GARBAGE 4
-extern void (*report_garbage)(unsigned seen_bits, const char *path);
-
-extern void prepare_packed_git(void);
-extern void reprepare_packed_git(void);
-extern void install_packed_git(struct packed_git *pack);
-
-/*
- * Give a rough count of objects in the repository. This sacrifices accuracy
- * for speed.
- */
-unsigned long approximate_object_count(void);
-
-extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
-                                        struct packed_git *packs);
-
-extern void pack_report(void);
-
 /*
  * Create a temporary file rooted in the object database directory, or
  * die on failure. The filename is taken from "pattern", which should have the
@@ -1651,15 +1612,6 @@ extern void pack_report(void);
  */
 extern int odb_mkstemp(struct strbuf *template, const char *pattern);
 
-/*
- * Generate the filename to be used for a pack file with checksum "sha1" and
- * extension "ext". The result is written into the strbuf "buf", overwriting
- * any existing contents. A pointer to buf->buf is returned as a convenience.
- *
- * Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx"
- */
-extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
-
 /*
  * Create a pack .keep file named "name" (which should generally be the output
  * of odb_pack_name). Returns a file descriptor opened for writing, or -1 on
@@ -1667,67 +1619,6 @@ extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const
  */
 extern int odb_pack_keep(const char *name);
 
-/*
- * mmap the index file for the specified packfile (if it is not
- * already mmapped).  Return 0 on success.
- */
-extern int open_pack_index(struct packed_git *);
-
-/*
- * munmap the index file for the specified packfile (if it is
- * currently mmapped).
- */
-extern void close_pack_index(struct packed_git *);
-
-extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
-extern void close_pack_windows(struct packed_git *);
-extern void close_all_packs(void);
-extern void unuse_pack(struct pack_window **);
-extern void clear_delta_base_cache(void);
-extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
-
-/*
- * Make sure that a pointer access into an mmap'd index file is within bounds,
- * and can provide at least 8 bytes of data.
- *
- * Note that this is only necessary for variable-length segments of the file
- * (like the 64-bit extended offset table), as we compare the size to the
- * fixed-length parts when we open the file.
- */
-extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
-
-/*
- * Return the SHA-1 of the nth object within the specified packfile.
- * Open the index if it is not already open.  The return value points
- * at the SHA-1 within the mmapped index.  Return NULL if there is an
- * error.
- */
-extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
-/*
- * Like nth_packed_object_sha1, but write the data into the object specified by
- * the the first argument.  Returns the first argument on success, and NULL on
- * error.
- */
-extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
-
-/*
- * Return the offset of the nth object within the specified packfile.
- * The index must already be opened.
- */
-extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
-
-/*
- * If the object named sha1 is present in the specified packfile,
- * return its offset within the packfile; otherwise, return 0.
- */
-extern off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
-
-extern int is_pack_valid(struct packed_git *);
-extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
-extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
-extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
-extern int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, unsigned long *);
-
 /*
  * Iterate over the files in the loose-object parts of the object
  * directory "path", triggering the following callbacks:
@@ -1777,17 +1668,12 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
                                      void *data);
 
 /*
- * Iterate over loose and packed objects in both the local
+ * Iterate over loose objects in both the local
  * repository and any alternates repositories (unless the
  * LOCAL_ONLY flag is set).
  */
 #define FOR_EACH_OBJECT_LOCAL_ONLY 0x1
-typedef int each_packed_object_fn(const struct object_id *oid,
-                                 struct packed_git *pack,
-                                 uint32_t pos,
-                                 void *data);
 extern int for_each_loose_object(each_loose_object_fn, void *, unsigned flags);
-extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
 
 struct object_info {
        /* Request */
@@ -1837,7 +1723,6 @@ struct object_info {
 /* Do not retry packed storage after checking packed and loose storage */
 #define OBJECT_INFO_QUICK 8
 extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
-extern int packed_object_info(struct packed_git *pack, off_t offset, struct object_info *);
 
 /* Dumb servers support */
 extern int update_server_info(int);
@@ -1956,6 +1841,8 @@ void shift_tree_by(const struct object_id *, const struct object_id *, struct ob
 #define WS_TRAILING_SPACE      (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
 #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
 #define WS_TAB_WIDTH_MASK        077
+/* All WS_* -- when extended, adapt diff.c emit_symbol */
+#define WS_RULE_MASK           07777
 extern unsigned whitespace_rule_cfg;
 extern unsigned whitespace_rule(const char *);
 extern unsigned parse_whitespace_rule(const char *);
diff --git a/ci/install-dependencies.sh b/ci/install-dependencies.sh
new file mode 100755 (executable)
index 0000000..a29246a
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/env bash
+#
+# Install dependencies required to build and test Git on Linux and macOS
+#
+
+. ${0%/*}/lib-travisci.sh
+
+P4WHENCE=http://filehost.perforce.com/perforce/r$LINUX_P4_VERSION
+LFSWHENCE=https://github.com/github/git-lfs/releases/download/v$LINUX_GIT_LFS_VERSION
+
+case "${TRAVIS_OS_NAME:-linux}" in
+linux)
+       export GIT_TEST_HTTPD=YesPlease
+
+       mkdir --parents custom/p4
+       pushd custom/p4
+               wget --quiet "$P4WHENCE/bin.linux26x86_64/p4d"
+               wget --quiet "$P4WHENCE/bin.linux26x86_64/p4"
+               chmod u+x p4d
+               chmod u+x p4
+               export PATH="$(pwd):$PATH"
+       popd
+       mkdir --parents custom/git-lfs
+       pushd custom/git-lfs
+               wget --quiet "$LFSWHENCE/git-lfs-linux-amd64-$LINUX_GIT_LFS_VERSION.tar.gz"
+               tar --extract --gunzip --file "git-lfs-linux-amd64-$LINUX_GIT_LFS_VERSION.tar.gz"
+               cp git-lfs-$LINUX_GIT_LFS_VERSION/git-lfs .
+               export PATH="$(pwd):$PATH"
+       popd
+       ;;
+osx)
+       brew update --quiet
+       # Uncomment this if you want to run perf tests:
+       # brew install gnu-time
+       brew install git-lfs gettext
+       brew link --force gettext
+       brew install caskroom/cask/perforce
+       ;;
+esac
+
+echo "$(tput setaf 6)Perforce Server Version$(tput sgr0)"
+p4d -V | grep Rev.
+echo "$(tput setaf 6)Perforce Client Version$(tput sgr0)"
+p4 -V | grep Rev.
+echo "$(tput setaf 6)Git-LFS Version$(tput sgr0)"
+git-lfs version
diff --git a/ci/lib-travisci.sh b/ci/lib-travisci.sh
new file mode 100755 (executable)
index 0000000..b3ed0a0
--- /dev/null
@@ -0,0 +1,28 @@
+# Library of functions shared by all CI scripts
+
+skip_branch_tip_with_tag () {
+       # Sometimes, a branch is pushed at the same time the tag that points
+       # at the same commit as the tip of the branch is pushed, and building
+       # both at the same time is a waste.
+       #
+       # Travis gives a tagname e.g. v2.14.0 in $TRAVIS_BRANCH when
+       # the build is triggered by a push to a tag.  Let's see if
+       # $TRAVIS_BRANCH is exactly at a tag, and if so, if it is
+       # different from $TRAVIS_BRANCH.  That way, we can tell if
+       # we are building the tip of a branch that is tagged and
+       # we can skip the build because we won't be skipping a build
+       # of a tag.
+
+       if TAG=$(git describe --exact-match "$TRAVIS_BRANCH" 2>/dev/null) &&
+               test "$TAG" != "$TRAVIS_BRANCH"
+       then
+               echo "Tip of $TRAVIS_BRANCH is exactly at $TAG"
+               exit 0
+       fi
+}
+
+# Set 'exit on error' for all CI scripts to let the caller know that
+# something went wrong
+set -e
+
+skip_branch_tip_with_tag
diff --git a/ci/print-test-failures.sh b/ci/print-test-failures.sh
new file mode 100755 (executable)
index 0000000..8c8973c
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/sh
+#
+# Print output of failing tests
+#
+
+. ${0%/*}/lib-travisci.sh
+
+for TEST_EXIT in t/test-results/*.exit
+do
+       if [ "$(cat "$TEST_EXIT")" != "0" ]
+       then
+               TEST_OUT="${TEST_EXIT%exit}out"
+               echo "------------------------------------------------------------------------"
+               echo "$(tput setaf 1)${TEST_OUT}...$(tput sgr0)"
+               echo "------------------------------------------------------------------------"
+               cat "${TEST_OUT}"
+       fi
+done
diff --git a/ci/run-build.sh b/ci/run-build.sh
new file mode 100755 (executable)
index 0000000..4f940d1
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/sh
+#
+# Build Git
+#
+
+. ${0%/*}/lib-travisci.sh
+
+make --jobs=2
diff --git a/ci/run-linux32-docker.sh b/ci/run-linux32-docker.sh
new file mode 100755 (executable)
index 0000000..0edf63a
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# Download and run Docker image to build and test 32-bit Git
+#
+
+. ${0%/*}/lib-travisci.sh
+
+docker pull daald/ubuntu32:xenial
+
+# Use the following command to debug the docker build locally:
+# $ docker run -itv "${PWD}:/usr/src/git" --entrypoint /bin/bash daald/ubuntu32:xenial
+# root@container:/# /usr/src/git/ci/run-linux32-build.sh
+
+docker run \
+       --interactive \
+       --env DEVELOPER \
+       --env DEFAULT_TEST_TARGET \
+       --env GIT_PROVE_OPTS \
+       --env GIT_TEST_OPTS \
+       --env GIT_TEST_CLONE_2GB \
+       --volume "${PWD}:/usr/src/git" \
+       daald/ubuntu32:xenial \
+       /usr/src/git/ci/run-linux32-build.sh $(id -u $USER)
diff --git a/ci/run-static-analysis.sh b/ci/run-static-analysis.sh
new file mode 100755 (executable)
index 0000000..68dd0f0
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/sh
+#
+# Perform various static code analysis checks
+#
+
+. ${0%/*}/lib-travisci.sh
+
+make coccicheck
diff --git a/ci/run-tests.sh b/ci/run-tests.sh
new file mode 100755 (executable)
index 0000000..f0c743d
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/sh
+#
+# Test Git
+#
+
+. ${0%/*}/lib-travisci.sh
+
+mkdir -p $HOME/travis-cache
+ln -s $HOME/travis-cache/.prove t/.prove
+make --quiet test
index 2d98f6b2f94b12aafbebcd440b3809bf48c2aca6..8757b3a97c5d7598bfdb2ec1f42cbb6851bae448 100755 (executable)
@@ -6,6 +6,8 @@
 # supported) and a commit hash.
 #
 
+. ${0%/*}/lib-travisci.sh
+
 test $# -ne 2 && echo "Unexpected number of parameters" && exit 1
 test -z "$GFW_CI_TOKEN" && echo "GFW_CI_TOKEN not defined" && exit
 
index 6214e6acb4a1acbc3eef764a8c610a5f6e100ee0..7a0a848e83d68152acaa1de50fd7fb5fe9990151 100755 (executable)
@@ -3,7 +3,9 @@
 # Perform sanity checks on documentation and build it.
 #
 
-set -e
+. ${0%/*}/lib-travisci.sh
+
+gem install asciidoctor
 
 make check-builtins
 make check-docs
diff --git a/color.c b/color.c
index 7aa8b076f045e5c0b74826153030e2923c0fc56e..9c0dc823701d6aff0979659cd19ebb9c92f2eb8a 100644 (file)
--- a/color.c
+++ b/color.c
@@ -308,7 +308,7 @@ int git_config_colorbool(const char *var, const char *value)
                if (!strcasecmp(value, "never"))
                        return 0;
                if (!strcasecmp(value, "always"))
-                       return 1;
+                       return var ? GIT_COLOR_AUTO : 1;
                if (!strcasecmp(value, "auto"))
                        return GIT_COLOR_AUTO;
        }
@@ -338,6 +338,13 @@ static int check_auto_color(void)
 
 int want_color(int var)
 {
+       /*
+        * NEEDSWORK: This function is sometimes used from multiple threads, and
+        * we end up using want_auto racily. That "should not matter" since
+        * we always write the same value, but it's still wrong. This function
+        * is listed in .tsan-suppressions for the time being.
+        */
+
        static int want_auto = -1;
 
        if (var < 0)
diff --git a/color.h b/color.h
index 90627650fccc67c31261977a22544af131812bd1..fd2b688dfbccbe4eef4c1f79b680db2a01e2f9ef 100644 (file)
--- a/color.h
+++ b/color.h
@@ -42,6 +42,8 @@ struct strbuf;
 #define GIT_COLOR_BG_BLUE      "\033[44m"
 #define GIT_COLOR_BG_MAGENTA   "\033[45m"
 #define GIT_COLOR_BG_CYAN      "\033[46m"
+#define GIT_COLOR_FAINT                "\033[2m"
+#define GIT_COLOR_FAINT_ITALIC "\033[2;3m"
 
 /* A special value meaning "no color selected" */
 #define GIT_COLOR_NIL "NIL"
index 333d81e370b188aae2110860df9eaff7b9d9582f..dcaab8ca0437b3adefb523cad531ac1f4b9264e0 100644 (file)
@@ -78,7 +78,7 @@ static MAYBE_UNUSED void init_ ##slabname(struct slabname *s)         \
                                                                        \
 static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s)         \
 {                                                                      \
-       int i;                                                          \
+       unsigned int i;                                                 \
        for (i = 0; i < s->slab_count; i++)                             \
                free(s->slab[i]);                                       \
        s->slab_count = 0;                                              \
@@ -89,13 +89,13 @@ static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s,       \
                                                  const struct commit *c, \
                                                  int add_if_missing)   \
 {                                                                      \
-       int nth_slab, nth_slot;                                         \
+       unsigned int nth_slab, nth_slot;                                \
                                                                        \
        nth_slab = c->index / s->slab_size;                             \
        nth_slot = c->index % s->slab_size;                             \
                                                                        \
        if (s->slab_count <= nth_slab) {                                \
-               int i;                                                  \
+               unsigned int i;                                         \
                if (!add_if_missing)                                    \
                        return NULL;                                    \
                REALLOC_ARRAY(s->slab, nth_slab + 1);                   \
index d3150d627071316accafb7d9c69b5988050f4a8e..1e0e633790bb834ad05ed9619e4afa0bac33ce19 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -59,7 +59,7 @@ struct commit *lookup_commit_reference_by_name(const char *name)
        struct object_id oid;
        struct commit *commit;
 
-       if (get_sha1_committish(name, oid.hash))
+       if (get_oid_committish(name, &oid))
                return NULL;
        commit = lookup_commit_reference(&oid);
        if (parse_commit(commit))
@@ -134,35 +134,41 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
        return 0;
 }
 
-struct commit_graft *read_graft_line(char *buf, int len)
+struct commit_graft *read_graft_line(struct strbuf *line)
 {
        /* The format is just "Commit Parent1 Parent2 ...\n" */
-       int i;
+       int i, phase;
+       const char *tail = NULL;
        struct commit_graft *graft = NULL;
-       const int entry_size = GIT_SHA1_HEXSZ + 1;
+       struct object_id dummy_oid, *oid;
 
-       while (len && isspace(buf[len-1]))
-               buf[--len] = '\0';
-       if (buf[0] == '#' || buf[0] == '\0')
+       strbuf_rtrim(line);
+       if (!line->len || line->buf[0] == '#')
                return NULL;
-       if ((len + 1) % entry_size)
-               goto bad_graft_data;
-       i = (len + 1) / entry_size - 1;
-       graft = xmalloc(st_add(sizeof(*graft), st_mult(GIT_SHA1_RAWSZ, i)));
-       graft->nr_parent = i;
-       if (get_oid_hex(buf, &graft->oid))
-               goto bad_graft_data;
-       for (i = GIT_SHA1_HEXSZ; i < len; i += entry_size) {
-               if (buf[i] != ' ')
-                       goto bad_graft_data;
-               if (get_sha1_hex(buf + i + 1, graft->parent[i/entry_size].hash))
+       /*
+        * phase 0 verifies line, counts hashes in line and allocates graft
+        * phase 1 fills graft
+        */
+       for (phase = 0; phase < 2; phase++) {
+               oid = graft ? &graft->oid : &dummy_oid;
+               if (parse_oid_hex(line->buf, oid, &tail))
                        goto bad_graft_data;
+               for (i = 0; *tail != '\0'; i++) {
+                       oid = graft ? &graft->parent[i] : &dummy_oid;
+                       if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
+                               goto bad_graft_data;
+               }
+               if (!graft) {
+                       graft = xmalloc(st_add(sizeof(*graft),
+                                              st_mult(sizeof(struct object_id), i)));
+                       graft->nr_parent = i;
+               }
        }
        return graft;
 
 bad_graft_data:
-       error("bad graft data: %s", buf);
-       free(graft);
+       error("bad graft data: %s", line->buf);
+       assert(!graft);
        return NULL;
 }
 
@@ -174,7 +180,7 @@ static int read_graft_file(const char *graft_file)
                return -1;
        while (!strbuf_getwholeline(&buf, fp, '\n')) {
                /* The format is just "Commit Parent1 Parent2 ...\n" */
-               struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
+               struct commit_graft *graft = read_graft_line(&buf);
                if (!graft)
                        continue;
                if (register_commit_graft(graft, 1))
@@ -199,11 +205,11 @@ static void prepare_commit_graft(void)
        commit_graft_prepared = 1;
 }
 
-struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
+struct commit_graft *lookup_commit_graft(const struct object_id *oid)
 {
        int pos;
        prepare_commit_graft();
-       pos = commit_graft_pos(sha1);
+       pos = commit_graft_pos(oid->hash);
        if (pos < 0)
                return NULL;
        return commit_graft[pos];
@@ -334,7 +340,7 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
        bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
        pptr = &item->parents;
 
-       graft = lookup_commit_graft(item->object.oid.hash);
+       graft = lookup_commit_graft(&item->object.oid);
        while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
                struct commit *new_parent;
 
@@ -1080,6 +1086,7 @@ struct commit_list *reduce_heads(struct commit_list *heads)
        num_head = remove_redundant(array, num_head);
        for (i = 0; i < num_head; i++)
                tail = &commit_list_insert(array[i], tail)->next;
+       free(array);
        return result;
 }
 
@@ -1564,10 +1571,13 @@ int commit_tree_extended(const char *msg, size_t msg_len,
        if (encoding_is_utf8 && !verify_utf8(&buffer))
                fprintf(stderr, _(commit_utf8_warn));
 
-       if (sign_commit && do_sign_commit(&buffer, sign_commit))
-               return -1;
+       if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
+               result = -1;
+               goto out;
+       }
 
        result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
+out:
        strbuf_release(&buffer);
        return result;
 }
@@ -1586,7 +1596,7 @@ struct commit *get_merge_parent(const char *name)
        struct object *obj;
        struct commit *commit;
        struct object_id oid;
-       if (get_sha1(name, oid.hash))
+       if (get_oid(name, &oid))
                return NULL;
        obj = parse_object(&oid);
        commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
index f3805fec6b3a5853316d1476619adc5410224a7e..6d769590f285e534d20bfd8108e8e980253f8815 100644 (file)
--- a/commit.h
+++ b/commit.h
@@ -247,9 +247,9 @@ struct commit_graft {
 };
 typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
 
-struct commit_graft *read_graft_line(char *buf, int len);
+struct commit_graft *read_graft_line(struct strbuf *line);
 int register_commit_graft(struct commit_graft *, int);
-struct commit_graft *lookup_commit_graft(const unsigned char *sha1);
+struct commit_graft *lookup_commit_graft(const struct object_id *oid);
 
 extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2);
 extern struct commit_list *get_merge_bases_many(struct commit *one, int n, struct commit **twos);
index b10adc780fc334383f2684d21fa1c5a8d92771b9..ae03b74a6f4e9c8ff6d519d9a784ab6b84179872 100644 (file)
@@ -438,6 +438,10 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
            pfd[i].revents = happened;
            rc++;
          }
+       else
+         {
+           pfd[i].revents = 0;
+         }
       }
 
   return rc;
diff --git a/compat/win32/lazyload.h b/compat/win32/lazyload.h
new file mode 100644 (file)
index 0000000..9e631c8
--- /dev/null
@@ -0,0 +1,57 @@
+#ifndef LAZYLOAD_H
+#define LAZYLOAD_H
+
+/*
+ * A pair of macros to simplify loading of DLL functions. Example:
+ *
+ *   DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,
+ *                     LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
+ *
+ *   if (!INIT_PROC_ADDR(CreateHardLinkW))
+ *           return error("Could not find CreateHardLinkW() function";
+ *
+ *   if (!CreateHardLinkW(source, target, NULL))
+ *           return error("could not create hardlink from %S to %S",
+ *                        source, target);
+ */
+
+struct proc_addr {
+       const char *const dll;
+       const char *const function;
+       FARPROC pfunction;
+       unsigned initialized : 1;
+};
+
+/* Declares a function to be loaded dynamically from a DLL. */
+#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \
+       static struct proc_addr proc_addr_##function = \
+       { #dll, #function, NULL, 0 }; \
+       static rettype (WINAPI *function)(__VA_ARGS__)
+
+/*
+ * Loads a function from a DLL (once-only).
+ * Returns non-NULL function pointer on success.
+ * Returns NULL + errno == ENOSYS on failure.
+ * This function is not thread-safe.
+ */
+#define INIT_PROC_ADDR(function) \
+       (function = get_proc_addr(&proc_addr_##function))
+
+static inline void *get_proc_addr(struct proc_addr *proc)
+{
+       /* only do this once */
+       if (!proc->initialized) {
+               HANDLE hnd;
+               proc->initialized = 1;
+               hnd = LoadLibraryExA(proc->dll, NULL,
+                                    LOAD_LIBRARY_SEARCH_SYSTEM32);
+               if (hnd)
+                       proc->pfunction = GetProcAddress(hnd, proc->function);
+       }
+       /* set ENOSYS if DLL or function was not found */
+       if (!proc->pfunction)
+               errno = ENOSYS;
+       return proc->pfunction;
+}
+
+#endif
index acebc85d81372f15d3d490d84dc751f59bde26c4..4831c1273542706437139ccc379afb38e7ca3d3b 100644 (file)
--- a/config.c
+++ b/config.c
@@ -929,7 +929,7 @@ ssize_t git_config_ssize_t(const char *name, const char *value)
        return ret;
 }
 
-int git_parse_maybe_bool(const char *value)
+static int git_parse_maybe_bool_text(const char *value)
 {
        if (!value)
                return 1;
@@ -946,9 +946,9 @@ int git_parse_maybe_bool(const char *value)
        return -1;
 }
 
-int git_config_maybe_bool(const char *name, const char *value)
+int git_parse_maybe_bool(const char *value)
 {
-       int v = git_parse_maybe_bool(value);
+       int v = git_parse_maybe_bool_text(value);
        if (0 <= v)
                return v;
        if (git_parse_int(value, &v))
@@ -958,7 +958,7 @@ int git_config_maybe_bool(const char *name, const char *value)
 
 int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
 {
-       int v = git_parse_maybe_bool(value);
+       int v = git_parse_maybe_bool_text(value);
        if (0 <= v) {
                *is_bool = 1;
                return v;
@@ -1464,9 +1464,9 @@ int git_config_from_mem(config_fn_t fn, const enum config_origin_type origin_typ
        return do_config_from(&top, fn, data);
 }
 
-int git_config_from_blob_sha1(config_fn_t fn,
+int git_config_from_blob_oid(config_fn_t fn,
                              const char *name,
-                             const unsigned char *sha1,
+                             const struct object_id *oid,
                              void *data)
 {
        enum object_type type;
@@ -1474,7 +1474,7 @@ int git_config_from_blob_sha1(config_fn_t fn,
        unsigned long size;
        int ret;
 
-       buf = read_sha1_file(sha1, &type, &size);
+       buf = read_sha1_file(oid->hash, &type, &size);
        if (!buf)
                return error("unable to load config blob object '%s'", name);
        if (type != OBJ_BLOB) {
@@ -1492,11 +1492,11 @@ static int git_config_from_blob_ref(config_fn_t fn,
                                    const char *name,
                                    void *data)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
 
-       if (get_sha1(name, sha1) < 0)
+       if (get_oid(name, &oid) < 0)
                return error("unable to resolve config blob '%s'", name);
-       return git_config_from_blob_sha1(fn, name, sha1, data);
+       return git_config_from_blob_oid(fn, name, &oid, data);
 }
 
 const char *git_etc_gitconfig(void)
@@ -1719,17 +1719,19 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
 }
 
 static int config_set_element_cmp(const void *unused_cmp_data,
-                                 const struct config_set_element *e1,
-                                 const struct config_set_element *e2,
+                                 const void *entry,
+                                 const void *entry_or_key,
                                  const void *unused_keydata)
 {
+       const struct config_set_element *e1 = entry;
+       const struct config_set_element *e2 = entry_or_key;
+
        return strcmp(e1->key, e2->key);
 }
 
 void git_configset_init(struct config_set *cs)
 {
-       hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
-                    NULL, 0);
+       hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0);
        cs->hash_initialized = 1;
        cs->list.nr = 0;
        cs->list.alloc = 0;
@@ -1850,7 +1852,7 @@ int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *de
 {
        const char *value;
        if (!git_configset_get_value(cs, key, &value)) {
-               *dest = git_config_maybe_bool(key, value);
+               *dest = git_parse_maybe_bool(value);
                if (*dest == -1)
                        return -1;
                return 0;
@@ -2057,6 +2059,23 @@ int git_config_get_pathname(const char *key, const char **dest)
        return repo_config_get_pathname(the_repository, key, dest);
 }
 
+/*
+ * Note: This function exists solely to maintain backward compatibility with
+ * 'fetch' and 'update_clone' storing configuration in '.gitmodules' and should
+ * NOT be used anywhere else.
+ *
+ * Runs the provided config function on the '.gitmodules' file found in the
+ * working directory.
+ */
+void config_from_gitmodules(config_fn_t fn, void *data)
+{
+       if (the_repository->worktree) {
+               char *file = repo_worktree_path(the_repository, GITMODULES_FILE);
+               git_config_from_file(fn, file, data);
+               free(file);
+       }
+}
+
 int git_config_get_expiry(const char *key, const char **output)
 {
        int ret = git_config_get_string_const(key, output);
@@ -2070,6 +2089,28 @@ int git_config_get_expiry(const char *key, const char **output)
        return ret;
 }
 
+int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestamp_t now)
+{
+       char *expiry_string;
+       intmax_t days;
+       timestamp_t when;
+
+       if (git_config_get_string(key, &expiry_string))
+               return 1; /* no such thing */
+
+       if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) {
+               const int scale = 86400;
+               *expiry = now - days * scale;
+               return 0;
+       }
+
+       if (!parse_expiry_date(expiry_string, &when)) {
+               *expiry = when;
+               return 0;
+       }
+       return -1; /* thing exists but cannot be parsed */
+}
+
 int git_config_get_untracked_cache(void)
 {
        int val = -1;
@@ -2159,7 +2200,7 @@ static struct {
        size_t *offset;
        unsigned int offset_alloc;
        enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
-       int seen;
+       unsigned int seen;
 } store;
 
 static int matches(const char *key, const char *value)
@@ -2251,10 +2292,10 @@ static int write_error(const char *filename)
        return 4;
 }
 
-static int store_write_section(int fd, const char *key)
+static struct strbuf store_create_section(const char *key)
 {
        const char *dot;
-       int i, success;
+       int i;
        struct strbuf sb = STRBUF_INIT;
 
        dot = memchr(key, '.', store.baselen);
@@ -2270,15 +2311,24 @@ static int store_write_section(int fd, const char *key)
                strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
        }
 
-       success = write_in_full(fd, sb.buf, sb.len) == 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 success;
+       return ret;
 }
 
-static int store_write_pair(int fd, const char *key, const char *value)
+static ssize_t write_pair(int fd, const char *key, const char *value)
 {
-       int i, success;
+       int i;
+       ssize_t ret;
        int length = strlen(key + store.baselen + 1);
        const char *quote = "";
        struct strbuf sb = STRBUF_INIT;
@@ -2312,16 +2362,17 @@ static int store_write_pair(int fd, const char *key, const char *value)
                case '"':
                case '\\':
                        strbuf_addch(&sb, '\\');
+                       /* fallthrough */
                default:
                        strbuf_addch(&sb, value[i]);
                        break;
                }
        strbuf_addf(&sb, "%s\n", quote);
 
-       success = write_in_full(fd, sb.buf, sb.len) == sb.len;
+       ret = write_in_full(fd, sb.buf, sb.len);
        strbuf_release(&sb);
 
-       return success;
+       return ret;
 }
 
 static ssize_t find_beginning_of_line(const char *contents, size_t size,
@@ -2404,7 +2455,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
 {
        int fd = -1, in_fd = -1;
        int ret;
-       struct lock_file *lock = NULL;
+       struct lock_file lock = LOCK_INIT;
        char *filename_buf = NULL;
        char *contents = NULL;
        size_t contents_sz;
@@ -2423,8 +2474,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
         * The lock serves a purpose in addition to locking: the new
         * contents of .git/config will be written into it.
         */
-       lock = xcalloc(1, sizeof(struct lock_file));
-       fd = hold_lock_file_for_update(lock, config_filename, 0);
+       fd = hold_lock_file_for_update(&lock, config_filename, 0);
        if (fd < 0) {
                error_errno("could not lock config file %s", config_filename);
                free(store.key);
@@ -2451,8 +2501,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
                }
 
                store.key = (char *)key;
-               if (!store_write_section(fd, key) ||
-                   !store_write_pair(fd, key, value))
+               if (write_section(fd, key) < 0 ||
+                   write_pair(fd, key, value) < 0)
                        goto write_err_out;
        } else {
                struct stat st;
@@ -2537,8 +2587,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
                close(in_fd);
                in_fd = -1;
 
-               if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
-                       error_errno("chmod on %s failed", get_lock_file_path(lock));
+               if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
+                       error_errno("chmod on %s failed", get_lock_file_path(&lock));
                        ret = CONFIG_NO_WRITE;
                        goto out_free;
                }
@@ -2562,11 +2612,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
                        /* write the first part of the config */
                        if (copy_end > copy_begin) {
                                if (write_in_full(fd, contents + copy_begin,
-                                                 copy_end - copy_begin) <
-                                   copy_end - copy_begin)
+                                                 copy_end - copy_begin) < 0)
                                        goto write_err_out;
                                if (new_line &&
-                                   write_str_in_full(fd, "\n") != 1)
+                                   write_str_in_full(fd, "\n") < 0)
                                        goto write_err_out;
                        }
                        copy_begin = store.offset[i];
@@ -2575,46 +2624,36 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
                /* write the pair (value == NULL means unset) */
                if (value != NULL) {
                        if (store.state == START) {
-                               if (!store_write_section(fd, key))
+                               if (write_section(fd, key) < 0)
                                        goto write_err_out;
                        }
-                       if (!store_write_pair(fd, key, value))
+                       if (write_pair(fd, key, value) < 0)
                                goto write_err_out;
                }
 
                /* write the rest of the config */
                if (copy_begin < contents_sz)
                        if (write_in_full(fd, contents + copy_begin,
-                                         contents_sz - copy_begin) <
-                           contents_sz - copy_begin)
+                                         contents_sz - copy_begin) < 0)
                                goto write_err_out;
 
                munmap(contents, contents_sz);
                contents = NULL;
        }
 
-       if (commit_lock_file(lock) < 0) {
+       if (commit_lock_file(&lock) < 0) {
                error_errno("could not write config file %s", config_filename);
                ret = CONFIG_NO_WRITE;
-               lock = NULL;
                goto out_free;
        }
 
-       /*
-        * lock is committed, so don't try to roll it back below.
-        * NOTE: Since lockfile.c keeps a linked list of all created
-        * lock_file structures, it isn't safe to free(lock).  It's
-        * better to just leave it hanging around.
-        */
-       lock = NULL;
        ret = 0;
 
        /* Invalidate the config cache */
        git_config_clear();
 
 out_free:
-       if (lock)
-               rollback_lock_file(lock);
+       rollback_lock_file(&lock);
        free(filename_buf);
        if (contents)
                munmap(contents, contents_sz);
@@ -2623,7 +2662,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
        return ret;
 
 write_err_out:
-       ret = write_error(get_lock_file_path(lock));
+       ret = write_error(get_lock_file_path(&lock));
        goto out_free;
 
 }
@@ -2711,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;
@@ -2721,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);
@@ -2759,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) {
@@ -2772,25 +2830,29 @@ int git_config_rename_section_in_file(const char *config_filename,
                                        continue;
                                }
                                store.baselen = strlen(new_name);
-                               if (!store_write_section(out_fd, new_name)) {
-                                       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;
@@ -2798,11 +2860,30 @@ int git_config_rename_section_in_file(const char *config_filename,
                if (remove)
                        continue;
                length = strlen(output);
-               if (write_in_full(out_fd, output, length) != length) {
+
+               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:
@@ -2818,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 0352da117b9a61498311da5a5b5fdf5e14af8b58..a49d26441622508fd3c6560ac9784e84541c10dc 100644 (file)
--- a/config.h
+++ b/config.h
@@ -39,8 +39,8 @@ extern int git_default_config(const char *, const char *, void *);
 extern int git_config_from_file(config_fn_t fn, const char *, void *);
 extern int git_config_from_mem(config_fn_t fn, const enum config_origin_type,
                                        const char *name, const char *buf, size_t len, void *data);
-extern int git_config_from_blob_sha1(config_fn_t fn, const char *name,
-                                    const unsigned char *sha1, void *data);
+extern int git_config_from_blob_oid(config_fn_t fn, const char *name,
+                                   const struct object_id *oid, void *data);
 extern void git_config_push_parameter(const char *text);
 extern int git_config_from_parameters(config_fn_t fn, void *data);
 extern void read_early_config(config_fn_t cb, void *data);
@@ -56,7 +56,6 @@ extern unsigned long git_config_ulong(const char *, const char *);
 extern ssize_t git_config_ssize_t(const char *, const char *);
 extern int git_config_bool_or_int(const char *, const char *, int *);
 extern int git_config_bool(const char *, const char *);
-extern int git_config_maybe_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
 extern int git_config_pathname(const char **, const char *, const char *);
 extern int git_config_set_in_file_gently(const char *, const char *, const char *);
@@ -71,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);
@@ -187,6 +188,16 @@ extern int repo_config_get_maybe_bool(struct repository *repo,
 extern int repo_config_get_pathname(struct repository *repo,
                                    const char *key, const char **dest);
 
+/*
+ * Note: This function exists solely to maintain backward compatibility with
+ * 'fetch' and 'update_clone' storing configuration in '.gitmodules' and should
+ * NOT be used anywhere else.
+ *
+ * Runs the provided config function on the '.gitmodules' file found in the
+ * working directory.
+ */
+extern void config_from_gitmodules(config_fn_t fn, void *data);
+
 extern int git_config_get_value(const char *key, const char **value);
 extern const struct string_list *git_config_get_value_multi(const char *key);
 extern void git_config_clear(void);
@@ -205,6 +216,9 @@ extern int git_config_get_max_percent_split_change(void);
 /* This dies if the configured or default date is in the future */
 extern int git_config_get_expiry(const char *key, const char **output);
 
+/* parse either "this many days" integer, or "5.days.ago" approxidate */
+extern int git_config_get_expiry_in_days(const char *key, timestamp_t *, timestamp_t now);
+
 struct key_value_info {
        const char *filename;
        int linenr;
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 49b28b83be2717027ac33570b1041604db5a062d..df56c0cbff4dfb1263917f6147bd7e1912418dd6 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -778,7 +778,6 @@ struct child_process *git_connect(int fd[2], const char *url,
        char *hostandport, *path;
        struct child_process *conn = &no_fork;
        enum protocol protocol;
-       struct strbuf cmd = STRBUF_INIT;
 
        /* Without this we cannot rely on waitpid() to tell
         * what happened to our children.
@@ -826,6 +825,8 @@ struct child_process *git_connect(int fd[2], const char *url,
                             target_host, 0);
                free(target_host);
        } else {
+               struct strbuf cmd = STRBUF_INIT;
+
                conn = xmalloc(sizeof(*conn));
                child_process_init(conn);
 
@@ -862,6 +863,7 @@ struct child_process *git_connect(int fd[2], const char *url,
                                free(hostandport);
                                free(path);
                                free(conn);
+                               strbuf_release(&cmd);
                                return NULL;
                        }
 
index 136c2ac16839b8d4de9b653337dc4794c2a4ea34..f416b05051f3b794c9be74e9baaf3b84e18ff6a2 100644 (file)
@@ -3,6 +3,7 @@
 #include "sigchain.h"
 #include "connected.h"
 #include "transport.h"
+#include "packfile.h"
 
 /*
  * If we feed all the commits we want to verify to this command
index c61d1ca8dc49f1da8dd70cf0c00ce97b1f18fcd2..01586821dc7c15be2a94c30bfbf4f3b4c94ac5f3 100644 (file)
@@ -4,7 +4,7 @@ T *dst;
 T *src;
 expression n;
 @@
-- memcpy(dst, src, n * sizeof(*dst));
+- memcpy(dst, src, (n) * sizeof(*dst));
 + COPY_ARRAY(dst, src, n);
 
 @@
@@ -13,7 +13,7 @@ T *dst;
 T *src;
 expression n;
 @@
-- memcpy(dst, src, n * sizeof(*src));
+- memcpy(dst, src, (n) * sizeof(*src));
 + COPY_ARRAY(dst, src, n);
 
 @@
@@ -22,7 +22,7 @@ T *dst;
 T *src;
 expression n;
 @@
-- memcpy(dst, src, n * sizeof(T));
+- memcpy(dst, src, (n) * sizeof(T));
 + COPY_ARRAY(dst, src, n);
 
 @@
@@ -47,7 +47,7 @@ type T;
 T *ptr;
 expression n;
 @@
-- ptr = xmalloc(n * sizeof(*ptr));
+- ptr = xmalloc((n) * sizeof(*ptr));
 + ALLOC_ARRAY(ptr, n);
 
 @@
@@ -55,5 +55,5 @@ type T;
 T *ptr;
 expression n;
 @@
-- ptr = xmalloc(n * sizeof(T));
+- ptr = xmalloc((n) * sizeof(T));
 + ALLOC_ARRAY(ptr, n);
index dbe2abf27730e20fcbd692012e9dd07a2e70404f..85ad732fc0636062634453a58be69454dd745b80 100755 (executable)
@@ -11,7 +11,7 @@ use IPC::Open2;
 
 my $since = '5-years-ago';
 my $min_percent = 10;
-my $labels_rx = qr/Signed-off-by|Reviewed-by|Acked-by|Cc/i;
+my $labels_rx = qr/Signed-off-by|Reviewed-by|Acked-by|Cc|Reported-by/i;
 my %seen;
 
 sub format_contact {
index fbf5c582496eece56b29a91a2f3085716f4b0e9f..f2be7cc92437191219d16ddf1536883da23541c0 100644 (file)
@@ -17,4 +17,7 @@ shebang.perl: FORCE
 test: all
        $(MAKE) -C t
 
+clean:
+       $(RM) diff-highlight
+
 .PHONY: FORCE
index ee99f1a4ee89a9e9f358b6cc89fc84cab554b439..932e78dbfee97750ae38afcf699e95f118f956dd 100755 (executable)
@@ -399,7 +399,7 @@ case "$allow_fast_forward,$#,$common,$no_commit" in
 ?,1,"$1",*)
        # If head can reach all the merge then we are up to date.
        # but first the most common case of merging one remote.
-       finish_up_to_date "Already up-to-date."
+       finish_up_to_date "Already up to date."
        exit 0
        ;;
 t,1,"$head",*)
@@ -459,7 +459,7 @@ t,1,"$head",*)
        done
        if test "$up_to_date" = t
        then
-               finish_up_to_date "Already up-to-date. Yeeah!"
+               finish_up_to_date "Already up to date. Yeeah!"
                exit 0
        fi
        ;;
index 70fdc27b724f28793c6a4bff713957a22689b78b..3099dc851ac26a9280e7e6d1a73638c05d01cc17 100755 (executable)
@@ -41,7 +41,7 @@ fi
 
 case "$common" in
 "$merge")
-       echo "Already up-to-date. Yeeah!"
+       echo "Already up to date. Yeeah!"
        dropheads
        exit 0
        ;;
index 52ad9e41fb7aba37abf71dad2bf5a0015b01dca7..eeee45dd341b25d51b7497c4def555c4133c4267 100755 (executable)
@@ -3,10 +3,56 @@
 # Prime rerere database from existing merge commits
 
 me=rerere-train
-USAGE="$me rev-list-args"
+USAGE=$(cat <<-EOF
+usage: $me [--overwrite] <rev-list-args>
+
+    -h, --help            show the help
+    -o, --overwrite       overwrite any existing rerere cache
+EOF
+)
 
 SUBDIRECTORY_OK=Yes
-OPTIONS_SPEC=
+
+overwrite=0
+
+while test $# -gt 0
+do
+       opt="$1"
+       case "$opt" in
+       -h|--help)
+               echo "$USAGE"
+               exit 0
+               ;;
+       -o|--overwrite)
+               overwrite=1
+               shift
+               break
+               ;;
+       --)
+               shift
+               break
+               ;;
+       *)
+               break
+               ;;
+       esac
+done
+
+# Overwrite or help options are not valid except as first arg
+for opt in "$@"
+do
+       case "$opt" in
+       -h|--help)
+               echo "$USAGE"
+               exit 0
+               ;;
+       -o|--overwrite)
+               echo "$USAGE"
+               exit 0
+               ;;
+       esac
+done
+
 . "$(git --exec-path)/git-sh-setup"
 require_work_tree
 cd_to_toplevel
@@ -34,6 +80,10 @@ do
                # Cleanly merges
                continue
        fi
+       if test $overwrite = 1
+       then
+               git rerere forget .
+       fi
        if test -s "$GIT_DIR/MERGE_RR"
        then
                git show -s --pretty=format:"Learning from %h %s" "$commit"
index 3c87ebaf57baede9810c3b9b489eed9751189706..d05c613c9718697f64bc2334dc9ae85bf473f13e 100755 (executable)
@@ -253,7 +253,7 @@ test_expect_success 'merge the added subproj again, should do nothing' '
                # this shouldn not actually do anything, since FETCH_HEAD
                # is already a parent
                result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
-               check_equal "${result}" "Already up-to-date."
+               check_equal "${result}" "Already up to date."
        )
 '
 
index 387c1c5455be954d2be3d35f8be794bb5bd7f3e8..20d7ab67bdf889feb255e01a9b18cf4efa6107a3 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -423,8 +423,10 @@ static int filter_buffer_or_fd(int in, int out, void *data)
        child_process.in = -1;
        child_process.out = out;
 
-       if (start_command(&child_process))
+       if (start_command(&child_process)) {
+               strbuf_release(&cmd);
                return error("cannot fork to run external filter '%s'", params->cmd);
+       }
 
        sigchain_push(SIGPIPE, SIG_IGN);
 
@@ -564,8 +566,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
 
        if (!subprocess_map_initialized) {
                subprocess_map_initialized = 1;
-               hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
-                            NULL, 0);
+               hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
                entry = NULL;
        } else {
                entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
@@ -1041,7 +1042,6 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)
                ca->crlf_action = git_path_check_crlf(ccheck + 4);
                if (ca->crlf_action == CRLF_UNDEFINED)
                        ca->crlf_action = git_path_check_crlf(ccheck + 0);
-               ca->attr_action = ca->crlf_action;
                ca->ident = git_path_check_ident(ccheck + 1);
                ca->drv = git_path_check_convert(ccheck + 2);
                if (ca->crlf_action != CRLF_BINARY) {
@@ -1055,12 +1055,14 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)
                        else if (eol_attr == EOL_CRLF)
                                ca->crlf_action = CRLF_TEXT_CRLF;
                }
-               ca->attr_action = ca->crlf_action;
        } else {
                ca->drv = NULL;
                ca->crlf_action = CRLF_UNDEFINED;
                ca->ident = 0;
        }
+
+       /* Save attr and make a decision for action */
+       ca->attr_action = ca->crlf_action;
        if (ca->crlf_action == CRLF_TEXT)
                ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
        if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
@@ -1543,8 +1545,9 @@ static int ident_filter_fn(struct stream_filter *filter,
                switch (ident->state) {
                default:
                        strbuf_add(&ident->left, head, ident->state);
+                       /* fallthrough */
                case IDENT_SKIPPING:
-                       /* fallthru */
+                       /* fallthrough */
                case IDENT_DRAINING:
                        ident_drain(ident, &output, osize_p);
                }
index 0d5c6250940633e75e2c9fb4b59b242a52188411..4dfbc8c9f917a600c0995b7e33927df3fa19453d 100644 (file)
@@ -5,8 +5,6 @@
 #include "unix-socket.h"
 #include "parse-options.h"
 
-static struct tempfile socket_file;
-
 struct credential_cache_entry {
        struct credential item;
        timestamp_t expiration;
@@ -260,6 +258,7 @@ static void init_socket_directory(const char *path)
 
 int cmd_main(int argc, const char **argv)
 {
+       struct tempfile *socket_file;
        const char *socket_path;
        int ignore_sighup = 0;
        static const char *usage[] = {
@@ -285,7 +284,7 @@ int cmd_main(int argc, const char **argv)
                die("socket directory must be an absolute path");
 
        init_socket_directory(socket_path);
-       register_tempfile(&socket_file, socket_path);
+       socket_file = register_tempfile(socket_path);
 
        if (ignore_sighup)
                signal(SIGHUP, SIG_IGN);
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);
index 3797ce6041981d1facda44d3a95d4448dbb8a091..e49643353bf56807b3d4ac4011784b5d8dd6f7a4 100644 (file)
@@ -319,7 +319,9 @@ create_delta(const struct delta_index *index,
             const void *trg_buf, unsigned long trg_size,
             unsigned long *delta_size, unsigned long max_size)
 {
-       unsigned int i, outpos, outsize, moff, msize, val;
+       unsigned int i, val;
+       off_t outpos, moff;
+       size_t l, outsize, msize;
        int inscnt;
        const unsigned char *ref_data, *ref_top, *data, *top;
        unsigned char *out;
@@ -336,20 +338,20 @@ create_delta(const struct delta_index *index,
                return NULL;
 
        /* store reference buffer size */
-       i = index->src_size;
-       while (i >= 0x80) {
-               out[outpos++] = i | 0x80;
-               i >>= 7;
+       l = index->src_size;
+       while (l >= 0x80) {
+               out[outpos++] = l | 0x80;
+               l >>= 7;
        }
-       out[outpos++] = i;
+       out[outpos++] = l;
 
        /* store target buffer size */
-       i = trg_size;
-       while (i >= 0x80) {
-               out[outpos++] = i | 0x80;
-               i >>= 7;
+       l = trg_size;
+       while (l >= 0x80) {
+               out[outpos++] = l | 0x80;
+               l >>= 7;
        }
-       out[outpos++] = i;
+       out[outpos++] = l;
 
        ref_data = index->src_buf;
        ref_top = ref_data + index->src_size;
@@ -452,6 +454,9 @@ create_delta(const struct delta_index *index,
                        moff += msize;
                        msize = left;
 
+                       if (moff > 0xffffffff)
+                               msize = 0;
+
                        if (msize < 4096) {
                                int j;
                                val = 0;
index 2a52b079546cb01d8dfe708f7e7c7e38ec582435..4e0980caa80fb980938f68c8d350ec00cc24398b 100644 (file)
@@ -549,7 +549,6 @@ int index_differs_from(const char *def, int diff_flags,
        rev.diffopt.flags |= diff_flags;
        rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
        run_diff_index(&rev, 1);
-       if (rev.pending.alloc)
-               free(rev.pending.objects);
+       object_array_clear(&rev.pending);
        return (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);
 }
diff --git a/diff.c b/diff.c
index 9c382580306e340ed6333f96bc4919c4c507a7b9..69f03570adf6cf2c3642aea1fb0dba5011ef966f 100644 (file)
--- a/diff.c
+++ b/diff.c
 #include "userdiff.h"
 #include "submodule-config.h"
 #include "submodule.h"
+#include "hashmap.h"
 #include "ll-merge.h"
 #include "string-list.h"
 #include "argv-array.h"
 #include "graph.h"
+#include "packfile.h"
 
 #ifdef NO_FAST_WORKING_DIRECTORY
 #define FAST_WORKING_DIRECTORY 0
@@ -32,6 +34,7 @@ static int diff_indent_heuristic = 1;
 static int diff_rename_limit_default = 400;
 static int diff_suppress_blank_empty;
 static int diff_use_color_default = -1;
+static int diff_color_moved_default;
 static int diff_context_default = 3;
 static int diff_interhunk_context_default;
 static const char *diff_word_regex_cfg;
@@ -56,6 +59,14 @@ static char diff_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_YELLOW,       /* COMMIT */
        GIT_COLOR_BG_RED,       /* WHITESPACE */
        GIT_COLOR_NORMAL,       /* FUNCINFO */
+       GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
+       GIT_COLOR_BOLD_BLUE,    /* OLD_MOVED ALTERNATIVE */
+       GIT_COLOR_FAINT,        /* OLD_MOVED_DIM */
+       GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
+       GIT_COLOR_BOLD_CYAN,    /* NEW_MOVED */
+       GIT_COLOR_BOLD_YELLOW,  /* NEW_MOVED ALTERNATIVE */
+       GIT_COLOR_FAINT,        /* NEW_MOVED_DIM */
+       GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
 };
 
 static NORETURN void die_want_option(const char *option_name)
@@ -81,6 +92,22 @@ static int parse_diff_color_slot(const char *var)
                return DIFF_WHITESPACE;
        if (!strcasecmp(var, "func"))
                return DIFF_FUNCINFO;
+       if (!strcasecmp(var, "oldmoved"))
+               return DIFF_FILE_OLD_MOVED;
+       if (!strcasecmp(var, "oldmovedalternative"))
+               return DIFF_FILE_OLD_MOVED_ALT;
+       if (!strcasecmp(var, "oldmoveddimmed"))
+               return DIFF_FILE_OLD_MOVED_DIM;
+       if (!strcasecmp(var, "oldmovedalternativedimmed"))
+               return DIFF_FILE_OLD_MOVED_ALT_DIM;
+       if (!strcasecmp(var, "newmoved"))
+               return DIFF_FILE_NEW_MOVED;
+       if (!strcasecmp(var, "newmovedalternative"))
+               return DIFF_FILE_NEW_MOVED_ALT;
+       if (!strcasecmp(var, "newmoveddimmed"))
+               return DIFF_FILE_NEW_MOVED_DIM;
+       if (!strcasecmp(var, "newmovedalternativedimmed"))
+               return DIFF_FILE_NEW_MOVED_ALT_DIM;
        return -1;
 }
 
@@ -229,12 +256,44 @@ int git_diff_heuristic_config(const char *var, const char *value, void *cb)
        return 0;
 }
 
+static int parse_color_moved(const char *arg)
+{
+       switch (git_parse_maybe_bool(arg)) {
+       case 0:
+               return COLOR_MOVED_NO;
+       case 1:
+               return COLOR_MOVED_DEFAULT;
+       default:
+               break;
+       }
+
+       if (!strcmp(arg, "no"))
+               return COLOR_MOVED_NO;
+       else if (!strcmp(arg, "plain"))
+               return COLOR_MOVED_PLAIN;
+       else if (!strcmp(arg, "zebra"))
+               return COLOR_MOVED_ZEBRA;
+       else if (!strcmp(arg, "default"))
+               return COLOR_MOVED_DEFAULT;
+       else if (!strcmp(arg, "dimmed_zebra"))
+               return COLOR_MOVED_ZEBRA_DIM;
+       else
+               return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
+}
+
 int git_diff_ui_config(const char *var, const char *value, void *cb)
 {
        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
                diff_use_color_default = git_config_colorbool(var, value);
                return 0;
        }
+       if (!strcmp(var, "diff.colormoved")) {
+               int cm = parse_color_moved(value);
+               if (cm < 0)
+                       return -1;
+               diff_color_moved_default = cm;
+               return 0;
+       }
        if (!strcmp(var, "diff.context")) {
                diff_context_default = git_config_int(var, value);
                if (diff_context_default < 0)
@@ -343,9 +402,6 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
                return 0;
        }
 
-       if (starts_with(var, "submodule."))
-               return parse_submodule_config_option(var, value);
-
        if (git_diff_heuristic_config(var, value, cb) < 0)
                return -1;
 
@@ -403,11 +459,9 @@ static struct diff_tempfile {
         * If this diff_tempfile instance refers to a temporary file,
         * this tempfile object is used to manage its lifetime.
         */
-       struct tempfile tempfile;
+       struct tempfile *tempfile;
 } diff_temp[2];
 
-typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
-
 struct emit_callback {
        int color_diff;
        unsigned ws_rule;
@@ -415,7 +469,6 @@ struct emit_callback {
        int blank_at_eof_in_postimage;
        int lno_in_preimage;
        int lno_in_postimage;
-       sane_truncate_fn truncate;
        const char **label_path;
        struct diff_words_data *diff_words;
        struct diff_options *opt;
@@ -557,68 +610,735 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
        emit_line_0(o, set, reset, line[0], line+1, len-1);
 }
 
-static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
+enum diff_symbol {
+       DIFF_SYMBOL_BINARY_DIFF_HEADER,
+       DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
+       DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
+       DIFF_SYMBOL_BINARY_DIFF_BODY,
+       DIFF_SYMBOL_BINARY_DIFF_FOOTER,
+       DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
+       DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
+       DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
+       DIFF_SYMBOL_STATS_LINE,
+       DIFF_SYMBOL_WORD_DIFF,
+       DIFF_SYMBOL_STAT_SEP,
+       DIFF_SYMBOL_SUMMARY,
+       DIFF_SYMBOL_SUBMODULE_ADD,
+       DIFF_SYMBOL_SUBMODULE_DEL,
+       DIFF_SYMBOL_SUBMODULE_UNTRACKED,
+       DIFF_SYMBOL_SUBMODULE_MODIFIED,
+       DIFF_SYMBOL_SUBMODULE_HEADER,
+       DIFF_SYMBOL_SUBMODULE_ERROR,
+       DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
+       DIFF_SYMBOL_REWRITE_DIFF,
+       DIFF_SYMBOL_BINARY_FILES,
+       DIFF_SYMBOL_HEADER,
+       DIFF_SYMBOL_FILEPAIR_PLUS,
+       DIFF_SYMBOL_FILEPAIR_MINUS,
+       DIFF_SYMBOL_WORDS_PORCELAIN,
+       DIFF_SYMBOL_WORDS,
+       DIFF_SYMBOL_CONTEXT,
+       DIFF_SYMBOL_CONTEXT_INCOMPLETE,
+       DIFF_SYMBOL_PLUS,
+       DIFF_SYMBOL_MINUS,
+       DIFF_SYMBOL_NO_LF_EOF,
+       DIFF_SYMBOL_CONTEXT_FRAGINFO,
+       DIFF_SYMBOL_CONTEXT_MARKER,
+       DIFF_SYMBOL_SEPARATOR
+};
+/*
+ * Flags for content lines:
+ * 0..12 are whitespace rules
+ * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
+ * 16 is marking if the line is blank at EOF
+ */
+#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF     (1<<16)
+#define DIFF_SYMBOL_MOVED_LINE                 (1<<17)
+#define DIFF_SYMBOL_MOVED_LINE_ALT             (1<<18)
+#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING   (1<<19)
+#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
+
+/*
+ * This struct is used when we need to buffer the output of the diff output.
+ *
+ * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
+ * into the pre/post image file. This pointer could be a union with the
+ * line pointer. By storing an offset into the file instead of the literal line,
+ * we can decrease the memory footprint for the buffered output. At first we
+ * may want to only have indirection for the content lines, but we could also
+ * enhance the state for emitting prefabricated lines, e.g. the similarity
+ * score line or hunk/file headers would only need to store a number or path
+ * and then the output can be constructed later on depending on state.
+ */
+struct emitted_diff_symbol {
+       const char *line;
+       int len;
+       int flags;
+       enum diff_symbol s;
+};
+#define EMITTED_DIFF_SYMBOL_INIT {NULL}
+
+struct emitted_diff_symbols {
+       struct emitted_diff_symbol *buf;
+       int nr, alloc;
+};
+#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
+
+static void append_emitted_diff_symbol(struct diff_options *o,
+                                      struct emitted_diff_symbol *e)
 {
-       if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
-             ecbdata->blank_at_eof_in_preimage &&
-             ecbdata->blank_at_eof_in_postimage &&
-             ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
-             ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
-               return 0;
-       return ws_blank_line(line, len, ecbdata->ws_rule);
+       struct emitted_diff_symbol *f;
+
+       ALLOC_GROW(o->emitted_symbols->buf,
+                  o->emitted_symbols->nr + 1,
+                  o->emitted_symbols->alloc);
+       f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
+
+       memcpy(f, e, sizeof(struct emitted_diff_symbol));
+       f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
 }
 
-static void emit_line_checked(const char *reset,
-                             struct emit_callback *ecbdata,
-                             const char *line, int len,
-                             enum color_diff color,
-                             unsigned ws_error_highlight,
-                             char sign)
+struct moved_entry {
+       struct hashmap_entry ent;
+       const struct emitted_diff_symbol *es;
+       struct moved_entry *next_line;
+};
+
+static int next_byte(const char **cp, const char **endp,
+                    const struct diff_options *diffopt)
+{
+       int retval;
+
+       if (*cp > *endp)
+               return -1;
+
+       if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
+               while (*cp < *endp && isspace(**cp))
+                       (*cp)++;
+               /*
+                * After skipping a couple of whitespaces, we still have to
+                * account for one space.
+                */
+               return (int)' ';
+       }
+
+       if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
+               while (*cp < *endp && isspace(**cp))
+                       (*cp)++;
+               /* return the first non-ws character via the usual below */
+       }
+
+       retval = (unsigned char)(**cp);
+       (*cp)++;
+       return retval;
+}
+
+static int moved_entry_cmp(const struct diff_options *diffopt,
+                          const struct moved_entry *a,
+                          const struct moved_entry *b,
+                          const void *keydata)
+{
+       const char *ap = a->es->line, *ae = a->es->line + a->es->len;
+       const char *bp = b->es->line, *be = b->es->line + b->es->len;
+
+       if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
+               return a->es->len != b->es->len  || memcmp(ap, bp, a->es->len);
+
+       if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
+               while (ae > ap && isspace(*ae))
+                       ae--;
+               while (be > bp && isspace(*be))
+                       be--;
+       }
+
+       while (1) {
+               int ca, cb;
+               ca = next_byte(&ap, &ae, diffopt);
+               cb = next_byte(&bp, &be, diffopt);
+               if (ca != cb)
+                       return 1;
+               if (ca < 0)
+                       return 0;
+       }
+}
+
+static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
+{
+       if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
+               static struct strbuf sb = STRBUF_INIT;
+               const char *ap = es->line, *ae = es->line + es->len;
+               int c;
+
+               strbuf_reset(&sb);
+               while (ae > ap && isspace(*ae))
+                       ae--;
+               while ((c = next_byte(&ap, &ae, o)) > 0)
+                       strbuf_addch(&sb, c);
+
+               return memhash(sb.buf, sb.len);
+       } else {
+               return memhash(es->line, es->len);
+       }
+}
+
+static struct moved_entry *prepare_entry(struct diff_options *o,
+                                        int line_no)
+{
+       struct moved_entry *ret = xmalloc(sizeof(*ret));
+       struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
+
+       ret->ent.hash = get_string_hash(l, o);
+       ret->es = l;
+       ret->next_line = NULL;
+
+       return ret;
+}
+
+static void add_lines_to_move_detection(struct diff_options *o,
+                                       struct hashmap *add_lines,
+                                       struct hashmap *del_lines)
+{
+       struct moved_entry *prev_line = NULL;
+
+       int n;
+       for (n = 0; n < o->emitted_symbols->nr; n++) {
+               struct hashmap *hm;
+               struct moved_entry *key;
+
+               switch (o->emitted_symbols->buf[n].s) {
+               case DIFF_SYMBOL_PLUS:
+                       hm = add_lines;
+                       break;
+               case DIFF_SYMBOL_MINUS:
+                       hm = del_lines;
+                       break;
+               default:
+                       prev_line = NULL;
+                       continue;
+               }
+
+               key = prepare_entry(o, n);
+               if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
+                       prev_line->next_line = key;
+
+               hashmap_add(hm, key);
+               prev_line = key;
+       }
+}
+
+static int shrink_potential_moved_blocks(struct moved_entry **pmb,
+                                        int pmb_nr)
+{
+       int lp, rp;
+
+       /* Shrink the set of potential block to the remaining running */
+       for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
+               while (lp < pmb_nr && pmb[lp])
+                       lp++;
+               /* lp points at the first NULL now */
+
+               while (rp > -1 && !pmb[rp])
+                       rp--;
+               /* rp points at the last non-NULL */
+
+               if (lp < pmb_nr && rp > -1 && lp < rp) {
+                       pmb[lp] = pmb[rp];
+                       pmb[rp] = NULL;
+                       rp--;
+                       lp++;
+               }
+       }
+
+       /* Remember the number of running sets */
+       return rp + 1;
+}
+
+/*
+ * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
+ *
+ * Otherwise, if the last block has fewer alphanumeric characters than
+ * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
+ * that block.
+ *
+ * The last block consists of the (n - block_length)'th line up to but not
+ * including the nth line.
+ *
+ * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
+ * Think of a way to unify them.
+ */
+static void adjust_last_block(struct diff_options *o, int n, int block_length)
+{
+       int i, alnum_count = 0;
+       if (o->color_moved == COLOR_MOVED_PLAIN)
+               return;
+       for (i = 1; i < block_length + 1; i++) {
+               const char *c = o->emitted_symbols->buf[n - i].line;
+               for (; *c; c++) {
+                       if (!isalnum(*c))
+                               continue;
+                       alnum_count++;
+                       if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
+                               return;
+               }
+       }
+       for (i = 1; i < block_length + 1; i++)
+               o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
+}
+
+/* Find blocks of moved code, delegate actual coloring decision to helper */
+static void mark_color_as_moved(struct diff_options *o,
+                               struct hashmap *add_lines,
+                               struct hashmap *del_lines)
+{
+       struct moved_entry **pmb = NULL; /* potentially moved blocks */
+       int pmb_nr = 0, pmb_alloc = 0;
+       int n, flipped_block = 1, block_length = 0;
+
+
+       for (n = 0; n < o->emitted_symbols->nr; n++) {
+               struct hashmap *hm = NULL;
+               struct moved_entry *key;
+               struct moved_entry *match = NULL;
+               struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
+               int i;
+
+               switch (l->s) {
+               case DIFF_SYMBOL_PLUS:
+                       hm = del_lines;
+                       key = prepare_entry(o, n);
+                       match = hashmap_get(hm, key, o);
+                       free(key);
+                       break;
+               case DIFF_SYMBOL_MINUS:
+                       hm = add_lines;
+                       key = prepare_entry(o, n);
+                       match = hashmap_get(hm, key, o);
+                       free(key);
+                       break;
+               default:
+                       flipped_block = 1;
+               }
+
+               if (!match) {
+                       adjust_last_block(o, n, block_length);
+                       pmb_nr = 0;
+                       block_length = 0;
+                       continue;
+               }
+
+               l->flags |= DIFF_SYMBOL_MOVED_LINE;
+
+               if (o->color_moved == COLOR_MOVED_PLAIN)
+                       continue;
+
+               /* Check any potential block runs, advance each or nullify */
+               for (i = 0; i < pmb_nr; i++) {
+                       struct moved_entry *p = pmb[i];
+                       struct moved_entry *pnext = (p && p->next_line) ?
+                                       p->next_line : NULL;
+                       if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
+                               pmb[i] = p->next_line;
+                       } else {
+                               pmb[i] = NULL;
+                       }
+               }
+
+               pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
+
+               if (pmb_nr == 0) {
+                       /*
+                        * The current line is the start of a new block.
+                        * Setup the set of potential blocks.
+                        */
+                       for (; match; match = hashmap_get_next(hm, match)) {
+                               ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
+                               pmb[pmb_nr++] = match;
+                       }
+
+                       flipped_block = (flipped_block + 1) % 2;
+
+                       adjust_last_block(o, n, block_length);
+                       block_length = 0;
+               }
+
+               block_length++;
+
+               if (flipped_block)
+                       l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
+       }
+       adjust_last_block(o, n, block_length);
+
+       free(pmb);
+}
+
+#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
+  (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
+static void dim_moved_lines(struct diff_options *o)
+{
+       int n;
+       for (n = 0; n < o->emitted_symbols->nr; n++) {
+               struct emitted_diff_symbol *prev = (n != 0) ?
+                               &o->emitted_symbols->buf[n - 1] : NULL;
+               struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
+               struct emitted_diff_symbol *next =
+                               (n < o->emitted_symbols->nr - 1) ?
+                               &o->emitted_symbols->buf[n + 1] : NULL;
+
+               /* Not a plus or minus line? */
+               if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
+                       continue;
+
+               /* Not a moved line? */
+               if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
+                       continue;
+
+               /*
+                * If prev or next are not a plus or minus line,
+                * pretend they don't exist
+                */
+               if (prev && prev->s != DIFF_SYMBOL_PLUS &&
+                           prev->s != DIFF_SYMBOL_MINUS)
+                       prev = NULL;
+               if (next && next->s != DIFF_SYMBOL_PLUS &&
+                           next->s != DIFF_SYMBOL_MINUS)
+                       next = NULL;
+
+               /* Inside a block? */
+               if ((prev &&
+                   (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
+                   (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
+                   (next &&
+                   (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
+                   (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
+                       l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
+                       continue;
+               }
+
+               /* Check if we are at an interesting bound: */
+               if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
+                   (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
+                      (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
+                       continue;
+               if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
+                   (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
+                      (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
+                       continue;
+
+               /*
+                * The boundary to prev and next are not interesting,
+                * so this line is not interesting as a whole
+                */
+               l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
+       }
+}
+
+static void emit_line_ws_markup(struct diff_options *o,
+                               const char *set, const char *reset,
+                               const char *line, int len, char sign,
+                               unsigned ws_rule, int blank_at_eof)
 {
-       const char *set = diff_get_color(ecbdata->color_diff, color);
        const char *ws = NULL;
 
-       if (ecbdata->opt->ws_error_highlight & ws_error_highlight) {
-               ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
+       if (o->ws_error_highlight & ws_rule) {
+               ws = diff_get_color_opt(o, DIFF_WHITESPACE);
                if (!*ws)
                        ws = NULL;
        }
 
        if (!ws)
-               emit_line_0(ecbdata->opt, set, reset, sign, line, len);
-       else if (sign == '+' && new_blank_line_at_eof(ecbdata, line, len))
+               emit_line_0(o, set, reset, sign, line, len);
+       else if (blank_at_eof)
                /* Blank line at EOF - paint '+' as well */
-               emit_line_0(ecbdata->opt, ws, reset, sign, line, len);
+               emit_line_0(o, ws, reset, sign, line, len);
        else {
                /* Emit just the prefix, then the rest. */
-               emit_line_0(ecbdata->opt, set, reset, sign, "", 0);
-               ws_check_emit(line, len, ecbdata->ws_rule,
-                             ecbdata->opt->file, set, reset, ws);
+               emit_line_0(o, set, reset, sign, "", 0);
+               ws_check_emit(line, len, ws_rule,
+                             o->file, set, reset, ws);
        }
 }
 
+static void emit_diff_symbol_from_struct(struct diff_options *o,
+                                        struct emitted_diff_symbol *eds)
+{
+       static const char *nneof = " No newline at end of file\n";
+       const char *context, *reset, *set, *meta, *fraginfo;
+       struct strbuf sb = STRBUF_INIT;
+
+       enum diff_symbol s = eds->s;
+       const char *line = eds->line;
+       int len = eds->len;
+       unsigned flags = eds->flags;
+
+       switch (s) {
+       case DIFF_SYMBOL_NO_LF_EOF:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               putc('\n', o->file);
+               emit_line_0(o, context, reset, '\\',
+                           nneof, strlen(nneof));
+               break;
+       case DIFF_SYMBOL_SUBMODULE_HEADER:
+       case DIFF_SYMBOL_SUBMODULE_ERROR:
+       case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
+       case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
+       case DIFF_SYMBOL_SUMMARY:
+       case DIFF_SYMBOL_STATS_LINE:
+       case DIFF_SYMBOL_BINARY_DIFF_BODY:
+       case DIFF_SYMBOL_CONTEXT_FRAGINFO:
+               emit_line(o, "", "", line, len);
+               break;
+       case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
+       case DIFF_SYMBOL_CONTEXT_MARKER:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, context, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SEPARATOR:
+               fprintf(o->file, "%s%c",
+                       diff_line_prefix(o),
+                       o->line_termination);
+               break;
+       case DIFF_SYMBOL_CONTEXT:
+               set = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line_ws_markup(o, set, reset, line, len, ' ',
+                                   flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
+               break;
+       case DIFF_SYMBOL_PLUS:
+               switch (flags & (DIFF_SYMBOL_MOVED_LINE |
+                                DIFF_SYMBOL_MOVED_LINE_ALT |
+                                DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
+               case DIFF_SYMBOL_MOVED_LINE |
+                    DIFF_SYMBOL_MOVED_LINE_ALT |
+                    DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+                       set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
+                       break;
+               case DIFF_SYMBOL_MOVED_LINE |
+                    DIFF_SYMBOL_MOVED_LINE_ALT:
+                       set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
+                       break;
+               case DIFF_SYMBOL_MOVED_LINE |
+                    DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+                       set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
+                       break;
+               case DIFF_SYMBOL_MOVED_LINE:
+                       set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
+                       break;
+               default:
+                       set = diff_get_color_opt(o, DIFF_FILE_NEW);
+               }
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line_ws_markup(o, set, reset, line, len, '+',
+                                   flags & DIFF_SYMBOL_CONTENT_WS_MASK,
+                                   flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
+               break;
+       case DIFF_SYMBOL_MINUS:
+               switch (flags & (DIFF_SYMBOL_MOVED_LINE |
+                                DIFF_SYMBOL_MOVED_LINE_ALT |
+                                DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
+               case DIFF_SYMBOL_MOVED_LINE |
+                    DIFF_SYMBOL_MOVED_LINE_ALT |
+                    DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+                       set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
+                       break;
+               case DIFF_SYMBOL_MOVED_LINE |
+                    DIFF_SYMBOL_MOVED_LINE_ALT:
+                       set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
+                       break;
+               case DIFF_SYMBOL_MOVED_LINE |
+                    DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
+                       set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
+                       break;
+               case DIFF_SYMBOL_MOVED_LINE:
+                       set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
+                       break;
+               default:
+                       set = diff_get_color_opt(o, DIFF_FILE_OLD);
+               }
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line_ws_markup(o, set, reset, line, len, '-',
+                                   flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
+               break;
+       case DIFF_SYMBOL_WORDS_PORCELAIN:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, context, reset, line, len);
+               fputs("~\n", o->file);
+               break;
+       case DIFF_SYMBOL_WORDS:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               /*
+                * Skip the prefix character, if any.  With
+                * diff_suppress_blank_empty, there may be
+                * none.
+                */
+               if (line[0] != '\n') {
+                       line++;
+                       len--;
+               }
+               emit_line(o, context, reset, line, len);
+               break;
+       case DIFF_SYMBOL_FILEPAIR_PLUS:
+               meta = diff_get_color_opt(o, DIFF_METAINFO);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
+                       line, reset,
+                       strchr(line, ' ') ? "\t" : "");
+               break;
+       case DIFF_SYMBOL_FILEPAIR_MINUS:
+               meta = diff_get_color_opt(o, DIFF_METAINFO);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
+                       line, reset,
+                       strchr(line, ' ') ? "\t" : "");
+               break;
+       case DIFF_SYMBOL_BINARY_FILES:
+       case DIFF_SYMBOL_HEADER:
+               fprintf(o->file, "%s", line);
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_HEADER:
+               fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
+               fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
+               fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
+               fputs(diff_line_prefix(o), o->file);
+               fputc('\n', o->file);
+               break;
+       case DIFF_SYMBOL_REWRITE_DIFF:
+               fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, fraginfo, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_ADD:
+               set = diff_get_color_opt(o, DIFF_FILE_NEW);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, set, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_DEL:
+               set = diff_get_color_opt(o, DIFF_FILE_OLD);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, set, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
+               fprintf(o->file, "%sSubmodule %s contains untracked content\n",
+                       diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_MODIFIED:
+               fprintf(o->file, "%sSubmodule %s contains modified content\n",
+                       diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
+               emit_line(o, "", "", " 0 files changed\n",
+                         strlen(" 0 files changed\n"));
+               break;
+       case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
+               emit_line(o, "", "", " ...\n", strlen(" ...\n"));
+               break;
+       case DIFF_SYMBOL_WORD_DIFF:
+               fprintf(o->file, "%.*s", len, line);
+               break;
+       case DIFF_SYMBOL_STAT_SEP:
+               fputs(o->stat_sep, o->file);
+               break;
+       default:
+               die("BUG: unknown diff symbol");
+       }
+       strbuf_release(&sb);
+}
+
+static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
+                            const char *line, int len, unsigned flags)
+{
+       struct emitted_diff_symbol e = {line, len, flags, s};
+
+       if (o->emitted_symbols)
+               append_emitted_diff_symbol(o, &e);
+       else
+               emit_diff_symbol_from_struct(o, &e);
+}
+
+void diff_emit_submodule_del(struct diff_options *o, const char *line)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
+}
+
+void diff_emit_submodule_add(struct diff_options *o, const char *line)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
+}
+
+void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
+                        path, strlen(path), 0);
+}
+
+void diff_emit_submodule_modified(struct diff_options *o, const char *path)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
+                        path, strlen(path), 0);
+}
+
+void diff_emit_submodule_header(struct diff_options *o, const char *header)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
+                        header, strlen(header), 0);
+}
+
+void diff_emit_submodule_error(struct diff_options *o, const char *err)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
+}
+
+void diff_emit_submodule_pipethrough(struct diff_options *o,
+                                    const char *line, int len)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
+}
+
+static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
+{
+       if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
+             ecbdata->blank_at_eof_in_preimage &&
+             ecbdata->blank_at_eof_in_postimage &&
+             ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
+             ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
+               return 0;
+       return ws_blank_line(line, len, ecbdata->ws_rule);
+}
+
 static void emit_add_line(const char *reset,
                          struct emit_callback *ecbdata,
                          const char *line, int len)
 {
-       emit_line_checked(reset, ecbdata, line, len,
-                         DIFF_FILE_NEW, WSEH_NEW, '+');
+       unsigned flags = WSEH_NEW | ecbdata->ws_rule;
+       if (new_blank_line_at_eof(ecbdata, line, len))
+               flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
+
+       emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
 }
 
 static void emit_del_line(const char *reset,
                          struct emit_callback *ecbdata,
                          const char *line, int len)
 {
-       emit_line_checked(reset, ecbdata, line, len,
-                         DIFF_FILE_OLD, WSEH_OLD, '-');
+       unsigned flags = WSEH_OLD | ecbdata->ws_rule;
+       emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
 }
 
 static void emit_context_line(const char *reset,
                              struct emit_callback *ecbdata,
                              const char *line, int len)
 {
-       emit_line_checked(reset, ecbdata, line, len,
-                         DIFF_CONTEXT, WSEH_CONTEXT, ' ');
+       unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
+       emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
 }
 
 static void emit_hunk_header(struct emit_callback *ecbdata,
@@ -641,7 +1361,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
        if (len < 10 ||
            memcmp(line, atat, 2) ||
            !(ep = memmem(line + 2, len - 2, atat, 2))) {
-               emit_line(ecbdata->opt, context, reset, line, len);
+               emit_diff_symbol(ecbdata->opt,
+                                DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
                return;
        }
        ep += 2; /* skip over @@ */
@@ -675,7 +1396,9 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
        }
 
        strbuf_add(&msgbuf, line + len, org_len - len);
-       emit_line(ecbdata->opt, "", "", msgbuf.buf, msgbuf.len);
+       strbuf_complete_line(&msgbuf);
+       emit_diff_symbol(ecbdata->opt,
+                        DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
        strbuf_release(&msgbuf);
 }
 
@@ -691,23 +1414,23 @@ static void remove_tempfile(void)
 {
        int i;
        for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
-               if (is_tempfile_active(&diff_temp[i].tempfile))
+               if (is_tempfile_active(diff_temp[i].tempfile))
                        delete_tempfile(&diff_temp[i].tempfile);
                diff_temp[i].name = NULL;
        }
 }
 
-static void print_line_count(FILE *file, int count)
+static void add_line_count(struct strbuf *out, int count)
 {
        switch (count) {
        case 0:
-               fprintf(file, "0,0");
+               strbuf_addstr(out, "0,0");
                break;
        case 1:
-               fprintf(file, "1");
+               strbuf_addstr(out, "1");
                break;
        default:
-               fprintf(file, "1,%d", count);
+               strbuf_addf(out, "1,%d", count);
                break;
        }
 }
@@ -716,7 +1439,6 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
                               int prefix, const char *data, int size)
 {
        const char *endp = NULL;
-       static const char *nneof = " No newline at end of file\n";
        const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
 
        while (0 < size) {
@@ -734,13 +1456,8 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
                size -= len;
                data += len;
        }
-       if (!endp) {
-               const char *context = diff_get_color(ecb->color_diff,
-                                                    DIFF_CONTEXT);
-               putc('\n', ecb->opt->file);
-               emit_line_0(ecb->opt, context, reset, '\\',
-                           nneof, strlen(nneof));
-       }
+       if (!endp)
+               emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
 }
 
 static void emit_rewrite_diff(const char *name_a,
@@ -752,16 +1469,12 @@ static void emit_rewrite_diff(const char *name_a,
                              struct diff_options *o)
 {
        int lc_a, lc_b;
-       const char *name_a_tab, *name_b_tab;
-       const char *metainfo = diff_get_color(o->use_color, DIFF_METAINFO);
-       const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
-       const char *reset = diff_get_color(o->use_color, DIFF_RESET);
        static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
        const char *a_prefix, *b_prefix;
        char *data_one, *data_two;
        size_t size_one, size_two;
        struct emit_callback ecbdata;
-       const char *line_prefix = diff_line_prefix(o);
+       struct strbuf out = STRBUF_INIT;
 
        if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
                a_prefix = o->b_prefix;
@@ -773,8 +1486,6 @@ static void emit_rewrite_diff(const char *name_a,
 
        name_a += (*name_a == '/');
        name_b += (*name_b == '/');
-       name_a_tab = strchr(name_a, ' ') ? "\t" : "";
-       name_b_tab = strchr(name_b, ' ') ? "\t" : "";
 
        strbuf_reset(&a_name);
        strbuf_reset(&b_name);
@@ -801,18 +1512,23 @@ static void emit_rewrite_diff(const char *name_a,
 
        lc_a = count_lines(data_one, size_one);
        lc_b = count_lines(data_two, size_two);
-       fprintf(o->file,
-               "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
-               line_prefix, metainfo, a_name.buf, name_a_tab, reset,
-               line_prefix, metainfo, b_name.buf, name_b_tab, reset,
-               line_prefix, fraginfo);
+
+       emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
+                        a_name.buf, a_name.len, 0);
+       emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
+                        b_name.buf, b_name.len, 0);
+
+       strbuf_addstr(&out, "@@ -");
        if (!o->irreversible_delete)
-               print_line_count(o->file, lc_a);
+               add_line_count(&out, lc_a);
        else
-               fprintf(o->file, "?,?");
-       fprintf(o->file, " +");
-       print_line_count(o->file, lc_b);
-       fprintf(o->file, " @@%s\n", reset);
+               strbuf_addstr(&out, "?,?");
+       strbuf_addstr(&out, " +");
+       add_line_count(&out, lc_b);
+       strbuf_addstr(&out, " @@\n");
+       emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
+       strbuf_release(&out);
+
        if (lc_a && !o->irreversible_delete)
                emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
        if (lc_b)
@@ -825,7 +1541,7 @@ static void emit_rewrite_diff(const char *name_a,
 
 struct diff_words_buffer {
        mmfile_t text;
-       long alloc;
+       unsigned long alloc;
        struct diff_words_orig {
                const char *begin, *end;
        } *orig;
@@ -872,37 +1588,49 @@ struct diff_words_data {
        struct diff_words_style *style;
 };
 
-static int fn_out_diff_words_write_helper(FILE *fp,
+static int fn_out_diff_words_write_helper(struct diff_options *o,
                                          struct diff_words_style_elem *st_el,
                                          const char *newline,
-                                         size_t count, const char *buf,
-                                         const char *line_prefix)
+                                         size_t count, const char *buf)
 {
        int print = 0;
+       struct strbuf sb = STRBUF_INIT;
 
        while (count) {
                char *p = memchr(buf, '\n', count);
                if (print)
-                       fputs(line_prefix, fp);
+                       strbuf_addstr(&sb, diff_line_prefix(o));
+
                if (p != buf) {
-                       if (st_el->color && fputs(st_el->color, fp) < 0)
-                               return -1;
-                       if (fputs(st_el->prefix, fp) < 0 ||
-                           fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
-                           fputs(st_el->suffix, fp) < 0)
-                               return -1;
-                       if (st_el->color && *st_el->color
-                           && fputs(GIT_COLOR_RESET, fp) < 0)
-                               return -1;
+                       const char *reset = st_el->color && *st_el->color ?
+                                           GIT_COLOR_RESET : NULL;
+                       if (st_el->color && *st_el->color)
+                               strbuf_addstr(&sb, st_el->color);
+                       strbuf_addstr(&sb, st_el->prefix);
+                       strbuf_add(&sb, buf, p ? p - buf : count);
+                       strbuf_addstr(&sb, st_el->suffix);
+                       if (reset)
+                               strbuf_addstr(&sb, reset);
                }
                if (!p)
-                       return 0;
-               if (fputs(newline, fp) < 0)
-                       return -1;
+                       goto out;
+
+               strbuf_addstr(&sb, newline);
                count -= p + 1 - buf;
                buf = p + 1;
                print = 1;
+               if (count) {
+                       emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
+                                        sb.buf, sb.len, 0);
+                       strbuf_reset(&sb);
+               }
        }
+
+out:
+       if (sb.len)
+               emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
+                                sb.buf, sb.len, 0);
+       strbuf_release(&sb);
        return 0;
 }
 
@@ -984,24 +1712,20 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
                fputs(line_prefix, diff_words->opt->file);
        }
        if (diff_words->current_plus != plus_begin) {
-               fn_out_diff_words_write_helper(diff_words->opt->file,
+               fn_out_diff_words_write_helper(diff_words->opt,
                                &style->ctx, style->newline,
                                plus_begin - diff_words->current_plus,
-                               diff_words->current_plus, line_prefix);
-               if (*(plus_begin - 1) == '\n')
-                       fputs(line_prefix, diff_words->opt->file);
+                               diff_words->current_plus);
        }
        if (minus_begin != minus_end) {
-               fn_out_diff_words_write_helper(diff_words->opt->file,
+               fn_out_diff_words_write_helper(diff_words->opt,
                                &style->old, style->newline,
-                               minus_end - minus_begin, minus_begin,
-                               line_prefix);
+                               minus_end - minus_begin, minus_begin);
        }
        if (plus_begin != plus_end) {
-               fn_out_diff_words_write_helper(diff_words->opt->file,
+               fn_out_diff_words_write_helper(diff_words->opt,
                                &style->new, style->newline,
-                               plus_end - plus_begin, plus_begin,
-                               line_prefix);
+                               plus_end - plus_begin, plus_begin);
        }
 
        diff_words->current_plus = plus_end;
@@ -1095,11 +1819,12 @@ static void diff_words_show(struct diff_words_data *diff_words)
 
        /* special case: only removal */
        if (!diff_words->plus.text.size) {
-               fputs(line_prefix, diff_words->opt->file);
-               fn_out_diff_words_write_helper(diff_words->opt->file,
+               emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
+                                line_prefix, strlen(line_prefix), 0);
+               fn_out_diff_words_write_helper(diff_words->opt,
                        &style->old, style->newline,
                        diff_words->minus.text.size,
-                       diff_words->minus.text.ptr, line_prefix);
+                       diff_words->minus.text.ptr);
                diff_words->minus.text.size = 0;
                return;
        }
@@ -1122,12 +1847,12 @@ static void diff_words_show(struct diff_words_data *diff_words)
        if (diff_words->current_plus != diff_words->plus.text.ptr +
                        diff_words->plus.text.size) {
                if (color_words_output_graph_prefix(diff_words))
-                       fputs(line_prefix, diff_words->opt->file);
-               fn_out_diff_words_write_helper(diff_words->opt->file,
+                       emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
+                                        line_prefix, strlen(line_prefix), 0);
+               fn_out_diff_words_write_helper(diff_words->opt,
                        &style->ctx, style->newline,
                        diff_words->plus.text.ptr + diff_words->plus.text.size
-                       - diff_words->current_plus, diff_words->current_plus,
-                       line_prefix);
+                       - diff_words->current_plus, diff_words->current_plus);
        }
        diff_words->minus.text.size = diff_words->plus.text.size = 0;
 }
@@ -1135,9 +1860,29 @@ static void diff_words_show(struct diff_words_data *diff_words)
 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
 static void diff_words_flush(struct emit_callback *ecbdata)
 {
+       struct diff_options *wo = ecbdata->diff_words->opt;
+
        if (ecbdata->diff_words->minus.text.size ||
            ecbdata->diff_words->plus.text.size)
                diff_words_show(ecbdata->diff_words);
+
+       if (wo->emitted_symbols) {
+               struct diff_options *o = ecbdata->opt;
+               struct emitted_diff_symbols *wol = wo->emitted_symbols;
+               int i;
+
+               /*
+                * NEEDSWORK:
+                * Instead of appending each, concat all words to a line?
+                */
+               for (i = 0; i < wol->nr; i++)
+                       append_emitted_diff_symbol(o, &wol->buf[i]);
+
+               for (i = 0; i < wol->nr; i++)
+                       free((void *)wol->buf[i].line);
+
+               wol->nr = 0;
+       }
 }
 
 static void diff_filespec_load_driver(struct diff_filespec *one)
@@ -1173,6 +1918,11 @@ static void init_diff_words_data(struct emit_callback *ecbdata,
                xcalloc(1, sizeof(struct diff_words_data));
        ecbdata->diff_words->type = o->word_diff;
        ecbdata->diff_words->opt = o;
+
+       if (orig_opts->emitted_symbols)
+               o->emitted_symbols =
+                       xcalloc(1, sizeof(struct emitted_diff_symbols));
+
        if (!o->word_regex)
                o->word_regex = userdiff_word_regex(one);
        if (!o->word_regex)
@@ -1207,6 +1957,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
 {
        if (ecbdata->diff_words) {
                diff_words_flush(ecbdata);
+               free (ecbdata->diff_words->opt->emitted_symbols);
                free (ecbdata->diff_words->opt);
                free (ecbdata->diff_words->minus.text.ptr);
                free (ecbdata->diff_words->minus.orig);
@@ -1243,8 +1994,6 @@ static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, u
        unsigned long allot;
        size_t l = len;
 
-       if (ecb->truncate)
-               return ecb->truncate(line, len);
        cp = line;
        allot = l;
        while (0 < l) {
@@ -1273,30 +2022,25 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)
 static void fn_out_consume(void *priv, char *line, unsigned long len)
 {
        struct emit_callback *ecbdata = priv;
-       const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
-       const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
        struct diff_options *o = ecbdata->opt;
-       const char *line_prefix = diff_line_prefix(o);
 
        o->found_changes = 1;
 
        if (ecbdata->header) {
-               fprintf(o->file, "%s", ecbdata->header->buf);
+               emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                ecbdata->header->buf, ecbdata->header->len, 0);
                strbuf_reset(ecbdata->header);
                ecbdata->header = NULL;
        }
 
        if (ecbdata->label_path[0]) {
-               const char *name_a_tab, *name_b_tab;
-
-               name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
-               name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
-
-               fprintf(o->file, "%s%s--- %s%s%s\n",
-                       line_prefix, meta, ecbdata->label_path[0], reset, name_a_tab);
-               fprintf(o->file, "%s%s+++ %s%s%s\n",
-                       line_prefix, meta, ecbdata->label_path[1], reset, name_b_tab);
+               emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
+                                ecbdata->label_path[0],
+                                strlen(ecbdata->label_path[0]), 0);
+               emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
+                                ecbdata->label_path[1],
+                                strlen(ecbdata->label_path[1]), 0);
                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
        }
 
@@ -1312,12 +2056,13 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
                len = sane_truncate_line(ecbdata, line, len);
                find_lno(line, ecbdata);
                emit_hunk_header(ecbdata, line, len);
-               if (line[len-1] != '\n')
-                       putc('\n', o->file);
                return;
        }
 
        if (ecbdata->diff_words) {
+               enum diff_symbol s =
+                       ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
+                       DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
                if (line[0] == '-') {
                        diff_words_append(line, len,
                                          &ecbdata->diff_words->minus);
@@ -1337,21 +2082,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
                        return;
                }
                diff_words_flush(ecbdata);
-               if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
-                       emit_line(o, context, reset, line, len);
-                       fputs("~\n", o->file);
-               } else {
-                       /*
-                        * Skip the prefix character, if any.  With
-                        * diff_suppress_blank_empty, there may be
-                        * none.
-                        */
-                       if (line[0] != '\n') {
-                             line++;
-                             len--;
-                       }
-                       emit_line(o, context, reset, line, len);
-               }
+               emit_diff_symbol(o, s, line, len, 0);
                return;
        }
 
@@ -1372,8 +2103,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
        default:
                /* incomplete line at the end */
                ecbdata->lno_in_preimage++;
-               emit_line(o, diff_get_color(ecbdata->color_diff, DIFF_CONTEXT),
-                         reset, line, len);
+               emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
+                                line, len, 0);
                break;
        }
 }
@@ -1518,20 +2249,14 @@ static int scale_linear(int it, int width, int max_change)
        return 1 + (it * (width - 1) / max_change);
 }
 
-static void show_name(FILE *file,
-                     const char *prefix, const char *name, int len)
-{
-       fprintf(file, " %s%-*s |", prefix, len, name);
-}
-
-static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
+static void show_graph(struct strbuf *out, char ch, int cnt,
+                      const char *set, const char *reset)
 {
        if (cnt <= 0)
                return;
-       fprintf(file, "%s", set);
-       while (cnt--)
-               putc(ch, file);
-       fprintf(file, "%s", reset);
+       strbuf_addstr(out, set);
+       strbuf_addchars(out, ch, cnt);
+       strbuf_addstr(out, reset);
 }
 
 static void fill_print_name(struct diffstat_file *file)
@@ -1555,14 +2280,16 @@ static void fill_print_name(struct diffstat_file *file)
        file->print_name = pname;
 }
 
-int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
+static void print_stat_summary_inserts_deletes(struct diff_options *options,
+               int files, int insertions, int deletions)
 {
        struct strbuf sb = STRBUF_INIT;
-       int ret;
 
        if (!files) {
                assert(insertions == 0 && deletions == 0);
-               return fprintf(fp, "%s\n", " 0 files changed");
+               emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
+                                NULL, 0, 0);
+               return;
        }
 
        strbuf_addf(&sb,
@@ -1589,9 +2316,19 @@ int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
                            deletions);
        }
        strbuf_addch(&sb, '\n');
-       ret = fputs(sb.buf, fp);
+       emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
+                        sb.buf, sb.len, 0);
        strbuf_release(&sb);
-       return ret;
+}
+
+void print_stat_summary(FILE *fp, int files,
+                       int insertions, int deletions)
+{
+       struct diff_options o;
+       memset(&o, 0, sizeof(o));
+       o.file = fp;
+
+       print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
 }
 
 static void show_stats(struct diffstat_t *data, struct diff_options *options)
@@ -1601,13 +2338,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
        int total_files = data->nr, count;
        int width, name_width, graph_width, number_width = 0, bin_width = 0;
        const char *reset, *add_c, *del_c;
-       const char *line_prefix = "";
        int extra_shown = 0;
+       const char *line_prefix = diff_line_prefix(options);
+       struct strbuf out = STRBUF_INIT;
 
        if (data->nr == 0)
                return;
 
-       line_prefix = diff_line_prefix(options);
        count = options->stat_count ? options->stat_count : data->nr;
 
        reset = diff_get_color_opt(options, DIFF_RESET);
@@ -1761,26 +2498,32 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
                }
 
                if (file->is_binary) {
-                       fprintf(options->file, "%s", line_prefix);
-                       show_name(options->file, prefix, name, len);
-                       fprintf(options->file, " %*s", number_width, "Bin");
+                       strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+                       strbuf_addf(&out, " %*s", number_width, "Bin");
                        if (!added && !deleted) {
-                               putc('\n', options->file);
+                               strbuf_addch(&out, '\n');
+                               emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                                out.buf, out.len, 0);
+                               strbuf_reset(&out);
                                continue;
                        }
-                       fprintf(options->file, " %s%"PRIuMAX"%s",
+                       strbuf_addf(&out, " %s%"PRIuMAX"%s",
                                del_c, deleted, reset);
-                       fprintf(options->file, " -> ");
-                       fprintf(options->file, "%s%"PRIuMAX"%s",
+                       strbuf_addstr(&out, " -> ");
+                       strbuf_addf(&out, "%s%"PRIuMAX"%s",
                                add_c, added, reset);
-                       fprintf(options->file, " bytes");
-                       fprintf(options->file, "\n");
+                       strbuf_addstr(&out, " bytes\n");
+                       emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                        out.buf, out.len, 0);
+                       strbuf_reset(&out);
                        continue;
                }
                else if (file->is_unmerged) {
-                       fprintf(options->file, "%s", line_prefix);
-                       show_name(options->file, prefix, name, len);
-                       fprintf(options->file, " Unmerged\n");
+                       strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+                       strbuf_addstr(&out, " Unmerged\n");
+                       emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                        out.buf, out.len, 0);
+                       strbuf_reset(&out);
                        continue;
                }
 
@@ -1803,14 +2546,16 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
                                add = total - del;
                        }
                }
-               fprintf(options->file, "%s", line_prefix);
-               show_name(options->file, prefix, name, len);
-               fprintf(options->file, " %*"PRIuMAX"%s",
+               strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+               strbuf_addf(&out, " %*"PRIuMAX"%s",
                        number_width, added + deleted,
                        added + deleted ? " " : "");
-               show_graph(options->file, '+', add, add_c, reset);
-               show_graph(options->file, '-', del, del_c, reset);
-               fprintf(options->file, "\n");
+               show_graph(&out, '+', add, add_c, reset);
+               show_graph(&out, '-', del, del_c, reset);
+               strbuf_addch(&out, '\n');
+               emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                out.buf, out.len, 0);
+               strbuf_reset(&out);
        }
 
        for (i = 0; i < data->nr; i++) {
@@ -1831,11 +2576,14 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
                if (i < count)
                        continue;
                if (!extra_shown)
-                       fprintf(options->file, "%s ...\n", line_prefix);
+                       emit_diff_symbol(options,
+                                        DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
+                                        NULL, 0, 0);
                extra_shown = 1;
        }
-       fprintf(options->file, "%s", line_prefix);
-       print_stat_summary(options->file, total_files, adds, dels);
+
+       print_stat_summary_inserts_deletes(options, total_files, adds, dels);
+       strbuf_release(&out);
 }
 
 static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
@@ -1847,7 +2595,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
 
        for (i = 0; i < data->nr; i++) {
                int added = data->files[i]->added;
-               int deleted= data->files[i]->deleted;
+               int deleted = data->files[i]->deleted;
 
                if (data->files[i]->is_unmerged ||
                    (!data->files[i]->is_interesting && (added + deleted == 0))) {
@@ -1857,8 +2605,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
                        dels += deleted;
                }
        }
-       fprintf(options->file, "%s", diff_line_prefix(options));
-       print_stat_summary(options->file, total_files, adds, dels);
+       print_stat_summary_inserts_deletes(options, total_files, adds, dels);
 }
 
 static void show_numstat(struct diffstat_t *data, struct diff_options *options)
@@ -2222,8 +2969,8 @@ static unsigned char *deflate_it(char *data,
        return deflated;
 }
 
-static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
-                                 const char *prefix)
+static void emit_binary_diff_body(struct diff_options *o,
+                                 mmfile_t *one, mmfile_t *two)
 {
        void *cp;
        void *delta;
@@ -2252,13 +2999,18 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
        }
 
        if (delta && delta_size < deflate_size) {
-               fprintf(file, "%sdelta %lu\n", prefix, orig_size);
+               char *s = xstrfmt("%lu", orig_size);
+               emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
+                                s, strlen(s), 0);
+               free(s);
                free(deflated);
                data = delta;
                data_size = delta_size;
-       }
-       else {
-               fprintf(file, "%sliteral %lu\n", prefix, two->size);
+       } else {
+               char *s = xstrfmt("%lu", two->size);
+               emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
+                                s, strlen(s), 0);
+               free(s);
                free(delta);
                data = deflated;
                data_size = deflate_size;
@@ -2267,8 +3019,9 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
        /* emit data encoded in base85 */
        cp = data;
        while (data_size) {
+               int len;
                int bytes = (52 < data_size) ? 52 : data_size;
-               char line[70];
+               char line[71];
                data_size -= bytes;
                if (bytes <= 26)
                        line[0] = bytes + 'A' - 1;
@@ -2276,20 +3029,24 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
                        line[0] = bytes - 26 + 'a' - 1;
                encode_85(line + 1, cp, bytes);
                cp = (char *) cp + bytes;
-               fprintf(file, "%s", prefix);
-               fputs(line, file);
-               fputc('\n', file);
+
+               len = strlen(line);
+               line[len++] = '\n';
+               line[len] = '\0';
+
+               emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
+                                line, len, 0);
        }
-       fprintf(file, "%s\n", prefix);
+       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
        free(data);
 }
 
-static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two,
-                            const char *prefix)
+static void emit_binary_diff(struct diff_options *o,
+                            mmfile_t *one, mmfile_t *two)
 {
-       fprintf(file, "%sGIT binary patch\n", prefix);
-       emit_binary_diff_body(file, one, two, prefix);
-       emit_binary_diff_body(file, two, one, prefix);
+       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
+       emit_binary_diff_body(o, one, two);
+       emit_binary_diff_body(o, two, one);
 }
 
 int diff_filespec_is_binary(struct diff_filespec *one)
@@ -2366,24 +3123,16 @@ static void builtin_diff(const char *name_a,
        if (o->submodule_format == DIFF_SUBMODULE_LOG &&
            (!one->mode || S_ISGITLINK(one->mode)) &&
            (!two->mode || S_ISGITLINK(two->mode))) {
-               const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
-               const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
-               show_submodule_summary(o->file, one->path ? one->path : two->path,
-                               line_prefix,
+               show_submodule_summary(o, one->path ? one->path : two->path,
                                &one->oid, &two->oid,
-                               two->dirty_submodule,
-                               meta, del, add, reset);
+                               two->dirty_submodule);
                return;
        } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
                   (!one->mode || S_ISGITLINK(one->mode)) &&
                   (!two->mode || S_ISGITLINK(two->mode))) {
-               const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
-               const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
-               show_submodule_inline_diff(o->file, one->path ? one->path : two->path,
-                               line_prefix,
+               show_submodule_inline_diff(o, one->path ? one->path : two->path,
                                &one->oid, &two->oid,
-                               two->dirty_submodule,
-                               meta, del, add, reset, o);
+                               two->dirty_submodule);
                return;
        }
 
@@ -2432,7 +3181,8 @@ static void builtin_diff(const char *name_a,
                if (complete_rewrite &&
                    (textconv_one || !diff_filespec_is_binary(one)) &&
                    (textconv_two || !diff_filespec_is_binary(two))) {
-                       fprintf(o->file, "%s", header.buf);
+                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                        header.buf, header.len, 0);
                        strbuf_reset(&header);
                        emit_rewrite_diff(name_a, name_b, one, two,
                                                textconv_one, textconv_two, o);
@@ -2442,23 +3192,31 @@ static void builtin_diff(const char *name_a,
        }
 
        if (o->irreversible_delete && lbl[1][0] == '/') {
-               fprintf(o->file, "%s", header.buf);
+               emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
+                                header.len, 0);
                strbuf_reset(&header);
                goto free_ab_and_return;
        } else if (!DIFF_OPT_TST(o, TEXT) &&
            ( (!textconv_one && diff_filespec_is_binary(one)) ||
              (!textconv_two && diff_filespec_is_binary(two)) )) {
+               struct strbuf sb = STRBUF_INIT;
                if (!one->data && !two->data &&
                    S_ISREG(one->mode) && S_ISREG(two->mode) &&
                    !DIFF_OPT_TST(o, BINARY)) {
                        if (!oidcmp(&one->oid, &two->oid)) {
                                if (must_show_header)
-                                       fprintf(o->file, "%s", header.buf);
+                                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                                        header.buf, header.len,
+                                                        0);
                                goto free_ab_and_return;
                        }
-                       fprintf(o->file, "%s", header.buf);
-                       fprintf(o->file, "%sBinary files %s and %s differ\n",
-                               line_prefix, lbl[0], lbl[1]);
+                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                        header.buf, header.len, 0);
+                       strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
+                                   diff_line_prefix(o), lbl[0], lbl[1]);
+                       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
+                                        sb.buf, sb.len, 0);
+                       strbuf_release(&sb);
                        goto free_ab_and_return;
                }
                if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
@@ -2467,16 +3225,21 @@ static void builtin_diff(const char *name_a,
                if (mf1.size == mf2.size &&
                    !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
                        if (must_show_header)
-                               fprintf(o->file, "%s", header.buf);
+                               emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                                header.buf, header.len, 0);
                        goto free_ab_and_return;
                }
-               fprintf(o->file, "%s", header.buf);
+               emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
                strbuf_reset(&header);
                if (DIFF_OPT_TST(o, BINARY))
-                       emit_binary_diff(o->file, &mf1, &mf2, line_prefix);
-               else
-                       fprintf(o->file, "%sBinary files %s and %s differ\n",
-                               line_prefix, lbl[0], lbl[1]);
+                       emit_binary_diff(o, &mf1, &mf2);
+               else {
+                       strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
+                                   diff_line_prefix(o), lbl[0], lbl[1]);
+                       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
+                                        sb.buf, sb.len, 0);
+                       strbuf_release(&sb);
+               }
                o->found_changes = 1;
        } else {
                /* Crazy xdl interfaces.. */
@@ -2488,7 +3251,8 @@ static void builtin_diff(const char *name_a,
                const struct userdiff_funcname *pe;
 
                if (must_show_header) {
-                       fprintf(o->file, "%s", header.buf);
+                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                        header.buf, header.len, 0);
                        strbuf_reset(&header);
                }
 
@@ -2957,7 +3721,6 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
                           const struct object_id *oid,
                           int mode)
 {
-       int fd;
        struct strbuf buf = STRBUF_INIT;
        struct strbuf template = STRBUF_INIT;
        char *path_dup = xstrdup(path);
@@ -2967,18 +3730,18 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
        strbuf_addstr(&template, "XXXXXX_");
        strbuf_addstr(&template, base);
 
-       fd = mks_tempfile_ts(&temp->tempfile, template.buf, strlen(base) + 1);
-       if (fd < 0)
+       temp->tempfile = mks_tempfile_ts(template.buf, strlen(base) + 1);
+       if (!temp->tempfile)
                die_errno("unable to create temp-file");
        if (convert_to_working_tree(path,
                        (const char *)blob, (size_t)size, &buf)) {
                blob = buf.buf;
                size = buf.len;
        }
-       if (write_in_full(fd, blob, size) != size)
+       if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
+           close_tempfile_gently(temp->tempfile))
                die_errno("unable to write temp-file");
-       close_tempfile(&temp->tempfile);
-       temp->name = get_tempfile_path(&temp->tempfile);
+       temp->name = get_tempfile_path(temp->tempfile);
        oid_to_hex_r(temp->hex, oid);
        xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
        strbuf_release(&buf);
@@ -3246,7 +4009,7 @@ static void diff_fill_oid_info(struct diff_filespec *one)
                        }
                        if (lstat(one->path, &st) < 0)
                                die_errno("stat '%s'", one->path);
-                       if (index_path(one->oid.hash, one->path, &st, 0))
+                       if (index_path(&one->oid, one->path, &st, 0))
                                die("cannot hash %s", one->path);
                }
        }
@@ -3279,8 +4042,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
        const char *other;
        const char *attr_path;
 
-       name  = p->one->path;
-       other = (strcmp(name, p->two->path) ? p->two->path : NULL);
+       name  = one->path;
+       other = (strcmp(name, two->path) ? two->path : NULL);
        attr_path = name;
        if (o->prefix_length)
                strip_prefix(o->prefix_length, &name, &other);
@@ -3403,6 +4166,8 @@ void diff_setup(struct diff_options *options)
                options->a_prefix = "a/";
                options->b_prefix = "b/";
        }
+
+       options->color_moved = diff_color_moved_default;
 }
 
 void diff_setup_done(struct diff_options *options)
@@ -3512,6 +4277,9 @@ void diff_setup_done(struct diff_options *options)
 
        if (DIFF_OPT_TST(options, FOLLOW_RENAMES) && options->pathspec.nr != 1)
                die(_("--follow requires exactly one pathspec"));
+
+       if (!options->use_color || external_diff())
+               options->color_moved = 0;
 }
 
 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
@@ -3936,7 +4704,19 @@ int diff_opt_parse(struct diff_options *options,
        }
        else if (!strcmp(arg, "--no-color"))
                options->use_color = 0;
-       else if (!strcmp(arg, "--color-words")) {
+       else if (!strcmp(arg, "--color-moved")) {
+               if (diff_color_moved_default)
+                       options->color_moved = diff_color_moved_default;
+               if (options->color_moved == COLOR_MOVED_NO)
+                       options->color_moved = COLOR_MOVED_DEFAULT;
+       } else if (!strcmp(arg, "--no-color-moved"))
+               options->color_moved = COLOR_MOVED_NO;
+       else if (skip_prefix(arg, "--color-moved=", &arg)) {
+               int cm = parse_color_moved(arg);
+               if (cm < 0)
+                       die("bad --color-moved argument: %s", arg);
+               options->color_moved = cm;
+       } else if (!strcmp(arg, "--color-words")) {
                options->use_color = 1;
                options->word_diff = DIFF_WORDS_COLOR;
        }
@@ -4466,67 +5246,79 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
        }
 }
 
-static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
+static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
 {
+       struct strbuf sb = STRBUF_INIT;
        if (fs->mode)
-               fprintf(file, " %s mode %06o ", newdelete, fs->mode);
+               strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
        else
-               fprintf(file, " %s ", newdelete);
-       write_name_quoted(fs->path, file, '\n');
-}
+               strbuf_addf(&sb, " %s ", newdelete);
 
+       quote_c_style(fs->path, &sb, NULL, 0);
+       strbuf_addch(&sb, '\n');
+       emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
+                        sb.buf, sb.len, 0);
+       strbuf_release(&sb);
+}
 
-static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name,
-               const char *line_prefix)
+static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
+               int show_name)
 {
        if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
-               fprintf(file, "%s mode change %06o => %06o%c", line_prefix, p->one->mode,
-                       p->two->mode, show_name ? ' ' : '\n');
+               struct strbuf sb = STRBUF_INIT;
+               strbuf_addf(&sb, " mode change %06o => %06o",
+                           p->one->mode, p->two->mode);
                if (show_name) {
-                       write_name_quoted(p->two->path, file, '\n');
+                       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);
        }
 }
 
-static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p,
-                       const char *line_prefix)
+static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
+               struct diff_filepair *p)
 {
+       struct strbuf sb = STRBUF_INIT;
        char *names = pprint_rename(p->one->path, p->two->path);
-
-       fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
+       strbuf_addf(&sb, " %s %s (%d%%)\n",
+                       renamecopy, names, similarity_index(p));
        free(names);
-       show_mode_change(file, p, 0, line_prefix);
+       emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
+                                sb.buf, sb.len, 0);
+       show_mode_change(opt, p, 0);
+       strbuf_release(&sb);
 }
 
 static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
 {
-       FILE *file = opt->file;
-       const char *line_prefix = diff_line_prefix(opt);
-
        switch(p->status) {
        case DIFF_STATUS_DELETED:
-               fputs(line_prefix, file);
-               show_file_mode_name(file, "delete", p->one);
+               show_file_mode_name(opt, "delete", p->one);
                break;
        case DIFF_STATUS_ADDED:
-               fputs(line_prefix, file);
-               show_file_mode_name(file, "create", p->two);
+               show_file_mode_name(opt, "create", p->two);
                break;
        case DIFF_STATUS_COPIED:
-               fputs(line_prefix, file);
-               show_rename_copy(file, "copy", p, line_prefix);
+               show_rename_copy(opt, "copy", p);
                break;
        case DIFF_STATUS_RENAMED:
-               fputs(line_prefix, file);
-               show_rename_copy(file, "rename", p, line_prefix);
+               show_rename_copy(opt, "rename", p);
                break;
        default:
                if (p->score) {
-                       fprintf(file, "%s rewrite ", line_prefix);
-                       write_name_quoted(p->two->path, file, ' ');
-                       fprintf(file, "(%d%%)\n", similarity_index(p));
+                       struct strbuf sb = STRBUF_INIT;
+                       strbuf_addstr(&sb, " rewrite ");
+                       quote_c_style(p->two->path, &sb, NULL, 0);
+                       strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
+                       emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
+                                        sb.buf, sb.len, 0);
+                       strbuf_release(&sb);
                }
-               show_mode_change(file, p, !p->score, line_prefix);
+               show_mode_change(opt, p, !p->score);
                break;
        }
 }
@@ -4731,6 +5523,51 @@ void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
                warning(_(rename_limit_advice), varname, needed);
 }
 
+static void diff_flush_patch_all_file_pairs(struct diff_options *o)
+{
+       int i;
+       static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
+       struct diff_queue_struct *q = &diff_queued_diff;
+
+       if (WSEH_NEW & WS_RULE_MASK)
+               die("BUG: WS rules bit mask overlaps with diff symbol flags");
+
+       if (o->color_moved)
+               o->emitted_symbols = &esm;
+
+       for (i = 0; i < q->nr; i++) {
+               struct diff_filepair *p = q->queue[i];
+               if (check_pair_status(p))
+                       diff_flush_patch(p, o);
+       }
+
+       if (o->emitted_symbols) {
+               if (o->color_moved) {
+                       struct hashmap add_lines, del_lines;
+
+                       hashmap_init(&del_lines,
+                                    (hashmap_cmp_fn)moved_entry_cmp, o, 0);
+                       hashmap_init(&add_lines,
+                                    (hashmap_cmp_fn)moved_entry_cmp, o, 0);
+
+                       add_lines_to_move_detection(o, &add_lines, &del_lines);
+                       mark_color_as_moved(o, &add_lines, &del_lines);
+                       if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
+                               dim_moved_lines(o);
+
+                       hashmap_free(&add_lines, 0);
+                       hashmap_free(&del_lines, 0);
+               }
+
+               for (i = 0; i < esm.nr; i++)
+                       emit_diff_symbol_from_struct(o, &esm.buf[i]);
+
+               for (i = 0; i < esm.nr; i++)
+                       free((void *)esm.buf[i].line);
+       }
+       esm.nr = 0;
+}
+
 void diff_flush(struct diff_options *options)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
@@ -4803,6 +5640,7 @@ void diff_flush(struct diff_options *options)
                        fclose(options->file);
                options->file = xfopen("/dev/null", "w");
                options->close_file = 1;
+               options->color_moved = 0;
                for (i = 0; i < q->nr; i++) {
                        struct diff_filepair *p = q->queue[i];
                        if (check_pair_status(p))
@@ -4814,20 +5652,14 @@ void diff_flush(struct diff_options *options)
 
        if (output_format & DIFF_FORMAT_PATCH) {
                if (separator) {
-                       fprintf(options->file, "%s%c",
-                               diff_line_prefix(options),
-                               options->line_termination);
-                       if (options->stat_sep) {
+                       emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
+                       if (options->stat_sep)
                                /* attach patch instead of inline */
-                               fputs(options->stat_sep, options->file);
-                       }
+                               emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
+                                                NULL, 0, 0);
                }
 
-               for (i = 0; i < q->nr; i++) {
-                       struct diff_filepair *p = q->queue[i];
-                       if (check_pair_status(p))
-                               diff_flush_patch(p, options);
-               }
+               diff_flush_patch_all_file_pairs(options);
        }
 
        if (output_format & DIFF_FORMAT_CALLBACK)
diff --git a/diff.h b/diff.h
index 2d442e296f9821ea4085188602b9ea4a4ba159a6..aca150ba2e0f0b0a984e7f15c292af5f5b057d61 100644 (file)
--- a/diff.h
+++ b/diff.h
@@ -148,9 +148,9 @@ struct diff_options {
        int abbrev;
        int ita_invisible_in_index;
 /* white-space error highlighting */
-#define WSEH_NEW 1
-#define WSEH_CONTEXT 2
-#define WSEH_OLD 4
+#define WSEH_NEW (1<<12)
+#define WSEH_CONTEXT (1<<13)
+#define WSEH_OLD (1<<14)
        unsigned ws_error_highlight;
        const char *prefix;
        int prefix_length;
@@ -186,8 +186,27 @@ struct diff_options {
        void *output_prefix_data;
 
        int diff_path_counter;
+
+       struct emitted_diff_symbols *emitted_symbols;
+       enum {
+               COLOR_MOVED_NO = 0,
+               COLOR_MOVED_PLAIN = 1,
+               COLOR_MOVED_ZEBRA = 2,
+               COLOR_MOVED_ZEBRA_DIM = 3,
+       } color_moved;
+       #define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA
+       #define COLOR_MOVED_MIN_ALNUM_COUNT 20
 };
 
+void diff_emit_submodule_del(struct diff_options *o, const char *line);
+void diff_emit_submodule_add(struct diff_options *o, const char *line);
+void diff_emit_submodule_untracked(struct diff_options *o, const char *path);
+void diff_emit_submodule_modified(struct diff_options *o, const char *path);
+void diff_emit_submodule_header(struct diff_options *o, const char *header);
+void diff_emit_submodule_error(struct diff_options *o, const char *err);
+void diff_emit_submodule_pipethrough(struct diff_options *o,
+                                    const char *line, int len);
+
 enum color_diff {
        DIFF_RESET = 0,
        DIFF_CONTEXT = 1,
@@ -197,7 +216,15 @@ enum color_diff {
        DIFF_FILE_NEW = 5,
        DIFF_COMMIT = 6,
        DIFF_WHITESPACE = 7,
-       DIFF_FUNCINFO = 8
+       DIFF_FUNCINFO = 8,
+       DIFF_FILE_OLD_MOVED = 9,
+       DIFF_FILE_OLD_MOVED_ALT = 10,
+       DIFF_FILE_OLD_MOVED_DIM = 11,
+       DIFF_FILE_OLD_MOVED_ALT_DIM = 12,
+       DIFF_FILE_NEW_MOVED = 13,
+       DIFF_FILE_NEW_MOVED_ALT = 14,
+       DIFF_FILE_NEW_MOVED_DIM = 15,
+       DIFF_FILE_NEW_MOVED_ALT_DIM = 16
 };
 const char *diff_get_color(int diff_use_color, enum color_diff ix);
 #define diff_get_color_opt(o, ix) \
@@ -396,8 +423,8 @@ extern int parse_rename_score(const char **cp_p);
 
 extern long parse_algorithm_value(const char *value);
 
-extern int print_stat_summary(FILE *fp, int files,
-                             int insertions, int deletions);
+extern void print_stat_summary(FILE *fp, int files,
+                              int insertions, int deletions);
 extern void setup_diff_pager(struct diff_options *);
 
 #endif /* DIFF_H */
index 786f3894984b5652482f313aa489200dd7ff9355..0d8c3d2ee480d70718f1e6f0b43a1175a14e4584 100644 (file)
@@ -532,9 +532,9 @@ void diffcore_rename(struct diff_options *options)
        }
 
        if (options->show_rename_progress) {
-               progress = start_progress_delay(
+               progress = start_delayed_progress(
                                _("Performing inexact rename detection"),
-                               rename_dst_nr * rename_src_nr, 50, 1);
+                               rename_dst_nr * rename_src_nr);
        }
 
        mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_create), sizeof(*mx));
diff --git a/dir.c b/dir.c
index 1c55dc3e366f8c9e8bcbc62305d7085533059512..1d17b800cf374d179d7cef3c6bbb180a5a1b4aba 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -49,7 +49,7 @@ struct cached_dir {
 static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        struct index_state *istate, const char *path, int len,
        struct untracked_cache_dir *untracked,
-       int check_only, const struct pathspec *pathspec);
+       int check_only, int stop_at_first_file, const struct pathspec *pathspec);
 static int get_dtype(struct dirent *de, struct index_state *istate,
                     const char *path, int len);
 
@@ -1404,8 +1404,13 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
 
        untracked = lookup_untracked(dir->untracked, untracked,
                                     dirname + baselen, len - baselen);
+
+       /*
+        * If this is an excluded directory, then we only need to check if
+        * the directory contains any files.
+        */
        return read_directory_recursive(dir, istate, dirname, len,
-                                       untracked, 1, pathspec);
+                                       untracked, 1, exclude, pathspec);
 }
 
 /*
@@ -1633,7 +1638,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,
                 * with check_only set.
                 */
                return read_directory_recursive(dir, istate, path->buf, path->len,
-                                               cdir->ucd, 1, pathspec);
+                                               cdir->ucd, 1, 0, pathspec);
        /*
         * We get path_recurse in the first run when
         * directory_exists_in_index() returns index_nonexistent. We
@@ -1793,12 +1798,20 @@ static void close_cached_dir(struct cached_dir *cdir)
  * Also, we ignore the name ".git" (even if it is not a directory).
  * That likely will not change.
  *
+ * If 'stop_at_first_file' is specified, 'path_excluded' is returned
+ * to signal that a file was found. This is the least significant value that
+ * indicates that a file was encountered that does not depend on the order of
+ * whether an untracked or exluded path was encountered first.
+ *
  * Returns the most significant path_treatment value encountered in the scan.
+ * If 'stop_at_first_file' is specified, `path_excluded` is the most
+ * significant path_treatment value that will be returned.
  */
+
 static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        struct index_state *istate, const char *base, int baselen,
        struct untracked_cache_dir *untracked, int check_only,
-       const struct pathspec *pathspec)
+       int stop_at_first_file, const struct pathspec *pathspec)
 {
        struct cached_dir cdir;
        enum path_treatment state, subdir_state, dir_state = path_none;
@@ -1832,12 +1845,34 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
                        subdir_state =
                                read_directory_recursive(dir, istate, path.buf,
                                                         path.len, ud,
-                                                        check_only, pathspec);
+                                                        check_only, stop_at_first_file, pathspec);
                        if (subdir_state > dir_state)
                                dir_state = subdir_state;
                }
 
                if (check_only) {
+                       if (stop_at_first_file) {
+                               /*
+                                * If stopping at first file, then
+                                * signal that a file was found by
+                                * returning `path_excluded`. This is
+                                * to return a consistent value
+                                * regardless of whether an ignored or
+                                * excluded file happened to be
+                                * encountered 1st.
+                                *
+                                * In current usage, the
+                                * `stop_at_first_file` is passed when
+                                * an ancestor directory has matched
+                                * an exclude pattern, so any found
+                                * files will be excluded.
+                                */
+                               if (dir_state >= path_excluded) {
+                                       dir_state = path_excluded;
+                                       break;
+                               }
+                       }
+
                        /* abort early if maximum state has been reached */
                        if (dir_state == path_untracked) {
                                if (cdir.fdir)
@@ -2108,7 +2143,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
                 */
                dir->untracked = NULL;
        if (!len || treat_leading_path(dir, istate, path, len, pathspec))
-               read_directory_recursive(dir, istate, path, len, untracked, 0, pathspec);
+               read_directory_recursive(dir, istate, path, len, untracked, 0, 0, pathspec);
        QSORT(dir->entries, dir->nr, cmp_dir_entry);
        QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);
 
diff --git a/entry.c b/entry.c
index 65458f07a4453fa29731fe2b9c20848d6b192b20..1c7e3c11d5075d4b39eeca76ab2272e781f773c8 100644 (file)
--- a/entry.c
+++ b/entry.c
@@ -3,6 +3,7 @@
 #include "dir.h"
 #include "streaming.h"
 #include "submodule.h"
+#include "progress.h"
 
 static void create_directories(const char *path, int path_len,
                               const struct checkout *state)
@@ -161,16 +162,22 @@ static int remove_available_paths(struct string_list_item *item, void *cb_data)
 int finish_delayed_checkout(struct checkout *state)
 {
        int errs = 0;
+       unsigned delayed_object_count;
+       off_t filtered_bytes = 0;
        struct string_list_item *filter, *path;
+       struct progress *progress;
        struct delayed_checkout *dco = state->delayed_checkout;
 
        if (!state->delayed_checkout)
                return errs;
 
        dco->state = CE_RETRY;
+       delayed_object_count = dco->paths.nr;
+       progress = start_delayed_progress(_("Filtering content"), delayed_object_count);
        while (dco->filters.nr > 0) {
                for_each_string_list_item(filter, &dco->filters) {
                        struct string_list available_paths = STRING_LIST_INIT_NODUP;
+                       display_progress(progress, delayed_object_count - dco->paths.nr);
 
                        if (!async_query_available_blobs(filter->string, &available_paths)) {
                                /* Filter reported an error */
@@ -216,11 +223,17 @@ int finish_delayed_checkout(struct checkout *state)
                                }
                                ce = index_file_exists(state->istate, path->string,
                                                       strlen(path->string), 0);
-                               errs |= (ce ? checkout_entry(ce, state, NULL) : 1);
+                               if (ce) {
+                                       errs |= checkout_entry(ce, state, NULL);
+                                       filtered_bytes += ce->ce_stat_data.sd_size;
+                                       display_throughput(progress, filtered_bytes);
+                               } else
+                                       errs = 1;
                        }
                }
                string_list_remove_empty_items(&dco->filters, 0);
        }
+       stop_progress(&progress);
        string_list_clear(&dco->filters, 0);
 
        /* At this point we should not have any delayed paths anymore. */
@@ -244,7 +257,8 @@ static int write_entry(struct cache_entry *ce,
        char *new;
        struct strbuf buf = STRBUF_INIT;
        unsigned long size;
-       size_t wrote, newsize = 0;
+       ssize_t wrote;
+       size_t newsize = 0;
        struct stat st;
        const struct submodule *sub;
 
@@ -319,7 +333,7 @@ static int write_entry(struct cache_entry *ce,
                        fstat_done = fstat_output(fd, state, &st);
                close(fd);
                free(new);
-               if (wrote != size)
+               if (wrote < 0)
                        return error("unable to write file %s", path);
                break;
        case S_IFGITLINK:
index 3fd4b1084590d0118cad5e7ae32fb37e570e7523..8289c25b44d74a0f054f9fe3eaf44c5fdcf0b3d2 100644 (file)
@@ -97,7 +97,7 @@ int ignore_untracked_cache_config;
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
 char *git_work_tree_cfg;
 
-static const char *namespace;
+static char *namespace;
 
 static const char *super_prefix;
 
@@ -152,8 +152,10 @@ void setup_git_env(void)
        if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
                check_replace_refs = 0;
        replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
+       free(git_replace_ref_base);
        git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
                                                          : "refs/replace/");
+       free(namespace);
        namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
        shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
        if (shallow_file)
@@ -336,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 fb94aeba9cef2893ea7173e5634640e596659474..ce192a2d64c846e9d7ccffe10c9476f9c5abb8b8 100644 (file)
@@ -5,21 +5,14 @@
 #define MAX_ARGS       32
 
 static const char *argv_exec_path;
+
+#ifdef RUNTIME_PREFIX
 static const char *argv0_path;
 
-char *system_path(const char *path)
+static const char *system_prefix(void)
 {
-#ifdef RUNTIME_PREFIX
        static const char *prefix;
-#else
-       static const char *prefix = PREFIX;
-#endif
-       struct strbuf d = STRBUF_INIT;
-
-       if (is_absolute_path(path))
-               return xstrdup(path);
 
-#ifdef RUNTIME_PREFIX
        assert(argv0_path);
        assert(is_absolute_path(argv0_path));
 
@@ -32,10 +25,7 @@ char *system_path(const char *path)
                                "but prefix computation failed.  "
                                "Using static fallback '%s'.\n", prefix);
        }
-#endif
-
-       strbuf_addf(&d, "%s/%s", prefix, path);
-       return strbuf_detach(&d, NULL);
+       return prefix;
 }
 
 void git_extract_argv0_path(const char *argv0)
@@ -51,6 +41,30 @@ void git_extract_argv0_path(const char *argv0)
                argv0_path = xstrndup(argv0, slash - argv0);
 }
 
+#else
+
+static const char *system_prefix(void)
+{
+       return PREFIX;
+}
+
+void git_extract_argv0_path(const char *argv0)
+{
+}
+
+#endif /* RUNTIME_PREFIX */
+
+char *system_path(const char *path)
+{
+       struct strbuf d = STRBUF_INIT;
+
+       if (is_absolute_path(path))
+               return xstrdup(path);
+
+       strbuf_addf(&d, "%s/%s", system_prefix(), path);
+       return strbuf_detach(&d, NULL);
+}
+
 void git_set_argv_exec_path(const char *exec_path)
 {
        argv_exec_path = exec_path;
index a959161b4631e134602aa9b5502105ee5c5bcfb8..d5e4cf0bad411dad859dbe421ab7583cfb0f1561 100644 (file)
@@ -167,6 +167,7 @@ Format of STDIN stream:
 #include "quote.h"
 #include "dir.h"
 #include "run-command.h"
+#include "packfile.h"
 
 #define PACK_ID_BITS 16
 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
@@ -2951,7 +2952,7 @@ static void parse_reset_branch(const char *arg)
 
 static void cat_blob_write(const char *buf, unsigned long size)
 {
-       if (write_in_full(cat_blob_fd, buf, size) != size)
+       if (write_in_full(cat_blob_fd, buf, size) < 0)
                die_errno("Write to frontend failed");
 }
 
@@ -3188,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 fbbc99c88856a773326e4bb310d30fe3021e2333..008b25d3db08726b0fc1569f88fba76694bbcc12 100644 (file)
@@ -17,6 +17,7 @@
 #include "prio-queue.h"
 #include "sha1-array.h"
 #include "oidset.h"
+#include "packfile.h"
 
 static int transfer_unpack_limit = -1;
 static int fetch_unpack_limit = -1;
@@ -610,7 +611,7 @@ static int tip_oids_contain(struct oidset *tip_oids,
         * add to "newlist" between calls, the additions will always be for
         * oids that are already in the set.
         */
-       if (!tip_oids->map.tablesize) {
+       if (!tip_oids->map.map.tablesize) {
                add_refs_to_oidset(tip_oids, unmatched);
                add_refs_to_oidset(tip_oids, newlist);
        }
diff --git a/fsck.c b/fsck.c
index b1579c7e2821a99729af85b1382f7ea5c3824b8f..032699e9ac2622c7d089523a456db0fa83310e14 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -588,6 +588,7 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
                case S_IFREG | 0664:
                        if (!options->strict)
                                break;
+                       /* fallthrough */
                default:
                        has_bad_modes = 1;
                }
@@ -736,7 +737,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
                buffer += 41;
                parent_line_count++;
        }
-       graft = lookup_commit_graft(commit->object.oid.hash);
+       graft = lookup_commit_graft(&commit->object.oid);
        parent_count = commit_list_count(commit->parents);
        if (graft) {
                if (graft->nr_parent == -1 && !parent_count)
index 7d2c0ca75922ea1ca8c67ec798ff8ce1249c925f..cedad4d5818a6e5c6e1732d34cf8fef6dab36718 100644 (file)
@@ -749,8 +749,6 @@ const char *inet_ntop(int af, const void *src, char *dst, size_t size);
 extern int git_atexit(void (*handler)(void));
 #endif
 
-extern void release_pack_memory(size_t);
-
 typedef void (*try_to_free_t)(size_t);
 extern try_to_free_t set_try_to_free_routine(try_to_free_t);
 
@@ -900,9 +898,11 @@ static inline char *xstrdup_or_null(const char *str)
 
 static inline size_t xsize_t(off_t len)
 {
-       if (len > (size_t) len)
+       size_t size = (size_t) len;
+
+       if (len != (off_t) size)
                die("Cannot handle files this big");
-       return (size_t)len;
+       return size;
 }
 
 __attribute__((format (printf, 3, 4)))
@@ -1171,4 +1171,24 @@ static inline int is_missing_file_error(int errno_)
 
 extern int cmd_main(int, const char **);
 
+/*
+ * You can mark a stack variable with UNLEAK(var) to avoid it being
+ * reported as a leak by tools like LSAN or valgrind. The argument
+ * should generally be the variable itself (not its address and not what
+ * it points to). It's safe to use this on pointers which may already
+ * have been freed, or on pointers which may still be in use.
+ *
+ * Use this _only_ for a variable that leaks by going out of scope at
+ * program exit (so only from cmd_* functions or their direct helpers).
+ * Normal functions, especially those which may be called multiple
+ * times, should actually free their memory. This is only meant as
+ * an annotation, and does nothing in non-leak-checking builds.
+ */
+#ifdef SUPPRESS_ANNOTATED_LEAKS
+extern void unleak_memory(const void *ptr, size_t len);
+#define UNLEAK(var) unleak_memory(&(var), sizeof(var))
+#else
+#define UNLEAK(var) do {} while (0)
+#endif
+
 #endif
index 3a74602ef37712e16dc28567130ca8ee9e8fc579..3365a3b866b510db5bf9ca20a0341c34c21b7a69 100755 (executable)
@@ -86,7 +86,7 @@ USAGE="[--setup <command>] [--env-filter <command>]
        [--parent-filter <command>] [--msg-filter <command>]
        [--commit-filter <command>] [--tag-name-filter <command>]
        [--subdirectory-filter <directory>] [--original <namespace>]
-       [-d <directory>] [-f | --force]
+       [-d <directory>] [-f | --force] [--state-branch <branch>]
        [--] [<rev-list options>...]"
 
 OPTIONS_SPEC=
@@ -106,6 +106,7 @@ filter_msg=cat
 filter_commit=
 filter_tag_name=
 filter_subdir=
+state_branch=
 orig_namespace=refs/original/
 force=
 prune_empty=
@@ -181,6 +182,9 @@ do
        --original)
                orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
                ;;
+       --state-branch)
+               state_branch="$OPTARG"
+               ;;
        *)
                usage
                ;;
@@ -219,6 +223,13 @@ trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
 ORIG_GIT_DIR="$GIT_DIR"
 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
+ORIG_GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME"
+ORIG_GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL"
+ORIG_GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE"
+ORIG_GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME"
+ORIG_GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL"
+ORIG_GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE"
+
 GIT_WORK_TREE=.
 export GIT_DIR GIT_WORK_TREE
 
@@ -252,6 +263,26 @@ export GIT_INDEX_FILE
 # map old->new commit ids for rewriting parents
 mkdir ../map || die "Could not create map/ directory"
 
+if test -n "$state_branch"
+then
+       state_commit=$(git rev-parse --no-flags --revs-only "$state_branch")
+       if test -n "$state_commit"
+       then
+               echo "Populating map from $state_branch ($state_commit)" 1>&2
+               perl -e'open(MAP, "-|", "git show $ARGV[0]:filter.map") or die;
+                       while (<MAP>) {
+                               m/(.*):(.*)/ or die;
+                               open F, ">../map/$1" or die;
+                               print F "$2" or die;
+                               close(F) or die;
+                       }
+                       close(MAP) or die;' "$state_commit" \
+                               || die "Unable to load state from $state_branch:filter.map"
+       else
+               echo "Branch $state_branch does not exist. Will create" 1>&2
+       fi
+fi
+
 # we need "--" only if there are no path arguments in $@
 nonrevs=$(git rev-parse --no-revs "$@") || exit
 if test -z "$nonrevs"
@@ -530,7 +561,7 @@ if [ "$filter_tag_name" ]; then
                                        }' \
                                    -e '/^-----BEGIN PGP SIGNATURE-----/q' \
                                    -e 'p' ) |
-                               git mktag) ||
+                               git hash-object -t tag -w --stdin) ||
                                die "Could not create new tag object for $ref"
                        if git cat-file tag "$ref" | \
                           sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
@@ -544,12 +575,9 @@ if [ "$filter_tag_name" ]; then
        done
 fi
 
-cd "$orig_dir"
-rm -rf "$tempdir"
-
-trap - 0
-
 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
+unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
+unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
 test -z "$ORIG_GIT_DIR" || {
        GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
 }
@@ -561,6 +589,58 @@ test -z "$ORIG_GIT_INDEX_FILE" || {
        GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
        export GIT_INDEX_FILE
 }
+test -z "$ORIG_GIT_AUTHOR_NAME" || {
+       GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" &&
+       export GIT_AUTHOR_NAME
+}
+test -z "$ORIG_GIT_AUTHOR_EMAIL" || {
+       GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" &&
+       export GIT_AUTHOR_EMAIL
+}
+test -z "$ORIG_GIT_AUTHOR_DATE" || {
+       GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" &&
+       export GIT_AUTHOR_DATE
+}
+test -z "$ORIG_GIT_COMMITTER_NAME" || {
+       GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" &&
+       export GIT_COMMITTER_NAME
+}
+test -z "$ORIG_GIT_COMMITTER_EMAIL" || {
+       GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" &&
+       export GIT_COMMITTER_EMAIL
+}
+test -z "$ORIG_GIT_COMMITTER_DATE" || {
+       GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" &&
+       export GIT_COMMITTER_DATE
+}
+
+if test -n "$state_branch"
+then
+       echo "Saving rewrite state to $state_branch" 1>&2
+       state_blob=$(
+               perl -e'opendir D, "../map" or die;
+                       open H, "|-", "git hash-object -w --stdin" or die;
+                       foreach (sort readdir(D)) {
+                               next if m/^\.\.?$/;
+                               open F, "<../map/$_" or die;
+                               chomp($f = <F>);
+                               print H "$_:$f\n" or die;
+                       }
+                       close(H) or die;' || die "Unable to save state")
+       state_tree=$(/bin/echo -e "100644 blob $state_blob\tfilter.map" | git mktree)
+       if test -n "$state_commit"
+       then
+               state_commit=$(/bin/echo "Sync" | git commit-tree "$state_tree" -p "$state_commit")
+       else
+               state_commit=$(/bin/echo "Sync" | git commit-tree "$state_tree" )
+       fi
+       git update-ref "$state_branch" "$state_commit"
+fi
+
+cd "$orig_dir"
+rm -rf "$tempdir"
+
+trap - 0
 
 if [ "$(is_bare_repository)" = false ]; then
        git read-tree -u -m HEAD || exit
index 0f5837d48e7040e9d2b07513666b608353736645..2514bc22abf4aedfaa8d641976c96fbf222bb8a7 100644 (file)
@@ -165,7 +165,7 @@ to contribute an update.  This may be because you would want to improve
 the translation of existing messages, or because the git-gui software
 itself was updated and there are new messages that need translation.
 
-In any case, make sure you are up-to-date before starting your work:
+In any case, make sure you are up to date before starting your work:
 
        $ git checkout master
        $ git pull
index 6c390d6c229d7ef8cca8ae01d33b42b57e29c3b3..7d19d379512b52168d7c604002e872fc3eef9fd7 100755 (executable)
@@ -74,7 +74,7 @@ do
 
        case "$LF$common$LF" in
        *"$LF$SHA1$LF"*)
-               eval_gettextln "Already up-to-date with \$pretty_name"
+               eval_gettextln "Already up to date with \$pretty_name"
                continue
                ;;
        esac
index 2fa581789c5d9d4dd967e1783c513d64660f8a9c..76859b453afc2d5ba6897d15a680245b455f4f38 100755 (executable)
--- a/git-p4.py
+++ b/git-p4.py
@@ -3614,7 +3614,7 @@ def run(self, args):
 
     def rebase(self):
         if os.system("git update-index --refresh") != 0:
-            die("Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up-to-date or stash away all your changes with git stash.");
+            die("Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up to date or stash away all your changes with git stash.");
         if len(read_pipe("git diff-index HEAD --")) > 0:
             die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
 
index 319933e70a34f9da4ec93d063eb102eff33b6787..6e64d40d6fb8fe1566dd26c3df534afe2278390c 100644 (file)
@@ -53,6 +53,7 @@ else
 
        git format-patch -k --stdout --full-index --cherry-pick --right-only \
                --src-prefix=a/ --dst-prefix=b/ --no-renames --no-cover-letter \
+               $git_format_patch_opt \
                "$revisions" ${restrict_revision+^$restrict_revision} \
                >"$GIT_DIR/rebased-patches"
        ret=$?
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 2cf73b88e8e83ca34b9eb319dbc2b0a220139b0f..6344e8d5e38ca237dc5b5b67844f87f48aaa74ca 100755 (executable)
@@ -55,9 +55,10 @@ LF='
 '
 ok_to_skip_pre_rebase=
 resolvemsg="
-$(gettext 'When you have resolved this problem, run "git rebase --continue".
-If you prefer to skip this patch, run "git rebase --skip" instead.
-To check out the original branch and stop rebasing, run "git rebase --abort".')
+$(gettext 'Resolve all conflicts manually, mark them as resolved with
+"git add/rm <conflicted_files>", then run "git rebase --continue".
+You can instead skip this commit: run "git rebase --skip".
+To abort and get back to the state before "git rebase", run "git rebase --abort".')
 "
 unset onto
 unset restrict_revision
@@ -73,6 +74,7 @@ test "$(git config --bool rebase.stat)" = true && diffstat=t
 autostash="$(git config --bool rebase.autostash || echo false)"
 fork_point=auto
 git_am_opt=
+git_format_patch_opt=
 rebase_root=
 force_rebase=
 allow_rerere_autoupdate=
@@ -348,6 +350,9 @@ do
                shift
                break
                ;;
+       *)
+               usage
+               ;;
        esac
        shift
 done
@@ -444,6 +449,11 @@ else
        state_dir="$apply_dir"
 fi
 
+if test -t 2 && test -z "$GIT_QUIET"
+then
+       git_format_patch_opt="$git_format_patch_opt --progress"
+fi
+
 if test -z "$rebase_root"
 then
        case "$#" in
index eebd33276da9028afa1f2ed3b03e60f037d7e646..13c172bd94fc5d4a9658b27fae9733f73a272376 100755 (executable)
@@ -128,7 +128,7 @@ git show -s --format='The following changes since commit %H:
 
   %s (%ci)
 
-are available in the git repository at:
+are available in the Git repository at:
 ' $merge_base &&
 echo "  $url $pretty_remote" &&
 git show -s --format='
index fa6526986ed40143a12ffcd6563fd52b49d209e7..2208dcc2139be7130749fd3bae462320f17d3b21 100755 (executable)
@@ -155,7 +155,6 @@ sub format_2822_time {
 }
 
 my $have_email_valid = eval { require Email::Valid; 1 };
-my $have_mail_address = eval { require Mail::Address; 1 };
 my $smtp;
 my $auth;
 my $num_sent = 0;
@@ -490,11 +489,7 @@ sub read_config {
 ($repocommitter) = Git::ident_person(@repo, 'committer');
 
 sub parse_address_line {
-       if ($have_mail_address) {
-               return map { $_->format } Mail::Address->parse($_[0]);
-       } else {
-               return Git::parse_mailboxes($_[0]);
-       }
+       return Git::parse_mailboxes($_[0]);
 }
 
 sub split_addrs {
@@ -1089,6 +1084,26 @@ sub sanitize_address {
 
 }
 
+sub strip_garbage_one_address {
+       my ($addr) = @_;
+       chomp $addr;
+       if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
+               # "Foo Bar" <foobar@example.com> [possibly garbage here]
+               # Foo Bar <foobar@example.com> [possibly garbage here]
+               return $1;
+       }
+       if ($addr =~ /^(<[^>]*>).*/) {
+               # <foo@example.com> [possibly garbage here]
+               # if garbage contains other addresses, they are ignored.
+               return $1;
+       }
+       if ($addr =~ /^([^"#,\s]*)/) {
+               # address without quoting: remove anything after the address
+               return $1;
+       }
+       return $addr;
+}
+
 sub sanitize_address_list {
        return (map { sanitize_address($_) } @_);
 }
@@ -1590,10 +1605,12 @@ sub send_message {
        # Now parse the message body
        while(<$fh>) {
                $message .=  $_;
-               if (/^(Signed-off-by|Cc): ([^>]*>?)/i) {
+               if (/^(Signed-off-by|Cc): (.*)/i) {
                        chomp;
                        my ($what, $c) = ($1, $2);
-                       chomp $c;
+                       # strip garbage for the address we'll use:
+                       $c = strip_garbage_one_address($c);
+                       # sanitize a bit more to decide whether to suppress the address:
                        my $sc = sanitize_address($c);
                        if ($sc eq $sender) {
                                next if ($suppress_cc{'self'});
index 328cd80d83a03a674e0541bddb6bce0532c682b7..8b2ce9afdab6d67e8b9af5fc97a30dc6941323f2 100755 (executable)
@@ -307,6 +307,12 @@ push_stash () {
 
        if test -z "$patch_mode"
        then
+               test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
+               if test -n "$untracked"
+               then
+                       git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
+               fi
+
                if test $# != 0
                then
                        git reset -q -- "$@"
@@ -316,11 +322,6 @@ push_stash () {
                else
                        git reset --hard -q
                fi
-               test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
-               if test -n "$untracked"
-               then
-                       git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
-               fi
 
                if test "$keep_index" = "t" && test -n "$i_tree"
                then
diff --git a/git.c b/git.c
index 6b6d9f68e1ea010cb951501cab1fdfa82037b878..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)--;
@@ -404,7 +408,7 @@ static struct cmd_struct commands[] = {
        { "fsck-objects", cmd_fsck, RUN_SETUP },
        { "gc", cmd_gc, RUN_SETUP },
        { "get-tar-commit-id", cmd_get_tar_commit_id },
-       { "grep", cmd_grep, RUN_SETUP_GENTLY | SUPPORT_SUPER_PREFIX },
+       { "grep", cmd_grep, RUN_SETUP_GENTLY },
        { "hash-object", cmd_hash_object },
        { "help", cmd_help },
        { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
index 9208f42ed1753530b8ea46761f8ecc61bbe95976..959f04b494e610258488867aa7de013a3477e5fc 100755 (executable)
@@ -5967,6 +5967,9 @@ sub git_history_body {
                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
 
                if ($ftype eq 'blob') {
+                       print " | " .
+                             $cgi->a({-href => href(action=>"blob_plain", hash_base=>$commit, file_name=>$file_name)}, "raw");
+
                        my $blob_current = $file_hash;
                        my $blob_parent  = git_get_hash_by_path($commit, $file_name);
                        if (defined $blob_current && defined $blob_parent &&
index d936f3a32fe5d38c2b41ae0488ac2c02250a1618..4feacf16e5bcd93dcde4a2ea769f6b61ca369593 100644 (file)
@@ -202,26 +202,26 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
                         struct strbuf *gpg_output, struct strbuf *gpg_status)
 {
        struct child_process gpg = CHILD_PROCESS_INIT;
-       static struct tempfile temp;
-       int fd, ret;
+       struct tempfile *temp;
+       int ret;
        struct strbuf buf = STRBUF_INIT;
 
-       fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
-       if (fd < 0)
+       temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
+       if (!temp)
                return error_errno(_("could not create temporary file"));
-       if (write_in_full(fd, signature, signature_size) < 0) {
+       if (write_in_full(temp->fd, signature, signature_size) < 0 ||
+           close_tempfile_gently(temp) < 0) {
                error_errno(_("failed writing detached signature to '%s'"),
-                           temp.filename.buf);
+                           temp->filename.buf);
                delete_tempfile(&temp);
                return -1;
        }
-       close(fd);
 
        argv_array_pushl(&gpg.args,
                         gpg_program,
                         "--status-fd=1",
                         "--keyid-format=long",
-                        "--verify", temp.filename.buf, "-",
+                        "--verify", temp->filename.buf, "-",
                         NULL);
 
        if (!gpg_status)
diff --git a/graph.c b/graph.c
index e7e20650da6983ada7c94c22aea7ecaec41c5c17..e1f6d3bddb38aadd4e439aea30f542d286ecb98e 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -696,12 +696,8 @@ static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
         * This way, fields printed to the right of the graph will remain
         * aligned for the entire commit.
         */
-       int extra;
-       if (chars_written >= graph->width)
-               return;
-
-       extra = graph->width - chars_written;
-       strbuf_addf(sb, "%*s", (int) extra, "");
+       if (chars_written < graph->width)
+               strbuf_addchars(sb, ' ', graph->width - chars_written);
 }
 
 static void graph_output_padding_line(struct git_graph *graph,
@@ -787,7 +783,7 @@ static void graph_output_pre_commit_line(struct git_graph *graph,
                if (col->commit == graph->commit) {
                        seen_this = 1;
                        strbuf_write_column(sb, col, '|');
-                       strbuf_addf(sb, "%*s", graph->expansion_row, "");
+                       strbuf_addchars(sb, ' ', graph->expansion_row);
                        chars_written += 1 + graph->expansion_row;
                } else if (seen_this && (graph->expansion_row == 0)) {
                        /*
diff --git a/grep.c b/grep.c
index c9e7cc735681ed4c670abd7f319336204391173b..ce6a48e634105154ca4bb267b4eb744c74d4d803 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -1927,16 +1927,6 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
        case GREP_SOURCE_FILE:
                gs->identifier = xstrdup(identifier);
                break;
-       case GREP_SOURCE_SUBMODULE:
-               if (!identifier) {
-                       gs->identifier = NULL;
-                       break;
-               }
-               /*
-                * FALL THROUGH
-                * If the identifier is non-NULL (in the submodule case) it
-                * will be a SHA1 that needs to be copied.
-                */
        case GREP_SOURCE_OID:
                gs->identifier = oiddup(identifier);
                break;
@@ -1959,7 +1949,6 @@ void grep_source_clear_data(struct grep_source *gs)
        switch (gs->type) {
        case GREP_SOURCE_FILE:
        case GREP_SOURCE_OID:
-       case GREP_SOURCE_SUBMODULE:
                FREE_AND_NULL(gs->buf);
                gs->size = 0;
                break;
@@ -2030,8 +2019,6 @@ static int grep_source_load(struct grep_source *gs)
                return grep_source_load_oid(gs);
        case GREP_SOURCE_BUF:
                return gs->buf ? 0 : -1;
-       case GREP_SOURCE_SUBMODULE:
-               break;
        }
        die("BUG: invalid grep_source type to load");
 }
diff --git a/grep.h b/grep.h
index 0c091e5104a83c0f83c65fd0bef9f39caf8f644d..52aecfab6ebdd6c5565e79db6062b7d7a99cf3df 100644 (file)
--- a/grep.h
+++ b/grep.h
@@ -193,7 +193,6 @@ struct grep_source {
                GREP_SOURCE_OID,
                GREP_SOURCE_FILE,
                GREP_SOURCE_BUF,
-               GREP_SOURCE_SUBMODULE,
        } type;
        void *identifier;
 
diff --git a/hash.h b/hash.h
index bef3e630a093b508e78419c5f4e228e836ecb88c..024d0d3d50b174aac1d948e9ecfa7ae92020355a 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -8,11 +8,7 @@
 #elif defined(SHA1_OPENSSL)
 #include <openssl/sha.h>
 #elif defined(SHA1_DC)
-#ifdef DC_SHA1_SUBMODULE
-#include "sha1collisiondetection/lib/sha1.h"
-#else
-#include "sha1dc/sha1.h"
-#endif
+#include "sha1dc_git.h"
 #else /* SHA1_BLK */
 #include "block-sha1/sha1.h"
 #endif
index 9b6a12859be67a09685f13310f9769df260d558d..d42f01ff5ae6f3494d5b427c64bc64406c35d576 100644 (file)
--- a/hashmap.c
+++ b/hashmap.c
@@ -116,9 +116,6 @@ static void rehash(struct hashmap *map, unsigned int newsize)
        unsigned int i, oldsize = map->tablesize;
        struct hashmap_entry **oldtable = map->table;
 
-       if (map->disallow_rehash)
-               return;
-
        alloc_table(map, newsize);
        for (i = 0; i < oldsize; i++) {
                struct hashmap_entry *e = oldtable[i];
@@ -166,6 +163,12 @@ void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
        while (initial_size > size)
                size <<= HASHMAP_RESIZE_BITS;
        alloc_table(map, size);
+
+       /*
+        * Keep track of the number of items in the map and
+        * allow the map to automatically grow as necessary.
+        */
+       map->do_count_items = 1;
 }
 
 void hashmap_free(struct hashmap *map, int free_entries)
@@ -206,9 +209,11 @@ void hashmap_add(struct hashmap *map, void *entry)
        map->table[b] = entry;
 
        /* fix size and rehash if appropriate */
-       map->size++;
-       if (map->size > map->grow_at)
-               rehash(map, map->tablesize << HASHMAP_RESIZE_BITS);
+       if (map->do_count_items) {
+               map->private_size++;
+               if (map->private_size > map->grow_at)
+                       rehash(map, map->tablesize << HASHMAP_RESIZE_BITS);
+       }
 }
 
 void *hashmap_remove(struct hashmap *map, const void *key, const void *keydata)
@@ -224,9 +229,12 @@ void *hashmap_remove(struct hashmap *map, const void *key, const void *keydata)
        old->next = NULL;
 
        /* fix size and rehash if appropriate */
-       map->size--;
-       if (map->size < map->shrink_at)
-               rehash(map, map->tablesize >> HASHMAP_RESIZE_BITS);
+       if (map->do_count_items) {
+               map->private_size--;
+               if (map->private_size < map->shrink_at)
+                       rehash(map, map->tablesize >> HASHMAP_RESIZE_BITS);
+       }
+
        return old;
 }
 
index 7a8fa7fa3da147385969dec392c9406c9dae34ea..7cb29a6aede8e24320555e1845f8e4eb2032ade9 100644 (file)
--- a/hashmap.h
+++ b/hashmap.h
@@ -183,7 +183,7 @@ struct hashmap {
        const void *cmpfn_data;
 
        /* total number of entries (0 means the hashmap is empty) */
-       unsigned int size;
+       unsigned int private_size; /* use hashmap_get_size() */
 
        /*
         * tablesize is the allocated size of the hash table. A non-0 value
@@ -196,8 +196,7 @@ struct hashmap {
        unsigned int grow_at;
        unsigned int shrink_at;
 
-       /* See `hashmap_disallow_rehash`. */
-       unsigned disallow_rehash : 1;
+       unsigned int do_count_items : 1;
 };
 
 /* hashmap functions */
@@ -252,6 +251,18 @@ static inline void hashmap_entry_init(void *entry, unsigned int hash)
        e->next = NULL;
 }
 
+/*
+ * Return the number of items in the map.
+ */
+static inline unsigned int hashmap_get_size(struct hashmap *map)
+{
+       if (map->do_count_items)
+               return map->private_size;
+
+       BUG("hashmap_get_size: size not set");
+       return 0;
+}
+
 /*
  * Returns the hashmap entry for the specified key, or NULL if not found.
  *
@@ -344,24 +355,6 @@ extern void *hashmap_remove(struct hashmap *map, const void *key,
  */
 int hashmap_bucket(const struct hashmap *map, unsigned int hash);
 
-/*
- * Disallow/allow rehashing of the hashmap.
- * This is useful if the caller knows that the hashmap needs multi-threaded
- * access.  The caller is still required to guard/lock searches and inserts
- * in a manner appropriate to their usage.  This simply prevents the table
- * from being unexpectedly re-mapped.
- *
- * It is up to the caller to ensure that the hashmap is initialized to a
- * reasonable size to prevent poor performance.
- *
- * A call to allow rehashing does not force a rehash; that might happen
- * with the next insert or delete.
- */
-static inline void hashmap_disallow_rehash(struct hashmap *map, unsigned value)
-{
-       map->disallow_rehash = value;
-}
-
 /*
  * Used to iterate over all entries of a hashmap. Note that it is
  * not safe to add or remove entries to the hashmap while
@@ -387,6 +380,43 @@ static inline void *hashmap_iter_first(struct hashmap *map,
        return hashmap_iter_next(iter);
 }
 
+/*
+ * Disable item counting and automatic rehashing when adding/removing items.
+ *
+ * Normally, the hashmap keeps track of the number of items in the map
+ * and uses it to dynamically resize it.  This (both the counting and
+ * the resizing) can cause problems when the map is being used by
+ * threaded callers (because the hashmap code does not know about the
+ * locking strategy used by the threaded callers and therefore, does
+ * not know how to protect the "private_size" counter).
+ */
+static inline void hashmap_disable_item_counting(struct hashmap *map)
+{
+       map->do_count_items = 0;
+}
+
+/*
+ * Re-enable item couting when adding/removing items.
+ * If counting is currently disabled, it will force count them.
+ * It WILL NOT automatically rehash them.
+ */
+static inline void hashmap_enable_item_counting(struct hashmap *map)
+{
+       void *item;
+       unsigned int n = 0;
+       struct hashmap_iter iter;
+
+       if (map->do_count_items)
+               return;
+
+       hashmap_iter_init(map, &iter);
+       while ((item = hashmap_iter_next(&iter)))
+               n++;
+
+       map->do_count_items = 1;
+       map->private_size = n;
+}
+
 /* String interning */
 
 /*
index 519025d2c3d944afb6fdd6ca1f3b091827a67272..f3dc218b2a3d2662efda56fb6c8878a37a4e67a4 100644 (file)
@@ -9,6 +9,7 @@
 #include "string-list.h"
 #include "url.h"
 #include "argv-array.h"
+#include "packfile.h"
 
 static const char content_type[] = "Content-Type";
 static const char content_length[] = "Content-Length";
@@ -357,7 +358,7 @@ static void inflate_request(const char *prog_name, int out, int buffer_input)
                                die("zlib error inflating request, result %d", ret);
 
                        n = stream.total_out - cnt;
-                       if (write_in_full(out, out_buf, n) != n)
+                       if (write_in_full(out, out_buf, n) < 0)
                                die("%s aborted reading request", prog_name);
                        cnt += n;
 
@@ -378,7 +379,7 @@ static void copy_request(const char *prog_name, int out)
        ssize_t n = read_request(0, &buf);
        if (n < 0)
                die_errno("error reading request body");
-       if (write_in_full(out, buf, n) != n)
+       if (write_in_full(out, buf, n) < 0)
                die("%s aborted reading request", prog_name);
        close(out);
        free(buf);
@@ -485,10 +486,9 @@ static int show_head_ref(const char *refname, const struct object_id *oid,
        struct strbuf *buf = cb_data;
 
        if (flag & REF_ISSYMREF) {
-               struct object_id unused;
                const char *target = resolve_ref_unsafe(refname,
                                                        RESOLVE_REF_READING,
-                                                       unused.hash, NULL);
+                                                       NULL, NULL);
 
                if (target)
                        strbuf_addf(buf, "ref: %s\n", strip_namespace(target));
index c91f40a610541d6201edf2a2c95b8fef203f3c2f..493ee7d719d488f25cd6c45bb4c7ce1d5a75977b 100644 (file)
@@ -11,6 +11,7 @@
 #include "list-objects.h"
 #include "sigchain.h"
 #include "argv-array.h"
+#include "packfile.h"
 
 #ifdef EXPAT_NEEDS_XMLPARSE_H
 #include <xmlparse.h>
@@ -1017,7 +1018,7 @@ static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
        memcpy(hex, path, 2);
        path += 2;
        path++; /* skip '/' */
-       memcpy(hex, path, GIT_SHA1_HEXSZ - 2);
+       memcpy(hex + 2, path, GIT_SHA1_HEXSZ - 2);
 
        return get_oid_hex(hex, oid);
 }
@@ -1522,6 +1523,7 @@ static int remote_exists(const char *path)
                break;
        case HTTP_ERROR:
                error("unable to access '%s': %s", url, curl_errorstr);
+               /* fallthrough */
        default:
                ret = -1;
        }
index ee049cb13df6ed6a64b1980004100a5dd55dde51..1ae8363de2bb618df534cc85c86eba3eac9e9c0a 100644 (file)
@@ -4,6 +4,7 @@
 #include "http.h"
 #include "list.h"
 #include "transport.h"
+#include "packfile.h"
 
 struct alt_base {
        char *base;
diff --git a/http.c b/http.c
index 76ff63c14d44057f78b532fb9d873c94ae56231e..713525f38ed3f254aed2b0dd65fece7972444072 100644 (file)
--- a/http.c
+++ b/http.c
@@ -11,6 +11,7 @@
 #include "pkt-line.h"
 #include "gettext.h"
 #include "transport.h"
+#include "packfile.h"
 
 static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
 #if LIBCURL_VERSION_NUM >= 0x070a08
@@ -91,7 +92,7 @@ static struct {
         * here, too
         */
 };
-#if LIBCURL_VERSION_NUM >= 0x071600
+#ifdef CURLGSSAPI_DELEGATION_FLAG
 static const char *curl_deleg;
 static struct {
        const char *name;
@@ -352,7 +353,7 @@ static int http_options(const char *var, const char *value, void *cb)
        }
 
        if (!strcmp("http.delegation", var)) {
-#if LIBCURL_VERSION_NUM >= 0x071600
+#ifdef CURLGSSAPI_DELEGATION_FLAG
                return git_config_string(&curl_deleg, var, value);
 #else
                warning(_("Delegation control is not supported with cURL < 7.22.0"));
@@ -637,9 +638,7 @@ static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size,
        switch (type) {
        case CURLINFO_TEXT:
                trace_printf_key(&trace_curl, "== Info: %s", data);
-       default:                /* we ignore unknown types by default */
-               return 0;
-
+               break;
        case CURLINFO_HEADER_OUT:
                text = "=> Send header";
                curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
@@ -664,6 +663,9 @@ static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size,
                text = "<= Recv SSL data";
                curl_dump_data(text, (unsigned char *)data, size);
                break;
+
+       default:                /* we ignore unknown types by default */
+               return 0;
        }
        return 0;
 }
@@ -677,6 +679,7 @@ void setup_curl_trace(CURL *handle)
        curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
 }
 
+#ifdef CURLPROTO_HTTP
 static long get_curl_allowed_protocols(int from_user)
 {
        long allowed_protocols = 0;
@@ -692,6 +695,7 @@ static long get_curl_allowed_protocols(int from_user)
 
        return allowed_protocols;
 }
+#endif
 
 static CURL *get_curl_handle(void)
 {
@@ -717,7 +721,7 @@ static CURL *get_curl_handle(void)
        curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
 #endif
 
-#if LIBCURL_VERSION_NUM >= 0x071600
+#ifdef CURLGSSAPI_DELEGATION_FLAG
        if (curl_deleg) {
                int i;
                for (i = 0; i < ARRAY_SIZE(curl_deleg_levels); i++) {
@@ -790,7 +794,7 @@ static CURL *get_curl_handle(void)
 #elif LIBCURL_VERSION_NUM >= 0x071101
        curl_easy_setopt(result, CURLOPT_POST301, 1);
 #endif
-#if LIBCURL_VERSION_NUM >= 0x071304
+#ifdef CURLPROTO_HTTP
        curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
                         get_curl_allowed_protocols(0));
        curl_easy_setopt(result, CURLOPT_PROTOCOLS,
index b2d0b849bb20c88532dd2952a4a5ae94204ccad8..8c785f3ca20c52266d7f99ca0d306a324756ede8 100644 (file)
@@ -35,11 +35,11 @@ typedef void *SSL;
 #include "http.h"
 #endif
 
-#if defined(USE_CURL_FOR_IMAP_SEND) && defined(NO_OPENSSL)
-/* only available option */
+#if defined(USE_CURL_FOR_IMAP_SEND)
+/* Always default to curl if it's available. */
 #define USE_CURL_DEFAULT 1
 #else
-/* strictly opt in */
+/* We don't have curl, so continue to use the historical implementation */
 #define USE_CURL_DEFAULT 0
 #endif
 
@@ -926,6 +926,25 @@ static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const cha
        return 0;
 }
 
+static void server_fill_credential(struct imap_server_conf *srvc, struct credential *cred)
+{
+       if (srvc->user && srvc->pass)
+               return;
+
+       cred->protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
+       cred->host = xstrdup(srvc->host);
+
+       cred->username = xstrdup_or_null(srvc->user);
+       cred->password = xstrdup_or_null(srvc->pass);
+
+       credential_fill(cred);
+
+       if (!srvc->user)
+               srvc->user = xstrdup(cred->username);
+       if (!srvc->pass)
+               srvc->pass = xstrdup(cred->password);
+}
+
 static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *folder)
 {
        struct credential cred = CREDENTIAL_INIT;
@@ -1078,20 +1097,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
                }
 #endif
                imap_info("Logging in...\n");
-               if (!srvc->user || !srvc->pass) {
-                       cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
-                       cred.host = xstrdup(srvc->host);
-
-                       cred.username = xstrdup_or_null(srvc->user);
-                       cred.password = xstrdup_or_null(srvc->pass);
-
-                       credential_fill(&cred);
-
-                       if (!srvc->user)
-                               srvc->user = xstrdup(cred.username);
-                       if (!srvc->pass)
-                               srvc->pass = xstrdup(cred.password);
-               }
+               server_fill_credential(srvc, &cred);
 
                if (srvc->auth_method) {
                        struct imap_cmd_cb cb;
@@ -1392,7 +1398,7 @@ static int append_msgs_to_imap(struct imap_server_conf *server,
 }
 
 #ifdef USE_CURL_FOR_IMAP_SEND
-static CURL *setup_curl(struct imap_server_conf *srvc)
+static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
 {
        CURL *curl;
        struct strbuf path = STRBUF_INIT;
@@ -1405,6 +1411,7 @@ static CURL *setup_curl(struct imap_server_conf *srvc)
        if (!curl)
                die("curl_easy_init failed");
 
+       server_fill_credential(&server, cred);
        curl_easy_setopt(curl, CURLOPT_USERNAME, server.user);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, server.pass);
 
@@ -1454,8 +1461,9 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
        struct buffer msgbuf = { STRBUF_INIT, 0 };
        CURL *curl;
        CURLcode res = CURLE_OK;
+       struct credential cred = CREDENTIAL_INIT;
 
-       curl = setup_curl(server);
+       curl = setup_curl(server, &cred);
        curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf);
 
        fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
@@ -1490,7 +1498,20 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
        curl_easy_cleanup(curl);
        curl_global_cleanup();
 
-       return 0;
+       if (cred.username) {
+               if (res == CURLE_OK)
+                       credential_approve(&cred);
+#if LIBCURL_VERSION_NUM >= 0x070d01
+               else if (res == CURLE_LOGIN_DENIED)
+#else
+               else
+#endif
+                       credential_reject(&cred);
+       }
+
+       credential_clear(&cred);
+
+       return res != CURLE_OK;
 }
 #endif
 
index ab0709f9aecaacdfaf44aa31c88873b4ab5d8278..545ad0f28bce0ac0f92a31019b92f7ef8a668356 100644 (file)
@@ -90,7 +90,7 @@ static int range_cmp(const void *_r, const void *_s)
  */
 static void range_set_check_invariants(struct range_set *rs)
 {
-       int i;
+       unsigned int i;
 
        if (!rs)
                return;
@@ -110,8 +110,8 @@ static void range_set_check_invariants(struct range_set *rs)
  */
 void sort_and_merge_range_set(struct range_set *rs)
 {
-       int i;
-       int o = 0; /* output cursor */
+       unsigned int i;
+       unsigned int o = 0; /* output cursor */
 
        QSORT(rs->ranges, rs->nr, range_cmp);
 
@@ -144,7 +144,7 @@ void sort_and_merge_range_set(struct range_set *rs)
 static void range_set_union(struct range_set *out,
                             struct range_set *a, struct range_set *b)
 {
-       int i = 0, j = 0;
+       unsigned int i = 0, j = 0;
        struct range *ra = a->ranges;
        struct range *rb = b->ranges;
        /* cannot make an alias of out->ranges: it may change during grow */
@@ -186,7 +186,7 @@ static void range_set_union(struct range_set *out,
 static void range_set_difference(struct range_set *out,
                                  struct range_set *a, struct range_set *b)
 {
-       int i, j =  0;
+       unsigned int i, j =  0;
        for (i = 0; i < a->nr; i++) {
                long start = a->ranges[i].start;
                long end = a->ranges[i].end;
@@ -397,7 +397,7 @@ static void diff_ranges_filter_touched(struct diff_ranges *out,
                                       struct diff_ranges *diff,
                                       struct range_set *rs)
 {
-       int i, j = 0;
+       unsigned int i, j = 0;
 
        assert(out->target.nr == 0);
 
@@ -426,7 +426,7 @@ static void range_set_shift_diff(struct range_set *out,
                                 struct range_set *rs,
                                 struct diff_ranges *diff)
 {
-       int i, j = 0;
+       unsigned int i, j = 0;
        long offset = 0;
        struct range *src = rs->ranges;
        struct range *target = diff->target.ranges;
@@ -873,7 +873,7 @@ static char *output_prefix(struct diff_options *opt)
 
 static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *range)
 {
-       int i, j = 0;
+       unsigned int i, j = 0;
        long p_lines, t_lines;
        unsigned long *p_ends = NULL, *t_ends = NULL;
        struct diff_filepair *pair = range->pair;
@@ -906,7 +906,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
                long t_start = range->ranges.ranges[i].start;
                long t_end = range->ranges.ranges[i].end;
                long t_cur = t_start;
-               int j_last;
+               unsigned int j_last;
 
                while (j < diff->target.nr && diff->target.ranges[j].end < t_start)
                        j++;
index 7a5c24e2df40c09274077928574bc91349d475d1..e2a5ee7c6d077df353a20d2d55d8b4fc24642bc3 100644 (file)
@@ -14,7 +14,7 @@ struct range {
 
 /* A set of ranges.  The ranges must always be disjoint and sorted. */
 struct range_set {
-       int alloc, nr;
+       unsigned int alloc, nr;
        struct range *ranges;
 };
 
diff --git a/list.h b/list.h
index a226a870dc09d6b58346a92d5f98078d2e55163a..eb601192f4ca9a6af126c82f4c2a24cb9145009d 100644 (file)
--- a/list.h
+++ b/list.h
@@ -163,4 +163,42 @@ static inline void list_replace_init(struct list_head *old,
        INIT_LIST_HEAD(old);
 }
 
+/*
+ * This is exactly the same as a normal list_head, except that it can be
+ * declared volatile (e.g., if you have a list that may be accessed from signal
+ * handlers).
+ */
+struct volatile_list_head {
+       volatile struct volatile_list_head *next, *prev;
+};
+
+#define VOLATILE_LIST_HEAD(name) \
+       volatile struct volatile_list_head name = { &(name), &(name) }
+
+static inline void __volatile_list_del(volatile struct volatile_list_head *prev,
+                                      volatile struct volatile_list_head *next)
+{
+       next->prev = prev;
+       prev->next = next;
+}
+
+static inline void volatile_list_del(volatile struct volatile_list_head *elem)
+{
+       __volatile_list_del(elem->prev, elem->next);
+}
+
+static inline int volatile_list_empty(volatile struct volatile_list_head *head)
+{
+       return head == head->next;
+}
+
+static inline void volatile_list_add(volatile struct volatile_list_head *newp,
+                                    volatile struct volatile_list_head *head)
+{
+       head->next->prev = newp;
+       newp->next = head->next;
+       newp->prev = head;
+       head->next = newp;
+}
+
 #endif /* LIST_H */
index 9fb855a90030e07e28c1ae9d0994006da22cd4ed..a6ad2ec12dc9c1ece81f120196c81fd259e40eb6 100644 (file)
@@ -154,7 +154,7 @@ static void create_temp(mmfile_t *src, char *path, size_t len)
 
        xsnprintf(path, len, ".merge_file_XXXXXX");
        fd = xmkstemp(path);
-       if (write_in_full(fd, src->ptr, src->size) != src->size)
+       if (write_in_full(fd, src->ptr, src->size) < 0)
                die_errno("unable to write temp-file");
        close(fd);
 }
index aa69210d8b3a9063517d2d84153dc3ba738bef30..efcb7d7dfe30a2fbad03244ba4ca9e536a3649e4 100644 (file)
@@ -72,7 +72,6 @@ static void resolve_symlink(struct strbuf *path)
 /* Make sure errno contains a meaningful value on error */
 static int lock_file(struct lock_file *lk, const char *path, int flags)
 {
-       int fd;
        struct strbuf filename = STRBUF_INIT;
 
        strbuf_addstr(&filename, path);
@@ -80,9 +79,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
                resolve_symlink(&filename);
 
        strbuf_addstr(&filename, LOCK_SUFFIX);
-       fd = create_tempfile(&lk->tempfile, filename.buf);
+       lk->tempfile = create_tempfile(filename.buf);
        strbuf_release(&filename);
-       return fd;
+       return lk->tempfile ? lk->tempfile->fd : -1;
 }
 
 /*
@@ -191,7 +190,7 @@ char *get_locked_file_path(struct lock_file *lk)
 {
        struct strbuf ret = STRBUF_INIT;
 
-       strbuf_addstr(&ret, get_tempfile_path(&lk->tempfile));
+       strbuf_addstr(&ret, get_tempfile_path(lk->tempfile));
        if (ret.len <= LOCK_SUFFIX_LEN ||
            strcmp(ret.buf + ret.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))
                die("BUG: get_locked_file_path() called for malformed lock object");
index 572064939c718281b56abda7757bb21d35f42de3..7c1c484d7cfdfab126fd0ce947ac40f650e5d107 100644 (file)
  *
  * The caller:
  *
- * * Allocates a `struct lock_file` either as a static variable or on
- *   the heap, initialized to zeros. Once you use the structure to
- *   call the `hold_lock_file_for_*()` family of functions, it belongs
- *   to the lockfile subsystem and its storage must remain valid
- *   throughout the life of the program (i.e. you cannot use an
- *   on-stack variable to hold this structure).
+ * * Allocates a `struct lock_file` with whatever storage duration you
+ *   desire. The struct does not have to be initialized before being
+ *   used, but it is good practice to do so using by setting it to
+ *   all-zeros (or using the LOCK_INIT macro). This puts the object in a
+ *   consistent state that allows you to call rollback_lock_file() even
+ *   if the lock was never taken (in which case it is a noop).
  *
  * * Attempts to create a lockfile by calling `hold_lock_file_for_update()`.
  *
  *   `rollback_lock_file()`.
  *
  * * Close the file descriptor without removing or renaming the
- *   lockfile by calling `close_lock_file()`, and later call
+ *   lockfile by calling `close_lock_file_gently()`, and later call
  *   `commit_lock_file()`, `commit_lock_file_to()`,
  *   `rollback_lock_file()`, or `reopen_lock_file()`.
  *
- * Even after the lockfile is committed or rolled back, the
- * `lock_file` object must not be freed or altered by the caller.
- * However, it may be reused; just pass it to another call of
- * `hold_lock_file_for_update()`.
+ * After the lockfile is committed or rolled back, the `lock_file`
+ * object can be discarded or reused.
  *
  * If the program exits before `commit_lock_file()`,
  * `commit_lock_file_to()`, or `rollback_lock_file()` is called, the
@@ -85,7 +83,7 @@
  *
  * If you need to close the file descriptor you obtained from a
  * `hold_lock_file_for_*()` function yourself, do so by calling
- * `close_lock_file()`. See "tempfile.h" for more information.
+ * `close_lock_file_gently()`. See "tempfile.h" for more information.
  *
  *
  * Under the covers, a lockfile is just a tempfile with a few helper
  *
  * Similarly, `commit_lock_file`, `commit_lock_file_to`, and
  * `close_lock_file` return 0 on success. On failure they set `errno`
- * appropriately, do their best to roll back the lockfile, and return
- * -1.
+ * appropriately and return -1. The `commit` variants (but not `close`)
+ * do their best to delete the temporary file before returning.
  */
 
 #include "tempfile.h"
 
 struct lock_file {
-       struct tempfile tempfile;
+       struct tempfile *tempfile;
 };
 
+#define LOCK_INIT { NULL }
+
 /* String appended to a filename to derive the lockfile name: */
 #define LOCK_SUFFIX ".lock"
 #define LOCK_SUFFIX_LEN 5
@@ -180,7 +180,7 @@ static inline int hold_lock_file_for_update(
  */
 static inline int is_lock_file_locked(struct lock_file *lk)
 {
-       return is_tempfile_active(&lk->tempfile);
+       return is_tempfile_active(lk->tempfile);
 }
 
 /*
@@ -202,12 +202,13 @@ extern NORETURN void unable_to_lock_die(const char *path, int err);
 /*
  * Associate a stdio stream with the lockfile (which must still be
  * open). Return `NULL` (*without* rolling back the lockfile) on
- * error. The stream is closed automatically when `close_lock_file()`
- * is called or when the file is committed or rolled back.
+ * error. The stream is closed automatically when
+ * `close_lock_file_gently()` is called or when the file is committed or
+ * rolled back.
  */
 static inline FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
 {
-       return fdopen_tempfile(&lk->tempfile, mode);
+       return fdopen_tempfile(lk->tempfile, mode);
 }
 
 /*
@@ -216,17 +217,17 @@ static inline FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
  */
 static inline const char *get_lock_file_path(struct lock_file *lk)
 {
-       return get_tempfile_path(&lk->tempfile);
+       return get_tempfile_path(lk->tempfile);
 }
 
 static inline int get_lock_file_fd(struct lock_file *lk)
 {
-       return get_tempfile_fd(&lk->tempfile);
+       return get_tempfile_fd(lk->tempfile);
 }
 
 static inline FILE *get_lock_file_fp(struct lock_file *lk)
 {
-       return get_tempfile_fp(&lk->tempfile);
+       return get_tempfile_fp(lk->tempfile);
 }
 
 /*
@@ -241,22 +242,21 @@ extern char *get_locked_file_path(struct lock_file *lk);
  * lockfile over the file being locked. Return 0 upon success. On
  * failure to `close(2)`, return a negative value and roll back the
  * lock file. Usually `commit_lock_file()`, `commit_lock_file_to()`,
- * or `rollback_lock_file()` should eventually be called if
- * `close_lock_file()` succeeds.
+ * or `rollback_lock_file()` should eventually be called.
  */
-static inline int close_lock_file(struct lock_file *lk)
+static inline int close_lock_file_gently(struct lock_file *lk)
 {
-       return close_tempfile(&lk->tempfile);
+       return close_tempfile_gently(lk->tempfile);
 }
 
 /*
- * Re-open a lockfile that has been closed using `close_lock_file()`
+ * Re-open a lockfile that has been closed using `close_lock_file_gently()`
  * but not yet committed or rolled back. This can be used to implement
  * a sequence of operations like the following:
  *
  * * Lock file.
  *
- * * Write new contents to lockfile, then `close_lock_file()` to
+ * * Write new contents to lockfile, then `close_lock_file_gently()` to
  *   cause the contents to be written to disk.
  *
  * * Pass the name of the lockfile to another program to allow it (and
@@ -270,7 +270,7 @@ static inline int close_lock_file(struct lock_file *lk)
  */
 static inline int reopen_lock_file(struct lock_file *lk)
 {
-       return reopen_tempfile(&lk->tempfile);
+       return reopen_tempfile(lk->tempfile);
 }
 
 /*
index 410ab4f02de5b532485c46b5cceffc3c677df214..cea056234d0a0b605c988a4e157852cd659f22ef 100644 (file)
@@ -185,7 +185,6 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d
 {
        const struct name_decoration *list, *head = NULL;
        const char *branch_name = NULL;
-       struct object_id unused;
        int rru_flags;
 
        /* First find HEAD */
@@ -198,7 +197,7 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d
                return NULL;
 
        /* Now resolve and find the matching current branch */
-       branch_name = resolve_ref_unsafe("HEAD", 0, unused.hash, &rru_flags);
+       branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
        if (!(rru_flags & REF_ISSYMREF))
                return NULL;
 
index bd574cb75210334b1a4628f182743b49bd389cd7..a89db22ab0c8ef50ae31d5811de5fc48d09224a2 100644 (file)
@@ -149,16 +149,14 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
        at = strchr(f.buf, '@');
        if (!at) {
                parse_bogus_from(mi, from);
-               return;
+               goto out;
        }
 
        /*
         * If we already have one email, don't take any confusing lines
         */
-       if (mi->email.len && strchr(at + 1, '@')) {
-               strbuf_release(&f);
-               return;
-       }
+       if (mi->email.len && strchr(at + 1, '@'))
+               goto out;
 
        /* Pick up the string around '@', possibly delimited with <>
         * pair; that is the email part.
@@ -198,6 +196,7 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
        }
 
        get_sane_name(&mi->name, &f, &mi->email);
+out:
        strbuf_release(&f);
 }
 
@@ -368,11 +367,16 @@ static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
 
        while ((c = *in++) != 0) {
                if (c == '=') {
-                       int d = *in++;
+                       int ch, d = *in;
                        if (d == '\n' || !d)
                                break; /* drop trailing newline */
-                       strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
-                       continue;
+                       ch = hex2chr(in);
+                       if (ch >= 0) {
+                               strbuf_addch(out, ch);
+                               in += 2;
+                               continue;
+                       }
+                       /* garbage -- fall through */
                }
                if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
                        c = 0x20;
@@ -823,6 +827,7 @@ static void handle_filter(struct mailinfo *mi, struct strbuf *line)
                if (!handle_commit_msg(mi, line))
                        break;
                mi->filter_stage++;
+               /* fallthrough */
        case 1:
                handle_patch(mi, line);
                break;
@@ -929,6 +934,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
                        error("Detected mismatched boundaries, can't recover");
                        mi->input_error = -1;
                        mi->content_top = mi->content;
+                       strbuf_release(&newline);
                        return 0;
                }
                handle_filter(mi, &newline);
index c1a79c100c9a71083a58401081fb2929420ce361..cb921b4db676e3db918ee16f419cd2b78e0bf57e 100644 (file)
--- a/mailmap.c
+++ b/mailmap.c
@@ -214,17 +214,17 @@ static int read_mailmap_blob(struct string_list *map,
                             const char *name,
                             char **repo_abbrev)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
        char *buf;
        unsigned long size;
        enum object_type type;
 
        if (!name)
                return 0;
-       if (get_sha1(name, sha1) < 0)
+       if (get_oid(name, &oid) < 0)
                return 0;
 
-       buf = read_sha1_file(sha1, &type, &size);
+       buf = read_sha1_file(oid.hash, &type, &size);
        if (!buf)
                return error("unable to read mailmap object at %s", name);
        if (type != OBJ_BLOB)
index 1494ffdb828106882053065e3ac8cc383136cf80..1d3f8f0d22157e11c1e0db6f230e39240d2f3e63 100644 (file)
 #include "dir.h"
 #include "submodule.h"
 
+struct path_hashmap_entry {
+       struct hashmap_entry e;
+       char path[FLEX_ARRAY];
+};
+
+static int path_hashmap_cmp(const void *cmp_data,
+                           const void *entry,
+                           const void *entry_or_key,
+                           const void *keydata)
+{
+       const struct path_hashmap_entry *a = entry;
+       const struct path_hashmap_entry *b = entry_or_key;
+       const char *key = keydata;
+
+       if (ignore_case)
+               return strcasecmp(a->path, key ? key : b->path);
+       else
+               return strcmp(a->path, key ? key : b->path);
+}
+
+static unsigned int path_hash(const char *path)
+{
+       return ignore_case ? strihash(path) : strhash(path);
+}
+
 static void flush_output(struct merge_options *o)
 {
        if (o->buffer_output < 2 && o->obuf.len) {
@@ -314,29 +339,25 @@ static int save_files_dirs(const unsigned char *sha1,
                struct strbuf *base, const char *path,
                unsigned int mode, int stage, void *context)
 {
+       struct path_hashmap_entry *entry;
        int baselen = base->len;
        struct merge_options *o = context;
 
        strbuf_addstr(base, path);
 
-       if (S_ISDIR(mode))
-               string_list_insert(&o->current_directory_set, base->buf);
-       else
-               string_list_insert(&o->current_file_set, base->buf);
+       FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
+       hashmap_entry_init(entry, path_hash(entry->path));
+       hashmap_add(&o->current_file_dir_set, entry);
 
        strbuf_setlen(base, baselen);
        return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
 }
 
-static int get_files_dirs(struct merge_options *o, struct tree *tree)
+static void get_files_dirs(struct merge_options *o, struct tree *tree)
 {
-       int n;
        struct pathspec match_all;
        memset(&match_all, 0, sizeof(match_all));
-       if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
-               return 0;
-       n = o->current_file_set.nr + o->current_directory_set.nr;
-       return n;
+       read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o);
 }
 
 /*
@@ -646,6 +667,7 @@ static void add_flattened_path(struct strbuf *out, const char *s)
 
 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
 {
+       struct path_hashmap_entry *entry;
        struct strbuf newpath = STRBUF_INIT;
        int suffix = 0;
        size_t base_len;
@@ -654,14 +676,16 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
        add_flattened_path(&newpath, branch);
 
        base_len = newpath.len;
-       while (string_list_has_string(&o->current_file_set, newpath.buf) ||
-              string_list_has_string(&o->current_directory_set, newpath.buf) ||
+       while (hashmap_get_from_hash(&o->current_file_dir_set,
+                                    path_hash(newpath.buf), newpath.buf) ||
               (!o->call_depth && file_exists(newpath.buf))) {
                strbuf_setlen(&newpath, base_len);
                strbuf_addf(&newpath, "_%d", suffix++);
        }
 
-       string_list_insert(&o->current_file_set, newpath.buf);
+       FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
+       hashmap_entry_init(entry, path_hash(entry->path));
+       hashmap_add(&o->current_file_dir_set, entry);
        return strbuf_detach(&newpath, NULL);
 }
 
@@ -1927,7 +1951,7 @@ int merge_trees(struct merge_options *o,
        }
 
        if (oid_eq(&common->object.oid, &merge->object.oid)) {
-               output(o, 0, _("Already up-to-date!"));
+               output(o, 0, _("Already up to date!"));
                *result = head;
                return 1;
        }
@@ -1945,8 +1969,14 @@ int merge_trees(struct merge_options *o,
        if (unmerged_cache()) {
                struct string_list *entries, *re_head, *re_merge;
                int i;
-               string_list_clear(&o->current_file_set, 1);
-               string_list_clear(&o->current_directory_set, 1);
+               /*
+                * Only need the hashmap while processing entries, so
+                * initialize it here and free it when we are done running
+                * through the entries. Keeping it in the merge_options as
+                * opposed to decaring a local hashmap is for convenience
+                * so that we don't have to pass it to around.
+                */
+               hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL, 512);
                get_files_dirs(o, head);
                get_files_dirs(o, merge);
 
@@ -1956,7 +1986,7 @@ int merge_trees(struct merge_options *o,
                re_merge = get_renames(o, merge, common, head, merge, entries);
                clean = process_renames(o, re_head, re_merge);
                if (clean < 0)
-                       return clean;
+                       goto cleanup;
                for (i = entries->nr-1; 0 <= i; i--) {
                        const char *path = entries->items[i].string;
                        struct stage_data *e = entries->items[i].util;
@@ -1964,8 +1994,10 @@ int merge_trees(struct merge_options *o,
                                int ret = process_entry(o, path, e);
                                if (!ret)
                                        clean = 0;
-                               else if (ret < 0)
-                                       return ret;
+                               else if (ret < 0) {
+                                       clean = ret;
+                                       goto cleanup;
+                               }
                        }
                }
                for (i = 0; i < entries->nr; i++) {
@@ -1975,13 +2007,19 @@ int merge_trees(struct merge_options *o,
                                    entries->items[i].string);
                }
 
+cleanup:
                string_list_clear(re_merge, 0);
                string_list_clear(re_head, 0);
                string_list_clear(entries, 1);
 
+               hashmap_free(&o->current_file_dir_set, 1);
+
                free(re_merge);
                free(re_head);
                free(entries);
+
+               if (clean < 0)
+                       return clean;
        }
        else
                clean = 1;
@@ -2177,8 +2215,6 @@ void init_merge_options(struct merge_options *o)
        if (o->verbosity >= 5)
                o->buffer_output = 0;
        strbuf_init(&o->obuf, 0);
-       string_list_init(&o->current_file_set, 1);
-       string_list_init(&o->current_directory_set, 1);
        string_list_init(&o->df_conflict_file_set, 1);
 }
 
index 735343b4136d998ee3d2ac3672bdd7b5a5ba017e..80d69d140195cc3ba1054050569e56bfc0277b56 100644 (file)
@@ -25,8 +25,7 @@ struct merge_options {
        int show_rename_progress;
        int call_depth;
        struct strbuf obuf;
-       struct string_list current_file_set;
-       struct string_list current_directory_set;
+       struct hashmap current_file_dir_set;
        struct string_list df_conflict_file_set;
 };
 
index 0e10f3eab804cdc08d3a2f97021e8b18edcb552a..45c98db0a057e93b5f3f84eba2008da86130010d 100644 (file)
@@ -17,10 +17,14 @@ struct dir_entry {
 };
 
 static int dir_entry_cmp(const void *unused_cmp_data,
-                        const struct dir_entry *e1,
-                        const struct dir_entry *e2,
-                        const char *name)
+                        const void *entry,
+                        const void *entry_or_key,
+                        const void *keydata)
 {
+       const struct dir_entry *e1 = entry;
+       const struct dir_entry *e2 = entry_or_key;
+       const char *name = keydata;
+
        return e1->namelen != e2->namelen || strncasecmp(e1->name,
                        name ? name : e2->name, e1->namelen);
 }
@@ -110,10 +114,12 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
 }
 
 static int cache_entry_cmp(const void *unused_cmp_data,
-                          const struct cache_entry *ce1,
-                          const struct cache_entry *ce2,
+                          const void *entry,
+                          const void *entry_or_key,
                           const void *remove)
 {
+       const struct cache_entry *ce1 = entry;
+       const struct cache_entry *ce2 = entry_or_key;
        /*
         * For remove_name_hash, find the exact entry (pointer equality); for
         * index_file_exists, find all entries with matching hash code and
@@ -574,15 +580,19 @@ static void lazy_init_name_hash(struct index_state *istate)
 {
        if (istate->name_hash_initialized)
                return;
-       hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
-                       NULL, istate->cache_nr);
-       hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
-                       NULL, istate->cache_nr);
+       hashmap_init(&istate->name_hash, cache_entry_cmp, NULL, istate->cache_nr);
+       hashmap_init(&istate->dir_hash, dir_entry_cmp, NULL, istate->cache_nr);
 
        if (lookup_lazy_params(istate)) {
-               hashmap_disallow_rehash(&istate->dir_hash, 1);
+               /*
+                * Disable item counting and automatic rehashing because
+                * we do per-chain (mod n) locking rather than whole hashmap
+                * locking and we need to prevent the table-size from changing
+                * and bucket items from being redistributed.
+                */
+               hashmap_disable_item_counting(&istate->dir_hash);
                threaded_lazy_init_name_hash(istate);
-               hashmap_disallow_rehash(&istate->dir_hash, 0);
+               hashmap_enable_item_counting(&istate->dir_hash);
        } else {
                int nr;
                for (nr = 0; nr < istate->cache_nr; nr++)
index c12b354f1003a29c6faf58e7ec970e4ac761812e..4352c34a6ee50f907f2d03e7d0ed1a596345a5d3 100644 (file)
@@ -302,14 +302,12 @@ static void write_buf_to_worktree(const struct object_id *obj,
        fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
 
        while (size > 0) {
-               long ret = write_in_full(fd, buf, size);
+               ssize_t ret = write_in_full(fd, buf, size);
                if (ret < 0) {
                        /* Ignore epipe */
                        if (errno == EPIPE)
                                break;
                        die_errno("notes-merge");
-               } else if (!ret) {
-                       die("notes-merge: disk full?");
                }
                size -= ret;
                buf += ret;
@@ -624,7 +622,7 @@ int notes_merge(struct notes_merge_options *o,
        if (!oidcmp(&remote->object.oid, base_oid)) {
                /* Already merged; result == local commit */
                if (o->verbosity >= 2)
-                       printf("Already up-to-date!\n");
+                       printf("Already up to date!\n");
                oidcpy(result_oid, &local->object.oid);
                goto found_result;
        }
@@ -709,7 +707,7 @@ int notes_merge_commit(struct notes_merge_options *o,
                /* write file as blob, and add to partial_tree */
                if (stat(path.buf, &st))
                        die_errno("Failed to stat '%s'", path.buf);
-               if (index_path(blob_oid.hash, path.buf, &st, HASH_WRITE_OBJECT))
+               if (index_path(&blob_oid, path.buf, &st, HASH_WRITE_OBJECT))
                        die("Failed to write blob object from '%s'", path.buf);
                if (add_note(partial_tree, &obj_oid, &blob_oid, NULL))
                        die("Failed to add resolved note '%s' to notes tree",
diff --git a/notes.c b/notes.c
index 8f47c202c58752b6d9d56852ae9c315e2f144769..5c62862574399acb486530d3fab5a7f659eb7c2f 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -64,7 +64,7 @@ struct non_note {
 #define CLR_PTR_TYPE(ptr)       ((void *) ((uintptr_t) (ptr) & ~3))
 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
 
-#define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
+#define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
 
 #define KEY_INDEX (GIT_SHA1_RAWSZ - 1)
 #define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1)
@@ -335,31 +335,20 @@ static void note_tree_free(struct int_node *tree)
 }
 
 /*
- * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
- * - hex      - Partial SHA1 segment in ASCII hex format
- * - hex_len  - Length of above segment. Must be multiple of 2 between 0 and 40
- * - sha1     - Partial SHA1 value is written here
- * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
- * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).
- * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
- * Pads sha1 with NULs up to sha1_len (not included in returned length).
+ * Read `len` pairs of hexadecimal digits from `hex` and write the
+ * values to `binary` as `len` bytes. Return 0 on success, or -1 if
+ * the input does not consist of hex digits).
  */
-static int get_oid_hex_segment(const char *hex, unsigned int hex_len,
-               unsigned char *oid, unsigned int oid_len)
+static int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
 {
-       unsigned int i, len = hex_len >> 1;
-       if (hex_len % 2 != 0 || len > oid_len)
-               return -1;
-       for (i = 0; i < len; i++) {
+       for (; len; len--, hex += 2) {
                unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
+
                if (val & ~0xff)
                        return -1;
-               *oid++ = val;
-               hex += 2;
+               *binary++ = val;
        }
-       for (; i < oid_len; i++)
-               *oid++ = 0;
-       return len;
+       return 0;
 }
 
 static int non_note_cmp(const struct non_note *a, const struct non_note *b)
@@ -417,80 +406,90 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
                struct int_node *node, unsigned int n)
 {
        struct object_id object_oid;
-       unsigned int prefix_len;
+       size_t prefix_len;
        void *buf;
        struct tree_desc desc;
        struct name_entry entry;
-       int len, path_len;
-       unsigned char type;
-       struct leaf_node *l;
 
-       buf = fill_tree_descriptor(&desc, subtree->val_oid.hash);
+       buf = fill_tree_descriptor(&desc, &subtree->val_oid);
        if (!buf)
                die("Could not read %s for notes-index",
                     oid_to_hex(&subtree->val_oid));
 
        prefix_len = subtree->key_oid.hash[KEY_INDEX];
-       assert(prefix_len * 2 >= n);
+       if (prefix_len >= GIT_SHA1_RAWSZ)
+               BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
+       if (prefix_len * 2 < n)
+               BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
        memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
        while (tree_entry(&desc, &entry)) {
-               path_len = strlen(entry.path);
-               len = get_oid_hex_segment(entry.path, path_len,
-                               object_oid.hash + prefix_len, GIT_SHA1_RAWSZ - prefix_len);
-               if (len < 0)
-                       goto handle_non_note; /* entry.path is not a SHA1 */
-               len += prefix_len;
+               unsigned char type;
+               struct leaf_node *l;
+               size_t path_len = strlen(entry.path);
+
+               if (path_len == 2 * (GIT_SHA1_RAWSZ - prefix_len)) {
+                       /* This is potentially the remainder of the SHA-1 */
+
+                       if (!S_ISREG(entry.mode))
+                               /* notes must be blobs */
+                               goto handle_non_note;
+
+                       if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
+                                        GIT_SHA1_RAWSZ - prefix_len))
+                               goto handle_non_note; /* entry.path is not a SHA1 */
 
-               /*
-                * If object SHA1 is complete (len == 20), assume note object
-                * If object SHA1 is incomplete (len < 20), and current
-                * component consists of 2 hex chars, assume note subtree
-                */
-               if (len <= GIT_SHA1_RAWSZ) {
                        type = PTR_TYPE_NOTE;
-                       l = (struct leaf_node *)
-                               xcalloc(1, sizeof(struct leaf_node));
-                       oidcpy(&l->key_oid, &object_oid);
-                       oidcpy(&l->val_oid, entry.oid);
-                       if (len < GIT_SHA1_RAWSZ) {
-                               if (!S_ISDIR(entry.mode) || path_len != 2)
-                                       goto handle_non_note; /* not subtree */
-                               l->key_oid.hash[KEY_INDEX] = (unsigned char) len;
-                               type = PTR_TYPE_SUBTREE;
-                       }
-                       if (note_tree_insert(t, node, n, l, type,
-                                            combine_notes_concatenate))
-                               die("Failed to load %s %s into notes tree "
-                                   "from %s",
-                                   type == PTR_TYPE_NOTE ? "note" : "subtree",
-                                   oid_to_hex(&l->key_oid), t->ref);
+               } else if (path_len == 2) {
+                       /* This is potentially an internal node */
+                       size_t len = prefix_len;
+
+                       if (!S_ISDIR(entry.mode))
+                               /* internal nodes must be trees */
+                               goto handle_non_note;
+
+                       if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
+                               goto handle_non_note; /* entry.path is not a SHA1 */
+
+                       /*
+                        * Pad the rest of the SHA-1 with zeros,
+                        * except for the last byte, where we write
+                        * the length:
+                        */
+                       memset(object_oid.hash + len, 0, GIT_SHA1_RAWSZ - len - 1);
+                       object_oid.hash[KEY_INDEX] = (unsigned char)len;
+
+                       type = PTR_TYPE_SUBTREE;
+               } else {
+                       /* This can't be part of a note */
+                       goto handle_non_note;
                }
+
+               l = xcalloc(1, sizeof(*l));
+               oidcpy(&l->key_oid, &object_oid);
+               oidcpy(&l->val_oid, entry.oid);
+               if (note_tree_insert(t, node, n, l, type,
+                                    combine_notes_concatenate))
+                       die("Failed to load %s %s into notes tree "
+                           "from %s",
+                           type == PTR_TYPE_NOTE ? "note" : "subtree",
+                           oid_to_hex(&l->key_oid), t->ref);
+
                continue;
 
 handle_non_note:
                /*
-                * Determine full path for this non-note entry:
-                * The filename is already found in entry.path, but the
-                * directory part of the path must be deduced from the subtree
-                * containing this entry. We assume here that the overall notes
-                * tree follows a strict byte-based progressive fanout
-                * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
-                * e.g. 4/36 fanout). This means that if a non-note is found at
-                * path "dead/beef", the following code will register it as
-                * being found on "de/ad/beef".
-                * On the other hand, if you use such non-obvious non-note
-                * paths in the middle of a notes tree, you deserve what's
-                * coming to you ;). Note that for non-notes that are not
-                * SHA1-like at the top level, there will be no problems.
-                *
-                * To conclude, it is strongly advised to make sure non-notes
-                * have at least one non-hex character in the top-level path
-                * component.
+                * Determine full path for this non-note entry. The
+                * filename is already found in entry.path, but the
+                * directory part of the path must be deduced from the
+                * subtree containing this entry based on our
+                * knowledge that the overall notes tree follows a
+                * strict byte-based progressive fanout structure
+                * (i.e. using 2/38, 2/2/36, etc. fanouts).
                 */
                {
                        struct strbuf non_note_path = STRBUF_INIT;
                        const char *q = oid_to_hex(&subtree->key_oid);
-                       int i;
+                       size_t i;
                        for (i = 0; i < prefix_len; i++) {
                                strbuf_addch(&non_note_path, *q++);
                                strbuf_addch(&non_note_path, *q++);
@@ -1026,7 +1025,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
        t->dirty = 0;
 
        if (flags & NOTES_INIT_EMPTY || !notes_ref ||
-           get_sha1_treeish(notes_ref, object_oid.hash))
+           get_oid_treeish(notes_ref, &object_oid))
                return;
        if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, object_oid.hash))
                die("Cannot use notes ref %s", notes_ref);
index 321d7e9201b2ba6c8b39592a591786409a87edb1..b9a4a0e50172fb169226786b17134c6ab0627333 100644 (file)
--- a/object.c
+++ b/object.c
@@ -353,6 +353,19 @@ static void object_array_release_entry(struct object_array_entry *ent)
        free(ent->path);
 }
 
+struct object *object_array_pop(struct object_array *array)
+{
+       struct object *ret;
+
+       if (!array->nr)
+               return NULL;
+
+       ret = array->objects[array->nr - 1].item;
+       object_array_release_entry(&array->objects[array->nr - 1]);
+       array->nr--;
+       return ret;
+}
+
 void object_array_filter(struct object_array *array,
                         object_array_each_func_t want, void *cb_data)
 {
index 0a419ba8da52453ebbd202f50d5c2aa1c9aea2f0..df8abe96f7da2585f7f07c68f53695a2d450e2ae 100644 (file)
--- a/object.h
+++ b/object.h
@@ -116,6 +116,14 @@ int object_list_contains(struct object_list *list, struct object *obj);
 void add_object_array(struct object *obj, const char *name, struct object_array *array);
 void add_object_array_with_path(struct object *obj, const char *name, struct object_array *array, unsigned mode, const char *path);
 
+/*
+ * Returns NULL if the array is empty. Otherwise, returns the last object
+ * after removing its entry from the array. Other resources associated
+ * with that object are left in an unspecified state and should not be
+ * examined.
+ */
+struct object *object_array_pop(struct object_array *array);
+
 typedef int (*object_array_each_func_t)(struct object_array_entry *, void *);
 
 /*
diff --git a/oidmap.c b/oidmap.c
new file mode 100644 (file)
index 0000000..6db4fff
--- /dev/null
+++ b/oidmap.c
@@ -0,0 +1,51 @@
+#include "cache.h"
+#include "oidmap.h"
+
+static int cmpfn(const void *hashmap_cmp_fn_data,
+                const void *entry, const void *entry_or_key,
+                const void *keydata)
+{
+       const struct oidmap_entry *entry_ = entry;
+       if (keydata)
+               return oidcmp(&entry_->oid, (const struct object_id *) keydata);
+       return oidcmp(&entry_->oid,
+                     &((const struct oidmap_entry *) entry_or_key)->oid);
+}
+
+static int hash(const struct object_id *oid)
+{
+       int hash;
+       memcpy(&hash, oid->hash, sizeof(hash));
+       return hash;
+}
+
+void oidmap_init(struct oidmap *map, size_t initial_size)
+{
+       hashmap_init(&map->map, cmpfn, NULL, initial_size);
+}
+
+void oidmap_free(struct oidmap *map, int free_entries)
+{
+       if (!map)
+               return;
+       hashmap_free(&map->map, free_entries);
+}
+
+void *oidmap_get(const struct oidmap *map, const struct object_id *key)
+{
+       return hashmap_get_from_hash(&map->map, hash(key), key);
+}
+
+void *oidmap_remove(struct oidmap *map, const struct object_id *key)
+{
+       struct hashmap_entry entry;
+       hashmap_entry_init(&entry, hash(key));
+       return hashmap_remove(&map->map, &entry, key);
+}
+
+void *oidmap_put(struct oidmap *map, void *entry)
+{
+       struct oidmap_entry *to_put = entry;
+       hashmap_entry_init(&to_put->internal_entry, hash(&to_put->oid));
+       return hashmap_put(&map->map, to_put);
+}
diff --git a/oidmap.h b/oidmap.h
new file mode 100644 (file)
index 0000000..18f54cd
--- /dev/null
+++ b/oidmap.h
@@ -0,0 +1,68 @@
+#ifndef OIDMAP_H
+#define OIDMAP_H
+
+#include "hashmap.h"
+
+/*
+ * struct oidmap_entry is a structure representing an entry in the hash table,
+ * which must be used as first member of user data structures.
+ *
+ * Users should set the oid field. oidmap_put() will populate the
+ * internal_entry field.
+ */
+struct oidmap_entry {
+       /* For internal use only */
+       struct hashmap_entry internal_entry;
+
+       struct object_id oid;
+};
+
+struct oidmap {
+       struct hashmap map;
+};
+
+#define OIDMAP_INIT { { NULL } }
+
+/*
+ * Initializes an oidmap structure.
+ *
+ * `map` is the oidmap to initialize.
+ *
+ * If the total number of entries is known in advance, the `initial_size`
+ * parameter may be used to preallocate a sufficiently large table and thus
+ * prevent expensive resizing. If 0, the table is dynamically resized.
+ */
+extern void oidmap_init(struct oidmap *map, size_t initial_size);
+
+/*
+ * Frees an oidmap structure and allocated memory.
+ *
+ * If `free_entries` is true, each oidmap_entry in the map is freed as well
+ * using stdlibs free().
+ */
+extern void oidmap_free(struct oidmap *map, int free_entries);
+
+/*
+ * Returns the oidmap entry for the specified oid, or NULL if not found.
+ */
+extern void *oidmap_get(const struct oidmap *map,
+                       const struct object_id *key);
+
+/*
+ * Adds or replaces an oidmap entry.
+ *
+ * ((struct oidmap_entry *) entry)->internal_entry will be populated by this
+ * function.
+ *
+ * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
+ */
+extern void *oidmap_put(struct oidmap *map, void *entry);
+
+/*
+ * Removes an oidmap entry matching the specified oid.
+ *
+ * Returns the removed entry, or NULL if not found.
+ */
+extern void *oidmap_remove(struct oidmap *map, const struct object_id *key);
+
+#endif
index a6a08ba52abdcda75b0bd5cc28349cd3c4be871c..f1f874aaad2c0375eb80d3b714ae6ad460c75ddc 100644 (file)
--- a/oidset.c
+++ b/oidset.c
@@ -1,50 +1,30 @@
 #include "cache.h"
 #include "oidset.h"
 
-struct oidset_entry {
-       struct hashmap_entry hash;
-       struct object_id oid;
-};
-
-static int oidset_hashcmp(const void *unused_cmp_data,
-                         const void *va, const void *vb,
-                         const void *vkey)
-{
-       const struct oidset_entry *a = va, *b = vb;
-       const struct object_id *key = vkey;
-       return oidcmp(&a->oid, key ? key : &b->oid);
-}
-
 int oidset_contains(const struct oidset *set, const struct object_id *oid)
 {
-       struct hashmap_entry key;
-
-       if (!set->map.cmpfn)
+       if (!set->map.map.tablesize)
                return 0;
-
-       hashmap_entry_init(&key, sha1hash(oid->hash));
-       return !!hashmap_get(&set->map, &key, oid);
+       return !!oidmap_get(&set->map, oid);
 }
 
 int oidset_insert(struct oidset *set, const struct object_id *oid)
 {
-       struct oidset_entry *entry;
-
-       if (!set->map.cmpfn)
-               hashmap_init(&set->map, oidset_hashcmp, NULL, 0);
+       struct oidmap_entry *entry;
 
-       if (oidset_contains(set, oid))
+       if (!set->map.map.tablesize)
+               oidmap_init(&set->map, 0);
+       else if (oidset_contains(set, oid))
                return 1;
 
        entry = xmalloc(sizeof(*entry));
-       hashmap_entry_init(&entry->hash, sha1hash(oid->hash));
        oidcpy(&entry->oid, oid);
 
-       hashmap_add(&set->map, entry);
+       oidmap_put(&set->map, entry);
        return 0;
 }
 
 void oidset_clear(struct oidset *set)
 {
-       hashmap_free(&set->map, 1);
+       oidmap_free(&set->map, 1);
 }
index b7eaab5b88f3e6e287384eceb76757e6be9fe015..f4c9e0f9c04e71440aee3c1382297cbf8b1c7827 100644 (file)
--- a/oidset.h
+++ b/oidset.h
@@ -1,6 +1,8 @@
 #ifndef OIDSET_H
 #define OIDSET_H
 
+#include "oidmap.h"
+
 /**
  * This API is similar to sha1-array, in that it maintains a set of object ids
  * in a memory-efficient way. The major differences are:
  * A single oidset; should be zero-initialized (or use OIDSET_INIT).
  */
 struct oidset {
-       struct hashmap map;
+       struct oidmap map;
 };
 
-#define OIDSET_INIT { { NULL } }
+#define OIDSET_INIT { OIDMAP_INIT }
 
 /**
  * Returns true iff `set` contains `oid`.
index 8e47a96b3bb68f8d5ccb87f1c955863aeda39bf6..a8df5ce2ab67bfdb5445e4c02e4ad65032abe9a0 100644 (file)
@@ -297,9 +297,7 @@ void bitmap_writer_build(struct packing_data *to_pack)
 
                        traverse_commit_list(&revs, show_commit, show_object, base);
 
-                       revs.pending.nr = 0;
-                       revs.pending.alloc = 0;
-                       revs.pending.objects = NULL;
+                       object_array_clear(&revs.pending);
 
                        stored->bitmap = bitmap_to_ewah(base);
                        need_reset = 0;
index 327634cd71b5c8fdd5ab531493c2de020bee869d..42e3d5f4f26ee0f31d13c2a3d15bc31da5f7c1d4 100644 (file)
@@ -9,6 +9,7 @@
 #include "pack-bitmap.h"
 #include "pack-revindex.h"
 #include "pack-objects.h"
+#include "packfile.h"
 
 /*
  * An entry on the bitmap index, representing the bitmap for a given
@@ -653,8 +654,6 @@ static int in_bitmapped_pack(struct object_list *roots)
 int prepare_bitmap_walk(struct rev_info *revs)
 {
        unsigned int i;
-       unsigned int pending_nr = revs->pending.nr;
-       struct object_array_entry *pending_e = revs->pending.objects;
 
        struct object_list *wants = NULL;
        struct object_list *haves = NULL;
@@ -669,8 +668,8 @@ int prepare_bitmap_walk(struct rev_info *revs)
                        return -1;
        }
 
-       for (i = 0; i < pending_nr; ++i) {
-               struct object *object = pending_e[i].item;
+       for (i = 0; i < revs->pending.nr; ++i) {
+               struct object *object = revs->pending.objects[i].item;
 
                if (object->type == OBJ_NONE)
                        parse_object_or_die(&object->oid, NULL);
@@ -714,9 +713,7 @@ int prepare_bitmap_walk(struct rev_info *revs)
        if (!bitmap_git.loaded && load_pack_bitmap() < 0)
                return -1;
 
-       revs->pending.nr = 0;
-       revs->pending.alloc = 0;
-       revs->pending.objects = NULL;
+       object_array_clear(&revs->pending);
 
        if (haves) {
                revs->ignore_missing_links = 1;
index e1fcb228fa126e34528e9d5a38b81a6b2eae2f2a..073c1fbd46dfeeb215c209aa0659768e72b81c3b 100644 (file)
@@ -2,6 +2,7 @@
 #include "pack.h"
 #include "pack-revindex.h"
 #include "progress.h"
+#include "packfile.h"
 
 struct idx_entry {
        off_t                offset;
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));
diff --git a/packfile.c b/packfile.c
new file mode 100644 (file)
index 0000000..eab7542
--- /dev/null
@@ -0,0 +1,1901 @@
+#include "cache.h"
+#include "mru.h"
+#include "pack.h"
+#include "dir.h"
+#include "mergesort.h"
+#include "packfile.h"
+#include "delta.h"
+#include "list.h"
+#include "streaming.h"
+#include "sha1-lookup.h"
+
+char *odb_pack_name(struct strbuf *buf,
+                   const unsigned char *sha1,
+                   const char *ext)
+{
+       strbuf_reset(buf);
+       strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
+                   sha1_to_hex(sha1), ext);
+       return buf->buf;
+}
+
+char *sha1_pack_name(const unsigned char *sha1)
+{
+       static struct strbuf buf = STRBUF_INIT;
+       return odb_pack_name(&buf, sha1, "pack");
+}
+
+char *sha1_pack_index_name(const unsigned char *sha1)
+{
+       static struct strbuf buf = STRBUF_INIT;
+       return odb_pack_name(&buf, sha1, "idx");
+}
+
+static unsigned int pack_used_ctr;
+static unsigned int pack_mmap_calls;
+static unsigned int peak_pack_open_windows;
+static unsigned int pack_open_windows;
+static unsigned int pack_open_fds;
+static unsigned int pack_max_fds;
+static size_t peak_pack_mapped;
+static size_t pack_mapped;
+struct packed_git *packed_git;
+struct mru packed_git_mru;
+
+#define SZ_FMT PRIuMAX
+static inline uintmax_t sz_fmt(size_t s) { return s; }
+
+void pack_report(void)
+{
+       fprintf(stderr,
+               "pack_report: getpagesize()            = %10" SZ_FMT "\n"
+               "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
+               "pack_report: core.packedGitLimit      = %10" SZ_FMT "\n",
+               sz_fmt(getpagesize()),
+               sz_fmt(packed_git_window_size),
+               sz_fmt(packed_git_limit));
+       fprintf(stderr,
+               "pack_report: pack_used_ctr            = %10u\n"
+               "pack_report: pack_mmap_calls          = %10u\n"
+               "pack_report: pack_open_windows        = %10u / %10u\n"
+               "pack_report: pack_mapped              = "
+                       "%10" SZ_FMT " / %10" SZ_FMT "\n",
+               pack_used_ctr,
+               pack_mmap_calls,
+               pack_open_windows, peak_pack_open_windows,
+               sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
+}
+
+/*
+ * Open and mmap the index file at path, perform a couple of
+ * consistency checks, then record its information to p.  Return 0 on
+ * success.
+ */
+static int check_packed_git_idx(const char *path, struct packed_git *p)
+{
+       void *idx_map;
+       struct pack_idx_header *hdr;
+       size_t idx_size;
+       uint32_t version, nr, i, *index;
+       int fd = git_open(path);
+       struct stat st;
+
+       if (fd < 0)
+               return -1;
+       if (fstat(fd, &st)) {
+               close(fd);
+               return -1;
+       }
+       idx_size = xsize_t(st.st_size);
+       if (idx_size < 4 * 256 + 20 + 20) {
+               close(fd);
+               return error("index file %s is too small", path);
+       }
+       idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       close(fd);
+
+       hdr = idx_map;
+       if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
+               version = ntohl(hdr->idx_version);
+               if (version < 2 || version > 2) {
+                       munmap(idx_map, idx_size);
+                       return error("index file %s is version %"PRIu32
+                                    " and is not supported by this binary"
+                                    " (try upgrading GIT to a newer version)",
+                                    path, version);
+               }
+       } else
+               version = 1;
+
+       nr = 0;
+       index = idx_map;
+       if (version > 1)
+               index += 2;  /* skip index header */
+       for (i = 0; i < 256; i++) {
+               uint32_t n = ntohl(index[i]);
+               if (n < nr) {
+                       munmap(idx_map, idx_size);
+                       return error("non-monotonic index %s", path);
+               }
+               nr = n;
+       }
+
+       if (version == 1) {
+               /*
+                * Total size:
+                *  - 256 index entries 4 bytes each
+                *  - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
+                *  - 20-byte SHA1 of the packfile
+                *  - 20-byte SHA1 file checksum
+                */
+               if (idx_size != 4*256 + nr * 24 + 20 + 20) {
+                       munmap(idx_map, idx_size);
+                       return error("wrong index v1 file size in %s", path);
+               }
+       } else if (version == 2) {
+               /*
+                * Minimum size:
+                *  - 8 bytes of header
+                *  - 256 index entries 4 bytes each
+                *  - 20-byte sha1 entry * nr
+                *  - 4-byte crc entry * nr
+                *  - 4-byte offset entry * nr
+                *  - 20-byte SHA1 of the packfile
+                *  - 20-byte SHA1 file checksum
+                * And after the 4-byte offset table might be a
+                * variable sized table containing 8-byte entries
+                * for offsets larger than 2^31.
+                */
+               unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
+               unsigned long max_size = min_size;
+               if (nr)
+                       max_size += (nr - 1)*8;
+               if (idx_size < min_size || idx_size > max_size) {
+                       munmap(idx_map, idx_size);
+                       return error("wrong index v2 file size in %s", path);
+               }
+               if (idx_size != min_size &&
+                   /*
+                    * make sure we can deal with large pack offsets.
+                    * 31-bit signed offset won't be enough, neither
+                    * 32-bit unsigned one will be.
+                    */
+                   (sizeof(off_t) <= 4)) {
+                       munmap(idx_map, idx_size);
+                       return error("pack too large for current definition of off_t in %s", path);
+               }
+       }
+
+       p->index_version = version;
+       p->index_data = idx_map;
+       p->index_size = idx_size;
+       p->num_objects = nr;
+       return 0;
+}
+
+int open_pack_index(struct packed_git *p)
+{
+       char *idx_name;
+       size_t len;
+       int ret;
+
+       if (p->index_data)
+               return 0;
+
+       if (!strip_suffix(p->pack_name, ".pack", &len))
+               die("BUG: pack_name does not end in .pack");
+       idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
+       ret = check_packed_git_idx(idx_name, p);
+       free(idx_name);
+       return ret;
+}
+
+static struct packed_git *alloc_packed_git(int extra)
+{
+       struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
+       memset(p, 0, sizeof(*p));
+       p->pack_fd = -1;
+       return p;
+}
+
+struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
+{
+       const char *path = sha1_pack_name(sha1);
+       size_t alloc = st_add(strlen(path), 1);
+       struct packed_git *p = alloc_packed_git(alloc);
+
+       memcpy(p->pack_name, path, alloc); /* includes NUL */
+       hashcpy(p->sha1, sha1);
+       if (check_packed_git_idx(idx_path, p)) {
+               free(p);
+               return NULL;
+       }
+
+       return p;
+}
+
+static void scan_windows(struct packed_git *p,
+       struct packed_git **lru_p,
+       struct pack_window **lru_w,
+       struct pack_window **lru_l)
+{
+       struct pack_window *w, *w_l;
+
+       for (w_l = NULL, w = p->windows; w; w = w->next) {
+               if (!w->inuse_cnt) {
+                       if (!*lru_w || w->last_used < (*lru_w)->last_used) {
+                               *lru_p = p;
+                               *lru_w = w;
+                               *lru_l = w_l;
+                       }
+               }
+               w_l = w;
+       }
+}
+
+static int unuse_one_window(struct packed_git *current)
+{
+       struct packed_git *p, *lru_p = NULL;
+       struct pack_window *lru_w = NULL, *lru_l = NULL;
+
+       if (current)
+               scan_windows(current, &lru_p, &lru_w, &lru_l);
+       for (p = packed_git; p; p = p->next)
+               scan_windows(p, &lru_p, &lru_w, &lru_l);
+       if (lru_p) {
+               munmap(lru_w->base, lru_w->len);
+               pack_mapped -= lru_w->len;
+               if (lru_l)
+                       lru_l->next = lru_w->next;
+               else
+                       lru_p->windows = lru_w->next;
+               free(lru_w);
+               pack_open_windows--;
+               return 1;
+       }
+       return 0;
+}
+
+void release_pack_memory(size_t need)
+{
+       size_t cur = pack_mapped;
+       while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
+               ; /* nothing */
+}
+
+void close_pack_windows(struct packed_git *p)
+{
+       while (p->windows) {
+               struct pack_window *w = p->windows;
+
+               if (w->inuse_cnt)
+                       die("pack '%s' still has open windows to it",
+                           p->pack_name);
+               munmap(w->base, w->len);
+               pack_mapped -= w->len;
+               pack_open_windows--;
+               p->windows = w->next;
+               free(w);
+       }
+}
+
+static int close_pack_fd(struct packed_git *p)
+{
+       if (p->pack_fd < 0)
+               return 0;
+
+       close(p->pack_fd);
+       pack_open_fds--;
+       p->pack_fd = -1;
+
+       return 1;
+}
+
+void close_pack_index(struct packed_git *p)
+{
+       if (p->index_data) {
+               munmap((void *)p->index_data, p->index_size);
+               p->index_data = NULL;
+       }
+}
+
+static void close_pack(struct packed_git *p)
+{
+       close_pack_windows(p);
+       close_pack_fd(p);
+       close_pack_index(p);
+}
+
+void close_all_packs(void)
+{
+       struct packed_git *p;
+
+       for (p = packed_git; p; p = p->next)
+               if (p->do_not_close)
+                       die("BUG: want to close pack marked 'do-not-close'");
+               else
+                       close_pack(p);
+}
+
+/*
+ * The LRU pack is the one with the oldest MRU window, preferring packs
+ * with no used windows, or the oldest mtime if it has no windows allocated.
+ */
+static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
+{
+       struct pack_window *w, *this_mru_w;
+       int has_windows_inuse = 0;
+
+       /*
+        * Reject this pack if it has windows and the previously selected
+        * one does not.  If this pack does not have windows, reject
+        * it if the pack file is newer than the previously selected one.
+        */
+       if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
+               return;
+
+       for (w = this_mru_w = p->windows; w; w = w->next) {
+               /*
+                * Reject this pack if any of its windows are in use,
+                * but the previously selected pack did not have any
+                * inuse windows.  Otherwise, record that this pack
+                * has windows in use.
+                */
+               if (w->inuse_cnt) {
+                       if (*accept_windows_inuse)
+                               has_windows_inuse = 1;
+                       else
+                               return;
+               }
+
+               if (w->last_used > this_mru_w->last_used)
+                       this_mru_w = w;
+
+               /*
+                * Reject this pack if it has windows that have been
+                * used more recently than the previously selected pack.
+                * If the previously selected pack had windows inuse and
+                * we have not encountered a window in this pack that is
+                * inuse, skip this check since we prefer a pack with no
+                * inuse windows to one that has inuse windows.
+                */
+               if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
+                   this_mru_w->last_used > (*mru_w)->last_used)
+                       return;
+       }
+
+       /*
+        * Select this pack.
+        */
+       *mru_w = this_mru_w;
+       *lru_p = p;
+       *accept_windows_inuse = has_windows_inuse;
+}
+
+static int close_one_pack(void)
+{
+       struct packed_git *p, *lru_p = NULL;
+       struct pack_window *mru_w = NULL;
+       int accept_windows_inuse = 1;
+
+       for (p = packed_git; p; p = p->next) {
+               if (p->pack_fd == -1)
+                       continue;
+               find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
+       }
+
+       if (lru_p)
+               return close_pack_fd(lru_p);
+
+       return 0;
+}
+
+static unsigned int get_max_fd_limit(void)
+{
+#ifdef RLIMIT_NOFILE
+       {
+               struct rlimit lim;
+
+               if (!getrlimit(RLIMIT_NOFILE, &lim))
+                       return lim.rlim_cur;
+       }
+#endif
+
+#ifdef _SC_OPEN_MAX
+       {
+               long open_max = sysconf(_SC_OPEN_MAX);
+               if (0 < open_max)
+                       return open_max;
+               /*
+                * Otherwise, we got -1 for one of the two
+                * reasons:
+                *
+                * (1) sysconf() did not understand _SC_OPEN_MAX
+                *     and signaled an error with -1; or
+                * (2) sysconf() said there is no limit.
+                *
+                * We _could_ clear errno before calling sysconf() to
+                * tell these two cases apart and return a huge number
+                * in the latter case to let the caller cap it to a
+                * value that is not so selfish, but letting the
+                * fallback OPEN_MAX codepath take care of these cases
+                * is a lot simpler.
+                */
+       }
+#endif
+
+#ifdef OPEN_MAX
+       return OPEN_MAX;
+#else
+       return 1; /* see the caller ;-) */
+#endif
+}
+
+/*
+ * Do not call this directly as this leaks p->pack_fd on error return;
+ * call open_packed_git() instead.
+ */
+static int open_packed_git_1(struct packed_git *p)
+{
+       struct stat st;
+       struct pack_header hdr;
+       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);
+
+       if (!pack_max_fds) {
+               unsigned int max_fds = get_max_fd_limit();
+
+               /* Save 3 for stdin/stdout/stderr, 22 for work */
+               if (25 < max_fds)
+                       pack_max_fds = max_fds - 25;
+               else
+                       pack_max_fds = 1;
+       }
+
+       while (pack_max_fds <= pack_open_fds && close_one_pack())
+               ; /* nothing */
+
+       p->pack_fd = git_open(p->pack_name);
+       if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
+               return -1;
+       pack_open_fds++;
+
+       /* If we created the struct before we had the pack we lack size. */
+       if (!p->pack_size) {
+               if (!S_ISREG(st.st_mode))
+                       return error("packfile %s not a regular file", p->pack_name);
+               p->pack_size = st.st_size;
+       } else if (p->pack_size != st.st_size)
+               return error("packfile %s size changed", p->pack_name);
+
+       /* We leave these file descriptors open with sliding mmap;
+        * there is no point keeping them open across exec(), though.
+        */
+       fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
+       if (fd_flag < 0)
+               return error("cannot determine file descriptor flags");
+       fd_flag |= FD_CLOEXEC;
+       if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
+               return error("cannot set FD_CLOEXEC");
+
+       /* Verify we recognize this pack file format. */
+       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);
+       if (!pack_version_ok(hdr.hdr_version))
+               return error("packfile %s is version %"PRIu32" and not"
+                       " supported (try upgrading GIT to a newer version)",
+                       p->pack_name, ntohl(hdr.hdr_version));
+
+       /* Verify the pack matches its index. */
+       if (p->num_objects != ntohl(hdr.hdr_entries))
+               return error("packfile %s claims to have %"PRIu32" objects"
+                            " while index indicates %"PRIu32" objects",
+                            p->pack_name, ntohl(hdr.hdr_entries),
+                            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);
+       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))
+               return error("packfile %s does not match index", p->pack_name);
+       return 0;
+}
+
+static int open_packed_git(struct packed_git *p)
+{
+       if (!open_packed_git_1(p))
+               return 0;
+       close_pack_fd(p);
+       return -1;
+}
+
+static int in_window(struct pack_window *win, off_t offset)
+{
+       /* We must promise at least 20 bytes (one hash) after the
+        * offset is available from this window, otherwise the offset
+        * is not actually in this window and a different window (which
+        * has that one hash excess) must be used.  This is to support
+        * the object header and delta base parsing routines below.
+        */
+       off_t win_off = win->offset;
+       return win_off <= offset
+               && (offset + 20) <= (win_off + win->len);
+}
+
+unsigned char *use_pack(struct packed_git *p,
+               struct pack_window **w_cursor,
+               off_t offset,
+               unsigned long *left)
+{
+       struct pack_window *win = *w_cursor;
+
+       /* Since packfiles end in a hash of their content and it's
+        * pointless to ask for an offset into the middle of that
+        * hash, and the in_window function above wouldn't match
+        * don't allow an offset too close to the end of the file.
+        */
+       if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
+               die("packfile %s cannot be accessed", p->pack_name);
+       if (offset > (p->pack_size - 20))
+               die("offset beyond end of packfile (truncated pack?)");
+       if (offset < 0)
+               die(_("offset before end of packfile (broken .idx?)"));
+
+       if (!win || !in_window(win, offset)) {
+               if (win)
+                       win->inuse_cnt--;
+               for (win = p->windows; win; win = win->next) {
+                       if (in_window(win, offset))
+                               break;
+               }
+               if (!win) {
+                       size_t window_align = packed_git_window_size / 2;
+                       off_t len;
+
+                       if (p->pack_fd == -1 && open_packed_git(p))
+                               die("packfile %s cannot be accessed", p->pack_name);
+
+                       win = xcalloc(1, sizeof(*win));
+                       win->offset = (offset / window_align) * window_align;
+                       len = p->pack_size - win->offset;
+                       if (len > packed_git_window_size)
+                               len = packed_git_window_size;
+                       win->len = (size_t)len;
+                       pack_mapped += win->len;
+                       while (packed_git_limit < pack_mapped
+                               && unuse_one_window(p))
+                               ; /* nothing */
+                       win->base = xmmap(NULL, win->len,
+                               PROT_READ, MAP_PRIVATE,
+                               p->pack_fd, win->offset);
+                       if (win->base == MAP_FAILED)
+                               die_errno("packfile %s cannot be mapped",
+                                         p->pack_name);
+                       if (!win->offset && win->len == p->pack_size
+                               && !p->do_not_close)
+                               close_pack_fd(p);
+                       pack_mmap_calls++;
+                       pack_open_windows++;
+                       if (pack_mapped > peak_pack_mapped)
+                               peak_pack_mapped = pack_mapped;
+                       if (pack_open_windows > peak_pack_open_windows)
+                               peak_pack_open_windows = pack_open_windows;
+                       win->next = p->windows;
+                       p->windows = win;
+               }
+       }
+       if (win != *w_cursor) {
+               win->last_used = pack_used_ctr++;
+               win->inuse_cnt++;
+               *w_cursor = win;
+       }
+       offset -= win->offset;
+       if (left)
+               *left = win->len - xsize_t(offset);
+       return win->base + offset;
+}
+
+void unuse_pack(struct pack_window **w_cursor)
+{
+       struct pack_window *w = *w_cursor;
+       if (w) {
+               w->inuse_cnt--;
+               *w_cursor = NULL;
+       }
+}
+
+static void try_to_free_pack_memory(size_t size)
+{
+       release_pack_memory(size);
+}
+
+struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
+{
+       static int have_set_try_to_free_routine;
+       struct stat st;
+       size_t alloc;
+       struct packed_git *p;
+
+       if (!have_set_try_to_free_routine) {
+               have_set_try_to_free_routine = 1;
+               set_try_to_free_routine(try_to_free_pack_memory);
+       }
+
+       /*
+        * Make sure a corresponding .pack file exists and that
+        * the index looks sane.
+        */
+       if (!strip_suffix_mem(path, &path_len, ".idx"))
+               return NULL;
+
+       /*
+        * ".pack" is long enough to hold any suffix we're adding (and
+        * the use xsnprintf double-checks that)
+        */
+       alloc = st_add3(path_len, strlen(".pack"), 1);
+       p = alloc_packed_git(alloc);
+       memcpy(p->pack_name, path, path_len);
+
+       xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
+       if (!access(p->pack_name, F_OK))
+               p->pack_keep = 1;
+
+       xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
+       if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
+               free(p);
+               return NULL;
+       }
+
+       /* ok, it looks sane as far as we can check without
+        * actually mapping the pack file.
+        */
+       p->pack_size = st.st_size;
+       p->pack_local = local;
+       p->mtime = st.st_mtime;
+       if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
+               hashclr(p->sha1);
+       return p;
+}
+
+void install_packed_git(struct packed_git *pack)
+{
+       if (pack->pack_fd != -1)
+               pack_open_fds++;
+
+       pack->next = packed_git;
+       packed_git = pack;
+}
+
+void (*report_garbage)(unsigned seen_bits, const char *path);
+
+static void report_helper(const struct string_list *list,
+                         int seen_bits, int first, int last)
+{
+       if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
+               return;
+
+       for (; first < last; first++)
+               report_garbage(seen_bits, list->items[first].string);
+}
+
+static void report_pack_garbage(struct string_list *list)
+{
+       int i, baselen = -1, first = 0, seen_bits = 0;
+
+       if (!report_garbage)
+               return;
+
+       string_list_sort(list);
+
+       for (i = 0; i < list->nr; i++) {
+               const char *path = list->items[i].string;
+               if (baselen != -1 &&
+                   strncmp(path, list->items[first].string, baselen)) {
+                       report_helper(list, seen_bits, first, i);
+                       baselen = -1;
+                       seen_bits = 0;
+               }
+               if (baselen == -1) {
+                       const char *dot = strrchr(path, '.');
+                       if (!dot) {
+                               report_garbage(PACKDIR_FILE_GARBAGE, path);
+                               continue;
+                       }
+                       baselen = dot - path + 1;
+                       first = i;
+               }
+               if (!strcmp(path + baselen, "pack"))
+                       seen_bits |= 1;
+               else if (!strcmp(path + baselen, "idx"))
+                       seen_bits |= 2;
+       }
+       report_helper(list, seen_bits, first, list->nr);
+}
+
+static void prepare_packed_git_one(char *objdir, int local)
+{
+       struct strbuf path = STRBUF_INIT;
+       size_t dirnamelen;
+       DIR *dir;
+       struct dirent *de;
+       struct string_list garbage = STRING_LIST_INIT_DUP;
+
+       strbuf_addstr(&path, objdir);
+       strbuf_addstr(&path, "/pack");
+       dir = opendir(path.buf);
+       if (!dir) {
+               if (errno != ENOENT)
+                       error_errno("unable to open object pack directory: %s",
+                                   path.buf);
+               strbuf_release(&path);
+               return;
+       }
+       strbuf_addch(&path, '/');
+       dirnamelen = path.len;
+       while ((de = readdir(dir)) != NULL) {
+               struct packed_git *p;
+               size_t base_len;
+
+               if (is_dot_or_dotdot(de->d_name))
+                       continue;
+
+               strbuf_setlen(&path, dirnamelen);
+               strbuf_addstr(&path, de->d_name);
+
+               base_len = path.len;
+               if (strip_suffix_mem(path.buf, &base_len, ".idx")) {
+                       /* Don't reopen a pack we already have. */
+                       for (p = packed_git; p; p = p->next) {
+                               size_t len;
+                               if (strip_suffix(p->pack_name, ".pack", &len) &&
+                                   len == base_len &&
+                                   !memcmp(p->pack_name, path.buf, len))
+                                       break;
+                       }
+                       if (p == NULL &&
+                           /*
+                            * See if it really is a valid .idx file with
+                            * corresponding .pack file that we can map.
+                            */
+                           (p = add_packed_git(path.buf, path.len, local)) != NULL)
+                               install_packed_git(p);
+               }
+
+               if (!report_garbage)
+                       continue;
+
+               if (ends_with(de->d_name, ".idx") ||
+                   ends_with(de->d_name, ".pack") ||
+                   ends_with(de->d_name, ".bitmap") ||
+                   ends_with(de->d_name, ".keep"))
+                       string_list_append(&garbage, path.buf);
+               else
+                       report_garbage(PACKDIR_FILE_GARBAGE, path.buf);
+       }
+       closedir(dir);
+       report_pack_garbage(&garbage);
+       string_list_clear(&garbage, 0);
+       strbuf_release(&path);
+}
+
+static int approximate_object_count_valid;
+
+/*
+ * Give a fast, rough count of the number of objects in the repository. This
+ * ignores loose objects completely. If you have a lot of them, then either
+ * you should repack because your performance will be awful, or they are
+ * all unreachable objects about to be pruned, in which case they're not really
+ * interesting as a measure of repo size in the first place.
+ */
+unsigned long approximate_object_count(void)
+{
+       static unsigned long count;
+       if (!approximate_object_count_valid) {
+               struct packed_git *p;
+
+               prepare_packed_git();
+               count = 0;
+               for (p = packed_git; p; p = p->next) {
+                       if (open_pack_index(p))
+                               continue;
+                       count += p->num_objects;
+               }
+       }
+       return count;
+}
+
+static void *get_next_packed_git(const void *p)
+{
+       return ((const struct packed_git *)p)->next;
+}
+
+static void set_next_packed_git(void *p, void *next)
+{
+       ((struct packed_git *)p)->next = next;
+}
+
+static int sort_pack(const void *a_, const void *b_)
+{
+       const struct packed_git *a = a_;
+       const struct packed_git *b = b_;
+       int st;
+
+       /*
+        * Local packs tend to contain objects specific to our
+        * variant of the project than remote ones.  In addition,
+        * remote ones could be on a network mounted filesystem.
+        * Favor local ones for these reasons.
+        */
+       st = a->pack_local - b->pack_local;
+       if (st)
+               return -st;
+
+       /*
+        * Younger packs tend to contain more recent objects,
+        * and more recent objects tend to get accessed more
+        * often.
+        */
+       if (a->mtime < b->mtime)
+               return 1;
+       else if (a->mtime == b->mtime)
+               return 0;
+       return -1;
+}
+
+static void rearrange_packed_git(void)
+{
+       packed_git = llist_mergesort(packed_git, get_next_packed_git,
+                                    set_next_packed_git, sort_pack);
+}
+
+static void prepare_packed_git_mru(void)
+{
+       struct packed_git *p;
+
+       mru_clear(&packed_git_mru);
+       for (p = packed_git; p; p = p->next)
+               mru_append(&packed_git_mru, p);
+}
+
+static int prepare_packed_git_run_once = 0;
+void prepare_packed_git(void)
+{
+       struct alternate_object_database *alt;
+
+       if (prepare_packed_git_run_once)
+               return;
+       prepare_packed_git_one(get_object_directory(), 1);
+       prepare_alt_odb();
+       for (alt = alt_odb_list; alt; alt = alt->next)
+               prepare_packed_git_one(alt->path, 0);
+       rearrange_packed_git();
+       prepare_packed_git_mru();
+       prepare_packed_git_run_once = 1;
+}
+
+void reprepare_packed_git(void)
+{
+       approximate_object_count_valid = 0;
+       prepare_packed_git_run_once = 0;
+       prepare_packed_git();
+}
+
+unsigned long unpack_object_header_buffer(const unsigned char *buf,
+               unsigned long len, enum object_type *type, unsigned long *sizep)
+{
+       unsigned shift;
+       unsigned long size, c;
+       unsigned long used = 0;
+
+       c = buf[used++];
+       *type = (c >> 4) & 7;
+       size = c & 15;
+       shift = 4;
+       while (c & 0x80) {
+               if (len <= used || bitsizeof(long) <= shift) {
+                       error("bad object header");
+                       size = used = 0;
+                       break;
+               }
+               c = buf[used++];
+               size += (c & 0x7f) << shift;
+               shift += 7;
+       }
+       *sizep = size;
+       return used;
+}
+
+unsigned long get_size_from_delta(struct packed_git *p,
+                                 struct pack_window **w_curs,
+                                 off_t curpos)
+{
+       const unsigned char *data;
+       unsigned char delta_head[20], *in;
+       git_zstream stream;
+       int st;
+
+       memset(&stream, 0, sizeof(stream));
+       stream.next_out = delta_head;
+       stream.avail_out = sizeof(delta_head);
+
+       git_inflate_init(&stream);
+       do {
+               in = use_pack(p, w_curs, curpos, &stream.avail_in);
+               stream.next_in = in;
+               st = git_inflate(&stream, Z_FINISH);
+               curpos += stream.next_in - in;
+       } while ((st == Z_OK || st == Z_BUF_ERROR) &&
+                stream.total_out < sizeof(delta_head));
+       git_inflate_end(&stream);
+       if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
+               error("delta data unpack-initial failed");
+               return 0;
+       }
+
+       /* Examine the initial part of the delta to figure out
+        * the result size.
+        */
+       data = delta_head;
+
+       /* ignore base size */
+       get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
+
+       /* Read the result size */
+       return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
+}
+
+int unpack_object_header(struct packed_git *p,
+                        struct pack_window **w_curs,
+                        off_t *curpos,
+                        unsigned long *sizep)
+{
+       unsigned char *base;
+       unsigned long left;
+       unsigned long used;
+       enum object_type type;
+
+       /* use_pack() assures us we have [base, base + 20) available
+        * as a range that we can look at.  (Its actually the hash
+        * size that is assured.)  With our object header encoding
+        * the maximum deflated object size is 2^137, which is just
+        * insane, so we know won't exceed what we have been given.
+        */
+       base = use_pack(p, w_curs, *curpos, &left);
+       used = unpack_object_header_buffer(base, left, &type, sizep);
+       if (!used) {
+               type = OBJ_BAD;
+       } else
+               *curpos += used;
+
+       return type;
+}
+
+void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1)
+{
+       unsigned i;
+       for (i = 0; i < p->num_bad_objects; i++)
+               if (!hashcmp(sha1, p->bad_object_sha1 + GIT_SHA1_RAWSZ * i))
+                       return;
+       p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
+                                     st_mult(GIT_MAX_RAWSZ,
+                                             st_add(p->num_bad_objects, 1)));
+       hashcpy(p->bad_object_sha1 + GIT_SHA1_RAWSZ * p->num_bad_objects, sha1);
+       p->num_bad_objects++;
+}
+
+const struct packed_git *has_packed_and_bad(const unsigned char *sha1)
+{
+       struct packed_git *p;
+       unsigned i;
+
+       for (p = packed_git; p; p = p->next)
+               for (i = 0; i < p->num_bad_objects; i++)
+                       if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
+                               return p;
+       return NULL;
+}
+
+static off_t get_delta_base(struct packed_git *p,
+                                   struct pack_window **w_curs,
+                                   off_t *curpos,
+                                   enum object_type type,
+                                   off_t delta_obj_offset)
+{
+       unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
+       off_t base_offset;
+
+       /* use_pack() assured us we have [base_info, base_info + 20)
+        * as a range that we can look at without walking off the
+        * end of the mapped window.  Its actually the hash size
+        * that is assured.  An OFS_DELTA longer than the hash size
+        * is stupid, as then a REF_DELTA would be smaller to store.
+        */
+       if (type == OBJ_OFS_DELTA) {
+               unsigned used = 0;
+               unsigned char c = base_info[used++];
+               base_offset = c & 127;
+               while (c & 128) {
+                       base_offset += 1;
+                       if (!base_offset || MSB(base_offset, 7))
+                               return 0;  /* overflow */
+                       c = base_info[used++];
+                       base_offset = (base_offset << 7) + (c & 127);
+               }
+               base_offset = delta_obj_offset - base_offset;
+               if (base_offset <= 0 || base_offset >= delta_obj_offset)
+                       return 0;  /* out of bound */
+               *curpos += used;
+       } else if (type == OBJ_REF_DELTA) {
+               /* The base entry _must_ be in the same pack */
+               base_offset = find_pack_entry_one(base_info, p);
+               *curpos += 20;
+       } else
+               die("I am totally screwed");
+       return base_offset;
+}
+
+/*
+ * Like get_delta_base above, but we return the sha1 instead of the pack
+ * offset. This means it is cheaper for REF deltas (we do not have to do
+ * the final object lookup), but more expensive for OFS deltas (we
+ * have to load the revidx to convert the offset back into a sha1).
+ */
+static const unsigned char *get_delta_base_sha1(struct packed_git *p,
+                                               struct pack_window **w_curs,
+                                               off_t curpos,
+                                               enum object_type type,
+                                               off_t delta_obj_offset)
+{
+       if (type == OBJ_REF_DELTA) {
+               unsigned char *base = use_pack(p, w_curs, curpos, NULL);
+               return base;
+       } else if (type == OBJ_OFS_DELTA) {
+               struct revindex_entry *revidx;
+               off_t base_offset = get_delta_base(p, w_curs, &curpos,
+                                                  type, delta_obj_offset);
+
+               if (!base_offset)
+                       return NULL;
+
+               revidx = find_pack_revindex(p, base_offset);
+               if (!revidx)
+                       return NULL;
+
+               return nth_packed_object_sha1(p, revidx->nr);
+       } else
+               return NULL;
+}
+
+static int retry_bad_packed_offset(struct packed_git *p, off_t obj_offset)
+{
+       int type;
+       struct revindex_entry *revidx;
+       const unsigned char *sha1;
+       revidx = find_pack_revindex(p, obj_offset);
+       if (!revidx)
+               return OBJ_BAD;
+       sha1 = nth_packed_object_sha1(p, revidx->nr);
+       mark_bad_packed_object(p, sha1);
+       type = sha1_object_info(sha1, NULL);
+       if (type <= OBJ_NONE)
+               return OBJ_BAD;
+       return type;
+}
+
+#define POI_STACK_PREALLOC 64
+
+static enum object_type packed_to_object_type(struct packed_git *p,
+                                             off_t obj_offset,
+                                             enum object_type type,
+                                             struct pack_window **w_curs,
+                                             off_t curpos)
+{
+       off_t small_poi_stack[POI_STACK_PREALLOC];
+       off_t *poi_stack = small_poi_stack;
+       int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
+
+       while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
+               off_t base_offset;
+               unsigned long size;
+               /* Push the object we're going to leave behind */
+               if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
+                       poi_stack_alloc = alloc_nr(poi_stack_nr);
+                       ALLOC_ARRAY(poi_stack, poi_stack_alloc);
+                       memcpy(poi_stack, small_poi_stack, sizeof(off_t)*poi_stack_nr);
+               } else {
+                       ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
+               }
+               poi_stack[poi_stack_nr++] = obj_offset;
+               /* If parsing the base offset fails, just unwind */
+               base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
+               if (!base_offset)
+                       goto unwind;
+               curpos = obj_offset = base_offset;
+               type = unpack_object_header(p, w_curs, &curpos, &size);
+               if (type <= OBJ_NONE) {
+                       /* If getting the base itself fails, we first
+                        * retry the base, otherwise unwind */
+                       type = retry_bad_packed_offset(p, base_offset);
+                       if (type > OBJ_NONE)
+                               goto out;
+                       goto unwind;
+               }
+       }
+
+       switch (type) {
+       case OBJ_BAD:
+       case OBJ_COMMIT:
+       case OBJ_TREE:
+       case OBJ_BLOB:
+       case OBJ_TAG:
+               break;
+       default:
+               error("unknown object type %i at offset %"PRIuMAX" in %s",
+                     type, (uintmax_t)obj_offset, p->pack_name);
+               type = OBJ_BAD;
+       }
+
+out:
+       if (poi_stack != small_poi_stack)
+               free(poi_stack);
+       return type;
+
+unwind:
+       while (poi_stack_nr) {
+               obj_offset = poi_stack[--poi_stack_nr];
+               type = retry_bad_packed_offset(p, obj_offset);
+               if (type > OBJ_NONE)
+                       goto out;
+       }
+       type = OBJ_BAD;
+       goto out;
+}
+
+static struct hashmap delta_base_cache;
+static size_t delta_base_cached;
+
+static LIST_HEAD(delta_base_cache_lru);
+
+struct delta_base_cache_key {
+       struct packed_git *p;
+       off_t base_offset;
+};
+
+struct delta_base_cache_entry {
+       struct hashmap hash;
+       struct delta_base_cache_key key;
+       struct list_head lru;
+       void *data;
+       unsigned long size;
+       enum object_type type;
+};
+
+static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
+{
+       unsigned int hash;
+
+       hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
+       hash += (hash >> 8) + (hash >> 16);
+       return hash;
+}
+
+static struct delta_base_cache_entry *
+get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
+{
+       struct hashmap_entry entry;
+       struct delta_base_cache_key key;
+
+       if (!delta_base_cache.cmpfn)
+               return NULL;
+
+       hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
+       key.p = p;
+       key.base_offset = base_offset;
+       return hashmap_get(&delta_base_cache, &entry, &key);
+}
+
+static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
+                                  const struct delta_base_cache_key *b)
+{
+       return a->p == b->p && a->base_offset == b->base_offset;
+}
+
+static int delta_base_cache_hash_cmp(const void *unused_cmp_data,
+                                    const void *va, const void *vb,
+                                    const void *vkey)
+{
+       const struct delta_base_cache_entry *a = va, *b = vb;
+       const struct delta_base_cache_key *key = vkey;
+       if (key)
+               return !delta_base_cache_key_eq(&a->key, key);
+       else
+               return !delta_base_cache_key_eq(&a->key, &b->key);
+}
+
+static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
+{
+       return !!get_delta_base_cache_entry(p, base_offset);
+}
+
+/*
+ * Remove the entry from the cache, but do _not_ free the associated
+ * entry data. The caller takes ownership of the "data" buffer, and
+ * should copy out any fields it wants before detaching.
+ */
+static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
+{
+       hashmap_remove(&delta_base_cache, ent, &ent->key);
+       list_del(&ent->lru);
+       delta_base_cached -= ent->size;
+       free(ent);
+}
+
+static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
+       unsigned long *base_size, enum object_type *type)
+{
+       struct delta_base_cache_entry *ent;
+
+       ent = get_delta_base_cache_entry(p, base_offset);
+       if (!ent)
+               return unpack_entry(p, base_offset, type, base_size);
+
+       if (type)
+               *type = ent->type;
+       if (base_size)
+               *base_size = ent->size;
+       return xmemdupz(ent->data, ent->size);
+}
+
+static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
+{
+       free(ent->data);
+       detach_delta_base_cache_entry(ent);
+}
+
+void clear_delta_base_cache(void)
+{
+       struct list_head *lru, *tmp;
+       list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
+               struct delta_base_cache_entry *entry =
+                       list_entry(lru, struct delta_base_cache_entry, lru);
+               release_delta_base_cache(entry);
+       }
+}
+
+static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
+       void *base, unsigned long base_size, enum object_type type)
+{
+       struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
+       struct list_head *lru, *tmp;
+
+       delta_base_cached += base_size;
+
+       list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
+               struct delta_base_cache_entry *f =
+                       list_entry(lru, struct delta_base_cache_entry, lru);
+               if (delta_base_cached <= delta_base_cache_limit)
+                       break;
+               release_delta_base_cache(f);
+       }
+
+       ent->key.p = p;
+       ent->key.base_offset = base_offset;
+       ent->type = type;
+       ent->data = base;
+       ent->size = base_size;
+       list_add_tail(&ent->lru, &delta_base_cache_lru);
+
+       if (!delta_base_cache.cmpfn)
+               hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
+       hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
+       hashmap_add(&delta_base_cache, ent);
+}
+
+int packed_object_info(struct packed_git *p, off_t obj_offset,
+                      struct object_info *oi)
+{
+       struct pack_window *w_curs = NULL;
+       unsigned long size;
+       off_t curpos = obj_offset;
+       enum object_type type;
+
+       /*
+        * We always get the representation type, but only convert it to
+        * a "real" type later if the caller is interested.
+        */
+       if (oi->contentp) {
+               *oi->contentp = cache_or_unpack_entry(p, obj_offset, oi->sizep,
+                                                     &type);
+               if (!*oi->contentp)
+                       type = OBJ_BAD;
+       } else {
+               type = unpack_object_header(p, &w_curs, &curpos, &size);
+       }
+
+       if (!oi->contentp && oi->sizep) {
+               if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
+                       off_t tmp_pos = curpos;
+                       off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
+                                                          type, obj_offset);
+                       if (!base_offset) {
+                               type = OBJ_BAD;
+                               goto out;
+                       }
+                       *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos);
+                       if (*oi->sizep == 0) {
+                               type = OBJ_BAD;
+                               goto out;
+                       }
+               } else {
+                       *oi->sizep = size;
+               }
+       }
+
+       if (oi->disk_sizep) {
+               struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
+               *oi->disk_sizep = revidx[1].offset - obj_offset;
+       }
+
+       if (oi->typep || oi->typename) {
+               enum object_type ptot;
+               ptot = packed_to_object_type(p, obj_offset, type, &w_curs,
+                                            curpos);
+               if (oi->typep)
+                       *oi->typep = ptot;
+               if (oi->typename) {
+                       const char *tn = typename(ptot);
+                       if (tn)
+                               strbuf_addstr(oi->typename, tn);
+               }
+               if (ptot < 0) {
+                       type = OBJ_BAD;
+                       goto out;
+               }
+       }
+
+       if (oi->delta_base_sha1) {
+               if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
+                       const unsigned char *base;
+
+                       base = get_delta_base_sha1(p, &w_curs, curpos,
+                                                  type, obj_offset);
+                       if (!base) {
+                               type = OBJ_BAD;
+                               goto out;
+                       }
+
+                       hashcpy(oi->delta_base_sha1, base);
+               } else
+                       hashclr(oi->delta_base_sha1);
+       }
+
+       oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED :
+                                                         OI_PACKED;
+
+out:
+       unuse_pack(&w_curs);
+       return type;
+}
+
+static void *unpack_compressed_entry(struct packed_git *p,
+                                   struct pack_window **w_curs,
+                                   off_t curpos,
+                                   unsigned long size)
+{
+       int st;
+       git_zstream stream;
+       unsigned char *buffer, *in;
+
+       buffer = xmallocz_gently(size);
+       if (!buffer)
+               return NULL;
+       memset(&stream, 0, sizeof(stream));
+       stream.next_out = buffer;
+       stream.avail_out = size + 1;
+
+       git_inflate_init(&stream);
+       do {
+               in = use_pack(p, w_curs, curpos, &stream.avail_in);
+               stream.next_in = in;
+               st = git_inflate(&stream, Z_FINISH);
+               if (!stream.avail_out)
+                       break; /* the payload is larger than it should be */
+               curpos += stream.next_in - in;
+       } while (st == Z_OK || st == Z_BUF_ERROR);
+       git_inflate_end(&stream);
+       if ((st != Z_STREAM_END) || stream.total_out != size) {
+               free(buffer);
+               return NULL;
+       }
+
+       return buffer;
+}
+
+static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
+{
+       static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
+       trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
+                        p->pack_name, (uintmax_t)obj_offset);
+}
+
+int do_check_packed_object_crc;
+
+#define UNPACK_ENTRY_STACK_PREALLOC 64
+struct unpack_entry_stack_ent {
+       off_t obj_offset;
+       off_t curpos;
+       unsigned long size;
+};
+
+static void *read_object(const unsigned char *sha1, enum object_type *type,
+                        unsigned long *size)
+{
+       struct object_info oi = OBJECT_INFO_INIT;
+       void *content;
+       oi.typep = type;
+       oi.sizep = size;
+       oi.contentp = &content;
+
+       if (sha1_object_info_extended(sha1, &oi, 0) < 0)
+               return NULL;
+       return content;
+}
+
+void *unpack_entry(struct packed_git *p, off_t obj_offset,
+                  enum object_type *final_type, unsigned long *final_size)
+{
+       struct pack_window *w_curs = NULL;
+       off_t curpos = obj_offset;
+       void *data = NULL;
+       unsigned long size;
+       enum object_type type;
+       struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
+       struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
+       int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
+       int base_from_cache = 0;
+
+       write_pack_access_log(p, obj_offset);
+
+       /* PHASE 1: drill down to the innermost base object */
+       for (;;) {
+               off_t base_offset;
+               int i;
+               struct delta_base_cache_entry *ent;
+
+               ent = get_delta_base_cache_entry(p, curpos);
+               if (ent) {
+                       type = ent->type;
+                       data = ent->data;
+                       size = ent->size;
+                       detach_delta_base_cache_entry(ent);
+                       base_from_cache = 1;
+                       break;
+               }
+
+               if (do_check_packed_object_crc && p->index_version > 1) {
+                       struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
+                       off_t len = revidx[1].offset - obj_offset;
+                       if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
+                               const unsigned char *sha1 =
+                                       nth_packed_object_sha1(p, revidx->nr);
+                               error("bad packed object CRC for %s",
+                                     sha1_to_hex(sha1));
+                               mark_bad_packed_object(p, sha1);
+                               data = NULL;
+                               goto out;
+                       }
+               }
+
+               type = unpack_object_header(p, &w_curs, &curpos, &size);
+               if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
+                       break;
+
+               base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
+               if (!base_offset) {
+                       error("failed to validate delta base reference "
+                             "at offset %"PRIuMAX" from %s",
+                             (uintmax_t)curpos, p->pack_name);
+                       /* bail to phase 2, in hopes of recovery */
+                       data = NULL;
+                       break;
+               }
+
+               /* push object, proceed to base */
+               if (delta_stack_nr >= delta_stack_alloc
+                   && delta_stack == small_delta_stack) {
+                       delta_stack_alloc = alloc_nr(delta_stack_nr);
+                       ALLOC_ARRAY(delta_stack, delta_stack_alloc);
+                       memcpy(delta_stack, small_delta_stack,
+                              sizeof(*delta_stack)*delta_stack_nr);
+               } else {
+                       ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
+               }
+               i = delta_stack_nr++;
+               delta_stack[i].obj_offset = obj_offset;
+               delta_stack[i].curpos = curpos;
+               delta_stack[i].size = size;
+
+               curpos = obj_offset = base_offset;
+       }
+
+       /* PHASE 2: handle the base */
+       switch (type) {
+       case OBJ_OFS_DELTA:
+       case OBJ_REF_DELTA:
+               if (data)
+                       die("BUG: unpack_entry: left loop at a valid delta");
+               break;
+       case OBJ_COMMIT:
+       case OBJ_TREE:
+       case OBJ_BLOB:
+       case OBJ_TAG:
+               if (!base_from_cache)
+                       data = unpack_compressed_entry(p, &w_curs, curpos, size);
+               break;
+       default:
+               data = NULL;
+               error("unknown object type %i at offset %"PRIuMAX" in %s",
+                     type, (uintmax_t)obj_offset, p->pack_name);
+       }
+
+       /* PHASE 3: apply deltas in order */
+
+       /* invariants:
+        *   'data' holds the base data, or NULL if there was corruption
+        */
+       while (delta_stack_nr) {
+               void *delta_data;
+               void *base = data;
+               void *external_base = NULL;
+               unsigned long delta_size, base_size = size;
+               int i;
+
+               data = NULL;
+
+               if (base)
+                       add_delta_base_cache(p, obj_offset, base, base_size, type);
+
+               if (!base) {
+                       /*
+                        * We're probably in deep shit, but let's try to fetch
+                        * the required base anyway from another pack or loose.
+                        * This is costly but should happen only in the presence
+                        * of a corrupted pack, and is better than failing outright.
+                        */
+                       struct revindex_entry *revidx;
+                       const unsigned char *base_sha1;
+                       revidx = find_pack_revindex(p, obj_offset);
+                       if (revidx) {
+                               base_sha1 = nth_packed_object_sha1(p, revidx->nr);
+                               error("failed to read delta base object %s"
+                                     " at offset %"PRIuMAX" from %s",
+                                     sha1_to_hex(base_sha1), (uintmax_t)obj_offset,
+                                     p->pack_name);
+                               mark_bad_packed_object(p, base_sha1);
+                               base = read_object(base_sha1, &type, &base_size);
+                               external_base = base;
+                       }
+               }
+
+               i = --delta_stack_nr;
+               obj_offset = delta_stack[i].obj_offset;
+               curpos = delta_stack[i].curpos;
+               delta_size = delta_stack[i].size;
+
+               if (!base)
+                       continue;
+
+               delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
+
+               if (!delta_data) {
+                       error("failed to unpack compressed delta "
+                             "at offset %"PRIuMAX" from %s",
+                             (uintmax_t)curpos, p->pack_name);
+                       data = NULL;
+                       free(external_base);
+                       continue;
+               }
+
+               data = patch_delta(base, base_size,
+                                  delta_data, delta_size,
+                                  &size);
+
+               /*
+                * We could not apply the delta; warn the user, but keep going.
+                * Our failure will be noticed either in the next iteration of
+                * the loop, or if this is the final delta, in the caller when
+                * we return NULL. Those code paths will take care of making
+                * a more explicit warning and retrying with another copy of
+                * the object.
+                */
+               if (!data)
+                       error("failed to apply delta");
+
+               free(delta_data);
+               free(external_base);
+       }
+
+       if (final_type)
+               *final_type = type;
+       if (final_size)
+               *final_size = size;
+
+out:
+       unuse_pack(&w_curs);
+
+       if (delta_stack != small_delta_stack)
+               free(delta_stack);
+
+       return data;
+}
+
+const unsigned char *nth_packed_object_sha1(struct packed_git *p,
+                                           uint32_t n)
+{
+       const unsigned char *index = p->index_data;
+       if (!index) {
+               if (open_pack_index(p))
+                       return NULL;
+               index = p->index_data;
+       }
+       if (n >= p->num_objects)
+               return NULL;
+       index += 4 * 256;
+       if (p->index_version == 1) {
+               return index + 24 * n + 4;
+       } else {
+               index += 8;
+               return index + 20 * n;
+       }
+}
+
+const struct object_id *nth_packed_object_oid(struct object_id *oid,
+                                             struct packed_git *p,
+                                             uint32_t n)
+{
+       const unsigned char *hash = nth_packed_object_sha1(p, n);
+       if (!hash)
+               return NULL;
+       hashcpy(oid->hash, hash);
+       return oid;
+}
+
+void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
+{
+       const unsigned char *ptr = vptr;
+       const unsigned char *start = p->index_data;
+       const unsigned char *end = start + p->index_size;
+       if (ptr < start)
+               die(_("offset before start of pack index for %s (corrupt index?)"),
+                   p->pack_name);
+       /* No need to check for underflow; .idx files must be at least 8 bytes */
+       if (ptr >= end - 8)
+               die(_("offset beyond end of pack index for %s (truncated index?)"),
+                   p->pack_name);
+}
+
+off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
+{
+       const unsigned char *index = p->index_data;
+       index += 4 * 256;
+       if (p->index_version == 1) {
+               return ntohl(*((uint32_t *)(index + 24 * n)));
+       } else {
+               uint32_t off;
+               index += 8 + p->num_objects * (20 + 4);
+               off = ntohl(*((uint32_t *)(index + 4 * n)));
+               if (!(off & 0x80000000))
+                       return off;
+               index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
+               check_pack_index_ptr(p, index);
+               return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
+                                  ntohl(*((uint32_t *)(index + 4)));
+       }
+}
+
+off_t find_pack_entry_one(const unsigned char *sha1,
+                                 struct packed_git *p)
+{
+       const uint32_t *level1_ofs = p->index_data;
+       const unsigned char *index = p->index_data;
+       unsigned hi, lo, stride;
+       static int debug_lookup = -1;
+
+       if (debug_lookup < 0)
+               debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
+
+       if (!index) {
+               if (open_pack_index(p))
+                       return 0;
+               level1_ofs = p->index_data;
+               index = p->index_data;
+       }
+       if (p->index_version > 1) {
+               level1_ofs += 2;
+               index += 8;
+       }
+       index += 4 * 256;
+       hi = ntohl(level1_ofs[*sha1]);
+       lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
+       if (p->index_version > 1) {
+               stride = 20;
+       } else {
+               stride = 24;
+               index += 4;
+       }
+
+       if (debug_lookup)
+               printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
+                      sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
+
+       while (lo < hi) {
+               unsigned mi = (lo + hi) / 2;
+               int cmp = hashcmp(index + mi * stride, sha1);
+
+               if (debug_lookup)
+                       printf("lo %u hi %u rg %u mi %u\n",
+                              lo, hi, hi - lo, mi);
+               if (!cmp)
+                       return nth_packed_object_offset(p, mi);
+               if (cmp > 0)
+                       hi = mi;
+               else
+                       lo = mi+1;
+       }
+       return 0;
+}
+
+int is_pack_valid(struct packed_git *p)
+{
+       /* An already open pack is known to be valid. */
+       if (p->pack_fd != -1)
+               return 1;
+
+       /* If the pack has one window completely covering the
+        * file size, the pack is known to be valid even if
+        * the descriptor is not currently open.
+        */
+       if (p->windows) {
+               struct pack_window *w = p->windows;
+
+               if (!w->offset && w->len == p->pack_size)
+                       return 1;
+       }
+
+       /* Force the pack to open to prove its valid. */
+       return !open_packed_git(p);
+}
+
+struct packed_git *find_sha1_pack(const unsigned char *sha1,
+                                 struct packed_git *packs)
+{
+       struct packed_git *p;
+
+       for (p = packs; p; p = p->next) {
+               if (find_pack_entry_one(sha1, p))
+                       return p;
+       }
+       return NULL;
+
+}
+
+static int fill_pack_entry(const unsigned char *sha1,
+                          struct pack_entry *e,
+                          struct packed_git *p)
+{
+       off_t offset;
+
+       if (p->num_bad_objects) {
+               unsigned i;
+               for (i = 0; i < p->num_bad_objects; i++)
+                       if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
+                               return 0;
+       }
+
+       offset = find_pack_entry_one(sha1, p);
+       if (!offset)
+               return 0;
+
+       /*
+        * We are about to tell the caller where they can locate the
+        * requested object.  We better make sure the packfile is
+        * still here and can be accessed before supplying that
+        * answer, as it may have been deleted since the index was
+        * loaded!
+        */
+       if (!is_pack_valid(p))
+               return 0;
+       e->offset = offset;
+       e->p = p;
+       hashcpy(e->sha1, sha1);
+       return 1;
+}
+
+/*
+ * Iff a pack file contains the object named by sha1, return true and
+ * store its location to e.
+ */
+int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
+{
+       struct mru_entry *p;
+
+       prepare_packed_git();
+       if (!packed_git)
+               return 0;
+
+       for (p = packed_git_mru.head; p; p = p->next) {
+               if (fill_pack_entry(sha1, e, p->item)) {
+                       mru_mark(&packed_git_mru, p);
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+int has_sha1_pack(const unsigned char *sha1)
+{
+       struct pack_entry e;
+       return find_pack_entry(sha1, &e);
+}
+
+int has_pack_index(const unsigned char *sha1)
+{
+       struct stat st;
+       if (stat(sha1_pack_index_name(sha1), &st))
+               return 0;
+       return 1;
+}
+
+static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
+{
+       uint32_t i;
+       int r = 0;
+
+       for (i = 0; i < p->num_objects; i++) {
+               struct object_id oid;
+
+               if (!nth_packed_object_oid(&oid, p, i))
+                       return error("unable to get sha1 of object %u in %s",
+                                    i, p->pack_name);
+
+               r = cb(&oid, p, i, data);
+               if (r)
+                       break;
+       }
+       return r;
+}
+
+int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
+{
+       struct packed_git *p;
+       int r = 0;
+       int pack_errors = 0;
+
+       prepare_packed_git();
+       for (p = packed_git; p; p = p->next) {
+               if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
+                       continue;
+               if (open_pack_index(p)) {
+                       pack_errors = 1;
+                       continue;
+               }
+               r = for_each_object_in_pack(p, cb, data);
+               if (r)
+                       break;
+       }
+       return r ? r : pack_errors;
+}
diff --git a/packfile.h b/packfile.h
new file mode 100644 (file)
index 0000000..0cdeb54
--- /dev/null
@@ -0,0 +1,138 @@
+#ifndef PACKFILE_H
+#define PACKFILE_H
+
+/*
+ * Generate the filename to be used for a pack file with checksum "sha1" and
+ * extension "ext". The result is written into the strbuf "buf", overwriting
+ * any existing contents. A pointer to buf->buf is returned as a convenience.
+ *
+ * Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx"
+ */
+extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
+
+/*
+ * Return the name of the (local) packfile with the specified sha1 in
+ * its name.  The return value is a pointer to memory that is
+ * overwritten each time this function is called.
+ */
+extern char *sha1_pack_name(const unsigned char *sha1);
+
+/*
+ * Return the name of the (local) pack index file with the specified
+ * sha1 in its name.  The return value is a pointer to memory that is
+ * overwritten each time this function is called.
+ */
+extern char *sha1_pack_index_name(const unsigned char *sha1);
+
+extern struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path);
+
+/* A hook to report invalid files in pack directory */
+#define PACKDIR_FILE_PACK 1
+#define PACKDIR_FILE_IDX 2
+#define PACKDIR_FILE_GARBAGE 4
+extern void (*report_garbage)(unsigned seen_bits, const char *path);
+
+extern void prepare_packed_git(void);
+extern void reprepare_packed_git(void);
+extern void install_packed_git(struct packed_git *pack);
+
+/*
+ * Give a rough count of objects in the repository. This sacrifices accuracy
+ * for speed.
+ */
+unsigned long approximate_object_count(void);
+
+extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
+                                        struct packed_git *packs);
+
+extern void pack_report(void);
+
+/*
+ * mmap the index file for the specified packfile (if it is not
+ * already mmapped).  Return 0 on success.
+ */
+extern int open_pack_index(struct packed_git *);
+
+/*
+ * munmap the index file for the specified packfile (if it is
+ * currently mmapped).
+ */
+extern void close_pack_index(struct packed_git *);
+
+extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
+extern void close_pack_windows(struct packed_git *);
+extern void close_all_packs(void);
+extern void unuse_pack(struct pack_window **);
+extern void clear_delta_base_cache(void);
+extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
+
+/*
+ * Make sure that a pointer access into an mmap'd index file is within bounds,
+ * and can provide at least 8 bytes of data.
+ *
+ * Note that this is only necessary for variable-length segments of the file
+ * (like the 64-bit extended offset table), as we compare the size to the
+ * fixed-length parts when we open the file.
+ */
+extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
+
+/*
+ * Return the SHA-1 of the nth object within the specified packfile.
+ * Open the index if it is not already open.  The return value points
+ * at the SHA-1 within the mmapped index.  Return NULL if there is an
+ * error.
+ */
+extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
+/*
+ * Like nth_packed_object_sha1, but write the data into the object specified by
+ * the the first argument.  Returns the first argument on success, and NULL on
+ * error.
+ */
+extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
+
+/*
+ * Return the offset of the nth object within the specified packfile.
+ * The index must already be opened.
+ */
+extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
+
+/*
+ * If the object named sha1 is present in the specified packfile,
+ * return its offset within the packfile; otherwise, return 0.
+ */
+extern off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
+
+extern int is_pack_valid(struct packed_git *);
+extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
+extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
+extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
+extern int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, unsigned long *);
+
+extern void release_pack_memory(size_t);
+
+/* global flag to enable extra checks when accessing packed objects */
+extern int do_check_packed_object_crc;
+
+extern int packed_object_info(struct packed_git *pack, off_t offset, struct object_info *);
+
+extern void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1);
+extern const struct packed_git *has_packed_and_bad(const unsigned char *sha1);
+
+extern int find_pack_entry(const unsigned char *sha1, struct pack_entry *e);
+
+extern int has_sha1_pack(const unsigned char *sha1);
+
+extern int has_pack_index(const unsigned char *sha1);
+
+/*
+ * Iterate over packed objects in both the local
+ * repository and any alternates repositories (unless the
+ * FOR_EACH_OBJECT_LOCAL_ONLY flag, defined in cache.h, is set).
+ */
+typedef int each_packed_object_fn(const struct object_id *oid,
+                                 struct packed_git *pack,
+                                 uint32_t pos,
+                                 void *data);
+extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
+
+#endif
diff --git a/pager.c b/pager.c
index 4dd9e1b26592bd3e7a7ddb52182ea79971557db0..92b23e6cd1d44a26c86afeeb748ddc0aee3f9154 100644 (file)
--- a/pager.c
+++ b/pager.c
@@ -194,7 +194,7 @@ static int pager_command_config(const char *var, const char *value, void *vdata)
        const char *cmd;
 
        if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
-               int b = git_config_maybe_bool(var, value);
+               int b = git_parse_maybe_bool(value);
                if (b >= 0)
                        data->want = b;
                else {
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)
index b4166b0f384691ada862ff0b1e37387b018587b1..7a583b3011390c1a3cb1c0885e420e76b7751d26 100644 (file)
@@ -35,11 +35,16 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  * the side of safety.  The actual value being negative does not have
  * any significance; only that it is non-zero matters.
  */
-static int patch_id_cmp(struct diff_options *opt,
-                       struct patch_id *a,
-                       struct patch_id *b,
+static int patch_id_cmp(const void *cmpfn_data,
+                       const void *entry,
+                       const void *entry_or_key,
                        const void *unused_keydata)
 {
+       /* NEEDSWORK: const correctness? */
+       struct diff_options *opt = (void *)cmpfn_data;
+       struct patch_id *a = (void *)entry;
+       struct patch_id *b = (void *)entry_or_key;
+
        if (is_null_oid(&a->patch_id) &&
            commit_patch_id(a->commit, opt, &a->patch_id, 0))
                return error("Could not get patch ID for %s",
@@ -58,8 +63,7 @@ int init_patch_ids(struct patch_ids *ids)
        ids->diffopts.detect_rename = 0;
        DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
        diff_setup_done(&ids->diffopts);
-       hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
-                    &ids->diffopts, 256);
+       hashmap_init(&ids->patches, patch_id_cmp, &ids->diffopts, 256);
        return 0;
 }
 
diff --git a/path.c b/path.c
index e50d2befcf826680c70ac59295c06004dd6221cf..2e09a7bce0688acccaca8b980c2c337a71885966 100644 (file)
--- a/path.c
+++ b/path.c
@@ -9,6 +9,7 @@
 #include "worktree.h"
 #include "submodule-config.h"
 #include "path.h"
+#include "packfile.h"
 
 static int get_st_mode_bits(const char *path, int *mode)
 {
@@ -33,11 +34,10 @@ static struct strbuf *get_pathname(void)
        return sb;
 }
 
-static char *cleanup_path(char *path)
+static const char *cleanup_path(const char *path)
 {
        /* Clean it up */
-       if (!memcmp(path, "./", 2)) {
-               path += 2;
+       if (skip_prefix(path, "./", &path)) {
                while (*path == '/')
                        path++;
        }
@@ -46,7 +46,7 @@ static char *cleanup_path(char *path)
 
 static void strbuf_cleanup_path(struct strbuf *sb)
 {
-       char *path = cleanup_path(sb->buf);
+       const char *path = cleanup_path(sb->buf);
        if (path > sb->buf)
                strbuf_remove(sb, 0, path - sb->buf);
 }
@@ -63,7 +63,7 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...)
                strlcpy(buf, bad_path, n);
                return buf;
        }
-       return cleanup_path(buf);
+       return (char *)cleanup_path(buf);
 }
 
 static int dir_prefix(const char *buf, const char *dir)
@@ -636,8 +636,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;
 
@@ -661,24 +662,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;
@@ -716,7 +717,7 @@ char *expand_user_path(const char *path, int real_home)
                        if (!home)
                                goto return_null;
                        if (real_home)
-                               strbuf_addstr(&user_path, real_path(home));
+                               strbuf_add_real_path(&user_path, home);
                        else
                                strbuf_addstr(&user_path, home);
 #ifdef GIT_WINDOWS_NATIVE
index e2a23ebc9668b066d337391e96139af49907edfa..cdefdc7cc0e0be248297d1191eefe72418923f7a 100644 (file)
@@ -526,10 +526,6 @@ static void NORETURN unsupported_magic(const char *pattern,
            pattern, sb.buf);
 }
 
-/*
- * Given command line arguments and a prefix, convert the input to
- * pathspec. die() if any magic in magic_mask is used.
- */
 void parse_pathspec(struct pathspec *pathspec,
                    unsigned magic_mask, unsigned flags,
                    const char *prefix, const char **argv)
index 60e6500401e687bc0d764c38c5d8b4928b7bfde8..6420d1080ae4165ad81b60376c714bd178463b6a 100644 (file)
@@ -70,6 +70,13 @@ struct pathspec {
  */
 #define PATHSPEC_LITERAL_PATH (1<<6)
 
+/*
+ * Given command line arguments and a prefix, convert the input to
+ * pathspec. die() if any magic in magic_mask is used.
+ *
+ * Any arguments used are copied. It is safe for the caller to modify
+ * or free 'prefix' and 'args' after calling this function.
+ */
 extern void parse_pathspec(struct pathspec *pathspec,
                           unsigned magic_mask,
                           unsigned flags,
index 7db9119573abe2c8308bd37c670c027c07b425c8..93ea311443a37b81b85a72dc569c715417d7ef29 100644 (file)
@@ -94,9 +94,9 @@ void packet_flush(int fd)
 int packet_flush_gently(int fd)
 {
        packet_trace("0000", 4, 1);
-       if (write_in_full(fd, "0000", 4) == 4)
-               return 0;
-       return error("flush packet write failed");
+       if (write_in_full(fd, "0000", 4) < 0)
+               return error("flush packet write failed");
+       return 0;
 }
 
 void packet_buf_flush(struct strbuf *buf)
@@ -136,19 +136,19 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)
 static int packet_write_fmt_1(int fd, int gently,
                              const char *fmt, va_list args)
 {
-       struct strbuf buf = STRBUF_INIT;
-       ssize_t count;
+       static struct strbuf buf = STRBUF_INIT;
 
+       strbuf_reset(&buf);
        format_packet(&buf, fmt, args);
-       count = write_in_full(fd, buf.buf, buf.len);
-       if (count == buf.len)
-               return 0;
-
-       if (!gently) {
-               check_pipe(errno);
-               die_errno("packet write with format failed");
+       if (write_in_full(fd, buf.buf, buf.len) < 0) {
+               if (!gently) {
+                       check_pipe(errno);
+                       die_errno("packet write with format failed");
+               }
+               return error("packet write with format failed");
        }
-       return error("packet write with format failed");
+
+       return 0;
 }
 
 void packet_write_fmt(int fd, const char *fmt, ...)
@@ -183,9 +183,9 @@ static int packet_write_gently(const int fd_out, const char *buf, size_t size)
        packet_size = size + 4;
        set_packet_header(packet_write_buffer, packet_size);
        memcpy(packet_write_buffer + 4, buf, size);
-       if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)
-               return 0;
-       return error("packet write failed");
+       if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
+               return error("packet write failed");
+       return 0;
 }
 
 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
@@ -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 39cad5112b603df9c8ed927e738b2f7917da7522..2f6b0ae6c1486660bb37442b991cb1140a9f2bb6 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -871,16 +871,6 @@ const char *format_subject(struct strbuf *sb, const char *msg,
        return msg;
 }
 
-static void format_trailers(struct strbuf *sb, const char *msg)
-{
-       struct trailer_info info;
-
-       trailer_info_get(&info, msg);
-       strbuf_add(sb, info.trailer_start,
-                  info.trailer_end - info.trailer_start);
-       trailer_info_release(&info);
-}
-
 static void parse_commit_message(struct format_commit_context *c)
 {
        const char *msg = c->message + c->message_off;
@@ -1066,6 +1056,24 @@ static size_t parse_padding_placeholder(struct strbuf *sb,
        return 0;
 }
 
+static int match_placeholder_arg(const char *to_parse, const char *candidate,
+                                const char **end)
+{
+       const char *p;
+
+       if (!(skip_prefix(to_parse, candidate, &p)))
+               return 0;
+       if (*p == ',') {
+               *end = p + 1;
+               return 1;
+       }
+       if (*p == ')') {
+               *end = p;
+               return 1;
+       }
+       return 0;
+}
+
 static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                                const char *placeholder,
                                void *context)
@@ -1074,6 +1082,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
        const struct commit *commit = c->commit;
        const char *msg = c->message;
        struct commit_list *p;
+       const char *arg;
        int ch;
 
        /* these are independent of the commit */
@@ -1292,9 +1301,23 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                return 1;
        }
 
-       if (starts_with(placeholder, "(trailers)")) {
-               format_trailers(sb, msg + c->subject_off);
-               return strlen("(trailers)");
+       if (skip_prefix(placeholder, "(trailers", &arg)) {
+               struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
+               if (*arg == ':') {
+                       arg++;
+                       for (;;) {
+                               if (match_placeholder_arg(arg, "only", &arg))
+                                       opts.only_trailers = 1;
+                               else if (match_placeholder_arg(arg, "unfold", &arg))
+                                       opts.unfold = 1;
+                               else
+                                       break;
+                       }
+               }
+               if (*arg == ')') {
+                       format_trailers_from_commit(sb, msg + c->subject_off, &opts);
+                       return arg - placeholder + 1;
+               }
        }
 
        return 0;       /* unknown placeholder */
index 73e36d4a423067756ccd52af8cdb4cf0f762684b..289678d43d801411dbe56b8090a2e1a18a158499 100644 (file)
@@ -34,7 +34,7 @@ struct progress {
        unsigned total;
        unsigned last_percent;
        unsigned delay;
-       unsigned delayed_percent_treshold;
+       unsigned delayed_percent_threshold;
        struct throughput *throughput;
        uint64_t start_ns;
 };
@@ -88,7 +88,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
                        return 0;
                if (progress->total) {
                        unsigned percent = n * 100 / progress->total;
-                       if (percent > progress->delayed_percent_treshold) {
+                       if (percent > progress->delayed_percent_threshold) {
                                /* inhibit this progress report entirely */
                                clear_progress_signal();
                                progress->delay = -1;
@@ -205,8 +205,8 @@ int display_progress(struct progress *progress, unsigned n)
        return progress ? display(progress, n, NULL) : 0;
 }
 
-struct progress *start_progress_delay(const char *title, unsigned total,
-                                      unsigned percent_treshold, unsigned delay)
+static struct progress *start_progress_delay(const char *title, unsigned total,
+                                            unsigned percent_threshold, unsigned delay)
 {
        struct progress *progress = malloc(sizeof(*progress));
        if (!progress) {
@@ -219,7 +219,7 @@ struct progress *start_progress_delay(const char *title, unsigned total,
        progress->total = total;
        progress->last_value = -1;
        progress->last_percent = -1;
-       progress->delayed_percent_treshold = percent_treshold;
+       progress->delayed_percent_threshold = percent_threshold;
        progress->delay = delay;
        progress->throughput = NULL;
        progress->start_ns = getnanotime();
@@ -227,6 +227,11 @@ struct progress *start_progress_delay(const char *title, unsigned total,
        return progress;
 }
 
+struct progress *start_delayed_progress(const char *title, unsigned total)
+{
+       return start_progress_delay(title, total, 0, 2);
+}
+
 struct progress *start_progress(const char *title, unsigned total)
 {
        return start_progress_delay(title, total, 0, 0);
index 611e4c4d42d8d1164add09f926ad5b2ce088db5e..6392b633710c84dcff3a238a26f4d4b359053059 100644 (file)
@@ -6,8 +6,7 @@ struct progress;
 void display_throughput(struct progress *progress, off_t total);
 int display_progress(struct progress *progress, unsigned n);
 struct progress *start_progress(const char *title, unsigned total);
-struct progress *start_progress_delay(const char *title, unsigned total,
-                                      unsigned percent_treshold, unsigned delay);
+struct progress *start_delayed_progress(const char *title, unsigned total);
 void stop_progress(struct progress **progress);
 void stop_progress_msg(struct progress **progress, const char *msg);
 
diff --git a/quote.c b/quote.c
index 53b98a5b840d8fd4d73fab27348b657501923dca..de2922ddd63d6fc001822d116387c8ced7d7630d 100644 (file)
--- a/quote.c
+++ b/quote.c
@@ -431,6 +431,7 @@ void tcl_quote_buf(struct strbuf *sb, const char *src)
                case '{': case '}':
                case '$': case '\\': case '"':
                        strbuf_addch(sb, '\\');
+                       /* fallthrough */
                default:
                        strbuf_addch(sb, c);
                        break;
index c62efbfd43b9c4ddc25d6768d55bddc745f5c14b..88d7d679da9c837463f74d9a9277025b008635af 100644 (file)
@@ -9,6 +9,8 @@
 #include "cache-tree.h"
 #include "progress.h"
 #include "list-objects.h"
+#include "packfile.h"
+#include "worktree.h"
 
 struct connectivity_progress {
        struct progress *progress;
@@ -176,6 +178,7 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 
        /* detached HEAD is not included in the list above */
        head_ref(add_one_ref, revs);
+       other_head_refs(add_one_ref, revs);
 
        /* Add all reflog info */
        if (mark_reflog)
index acfb028f480b5545aa82d5c0533fe8bdfc581dfd..65f4fe8375d59234d5e58f87a8593fc50e6336a2 100644 (file)
@@ -160,9 +160,9 @@ static int ce_compare_data(const struct cache_entry *ce, struct stat *st)
        int fd = git_open_cloexec(ce->name, O_RDONLY);
 
        if (fd >= 0) {
-               unsigned char sha1[20];
-               if (!index_fd(sha1, fd, st, OBJ_BLOB, ce->name, 0))
-                       match = hashcmp(sha1, ce->oid.hash);
+               struct object_id oid;
+               if (!index_fd(&oid, fd, st, OBJ_BLOB, ce->name, 0))
+                       match = oidcmp(&oid, &ce->oid);
                /* index_fd() closed the file descriptor already */
        }
        return match;
@@ -220,6 +220,7 @@ static int ce_modified_check_fs(const struct cache_entry *ce, struct stat *st)
        case S_IFDIR:
                if (S_ISGITLINK(ce->ce_mode))
                        return ce_compare_gitlink(ce) ? DATA_CHANGED : 0;
+               /* else fallthrough */
        default:
                return TYPE_CHANGED;
        }
@@ -689,7 +690,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
                return 0;
        }
        if (!intent_only) {
-               if (index_path(ce->oid.hash, path, st, HASH_WRITE_OBJECT)) {
+               if (index_path(&ce->oid, path, st, HASH_WRITE_OBJECT)) {
                        free(ce);
                        return error("unable to index file %s", path);
                }
@@ -1499,6 +1500,7 @@ struct ondisk_cache_entry_extended {
 };
 
 /* These are only used for v3 or lower */
+#define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len)
 #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
 #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
 #define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)
@@ -1921,7 +1923,7 @@ static int ce_write_flush(git_SHA_CTX *context, int fd)
        unsigned int buffered = write_buffer_len;
        if (buffered) {
                git_SHA1_Update(context, write_buffer, buffered);
-               if (write_in_full(fd, write_buffer, buffered) != buffered)
+               if (write_in_full(fd, write_buffer, buffered) < 0)
                        return -1;
                write_buffer_len = 0;
        }
@@ -1970,7 +1972,7 @@ static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)
 
        /* Flush first if not enough space for SHA1 signature */
        if (left + 20 > WRITE_BUFFER_SIZE) {
-               if (write_in_full(fd, write_buffer, left) != left)
+               if (write_in_full(fd, write_buffer, left) < 0)
                        return -1;
                left = 0;
        }
@@ -1979,7 +1981,7 @@ static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)
        git_SHA1_Final(write_buffer + left, context);
        hashcpy(sha1, write_buffer + left);
        left += 20;
-       return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
+       return (write_in_full(fd, write_buffer, left) < 0) ? -1 : 0;
 }
 
 static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
@@ -2032,7 +2034,7 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
 }
 
 /* Copy miscellaneous fields but not the name */
-static char *copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
+static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
                                       struct cache_entry *ce)
 {
        short flags;
@@ -2056,32 +2058,35 @@ static char *copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
                struct ondisk_cache_entry_extended *ondisk2;
                ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
                ondisk2->flags2 = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16);
-               return ondisk2->name;
-       }
-       else {
-               return ondisk->name;
        }
 }
 
 static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
-                         struct strbuf *previous_name)
+                         struct strbuf *previous_name, struct ondisk_cache_entry *ondisk)
 {
        int size;
-       struct ondisk_cache_entry *ondisk;
        int saved_namelen = saved_namelen; /* compiler workaround */
-       char *name;
        int result;
+       static unsigned char padding[8] = { 0x00 };
 
        if (ce->ce_flags & CE_STRIP_NAME) {
                saved_namelen = ce_namelen(ce);
                ce->ce_namelen = 0;
        }
 
+       if (ce->ce_flags & CE_EXTENDED)
+               size = offsetof(struct ondisk_cache_entry_extended, name);
+       else
+               size = offsetof(struct ondisk_cache_entry, name);
+
        if (!previous_name) {
-               size = ondisk_ce_size(ce);
-               ondisk = xcalloc(1, size);
-               name = copy_cache_entry_to_ondisk(ondisk, ce);
-               memcpy(name, ce->name, ce_namelen(ce));
+               int len = ce_namelen(ce);
+               copy_cache_entry_to_ondisk(ondisk, ce);
+               result = ce_write(c, fd, ondisk, size);
+               if (!result)
+                       result = ce_write(c, fd, ce->name, len);
+               if (!result)
+                       result = ce_write(c, fd, padding, align_padding_size(size, len));
        } else {
                int common, to_remove, prefix_size;
                unsigned char to_remove_vi[16];
@@ -2094,16 +2099,14 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
                to_remove = previous_name->len - common;
                prefix_size = encode_varint(to_remove, to_remove_vi);
 
-               if (ce->ce_flags & CE_EXTENDED)
-                       size = offsetof(struct ondisk_cache_entry_extended, name);
-               else
-                       size = offsetof(struct ondisk_cache_entry, name);
-               size += prefix_size + (ce_namelen(ce) - common + 1);
-
-               ondisk = xcalloc(1, size);
-               name = copy_cache_entry_to_ondisk(ondisk, ce);
-               memcpy(name, to_remove_vi, prefix_size);
-               memcpy(name + prefix_size, ce->name + common, ce_namelen(ce) - common);
+               copy_cache_entry_to_ondisk(ondisk, ce);
+               result = ce_write(c, fd, ondisk, size);
+               if (!result)
+                       result = ce_write(c, fd, to_remove_vi, prefix_size);
+               if (!result)
+                       result = ce_write(c, fd, ce->name + common, ce_namelen(ce) - common);
+               if (!result)
+                       result = ce_write(c, fd, padding, 1);
 
                strbuf_splice(previous_name, common, to_remove,
                              ce->name + common, ce_namelen(ce) - common);
@@ -2113,8 +2116,6 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
                ce->ce_flags &= ~CE_STRIP_NAME;
        }
 
-       result = ce_write(c, fd, ondisk, size);
-       free(ondisk);
        return result;
 }
 
@@ -2192,10 +2193,11 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
        int newfd = tempfile->fd;
        git_SHA_CTX c;
        struct cache_header hdr;
-       int i, err, removed, extended, hdr_version;
+       int i, err = 0, removed, extended, hdr_version;
        struct cache_entry **cache = istate->cache;
        int entries = istate->cache_nr;
        struct stat st;
+       struct ondisk_cache_entry_extended ondisk;
        struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
        int drop_cache_tree = 0;
 
@@ -2232,6 +2234,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
                return -1;
 
        previous_name = (hdr_version == 4) ? &previous_name_buf : NULL;
+
        for (i = 0; i < entries; i++) {
                struct cache_entry *ce = cache[i];
                if (ce->ce_flags & CE_REMOVE)
@@ -2247,15 +2250,21 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
                        if (allow)
                                warning(msg, ce->name);
                        else
-                               return error(msg, ce->name);
+                               err = error(msg, ce->name);
 
                        drop_cache_tree = 1;
                }
-               if (ce_write_entry(&c, newfd, ce, previous_name) < 0)
-                       return -1;
+               if (ce_write_entry(&c, newfd, ce, previous_name, (struct ondisk_cache_entry *)&ondisk) < 0)
+                       err = -1;
+
+               if (err)
+                       break;
        }
        strbuf_release(&previous_name_buf);
 
+       if (err)
+               return err;
+
        /* Write extension data here */
        if (!strip_extensions && istate->split_index) {
                struct strbuf sb = STRBUF_INIT;
@@ -2303,8 +2312,11 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
 
        if (ce_flush(&c, newfd, istate->sha1))
                return -1;
-       if (close_tempfile(tempfile))
-               return error(_("could not close '%s'"), tempfile->filename.buf);
+       if (close_tempfile_gently(tempfile)) {
+               error(_("could not close '%s'"), tempfile->filename.buf);
+               delete_tempfile(&tempfile);
+               return -1;
+       }
        if (stat(tempfile->filename.buf, &st))
                return -1;
        istate->timestamp.sec = (unsigned int)st.st_mtime;
@@ -2328,7 +2340,7 @@ static int commit_locked_index(struct lock_file *lk)
 static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
                                 unsigned flags)
 {
-       int ret = do_write_index(istate, &lock->tempfile, 0);
+       int ret = do_write_index(istate, lock->tempfile, 0);
        if (ret)
                return ret;
        assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
@@ -2336,7 +2348,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
        if (flags & COMMIT_LOCK)
                return commit_locked_index(lock);
        else if (flags & CLOSE_LOCK)
-               return close_lock_file(lock);
+               return close_lock_file_gently(lock);
        else
                return ret;
 }
@@ -2411,34 +2423,33 @@ static int clean_shared_index_files(const char *current_hex)
        return 0;
 }
 
-static struct tempfile temporary_sharedindex;
-
 static int write_shared_index(struct index_state *istate,
                              struct lock_file *lock, unsigned flags)
 {
+       struct tempfile *temp;
        struct split_index *si = istate->split_index;
-       int fd, ret;
+       int ret;
 
-       fd = mks_tempfile(&temporary_sharedindex, git_path("sharedindex_XXXXXX"));
-       if (fd < 0) {
+       temp = mks_tempfile(git_path("sharedindex_XXXXXX"));
+       if (!temp) {
                hashclr(si->base_sha1);
                return do_write_locked_index(istate, lock, flags);
        }
        move_cache_to_base_index(istate);
-       ret = do_write_index(si->base, &temporary_sharedindex, 1);
+       ret = do_write_index(si->base, temp, 1);
        if (ret) {
-               delete_tempfile(&temporary_sharedindex);
+               delete_tempfile(&temp);
                return ret;
        }
-       ret = adjust_shared_perm(get_tempfile_path(&temporary_sharedindex));
+       ret = adjust_shared_perm(get_tempfile_path(temp));
        if (ret) {
                int save_errno = errno;
-               error("cannot fix permission bits on %s", get_tempfile_path(&temporary_sharedindex));
-               delete_tempfile(&temporary_sharedindex);
+               error("cannot fix permission bits on %s", get_tempfile_path(temp));
+               delete_tempfile(&temp);
                errno = save_errno;
                return ret;
        }
-       ret = rename_tempfile(&temporary_sharedindex,
+       ret = rename_tempfile(&temp,
                              git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
        if (!ret) {
                hashcpy(si->base_sha1, si->base->sha1);
index bc591f4f3de0c07b0cfe3813d5d9daaf1ad44b63..e728b15b3aeec8e8a9154d4ee4a4284cf06b5931 100644 (file)
@@ -82,6 +82,7 @@ static struct used_atom {
                } remote_ref;
                struct {
                        enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
+                       struct process_trailer_options trailer_opts;
                        unsigned int nlines;
                } contents;
                struct {
@@ -182,9 +183,23 @@ static void subject_atom_parser(const struct ref_format *format, struct used_ato
 
 static void trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
 {
-       if (arg)
-               die(_("%%(trailers) does not take arguments"));
+       struct string_list params = STRING_LIST_INIT_DUP;
+       int i;
+
+       if (arg) {
+               string_list_split(&params, arg, ',', -1);
+               for (i = 0; i < params.nr; i++) {
+                       const char *s = params.items[i].string;
+                       if (!strcmp(s, "unfold"))
+                               atom->u.contents.trailer_opts.unfold = 1;
+                       else if (!strcmp(s, "only"))
+                               atom->u.contents.trailer_opts.only_trailers = 1;
+                       else
+                               die(_("unknown %%(trailers) argument: %s"), s);
+               }
+       }
        atom->u.contents.option = C_TRAILERS;
+       string_list_clear(&params, 0);
 }
 
 static void contents_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
@@ -197,9 +212,10 @@ static void contents_atom_parser(const struct ref_format *format, struct used_at
                atom->u.contents.option = C_SIG;
        else if (!strcmp(arg, "subject"))
                atom->u.contents.option = C_SUB;
-       else if (!strcmp(arg, "trailers"))
-               atom->u.contents.option = C_TRAILERS;
-       else if (skip_prefix(arg, "lines=", &arg)) {
+       else if (skip_prefix(arg, "trailers", &arg)) {
+               skip_prefix(arg, ":", &arg);
+               trailers_atom_parser(format, atom, *arg ? arg : NULL);
+       } else if (skip_prefix(arg, "lines=", &arg)) {
                atom->u.contents.option = C_LINES;
                if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
                        die(_("positive value expected contents:lines=%s"), arg);
@@ -295,9 +311,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 {
@@ -415,8 +429,16 @@ static int parse_ref_filter_atom(const struct ref_format *format,
        REALLOC_ARRAY(used_atom, used_atom_cnt);
        used_atom[at].name = xmemdupz(atom, ep - atom);
        used_atom[at].type = valid_atom[i].cmp_type;
-       if (arg)
+       if (arg) {
                arg = used_atom[at].name + (arg - atom) + 1;
+               if (!*arg) {
+                       /*
+                        * Treat empty sub-arguments list as NULL (i.e.,
+                        * "%(atom:)" is equivalent to "%(atom)").
+                        */
+                       arg = NULL;
+               }
+       }
        memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
        if (valid_atom[i].parser)
                valid_atom[i].parser(format, &used_atom[at], arg);
@@ -1042,7 +1064,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                        name++;
                if (strcmp(name, "subject") &&
                    strcmp(name, "body") &&
-                   strcmp(name, "trailers") &&
+                   !starts_with(name, "trailers") &&
                    !starts_with(name, "contents"))
                        continue;
                if (!subpos)
@@ -1067,13 +1089,12 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                        append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
                        v->s = strbuf_detach(&s, NULL);
                } else if (atom->u.contents.option == C_TRAILERS) {
-                       struct trailer_info info;
+                       struct strbuf s = STRBUF_INIT;
 
-                       /* Search for trailer info */
-                       trailer_info_get(&info, subpos);
-                       v->s = xmemdupz(info.trailer_start,
-                                       info.trailer_end - info.trailer_start);
-                       trailer_info_release(&info);
+                       /* Format the trailer info according to the trailer_opts given */
+                       format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
+
+                       v->s = strbuf_detach(&s, NULL);
                } else if (atom->u.contents.option == C_BARE)
                        v->s = xstrdup(subpos);
        }
@@ -1317,9 +1338,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 ea2b9f84f8d3fab5452105f10f4492d672cd1d90..df075fcd06b0826982c94f27ab38395ac7ce30a6 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -174,6 +174,24 @@ int refname_is_safe(const char *refname)
        return 1;
 }
 
+/*
+ * Return true if refname, which has the specified oid and flags, can
+ * be resolved to an object in the database. If the referred-to object
+ * does not exist, emit a warning and return false.
+ */
+int ref_resolves_to_object(const char *refname,
+                          const struct object_id *oid,
+                          unsigned int flags)
+{
+       if (flags & REF_ISBROKEN)
+               return 0;
+       if (!has_sha1_file(oid->hash)) {
+               error("%s does not point to a valid object!", refname);
+               return 0;
+       }
+       return 1;
+}
+
 char *refs_resolve_refdup(struct ref_store *refs,
                          const char *refname, int resolve_flags,
                          unsigned char *sha1, int *flags)
@@ -221,8 +239,7 @@ int read_ref(const char *refname, unsigned char *sha1)
 
 int ref_exists(const char *refname)
 {
-       unsigned char sha1[20];
-       return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
+       return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, NULL, NULL);
 }
 
 static int filter_refs(const char *refname, const struct object_id *oid,
@@ -268,12 +285,11 @@ static int warn_if_dangling_symref(const char *refname, const struct object_id *
 {
        struct warn_if_dangling_data *d = cb_data;
        const char *resolves_to;
-       struct object_id junk;
 
        if (!(flags & REF_ISSYMREF))
                return 0;
 
-       resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
+       resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL);
        if (!resolves_to
            || (d->refname
                ? strcmp(resolves_to, d->refname)
@@ -318,12 +334,6 @@ int for_each_tag_ref(each_ref_fn fn, void *cb_data)
        return refs_for_each_tag_ref(get_main_ref_store(), fn, cb_data);
 }
 
-int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
-{
-       return refs_for_each_tag_ref(get_submodule_ref_store(submodule),
-                                    fn, cb_data);
-}
-
 int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
        return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
@@ -334,12 +344,6 @@ int for_each_branch_ref(each_ref_fn fn, void *cb_data)
        return refs_for_each_branch_ref(get_main_ref_store(), fn, cb_data);
 }
 
-int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
-{
-       return refs_for_each_branch_ref(get_submodule_ref_store(submodule),
-                                       fn, cb_data);
-}
-
 int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
        return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
@@ -350,12 +354,6 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data)
        return refs_for_each_remote_ref(get_main_ref_store(), fn, cb_data);
 }
 
-int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
-{
-       return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
-                                       fn, cb_data);
-}
-
 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
 {
        struct strbuf buf = STRBUF_INIT;
@@ -561,6 +559,21 @@ enum ref_type ref_type(const char *refname)
        return REF_TYPE_NORMAL;
 }
 
+long get_files_ref_lock_timeout_ms(void)
+{
+       static int configured = 0;
+
+       /* The default timeout is 100 ms: */
+       static int timeout_ms = 100;
+
+       if (!configured) {
+               git_config_get_int("core.filesreflocktimeout", &timeout_ms);
+               configured = 1;
+       }
+
+       return timeout_ms;
+}
+
 static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
                           const unsigned char *old_sha1, struct strbuf *err)
 {
@@ -573,11 +586,13 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
        strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
 
        filename = git_path("%s", pseudoref);
-       fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
+       fd = hold_lock_file_for_update_timeout(&lock, filename,
+                                              LOCK_DIE_ON_ERROR,
+                                              get_files_ref_lock_timeout_ms());
        if (fd < 0) {
                strbuf_addf(err, "could not open '%s' for writing: %s",
                            filename, strerror(errno));
-               return -1;
+               goto done;
        }
 
        if (old_sha1) {
@@ -592,7 +607,7 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
                }
        }
 
-       if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
+       if (write_in_full(fd, buf.buf, buf.len) < 0) {
                strbuf_addf(err, "could not write to '%s'", filename);
                rollback_lock_file(&lock);
                goto done;
@@ -616,8 +631,9 @@ static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1
                int fd;
                unsigned char actual_old_sha1[20];
 
-               fd = hold_lock_file_for_update(&lock, filename,
-                                              LOCK_DIE_ON_ERROR);
+               fd = hold_lock_file_for_update_timeout(
+                               &lock, filename, LOCK_DIE_ON_ERROR,
+                               get_files_ref_lock_timeout_ms());
                if (fd < 0)
                        die_errno(_("Could not open '%s' for writing"), filename);
                if (read_ref(pseudoref, actual_old_sha1))
@@ -818,7 +834,7 @@ int read_ref_at(const char *refname, unsigned int flags, timestamp_t at_time, in
        for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
 
        if (!cb.reccnt) {
-               if (flags & GET_SHA1_QUIETLY)
+               if (flags & GET_OID_QUIETLY)
                        exit(128);
                else
                        die("Log for %s is empty.", refname);
@@ -921,6 +937,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
                return -1;
        }
 
+       flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
+
        flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
 
        ref_transaction_add_update(transaction, refname, flags,
@@ -1230,19 +1248,13 @@ int refs_rename_ref_available(struct ref_store *refs,
        return ok;
 }
 
-int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
        struct object_id oid;
        int flag;
 
-       if (submodule) {
-               if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
-                       return fn("HEAD", &oid, 0, cb_data);
-
-               return 0;
-       }
-
-       if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
+       if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING,
+                               oid.hash, &flag))
                return fn("HEAD", &oid, flag, cb_data);
 
        return 0;
@@ -1250,7 +1262,7 @@ int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 
 int head_ref(each_ref_fn fn, void *cb_data)
 {
-       return head_ref_submodule(NULL, fn, cb_data);
+       return refs_head_ref(get_main_ref_store(), fn, cb_data);
 }
 
 struct ref_iterator *refs_ref_iterator_begin(
@@ -1273,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;
 }
 
@@ -1308,11 +1324,6 @@ int for_each_ref(each_ref_fn fn, void *cb_data)
        return refs_for_each_ref(get_main_ref_store(), fn, cb_data);
 }
 
-int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
-{
-       return refs_for_each_ref(get_submodule_ref_store(submodule), fn, cb_data);
-}
-
 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
                         each_ref_fn fn, void *cb_data)
 {
@@ -1334,23 +1345,15 @@ int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsig
                               prefix, fn, 0, flag, cb_data);
 }
 
-int for_each_ref_in_submodule(const char *submodule, const char *prefix,
-                             each_ref_fn fn, void *cb_data)
-{
-       return refs_for_each_ref_in(get_submodule_ref_store(submodule),
-                                   prefix, fn, cb_data);
-}
-
-int for_each_fullref_in_submodule(const char *submodule, const char *prefix,
-                                 each_ref_fn fn, void *cb_data,
-                                 unsigned int broken)
+int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
+                            each_ref_fn fn, void *cb_data,
+                            unsigned int broken)
 {
        unsigned int flag = 0;
 
        if (broken)
                flag = DO_FOR_EACH_INCLUDE_BROKEN;
-       return do_for_each_ref(get_submodule_ref_store(submodule),
-                              prefix, fn, 0, flag, cb_data);
+       return do_for_each_ref(refs, prefix, fn, 0, flag, cb_data);
 }
 
 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
@@ -1358,7 +1361,7 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
        return do_for_each_ref(get_main_ref_store(),
                               git_replace_ref_base, fn,
                               strlen(git_replace_ref_base),
-                              0, cb_data);
+                              DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
 }
 
 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
@@ -1397,9 +1400,12 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
                                    unsigned char *sha1, int *flags)
 {
        static struct strbuf sb_refname = STRBUF_INIT;
+       struct object_id unused_oid;
        int unused_flags;
        int symref_count;
 
+       if (!sha1)
+               sha1 = unused_oid.hash;
        if (!flags)
                flags = &unused_flags;
 
@@ -1485,25 +1491,10 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
 int resolve_gitlink_ref(const char *submodule, const char *refname,
                        unsigned char *sha1)
 {
-       size_t len = strlen(submodule);
        struct ref_store *refs;
        int flags;
 
-       while (len && submodule[len - 1] == '/')
-               len--;
-
-       if (!len)
-               return -1;
-
-       if (submodule[len]) {
-               /* We need to strip off one or more trailing slashes */
-               char *stripped = xmemdupz(submodule, len);
-
-               refs = get_submodule_ref_store(stripped);
-               free(stripped);
-       } else {
-               refs = get_submodule_ref_store(submodule);
-       }
+       refs = get_submodule_ref_store(submodule);
 
        if (!refs)
                return -1;
@@ -1618,31 +1609,32 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
 {
        struct strbuf submodule_sb = STRBUF_INIT;
        struct ref_store *refs;
-       int ret;
+       char *to_free = NULL;
+       size_t len;
 
-       if (!submodule || !*submodule) {
-               /*
-                * FIXME: This case is ideally not allowed. But that
-                * can't happen until we clean up all the callers.
-                */
-               return get_main_ref_store();
-       }
+       if (!submodule)
+               return NULL;
+
+       len = strlen(submodule);
+       while (len && is_dir_sep(submodule[len - 1]))
+               len--;
+       if (!len)
+               return NULL;
+
+       if (submodule[len])
+               /* We need to strip off one or more trailing slashes */
+               submodule = to_free = xmemdupz(submodule, len);
 
        refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
        if (refs)
-               return refs;
+               goto done;
 
        strbuf_addstr(&submodule_sb, submodule);
-       ret = is_nonbare_repository_dir(&submodule_sb);
-       strbuf_release(&submodule_sb);
-       if (!ret)
-               return NULL;
+       if (!is_nonbare_repository_dir(&submodule_sb))
+               goto done;
 
-       ret = submodule_to_gitdir(&submodule_sb, submodule);
-       if (ret) {
-               strbuf_release(&submodule_sb);
-               return NULL;
-       }
+       if (submodule_to_gitdir(&submodule_sb, submodule))
+               goto done;
 
        /* assume that add_submodule_odb() has been called */
        refs = ref_store_init(submodule_sb.buf,
@@ -1650,7 +1642,10 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
        register_ref_store_map(&submodule_ref_stores, "submodule",
                               refs, submodule);
 
+done:
        strbuf_release(&submodule_sb);
+       free(to_free);
+
        return refs;
 }
 
@@ -1695,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)
@@ -2045,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 6daa78eb50ddc195c6d5bfb568d0b05ab08f7923..a02b628c8fe81b648f13c66b7e618d8660151e77 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -10,10 +10,11 @@ struct worktree;
 /*
  * Resolve a reference, recursively following symbolic refererences.
  *
- * Store the referred-to object's name in sha1 and return the name of
- * the non-symbolic reference that ultimately pointed at it.  The
- * return value, if not NULL, is a pointer into either a static buffer
- * or the input ref.
+ * Return the name of the non-symbolic reference that ultimately pointed
+ * at the resolved object name.  The return value, if not NULL, is a
+ * pointer into either a static buffer or the input ref.
+ *
+ * If sha1 is non-NULL, store the referred-to object's name in it.
  *
  * If the reference cannot be resolved to an object, the behavior
  * depends on the RESOLVE_REF_READING flag:
@@ -275,6 +276,8 @@ typedef int each_ref_fn(const char *refname,
  * modifies the reference also returns a nonzero value to immediately
  * stop the iteration. Returned references are sorted.
  */
+int refs_head_ref(struct ref_store *refs,
+                 each_ref_fn fn, void *cb_data);
 int refs_for_each_ref(struct ref_store *refs,
                      each_ref_fn fn, void *cb_data);
 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
@@ -289,6 +292,9 @@ int refs_for_each_remote_ref(struct ref_store *refs,
 int head_ref(each_ref_fn fn, void *cb_data);
 int for_each_ref(each_ref_fn fn, void *cb_data);
 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
+int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
+                            each_ref_fn fn, void *cb_data,
+                            unsigned int broken);
 int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data,
                        unsigned int broken);
 int for_each_tag_ref(each_ref_fn fn, void *cb_data);
@@ -299,21 +305,6 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
 int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
                         const char *prefix, void *cb_data);
 
-int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
-int for_each_ref_submodule(const char *submodule,
-                          each_ref_fn fn, void *cb_data);
-int for_each_ref_in_submodule(const char *submodule, const char *prefix,
-                             each_ref_fn fn, void *cb_data);
-int for_each_fullref_in_submodule(const char *submodule, const char *prefix,
-                                 each_ref_fn fn, void *cb_data,
-                                 unsigned int broken);
-int for_each_tag_ref_submodule(const char *submodule,
-                              each_ref_fn fn, void *cb_data);
-int for_each_branch_ref_submodule(const char *submodule,
-                                 each_ref_fn fn, void *cb_data);
-int for_each_remote_ref_submodule(const char *submodule,
-                                 each_ref_fn fn, void *cb_data);
-
 int head_ref_namespaced(each_ref_fn fn, void *cb_data);
 int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
 
@@ -354,6 +345,14 @@ int refs_pack_refs(struct ref_store *refs, unsigned int flags);
 #define REF_NODEREF    0x01
 #define REF_FORCE_CREATE_REFLOG 0x40
 
+/*
+ * Flags that can be passed in to ref_transaction_update
+ */
+#define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
+       REF_ISPRUNING |                      \
+       REF_FORCE_CREATE_REFLOG |            \
+       REF_NODEREF
+
 /*
  * Setup reflog before using. Fill in err and return -1 on failure.
  */
@@ -443,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 0404f2c2333c0fadeb57d225deedc6f0d4afadad..014dabb0bf3647b9392d6087ebf6c40e1bc68108 100644 (file)
@@ -3,6 +3,7 @@
 #include "../refs.h"
 #include "refs-internal.h"
 #include "ref-cache.h"
+#include "packed-backend.h"
 #include "../iterator.h"
 #include "../dir-iterator.h"
 #include "../lockfile.h"
 
 struct ref_lock {
        char *ref_name;
-       struct lock_file *lk;
+       struct lock_file lk;
        struct object_id old_oid;
 };
 
-/*
- * Return true if refname, which has the specified oid and flags, can
- * be resolved to an object in the database. If the referred-to object
- * does not exist, emit a warning and return false.
- */
-static int ref_resolves_to_object(const char *refname,
-                                 const struct object_id *oid,
-                                 unsigned int flags)
-{
-       if (flags & REF_ISBROKEN)
-               return 0;
-       if (!has_sha1_file(oid->hash)) {
-               error("%s does not point to a valid object!", refname);
-               return 0;
-       }
-       return 1;
-}
-
-struct packed_ref_cache {
-       struct ref_cache *cache;
-
-       /*
-        * 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.
-        */
-       unsigned int referrers;
-
-       /* The metadata from when this packed-refs cache was read */
-       struct stat_validity validity;
-};
-
 /*
  * Future: need to be in "struct repository"
  * when doing a full libification.
@@ -58,54 +26,12 @@ struct files_ref_store {
 
        char *gitdir;
        char *gitcommondir;
-       char *packed_refs_path;
 
        struct ref_cache *loose;
-       struct packed_ref_cache *packed;
 
-       /*
-        * Lock used for the "packed-refs" file. Note that this (and
-        * thus the enclosing `files_ref_store`) must not be freed.
-        */
-       struct lock_file packed_refs_lock;
+       struct ref_store *packed_ref_store;
 };
 
-/*
- * Increment the reference count of *packed_refs.
- */
-static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
-{
-       packed_refs->referrers++;
-}
-
-/*
- * Decrease the reference count of *packed_refs.  If it goes to zero,
- * free *packed_refs and return true; otherwise return false.
- */
-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;
-       }
-}
-
-static void clear_packed_ref_cache(struct files_ref_store *refs)
-{
-       if (refs->packed) {
-               struct packed_ref_cache *packed_refs = refs->packed;
-
-               if (is_lock_file_locked(&refs->packed_refs_lock))
-                       die("BUG: packed-ref cache cleared while locked");
-               refs->packed = NULL;
-               release_packed_ref_cache(packed_refs);
-       }
-}
-
 static void clear_loose_ref_cache(struct files_ref_store *refs)
 {
        if (refs->loose) {
@@ -132,7 +58,8 @@ static struct ref_store *files_ref_store_create(const char *gitdir,
        get_common_dir_noenv(&sb, gitdir);
        refs->gitcommondir = strbuf_detach(&sb, NULL);
        strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
-       refs->packed_refs_path = strbuf_detach(&sb, NULL);
+       refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
+       strbuf_release(&sb);
 
        return ref_store;
 }
@@ -175,169 +102,10 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store,
        return refs;
 }
 
-/* The length of a peeled reference line in packed-refs, including EOL: */
-#define PEELED_LINE_LENGTH 42
-
-/*
- * The packed-refs header line that we write out.  Perhaps other
- * traits will be added later.  The trailing space is required.
- */
-static const char PACKED_REFS_HEADER[] =
-       "# pack-refs with: peeled fully-peeled \n";
-
-/*
- * 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.
- */
-static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
-{
-       const char *ref;
-
-       if (parse_oid_hex(line->buf, oid, &ref) < 0)
-               return NULL;
-       if (!isspace(*ref++))
-               return NULL;
-
-       if (isspace(*ref))
-               return NULL;
-
-       if (line->buf[line->len - 1] != '\n')
-               return NULL;
-       line->buf[--line->len] = 0;
-
-       return ref;
-}
-
-/*
- * 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.
- *
- * A comment line of the form "# pack-refs with: " may contain zero or
- * more traits. We interpret the traits as follows:
- *
- *   No traits:
- *
- *      Probably no references are peeled. But if the file contains a
- *      peeled value for a reference, we will use it.
- *
- *   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:
- *
- *      All references in the file that can be peeled are peeled.
- *      Inversely (and this is more important), any references in the
- *      file for which no peeled value is recorded is not peelable. This
- *      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).
- */
-static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
-{
-       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);
-               }
-       }
-
-       stat_validity_update(&packed_refs->validity, fileno(f));
-
-       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 (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;
-               }
-
-               refname = parse_ref_line(&line, &oid);
-               if (refname) {
-                       int flag = REF_ISPACKED;
-
-                       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);
-                       continue;
-               }
-               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;
-               }
-       }
-
-       fclose(f);
-       strbuf_release(&line);
-
-       return packed_refs;
-}
-
-static const char *files_packed_refs_path(struct files_ref_store *refs)
-{
-       return refs->packed_refs_path;
-}
-
 static void files_reflog_path(struct files_ref_store *refs,
                              struct strbuf *sb,
                              const char *refname)
 {
-       if (!refname) {
-               /*
-                * FIXME: of course this is wrong in multi worktree
-                * setting. To be fixed real soon.
-                */
-               strbuf_addf(sb, "%s/logs", refs->gitcommondir);
-               return;
-       }
-
        switch (ref_type(refname)) {
        case REF_TYPE_PER_WORKTREE:
        case REF_TYPE_PSEUDOREF:
@@ -370,70 +138,6 @@ static void files_ref_path(struct files_ref_store *refs,
        }
 }
 
-/*
- * Check that the packed refs cache (if any) still reflects the
- * contents of the file. If not, clear the cache.
- */
-static void validate_packed_ref_cache(struct files_ref_store *refs)
-{
-       if (refs->packed &&
-           !stat_validity_check(&refs->packed->validity,
-                                files_packed_refs_path(refs)))
-               clear_packed_ref_cache(refs);
-}
-
-/*
- * Get the packed_ref_cache for the specified files_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()`.
- */
-static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
-{
-       const char *packed_refs_file = files_packed_refs_path(refs);
-
-       if (!is_lock_file_locked(&refs->packed_refs_lock))
-               validate_packed_ref_cache(refs);
-
-       if (!refs->packed)
-               refs->packed = read_packed_refs(packed_refs_file);
-
-       return refs->packed;
-}
-
-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 files_ref_store *refs)
-{
-       return get_packed_ref_dir(get_packed_ref_cache(refs));
-}
-
-/*
- * Add a reference to the in-memory packed reference cache.  This may
- * only be called while the packed-refs file is locked (see
- * lock_packed_refs()).  To actually write the packed-refs file, call
- * commit_packed_refs().
- */
-static void add_packed_ref(struct files_ref_store *refs,
-                          const char *refname, const struct object_id *oid)
-{
-       struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
-
-       if (!is_lock_file_locked(&refs->packed_refs_lock))
-               die("BUG: packed refs not locked");
-
-       if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
-               die("Reference has invalid format: '%s'", refname);
-
-       add_ref_entry(get_packed_ref_dir(packed_ref_cache),
-                     create_ref_entry(refname, oid, REF_ISPACKED));
-}
-
 /*
  * Read the loose references from the namespace dirname into dir
  * (without recursing).  dirname must end with '/'.  dir must be the
@@ -556,39 +260,6 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
        return refs->loose;
 }
 
-/*
- * 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 files_ref_store *refs,
-                                       const char *refname)
-{
-       return find_ref_entry(get_packed_refs(refs), refname);
-}
-
-/*
- * A loose ref file doesn't exist; check for a packed ref.
- */
-static int resolve_packed_ref(struct files_ref_store *refs,
-                             const char *refname,
-                             unsigned char *sha1, unsigned int *flags)
-{
-       struct ref_entry *entry;
-
-       /*
-        * The loose reference file does not exist; check for a packed
-        * reference.
-        */
-       entry = get_packed_ref(refs, refname);
-       if (entry) {
-               hashcpy(sha1, entry->u.value.oid.hash);
-               *flags |= REF_ISPACKED;
-               return 0;
-       }
-       /* refname is not a packed reference. */
-       return -1;
-}
-
 static int files_read_raw_ref(struct ref_store *ref_store,
                              const char *refname, unsigned char *sha1,
                              struct strbuf *referent, unsigned int *type)
@@ -632,7 +303,8 @@ static int files_read_raw_ref(struct ref_store *ref_store,
        if (lstat(path, &st) < 0) {
                if (errno != ENOENT)
                        goto out;
-               if (resolve_packed_ref(refs, refname, sha1, type)) {
+               if (refs_read_raw_ref(refs->packed_ref_store, refname,
+                                     sha1, referent, type)) {
                        errno = ENOENT;
                        goto out;
                }
@@ -671,7 +343,8 @@ static int files_read_raw_ref(struct ref_store *ref_store,
                 * ref is supposed to be, there could still be a
                 * packed ref:
                 */
-               if (resolve_packed_ref(refs, refname, sha1, type)) {
+               if (refs_read_raw_ref(refs->packed_ref_store, refname,
+                                     sha1, referent, type)) {
                        errno = EISDIR;
                        goto out;
                }
@@ -736,9 +409,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 
 static void unlock_ref(struct ref_lock *lock)
 {
-       /* Do not free lock->lk -- atexit() still looks at them */
-       if (lock->lk)
-               rollback_lock_file(lock->lk);
+       rollback_lock_file(&lock->lk);
        free(lock->ref_name);
        free(lock);
 }
@@ -852,10 +523,9 @@ static int lock_raw_ref(struct files_ref_store *refs,
                goto error_return;
        }
 
-       if (!lock->lk)
-               lock->lk = xcalloc(1, sizeof(struct lock_file));
-
-       if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {
+       if (hold_lock_file_for_update_timeout(
+                           &lock->lk, ref_file.buf, LOCK_NO_DEREF,
+                           get_files_ref_lock_timeout_ms()) < 0) {
                if (errno == ENOENT && --attempts_remaining > 0) {
                        /*
                         * Maybe somebody just deleted one of the
@@ -950,11 +620,11 @@ static int lock_raw_ref(struct files_ref_store *refs,
 
                /*
                 * If the ref did not exist and we are creating it,
-                * make sure there is no existing ref that conflicts
-                * with refname:
+                * make sure there is no existing packed ref that
+                * conflicts with refname:
                 */
                if (refs_verify_refname_available(
-                                   &refs->base, refname,
+                                   refs->packed_ref_store, refname,
                                    extras, skip, err))
                        goto error_return;
        }
@@ -971,53 +641,9 @@ 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) {
-               struct ref_entry *r = get_packed_ref(refs, refname);
-               if (r) {
-                       if (peel_entry(r, 0))
-                               return -1;
-                       hashcpy(sha1, r->u.value.peeled.hash);
-                       return 0;
-               }
-       }
-
-       return peel_object(base, sha1);
-}
-
 struct files_ref_iterator {
        struct ref_iterator base;
 
-       struct packed_ref_cache *packed_ref_cache;
        struct ref_iterator *iter0;
        unsigned int flags;
 };
@@ -1070,7 +696,6 @@ static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
        if (iter->iter0)
                ok = ref_iterator_abort(iter->iter0);
 
-       release_packed_ref_cache(iter->packed_ref_cache);
        base_ref_iterator_free(ref_iterator);
        return ok;
 }
@@ -1086,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;
@@ -1096,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
@@ -1112,20 +733,36 @@ static struct ref_iterator *files_ref_iterator_begin(
         * (If they've already been read, that's OK; we only need to
         * guarantee that they're read before the packed refs, not
         * *how much* before.) After that, we call
-        * get_packed_ref_cache(), which internally checks whether the
-        * packed-ref cache is up to date with what is on disk, and
-        * re-reads it if not.
+        * packed_ref_iterator_begin(), which internally checks
+        * whether the packed-ref cache is up to date with what is on
+        * disk, and re-reads it if not.
         */
 
        loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
                                              prefix, 1);
 
-       iter->packed_ref_cache = get_packed_ref_cache(refs);
-       acquire_packed_ref_cache(iter->packed_ref_cache);
-       packed_iter = cache_ref_iterator_begin(iter->packed_ref_cache->cache,
-                                              prefix, 0);
+       /*
+        * The packed-refs file might contain broken references, for
+        * example an old version of a reference that points at an
+        * object that has since been garbage-collected. This is OK as
+        * long as there is a corresponding loose reference that
+        * overrides it, and we don't want to emit an error message in
+        * this case. So ask the packed_ref_store for all of its
+        * references, and (if needed) do our own check for broken
+        * ones in files_ref_iterator_advance(), after we have merged
+        * the packed and loose references.
+        */
+       packed_iter = refs_ref_iterator_begin(
+                       refs->packed_ref_store, prefix, 0,
+                       DO_FOR_EACH_INCLUDE_BROKEN);
+
+       overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter);
 
-       iter->iter0 = 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;
@@ -1181,7 +818,9 @@ static int create_reflock(const char *path, void *cb)
 {
        struct lock_file *lk = cb;
 
-       return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;
+       return hold_lock_file_for_update_timeout(
+                       lk, path, LOCK_NO_DEREF,
+                       get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
 }
 
 /*
@@ -1255,17 +894,15 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
         * our refname.
         */
        if (is_null_oid(&lock->old_oid) &&
-           refs_verify_refname_available(&refs->base, refname,
+           refs_verify_refname_available(refs->packed_ref_store, refname,
                                          extras, skip, err)) {
                last_errno = ENOTDIR;
                goto error_return;
        }
 
-       lock->lk = xcalloc(1, sizeof(struct lock_file));
-
        lock->ref_name = xstrdup(refname);
 
-       if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {
+       if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) {
                last_errno = errno;
                unable_to_lock_message(ref_file.buf, errno, err);
                goto error_return;
@@ -1287,124 +924,6 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
        return lock;
 }
 
-/*
- * Write an entry to the packed-refs file for the specified refname.
- * If peeled is non-NULL, write it as the entry's peeled value.
- */
-static void write_packed_entry(FILE *fh, const char *refname,
-                              const unsigned char *sha1,
-                              const unsigned char *peeled)
-{
-       fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
-       if (peeled)
-               fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
-}
-
-/*
- * Lock the packed-refs file for writing. Flags is passed to
- * hold_lock_file_for_update(). Return 0 on success. On errors, set
- * errno appropriately and return a nonzero value.
- */
-static int lock_packed_refs(struct files_ref_store *refs, int flags)
-{
-       static int timeout_configured = 0;
-       static int timeout_value = 1000;
-       struct packed_ref_cache *packed_ref_cache;
-
-       files_assert_main_repository(refs, "lock_packed_refs");
-
-       if (!timeout_configured) {
-               git_config_get_int("core.packedrefstimeout", &timeout_value);
-               timeout_configured = 1;
-       }
-
-       if (hold_lock_file_for_update_timeout(
-                           &refs->packed_refs_lock, files_packed_refs_path(refs),
-                           flags, timeout_value) < 0)
-               return -1;
-
-       /*
-        * 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.
-        */
-       validate_packed_ref_cache(refs);
-
-       packed_ref_cache = get_packed_ref_cache(refs);
-       /* Increment the reference count to prevent it from being freed: */
-       acquire_packed_ref_cache(packed_ref_cache);
-       return 0;
-}
-
-/*
- * Write the current version of the packed refs cache from memory to
- * disk. The packed-refs file must already be locked for writing (see
- * lock_packed_refs()). Return zero on success. On errors, set errno
- * and return a nonzero value
- */
-static int commit_packed_refs(struct files_ref_store *refs)
-{
-       struct packed_ref_cache *packed_ref_cache =
-               get_packed_ref_cache(refs);
-       int ok, error = 0;
-       int save_errno = 0;
-       FILE *out;
-       struct ref_iterator *iter;
-
-       files_assert_main_repository(refs, "commit_packed_refs");
-
-       if (!is_lock_file_locked(&refs->packed_refs_lock))
-               die("BUG: packed-refs not locked");
-
-       out = fdopen_lock_file(&refs->packed_refs_lock, "w");
-       if (!out)
-               die_errno("unable to fdopen packed-refs descriptor");
-
-       fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
-
-       iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
-       while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
-               struct object_id peeled;
-               int peel_error = ref_iterator_peel(iter, &peeled);
-
-               write_packed_entry(out, iter->refname, iter->oid->hash,
-                                  peel_error ? NULL : peeled.hash);
-       }
-
-       if (ok != ITER_DONE)
-               die("error while iterating over references");
-
-       if (commit_lock_file(&refs->packed_refs_lock)) {
-               save_errno = errno;
-               error = -1;
-       }
-       release_packed_ref_cache(packed_ref_cache);
-       errno = save_errno;
-       return error;
-}
-
-/*
- * Rollback the lockfile for the packed-refs file, and discard the
- * in-memory packed reference cache.  (The packed-refs file will be
- * read anew if it is needed again after this function is called.)
- */
-static void rollback_packed_refs(struct files_ref_store *refs)
-{
-       struct packed_ref_cache *packed_ref_cache =
-               get_packed_ref_cache(refs);
-
-       files_assert_main_repository(refs, "rollback_packed_refs");
-
-       if (!is_lock_file_locked(&refs->packed_refs_lock))
-               die("BUG: packed-refs not locked");
-       rollback_lock_file(&refs->packed_refs_lock);
-       release_packed_ref_cache(packed_ref_cache);
-       clear_packed_ref_cache(refs);
-}
-
 struct ref_to_prune {
        struct ref_to_prune *next;
        unsigned char sha1[20];
@@ -1487,11 +1006,17 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
        strbuf_release(&err);
 }
 
-static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)
+/*
+ * Prune the loose versions of the references in the linked list
+ * `*refs_to_prune`, freeing the entries in the list as we go.
+ */
+static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune)
 {
-       while (r) {
+       while (*refs_to_prune) {
+               struct ref_to_prune *r = *refs_to_prune;
+               *refs_to_prune = r->next;
                prune_ref(refs, r);
-               r = r->next;
+               free(r);
        }
 }
 
@@ -1527,12 +1052,16 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
                files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
                               "pack_refs");
        struct ref_iterator *iter;
-       struct ref_dir *packed_refs;
        int ok;
        struct ref_to_prune *refs_to_prune = NULL;
+       struct strbuf err = STRBUF_INIT;
+       struct ref_transaction *transaction;
+
+       transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
+       if (!transaction)
+               return -1;
 
-       lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
-       packed_refs = get_packed_refs(refs);
+       packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
 
        iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);
        while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
@@ -1541,29 +1070,19 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
                 * in the packed ref cache. If the reference should be
                 * pruned, also add it to refs_to_prune.
                 */
-               struct ref_entry *packed_entry;
-
                if (!should_pack_ref(iter->refname, iter->oid, iter->flags,
                                     flags))
                        continue;
 
                /*
-                * Create an entry in the packed-refs cache equivalent
-                * to the one from the loose ref cache, except that
-                * we don't copy the peeled status, because we want it
-                * to be re-peeled.
+                * Add a reference creation for this reference to the
+                * packed-refs transaction:
                 */
-               packed_entry = find_ref_entry(packed_refs, iter->refname);
-               if (packed_entry) {
-                       /* Overwrite existing packed entry with info from loose entry */
-                       packed_entry->flag = REF_ISPACKED;
-                       oidcpy(&packed_entry->u.value.oid, iter->oid);
-               } else {
-                       packed_entry = create_ref_entry(iter->refname, iter->oid,
-                                                       REF_ISPACKED);
-                       add_ref_entry(packed_refs, packed_entry);
-               }
-               oidclr(&packed_entry->u.value.peeled);
+               if (ref_transaction_update(transaction, iter->refname,
+                                          iter->oid->hash, NULL,
+                                          REF_NODEREF, NULL, &err))
+                       die("failure preparing to create packed reference %s: %s",
+                           iter->refname, err.buf);
 
                /* Schedule the loose reference for pruning if requested. */
                if ((flags & PACK_REFS_PRUNE)) {
@@ -1577,67 +1096,16 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
        if (ok != ITER_DONE)
                die("error while iterating over references");
 
-       if (commit_packed_refs(refs))
-               die_errno("unable to overwrite old ref-pack file");
-
-       prune_refs(refs, refs_to_prune);
-       return 0;
-}
+       if (ref_transaction_commit(transaction, &err))
+               die("unable to write new packed-refs: %s", err.buf);
 
-/*
- * Rewrite the packed-refs file, omitting any refs listed in
- * 'refnames'. On error, leave packed-refs unchanged, write an error
- * message to 'err', and return a nonzero value.
- *
- * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
- */
-static int repack_without_refs(struct files_ref_store *refs,
-                              struct string_list *refnames, struct strbuf *err)
-{
-       struct ref_dir *packed;
-       struct string_list_item *refname;
-       int ret, needs_repacking = 0, removed = 0;
-
-       files_assert_main_repository(refs, "repack_without_refs");
-       assert(err);
-
-       /* Look for a packed ref */
-       for_each_string_list_item(refname, refnames) {
-               if (get_packed_ref(refs, refname->string)) {
-                       needs_repacking = 1;
-                       break;
-               }
-       }
-
-       /* Avoid locking if we have nothing to do */
-       if (!needs_repacking)
-               return 0; /* no refname exists in packed refs */
-
-       if (lock_packed_refs(refs, 0)) {
-               unable_to_lock_message(files_packed_refs_path(refs), errno, err);
-               return -1;
-       }
-       packed = get_packed_refs(refs);
+       ref_transaction_free(transaction);
 
-       /* Remove refnames from the cache */
-       for_each_string_list_item(refname, refnames)
-               if (remove_entry_from_dir(packed, refname->string) != -1)
-                       removed = 1;
-       if (!removed) {
-               /*
-                * All packed entries disappeared while we were
-                * acquiring the lock.
-                */
-               rollback_packed_refs(refs);
-               return 0;
-       }
+       packed_refs_unlock(refs->packed_ref_store);
 
-       /* Write what remains */
-       ret = commit_packed_refs(refs);
-       if (ret)
-               strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
-                           strerror(errno));
-       return ret;
+       prune_refs(refs, &refs_to_prune);
+       strbuf_release(&err);
+       return 0;
 }
 
 static int files_delete_refs(struct ref_store *ref_store, const char *msg,
@@ -1651,24 +1119,16 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
        if (!refnames->nr)
                return 0;
 
-       result = repack_without_refs(refs, refnames, &err);
-       if (result) {
-               /*
-                * If we failed to rewrite the packed-refs file, then
-                * it is unsafe to try to remove loose refs, because
-                * doing so might expose an obsolete packed value for
-                * a reference that might even point at an object that
-                * has been garbage collected.
-                */
-               if (refnames->nr == 1)
-                       error(_("could not delete reference %s: %s"),
-                             refnames->items[0].string, err.buf);
-               else
-                       error(_("could not delete references: %s"), err.buf);
+       if (packed_refs_lock(refs->packed_ref_store, 0, &err))
+               goto error;
 
-               goto out;
+       if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {
+               packed_refs_unlock(refs->packed_ref_store);
+               goto error;
        }
 
+       packed_refs_unlock(refs->packed_ref_store);
+
        for (i = 0; i < refnames->nr; i++) {
                const char *refname = refnames->items[i].string;
 
@@ -1676,9 +1136,24 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
                        result |= error(_("could not remove reference %s"), refname);
        }
 
-out:
        strbuf_release(&err);
        return result;
+
+error:
+       /*
+        * If we failed to rewrite the packed-refs file, then it is
+        * unsafe to try to remove loose refs, because doing so might
+        * expose an obsolete packed value for a reference that might
+        * even point at an object that has been garbage collected.
+        */
+       if (refnames->nr == 1)
+               error(_("could not delete reference %s: %s"),
+                     refnames->items[0].string, err.buf);
+       else
+               error(_("could not delete references: %s"), err.buf);
+
+       strbuf_release(&err);
+       return -1;
 }
 
 /*
@@ -1748,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");
@@ -1782,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)) {
@@ -1791,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;
@@ -1810,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,
@@ -1841,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;
        }
@@ -1892,16 +1380,32 @@ static int files_rename_ref(struct ref_store *ref_store,
        return ret;
 }
 
-static int close_ref(struct ref_lock *lock)
+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(lock->lk))
+       if (close_lock_file_gently(&lock->lk))
                return -1;
        return 0;
 }
 
 static int commit_ref(struct ref_lock *lock)
 {
-       char *path = get_locked_file_path(lock->lk);
+       char *path = get_locked_file_path(&lock->lk);
        struct stat st;
 
        if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
@@ -1925,7 +1429,7 @@ static int commit_ref(struct ref_lock *lock)
                free(path);
        }
 
-       if (commit_lock_file(lock->lk))
+       if (commit_lock_file(&lock->lk))
                return -1;
        return 0;
 }
@@ -2039,7 +1543,7 @@ static int log_ref_write_fd(int fd, const struct object_id *old_oid,
 
        written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
        free(logrec);
-       if (written != len)
+       if (written < 0)
                return -1;
 
        return 0;
@@ -2117,12 +1621,12 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
                unlock_ref(lock);
                return -1;
        }
-       fd = get_lock_file_fd(lock->lk);
-       if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
-           write_in_full(fd, &term, 1) != 1 ||
-           close_ref(lock) < 0) {
+       fd = get_lock_file_fd(&lock->lk);
+       if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 ||
+           write_in_full(fd, &term, 1) < 0 ||
+           close_ref_gently(lock) < 0) {
                strbuf_addf(err,
-                           "couldn't write '%s'", get_lock_file_path(lock->lk));
+                           "couldn't write '%s'", get_lock_file_path(&lock->lk));
                unlock_ref(lock);
                return -1;
        }
@@ -2166,13 +1670,12 @@ static int commit_ref_update(struct files_ref_store *refs,
                 * check with HEAD only which should cover 99% of all usage
                 * scenarios (even 100% of the default ones).
                 */
-               struct object_id head_oid;
                int head_flag;
                const char *head_ref;
 
                head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
                                                   RESOLVE_REF_READING,
-                                                  head_oid.hash, &head_flag);
+                                                  NULL, &head_flag);
                if (head_ref && (head_flag & REF_ISSYMREF) &&
                    !strcmp(head_ref, lock->ref_name)) {
                        struct strbuf log_err = STRBUF_INIT;
@@ -2199,7 +1702,7 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
 {
        int ret = -1;
 #ifndef NO_SYMLINK_HEAD
-       char *ref_path = get_locked_file_path(lock->lk);
+       char *ref_path = get_locked_file_path(&lock->lk);
        unlink(ref_path);
        ret = symlink(target, ref_path);
        free(ref_path);
@@ -2235,14 +1738,14 @@ static int create_symref_locked(struct files_ref_store *refs,
                return 0;
        }
 
-       if (!fdopen_lock_file(lock->lk, "w"))
+       if (!fdopen_lock_file(&lock->lk, "w"))
                return error("unable to fdopen %s: %s",
-                            lock->lk->tempfile.filename.buf, strerror(errno));
+                            lock->lk.tempfile->filename.buf, strerror(errno));
 
        update_symref_reflog(refs, lock, refname, target, logmsg);
 
        /* no error check; commit_ref will check ferror */
-       fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
+       fprintf(lock->lk.tempfile->fp, "ref: %s\n", target);
        if (commit_ref(lock) < 0)
                return error("unable to write symref for %s: %s", refname,
                             strerror(errno));
@@ -2549,23 +2052,64 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {
        files_reflog_iterator_abort
 };
 
-static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
+static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
+                                                 const char *gitdir)
 {
-       struct files_ref_store *refs =
-               files_downcast(ref_store, REF_STORE_READ,
-                              "reflog_iterator_begin");
        struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
        struct ref_iterator *ref_iterator = &iter->base;
        struct strbuf sb = STRBUF_INIT;
 
-       base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
-       files_reflog_path(refs, &sb, NULL);
+       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;
        strbuf_release(&sb);
+
        return ref_iterator;
 }
 
+static enum iterator_selection reflog_iterator_select(
+       struct ref_iterator *iter_worktree,
+       struct ref_iterator *iter_common,
+       void *cb_data)
+{
+       if (iter_worktree) {
+               /*
+                * We're a bit loose here. We probably should ignore
+                * common refs if they are accidentally added as
+                * per-worktree refs.
+                */
+               return ITER_SELECT_0;
+       } else if (iter_common) {
+               if (ref_type(iter_common->refname) == REF_TYPE_NORMAL)
+                       return ITER_SELECT_1;
+
+               /*
+                * The main ref store may contain main worktree's
+                * per-worktree refs, which should be ignored
+                */
+               return ITER_SKIP_1;
+       } else
+               return ITER_DONE;
+}
+
+static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
+{
+       struct files_ref_store *refs =
+               files_downcast(ref_store, REF_STORE_READ,
+                              "reflog_iterator_begin");
+
+       if (!strcmp(refs->gitdir, refs->gitcommondir)) {
+               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);
+       }
+}
+
 /*
  * If update is a direct update of head_ref (the reference pointed to
  * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
@@ -2589,11 +2133,10 @@ static int split_head_update(struct ref_update *update,
 
        /*
         * First make sure that HEAD is not already in the
-        * transaction. This insertion is O(N) in the transaction
+        * transaction. This check is O(lg N) in the transaction
         * size, but it happens at most once per transaction.
         */
-       item = string_list_insert(affected_refnames, "HEAD");
-       if (item->util) {
+       if (string_list_has_string(affected_refnames, "HEAD")) {
                /* An entry already existed */
                strbuf_addf(err,
                            "multiple updates for 'HEAD' (including one "
@@ -2608,6 +2151,14 @@ static int split_head_update(struct ref_update *update,
                        update->new_oid.hash, update->old_oid.hash,
                        update->msg);
 
+       /*
+        * Add "HEAD". This insertion is O(N) in the transaction
+        * size, but it happens at most once per transaction.
+        * Add new_update->refname instead of a literal "HEAD".
+        */
+       if (strcmp(new_update->refname, "HEAD"))
+               BUG("%s unexpectedly not 'HEAD'", new_update->refname);
+       item = string_list_insert(affected_refnames, new_update->refname);
        item->util = new_update;
 
        return 0;
@@ -2634,13 +2185,12 @@ static int split_symref_update(struct files_ref_store *refs,
 
        /*
         * First make sure that referent is not already in the
-        * transaction. This insertion is O(N) in the transaction
+        * transaction. This check is O(lg N) in the transaction
         * size, but it happens at most once per symref in a
         * transaction.
         */
-       item = string_list_insert(affected_refnames, referent);
-       if (item->util) {
-               /* An entry already existed */
+       if (string_list_has_string(affected_refnames, referent)) {
+               /* An entry already exists */
                strbuf_addf(err,
                            "multiple updates for '%s' (including one "
                            "via symref '%s') are not allowed",
@@ -2675,6 +2225,17 @@ static int split_symref_update(struct files_ref_store *refs,
        update->flags |= REF_LOG_ONLY | REF_NODEREF;
        update->flags &= ~REF_HAVE_OLD;
 
+       /*
+        * Add the referent. This insertion is O(N) in the transaction
+        * size, but it happens at most once per symref in a
+        * transaction. Make sure to add new_update->refname, which will
+        * be valid as long as affected_refnames is in use, and NOT
+        * referent, which might soon be freed by our caller.
+        */
+       item = string_list_insert(affected_refnames, new_update->refname);
+       if (item->util)
+               BUG("%s unexpectedly found in affected_refnames",
+                   new_update->refname);
        item->util = new_update;
 
        return 0;
@@ -2746,7 +2307,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
        struct strbuf referent = STRBUF_INIT;
        int mustexist = (update->flags & REF_HAVE_OLD) &&
                !is_null_oid(&update->old_oid);
-       int ret;
+       int ret = 0;
        struct ref_lock *lock;
 
        files_assert_main_repository(refs, "lock_ref_for_update");
@@ -2758,7 +2319,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                ret = split_head_update(update, transaction, head_ref,
                                        affected_refnames, err);
                if (ret)
-                       return ret;
+                       goto out;
        }
 
        ret = lock_raw_ref(refs, update->refname, mustexist,
@@ -2772,7 +2333,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                strbuf_addf(err, "cannot lock ref '%s': %s",
                            original_update_refname(update), reason);
                free(reason);
-               return ret;
+               goto out;
        }
 
        update->backend_data = lock;
@@ -2791,10 +2352,12 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                                        strbuf_addf(err, "cannot lock ref '%s': "
                                                    "error reading reference",
                                                    original_update_refname(update));
-                                       return -1;
+                                       ret = TRANSACTION_GENERIC_ERROR;
+                                       goto out;
                                }
                        } else if (check_old_oid(update, &lock->old_oid, err)) {
-                               return TRANSACTION_GENERIC_ERROR;
+                               ret = TRANSACTION_GENERIC_ERROR;
+                               goto out;
                        }
                } else {
                        /*
@@ -2808,13 +2371,15 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                                                  referent.buf, transaction,
                                                  affected_refnames, err);
                        if (ret)
-                               return ret;
+                               goto out;
                }
        } else {
                struct ref_update *parent_update;
 
-               if (check_old_oid(update, &lock->old_oid, err))
-                       return TRANSACTION_GENERIC_ERROR;
+               if (check_old_oid(update, &lock->old_oid, err)) {
+                       ret = TRANSACTION_GENERIC_ERROR;
+                       goto out;
+               }
 
                /*
                 * If this update is happening indirectly because of a
@@ -2851,7 +2416,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                                    "cannot update ref '%s': %s",
                                    update->refname, write_err);
                        free(write_err);
-                       return TRANSACTION_GENERIC_ERROR;
+                       ret = TRANSACTION_GENERIC_ERROR;
+                       goto out;
                } else {
                        update->flags |= REF_NEEDS_COMMIT;
                }
@@ -2862,22 +2428,35 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                 * the lockfile is still open. Close it to
                 * free up the file descriptor:
                 */
-               if (close_ref(lock)) {
+               if (close_ref_gently(lock)) {
                        strbuf_addf(err, "couldn't close '%s.lock'",
                                    update->refname);
-                       return TRANSACTION_GENERIC_ERROR;
+                       ret = TRANSACTION_GENERIC_ERROR;
+                       goto out;
                }
        }
-       return 0;
+
+out:
+       strbuf_release(&referent);
+       return ret;
 }
 
+struct files_transaction_backend_data {
+       struct ref_transaction *packed_transaction;
+       int packed_refs_locked;
+};
+
 /*
  * Unlock any references in `transaction` that are still locked, and
  * mark the transaction closed.
  */
-static void files_transaction_cleanup(struct ref_transaction *transaction)
+static void files_transaction_cleanup(struct files_ref_store *refs,
+                                     struct ref_transaction *transaction)
 {
        size_t i;
+       struct files_transaction_backend_data *backend_data =
+               transaction->backend_data;
+       struct strbuf err = STRBUF_INIT;
 
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
@@ -2889,6 +2468,17 @@ static void files_transaction_cleanup(struct ref_transaction *transaction)
                }
        }
 
+       if (backend_data->packed_transaction &&
+           ref_transaction_abort(backend_data->packed_transaction, &err)) {
+               error("error aborting transaction: %s", err.buf);
+               strbuf_release(&err);
+       }
+
+       if (backend_data->packed_refs_locked)
+               packed_refs_unlock(refs->packed_ref_store);
+
+       free(backend_data);
+
        transaction->state = REF_TRANSACTION_CLOSED;
 }
 
@@ -2904,13 +2494,17 @@ 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;
 
        assert(err);
 
        if (!transaction->nr)
                goto cleanup;
 
+       backend_data = xcalloc(1, sizeof(*backend_data));
+       transaction->backend_data = backend_data;
+
        /*
         * Fail if a refname appears more than once in the
         * transaction. (If we end up splitting up any updates using
@@ -2956,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);
@@ -2977,6 +2571,41 @@ static int files_transaction_prepare(struct ref_store *ref_store,
                                          head_ref, &affected_refnames, err);
                if (ret)
                        break;
+
+               if (update->flags & REF_DELETING &&
+                   !(update->flags & REF_LOG_ONLY) &&
+                   !(update->flags & REF_ISPRUNING)) {
+                       /*
+                        * This reference has to be deleted from
+                        * packed-refs if it exists there.
+                        */
+                       if (!packed_transaction) {
+                               packed_transaction = ref_store_transaction_begin(
+                                               refs->packed_ref_store, err);
+                               if (!packed_transaction) {
+                                       ret = TRANSACTION_GENERIC_ERROR;
+                                       goto cleanup;
+                               }
+
+                               backend_data->packed_transaction =
+                                       packed_transaction;
+                       }
+
+                       ref_transaction_add_update(
+                                       packed_transaction, update->refname,
+                                       update->flags & ~REF_HAVE_OLD,
+                                       update->new_oid.hash, update->old_oid.hash,
+                                       NULL);
+               }
+       }
+
+       if (packed_transaction) {
+               if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
+                       ret = TRANSACTION_GENERIC_ERROR;
+                       goto cleanup;
+               }
+               backend_data->packed_refs_locked = 1;
+               ret = ref_transaction_prepare(packed_transaction, err);
        }
 
 cleanup:
@@ -2984,7 +2613,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
        string_list_clear(&affected_refnames, 0);
 
        if (ret)
-               files_transaction_cleanup(transaction);
+               files_transaction_cleanup(refs, transaction);
        else
                transaction->state = REF_TRANSACTION_PREPARED;
 
@@ -2999,9 +2628,10 @@ static int files_transaction_finish(struct ref_store *ref_store,
                files_downcast(ref_store, 0, "ref_transaction_finish");
        size_t i;
        int ret = 0;
-       struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
-       struct string_list_item *ref_to_delete;
        struct strbuf sb = STRBUF_INIT;
+       struct files_transaction_backend_data *backend_data;
+       struct ref_transaction *packed_transaction;
+
 
        assert(err);
 
@@ -3010,6 +2640,9 @@ static int files_transaction_finish(struct ref_store *ref_store,
                return 0;
        }
 
+       backend_data = transaction->backend_data;
+       packed_transaction = backend_data->packed_transaction;
+
        /* Perform updates first so live commits remain referenced */
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
@@ -3045,7 +2678,44 @@ static int files_transaction_finish(struct ref_store *ref_store,
                        }
                }
        }
-       /* Perform deletes now that updates are safely completed */
+
+       /*
+        * Now that updates are safely completed, we can perform
+        * deletes. First delete the reflogs of any references that
+        * will be deleted, since (in the unexpected event of an
+        * error) leaving a reference without a reflog is less bad
+        * than leaving a reflog without a reference (the latter is a
+        * mildly invalid repository state):
+        */
+       for (i = 0; i < transaction->nr; i++) {
+               struct ref_update *update = transaction->updates[i];
+               if (update->flags & REF_DELETING &&
+                   !(update->flags & REF_LOG_ONLY) &&
+                   !(update->flags & REF_ISPRUNING)) {
+                       strbuf_reset(&sb);
+                       files_reflog_path(refs, &sb, update->refname);
+                       if (!unlink_or_warn(sb.buf))
+                               try_remove_empty_parents(refs, update->refname,
+                                                        REMOVE_EMPTY_PARENTS_REFLOG);
+               }
+       }
+
+       /*
+        * Perform deletes now that updates are safely completed.
+        *
+        * First delete any packed versions of the references, while
+        * retaining the packed-refs lock:
+        */
+       if (packed_transaction) {
+               ret = ref_transaction_commit(packed_transaction, err);
+               ref_transaction_free(packed_transaction);
+               packed_transaction = NULL;
+               backend_data->packed_transaction = NULL;
+               if (ret)
+                       goto cleanup;
+       }
+
+       /* Now delete the loose versions of the references: */
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
                struct ref_lock *lock = update->backend_data;
@@ -3063,31 +2733,13 @@ static int files_transaction_finish(struct ref_store *ref_store,
                                }
                                update->flags |= REF_DELETED_LOOSE;
                        }
-
-                       if (!(update->flags & REF_ISPRUNING))
-                               string_list_append(&refs_to_delete,
-                                                  lock->ref_name);
                }
        }
 
-       if (repack_without_refs(refs, &refs_to_delete, err)) {
-               ret = TRANSACTION_GENERIC_ERROR;
-               goto cleanup;
-       }
-
-       /* Delete the reflogs of any references that were deleted: */
-       for_each_string_list_item(ref_to_delete, &refs_to_delete) {
-               strbuf_reset(&sb);
-               files_reflog_path(refs, &sb, ref_to_delete->string);
-               if (!unlink_or_warn(sb.buf))
-                       try_remove_empty_parents(refs, ref_to_delete->string,
-                                                REMOVE_EMPTY_PARENTS_REFLOG);
-       }
-
        clear_loose_ref_cache(refs);
 
 cleanup:
-       files_transaction_cleanup(transaction);
+       files_transaction_cleanup(refs, transaction);
 
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
@@ -3105,7 +2757,6 @@ static int files_transaction_finish(struct ref_store *ref_store,
        }
 
        strbuf_release(&sb);
-       string_list_clear(&refs_to_delete, 0);
        return ret;
 }
 
@@ -3113,7 +2764,10 @@ static int files_transaction_abort(struct ref_store *ref_store,
                                   struct ref_transaction *transaction,
                                   struct strbuf *err)
 {
-       files_transaction_cleanup(transaction);
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 0, "ref_transaction_abort");
+
+       files_transaction_cleanup(refs, transaction);
        return 0;
 }
 
@@ -3135,6 +2789,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
        size_t i;
        int ret = 0;
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+       struct ref_transaction *packed_transaction = NULL;
 
        assert(err);
 
@@ -3167,6 +2822,12 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
                                 &affected_refnames))
                die("BUG: initial ref transaction called with existing refs");
 
+       packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);
+       if (!packed_transaction) {
+               ret = TRANSACTION_GENERIC_ERROR;
+               goto cleanup;
+       }
+
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
 
@@ -3179,32 +2840,31 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
                        ret = TRANSACTION_NAME_CONFLICT;
                        goto cleanup;
                }
+
+               /*
+                * Add a reference creation for this reference to the
+                * packed-refs transaction:
+                */
+               ref_transaction_add_update(packed_transaction, update->refname,
+                                          update->flags & ~REF_HAVE_OLD,
+                                          update->new_oid.hash, update->old_oid.hash,
+                                          NULL);
        }
 
-       if (lock_packed_refs(refs, 0)) {
-               strbuf_addf(err, "unable to lock packed-refs file: %s",
-                           strerror(errno));
+       if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
                ret = TRANSACTION_GENERIC_ERROR;
                goto cleanup;
        }
 
-       for (i = 0; i < transaction->nr; i++) {
-               struct ref_update *update = transaction->updates[i];
-
-               if ((update->flags & REF_HAVE_NEW) &&
-                   !is_null_oid(&update->new_oid))
-                       add_packed_ref(refs, update->refname,
-                                      &update->new_oid);
-       }
-
-       if (commit_packed_refs(refs)) {
-               strbuf_addf(err, "unable to commit packed-refs file: %s",
-                           strerror(errno));
+       if (initial_ref_transaction_commit(packed_transaction, err)) {
                ret = TRANSACTION_GENERIC_ERROR;
                goto cleanup;
        }
 
 cleanup:
+       if (packed_transaction)
+               ref_transaction_free(packed_transaction);
+       packed_refs_unlock(refs->packed_ref_store);
        transaction->state = REF_TRANSACTION_CLOSED;
        string_list_clear(&affected_refnames, 0);
        return ret;
@@ -3333,16 +2993,17 @@ static int files_reflog_expire(struct ref_store *ref_store,
                        !(type & REF_ISSYMREF) &&
                        !is_null_oid(&cb.last_kept_oid);
 
-               if (close_lock_file(&reflog_lock)) {
+               if (close_lock_file_gently(&reflog_lock)) {
                        status |= error("couldn't write %s: %s", log_file,
                                        strerror(errno));
+                       rollback_lock_file(&reflog_lock);
                } else if (update &&
-                          (write_in_full(get_lock_file_fd(lock->lk),
-                               oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
-                           write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
-                           close_ref(lock) < 0)) {
+                          (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") < 0 ||
+                           close_ref_gently(lock) < 0)) {
                        status |= error("couldn't write %s",
-                                       get_lock_file_path(lock->lk));
+                                       get_lock_file_path(&lock->lk));
                        rollback_lock_file(&reflog_lock);
                } else if (commit_lock_file(&reflog_lock)) {
                        status |= error("unable to write reflog '%s' (%s)",
@@ -3393,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);
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
new file mode 100644 (file)
index 0000000..3279d42
--- /dev/null
@@ -0,0 +1,1558 @@
+#include "../cache.h"
+#include "../config.h"
+#include "../refs.h"
+#include "refs-internal.h"
+#include "packed-backend.h"
+#include "../iterator.h"
+#include "../lockfile.h"
+
+enum mmap_strategy {
+       /*
+        * Don't use mmap() at all for reading `packed-refs`.
+        */
+       MMAP_NONE,
+
+       /*
+        * 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.
+        */
+       MMAP_TEMPORARY,
+
+       /*
+        * It is OK to leave the `packed-refs` file mmapped while
+        * arbitrary other code is running.
+        */
+       MMAP_OK
+};
+
+#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;
+
+/*
+ * 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.
+ */
+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 `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;
+
+       unsigned int store_flags;
+
+       /* The path of the "packed-refs" file: */
+       char *path;
+
+       /*
+        * A snapshot of the values read from the `packed-refs` file,
+        * if it might still be current; otherwise, NULL.
+        */
+       struct snapshot *snapshot;
+
+       /*
+        * Lock used for the "packed-refs" file. Note that this (and
+        * thus the enclosing `packed_ref_store`) must not be freed.
+        */
+       struct lock_file lock;
+
+       /*
+        * Temporary file used when rewriting new contents to the
+        * "packed-refs" file. Note that this (and thus the enclosing
+        * `packed_ref_store`) must not be freed.
+        */
+       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)
+{
+       struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
+       struct ref_store *ref_store = (struct ref_store *)refs;
+
+       base_ref_store_init(ref_store, &refs_be_packed);
+       refs->store_flags = store_flags;
+
+       refs->path = xstrdup(path);
+       return ref_store;
+}
+
+/*
+ * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
+ * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
+ * support at least the flags specified in `required_flags`. `caller`
+ * is used in any necessary error messages.
+ */
+static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
+                                               unsigned int required_flags,
+                                               const char *caller)
+{
+       struct packed_ref_store *refs;
+
+       if (ref_store->be != &refs_be_packed)
+               die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
+                   ref_store->be->name, caller);
+
+       refs = (struct packed_ref_store *)ref_store;
+
+       if ((refs->store_flags & required_flags) != required_flags)
+               die("BUG: unallowed operation (%s), requires %x, has %x\n",
+                   caller, required_flags, refs->store_flags);
+
+       return refs;
+}
+
+static void clear_snapshot(struct packed_ref_store *refs)
+{
+       if (refs->snapshot) {
+               struct snapshot *snapshot = refs->snapshot;
+
+               refs->snapshot = NULL;
+               release_snapshot(snapshot);
+       }
+}
+
+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++;
+       }
+}
+
+/*
+ * Compare a snapshot record at `rec` to the specified NUL-terminated
+ * refname.
+ */
+static int cmp_record_to_refname(const char *rec, const char *refname)
+{
+       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++;
+       }
+}
+
+/*
+ * `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;
+
+       pos = snapshot->buf + snapshot->header_len;
+       eof = snapshot->eof;
+       len = eof - pos;
+
+       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);
+}
+
+/*
+ * 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;
+}
+
+/*
+ * 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:
+ *
+ *   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`:
+ *
+ *      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`:
+ *
+ *      All references in the file that can be peeled are peeled.
+ *      Inversely (and this is more important), any references in the
+ *      file for which no peeled value is recorded is not peelable. This
+ *      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 snapshot *create_snapshot(struct packed_ref_store *refs)
+{
+       struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot));
+       int sorted = 0;
+
+       snapshot->refs = refs;
+       acquire_snapshot(snapshot);
+       snapshot->peeled = PEELED_NONE;
+
+       if (!load_contents(snapshot))
+               return snapshot;
+
+       /* 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;
+
+               eol = memchr(snapshot->buf, '\n',
+                            snapshot->eof - snapshot->buf);
+               if (!eol)
+                       die_unterminated_line(refs->path,
+                                             snapshot->buf,
+                                             snapshot->eof - snapshot->buf);
+
+               strbuf_add(&tmp, snapshot->buf, eol - snapshot->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);
+       }
+
+       verify_buffer_safe(snapshot);
+
+       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 `refs->snapshot` (if present) still reflects the
+ * contents of the `packed-refs` file. If not, clear the snapshot.
+ */
+static void validate_snapshot(struct packed_ref_store *refs)
+{
+       if (refs->snapshot &&
+           !stat_validity_check(&refs->snapshot->validity, refs->path))
+               clear_snapshot(refs);
+}
+
+/*
+ * 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 snapshot *get_snapshot(struct packed_ref_store *refs)
+{
+       if (!is_lock_file_locked(&refs->lock))
+               validate_snapshot(refs);
+
+       if (!refs->snapshot)
+               refs->snapshot = create_snapshot(refs);
+
+       return refs->snapshot;
+}
+
+static int packed_read_raw_ref(struct ref_store *ref_store,
+                              const char *refname, unsigned char *sha1,
+                              struct strbuf *referent, unsigned int *type)
+{
+       struct packed_ref_store *refs =
+               packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
+       struct snapshot *snapshot = get_snapshot(refs);
+       const char *rec;
+
+       *type = 0;
+
+       rec = find_reference_location(snapshot, refname, 1);
+
+       if (!rec) {
+               /* refname is not a packed reference. */
+               errno = ENOENT;
+               return -1;
+       }
+
+       if (get_sha1_hex(rec, sha1))
+               die_invalid_line(refs->path, rec, snapshot->eof - rec);
+
+       *type = REF_ISPACKED;
+       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 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 = next_record(iter)) == ITER_OK) {
+               if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
+                   ref_type(iter->base.refname) != REF_TYPE_PER_WORKTREE)
+                       continue;
+
+               if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
+                   !ref_resolves_to_object(iter->base.refname, &iter->oid,
+                                           iter->flags))
+                       continue;
+
+               return ITER_OK;
+       }
+
+       if (ref_iterator_abort(ref_iterator) != ITER_DONE)
+               ok = ITER_ERROR;
+
+       return ok;
+}
+
+static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
+                                  struct object_id *peeled)
+{
+       struct packed_ref_iterator *iter =
+               (struct packed_ref_iterator *)ref_iterator;
+
+       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)
+{
+       struct packed_ref_iterator *iter =
+               (struct packed_ref_iterator *)ref_iterator;
+       int ok = ITER_DONE;
+
+       strbuf_release(&iter->refname_buf);
+       release_snapshot(iter->snapshot);
+       base_ref_iterator_free(ref_iterator);
+       return ok;
+}
+
+static struct ref_iterator_vtable packed_ref_iterator_vtable = {
+       packed_ref_iterator_advance,
+       packed_ref_iterator_peel,
+       packed_ref_iterator_abort
+};
+
+static struct ref_iterator *packed_ref_iterator_begin(
+               struct ref_store *ref_store,
+               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;
+
+       if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
+               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, 1);
+
+       iter->snapshot = snapshot;
+       acquire_snapshot(snapshot);
+
+       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;
+}
+
+/*
+ * Write an entry to the packed-refs file for the specified refname.
+ * If peeled is non-NULL, write it as the entry's peeled value. On
+ * error, return a nonzero value and leave errno set at the value left
+ * by the failing call to `fprintf()`.
+ */
+static int write_packed_entry(FILE *fh, const char *refname,
+                             const unsigned char *sha1,
+                             const unsigned char *peeled)
+{
+       if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
+           (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
+               return -1;
+
+       return 0;
+}
+
+int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
+{
+       struct packed_ref_store *refs =
+               packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
+                               "packed_refs_lock");
+       static int timeout_configured = 0;
+       static int timeout_value = 1000;
+
+       if (!timeout_configured) {
+               git_config_get_int("core.packedrefstimeout", &timeout_value);
+               timeout_configured = 1;
+       }
+
+       /*
+        * Note that we close the lockfile immediately because we
+        * don't write new content to it, but rather to a separate
+        * tempfile.
+        */
+       if (hold_lock_file_for_update_timeout(
+                           &refs->lock,
+                           refs->path,
+                           flags, timeout_value) < 0) {
+               unable_to_lock_message(refs->path, errno, err);
+               return -1;
+       }
+
+       if (close_lock_file_gently(&refs->lock)) {
+               strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
+               rollback_lock_file(&refs->lock);
+               return -1;
+       }
+
+       /*
+        * Now that we hold the `packed-refs` lock, make sure that our
+        * 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_snapshot(refs);
+
+       /*
+        * Now make sure that the packed-refs file as it exists in the
+        * locked state is loaded into the snapshot:
+        */
+       get_snapshot(refs);
+       return 0;
+}
+
+void packed_refs_unlock(struct ref_store *ref_store)
+{
+       struct packed_ref_store *refs = packed_downcast(
+                       ref_store,
+                       REF_STORE_READ | REF_STORE_WRITE,
+                       "packed_refs_unlock");
+
+       if (!is_lock_file_locked(&refs->lock))
+               die("BUG: packed_refs_unlock() called when not locked");
+       rollback_lock_file(&refs->lock);
+}
+
+int packed_refs_is_locked(struct ref_store *ref_store)
+{
+       struct packed_ref_store *refs = packed_downcast(
+                       ref_store,
+                       REF_STORE_READ | REF_STORE_WRITE,
+                       "packed_refs_is_locked");
+
+       return is_lock_file_locked(&refs->lock);
+}
+
+/*
+ * 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 sorted \n";
+
+static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
+{
+       /* Nothing to do. */
+       return 0;
+}
+
+/*
+ * 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.
+ *
+ * The packfile must be locked before calling this function and will
+ * remain locked when it is done.
+ */
+static int write_with_updates(struct packed_ref_store *refs,
+                             struct string_list *updates,
+                             struct strbuf *err)
+{
+       struct ref_iterator *iter = NULL;
+       size_t i;
+       int ok;
+       FILE *out;
+       struct strbuf sb = STRBUF_INIT;
+       char *packed_refs_path;
+
+       if (!is_lock_file_locked(&refs->lock))
+               die("BUG: write_with_updates() called while unlocked");
+
+       /*
+        * If packed-refs is a symlink, we want to overwrite the
+        * symlinked-to file, not the symlink itself. Also, put the
+        * staging file next to it:
+        */
+       packed_refs_path = get_locked_file_path(&refs->lock);
+       strbuf_addf(&sb, "%s.new", packed_refs_path);
+       free(packed_refs_path);
+       refs->tempfile = create_tempfile(sb.buf);
+       if (!refs->tempfile) {
+               strbuf_addf(err, "unable to create file %s: %s",
+                           sb.buf, strerror(errno));
+               strbuf_release(&sb);
+               return -1;
+       }
+       strbuf_release(&sb);
+
+       out = fdopen_tempfile(refs->tempfile, "w");
+       if (!out) {
+               strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
+                           strerror(errno));
+               goto error;
+       }
+
+       if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
+               goto write_error;
+
+       /*
+        * We iterate in parallel through the current list of refs and
+        * the list of updates, processing an entry from at least one
+        * of the lists each time through the loop. When the current
+        * list of refs is exhausted, set iter to NULL. When the list
+        * of updates is exhausted, leave i set to updates->nr.
+        */
+       iter = packed_ref_iterator_begin(&refs->base, "",
+                                        DO_FOR_EACH_INCLUDE_BROKEN);
+       if ((ok = ref_iterator_advance(iter)) != ITER_OK)
+               iter = NULL;
+
+       i = 0;
+
+       while (iter || i < updates->nr) {
+               struct ref_update *update = NULL;
+               int cmp;
+
+               if (i >= updates->nr) {
+                       cmp = -1;
+               } else {
+                       update = updates->items[i].util;
+
+                       if (!iter)
+                               cmp = +1;
+                       else
+                               cmp = strcmp(iter->refname, update->refname);
+               }
+
+               if (!cmp) {
+                       /*
+                        * There is both an old value and an update
+                        * for this reference. Check the old value if
+                        * necessary:
+                        */
+                       if ((update->flags & REF_HAVE_OLD)) {
+                               if (is_null_oid(&update->old_oid)) {
+                                       strbuf_addf(err, "cannot update ref '%s': "
+                                                   "reference already exists",
+                                                   update->refname);
+                                       goto error;
+                               } else if (oidcmp(&update->old_oid, iter->oid)) {
+                                       strbuf_addf(err, "cannot update ref '%s': "
+                                                   "is at %s but expected %s",
+                                                   update->refname,
+                                                   oid_to_hex(iter->oid),
+                                                   oid_to_hex(&update->old_oid));
+                                       goto error;
+                               }
+                       }
+
+                       /* Now figure out what to use for the new value: */
+                       if ((update->flags & REF_HAVE_NEW)) {
+                               /*
+                                * The update takes precedence. Skip
+                                * the iterator over the unneeded
+                                * value.
+                                */
+                               if ((ok = ref_iterator_advance(iter)) != ITER_OK)
+                                       iter = NULL;
+                               cmp = +1;
+                       } else {
+                               /*
+                                * The update doesn't actually want to
+                                * change anything. We're done with it.
+                                */
+                               i++;
+                               cmp = -1;
+                       }
+               } else if (cmp > 0) {
+                       /*
+                        * There is no old value but there is an
+                        * update for this reference. Make sure that
+                        * the update didn't expect an existing value:
+                        */
+                       if ((update->flags & REF_HAVE_OLD) &&
+                           !is_null_oid(&update->old_oid)) {
+                               strbuf_addf(err, "cannot update ref '%s': "
+                                           "reference is missing but expected %s",
+                                           update->refname,
+                                           oid_to_hex(&update->old_oid));
+                               goto error;
+                       }
+               }
+
+               if (cmp < 0) {
+                       /* Pass the old reference through. */
+
+                       struct object_id peeled;
+                       int peel_error = ref_iterator_peel(iter, &peeled);
+
+                       if (write_packed_entry(out, iter->refname,
+                                              iter->oid->hash,
+                                              peel_error ? NULL : peeled.hash))
+                               goto write_error;
+
+                       if ((ok = ref_iterator_advance(iter)) != ITER_OK)
+                               iter = NULL;
+               } else if (is_null_oid(&update->new_oid)) {
+                       /*
+                        * The update wants to delete the reference,
+                        * and the reference either didn't exist or we
+                        * have already skipped it. So we're done with
+                        * the update (and don't have to write
+                        * anything).
+                        */
+                       i++;
+               } else {
+                       struct object_id peeled;
+                       int peel_error = peel_object(update->new_oid.hash,
+                                                    peeled.hash);
+
+                       if (write_packed_entry(out, update->refname,
+                                              update->new_oid.hash,
+                                              peel_error ? NULL : peeled.hash))
+                               goto write_error;
+
+                       i++;
+               }
+       }
+
+       if (ok != ITER_DONE) {
+               strbuf_addstr(err, "unable to write packed-refs file: "
+                             "error iterating over old contents");
+               goto error;
+       }
+
+       if (close_tempfile_gently(refs->tempfile)) {
+               strbuf_addf(err, "error closing file %s: %s",
+                           get_tempfile_path(refs->tempfile),
+                           strerror(errno));
+               strbuf_release(&sb);
+               delete_tempfile(&refs->tempfile);
+               return -1;
+       }
+
+       return 0;
+
+write_error:
+       strbuf_addf(err, "error writing to %s: %s",
+                   get_tempfile_path(refs->tempfile), strerror(errno));
+
+error:
+       if (iter)
+               ref_iterator_abort(iter);
+
+       delete_tempfile(&refs->tempfile);
+       return -1;
+}
+
+struct packed_transaction_backend_data {
+       /* True iff the transaction owns the packed-refs lock. */
+       int own_lock;
+
+       struct string_list updates;
+};
+
+static void packed_transaction_cleanup(struct packed_ref_store *refs,
+                                      struct ref_transaction *transaction)
+{
+       struct packed_transaction_backend_data *data = transaction->backend_data;
+
+       if (data) {
+               string_list_clear(&data->updates, 0);
+
+               if (is_tempfile_active(refs->tempfile))
+                       delete_tempfile(&refs->tempfile);
+
+               if (data->own_lock && is_lock_file_locked(&refs->lock)) {
+                       packed_refs_unlock(&refs->base);
+                       data->own_lock = 0;
+               }
+
+               free(data);
+               transaction->backend_data = NULL;
+       }
+
+       transaction->state = REF_TRANSACTION_CLOSED;
+}
+
+static int packed_transaction_prepare(struct ref_store *ref_store,
+                                     struct ref_transaction *transaction,
+                                     struct strbuf *err)
+{
+       struct packed_ref_store *refs = packed_downcast(
+                       ref_store,
+                       REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
+                       "ref_transaction_prepare");
+       struct packed_transaction_backend_data *data;
+       size_t i;
+       int ret = TRANSACTION_GENERIC_ERROR;
+
+       /*
+        * 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 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));
+       string_list_init(&data->updates, 0);
+
+       transaction->backend_data = data;
+
+       /*
+        * Stick the updates in a string list by refname so that we
+        * can sort them:
+        */
+       for (i = 0; i < transaction->nr; i++) {
+               struct ref_update *update = transaction->updates[i];
+               struct string_list_item *item =
+                       string_list_append(&data->updates, update->refname);
+
+               /* Store a pointer to update in item->util: */
+               item->util = update;
+       }
+       string_list_sort(&data->updates);
+
+       if (ref_update_reject_duplicates(&data->updates, err))
+               goto failure;
+
+       if (!is_lock_file_locked(&refs->lock)) {
+               if (packed_refs_lock(ref_store, 0, err))
+                       goto failure;
+               data->own_lock = 1;
+       }
+
+       if (write_with_updates(refs, &data->updates, err))
+               goto failure;
+
+       transaction->state = REF_TRANSACTION_PREPARED;
+       return 0;
+
+failure:
+       packed_transaction_cleanup(refs, transaction);
+       return ret;
+}
+
+static int packed_transaction_abort(struct ref_store *ref_store,
+                                   struct ref_transaction *transaction,
+                                   struct strbuf *err)
+{
+       struct packed_ref_store *refs = packed_downcast(
+                       ref_store,
+                       REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
+                       "ref_transaction_abort");
+
+       packed_transaction_cleanup(refs, transaction);
+       return 0;
+}
+
+static int packed_transaction_finish(struct ref_store *ref_store,
+                                    struct ref_transaction *transaction,
+                                    struct strbuf *err)
+{
+       struct packed_ref_store *refs = packed_downcast(
+                       ref_store,
+                       REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
+                       "ref_transaction_finish");
+       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",
+                           refs->path, strerror(errno));
+               goto cleanup;
+       }
+
+       ret = 0;
+
+cleanup:
+       free(packed_refs_path);
+       packed_transaction_cleanup(refs, transaction);
+       return ret;
+}
+
+static int packed_initial_transaction_commit(struct ref_store *ref_store,
+                                           struct ref_transaction *transaction,
+                                           struct strbuf *err)
+{
+       return ref_transaction_commit(transaction, err);
+}
+
+static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
+                            struct string_list *refnames, unsigned int flags)
+{
+       struct packed_ref_store *refs =
+               packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
+       struct strbuf err = STRBUF_INIT;
+       struct ref_transaction *transaction;
+       struct string_list_item *item;
+       int ret;
+
+       (void)refs; /* We need the check above, but don't use the variable */
+
+       if (!refnames->nr)
+               return 0;
+
+       /*
+        * Since we don't check the references' old_oids, the
+        * individual updates can't fail, so we can pack all of the
+        * updates into a single transaction.
+        */
+
+       transaction = ref_store_transaction_begin(ref_store, &err);
+       if (!transaction)
+               return -1;
+
+       for_each_string_list_item(item, refnames) {
+               if (ref_transaction_delete(transaction, item->string, NULL,
+                                          flags, msg, &err)) {
+                       warning(_("could not delete reference %s: %s"),
+                               item->string, err.buf);
+                       strbuf_reset(&err);
+               }
+       }
+
+       ret = ref_transaction_commit(transaction, &err);
+
+       if (ret) {
+               if (refnames->nr == 1)
+                       error(_("could not delete reference %s: %s"),
+                             refnames->items[0].string, err.buf);
+               else
+                       error(_("could not delete references: %s"), err.buf);
+       }
+
+       ref_transaction_free(transaction);
+       strbuf_release(&err);
+       return ret;
+}
+
+static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
+{
+       /*
+        * Packed refs are already packed. It might be that loose refs
+        * are packed *into* a packed refs store, but that is done by
+        * updating the packed references via a transaction.
+        */
+       return 0;
+}
+
+static int packed_create_symref(struct ref_store *ref_store,
+                              const char *refname, const char *target,
+                              const char *logmsg)
+{
+       die("BUG: packed reference store does not support symrefs");
+}
+
+static int packed_rename_ref(struct ref_store *ref_store,
+                           const char *oldrefname, const char *newrefname,
+                           const char *logmsg)
+{
+       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();
+}
+
+static int packed_for_each_reflog_ent(struct ref_store *ref_store,
+                                     const char *refname,
+                                     each_reflog_ent_fn fn, void *cb_data)
+{
+       return 0;
+}
+
+static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
+                                             const char *refname,
+                                             each_reflog_ent_fn fn,
+                                             void *cb_data)
+{
+       return 0;
+}
+
+static int packed_reflog_exists(struct ref_store *ref_store,
+                              const char *refname)
+{
+       return 0;
+}
+
+static int packed_create_reflog(struct ref_store *ref_store,
+                              const char *refname, int force_create,
+                              struct strbuf *err)
+{
+       die("BUG: packed reference store does not support reflogs");
+}
+
+static int packed_delete_reflog(struct ref_store *ref_store,
+                              const char *refname)
+{
+       return 0;
+}
+
+static int packed_reflog_expire(struct ref_store *ref_store,
+                               const char *refname, const unsigned char *sha1,
+                               unsigned int flags,
+                               reflog_expiry_prepare_fn prepare_fn,
+                               reflog_expiry_should_prune_fn should_prune_fn,
+                               reflog_expiry_cleanup_fn cleanup_fn,
+                               void *policy_cb_data)
+{
+       return 0;
+}
+
+struct ref_storage_be refs_be_packed = {
+       NULL,
+       "packed",
+       packed_ref_store_create,
+       packed_init_db,
+       packed_transaction_prepare,
+       packed_transaction_finish,
+       packed_transaction_abort,
+       packed_initial_transaction_commit,
+
+       packed_pack_refs,
+       packed_create_symref,
+       packed_delete_refs,
+       packed_rename_ref,
+       packed_copy_ref,
+
+       packed_ref_iterator_begin,
+       packed_read_raw_ref,
+
+       packed_reflog_iterator_begin,
+       packed_for_each_reflog_ent,
+       packed_for_each_reflog_ent_reverse,
+       packed_reflog_exists,
+       packed_create_reflog,
+       packed_delete_reflog,
+       packed_reflog_expire
+};
diff --git a/refs/packed-backend.h b/refs/packed-backend.h
new file mode 100644 (file)
index 0000000..61687e4
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef REFS_PACKED_BACKEND_H
+#define REFS_PACKED_BACKEND_H
+
+/*
+ * Support for storing references in a `packed-refs` file.
+ *
+ * Note that this backend doesn't check for D/F conflicts, because it
+ * doesn't care about them. But usually it should be wrapped in a
+ * `files_ref_store` that prevents D/F conflicts from being created,
+ * even among packed refs.
+ */
+
+struct ref_store *packed_ref_store_create(const char *path,
+                                         unsigned int store_flags);
+
+/*
+ * Lock the packed-refs file for writing. Flags is passed to
+ * hold_lock_file_for_update(). Return 0 on success. On errors, write
+ * an error message to `err` and return a nonzero value.
+ */
+int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err);
+
+void packed_refs_unlock(struct ref_store *ref_store);
+int packed_refs_is_locked(struct ref_store *ref_store);
+
+#endif /* REFS_PACKED_BACKEND_H */
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 192f9f85c97c0d7ca4e9c933e27ba0686da31dbe..448de4bccb8f3d9531b50d5489af001ec7ce4732 100644 (file)
  */
 #define REF_DELETED_LOOSE 0x200
 
+/*
+ * Return the length of time to retry acquiring a loose reference lock
+ * before giving up, in milliseconds:
+ */
+long get_files_ref_lock_timeout_ms(void);
+
 /*
  * Return true iff refname is minimally safe. "Safe" here means that
  * deleting a loose reference by this name will not do any damage, for
  */
 int refname_is_safe(const char *refname);
 
+/*
+ * Helper function: return true if refname, which has the specified
+ * oid and flags, can be resolved to an object in the database. If the
+ * referred-to object does not exist, emit a warning and return false.
+ */
+int ref_resolves_to_object(const char *refname,
+                          const struct object_id *oid,
+                          unsigned int flags);
+
 enum peel_status {
        /* object was peeled successfully: */
        PEEL_PEELED = 0,
@@ -227,6 +242,7 @@ struct ref_transaction {
        size_t alloc;
        size_t nr;
        enum ref_transaction_state state;
+       void *backend_data;
 };
 
 /*
@@ -313,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;
@@ -358,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,
@@ -384,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);
 
@@ -415,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,
@@ -425,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
@@ -532,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,
@@ -543,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,
@@ -637,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;
@@ -655,6 +687,7 @@ struct ref_storage_be {
 };
 
 extern struct ref_storage_be refs_be_files;
+extern struct ref_storage_be refs_be_packed;
 
 /*
  * A representation of the reference store for the main repository or
index 60d004392109f1e39f0bf34578e195cdf3af1639..b220f0dfc619a6d3150cb2bcee00114f76bd0223 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -134,10 +134,14 @@ struct remotes_hash_key {
 };
 
 static int remotes_hash_cmp(const void *unused_cmp_data,
-                           const struct remote *a,
-                           const struct remote *b,
-                           const struct remotes_hash_key *key)
+                           const void *entry,
+                           const void *entry_or_key,
+                           const void *keydata)
 {
+       const struct remote *a = entry;
+       const struct remote *b = entry_or_key;
+       const struct remotes_hash_key *key = keydata;
+
        if (key)
                return strncmp(a->name, key->str, key->len) || a->name[key->len];
        else
@@ -147,7 +151,7 @@ static int remotes_hash_cmp(const void *unused_cmp_data,
 static inline void init_remotes_hash(void)
 {
        if (!remotes_hash.cmpfn)
-               hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, NULL, 0);
+               hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
 }
 
 static struct remote *make_remote(const char *name, int len)
@@ -462,7 +466,6 @@ static void alias_all_urls(void)
 static void read_config(void)
 {
        static int loaded;
-       struct object_id oid;
        int flag;
 
        if (loaded)
@@ -471,7 +474,7 @@ static void read_config(void)
 
        current_branch = NULL;
        if (startup_info->have_repository) {
-               const char *head_ref = resolve_ref_unsafe("HEAD", 0, oid.hash, &flag);
+               const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
                if (head_ref && (flag & REF_ISSYMREF) &&
                    skip_prefix(head_ref, "refs/heads/", &head_ref)) {
                        current_branch = make_branch(head_ref, 0);
@@ -1081,7 +1084,7 @@ static int try_explicit_object_name(const char *name,
                return 0;
        }
 
-       if (get_sha1(name, oid.hash))
+       if (get_oid(name, &oid))
                return -1;
 
        if (match) {
@@ -1101,10 +1104,9 @@ static struct ref *make_linked_ref(const char *name, struct ref ***tail)
 static char *guess_ref(const char *name, struct ref *peer)
 {
        struct strbuf buf = STRBUF_INIT;
-       struct object_id oid;
 
        const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING,
-                                          oid.hash, NULL);
+                                          NULL, NULL);
        if (!r)
                return NULL;
 
@@ -1162,12 +1164,11 @@ static int match_explicit(struct ref *src, struct ref *dst,
                return -1;
 
        if (!dst_value) {
-               struct object_id oid;
                int flag;
 
                dst_value = resolve_ref_unsafe(matched_src->name,
                                               RESOLVE_REF_READING,
-                                              oid.hash, &flag);
+                                              NULL, &flag);
                if (!dst_value ||
                    ((flag & REF_ISSYMREF) &&
                     !starts_with(dst_value, "refs/heads/")))
@@ -1788,10 +1789,9 @@ const char *branch_get_push(struct branch *branch, struct strbuf *err)
 
 static int ignore_symref_update(const char *refname)
 {
-       struct object_id oid;
        int flag;
 
-       if (!resolve_ref_unsafe(refname, 0, oid.hash, &flag))
+       if (!resolve_ref_unsafe(refname, 0, NULL, &flag))
                return 0; /* non-existing refs are OK */
        return (flag & REF_ISSYMREF);
 }
@@ -2080,7 +2080,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
                                _("  (use \"git branch --unset-upstream\" to fixup)\n"));
        } else if (!ours && !theirs) {
                strbuf_addf(sb,
-                       _("Your branch is up-to-date with '%s'.\n"),
+                       _("Your branch is up to date with '%s'.\n"),
                        base);
        } else if (!theirs) {
                strbuf_addf(sb,
@@ -2297,8 +2297,8 @@ static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, i
        if (!*colon)
                entry->use_tracking = 1;
        else if (!colon[1])
-               hashclr(entry->expect);
-       else if (get_sha1(colon + 1, entry->expect))
+               oidclr(&entry->expect);
+       else if (get_oid(colon + 1, &entry->expect))
                return error("cannot parse expected object name '%s'", colon + 1);
        return 0;
 }
@@ -2345,7 +2345,7 @@ static void apply_cas(struct push_cas_option *cas,
                        continue;
                ref->expect_old_sha1 = 1;
                if (!entry->use_tracking)
-                       hashcpy(ref->old_oid_expect.hash, cas->entry[i].expect);
+                       oidcpy(&ref->old_oid_expect, &entry->expect);
                else if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
                        oidclr(&ref->old_oid_expect);
                return;
index 6c28cd3e4bfe2e8be058485d0b963b23622b999f..2ecf4c8c74ce590c9bcc917d997b3c7acf9c07c1 100644 (file)
--- a/remote.h
+++ b/remote.h
@@ -282,7 +282,7 @@ struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fet
 struct push_cas_option {
        unsigned use_tracking_for_rest:1;
        struct push_cas {
-               unsigned char expect[20];
+               struct object_id expect;
                unsigned use_tracking:1;
                char *refname;
        } *entry;
index edca9074046ab1ca9826d2479bcca11018ba1873..bb2fae5446b7ea8d2bfac87faa30c165f714d121 100644 (file)
@@ -4,7 +4,9 @@
 #include "submodule-config.h"
 
 /* The main repository */
-static struct repository the_repo;
+static struct repository the_repo = {
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &the_index, 0, 0
+};
 struct repository *the_repository = &the_repo;
 
 static char *git_path_from_env(const char *envvar, const char *git_dir,
@@ -38,11 +40,15 @@ static void repo_setup_env(struct repository *repo)
 
        repo->different_commondir = find_common_dir(&sb, repo->gitdir,
                                                    !repo->ignore_env);
+       free(repo->commondir);
        repo->commondir = strbuf_detach(&sb, NULL);
+       free(repo->objectdir);
        repo->objectdir = git_path_from_env(DB_ENVIRONMENT, repo->commondir,
                                            "objects", !repo->ignore_env);
+       free(repo->graft_file);
        repo->graft_file = git_path_from_env(GRAFT_ENVIRONMENT, repo->commondir,
                                             "info/grafts", !repo->ignore_env);
+       free(repo->index_file);
        repo->index_file = git_path_from_env(INDEX_ENVIRONMENT, repo->gitdir,
                                             "index", !repo->ignore_env);
 }
@@ -50,16 +56,12 @@ static void repo_setup_env(struct repository *repo)
 void repo_set_gitdir(struct repository *repo, const char *path)
 {
        const char *gitfile = read_gitfile(path);
+       char *old_gitdir = repo->gitdir;
 
-       /*
-        * NEEDSWORK: Eventually we want to be able to free gitdir and the rest
-        * of the environment before reinitializing it again, but we have some
-        * crazy code paths where we try to set gitdir with the current gitdir
-        * and we don't want to free gitdir before copying the passed in value.
-        */
        repo->gitdir = xstrdup(gitfile ? gitfile : path);
-
        repo_setup_env(repo);
+
+       free(old_gitdir);
 }
 
 /*
@@ -158,7 +160,7 @@ int repo_submodule_init(struct repository *submodule,
        struct strbuf worktree = STRBUF_INIT;
        int ret = 0;
 
-       sub = submodule_from_cache(superproject, null_sha1, path);
+       sub = submodule_from_cache(superproject, &null_oid, path);
        if (!sub) {
                ret = -1;
                goto out;
@@ -198,25 +200,17 @@ int repo_submodule_init(struct repository *submodule,
 
 void repo_clear(struct repository *repo)
 {
-       free(repo->gitdir);
-       repo->gitdir = NULL;
-       free(repo->commondir);
-       repo->commondir = NULL;
-       free(repo->objectdir);
-       repo->objectdir = NULL;
-       free(repo->graft_file);
-       repo->graft_file = NULL;
-       free(repo->index_file);
-       repo->index_file = NULL;
-       free(repo->worktree);
-       repo->worktree = NULL;
-       free(repo->submodule_prefix);
-       repo->submodule_prefix = NULL;
+       FREE_AND_NULL(repo->gitdir);
+       FREE_AND_NULL(repo->commondir);
+       FREE_AND_NULL(repo->objectdir);
+       FREE_AND_NULL(repo->graft_file);
+       FREE_AND_NULL(repo->index_file);
+       FREE_AND_NULL(repo->worktree);
+       FREE_AND_NULL(repo->submodule_prefix);
 
        if (repo->config) {
                git_configset_clear(repo->config);
-               free(repo->config);
-               repo->config = NULL;
+               FREE_AND_NULL(repo->config);
        }
 
        if (repo->submodule_cache) {
@@ -226,8 +220,7 @@ void repo_clear(struct repository *repo)
 
        if (repo->index) {
                discard_index(repo->index);
-               free(repo->index);
-               repo->index = NULL;
+               FREE_AND_NULL(repo->index);
        }
 }
 
@@ -235,8 +228,6 @@ int repo_read_index(struct repository *repo)
 {
        if (!repo->index)
                repo->index = xcalloc(1, sizeof(*repo->index));
-       else
-               discard_index(repo->index);
 
        return read_index_from(repo->index, repo->index_file);
 }
index 417787f3ef02e871406fa81d1a1bebcdac6e5f13..7f5e24a0a24011393e9b1b9986895ebe8b3f10ab 100644 (file)
@@ -92,6 +92,14 @@ extern int repo_submodule_init(struct repository *submodule,
                               const char *path);
 extern void repo_clear(struct repository *repo);
 
+/*
+ * Populates the repository's index from its index_file, an index struct will
+ * be allocated if needed.
+ *
+ * Return the number of index entries in the populated index or a value less
+ * than zero if an error occured.  If the repository's index has already been
+ * populated then the number of entries will simply be returned.
+ */
 extern int repo_read_index(struct repository *repo);
 
 #endif /* REPOSITORY_H */
index 70634d456cc0b70cdbac1579b82016761252c7d9..1ce440f4bb84d001ff2b0ac1a67772f4bf5926c0 100644 (file)
--- a/rerere.c
+++ b/rerere.c
@@ -258,7 +258,7 @@ static int write_rr(struct string_list *rr, int out_fd)
                                    rerere_id_hex(id),
                                    rr->items[i].string, 0);
 
-               if (write_in_full(out_fd, buf.buf, buf.len) != buf.len)
+               if (write_in_full(out_fd, buf.buf, buf.len) < 0)
                        die("unable to write rerere record");
 
                strbuf_release(&buf);
@@ -1133,14 +1133,14 @@ int rerere_forget(struct pathspec *pathspec)
  * Garbage collection support
  */
 
-static time_t rerere_created_at(struct rerere_id *id)
+static timestamp_t rerere_created_at(struct rerere_id *id)
 {
        struct stat st;
 
        return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
 }
 
-static time_t rerere_last_used_at(struct rerere_id *id)
+static timestamp_t rerere_last_used_at(struct rerere_id *id)
 {
        struct stat st;
 
@@ -1157,11 +1157,11 @@ static void unlink_rr_item(struct rerere_id *id)
        id->collection->status[id->variant] = 0;
 }
 
-static void prune_one(struct rerere_id *id, time_t now,
-                     int cutoff_resolve, int cutoff_noresolve)
+static void prune_one(struct rerere_id *id,
+                     timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
 {
-       time_t then;
-       int cutoff;
+       timestamp_t then;
+       timestamp_t cutoff;
 
        then = rerere_last_used_at(id);
        if (then)
@@ -1172,7 +1172,7 @@ static void prune_one(struct rerere_id *id, time_t now,
                        return;
                cutoff = cutoff_noresolve;
        }
-       if (then < now - cutoff * 86400)
+       if (then < cutoff)
                unlink_rr_item(id);
 }
 
@@ -1182,15 +1182,15 @@ void rerere_gc(struct string_list *rr)
        DIR *dir;
        struct dirent *e;
        int i;
-       time_t now = time(NULL);
-       int cutoff_noresolve = 15;
-       int cutoff_resolve = 60;
+       timestamp_t now = time(NULL);
+       timestamp_t cutoff_noresolve = now - 15 * 86400;
+       timestamp_t cutoff_resolve = now - 60 * 86400;
 
        if (setup_rerere(rr, 0) < 0)
                return;
 
-       git_config_get_int("gc.rerereresolved", &cutoff_resolve);
-       git_config_get_int("gc.rerereunresolved", &cutoff_noresolve);
+       git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve, now);
+       git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve, now);
        git_config(git_default_config, NULL);
        dir = opendir(git_path("rr-cache"));
        if (!dir)
@@ -1211,7 +1211,7 @@ void rerere_gc(struct string_list *rr)
                for (id.variant = 0, id.collection = rr_dir;
                     id.variant < id.collection->status_nr;
                     id.variant++) {
-                       prune_one(&id, now, cutoff_resolve, cutoff_noresolve);
+                       prune_one(&id, cutoff_resolve, cutoff_noresolve);
                        if (id.collection->status[id.variant])
                                now_empty = 0;
                }
index f032ab2e5c5c73c691338cdaf99d5b3b3d16b674..d167223e694e54626786740954454d4973e28752 100644 (file)
@@ -19,6 +19,9 @@
 #include "dir.h"
 #include "cache-tree.h"
 #include "bisect.h"
+#include "packfile.h"
+#include "worktree.h"
+#include "argv-array.h"
 
 volatile show_early_output_fn_t show_early_output;
 
@@ -1103,7 +1106,7 @@ static void add_rev_cmdline(struct rev_info *revs,
                            unsigned flags)
 {
        struct rev_cmdline_info *info = &revs->cmdline;
-       int nr = info->nr;
+       unsigned int nr = info->nr;
 
        ALLOC_GROW(info->rev, nr + 1, info->alloc);
        info->rev[nr].item = item;
@@ -1131,6 +1134,7 @@ struct all_refs_cb {
        int warned_bad_reflog;
        struct rev_info *all_revs;
        const char *name_for_errormsg;
+       struct ref_store *refs;
 };
 
 int ref_excluded(struct string_list *ref_excludes, const char *path)
@@ -1167,6 +1171,7 @@ static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
        cb->all_revs = revs;
        cb->all_flags = flags;
        revs->rev_input_given = 1;
+       cb->refs = NULL;
 }
 
 void clear_ref_exclusion(struct string_list **ref_excludes_p)
@@ -1187,12 +1192,19 @@ void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
        string_list_append(*ref_excludes_p, exclude);
 }
 
-static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
-               int (*for_each)(const char *, each_ref_fn, void *))
+static void handle_refs(struct ref_store *refs,
+                       struct rev_info *revs, unsigned flags,
+                       int (*for_each)(struct ref_store *, each_ref_fn, void *))
 {
        struct all_refs_cb cb;
+
+       if (!refs) {
+               /* this could happen with uninitialized submodules */
+               return;
+       }
+
        init_all_refs_cb(&cb, revs, flags);
-       for_each(submodule, handle_one_ref, &cb);
+       for_each(refs, handle_one_ref, &cb);
 }
 
 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
@@ -1228,17 +1240,41 @@ static int handle_one_reflog(const char *path, const struct object_id *oid,
        struct all_refs_cb *cb = cb_data;
        cb->warned_bad_reflog = 0;
        cb->name_for_errormsg = path;
-       for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
+       refs_for_each_reflog_ent(cb->refs, path,
+                                handle_one_reflog_ent, cb_data);
        return 0;
 }
 
+static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
+{
+       struct worktree **worktrees, **p;
+
+       worktrees = get_worktrees(0);
+       for (p = worktrees; *p; p++) {
+               struct worktree *wt = *p;
+
+               if (wt->is_current)
+                       continue;
+
+               cb->refs = get_worktree_ref_store(wt);
+               refs_for_each_reflog(cb->refs,
+                                    handle_one_reflog,
+                                    cb);
+       }
+       free_worktrees(worktrees);
+}
+
 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
 {
        struct all_refs_cb cb;
 
        cb.all_revs = revs;
        cb.all_flags = flags;
+       cb.refs = get_main_ref_store();
        for_each_reflog(handle_one_reflog, &cb);
+
+       if (!revs->single_worktree)
+               add_other_reflogs_to_pending(&cb);
 }
 
 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
@@ -1262,13 +1298,13 @@ static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
 
 }
 
-void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
+static void do_add_index_objects_to_pending(struct rev_info *revs,
+                                           struct index_state *istate)
 {
        int i;
 
-       read_cache();
-       for (i = 0; i < active_nr; i++) {
-               struct cache_entry *ce = active_cache[i];
+       for (i = 0; i < istate->cache_nr; i++) {
+               struct cache_entry *ce = istate->cache[i];
                struct blob *blob;
 
                if (S_ISGITLINK(ce->ce_mode))
@@ -1281,13 +1317,39 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
                                             ce->ce_mode, ce->name);
        }
 
-       if (active_cache_tree) {
+       if (istate->cache_tree) {
                struct strbuf path = STRBUF_INIT;
-               add_cache_tree(active_cache_tree, revs, &path);
+               add_cache_tree(istate->cache_tree, revs, &path);
                strbuf_release(&path);
        }
 }
 
+void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
+{
+       struct worktree **worktrees, **p;
+
+       read_cache();
+       do_add_index_objects_to_pending(revs, &the_index);
+
+       if (revs->single_worktree)
+               return;
+
+       worktrees = get_worktrees(0);
+       for (p = worktrees; *p; p++) {
+               struct worktree *wt = *p;
+               struct index_state istate = { NULL };
+
+               if (wt->is_current)
+                       continue; /* current index already taken care of */
+
+               if (read_index_from(&istate,
+                                   worktree_git_path(wt, "index")) > 0)
+                       do_add_index_objects_to_pending(revs, &istate);
+               discard_index(&istate);
+       }
+       free_worktrees(worktrees);
+}
+
 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
                            int exclude_parent)
 {
@@ -1302,7 +1364,7 @@ static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
                flags ^= UNINTERESTING | BOTTOM;
                arg++;
        }
-       if (get_sha1_committish(arg, oid.hash))
+       if (get_oid_committish(arg, &oid))
                return 0;
        while (1) {
                it = get_reference(revs, arg, &oid, 0);
@@ -1451,7 +1513,7 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
        unsigned int a_flags, b_flags;
        int symmetric = 0;
        unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
-       unsigned int oc_flags = GET_SHA1_COMMITTISH | GET_SHA1_RECORD_PATH;
+       unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
 
        a_name = arg;
        if (!*a_name)
@@ -1465,8 +1527,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
        if (!*b_name)
                b_name = "HEAD";
 
-       if (get_sha1_with_context(a_name, oc_flags, a_oid.hash, a_oc) ||
-           get_sha1_with_context(b_name, oc_flags, b_oid.hash, b_oc))
+       if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
+           get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
                return -1;
 
        if (!cant_be_filename) {
@@ -1547,7 +1609,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
        int local_flags;
        const char *arg = arg_;
        int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
-       unsigned get_sha1_flags = GET_SHA1_RECORD_PATH;
+       unsigned get_sha1_flags = GET_OID_RECORD_PATH;
 
        flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
 
@@ -1598,9 +1660,9 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
        }
 
        if (revarg_opt & REVARG_COMMITTISH)
-               get_sha1_flags |= GET_SHA1_COMMITTISH;
+               get_sha1_flags |= GET_OID_COMMITTISH;
 
-       if (get_sha1_with_context(arg, get_sha1_flags, oid.hash, &oc))
+       if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
                return revs->ignore_missing ? 0 : -1;
        if (!cant_be_filename)
                verify_non_filename(revs->prefix, arg);
@@ -1611,31 +1673,15 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
        return 0;
 }
 
-struct cmdline_pathspec {
-       int alloc;
-       int nr;
-       const char **path;
-};
-
-static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
-{
-       while (*av) {
-               ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
-               prune->path[prune->nr++] = *(av++);
-       }
-}
-
 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
-                                    struct cmdline_pathspec *prune)
+                                    struct argv_array *prune)
 {
-       while (strbuf_getline(sb, stdin) != EOF) {
-               ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
-               prune->path[prune->nr++] = xstrdup(sb->buf);
-       }
+       while (strbuf_getline(sb, stdin) != EOF)
+               argv_array_push(prune, sb->buf);
 }
 
 static void read_revisions_from_stdin(struct rev_info *revs,
-                                     struct cmdline_pathspec *prune)
+                                     struct argv_array *prune)
 {
        struct strbuf sb;
        int seen_dashdash = 0;
@@ -2068,23 +2114,25 @@ void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
        ctx->argc -= n;
 }
 
-static int for_each_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data, const char *term) {
+static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
+                              void *cb_data, const char *term)
+{
        struct strbuf bisect_refs = STRBUF_INIT;
        int status;
        strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
-       status = for_each_fullref_in_submodule(submodule, bisect_refs.buf, fn, cb_data, 0);
+       status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
        strbuf_release(&bisect_refs);
        return status;
 }
 
-static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
+static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
-       return for_each_bisect_ref(submodule, fn, cb_data, term_bad);
+       return for_each_bisect_ref(refs, fn, cb_data, term_bad);
 }
 
-static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
+static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
-       return for_each_bisect_ref(submodule, fn, cb_data, term_good);
+       return for_each_bisect_ref(refs, fn, cb_data, term_good);
 }
 
 static int handle_revision_pseudo_opt(const char *submodule,
@@ -2093,8 +2141,22 @@ static int handle_revision_pseudo_opt(const char *submodule,
 {
        const char *arg = argv[0];
        const char *optarg;
+       struct ref_store *refs;
        int argcount;
 
+       if (submodule) {
+               /*
+                * We need some something like get_submodule_worktrees()
+                * before we can go through all worktrees of a submodule,
+                * .e.g with adding all HEADs from --all, which is not
+                * supported right now, so stick to single worktree.
+                */
+               if (!revs->single_worktree)
+                       die("BUG: --single-worktree cannot be used together with submodule");
+               refs = get_submodule_ref_store(submodule);
+       } else
+               refs = get_main_ref_store();
+
        /*
         * NOTE!
         *
@@ -2106,22 +2168,29 @@ static int handle_revision_pseudo_opt(const char *submodule,
         * register it in the list at the top of handle_revision_opt.
         */
        if (!strcmp(arg, "--all")) {
-               handle_refs(submodule, revs, *flags, for_each_ref_submodule);
-               handle_refs(submodule, revs, *flags, head_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_ref);
+               handle_refs(refs, revs, *flags, refs_head_ref);
+               if (!revs->single_worktree) {
+                       struct all_refs_cb cb;
+
+                       init_all_refs_cb(&cb, revs, *flags);
+                       other_head_refs(handle_one_ref, &cb);
+               }
                clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--branches")) {
-               handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
                clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--bisect")) {
                read_bisect_terms(&term_bad, &term_good);
-               handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
-               handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
+               handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
+               handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
+                           for_each_good_bisect_ref);
                revs->bisect = 1;
        } else if (!strcmp(arg, "--tags")) {
-               handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
                clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--remotes")) {
-               handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
                clear_ref_exclusion(&revs->ref_excludes);
        } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
                struct all_refs_cb cb;
@@ -2168,6 +2237,8 @@ static int handle_revision_pseudo_opt(const char *submodule,
                        return error("invalid argument to --no-walk");
        } else if (!strcmp(arg, "--do-walk")) {
                revs->no_walk = 0;
+       } else if (!strcmp(arg, "--single-worktree")) {
+               revs->single_worktree = 1;
        } else {
                return 0;
        }
@@ -2177,11 +2248,10 @@ static int handle_revision_pseudo_opt(const char *submodule,
 
 static void NORETURN diagnose_missing_default(const char *def)
 {
-       unsigned char sha1[20];
        int flags;
        const char *refname;
 
-       refname = resolve_ref_unsafe(def, 0, sha1, &flags);
+       refname = resolve_ref_unsafe(def, 0, NULL, &flags);
        if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
                die(_("your current branch appears to be broken"));
 
@@ -2200,10 +2270,9 @@ static void NORETURN diagnose_missing_default(const char *def)
 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
 {
        int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
-       struct cmdline_pathspec prune_data;
+       struct argv_array prune_data = ARGV_ARRAY_INIT;
        const char *submodule = NULL;
 
-       memset(&prune_data, 0, sizeof(prune_data));
        if (opt)
                submodule = opt->submodule;
 
@@ -2219,7 +2288,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                        argv[i] = NULL;
                        argc = i;
                        if (argv[i + 1])
-                               append_prune_data(&prune_data, argv + i + 1);
+                               argv_array_pushv(&prune_data, argv + i + 1);
                        seen_dashdash = 1;
                        break;
                }
@@ -2280,14 +2349,14 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                        for (j = i; j < argc; j++)
                                verify_filename(revs->prefix, argv[j], j == i);
 
-                       append_prune_data(&prune_data, argv + i);
+                       argv_array_pushv(&prune_data, argv + i);
                        break;
                }
                else
                        got_rev_arg = 1;
        }
 
-       if (prune_data.nr) {
+       if (prune_data.argc) {
                /*
                 * If we need to introduce the magic "a lone ':' means no
                 * pathspec whatsoever", here is the place to do so.
@@ -2302,11 +2371,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                 *      call init_pathspec() to set revs->prune_data here.
                 * }
                 */
-               ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
-               prune_data.path[prune_data.nr++] = NULL;
                parse_pathspec(&revs->prune_data, 0, 0,
-                              revs->prefix, prune_data.path);
+                              revs->prefix, prune_data.argv);
        }
+       argv_array_clear(&prune_data);
 
        if (revs->def == NULL)
                revs->def = opt ? opt->def : NULL;
@@ -2318,7 +2386,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                struct object_id oid;
                struct object *object;
                struct object_context oc;
-               if (get_sha1_with_context(revs->def, 0, oid.hash, &oc))
+               if (get_oid_with_context(revs->def, 0, &oid, &oc))
                        diagnose_missing_default(revs->def);
                object = get_reference(revs, revs->def, &oid, 0);
                add_pending_object_with_mode(revs, object, revs->def, oc.mode);
index bc18487d6fff5e86dff60748c084995a77d2b588..54761200adf2d5111b8aba097299bd2fd080a949 100644 (file)
@@ -96,6 +96,7 @@ struct rev_info {
                        topo_order:1,
                        simplify_merges:1,
                        simplify_by_decoration:1,
+                       single_worktree:1,
                        tag_objects:1,
                        tree_objects:1,
                        blob_objects:1,
@@ -149,6 +150,17 @@ struct rev_info {
                        date_mode_explicit:1,
                        preserve_subject:1;
        unsigned int    disable_stdin:1;
+       /*
+        * Set `leak_pending` to prevent `prepare_revision_walk()` from clearing
+        * the array of pending objects (`pending`). It will still forget about
+        * the array and its entries, so they really are leaked. This can be
+        * useful if the `struct object_array` `pending` is copied before
+        * calling `prepare_revision_walk()`. By setting `leak_pending`, you
+        * effectively claim ownership of the old array, so you should most
+        * likely call `object_array_clear(&pending_copy)` once you are done.
+        * Observe that this is about ownership of the array and its entries,
+        * not the commits referenced by those entries.
+        */
        unsigned int    leak_pending:1;
        /* --show-linear-break */
        unsigned int    track_linear:1,
index b5e6eb37c0eb3d6ed0cbd4fc35e879bf9fbd3db6..014b2165b5a2f92ff18869effafd4c6df01b33eb 100644 (file)
@@ -452,7 +452,7 @@ static char **prep_childenv(const char *const *deltaenv)
        }
 
        /* Create an array of 'char *' to be used as the childenv */
-       childenv = xmalloc((env.nr + 1) * sizeof(char *));
+       ALLOC_ARRAY(childenv, env.nr + 1);
        for (i = 0; i < env.nr; i++)
                childenv[i] = env.items[i].util;
        childenv[env.nr] = NULL;
index 11d6f3d98398e8b7a1add501dd7ee85a736531b6..a8cc6b266e67847abafa613184752b3b781eb8dd 100644 (file)
@@ -492,9 +492,12 @@ int send_pack(struct send_pack_args *args,
                         * we were to send it and we're trying to send the refs
                         * atomically, abort the whole operation.
                         */
-                       if (use_atomic)
+                       if (use_atomic) {
+                               strbuf_release(&req_buf);
+                               strbuf_release(&cap_buf);
                                return atomic_push_failure(args, remote_refs, ref);
-                       /* Fallthrough for non atomic case. */
+                       }
+                       /* else fallthrough */
                default:
                        continue;
                }
index e0e66b987b27072da4aea6304a565ab708be91e4..e258bb6469a77123b191f85ba679e446ef9c73b6 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"
 
@@ -203,7 +204,7 @@ int sequencer_remove_state(struct replay_opts *opts)
                free(opts->xopts[i]);
        free(opts->xopts);
 
-       strbuf_addf(&dir, "%s", get_dir(opts));
+       strbuf_addstr(&dir, get_dir(opts));
        remove_dir_recursively(&dir, 0);
        strbuf_release(&dir);
 
@@ -692,7 +693,7 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
 
 static int is_original_commit_empty(struct commit *commit)
 {
-       const unsigned char *ptree_sha1;
+       const struct object_id *ptree_oid;
 
        if (parse_commit(commit))
                return error(_("could not parse commit %s\n"),
@@ -702,12 +703,12 @@ static int is_original_commit_empty(struct commit *commit)
                if (parse_commit(parent))
                        return error(_("could not parse parent commit %s\n"),
                                oid_to_hex(&parent->object.oid));
-               ptree_sha1 = parent->tree->object.oid.hash;
+               ptree_oid = &parent->tree->object.oid;
        } else {
-               ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */
+               ptree_oid = &empty_tree_oid; /* commit is root */
        }
 
-       return !hashcmp(ptree_sha1, commit->tree->object.oid.hash);
+       return !oidcmp(ptree_oid, &commit->tree->object.oid);
 }
 
 /*
@@ -897,18 +898,18 @@ static int update_squash_messages(enum todo_command command,
 
 static void flush_rewritten_pending(void) {
        struct strbuf buf = STRBUF_INIT;
-       unsigned char newsha1[20];
+       struct object_id newoid;
        FILE *out;
 
-       if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), 82) > 0 &&
-           !get_sha1("HEAD", newsha1) &&
+       if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 &&
+           !get_oid("HEAD", &newoid) &&
            (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
                char *bol = buf.buf, *eol;
 
                while (*bol) {
                        eol = strchrnul(bol, '\n');
                        fprintf(out, "%.*s %s\n", (int)(eol - bol),
-                                       bol, sha1_to_hex(newsha1));
+                                       bol, oid_to_hex(&newoid));
                        if (!*eol)
                                break;
                        bol = eol + 1;
@@ -1565,6 +1566,7 @@ static int save_head(const char *head)
        static struct lock_file head_lock;
        struct strbuf buf = STRBUF_INIT;
        int fd;
+       ssize_t written;
 
        fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
        if (fd < 0) {
@@ -1572,7 +1574,9 @@ static int save_head(const char *head)
                return error_errno(_("could not lock HEAD"));
        }
        strbuf_addf(&buf, "%s\n", head);
-       if (write_in_full(fd, buf.buf, buf.len) < 0) {
+       written = write_in_full(fd, buf.buf, buf.len);
+       strbuf_release(&buf);
+       if (written < 0) {
                rollback_lock_file(&head_lock);
                return error_errno(_("could not write to '%s'"),
                                   git_path_head_file());
@@ -1608,36 +1612,37 @@ static int rollback_is_safe(void)
        return !oidcmp(&actual_head, &expected_head);
 }
 
-static int reset_for_rollback(const unsigned char *sha1)
+static int reset_for_rollback(const struct object_id *oid)
 {
        const char *argv[4];    /* reset --merge <arg> + NULL */
 
        argv[0] = "reset";
        argv[1] = "--merge";
-       argv[2] = sha1_to_hex(sha1);
+       argv[2] = oid_to_hex(oid);
        argv[3] = NULL;
        return run_command_v_opt(argv, RUN_GIT_CMD);
 }
 
 static int rollback_single_pick(void)
 {
-       unsigned char head_sha1[20];
+       struct object_id head_oid;
 
        if (!file_exists(git_path_cherry_pick_head()) &&
            !file_exists(git_path_revert_head()))
                return error(_("no cherry-pick or revert in progress"));
-       if (read_ref_full("HEAD", 0, head_sha1, NULL))
+       if (read_ref_full("HEAD", 0, head_oid.hash, NULL))
                return error(_("cannot resolve HEAD"));
-       if (is_null_sha1(head_sha1))
+       if (is_null_oid(&head_oid))
                return error(_("cannot abort from a branch yet to be born"));
-       return reset_for_rollback(head_sha1);
+       return reset_for_rollback(&head_oid);
 }
 
 int sequencer_rollback(struct replay_opts *opts)
 {
        FILE *f;
-       unsigned char sha1[20];
+       struct object_id oid;
        struct strbuf buf = STRBUF_INIT;
+       const char *p;
 
        f = fopen(git_path_head_file(), "r");
        if (!f && errno == ENOENT) {
@@ -1657,12 +1662,12 @@ int sequencer_rollback(struct replay_opts *opts)
                goto fail;
        }
        fclose(f);
-       if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') {
+       if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') {
                error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
                        git_path_head_file());
                goto fail;
        }
-       if (is_null_sha1(sha1)) {
+       if (is_null_oid(&oid)) {
                error(_("cannot abort from a branch yet to be born"));
                goto fail;
        }
@@ -1672,7 +1677,7 @@ int sequencer_rollback(struct replay_opts *opts)
                warning(_("You seem to have moved HEAD. "
                          "Not rewinding, check your HEAD!"));
        } else
-       if (reset_for_rollback(sha1))
+       if (reset_for_rollback(&oid))
                goto fail;
        strbuf_release(&buf);
        return sequencer_remove_state(opts);
@@ -1806,13 +1811,13 @@ static int make_patch(struct commit *commit, struct replay_opts *opts)
 
 static int intend_to_amend(void)
 {
-       unsigned char head[20];
+       struct object_id head;
        char *p;
 
-       if (get_sha1("HEAD", head))
+       if (get_oid("HEAD", &head))
                return error(_("cannot read HEAD"));
 
-       p = sha1_to_hex(head);
+       p = oid_to_hex(&head);
        return write_message(p, strlen(p), rebase_path_amend(), 1);
 }
 
@@ -2097,10 +2102,10 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                if (read_oneliner(&head_ref, rebase_path_head_name(), 0) &&
                                starts_with(head_ref.buf, "refs/")) {
                        const char *msg;
-                       unsigned char head[20], orig[20];
+                       struct object_id head, orig;
                        int res;
 
-                       if (get_sha1("HEAD", head)) {
+                       if (get_oid("HEAD", &head)) {
                                res = error(_("cannot read HEAD"));
 cleanup_head_ref:
                                strbuf_release(&head_ref);
@@ -2108,7 +2113,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                                return res;
                        }
                        if (!read_oneliner(&buf, rebase_path_orig_head(), 0) ||
-                                       get_sha1_hex(buf.buf, orig)) {
+                                       get_oid_hex(buf.buf, &orig)) {
                                res = error(_("could not read orig-head"));
                                goto cleanup_head_ref;
                        }
@@ -2119,7 +2124,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                        }
                        msg = reflog_message(opts, "finish", "%s onto %s",
                                head_ref.buf, buf.buf);
-                       if (update_ref(msg, head_ref.buf, head, orig,
+                       if (update_ref(msg, head_ref.buf, head.hash, orig.hash,
                                        REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) {
                                res = error(_("could not update %s"),
                                        head_ref.buf);
@@ -2147,8 +2152,8 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                        log_tree_opt.disable_stdin = 1;
 
                        if (read_oneliner(&buf, rebase_path_orig_head(), 0) &&
-                           !get_sha1(buf.buf, orig.hash) &&
-                           !get_sha1("HEAD", head.hash)) {
+                           !get_oid(buf.buf, &orig) &&
+                           !get_oid("HEAD", &head)) {
                                diff_tree_oid(&orig, &head, "",
                                              &log_tree_opt.diffopt);
                                log_tree_diff_flush(&log_tree_opt);
@@ -2223,16 +2228,16 @@ static int commit_staged_changes(struct replay_opts *opts)
 
        if (file_exists(rebase_path_amend())) {
                struct strbuf rev = STRBUF_INIT;
-               unsigned char head[20], to_amend[20];
+               struct object_id head, to_amend;
 
-               if (get_sha1("HEAD", head))
+               if (get_oid("HEAD", &head))
                        return error(_("cannot amend non-existing commit"));
                if (!read_oneliner(&rev, rebase_path_amend(), 0))
                        return error(_("invalid file: '%s'"), rebase_path_amend());
-               if (get_sha1_hex(rev.buf, to_amend))
+               if (get_oid_hex(rev.buf, &to_amend))
                        return error(_("invalid contents: '%s'"),
                                rebase_path_amend());
-               if (hashcmp(head, to_amend))
+               if (oidcmp(&head, &to_amend))
                        return error(_("\nYou have uncommitted changes in your "
                                       "working tree. Please, commit them\n"
                                       "first and then run 'git rebase "
@@ -2284,7 +2289,7 @@ int sequencer_continue(struct replay_opts *opts)
                struct object_id oid;
 
                if (read_oneliner(&buf, rebase_path_stopped_sha(), 1) &&
-                   !get_sha1_committish(buf.buf, oid.hash))
+                   !get_oid_committish(buf.buf, &oid))
                        record_in_rewritten(&oid, peek_command(&todo_list, 0));
                strbuf_release(&buf);
        }
@@ -2431,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);
index 5ec5b1d827a54486c83efc802ebc6eb1f40062c4..26a6c20b7d420f679d7cceaeba4acdb8ae8c00e7 100644 (file)
@@ -3,6 +3,7 @@
 #include "object.h"
 #include "commit.h"
 #include "tag.h"
+#include "packfile.h"
 
 /*
  * Create the file "path" by writing to a temporary file and renaming
diff --git a/setup.c b/setup.c
index 860507e1fdb2d61da71b27eb00d8413f2fabcfa9..03f51e056cd6e672ecd80ba94348173b9d496cc0 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -230,7 +230,7 @@ void verify_filename(const char *prefix,
                     int diagnose_misspelt_rev)
 {
        if (*arg == '-')
-               die("bad flag '%s' used after filename", arg);
+               die("option '%s' must come before non-option arguments", arg);
        if (looks_like_pathspec(arg) || check_filename(prefix, arg))
                return;
        die_verify_filename(prefix, arg, diagnose_misspelt_rev);
@@ -399,11 +399,6 @@ void setup_work_tree(void)
        if (getenv(GIT_WORK_TREE_ENVIRONMENT))
                setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
 
-       /*
-        * NEEDSWORK: this call can essentially be set_git_dir(get_git_dir())
-        * which can cause some problems when trying to free the old value of
-        * gitdir.
-        */
        set_git_dir(remove_leading_path(git_dir, work_tree));
        initialized = 1;
 }
@@ -546,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
@@ -1027,7 +1023,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 {
        static struct strbuf cwd = STRBUF_INIT;
        struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
-       const char *prefix, *env_prefix;
+       const char *prefix;
 
        /*
         * We may have read an incomplete configuration before
@@ -1085,16 +1081,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
                die("BUG: unhandled setup_git_directory_1() result");
        }
 
-       /*
-        * NEEDSWORK: This was a hack in order to get ls-files and grep to have
-        * properly formated output when recursing submodules.  Once ls-files
-        * and grep have been changed to perform this recursing in-process this
-        * needs to be removed.
-        */
-       env_prefix = getenv(GIT_TOPLEVEL_PREFIX_ENVIRONMENT);
-       if (env_prefix)
-               prefix = env_prefix;
-
        if (prefix)
                setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
        else
index 5f069214d9060da8ceb001e6c037607c1d0096bf..2552b7902c70154282eb278ffed7e21b2d618514 100644 (file)
@@ -99,219 +99,3 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
        } while (lo < hi);
        return -lo-1;
 }
-
-/*
- * Conventional binary search loop looks like this:
- *
- *     unsigned lo, hi;
- *      do {
- *              unsigned mi = (lo + hi) / 2;
- *              int cmp = "entry pointed at by mi" minus "target";
- *              if (!cmp)
- *                      return (mi is the wanted one)
- *              if (cmp > 0)
- *                      hi = mi; "mi is larger than target"
- *              else
- *                      lo = mi+1; "mi is smaller than target"
- *      } while (lo < hi);
- *
- * The invariants are:
- *
- * - When entering the loop, lo points at a slot that is never
- *   above the target (it could be at the target), hi points at a
- *   slot that is guaranteed to be above the target (it can never
- *   be at the target).
- *
- * - We find a point 'mi' between lo and hi (mi could be the same
- *   as lo, but never can be as same as hi), and check if it hits
- *   the target.  There are three cases:
- *
- *    - if it is a hit, we are happy.
- *
- *    - if it is strictly higher than the target, we set it to hi,
- *      and repeat the search.
- *
- *    - if it is strictly lower than the target, we update lo to
- *      one slot after it, because we allow lo to be at the target.
- *
- *   If the loop exits, there is no matching entry.
- *
- * When choosing 'mi', we do not have to take the "middle" but
- * anywhere in between lo and hi, as long as lo <= mi < hi is
- * satisfied.  When we somehow know that the distance between the
- * target and lo is much shorter than the target and hi, we could
- * pick mi that is much closer to lo than the midway.
- *
- * Now, we can take advantage of the fact that SHA-1 is a good hash
- * function, and as long as there are enough entries in the table, we
- * can expect uniform distribution.  An entry that begins with for
- * example "deadbeef..." is much likely to appear much later than in
- * the midway of the table.  It can reasonably be expected to be near
- * 87% (222/256) from the top of the table.
- *
- * However, we do not want to pick "mi" too precisely.  If the entry at
- * the 87% in the above example turns out to be higher than the target
- * we are looking for, we would end up narrowing the search space down
- * only by 13%, instead of 50% we would get if we did a simple binary
- * search.  So we would want to hedge our bets by being less aggressive.
- *
- * The table at "table" holds at least "nr" entries of "elem_size"
- * bytes each.  Each entry has the SHA-1 key at "key_offset".  The
- * table is sorted by the SHA-1 key of the entries.  The caller wants
- * to find the entry with "key", and knows that the entry at "lo" is
- * not higher than the entry it is looking for, and that the entry at
- * "hi" is higher than the entry it is looking for.
- */
-int sha1_entry_pos(const void *table,
-                  size_t elem_size,
-                  size_t key_offset,
-                  unsigned lo, unsigned hi, unsigned nr,
-                  const unsigned char *key)
-{
-       const unsigned char *base = table;
-       const unsigned char *hi_key, *lo_key;
-       unsigned ofs_0;
-       static int debug_lookup = -1;
-
-       if (debug_lookup < 0)
-               debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
-
-       if (!nr || lo >= hi)
-               return -1;
-
-       if (nr == hi)
-               hi_key = NULL;
-       else
-               hi_key = base + elem_size * hi + key_offset;
-       lo_key = base + elem_size * lo + key_offset;
-
-       ofs_0 = 0;
-       do {
-               int cmp;
-               unsigned ofs, mi, range;
-               unsigned lov, hiv, kyv;
-               const unsigned char *mi_key;
-
-               range = hi - lo;
-               if (hi_key) {
-                       for (ofs = ofs_0; ofs < 20; ofs++)
-                               if (lo_key[ofs] != hi_key[ofs])
-                                       break;
-                       ofs_0 = ofs;
-                       /*
-                        * byte 0 thru (ofs-1) are the same between
-                        * lo and hi; ofs is the first byte that is
-                        * different.
-                        *
-                        * If ofs==20, then no bytes are different,
-                        * meaning we have entries with duplicate
-                        * keys. We know that we are in a solid run
-                        * of this entry (because the entries are
-                        * sorted, and our lo and hi are the same,
-                        * there can be nothing but this single key
-                        * in between). So we can stop the search.
-                        * Either one of these entries is it (and
-                        * we do not care which), or we do not have
-                        * it.
-                        *
-                        * Furthermore, we know that one of our
-                        * endpoints must be the edge of the run of
-                        * duplicates. For example, given this
-                        * sequence:
-                        *
-                        *     idx 0 1 2 3 4 5
-                        *     key A C C C C D
-                        *
-                        * If we are searching for "B", we might
-                        * hit the duplicate run at lo=1, hi=3
-                        * (e.g., by first mi=3, then mi=0). But we
-                        * can never have lo > 1, because B < C.
-                        * That is, if our key is less than the
-                        * run, we know that "lo" is the edge, but
-                        * we can say nothing of "hi". Similarly,
-                        * if our key is greater than the run, we
-                        * know that "hi" is the edge, but we can
-                        * say nothing of "lo".
-                        *
-                        * Therefore if we do not find it, we also
-                        * know where it would go if it did exist:
-                        * just on the far side of the edge that we
-                        * know about.
-                        */
-                       if (ofs == 20) {
-                               mi = lo;
-                               mi_key = base + elem_size * mi + key_offset;
-                               cmp = memcmp(mi_key, key, 20);
-                               if (!cmp)
-                                       return mi;
-                               if (cmp < 0)
-                                       return -1 - hi;
-                               else
-                                       return -1 - lo;
-                       }
-
-                       hiv = hi_key[ofs_0];
-                       if (ofs_0 < 19)
-                               hiv = (hiv << 8) | hi_key[ofs_0+1];
-               } else {
-                       hiv = 256;
-                       if (ofs_0 < 19)
-                               hiv <<= 8;
-               }
-               lov = lo_key[ofs_0];
-               kyv = key[ofs_0];
-               if (ofs_0 < 19) {
-                       lov = (lov << 8) | lo_key[ofs_0+1];
-                       kyv = (kyv << 8) | key[ofs_0+1];
-               }
-               assert(lov < hiv);
-
-               if (kyv < lov)
-                       return -1 - lo;
-               if (hiv < kyv)
-                       return -1 - hi;
-
-               /*
-                * Even if we know the target is much closer to 'hi'
-                * than 'lo', if we pick too precisely and overshoot
-                * (e.g. when we know 'mi' is closer to 'hi' than to
-                * 'lo', pick 'mi' that is higher than the target), we
-                * end up narrowing the search space by a smaller
-                * amount (i.e. the distance between 'mi' and 'hi')
-                * than what we would have (i.e. about half of 'lo'
-                * and 'hi').  Hedge our bets to pick 'mi' less
-                * aggressively, i.e. make 'mi' a bit closer to the
-                * middle than we would otherwise pick.
-                */
-               kyv = (kyv * 6 + lov + hiv) / 8;
-               if (lov < hiv - 1) {
-                       if (kyv == lov)
-                               kyv++;
-                       else if (kyv == hiv)
-                               kyv--;
-               }
-               mi = (range - 1) * (kyv - lov) / (hiv - lov) + lo;
-
-               if (debug_lookup) {
-                       printf("lo %u hi %u rg %u mi %u ", lo, hi, range, mi);
-                       printf("ofs %u lov %x, hiv %x, kyv %x\n",
-                              ofs_0, lov, hiv, kyv);
-               }
-               if (!(lo <= mi && mi < hi))
-                       die("assertion failure lo %u mi %u hi %u %s",
-                           lo, mi, hi, sha1_to_hex(key));
-
-               mi_key = base + elem_size * mi + key_offset;
-               cmp = memcmp(mi_key + ofs_0, key + ofs_0, 20 - ofs_0);
-               if (!cmp)
-                       return mi;
-               if (cmp > 0) {
-                       hi = mi;
-                       hi_key = mi_key;
-               } else {
-                       lo = mi + 1;
-                       lo_key = mi_key + elem_size;
-               }
-       } while (lo < hi);
-       return -lo-1;
-}
index 20af2856818ed51b2afb1718a7e317133ee0d7bd..cf5314f402ce78f0d5ab2bd72ee7f334b6394e04 100644 (file)
@@ -7,10 +7,4 @@ extern int sha1_pos(const unsigned char *sha1,
                    void *table,
                    size_t nr,
                    sha1_access_fn fn);
-
-extern int sha1_entry_pos(const void *table,
-                         size_t elem_size,
-                         size_t key_offset,
-                         unsigned lo, unsigned hi, unsigned nr,
-                         const unsigned char *key);
 #endif
index 4fa4b185f312d903929ed8c601c9ae04026bce59..10c3a0083dedee98dc3d48267ad349589adef3f0 100644 (file)
 #include "list.h"
 #include "mergesort.h"
 #include "quote.h"
+#include "packfile.h"
 
-#define SZ_FMT PRIuMAX
-static inline uintmax_t sz_fmt(size_t s) { return s; }
-
-const unsigned char null_sha1[20];
+const unsigned char null_sha1[GIT_MAX_RAWSZ];
 const struct object_id null_oid;
 const struct object_id empty_tree_oid = {
        EMPTY_TREE_SHA1_BIN_LITERAL
@@ -278,28 +276,6 @@ static const char *alt_sha1_path(struct alternate_object_database *alt,
        return buf->buf;
 }
 
- char *odb_pack_name(struct strbuf *buf,
-                    const unsigned char *sha1,
-                    const char *ext)
-{
-       strbuf_reset(buf);
-       strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
-                   sha1_to_hex(sha1), ext);
-       return buf->buf;
-}
-
-char *sha1_pack_name(const unsigned char *sha1)
-{
-       static struct strbuf buf = STRBUF_INIT;
-       return odb_pack_name(&buf, sha1, "pack");
-}
-
-char *sha1_pack_index_name(const unsigned char *sha1)
-{
-       static struct strbuf buf = STRBUF_INIT;
-       return odb_pack_name(&buf, sha1, "idx");
-}
-
 struct alternate_object_database *alt_odb_list;
 static struct alternate_object_database **alt_odb_tail;
 
@@ -422,7 +398,7 @@ static const char *parse_alt_odb_entry(const char *string,
        return end;
 }
 
-static void link_alt_odb_entries(const char *alt, int len, int sep,
+static void link_alt_odb_entries(const char *alt, int sep,
                                 const char *relative_base, int depth)
 {
        struct strbuf objdirbuf = STRBUF_INIT;
@@ -451,28 +427,19 @@ static void link_alt_odb_entries(const char *alt, int len, int sep,
 
 static void read_info_alternates(const char * relative_base, int depth)
 {
-       char *map;
-       size_t mapsz;
-       struct stat st;
        char *path;
-       int fd;
+       struct strbuf buf = STRBUF_INIT;
 
        path = xstrfmt("%s/info/alternates", relative_base);
-       fd = git_open(path);
-       free(path);
-       if (fd < 0)
-               return;
-       if (fstat(fd, &st) || (st.st_size == 0)) {
-               close(fd);
+       if (strbuf_read_file(&buf, path, 1024) < 0) {
+               warn_on_fopen_errors(path);
+               free(path);
                return;
        }
-       mapsz = xsize_t(st.st_size);
-       map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
-       close(fd);
 
-       link_alt_odb_entries(map, mapsz, '\n', relative_base, depth);
-
-       munmap(map, mapsz);
+       link_alt_odb_entries(buf.buf, '\n', relative_base, depth);
+       strbuf_release(&buf);
+       free(path);
 }
 
 struct alternate_object_database *alloc_alt_odb(const char *dir)
@@ -527,7 +494,7 @@ void add_to_alternates_file(const char *reference)
                if (commit_lock_file(lock))
                        die_errno("unable to move new alternates file into place");
                if (alt_odb_tail)
-                       link_alt_odb_entries(reference, strlen(reference), '\n', NULL, 0);
+                       link_alt_odb_entries(reference, '\n', NULL, 0);
        }
        free(alts);
 }
@@ -540,7 +507,7 @@ void add_to_alternates_memory(const char *reference)
         */
        prepare_alt_odb();
 
-       link_alt_odb_entries(reference, strlen(reference), '\n', NULL, 0);
+       link_alt_odb_entries(reference, '\n', NULL, 0);
 }
 
 /*
@@ -643,7 +610,7 @@ void prepare_alt_odb(void)
        if (!alt) alt = "";
 
        alt_odb_tail = &alt_odb_list;
-       link_alt_odb_entries(alt, strlen(alt), PATH_SEP, NULL, 0);
+       link_alt_odb_entries(alt, PATH_SEP, NULL, 0);
 
        read_info_alternates(get_object_directory(), 0);
 }
@@ -705,213 +672,6 @@ static int has_loose_object(const unsigned char *sha1)
        return check_and_freshen(sha1, 0);
 }
 
-static unsigned int pack_used_ctr;
-static unsigned int pack_mmap_calls;
-static unsigned int peak_pack_open_windows;
-static unsigned int pack_open_windows;
-static unsigned int pack_open_fds;
-static unsigned int pack_max_fds;
-static size_t peak_pack_mapped;
-static size_t pack_mapped;
-struct packed_git *packed_git;
-
-static struct mru packed_git_mru_storage;
-struct mru *packed_git_mru = &packed_git_mru_storage;
-
-void pack_report(void)
-{
-       fprintf(stderr,
-               "pack_report: getpagesize()            = %10" SZ_FMT "\n"
-               "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
-               "pack_report: core.packedGitLimit      = %10" SZ_FMT "\n",
-               sz_fmt(getpagesize()),
-               sz_fmt(packed_git_window_size),
-               sz_fmt(packed_git_limit));
-       fprintf(stderr,
-               "pack_report: pack_used_ctr            = %10u\n"
-               "pack_report: pack_mmap_calls          = %10u\n"
-               "pack_report: pack_open_windows        = %10u / %10u\n"
-               "pack_report: pack_mapped              = "
-                       "%10" SZ_FMT " / %10" SZ_FMT "\n",
-               pack_used_ctr,
-               pack_mmap_calls,
-               pack_open_windows, peak_pack_open_windows,
-               sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
-}
-
-/*
- * Open and mmap the index file at path, perform a couple of
- * consistency checks, then record its information to p.  Return 0 on
- * success.
- */
-static int check_packed_git_idx(const char *path, struct packed_git *p)
-{
-       void *idx_map;
-       struct pack_idx_header *hdr;
-       size_t idx_size;
-       uint32_t version, nr, i, *index;
-       int fd = git_open(path);
-       struct stat st;
-
-       if (fd < 0)
-               return -1;
-       if (fstat(fd, &st)) {
-               close(fd);
-               return -1;
-       }
-       idx_size = xsize_t(st.st_size);
-       if (idx_size < 4 * 256 + 20 + 20) {
-               close(fd);
-               return error("index file %s is too small", path);
-       }
-       idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
-       close(fd);
-
-       hdr = idx_map;
-       if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
-               version = ntohl(hdr->idx_version);
-               if (version < 2 || version > 2) {
-                       munmap(idx_map, idx_size);
-                       return error("index file %s is version %"PRIu32
-                                    " and is not supported by this binary"
-                                    " (try upgrading GIT to a newer version)",
-                                    path, version);
-               }
-       } else
-               version = 1;
-
-       nr = 0;
-       index = idx_map;
-       if (version > 1)
-               index += 2;  /* skip index header */
-       for (i = 0; i < 256; i++) {
-               uint32_t n = ntohl(index[i]);
-               if (n < nr) {
-                       munmap(idx_map, idx_size);
-                       return error("non-monotonic index %s", path);
-               }
-               nr = n;
-       }
-
-       if (version == 1) {
-               /*
-                * Total size:
-                *  - 256 index entries 4 bytes each
-                *  - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
-                *  - 20-byte SHA1 of the packfile
-                *  - 20-byte SHA1 file checksum
-                */
-               if (idx_size != 4*256 + nr * 24 + 20 + 20) {
-                       munmap(idx_map, idx_size);
-                       return error("wrong index v1 file size in %s", path);
-               }
-       } else if (version == 2) {
-               /*
-                * Minimum size:
-                *  - 8 bytes of header
-                *  - 256 index entries 4 bytes each
-                *  - 20-byte sha1 entry * nr
-                *  - 4-byte crc entry * nr
-                *  - 4-byte offset entry * nr
-                *  - 20-byte SHA1 of the packfile
-                *  - 20-byte SHA1 file checksum
-                * And after the 4-byte offset table might be a
-                * variable sized table containing 8-byte entries
-                * for offsets larger than 2^31.
-                */
-               unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
-               unsigned long max_size = min_size;
-               if (nr)
-                       max_size += (nr - 1)*8;
-               if (idx_size < min_size || idx_size > max_size) {
-                       munmap(idx_map, idx_size);
-                       return error("wrong index v2 file size in %s", path);
-               }
-               if (idx_size != min_size &&
-                   /*
-                    * make sure we can deal with large pack offsets.
-                    * 31-bit signed offset won't be enough, neither
-                    * 32-bit unsigned one will be.
-                    */
-                   (sizeof(off_t) <= 4)) {
-                       munmap(idx_map, idx_size);
-                       return error("pack too large for current definition of off_t in %s", path);
-               }
-       }
-
-       p->index_version = version;
-       p->index_data = idx_map;
-       p->index_size = idx_size;
-       p->num_objects = nr;
-       return 0;
-}
-
-int open_pack_index(struct packed_git *p)
-{
-       char *idx_name;
-       size_t len;
-       int ret;
-
-       if (p->index_data)
-               return 0;
-
-       if (!strip_suffix(p->pack_name, ".pack", &len))
-               die("BUG: pack_name does not end in .pack");
-       idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
-       ret = check_packed_git_idx(idx_name, p);
-       free(idx_name);
-       return ret;
-}
-
-static void scan_windows(struct packed_git *p,
-       struct packed_git **lru_p,
-       struct pack_window **lru_w,
-       struct pack_window **lru_l)
-{
-       struct pack_window *w, *w_l;
-
-       for (w_l = NULL, w = p->windows; w; w = w->next) {
-               if (!w->inuse_cnt) {
-                       if (!*lru_w || w->last_used < (*lru_w)->last_used) {
-                               *lru_p = p;
-                               *lru_w = w;
-                               *lru_l = w_l;
-                       }
-               }
-               w_l = w;
-       }
-}
-
-static int unuse_one_window(struct packed_git *current)
-{
-       struct packed_git *p, *lru_p = NULL;
-       struct pack_window *lru_w = NULL, *lru_l = NULL;
-
-       if (current)
-               scan_windows(current, &lru_p, &lru_w, &lru_l);
-       for (p = packed_git; p; p = p->next)
-               scan_windows(p, &lru_p, &lru_w, &lru_l);
-       if (lru_p) {
-               munmap(lru_w->base, lru_w->len);
-               pack_mapped -= lru_w->len;
-               if (lru_l)
-                       lru_l->next = lru_w->next;
-               else
-                       lru_p->windows = lru_w->next;
-               free(lru_w);
-               pack_open_windows--;
-               return 1;
-       }
-       return 0;
-}
-
-void release_pack_memory(size_t need)
-{
-       size_t cur = pack_mapped;
-       while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
-               ; /* nothing */
-}
-
 static void mmap_limit_check(size_t length)
 {
        static size_t limit = 0;
@@ -950,1959 +710,360 @@ void *xmmap(void *start, size_t length,
        return ret;
 }
 
-void close_pack_windows(struct packed_git *p)
-{
-       while (p->windows) {
-               struct pack_window *w = p->windows;
-
-               if (w->inuse_cnt)
-                       die("pack '%s' still has open windows to it",
-                           p->pack_name);
-               munmap(w->base, w->len);
-               pack_mapped -= w->len;
-               pack_open_windows--;
-               p->windows = w->next;
-               free(w);
-       }
-}
-
-static int close_pack_fd(struct packed_git *p)
-{
-       if (p->pack_fd < 0)
-               return 0;
-
-       close(p->pack_fd);
-       pack_open_fds--;
-       p->pack_fd = -1;
-
-       return 1;
-}
-
-static void close_pack(struct packed_git *p)
-{
-       close_pack_windows(p);
-       close_pack_fd(p);
-       close_pack_index(p);
-}
-
-void close_all_packs(void)
-{
-       struct packed_git *p;
-
-       for (p = packed_git; p; p = p->next)
-               if (p->do_not_close)
-                       die("BUG: want to close pack marked 'do-not-close'");
-               else
-                       close_pack(p);
-}
-
-
 /*
- * The LRU pack is the one with the oldest MRU window, preferring packs
- * with no used windows, or the oldest mtime if it has no windows allocated.
+ * With an in-core object data in "map", rehash it to make sure the
+ * object name actually matches "sha1" to detect object corruption.
+ * With "map" == NULL, try reading the object named with "sha1" using
+ * the streaming interface and rehash it to do the same.
  */
-static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
-{
-       struct pack_window *w, *this_mru_w;
-       int has_windows_inuse = 0;
-
-       /*
-        * Reject this pack if it has windows and the previously selected
-        * one does not.  If this pack does not have windows, reject
-        * it if the pack file is newer than the previously selected one.
-        */
-       if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
-               return;
-
-       for (w = this_mru_w = p->windows; w; w = w->next) {
-               /*
-                * Reject this pack if any of its windows are in use,
-                * but the previously selected pack did not have any
-                * inuse windows.  Otherwise, record that this pack
-                * has windows in use.
-                */
-               if (w->inuse_cnt) {
-                       if (*accept_windows_inuse)
-                               has_windows_inuse = 1;
-                       else
-                               return;
-               }
-
-               if (w->last_used > this_mru_w->last_used)
-                       this_mru_w = w;
-
-               /*
-                * Reject this pack if it has windows that have been
-                * used more recently than the previously selected pack.
-                * If the previously selected pack had windows inuse and
-                * we have not encountered a window in this pack that is
-                * inuse, skip this check since we prefer a pack with no
-                * inuse windows to one that has inuse windows.
-                */
-               if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
-                   this_mru_w->last_used > (*mru_w)->last_used)
-                       return;
-       }
-
-       /*
-        * Select this pack.
-        */
-       *mru_w = this_mru_w;
-       *lru_p = p;
-       *accept_windows_inuse = has_windows_inuse;
-}
-
-static int close_one_pack(void)
+int check_sha1_signature(const unsigned char *sha1, void *map,
+                        unsigned long size, const char *type)
 {
-       struct packed_git *p, *lru_p = NULL;
-       struct pack_window *mru_w = NULL;
-       int accept_windows_inuse = 1;
+       unsigned char real_sha1[20];
+       enum object_type obj_type;
+       struct git_istream *st;
+       git_SHA_CTX c;
+       char hdr[32];
+       int hdrlen;
 
-       for (p = packed_git; p; p = p->next) {
-               if (p->pack_fd == -1)
-                       continue;
-               find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
+       if (map) {
+               hash_sha1_file(map, size, type, real_sha1);
+               return hashcmp(sha1, real_sha1) ? -1 : 0;
        }
 
-       if (lru_p)
-               return close_pack_fd(lru_p);
+       st = open_istream(sha1, &obj_type, &size, NULL);
+       if (!st)
+               return -1;
 
-       return 0;
-}
+       /* Generate the header */
+       hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
 
-void unuse_pack(struct pack_window **w_cursor)
-{
-       struct pack_window *w = *w_cursor;
-       if (w) {
-               w->inuse_cnt--;
-               *w_cursor = NULL;
-       }
-}
+       /* Sha1.. */
+       git_SHA1_Init(&c);
+       git_SHA1_Update(&c, hdr, hdrlen);
+       for (;;) {
+               char buf[1024 * 16];
+               ssize_t readlen = read_istream(st, buf, sizeof(buf));
 
-void close_pack_index(struct packed_git *p)
-{
-       if (p->index_data) {
-               munmap((void *)p->index_data, p->index_size);
-               p->index_data = NULL;
+               if (readlen < 0) {
+                       close_istream(st);
+                       return -1;
+               }
+               if (!readlen)
+                       break;
+               git_SHA1_Update(&c, buf, readlen);
        }
+       git_SHA1_Final(real_sha1, &c);
+       close_istream(st);
+       return hashcmp(sha1, real_sha1) ? -1 : 0;
 }
 
-static unsigned int get_max_fd_limit(void)
+int git_open_cloexec(const char *name, int flags)
 {
-#ifdef RLIMIT_NOFILE
-       {
-               struct rlimit lim;
+       int fd;
+       static int o_cloexec = O_CLOEXEC;
 
-               if (!getrlimit(RLIMIT_NOFILE, &lim))
-                       return lim.rlim_cur;
+       fd = open(name, flags | o_cloexec);
+       if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
+               /* Try again w/o O_CLOEXEC: the kernel might not support it */
+               o_cloexec &= ~O_CLOEXEC;
+               fd = open(name, flags | o_cloexec);
        }
-#endif
 
-#ifdef _SC_OPEN_MAX
+#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
        {
-               long open_max = sysconf(_SC_OPEN_MAX);
-               if (0 < open_max)
-                       return open_max;
-               /*
-                * Otherwise, we got -1 for one of the two
-                * reasons:
-                *
-                * (1) sysconf() did not understand _SC_OPEN_MAX
-                *     and signaled an error with -1; or
-                * (2) sysconf() said there is no limit.
-                *
-                * We _could_ clear errno before calling sysconf() to
-                * tell these two cases apart and return a huge number
-                * in the latter case to let the caller cap it to a
-                * value that is not so selfish, but letting the
-                * fallback OPEN_MAX codepath take care of these cases
-                * is a lot simpler.
-                */
-       }
-#endif
+               static int fd_cloexec = FD_CLOEXEC;
 
-#ifdef OPEN_MAX
-       return OPEN_MAX;
-#else
-       return 1; /* see the caller ;-) */
+               if (!o_cloexec && 0 <= fd && fd_cloexec) {
+                       /* Opened w/o O_CLOEXEC?  try with fcntl(2) to add it */
+                       int flags = fcntl(fd, F_GETFD);
+                       if (fcntl(fd, F_SETFD, flags | fd_cloexec))
+                               fd_cloexec = 0;
+               }
+       }
 #endif
+       return fd;
 }
 
 /*
- * Do not call this directly as this leaks p->pack_fd on error return;
- * call open_packed_git() instead.
+ * Find "sha1" as a loose object in the local repository or in an alternate.
+ * Returns 0 on success, negative on failure.
+ *
+ * The "path" out-parameter will give the path of the object we found (if any).
+ * Note that it may point to static storage and is only valid until another
+ * call to sha1_file_name(), etc.
  */
-static int open_packed_git_1(struct packed_git *p)
+static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
+                         const char **path)
 {
-       struct stat st;
-       struct pack_header hdr;
-       unsigned char sha1[20];
-       unsigned char *idx_sha1;
-       long fd_flag;
-
-       if (!p->index_data && open_pack_index(p))
-               return error("packfile %s index unavailable", p->pack_name);
+       struct alternate_object_database *alt;
 
-       if (!pack_max_fds) {
-               unsigned int max_fds = get_max_fd_limit();
+       *path = sha1_file_name(sha1);
+       if (!lstat(*path, st))
+               return 0;
 
-               /* Save 3 for stdin/stdout/stderr, 22 for work */
-               if (25 < max_fds)
-                       pack_max_fds = max_fds - 25;
-               else
-                       pack_max_fds = 1;
+       prepare_alt_odb();
+       errno = ENOENT;
+       for (alt = alt_odb_list; alt; alt = alt->next) {
+               *path = alt_sha1_path(alt, sha1);
+               if (!lstat(*path, st))
+                       return 0;
        }
 
-       while (pack_max_fds <= pack_open_fds && close_one_pack())
-               ; /* nothing */
-
-       p->pack_fd = git_open(p->pack_name);
-       if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
-               return -1;
-       pack_open_fds++;
-
-       /* If we created the struct before we had the pack we lack size. */
-       if (!p->pack_size) {
-               if (!S_ISREG(st.st_mode))
-                       return error("packfile %s not a regular file", p->pack_name);
-               p->pack_size = st.st_size;
-       } else if (p->pack_size != st.st_size)
-               return error("packfile %s size changed", p->pack_name);
-
-       /* We leave these file descriptors open with sliding mmap;
-        * there is no point keeping them open across exec(), though.
-        */
-       fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
-       if (fd_flag < 0)
-               return error("cannot determine file descriptor flags");
-       fd_flag |= FD_CLOEXEC;
-       if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
-               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))
-               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);
-       if (!pack_version_ok(hdr.hdr_version))
-               return error("packfile %s is version %"PRIu32" and not"
-                       " supported (try upgrading GIT to a newer version)",
-                       p->pack_name, ntohl(hdr.hdr_version));
-
-       /* Verify the pack matches its index. */
-       if (p->num_objects != ntohl(hdr.hdr_entries))
-               return error("packfile %s claims to have %"PRIu32" objects"
-                            " while index indicates %"PRIu32" objects",
-                            p->pack_name, ntohl(hdr.hdr_entries),
-                            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))
-               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))
-               return error("packfile %s does not match index", p->pack_name);
-       return 0;
-}
-
-static int open_packed_git(struct packed_git *p)
-{
-       if (!open_packed_git_1(p))
-               return 0;
-       close_pack_fd(p);
        return -1;
 }
 
-static int in_window(struct pack_window *win, off_t offset)
+/*
+ * Like stat_sha1_file(), but actually open the object and return the
+ * descriptor. See the caveats on the "path" parameter above.
+ */
+static int open_sha1_file(const unsigned char *sha1, const char **path)
 {
-       /* We must promise at least 20 bytes (one hash) after the
-        * offset is available from this window, otherwise the offset
-        * is not actually in this window and a different window (which
-        * has that one hash excess) must be used.  This is to support
-        * the object header and delta base parsing routines below.
-        */
-       off_t win_off = win->offset;
-       return win_off <= offset
-               && (offset + 20) <= (win_off + win->len);
-}
+       int fd;
+       struct alternate_object_database *alt;
+       int most_interesting_errno;
 
-unsigned char *use_pack(struct packed_git *p,
-               struct pack_window **w_cursor,
-               off_t offset,
-               unsigned long *left)
-{
-       struct pack_window *win = *w_cursor;
+       *path = sha1_file_name(sha1);
+       fd = git_open(*path);
+       if (fd >= 0)
+               return fd;
+       most_interesting_errno = errno;
 
-       /* Since packfiles end in a hash of their content and it's
-        * pointless to ask for an offset into the middle of that
-        * hash, and the in_window function above wouldn't match
-        * don't allow an offset too close to the end of the file.
-        */
-       if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
-               die("packfile %s cannot be accessed", p->pack_name);
-       if (offset > (p->pack_size - 20))
-               die("offset beyond end of packfile (truncated pack?)");
-       if (offset < 0)
-               die(_("offset before end of packfile (broken .idx?)"));
-
-       if (!win || !in_window(win, offset)) {
-               if (win)
-                       win->inuse_cnt--;
-               for (win = p->windows; win; win = win->next) {
-                       if (in_window(win, offset))
-                               break;
-               }
-               if (!win) {
-                       size_t window_align = packed_git_window_size / 2;
-                       off_t len;
-
-                       if (p->pack_fd == -1 && open_packed_git(p))
-                               die("packfile %s cannot be accessed", p->pack_name);
-
-                       win = xcalloc(1, sizeof(*win));
-                       win->offset = (offset / window_align) * window_align;
-                       len = p->pack_size - win->offset;
-                       if (len > packed_git_window_size)
-                               len = packed_git_window_size;
-                       win->len = (size_t)len;
-                       pack_mapped += win->len;
-                       while (packed_git_limit < pack_mapped
-                               && unuse_one_window(p))
-                               ; /* nothing */
-                       win->base = xmmap(NULL, win->len,
-                               PROT_READ, MAP_PRIVATE,
-                               p->pack_fd, win->offset);
-                       if (win->base == MAP_FAILED)
-                               die_errno("packfile %s cannot be mapped",
-                                         p->pack_name);
-                       if (!win->offset && win->len == p->pack_size
-                               && !p->do_not_close)
-                               close_pack_fd(p);
-                       pack_mmap_calls++;
-                       pack_open_windows++;
-                       if (pack_mapped > peak_pack_mapped)
-                               peak_pack_mapped = pack_mapped;
-                       if (pack_open_windows > peak_pack_open_windows)
-                               peak_pack_open_windows = pack_open_windows;
-                       win->next = p->windows;
-                       p->windows = win;
-               }
-       }
-       if (win != *w_cursor) {
-               win->last_used = pack_used_ctr++;
-               win->inuse_cnt++;
-               *w_cursor = win;
+       prepare_alt_odb();
+       for (alt = alt_odb_list; alt; alt = alt->next) {
+               *path = alt_sha1_path(alt, sha1);
+               fd = git_open(*path);
+               if (fd >= 0)
+                       return fd;
+               if (most_interesting_errno == ENOENT)
+                       most_interesting_errno = errno;
        }
-       offset -= win->offset;
-       if (left)
-               *left = win->len - xsize_t(offset);
-       return win->base + offset;
-}
-
-static struct packed_git *alloc_packed_git(int extra)
-{
-       struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
-       memset(p, 0, sizeof(*p));
-       p->pack_fd = -1;
-       return p;
-}
-
-static void try_to_free_pack_memory(size_t size)
-{
-       release_pack_memory(size);
-}
-
-struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
-{
-       static int have_set_try_to_free_routine;
-       struct stat st;
-       size_t alloc;
-       struct packed_git *p;
-
-       if (!have_set_try_to_free_routine) {
-               have_set_try_to_free_routine = 1;
-               set_try_to_free_routine(try_to_free_pack_memory);
-       }
-
-       /*
-        * Make sure a corresponding .pack file exists and that
-        * the index looks sane.
-        */
-       if (!strip_suffix_mem(path, &path_len, ".idx"))
-               return NULL;
-
-       /*
-        * ".pack" is long enough to hold any suffix we're adding (and
-        * the use xsnprintf double-checks that)
-        */
-       alloc = st_add3(path_len, strlen(".pack"), 1);
-       p = alloc_packed_git(alloc);
-       memcpy(p->pack_name, path, path_len);
-
-       xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
-       if (!access(p->pack_name, F_OK))
-               p->pack_keep = 1;
-
-       xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
-       if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
-               free(p);
-               return NULL;
-       }
-
-       /* ok, it looks sane as far as we can check without
-        * actually mapping the pack file.
-        */
-       p->pack_size = st.st_size;
-       p->pack_local = local;
-       p->mtime = st.st_mtime;
-       if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
-               hashclr(p->sha1);
-       return p;
-}
-
-struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
-{
-       const char *path = sha1_pack_name(sha1);
-       size_t alloc = st_add(strlen(path), 1);
-       struct packed_git *p = alloc_packed_git(alloc);
-
-       memcpy(p->pack_name, path, alloc); /* includes NUL */
-       hashcpy(p->sha1, sha1);
-       if (check_packed_git_idx(idx_path, p)) {
-               free(p);
-               return NULL;
-       }
-
-       return p;
-}
-
-void install_packed_git(struct packed_git *pack)
-{
-       if (pack->pack_fd != -1)
-               pack_open_fds++;
-
-       pack->next = packed_git;
-       packed_git = pack;
-}
-
-void (*report_garbage)(unsigned seen_bits, const char *path);
-
-static void report_helper(const struct string_list *list,
-                         int seen_bits, int first, int last)
-{
-       if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
-               return;
-
-       for (; first < last; first++)
-               report_garbage(seen_bits, list->items[first].string);
-}
-
-static void report_pack_garbage(struct string_list *list)
-{
-       int i, baselen = -1, first = 0, seen_bits = 0;
-
-       if (!report_garbage)
-               return;
-
-       string_list_sort(list);
-
-       for (i = 0; i < list->nr; i++) {
-               const char *path = list->items[i].string;
-               if (baselen != -1 &&
-                   strncmp(path, list->items[first].string, baselen)) {
-                       report_helper(list, seen_bits, first, i);
-                       baselen = -1;
-                       seen_bits = 0;
-               }
-               if (baselen == -1) {
-                       const char *dot = strrchr(path, '.');
-                       if (!dot) {
-                               report_garbage(PACKDIR_FILE_GARBAGE, path);
-                               continue;
-                       }
-                       baselen = dot - path + 1;
-                       first = i;
-               }
-               if (!strcmp(path + baselen, "pack"))
-                       seen_bits |= 1;
-               else if (!strcmp(path + baselen, "idx"))
-                       seen_bits |= 2;
-       }
-       report_helper(list, seen_bits, first, list->nr);
-}
-
-static void prepare_packed_git_one(char *objdir, int local)
-{
-       struct strbuf path = STRBUF_INIT;
-       size_t dirnamelen;
-       DIR *dir;
-       struct dirent *de;
-       struct string_list garbage = STRING_LIST_INIT_DUP;
-
-       strbuf_addstr(&path, objdir);
-       strbuf_addstr(&path, "/pack");
-       dir = opendir(path.buf);
-       if (!dir) {
-               if (errno != ENOENT)
-                       error_errno("unable to open object pack directory: %s",
-                                   path.buf);
-               strbuf_release(&path);
-               return;
-       }
-       strbuf_addch(&path, '/');
-       dirnamelen = path.len;
-       while ((de = readdir(dir)) != NULL) {
-               struct packed_git *p;
-               size_t base_len;
-
-               if (is_dot_or_dotdot(de->d_name))
-                       continue;
-
-               strbuf_setlen(&path, dirnamelen);
-               strbuf_addstr(&path, de->d_name);
-
-               base_len = path.len;
-               if (strip_suffix_mem(path.buf, &base_len, ".idx")) {
-                       /* Don't reopen a pack we already have. */
-                       for (p = packed_git; p; p = p->next) {
-                               size_t len;
-                               if (strip_suffix(p->pack_name, ".pack", &len) &&
-                                   len == base_len &&
-                                   !memcmp(p->pack_name, path.buf, len))
-                                       break;
-                       }
-                       if (p == NULL &&
-                           /*
-                            * See if it really is a valid .idx file with
-                            * corresponding .pack file that we can map.
-                            */
-                           (p = add_packed_git(path.buf, path.len, local)) != NULL)
-                               install_packed_git(p);
-               }
-
-               if (!report_garbage)
-                       continue;
-
-               if (ends_with(de->d_name, ".idx") ||
-                   ends_with(de->d_name, ".pack") ||
-                   ends_with(de->d_name, ".bitmap") ||
-                   ends_with(de->d_name, ".keep"))
-                       string_list_append(&garbage, path.buf);
-               else
-                       report_garbage(PACKDIR_FILE_GARBAGE, path.buf);
-       }
-       closedir(dir);
-       report_pack_garbage(&garbage);
-       string_list_clear(&garbage, 0);
-       strbuf_release(&path);
-}
-
-static int approximate_object_count_valid;
-
-/*
- * Give a fast, rough count of the number of objects in the repository. This
- * ignores loose objects completely. If you have a lot of them, then either
- * you should repack because your performance will be awful, or they are
- * all unreachable objects about to be pruned, in which case they're not really
- * interesting as a measure of repo size in the first place.
- */
-unsigned long approximate_object_count(void)
-{
-       static unsigned long count;
-       if (!approximate_object_count_valid) {
-               struct packed_git *p;
-
-               prepare_packed_git();
-               count = 0;
-               for (p = packed_git; p; p = p->next) {
-                       if (open_pack_index(p))
-                               continue;
-                       count += p->num_objects;
-               }
-       }
-       return count;
-}
-
-static void *get_next_packed_git(const void *p)
-{
-       return ((const struct packed_git *)p)->next;
-}
-
-static void set_next_packed_git(void *p, void *next)
-{
-       ((struct packed_git *)p)->next = next;
-}
-
-static int sort_pack(const void *a_, const void *b_)
-{
-       const struct packed_git *a = a_;
-       const struct packed_git *b = b_;
-       int st;
-
-       /*
-        * Local packs tend to contain objects specific to our
-        * variant of the project than remote ones.  In addition,
-        * remote ones could be on a network mounted filesystem.
-        * Favor local ones for these reasons.
-        */
-       st = a->pack_local - b->pack_local;
-       if (st)
-               return -st;
-
-       /*
-        * Younger packs tend to contain more recent objects,
-        * and more recent objects tend to get accessed more
-        * often.
-        */
-       if (a->mtime < b->mtime)
-               return 1;
-       else if (a->mtime == b->mtime)
-               return 0;
-       return -1;
-}
-
-static void rearrange_packed_git(void)
-{
-       packed_git = llist_mergesort(packed_git, get_next_packed_git,
-                                    set_next_packed_git, sort_pack);
-}
-
-static void prepare_packed_git_mru(void)
-{
-       struct packed_git *p;
-
-       mru_clear(packed_git_mru);
-       for (p = packed_git; p; p = p->next)
-               mru_append(packed_git_mru, p);
-}
-
-static int prepare_packed_git_run_once = 0;
-void prepare_packed_git(void)
-{
-       struct alternate_object_database *alt;
-
-       if (prepare_packed_git_run_once)
-               return;
-       prepare_packed_git_one(get_object_directory(), 1);
-       prepare_alt_odb();
-       for (alt = alt_odb_list; alt; alt = alt->next)
-               prepare_packed_git_one(alt->path, 0);
-       rearrange_packed_git();
-       prepare_packed_git_mru();
-       prepare_packed_git_run_once = 1;
-}
-
-void reprepare_packed_git(void)
-{
-       approximate_object_count_valid = 0;
-       prepare_packed_git_run_once = 0;
-       prepare_packed_git();
-}
-
-static void mark_bad_packed_object(struct packed_git *p,
-                                  const unsigned char *sha1)
-{
-       unsigned i;
-       for (i = 0; i < p->num_bad_objects; i++)
-               if (!hashcmp(sha1, p->bad_object_sha1 + GIT_SHA1_RAWSZ * i))
-                       return;
-       p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
-                                     st_mult(GIT_MAX_RAWSZ,
-                                             st_add(p->num_bad_objects, 1)));
-       hashcpy(p->bad_object_sha1 + GIT_SHA1_RAWSZ * p->num_bad_objects, sha1);
-       p->num_bad_objects++;
-}
-
-static const struct packed_git *has_packed_and_bad(const unsigned char *sha1)
-{
-       struct packed_git *p;
-       unsigned i;
-
-       for (p = packed_git; p; p = p->next)
-               for (i = 0; i < p->num_bad_objects; i++)
-                       if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
-                               return p;
-       return NULL;
-}
-
-/*
- * With an in-core object data in "map", rehash it to make sure the
- * object name actually matches "sha1" to detect object corruption.
- * With "map" == NULL, try reading the object named with "sha1" using
- * the streaming interface and rehash it to do the same.
- */
-int check_sha1_signature(const unsigned char *sha1, void *map,
-                        unsigned long size, const char *type)
-{
-       unsigned char real_sha1[20];
-       enum object_type obj_type;
-       struct git_istream *st;
-       git_SHA_CTX c;
-       char hdr[32];
-       int hdrlen;
-
-       if (map) {
-               hash_sha1_file(map, size, type, real_sha1);
-               return hashcmp(sha1, real_sha1) ? -1 : 0;
-       }
-
-       st = open_istream(sha1, &obj_type, &size, NULL);
-       if (!st)
-               return -1;
-
-       /* Generate the header */
-       hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
-
-       /* Sha1.. */
-       git_SHA1_Init(&c);
-       git_SHA1_Update(&c, hdr, hdrlen);
-       for (;;) {
-               char buf[1024 * 16];
-               ssize_t readlen = read_istream(st, buf, sizeof(buf));
-
-               if (readlen < 0) {
-                       close_istream(st);
-                       return -1;
-               }
-               if (!readlen)
-                       break;
-               git_SHA1_Update(&c, buf, readlen);
-       }
-       git_SHA1_Final(real_sha1, &c);
-       close_istream(st);
-       return hashcmp(sha1, real_sha1) ? -1 : 0;
-}
-
-int git_open_cloexec(const char *name, int flags)
-{
-       int fd;
-       static int o_cloexec = O_CLOEXEC;
-
-       fd = open(name, flags | o_cloexec);
-       if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
-               /* Try again w/o O_CLOEXEC: the kernel might not support it */
-               o_cloexec &= ~O_CLOEXEC;
-               fd = open(name, flags | o_cloexec);
-       }
-
-#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
-       {
-               static int fd_cloexec = FD_CLOEXEC;
-
-               if (!o_cloexec && 0 <= fd && fd_cloexec) {
-                       /* Opened w/o O_CLOEXEC?  try with fcntl(2) to add it */
-                       int flags = fcntl(fd, F_GETFD);
-                       if (fcntl(fd, F_SETFD, flags | fd_cloexec))
-                               fd_cloexec = 0;
-               }
-       }
-#endif
-       return fd;
-}
-
-/*
- * Find "sha1" as a loose object in the local repository or in an alternate.
- * Returns 0 on success, negative on failure.
- *
- * The "path" out-parameter will give the path of the object we found (if any).
- * Note that it may point to static storage and is only valid until another
- * call to sha1_file_name(), etc.
- */
-static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
-                         const char **path)
-{
-       struct alternate_object_database *alt;
-
-       *path = sha1_file_name(sha1);
-       if (!lstat(*path, st))
-               return 0;
-
-       prepare_alt_odb();
-       errno = ENOENT;
-       for (alt = alt_odb_list; alt; alt = alt->next) {
-               *path = alt_sha1_path(alt, sha1);
-               if (!lstat(*path, st))
-                       return 0;
-       }
-
-       return -1;
-}
-
-/*
- * Like stat_sha1_file(), but actually open the object and return the
- * descriptor. See the caveats on the "path" parameter above.
- */
-static int open_sha1_file(const unsigned char *sha1, const char **path)
-{
-       int fd;
-       struct alternate_object_database *alt;
-       int most_interesting_errno;
-
-       *path = sha1_file_name(sha1);
-       fd = git_open(*path);
-       if (fd >= 0)
-               return fd;
-       most_interesting_errno = errno;
-
-       prepare_alt_odb();
-       for (alt = alt_odb_list; alt; alt = alt->next) {
-               *path = alt_sha1_path(alt, sha1);
-               fd = git_open(*path);
-               if (fd >= 0)
-                       return fd;
-               if (most_interesting_errno == ENOENT)
-                       most_interesting_errno = errno;
-       }
-       errno = most_interesting_errno;
-       return -1;
-}
-
-/*
- * Map the loose object at "path" if it is not NULL, or the path found by
- * searching for a loose object named "sha1".
- */
-static void *map_sha1_file_1(const char *path,
-                            const unsigned char *sha1,
-                            unsigned long *size)
-{
-       void *map;
-       int fd;
-
-       if (path)
-               fd = git_open(path);
-       else
-               fd = open_sha1_file(sha1, &path);
-       map = NULL;
-       if (fd >= 0) {
-               struct stat st;
-
-               if (!fstat(fd, &st)) {
-                       *size = xsize_t(st.st_size);
-                       if (!*size) {
-                               /* mmap() is forbidden on empty files */
-                               error("object file %s is empty", path);
-                               return NULL;
-                       }
-                       map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
-               }
-               close(fd);
-       }
-       return map;
-}
-
-void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
-{
-       return map_sha1_file_1(NULL, sha1, size);
-}
-
-unsigned long unpack_object_header_buffer(const unsigned char *buf,
-               unsigned long len, enum object_type *type, unsigned long *sizep)
-{
-       unsigned shift;
-       unsigned long size, c;
-       unsigned long used = 0;
-
-       c = buf[used++];
-       *type = (c >> 4) & 7;
-       size = c & 15;
-       shift = 4;
-       while (c & 0x80) {
-               if (len <= used || bitsizeof(long) <= shift) {
-                       error("bad object header");
-                       size = used = 0;
-                       break;
-               }
-               c = buf[used++];
-               size += (c & 0x7f) << shift;
-               shift += 7;
-       }
-       *sizep = size;
-       return used;
-}
-
-static int unpack_sha1_short_header(git_zstream *stream,
-                                   unsigned char *map, unsigned long mapsize,
-                                   void *buffer, unsigned long bufsiz)
-{
-       /* Get the data stream */
-       memset(stream, 0, sizeof(*stream));
-       stream->next_in = map;
-       stream->avail_in = mapsize;
-       stream->next_out = buffer;
-       stream->avail_out = bufsiz;
-
-       git_inflate_init(stream);
-       return git_inflate(stream, 0);
-}
-
-int unpack_sha1_header(git_zstream *stream,
-                      unsigned char *map, unsigned long mapsize,
-                      void *buffer, unsigned long bufsiz)
-{
-       int status = unpack_sha1_short_header(stream, map, mapsize,
-                                             buffer, bufsiz);
-
-       if (status < Z_OK)
-               return status;
-
-       /* Make sure we have the terminating NUL */
-       if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
-               return -1;
-       return 0;
-}
-
-static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
-                                       unsigned long mapsize, void *buffer,
-                                       unsigned long bufsiz, struct strbuf *header)
-{
-       int status;
-
-       status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
-       if (status < Z_OK)
-               return -1;
-
-       /*
-        * Check if entire header is unpacked in the first iteration.
-        */
-       if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
-               return 0;
-
-       /*
-        * buffer[0..bufsiz] was not large enough.  Copy the partial
-        * result out to header, and then append the result of further
-        * reading the stream.
-        */
-       strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
-       stream->next_out = buffer;
-       stream->avail_out = bufsiz;
-
-       do {
-               status = git_inflate(stream, 0);
-               strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
-               if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
-                       return 0;
-               stream->next_out = buffer;
-               stream->avail_out = bufsiz;
-       } while (status != Z_STREAM_END);
-       return -1;
-}
-
-static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
-{
-       int bytes = strlen(buffer) + 1;
-       unsigned char *buf = xmallocz(size);
-       unsigned long n;
-       int status = Z_OK;
-
-       n = stream->total_out - bytes;
-       if (n > size)
-               n = size;
-       memcpy(buf, (char *) buffer + bytes, n);
-       bytes = n;
-       if (bytes <= size) {
-               /*
-                * The above condition must be (bytes <= size), not
-                * (bytes < size).  In other words, even though we
-                * expect no more output and set avail_out to zero,
-                * the input zlib stream may have bytes that express
-                * "this concludes the stream", and we *do* want to
-                * eat that input.
-                *
-                * Otherwise we would not be able to test that we
-                * consumed all the input to reach the expected size;
-                * we also want to check that zlib tells us that all
-                * went well with status == Z_STREAM_END at the end.
-                */
-               stream->next_out = buf + bytes;
-               stream->avail_out = size - bytes;
-               while (status == Z_OK)
-                       status = git_inflate(stream, Z_FINISH);
-       }
-       if (status == Z_STREAM_END && !stream->avail_in) {
-               git_inflate_end(stream);
-               return buf;
-       }
-
-       if (status < 0)
-               error("corrupt loose object '%s'", sha1_to_hex(sha1));
-       else if (stream->avail_in)
-               error("garbage at end of loose object '%s'",
-                     sha1_to_hex(sha1));
-       free(buf);
-       return NULL;
-}
-
-/*
- * We used to just use "sscanf()", but that's actually way
- * too permissive for what we want to check. So do an anal
- * object header parse by hand.
- */
-static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
-                              unsigned int flags)
-{
-       const char *type_buf = hdr;
-       unsigned long size;
-       int type, type_len = 0;
-
-       /*
-        * The type can be of any size but is followed by
-        * a space.
-        */
-       for (;;) {
-               char c = *hdr++;
-               if (!c)
-                       return -1;
-               if (c == ' ')
-                       break;
-               type_len++;
-       }
-
-       type = type_from_string_gently(type_buf, type_len, 1);
-       if (oi->typename)
-               strbuf_add(oi->typename, type_buf, type_len);
-       /*
-        * Set type to 0 if its an unknown object and
-        * we're obtaining the type using '--allow-unknown-type'
-        * option.
-        */
-       if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0))
-               type = 0;
-       else if (type < 0)
-               die("invalid object type");
-       if (oi->typep)
-               *oi->typep = type;
-
-       /*
-        * The length must follow immediately, and be in canonical
-        * decimal format (ie "010" is not valid).
-        */
-       size = *hdr++ - '0';
-       if (size > 9)
-               return -1;
-       if (size) {
-               for (;;) {
-                       unsigned long c = *hdr - '0';
-                       if (c > 9)
-                               break;
-                       hdr++;
-                       size = size * 10 + c;
-               }
-       }
-
-       if (oi->sizep)
-               *oi->sizep = size;
-
-       /*
-        * The length must be followed by a zero byte
-        */
-       return *hdr ? -1 : type;
-}
-
-int parse_sha1_header(const char *hdr, unsigned long *sizep)
-{
-       struct object_info oi = OBJECT_INFO_INIT;
-
-       oi.sizep = sizep;
-       return parse_sha1_header_extended(hdr, &oi, 0);
-}
-
-unsigned long get_size_from_delta(struct packed_git *p,
-                                 struct pack_window **w_curs,
-                                 off_t curpos)
-{
-       const unsigned char *data;
-       unsigned char delta_head[20], *in;
-       git_zstream stream;
-       int st;
-
-       memset(&stream, 0, sizeof(stream));
-       stream.next_out = delta_head;
-       stream.avail_out = sizeof(delta_head);
-
-       git_inflate_init(&stream);
-       do {
-               in = use_pack(p, w_curs, curpos, &stream.avail_in);
-               stream.next_in = in;
-               st = git_inflate(&stream, Z_FINISH);
-               curpos += stream.next_in - in;
-       } while ((st == Z_OK || st == Z_BUF_ERROR) &&
-                stream.total_out < sizeof(delta_head));
-       git_inflate_end(&stream);
-       if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
-               error("delta data unpack-initial failed");
-               return 0;
-       }
-
-       /* Examine the initial part of the delta to figure out
-        * the result size.
-        */
-       data = delta_head;
-
-       /* ignore base size */
-       get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
-
-       /* Read the result size */
-       return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
-}
-
-static off_t get_delta_base(struct packed_git *p,
-                                   struct pack_window **w_curs,
-                                   off_t *curpos,
-                                   enum object_type type,
-                                   off_t delta_obj_offset)
-{
-       unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
-       off_t base_offset;
-
-       /* use_pack() assured us we have [base_info, base_info + 20)
-        * as a range that we can look at without walking off the
-        * end of the mapped window.  Its actually the hash size
-        * that is assured.  An OFS_DELTA longer than the hash size
-        * is stupid, as then a REF_DELTA would be smaller to store.
-        */
-       if (type == OBJ_OFS_DELTA) {
-               unsigned used = 0;
-               unsigned char c = base_info[used++];
-               base_offset = c & 127;
-               while (c & 128) {
-                       base_offset += 1;
-                       if (!base_offset || MSB(base_offset, 7))
-                               return 0;  /* overflow */
-                       c = base_info[used++];
-                       base_offset = (base_offset << 7) + (c & 127);
-               }
-               base_offset = delta_obj_offset - base_offset;
-               if (base_offset <= 0 || base_offset >= delta_obj_offset)
-                       return 0;  /* out of bound */
-               *curpos += used;
-       } else if (type == OBJ_REF_DELTA) {
-               /* The base entry _must_ be in the same pack */
-               base_offset = find_pack_entry_one(base_info, p);
-               *curpos += 20;
-       } else
-               die("I am totally screwed");
-       return base_offset;
+       errno = most_interesting_errno;
+       return -1;
 }
 
 /*
- * Like get_delta_base above, but we return the sha1 instead of the pack
- * offset. This means it is cheaper for REF deltas (we do not have to do
- * the final object lookup), but more expensive for OFS deltas (we
- * have to load the revidx to convert the offset back into a sha1).
+ * Map the loose object at "path" if it is not NULL, or the path found by
+ * searching for a loose object named "sha1".
  */
-static const unsigned char *get_delta_base_sha1(struct packed_git *p,
-                                               struct pack_window **w_curs,
-                                               off_t curpos,
-                                               enum object_type type,
-                                               off_t delta_obj_offset)
-{
-       if (type == OBJ_REF_DELTA) {
-               unsigned char *base = use_pack(p, w_curs, curpos, NULL);
-               return base;
-       } else if (type == OBJ_OFS_DELTA) {
-               struct revindex_entry *revidx;
-               off_t base_offset = get_delta_base(p, w_curs, &curpos,
-                                                  type, delta_obj_offset);
-
-               if (!base_offset)
-                       return NULL;
-
-               revidx = find_pack_revindex(p, base_offset);
-               if (!revidx)
-                       return NULL;
-
-               return nth_packed_object_sha1(p, revidx->nr);
-       } else
-               return NULL;
-}
-
-int unpack_object_header(struct packed_git *p,
-                        struct pack_window **w_curs,
-                        off_t *curpos,
-                        unsigned long *sizep)
-{
-       unsigned char *base;
-       unsigned long left;
-       unsigned long used;
-       enum object_type type;
-
-       /* use_pack() assures us we have [base, base + 20) available
-        * as a range that we can look at.  (Its actually the hash
-        * size that is assured.)  With our object header encoding
-        * the maximum deflated object size is 2^137, which is just
-        * insane, so we know won't exceed what we have been given.
-        */
-       base = use_pack(p, w_curs, *curpos, &left);
-       used = unpack_object_header_buffer(base, left, &type, sizep);
-       if (!used) {
-               type = OBJ_BAD;
-       } else
-               *curpos += used;
-
-       return type;
-}
-
-static int retry_bad_packed_offset(struct packed_git *p, off_t obj_offset)
-{
-       int type;
-       struct revindex_entry *revidx;
-       const unsigned char *sha1;
-       revidx = find_pack_revindex(p, obj_offset);
-       if (!revidx)
-               return OBJ_BAD;
-       sha1 = nth_packed_object_sha1(p, revidx->nr);
-       mark_bad_packed_object(p, sha1);
-       type = sha1_object_info(sha1, NULL);
-       if (type <= OBJ_NONE)
-               return OBJ_BAD;
-       return type;
-}
-
-#define POI_STACK_PREALLOC 64
-
-static enum object_type packed_to_object_type(struct packed_git *p,
-                                             off_t obj_offset,
-                                             enum object_type type,
-                                             struct pack_window **w_curs,
-                                             off_t curpos)
-{
-       off_t small_poi_stack[POI_STACK_PREALLOC];
-       off_t *poi_stack = small_poi_stack;
-       int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
-
-       while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
-               off_t base_offset;
-               unsigned long size;
-               /* Push the object we're going to leave behind */
-               if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
-                       poi_stack_alloc = alloc_nr(poi_stack_nr);
-                       ALLOC_ARRAY(poi_stack, poi_stack_alloc);
-                       memcpy(poi_stack, small_poi_stack, sizeof(off_t)*poi_stack_nr);
-               } else {
-                       ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
-               }
-               poi_stack[poi_stack_nr++] = obj_offset;
-               /* If parsing the base offset fails, just unwind */
-               base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
-               if (!base_offset)
-                       goto unwind;
-               curpos = obj_offset = base_offset;
-               type = unpack_object_header(p, w_curs, &curpos, &size);
-               if (type <= OBJ_NONE) {
-                       /* If getting the base itself fails, we first
-                        * retry the base, otherwise unwind */
-                       type = retry_bad_packed_offset(p, base_offset);
-                       if (type > OBJ_NONE)
-                               goto out;
-                       goto unwind;
-               }
-       }
-
-       switch (type) {
-       case OBJ_BAD:
-       case OBJ_COMMIT:
-       case OBJ_TREE:
-       case OBJ_BLOB:
-       case OBJ_TAG:
-               break;
-       default:
-               error("unknown object type %i at offset %"PRIuMAX" in %s",
-                     type, (uintmax_t)obj_offset, p->pack_name);
-               type = OBJ_BAD;
-       }
-
-out:
-       if (poi_stack != small_poi_stack)
-               free(poi_stack);
-       return type;
-
-unwind:
-       while (poi_stack_nr) {
-               obj_offset = poi_stack[--poi_stack_nr];
-               type = retry_bad_packed_offset(p, obj_offset);
-               if (type > OBJ_NONE)
-                       goto out;
-       }
-       type = OBJ_BAD;
-       goto out;
-}
-
-static struct hashmap delta_base_cache;
-static size_t delta_base_cached;
-
-static LIST_HEAD(delta_base_cache_lru);
-
-struct delta_base_cache_key {
-       struct packed_git *p;
-       off_t base_offset;
-};
-
-struct delta_base_cache_entry {
-       struct hashmap hash;
-       struct delta_base_cache_key key;
-       struct list_head lru;
-       void *data;
-       unsigned long size;
-       enum object_type type;
-};
-
-static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
-{
-       unsigned int hash;
-
-       hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
-       hash += (hash >> 8) + (hash >> 16);
-       return hash;
-}
-
-static struct delta_base_cache_entry *
-get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
-{
-       struct hashmap_entry entry;
-       struct delta_base_cache_key key;
-
-       if (!delta_base_cache.cmpfn)
-               return NULL;
-
-       hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
-       key.p = p;
-       key.base_offset = base_offset;
-       return hashmap_get(&delta_base_cache, &entry, &key);
-}
-
-static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
-                                  const struct delta_base_cache_key *b)
+static void *map_sha1_file_1(const char *path,
+                            const unsigned char *sha1,
+                            unsigned long *size)
 {
-       return a->p == b->p && a->base_offset == b->base_offset;
-}
+       void *map;
+       int fd;
 
-static int delta_base_cache_hash_cmp(const void *unused_cmp_data,
-                                    const void *va, const void *vb,
-                                    const void *vkey)
-{
-       const struct delta_base_cache_entry *a = va, *b = vb;
-       const struct delta_base_cache_key *key = vkey;
-       if (key)
-               return !delta_base_cache_key_eq(&a->key, key);
+       if (path)
+               fd = git_open(path);
        else
-               return !delta_base_cache_key_eq(&a->key, &b->key);
-}
-
-static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
-{
-       return !!get_delta_base_cache_entry(p, base_offset);
-}
-
-/*
- * Remove the entry from the cache, but do _not_ free the associated
- * entry data. The caller takes ownership of the "data" buffer, and
- * should copy out any fields it wants before detaching.
- */
-static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
-{
-       hashmap_remove(&delta_base_cache, ent, &ent->key);
-       list_del(&ent->lru);
-       delta_base_cached -= ent->size;
-       free(ent);
-}
-
-static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
-       unsigned long *base_size, enum object_type *type)
-{
-       struct delta_base_cache_entry *ent;
-
-       ent = get_delta_base_cache_entry(p, base_offset);
-       if (!ent)
-               return unpack_entry(p, base_offset, type, base_size);
-
-       if (type)
-               *type = ent->type;
-       if (base_size)
-               *base_size = ent->size;
-       return xmemdupz(ent->data, ent->size);
-}
-
-static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
-{
-       free(ent->data);
-       detach_delta_base_cache_entry(ent);
-}
-
-void clear_delta_base_cache(void)
-{
-       struct list_head *lru, *tmp;
-       list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
-               struct delta_base_cache_entry *entry =
-                       list_entry(lru, struct delta_base_cache_entry, lru);
-               release_delta_base_cache(entry);
-       }
-}
-
-static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
-       void *base, unsigned long base_size, enum object_type type)
-{
-       struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
-       struct list_head *lru, *tmp;
-
-       delta_base_cached += base_size;
-
-       list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
-               struct delta_base_cache_entry *f =
-                       list_entry(lru, struct delta_base_cache_entry, lru);
-               if (delta_base_cached <= delta_base_cache_limit)
-                       break;
-               release_delta_base_cache(f);
-       }
-
-       ent->key.p = p;
-       ent->key.base_offset = base_offset;
-       ent->type = type;
-       ent->data = base;
-       ent->size = base_size;
-       list_add_tail(&ent->lru, &delta_base_cache_lru);
-
-       if (!delta_base_cache.cmpfn)
-               hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
-       hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
-       hashmap_add(&delta_base_cache, ent);
-}
-
-int packed_object_info(struct packed_git *p, off_t obj_offset,
-                      struct object_info *oi)
-{
-       struct pack_window *w_curs = NULL;
-       unsigned long size;
-       off_t curpos = obj_offset;
-       enum object_type type;
-
-       /*
-        * We always get the representation type, but only convert it to
-        * a "real" type later if the caller is interested.
-        */
-       if (oi->contentp) {
-               *oi->contentp = cache_or_unpack_entry(p, obj_offset, oi->sizep,
-                                                     &type);
-               if (!*oi->contentp)
-                       type = OBJ_BAD;
-       } else {
-               type = unpack_object_header(p, &w_curs, &curpos, &size);
-       }
-
-       if (!oi->contentp && oi->sizep) {
-               if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
-                       off_t tmp_pos = curpos;
-                       off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
-                                                          type, obj_offset);
-                       if (!base_offset) {
-                               type = OBJ_BAD;
-                               goto out;
-                       }
-                       *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos);
-                       if (*oi->sizep == 0) {
-                               type = OBJ_BAD;
-                               goto out;
-                       }
-               } else {
-                       *oi->sizep = size;
-               }
-       }
-
-       if (oi->disk_sizep) {
-               struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
-               *oi->disk_sizep = revidx[1].offset - obj_offset;
-       }
-
-       if (oi->typep || oi->typename) {
-               enum object_type ptot;
-               ptot = packed_to_object_type(p, obj_offset, type, &w_curs,
-                                            curpos);
-               if (oi->typep)
-                       *oi->typep = ptot;
-               if (oi->typename) {
-                       const char *tn = typename(ptot);
-                       if (tn)
-                               strbuf_addstr(oi->typename, tn);
-               }
-               if (ptot < 0) {
-                       type = OBJ_BAD;
-                       goto out;
-               }
-       }
-
-       if (oi->delta_base_sha1) {
-               if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
-                       const unsigned char *base;
-
-                       base = get_delta_base_sha1(p, &w_curs, curpos,
-                                                  type, obj_offset);
-                       if (!base) {
-                               type = OBJ_BAD;
-                               goto out;
-                       }
-
-                       hashcpy(oi->delta_base_sha1, base);
-               } else
-                       hashclr(oi->delta_base_sha1);
-       }
-
-out:
-       unuse_pack(&w_curs);
-       return type;
-}
-
-static void *unpack_compressed_entry(struct packed_git *p,
-                                   struct pack_window **w_curs,
-                                   off_t curpos,
-                                   unsigned long size)
-{
-       int st;
-       git_zstream stream;
-       unsigned char *buffer, *in;
-
-       buffer = xmallocz_gently(size);
-       if (!buffer)
-               return NULL;
-       memset(&stream, 0, sizeof(stream));
-       stream.next_out = buffer;
-       stream.avail_out = size + 1;
-
-       git_inflate_init(&stream);
-       do {
-               in = use_pack(p, w_curs, curpos, &stream.avail_in);
-               stream.next_in = in;
-               st = git_inflate(&stream, Z_FINISH);
-               if (!stream.avail_out)
-                       break; /* the payload is larger than it should be */
-               curpos += stream.next_in - in;
-       } while (st == Z_OK || st == Z_BUF_ERROR);
-       git_inflate_end(&stream);
-       if ((st != Z_STREAM_END) || stream.total_out != size) {
-               free(buffer);
-               return NULL;
-       }
-
-       return buffer;
-}
-
-static void *read_object(const unsigned char *sha1, enum object_type *type,
-                        unsigned long *size);
-
-static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
-{
-       static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
-       trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
-                        p->pack_name, (uintmax_t)obj_offset);
-}
-
-int do_check_packed_object_crc;
-
-#define UNPACK_ENTRY_STACK_PREALLOC 64
-struct unpack_entry_stack_ent {
-       off_t obj_offset;
-       off_t curpos;
-       unsigned long size;
-};
-
-void *unpack_entry(struct packed_git *p, off_t obj_offset,
-                  enum object_type *final_type, unsigned long *final_size)
-{
-       struct pack_window *w_curs = NULL;
-       off_t curpos = obj_offset;
-       void *data = NULL;
-       unsigned long size;
-       enum object_type type;
-       struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
-       struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
-       int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
-       int base_from_cache = 0;
-
-       write_pack_access_log(p, obj_offset);
-
-       /* PHASE 1: drill down to the innermost base object */
-       for (;;) {
-               off_t base_offset;
-               int i;
-               struct delta_base_cache_entry *ent;
-
-               ent = get_delta_base_cache_entry(p, curpos);
-               if (ent) {
-                       type = ent->type;
-                       data = ent->data;
-                       size = ent->size;
-                       detach_delta_base_cache_entry(ent);
-                       base_from_cache = 1;
-                       break;
-               }
-
-               if (do_check_packed_object_crc && p->index_version > 1) {
-                       struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
-                       off_t len = revidx[1].offset - obj_offset;
-                       if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
-                               const unsigned char *sha1 =
-                                       nth_packed_object_sha1(p, revidx->nr);
-                               error("bad packed object CRC for %s",
-                                     sha1_to_hex(sha1));
-                               mark_bad_packed_object(p, sha1);
-                               data = NULL;
-                               goto out;
-                       }
-               }
-
-               type = unpack_object_header(p, &w_curs, &curpos, &size);
-               if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
-                       break;
-
-               base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
-               if (!base_offset) {
-                       error("failed to validate delta base reference "
-                             "at offset %"PRIuMAX" from %s",
-                             (uintmax_t)curpos, p->pack_name);
-                       /* bail to phase 2, in hopes of recovery */
-                       data = NULL;
-                       break;
-               }
-
-               /* push object, proceed to base */
-               if (delta_stack_nr >= delta_stack_alloc
-                   && delta_stack == small_delta_stack) {
-                       delta_stack_alloc = alloc_nr(delta_stack_nr);
-                       ALLOC_ARRAY(delta_stack, delta_stack_alloc);
-                       memcpy(delta_stack, small_delta_stack,
-                              sizeof(*delta_stack)*delta_stack_nr);
-               } else {
-                       ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
-               }
-               i = delta_stack_nr++;
-               delta_stack[i].obj_offset = obj_offset;
-               delta_stack[i].curpos = curpos;
-               delta_stack[i].size = size;
-
-               curpos = obj_offset = base_offset;
-       }
-
-       /* PHASE 2: handle the base */
-       switch (type) {
-       case OBJ_OFS_DELTA:
-       case OBJ_REF_DELTA:
-               if (data)
-                       die("BUG: unpack_entry: left loop at a valid delta");
-               break;
-       case OBJ_COMMIT:
-       case OBJ_TREE:
-       case OBJ_BLOB:
-       case OBJ_TAG:
-               if (!base_from_cache)
-                       data = unpack_compressed_entry(p, &w_curs, curpos, size);
-               break;
-       default:
-               data = NULL;
-               error("unknown object type %i at offset %"PRIuMAX" in %s",
-                     type, (uintmax_t)obj_offset, p->pack_name);
-       }
-
-       /* PHASE 3: apply deltas in order */
+               fd = open_sha1_file(sha1, &path);
+       map = NULL;
+       if (fd >= 0) {
+               struct stat st;
 
-       /* invariants:
-        *   'data' holds the base data, or NULL if there was corruption
-        */
-       while (delta_stack_nr) {
-               void *delta_data;
-               void *base = data;
-               void *external_base = NULL;
-               unsigned long delta_size, base_size = size;
-               int i;
-
-               data = NULL;
-
-               if (base)
-                       add_delta_base_cache(p, obj_offset, base, base_size, type);
-
-               if (!base) {
-                       /*
-                        * We're probably in deep shit, but let's try to fetch
-                        * the required base anyway from another pack or loose.
-                        * This is costly but should happen only in the presence
-                        * of a corrupted pack, and is better than failing outright.
-                        */
-                       struct revindex_entry *revidx;
-                       const unsigned char *base_sha1;
-                       revidx = find_pack_revindex(p, obj_offset);
-                       if (revidx) {
-                               base_sha1 = nth_packed_object_sha1(p, revidx->nr);
-                               error("failed to read delta base object %s"
-                                     " at offset %"PRIuMAX" from %s",
-                                     sha1_to_hex(base_sha1), (uintmax_t)obj_offset,
-                                     p->pack_name);
-                               mark_bad_packed_object(p, base_sha1);
-                               base = read_object(base_sha1, &type, &base_size);
-                               external_base = base;
+               if (!fstat(fd, &st)) {
+                       *size = xsize_t(st.st_size);
+                       if (!*size) {
+                               /* mmap() is forbidden on empty files */
+                               error("object file %s is empty", path);
+                               return NULL;
                        }
+                       map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
                }
-
-               i = --delta_stack_nr;
-               obj_offset = delta_stack[i].obj_offset;
-               curpos = delta_stack[i].curpos;
-               delta_size = delta_stack[i].size;
-
-               if (!base)
-                       continue;
-
-               delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
-
-               if (!delta_data) {
-                       error("failed to unpack compressed delta "
-                             "at offset %"PRIuMAX" from %s",
-                             (uintmax_t)curpos, p->pack_name);
-                       data = NULL;
-                       free(external_base);
-                       continue;
-               }
-
-               data = patch_delta(base, base_size,
-                                  delta_data, delta_size,
-                                  &size);
-
-               /*
-                * We could not apply the delta; warn the user, but keep going.
-                * Our failure will be noticed either in the next iteration of
-                * the loop, or if this is the final delta, in the caller when
-                * we return NULL. Those code paths will take care of making
-                * a more explicit warning and retrying with another copy of
-                * the object.
-                */
-               if (!data)
-                       error("failed to apply delta");
-
-               free(delta_data);
-               free(external_base);
-       }
-
-       if (final_type)
-               *final_type = type;
-       if (final_size)
-               *final_size = size;
-
-out:
-       unuse_pack(&w_curs);
-
-       if (delta_stack != small_delta_stack)
-               free(delta_stack);
-
-       return data;
-}
-
-const unsigned char *nth_packed_object_sha1(struct packed_git *p,
-                                           uint32_t n)
-{
-       const unsigned char *index = p->index_data;
-       if (!index) {
-               if (open_pack_index(p))
-                       return NULL;
-               index = p->index_data;
-       }
-       if (n >= p->num_objects)
-               return NULL;
-       index += 4 * 256;
-       if (p->index_version == 1) {
-               return index + 24 * n + 4;
-       } else {
-               index += 8;
-               return index + 20 * n;
+               close(fd);
        }
+       return map;
 }
 
-const struct object_id *nth_packed_object_oid(struct object_id *oid,
-                                             struct packed_git *p,
-                                             uint32_t n)
-{
-       const unsigned char *hash = nth_packed_object_sha1(p, n);
-       if (!hash)
-               return NULL;
-       hashcpy(oid->hash, hash);
-       return oid;
-}
-
-void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
+void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
 {
-       const unsigned char *ptr = vptr;
-       const unsigned char *start = p->index_data;
-       const unsigned char *end = start + p->index_size;
-       if (ptr < start)
-               die(_("offset before start of pack index for %s (corrupt index?)"),
-                   p->pack_name);
-       /* No need to check for underflow; .idx files must be at least 8 bytes */
-       if (ptr >= end - 8)
-               die(_("offset beyond end of pack index for %s (truncated index?)"),
-                   p->pack_name);
+       return map_sha1_file_1(NULL, sha1, size);
 }
 
-off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
+static int unpack_sha1_short_header(git_zstream *stream,
+                                   unsigned char *map, unsigned long mapsize,
+                                   void *buffer, unsigned long bufsiz)
 {
-       const unsigned char *index = p->index_data;
-       index += 4 * 256;
-       if (p->index_version == 1) {
-               return ntohl(*((uint32_t *)(index + 24 * n)));
-       } else {
-               uint32_t off;
-               index += 8 + p->num_objects * (20 + 4);
-               off = ntohl(*((uint32_t *)(index + 4 * n)));
-               if (!(off & 0x80000000))
-                       return off;
-               index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
-               check_pack_index_ptr(p, index);
-               return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
-                                  ntohl(*((uint32_t *)(index + 4)));
-       }
+       /* Get the data stream */
+       memset(stream, 0, sizeof(*stream));
+       stream->next_in = map;
+       stream->avail_in = mapsize;
+       stream->next_out = buffer;
+       stream->avail_out = bufsiz;
+
+       git_inflate_init(stream);
+       return git_inflate(stream, 0);
 }
 
-off_t find_pack_entry_one(const unsigned char *sha1,
-                                 struct packed_git *p)
+int unpack_sha1_header(git_zstream *stream,
+                      unsigned char *map, unsigned long mapsize,
+                      void *buffer, unsigned long bufsiz)
 {
-       const uint32_t *level1_ofs = p->index_data;
-       const unsigned char *index = p->index_data;
-       unsigned hi, lo, stride;
-       static int use_lookup = -1;
-       static int debug_lookup = -1;
-
-       if (debug_lookup < 0)
-               debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
-
-       if (!index) {
-               if (open_pack_index(p))
-                       return 0;
-               level1_ofs = p->index_data;
-               index = p->index_data;
-       }
-       if (p->index_version > 1) {
-               level1_ofs += 2;
-               index += 8;
-       }
-       index += 4 * 256;
-       hi = ntohl(level1_ofs[*sha1]);
-       lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
-       if (p->index_version > 1) {
-               stride = 20;
-       } else {
-               stride = 24;
-               index += 4;
-       }
-
-       if (debug_lookup)
-               printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
-                      sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
+       int status = unpack_sha1_short_header(stream, map, mapsize,
+                                             buffer, bufsiz);
 
-       if (use_lookup < 0)
-               use_lookup = !!getenv("GIT_USE_LOOKUP");
-       if (use_lookup) {
-               int pos = sha1_entry_pos(index, stride, 0,
-                                        lo, hi, p->num_objects, sha1);
-               if (pos < 0)
-                       return 0;
-               return nth_packed_object_offset(p, pos);
-       }
+       if (status < Z_OK)
+               return status;
 
-       while (lo < hi) {
-               unsigned mi = (lo + hi) / 2;
-               int cmp = hashcmp(index + mi * stride, sha1);
-
-               if (debug_lookup)
-                       printf("lo %u hi %u rg %u mi %u\n",
-                              lo, hi, hi - lo, mi);
-               if (!cmp)
-                       return nth_packed_object_offset(p, mi);
-               if (cmp > 0)
-                       hi = mi;
-               else
-                       lo = mi+1;
-       }
+       /* Make sure we have the terminating NUL */
+       if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
+               return -1;
        return 0;
 }
 
-int is_pack_valid(struct packed_git *p)
+static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
+                                       unsigned long mapsize, void *buffer,
+                                       unsigned long bufsiz, struct strbuf *header)
 {
-       /* An already open pack is known to be valid. */
-       if (p->pack_fd != -1)
-               return 1;
+       int status;
+
+       status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
+       if (status < Z_OK)
+               return -1;
 
-       /* If the pack has one window completely covering the
-        * file size, the pack is known to be valid even if
-        * the descriptor is not currently open.
+       /*
+        * Check if entire header is unpacked in the first iteration.
         */
-       if (p->windows) {
-               struct pack_window *w = p->windows;
+       if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
+               return 0;
 
-               if (!w->offset && w->len == p->pack_size)
-                       return 1;
-       }
+       /*
+        * buffer[0..bufsiz] was not large enough.  Copy the partial
+        * result out to header, and then append the result of further
+        * reading the stream.
+        */
+       strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
+       stream->next_out = buffer;
+       stream->avail_out = bufsiz;
 
-       /* Force the pack to open to prove its valid. */
-       return !open_packed_git(p);
+       do {
+               status = git_inflate(stream, 0);
+               strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
+               if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
+                       return 0;
+               stream->next_out = buffer;
+               stream->avail_out = bufsiz;
+       } while (status != Z_STREAM_END);
+       return -1;
 }
 
-static int fill_pack_entry(const unsigned char *sha1,
-                          struct pack_entry *e,
-                          struct packed_git *p)
+static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
 {
-       off_t offset;
+       int bytes = strlen(buffer) + 1;
+       unsigned char *buf = xmallocz(size);
+       unsigned long n;
+       int status = Z_OK;
 
-       if (p->num_bad_objects) {
-               unsigned i;
-               for (i = 0; i < p->num_bad_objects; i++)
-                       if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
-                               return 0;
+       n = stream->total_out - bytes;
+       if (n > size)
+               n = size;
+       memcpy(buf, (char *) buffer + bytes, n);
+       bytes = n;
+       if (bytes <= size) {
+               /*
+                * The above condition must be (bytes <= size), not
+                * (bytes < size).  In other words, even though we
+                * expect no more output and set avail_out to zero,
+                * the input zlib stream may have bytes that express
+                * "this concludes the stream", and we *do* want to
+                * eat that input.
+                *
+                * Otherwise we would not be able to test that we
+                * consumed all the input to reach the expected size;
+                * we also want to check that zlib tells us that all
+                * went well with status == Z_STREAM_END at the end.
+                */
+               stream->next_out = buf + bytes;
+               stream->avail_out = size - bytes;
+               while (status == Z_OK)
+                       status = git_inflate(stream, Z_FINISH);
+       }
+       if (status == Z_STREAM_END && !stream->avail_in) {
+               git_inflate_end(stream);
+               return buf;
        }
 
-       offset = find_pack_entry_one(sha1, p);
-       if (!offset)
-               return 0;
-
-       /*
-        * We are about to tell the caller where they can locate the
-        * requested object.  We better make sure the packfile is
-        * still here and can be accessed before supplying that
-        * answer, as it may have been deleted since the index was
-        * loaded!
-        */
-       if (!is_pack_valid(p))
-               return 0;
-       e->offset = offset;
-       e->p = p;
-       hashcpy(e->sha1, sha1);
-       return 1;
+       if (status < 0)
+               error("corrupt loose object '%s'", sha1_to_hex(sha1));
+       else if (stream->avail_in)
+               error("garbage at end of loose object '%s'",
+                     sha1_to_hex(sha1));
+       free(buf);
+       return NULL;
 }
 
 /*
- * Iff a pack file contains the object named by sha1, return true and
- * store its location to e.
+ * We used to just use "sscanf()", but that's actually way
+ * too permissive for what we want to check. So do an anal
+ * object header parse by hand.
  */
-static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
+static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
+                              unsigned int flags)
 {
-       struct mru_entry *p;
+       const char *type_buf = hdr;
+       unsigned long size;
+       int type, type_len = 0;
 
-       prepare_packed_git();
-       if (!packed_git)
-               return 0;
+       /*
+        * The type can be of any size but is followed by
+        * a space.
+        */
+       for (;;) {
+               char c = *hdr++;
+               if (!c)
+                       return -1;
+               if (c == ' ')
+                       break;
+               type_len++;
+       }
 
-       for (p = packed_git_mru->head; p; p = p->next) {
-               if (fill_pack_entry(sha1, e, p->item)) {
-                       mru_mark(packed_git_mru, p);
-                       return 1;
+       type = type_from_string_gently(type_buf, type_len, 1);
+       if (oi->typename)
+               strbuf_add(oi->typename, type_buf, type_len);
+       /*
+        * Set type to 0 if its an unknown object and
+        * we're obtaining the type using '--allow-unknown-type'
+        * option.
+        */
+       if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0))
+               type = 0;
+       else if (type < 0)
+               die("invalid object type");
+       if (oi->typep)
+               *oi->typep = type;
+
+       /*
+        * The length must follow immediately, and be in canonical
+        * decimal format (ie "010" is not valid).
+        */
+       size = *hdr++ - '0';
+       if (size > 9)
+               return -1;
+       if (size) {
+               for (;;) {
+                       unsigned long c = *hdr - '0';
+                       if (c > 9)
+                               break;
+                       hdr++;
+                       size = size * 10 + c;
                }
        }
-       return 0;
+
+       if (oi->sizep)
+               *oi->sizep = size;
+
+       /*
+        * The length must be followed by a zero byte
+        */
+       return *hdr ? -1 : type;
 }
 
-struct packed_git *find_sha1_pack(const unsigned char *sha1,
-                                 struct packed_git *packs)
+int parse_sha1_header(const char *hdr, unsigned long *sizep)
 {
-       struct packed_git *p;
-
-       for (p = packs; p; p = p->next) {
-               if (find_pack_entry_one(sha1, p))
-                       return p;
-       }
-       return NULL;
+       struct object_info oi = OBJECT_INFO_INIT;
 
+       oi.sizep = sizep;
+       return parse_sha1_header_extended(hdr, &oi, 0);
 }
 
 static int sha1_loose_object_info(const unsigned char *sha1,
@@ -2963,10 +1124,14 @@ static int sha1_loose_object_info(const unsigned char *sha1,
        } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
                status = error("unable to parse %s header", sha1_to_hex(sha1));
 
-       if (status >= 0 && oi->contentp)
+       if (status >= 0 && oi->contentp) {
                *oi->contentp = unpack_sha1_rest(&stream, hdr,
                                                 *oi->sizep, sha1);
-       else
+               if (!*oi->contentp) {
+                       git_inflate_end(&stream);
+                       status = -1;
+               }
+       } else
                git_inflate_end(&stream);
 
        munmap(map, mapsize);
@@ -2975,6 +1140,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
        if (oi->sizep == &size_scratch)
                oi->sizep = NULL;
        strbuf_release(&hdrbuf);
+       oi->whence = OI_LOOSE;
        return (status < 0) ? status : 0;
 }
 
@@ -3012,10 +1178,8 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
 
        if (!find_pack_entry(real, &e)) {
                /* Most likely it's a loose object. */
-               if (!sha1_loose_object_info(real, oi, flags)) {
-                       oi->whence = OI_LOOSE;
+               if (!sha1_loose_object_info(real, oi, flags))
                        return 0;
-               }
 
                /* Not a loose object; someone else may have just packed it. */
                if (flags & OBJECT_INFO_QUICK) {
@@ -3038,10 +1202,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
        if (rtype < 0) {
                mark_bad_packed_object(e.p, real);
                return sha1_object_info_extended(real, oi, 0);
-       } else if (in_delta_base_cache(e.p, e.offset)) {
-               oi->whence = OI_DBCACHED;
-       } else {
-               oi->whence = OI_PACKED;
+       } else if (oi->whence == OI_PACKED) {
                oi->u.packed.offset = e.offset;
                oi->u.packed.pack = e.p;
                oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
@@ -3065,28 +1226,18 @@ int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
        return type;
 }
 
-static void *read_packed_sha1(const unsigned char *sha1,
-                             enum object_type *type, unsigned long *size)
+static void *read_object(const unsigned char *sha1, enum object_type *type,
+                        unsigned long *size)
 {
-       struct pack_entry e;
-       void *data;
+       struct object_info oi = OBJECT_INFO_INIT;
+       void *content;
+       oi.typep = type;
+       oi.sizep = size;
+       oi.contentp = &content;
 
-       if (!find_pack_entry(sha1, &e))
+       if (sha1_object_info_extended(sha1, &oi, 0) < 0)
                return NULL;
-       data = cache_or_unpack_entry(e.p, e.offset, size, type);
-       if (!data) {
-               /*
-                * We're probably in deep shit, but let's try to fetch
-                * the required object anyway from another pack or loose.
-                * This should happen only in the presence of a corrupted
-                * pack, and is better than failing outright.
-                */
-               error("failed to read object %s at offset %"PRIuMAX" from %s",
-                     sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name);
-               mark_bad_packed_object(e.p, sha1);
-               data = read_object(sha1, type, size);
-       }
-       return data;
+       return content;
 }
 
 int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
@@ -3107,20 +1258,6 @@ int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
        return 0;
 }
 
-static void *read_object(const unsigned char *sha1, enum object_type *type,
-                        unsigned long *size)
-{
-       struct object_info oi = OBJECT_INFO_INIT;
-       void *content;
-       oi.typep = type;
-       oi.sizep = size;
-       oi.contentp = &content;
-
-       if (sha1_object_info_extended(sha1, &oi, 0) < 0)
-               return NULL;
-       return content;
-}
-
 /*
  * This function dies on corrupt objects; the callers who want to
  * deal with them should arrange to call read_object() and give error
@@ -3439,7 +1576,7 @@ int write_sha1_file(const void *buf, unsigned long len, const char *type, unsign
 }
 
 int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type,
-                            unsigned char *sha1, unsigned flags)
+                            struct object_id *oid, unsigned flags)
 {
        char *header;
        int hdrlen, status = 0;
@@ -3447,13 +1584,13 @@ int hash_sha1_file_literally(const void *buf, unsigned long len, const char *typ
        /* type string, SP, %lu of the length plus NUL must fit this */
        hdrlen = strlen(type) + 32;
        header = xmalloc(hdrlen);
-       write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
+       write_sha1_file_prepare(buf, len, type, oid->hash, header, &hdrlen);
 
        if (!(flags & HASH_WRITE_OBJECT))
                goto cleanup;
-       if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
+       if (freshen_packed_object(oid->hash) || freshen_loose_object(oid->hash))
                goto cleanup;
-       status = write_loose_object(sha1, header, hdrlen, buf, len, 0);
+       status = write_loose_object(oid->hash, header, hdrlen, buf, len, 0);
 
 cleanup:
        free(header);
@@ -3471,7 +1608,7 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)
 
        if (has_loose_object(sha1))
                return 0;
-       buf = read_packed_sha1(sha1, &type, &len);
+       buf = read_object(sha1, &type, &len);
        if (!buf)
                return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
        hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
@@ -3481,20 +1618,6 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)
        return ret;
 }
 
-int has_pack_index(const unsigned char *sha1)
-{
-       struct stat st;
-       if (stat(sha1_pack_index_name(sha1), &st))
-               return 0;
-       return 1;
-}
-
-int has_sha1_pack(const unsigned char *sha1)
-{
-       struct pack_entry e;
-       return find_pack_entry(sha1, &e);
-}
-
 int has_sha1_file_with_flags(const unsigned char *sha1, int flags)
 {
        if (!startup_info->have_repository)
@@ -3629,10 +1752,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);
@@ -3657,14 +1785,14 @@ static int index_core(unsigned char *sha1, int fd, size_t size,
  * binary blobs, they generally do not want to get any conversion, and
  * callers should avoid this code path when filters are requested.
  */
-static int index_stream(unsigned char *sha1, int fd, size_t size,
+static int index_stream(struct object_id *oid, int fd, size_t size,
                        enum object_type type, const char *path,
                        unsigned flags)
 {
-       return index_bulk_checkin(sha1, fd, size, type, path, flags);
+       return index_bulk_checkin(oid->hash, fd, size, type, path, flags);
 }
 
-int index_fd(unsigned char *sha1, int fd, struct stat *st,
+int index_fd(struct object_id *oid, int fd, struct stat *st,
             enum object_type type, const char *path, unsigned flags)
 {
        int ret;
@@ -3674,31 +1802,32 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st,
         * die() for large files.
         */
        if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
-               ret = index_stream_convert_blob(sha1, fd, path, flags);
+               ret = index_stream_convert_blob(oid->hash, fd, path, flags);
        else if (!S_ISREG(st->st_mode))
-               ret = index_pipe(sha1, fd, type, path, flags);
+               ret = index_pipe(oid->hash, fd, type, path, flags);
        else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
                 (path && would_convert_to_git(&the_index, path)))
-               ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
+               ret = index_core(oid->hash, fd, xsize_t(st->st_size), type, path,
                                 flags);
        else
-               ret = index_stream(sha1, fd, xsize_t(st->st_size), type, path,
+               ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
                                   flags);
        close(fd);
        return ret;
 }
 
-int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags)
+int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags)
 {
        int fd;
        struct strbuf sb = STRBUF_INIT;
+       int rc = 0;
 
        switch (st->st_mode & S_IFMT) {
        case S_IFREG:
                fd = open(path, O_RDONLY);
                if (fd < 0)
                        return error_errno("open(\"%s\")", path);
-               if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)
+               if (index_fd(oid, fd, st, OBJ_BLOB, path, flags) < 0)
                        return error("%s: failed to insert into database",
                                     path);
                break;
@@ -3706,23 +1835,22 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned
                if (strbuf_readlink(&sb, path, st->st_size))
                        return error_errno("readlink(\"%s\")", path);
                if (!(flags & HASH_WRITE_OBJECT))
-                       hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
-               else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
-                       return error("%s: failed to insert into database",
-                                    path);
+                       hash_sha1_file(sb.buf, sb.len, blob_type, oid->hash);
+               else if (write_sha1_file(sb.buf, sb.len, blob_type, oid->hash))
+                       rc = error("%s: failed to insert into database", path);
                strbuf_release(&sb);
                break;
        case S_IFDIR:
-               return resolve_gitlink_ref(path, "HEAD", sha1);
+               return resolve_gitlink_ref(path, "HEAD", oid->hash);
        default:
                return error("%s: unsupported file type", path);
        }
-       return 0;
+       return rc;
 }
 
 int read_pack_header(int fd, struct pack_header *header)
 {
-       if (read_in_full(fd, header, sizeof(*header)) < sizeof(*header))
+       if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
                /* "eof before pack header was fully read" */
                return PH_ERROR_EOF;
 
@@ -3887,46 +2015,6 @@ int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags)
        return foreach_alt_odb(loose_from_alt_odb, &alt);
 }
 
-static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
-{
-       uint32_t i;
-       int r = 0;
-
-       for (i = 0; i < p->num_objects; i++) {
-               struct object_id oid;
-
-               if (!nth_packed_object_oid(&oid, p, i))
-                       return error("unable to get sha1 of object %u in %s",
-                                    i, p->pack_name);
-
-               r = cb(&oid, p, i, data);
-               if (r)
-                       break;
-       }
-       return r;
-}
-
-int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
-{
-       struct packed_git *p;
-       int r = 0;
-       int pack_errors = 0;
-
-       prepare_packed_git();
-       for (p = packed_git; p; p = p->next) {
-               if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
-                       continue;
-               if (open_pack_index(p)) {
-                       pack_errors = 1;
-                       continue;
-               }
-               r = for_each_object_in_pack(p, cb, data);
-               if (r)
-                       break;
-       }
-       return r ? r : pack_errors;
-}
-
 static int check_stream_sha1(git_zstream *stream,
                             const char *hdr,
                             unsigned long size,
index 74fcb6d788f4cb591a9f7d3c6a2692f5c6d84022..134ac9742f9eed3dc6e587875bdb9aa77b7a8307 100644 (file)
@@ -9,8 +9,9 @@
 #include "remote.h"
 #include "dir.h"
 #include "sha1-array.h"
+#include "packfile.h"
 
-static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
+static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
 
 typedef int (*disambiguate_hint_fn)(const struct object_id *, void *);
 
@@ -200,7 +201,7 @@ static void find_short_packed_object(struct disambiguate_state *ds)
 #define SHORT_NAME_AMBIGUOUS (-2)
 
 static int finish_object_disambiguation(struct disambiguate_state *ds,
-                                       unsigned char *sha1)
+                                       struct object_id *oid)
 {
        if (ds->ambiguous)
                return SHORT_NAME_AMBIGUOUS;
@@ -229,7 +230,7 @@ static int finish_object_disambiguation(struct disambiguate_state *ds,
        if (!ds->candidate_ok)
                return SHORT_NAME_AMBIGUOUS;
 
-       hashcpy(sha1, ds->candidate.hash);
+       oidcpy(oid, &ds->candidate);
        return 0;
 }
 
@@ -385,35 +386,35 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
        return 0;
 }
 
-static int get_short_sha1(const char *name, int len, unsigned char *sha1,
+static int get_short_oid(const char *name, int len, struct object_id *oid,
                          unsigned flags)
 {
        int status;
        struct disambiguate_state ds;
-       int quietly = !!(flags & GET_SHA1_QUIETLY);
+       int quietly = !!(flags & GET_OID_QUIETLY);
 
        if (init_object_disambiguation(name, len, &ds) < 0)
                return -1;
 
-       if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS))
-               die("BUG: multiple get_short_sha1 disambiguator flags");
+       if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
+               die("BUG: multiple get_short_oid disambiguator flags");
 
-       if (flags & GET_SHA1_COMMIT)
+       if (flags & GET_OID_COMMIT)
                ds.fn = disambiguate_commit_only;
-       else if (flags & GET_SHA1_COMMITTISH)
+       else if (flags & GET_OID_COMMITTISH)
                ds.fn = disambiguate_committish_only;
-       else if (flags & GET_SHA1_TREE)
+       else if (flags & GET_OID_TREE)
                ds.fn = disambiguate_tree_only;
-       else if (flags & GET_SHA1_TREEISH)
+       else if (flags & GET_OID_TREEISH)
                ds.fn = disambiguate_treeish_only;
-       else if (flags & GET_SHA1_BLOB)
+       else if (flags & GET_OID_BLOB)
                ds.fn = disambiguate_blob_only;
        else
                ds.fn = default_disambiguate_hint;
 
        find_short_object_filename(&ds);
        find_short_packed_object(&ds);
-       status = finish_object_disambiguation(&ds, sha1);
+       status = finish_object_disambiguation(&ds, oid);
 
        if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
                error(_("short SHA1 %s is ambiguous"), ds.hex_pfx);
@@ -500,12 +501,12 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
        }
 
        sha1_to_hex_r(hex, sha1);
-       if (len == 40 || !len)
-               return 40;
+       if (len == GIT_SHA1_HEXSZ || !len)
+               return GIT_SHA1_HEXSZ;
        exists = has_sha1_file(sha1);
-       while (len < 40) {
-               unsigned char sha1_ret[20];
-               status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
+       while (len < GIT_SHA1_HEXSZ) {
+               struct object_id oid_ret;
+               status = get_short_oid(hex, len, &oid_ret, GET_OID_QUIETLY);
                if (exists
                    ? !status
                    : status == SHORT_NAME_NOT_FOUND) {
@@ -578,10 +579,10 @@ static inline int push_mark(const char *string, int len)
        return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
 }
 
-static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
+static int get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags);
 static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
 
-static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
+static int get_oid_basic(const char *str, int len, struct object_id *oid,
                          unsigned int flags)
 {
        static const char *warn_msg = "refname '%.*s' is ambiguous.";
@@ -595,14 +596,14 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
        "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
        "examine these refs and maybe delete them. Turn this message off by\n"
        "running \"git config advice.objectNameWarning false\"");
-       unsigned char tmp_sha1[20];
+       struct object_id tmp_oid;
        char *real_ref = NULL;
        int refs_found = 0;
        int at, reflog_len, nth_prior = 0;
 
-       if (len == 40 && !get_sha1_hex(str, sha1)) {
+       if (len == GIT_SHA1_HEXSZ && !get_oid_hex(str, oid)) {
                if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
-                       refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
+                       refs_found = dwim_ref(str, len, tmp_oid.hash, &real_ref);
                        if (refs_found > 0) {
                                warning(warn_msg, len, str);
                                if (advice_object_name_warning)
@@ -644,7 +645,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
                int detached;
 
                if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
-                       detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1));
+                       detached = (buf.len == GIT_SHA1_HEXSZ && !get_oid_hex(buf.buf, oid));
                        strbuf_release(&buf);
                        if (detached)
                                return 0;
@@ -653,18 +654,18 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
 
        if (!len && reflog_len)
                /* allow "@{...}" to mean the current branch reflog */
-               refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
+               refs_found = dwim_ref("HEAD", 4, oid->hash, &real_ref);
        else if (reflog_len)
-               refs_found = dwim_log(str, len, sha1, &real_ref);
+               refs_found = dwim_log(str, len, oid->hash, &real_ref);
        else
-               refs_found = dwim_ref(str, len, sha1, &real_ref);
+               refs_found = dwim_ref(str, len, oid->hash, &real_ref);
 
        if (!refs_found)
                return -1;
 
-       if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&
+       if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
            (refs_found > 1 ||
-            !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
+            !get_short_oid(str, len, &tmp_oid, GET_OID_QUIETLY)))
                warning(warn_msg, len, str);
 
        if (reflog_len) {
@@ -696,7 +697,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
                                return -1;
                        }
                }
-               if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL,
+               if (read_ref_at(real_ref, flags, at_time, nth, oid->hash, NULL,
                                &co_time, &co_tz, &co_cnt)) {
                        if (!len) {
                                if (starts_with(real_ref, "refs/heads/")) {
@@ -709,13 +710,13 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
                                }
                        }
                        if (at_time) {
-                               if (!(flags & GET_SHA1_QUIETLY)) {
+                               if (!(flags & GET_OID_QUIETLY)) {
                                        warning("Log for '%.*s' only goes "
                                                "back to %s.", len, str,
                                                show_date(co_time, co_tz, DATE_MODE(RFC2822)));
                                }
                        } else {
-                               if (flags & GET_SHA1_QUIETLY) {
+                               if (flags & GET_OID_QUIETLY) {
                                        exit(128);
                                }
                                die("Log for '%.*s' only has %d entries.",
@@ -729,10 +730,10 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
 }
 
 static int get_parent(const char *name, int len,
-                     unsigned char *result, int idx)
+                     struct object_id *result, int idx)
 {
        struct object_id oid;
-       int ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
+       int ret = get_oid_1(name, len, &oid, GET_OID_COMMITTISH);
        struct commit *commit;
        struct commit_list *p;
 
@@ -742,13 +743,13 @@ static int get_parent(const char *name, int len,
        if (parse_commit(commit))
                return -1;
        if (!idx) {
-               hashcpy(result, commit->object.oid.hash);
+               oidcpy(result, &commit->object.oid);
                return 0;
        }
        p = commit->parents;
        while (p) {
                if (!--idx) {
-                       hashcpy(result, p->item->object.oid.hash);
+                       oidcpy(result, &p->item->object.oid);
                        return 0;
                }
                p = p->next;
@@ -757,13 +758,13 @@ static int get_parent(const char *name, int len,
 }
 
 static int get_nth_ancestor(const char *name, int len,
-                           unsigned char *result, int generation)
+                           struct object_id *result, int generation)
 {
        struct object_id oid;
        struct commit *commit;
        int ret;
 
-       ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
+       ret = get_oid_1(name, len, &oid, GET_OID_COMMITTISH);
        if (ret)
                return ret;
        commit = lookup_commit_reference(&oid);
@@ -775,7 +776,7 @@ static int get_nth_ancestor(const char *name, int len,
                        return -1;
                commit = commit->parents->item;
        }
-       hashcpy(result, commit->object.oid.hash);
+       oidcpy(result, &commit->object.oid);
        return 0;
 }
 
@@ -804,7 +805,7 @@ struct object *peel_to_type(const char *name, int namelen,
        }
 }
 
-static int peel_onion(const char *name, int len, unsigned char *sha1,
+static int peel_onion(const char *name, int len, struct object_id *oid,
                      unsigned lookup_flags)
 {
        struct object_id outer;
@@ -849,13 +850,13 @@ static int peel_onion(const char *name, int len, unsigned char *sha1,
        else
                return -1;
 
-       lookup_flags &= ~GET_SHA1_DISAMBIGUATORS;
+       lookup_flags &= ~GET_OID_DISAMBIGUATORS;
        if (expected_type == OBJ_COMMIT)
-               lookup_flags |= GET_SHA1_COMMITTISH;
+               lookup_flags |= GET_OID_COMMITTISH;
        else if (expected_type == OBJ_TREE)
-               lookup_flags |= GET_SHA1_TREEISH;
+               lookup_flags |= GET_OID_TREEISH;
 
-       if (get_sha1_1(name, sp - name - 2, outer.hash, lookup_flags))
+       if (get_oid_1(name, sp - name - 2, &outer, lookup_flags))
                return -1;
 
        o = parse_object(&outer);
@@ -865,7 +866,7 @@ static int peel_onion(const char *name, int len, unsigned char *sha1,
                o = deref_tag(o, name, sp - name - 2);
                if (!o || (!o->parsed && !parse_object(&o->oid)))
                        return -1;
-               hashcpy(sha1, o->oid.hash);
+               oidcpy(oid, &o->oid);
                return 0;
        }
 
@@ -878,7 +879,7 @@ static int peel_onion(const char *name, int len, unsigned char *sha1,
        if (!o)
                return -1;
 
-       hashcpy(sha1, o->oid.hash);
+       oidcpy(oid, &o->oid);
        if (sp[0] == '/') {
                /* "$commit^{/foo}" */
                char *prefix;
@@ -894,17 +895,17 @@ static int peel_onion(const char *name, int len, unsigned char *sha1,
 
                prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
                commit_list_insert((struct commit *)o, &list);
-               ret = get_sha1_oneline(prefix, sha1, list);
+               ret = get_oid_oneline(prefix, oid, list);
                free(prefix);
                return ret;
        }
        return 0;
 }
 
-static int get_describe_name(const char *name, int len, unsigned char *sha1)
+static int get_describe_name(const char *name, int len, struct object_id *oid)
 {
        const char *cp;
-       unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT;
+       unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
 
        for (cp = name + len - 1; name + 2 <= cp; cp--) {
                char ch = *cp;
@@ -915,14 +916,14 @@ static int get_describe_name(const char *name, int len, unsigned char *sha1)
                        if (ch == 'g' && cp[-1] == '-') {
                                cp++;
                                len -= cp - name;
-                               return get_short_sha1(cp, len, sha1, flags);
+                               return get_short_oid(cp, len, oid, flags);
                        }
                }
        }
        return -1;
 }
 
-static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags)
+static int get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags)
 {
        int ret, has_suffix;
        const char *cp;
@@ -949,25 +950,25 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned l
                if (!num && len1 == len - 1)
                        num = 1;
                if (has_suffix == '^')
-                       return get_parent(name, len1, sha1, num);
+                       return get_parent(name, len1, oid, num);
                /* else if (has_suffix == '~') -- goes without saying */
-               return get_nth_ancestor(name, len1, sha1, num);
+               return get_nth_ancestor(name, len1, oid, num);
        }
 
-       ret = peel_onion(name, len, sha1, lookup_flags);
+       ret = peel_onion(name, len, oid, lookup_flags);
        if (!ret)
                return 0;
 
-       ret = get_sha1_basic(name, len, sha1, lookup_flags);
+       ret = get_oid_basic(name, len, oid, lookup_flags);
        if (!ret)
                return 0;
 
        /* It could be describe output that is "SOMETHING-gXXXX" */
-       ret = get_describe_name(name, len, sha1);
+       ret = get_describe_name(name, len, oid);
        if (!ret)
                return 0;
 
-       return get_short_sha1(name, len, sha1, lookup_flags);
+       return get_short_oid(name, len, oid, lookup_flags);
 }
 
 /*
@@ -1004,7 +1005,7 @@ static int handle_one_ref(const char *path, const struct object_id *oid,
        return 0;
 }
 
-static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
+static int get_oid_oneline(const char *prefix, struct object_id *oid,
                            struct commit_list *list)
 {
        struct commit_list *backup = NULL, *l;
@@ -1044,7 +1045,7 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
                unuse_commit_buffer(commit, buf);
 
                if (matches) {
-                       hashcpy(sha1, commit->object.oid.hash);
+                       oidcpy(oid, &commit->object.oid);
                        found = 1;
                        break;
                }
@@ -1140,7 +1141,7 @@ int get_oid_mb(const char *name, struct object_id *oid)
                struct strbuf sb;
                strbuf_init(&sb, dots - name);
                strbuf_add(&sb, name, dots - name);
-               st = get_sha1_committish(sb.buf, oid_tmp.hash);
+               st = get_oid_committish(sb.buf, &oid_tmp);
                strbuf_release(&sb);
        }
        if (st)
@@ -1149,7 +1150,7 @@ int get_oid_mb(const char *name, struct object_id *oid)
        if (!one)
                return -1;
 
-       if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash))
+       if (get_oid_committish(dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
                return -1;
        two = lookup_commit_reference_gently(&oid_tmp, 0);
        if (!two)
@@ -1338,21 +1339,13 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
 }
 
 /*
- * This is like "get_sha1_basic()", except it allows "sha1 expressions",
+ * This is like "get_oid_basic()", except it allows "object ID expressions",
  * notably "xyz^" for "parent of xyz"
  */
-int get_sha1(const char *name, unsigned char *sha1)
-{
-       struct object_context unused;
-       return get_sha1_with_context(name, 0, sha1, &unused);
-}
-
-/*
- * This is like "get_sha1()", but for struct object_id.
- */
 int get_oid(const char *name, struct object_id *oid)
 {
-       return get_sha1(name, oid->hash);
+       struct object_context unused;
+       return get_oid_with_context(name, 0, oid, &unused);
 }
 
 
@@ -1366,49 +1359,49 @@ int get_oid(const char *name, struct object_id *oid)
  * commit-ish. It is merely to give a hint to the disambiguation
  * machinery.
  */
-int get_sha1_committish(const char *name, unsigned char *sha1)
+int get_oid_committish(const char *name, struct object_id *oid)
 {
        struct object_context unused;
-       return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
-                                    sha1, &unused);
+       return get_oid_with_context(name, GET_OID_COMMITTISH,
+                                   oid, &unused);
 }
 
-int get_sha1_treeish(const char *name, unsigned char *sha1)
+int get_oid_treeish(const char *name, struct object_id *oid)
 {
        struct object_context unused;
-       return get_sha1_with_context(name, GET_SHA1_TREEISH,
-                                    sha1, &unused);
+       return get_oid_with_context(name, GET_OID_TREEISH,
+                                   oid, &unused);
 }
 
-int get_sha1_commit(const char *name, unsigned char *sha1)
+int get_oid_commit(const char *name, struct object_id *oid)
 {
        struct object_context unused;
-       return get_sha1_with_context(name, GET_SHA1_COMMIT,
-                                    sha1, &unused);
+       return get_oid_with_context(name, GET_OID_COMMIT,
+                                   oid, &unused);
 }
 
-int get_sha1_tree(const char *name, unsigned char *sha1)
+int get_oid_tree(const char *name, struct object_id *oid)
 {
        struct object_context unused;
-       return get_sha1_with_context(name, GET_SHA1_TREE,
-                                    sha1, &unused);
+       return get_oid_with_context(name, GET_OID_TREE,
+                                   oid, &unused);
 }
 
-int get_sha1_blob(const char *name, unsigned char *sha1)
+int get_oid_blob(const char *name, struct object_id *oid)
 {
        struct object_context unused;
-       return get_sha1_with_context(name, GET_SHA1_BLOB,
-                                    sha1, &unused);
+       return get_oid_with_context(name, GET_OID_BLOB,
+                                   oid, &unused);
 }
 
 /* Must be called only when object_name:filename doesn't exist. */
-static void diagnose_invalid_sha1_path(const char *prefix,
-                                      const char *filename,
-                                      const unsigned char *tree_sha1,
-                                      const char *object_name,
-                                      int object_name_len)
+static void diagnose_invalid_oid_path(const char *prefix,
+                                     const char *filename,
+                                     const struct object_id *tree_oid,
+                                     const char *object_name,
+                                     int object_name_len)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
        unsigned mode;
 
        if (!prefix)
@@ -1420,8 +1413,8 @@ static void diagnose_invalid_sha1_path(const char *prefix,
        if (is_missing_file_error(errno)) {
                char *fullname = xstrfmt("%s%s", prefix, filename);
 
-               if (!get_tree_entry(tree_sha1, fullname,
-                                   sha1, &mode)) {
+               if (!get_tree_entry(tree_oid->hash, fullname,
+                                   oid.hash, &mode)) {
                        die("Path '%s' exists, but not '%s'.\n"
                            "Did you mean '%.*s:%s' aka '%.*s:./%s'?",
                            fullname,
@@ -1504,24 +1497,24 @@ static char *resolve_relative_path(const char *rel)
                           rel);
 }
 
-static int get_sha1_with_context_1(const char *name,
-                                  unsigned flags,
-                                  const char *prefix,
-                                  unsigned char *sha1,
-                                  struct object_context *oc)
+static int get_oid_with_context_1(const char *name,
+                                 unsigned flags,
+                                 const char *prefix,
+                                 struct object_id *oid,
+                                 struct object_context *oc)
 {
        int ret, bracket_depth;
        int namelen = strlen(name);
        const char *cp;
-       int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
+       int only_to_die = flags & GET_OID_ONLY_TO_DIE;
 
        if (only_to_die)
-               flags |= GET_SHA1_QUIETLY;
+               flags |= GET_OID_QUIETLY;
 
        memset(oc, 0, sizeof(*oc));
        oc->mode = S_IFINVALID;
        strbuf_init(&oc->symlink_path, 0);
-       ret = get_sha1_1(name, namelen, sha1, flags);
+       ret = get_oid_1(name, namelen, oid, flags);
        if (!ret)
                return ret;
        /*
@@ -1541,7 +1534,7 @@ static int get_sha1_with_context_1(const char *name,
 
                        for_each_ref(handle_one_ref, &list);
                        commit_list_sort_by_date(&list);
-                       return get_sha1_oneline(name + 2, sha1, list);
+                       return get_oid_oneline(name + 2, oid, list);
                }
                if (namelen < 3 ||
                    name[2] != ':' ||
@@ -1559,7 +1552,7 @@ static int get_sha1_with_context_1(const char *name,
                        namelen = strlen(cp);
                }
 
-               if (flags & GET_SHA1_RECORD_PATH)
+               if (flags & GET_OID_RECORD_PATH)
                        oc->path = xstrdup(cp);
 
                if (!active_cache)
@@ -1573,7 +1566,7 @@ static int get_sha1_with_context_1(const char *name,
                            memcmp(ce->name, cp, namelen))
                                break;
                        if (ce_stage(ce) == stage) {
-                               hashcpy(sha1, ce->oid.hash);
+                               oidcpy(oid, &ce->oid);
                                oc->mode = ce->ce_mode;
                                free(new_path);
                                return 0;
@@ -1594,36 +1587,36 @@ static int get_sha1_with_context_1(const char *name,
                        break;
        }
        if (*cp == ':') {
-               unsigned char tree_sha1[20];
+               struct object_id tree_oid;
                int len = cp - name;
                unsigned sub_flags = flags;
 
-               sub_flags &= ~GET_SHA1_DISAMBIGUATORS;
-               sub_flags |= GET_SHA1_TREEISH;
+               sub_flags &= ~GET_OID_DISAMBIGUATORS;
+               sub_flags |= GET_OID_TREEISH;
 
-               if (!get_sha1_1(name, len, tree_sha1, sub_flags)) {
+               if (!get_oid_1(name, len, &tree_oid, sub_flags)) {
                        const char *filename = cp+1;
                        char *new_filename = NULL;
 
                        new_filename = resolve_relative_path(filename);
                        if (new_filename)
                                filename = new_filename;
-                       if (flags & GET_SHA1_FOLLOW_SYMLINKS) {
-                               ret = get_tree_entry_follow_symlinks(tree_sha1,
-                                       filename, sha1, &oc->symlink_path,
+                       if (flags & GET_OID_FOLLOW_SYMLINKS) {
+                               ret = get_tree_entry_follow_symlinks(tree_oid.hash,
+                                       filename, oid->hash, &oc->symlink_path,
                                        &oc->mode);
                        } else {
-                               ret = get_tree_entry(tree_sha1, filename,
-                                                    sha1, &oc->mode);
+                               ret = get_tree_entry(tree_oid.hash, filename,
+                                                    oid->hash, &oc->mode);
                                if (ret && only_to_die) {
-                                       diagnose_invalid_sha1_path(prefix,
+                                       diagnose_invalid_oid_path(prefix,
                                                                   filename,
-                                                                  tree_sha1,
+                                                                  &tree_oid,
                                                                   name, len);
                                }
                        }
-                       hashcpy(oc->tree, tree_sha1);
-                       if (flags & GET_SHA1_RECORD_PATH)
+                       hashcpy(oc->tree, tree_oid.hash);
+                       if (flags & GET_OID_RECORD_PATH)
                                oc->path = xstrdup(filename);
 
                        free(new_filename);
@@ -1646,13 +1639,13 @@ static int get_sha1_with_context_1(const char *name,
 void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
 {
        struct object_context oc;
-       unsigned char sha1[20];
-       get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
+       struct object_id oid;
+       get_oid_with_context_1(name, GET_OID_ONLY_TO_DIE, prefix, &oid, &oc);
 }
 
-int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc)
+int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc)
 {
-       if (flags & GET_SHA1_FOLLOW_SYMLINKS && flags & GET_SHA1_ONLY_TO_DIE)
+       if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
                die("BUG: incompatible flags for get_sha1_with_context");
-       return get_sha1_with_context_1(str, flags, NULL, sha1, oc);
+       return get_oid_with_context_1(str, flags, NULL, oid, oc);
 }
index 4d32b4f77e042f0da2b5181f753d0933ad438567..e0cc9d988c70337977853101c397960132af1981 100644 (file)
@@ -1,8 +1,19 @@
+#include "cache.h"
+
+#ifdef DC_SHA1_EXTERNAL
 /*
- * This code is included at the end of sha1dc/sha1.c with the
- * SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C macro.
+ * Same as SHA1DCInit, but with default save_hash=0
  */
+void git_SHA1DCInit(SHA1_CTX *ctx)
+{
+       SHA1DCInit(ctx);
+       SHA1DCSetSafeHash(ctx, 0);
+}
+#endif
 
+/*
+ * Same as SHA1DCFinal, but convert collision attack case into a verbose die().
+ */
 void git_SHA1DCFinal(unsigned char hash[20], SHA1_CTX *ctx)
 {
        if (!SHA1DCFinal(hash, ctx))
@@ -11,6 +22,9 @@ void git_SHA1DCFinal(unsigned char hash[20], SHA1_CTX *ctx)
            sha1_to_hex(hash));
 }
 
+/*
+ * Same as SHA1DCUpdate, but adjust types to match git's usual interface.
+ */
 void git_SHA1DCUpdate(SHA1_CTX *ctx, const void *vdata, unsigned long len)
 {
        const char *data = vdata;
index a8a5c1da169e42360fa39851f27a8c4863d0e9ad..a8c272927842901190b07098911ef785a458bcf3 100644 (file)
@@ -1,19 +1,23 @@
-/*
- * This code is included at the end of sha1dc/sha1.h with the
- * SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H macro.
- */
+/* Plumbing with collition-detecting SHA1 code */
 
-/*
- * Same as SHA1DCFinal, but convert collision attack case into a verbose die().
- */
-void git_SHA1DCFinal(unsigned char [20], SHA1_CTX *);
+#ifdef DC_SHA1_SUBMODULE
+#include "sha1collisiondetection/lib/sha1.h"
+#elif defined(DC_SHA1_EXTERNAL)
+#include <sha1dc/sha1.h>
+#else
+#include "sha1dc/sha1.h"
+#endif
+
+#ifdef DC_SHA1_EXTERNAL
+void git_SHA1DCInit(SHA1_CTX *);
+#else
+#define git_SHA1DCInit SHA1DCInit
+#endif
 
-/*
- * Same as SHA1DCUpdate, but adjust types to match git's usual interface.
- */
+void git_SHA1DCFinal(unsigned char [20], SHA1_CTX *);
 void git_SHA1DCUpdate(SHA1_CTX *ctx, const void *data, unsigned long len);
 
 #define platform_SHA_CTX SHA1_CTX
-#define platform_SHA1_Init SHA1DCInit
+#define platform_SHA1_Init git_SHA1DCInit
 #define platform_SHA1_Update git_SHA1DCUpdate
 #define platform_SHA1_Final git_SHA1DCFinal
index 54359d549075b5cfe1670ca979ff3cf5eeedc9d1..df4d44ea7a34a6d6bef119d1494f1c055614cbb1 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -99,7 +99,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
                                cur_depth = 0;
                        } else {
                                commit = (struct commit *)
-                                       stack.objects[--stack.nr].item;
+                                       object_array_pop(&stack);
                                cur_depth = *(int *)commit->util;
                        }
                }
@@ -107,7 +107,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
                cur_depth++;
                if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
                    (is_repository_shallow() && !commit->parents &&
-                    (graft = lookup_commit_graft(commit->object.oid.hash)) != NULL &&
+                    (graft = lookup_commit_graft(&commit->object.oid)) != NULL &&
                     graft->nr_parent < 0)) {
                        commit_list_insert(commit, &result);
                        commit->object.flags |= shallow_flag;
@@ -286,28 +286,26 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
        return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
 }
 
-static struct tempfile temporary_shallow;
-
 const char *setup_temporary_shallow(const struct oid_array *extra)
 {
+       struct tempfile *temp;
        struct strbuf sb = STRBUF_INIT;
-       int fd;
 
        if (write_shallow_commits(&sb, 0, extra)) {
-               fd = xmks_tempfile(&temporary_shallow, git_path("shallow_XXXXXX"));
+               temp = xmks_tempfile(git_path("shallow_XXXXXX"));
 
-               if (write_in_full(fd, sb.buf, sb.len) != sb.len)
+               if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
+                   close_tempfile_gently(temp) < 0)
                        die_errno("failed to write to %s",
-                                 get_tempfile_path(&temporary_shallow));
-               close_tempfile(&temporary_shallow);
+                                 get_tempfile_path(temp));
                strbuf_release(&sb);
-               return get_tempfile_path(&temporary_shallow);
+               return get_tempfile_path(temp);
        }
        /*
         * is_repository_shallow() sees empty string as "no shallow
         * file".
         */
-       return get_tempfile_path(&temporary_shallow);
+       return "";
 }
 
 void setup_alternate_shallow(struct lock_file *shallow_lock,
@@ -321,7 +319,7 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
                                       LOCK_DIE_ON_ERROR);
        check_shallow_file_for_update();
        if (write_shallow_commits(&sb, 0, extra)) {
-               if (write_in_full(fd, sb.buf, sb.len) != sb.len)
+               if (write_in_full(fd, sb.buf, sb.len) < 0)
                        die_errno("failed to write to %s",
                                  get_lock_file_path(shallow_lock));
                *alternate_shallow_file = get_lock_file_path(shallow_lock);
@@ -368,7 +366,7 @@ void prune_shallow(int show_only)
                                       LOCK_DIE_ON_ERROR);
        check_shallow_file_for_update();
        if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
-               if (write_in_full(fd, sb.buf, sb.len) != sb.len)
+               if (write_in_full(fd, sb.buf, sb.len) < 0)
                        die_errno("failed to write to %s",
                                  get_lock_file_path(&shallow_lock));
                commit_lock_file(&shallow_lock);
@@ -398,7 +396,7 @@ void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
        for (i = 0; i < sa->nr; i++) {
                if (has_object_file(sa->oid + i)) {
                        struct commit_graft *graft;
-                       graft = lookup_commit_graft(sa->oid[i].hash);
+                       graft = lookup_commit_graft(&sa->oid[i]);
                        if (graft && graft->nr_parent < 0)
                                continue;
                        info->ours[info->nr_ours++] = i;
index 89d22e3b0903a220fa958b8d912607828ab2a9ba..323c49ceb35cb053434248df869578e649ccadc0 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -476,6 +476,7 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
        /* Translate slopbuf to NULL, as we cannot call realloc on it */
        if (!sb->alloc)
                sb->buf = NULL;
+       errno = 0;
        r = getdelim(&sb->buf, &sb->alloc, term, fp);
 
        if (r > 0) {
index 2075384e0b2df97a6d113acebcbcdcc853237166..0a74acb2369ac26cb56b05a953171330c3abe652 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -68,7 +68,7 @@ struct strbuf {
 };
 
 extern char strbuf_slopbuf[];
-#define STRBUF_INIT  { 0, 0, strbuf_slopbuf }
+#define STRBUF_INIT  { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
 
 /**
  * Life Cycle Functions
@@ -82,8 +82,12 @@ extern char strbuf_slopbuf[];
 extern void strbuf_init(struct strbuf *, size_t);
 
 /**
- * Release a string buffer and the memory it used. You should not use the
- * string buffer after using this function, unless you initialize it again.
+ * Release a string buffer and the memory it used. After this call, the
+ * strbuf points to an empty string that does not need to be free()ed, as
+ * if it had been set to `STRBUF_INIT` and never modified.
+ *
+ * To clear a strbuf in preparation for further use without the overhead
+ * of free()ing and malloc()ing again, use strbuf_reset() instead.
  */
 extern void strbuf_release(struct strbuf *);
 
@@ -91,6 +95,9 @@ extern void strbuf_release(struct strbuf *);
  * Detach the string from the strbuf and returns it; you now own the
  * storage the string occupies and it is your responsibility from then on
  * to release it with `free(3)` when you are done with it.
+ *
+ * The strbuf that previously held the string is reset to `STRBUF_INIT` so
+ * it can be reused after calling this function.
  */
 extern char *strbuf_detach(struct strbuf *, size_t *);
 
@@ -147,7 +154,10 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len)
        if (len > (sb->alloc ? sb->alloc - 1 : 0))
                die("BUG: strbuf_setlen() beyond buffer");
        sb->len = len;
-       sb->buf[len] = '\0';
+       if (sb->buf != strbuf_slopbuf)
+               sb->buf[len] = '\0';
+       else
+               assert(!strbuf_slopbuf[0]);
 }
 
 /**
index 9afa66b8be6c3b5481e4542c033bfebdb2abbfda..5892b50bd89c3c66bdb541ca0100f0671834a542 100644 (file)
@@ -3,6 +3,7 @@
  */
 #include "cache.h"
 #include "streaming.h"
+#include "packfile.h"
 
 enum input_source {
        stream_error = -1,
@@ -539,7 +540,7 @@ int stream_blob_to_fd(int fd, const struct object_id *oid, struct stream_filter
                        kept = 0;
                wrote = write_in_full(fd, buf, readlen);
 
-               if (wrote != readlen)
+               if (wrote < 0)
                        goto close_and_exit;
        }
        if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
index 29bfb7ae45931686e14c91b1a47a703e7b32fe52..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,33 +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);
-#define for_each_string_list_item(item,list) \
-       for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
+                        string_list_each_func_t func, void *cb_data);
 
-/*
- * 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);
+/** 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)
 
-/*
+/**
  * 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.
@@ -52,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);
@@ -85,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
@@ -100,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 fcc4832c14f3fdbb41bea12142a8fbf167d2aa55..8d2a1707cfe1a7d0fb6b589f0857a6389a8a3d70 100644 (file)
@@ -6,10 +6,13 @@
 #include "pkt-line.h"
 
 int cmd2process_cmp(const void *unused_cmp_data,
-                   const struct subprocess_entry *e1,
-                   const struct subprocess_entry *e2,
+                   const void *entry,
+                   const void *entry_or_key,
                    const void *unused_keydata)
 {
+       const struct subprocess_entry *e1 = entry;
+       const struct subprocess_entry *e2 = entry_or_key;
+
        return strcmp(e1->cmd, e2->cmd);
 }
 
@@ -74,13 +77,12 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
 {
        int err;
        struct child_process *process;
-       const char *argv[] = { cmd, NULL };
 
        entry->cmd = cmd;
        process = &entry->process;
 
        child_process_init(process);
-       process->argv = argv;
+       argv_array_push(&process->args, cmd);
        process->use_shell = 1;
        process->in = -1;
        process->out = -1;
@@ -181,8 +183,8 @@ static int handshake_capabilities(struct child_process *process,
                        if (supported_capabilities)
                                *supported_capabilities |= capabilities[i].flag;
                } else {
-                       warning("subprocess '%s' requested unsupported capability '%s'",
-                               process->argv[0], p);
+                       die("subprocess '%s' requested unsupported capability '%s'",
+                           process->argv[0], p);
                }
        }
 
index caa91a9b92f2256a33d3330b1084a205bef98632..49701998c9bd663073244e64c4edd7e80991a3ef 100644 (file)
@@ -43,8 +43,8 @@ struct subprocess_capability {
 
 /* Function to test two subprocess hashmap entries for equality. */
 extern int cmd2process_cmp(const void *unused_cmp_data,
-                          const struct subprocess_entry *e1,
-                          const struct subprocess_entry *e2,
+                          const void *e1,
+                          const void *e2,
                           const void *unused_keydata);
 
 /*
index 5fe2d0787745de4d97218c738b808667dc8348d3..2aa8a1747f8586839aa3036fbbc59f6c716c6128 100644 (file)
@@ -18,6 +18,7 @@ struct submodule_cache {
        struct hashmap for_path;
        struct hashmap for_name;
        unsigned initialized:1;
+       unsigned gitmodules_read:1;
 };
 
 /*
@@ -35,19 +36,25 @@ enum lookup_type {
 };
 
 static int config_path_cmp(const void *unused_cmp_data,
-                          const struct submodule_entry *a,
-                          const struct submodule_entry *b,
+                          const void *entry,
+                          const void *entry_or_key,
                           const void *unused_keydata)
 {
+       const struct submodule_entry *a = entry;
+       const struct submodule_entry *b = entry_or_key;
+
        return strcmp(a->config->path, b->config->path) ||
               hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
 }
 
 static int config_name_cmp(const void *unused_cmp_data,
-                          const struct submodule_entry *a,
-                          const struct submodule_entry *b,
+                          const void *entry,
+                          const void *entry_or_key,
                           const void *unused_keydata)
 {
+       const struct submodule_entry *a = entry;
+       const struct submodule_entry *b = entry_or_key;
+
        return strcmp(a->config->name, b->config->name) ||
               hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
 }
@@ -59,8 +66,8 @@ static struct submodule_cache *submodule_cache_alloc(void)
 
 static void submodule_cache_init(struct submodule_cache *cache)
 {
-       hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
-       hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
+       hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
+       hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
        cache->initialized = 1;
 }
 
@@ -93,6 +100,7 @@ static void submodule_cache_clear(struct submodule_cache *cache)
        hashmap_free(&cache->for_path, 1);
        hashmap_free(&cache->for_name, 1);
        cache->initialized = 0;
+       cache->gitmodules_read = 0;
 }
 
 void submodule_cache_free(struct submodule_cache *cache)
@@ -232,7 +240,7 @@ static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
 static int parse_fetch_recurse(const char *opt, const char *arg,
                               int die_on_error)
 {
-       switch (git_config_maybe_bool(opt, arg)) {
+       switch (git_parse_maybe_bool(arg)) {
        case 1:
                return RECURSE_SUBMODULES_ON;
        case 0:
@@ -248,6 +256,14 @@ static int parse_fetch_recurse(const char *opt, const char *arg,
        }
 }
 
+int parse_submodule_fetchjobs(const char *var, const char *value)
+{
+       int fetchjobs = git_config_int(var, value);
+       if (fetchjobs < 0)
+               die(_("negative values not allowed for submodule.fetchjobs"));
+       return fetchjobs;
+}
+
 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
 {
        return parse_fetch_recurse(opt, arg, 1);
@@ -277,7 +293,7 @@ int option_fetch_parse_recurse_submodules(const struct option *opt,
 static int parse_update_recurse(const char *opt, const char *arg,
                                int die_on_error)
 {
-       switch (git_config_maybe_bool(opt, arg)) {
+       switch (git_parse_maybe_bool(arg)) {
        case 1:
                return RECURSE_SUBMODULES_ON;
        case 0:
@@ -297,7 +313,7 @@ int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
 static int parse_push_recurse(const char *opt, const char *arg,
                               int die_on_error)
 {
-       switch (git_config_maybe_bool(opt, arg)) {
+       switch (git_parse_maybe_bool(arg)) {
        case 1:
                /* There's no simple "on" value when pushing */
                if (die_on_error)
@@ -441,19 +457,19 @@ static int parse_config(const char *var, const char *value, void *data)
        return ret;
 }
 
-int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
-                                     unsigned char *gitmodules_sha1,
-                                     struct strbuf *rev)
+static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
+                                    struct object_id *gitmodules_oid,
+                                    struct strbuf *rev)
 {
        int ret = 0;
 
-       if (is_null_sha1(treeish_name)) {
-               hashclr(gitmodules_sha1);
+       if (is_null_oid(treeish_name)) {
+               oidclr(gitmodules_oid);
                return 1;
        }
 
-       strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
-       if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
+       strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
+       if (get_oid(rev->buf, gitmodules_oid) >= 0)
                ret = 1;
 
        return ret;
@@ -464,13 +480,13 @@ int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
  * revisions.
  */
 static const struct submodule *config_from(struct submodule_cache *cache,
-               const unsigned char *treeish_name, const char *key,
+               const struct object_id *treeish_name, const char *key,
                enum lookup_type lookup_type)
 {
        struct strbuf rev = STRBUF_INIT;
        unsigned long config_size;
        char *config = NULL;
-       unsigned char sha1[20];
+       struct object_id oid;
        enum object_type type;
        const struct submodule *submodule = NULL;
        struct parse_config_parameter parameter;
@@ -490,28 +506,28 @@ static const struct submodule *config_from(struct submodule_cache *cache,
                return entry->config;
        }
 
-       if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
+       if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
                goto out;
 
        switch (lookup_type) {
        case lookup_name:
-               submodule = cache_lookup_name(cache, sha1, key);
+               submodule = cache_lookup_name(cache, oid.hash, key);
                break;
        case lookup_path:
-               submodule = cache_lookup_path(cache, sha1, key);
+               submodule = cache_lookup_path(cache, oid.hash, key);
                break;
        }
        if (submodule)
                goto out;
 
-       config = read_sha1_file(sha1, &type, &config_size);
+       config = read_sha1_file(oid.hash, &type, &config_size);
        if (!config || type != OBJ_BLOB)
                goto out;
 
        /* fill the submodule config into the cache */
        parameter.cache = cache;
-       parameter.treeish_name = treeish_name;
-       parameter.gitmodules_sha1 = sha1;
+       parameter.treeish_name = treeish_name->hash;
+       parameter.gitmodules_sha1 = oid.hash;
        parameter.overwrite = 0;
        git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
                        config, config_size, &parameter);
@@ -520,9 +536,9 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 
        switch (lookup_type) {
        case lookup_name:
-               return cache_lookup_name(cache, sha1, key);
+               return cache_lookup_name(cache, oid.hash, key);
        case lookup_path:
-               return cache_lookup_path(cache, sha1, key);
+               return cache_lookup_path(cache, oid.hash, key);
        default:
                return NULL;
        }
@@ -544,13 +560,11 @@ static void submodule_cache_check_init(struct repository *repo)
        submodule_cache_init(repo->submodule_cache);
 }
 
-int submodule_config_option(struct repository *repo,
-                           const char *var, const char *value)
+static int gitmodules_cb(const char *var, const char *value, void *data)
 {
+       struct repository *repo = data;
        struct parse_config_parameter parameter;
 
-       submodule_cache_check_init(repo);
-
        parameter.cache = repo->submodule_cache;
        parameter.treeish_name = NULL;
        parameter.gitmodules_sha1 = null_sha1;
@@ -559,30 +573,71 @@ int submodule_config_option(struct repository *repo,
        return parse_config(var, value, &parameter);
 }
 
-int parse_submodule_config_option(const char *var, const char *value)
+void repo_read_gitmodules(struct repository *repo)
 {
-       return submodule_config_option(the_repository, var, value);
+       submodule_cache_check_init(repo);
+
+       if (repo->worktree) {
+               char *gitmodules;
+
+               if (repo_read_index(repo) < 0)
+                       return;
+
+               gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
+
+               if (!is_gitmodules_unmerged(repo->index))
+                       git_config_from_file(gitmodules_cb, gitmodules, repo);
+
+               free(gitmodules);
+       }
+
+       repo->submodule_cache->gitmodules_read = 1;
 }
 
-const struct submodule *submodule_from_name(const unsigned char *treeish_name,
-               const char *name)
+void gitmodules_config_oid(const struct object_id *commit_oid)
 {
+       struct strbuf rev = STRBUF_INIT;
+       struct object_id oid;
+
        submodule_cache_check_init(the_repository);
+
+       if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
+               git_config_from_blob_oid(gitmodules_cb, rev.buf,
+                                        &oid, the_repository);
+       }
+       strbuf_release(&rev);
+
+       the_repository->submodule_cache->gitmodules_read = 1;
+}
+
+static void gitmodules_read_check(struct repository *repo)
+{
+       submodule_cache_check_init(repo);
+
+       /* read the repo's .gitmodules file if it hasn't been already */
+       if (!repo->submodule_cache->gitmodules_read)
+               repo_read_gitmodules(repo);
+}
+
+const struct submodule *submodule_from_name(const struct object_id *treeish_name,
+               const char *name)
+{
+       gitmodules_read_check(the_repository);
        return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
 }
 
-const struct submodule *submodule_from_path(const unsigned char *treeish_name,
+const struct submodule *submodule_from_path(const struct object_id *treeish_name,
                const char *path)
 {
-       submodule_cache_check_init(the_repository);
+       gitmodules_read_check(the_repository);
        return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
 }
 
 const struct submodule *submodule_from_cache(struct repository *repo,
-                                            const unsigned char *treeish_name,
+                                            const struct object_id *treeish_name,
                                             const char *key)
 {
-       submodule_cache_check_init(repo);
+       gitmodules_read_check(repo);
        return config_from(repo->submodule_cache, treeish_name,
                           key, lookup_path);
 }
index 233bfcb7fff0b2dedc64fbb11d167734bf0fad7e..e3845831f6cbd25097ea54b3e2246faa8a32e192 100644 (file)
@@ -27,25 +27,22 @@ struct repository;
 
 extern void submodule_cache_free(struct submodule_cache *cache);
 
+extern int parse_submodule_fetchjobs(const char *var, const char *value);
 extern int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 struct option;
 extern int option_fetch_parse_recurse_submodules(const struct option *opt,
                                                 const char *arg, int unset);
 extern int parse_update_recurse_submodules_arg(const char *opt, const char *arg);
 extern int parse_push_recurse_submodules_arg(const char *opt, const char *arg);
-extern int parse_submodule_config_option(const char *var, const char *value);
-extern int submodule_config_option(struct repository *repo,
-                                  const char *var, const char *value);
+extern void repo_read_gitmodules(struct repository *repo);
+extern void gitmodules_config_oid(const struct object_id *commit_oid);
 extern const struct submodule *submodule_from_name(
-               const unsigned char *commit_or_tree, const char *name);
+               const struct object_id *commit_or_tree, const char *name);
 extern const struct submodule *submodule_from_path(
-               const unsigned char *commit_or_tree, const char *path);
+               const struct object_id *commit_or_tree, const char *path);
 extern const struct submodule *submodule_from_cache(struct repository *repo,
-                                                   const unsigned char *treeish_name,
+                                                   const struct object_id *treeish_name,
                                                    const char *key);
-extern int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
-                                     unsigned char *gitmodules_sha1,
-                                     struct strbuf *rev);
 extern void submodule_free(void);
 
 #endif /* SUBMODULE_CONFIG_H */
index 36f45f5a5a2adedd7e598371f98f6a54880668df..63e7094e1620c2c3eb7dda0a8c38a833ae2d1c5a 100644 (file)
 #include "worktree.h"
 #include "parse-options.h"
 
-static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
-static int parallel_jobs = 1;
 static struct string_list changed_submodule_paths = STRING_LIST_INIT_DUP;
 static int initialized_fetch_ref_tips;
 static struct oid_array ref_tips_before_fetch;
 static struct oid_array ref_tips_after_fetch;
 
 /*
- * The following flag is set if the .gitmodules file is unmerged. We then
- * disable recursion for all submodules where .git/config doesn't have a
- * matching config entry because we can't guess what might be configured in
- * .gitmodules unless the user resolves the conflict. When a command line
- * option is given (which always overrides configuration) this flag will be
- * ignored.
+ * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
+ * will be disabled because we can't guess what might be configured in
+ * .gitmodules unless the user resolves the conflict.
  */
-static int gitmodules_is_unmerged;
+int is_gitmodules_unmerged(const struct index_state *istate)
+{
+       int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
+       if (pos < 0) { /* .gitmodules not found or isn't merged */
+               pos = -1 - pos;
+               if (istate->cache_nr > pos) {  /* there is a .gitmodules */
+                       const struct cache_entry *ce = istate->cache[pos];
+                       if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
+                           !strcmp(ce->name, GITMODULES_FILE))
+                               return 1;
+               }
+       }
+
+       return 0;
+}
 
 /*
- * This flag is set if the .gitmodules file had unstaged modifications on
- * startup. This must be checked before allowing modifications to the
- * .gitmodules file with the intention to stage them later, because when
- * continuing we would stage the modifications the user didn't stage herself
- * too. That might change in a future version when we learn to stage the
- * changes we do ourselves without staging any previous modifications.
+ * Check if the .gitmodules file has unstaged modifications.  This must be
+ * checked before allowing modifications to the .gitmodules file with the
+ * intention to stage them later, because when continuing we would stage the
+ * modifications the user didn't stage herself too. That might change in a
+ * future version when we learn to stage the changes we do ourselves without
+ * staging any previous modifications.
  */
-static int gitmodules_is_modified;
+int is_staging_gitmodules_ok(const struct index_state *istate)
+{
+       int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
 
-int is_staging_gitmodules_ok(void)
+       if ((pos >= 0) && (pos < istate->cache_nr)) {
+               struct stat st;
+               if (lstat(GITMODULES_FILE, &st) == 0 &&
+                   ce_match_stat(istate->cache[pos], &st, 0) & DATA_CHANGED)
+                       return 0;
+       }
+
+       return 1;
+}
+
+static int for_each_remote_ref_submodule(const char *submodule,
+                                        each_ref_fn fn, void *cb_data)
 {
-       return !gitmodules_is_modified;
+       return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
+                                       fn, cb_data);
 }
 
 /*
@@ -63,13 +86,13 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
        struct strbuf entry = STRBUF_INIT;
        const struct submodule *submodule;
 
-       if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
+       if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
                return -1;
 
-       if (gitmodules_is_unmerged)
+       if (is_gitmodules_unmerged(&the_index))
                die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
 
-       submodule = submodule_from_path(null_sha1, oldpath);
+       submodule = submodule_from_path(&null_oid, oldpath);
        if (!submodule || !submodule->name) {
                warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
                return -1;
@@ -77,7 +100,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
        strbuf_addstr(&entry, "submodule.");
        strbuf_addstr(&entry, submodule->name);
        strbuf_addstr(&entry, ".path");
-       if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
+       if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
                /* Maybe the user already did that, don't error out here */
                warning(_("Could not update .gitmodules entry %s"), entry.buf);
                strbuf_release(&entry);
@@ -97,20 +120,20 @@ int remove_path_from_gitmodules(const char *path)
        struct strbuf sect = STRBUF_INIT;
        const struct submodule *submodule;
 
-       if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
+       if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
                return -1;
 
-       if (gitmodules_is_unmerged)
+       if (is_gitmodules_unmerged(&the_index))
                die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
 
-       submodule = submodule_from_path(null_sha1, path);
+       submodule = submodule_from_path(&null_oid, path);
        if (!submodule || !submodule->name) {
                warning(_("Could not find section in .gitmodules where path=%s"), path);
                return -1;
        }
        strbuf_addstr(&sect, "submodule.");
        strbuf_addstr(&sect, submodule->name);
-       if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
+       if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
                /* Maybe the user already did that, don't error out here */
                warning(_("Could not remove .gitmodules entry for %s"), path);
                strbuf_release(&sect);
@@ -122,7 +145,7 @@ int remove_path_from_gitmodules(const char *path)
 
 void stage_updated_gitmodules(void)
 {
-       if (add_file_to_cache(".gitmodules", 0))
+       if (add_file_to_cache(GITMODULES_FILE, 0))
                die(_("staging updated .gitmodules failed"));
 }
 
@@ -147,42 +170,20 @@ static int add_submodule_odb(const char *path)
 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
                                             const char *path)
 {
-       const struct submodule *submodule = submodule_from_path(null_sha1, path);
+       const struct submodule *submodule = submodule_from_path(&null_oid, path);
        if (submodule) {
-               if (submodule->ignore)
-                       handle_ignore_submodules_arg(diffopt, submodule->ignore);
-               else if (gitmodules_is_unmerged)
-                       DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
-       }
-}
+               const char *ignore;
+               char *key;
 
-/* For loading from the .gitmodules file. */
-static int git_modules_config(const char *var, const char *value, void *cb)
-{
-       if (!strcmp(var, "submodule.fetchjobs")) {
-               parallel_jobs = git_config_int(var, value);
-               if (parallel_jobs < 0)
-                       die(_("negative values not allowed for submodule.fetchJobs"));
-               return 0;
-       } else if (starts_with(var, "submodule."))
-               return parse_submodule_config_option(var, value);
-       else if (!strcmp(var, "fetch.recursesubmodules")) {
-               config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
-               return 0;
-       }
-       return 0;
-}
+               key = xstrfmt("submodule.%s.ignore", submodule->name);
+               if (repo_config_get_string_const(the_repository, key, &ignore))
+                       ignore = submodule->ignore;
+               free(key);
 
-/* Loads all submodule settings from the config. */
-int submodule_config(const char *var, const char *value, void *cb)
-{
-       if (!strcmp(var, "submodule.recurse")) {
-               int v = git_config_bool(var, value) ?
-                       RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
-               config_update_recurse_submodules = v;
-               return 0;
-       } else {
-               return git_modules_config(var, value, cb);
+               if (ignore)
+                       handle_ignore_submodules_arg(diffopt, ignore);
+               else if (is_gitmodules_unmerged(&the_index))
+                       DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
        }
 }
 
@@ -214,74 +215,6 @@ int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
        return 0;
 }
 
-void load_submodule_cache(void)
-{
-       if (config_update_recurse_submodules == RECURSE_SUBMODULES_OFF)
-               return;
-
-       gitmodules_config();
-       git_config(submodule_config, NULL);
-}
-
-void gitmodules_config(void)
-{
-       const char *work_tree = get_git_work_tree();
-       if (work_tree) {
-               struct strbuf gitmodules_path = STRBUF_INIT;
-               int pos;
-               strbuf_addstr(&gitmodules_path, work_tree);
-               strbuf_addstr(&gitmodules_path, "/.gitmodules");
-               if (read_cache() < 0)
-                       die("index file corrupt");
-               pos = cache_name_pos(".gitmodules", 11);
-               if (pos < 0) { /* .gitmodules not found or isn't merged */
-                       pos = -1 - pos;
-                       if (active_nr > pos) {  /* there is a .gitmodules */
-                               const struct cache_entry *ce = active_cache[pos];
-                               if (ce_namelen(ce) == 11 &&
-                                   !memcmp(ce->name, ".gitmodules", 11))
-                                       gitmodules_is_unmerged = 1;
-                       }
-               } else if (pos < active_nr) {
-                       struct stat st;
-                       if (lstat(".gitmodules", &st) == 0 &&
-                           ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
-                               gitmodules_is_modified = 1;
-               }
-
-               if (!gitmodules_is_unmerged)
-                       git_config_from_file(git_modules_config,
-                               gitmodules_path.buf, NULL);
-               strbuf_release(&gitmodules_path);
-       }
-}
-
-static int gitmodules_cb(const char *var, const char *value, void *data)
-{
-       struct repository *repo = data;
-       return submodule_config_option(repo, var, value);
-}
-
-void repo_read_gitmodules(struct repository *repo)
-{
-       char *gitmodules_path = repo_worktree_path(repo, ".gitmodules");
-
-       git_config_from_file(gitmodules_cb, gitmodules_path, repo);
-       free(gitmodules_path);
-}
-
-void gitmodules_config_sha1(const unsigned char *commit_sha1)
-{
-       struct strbuf rev = STRBUF_INIT;
-       unsigned char sha1[20];
-
-       if (gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) {
-               git_config_from_blob_sha1(git_modules_config, rev.buf,
-                                         sha1, NULL);
-       }
-       strbuf_release(&rev);
-}
-
 /*
  * Determine if a submodule has been initialized at a given 'path'
  */
@@ -293,7 +226,7 @@ int is_submodule_active(struct repository *repo, const char *path)
        const struct string_list *sl;
        const struct submodule *module;
 
-       module = submodule_from_cache(repo, null_sha1, path);
+       module = submodule_from_cache(repo, &null_oid, path);
 
        /* early return if there isn't a path->module mapping */
        if (!module)
@@ -410,24 +343,38 @@ void die_path_inside_submodule(const struct index_state *istate,
        }
 }
 
-int parse_submodule_update_strategy(const char *value,
-               struct submodule_update_strategy *dst)
+enum submodule_update_type parse_submodule_update_type(const char *value)
 {
-       free((void*)dst->command);
-       dst->command = NULL;
        if (!strcmp(value, "none"))
-               dst->type = SM_UPDATE_NONE;
+               return SM_UPDATE_NONE;
        else if (!strcmp(value, "checkout"))
-               dst->type = SM_UPDATE_CHECKOUT;
+               return SM_UPDATE_CHECKOUT;
        else if (!strcmp(value, "rebase"))
-               dst->type = SM_UPDATE_REBASE;
+               return SM_UPDATE_REBASE;
        else if (!strcmp(value, "merge"))
-               dst->type = SM_UPDATE_MERGE;
-       else if (skip_prefix(value, "!", &value)) {
-               dst->type = SM_UPDATE_COMMAND;
-               dst->command = xstrdup(value);
-       } else
+               return SM_UPDATE_MERGE;
+       else if (*value == '!')
+               return SM_UPDATE_COMMAND;
+       else
+               return SM_UPDATE_UNSPECIFIED;
+}
+
+int parse_submodule_update_strategy(const char *value,
+               struct submodule_update_strategy *dst)
+{
+       enum submodule_update_type type;
+
+       free((void*)dst->command);
+       dst->command = NULL;
+
+       type = parse_submodule_update_type(value);
+       if (type == SM_UPDATE_UNSPECIFIED)
                return -1;
+
+       dst->type = type;
+       if (type == SM_UPDATE_COMMAND)
+               dst->command = xstrdup(value + 1);
+
        return 0;
 }
 
@@ -490,9 +437,7 @@ static int prepare_submodule_summary(struct rev_info *rev, const char *path,
        return prepare_revision_walk(rev);
 }
 
-static void print_submodule_summary(struct rev_info *rev, FILE *f,
-               const char *line_prefix,
-               const char *del, const char *add, const char *reset)
+static void print_submodule_summary(struct rev_info *rev, struct diff_options *o)
 {
        static const char format[] = "  %m %s";
        struct strbuf sb = STRBUF_INIT;
@@ -503,18 +448,12 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f,
                ctx.date_mode = rev->date_mode;
                ctx.output_encoding = get_log_output_encoding();
                strbuf_setlen(&sb, 0);
-               strbuf_addstr(&sb, line_prefix);
-               if (commit->object.flags & SYMMETRIC_LEFT) {
-                       if (del)
-                               strbuf_addstr(&sb, del);
-               }
-               else if (add)
-                       strbuf_addstr(&sb, add);
                format_commit_message(commit, format, &sb, &ctx);
-               if (reset)
-                       strbuf_addstr(&sb, reset);
                strbuf_addch(&sb, '\n');
-               fprintf(f, "%s", sb.buf);
+               if (commit->object.flags & SYMMETRIC_LEFT)
+                       diff_emit_submodule_del(o, sb.buf);
+               else
+                       diff_emit_submodule_add(o, sb.buf);
        }
        strbuf_release(&sb);
 }
@@ -541,11 +480,9 @@ void prepare_submodule_repo_env(struct argv_array *out)
  * attempt to lookup both the left and right commits and put them into the
  * left and right pointers.
  */
-static void show_submodule_header(FILE *f, const char *path,
-               const char *line_prefix,
+static void show_submodule_header(struct diff_options *o, const char *path,
                struct object_id *one, struct object_id *two,
-               unsigned dirty_submodule, const char *meta,
-               const char *reset,
+               unsigned dirty_submodule,
                struct commit **left, struct commit **right,
                struct commit_list **merge_bases)
 {
@@ -554,11 +491,10 @@ static void show_submodule_header(FILE *f, const char *path,
        int fast_forward = 0, fast_backward = 0;
 
        if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
-               fprintf(f, "%sSubmodule %s contains untracked content\n",
-                       line_prefix, path);
+               diff_emit_submodule_untracked(o, path);
+
        if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
-               fprintf(f, "%sSubmodule %s contains modified content\n",
-                       line_prefix, path);
+               diff_emit_submodule_modified(o, path);
 
        if (is_null_oid(one))
                message = "(new submodule)";
@@ -567,7 +503,7 @@ static void show_submodule_header(FILE *f, const char *path,
 
        if (add_submodule_odb(path)) {
                if (!message)
-                       message = "(not initialized)";
+                       message = "(commits not present)";
                goto output_header;
        }
 
@@ -600,31 +536,29 @@ static void show_submodule_header(FILE *f, const char *path,
        }
 
 output_header:
-       strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
+       strbuf_addf(&sb, "Submodule %s ", path);
        strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
        strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
        strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
        if (message)
-               strbuf_addf(&sb, " %s%s\n", message, reset);
+               strbuf_addf(&sb, " %s\n", message);
        else
-               strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
-       fwrite(sb.buf, sb.len, 1, f);
+               strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
+       diff_emit_submodule_header(o, sb.buf);
 
        strbuf_release(&sb);
 }
 
-void show_submodule_summary(FILE *f, const char *path,
-               const char *line_prefix,
+void show_submodule_summary(struct diff_options *o, const char *path,
                struct object_id *one, struct object_id *two,
-               unsigned dirty_submodule, const char *meta,
-               const char *del, const char *add, const char *reset)
+               unsigned dirty_submodule)
 {
        struct rev_info rev;
        struct commit *left = NULL, *right = NULL;
        struct commit_list *merge_bases = NULL;
 
-       show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
-                             meta, reset, &left, &right, &merge_bases);
+       show_submodule_header(o, path, one, two, dirty_submodule,
+                             &left, &right, &merge_bases);
 
        /*
         * If we don't have both a left and a right pointer, there is no
@@ -636,11 +570,11 @@ void show_submodule_summary(FILE *f, const char *path,
 
        /* Treat revision walker failure the same as missing commits */
        if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
-               fprintf(f, "%s(revision walker failed)\n", line_prefix);
+               diff_emit_submodule_error(o, "(revision walker failed)\n");
                goto out;
        }
 
-       print_submodule_summary(&rev, f, line_prefix, del, add, reset);
+       print_submodule_summary(&rev, o);
 
 out:
        if (merge_bases)
@@ -649,21 +583,18 @@ void show_submodule_summary(FILE *f, const char *path,
        clear_commit_marks(right, ~0);
 }
 
-void show_submodule_inline_diff(FILE *f, const char *path,
-               const char *line_prefix,
+void show_submodule_inline_diff(struct diff_options *o, const char *path,
                struct object_id *one, struct object_id *two,
-               unsigned dirty_submodule, const char *meta,
-               const char *del, const char *add, const char *reset,
-               const struct diff_options *o)
+               unsigned dirty_submodule)
 {
        const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
        struct commit *left = NULL, *right = NULL;
        struct commit_list *merge_bases = NULL;
-       struct strbuf submodule_dir = STRBUF_INIT;
        struct child_process cp = CHILD_PROCESS_INIT;
+       struct strbuf sb = STRBUF_INIT;
 
-       show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
-                             meta, reset, &left, &right, &merge_bases);
+       show_submodule_header(o, path, one, two, dirty_submodule,
+                             &left, &right, &merge_bases);
 
        /* We need a valid left and right commit to display a difference */
        if (!(left || is_null_oid(one)) ||
@@ -675,16 +606,16 @@ void show_submodule_inline_diff(FILE *f, const char *path,
        if (right)
                new = two;
 
-       fflush(f);
        cp.git_cmd = 1;
        cp.dir = path;
-       cp.out = dup(fileno(f));
+       cp.out = -1;
        cp.no_stdin = 1;
 
        /* TODO: other options may need to be passed here. */
        argv_array_pushl(&cp.args, "diff", "--submodule=diff", NULL);
+       argv_array_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
+                        "always" : "never");
 
-       argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
        if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
                argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
                                 o->b_prefix, path);
@@ -707,11 +638,17 @@ void show_submodule_inline_diff(FILE *f, const char *path,
                argv_array_push(&cp.args, oid_to_hex(new));
 
        prepare_submodule_repo_env(&cp.env_array);
-       if (run_command(&cp))
-               fprintf(f, "(diff failed)\n");
+       if (start_command(&cp))
+               diff_emit_submodule_error(o, "(diff failed)\n");
+
+       while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
+               diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
+
+       if (finish_command(&cp))
+               diff_emit_submodule_error(o, "(diff failed)\n");
 
 done:
-       strbuf_release(&submodule_dir);
+       strbuf_release(&sb);
        if (merge_bases)
                free_commit_list(merge_bases);
        if (left)
@@ -720,11 +657,6 @@ void show_submodule_inline_diff(FILE *f, const char *path,
                clear_commit_marks(right, ~0);
 }
 
-void set_config_fetch_recurse_submodules(int value)
-{
-       config_fetch_recurse_submodules = value;
-}
-
 int should_update_submodules(void)
 {
        return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
@@ -738,7 +670,7 @@ const struct submodule *submodule_from_ce(const struct cache_entry *ce)
        if (!should_update_submodules())
                return NULL;
 
-       return submodule_from_path(null_sha1, ce->name);
+       return submodule_from_path(&null_oid, ce->name);
 }
 
 static struct oid_array *submodule_commits(struct string_list *submodules,
@@ -842,19 +774,36 @@ static int append_oid_to_argv(const struct object_id *oid, void *data)
        return 0;
 }
 
+struct has_commit_data {
+       int result;
+       const char *path;
+};
+
 static int check_has_commit(const struct object_id *oid, void *data)
 {
-       int *has_commit = data;
+       struct has_commit_data *cb = data;
 
-       if (!lookup_commit_reference(oid))
-               *has_commit = 0;
+       enum object_type type = sha1_object_info(oid->hash, NULL);
 
-       return 0;
+       switch (type) {
+       case OBJ_COMMIT:
+               return 0;
+       case OBJ_BAD:
+               /*
+                * Object is missing or invalid. If invalid, an error message
+                * has already been printed.
+                */
+               cb->result = 0;
+               return 0;
+       default:
+               die(_("submodule entry '%s' (%s) is a %s, not a commit"),
+                   cb->path, oid_to_hex(oid), typename(type));
+       }
 }
 
 static int submodule_has_commits(const char *path, struct oid_array *commits)
 {
-       int has_commit = 1;
+       struct has_commit_data has_commit = { 1, path };
 
        /*
         * Perform a cheap, but incorrect check for the existence of 'commits'.
@@ -870,7 +819,7 @@ static int submodule_has_commits(const char *path, struct oid_array *commits)
 
        oid_array_for_each_unique(commits, check_has_commit, &has_commit);
 
-       if (has_commit) {
+       if (has_commit.result) {
                /*
                 * Even if the submodule is checked out and the commit is
                 * present, make sure it exists in the submodule's object store
@@ -889,12 +838,12 @@ static int submodule_has_commits(const char *path, struct oid_array *commits)
                cp.dir = path;
 
                if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
-                       has_commit = 0;
+                       has_commit.result = 0;
 
                strbuf_release(&out);
        }
 
-       return has_commit;
+       return has_commit.result;
 }
 
 static int submodule_needs_pushing(const char *path, struct oid_array *commits)
@@ -1157,7 +1106,6 @@ int submodule_touches_in_range(struct object_id *excl_oid,
        struct argv_array args = ARGV_ARRAY_INIT;
        int ret;
 
-       gitmodules_config();
        /* No need to check if there are no submodules configured */
        if (!submodule_from_path(NULL, NULL))
                return 0;
@@ -1182,10 +1130,11 @@ struct submodule_parallel_fetch {
        const char *work_tree;
        const char *prefix;
        int command_line_option;
+       int default_option;
        int quiet;
        int result;
 };
-#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
+#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0, 0}
 
 static int get_next_submodule(struct child_process *cp,
                              struct strbuf *err, void *data, void **task_cb)
@@ -1204,29 +1153,36 @@ static int get_next_submodule(struct child_process *cp,
                if (!S_ISGITLINK(ce->ce_mode))
                        continue;
 
-               submodule = submodule_from_path(null_sha1, ce->name);
-               if (!submodule)
-                       submodule = submodule_from_name(null_sha1, ce->name);
+               submodule = submodule_from_path(&null_oid, ce->name);
 
                default_argv = "yes";
                if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
-                       if (submodule &&
-                           submodule->fetch_recurse !=
-                                               RECURSE_SUBMODULES_NONE) {
-                               if (submodule->fetch_recurse ==
-                                               RECURSE_SUBMODULES_OFF)
+                       int fetch_recurse = RECURSE_SUBMODULES_NONE;
+
+                       if (submodule) {
+                               char *key;
+                               const char *value;
+
+                               fetch_recurse = submodule->fetch_recurse;
+                               key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
+                               if (!repo_config_get_string_const(the_repository, key, &value)) {
+                                       fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
+                               }
+                               free(key);
+                       }
+
+                       if (fetch_recurse != RECURSE_SUBMODULES_NONE) {
+                               if (fetch_recurse == RECURSE_SUBMODULES_OFF)
                                        continue;
-                               if (submodule->fetch_recurse ==
-                                               RECURSE_SUBMODULES_ON_DEMAND) {
+                               if (fetch_recurse == RECURSE_SUBMODULES_ON_DEMAND) {
                                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
                                                continue;
                                        default_argv = "on-demand";
                                }
                        } else {
-                               if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
-                                   gitmodules_is_unmerged)
+                               if (spf->default_option == RECURSE_SUBMODULES_OFF)
                                        continue;
-                               if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
+                               if (spf->default_option == RECURSE_SUBMODULES_ON_DEMAND) {
                                        if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
                                                continue;
                                        default_argv = "on-demand";
@@ -1293,6 +1249,7 @@ static int fetch_finish(int retvalue, struct strbuf *err,
 
 int fetch_populated_submodules(const struct argv_array *options,
                               const char *prefix, int command_line_option,
+                              int default_option,
                               int quiet, int max_parallel_jobs)
 {
        int i;
@@ -1300,6 +1257,7 @@ int fetch_populated_submodules(const struct argv_array *options,
 
        spf.work_tree = get_git_work_tree();
        spf.command_line_option = command_line_option;
+       spf.default_option = default_option;
        spf.quiet = quiet;
        spf.prefix = prefix;
 
@@ -1315,9 +1273,6 @@ int fetch_populated_submodules(const struct argv_array *options,
        argv_array_push(&spf.args, "--recurse-submodules-default");
        /* default value, "--submodule-prefix" and its value are added later */
 
-       if (max_parallel_jobs < 0)
-               max_parallel_jobs = parallel_jobs;
-
        calculate_changed_submodule_paths();
        run_processes_parallel(max_parallel_jobs,
                               get_next_submodule,
@@ -1582,7 +1537,7 @@ int submodule_move_head(const char *path,
        if (old && !is_submodule_populated_gently(path, error_code_ptr))
                return 0;
 
-       sub = submodule_from_path(null_sha1, path);
+       sub = submodule_from_path(&null_oid, path);
 
        if (!sub)
                die("BUG: could not get submodule information for '%s'", path);
@@ -1696,6 +1651,8 @@ static int find_first_merges(struct object_array *result, const char *path,
                        oid_to_hex(&a->object.oid));
        init_revisions(&revs, NULL);
        rev_opts.submodule = path;
+       /* FIXME: can't handle linked worktrees in submodules yet */
+       revs.single_worktree = path != NULL;
        setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
 
        /* save all revisions from the above list that contain b */
@@ -1728,7 +1685,7 @@ static int find_first_merges(struct object_array *result, const char *path,
                        add_object_array(merges.objects[i].item, NULL, result);
        }
 
-       free(merges.objects);
+       object_array_clear(&merges);
        return result->nr;
 }
 
@@ -1833,15 +1790,10 @@ int merge_submodule(struct object_id *result, const char *path,
                        print_commit((struct commit *) merges.objects[i].item);
        }
 
-       free(merges.objects);
+       object_array_clear(&merges);
        return 0;
 }
 
-int parallel_submodules(void)
-{
-       return parallel_jobs;
-}
-
 /*
  * Embeds a single submodules git directory into the superprojects git dir,
  * non recursively.
@@ -1864,7 +1816,7 @@ static void relocate_single_git_dir_into_superproject(const char *prefix,
 
        real_old_git_dir = real_pathdup(old_git_dir, 1);
 
-       sub = submodule_from_path(null_sha1, path);
+       sub = submodule_from_path(&null_oid, path);
        if (!sub)
                die(_("could not lookup name for submodule '%s'"), path);
 
@@ -1920,7 +1872,7 @@ void absorb_git_dir_into_superproject(const char *prefix,
                * superproject did not rewrite the git file links yet,
                * fix it now.
                */
-               sub = submodule_from_path(null_sha1, path);
+               sub = submodule_from_path(&null_oid, path);
                if (!sub)
                        die(_("could not lookup name for submodule '%s'"), path);
                connect_work_tree_and_git_dir(path,
@@ -2045,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;
@@ -2062,8 +2018,7 @@ int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
                strbuf_addstr(buf, git_dir);
        }
        if (!is_git_directory(buf->buf)) {
-               gitmodules_config();
-               sub = submodule_from_path(null_sha1, submodule);
+               sub = submodule_from_path(&null_oid, submodule);
                if (!sub) {
                        ret = -1;
                        goto cleanup;
index e85b1448638ac7255a48a1bc1e2d6409c3a85ba6..f0da0277a45b49369d89cebdd4d30f44bef23d3d 100644 (file)
@@ -33,22 +33,18 @@ struct submodule_update_strategy {
 };
 #define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL}
 
-extern int is_staging_gitmodules_ok(void);
+extern int is_gitmodules_unmerged(const struct index_state *istate);
+extern int is_staging_gitmodules_ok(const struct index_state *istate);
 extern int update_path_in_gitmodules(const char *oldpath, const char *newpath);
 extern int remove_path_from_gitmodules(const char *path);
 extern void stage_updated_gitmodules(void);
 extern void set_diffopt_flags_from_submodule_config(struct diff_options *,
                const char *path);
-extern int submodule_config(const char *var, const char *value, void *cb);
 extern int git_default_submodule_config(const char *var, const char *value, void *cb);
 
 struct option;
 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
                                                     const char *arg, int unset);
-void load_submodule_cache(void);
-extern void gitmodules_config(void);
-extern void repo_read_gitmodules(struct repository *repo);
-extern void gitmodules_config_sha1(const unsigned char *commit_sha1);
 extern int is_submodule_active(struct repository *repo, const char *path);
 /*
  * Determine if a submodule has been populated at a given 'path' by checking if
@@ -61,22 +57,17 @@ extern void die_in_unpopulated_submodule(const struct index_state *istate,
                                         const char *prefix);
 extern void die_path_inside_submodule(const struct index_state *istate,
                                      const struct pathspec *ps);
+extern enum submodule_update_type parse_submodule_update_type(const char *value);
 extern int parse_submodule_update_strategy(const char *value,
                struct submodule_update_strategy *dst);
 extern const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);
 extern void handle_ignore_submodules_arg(struct diff_options *, const char *);
-extern void show_submodule_summary(FILE *f, const char *path,
-               const char *line_prefix,
+extern void show_submodule_summary(struct diff_options *o, const char *path,
                struct object_id *one, struct object_id *two,
-               unsigned dirty_submodule, const char *meta,
-               const char *del, const char *add, const char *reset);
-extern void show_submodule_inline_diff(FILE *f, const char *path,
-               const char *line_prefix,
+               unsigned dirty_submodule);
+extern void show_submodule_inline_diff(struct diff_options *o, const char *path,
                struct object_id *one, struct object_id *two,
-               unsigned dirty_submodule, const char *meta,
-               const char *del, const char *add, const char *reset,
-               const struct diff_options *opt);
-extern void set_config_fetch_recurse_submodules(int value);
+               unsigned dirty_submodule);
 /* Check if we want to update any submodule.*/
 extern int should_update_submodules(void);
 /*
@@ -87,6 +78,7 @@ extern const struct submodule *submodule_from_ce(const struct cache_entry *ce);
 extern void check_for_new_submodule_commits(struct object_id *oid);
 extern int fetch_populated_submodules(const struct argv_array *options,
                               const char *prefix, int command_line_option,
+                              int default_option,
                               int quiet, int max_parallel_jobs);
 extern unsigned is_submodule_modified(const char *path, int ignore_untracked);
 extern int submodule_uses_gitfile(const char *path);
@@ -112,7 +104,6 @@ extern int push_unpushed_submodules(struct oid_array *commits,
                                    const struct string_list *push_options,
                                    int dry_run);
 extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
-extern int parallel_submodules(void);
 /*
  * Given a submodule path (as in the index), return the repository
  * path of that submodule in 'buf'. Return -1 on error or when the
@@ -129,7 +120,7 @@ extern int submodule_move_head(const char *path,
 
 /*
  * Prepare the "env_array" parameter of a "struct child_process" for executing
- * a submodule by clearing any repo-specific envirionment variables, but
+ * a submodule by clearing any repo-specific environment variables, but
  * retaining any config in the environment.
  */
 extern void prepare_submodule_repo_env(struct argv_array *out);
index 2f958603697515f40520a7ff9df31de13eefc2d7..4b079e4494d9324f0b03dfe8f714b3717292c0b6 100644 (file)
--- a/t/README
+++ b/t/README
@@ -265,12 +265,12 @@ or:
 
     $ sh ./t9200-git-cvsexport-commit.sh --run='-3 21'
 
-As noted above, the test set is built going though items left to
-right, so this:
+As noted above, the test set is built by going through the items
+from left to right, so this:
 
     $ sh ./t9200-git-cvsexport-commit.sh --run='1-4 !3'
 
-will run tests 1, 2, and 4.  Items that comes later have higher
+will run tests 1, 2, and 4.  Items that come later have higher
 precedence.  It means that this:
 
     $ sh ./t9200-git-cvsexport-commit.sh --run='!3 1-4'
index b170cbc0452ca1b5a624f5cf330d420c397cb760..03dc9d28525730cdf57c74d13cab22da59dcdfbd 100755 (executable)
@@ -17,7 +17,7 @@ sub err {
 while (<>) {
        chomp;
        /\bsed\s+-i/ and err 'sed -i is not portable';
-       /\becho\s+-n/ and err 'echo -n is not portable (please use printf)';
+       /\becho\s+-[neE]/ and err 'echo with option is not portable (please use printf)';
        /^\s*declare\s+/ and err 'arrays/declare not portable';
        /^\s*[^#]\s*which\s/ and err 'which is not portable (please use type)';
        /\btest\s+[^=]*==/ and err '"test a == b" is not portable (please use =)';
index 721650256e63eb51c66d5985c04bc3095f183c1c..7c9d28a8348341c148f150c3371d09dd98693b53 100644 (file)
@@ -35,3 +35,4 @@
 /test-svn-fe
 /test-urlmatch-normalization
 /test-wildmatch
+/test-write-cache
index 59937dc1be1c4f0b3d80e3ef3a86e09bff3703b6..591730adc4f3940940fdb4691da0afb81648e353 100644 (file)
@@ -69,7 +69,7 @@ int cmd_main(int argc, const char **argv)
        }
 
        fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
-       if (fd < 0 || write_in_full(fd, out_buf, out_size) != out_size) {
+       if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
                perror(argv[4]);
                return 1;
        }
index 095d7395f3c25d042c60095e8c56d8a22a9a0ba6..1145d5167115a761d319b1e205500e765cd090cd 100644 (file)
@@ -13,20 +13,20 @@ static const char *get_value(const struct test_entry *e)
        return e->key + strlen(e->key) + 1;
 }
 
-static int test_entry_cmp(const void *unused_cmp_data,
-                         const struct test_entry *e1,
-                         const struct test_entry *e2,
-                         const char* key)
+static int test_entry_cmp(const void *cmp_data,
+                         const void *entry,
+                         const void *entry_or_key,
+                         const void *keydata)
 {
-       return strcmp(e1->key, key ? key : e2->key);
-}
-
-static int test_entry_cmp_icase(const void *unused_cmp_data,
-                               const struct test_entry *e1,
-                               const struct test_entry *e2,
-                               const char* key)
-{
-       return strcasecmp(e1->key, key ? key : e2->key);
+       const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
+       const struct test_entry *e1 = entry;
+       const struct test_entry *e2 = entry_or_key;
+       const char *key = keydata;
+
+       if (ignore_case)
+               return strcasecmp(e1->key, key ? key : e2->key);
+       else
+               return strcmp(e1->key, key ? key : e2->key);
 }
 
 static struct test_entry *alloc_test_entry(int hash, char *key, int klen,
@@ -96,8 +96,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
        if (method & TEST_ADD) {
                /* test adding to the map */
                for (j = 0; j < rounds; j++) {
-                       hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
-                                    NULL, 0);
+                       hashmap_init(&map, test_entry_cmp, NULL, 0);
 
                        /* add entries */
                        for (i = 0; i < TEST_SIZE; i++) {
@@ -109,7 +108,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
                }
        } else {
                /* test map lookups */
-               hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
+               hashmap_init(&map, test_entry_cmp, NULL, 0);
 
                /* fill the map (sparsely if specified) */
                j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -151,8 +150,7 @@ int cmd_main(int argc, const char **argv)
 
        /* init hash map */
        icase = argc > 1 && !strcmp("ignorecase", argv[1]);
-       hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
-                       : test_entry_cmp), NULL, 0);
+       hashmap_init(&map, test_entry_cmp, &icase, 0);
 
        /* process commands from stdin */
        while (fgets(line, sizeof(line), stdin)) {
@@ -237,7 +235,8 @@ int cmd_main(int argc, const char **argv)
                } else if (!strcmp("size", cmd)) {
 
                        /* print table sizes */
-                       printf("%u %u\n", map.tablesize, map.size);
+                       printf("%u %u\n", map.tablesize,
+                              hashmap_get_size(&map));
 
                } else if (!strcmp("intern", cmd) && l1) {
 
index 81575fe2ab91b550067ce8180650b003bdd939b7..078dd7e29d0844f217a3a76144df312879c8536c 100644 (file)
@@ -17,27 +17,17 @@ static uint32_t strtouint32(const char *s)
 
 static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
 {
-       switch (*command) {
-       case 'b':
-               if (starts_with(command, "binary ")) {
-                       struct strbuf sb = STRBUF_INIT;
-                       strbuf_addch(&sb, '>');
-                       buffer_read_binary(buf, &sb, strtouint32(arg));
-                       fwrite(sb.buf, 1, sb.len, stdout);
-                       strbuf_release(&sb);
-                       return;
-               }
-       case 'c':
-               if (starts_with(command, "copy ")) {
-                       buffer_copy_bytes(buf, strtouint32(arg));
-                       return;
-               }
-       case 's':
-               if (starts_with(command, "skip ")) {
-                       buffer_skip_bytes(buf, strtouint32(arg));
-                       return;
-               }
-       default:
+       if (starts_with(command, "binary ")) {
+               struct strbuf sb = STRBUF_INIT;
+               strbuf_addch(&sb, '>');
+               buffer_read_binary(buf, &sb, strtouint32(arg));
+               fwrite(sb.buf, 1, sb.len, stdout);
+               strbuf_release(&sb);
+       } else if (starts_with(command, "copy ")) {
+               buffer_copy_bytes(buf, strtouint32(arg));
+       } else if (starts_with(command, "skip ")) {
+               buffer_skip_bytes(buf, strtouint32(arg));
+       } else {
                die("unrecognized command: %s", command);
        }
 }
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 c502fa16d307957b6fe23cbd5481fc35f49c00da..829ec3d7d2f58b9538bb3bbab47213eddb9e62ec 100644 (file)
@@ -108,7 +108,7 @@ int cmd_main(int argc, const char **argv)
                 * Split by newline, but don't create a string_list item
                 * for the empty string after the last separator.
                 */
-               if (sb.buf[sb.len - 1] == '\n')
+               if (sb.len && sb.buf[sb.len - 1] == '\n')
                        strbuf_setlen(&sb, sb.len - 1);
                string_list_split_in_place(&list, sb.buf, '\n', -1);
 
index c6c57bba0d397e44e8897b05d479a30b53311517..f23db3b19a9911b554ca8eaf567cd0370d42af6e 100644 (file)
@@ -10,11 +10,6 @@ static void die_usage(int argc, const char **argv, const char *msg)
        exit(1);
 }
 
-static int git_test_config(const char *var, const char *value, void *cb)
-{
-       return parse_submodule_config_option(var, value);
-}
-
 int cmd_main(int argc, const char **argv)
 {
        const char **arg = argv;
@@ -37,11 +32,9 @@ int cmd_main(int argc, const char **argv)
                die_usage(argc, argv, "Wrong number of arguments.");
 
        setup_git_directory();
-       gitmodules_config();
-       git_config(git_test_config, NULL);
 
        while (*arg) {
-               unsigned char commit_sha1[20];
+               struct object_id commit_oid;
                const struct submodule *submodule;
                const char *commit;
                const char *path_or_name;
@@ -50,14 +43,14 @@ int cmd_main(int argc, const char **argv)
                path_or_name = arg[1];
 
                if (commit[0] == '\0')
-                       hashclr(commit_sha1);
-               else if (get_sha1(commit, commit_sha1) < 0)
+                       oidclr(&commit_oid);
+               else if (get_oid(commit, &commit_oid) < 0)
                        die_usage(argc, argv, "Commit not found.");
 
                if (lookup_name) {
-                       submodule = submodule_from_name(commit_sha1, path_or_name);
+                       submodule = submodule_from_name(&commit_oid, path_or_name);
                } else
-                       submodule = submodule_from_path(commit_sha1, path_or_name);
+                       submodule = submodule_from_path(&commit_oid, path_or_name);
                if (!submodule)
                        die_usage(argc, argv, "Submodule not found.");
 
diff --git a/t/helper/test-write-cache.c b/t/helper/test-write-cache.c
new file mode 100644 (file)
index 0000000..b7ee039
--- /dev/null
@@ -0,0 +1,23 @@
+#include "cache.h"
+#include "lockfile.h"
+
+static struct lock_file index_lock;
+
+int cmd_main(int argc, const char **argv)
+{
+       int i, cnt = 1, lockfd;
+       if (argc == 2)
+               cnt = strtol(argv[1], NULL, 0);
+       setup_git_directory();
+       read_cache();
+       for (i = 0; i < cnt; i++) {
+               lockfd = hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
+               if (0 <= lockfd) {
+                       write_locked_index(&the_index, &index_lock, COMMIT_LOCK);
+               } else {
+                       rollback_lock_file(&index_lock);
+               }
+       }
+
+       return 0;
+}
diff --git a/t/perf/p0007-write-cache.sh b/t/perf/p0007-write-cache.sh
new file mode 100755 (executable)
index 0000000..261fe92
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description="Tests performance of writing the index"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_expect_success "setup repo" '
+       if git rev-parse --verify refs/heads/p0006-ballast^{commit}
+       then
+               echo Assuming synthetic repo from many-files.sh
+               git config --local core.sparsecheckout 1
+               cat >.git/info/sparse-checkout <<-EOF
+               /*
+               !ballast/*
+               EOF
+       else
+               echo Assuming non-synthetic repo...
+       fi &&
+       nr_files=$(git ls-files | wc -l)
+'
+
+count=3
+test_perf "write_locked_index $count times ($nr_files files)" "
+       test-write-cache $count
+"
+
+test_done
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
diff --git a/t/t1200-tutorial.sh b/t/t1200-tutorial.sh
deleted file mode 100755 (executable)
index 397ccb6..0000000
+++ /dev/null
@@ -1,268 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2005 Johannes Schindelin
-#
-
-test_description='A simple turial in the form of a test case'
-
-. ./test-lib.sh
-
-test_expect_success 'blob'  '
-       echo "Hello World" > hello &&
-       echo "Silly example" > example &&
-
-       git update-index --add hello example &&
-
-       test blob = "$(git cat-file -t 557db03)"
-'
-
-test_expect_success 'blob 557db03' '
-       test "Hello World" = "$(git cat-file blob 557db03)"
-'
-
-echo "It's a new day for git" >>hello
-cat > diff.expect << EOF
-diff --git a/hello b/hello
-index 557db03..263414f 100644
---- a/hello
-+++ b/hello
-@@ -1 +1,2 @@
- Hello World
-+It's a new day for git
-EOF
-
-test_expect_success 'git diff-files -p' '
-       git diff-files -p > diff.output &&
-       test_cmp diff.expect diff.output
-'
-
-test_expect_success 'git diff' '
-       git diff > diff.output &&
-       test_cmp diff.expect diff.output
-'
-
-test_expect_success 'tree' '
-       tree=$(git write-tree 2>/dev/null) &&
-       test 8988da15d077d4829fc51d8544c097def6644dbb = $tree
-'
-
-test_expect_success 'git diff-index -p HEAD' '
-       test_tick &&
-       tree=$(git write-tree) &&
-       commit=$(echo "Initial commit" | git commit-tree $tree) &&
-       git update-ref HEAD $commit &&
-       git diff-index -p HEAD > diff.output &&
-       test_cmp diff.expect diff.output
-'
-
-test_expect_success 'git diff HEAD' '
-       git diff HEAD > diff.output &&
-       test_cmp diff.expect diff.output
-'
-
-cat > whatchanged.expect << EOF
-commit VARIABLE
-Author: VARIABLE
-Date:   VARIABLE
-
-    Initial commit
-
-diff --git a/example b/example
-new file mode 100644
-index 0000000..f24c74a
---- /dev/null
-+++ b/example
-@@ -0,0 +1 @@
-+Silly example
-diff --git a/hello b/hello
-new file mode 100644
-index 0000000..557db03
---- /dev/null
-+++ b/hello
-@@ -0,0 +1 @@
-+Hello World
-EOF
-
-test_expect_success 'git whatchanged -p --root' '
-       git whatchanged -p --root |
-               sed -e "1s/^\(.\{7\}\).\{40\}/\1VARIABLE/" \
-               -e "2,3s/^\(.\{8\}\).*$/\1VARIABLE/" \
-       > whatchanged.output &&
-       test_cmp whatchanged.expect whatchanged.output
-'
-
-test_expect_success 'git tag my-first-tag' '
-       git tag my-first-tag &&
-       test_cmp .git/refs/heads/master .git/refs/tags/my-first-tag
-'
-
-test_expect_success 'git checkout -b mybranch' '
-       git checkout -b mybranch &&
-       test_cmp .git/refs/heads/master .git/refs/heads/mybranch
-'
-
-cat > branch.expect <<EOF
-  master
-* mybranch
-EOF
-
-test_expect_success 'git branch' '
-       git branch > branch.output &&
-       test_cmp branch.expect branch.output
-'
-
-test_expect_success 'git resolve now fails' '
-       git checkout mybranch &&
-       echo "Work, work, work" >>hello &&
-       test_tick &&
-       git commit -m "Some work." -i hello &&
-
-       git checkout master &&
-
-       echo "Play, play, play" >>hello &&
-       echo "Lots of fun" >>example &&
-       test_tick &&
-       git commit -m "Some fun." -i hello example &&
-
-       test_must_fail git merge -m "Merge work in mybranch" mybranch
-'
-
-cat > hello << EOF
-Hello World
-It's a new day for git
-Play, play, play
-Work, work, work
-EOF
-
-cat > show-branch.expect << EOF
-* [master] Merge work in mybranch
- ! [mybranch] Some work.
---
--  [master] Merge work in mybranch
-*+ [mybranch] Some work.
-*  [master^] Some fun.
-EOF
-
-test_expect_success 'git show-branch' '
-       test_tick &&
-       git commit -m "Merge work in mybranch" -i hello &&
-       git show-branch --topo-order --more=1 master mybranch \
-               > show-branch.output &&
-       test_cmp show-branch.expect show-branch.output
-'
-
-cat > resolve.expect << EOF
-Updating VARIABLE..VARIABLE
-FASTFORWARD (no commit created; -m option ignored)
- example | 1 +
- hello   | 1 +
- 2 files changed, 2 insertions(+)
-EOF
-
-test_expect_success 'git resolve' '
-       git checkout mybranch &&
-       git merge -m "Merge upstream changes." master |
-               sed -e "1s/[0-9a-f]\{7\}/VARIABLE/g" \
-               -e "s/^Fast[- ]forward /FASTFORWARD /" >resolve.output
-'
-
-test_expect_success 'git resolve output' '
-       test_i18ncmp resolve.expect resolve.output
-'
-
-cat > show-branch2.expect << EOF
-! [master] Merge work in mybranch
- * [mybranch] Merge work in mybranch
---
--- [master] Merge work in mybranch
-EOF
-
-test_expect_success 'git show-branch (part 2)' '
-       git show-branch --topo-order master mybranch > show-branch2.output &&
-       test_cmp show-branch2.expect show-branch2.output
-'
-
-cat > show-branch3.expect << EOF
-! [master] Merge work in mybranch
- * [mybranch] Merge work in mybranch
---
--- [master] Merge work in mybranch
-+* [master^2] Some work.
-+* [master^] Some fun.
-EOF
-
-test_expect_success 'git show-branch (part 3)' '
-       git show-branch --topo-order --more=2 master mybranch \
-               > show-branch3.output &&
-       test_cmp show-branch3.expect show-branch3.output
-'
-
-test_expect_success 'rewind to "Some fun." and "Some work."' '
-       git checkout mybranch &&
-       git reset --hard master^2 &&
-       git checkout master &&
-       git reset --hard master^
-'
-
-cat > show-branch4.expect << EOF
-* [master] Some fun.
- ! [mybranch] Some work.
---
-*  [master] Some fun.
- + [mybranch] Some work.
-*+ [master^] Initial commit
-EOF
-
-test_expect_success 'git show-branch (part 4)' '
-       git show-branch --topo-order > show-branch4.output &&
-       test_cmp show-branch4.expect show-branch4.output
-'
-
-test_expect_success 'manual merge' '
-       mb=$(git merge-base HEAD mybranch) &&
-       git name-rev --name-only --tags $mb > name-rev.output &&
-       test "my-first-tag" = $(cat name-rev.output) &&
-
-       git read-tree -m -u $mb HEAD mybranch
-'
-
-cat > ls-files.expect << EOF
-100644 7f8b141b65fdcee47321e399a2598a235a032422 0      example
-100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1      hello
-100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2      hello
-100644 cc44c73eb783565da5831b4d820c962954019b69 3      hello
-EOF
-
-test_expect_success 'git ls-files --stage' '
-       git ls-files --stage > ls-files.output &&
-       test_cmp ls-files.expect ls-files.output
-'
-
-cat > ls-files-unmerged.expect << EOF
-100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1      hello
-100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2      hello
-100644 cc44c73eb783565da5831b4d820c962954019b69 3      hello
-EOF
-
-test_expect_success 'git ls-files --unmerged' '
-       git ls-files --unmerged > ls-files-unmerged.output &&
-       test_cmp ls-files-unmerged.expect ls-files-unmerged.output
-'
-
-test_expect_success 'git-merge-index' '
-       test_must_fail git merge-index git-merge-one-file hello
-'
-
-test_expect_success 'git ls-files --stage (part 2)' '
-       git ls-files --stage > ls-files.output2 &&
-       test_cmp ls-files.expect ls-files.output2
-'
-
-test_expect_success 'git repack' 'git repack'
-test_expect_success 'git prune-packed' 'git prune-packed'
-test_expect_success '-> only packed objects' '
-       git prune && # Remove conflict marked blobs
-       test $(find .git/objects/[0-9a-f][0-9a-f] -type f -print 2>/dev/null | wc -l) = 0
-'
-
-test_done
index dc98b4bc6dc7aab41eca38d9e1086ad2b6701fd3..664a3a4e4e9b81d3e017c22414b0bbe2ef25b83a 100755 (executable)
@@ -1253,7 +1253,10 @@ run_with_limited_open_files () {
        (ulimit -n 32 && "$@")
 }
 
-test_lazy_prereq ULIMIT_FILE_DESCRIPTORS 'run_with_limited_open_files true'
+test_lazy_prereq ULIMIT_FILE_DESCRIPTORS '
+       test_have_prereq !MINGW,!CYGWIN &&
+       run_with_limited_open_files true
+'
 
 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
 (
index c34ece48f5ad77a6d53b5f0a1dcf7d53d7413837..100d50e3623c2eb4b3498dd87f95402617c6d737 100755 (executable)
@@ -404,4 +404,77 @@ test_expect_success 'broken reference blocks indirect create' '
        test_cmp expected output.err
 '
 
+test_expect_success 'no bogus intermediate values during delete' '
+       prefix=refs/slow-transaction &&
+       # Set up a reference with differing loose and packed versions:
+       git update-ref $prefix/foo $C &&
+       git pack-refs --all &&
+       git update-ref $prefix/foo $D &&
+       git for-each-ref $prefix >unchanged &&
+       # Now try to update the reference, but hold the `packed-refs` lock
+       # for a while to see what happens while the process is blocked:
+       : >.git/packed-refs.lock &&
+       test_when_finished "rm -f .git/packed-refs.lock" &&
+       {
+               # Note: the following command is intentionally run in the
+               # background. We increase the timeout so that `update-ref`
+               # attempts to acquire the `packed-refs` lock for longer than
+               # it takes for us to do the check then delete it:
+               git -c core.packedrefstimeout=3000 update-ref -d $prefix/foo &
+       } &&
+       pid2=$! &&
+       # Give update-ref plenty of time to get to the point where it tries
+       # to lock packed-refs:
+       sleep 1 &&
+       # Make sure that update-ref did not complete despite the lock:
+       kill -0 $pid2 &&
+       # Verify that the reference still has its old value:
+       sha1=$(git rev-parse --verify --quiet $prefix/foo || echo undefined) &&
+       case "$sha1" in
+       $D)
+               # This is what we hope for; it means that nothing
+               # user-visible has changed yet.
+               : ;;
+       undefined)
+               # This is not correct; it means the deletion has happened
+               # already even though update-ref should not have been
+               # able to acquire the lock yet.
+               echo "$prefix/foo deleted prematurely" &&
+               break
+               ;;
+       $C)
+               # This value should never be seen. Probably the loose
+               # reference has been deleted but the packed reference
+               # is still there:
+               echo "$prefix/foo incorrectly observed to be C" &&
+               break
+               ;;
+       *)
+               # WTF?
+               echo "unexpected value observed for $prefix/foo: $sha1" &&
+               break
+               ;;
+       esac >out &&
+       rm -f .git/packed-refs.lock &&
+       wait $pid2 &&
+       test_must_be_empty out &&
+       test_must_fail git rev-parse --verify --quiet $prefix/foo
+'
+
+test_expect_success 'delete fails cleanly if packed-refs file is locked' '
+       prefix=refs/locked-packed-refs &&
+       # Set up a reference with differing loose and packed versions:
+       git update-ref $prefix/foo $C &&
+       git pack-refs --all &&
+       git update-ref $prefix/foo $D &&
+       git for-each-ref $prefix >unchanged &&
+       # Now try to delete it while the `packed-refs` lock is held:
+       : >.git/packed-refs.lock &&
+       test_when_finished "rm -f .git/packed-refs.lock" &&
+       test_must_fail git update-ref -d $prefix/foo >out 2>err &&
+       git for-each-ref $prefix >actual &&
+       test_i18ngrep "Unable to create $Q.*packed-refs.lock$Q: File exists" err &&
+       test_cmp unchanged actual
+'
+
 test_done
index 5df06f355641ad7a38daf34f9fc841e7fe4f8a0f..8842d0329fb7947e811d4ffccff3bba9e8885d5d 100755 (executable)
@@ -49,4 +49,34 @@ test_expect_success 'create_symref(FOO, refs/heads/master)' '
        test_cmp expected actual
 '
 
+test_expect_success 'for_each_reflog()' '
+       echo $_z40 > .git/logs/PSEUDO-MAIN &&
+       mkdir -p     .git/logs/refs/bisect &&
+       echo $_z40 > .git/logs/refs/bisect/random &&
+
+       echo $_z40 > .git/worktrees/wt/logs/PSEUDO-WT &&
+       mkdir -p     .git/worktrees/wt/logs/refs/bisect &&
+       echo $_z40 > .git/worktrees/wt/logs/refs/bisect/wt-random &&
+
+       $RWT for-each-reflog | cut -c 42- | sort >actual &&
+       cat >expected <<-\EOF &&
+       HEAD 0x1
+       PSEUDO-WT 0x0
+       refs/bisect/wt-random 0x0
+       refs/heads/master 0x0
+       refs/heads/wt-master 0x0
+       EOF
+       test_cmp expected actual &&
+
+       $RMAIN for-each-reflog | cut -c 42- | sort >actual &&
+       cat >expected <<-\EOF &&
+       HEAD 0x1
+       PSEUDO-MAIN 0x0
+       refs/bisect/random 0x0
+       refs/heads/master 0x0
+       refs/heads/wt-master 0x0
+       EOF
+       test_cmp expected actual
+'
+
 test_done
diff --git a/t/t1408-packed-refs.sh b/t/t1408-packed-refs.sh
new file mode 100755 (executable)
index 0000000..1e44a17
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='packed-refs entries are covered by loose refs'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+       test_tick &&
+       git commit --allow-empty -m one &&
+       one=$(git rev-parse HEAD) &&
+       git for-each-ref >actual &&
+       echo "$one commit       refs/heads/master" >expect &&
+       test_cmp expect actual &&
+
+       git pack-refs --all &&
+       git for-each-ref >actual &&
+       echo "$one commit       refs/heads/master" >expect &&
+       test_cmp expect actual &&
+
+       git checkout --orphan another &&
+       test_tick &&
+       git commit --allow-empty -m two &&
+       two=$(git rev-parse HEAD) &&
+       git checkout -B master &&
+       git branch -D another &&
+
+       git for-each-ref >actual &&
+       echo "$two commit       refs/heads/master" >expect &&
+       test_cmp expect actual &&
+
+       git reflog expire --expire=now --all &&
+       git prune &&
+       git tag -m v1.0 v1.0 master
+'
+
+test_expect_success 'no error from stale entry in packed-refs' '
+       git describe master >actual 2>&1 &&
+       echo "v1.0" >expect &&
+       test_cmp expect actual
+'
+
+test_done
index 03d3c7f6d65b46bf977adf76a6d68582206768c2..5c715fe2cf6c7afe2c39e6a7197cdd3d33329b8b 100755 (executable)
@@ -116,6 +116,21 @@ test_expect_success 'git-path inside sub-dir' '
        test_cmp expect actual
 '
 
+test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
+       test_commit test_commit &&
+       echo true >expect &&
+       git clone --depth 1 --no-local . shallow &&
+       test_when_finished "rm -rf shallow" &&
+       git -C shallow rev-parse --is-shallow-repository >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '
+       echo false >expect &&
+       git rev-parse --is-shallow-repository >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'showing the superproject correctly' '
        git rev-parse --show-superproject-working-tree >out &&
        test_must_be_empty out &&
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 9d707d2a40a457abcd85fb93d9f8195946d09d01..3ac7ebf85f3198cde56f784456e0024205aec853 100755 (executable)
@@ -162,6 +162,19 @@ test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD'
        grep "^0\{40\}.*$msg$" .git/logs/HEAD
 '
 
+test_expect_success 'git branch -M should leave orphaned HEAD alone' '
+       git init orphan &&
+       (
+               cd orphan &&
+               test_commit initial &&
+               git checkout --orphan lonely &&
+               grep lonely .git/HEAD &&
+               test_path_is_missing .git/refs/head/lonely &&
+               git branch -M master mistress &&
+               grep lonely .git/HEAD
+       )
+'
+
 test_expect_success 'resulting reflog can be shown by log -g' '
        oid=$(git rev-parse HEAD) &&
        cat >expect <<-EOF &&
@@ -368,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 &&
@@ -559,6 +828,7 @@ test_expect_success 'use --set-upstream-to modify HEAD' '
 test_expect_success 'use --set-upstream-to modify a particular branch' '
        git branch my13 &&
        git branch --set-upstream-to master my13 &&
+       test_when_finished "git branch --unset-upstream my13" &&
        test "$(git config branch.my13.remote)" = "." &&
        test "$(git config branch.my13.merge)" = "refs/heads/master"
 '
@@ -604,38 +874,8 @@ test_expect_success 'test --unset-upstream on a particular branch' '
        test_must_fail git config branch.my14.merge
 '
 
-test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' '
-       git update-ref refs/remotes/origin/master HEAD &&
-       git branch --set-upstream origin/master 2>actual &&
-       test_when_finished git update-ref -d refs/remotes/origin/master &&
-       test_when_finished git branch -d origin/master &&
-       cat >expected <<EOF &&
-The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
-
-If you wanted to make '"'master'"' track '"'origin/master'"', do this:
-
-    git branch -d origin/master
-    git branch --set-upstream-to origin/master
-EOF
-       test_i18ncmp expected actual
-'
-
-test_expect_success '--set-upstream with two args only shows the deprecation message' '
-       git branch --set-upstream master my13 2>actual &&
-       test_when_finished git branch --unset-upstream master &&
-       cat >expected <<EOF &&
-The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
-EOF
-       test_i18ncmp expected actual
-'
-
-test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' '
-       git branch --set-upstream my13 2>actual &&
-       test_when_finished git branch --unset-upstream my13 &&
-       cat >expected <<EOF &&
-The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
-EOF
-       test_i18ncmp expected actual
+test_expect_success '--set-upstream fails' '
+    test_must_fail git branch --set-upstream origin/master
 '
 
 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
@@ -960,19 +1200,6 @@ test_expect_success 'attempt to delete a branch merged to its base' '
        test_must_fail git branch -d my10
 '
 
-test_expect_success 'use set-upstream on the current branch' '
-       git checkout master &&
-       git --bare init myupstream.git &&
-       git push myupstream.git master:refs/heads/frotz &&
-       git remote add origin myupstream.git &&
-       git fetch &&
-       git branch --set-upstream master origin/frotz &&
-
-       test "z$(git config branch.master.remote)" = "zorigin" &&
-       test "z$(git config branch.master.merge)" = "zrefs/heads/frotz"
-
-'
-
 test_expect_success 'use --edit-description' '
        write_script editor <<-\EOF &&
                echo "New contents" >"$1"
index d2aec0f38b7913b27687b332029dc78be59bc182..ee6787614c80edb9b5b24065277dd413350a6fc6 100755 (executable)
@@ -253,13 +253,7 @@ test_expect_success '%(color) omitted without tty' '
 '
 
 test_expect_success TTY '%(color) present with tty' '
-       test_terminal env TERM=vt100 git branch $color_args >actual.raw &&
-       test_decode_color <actual.raw >actual &&
-       test_cmp expect.color actual
-'
-
-test_expect_success 'color.branch=always overrides auto-color' '
-       git -c color.branch=always branch $color_args >actual.raw &&
+       test_terminal git branch $color_args >actual.raw &&
        test_decode_color <actual.raw >actual &&
        test_cmp expect.color actual
 '
index 9343550f50a011ff563c1455384cd1b14fd83888..4f1e16bb44e279a0c5c0fc96f95eb285e4b22c3c 100755 (executable)
@@ -12,7 +12,6 @@ test_expect_success 'set up some sample branches' '
 # choose non-default colors to make sure config
 # is taking effect
 test_expect_success 'set up some color config' '
-       git config color.branch always &&
        git config color.branch.local blue &&
        git config color.branch.remote yellow &&
        git config color.branch.current cyan
@@ -24,7 +23,7 @@ test_expect_success 'regular output shows colors' '
          <BLUE>other<RESET>
          <YELLOW>remotes/origin/master<RESET>
        EOF
-       git branch -a >actual.raw &&
+       git branch --color -a >actual.raw &&
        test_decode_color <actual.raw >actual &&
        test_cmp expect actual
 '
@@ -36,7 +35,7 @@ test_expect_success 'verbose output shows colors' '
          <BLUE>other                <RESET> $oid foo
          <YELLOW>remotes/origin/master<RESET> $oid foo
        EOF
-       git branch -v -a >actual.raw &&
+       git branch --color -v -a >actual.raw &&
        test_decode_color <actual.raw >actual &&
        test_cmp expect actual
 '
index 9b182a0c328e9bc6eb1e24a0cbaec47d9fa36b24..afa27ffe2d869a5e7fdf8318d5b9957208a96c52 100755 (executable)
@@ -194,6 +194,33 @@ test_expect_success 'notice d/f conflict with existing ref' '
        test_must_fail git branch foo/bar/baz/lots/of/extra/components
 '
 
+test_expect_success 'reject packed-refs with unterminated line' '
+       cp .git/packed-refs .git/packed-refs.bak &&
+       test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+       printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
+       echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
+       test_must_fail git for-each-ref >out 2>err &&
+       test_cmp expected_err err
+'
+
+test_expect_success 'reject packed-refs containing junk' '
+       cp .git/packed-refs .git/packed-refs.bak &&
+       test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+       printf "%s\n" "bogus content" >>.git/packed-refs &&
+       echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
+       test_must_fail git for-each-ref >out 2>err &&
+       test_cmp expected_err err
+'
+
+test_expect_success 'reject packed-refs with a short SHA-1' '
+       cp .git/packed-refs .git/packed-refs.bak &&
+       test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+       printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
+       printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
+       test_must_fail git for-each-ref >out 2>err &&
+       test_cmp expected_err err
+'
+
 test_expect_success 'timeout if packed-refs.lock exists' '
        LOCK=.git/packed-refs.lock &&
        >"$LOCK" &&
@@ -211,4 +238,19 @@ test_expect_success 'retry acquiring packed-refs.lock' '
        git -c core.packedrefstimeout=3000 pack-refs --all --prune
 '
 
+test_expect_success SYMLINKS 'pack symlinked packed-refs' '
+       # First make sure that symlinking works when reading:
+       git update-ref refs/heads/loosy refs/heads/master &&
+       git for-each-ref >all-refs-before &&
+       mv .git/packed-refs .git/my-deviant-packed-refs &&
+       ln -s my-deviant-packed-refs .git/packed-refs &&
+       git for-each-ref >all-refs-linked &&
+       test_cmp all-refs-before all-refs-linked &&
+       git pack-refs --all --prune &&
+       git for-each-ref >all-refs-packed &&
+       test_cmp all-refs-before all-refs-packed &&
+       test -h .git/packed-refs &&
+       test "$(readlink .git/packed-refs)" = "my-deviant-packed-refs"
+'
+
 test_done
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 2f3e7cea64e897299d87196135032de22050354b..a49c12c79b28fac8545dffcbaba8e8905c8d6222 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='add -i basic tests'
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
 
 if ! test_have_prereq PERL
 then
@@ -380,14 +381,11 @@ test_expect_success 'patch mode ignores unmerged entries' '
        test_cmp expected diff
 '
 
-test_expect_success 'diffs can be colorized' '
+test_expect_success TTY 'diffs can be colorized' '
        git reset --hard &&
 
-       # force color even though the test script has no terminal
-       test_config color.ui always &&
-
        echo content >test &&
-       printf y | git add -p >output 2>&1 &&
+       printf y | test_terminal git add -p >output 2>&1 &&
 
        # We do not want to depend on the exact coloring scheme
        # git uses for diffs, so just check that we saw some kind of color.
@@ -485,4 +483,14 @@ test_expect_success 'hunk-editing handles custom comment char' '
        git diff --exit-code
 '
 
+test_expect_success 'add -p works even with color.ui=always' '
+       git reset --hard &&
+       echo change >>file &&
+       test_config color.ui always &&
+       echo y | git add -p &&
+       echo file >expect &&
+       git diff --cached --name-only >actual &&
+       test_cmp expect actual
+'
+
 test_done
index 4046817d70a0ac1dd5b11a49c500896688a4f0b1..3b1ac1971af1d972b77b3dfe8ae4a56e00e1fa55 100755 (executable)
@@ -444,6 +444,14 @@ test_expect_failure 'stash file to directory' '
        test foo = "$(cat file/file)"
 '
 
+test_expect_success 'stash create - no changes' '
+       git stash clear &&
+       test_when_finished "git reset --hard HEAD" &&
+       git reset --hard &&
+       git stash create >actual &&
+       test_must_be_empty actual
+'
+
 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
        git stash clear &&
        test_when_finished "git reset --hard HEAD" &&
@@ -648,6 +656,20 @@ test_expect_success 'stash branch should not drop the stash if the branch exists
        git rev-parse stash@{0} --
 '
 
+test_expect_success 'stash branch should not drop the stash if the apply fails' '
+       git stash clear &&
+       git reset HEAD~1 --hard &&
+       echo foo >file &&
+       git add file &&
+       git commit -m initial &&
+       echo bar >file &&
+       git stash &&
+       echo baz >file &&
+       test_when_finished "git checkout master" &&
+       test_must_fail git stash branch new_branch stash@{0} &&
+       git rev-parse stash@{0} --
+'
+
 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
        git stash clear &&
        echo 1 >subdir/subfile1 &&
@@ -800,6 +822,18 @@ test_expect_success 'create with multiple arguments for the message' '
        test_cmp expect actual
 '
 
+test_expect_success 'create in a detached state' '
+       test_when_finished "git checkout master" &&
+       git checkout HEAD~1 &&
+       >foo &&
+       git add foo &&
+       STASH_ID=$(git stash create) &&
+       HEAD_ID=$(git rev-parse --short HEAD) &&
+       echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
+       git show --pretty=%s -s ${STASH_ID} >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
        >foo &&
        >bar &&
index 193adc7b68d691526b804fa7ce02e1f42f21b8a4..bfde4057ad2afcdd3bd38cbb43ffa2ce241aaa67 100755 (executable)
@@ -211,4 +211,21 @@ test_expect_success 'stash push with $IFS character' '
        test_path_is_file bar
 '
 
+cat > .gitignore <<EOF
+ignored
+ignored.d/*
+EOF
+
+test_expect_success 'stash previously ignored file' '
+       git reset HEAD &&
+       git add .gitignore &&
+       git commit -m "Add .gitignore" &&
+       >ignored.d/foo &&
+       echo "!ignored.d/foo" >> .gitignore &&
+       git stash save --include-untracked &&
+       test_path_is_missing ignored.d/foo &&
+       git stash pop &&
+       test_path_is_file ignored.d/foo
+'
+
 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 289806d0c7eb02e0acc5244df5e3999d45b4e085..3bca958863e68783105b542765930ebd7518b49b 100755 (executable)
@@ -802,7 +802,6 @@ test_expect_success 'combined diff with autocrlf conversion' '
 # Start testing the colored format for whitespace checks
 
 test_expect_success 'setup diff colors' '
-       git config color.diff always &&
        git config color.diff.plain normal &&
        git config color.diff.meta bold &&
        git config color.diff.frag cyan &&
@@ -821,7 +820,7 @@ test_expect_success 'diff that introduces a line with only tabs' '
        echo "test" >x &&
        git commit -m "initial" x &&
        echo "{NTN}" | tr "NT" "\n\t" >>x &&
-       git -c color.diff=always diff | test_decode_color >current &&
+       git diff --color | test_decode_color >current &&
 
        cat >expected <<-\EOF &&
        <BOLD>diff --git a/x b/x<RESET>
@@ -851,7 +850,7 @@ test_expect_success 'diff that introduces and removes ws breakages' '
                echo "2. and a new line "
        } >x &&
 
-       git -c color.diff=always diff |
+       git diff --color |
        test_decode_color >current &&
 
        cat >expected <<-\EOF &&
@@ -923,15 +922,15 @@ test_expect_success 'ws-error-highlight test setup' '
 
 test_expect_success 'test --ws-error-highlight option' '
 
-       git -c color.diff=always diff --ws-error-highlight=default,old |
+       git diff --color --ws-error-highlight=default,old |
        test_decode_color >current &&
        test_cmp expect.default-old current &&
 
-       git -c color.diff=always diff --ws-error-highlight=all |
+       git diff --color --ws-error-highlight=all |
        test_decode_color >current &&
        test_cmp expect.all current &&
 
-       git -c color.diff=always diff --ws-error-highlight=none |
+       git diff --color --ws-error-highlight=none |
        test_decode_color >current &&
        test_cmp expect.none current
 
@@ -939,15 +938,15 @@ test_expect_success 'test --ws-error-highlight option' '
 
 test_expect_success 'test diff.wsErrorHighlight config' '
 
-       git -c color.diff=always -c diff.wsErrorHighlight=default,old diff |
+       git -c diff.wsErrorHighlight=default,old diff --color |
        test_decode_color >current &&
        test_cmp expect.default-old current &&
 
-       git -c color.diff=always -c diff.wsErrorHighlight=all diff |
+       git -c diff.wsErrorHighlight=all diff --color |
        test_decode_color >current &&
        test_cmp expect.all current &&
 
-       git -c color.diff=always -c diff.wsErrorHighlight=none diff |
+       git -c diff.wsErrorHighlight=none diff --color |
        test_decode_color >current &&
        test_cmp expect.none current
 
@@ -955,21 +954,580 @@ test_expect_success 'test diff.wsErrorHighlight config' '
 
 test_expect_success 'option overrides diff.wsErrorHighlight' '
 
-       git -c color.diff=always -c diff.wsErrorHighlight=none \
-               diff --ws-error-highlight=default,old |
+       git -c diff.wsErrorHighlight=none \
+               diff --color --ws-error-highlight=default,old |
        test_decode_color >current &&
        test_cmp expect.default-old current &&
 
-       git -c color.diff=always -c diff.wsErrorHighlight=default \
-               diff --ws-error-highlight=all |
+       git -c diff.wsErrorHighlight=default \
+               diff --color --ws-error-highlight=all |
        test_decode_color >current &&
        test_cmp expect.all current &&
 
-       git -c color.diff=always -c diff.wsErrorHighlight=all \
-               diff --ws-error-highlight=none |
+       git -c diff.wsErrorHighlight=all \
+               diff --color --ws-error-highlight=none |
        test_decode_color >current &&
        test_cmp expect.none current
 
 '
 
+test_expect_success 'detect moved code, complete file' '
+       git reset --hard &&
+       cat <<-\EOF >test.c &&
+       #include<stdio.h>
+       main()
+       {
+       printf("Hello World");
+       }
+       EOF
+       git add test.c &&
+       git commit -m "add main function" &&
+       git mv test.c main.c &&
+       test_config color.diff.oldMoved "normal red" &&
+       test_config color.diff.newMoved "normal green" &&
+       git diff HEAD --color-moved=zebra --color --no-renames | test_decode_color >actual &&
+       cat >expected <<-\EOF &&
+       <BOLD>diff --git a/main.c b/main.c<RESET>
+       <BOLD>new file mode 100644<RESET>
+       <BOLD>index 0000000..a986c57<RESET>
+       <BOLD>--- /dev/null<RESET>
+       <BOLD>+++ b/main.c<RESET>
+       <CYAN>@@ -0,0 +1,5 @@<RESET>
+       <BGREEN>+<RESET><BGREEN>#include<stdio.h><RESET>
+       <BGREEN>+<RESET><BGREEN>main()<RESET>
+       <BGREEN>+<RESET><BGREEN>{<RESET>
+       <BGREEN>+<RESET><BGREEN>printf("Hello World");<RESET>
+       <BGREEN>+<RESET><BGREEN>}<RESET>
+       <BOLD>diff --git a/test.c b/test.c<RESET>
+       <BOLD>deleted file mode 100644<RESET>
+       <BOLD>index a986c57..0000000<RESET>
+       <BOLD>--- a/test.c<RESET>
+       <BOLD>+++ /dev/null<RESET>
+       <CYAN>@@ -1,5 +0,0 @@<RESET>
+       <BRED>-#include<stdio.h><RESET>
+       <BRED>-main()<RESET>
+       <BRED>-{<RESET>
+       <BRED>-printf("Hello World");<RESET>
+       <BRED>-}<RESET>
+       EOF
+
+       test_cmp expected actual
+'
+
+test_expect_success 'detect malicious moved code, inside file' '
+       test_config color.diff.oldMoved "normal red" &&
+       test_config color.diff.newMoved "normal green" &&
+       test_config color.diff.oldMovedAlternative "blue" &&
+       test_config color.diff.newMovedAlternative "yellow" &&
+       git reset --hard &&
+       cat <<-\EOF >main.c &&
+               #include<stdio.h>
+               int stuff()
+               {
+                       printf("Hello ");
+                       printf("World\n");
+               }
+
+               int secure_foo(struct user *u)
+               {
+                       if (!u->is_allowed_foo)
+                               return;
+                       foo(u);
+               }
+
+               int main()
+               {
+                       foo();
+               }
+       EOF
+       cat <<-\EOF >test.c &&
+               #include<stdio.h>
+               int bar()
+               {
+                       printf("Hello World, but different\n");
+               }
+
+               int another_function()
+               {
+                       bar();
+               }
+       EOF
+       git add main.c test.c &&
+       git commit -m "add main and test file" &&
+       cat <<-\EOF >main.c &&
+               #include<stdio.h>
+               int stuff()
+               {
+                       printf("Hello ");
+                       printf("World\n");
+               }
+
+               int main()
+               {
+                       foo();
+               }
+       EOF
+       cat <<-\EOF >test.c &&
+               #include<stdio.h>
+               int bar()
+               {
+                       printf("Hello World, but different\n");
+               }
+
+               int secure_foo(struct user *u)
+               {
+                       foo(u);
+                       if (!u->is_allowed_foo)
+                               return;
+               }
+
+               int another_function()
+               {
+                       bar();
+               }
+       EOF
+       git diff HEAD --no-renames --color-moved=zebra --color | test_decode_color >actual &&
+       cat <<-\EOF >expected &&
+       <BOLD>diff --git a/main.c b/main.c<RESET>
+       <BOLD>index 27a619c..7cf9336 100644<RESET>
+       <BOLD>--- a/main.c<RESET>
+       <BOLD>+++ b/main.c<RESET>
+       <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
+        printf("World\n");<RESET>
+        }<RESET>
+        <RESET>
+       <BRED>-int secure_foo(struct user *u)<RESET>
+       <BRED>-{<RESET>
+       <BLUE>-if (!u->is_allowed_foo)<RESET>
+       <BLUE>-return;<RESET>
+       <RED>-foo(u);<RESET>
+       <RED>-}<RESET>
+       <RED>-<RESET>
+        int main()<RESET>
+        {<RESET>
+        foo();<RESET>
+       <BOLD>diff --git a/test.c b/test.c<RESET>
+       <BOLD>index 1dc1d85..2bedec9 100644<RESET>
+       <BOLD>--- a/test.c<RESET>
+       <BOLD>+++ b/test.c<RESET>
+       <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
+        printf("Hello World, but different\n");<RESET>
+        }<RESET>
+        <RESET>
+       <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
+       <BGREEN>+<RESET><BGREEN>{<RESET>
+       <GREEN>+<RESET><GREEN>foo(u);<RESET>
+       <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
+       <BGREEN>+<RESET><BGREEN>return;<RESET>
+       <GREEN>+<RESET><GREEN>}<RESET>
+       <GREEN>+<RESET>
+        int another_function()<RESET>
+        {<RESET>
+        bar();<RESET>
+       EOF
+
+       test_cmp expected actual
+'
+
+test_expect_success 'plain moved code, inside file' '
+       test_config color.diff.oldMoved "normal red" &&
+       test_config color.diff.newMoved "normal green" &&
+       test_config color.diff.oldMovedAlternative "blue" &&
+       test_config color.diff.newMovedAlternative "yellow" &&
+       # needs previous test as setup
+       git diff HEAD --no-renames --color-moved=plain --color | test_decode_color >actual &&
+       cat <<-\EOF >expected &&
+       <BOLD>diff --git a/main.c b/main.c<RESET>
+       <BOLD>index 27a619c..7cf9336 100644<RESET>
+       <BOLD>--- a/main.c<RESET>
+       <BOLD>+++ b/main.c<RESET>
+       <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
+        printf("World\n");<RESET>
+        }<RESET>
+        <RESET>
+       <BRED>-int secure_foo(struct user *u)<RESET>
+       <BRED>-{<RESET>
+       <BRED>-if (!u->is_allowed_foo)<RESET>
+       <BRED>-return;<RESET>
+       <BRED>-foo(u);<RESET>
+       <BRED>-}<RESET>
+       <BRED>-<RESET>
+        int main()<RESET>
+        {<RESET>
+        foo();<RESET>
+       <BOLD>diff --git a/test.c b/test.c<RESET>
+       <BOLD>index 1dc1d85..2bedec9 100644<RESET>
+       <BOLD>--- a/test.c<RESET>
+       <BOLD>+++ b/test.c<RESET>
+       <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
+        printf("Hello World, but different\n");<RESET>
+        }<RESET>
+        <RESET>
+       <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
+       <BGREEN>+<RESET><BGREEN>{<RESET>
+       <BGREEN>+<RESET><BGREEN>foo(u);<RESET>
+       <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
+       <BGREEN>+<RESET><BGREEN>return;<RESET>
+       <BGREEN>+<RESET><BGREEN>}<RESET>
+       <BGREEN>+<RESET>
+        int another_function()<RESET>
+        {<RESET>
+        bar();<RESET>
+       EOF
+
+       test_cmp expected actual
+'
+
+test_expect_success 'detect permutations inside moved code -- dimmed_zebra' '
+       git reset --hard &&
+       cat <<-\EOF >lines.txt &&
+               long line 1
+               long line 2
+               long line 3
+               line 4
+               line 5
+               line 6
+               line 7
+               line 8
+               line 9
+               line 10
+               line 11
+               line 12
+               line 13
+               long line 14
+               long line 15
+               long line 16
+       EOF
+       git add lines.txt &&
+       git commit -m "add poetry" &&
+       cat <<-\EOF >lines.txt &&
+               line 4
+               line 5
+               line 6
+               line 7
+               line 8
+               line 9
+               long line 1
+               long line 2
+               long line 3
+               long line 14
+               long line 15
+               long line 16
+               line 10
+               line 11
+               line 12
+               line 13
+       EOF
+       test_config color.diff.oldMoved "magenta" &&
+       test_config color.diff.newMoved "cyan" &&
+       test_config color.diff.oldMovedAlternative "blue" &&
+       test_config color.diff.newMovedAlternative "yellow" &&
+       test_config color.diff.oldMovedDimmed "normal magenta" &&
+       test_config color.diff.newMovedDimmed "normal cyan" &&
+       test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
+       test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
+       git diff HEAD --no-renames --color-moved=dimmed_zebra --color |
+               grep -v "index" |
+               test_decode_color >actual &&
+       cat <<-\EOF >expected &&
+       <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+       <BOLD>--- a/lines.txt<RESET>
+       <BOLD>+++ b/lines.txt<RESET>
+       <CYAN>@@ -1,16 +1,16 @@<RESET>
+       <BMAGENTA>-long line 1<RESET>
+       <BMAGENTA>-long line 2<RESET>
+       <BMAGENTA>-long line 3<RESET>
+        line 4<RESET>
+        line 5<RESET>
+        line 6<RESET>
+        line 7<RESET>
+        line 8<RESET>
+        line 9<RESET>
+       <BCYAN>+<RESET><BCYAN>long line 1<RESET>
+       <BCYAN>+<RESET><BCYAN>long line 2<RESET>
+       <CYAN>+<RESET><CYAN>long line 3<RESET>
+       <YELLOW>+<RESET><YELLOW>long line 14<RESET>
+       <BYELLOW>+<RESET><BYELLOW>long line 15<RESET>
+       <BYELLOW>+<RESET><BYELLOW>long line 16<RESET>
+        line 10<RESET>
+        line 11<RESET>
+        line 12<RESET>
+        line 13<RESET>
+       <BMAGENTA>-long line 14<RESET>
+       <BMAGENTA>-long line 15<RESET>
+       <BMAGENTA>-long line 16<RESET>
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success 'cmd option assumes configured colored-moved' '
+       test_config color.diff.oldMoved "magenta" &&
+       test_config color.diff.newMoved "cyan" &&
+       test_config color.diff.oldMovedAlternative "blue" &&
+       test_config color.diff.newMovedAlternative "yellow" &&
+       test_config color.diff.oldMovedDimmed "normal magenta" &&
+       test_config color.diff.newMovedDimmed "normal cyan" &&
+       test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
+       test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
+       test_config diff.colorMoved zebra &&
+       git diff HEAD --no-renames --color-moved --color |
+               grep -v "index" |
+               test_decode_color >actual &&
+       cat <<-\EOF >expected &&
+       <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+       <BOLD>--- a/lines.txt<RESET>
+       <BOLD>+++ b/lines.txt<RESET>
+       <CYAN>@@ -1,16 +1,16 @@<RESET>
+       <MAGENTA>-long line 1<RESET>
+       <MAGENTA>-long line 2<RESET>
+       <MAGENTA>-long line 3<RESET>
+        line 4<RESET>
+        line 5<RESET>
+        line 6<RESET>
+        line 7<RESET>
+        line 8<RESET>
+        line 9<RESET>
+       <CYAN>+<RESET><CYAN>long line 1<RESET>
+       <CYAN>+<RESET><CYAN>long line 2<RESET>
+       <CYAN>+<RESET><CYAN>long line 3<RESET>
+       <YELLOW>+<RESET><YELLOW>long line 14<RESET>
+       <YELLOW>+<RESET><YELLOW>long line 15<RESET>
+       <YELLOW>+<RESET><YELLOW>long line 16<RESET>
+        line 10<RESET>
+        line 11<RESET>
+        line 12<RESET>
+        line 13<RESET>
+       <MAGENTA>-long line 14<RESET>
+       <MAGENTA>-long line 15<RESET>
+       <MAGENTA>-long line 16<RESET>
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success 'no effect from --color-moved with --word-diff' '
+       cat <<-\EOF >text.txt &&
+       Lorem Ipsum is simply dummy text of the printing and typesetting industry.
+       EOF
+       git add text.txt &&
+       git commit -a -m "clean state" &&
+       cat <<-\EOF >text.txt &&
+       simply Lorem Ipsum dummy is text of the typesetting and printing industry.
+       EOF
+       git diff --color-moved --word-diff >actual &&
+       git diff --word-diff >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success 'move detection ignoring whitespace ' '
+       git reset --hard &&
+       cat <<\EOF >lines.txt &&
+line 1
+line 2
+line 3
+line 4
+long line 5
+long line 6
+long line 7
+EOF
+       git add lines.txt &&
+       git commit -m "add poetry" &&
+       cat <<\EOF >lines.txt &&
+       long line 5
+       long line 6
+       long line 7
+line 1
+line 2
+line 3
+line 4
+EOF
+       test_config color.diff.oldMoved "magenta" &&
+       test_config color.diff.newMoved "cyan" &&
+       git diff HEAD --no-renames --color-moved --color |
+               grep -v "index" |
+               test_decode_color >actual &&
+       cat <<-\EOF >expected &&
+       <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+       <BOLD>--- a/lines.txt<RESET>
+       <BOLD>+++ b/lines.txt<RESET>
+       <CYAN>@@ -1,7 +1,7 @@<RESET>
+       <GREEN>+<RESET> <GREEN>long line 5<RESET>
+       <GREEN>+<RESET> <GREEN>long line 6<RESET>
+       <GREEN>+<RESET> <GREEN>long line 7<RESET>
+        line 1<RESET>
+        line 2<RESET>
+        line 3<RESET>
+        line 4<RESET>
+       <RED>-long line 5<RESET>
+       <RED>-long line 6<RESET>
+       <RED>-long line 7<RESET>
+       EOF
+       test_cmp expected actual &&
+
+       git diff HEAD --no-renames -w --color-moved --color |
+               grep -v "index" |
+               test_decode_color >actual &&
+       cat <<-\EOF >expected &&
+       <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+       <BOLD>--- a/lines.txt<RESET>
+       <BOLD>+++ b/lines.txt<RESET>
+       <CYAN>@@ -1,7 +1,7 @@<RESET>
+       <CYAN>+<RESET>  <CYAN>long line 5<RESET>
+       <CYAN>+<RESET>  <CYAN>long line 6<RESET>
+       <CYAN>+<RESET>  <CYAN>long line 7<RESET>
+        line 1<RESET>
+        line 2<RESET>
+        line 3<RESET>
+        line 4<RESET>
+       <MAGENTA>-long line 5<RESET>
+       <MAGENTA>-long line 6<RESET>
+       <MAGENTA>-long line 7<RESET>
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success '--color-moved block at end of diff output respects MIN_ALNUM_COUNT' '
+       git reset --hard &&
+       >bar &&
+       cat <<-\EOF >foo &&
+       irrelevant_line
+       line1
+       EOF
+       git add foo bar &&
+       git commit -m x &&
+
+       cat <<-\EOF >bar &&
+       line1
+       EOF
+       cat <<-\EOF >foo &&
+       irrelevant_line
+       EOF
+
+       git diff HEAD --color-moved=zebra --color --no-renames |
+               grep -v "index" |
+               test_decode_color >actual &&
+       cat >expected <<-\EOF &&
+       <BOLD>diff --git a/bar b/bar<RESET>
+       <BOLD>--- a/bar<RESET>
+       <BOLD>+++ b/bar<RESET>
+       <CYAN>@@ -0,0 +1 @@<RESET>
+       <GREEN>+<RESET><GREEN>line1<RESET>
+       <BOLD>diff --git a/foo b/foo<RESET>
+       <BOLD>--- a/foo<RESET>
+       <BOLD>+++ b/foo<RESET>
+       <CYAN>@@ -1,2 +1 @@<RESET>
+        irrelevant_line<RESET>
+       <RED>-line1<RESET>
+       EOF
+
+       test_cmp expected actual
+'
+
+test_expect_success '--color-moved respects MIN_ALNUM_COUNT' '
+       git reset --hard &&
+       cat <<-\EOF >foo &&
+       nineteen chars 456789
+       irrelevant_line
+       twenty chars 234567890
+       EOF
+       >bar &&
+       git add foo bar &&
+       git commit -m x &&
+
+       cat <<-\EOF >foo &&
+       irrelevant_line
+       EOF
+       cat <<-\EOF >bar &&
+       twenty chars 234567890
+       nineteen chars 456789
+       EOF
+
+       git diff HEAD --color-moved=zebra --color --no-renames |
+               grep -v "index" |
+               test_decode_color >actual &&
+       cat >expected <<-\EOF &&
+       <BOLD>diff --git a/bar b/bar<RESET>
+       <BOLD>--- a/bar<RESET>
+       <BOLD>+++ b/bar<RESET>
+       <CYAN>@@ -0,0 +1,2 @@<RESET>
+       <BOLD;CYAN>+<RESET><BOLD;CYAN>twenty chars 234567890<RESET>
+       <GREEN>+<RESET><GREEN>nineteen chars 456789<RESET>
+       <BOLD>diff --git a/foo b/foo<RESET>
+       <BOLD>--- a/foo<RESET>
+       <BOLD>+++ b/foo<RESET>
+       <CYAN>@@ -1,3 +1 @@<RESET>
+       <RED>-nineteen chars 456789<RESET>
+        irrelevant_line<RESET>
+       <BOLD;MAGENTA>-twenty chars 234567890<RESET>
+       EOF
+
+       test_cmp expected actual
+'
+
+test_expect_success '--color-moved treats adjacent blocks as separate for MIN_ALNUM_COUNT' '
+       git reset --hard &&
+       cat <<-\EOF >foo &&
+       7charsA
+       irrelevant_line
+       7charsB
+       7charsC
+       EOF
+       >bar &&
+       git add foo bar &&
+       git commit -m x &&
+
+       cat <<-\EOF >foo &&
+       irrelevant_line
+       EOF
+       cat <<-\EOF >bar &&
+       7charsB
+       7charsC
+       7charsA
+       EOF
+
+       git diff HEAD --color-moved=zebra --color --no-renames | grep -v "index" | test_decode_color >actual &&
+       cat >expected <<-\EOF &&
+       <BOLD>diff --git a/bar b/bar<RESET>
+       <BOLD>--- a/bar<RESET>
+       <BOLD>+++ b/bar<RESET>
+       <CYAN>@@ -0,0 +1,3 @@<RESET>
+       <GREEN>+<RESET><GREEN>7charsB<RESET>
+       <GREEN>+<RESET><GREEN>7charsC<RESET>
+       <GREEN>+<RESET><GREEN>7charsA<RESET>
+       <BOLD>diff --git a/foo b/foo<RESET>
+       <BOLD>--- a/foo<RESET>
+       <BOLD>+++ b/foo<RESET>
+       <CYAN>@@ -1,4 +1 @@<RESET>
+       <RED>-7charsA<RESET>
+        irrelevant_line<RESET>
+       <RED>-7charsB<RESET>
+       <RED>-7charsC<RESET>
+       EOF
+
+       test_cmp expected actual
+'
+
+test_expect_success 'move detection with submodules' '
+       test_create_repo bananas &&
+       echo ripe >bananas/recipe &&
+       git -C bananas add recipe &&
+       test_commit fruit &&
+       test_commit -C bananas recipe &&
+       git submodule add ./bananas &&
+       git add bananas &&
+       git commit -a -m "bananas are like a heavy library?" &&
+       echo foul >bananas/recipe &&
+       echo ripe >fruit.t &&
+
+       git diff --submodule=diff --color-moved --color >actual &&
+
+       # no move detection as the moved line is across repository boundaries.
+       test_decode_color <actual >decoded_actual &&
+       ! grep BGREEN decoded_actual &&
+       ! grep BRED decoded_actual &&
+
+       # nor did we mess with it another way
+       git diff --submodule=diff --color | test_decode_color >expect &&
+       test_cmp expect decoded_actual
+'
+
 test_done
index 518bf9524e0b55181f3c440d202b69c12a0b314f..2ffd11a142d8c1d2f532f662951a27a90119a76e 100755 (executable)
@@ -113,35 +113,6 @@ test_expect_success 'git diff HEAD with dirty submodule (work tree, refs match)'
        ! test -s actual4
 '
 
-test_expect_success 'git diff HEAD with dirty submodule (work tree, refs match) [.git/config]' '
-       git config diff.ignoreSubmodules all &&
-       git diff HEAD >actual &&
-       ! test -s actual &&
-       git config submodule.subname.ignore none &&
-       git config submodule.subname.path sub &&
-       git diff HEAD >actual &&
-       sed -e "1,/^@@/d" actual >actual.body &&
-       expect_from_to >expect.body $subprev $subprev-dirty &&
-       test_cmp expect.body actual.body &&
-       git config submodule.subname.ignore all &&
-       git diff HEAD >actual2 &&
-       ! test -s actual2 &&
-       git config submodule.subname.ignore untracked &&
-       git diff HEAD >actual3 &&
-       sed -e "1,/^@@/d" actual3 >actual3.body &&
-       expect_from_to >expect.body $subprev $subprev-dirty &&
-       test_cmp expect.body actual3.body &&
-       git config submodule.subname.ignore dirty &&
-       git diff HEAD >actual4 &&
-       ! test -s actual4 &&
-       git diff HEAD --ignore-submodules=none >actual &&
-       sed -e "1,/^@@/d" actual >actual.body &&
-       expect_from_to >expect.body $subprev $subprev-dirty &&
-       test_cmp expect.body actual.body &&
-       git config --remove-section submodule.subname &&
-       git config --unset diff.ignoreSubmodules
-'
-
 test_expect_success 'git diff HEAD with dirty submodule (work tree, refs match) [.gitmodules]' '
        git config diff.ignoreSubmodules dirty &&
        git diff HEAD >actual &&
@@ -208,24 +179,6 @@ test_expect_success 'git diff HEAD with dirty submodule (untracked, refs match)'
        ! test -s actual4
 '
 
-test_expect_success 'git diff HEAD with dirty submodule (untracked, refs match) [.git/config]' '
-       git config submodule.subname.ignore all &&
-       git config submodule.subname.path sub &&
-       git diff HEAD >actual2 &&
-       ! test -s actual2 &&
-       git config submodule.subname.ignore untracked &&
-       git diff HEAD >actual3 &&
-       ! test -s actual3 &&
-       git config submodule.subname.ignore dirty &&
-       git diff HEAD >actual4 &&
-       ! test -s actual4 &&
-       git diff --ignore-submodules=none HEAD >actual &&
-       sed -e "1,/^@@/d" actual >actual.body &&
-       expect_from_to >expect.body $subprev $subprev-dirty &&
-       test_cmp expect.body actual.body &&
-       git config --remove-section submodule.subname
-'
-
 test_expect_success 'git diff HEAD with dirty submodule (untracked, refs match) [.gitmodules]' '
        git config --add -f .gitmodules submodule.subname.ignore all &&
        git config --add -f .gitmodules submodule.subname.path sub &&
@@ -261,26 +214,6 @@ test_expect_success 'git diff between submodule commits' '
        ! test -s actual
 '
 
-test_expect_success 'git diff between submodule commits [.git/config]' '
-       git diff HEAD^..HEAD >actual &&
-       sed -e "1,/^@@/d" actual >actual.body &&
-       expect_from_to >expect.body $subtip $subprev &&
-       test_cmp expect.body actual.body &&
-       git config submodule.subname.ignore dirty &&
-       git config submodule.subname.path sub &&
-       git diff HEAD^..HEAD >actual &&
-       sed -e "1,/^@@/d" actual >actual.body &&
-       expect_from_to >expect.body $subtip $subprev &&
-       test_cmp expect.body actual.body &&
-       git config submodule.subname.ignore all &&
-       git diff HEAD^..HEAD >actual &&
-       ! test -s actual &&
-       git diff --ignore-submodules=dirty HEAD^..HEAD >actual &&
-       sed -e "1,/^@@/d" actual >actual.body &&
-       expect_from_to >expect.body $subtip $subprev &&
-       git config --remove-section submodule.subname
-'
-
 test_expect_success 'git diff between submodule commits [.gitmodules]' '
        git diff HEAD^..HEAD >actual &&
        sed -e "1,/^@@/d" actual >actual.body &&
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 1a080e782371e2d7ad489c706a54fce1553b9579..d97d2bebc9850c8ba96a8263ed081fced2ecc153 100755 (executable)
@@ -239,6 +239,43 @@ test_expect_success 'old records rest in peace' '
        ! test -f $rr2/preimage
 '
 
+rerere_gc_custom_expiry_test () {
+       five_days="$1" right_now="$2"
+       test_expect_success "rerere gc with custom expiry ($five_days, $right_now)" '
+               rm -fr .git/rr-cache &&
+               rr=.git/rr-cache/$_z40 &&
+               mkdir -p "$rr" &&
+               >"$rr/preimage" &&
+               >"$rr/postimage" &&
+
+               two_days_ago=$((-2*86400)) &&
+               test-chmtime =$two_days_ago "$rr/preimage" &&
+               test-chmtime =$two_days_ago "$rr/postimage" &&
+
+               find .git/rr-cache -type f | sort >original &&
+
+               git -c "gc.rerereresolved=$five_days" \
+                   -c "gc.rerereunresolved=$five_days" rerere gc &&
+               find .git/rr-cache -type f | sort >actual &&
+               test_cmp original actual &&
+
+               git -c "gc.rerereresolved=$five_days" \
+                   -c "gc.rerereunresolved=$right_now" rerere gc &&
+               find .git/rr-cache -type f | sort >actual &&
+               test_cmp original actual &&
+
+               git -c "gc.rerereresolved=$right_now" \
+                   -c "gc.rerereunresolved=$right_now" rerere gc &&
+               find .git/rr-cache -type f | sort >actual &&
+               >expect &&
+               test_cmp expect actual
+       '
+}
+
+rerere_gc_custom_expiry_test 5 0
+
+rerere_gc_custom_expiry_test 5.days.ago now
+
 test_expect_success 'setup: file2 added differently in two branches' '
        git reset --hard &&
 
@@ -419,24 +456,6 @@ count_pre_post () {
        test_line_count = "$2" actual
 }
 
-test_expect_success 'rerere gc' '
-       find .git/rr-cache -type f >original &&
-       xargs test-chmtime -172800 <original &&
-
-       git -c gc.rerereresolved=5 -c gc.rerereunresolved=5 rerere gc &&
-       find .git/rr-cache -type f >actual &&
-       test_cmp original actual &&
-
-       git -c gc.rerereresolved=5 -c gc.rerereunresolved=0 rerere gc &&
-       find .git/rr-cache -type f >actual &&
-       test_cmp original actual &&
-
-       git -c gc.rerereresolved=0 -c gc.rerereunresolved=0 rerere gc &&
-       find .git/rr-cache -type f >actual &&
-       >expect &&
-       test_cmp expect actual
-'
-
 merge_conflict_resolve () {
        git reset --hard &&
        test_must_fail git merge six.1 &&
@@ -446,6 +465,8 @@ merge_conflict_resolve () {
 }
 
 test_expect_success 'multiple identical conflicts' '
+       rm -fr .git/rr-cache &&
+       mkdir .git/rr-cache &&
        git reset --hard &&
 
        test_seq 1 6 >early &&
index 36d120c969f6c116c2bed19c8bb0c027919d66a9..8f155da7a50a657db9fd092e24639f67cd03cd24 100755 (executable)
@@ -750,7 +750,7 @@ test_expect_success 'log.decorate config parsing' '
 '
 
 test_expect_success TTY 'log output on a TTY' '
-       git log --oneline --decorate >expect.short &&
+       git log --color --oneline --decorate >expect.short &&
 
        test_terminal git log --oneline >actual &&
        test_cmp expect.short actual
index 18aa1b5889749e706cba70c2a4f2633bcc9c9eb4..591f35daaf5825cfdcf8e7fccf4f52462bbf8f78 100755 (executable)
@@ -539,25 +539,62 @@ cat >trailers <<EOF
 Signed-off-by: A U Thor <author@example.com>
 Acked-by: A U Thor <author@example.com>
 [ v2 updated patch description ]
-Signed-off-by: A U Thor <author@example.com>
+Signed-off-by: A U Thor
+  <author@example.com>
 EOF
 
-test_expect_success 'pretty format %(trailers) shows trailers' '
+unfold () {
+       perl -0pe 's/\n\s+/ /g'
+}
+
+test_expect_success 'set up trailer tests' '
        echo "Some contents" >trailerfile &&
        git add trailerfile &&
-       git commit -F - <<-EOF &&
+       git commit -F - <<-EOF
        trailers: this commit message has trailers
 
        This commit is a test commit with trailers at the end. We parse this
-       message and display the trailers using %bT
+       message and display the trailers using %(trailers).
 
        $(cat trailers)
        EOF
+'
+
+test_expect_success 'pretty format %(trailers) shows trailers' '
        git log --no-walk --pretty="%(trailers)" >actual &&
-       cat >expect <<-EOF &&
-       $(cat trailers)
+       {
+               cat trailers &&
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
 
-       EOF
+test_expect_success '%(trailers:only) shows only "key: value" trailers' '
+       git log --no-walk --pretty="%(trailers:only)" >actual &&
+       {
+               grep -v patch.description <trailers &&
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '%(trailers:unfold) unfolds trailers' '
+       git log --no-walk --pretty="%(trailers:unfold)" >actual &&
+       {
+               unfold <trailers &&
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success ':only and :unfold work together' '
+       git log --no-walk --pretty="%(trailers:only,unfold)" >actual &&
+       git log --no-walk --pretty="%(trailers:unfold,only)" >reverse &&
+       test_cmp actual reverse &&
+       {
+               grep -v patch.description <trailers | unfold &&
+               echo
+       } >expect &&
        test_cmp expect actual
 '
 
index 897f6f06d55a9790a237b75c12eaab95763d1864..e9aa97117ab41beab708f4ded34628b015021e4e 100755 (executable)
@@ -73,7 +73,7 @@ test_expect_missing   archive-pathspec/ignored-by-tree
 test_expect_missing    archive-pathspec/ignored-by-tree.d
 test_expect_missing    archive-pathspec/ignored-by-tree.d/file
 test_expect_exists     archive-pathspec/ignored-by-worktree
-test_expect_missing    archive-pathspec/excluded-by-pathspec.d failure
+test_expect_missing    archive-pathspec/excluded-by-pathspec.d
 test_expect_missing    archive-pathspec/excluded-by-pathspec.d/file
 
 test_expect_success 'git archive with wildcard pathspec' '
index 6667d159ab0950f10a3271f1cb81a63953214f17..bda6d7d7e9e835213f9b1323407222850db47ad6 100755 (executable)
@@ -76,7 +76,7 @@ test_expect_missing   archive/deep/and/slashless/ &&
 test_expect_missing    archive/deep/and/slashless/foo &&
 test_expect_missing    archive/deep/with/wildcard/ &&
 test_expect_missing    archive/deep/with/wildcard/foo &&
-test_expect_exists     archive/one-level-lower/
+test_expect_missing    archive/one-level-lower/
 test_expect_missing    archive/one-level-lower/two-levels-lower/ignored-only-if-dir/
 test_expect_missing    archive/one-level-lower/two-levels-lower/ignored-ony-if-dir/ignored-by-ignored-dir
 
index f6207f42b5a4ed382b819cb81631cd0acab61463..ced44355cab99fc4f5fd9768daf10f5a2c1b21c7 100755 (executable)
@@ -108,14 +108,14 @@ test_expect_success 'archive empty subtree with no pathspec' '
        git archive --format=tar $root_tree >subtree-all.tar &&
        make_dir extract &&
        "$TAR" xf subtree-all.tar -C extract &&
-       check_dir extract sub
+       check_dir extract
 '
 
 test_expect_success 'archive empty subtree by direct pathspec' '
        git archive --format=tar $root_tree -- sub >subtree-path.tar &&
        make_dir extract &&
        "$TAR" xf subtree-path.tar -C extract &&
-       check_dir extract sub
+       check_dir extract
 '
 
 ZIPINFO=zipinfo
index 82c33b88e710b5fb6163a7bdcd9ecb783633aa83..08c210f03586ab8cd26d69cb0761180ef5588f58 100755 (executable)
@@ -68,7 +68,7 @@ test_expect_success 'setup: two scripts for reading pull requests' '
        cat <<-\EOT >read-request.sed &&
        #!/bin/sed -nf
        # Note that a request could ask for "tag $tagname"
-       / in the git repository at:$/!d
+       / in the Git repository at:$/!d
        n
        /^$/ n
        s/ tag \([^ ]*\)$/ tag--\1/
@@ -192,7 +192,7 @@ test_expect_success 'pull request format' '
 
          SUBJECT (DATE)
 
-       are available in the git repository at:
+       are available in the Git repository at:
 
          URL BRANCH
 
index 133b5842b1aee8e8e00eb0ca7e1b2d32ff4a4c9a..6694c19a1eecf10117b843bbacbc5bb47924c9c8 100755 (executable)
@@ -283,4 +283,41 @@ test_expect_success 'prune: handle alternate object database' '
        git -C B prune
 '
 
+test_expect_success 'prune: handle index in multiple worktrees' '
+       git worktree add second-worktree &&
+       echo "new blob for second-worktree" >second-worktree/blob &&
+       git -C second-worktree add blob &&
+       git prune --expire=now &&
+       git -C second-worktree show :blob >actual &&
+       test_cmp second-worktree/blob actual
+'
+
+test_expect_success 'prune: handle HEAD in multiple worktrees' '
+       git worktree add --detach third-worktree &&
+       echo "new blob for third-worktree" >third-worktree/blob &&
+       git -C third-worktree add blob &&
+       git -C third-worktree commit -m "third" &&
+       rm .git/worktrees/third-worktree/index &&
+       test_must_fail git -C third-worktree show :blob &&
+       git prune --expire=now &&
+       git -C third-worktree show HEAD:blob >actual &&
+       test_cmp third-worktree/blob actual
+'
+
+test_expect_success 'prune: handle HEAD reflog in multiple worktrees' '
+       git config core.logAllRefUpdates true &&
+       echo "lost blob for third-worktree" >expected &&
+       (
+               cd third-worktree &&
+               cat ../expected >blob &&
+               git add blob &&
+               git commit -m "second commit in third" &&
+               git reset --hard HEAD^
+       ) &&
+       git prune --expire=now &&
+       SHA1=`git hash-object expected` &&
+       git -C third-worktree show "$SHA1" >actual &&
+       test_cmp expected actual
+'
+
 test_done
index 9c5a8766ab0af40382b83cf3340b13a32f7f0c8e..156ae9e9d335029e63430a6cc7d0ec2fd0d68dd3 100755 (executable)
@@ -56,20 +56,11 @@ test_expect_success 'create batch-check test vectors' '
        EOF
 '
 
-test_expect_success 'lookup in duplicated pack (binary search)' '
+test_expect_success 'lookup in duplicated pack' '
        git cat-file --batch-check <input >actual &&
        test_cmp expect actual
 '
 
-test_expect_success 'lookup in duplicated pack (GIT_USE_LOOKUP)' '
-       (
-               GIT_USE_LOOKUP=1 &&
-               export GIT_USE_LOOKUP &&
-               git cat-file --batch-check <input >actual
-       ) &&
-       test_cmp expect actual
-'
-
 test_expect_success 'index-pack can reject packs with duplicates' '
        clear_packs &&
        create_pack dups.pack 2 &&
index f15f7a332960f15c7af32a3c1e7a66f5a8a68989..59c4b778d3a455b6ef4575a4b07b860d1e84720d 100755 (executable)
@@ -305,7 +305,7 @@ test_expect_success '--rebase with conflicts shows advice' '
        test_tick &&
        git commit -m "Create conflict" seq.txt &&
        test_must_fail git pull --rebase . seq 2>err >out &&
-       test_i18ngrep "When you have resolved this problem" out
+       test_i18ngrep "Resolve all conflicts manually" out
 '
 
 test_expect_success 'failed --rebase shows advice' '
@@ -319,7 +319,7 @@ test_expect_success 'failed --rebase shows advice' '
        git checkout -f -b fails-to-rebase HEAD^ &&
        test_commit v2-without-cr file "2" file2-lf &&
        test_must_fail git pull --rebase . diverging 2>err >out &&
-       test_i18ngrep "When you have resolved this problem" out
+       test_i18ngrep "Resolve all conflicts manually" out
 '
 
 test_expect_success '--rebase fails with multiple branches' '
index 0f84a53146f3bac1bded4d2b11212b8ddef1bf2c..39cb2c1c3489c25545ab3cd69706c0a21378c6b7 100755 (executable)
@@ -298,6 +298,16 @@ test_expect_success 'push succeeds if submodule commit disabling recursion from
        )
 '
 
+test_expect_success 'submodule entry pointing at a tag is error' '
+       git -C work/gar/bage tag -a test1 -m "tag" &&
+       tag=$(git -C work/gar/bage rev-parse test1^{tag}) &&
+       git -C work update-index --cacheinfo 160000 "$tag" gar/bage &&
+       git -C work commit -m "bad commit" &&
+       test_when_finished "git -C work reset --hard HEAD^" &&
+       test_must_fail git -C work push --recurse-submodules=on-demand ../pub.git master 2>err &&
+       test_i18ngrep "is a tag, not a commit" err
+'
+
 test_expect_success 'push fails if recurse submodules option passed as yes' '
        (
                cd work/gar/bage &&
index 464ffdd147abbae354ae3adfe593b89a37d5c726..1cea758f789edef35a61ce1a9bc11d9fda7bd7ac 100755 (executable)
@@ -71,6 +71,13 @@ test_expect_success 'push --signed fails with a receiver without push certificat
        test_i18ngrep "the receiving end does not support" err
 '
 
+test_expect_success 'push --signed=1 is accepted' '
+       prepare_dst &&
+       mkdir -p dst/.git/hooks &&
+       test_must_fail git push --signed=1 dst noop ff +noff 2>err &&
+       test_i18ngrep "the receiving end does not support" err
+'
+
 test_expect_success GPG 'no certificate for a signed push with no update' '
        prepare_dst &&
        mkdir -p dst/.git/hooks &&
index 077eb07e11594838f9850b4879bbbdeafd3b0c8e..321bd37deb3d32027c18f8184a9ad46aec9c96c7 100755 (executable)
@@ -65,6 +65,38 @@ test_expect_success 'recursive pull updates working tree' '
        test_path_is_file super/sub/merge_strategy.t
 '
 
+test_expect_success "submodule.recurse option triggers recursive pull" '
+       test_commit -C child merge_strategy_2 &&
+       git -C parent submodule update --remote &&
+       git -C parent add sub &&
+       git -C parent commit -m "update submodule" &&
+
+       git -C super -c submodule.recurse pull --no-rebase &&
+       test_path_is_file super/sub/merge_strategy_2.t
+'
+
+test_expect_success " --[no-]recurse-submodule and submodule.recurse" '
+       test_commit -C child merge_strategy_3 &&
+       git -C parent submodule update --remote &&
+       git -C parent add sub &&
+       git -C parent commit -m "update submodule" &&
+
+       git -C super -c submodule.recurse pull --no-recurse-submodules --no-rebase &&
+       test_path_is_missing super/sub/merge_strategy_3.t &&
+       git -C super -c submodule.recurse=false pull --recurse-submodules --no-rebase &&
+       test_path_is_file super/sub/merge_strategy_3.t &&
+
+       test_commit -C child merge_strategy_4 &&
+       git -C parent submodule update --remote &&
+       git -C parent add sub &&
+       git -C parent commit -m "update submodule" &&
+
+       git -C super -c submodule.recurse=false pull --no-recurse-submodules --no-rebase &&
+       test_path_is_missing super/sub/merge_strategy_4.t &&
+       git -C super -c submodule.recurse=true pull --recurse-submodules --no-rebase &&
+       test_path_is_file super/sub/merge_strategy_4.t
+'
+
 test_expect_success 'recursive rebasing pull' '
        # change upstream
        test_commit -C child rebase_strategy &&
index 534903bbd2972301965a3d7eed6fa8e380eeeb86..a6614080388353c828fdd3894c093ef03f71511d 100755 (executable)
@@ -236,17 +236,31 @@ test_sequence "--bisect"
 #
 #
 
-test_expect_success '--bisect can default to good/bad refs' '
+test_expect_success 'set up fake --bisect refs' '
        git update-ref refs/bisect/bad c3 &&
        good=$(git rev-parse b1) &&
        git update-ref refs/bisect/good-$good $good &&
        good=$(git rev-parse c1) &&
-       git update-ref refs/bisect/good-$good $good &&
+       git update-ref refs/bisect/good-$good $good
+'
 
+test_expect_success 'rev-list --bisect can default to good/bad refs' '
        # the only thing between c3 and c1 is c2
        git rev-parse c2 >expect &&
        git rev-list --bisect >actual &&
        test_cmp expect actual
 '
 
+test_expect_success 'rev-parse --bisect can default to good/bad refs' '
+       git rev-parse c3 ^b1 ^c1 >expect &&
+       git rev-parse --bisect >actual &&
+
+       # output order depends on the refnames, which in turn depends on
+       # the exact sha1s. We just want to make sure we have the same set
+       # of lines in any order.
+       sort <expect >expect.sorted &&
+       sort <actual >actual.sorted &&
+       test_cmp expect.sorted actual.sorted
+'
+
 test_done
index b326d550f3e281e4b19ab669b95f08e8c63bf555..25a9c65dc51926e3520c9b5f7e1fec709acede10 100755 (executable)
@@ -208,29 +208,13 @@ do
                has_no_color actual
        '
 
-       test_expect_success "$desc enables colors for color.diff" '
-               git -c color.diff=always log --format=$color -1 >actual &&
-               has_color actual
-       '
-
-       test_expect_success "$desc enables colors for color.ui" '
-               git -c color.ui=always log --format=$color -1 >actual &&
-               has_color actual
-       '
-
        test_expect_success "$desc respects --color" '
                git log --format=$color -1 --color >actual &&
                has_color actual
        '
 
-       test_expect_success "$desc respects --no-color" '
-               git -c color.ui=always log --format=$color -1 --no-color >actual &&
-               has_no_color actual
-       '
-
        test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
-               test_terminal env TERM=vt100 \
-                       git log --format=$color -1 --color=auto >actual &&
+               test_terminal git log --format=$color -1 --color=auto >actual &&
                has_color actual
        '
 
@@ -241,6 +225,11 @@ do
                        has_no_color actual
                )
        '
+
+       test_expect_success TTY "$desc respects --no-color" '
+               test_terminal git log --format=$color -1 --no-color >actual &&
+               has_no_color actual
+       '
 done
 
 test_expect_success '%C(always,...) enables color even without tty' '
index 97a07655a0e0d74747259498bfbb8d03ea6c7480..8f17fd9da8ef6b54ae0a97500f2154b1c9d968b3 100755 (executable)
@@ -100,7 +100,7 @@ test_expect_success 'checkout (up-to-date with upstream)' '
        (
                cd test && git checkout b6
        ) >actual &&
-       test_i18ngrep "Your branch is up-to-date with .origin/master" actual
+       test_i18ngrep "Your branch is up to date with .origin/master" actual
 '
 
 test_expect_success 'status (diverged from upstream)' '
@@ -130,7 +130,7 @@ test_expect_success 'status (up-to-date with upstream)' '
                # reports nothing to commit
                test_must_fail git commit --dry-run
        ) >actual &&
-       test_i18ngrep "Your branch is up-to-date with .origin/master" actual
+       test_i18ngrep "Your branch is up to date with .origin/master" actual
 '
 
 cat >expect <<\EOF
@@ -188,35 +188,29 @@ test_expect_success 'fail to track annotated tags' '
        test_must_fail git checkout heavytrack
 '
 
-test_expect_success 'setup tracking with branch --set-upstream on existing branch' '
+test_expect_success '--set-upstream-to does not change branch' '
        git branch from-master master &&
-       test_must_fail git config branch.from-master.merge > actual &&
-       git branch --set-upstream from-master master &&
-       git config branch.from-master.merge > actual &&
-       grep -q "^refs/heads/master$" actual
-'
-
-test_expect_success '--set-upstream does not change branch' '
+       git branch --set-upstream-to master from-master &&
        git branch from-master2 master &&
        test_must_fail git config branch.from-master2.merge > actual &&
        git rev-list from-master2 &&
        git update-ref refs/heads/from-master2 from-master2^ &&
        git rev-parse from-master2 >expect2 &&
-       git branch --set-upstream from-master2 master &&
+       git branch --set-upstream-to master from-master2 &&
        git config branch.from-master.merge > actual &&
        git rev-parse from-master2 >actual2 &&
        grep -q "^refs/heads/master$" actual &&
        cmp expect2 actual2
 '
 
-test_expect_success '--set-upstream @{-1}' '
-       git checkout from-master &&
+test_expect_success '--set-upstream-to @{-1}' '
+       git checkout follower &&
        git checkout from-master2 &&
        git config branch.from-master2.merge > expect2 &&
-       git branch --set-upstream @{-1} follower &&
+       git branch --set-upstream-to @{-1} from-master &&
        git config branch.from-master.merge > actual &&
        git config branch.from-master2.merge > actual2 &&
-       git branch --set-upstream from-master follower &&
+       git branch --set-upstream-to follower from-master &&
        git config branch.from-master.merge > expect &&
        test_cmp expect2 actual2 &&
        test_cmp expect actual
index aa74eb8f0d5dfb4f228b1e8c7b9dbbc0b88adb4d..1c0e8659d97c0792ce5761cf0704aa7f061575e0 100755 (executable)
@@ -182,10 +182,41 @@ check_describe "test2-lightweight-*" --tags --match="test2-*"
 
 check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
 
-check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
+check_describe "test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
 
 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
 
+check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
+
+check_describe "test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
+
+test_expect_success 'set-up branches' '
+       git branch branch_A A &&
+       git branch branch_C c &&
+       git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
+       git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
+       git update-ref refs/original/original_branch_A test-annotated~2
+'
+
+check_describe "heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
+
+check_describe "remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
+
+check_describe "original/original_branch_A*" --all test-annotated~1
+
+test_expect_success '--match does not work for other types' '
+       test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
+'
+
+test_expect_success '--exclude does not work for other types' '
+       R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
+       case "$R" in
+       *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
+               false;;
+       *) echo ok: Found some known type;;
+       esac
+'
+
 test_expect_success 'name-rev with exact tags' '
        echo A >expect &&
        tag_object=$(git rev-parse refs/tags/A) &&
@@ -198,6 +229,31 @@ test_expect_success 'name-rev with exact tags' '
        test_cmp expect actual
 '
 
+test_expect_success 'name-rev --all' '
+       >expect.unsorted &&
+       for rev in $(git rev-list --all)
+       do
+               git name-rev $rev >>expect.unsorted
+       done &&
+       sort <expect.unsorted >expect &&
+       git name-rev --all >actual.unsorted &&
+       sort <actual.unsorted >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'name-rev --stdin' '
+       >expect.unsorted &&
+       for rev in $(git rev-list --all)
+       do
+               name=$(git name-rev --name-only $rev) &&
+               echo "$rev ($name)" >>expect.unsorted
+       done &&
+       sort <expect.unsorted >expect &&
+       git rev-list --all | git name-rev --stdin >actual.unsorted &&
+       sort <actual.unsorted >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'describe --contains with the exact tags' '
        echo "A^0" >expect &&
        tag_object=$(git rev-parse refs/tags/A) &&
@@ -250,7 +306,38 @@ test_expect_success 'describe chokes on severely broken submodules' '
 '
 test_expect_success 'describe ignoring a borken submodule' '
        git describe --broken >out &&
+       test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
        grep broken out
 '
 
+test_expect_failure ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
+       i=1 &&
+       while test $i -lt 8000
+       do
+               echo "commit refs/heads/master
+committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
+data <<EOF
+commit #$i
+EOF"
+               test $i = 1 && echo "from refs/heads/master^0"
+               i=$(($i + 1))
+       done | git fast-import &&
+       git checkout master &&
+       git tag far-far-away HEAD^ &&
+       echo "HEAD~4000 tags/far-far-away~3999" >expect &&
+       git name-rev HEAD~4000 >actual &&
+       test_cmp expect actual &&
+       run_with_limited_stack git name-rev HEAD~4000 >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success ULIMIT_STACK_SIZE 'describe works in a deep repo' '
+       git tag -f far-far-away HEAD~7999 &&
+       echo "far-far-away" >expect &&
+       git describe --tags --abbrev=0 HEAD~4000 >actual &&
+       test_cmp expect actual &&
+       run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
+       test_cmp expect actual
+'
+
 test_done
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 2274a4b73310bfbc90886e0179a1cb12d9b6f3ed..416ff7d0b832d2898173dd2ac83201cf8e02955b 100755 (executable)
@@ -51,6 +51,7 @@ test_atom() {
 }
 
 test_atom head refname refs/heads/master
+test_atom head refname: refs/heads/master
 test_atom head refname:short master
 test_atom head refname:lstrip=1 heads/master
 test_atom head refname:lstrip=2 master
@@ -425,8 +426,7 @@ test_expect_success 'set up color tests' '
 '
 
 test_expect_success TTY '%(color) shows color with a tty' '
-       test_terminal env TERM=vt100 \
-               git for-each-ref --format="$color_format" >actual.raw &&
+       test_terminal git for-each-ref --format="$color_format" >actual.raw &&
        test_decode_color <actual.raw >actual &&
        test_cmp expected.color actual
 '
@@ -436,8 +436,8 @@ test_expect_success '%(color) does not show color without tty' '
        test_cmp expected.bare actual
 '
 
-test_expect_success 'color.ui=always can override tty check' '
-       git -c color.ui=always for-each-ref --format="$color_format" >actual.raw &&
+test_expect_success '--color can override tty check' '
+       git for-each-ref --color --format="$color_format" >actual.raw &&
        test_decode_color <actual.raw >actual &&
        test_cmp expected.color actual
 '
@@ -605,18 +605,104 @@ test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
 cat >trailers <<EOF
 Reviewed-by: A U Thor <author@example.com>
 Signed-off-by: A U Thor <author@example.com>
+[ v2 updated patch description ]
+Acked-by: A U Thor
+  <author@example.com>
 EOF
 
-test_expect_success 'basic atom: head contents:trailers' '
+unfold () {
+       perl -0pe 's/\n\s+/ /g'
+}
+
+test_expect_success 'set up trailers for next test' '
        echo "Some contents" > two &&
        git add two &&
-       git commit -F - <<-EOF &&
+       git commit -F - <<-EOF
        trailers: this commit message has trailers
 
        Some message contents
 
        $(cat trailers)
        EOF
+'
+
+test_expect_success '%(trailers:unfold) unfolds trailers' '
+       git for-each-ref --format="%(trailers:unfold)" refs/heads/master >actual &&
+       {
+               unfold <trailers
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '%(trailers:only) shows only "key: value" trailers' '
+       git for-each-ref --format="%(trailers:only)" refs/heads/master >actual &&
+       {
+               grep -v patch.description <trailers &&
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
+       git for-each-ref --format="%(trailers:only,unfold)" refs/heads/master >actual &&
+       git for-each-ref --format="%(trailers:unfold,only)" refs/heads/master >reverse &&
+       test_cmp actual reverse &&
+       {
+               grep -v patch.description <trailers | unfold &&
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '%(contents:trailers:unfold) unfolds trailers' '
+       git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/master >actual &&
+       {
+               unfold <trailers
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '%(contents:trailers:only) shows only "key: value" trailers' '
+       git for-each-ref --format="%(contents:trailers:only)" refs/heads/master >actual &&
+       {
+               grep -v patch.description <trailers &&
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '%(contents:trailers:only) and %(contents:trailers:unfold) work together' '
+       git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/master >actual &&
+       git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/master >reverse &&
+       test_cmp actual reverse &&
+       {
+               grep -v patch.description <trailers | unfold &&
+               echo
+       } >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '%(trailers) rejects unknown trailers arguments' '
+       # error message cannot be checked under i18n
+       cat >expect <<-EOF &&
+       fatal: unknown %(trailers) argument: unsupported
+       EOF
+       test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
+       test_i18ncmp expect actual
+'
+
+test_expect_success '%(contents:trailers) rejects unknown trailers arguments' '
+       # error message cannot be checked under i18n
+       cat >expect <<-EOF &&
+       fatal: unknown %(trailers) argument: unsupported
+       EOF
+       test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
+       test_i18ncmp expect actual
+'
+
+test_expect_success 'basic atom: head contents:trailers' '
        git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
        sanitize_pgp <actual >actual.clean &&
        # git for-each-ref ends with a blank line
index e365d1ff77ee30e0f40b8f01c7e70ebc74e97901..cbc5fb37fe531ff9a60c236a175fd6a4f29b1ece 100755 (executable)
@@ -491,4 +491,29 @@ test_expect_success 'moving a submodule in nested directories' '
        test_cmp actual expect
 '
 
+test_expect_failure 'moving nested submodules' '
+       git commit -am "cleanup commit" &&
+       mkdir sub_nested_nested &&
+       (cd sub_nested_nested &&
+               touch nested_level2 &&
+               git init &&
+               git add . &&
+               git commit -m "nested level 2"
+       ) &&
+       mkdir sub_nested &&
+       (cd sub_nested &&
+               touch nested_level1 &&
+               git init &&
+               git add . &&
+               git commit -m "nested level 1"
+               git submodule add ../sub_nested_nested &&
+               git commit -m "add nested level 2"
+       ) &&
+       git submodule add ./sub_nested nested_move &&
+       git commit -m "add nested_move" &&
+       git submodule update --init --recursive &&
+       git mv nested_move sub_nested_moved &&
+       git status
+'
+
 test_done
index dbcd6f623c7e233b487d4d0c1dd7d71cb34059b0..4e62c505fc9ce84adf4b0f74110b39d18c571670 100755 (executable)
@@ -1863,13 +1863,6 @@ test_expect_success 'version sort with very long prerelease suffix' '
        git tag -l --sort=version:refname
 '
 
-run_with_limited_stack () {
-       (ulimit -s 128 && "$@")
-}
-
-test_lazy_prereq ULIMIT_STACK_SIZE 'run_with_limited_stack true'
-
-# we require ulimit, this excludes Windows
 test_expect_success ULIMIT_STACK_SIZE '--contains and --no-contains work in a deep repo' '
        >expect &&
        i=1 &&
@@ -1914,13 +1907,13 @@ test_expect_success '%(color) omitted without tty' '
 '
 
 test_expect_success TTY '%(color) present with tty' '
-       test_terminal env TERM=vt100 git tag $color_args >actual.raw &&
+       test_terminal git tag $color_args >actual.raw &&
        test_decode_color <actual.raw >actual &&
        test_cmp expect.color actual
 '
 
-test_expect_success 'color.ui=always overrides auto-color' '
-       git -c color.ui=always tag $color_args >actual.raw &&
+test_expect_success '--color overrides auto-color' '
+       git tag --color $color_args >actual.raw &&
        test_decode_color <actual.raw >actual &&
        test_cmp expect.color actual
 '
index 9128ec5acda3b4b2f568dee50169046a333c4527..f0f1abd1c23d4303a686310d7325cd523d6c921b 100755 (executable)
@@ -239,7 +239,7 @@ test_expect_success 'no color when stdout is a regular file' '
 test_expect_success TTY 'color when writing to a pager' '
        rm -f paginated.out &&
        test_config color.ui auto &&
-       test_terminal env TERM=vt100 git log &&
+       test_terminal git log &&
        colorful paginated.out
 '
 
@@ -247,7 +247,7 @@ test_expect_success TTY 'colors are suppressed by color.pager' '
        rm -f paginated.out &&
        test_config color.ui auto &&
        test_config color.pager false &&
-       test_terminal env TERM=vt100 git log &&
+       test_terminal git log &&
        ! colorful paginated.out
 '
 
@@ -266,7 +266,7 @@ test_expect_success 'color when writing to a file intended for a pager' '
 test_expect_success TTY 'colors are sent to pager for external commands' '
        test_config alias.externallog "!git log" &&
        test_config color.ui auto &&
-       test_terminal env TERM=vt100 git -p externallog &&
+       test_terminal git -p externallog &&
        colorful paginated.out
 '
 
index 3ae394e934d972d29286ecd25596eedd45bee902..1bf9789c8a3cfbb1cc8a6ea33e4a0b1a0da0a7cd 100755 (executable)
@@ -3,6 +3,7 @@
 test_description='git clean -i basic tests'
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
 
 test_expect_success 'setup' '
 
@@ -472,4 +473,14 @@ test_expect_success 'git clean -id with prefix and path (ask)' '
 
 '
 
+test_expect_success TTY 'git clean -i paints the header in HEADER color' '
+       >a.out &&
+       echo q |
+       test_terminal git clean -i |
+       test_decode_color |
+       head -n 1 >header &&
+       # not i18ngrep
+       grep "^<BOLD>" header
+'
+
 test_done
index e9c3335b78bb6f78c640cf496f7b055462c1e275..6f8337ffb563cf13f194186b101bbaebf0beeb35 100755 (executable)
@@ -46,16 +46,6 @@ test_expect_success 'submodule update aborts on missing gitmodules url' '
        test_must_fail git submodule init
 '
 
-test_expect_success 'configuration parsing' '
-       test_when_finished "rm -f .gitmodules" &&
-       cat >.gitmodules <<-\EOF &&
-       [submodule "s"]
-               path
-               ignore
-       EOF
-       test_must_fail git status
-'
-
 test_expect_success 'setup - repository in init subdirectory' '
        mkdir init &&
        (
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 eea36f1dbe37859c5b45ca18bb3c8f1080ff3f9a..46c09c77654597b2ee501271b4688cc6137df8f1 100755 (executable)
@@ -31,6 +31,21 @@ test_expect_success 'submodule config cache setup' '
        )
 '
 
+test_expect_success 'configuration parsing with error' '
+       test_when_finished "rm -rf repo" &&
+       test_create_repo repo &&
+       cat >repo/.gitmodules <<-\EOF &&
+       [submodule "s"]
+               path
+               ignore
+       EOF
+       (
+               cd repo &&
+               test_must_fail test-submodule-config "" s 2>actual &&
+               test_i18ngrep "bad config" actual
+       )
+'
+
 cat >super/expect <<EOF
 Submodule name: 'a' for path 'a'
 Submodule name: 'a' for path 'b'
@@ -107,78 +122,6 @@ test_expect_success 'using different treeishs works' '
        )
 '
 
-cat >super/expect_url <<EOF
-Submodule url: 'git@somewhere.else.net:a.git' for path 'b'
-Submodule url: 'git@somewhere.else.net:submodule.git' for path 'submodule'
-EOF
-
-cat >super/expect_local_path <<EOF
-Submodule name: 'a' for path 'c'
-Submodule name: 'submodule' for path 'submodule'
-EOF
-
-test_expect_success 'reading of local configuration' '
-       (cd super &&
-               old_a=$(git config submodule.a.url) &&
-               old_submodule=$(git config submodule.submodule.url) &&
-               git config submodule.a.url git@somewhere.else.net:a.git &&
-               git config submodule.submodule.url git@somewhere.else.net:submodule.git &&
-               test-submodule-config --url \
-                       "" b \
-                       "" submodule \
-                               >actual &&
-               test_cmp expect_url actual &&
-               git config submodule.a.path c &&
-               test-submodule-config \
-                       "" c \
-                       "" submodule \
-                               >actual &&
-               test_cmp expect_local_path actual &&
-               git config submodule.a.url "$old_a" &&
-               git config submodule.submodule.url "$old_submodule" &&
-               git config --unset submodule.a.path c
-       )
-'
-
-cat >super/expect_url <<EOF
-Submodule url: '../submodule' for path 'b'
-Submodule url: 'git@somewhere.else.net:submodule.git' for path 'submodule'
-EOF
-
-test_expect_success 'reading of local configuration for uninitialized submodules' '
-       (
-               cd super &&
-               git submodule deinit -f b &&
-               old_submodule=$(git config submodule.submodule.url) &&
-               git config submodule.submodule.url git@somewhere.else.net:submodule.git &&
-               test-submodule-config --url \
-                       "" b \
-                       "" submodule \
-                               >actual &&
-               test_cmp expect_url actual &&
-               git config submodule.submodule.url "$old_submodule" &&
-               git submodule init b
-       )
-'
-
-cat >super/expect_fetchrecurse_die.err <<EOF
-fatal: bad submodule.submodule.fetchrecursesubmodules argument: blabla
-EOF
-
-test_expect_success 'local error in fetchrecursesubmodule dies early' '
-       (cd super &&
-               git config submodule.submodule.fetchrecursesubmodules blabla &&
-               test_must_fail test-submodule-config \
-                       "" b \
-                       "" submodule \
-                               >actual.out 2>actual.err &&
-               touch expect_fetchrecurse_die.out &&
-               test_cmp expect_fetchrecurse_die.out actual.out  &&
-               test_cmp expect_fetchrecurse_die.err actual.err  &&
-               git config --unset submodule.submodule.fetchrecursesubmodules
-       )
-'
-
 test_expect_success 'error in history in fetchrecursesubmodule lets continue' '
        (cd super &&
                git config -f .gitmodules \
index 725687d5d5f26d6535b3f26d1fa221a8c567d57c..d33a3cb331b6c022343aff0bb7091cf8219b40df 100755 (executable)
@@ -171,9 +171,9 @@ test_expect_success 'verbose' '
 
 test_expect_success 'verbose respects diff config' '
 
-       test_config color.diff always &&
+       test_config diff.noprefix true &&
        git status -v >actual &&
-       grep "\[1mdiff --git" actual
+       grep "diff --git negative negative" actual
 '
 
 mesg_with_comment_and_newlines='
index 88d4cda2992496d8ff5643caaaccf53e90dedc3a..302a3a2082a9da593ace637a2c32d8f4cf992cb3 100755 (executable)
@@ -101,6 +101,10 @@ cat > "$HOOK" <<EOF
 exit 1
 EOF
 
+commit_msg_is () {
+       test "$(git log --pretty=format:%s%b -1)" = "$1"
+}
+
 test_expect_success 'with failing hook' '
 
        echo "another" >> file &&
@@ -135,6 +139,32 @@ test_expect_success '--no-verify with failing hook (editor)' '
 
 '
 
+test_expect_success 'merge fails with failing hook' '
+
+       test_when_finished "git branch -D newbranch" &&
+       test_when_finished "git checkout -f master" &&
+       git checkout --orphan newbranch &&
+       : >file2 &&
+       git add file2 &&
+       git commit --no-verify file2 -m in-side-branch &&
+       test_must_fail git merge --allow-unrelated-histories master &&
+       commit_msg_is "in-side-branch" # HEAD before merge
+
+'
+
+test_expect_success 'merge bypasses failing hook with --no-verify' '
+
+       test_when_finished "git branch -D newbranch" &&
+       test_when_finished "git checkout -f master" &&
+       git checkout --orphan newbranch &&
+       : >file2 &&
+       git add file2 &&
+       git commit --no-verify file2 -m in-side-branch &&
+       git merge --no-verify --allow-unrelated-histories master &&
+       commit_msg_is "Merge branch '\''master'\'' into newbranch"
+'
+
+
 chmod -x "$HOOK"
 test_expect_success POSIXPERM 'with non-executable hook' '
 
@@ -178,10 +208,6 @@ exit 0
 EOF
 chmod +x "$HOOK"
 
-commit_msg_is () {
-       test "$(git log --pretty=format:%s%b -1)" = "$1"
-}
-
 test_expect_success 'hook edits commit message' '
 
        echo "additional" >> file &&
@@ -217,7 +243,36 @@ test_expect_success "hook doesn't edit commit message (editor)" '
        echo "more plus" > FAKE_MSG &&
        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
        commit_msg_is "more plus"
+'
 
+test_expect_success 'hook called in git-merge picks up commit message' '
+       test_when_finished "git branch -D newbranch" &&
+       test_when_finished "git checkout -f master" &&
+       git checkout --orphan newbranch &&
+       : >file2 &&
+       git add file2 &&
+       git commit --no-verify file2 -m in-side-branch &&
+       git merge --allow-unrelated-histories master &&
+       commit_msg_is "new message"
+'
+
+test_expect_failure 'merge --continue remembers --no-verify' '
+       test_when_finished "git branch -D newbranch" &&
+       test_when_finished "git checkout -f master" &&
+       git checkout master &&
+       echo a >file2 &&
+       git add file2 &&
+       git commit --no-verify -m "add file2 to master" &&
+       git checkout -b newbranch master^ &&
+       echo b >file2 &&
+       git add file2 &&
+       git commit --no-verify file2 -m in-side-branch &&
+       git merge --no-verify -m not-rewritten-by-hook master &&
+       # resolve conflict:
+       echo c >file2 &&
+       git add file2 &&
+       git merge --continue &&
+       commit_msg_is not-rewritten-by-hook
 '
 
 # set up fake editor to replace `pick` by `reword`
@@ -237,4 +292,5 @@ test_expect_success 'hook is called for reword during `rebase -i`' '
 
 '
 
+
 test_done
index 43d19a9b2292033fd0297afb7df162efa2eb5621..50052e28727dab74037a115003b6083256d7f344 100755 (executable)
@@ -6,6 +6,7 @@
 test_description='git status'
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
 
 test_expect_success 'status -h in broken repository' '
        git config --global advice.statusuoption false &&
@@ -667,7 +668,7 @@ test_expect_success 'setup unique colors' '
 
 '
 
-test_expect_success 'status with color.ui' '
+test_expect_success TTY 'status with color.ui' '
        cat >expect <<\EOF &&
 On branch <GREEN>master<RESET>
 Your branch and '\''upstream'\'' have diverged,
@@ -694,14 +695,14 @@ Untracked files:
        <BLUE>untracked<RESET>
 
 EOF
-       test_config color.ui always &&
-       git status | test_decode_color >output &&
+       test_config color.ui auto &&
+       test_terminal git status | test_decode_color >output &&
        test_i18ncmp expect output
 '
 
-test_expect_success 'status with color.status' '
-       test_config color.status always &&
-       git status | test_decode_color >output &&
+test_expect_success TTY 'status with color.status' '
+       test_config color.status auto &&
+       test_terminal git status | test_decode_color >output &&
        test_i18ncmp expect output
 '
 
@@ -714,19 +715,19 @@ cat >expect <<\EOF
 <BLUE>??<RESET> untracked
 EOF
 
-test_expect_success 'status -s with color.ui' '
+test_expect_success TTY 'status -s with color.ui' '
 
-       git config color.ui always &&
-       git status -s | test_decode_color >output &&
+       git config color.ui auto &&
+       test_terminal git status -s | test_decode_color >output &&
        test_cmp expect output
 
 '
 
-test_expect_success 'status -s with color.status' '
+test_expect_success TTY 'status -s with color.status' '
 
        git config --unset color.ui &&
-       git config color.status always &&
-       git status -s | test_decode_color >output &&
+       git config color.status auto &&
+       test_terminal git status -s | test_decode_color >output &&
        test_cmp expect output
 
 '
@@ -741,9 +742,9 @@ cat >expect <<\EOF
 <BLUE>??<RESET> untracked
 EOF
 
-test_expect_success 'status -s -b with color.status' '
+test_expect_success TTY 'status -s -b with color.status' '
 
-       git status -s -b | test_decode_color >output &&
+       test_terminal git status -s -b | test_decode_color >output &&
        test_i18ncmp expect output
 
 '
@@ -757,20 +758,20 @@ A  dir2/added
 ?? untracked
 EOF
 
-test_expect_success 'status --porcelain ignores color.ui' '
+test_expect_success TTY 'status --porcelain ignores color.ui' '
 
        git config --unset color.status &&
-       git config color.ui always &&
-       git status --porcelain | test_decode_color >output &&
+       git config color.ui auto &&
+       test_terminal git status --porcelain | test_decode_color >output &&
        test_cmp expect output
 
 '
 
-test_expect_success 'status --porcelain ignores color.status' '
+test_expect_success TTY 'status --porcelain ignores color.status' '
 
        git config --unset color.ui &&
-       git config color.status always &&
-       git status --porcelain | test_decode_color >output &&
+       git config color.status auto &&
+       test_terminal git status --porcelain | test_decode_color >output &&
        test_cmp expect output
 
 '
@@ -1670,4 +1671,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 0c6f91c4338cce131b3d6066e590f177f7e739d1..164719d1c9d3e76a08bbbeb968aaf535370db7d1 100755 (executable)
@@ -681,6 +681,36 @@ test_expect_success 'using "where = before"' '
        test_cmp expected actual
 '
 
+test_expect_success 'overriding configuration with "--where after"' '
+       git config trailer.ack.where "before" &&
+       cat complex_message_body >expected &&
+       sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+               Fixes: Z
+               Acked-by= Z
+               Acked-by= Peff
+               Reviewed-by: Z
+               Signed-off-by: Z
+       EOF
+       git interpret-trailers --where after --trailer "ack: Peff" \
+               complex_message >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'using "where = before" with "--no-where"' '
+       cat complex_message_body >expected &&
+       sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+               Bug #42
+               Fixes: Z
+               Acked-by= Peff
+               Acked-by= Z
+               Reviewed-by: Z
+               Signed-off-by: Z
+       EOF
+       git interpret-trailers --where after --no-where --trailer "ack: Peff" \
+               --trailer "bug: 42" complex_message >actual &&
+       test_cmp expected actual
+'
+
 test_expect_success 'using "where = after"' '
        git config trailer.ack.where "after" &&
        cat complex_message_body >expected &&
@@ -947,6 +977,23 @@ test_expect_success 'using "ifExists = add" with "where = after"' '
        test_cmp expected actual
 '
 
+test_expect_success 'overriding configuration with "--if-exists replace"' '
+       git config trailer.fix.key "Fixes: " &&
+       git config trailer.fix.ifExists "add" &&
+       cat complex_message_body >expected &&
+       sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+               Bug #42
+               Acked-by= Z
+               Reviewed-by:
+               Signed-off-by: Z
+               Fixes: 22
+       EOF
+       git interpret-trailers --if-exists replace --trailer "review:" \
+               --trailer "fix=53" --trailer "fix=22" --trailer "bug: 42" \
+               <complex_message >actual &&
+       test_cmp expected actual
+'
+
 test_expect_success 'using "ifExists = replace"' '
        git config trailer.fix.key "Fixes: " &&
        git config trailer.fix.ifExists "replace" &&
@@ -1026,6 +1073,25 @@ test_expect_success 'the default is "ifMissing = add"' '
        test_cmp expected actual
 '
 
+test_expect_success 'overriding configuration with "--if-missing doNothing"' '
+       git config trailer.ifmissing "add" &&
+       cat complex_message_body >expected &&
+       sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+               Fixes: Z
+               Acked-by= Z
+               Acked-by= Junio
+               Acked-by= Peff
+               Reviewed-by:
+               Signed-off-by: Z
+       EOF
+       git interpret-trailers --if-missing doNothing \
+               --trailer "review:" --trailer "fix=53" \
+               --trailer "cc=Linus" --trailer "ack: Junio" \
+               --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
+               <complex_message >actual &&
+       test_cmp expected actual
+'
+
 test_expect_success 'when default "ifMissing" is "doNothing"' '
        git config trailer.ifmissing "doNothing" &&
        cat complex_message_body >expected &&
@@ -1275,4 +1341,80 @@ test_expect_success 'with cut line' '
        test_cmp expected actual
 '
 
+test_expect_success 'only trailers' '
+       git config trailer.sign.command "echo config-value" &&
+       cat >expected <<-\EOF &&
+               existing: existing-value
+               sign: config-value
+               added: added-value
+       EOF
+       git interpret-trailers \
+               --trailer added:added-value \
+               --only-trailers >actual <<-\EOF &&
+               my subject
+
+               my body
+
+               existing: existing-value
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success 'only-trailers omits non-trailer in middle of block' '
+       git config trailer.sign.command "echo config-value" &&
+       cat >expected <<-\EOF &&
+               Signed-off-by: nobody <nobody@nowhere>
+               Signed-off-by: somebody <somebody@somewhere>
+               sign: config-value
+       EOF
+       git interpret-trailers --only-trailers >actual <<-\EOF &&
+               subject
+
+               it is important that the trailers below are signed-off-by
+               so that they meet the "25% trailers Git knows about" heuristic
+
+               Signed-off-by: nobody <nobody@nowhere>
+               this is not a trailer
+               Signed-off-by: somebody <somebody@somewhere>
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success 'only input' '
+       git config trailer.sign.command "echo config-value" &&
+       cat >expected <<-\EOF &&
+               existing: existing-value
+       EOF
+       git interpret-trailers \
+               --only-trailers --only-input >actual <<-\EOF &&
+               my subject
+
+               my body
+
+               existing: existing-value
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success 'unfold' '
+       cat >expected <<-\EOF &&
+               foo: continued across several lines
+       EOF
+       # pass through tr to make leading and trailing whitespace more obvious
+       tr _ " " <<-\EOF |
+               my subject
+
+               my body
+
+               foo:_
+               __continued
+               ___across
+               ____several
+               _____lines
+               ___
+       EOF
+       git interpret-trailers --only-trailers --only-input --unfold >actual &&
+       test_cmp expected actual
+'
+
 test_done
diff --git a/t/t7614-merge-signoff.sh b/t/t7614-merge-signoff.sh
new file mode 100755 (executable)
index 0000000..c1b8446
--- /dev/null
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+test_description='git merge --signoff
+
+This test runs git merge --signoff and makes sure that it works.
+'
+
+. ./test-lib.sh
+
+# Setup test files
+test_setup() {
+       # Expected commit message after merge --signoff
+       cat >expected-signed <<EOF &&
+Merge branch 'master' into other-branch
+
+Signed-off-by: $(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/")
+EOF
+
+       # Expected commit message after merge without --signoff (or with --no-signoff)
+       cat >expected-unsigned <<EOF &&
+Merge branch 'master' into other-branch
+EOF
+
+       # Initial commit and feature branch to merge master into it.
+       git commit --allow-empty -m "Initial empty commit" &&
+       git checkout -b other-branch &&
+       test_commit other-branch file1 1
+}
+
+# Setup repository, files & feature branch
+# This step must be run if You want to test 2,3 or 4
+# Order of 2,3,4 is not important, but 1 must be run before
+# For example `-r 1,4` or `-r 1,4,2 -v` etc
+# But not `-r 2` or `-r 4,3,2,1`
+test_expect_success 'setup' '
+       test_setup
+'
+
+# Test with --signoff flag
+test_expect_success 'git merge --signoff adds a sign-off line' '
+       git checkout master &&
+       test_commit master-branch-2 file2 2 &&
+       git checkout other-branch &&
+       git merge master --signoff --no-edit &&
+       git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+       test_cmp expected-signed actual
+'
+
+# Test without --signoff flag
+test_expect_success 'git merge does not add a sign-off line' '
+       git checkout master &&
+       test_commit master-branch-3 file3 3 &&
+       git checkout other-branch &&
+       git merge master --no-edit &&
+       git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+       test_cmp expected-unsigned actual
+'
+
+# Test for --no-signoff flag
+test_expect_success 'git merge --no-signoff flag cancels --signoff flag' '
+       git checkout master &&
+       test_commit master-branch-4 file4 4 &&
+       git checkout other-branch &&
+       git merge master --no-edit --signoff --no-signoff &&
+       git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+       test_cmp expected-unsigned actual
+'
+
+test_done
index d8242e467eaa4f0a46cfa0a4ad431a447e58dd6c..0f86c191745d65c88c70881c6520685289bc2405 100755 (executable)
@@ -51,6 +51,11 @@ test_expect_success '--path=<path> complains without --textconv/--filters' '
        grep "path.*needs.*filters" err
 '
 
+test_expect_success '--textconv/--filters complain without path' '
+       test_must_fail git cat-file --textconv HEAD &&
+       test_must_fail git cat-file --filters HEAD
+'
+
 test_expect_success 'cat-file --textconv --batch works' '
        sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
        test_config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <" &&
index d1e4e8ad19f3d3e005e177b98e4eee16f4ef1461..f30980895c2f1b93317870e6b471d67bc99b0fd2 100755 (executable)
@@ -148,6 +148,8 @@ cat >expected-cc <<\EOF
 !two@example.com!
 !three@example.com!
 !four@example.com!
+!five@example.com!
+!six@example.com!
 EOF
 "
 
@@ -161,6 +163,8 @@ test_expect_success $PREREQ 'cc trailer with various syntax' '
        Cc: <two@example.com> # trailing comments are ignored
        Cc: <three@example.com>, <not.four@example.com> one address per line
        Cc: "Some # Body" <four@example.com> [ <also.a.comment> ]
+       Cc: five@example.com # not.six@example.com
+       Cc: six@example.com, not.seven@example.com
        EOF
        clean_fake_sendmail &&
        git send-email -1 --to=recipient@example.com \
index 6dafe7e99ab4bd0006ed4f7204062fca31750689..8eaaca6f9906879f16f3658b0e04796086d4c435 100755 (executable)
@@ -4,12 +4,13 @@ test_description='check svn dumpfile importer'
 
 . ./test-lib.sh
 
+if test_have_prereq !PIPE
+then
+       skip_all="svn dumpfile importer testing requires the PIPE prerequisite"
+       test_done
+fi
+
 reinit_git () {
-       if ! test_declared_prereq PIPE
-       then
-               echo >&4 "reinit_git: need to declare PIPE prerequisite"
-               return 127
-       fi
        rm -fr .git &&
        rm -f stream backflow &&
        git init &&
@@ -54,19 +55,19 @@ text_no_props () {
 
 >empty
 
-test_expect_success PIPE 'empty dump' '
+test_expect_success 'empty dump' '
        reinit_git &&
        echo "SVN-fs-dump-format-version: 2" >input &&
        try_dump input
 '
 
-test_expect_success PIPE 'v4 dumps not supported' '
+test_expect_success 'v4 dumps not supported' '
        reinit_git &&
        echo "SVN-fs-dump-format-version: 4" >v4.dump &&
        try_dump v4.dump must_fail
 '
 
-test_expect_failure PIPE 'empty revision' '
+test_expect_failure 'empty revision' '
        reinit_git &&
        printf "rev <nobody, nobody@local>: %s\n" "" "" >expect &&
        cat >emptyrev.dump <<-\EOF &&
@@ -86,7 +87,7 @@ test_expect_failure PIPE 'empty revision' '
        test_cmp expect actual
 '
 
-test_expect_success PIPE 'empty properties' '
+test_expect_success 'empty properties' '
        reinit_git &&
        printf "rev <nobody, nobody@local>: %s\n" "" "" >expect &&
        cat >emptyprop.dump <<-\EOF &&
@@ -109,7 +110,7 @@ test_expect_success PIPE 'empty properties' '
        test_cmp expect actual
 '
 
-test_expect_success PIPE 'author name and commit message' '
+test_expect_success 'author name and commit message' '
        reinit_git &&
        echo "<author@example.com, author@example.com@local>" >expect.author &&
        cat >message <<-\EOF &&
@@ -143,7 +144,7 @@ test_expect_success PIPE 'author name and commit message' '
        test_cmp expect.author actual.author
 '
 
-test_expect_success PIPE 'unsupported properties are ignored' '
+test_expect_success 'unsupported properties are ignored' '
        reinit_git &&
        echo author >expect &&
        cat >extraprop.dump <<-\EOF &&
@@ -168,7 +169,7 @@ test_expect_success PIPE 'unsupported properties are ignored' '
        test_cmp expect actual
 '
 
-test_expect_failure PIPE 'timestamp and empty file' '
+test_expect_failure 'timestamp and empty file' '
        echo author@example.com >expect.author &&
        echo 1999-01-01 >expect.date &&
        echo file >expect.files &&
@@ -210,7 +211,7 @@ test_expect_failure PIPE 'timestamp and empty file' '
        test_cmp empty file
 '
 
-test_expect_success PIPE 'directory with files' '
+test_expect_success 'directory with files' '
        reinit_git &&
        printf "%s\n" directory/file1 directory/file2 >expect.files &&
        echo hi >hi &&
@@ -263,7 +264,7 @@ test_expect_success PIPE 'directory with files' '
        test_cmp hi directory/file2
 '
 
-test_expect_success PIPE 'branch name with backslash' '
+test_expect_success 'branch name with backslash' '
        reinit_git &&
        sort <<-\EOF >expect.branch-files &&
        trunk/file1
@@ -362,7 +363,7 @@ test_expect_success PIPE 'branch name with backslash' '
        test_cmp expect.branch-files actual.branch-files
 '
 
-test_expect_success PIPE 'node without action' '
+test_expect_success 'node without action' '
        reinit_git &&
        cat >inaction.dump <<-\EOF &&
        SVN-fs-dump-format-version: 3
@@ -383,7 +384,7 @@ test_expect_success PIPE 'node without action' '
        try_dump inaction.dump must_fail
 '
 
-test_expect_success PIPE 'action: add node without text' '
+test_expect_success 'action: add node without text' '
        reinit_git &&
        cat >textless.dump <<-\EOF &&
        SVN-fs-dump-format-version: 3
@@ -405,7 +406,7 @@ test_expect_success PIPE 'action: add node without text' '
        try_dump textless.dump must_fail
 '
 
-test_expect_failure PIPE 'change file mode but keep old content' '
+test_expect_failure 'change file mode but keep old content' '
        reinit_git &&
        cat >expect <<-\EOF &&
        OBJID
@@ -481,7 +482,7 @@ test_expect_failure PIPE 'change file mode but keep old content' '
        test_cmp hello actual.target
 '
 
-test_expect_success PIPE 'NUL in property value' '
+test_expect_success 'NUL in property value' '
        reinit_git &&
        echo "commit message" >expect.message &&
        {
@@ -507,7 +508,7 @@ test_expect_success PIPE 'NUL in property value' '
        test_cmp expect.message actual.message
 '
 
-test_expect_success PIPE 'NUL in log message, file content, and property name' '
+test_expect_success 'NUL in log message, file content, and property name' '
        # Caveat: svnadmin 1.6.16 (r1073529) truncates at \0 in the
        # svn:specialQnotreally example.
        reinit_git &&
@@ -587,7 +588,7 @@ test_expect_success PIPE 'NUL in log message, file content, and property name' '
        test_cmp expect.hello2 actual.hello2
 '
 
-test_expect_success PIPE 'change file mode and reiterate content' '
+test_expect_success 'change file mode and reiterate content' '
        reinit_git &&
        cat >expect <<-\EOF &&
        OBJID
@@ -667,7 +668,7 @@ test_expect_success PIPE 'change file mode and reiterate content' '
        test_cmp hello actual.target
 '
 
-test_expect_success PIPE 'deltas supported' '
+test_expect_success 'deltas supported' '
        reinit_git &&
        {
                # (old) h + (inline) ello + (old) \n
@@ -731,7 +732,7 @@ test_expect_success PIPE 'deltas supported' '
        try_dump delta.dump
 '
 
-test_expect_success PIPE 'property deltas supported' '
+test_expect_success 'property deltas supported' '
        reinit_git &&
        cat >expect <<-\EOF &&
        OBJID
@@ -796,7 +797,7 @@ test_expect_success PIPE 'property deltas supported' '
        test_cmp expect actual
 '
 
-test_expect_success PIPE 'properties on /' '
+test_expect_success 'properties on /' '
        reinit_git &&
        cat <<-\EOF >expect &&
        OBJID
@@ -850,7 +851,7 @@ test_expect_success PIPE 'properties on /' '
        test_cmp expect actual
 '
 
-test_expect_success PIPE 'deltas for typechange' '
+test_expect_success 'deltas for typechange' '
        reinit_git &&
        cat >expect <<-\EOF &&
        OBJID
@@ -935,7 +936,7 @@ test_expect_success PIPE 'deltas for typechange' '
        test_cmp expect actual
 '
 
-test_expect_success PIPE 'deltas need not consume the whole preimage' '
+test_expect_success 'deltas need not consume the whole preimage' '
        reinit_git &&
        cat >expect <<-\EOF &&
        OBJID
@@ -1040,7 +1041,7 @@ test_expect_success PIPE 'deltas need not consume the whole preimage' '
        test_cmp expect.3 actual.3
 '
 
-test_expect_success PIPE 'no hang for delta trying to read past end of preimage' '
+test_expect_success 'no hang for delta trying to read past end of preimage' '
        reinit_git &&
        {
                # COPY 1
@@ -1087,7 +1088,7 @@ test_expect_success 'set up svn repo' '
        fi
 '
 
-test_expect_success SVNREPO,PIPE 't9135/svn.dump' '
+test_expect_success SVNREPO 't9135/svn.dump' '
        mkdir -p simple-git &&
        (
                cd simple-git &&
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 8dcb05c4a5711e95bb64d8ba310e3490744fc015..866ddf60581e3fea1afdbe28e71e7f3137da5722 100755 (executable)
@@ -234,7 +234,7 @@ test_expect_success 'fast-export -C -C | fast-import' '
        mkdir new &&
        git --git-dir=new/.git init &&
        git fast-export -C -C --signed-tags=strip --all > output &&
-       grep "^C file6 file7\$" output &&
+       grep "^C file2 file4\$" output &&
        cat output |
        (cd new &&
         git fast-import &&
@@ -522,4 +522,22 @@ test_expect_success 'delete refspec' '
        test_cmp expected actual
 '
 
+test_expect_success 'when using -C, do not declare copy when source of copy is also modified' '
+       test_create_repo src &&
+       echo a_line >src/file.txt &&
+       git -C src add file.txt &&
+       git -C src commit -m 1st_commit &&
+
+       cp src/file.txt src/file2.txt &&
+       echo another_line >>src/file.txt &&
+       git -C src add file.txt file2.txt &&
+       git -C src commit -m 2nd_commit &&
+
+       test_create_repo dst &&
+       git -C src fast-export --all -C | git -C dst fast-import &&
+       git -C src show >expected &&
+       git -C dst show >actual &&
+       test_cmp expected actual
+'
+
 test_done
index 51f52dcd4e3afaebda1a0a4c1a493e869cda3cc0..9b61f16f7a8807ad96014d79f1d1b968397584bf 100644 (file)
@@ -44,6 +44,11 @@ GIT_BUILD_DIR="$TEST_DIRECTORY"/..
 : ${ASAN_OPTIONS=detect_leaks=0:abort_on_error=1}
 export ASAN_OPTIONS
 
+# If LSAN is in effect we _do_ want leak checking, but we still
+# want to abort so that we notice the problems.
+: ${LSAN_OPTIONS=abort_on_error=1}
+export LSAN_OPTIONS
+
 ################################################################
 # It appears that people try to run tests without building...
 "$GIT_BUILD_DIR/git" >/dev/null
@@ -99,7 +104,6 @@ unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
        my $ok = join("|", qw(
                TRACE
                DEBUG
-               USE_LOOKUP
                TEST
                .*_TEST
                PROVE
@@ -275,7 +279,7 @@ then
        test -z "$verbose" && verbose_only="$valgrind_only"
 elif test -n "$valgrind"
 then
-       verbose=t
+       test -z "$verbose_log" && verbose=t
 fi
 
 if test -n "$color"
@@ -1063,14 +1067,8 @@ test_i18ngrep () {
 
 test_lazy_prereq PIPE '
        # test whether the filesystem supports FIFOs
-       case $(uname -s) in
-       CYGWIN*|MINGW*)
-               false
-               ;;
-       *)
-               rm -f testfifo && mkfifo testfifo
-               ;;
-       esac
+       test_have_prereq !MINGW,!CYGWIN &&
+       rm -f testfifo && mkfifo testfifo
 '
 
 test_lazy_prereq SYMLINKS '
@@ -1166,7 +1164,19 @@ run_with_limited_cmdline () {
        (ulimit -s 128 && "$@")
 }
 
-test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'
+test_lazy_prereq CMDLINE_LIMIT '
+       test_have_prereq !MINGW,!CYGWIN &&
+       run_with_limited_cmdline true
+'
+
+run_with_limited_stack () {
+       (ulimit -s 128 && "$@")
+}
+
+test_lazy_prereq ULIMIT_STACK_SIZE '
+       test_have_prereq !MINGW,!CYGWIN &&
+       run_with_limited_stack true
+'
 
 build_option () {
        git version --build-options |
index 96b6a03e1c93c9ab6df356c2aad02514232f76ea..46bf6184798442560ad3ae7f68a7972680c5f8eb 100755 (executable)
@@ -80,6 +80,7 @@ sub copy_stdio {
 if ($#ARGV < 1) {
        die "usage: test-terminal program args";
 }
+$ENV{TERM} = 'vt100';
 my $master_in = new IO::Pty;
 my $master_out = new IO::Pty;
 my $master_err = new IO::Pty;
diff --git a/tag.c b/tag.c
index 47f60ae151c2cfd9855d1b6f1660ec2ba178bec3..fcbe012f7a2203e198dac9eac03aa9dab999f334 100644 (file)
--- a/tag.c
+++ b/tag.c
@@ -33,7 +33,7 @@ static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
        return ret;
 }
 
-int gpg_verify_tag(const unsigned char *sha1, const char *name_to_report,
+int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
                unsigned flags)
 {
        enum object_type type;
@@ -41,20 +41,20 @@ int gpg_verify_tag(const unsigned char *sha1, const char *name_to_report,
        unsigned long size;
        int ret;
 
-       type = sha1_object_info(sha1, NULL);
+       type = sha1_object_info(oid->hash, NULL);
        if (type != OBJ_TAG)
                return error("%s: cannot verify a non-tag object of type %s.",
                                name_to_report ?
                                name_to_report :
-                               find_unique_abbrev(sha1, DEFAULT_ABBREV),
+                               find_unique_abbrev(oid->hash, DEFAULT_ABBREV),
                                typename(type));
 
-       buf = read_sha1_file(sha1, &type, &size);
+       buf = read_sha1_file(oid->hash, &type, &size);
        if (!buf)
                return error("%s: unable to read file.",
                                name_to_report ?
                                name_to_report :
-                               find_unique_abbrev(sha1, DEFAULT_ABBREV));
+                               find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
 
        ret = run_gpg_verify(buf, size, flags);
 
@@ -142,13 +142,13 @@ int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
        bufptr = nl + 1;
 
        if (!strcmp(type, blob_type)) {
-               item->tagged = &lookup_blob(&oid)->object;
+               item->tagged = (struct object *)lookup_blob(&oid);
        } else if (!strcmp(type, tree_type)) {
-               item->tagged = &lookup_tree(&oid)->object;
+               item->tagged = (struct object *)lookup_tree(&oid);
        } else if (!strcmp(type, commit_type)) {
-               item->tagged = &lookup_commit(&oid)->object;
+               item->tagged = (struct object *)lookup_commit(&oid);
        } else if (!strcmp(type, tag_type)) {
-               item->tagged = &lookup_tag(&oid)->object;
+               item->tagged = (struct object *)lookup_tag(&oid);
        } else {
                error("Unknown type %s", type);
                item->tagged = NULL;
diff --git a/tag.h b/tag.h
index fdfcb4a84aa16b8173f8f308f7faecf8e9f000c7..d469534e82a87b651abe752469d0547c2b560e10 100644 (file)
--- a/tag.h
+++ b/tag.h
@@ -17,7 +17,7 @@ extern int parse_tag_buffer(struct tag *item, const void *data, unsigned long si
 extern int parse_tag(struct tag *item);
 extern struct object *deref_tag(struct object *, const char *, int);
 extern struct object *deref_tag_noverify(struct object *);
-extern int gpg_verify_tag(const unsigned char *sha1,
+extern int gpg_verify_tag(const struct object_id *oid,
                const char *name_to_report, unsigned flags);
 
 #endif /* TAG_H */
index 68437106703470f39a6079aaad7d3bb407ac3389..5fdafdd2d2d72390ee9fe3c2afd501ad222fac8e 100644 (file)
  *     `fdopen_tempfile()` has been called on the object
  *   - `owner` holds the PID of the process that created the file
  *
- * - Active, file closed (after successful `close_tempfile()`). Same
+ * - Active, file closed (after `close_tempfile_gently()`). Same
  *   as the previous state, except that the temporary file is closed,
  *   `fd` is -1, and `fp` is `NULL`.
  *
- * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, a
- *   failed attempt to create a temporary file, or a failed
- *   `close_tempfile()`). In this state:
+ * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
+ *   failed attempt to create a temporary file). In this state:
  *
  *   - `active` is unset
  *   - `filename` is empty (usually, though there are transitory
  *     states in which this condition doesn't hold). Client code should
  *     *not* rely on the filename being empty in this state.
  *   - `fd` is -1 and `fp` is `NULL`
- *   - the object is left registered in the `tempfile_list`, and
- *     `on_list` is set.
+ *   - the object is removed from `tempfile_list` (but could be used again)
  *
  * A temporary file is owned by the process that created it. The
  * `tempfile` has an `owner` field that records the owner's PID. This
 #include "tempfile.h"
 #include "sigchain.h"
 
-static struct tempfile *volatile tempfile_list;
+static VOLATILE_LIST_HEAD(tempfile_list);
 
-static void remove_tempfiles(int skip_fclose)
+static void remove_tempfiles(int in_signal_handler)
 {
        pid_t me = getpid();
+       volatile struct volatile_list_head *pos;
 
-       while (tempfile_list) {
-               if (tempfile_list->owner == me) {
-                       /* fclose() is not safe to call in a signal handler */
-                       if (skip_fclose)
-                               tempfile_list->fp = NULL;
-                       delete_tempfile(tempfile_list);
-               }
-               tempfile_list = tempfile_list->next;
+       list_for_each(pos, &tempfile_list) {
+               struct tempfile *p = list_entry(pos, struct tempfile, list);
+
+               if (!is_tempfile_active(p) || p->owner != me)
+                       continue;
+
+               if (p->fd >= 0)
+                       close(p->fd);
+
+               if (in_signal_handler)
+                       unlink(p->filename.buf);
+               else
+                       unlink_or_warn(p->filename.buf);
+
+               p->active = 0;
        }
 }
 
@@ -85,39 +91,48 @@ static void remove_tempfiles_on_signal(int signo)
        raise(signo);
 }
 
-/*
- * Initialize *tempfile if necessary and add it to tempfile_list.
- */
-static void prepare_tempfile_object(struct tempfile *tempfile)
+static struct tempfile *new_tempfile(void)
 {
-       if (!tempfile_list) {
-               /* One-time initialization */
+       struct tempfile *tempfile = xmalloc(sizeof(*tempfile));
+       tempfile->fd = -1;
+       tempfile->fp = NULL;
+       tempfile->active = 0;
+       tempfile->owner = 0;
+       INIT_LIST_HEAD(&tempfile->list);
+       strbuf_init(&tempfile->filename, 0);
+       return tempfile;
+}
+
+static void activate_tempfile(struct tempfile *tempfile)
+{
+       static int initialized;
+
+       if (is_tempfile_active(tempfile))
+               BUG("activate_tempfile called for active object");
+
+       if (!initialized) {
                sigchain_push_common(remove_tempfiles_on_signal);
                atexit(remove_tempfiles_on_exit);
+               initialized = 1;
        }
 
-       if (tempfile->active)
-               die("BUG: prepare_tempfile_object called for active object");
-       if (!tempfile->on_list) {
-               /* Initialize *tempfile and add it to tempfile_list: */
-               tempfile->fd = -1;
-               tempfile->fp = NULL;
-               tempfile->active = 0;
-               tempfile->owner = 0;
-               strbuf_init(&tempfile->filename, 0);
-               tempfile->next = tempfile_list;
-               tempfile_list = tempfile;
-               tempfile->on_list = 1;
-       } else if (tempfile->filename.len) {
-               /* This shouldn't happen, but better safe than sorry. */
-               die("BUG: prepare_tempfile_object called for improperly-reset object");
-       }
+       volatile_list_add(&tempfile->list, &tempfile_list);
+       tempfile->owner = getpid();
+       tempfile->active = 1;
+}
+
+static void deactivate_tempfile(struct tempfile *tempfile)
+{
+       tempfile->active = 0;
+       strbuf_release(&tempfile->filename);
+       volatile_list_del(&tempfile->list);
+       free(tempfile);
 }
 
 /* Make sure errno contains a meaningful value on error */
-int create_tempfile(struct tempfile *tempfile, const char *path)
+struct tempfile *create_tempfile(const char *path)
 {
-       prepare_tempfile_object(tempfile);
+       struct tempfile *tempfile = new_tempfile();
 
        strbuf_add_absolute_path(&tempfile->filename, path);
        tempfile->fd = open(tempfile->filename.buf,
@@ -127,52 +142,48 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
                tempfile->fd = open(tempfile->filename.buf,
                                    O_RDWR | O_CREAT | O_EXCL, 0666);
        if (tempfile->fd < 0) {
-               strbuf_reset(&tempfile->filename);
-               return -1;
+               deactivate_tempfile(tempfile);
+               return NULL;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
+       activate_tempfile(tempfile);
        if (adjust_shared_perm(tempfile->filename.buf)) {
                int save_errno = errno;
                error("cannot fix permission bits on %s", tempfile->filename.buf);
-               delete_tempfile(tempfile);
+               delete_tempfile(&tempfile);
                errno = save_errno;
-               return -1;
+               return NULL;
        }
-       return tempfile->fd;
+
+       return tempfile;
 }
 
-void register_tempfile(struct tempfile *tempfile, const char *path)
+struct tempfile *register_tempfile(const char *path)
 {
-       prepare_tempfile_object(tempfile);
+       struct tempfile *tempfile = new_tempfile();
        strbuf_add_absolute_path(&tempfile->filename, path);
-       tempfile->owner = getpid();
-       tempfile->active = 1;
+       activate_tempfile(tempfile);
+       return tempfile;
 }
 
-int mks_tempfile_sm(struct tempfile *tempfile,
-                   const char *template, int suffixlen, int mode)
+struct tempfile *mks_tempfile_sm(const char *template, int suffixlen, int mode)
 {
-       prepare_tempfile_object(tempfile);
+       struct tempfile *tempfile = new_tempfile();
 
        strbuf_add_absolute_path(&tempfile->filename, template);
        tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
        if (tempfile->fd < 0) {
-               strbuf_reset(&tempfile->filename);
-               return -1;
+               deactivate_tempfile(tempfile);
+               return NULL;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
-       return tempfile->fd;
+       activate_tempfile(tempfile);
+       return tempfile;
 }
 
-int mks_tempfile_tsm(struct tempfile *tempfile,
-                    const char *template, int suffixlen, int mode)
+struct tempfile *mks_tempfile_tsm(const char *template, int suffixlen, int mode)
 {
+       struct tempfile *tempfile = new_tempfile();
        const char *tmpdir;
 
-       prepare_tempfile_object(tempfile);
-
        tmpdir = getenv("TMPDIR");
        if (!tmpdir)
                tmpdir = "/tmp";
@@ -180,35 +191,34 @@ int mks_tempfile_tsm(struct tempfile *tempfile,
        strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
        tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
        if (tempfile->fd < 0) {
-               strbuf_reset(&tempfile->filename);
-               return -1;
+               deactivate_tempfile(tempfile);
+               return NULL;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
-       return tempfile->fd;
+       activate_tempfile(tempfile);
+       return tempfile;
 }
 
-int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode)
+struct tempfile *xmks_tempfile_m(const char *template, int mode)
 {
-       int fd;
+       struct tempfile *tempfile;
        struct strbuf full_template = STRBUF_INIT;
 
        strbuf_add_absolute_path(&full_template, template);
-       fd = mks_tempfile_m(tempfile, full_template.buf, mode);
-       if (fd < 0)
+       tempfile = mks_tempfile_m(full_template.buf, mode);
+       if (!tempfile)
                die_errno("Unable to create temporary file '%s'",
                          full_template.buf);
 
        strbuf_release(&full_template);
-       return fd;
+       return tempfile;
 }
 
 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
 {
-       if (!tempfile->active)
-               die("BUG: fdopen_tempfile() called for inactive object");
+       if (!is_tempfile_active(tempfile))
+               BUG("fdopen_tempfile() called for inactive object");
        if (tempfile->fp)
-               die("BUG: fdopen_tempfile() called for open object");
+               BUG("fdopen_tempfile() called for open object");
 
        tempfile->fp = fdopen(tempfile->fd, mode);
        return tempfile->fp;
@@ -216,34 +226,36 @@ FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
 
 const char *get_tempfile_path(struct tempfile *tempfile)
 {
-       if (!tempfile->active)
-               die("BUG: get_tempfile_path() called for inactive object");
+       if (!is_tempfile_active(tempfile))
+               BUG("get_tempfile_path() called for inactive object");
        return tempfile->filename.buf;
 }
 
 int get_tempfile_fd(struct tempfile *tempfile)
 {
-       if (!tempfile->active)
-               die("BUG: get_tempfile_fd() called for inactive object");
+       if (!is_tempfile_active(tempfile))
+               BUG("get_tempfile_fd() called for inactive object");
        return tempfile->fd;
 }
 
 FILE *get_tempfile_fp(struct tempfile *tempfile)
 {
-       if (!tempfile->active)
-               die("BUG: get_tempfile_fp() called for inactive object");
+       if (!is_tempfile_active(tempfile))
+               BUG("get_tempfile_fp() called for inactive object");
        return tempfile->fp;
 }
 
-int close_tempfile(struct tempfile *tempfile)
+int close_tempfile_gently(struct tempfile *tempfile)
 {
-       int fd = tempfile->fd;
-       FILE *fp = tempfile->fp;
+       int fd;
+       FILE *fp;
        int err;
 
-       if (fd < 0)
+       if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
                return 0;
 
+       fd = tempfile->fd;
+       fp = tempfile->fp;
        tempfile->fd = -1;
        if (fp) {
                tempfile->fp = NULL;
@@ -258,54 +270,52 @@ int close_tempfile(struct tempfile *tempfile)
                err = close(fd);
        }
 
-       if (err) {
-               int save_errno = errno;
-               delete_tempfile(tempfile);
-               errno = save_errno;
-               return -1;
-       }
-
-       return 0;
+       return err ? -1 : 0;
 }
 
 int reopen_tempfile(struct tempfile *tempfile)
 {
+       if (!is_tempfile_active(tempfile))
+               BUG("reopen_tempfile called for an inactive object");
        if (0 <= tempfile->fd)
-               die("BUG: reopen_tempfile called for an open object");
-       if (!tempfile->active)
-               die("BUG: reopen_tempfile called for an inactive object");
+               BUG("reopen_tempfile called for an open object");
        tempfile->fd = open(tempfile->filename.buf, O_WRONLY);
        return tempfile->fd;
 }
 
-int rename_tempfile(struct tempfile *tempfile, const char *path)
+int rename_tempfile(struct tempfile **tempfile_p, const char *path)
 {
-       if (!tempfile->active)
-               die("BUG: rename_tempfile called for inactive object");
+       struct tempfile *tempfile = *tempfile_p;
+
+       if (!is_tempfile_active(tempfile))
+               BUG("rename_tempfile called for inactive object");
 
-       if (close_tempfile(tempfile))
+       if (close_tempfile_gently(tempfile)) {
+               delete_tempfile(tempfile_p);
                return -1;
+       }
 
        if (rename(tempfile->filename.buf, path)) {
                int save_errno = errno;
-               delete_tempfile(tempfile);
+               delete_tempfile(tempfile_p);
                errno = save_errno;
                return -1;
        }
 
-       tempfile->active = 0;
-       strbuf_reset(&tempfile->filename);
+       deactivate_tempfile(tempfile);
+       *tempfile_p = NULL;
        return 0;
 }
 
-void delete_tempfile(struct tempfile *tempfile)
+void delete_tempfile(struct tempfile **tempfile_p)
 {
-       if (!tempfile->active)
+       struct tempfile *tempfile = *tempfile_p;
+
+       if (!is_tempfile_active(tempfile))
                return;
 
-       if (!close_tempfile(tempfile)) {
-               unlink_or_warn(tempfile->filename.buf);
-               tempfile->active = 0;
-               strbuf_reset(&tempfile->filename);
-       }
+       close_tempfile_gently(tempfile);
+       unlink_or_warn(tempfile->filename.buf);
+       deactivate_tempfile(tempfile);
+       *tempfile_p = NULL;
 }
index 2f0038decd5b6d00b55fa03ec8988a3810d1784f..b8f4b5e145b8b6b1d80cac299dbaedc8253e861b 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef TEMPFILE_H
 #define TEMPFILE_H
 
+#include "list.h"
+
 /*
  * Handle temporary files.
  *
  *
  * The caller:
  *
- * * Allocates a `struct tempfile` either as a static variable or on
- *   the heap, initialized to zeros. Once you use the structure to
- *   call `create_tempfile()`, it belongs to the tempfile subsystem
- *   and its storage must remain valid throughout the life of the
- *   program (i.e. you cannot use an on-stack variable to hold this
- *   structure).
- *
  * * Attempts to create a temporary file by calling
- *   `create_tempfile()`.
+ *   `create_tempfile()`. The resources used for the temporary file are
+ *   managed by the tempfile API.
  *
  * * Writes new content to the file by either:
  *
- *   * writing to the file descriptor returned by `create_tempfile()`
- *     (also available via `tempfile->fd`).
+ *   * writing to the `tempfile->fd` file descriptor
  *
  *   * calling `fdopen_tempfile()` to get a `FILE` pointer for the
  *     open file and writing to the file using stdio.
  *
- *   Note that the file descriptor returned by create_tempfile()
+ *   Note that the file descriptor created by create_tempfile()
  *   is marked O_CLOEXEC, so the new contents must be written by
  *   the current process, not any spawned one.
  *
  *   control of the file.
  *
  * * Close the file descriptor without removing or renaming the
- *   temporary file by calling `close_tempfile()`, and later call
+ *   temporary file by calling `close_tempfile_gently()`, and later call
  *   `delete_tempfile()` or `rename_tempfile()`.
  *
- * Even after the temporary file is renamed or deleted, the `tempfile`
- * object must not be freed or altered by the caller. However, it may
- * be reused; just pass it to another call of `create_tempfile()`.
+ * After the temporary file is renamed or deleted, the `tempfile`
+ * object is no longer valid and should not be reused.
  *
  * If the program exits before `rename_tempfile()` or
  * `delete_tempfile()` is called, an `atexit(3)` handler will close
  * and remove the temporary file.
  *
  * If you need to close the file descriptor yourself, do so by calling
- * `close_tempfile()`. You should never call `close(2)` or `fclose(3)`
+ * `close_tempfile_gently()`. You should never call `close(2)` or `fclose(3)`
  * yourself, otherwise the `struct tempfile` structure would still
  * think that the file descriptor needs to be closed, and a later
  * cleanup would result in duplicate calls to `close(2)`. Worse yet,
  * Error handling
  * --------------
  *
- * `create_tempfile()` returns a file descriptor on success or -1 on
- * failure. On errors, `errno` describes the reason for failure.
+ * `create_tempfile()` returns an allocated tempfile on success or NULL
+ * on failure. On errors, `errno` describes the reason for failure.
  *
- * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile()`
- * return 0 on success. On failure they set `errno` appropriately, do
- * their best to delete the temporary file, and return -1.
+ * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile_gently()`
+ * return 0 on success. On failure they set `errno` appropriately and return
+ * -1. `delete` and `rename` (but not `close`) do their best to delete the
+ * temporary file before returning.
  */
 
 struct tempfile {
-       struct tempfile *volatile next;
+       volatile struct volatile_list_head list;
        volatile sig_atomic_t active;
        volatile int fd;
        FILE *volatile fp;
        volatile pid_t owner;
-       char on_list;
        struct strbuf filename;
 };
 
 /*
  * Attempt to create a temporary file at the specified `path`. Return
- * a file descriptor for writing to it, or -1 on error. It is an error
- * if a file already exists at that path.
+ * a tempfile (whose "fd" member can be used for writing to it), or
+ * NULL on error. It is an error if a file already exists at that path.
  */
-extern int create_tempfile(struct tempfile *tempfile, const char *path);
+extern struct tempfile *create_tempfile(const char *path);
 
 /*
  * Register an existing file as a tempfile, meaning that it will be
@@ -102,7 +96,7 @@ extern int create_tempfile(struct tempfile *tempfile, const char *path);
  * but it can be worked with like any other closed tempfile (for
  * example, it can be opened using reopen_tempfile()).
  */
-extern void register_tempfile(struct tempfile *tempfile, const char *path);
+extern struct tempfile *register_tempfile(const char *path);
 
 
 /*
@@ -134,83 +128,78 @@ extern void register_tempfile(struct tempfile *tempfile, const char *path);
  * know the (absolute) path of the file that was created, it can be
  * read from tempfile->filename.
  *
- * On success, the functions return a file descriptor that is open for
- * writing the temporary file. On errors, they return -1 and set errno
- * appropriately (except for the "x" variants, which die() on errors).
+ * On success, the functions return a tempfile whose "fd" member is open
+ * for writing the temporary file. On errors, they return NULL and set
+ * errno appropriately (except for the "x" variants, which die() on
+ * errors).
  */
 
 /* See "mks_tempfile functions" above. */
-extern int mks_tempfile_sm(struct tempfile *tempfile,
-                          const char *template, int suffixlen, int mode);
+extern struct tempfile *mks_tempfile_sm(const char *template,
+                                       int suffixlen, int mode);
 
 /* See "mks_tempfile functions" above. */
-static inline int mks_tempfile_s(struct tempfile *tempfile,
-                                const char *template, int suffixlen)
+static inline struct tempfile *mks_tempfile_s(const char *template,
+                                             int suffixlen)
 {
-       return mks_tempfile_sm(tempfile, template, suffixlen, 0600);
+       return mks_tempfile_sm(template, suffixlen, 0600);
 }
 
 /* See "mks_tempfile functions" above. */
-static inline int mks_tempfile_m(struct tempfile *tempfile,
-                                const char *template, int mode)
+static inline struct tempfile *mks_tempfile_m(const char *template, int mode)
 {
-       return mks_tempfile_sm(tempfile, template, 0, mode);
+       return mks_tempfile_sm(template, 0, mode);
 }
 
 /* See "mks_tempfile functions" above. */
-static inline int mks_tempfile(struct tempfile *tempfile,
-                              const char *template)
+static inline struct tempfile *mks_tempfile(const char *template)
 {
-       return mks_tempfile_sm(tempfile, template, 0, 0600);
+       return mks_tempfile_sm(template, 0, 0600);
 }
 
 /* See "mks_tempfile functions" above. */
-extern int mks_tempfile_tsm(struct tempfile *tempfile,
-                           const char *template, int suffixlen, int mode);
+extern struct tempfile *mks_tempfile_tsm(const char *template,
+                                        int suffixlen, int mode);
 
 /* See "mks_tempfile functions" above. */
-static inline int mks_tempfile_ts(struct tempfile *tempfile,
-                                 const char *template, int suffixlen)
+static inline struct tempfile *mks_tempfile_ts(const char *template,
+                                              int suffixlen)
 {
-       return mks_tempfile_tsm(tempfile, template, suffixlen, 0600);
+       return mks_tempfile_tsm(template, suffixlen, 0600);
 }
 
 /* See "mks_tempfile functions" above. */
-static inline int mks_tempfile_tm(struct tempfile *tempfile,
-                                 const char *template, int mode)
+static inline struct tempfile *mks_tempfile_tm(const char *template, int mode)
 {
-       return mks_tempfile_tsm(tempfile, template, 0, mode);
+       return mks_tempfile_tsm(template, 0, mode);
 }
 
 /* See "mks_tempfile functions" above. */
-static inline int mks_tempfile_t(struct tempfile *tempfile,
-                                const char *template)
+static inline struct tempfile *mks_tempfile_t(const char *template)
 {
-       return mks_tempfile_tsm(tempfile, template, 0, 0600);
+       return mks_tempfile_tsm(template, 0, 0600);
 }
 
 /* See "mks_tempfile functions" above. */
-extern int xmks_tempfile_m(struct tempfile *tempfile,
-                          const char *template, int mode);
+extern struct tempfile *xmks_tempfile_m(const char *template, int mode);
 
 /* See "mks_tempfile functions" above. */
-static inline int xmks_tempfile(struct tempfile *tempfile,
-                               const char *template)
+static inline struct tempfile *xmks_tempfile(const char *template)
 {
-       return xmks_tempfile_m(tempfile, template, 0600);
+       return xmks_tempfile_m(template, 0600);
 }
 
 /*
  * Associate a stdio stream with the temporary file (which must still
  * be open). Return `NULL` (*without* deleting the file) on error. The
- * stream is closed automatically when `close_tempfile()` is called or
+ * stream is closed automatically when `close_tempfile_gently()` is called or
  * when the file is deleted or renamed.
  */
 extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
 
 static inline int is_tempfile_active(struct tempfile *tempfile)
 {
-       return tempfile->active;
+       return tempfile && tempfile->active;
 }
 
 /*
@@ -226,20 +215,20 @@ extern FILE *get_tempfile_fp(struct tempfile *tempfile);
  * If the temporary file is still open, close it (and the file pointer
  * too, if it has been opened using `fdopen_tempfile()`) without
  * deleting the file. Return 0 upon success. On failure to `close(2)`,
- * return a negative value and delete the file. Usually
- * `delete_tempfile()` or `rename_tempfile()` should eventually be
- * called if `close_tempfile()` succeeds.
+ * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()`
+ * should eventually be called regardless of whether `close_tempfile_gently()`
+ * succeeds.
  */
-extern int close_tempfile(struct tempfile *tempfile);
+extern int close_tempfile_gently(struct tempfile *tempfile);
 
 /*
  * Re-open a temporary file that has been closed using
- * `close_tempfile()` but not yet deleted or renamed. This can be used
+ * `close_tempfile_gently()` but not yet deleted or renamed. This can be used
  * to implement a sequence of operations like the following:
  *
  * * Create temporary file.
  *
- * * Write new contents to file, then `close_tempfile()` to cause the
+ * * Write new contents to file, then `close_tempfile_gently()` to cause the
  *   contents to be written to disk.
  *
  * * Pass the name of the temporary file to another program to allow
@@ -259,7 +248,7 @@ extern int reopen_tempfile(struct tempfile *tempfile);
  * `delete_tempfile()` for a `tempfile` object that has already been
  * deleted or renamed.
  */
-extern void delete_tempfile(struct tempfile *tempfile);
+extern void delete_tempfile(struct tempfile **tempfile_p);
 
 /*
  * Close the file descriptor and/or file pointer if they are still
@@ -270,6 +259,6 @@ extern void delete_tempfile(struct tempfile *tempfile);
  * `rename(2)`. It is a bug to call `rename_tempfile()` for a
  * `tempfile` object that is not currently active.
  */
-extern int rename_tempfile(struct tempfile *tempfile, const char *path);
+extern int rename_tempfile(struct tempfile **tempfile_p, const char *path);
 
 #endif /* TEMPFILE_H */
index b7f81c198e22e4ce578a6148fa3346becabbfb3b..db5feab8a1ee6433108ade9e165956678a3e260b 100755 (executable)
@@ -58,7 +58,7 @@ then
        not_in_topic=`git rev-list "^$topic" master`
        if test -z "$not_in_topic"
        then
-               echo >&2 "$topic is already up-to-date with master"
+               echo >&2 "$topic is already up to date with master"
                exit 1 ;# we could allow it, but there is no point.
        else
                exit 0
index 86b8f227ecab874a4af98bc5ba3164d370e5e4b5..318afe3fd865013e668fcd8a2213bc5567b46579 100755 (executable)
@@ -9,8 +9,8 @@
 #
 # To enable this hook, rename this file to "prepare-commit-msg".
 
-# This hook includes three examples.  The first comments out the
-# "Conflicts:" part of a merge commit.
+# This hook includes three examples. The first one removes the
+# "# Please enter the commit message..." help message.
 #
 # The second includes the output of "git diff --name-status -r"
 # into the message, just before the "git status" output.  It is
 # The third example adds a Signed-off-by line to the message, that can
 # still be edited.  This is rarely a good idea.
 
-case "$2,$3" in
-  merge,)
-    @PERL_PATH@ -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
+COMMIT_MSG_FILE=$1
+COMMIT_SOURCE=$2
+SHA1=$3
 
-# ,|template,)
-#   @PERL_PATH@ -i.bak -pe '
-#      print "\n" . `git diff --cached --name-status -r`
-#       if /^#/ && $first++ == 0' "$1" ;;
+@PERL_PATH@ -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"
 
-  *) ;;
-esac
+# case "$COMMIT_SOURCE,$SHA1" in
+#  ,|template,)
+#    @PERL_PATH@ -i.bak -pe '
+#       print "\n" . `git diff --cached --name-status -r`
+#       if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;;
+#  *) ;;
+# esac
 
-# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
-# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
+# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
+# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
+# if test -z "$COMMIT_SOURCE"
+# then
+#   @PERL_PATH@ -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE"
+# fi
index 751b56c009a8c2f6c3531dab65565f9e06029778..3ba157ed0d6157281f0a9e91f7b7f208652adfcc 100644 (file)
--- a/trailer.c
+++ b/trailer.c
  * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
  */
 
-enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
-enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
-                       EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
-enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
-
 struct conf_info {
        char *name;
        char *key;
        char *command;
-       enum action_where where;
-       enum action_if_exists if_exists;
-       enum action_if_missing if_missing;
+       enum trailer_where where;
+       enum trailer_if_exists if_exists;
+       enum trailer_if_missing if_missing;
 };
 
 static struct conf_info default_conf_info;
@@ -63,7 +58,7 @@ static const char *git_generated_prefixes[] = {
                pos != (head); \
                pos = is_reverse ? pos->prev : pos->next)
 
-static int after_or_end(enum action_where where)
+static int after_or_end(enum trailer_where where)
 {
        return (where == WHERE_AFTER) || (where == WHERE_END);
 }
@@ -164,13 +159,15 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val)
                fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
 }
 
-static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
+static void print_all(FILE *outfile, struct list_head *head,
+                     const struct process_trailer_options *opts)
 {
        struct list_head *pos;
        struct trailer_item *item;
        list_for_each(pos, head) {
                item = list_entry(pos, struct trailer_item, list);
-               if (!trim_empty || strlen(item->value) > 0)
+               if ((!opts->trim_empty || strlen(item->value) > 0) &&
+                   (!opts->only_trailers || item->token))
                        print_tok_val(outfile, item->token, item->value);
        }
 }
@@ -201,7 +198,7 @@ static int check_if_different(struct trailer_item *in_tok,
                              int check_all,
                              struct list_head *head)
 {
-       enum action_where where = arg_tok->conf.where;
+       enum trailer_where where = arg_tok->conf.where;
        struct list_head *next_head;
        do {
                if (same_trailer(in_tok, arg_tok))
@@ -300,13 +297,16 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
                else
                        free_arg_item(arg_tok);
                break;
+       default:
+               die("BUG: trailer.c: unhandled value %d",
+                   arg_tok->conf.if_exists);
        }
 }
 
 static void apply_arg_if_missing(struct list_head *head,
                                 struct arg_item *arg_tok)
 {
-       enum action_where where;
+       enum trailer_where where;
        struct trailer_item *to_add;
 
        switch (arg_tok->conf.if_missing) {
@@ -321,6 +321,10 @@ static void apply_arg_if_missing(struct list_head *head,
                        list_add_tail(&to_add->list, head);
                else
                        list_add(&to_add->list, head);
+               break;
+       default:
+               die("BUG: trailer.c: unhandled value %d",
+                   arg_tok->conf.if_missing);
        }
 }
 
@@ -331,7 +335,7 @@ static int find_same_and_apply_arg(struct list_head *head,
        struct trailer_item *in_tok;
        struct trailer_item *on_tok;
 
-       enum action_where where = arg_tok->conf.where;
+       enum trailer_where where = arg_tok->conf.where;
        int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
        int backwards = after_or_end(where);
        struct trailer_item *start_tok;
@@ -373,44 +377,50 @@ static void process_trailers_lists(struct list_head *head,
        }
 }
 
-static int set_where(struct conf_info *item, const char *value)
+int trailer_set_where(enum trailer_where *item, const char *value)
 {
-       if (!strcasecmp("after", value))
-               item->where = WHERE_AFTER;
+       if (!value)
+               *item = WHERE_DEFAULT;
+       else if (!strcasecmp("after", value))
+               *item = WHERE_AFTER;
        else if (!strcasecmp("before", value))
-               item->where = WHERE_BEFORE;
+               *item = WHERE_BEFORE;
        else if (!strcasecmp("end", value))
-               item->where = WHERE_END;
+               *item = WHERE_END;
        else if (!strcasecmp("start", value))
-               item->where = WHERE_START;
+               *item = WHERE_START;
        else
                return -1;
        return 0;
 }
 
-static int set_if_exists(struct conf_info *item, const char *value)
+int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
 {
-       if (!strcasecmp("addIfDifferent", value))
-               item->if_exists = EXISTS_ADD_IF_DIFFERENT;
+       if (!value)
+               *item = EXISTS_DEFAULT;
+       else if (!strcasecmp("addIfDifferent", value))
+               *item = EXISTS_ADD_IF_DIFFERENT;
        else if (!strcasecmp("addIfDifferentNeighbor", value))
-               item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
+               *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
        else if (!strcasecmp("add", value))
-               item->if_exists = EXISTS_ADD;
+               *item = EXISTS_ADD;
        else if (!strcasecmp("replace", value))
-               item->if_exists = EXISTS_REPLACE;
+               *item = EXISTS_REPLACE;
        else if (!strcasecmp("doNothing", value))
-               item->if_exists = EXISTS_DO_NOTHING;
+               *item = EXISTS_DO_NOTHING;
        else
                return -1;
        return 0;
 }
 
-static int set_if_missing(struct conf_info *item, const char *value)
+int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
 {
-       if (!strcasecmp("doNothing", value))
-               item->if_missing = MISSING_DO_NOTHING;
+       if (!value)
+               *item = MISSING_DEFAULT;
+       else if (!strcasecmp("doNothing", value))
+               *item = MISSING_DO_NOTHING;
        else if (!strcasecmp("add", value))
-               item->if_missing = MISSING_ADD;
+               *item = MISSING_ADD;
        else
                return -1;
        return 0;
@@ -470,15 +480,18 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v
        variable_name = strrchr(trailer_item, '.');
        if (!variable_name) {
                if (!strcmp(trailer_item, "where")) {
-                       if (set_where(&default_conf_info, value) < 0)
+                       if (trailer_set_where(&default_conf_info.where,
+                                             value) < 0)
                                warning(_("unknown value '%s' for key '%s'"),
                                        value, conf_key);
                } else if (!strcmp(trailer_item, "ifexists")) {
-                       if (set_if_exists(&default_conf_info, value) < 0)
+                       if (trailer_set_if_exists(&default_conf_info.if_exists,
+                                                 value) < 0)
                                warning(_("unknown value '%s' for key '%s'"),
                                        value, conf_key);
                } else if (!strcmp(trailer_item, "ifmissing")) {
-                       if (set_if_missing(&default_conf_info, value) < 0)
+                       if (trailer_set_if_missing(&default_conf_info.if_missing,
+                                                  value) < 0)
                                warning(_("unknown value '%s' for key '%s'"),
                                        value, conf_key);
                } else if (!strcmp(trailer_item, "separators")) {
@@ -532,15 +545,15 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
                conf->command = xstrdup(value);
                break;
        case TRAILER_WHERE:
-               if (set_where(conf, value))
+               if (trailer_set_where(&conf->where, value))
                        warning(_("unknown value '%s' for key '%s'"), value, conf_key);
                break;
        case TRAILER_IF_EXISTS:
-               if (set_if_exists(conf, value))
+               if (trailer_set_if_exists(&conf->if_exists, value))
                        warning(_("unknown value '%s' for key '%s'"), value, conf_key);
                break;
        case TRAILER_IF_MISSING:
-               if (set_if_missing(conf, value))
+               if (trailer_set_if_missing(&conf->if_missing, value))
                        warning(_("unknown value '%s' for key '%s'"), value, conf_key);
                break;
        default:
@@ -555,6 +568,9 @@ static void ensure_configured(void)
                return;
 
        /* Default config must be setup first */
+       default_conf_info.where = WHERE_END;
+       default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
+       default_conf_info.if_missing = MISSING_ADD;
        git_config(git_trailer_default_config, NULL);
        git_config(git_trailer_config, NULL);
        configured = 1;
@@ -658,19 +674,27 @@ static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
 }
 
 static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
-                        const struct conf_info *conf)
+                        const struct conf_info *conf,
+                        const struct new_trailer_item *new_trailer_item)
 {
        struct arg_item *new = xcalloc(sizeof(*new), 1);
        new->token = tok;
        new->value = val;
        duplicate_conf(&new->conf, conf);
+       if (new_trailer_item) {
+               if (new_trailer_item->where != WHERE_DEFAULT)
+                       new->conf.where = new_trailer_item->where;
+               if (new_trailer_item->if_exists != EXISTS_DEFAULT)
+                       new->conf.if_exists = new_trailer_item->if_exists;
+               if (new_trailer_item->if_missing != MISSING_DEFAULT)
+                       new->conf.if_missing = new_trailer_item->if_missing;
+       }
        list_add_tail(&new->list, arg_head);
 }
 
 static void process_command_line_args(struct list_head *arg_head,
-                                     struct string_list *trailers)
+                                     struct list_head *new_trailer_head)
 {
-       struct string_list_item *tr;
        struct arg_item *item;
        struct strbuf tok = STRBUF_INIT;
        struct strbuf val = STRBUF_INIT;
@@ -690,26 +714,29 @@ static void process_command_line_args(struct list_head *arg_head,
                        add_arg_item(arg_head,
                                     xstrdup(token_from_item(item, NULL)),
                                     xstrdup(""),
-                                    &item->conf);
+                                    &item->conf, NULL);
        }
 
        /* Add an arg item for each trailer on the command line */
-       for_each_string_list_item(tr, trailers) {
-               int separator_pos = find_separator(tr->string, cl_separators);
+       list_for_each(pos, new_trailer_head) {
+               struct new_trailer_item *tr =
+                       list_entry(pos, struct new_trailer_item, list);
+               int separator_pos = find_separator(tr->text, cl_separators);
+
                if (separator_pos == 0) {
                        struct strbuf sb = STRBUF_INIT;
-                       strbuf_addstr(&sb, tr->string);
+                       strbuf_addstr(&sb, tr->text);
                        strbuf_trim(&sb);
                        error(_("empty trailer token in trailer '%.*s'"),
                              (int) sb.len, sb.buf);
                        strbuf_release(&sb);
                } else {
-                       parse_trailer(&tok, &val, &conf, tr->string,
+                       parse_trailer(&tok, &val, &conf, tr->text,
                                      separator_pos);
                        add_arg_item(arg_head,
                                     strbuf_detach(&tok, NULL),
                                     strbuf_detach(&val, NULL),
-                                    conf);
+                                    conf, tr);
                }
        }
 
@@ -885,9 +912,37 @@ static int ends_with_blank_line(const char *buf, size_t len)
        return is_blank_line(buf + ll);
 }
 
+static void unfold_value(struct strbuf *val)
+{
+       struct strbuf out = STRBUF_INIT;
+       size_t i;
+
+       strbuf_grow(&out, val->len);
+       i = 0;
+       while (i < val->len) {
+               char c = val->buf[i++];
+               if (c == '\n') {
+                       /* Collapse continuation down to a single space. */
+                       while (i < val->len && isspace(val->buf[i]))
+                               i++;
+                       strbuf_addch(&out, ' ');
+               } else {
+                       strbuf_addch(&out, c);
+               }
+       }
+
+       /* Empty lines may have left us with whitespace cruft at the edges */
+       strbuf_trim(&out);
+
+       /* output goes back to val as if we modified it in-place */
+       strbuf_swap(&out, val);
+       strbuf_release(&out);
+}
+
 static int process_input_file(FILE *outfile,
                              const char *str,
-                             struct list_head *head)
+                             struct list_head *head,
+                             const struct process_trailer_options *opts)
 {
        struct trailer_info info;
        struct strbuf tok = STRBUF_INIT;
@@ -897,9 +952,10 @@ static int process_input_file(FILE *outfile,
        trailer_info_get(&info, str);
 
        /* Print lines before the trailers as is */
-       fwrite(str, 1, info.trailer_start - str, outfile);
+       if (!opts->only_trailers)
+               fwrite(str, 1, info.trailer_start - str, outfile);
 
-       if (!info.blank_line_before_trailer)
+       if (!opts->only_trailers && !info.blank_line_before_trailer)
                fprintf(outfile, "\n");
 
        for (i = 0; i < info.trailer_nr; i++) {
@@ -911,10 +967,12 @@ static int process_input_file(FILE *outfile,
                if (separator_pos >= 1) {
                        parse_trailer(&tok, &val, NULL, trailer,
                                      separator_pos);
+                       if (opts->unfold)
+                               unfold_value(&val);
                        add_trailer_item(head,
                                         strbuf_detach(&tok, NULL),
                                         strbuf_detach(&val, NULL));
-               } else {
+               } else if (!opts->only_trailers) {
                        strbuf_addstr(&val, trailer);
                        strbuf_strip_suffix(&val, "\n");
                        add_trailer_item(head,
@@ -937,7 +995,7 @@ static void free_all(struct list_head *head)
        }
 }
 
-static struct tempfile trailers_tempfile;
+static struct tempfile *trailers_tempfile;
 
 static FILE *create_in_place_tempfile(const char *file)
 {
@@ -959,19 +1017,20 @@ static FILE *create_in_place_tempfile(const char *file)
                strbuf_add(&template, file, tail - file + 1);
        strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
 
-       xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode);
+       trailers_tempfile = xmks_tempfile_m(template.buf, st.st_mode);
        strbuf_release(&template);
-       outfile = fdopen_tempfile(&trailers_tempfile, "w");
+       outfile = fdopen_tempfile(trailers_tempfile, "w");
        if (!outfile)
                die_errno(_("could not open temporary file"));
 
        return outfile;
 }
 
-void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
+void process_trailers(const char *file,
+                     const struct process_trailer_options *opts,
+                     struct list_head *new_trailer_head)
 {
        LIST_HEAD(head);
-       LIST_HEAD(arg_head);
        struct strbuf sb = STRBUF_INIT;
        int trailer_end;
        FILE *outfile = stdout;
@@ -980,24 +1039,27 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
 
        read_input_file(&sb, file);
 
-       if (in_place)
+       if (opts->in_place)
                outfile = create_in_place_tempfile(file);
 
        /* Print the lines before the trailers */
-       trailer_end = process_input_file(outfile, sb.buf, &head);
-
-       process_command_line_args(&arg_head, trailers);
+       trailer_end = process_input_file(outfile, sb.buf, &head, opts);
 
-       process_trailers_lists(&head, &arg_head);
+       if (!opts->only_input) {
+               LIST_HEAD(arg_head);
+               process_command_line_args(&arg_head, new_trailer_head);
+               process_trailers_lists(&head, &arg_head);
+       }
 
-       print_all(outfile, &head, trim_empty);
+       print_all(outfile, &head, opts);
 
        free_all(&head);
 
        /* Print the lines after the trailers as is */
-       fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
+       if (!opts->only_trailers)
+               fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
 
-       if (in_place)
+       if (opts->in_place)
                if (rename_tempfile(&trailers_tempfile, file))
                        die_errno(_("could not rename temporary file to %s"), file);
 
@@ -1054,3 +1116,49 @@ void trailer_info_release(struct trailer_info *info)
                free(info->trailers[i]);
        free(info->trailers);
 }
+
+static void format_trailer_info(struct strbuf *out,
+                               const struct trailer_info *info,
+                               const struct process_trailer_options *opts)
+{
+       int i;
+
+       /* If we want the whole block untouched, we can take the fast path. */
+       if (!opts->only_trailers && !opts->unfold) {
+               strbuf_add(out, info->trailer_start,
+                          info->trailer_end - info->trailer_start);
+               return;
+       }
+
+       for (i = 0; i < info->trailer_nr; i++) {
+               char *trailer = info->trailers[i];
+               int separator_pos = find_separator(trailer, separators);
+
+               if (separator_pos >= 1) {
+                       struct strbuf tok = STRBUF_INIT;
+                       struct strbuf val = STRBUF_INIT;
+
+                       parse_trailer(&tok, &val, NULL, trailer, separator_pos);
+                       if (opts->unfold)
+                               unfold_value(&val);
+
+                       strbuf_addf(out, "%s: %s\n", tok.buf, val.buf);
+                       strbuf_release(&tok);
+                       strbuf_release(&val);
+
+               } else if (!opts->only_trailers) {
+                       strbuf_addstr(out, trailer);
+               }
+       }
+
+}
+
+void format_trailers_from_commit(struct strbuf *out, const char *msg,
+                                const struct process_trailer_options *opts)
+{
+       struct trailer_info info;
+
+       trailer_info_get(&info, msg);
+       format_trailer_info(out, &info, opts);
+       trailer_info_release(&info);
+}
index 65cc5d79c6cecfce5cf3302dd7dcb6323f9f85dc..6d7f8c2a52305d3d937b69a877f1ae0b7af94363 100644 (file)
--- a/trailer.h
+++ b/trailer.h
@@ -1,6 +1,33 @@
 #ifndef TRAILER_H
 #define TRAILER_H
 
+#include "list.h"
+
+enum trailer_where {
+       WHERE_DEFAULT,
+       WHERE_END,
+       WHERE_AFTER,
+       WHERE_BEFORE,
+       WHERE_START
+};
+enum trailer_if_exists {
+       EXISTS_DEFAULT,
+       EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
+       EXISTS_ADD_IF_DIFFERENT,
+       EXISTS_ADD,
+       EXISTS_REPLACE,
+       EXISTS_DO_NOTHING
+};
+enum trailer_if_missing {
+       MISSING_DEFAULT,
+       MISSING_ADD,
+       MISSING_DO_NOTHING
+};
+
+int trailer_set_where(enum trailer_where *item, const char *value);
+int trailer_set_if_exists(enum trailer_if_exists *item, const char *value);
+int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
+
 struct trailer_info {
        /*
         * True if there is a blank line before the location pointed to by
@@ -22,11 +49,50 @@ struct trailer_info {
        size_t trailer_nr;
 };
 
-void process_trailers(const char *file, int in_place, int trim_empty,
-                     struct string_list *trailers);
+/*
+ * A list that represents newly-added trailers, such as those provided
+ * with the --trailer command line option of git-interpret-trailers.
+ */
+struct new_trailer_item {
+       struct list_head list;
+
+       const char *text;
+
+       enum trailer_where where;
+       enum trailer_if_exists if_exists;
+       enum trailer_if_missing if_missing;
+};
+
+struct process_trailer_options {
+       int in_place;
+       int trim_empty;
+       int only_trailers;
+       int only_input;
+       int unfold;
+};
+
+#define PROCESS_TRAILER_OPTIONS_INIT {0}
+
+void process_trailers(const char *file,
+                     const struct process_trailer_options *opts,
+                     struct list_head *new_trailer_head);
 
 void trailer_info_get(struct trailer_info *info, const char *str);
 
 void trailer_info_release(struct trailer_info *info);
 
+/*
+ * Format the trailers from the commit msg "msg" into the strbuf "out".
+ * Note two caveats about "opts":
+ *
+ *   - this is primarily a helper for pretty.c, and not
+ *     all of the flags are supported.
+ *
+ *   - this differs from process_trailers slightly in that we always format
+ *     only the trailer block itself, even if the "only_trailers" option is not
+ *     set.
+ */
+void format_trailers_from_commit(struct strbuf *out, const char *msg,
+                                const struct process_trailer_options *opts);
+
 #endif /* TRAILER_H */
index 33cff38cc0b9b4a2d05b26544c9edda9caa33e9a..c948d5215c22fbbb3e174e219017935741e9b2d5 100644 (file)
@@ -44,8 +44,7 @@ static void sendline(struct helper_data *helper, struct strbuf *buffer)
 {
        if (debug)
                fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
-       if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
-               != buffer->len)
+       if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
                die_errno("Full write to remote helper failed");
 }
 
@@ -74,7 +73,7 @@ static void write_constant(int fd, const char *str)
 {
        if (debug)
                fprintf(stderr, "Debug: Remote helper: -> %s", str);
-       if (write_in_full(fd, str, strlen(str)) != strlen(str))
+       if (write_in_full(fd, str, strlen(str)) < 0)
                die_errno("Full write to remote helper failed");
 }
 
@@ -604,6 +603,7 @@ static int process_connect_service(struct transport *transport,
                        cmdbuf.buf);
 
 exit:
+       strbuf_release(&cmdbuf);
        fclose(input);
        return ret;
 }
@@ -927,7 +927,7 @@ static int push_refs_with_export(struct transport *transport,
                struct object_id oid;
 
                private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
-               if (private && !get_sha1(private, oid.hash)) {
+               if (private && !get_oid(private, &oid)) {
                        strbuf_addf(&buf, "^%s", private);
                        string_list_append(&revlist_args, strbuf_detach(&buf, NULL));
                        oidcpy(&ref->old_oid, &oid);
@@ -1117,6 +1117,13 @@ int transport_helper_init(struct transport *transport, const char *name)
 __attribute__((format (printf, 1, 2)))
 static void transfer_debug(const char *fmt, ...)
 {
+       /*
+        * NEEDSWORK: This function is sometimes used from multiple threads, and
+        * we end up using debug_enabled racily. That "should not matter" since
+        * we always write the same value, but it's still wrong. This function
+        * is listed in .tsan-suppressions for the time being.
+        */
+
        va_list args;
        char msgbuf[PBUFFERSIZE];
        static int debug_enabled = -1;
index d75ff0514d8d4dd05da1d618ddf2602e5a4f26a9..f1e2f61991424f3314158676754f6b8d305a31f2 100644 (file)
@@ -26,7 +26,6 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
                const char *localname;
                const char *tmp;
                const char *remotename;
-               unsigned char sha[20];
                int flag = 0;
                /*
                 * Check suitability for tracking. Must be successful /
@@ -44,7 +43,7 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
                localname = ref->peer_ref->name;
                remotename = ref->name;
                tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
-                                        sha, &flag);
+                                        NULL, &flag);
                if (tmp && flag & REF_ISSYMREF &&
                        starts_with(tmp, "refs/heads/"))
                        localname = tmp;
@@ -472,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 bd6d65a40920a547b9f27512e31341231273fa75..4bb93155bc6e0349156b21ad1287d573593a79d5 100644 (file)
@@ -421,8 +421,8 @@ static struct combine_diff_path *ll_diff_tree_paths(
         *   diff_tree_oid(parent, commit) )
         */
        for (i = 0; i < nparent; ++i)
-               tptree[i] = fill_tree_descriptor(&tp[i], parents_oid[i]->hash);
-       ttree = fill_tree_descriptor(&t, oid->hash);
+               tptree[i] = fill_tree_descriptor(&tp[i], parents_oid[i]);
+       ttree = fill_tree_descriptor(&t, oid);
 
        /* Enable recursion indefinitely */
        opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
index 6a42e402b00a3da90a008adf6e4c68dc8c45e516..684f0e33736ca237a933f4def3e1560356bde2c2 100644 (file)
@@ -78,15 +78,16 @@ int init_tree_desc_gently(struct tree_desc *desc, const void *buffer, unsigned l
        return result;
 }
 
-void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
+void *fill_tree_descriptor(struct tree_desc *desc, const struct object_id *oid)
 {
        unsigned long size = 0;
        void *buf = NULL;
 
-       if (sha1) {
-               buf = read_object_with_reference(sha1, tree_type, &size, NULL);
+       if (oid) {
+               buf = read_object_with_reference(oid->hash, tree_type, &size,
+                                                NULL);
                if (!buf)
-                       die("unable to read tree %s", sha1_to_hex(sha1));
+                       die("unable to read tree %s", oid_to_hex(oid));
        }
        init_tree_desc(desc, buf, size);
        return buf;
@@ -581,12 +582,11 @@ enum follow_symlinks_result get_tree_entry_follow_symlinks(unsigned char *tree_s
        int retval = MISSING_OBJECT;
        struct dir_state *parents = NULL;
        size_t parents_alloc = 0;
-       ssize_t parents_nr = 0;
+       size_t i, parents_nr = 0;
        unsigned char current_tree_sha1[20];
        struct strbuf namebuf = STRBUF_INIT;
        struct tree_desc t;
        int follows_remaining = GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS;
-       int i;
 
        init_tree_desc(&t, NULL, 0UL);
        strbuf_addstr(&namebuf, name);
index 68bb78b928b5059202e5672f445fd5d6e22f9921..b6bd1b4ccfbb8bb69c464ea687c63a2058a424b8 100644 (file)
@@ -42,7 +42,7 @@ int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long
 int tree_entry(struct tree_desc *, struct name_entry *);
 int tree_entry_gently(struct tree_desc *, struct name_entry *);
 
-void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
+void *fill_tree_descriptor(struct tree_desc *desc, const struct object_id *oid);
 
 struct traverse_info;
 typedef int (*traverse_callback_t)(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *);
index 862cfce661e57e50f7c6030dd5a8eda0add0cca5..71b70ccb12756cde1f1c5aeda952be6e2b91f5b8 100644 (file)
@@ -1,5 +1,6 @@
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "repository.h"
 #include "config.h"
 #include "dir.h"
 #include "tree.h"
@@ -162,7 +163,7 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
        msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'.  Cannot bind.");
 
        msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] =
-               _("Cannot update sparse checkout: the following entries are not up-to-date:\n%s");
+               _("Cannot update sparse checkout: the following entries are not up to date:\n%s");
        msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] =
                _("The following working tree files would be overwritten by sparse checkout update:\n%s");
        msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] =
@@ -255,47 +256,41 @@ static int check_submodule_move_head(const struct cache_entry *ce,
 {
        unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;
        const struct submodule *sub = submodule_from_ce(ce);
+
        if (!sub)
                return 0;
 
        if (o->reset)
                flags |= SUBMODULE_MOVE_HEAD_FORCE;
 
-       switch (sub->update_strategy.type) {
-       case SM_UPDATE_UNSPECIFIED:
-       case SM_UPDATE_CHECKOUT:
-               if (submodule_move_head(ce->name, old_id, new_id, flags))
-                       return o->gently ? -1 :
-                               add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
-               return 0;
-       case SM_UPDATE_NONE:
-               return 0;
-       case SM_UPDATE_REBASE:
-       case SM_UPDATE_MERGE:
-       case SM_UPDATE_COMMAND:
-       default:
-               warning(_("submodule update strategy not supported for submodule '%s'"), ce->name);
-               return -1;
-       }
+       if (submodule_move_head(ce->name, old_id, new_id, flags))
+               return o->gently ? -1 :
+                                  add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
+       return 0;
 }
 
-static void reload_gitmodules_file(struct index_state *index,
-                                  struct checkout *state)
+/*
+ * Preform the loading of the repository's gitmodules file.  This function is
+ * used by 'check_update()' to perform loading of the gitmodules file in two
+ * differnt situations:
+ * (1) before removing entries from the working tree if the gitmodules file has
+ *     been marked for removal.  This situation is specified by 'state' == NULL.
+ * (2) before checking out entries to the working tree if the gitmodules file
+ *     has been marked for update.  This situation is specified by 'state' != NULL.
+ */
+static void load_gitmodules_file(struct index_state *index,
+                                struct checkout *state)
 {
-       int i;
-       for (i = 0; i < index->cache_nr; i++) {
-               struct cache_entry *ce = index->cache[i];
-               if (ce->ce_flags & CE_UPDATE) {
-                       int r = strcmp(ce->name, ".gitmodules");
-                       if (r < 0)
-                               continue;
-                       else if (r == 0) {
-                               submodule_free();
-                               checkout_entry(ce, state, NULL);
-                               gitmodules_config();
-                               git_config(submodule_config, NULL);
-                       } else
-                               break;
+       int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));
+
+       if (pos >= 0) {
+               struct cache_entry *ce = index->cache[pos];
+               if (!state && ce->ce_flags & CE_WT_REMOVE) {
+                       repo_read_gitmodules(the_repository);
+               } else if (state && (ce->ce_flags & CE_UPDATE)) {
+                       submodule_free();
+                       checkout_entry(ce, state, NULL);
+                       repo_read_gitmodules(the_repository);
                }
        }
 }
@@ -308,19 +303,9 @@ static void unlink_entry(const struct cache_entry *ce)
 {
        const struct submodule *sub = submodule_from_ce(ce);
        if (sub) {
-               switch (sub->update_strategy.type) {
-               case SM_UPDATE_UNSPECIFIED:
-               case SM_UPDATE_CHECKOUT:
-               case SM_UPDATE_REBASE:
-               case SM_UPDATE_MERGE:
-                       /* state.force is set at the caller. */
-                       submodule_move_head(ce->name, "HEAD", NULL,
-                                           SUBMODULE_MOVE_HEAD_FORCE);
-                       break;
-               case SM_UPDATE_NONE:
-               case SM_UPDATE_COMMAND:
-                       return; /* Do not touch the submodule. */
-               }
+               /* state.force is set at the caller. */
+               submodule_move_head(ce->name, "HEAD", NULL,
+                                   SUBMODULE_MOVE_HEAD_FORCE);
        }
        if (!check_leading_path(ce->name, ce_namelen(ce)))
                return;
@@ -343,8 +328,7 @@ static struct progress *get_progress(struct unpack_trees_options *o)
                        total++;
        }
 
-       return start_progress_delay(_("Checking out files"),
-                                   total, 50, 1);
+       return start_delayed_progress(_("Checking out files"), total);
 }
 
 static int check_updates(struct unpack_trees_options *o)
@@ -365,6 +349,10 @@ static int check_updates(struct unpack_trees_options *o)
 
        if (o->update)
                git_attr_set_direction(GIT_ATTR_CHECKOUT, index);
+
+       if (should_update_submodules() && o->update && !o->dry_run)
+               load_gitmodules_file(index, NULL);
+
        for (i = 0; i < index->cache_nr; i++) {
                const struct cache_entry *ce = index->cache[i];
 
@@ -378,7 +366,7 @@ static int check_updates(struct unpack_trees_options *o)
        remove_scheduled_dirs();
 
        if (should_update_submodules() && o->update && !o->dry_run)
-               reload_gitmodules_file(index, &state);
+               load_gitmodules_file(index, &state);
 
        enable_delayed_checkout(&state);
        for (i = 0; i < index->cache_nr; i++) {
@@ -395,8 +383,8 @@ static int check_updates(struct unpack_trees_options *o)
                        }
                }
        }
-       errs |= finish_delayed_checkout(&state);
        stop_progress(&progress);
+       errs |= finish_delayed_checkout(&state);
        if (o->update)
                git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
        return errs != 0;
@@ -662,10 +650,10 @@ static int traverse_trees_recursive(int n, unsigned long dirmask,
                else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))
                        t[i] = t[i - 2];
                else {
-                       const unsigned char *sha1 = NULL;
+                       const struct object_id *oid = NULL;
                        if (dirmask & 1)
-                               sha1 = names[i].oid->hash;
-                       buf[nr_buf++] = fill_tree_descriptor(t+i, sha1);
+                               oid = names[i].oid;
+                       buf[nr_buf++] = fill_tree_descriptor(t + i, oid);
                }
        }
 
index 7efff2fbfd76380fd448d53173f8295d7c9c9d87..e25f725c0feaa57c9a09c976628217c99dfb5652 100644 (file)
@@ -888,7 +888,7 @@ static void receive_needs(void)
                }
 
        shallow_nr += shallows.nr;
-       free(shallows.objects);
+       object_array_clear(&shallows);
 }
 
 /* return non-zero if the ref is hidden, otherwise 0 */
@@ -965,11 +965,10 @@ static int find_symref(const char *refname, const struct object_id *oid,
 {
        const char *symref_target;
        struct string_list_item *item;
-       struct object_id unused;
 
        if ((flag & REF_ISSYMREF) == 0)
                return 0;
-       symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag);
+       symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
        if (!symref_target || (flag & REF_ISSYMREF) == 0)
                die("'%s' is a symref but it is not?", refname);
        item = string_list_append(cb_data, refname);
diff --git a/usage.c b/usage.c
index 1ea7df9a202339972ee59f35a5ba8852502c915f..cdd534c9dfc4bd38ce112da62643ab2dc7b4fbe9 100644 (file)
--- a/usage.c
+++ b/usage.c
@@ -241,3 +241,18 @@ NORETURN void BUG(const char *fmt, ...)
        va_end(ap);
 }
 #endif
+
+#ifdef SUPPRESS_ANNOTATED_LEAKS
+void unleak_memory(const void *ptr, size_t len)
+{
+       static struct suppressed_leak_root {
+               struct suppressed_leak_root *next;
+               char data[FLEX_ARRAY];
+       } *suppressed_leaks;
+       struct suppressed_leak_root *root;
+
+       FLEX_ALLOC_MEM(root, data, ptr, len);
+       root->next = suppressed_leaks;
+       suppressed_leaks = root;
+}
+#endif
index 2c1502f719a17abaef6c5c56439c560994b45d37..dbfb4e13cddceaa44feee51016c9b837f7f4fce1 100644 (file)
@@ -38,7 +38,7 @@ IPATTERN("fortran",
         "|//|\\*\\*|::|[/<>=]="),
 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
         "[^ \t-]+"),
-PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
+PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
         "[^<>= \t]+"),
 PATTERNS("java",
         "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
@@ -293,6 +293,7 @@ struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
                strbuf_addf(&name, "textconv/%s", driver->name);
                notes_cache_init(c, name.buf, driver->textconv);
                driver->textconv_cache = c;
+               strbuf_release(&name);
        }
 
        return driver;
diff --git a/utf8.c b/utf8.c
index 0c8e011a58cae3c683851007ec81828ad8284471..47a42047c814fb5c1a933d75fe1b5ecf338dfda1 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -381,7 +381,7 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
                old = src;
                n = utf8_width((const char**)&src, NULL);
                if (!src)       /* broken utf-8, do nothing */
-                       return;
+                       goto out;
                if (n && w >= pos && w < pos + width) {
                        if (subst) {
                                memcpy(dst, subst, subst_len);
@@ -397,6 +397,7 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
        }
        strbuf_setlen(&sb_dst, dst - sb_dst.buf);
        strbuf_swap(sb_src, &sb_dst);
+out:
        strbuf_release(&sb_dst);
 }
 
index ec6b350611d367fd75f909c4695d9faf2d68f91e..08d136b8cc5ccdeb858eeb60c4ad4b2e42939aab 100644 (file)
@@ -318,6 +318,7 @@ static void end_revision(const char *note_ref)
                strbuf_addf(&mark, ":%"PRIu32, rev_ctx.revision);
                fast_export_note(mark.buf, "inline");
                fast_export_buf_to_data(&rev_ctx.note);
+               strbuf_release(&mark);
        }
 }
 
index e28ffbeb096a85d67b2e885c48ab096340b5d295..70015629dc69e4bdf7f8ef1973596f3360898da3 100644 (file)
@@ -30,7 +30,7 @@ static void add_head_info(struct worktree *wt)
 
        target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
                                         "HEAD",
-                                        RESOLVE_REF_READING,
+                                        0,
                                         wt->head_sha1, &flags);
        if (!target)
                return;
@@ -307,7 +307,6 @@ const struct worktree *find_shared_symref(const char *symref,
        for (i = 0; worktrees[i]; i++) {
                struct worktree *wt = worktrees[i];
                const char *symref_target;
-               unsigned char sha1[20];
                struct ref_store *refs;
                int flags;
 
@@ -327,7 +326,7 @@ const struct worktree *find_shared_symref(const char *symref,
 
                refs = get_worktree_ref_store(wt);
                symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
-                                                       sha1, &flags);
+                                                       NULL, &flags);
                if ((flags & REF_ISSYMREF) && !strcmp(symref_target, target)) {
                        existing = wt;
                        break;
@@ -386,3 +385,25 @@ int submodule_uses_worktrees(const char *path)
        closedir(dir);
        return ret;
 }
+
+int other_head_refs(each_ref_fn fn, void *cb_data)
+{
+       struct worktree **worktrees, **p;
+       int ret = 0;
+
+       worktrees = get_worktrees(0);
+       for (p = worktrees; *p; p++) {
+               struct worktree *wt = *p;
+               struct ref_store *refs;
+
+               if (wt->is_current)
+                       continue;
+
+               refs = get_worktree_ref_store(wt);
+               ret = refs_head_ref(refs, fn, cb_data);
+               if (ret)
+                       break;
+       }
+       free_worktrees(worktrees);
+       return ret;
+}
index 5ea5e503fbe491a76d3c8ce279b7e281a5d07d3e..9276c81ae7cb5c42a86fd9ebbdb28720be409e47 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef WORKTREE_H
 #define WORKTREE_H
 
+#include "refs.h"
+
 struct worktree {
        char *path;
        char *id;
@@ -70,6 +72,12 @@ extern void free_worktrees(struct worktree **);
 extern const struct worktree *find_shared_symref(const char *symref,
                                                 const char *target);
 
+/*
+ * Similar to head_ref() for all HEADs _except_ one from the current
+ * worktree, which is covered by head_ref().
+ */
+int other_head_refs(each_ref_fn fn, void *cb_data);
+
 int is_worktree_being_rebased(const struct worktree *wt, const char *target);
 int is_worktree_being_bisected(const struct worktree *wt, const char *target);
 
index 36630e5d1855a41407e217e610aa1f293288abe1..61aba0b5c1bece4aad04bee649accae990a8ce18 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -652,7 +652,7 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...)
 void write_file_buf(const char *path, const char *buf, size_t len)
 {
        int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-       if (write_in_full(fd, buf, len) != len)
+       if (write_in_full(fd, buf, len) < 0)
                die_errno(_("could not write to %s"), path);
        if (close(fd))
                die_errno(_("could not close %s"), path);
index 77c27c51134d2feff93befeee20f4523a37d524a..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();
@@ -934,7 +932,7 @@ size_t wt_status_locate_end(const char *s, size_t len)
 
 void wt_status_add_cut_line(FILE *fp)
 {
-       const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
+       const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
        struct strbuf buf = STRBUF_INIT;
 
        fprintf(fp, "%c %s", comment_line_char, cut_line);
@@ -1026,6 +1024,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
                                 comment_line_char);
        else
                fputs("\n", s->fp);
+       strbuf_release(&sb);
 }
 
 static int has_unmerged(struct wt_status *s)
@@ -1193,6 +1192,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines)
                string_list_append(lines, line.buf);
        }
        fclose(f);
+       strbuf_release(&line);
        return 0;
 }