Merge branch 'mm/push-default-warning'
authorJunio C Hamano <gitster@pobox.com>
Fri, 26 Feb 2016 21:37:25 +0000 (13:37 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 26 Feb 2016 21:37:25 +0000 (13:37 -0800)
Across the transition at around Git version 2.0, the user used to
get a pretty loud warning when running "git push" without setting
push.default configuration variable. We no longer warn, given that
the transition is over long time ago.

* mm/push-default-warning:
push: remove "push.default is unset" warning message

170 files changed:
Documentation/RelNotes/2.8.0.txt
Documentation/config.txt
Documentation/fetch-options.txt
Documentation/git-check-ignore.txt
Documentation/git-config.txt
Documentation/git-credential-cache.txt
Documentation/git-for-each-ref.txt
Documentation/git-push.txt
Documentation/git.txt
Documentation/gitignore.txt
Documentation/merge-strategies.txt
Documentation/technical/api-argv-array.txt
alias.c
archive.c
argv-array.c
argv-array.h
attr.c
bisect.c
branch.c
branch.h
builtin/am.c
builtin/apply.c
builtin/blame.c
builtin/branch.c
builtin/check-ref-format.c
builtin/checkout.c
builtin/clean.c
builtin/clone.c
builtin/config.c
builtin/fast-export.c
builtin/fetch-pack.c
builtin/fetch.c
builtin/grep.c
builtin/help.c
builtin/index-pack.c
builtin/init-db.c
builtin/merge-base.c
builtin/merge-tree.c
builtin/merge.c
builtin/mktree.c
builtin/mv.c
builtin/pack-objects.c
builtin/pack-redundant.c
builtin/push.c
builtin/receive-pack.c
builtin/reflog.c
builtin/remote-ext.c
builtin/remote.c
builtin/rev-list.c
builtin/rev-parse.c
builtin/submodule--helper.c
builtin/worktree.c
cache-tree.c
cache.h
column.c
combine-diff.c
commit.c
compat/mingw.c
compat/precompose_utf8.c
compat/qsort.c
compat/setenv.c
compat/win32/syslog.c
config.c
connect.c
connect.h
convert.c
credential-cache--daemon.c
daemon.c
diff.c
diff.h
diffcore-delta.c
diffcore-order.c
diffcore-rename.c
dir.c
dir.h
entry.c
ewah/bitmap.c
ewah/ewah_bitmap.c
ewah/ewah_io.c
ewah/ewok.h
exec_cmd.c
exec_cmd.h
fast-import.c
fsck.c
git-compat-util.h
git-merge-one-file.sh
git-submodule.sh
git.c
graph.c
grep.c
hashmap.c
help.c
http-push.c
http.c
http.h
imap-send.c
khash.h
levenshtein.c
line-log.c
list-objects.c
list-objects.h
ll-merge.c
log-tree.c
merge-blobs.c
merge-recursive.c
merge-recursive.h
name-hash.c
notes.c
pack-bitmap-write.c
pack-bitmap.c
pack-check.c
pack-revindex.c
pager.c
pathspec.c
progress.c
reachable.c
ref-filter.c
refs.c
refs/files-backend.c
remote-curl.c
remote.c
remote.h
revision.c
revision.h
run-command.c
sequencer.c
setup.c
sha1_file.c
sha1_name.c
shallow.c
show-index.c
strbuf.c
submodule-config.c
submodule.c
t/t0027-auto-crlf.sh
t/t1300-repo-config.sh
t/t1308-config-set.sh
t/t1501-work-tree.sh [new file with mode: 0755]
t/t1501-worktree.sh [deleted file]
t/t1509-root-work-tree.sh [new file with mode: 0755]
t/t1509-root-worktree.sh [deleted file]
t/t2019-checkout-ambiguous-ref.sh
t/t2025-worktree-add.sh
t/t2027-worktree-list.sh
t/t3001-ls-files-others-exclude.sh
t/t3007-ls-files-other-negative.sh [new file with mode: 0755]
t/t3032-merge-recursive-options.sh [deleted file]
t/t3032-merge-recursive-space-options.sh [new file with mode: 0755]
t/t3034-merge-recursive-rename-options.sh [new file with mode: 0755]
t/t3200-branch.sh
t/t5505-remote.sh
t/t6133-pathspec-rev-dwim.sh [new file with mode: 0755]
t/t6302-for-each-ref-filter.sh
t/t7008-grep-binary.sh
t/t7400-submodule-basic.sh
t/t7409-submodule-detached-work-tree.sh [new file with mode: 0755]
t/t7409-submodule-detached-worktree.sh [deleted file]
t/t8005-blame-i18n.sh
t/t9200-git-cvsexportcommit.sh
test-path-utils.c
transport-helper.c
transport.c
transport.h
tree-diff.c
userdiff.h
wrapper.c
xdiff-interface.c
xdiff/xdiff.h
xdiff/xemit.c
xdiff/xmerge.c
index dd540d032738eb372ef4c774b4e767f2c09da6ad..3df3a98f70c852490868014e1748d3fbedef2925 100644 (file)
@@ -98,6 +98,18 @@ UI, Workflows & Features
    variables, serving as a reminder for those who work on multiple
    projects and do not want to put these in their $HOME/.gitconfig.
 
+ * "git fetch" and friends that make network connections can now be
+   told to only use ipv4 (or ipv6).
+
+ * Some authentication methods do not need username or password, but
+   libcurl needs some hint that it needs to perform authentication.
+   Supplying an empty username and password string is a valid way to
+   do so, but you can set the http.[<url>.]emptyAuth configuration
+   variable to achieve the same, if you find it cleaner.
+
+ * You can now set http.[<url>.]pinnedpubkey to specify the pinned
+   public key when building with recent enough versions of libcURL.
+
 
 Performance, Internal Implementation, Development Support etc.
 
@@ -157,6 +169,16 @@ Performance, Internal Implementation, Development Support etc.
    calls to strcpy(3) in "git rerere" that are already safe has been
    rewritten to avoid false wanings.
 
+ * The "name_path" API was an attempt to reduce the need to construct
+   the full path out of a series of path components while walking a
+   tree hierarchy, but over time made less efficient because the path
+   needs to be flattened, e.g. to be compared with another path that
+   is already flat.  The API has been removed and its users have been
+   rewritten to simplify the overall code complexity.
+
+ * Help those who debug http(s) part of the system.
+   (merge 0054045 sp/remote-curl-ssl-strerror later to maint).
+
 Also contains various documentation updates and code clean-ups.
 
 
@@ -171,39 +193,31 @@ notes for details).
    exporting GIT_WORK_TREE to point at the root of the working tree,
    interfering when they tried to use a different working tree without
    setting GIT_WORK_TREE environment themselves.
-   (merge df1e6ea nd/stop-setenv-work-tree later to maint).
 
  * The "exclude_list" structure has the usual "alloc, nr" pair of
    fields to be used by ALLOC_GROW(), but clear_exclude_list() forgot
    to reset 'alloc' to 0 when it cleared 'nr' to discard the managed
    array.
-   (merge 2653a8c nd/dir-exclude-cleanup later to maint).
 
  * Paths that have been told the index about with "add -N" are not
    quite yet in the index, but a few commands behaved as if they
    already are in a harmful way.
-   (merge 4d55200 nd/ita-cleanup later to maint).
 
  * "git send-email" was confused by escaped quotes stored in the alias
    files saved by "mutt", which has been corrected.
-   (merge 2c510f2 ew/send-email-mutt-alias-fix later to maint).
 
  * A few unportable C construct have been spotted by clang compiler
    and have been fixed.
-   (merge a0df2e5 jk/clang-pedantic later to maint).
 
  * The documentation has been updated to hint the connection between
    the '--signoff' option and DCO.
-   (merge b2c150d dw/signoff-doc later to maint).
 
  * "git reflog" incorrectly assumed that all objects that used to be
    at the tip of a ref must be commits, which caused it to segfault.
-   (merge aecad37 dk/reflog-walk-with-non-commit later to maint).
 
  * The ignore mechanism saw a few regressions around untracked file
    listing and sparse checkout selection areas in 2.7.0; the change
    that is responsible for the regression has been reverted.
-   (merge 8c72236 nd/exclusion-regression-fix later to maint).
 
  * Some codepaths used fopen(3) when opening a fixed path in $GIT_DIR
    (e.g. COMMIT_EDITMSG) that is meant to be left after the command is
@@ -211,7 +225,6 @@ notes for details).
    be shared with core.sharedRepository and the umask of the previous
    user is tighter.  They have been made to work better by calling
    unlink(2) and retrying after fopen(3) fails with EPERM.
-   (merge ea56518 js/fopen-harder later to maint).
 
  * Asking gitweb for a nonexistent commit left a warning in the server
    log.
@@ -219,59 +232,44 @@ notes for details).
    Somebody may want to follow this up with an additional test, perhaps?
    IIRC, we do test that no Perl warnings are given to the server log,
    so this should have been caught if our test coverage were good.
-   (merge a9eb90a ho/gitweb-squelch-undef-warning later to maint).
 
  * "git rebase", unlike all other callers of "gc --auto", did not
    ignore the exit code from "gc --auto".
-   (merge 8c24f5b jk/ok-to-fail-gc-auto-in-rebase later to maint).
 
  * Many codepaths that run "gc --auto" before exiting kept packfiles
    mapped and left the file descriptors to them open, which was not
    friendly to systems that cannot remove files that are open.  They
    now close the packs before doing so.
-   (merge d562102 js/close-packs-before-gc later to maint).
 
  * A recent optimization to filter-branch in v2.7.0 introduced a
    regression when --prune-empty filter is used, which has been
    corrected.
-   (merge 1dc413e jk/filter-branch-no-index later to maint).
 
  * The description for SANITY prerequisite the test suite uses has
    been clarified both in the comment and in the implementation.
-   (merge 719c3da jk/sanity later to maint).
 
  * "git tag" started listing a tag "foo" as "tags/foo" when a branch
    named "foo" exists in the same repository; remove this unnecessary
    disambiguation, which is a regression introduced in v2.7.0.
-   (merge 0571979 jk/list-tag-2.7-regression later to maint).
 
  * The way "git svn" uses auth parameter was broken by Subversion
    1.9.0 and later.
-   (merge 0b66415 ew/svn-1.9.0-auth later to maint).
 
  * The "split" subcommand of "git subtree" (in contrib/) incorrectly
    skipped merges when it shouldn't, which was corrected.
-   (merge 933cfeb dw/subtree-split-do-not-drop-merge later to maint).
 
  * A few options of "git diff" did not work well when the command was
    run from a subdirectory.
-   (merge a97262c nd/diff-with-path-params later to maint).
 
  * The command line completion learned a handful of additional options
    and command specific syntax.
-   (merge fa4b5e3 jk/completion-rebase later to maint).
-   (merge f7c2e1a pw/completion-show-branch later to maint).
-   (merge d7d4ca8 pw/completion-stash later to maint).
-   (merge e6414b4 tb/complete-word-diff-regex later to maint).
 
  * dirname() emulation has been added, as Msys2 lacks it.
-   (merge e7d5ce8 js/dirname-basename later to maint).
 
  * The underlying machinery used by "ls-files -o" and other commands
    have been taught not to create empty submodule ref cache for a
    directory that is not a submodule.  This removes a ton of wasted
    CPU cycles.
-   (merge a2d5156 jk/ref-cache-non-repository-optim later to maint).
 
  * "git worktree" had a broken code that attempted to auto-fix
    possible inconsistency that results from end-users moving a
@@ -280,39 +278,46 @@ notes for details).
    "mv" run by end-users who are not familiar with that fact will
    obviously not adjust them), which actually made things worse
    when triggered.
-   (merge 618244e nd/do-not-move-worktree-manually later to maint).
 
  * The low-level merge machinery has been taught to use CRLF line
    termination when inserting conflict markers to merged contents that
    are themselves CRLF line-terminated.
-   (merge 15980de js/xmerge-marker-eol later to maint).
 
  * "git push --force-with-lease" has been taught to report if the push
    needed to force (or fast-forwarded).
-   (merge b2e93f8 aw/push-force-with-lease-reporting later to maint).
 
  * The emulated "yes" command used in our test scripts has been
    tweaked not to spend too much time generating unnecessary output
    that is not used, to help those who test on Windows where it would
    not stop until it fills the pipe buffer due to lack of SIGPIPE.
-   (merge 6129c93 js/test-lib-windows-emulated-yes later to maint).
 
  * The documentation for "git clean" has been corrected; it mentioned
    that .git/modules/* are removed by giving two "-f", which has never
    been the case.
-   (merge 31e3c2d mm/clean-doc-fix later to maint).
 
  * The vimdiff backend for "git mergetool" has been tweaked to arrange
    and number buffers in the order that would match the expectation of
    majority of people who read left to right, then top down and assign
    buffers 1 2 3 4 "mentally" to local base remote merge windows based
    on that order.
-   (merge 2300328 dw/mergetool-vim-window-shuffle later to maint).
+
+ * "git show 'HEAD:Foo[BAR]Baz'" did not interpret the argument as a
+   rev, i.e. the object named by the the pathname with wildcard
+   characters in a tree object.
+   (merge aac4fac nd/dwim-wildcards-as-pathspecs later to maint).
+
+ * "git rev-parse --git-common-dir" used in the worktree feature
+   misbehaved when run from a subdirectory.
+   (merge 17f1365 nd/git-common-dir-fix later to maint).
+
+ * Another try to add support to the ignore mechanism that lets you
+   say "this is excluded" and then later say "oh, no, this part (that
+   is a subset of the previous part) is not excluded".
+
+ * "git worktree add -B <branchname>" did not work.
+
+ * The "v(iew)" subcommand of the interactive "git am -i" command was
+   broken in 2.6.0 timeframe when the command was rewritten in C.
+   (merge 708b8cc jc/am-i-v-fix later to maint).
 
  * Other minor clean-ups and documentation updates
-   (merge 99487cf ss/user-manual later to maint).
-   (merge e914ef0 ew/for-each-ref-doc later to maint).
-   (merge 36fc7d8 sg/t6050-failing-editor-test-fix later to maint).
-   (merge 60253a6 ss/clone-depth-single-doc later to maint).
-   (merge bd02e97 lv/add-doc-working-tree later to maint).
-   (merge f562d7d ah/stripspace-optstring later to maint).
index 01cca0a70177091f4580a06ea53e498d3e5be4e4..2cd6bdd7d2bc2816c1a9aed1c6a26bbd3285777c 100644 (file)
@@ -1648,6 +1648,12 @@ http.proxyAuthMethod::
 * `ntlm` - NTLM authentication (compare the --ntlm option of `curl(1)`)
 --
 
+http.emptyAuth::
+       Attempt authentication without seeking a username or password.  This
+       can be used to attempt GSS-Negotiate authentication without specifying
+       a username in the URL, as libcurl normally requires a username for
+       authentication.
+
 http.cookieFile::
        File containing previously stored cookie lines which should be used
        in the Git http session, if they match the server. The file format
@@ -1727,6 +1733,14 @@ http.sslCAPath::
        with when fetching or pushing over HTTPS. Can be overridden
        by the 'GIT_SSL_CAPATH' environment variable.
 
+http.pinnedpubkey::
+       Public key of the https service. It may either be the filename of
+       a PEM or DER encoded public key file or a string starting with
+       'sha256//' followed by the base64 encoded sha256 hash of the
+       public key. See also libcurl 'CURLOPT_PINNEDPUBLICKEY'. git will
+       exit with an error if this option is set but not supported by
+       cURL.
+
 http.sslTry::
        Attempt to use AUTH SSL/TLS and encrypted data transfers
        when connecting via regular FTP protocol. This might be needed
index 952dfdfef098ca00b7976ef9d549401f171514f6..036edfb099c367262f34d2c1275ac5079a1e0914 100644 (file)
@@ -158,3 +158,11 @@ endif::git-pull[]
        by default when it is attached to a terminal, unless -q
        is specified. This flag forces progress status even if the
        standard error stream is not directed to a terminal.
+
+-4::
+--ipv4::
+       Use IPv4 addresses only, ignoring IPv6 addresses.
+
+-6::
+--ipv6::
+       Use IPv6 addresses only, ignoring IPv4 addresses.
index e94367a5ed8e8b94bca7036ff7616bc3cfa9ca97..f60ee051f8c0e930e790add0bd1be39f49c82785 100644 (file)
@@ -114,6 +114,7 @@ SEE ALSO
 linkgit:gitignore[5]
 linkgit:gitconfig[5]
 linkgit:git-ls-files[1]
+GIT_TRACE_EXCLUDE in linkgit:git[1]
 
 GIT
 ---
index 242fa5d1bb2b7eff9645e140613640d94d28852f..153b2d89b551b5c249c6a56604726f8175b0b7c6 100644 (file)
@@ -9,18 +9,18 @@ git-config - Get and set repository or global options
 SYNOPSIS
 --------
 [verse]
-'git config' [<file-option>] [type] [-z|--null] name [value [value_regex]]
+'git config' [<file-option>] [type] [--show-origin] [-z|--null] name [value [value_regex]]
 'git config' [<file-option>] [type] --add name value
 'git config' [<file-option>] [type] --replace-all name value [value_regex]
-'git config' [<file-option>] [type] [-z|--null] --get name [value_regex]
-'git config' [<file-option>] [type] [-z|--null] --get-all name [value_regex]
-'git config' [<file-option>] [type] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
+'git config' [<file-option>] [type] [--show-origin] [-z|--null] --get name [value_regex]
+'git config' [<file-option>] [type] [--show-origin] [-z|--null] --get-all name [value_regex]
+'git config' [<file-option>] [type] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
 'git config' [<file-option>] [type] [-z|--null] --get-urlmatch name URL
 'git config' [<file-option>] --unset name [value_regex]
 'git config' [<file-option>] --unset-all name [value_regex]
 'git config' [<file-option>] --rename-section old_name new_name
 'git config' [<file-option>] --remove-section name
-'git config' [<file-option>] [-z|--null] [--name-only] -l | --list
+'git config' [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
 'git config' [<file-option>] --get-color name [default]
 'git config' [<file-option>] --get-colorbool name [stdout-is-tty]
 'git config' [<file-option>] -e | --edit
@@ -194,6 +194,12 @@ See also <<FILES>>.
        Output only the names of config variables for `--list` or
        `--get-regexp`.
 
+--show-origin::
+       Augment the output of all queried config options with the
+       origin type (file, standard input, blob, command line) and
+       the actual origin (config file path, ref, or blob id if
+       applicable).
+
 --get-colorbool name [stdout-is-tty]::
 
        Find the color setting for `name` (e.g. `color.diff`) and output
index 89b730632d603771f36d2c93722f57fbecb1b313..96208f822e0995f97664423038abe1f406431986 100644 (file)
@@ -36,7 +36,7 @@ OPTIONS
        cache daemon if one is not started). Defaults to
        `~/.git-credential-cache/socket`. If your home directory is on a
        network-mounted filesystem, you may need to change this to a
-       local filesystem.
+       local filesystem. You must specify an absolute path.
 
 CONTROLLING THE DAEMON
 ----------------------
index 2e3e96f663e2ffa678f7242a661cf1f68f9c5d77..012e8f9a080d2dc386d2f878c2bc9812ba6113f8 100644 (file)
@@ -133,14 +133,18 @@ color::
 
 align::
        Left-, middle-, or right-align the content between
-       %(align:...) and %(end). The "align:" is followed by `<width>`
-       and `<position>` in any order separated by a comma, where the
-       `<position>` is either left, right or middle, default being
-       left and `<width>` is the total length of the content with
-       alignment. If the contents length is more than the width then
-       no alignment is performed. If used with '--quote' everything
-       in between %(align:...) and %(end) is quoted, but if nested
-       then only the topmost level performs quoting.
+       %(align:...) and %(end). The "align:" is followed by
+       `width=<width>` and `position=<position>` in any order
+       separated by a comma, where the `<position>` is either left,
+       right or middle, default being left and `<width>` is the total
+       length of the content with alignment. For brevity, the
+       "width=" and/or "position=" prefixes may be omitted, and bare
+       <width> and <position> used instead.  For instance,
+       `%(align:<width>,<position>)`. If the contents length is more
+       than the width then no alignment is performed. If used with
+       '--quote' everything in between %(align:...) and %(end) is
+       quoted, but if nested then only the topmost level performs
+       quoting.
 
 In addition to the above, for commit and tag objects, the header
 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
index 32482cec42d3ea5661813097919ff38657c2e01c..cf6ee4a4df5f91f585591c314854ed1a332d8909 100644 (file)
@@ -37,6 +37,13 @@ the default `<refspec>` by consulting `remote.*.push` configuration,
 and if it is not found, honors `push.default` configuration to decide
 what to push (See linkgit:git-config[1] for the meaning of `push.default`).
 
+When neither the command-line nor the configuration specify what to
+push, the default behavior is used, which corresponds to the `simple`
+value for `push.default`: the current branch is pushed to the
+corresponding upstream branch, but as a safety measure, the push is
+aborted if the upstream branch does not have the same name as the
+local one.
+
 
 OPTIONS[[OPTIONS]]
 ------------------
@@ -277,6 +284,13 @@ origin +master` to force a push to the `master` branch). See the
        default is --verify, giving the hook a chance to prevent the
        push.  With --no-verify, the hook is bypassed completely.
 
+-4::
+--ipv4::
+       Use IPv4 addresses only, ignoring IPv6 addresses.
+
+-6::
+--ipv6::
+       Use IPv6 addresses only, ignoring IPv4 addresses.
 
 include::urls-remotes.txt[]
 
index 9dffb4c03577770b0a2942938211ee05876003db..2754af8f7782d4a13fce4f5752737e3106491f22 100644 (file)
@@ -1065,6 +1065,11 @@ of clones and fetches.
        cloning of shallow repositories.
        See 'GIT_TRACE' for available trace output options.
 
+'GIT_TRACE_EXCLUDE'::
+       Enables trace messages that can help debugging .gitignore
+       processing. See 'GIT_TRACE' for available trace output
+       options.
+
 'GIT_LITERAL_PATHSPECS'::
        Setting this variable to `1` will cause Git to treat all
        pathspecs literally, rather than as glob patterns. For example,
index 473623d6318a859c9ed2cf600222ea6cb4a25d4c..3ded6fdc9972bb72a0efea73f0a2c73ae3e9906d 100644 (file)
@@ -82,12 +82,12 @@ PATTERN FORMAT
 
  - An optional prefix "`!`" which negates the pattern; any
    matching file excluded by a previous pattern will become
-   included again. It is not possible to re-include a file if a parent
-   directory of that file is excluded. Git doesn't list excluded
-   directories for performance reasons, so any patterns on contained
-   files have no effect, no matter where they are defined.
+   included again.
    Put a backslash ("`\`") in front of the first "`!`" for patterns
    that begin with a literal "`!`", for example, "`\!important!.txt`".
+   It is possible to re-include a file if a parent directory of that
+   file is excluded if certain conditions are met. See section NOTES
+   for detail.
 
  - If the pattern ends with a slash, it is removed for the
    purpose of the following description, but it would only find
@@ -141,6 +141,15 @@ not tracked by Git remain untracked.
 To stop tracking a file that is currently tracked, use
 'git rm --cached'.
 
+To re-include files or directories when their parent directory is
+excluded, the following conditions must be met:
+
+ - The rules to exclude a directory and re-include a subset back must
+   be in the same .gitignore file.
+
+ - The directory part in the re-include rules must be literal (i.e. no
+   wildcards)
+
 EXAMPLES
 --------
 
index 7bbd19b30032ca76966cbcf44a0b5b738c8f5ced..2eb92b93274df9fb5002336114654fe869903808 100644 (file)
@@ -81,9 +81,17 @@ no-renormalize;;
        Disables the `renormalize` option.  This overrides the
        `merge.renormalize` configuration variable.
 
+no-renames;;
+       Turn off rename detection.
+       See also linkgit:git-diff[1] `--no-renames`.
+
+find-renames[=<n>];;
+       Turn on rename detection, optionally setting the similarity
+       threshold.  This is the default.
+       See also linkgit:git-diff[1] `--find-renames`.
+
 rename-threshold=<n>;;
-       Controls the similarity threshold used for rename detection.
-       See also linkgit:git-diff[1] `-M`.
+       Deprecated synonym for `find-renames=<n>`.
 
 subtree[=<path>];;
        This option is a more advanced form of 'subtree' strategy, where
index 8076172a08ce0dfc80e9656cd1ee7712c32c1d3e..cfc063018c996a61b333b38d7b387f40b21e7a00 100644 (file)
@@ -56,3 +56,10 @@ Functions
 `argv_array_clear`::
        Free all memory associated with the array and return it to the
        initial, empty state.
+
+`argv_array_detach`::
+       Disconnect the `argv` member from the `argv_array` struct and
+       return it. The caller is responsible for freeing the memory used
+       by the array, and by the strings it references. After detaching,
+       the `argv_array` is in a reinitialized state and can be pushed
+       into again.
diff --git a/alias.c b/alias.c
index a11229db9e67b7b651356be3fb1d4b3d1014a8bd..3b90397a99d9f7ed4a0c1c5a83f5e69c879e752f 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)
        int src, dst, count = 0, size = 16;
        char quoted = 0;
 
-       *argv = xmalloc(sizeof(**argv) * size);
+       ALLOC_ARRAY(*argv, size);
 
        /* split alias_string */
        (*argv)[count++] = cmdline;
index 0687afae434e55d9b8a38aa5184e6e056b963dac..5d735ae6039f0d9708d61a27a490953ff27badf5 100644 (file)
--- a/archive.c
+++ b/archive.c
@@ -171,8 +171,8 @@ static void queue_directory(const unsigned char *sha1,
                unsigned mode, int stage, struct archiver_context *c)
 {
        struct directory *d;
-       size_t len = base->len + 1 + strlen(filename) + 1;
-       d = xmalloc(sizeof(*d) + len);
+       size_t len = st_add4(base->len, 1, strlen(filename), 1);
+       d = xmalloc(st_add(sizeof(*d), len));
        d->up      = c->bottom;
        d->baselen = base->len;
        d->mode    = mode;
index eaed47712b44ed1a5f9e5d35d356d0de4a5a4d54..5d370fa3366163f8c0c81ca0b6b1a64a7030c696 100644 (file)
@@ -74,3 +74,14 @@ void argv_array_clear(struct argv_array *array)
        }
        argv_array_init(array);
 }
+
+const char **argv_array_detach(struct argv_array *array)
+{
+       if (array->argv == empty_argv)
+               return xcalloc(1, sizeof(const char *));
+       else {
+               const char **ret = array->argv;
+               argv_array_init(array);
+               return ret;
+       }
+}
index a2fa0aa606a01d5277e87d2368d7d6876b699474..29056e49a1208b5506d0809c7311e4112dc1f7f3 100644 (file)
@@ -20,5 +20,6 @@ void argv_array_pushl(struct argv_array *, ...);
 void argv_array_pushv(struct argv_array *, const char **);
 void argv_array_pop(struct argv_array *);
 void argv_array_clear(struct argv_array *);
+const char **argv_array_detach(struct argv_array *);
 
 #endif /* ARGV_ARRAY_H */
diff --git a/attr.c b/attr.c
index 086c08dcfab613ef2927ff2769797fa40cd03308..6537a433da201e866208ab23bf32bcafee037683 100644 (file)
--- a/attr.c
+++ b/attr.c
@@ -93,9 +93,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
        if (invalid_attr_name(name, len))
                return NULL;
 
-       a = xmalloc(sizeof(*a) + len + 1);
-       memcpy(a->name, name, len);
-       a->name[len] = 0;
+       FLEX_ALLOC_MEM(a, name, name, len);
        a->h = hval;
        a->next = git_attr_hash[pos];
        a->attr_nr = attr_nr++;
@@ -799,7 +797,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
                        ++count;
        }
        *num = count;
-       *check = xmalloc(sizeof(**check) * count);
+       ALLOC_ARRAY(*check, count);
        j = 0;
        for (i = 0; i < attr_nr; i++) {
                const char *value = check_all_attr[i].value;
index 06ec54e599586f7cfe0f08c4b63354900af33d18..7996c2907b0e571578f2ce18095c83c134a503b3 100644 (file)
--- a/bisect.c
+++ b/bisect.c
@@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)
 
 static struct commit **get_bad_and_good_commits(int *rev_nr)
 {
-       int len = 1 + good_revs.nr;
-       struct commit **rev = xmalloc(len * sizeof(*rev));
+       struct commit **rev;
        int i, n = 0;
 
+       ALLOC_ARRAY(rev, 1 + good_revs.nr);
        rev[n++] = get_commit_reference(current_bad_oid->hash);
        for (i = 0; i < good_revs.nr; i++)
                rev[n++] = get_commit_reference(good_revs.sha1[i]);
index 7ff3f204964127374ae2a337819ce0bce7be9ab2..c50ea42172ceadd2a76d12833631301223607067 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -49,7 +49,13 @@ static int should_setup_rebase(const char *origin)
        return 0;
 }
 
-void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
+static const char tracking_advice[] =
+N_("\n"
+"After fixing the error cause you may try to fix up\n"
+"the remote tracking information by invoking\n"
+"\"git branch --set-upstream-to=%s%s%s\".");
+
+int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
 {
        const char *shortname = NULL;
        struct strbuf key = STRBUF_INIT;
@@ -60,20 +66,23 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
            && !origin) {
                warning(_("Not setting branch %s as its own upstream."),
                        local);
-               return;
+               return 0;
        }
 
        strbuf_addf(&key, "branch.%s.remote", local);
-       git_config_set(key.buf, origin ? origin : ".");
+       if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
+               goto out_err;
 
        strbuf_reset(&key);
        strbuf_addf(&key, "branch.%s.merge", local);
-       git_config_set(key.buf, remote);
+       if (git_config_set_gently(key.buf, remote) < 0)
+               goto out_err;
 
        if (rebasing) {
                strbuf_reset(&key);
                strbuf_addf(&key, "branch.%s.rebase", local);
-               git_config_set(key.buf, "true");
+               if (git_config_set_gently(key.buf, "true") < 0)
+                       goto out_err;
        }
        strbuf_release(&key);
 
@@ -102,6 +111,19 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
                                          local, remote);
                }
        }
+
+       return 0;
+
+out_err:
+       strbuf_release(&key);
+       error(_("Unable to write upstream branch configuration"));
+
+       advise(_(tracking_advice),
+              origin ? origin : "",
+              origin ? "/" : "",
+              shortname ? shortname : remote);
+
+       return -1;
 }
 
 /*
@@ -109,8 +131,8 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
  * to infer the settings for branch.<new_ref>.{remote,merge} from the
  * config.
  */
-static int setup_tracking(const char *new_ref, const char *orig_ref,
-                         enum branch_track track, int quiet)
+static void setup_tracking(const char *new_ref, const char *orig_ref,
+                          enum branch_track track, int quiet)
 {
        struct tracking tracking;
        int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
@@ -118,7 +140,7 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
        memset(&tracking, 0, sizeof(tracking));
        tracking.spec.dst = (char *)orig_ref;
        if (for_each_remote(find_tracked_branch, &tracking))
-               return 1;
+               return;
 
        if (!tracking.matches)
                switch (track) {
@@ -127,18 +149,18 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
                case BRANCH_TRACK_OVERRIDE:
                        break;
                default:
-                       return 1;
+                       return;
                }
 
        if (tracking.matches > 1)
-               return error(_("Not tracking: ambiguous information for ref %s"),
-                               orig_ref);
+               die(_("Not tracking: ambiguous information for ref %s"),
+                   orig_ref);
 
-       install_branch_config(config_flags, new_ref, tracking.remote,
-                             tracking.src ? tracking.src : orig_ref);
+       if (install_branch_config(config_flags, new_ref, tracking.remote,
+                             tracking.src ? tracking.src : orig_ref) < 0)
+               exit(-1);
 
        free(tracking.src);
-       return 0;
 }
 
 int read_branch_desc(struct strbuf *buf, const char *branch_name)
index 58aa45fe72ca356a8bd3648782b36e63e207ee36..78ad4387cd326ca01f228b109b446e2049f29ee7 100644 (file)
--- a/branch.h
+++ b/branch.h
@@ -43,9 +43,10 @@ void remove_branch_state(void);
 /*
  * Configure local branch "local" as downstream to branch "remote"
  * from remote "origin".  Used by git branch --set-upstream.
+ * Returns 0 on success.
  */
 #define BRANCH_CONFIG_VERBOSE 01
-extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);
+extern int install_branch_config(int flag, const char *local, const char *origin, const char *remote);
 
 /*
  * Read branch description
index 259dc1cd117ec18d37d4199873c11bbf70ad6dfb..d003939bc5c65947451de4a6e1817eb0f184211f 100644 (file)
@@ -1806,7 +1806,7 @@ static int do_interactive(struct am_state *state)
 
                        if (!pager)
                                pager = "cat";
-                       argv_array_push(&cp.args, pager);
+                       prepare_pager_args(&cp, pager);
                        argv_array_push(&cp.args, am_path(state, "patch"));
                        run_command(&cp);
                }
index d61ac65dab28a766a13782bdef572431e20e80ff..42c610e2ec180e789fdae4bc637dd96f533d1e10 100644 (file)
@@ -2632,7 +2632,7 @@ static void update_image(struct image *img,
        insert_count = postimage->len;
 
        /* Adjust the contents */
-       result = xmalloc(img->len + insert_count - remove_count + 1);
+       result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
        memcpy(result, img->buf, applied_at);
        memcpy(result + applied_at, postimage->buf, postimage->len);
        memcpy(result + applied_at + postimage->len,
index 55bf5fae9d5d6ea6c4e058e50d9353e77e09b34b..e982fb81379f57152e34eeda706a57fa1ea4c143 100644 (file)
@@ -466,13 +466,11 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,
 static struct origin *make_origin(struct commit *commit, const char *path)
 {
        struct origin *o;
-       size_t pathlen = strlen(path) + 1;
-       o = xcalloc(1, sizeof(*o) + pathlen);
+       FLEX_ALLOC_STR(o, path, path);
        o->commit = commit;
        o->refcnt = 1;
        o->next = commit->util;
        commit->util = o;
-       memcpy(o->path, path, pathlen); /* includes NUL */
        return o;
 }
 
@@ -2059,7 +2057,8 @@ static int prepare_lines(struct scoreboard *sb)
        for (p = buf; p < end; p = get_next_line(p, end))
                num++;
 
-       sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
+       ALLOC_ARRAY(sb->lineno, num + 1);
+       lineno = sb->lineno;
 
        for (p = buf; p < end; p = get_next_line(p, end))
                *lineno++ = p - buf;
index 3f6c825db1caf9c7cdd2ca7c1280e5ad157f9a27..7b45b6bd6b80613de9f894cfbe8e9d2dcae77b09 100644 (file)
@@ -570,7 +570,6 @@ static const char edit_description[] = "BRANCH_DESCRIPTION";
 
 static int edit_branch_description(const char *branch_name)
 {
-       int status;
        struct strbuf buf = STRBUF_INIT;
        struct strbuf name = STRBUF_INIT;
 
@@ -595,11 +594,11 @@ static int edit_branch_description(const char *branch_name)
        strbuf_stripspace(&buf, 1);
 
        strbuf_addf(&name, "branch.%s.description", branch_name);
-       status = git_config_set(name.buf, buf.len ? buf.buf : NULL);
+       git_config_set(name.buf, buf.len ? buf.buf : NULL);
        strbuf_release(&name);
        strbuf_release(&buf);
 
-       return status;
+       return 0;
 }
 
 int cmd_branch(int argc, const char **argv, const char *prefix)
index fd915d59841ecc1098e2d8a4356947215377d4da..eac499450f63554387fb8b32ef5780c32ded4a21 100644 (file)
@@ -20,7 +20,7 @@ static const char builtin_check_ref_format_usage[] =
  */
 static char *collapse_slashes(const char *refname)
 {
-       char *ret = xmalloc(strlen(refname) + 1);
+       char *ret = xmallocz(strlen(refname));
        char ch;
        char prev = '/';
        char *cp = ret;
index 5af84a3118d20da437d3e08c1667f76dc2f57845..cfa66e25eb0838ba70d87d3008c6d3c57e363bd1 100644 (file)
@@ -982,7 +982,8 @@ static int parse_branchname_arg(int argc, const char **argv,
                 */
                int recover_with_dwim = dwim_new_local_branch_ok;
 
-               if (check_filename(NULL, arg) && !has_dash_dash)
+               if (!has_dash_dash &&
+                   (check_filename(NULL, arg) || !no_wildcard(arg)))
                        recover_with_dwim = 0;
                /*
                 * Accept "git checkout foo" and "git checkout foo --"
index 7b08237480fde101640ce361b3b95106e7ffa930..0371010afbad54283ceca9883f2a9fbe6da4686b 100644 (file)
@@ -543,7 +543,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
        int eof = 0;
        int i;
 
-       chosen = xmalloc(sizeof(int) * stuff->nr);
+       ALLOC_ARRAY(chosen, stuff->nr);
        /* set chosen as uninitialized */
        for (i = 0; i < stuff->nr; i++)
                chosen[i] = -1;
@@ -615,7 +615,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
                                nr += chosen[i];
                }
 
-               result = xcalloc(nr + 1, sizeof(int));
+               result = xcalloc(st_add(nr, 1), sizeof(int));
                for (i = 0; i < stuff->nr && j < nr; i++) {
                        if (chosen[i])
                                result[j++] = i;
index bcba0805e1c9c5691c2e876f9748a2cf492b1c70..9ac6c014427908324c4a2d7ca79dbc858f5b622e 100644 (file)
@@ -47,6 +47,7 @@ static const char *real_git_dir;
 static char *option_upload_pack = "git-upload-pack";
 static int option_verbosity;
 static int option_progress = -1;
+static enum transport_family family;
 static struct string_list option_config;
 static struct string_list option_reference;
 static int option_dissociate;
@@ -92,6 +93,10 @@ static struct option builtin_clone_options[] = {
                   N_("separate git dir from working tree")),
        OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
                        N_("set config inside the new repository")),
+       OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
+                       TRANSPORT_FAMILY_IPV4),
+       OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
+                       TRANSPORT_FAMILY_IPV6),
        OPT_END()
 };
 
@@ -735,7 +740,7 @@ static int checkout(void)
 
 static int write_one_config(const char *key, const char *value, void *data)
 {
-       return git_config_set_multivar(key, value ? value : "true", "^$", 0);
+       return git_config_set_multivar_gently(key, value ? value : "true", "^$", 0);
 }
 
 static void write_config(struct string_list *config)
@@ -970,6 +975,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        remote = remote_get(option_origin);
        transport = transport_get(remote, remote->url[0]);
        transport_set_verbosity(transport, option_verbosity, option_progress);
+       transport->family = family;
 
        path = get_repo_path(remote->url[0], &is_bundle);
        is_local = option_local != 0 && path && !is_bundle;
index adc772786a7ddf9952aaccb7578b57d10396da93..8602b216d876e5f68c725bd004350d8bdfe819a5 100644 (file)
@@ -3,6 +3,7 @@
 #include "color.h"
 #include "parse-options.h"
 #include "urlmatch.h"
+#include "quote.h"
 
 static const char *const builtin_config_usage[] = {
        N_("git config [<options>]"),
@@ -27,6 +28,7 @@ static int actions, types;
 static const char *get_color_slot, *get_colorbool_slot;
 static int end_null;
 static int respect_includes = -1;
+static int show_origin;
 
 #define ACTION_GET (1<<0)
 #define ACTION_GET_ALL (1<<1)
@@ -81,6 +83,7 @@ static struct option builtin_config_options[] = {
        OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
        OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
        OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
+       OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
        OPT_END(),
 };
 
@@ -91,8 +94,28 @@ static void check_argc(int argc, int min, int max) {
        usage_with_options(builtin_config_usage, builtin_config_options);
 }
 
+static void show_config_origin(struct strbuf *buf)
+{
+       const char term = end_null ? '\0' : '\t';
+
+       strbuf_addstr(buf, current_config_origin_type());
+       strbuf_addch(buf, ':');
+       if (end_null)
+               strbuf_addstr(buf, current_config_name());
+       else
+               quote_c_style(current_config_name(), buf, NULL, 0);
+       strbuf_addch(buf, term);
+}
+
 static int show_all_config(const char *key_, const char *value_, void *cb)
 {
+       if (show_origin) {
+               struct strbuf buf = STRBUF_INIT;
+               show_config_origin(&buf);
+               /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
+               fwrite(buf.buf, 1, buf.len, stdout);
+               strbuf_release(&buf);
+       }
        if (!omit_values && value_)
                printf("%s%c%s%c", key_, delim, value_, term);
        else
@@ -108,6 +131,8 @@ struct strbuf_list {
 
 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 {
+       if (show_origin)
+               show_config_origin(buf);
        if (show_keys)
                strbuf_addstr(buf, key_);
        if (!omit_values) {
@@ -538,6 +563,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                error("--name-only is only applicable to --list or --get-regexp");
                usage_with_options(builtin_config_usage, builtin_config_options);
        }
+
+       if (show_origin && !(actions &
+               (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
+               error("--show-origin is only applicable to --get, --get-all, "
+                         "--get-regexp, and --list.");
+               usage_with_options(builtin_config_usage, builtin_config_options);
+       }
+
        if (actions == ACTION_LIST) {
                check_argc(argc, 0, 0);
                if (git_config_with_options(show_all_config, NULL,
@@ -582,7 +615,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]);
-               ret = git_config_set_in_file(given_config_source.file, argv[0], 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"
                        "       Use a regexp, --add or --replace-all to change %s.", argv[0]);
@@ -592,23 +625,23 @@ 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]);
-               return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], value, argv[2], 0);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], value, argv[2], 0);
        }
        else if (actions == ACTION_ADD) {
                check_write();
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
-               return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], value,
-                                                      CONFIG_REGEX_NONE, 0);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], value,
+                                                             CONFIG_REGEX_NONE, 0);
        }
        else if (actions == ACTION_REPLACE_ALL) {
                check_write();
                check_argc(argc, 2, 3);
                value = normalize_value(argv[0], argv[1]);
-               return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], value, argv[2], 1);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], value, argv[2], 1);
        }
        else if (actions == ACTION_GET) {
                check_argc(argc, 1, 2);
@@ -634,17 +667,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_write();
                check_argc(argc, 1, 2);
                if (argc == 2)
-                       return git_config_set_multivar_in_file(given_config_source.file,
-                                                              argv[0], NULL, argv[1], 0);
+                       return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                                     argv[0], NULL, argv[1], 0);
                else
-                       return git_config_set_in_file(given_config_source.file,
-                                                     argv[0], NULL);
+                       return git_config_set_in_file_gently(given_config_source.file,
+                                                            argv[0], NULL);
        }
        else if (actions == ACTION_UNSET_ALL) {
                check_write();
                check_argc(argc, 1, 2);
-               return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], NULL, argv[1], 1);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], NULL, argv[1], 1);
        }
        else if (actions == ACTION_RENAME_SECTION) {
                int ret;
index 2471297f7101964c61c055b10e24a7aa4b65326a..8164b581a66f257c5b4a74abbf1b76546946e2cf 100644 (file)
@@ -1021,7 +1021,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
                const char **refspecs_str;
                int i;
 
-               refspecs_str = xmalloc(sizeof(*refspecs_str) * refspecs_list.nr);
+               ALLOC_ARRAY(refspecs_str, refspecs_list.nr);
                for (i = 0; i < refspecs_list.nr; i++)
                        refspecs_str[i] = refspecs_list.items[i].string;
 
index 9b2a514e1d787784c2fac8d844d58013b6091aa6..79a611fda1f8b344ce619dd9318695a69eebb695 100644 (file)
@@ -10,33 +10,24 @@ static const char fetch_pack_usage[] =
 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
 
-static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc,
-                                const char *name, int namelen)
+static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
+                            const char *name)
 {
-       struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
+       struct ref *ref;
        struct object_id oid;
-       const int chunksz = GIT_SHA1_HEXSZ + 1;
 
-       if (namelen > chunksz && name[chunksz - 1] == ' ' &&
-               !get_oid_hex(name, &oid)) {
-               oidcpy(&ref->old_oid, &oid);
-               name += chunksz;
-               namelen -= chunksz;
-       }
+       if (!get_oid_hex(name, &oid) && name[GIT_SHA1_HEXSZ] == ' ')
+               name += GIT_SHA1_HEXSZ + 1;
+       else
+               oidclr(&oid);
 
-       memcpy(ref->name, name, namelen);
-       ref->name[namelen] = '\0';
+       ref = alloc_ref(name);
+       oidcpy(&ref->old_oid, &oid);
        (*nr)++;
        ALLOC_GROW(*sought, *nr, *alloc);
        (*sought)[*nr - 1] = ref;
 }
 
-static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
-                            const char *string)
-{
-       add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
-}
-
 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
 {
        int i, ret;
index 8e742135f049c2792bfc5a20299dac88518929cc..e4639d8eb1d5fda586520f10271c05a0897f2ea5 100644 (file)
@@ -38,6 +38,7 @@ static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosit
 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
 static int tags = TAGS_DEFAULT, unshallow, update_shallow;
 static int max_children = 1;
+static enum transport_family family;
 static const char *depth;
 static const char *upload_pack;
 static struct strbuf default_rla = STRBUF_INIT;
@@ -127,6 +128,10 @@ static struct option builtin_fetch_options[] = {
                 N_("accept refs that update .git/shallow")),
        { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
          N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
+       OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
+                       TRANSPORT_FAMILY_IPV4),
+       OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
+                       TRANSPORT_FAMILY_IPV6),
        OPT_END()
 };
 
@@ -864,6 +869,7 @@ static struct transport *prepare_transport(struct remote *remote)
        struct transport *transport;
        transport = transport_get(remote, NULL);
        transport_set_verbosity(transport, verbosity, progress);
+       transport->family = family;
        if (upload_pack)
                set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
        if (keep)
@@ -1016,10 +1022,9 @@ static int add_remote_or_group(const char *name, struct string_list *list)
 
        git_config(get_remote_group, &g);
        if (list->nr == prev_nr) {
-               struct remote *remote;
-               if (!remote_is_configured(name))
+               struct remote *remote = remote_get(name);
+               if (!remote_is_configured(remote))
                        return 0;
-               remote = remote_get(name);
                string_list_append(list, remote->name);
        }
        return 1;
@@ -1110,7 +1115,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
        if (argc > 0) {
                int j = 0;
                int i;
-               refs = xcalloc(argc + 1, sizeof(const char *));
+               refs = xcalloc(st_add(argc, 1), sizeof(const char *));
                for (i = 0; i < argc; i++) {
                        if (!strcmp(argv[i], "tag")) {
                                i++;
index 8c516a95438e5aa997049da24160764ff4ca985c..aa7435f380e95d87982cd1ae71e91bc53e77af50 100644 (file)
@@ -365,17 +365,17 @@ static void append_path(struct grep_opt *opt, const void *data, size_t len)
 static void run_pager(struct grep_opt *opt, const char *prefix)
 {
        struct string_list *path_list = opt->output_priv;
-       const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1));
+       struct child_process child = CHILD_PROCESS_INIT;
        int i, status;
 
        for (i = 0; i < path_list->nr; i++)
-               argv[i] = path_list->items[i].string;
-       argv[path_list->nr] = NULL;
+               argv_array_push(&child.args, path_list->items[i].string);
+       child.dir = prefix;
+       child.use_shell = 1;
 
-       status = run_command_v_opt_cd_env(argv, RUN_USING_SHELL, prefix, NULL);
+       status = run_command(&child);
        if (status)
                exit(status);
-       free(argv);
 }
 
 static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int cached)
index 1cd0c1ee44daf056befb8a26bba2f4fd343c8658..3c55ce456309ee7da95eac0517f11161ab3c1f7c 100644 (file)
@@ -171,12 +171,10 @@ static void exec_man_cmd(const char *cmd, const char *page)
 static void add_man_viewer(const char *name)
 {
        struct man_viewer_list **p = &man_viewer_list;
-       size_t len = strlen(name);
 
        while (*p)
                p = &((*p)->next);
-       *p = xcalloc(1, (sizeof(**p) + len + 1));
-       memcpy((*p)->name, name, len); /* NUL-terminated by xcalloc */
+       FLEX_ALLOC_STR(*p, name, name);
 }
 
 static int supported_man_viewer(const char *name, size_t len)
@@ -190,9 +188,8 @@ static void do_add_man_viewer_info(const char *name,
                                   size_t len,
                                   const char *value)
 {
-       struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
-
-       memcpy(new->name, name, len); /* NUL-terminated by xcalloc */
+       struct man_viewer_info_list *new;
+       FLEX_ALLOC_MEM(new, name, name, len);
        new->info = xstrdup(value);
        new->next = man_viewer_info_list;
        man_viewer_info_list = new;
index 6a015095877a8d201e68348ecd76ddc5331af357..193908a619307ff38bacb212bd0450aae53e2510 100644 (file)
@@ -1346,7 +1346,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
         * before deltas depending on them, a good heuristic is to start
         * resolving deltas in the same order as their position in the pack.
         */
-       sorted_by_pos = xmalloc(nr_ref_deltas * sizeof(*sorted_by_pos));
+       ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
        for (i = 0; i < nr_ref_deltas; i++)
                sorted_by_pos[i] = &ref_deltas[i];
        qsort(sorted_by_pos, nr_ref_deltas, sizeof(*sorted_by_pos), delta_pos_compare);
@@ -1744,9 +1744,9 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 
        curr_pack = open_pack_file(pack_name);
        parse_pack_header();
-       objects = xcalloc(nr_objects + 1, sizeof(struct object_entry));
+       objects = xcalloc(st_add(nr_objects, 1), sizeof(struct object_entry));
        if (show_stat)
-               obj_stat = xcalloc(nr_objects + 1, sizeof(struct object_stat));
+               obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
        ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
        parse_pack_objects(pack_sha1);
        resolve_deltas();
@@ -1759,7 +1759,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
        if (show_stat)
                show_pack_info(stat_only);
 
-       idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
+       ALLOC_ARRAY(idx_objects, nr_objects);
        for (i = 0; i < nr_objects; i++)
                idx_objects[i] = &objects[i].idx;
        curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
index 07229d60f1fd0d581c8cfae2bd0b1c21f06cbc95..6223b7d46af346b0d96870ee0c647ab9a5440b33 100644 (file)
@@ -250,7 +250,7 @@ static int create_default_files(const char *template_path)
                git_config_set("core.bare", "false");
                /* allow template config file to override the default */
                if (log_all_ref_updates == -1)
-                   git_config_set("core.logallrefupdates", "true");
+                       git_config_set("core.logallrefupdates", "true");
                if (needs_work_tree_config(get_git_dir(), work_tree))
                        git_config_set("core.worktree", work_tree);
        }
index a8911626c2ca9e178c8f4373d544c670afdb6aea..c0d1822eb3ad371b9ab0b830aed2cc2741a978bd 100644 (file)
@@ -252,7 +252,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
        if (argc < 2)
                usage_with_options(merge_base_usage, options);
 
-       rev = xmalloc(argc * sizeof(*rev));
+       ALLOC_ARRAY(rev, argc);
        while (argc-- > 0)
                rev[rev_nr++] = get_commit_reference(*argv++);
        return show_merge_base(rev, rev_nr, show_all);
index d4f0cbd45149eebe15f53ff8c34eefd9b3803d82..ca570041df0f67efa92b056cec01e7d9a4e38ad7 100644 (file)
@@ -174,7 +174,7 @@ static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsi
 
 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
 {
-       char *path = xmalloc(traverse_path_len(info, n) + 1);
+       char *path = xmallocz(traverse_path_len(info, n));
        return make_traverse_path(path, info, n);
 }
 
index b98a3489bf24c0726b3847704e7ec0b658a79e51..101ffeff4c942636e0ca688357a4b8ec8aa2a431 100644 (file)
@@ -939,7 +939,7 @@ static int setup_with_upstream(const char ***argv)
        if (!branch->merge_nr)
                die(_("No default upstream defined for the current branch."));
 
-       args = xcalloc(branch->merge_nr + 1, sizeof(char *));
+       args = xcalloc(st_add(branch->merge_nr, 1), sizeof(char *));
        for (i = 0; i < branch->merge_nr; i++) {
                if (!branch->merge[i]->dst)
                        die(_("No remote-tracking branch for %s from %s"),
index a237caacfdfc5905067bb6e3f9ae517b5f83ed17..4282b62c595edd987a87eb23ba679079211e0835 100644 (file)
@@ -19,16 +19,17 @@ static int alloc, used;
 static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
 {
        struct treeent *ent;
-       int len = strlen(path);
+       size_t len = strlen(path);
        if (strchr(path, '/'))
                die("path %s contains slash", path);
 
-       ALLOC_GROW(entries, used + 1, alloc);
-       ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
+       FLEX_ALLOC_MEM(ent, name, path, len);
        ent->mode = mode;
        ent->len = len;
        hashcpy(ent->sha1, sha1);
-       memcpy(ent->name, path, len+1);
+
+       ALLOC_GROW(entries, used + 1, alloc);
+       entries[used++] = ent;
 }
 
 static int ent_compare(const void *a_, const void *b_)
index d1d43168ae79d0157f8adf9dc36a4d2683afe91b..aeae855e2b95399db4ae6032d118848449488267 100644 (file)
@@ -24,7 +24,8 @@ static const char **internal_copy_pathspec(const char *prefix,
                                           int count, unsigned flags)
 {
        int i;
-       const char **result = xmalloc((count + 1) * sizeof(const char *));
+       const char **result;
+       ALLOC_ARRAY(result, count + 1);
        memcpy(result, pathspec, count * sizeof(const char *));
        result[count] = NULL;
        for (i = 0; i < count; i++) {
@@ -47,9 +48,9 @@ static const char **internal_copy_pathspec(const char *prefix,
 
 static const char *add_slash(const char *path)
 {
-       int len = strlen(path);
+       size_t len = strlen(path);
        if (path[len - 1] != '/') {
-               char *with_slash = xmalloc(len + 2);
+               char *with_slash = xmalloc(st_add(len, 2));
                memcpy(with_slash, path, len);
                with_slash[len++] = '/';
                with_slash[len] = 0;
index 4dae5b11c28deb3c296cbd89753cafe3ca7a1e90..a27de5b323f3fc7852a48fdc6de99414e8005c10 100644 (file)
@@ -624,7 +624,7 @@ static struct object_entry **compute_write_order(void)
 {
        unsigned int i, wo_end, last_untagged;
 
-       struct object_entry **wo = xmalloc(to_pack.nr_objects * sizeof(*wo));
+       struct object_entry **wo;
        struct object_entry *objects = to_pack.objects;
 
        for (i = 0; i < to_pack.nr_objects; i++) {
@@ -657,6 +657,7 @@ static struct object_entry **compute_write_order(void)
         * Give the objects in the original recency order until
         * we see a tagged tip.
         */
+       ALLOC_ARRAY(wo, to_pack.nr_objects);
        for (i = wo_end = 0; i < to_pack.nr_objects; i++) {
                if (objects[i].tagged)
                        break;
@@ -769,7 +770,7 @@ static void write_pack_file(void)
 
        if (progress > pack_to_stdout)
                progress_state = start_progress(_("Writing objects"), nr_result);
-       written_list = xmalloc(to_pack.nr_objects * sizeof(*written_list));
+       ALLOC_ARRAY(written_list, to_pack.nr_objects);
        write_order = compute_write_order();
 
        do {
@@ -2129,7 +2130,7 @@ static void prepare_pack(int window, int depth)
        if (!to_pack.nr_objects || !window || !depth)
                return;
 
-       delta_list = xmalloc(to_pack.nr_objects * sizeof(*delta_list));
+       ALLOC_ARRAY(delta_list, to_pack.nr_objects);
        nr_deltas = n = 0;
 
        for (i = 0; i < to_pack.nr_objects; i++) {
@@ -2284,21 +2285,11 @@ static void show_commit(struct commit *commit, void *data)
                index_commit_for_bitmap(commit);
 }
 
-static void show_object(struct object *obj,
-                       const struct name_path *path, const char *last,
-                       void *data)
+static void show_object(struct object *obj, const char *name, void *data)
 {
-       char *name = path_name(path, last);
-
        add_preferred_base_object(name);
        add_object_entry(obj->oid.hash, obj->type, name, 0);
        obj->flags |= OBJECT_ADDED;
-
-       /*
-        * We will have generated the hash from the name,
-        * but not saved a pointer to it - we can free it
-        */
-       free((char *)name);
 }
 
 static void show_edge(struct commit *commit)
@@ -2480,8 +2471,7 @@ static int get_object_list_from_bitmap(struct rev_info *revs)
 }
 
 static void record_recent_object(struct object *obj,
-                                const struct name_path *path,
-                                const char *last,
+                                const char *name,
                                 void *data)
 {
        sha1_array_append(&recent_objects, obj->oid.hash);
index d0532f66b1d4a479360263bb031e001f4cb42a46..72c815844dd2abe7f2b4bc5a641eb692d2a45103 100644 (file)
@@ -53,7 +53,7 @@ static inline struct llist_item *llist_item_get(void)
                free_nodes = free_nodes->next;
        } else {
                int i = 1;
-               new = xmalloc(sizeof(struct llist_item) * BLKSIZE);
+               ALLOC_ARRAY(new, BLKSIZE);
                for (; i < BLKSIZE; i++)
                        llist_item_put(&new[i]);
        }
index 270db40196cfc1e92a2650aedae5b792819bb959..4e9e4dbab23e5fb78239eadde724a63240e43505 100644 (file)
@@ -23,6 +23,7 @@ static const char *receivepack;
 static int verbosity;
 static int progress = -1;
 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
+static enum transport_family family;
 
 static struct push_cas_option cas;
 
@@ -312,6 +313,7 @@ static int push_with_options(struct transport *transport, int flags)
        unsigned int reject_reasons;
 
        transport_set_verbosity(transport, verbosity, progress);
+       transport->family = family;
 
        if (receivepack)
                transport_set_option(transport,
@@ -531,6 +533,10 @@ int cmd_push(int argc, const char **argv, const char *prefix)
                  0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
                  PARSE_OPT_OPTARG, option_parse_push_signed },
                OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC),
+               OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
+                               TRANSPORT_FAMILY_IPV4),
+               OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
+                               TRANSPORT_FAMILY_IPV6),
                OPT_END()
        };
 
index f2d6761af66c8bcc43c2da13f69fa083cf1e1e60..c8e32b297c4be68bb0d33a6a88c15470821ae4ea 100644 (file)
@@ -1031,7 +1031,6 @@ static void run_update_post_hook(struct command *commands)
 {
        struct command *cmd;
        int argc;
-       const char **argv;
        struct child_process proc = CHILD_PROCESS_INIT;
        const char *hook;
 
@@ -1044,21 +1043,16 @@ static void run_update_post_hook(struct command *commands)
        if (!argc || !hook)
                return;
 
-       argv = xmalloc(sizeof(*argv) * (2 + argc));
-       argv[0] = hook;
-
-       for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
+       argv_array_push(&proc.args, hook);
+       for (cmd = commands; cmd; cmd = cmd->next) {
                if (cmd->error_string || cmd->did_not_exist)
                        continue;
-               argv[argc] = xstrdup(cmd->ref_name);
-               argc++;
+               argv_array_push(&proc.args, cmd->ref_name);
        }
-       argv[argc] = NULL;
 
        proc.no_stdin = 1;
        proc.stdout_to_stderr = 1;
        proc.err = use_sideband ? -1 : 0;
-       proc.argv = argv;
 
        if (!start_command(&proc)) {
                if (use_sideband)
@@ -1378,7 +1372,7 @@ static struct command **queue_command(struct command **tail,
 
        refname = line + 82;
        reflen = linelen - 82;
-       cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
+       cmd = xcalloc(1, st_add3(sizeof(struct command), reflen, 1));
        hashcpy(cmd->old_sha1, old_sha1);
        hashcpy(cmd->new_sha1, new_sha1);
        memcpy(cmd->ref_name, refname, reflen);
@@ -1597,8 +1591,7 @@ static void prepare_shallow_update(struct command *commands,
 {
        int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
 
-       si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
-                                  si->shallow->nr);
+       ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
        assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
 
        si->need_reachability_test =
@@ -1664,7 +1657,7 @@ static void update_shallow_info(struct command *commands,
                return;
        }
 
-       ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
+       ALLOC_ARRAY(ref_status, ref->nr);
        assign_shallow_commits_to_refs(si, NULL, ref_status);
        for (cmd = commands; cmd; cmd = cmd->next) {
                if (is_null_sha1(cmd->new_sha1))
index f39960e5e480abcd08898b2f04aa444c1d7bec17..2d46b6482a0110072828e6d16da2d2fa3e0f8389 100644 (file)
@@ -382,11 +382,9 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus
 {
        struct collected_reflog *e;
        struct collect_reflog_cb *cb = cb_data;
-       size_t namelen = strlen(ref);
 
-       e = xmalloc(sizeof(*e) + namelen + 1);
+       FLEX_ALLOC_STR(e, reflog, ref);
        hashcpy(e->sha1, oid->hash);
-       memcpy(e->reflog, ref, namelen + 1);
        ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
        cb->e[cb->nr++] = e;
        return 0;
@@ -396,7 +394,6 @@ static struct reflog_expire_cfg {
        struct reflog_expire_cfg *next;
        unsigned long expire_total;
        unsigned long expire_unreachable;
-       size_t len;
        char pattern[FLEX_ARRAY];
 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
 
@@ -408,13 +405,11 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
                reflog_expire_cfg_tail = &reflog_expire_cfg;
 
        for (ent = reflog_expire_cfg; ent; ent = ent->next)
-               if (ent->len == len &&
-                   !memcmp(ent->pattern, pattern, len))
+               if (!strncmp(ent->pattern, pattern, len) &&
+                   ent->pattern[len] == '\0')
                        return ent;
 
-       ent = xcalloc(1, (sizeof(*ent) + len));
-       memcpy(ent->pattern, pattern, len);
-       ent->len = len;
+       FLEX_ALLOC_MEM(ent, pattern, pattern, len);
        *reflog_expire_cfg_tail = ent;
        reflog_expire_cfg_tail = &(ent->next);
        return ent;
index e3cd25d580b0f1ee14b4a5037ceae8dc20b31f30..7457c743e8d8539c4f08df81f86c8b27c0bce392 100644 (file)
@@ -114,30 +114,14 @@ static char *strip_escapes(const char *str, const char *service,
        }
 }
 
-/* Should be enough... */
-#define MAXARGUMENTS 256
-
-static const char **parse_argv(const char *arg, const char *service)
+static void parse_argv(struct argv_array *out, const char *arg, const char *service)
 {
-       int arguments = 0;
-       int i;
-       const char **ret;
-       char *temparray[MAXARGUMENTS + 1];
-
        while (*arg) {
-               char *expanded;
-               if (arguments == MAXARGUMENTS)
-                       die("remote-ext command has too many arguments");
-               expanded = strip_escapes(arg, service, &arg);
+               char *expanded = strip_escapes(arg, service, &arg);
                if (expanded)
-                       temparray[arguments++] = expanded;
+                       argv_array_push(out, expanded);
+               free(expanded);
        }
-
-       ret = xmalloc((arguments + 1) * sizeof(char *));
-       for (i = 0; i < arguments; i++)
-               ret[i] = temparray[i];
-       ret[arguments] = NULL;
-       return ret;
 }
 
 static void send_git_request(int stdin_fd, const char *serv, const char *repo,
@@ -158,7 +142,7 @@ static int run_child(const char *arg, const char *service)
        child.in = -1;
        child.out = -1;
        child.err = 0;
-       child.argv = parse_argv(arg, service);
+       parse_argv(&child.args, arg, service);
 
        if (start_command(&child) < 0)
                die("Can't run specified command");
index 2b2ff9b7d2152ce4ac9314bc8f111c02723703e6..fda5c2e53d28ae84f4876ca77ac9fefebee9ada2 100644 (file)
@@ -108,8 +108,8 @@ enum {
 #define MIRROR_PUSH 2
 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
 
-static int add_branch(const char *key, const char *branchname,
-               const char *remotename, int mirror, struct strbuf *tmp)
+static void add_branch(const char *key, const char *branchname,
+                      const char *remotename, int mirror, struct strbuf *tmp)
 {
        strbuf_reset(tmp);
        strbuf_addch(tmp, '+');
@@ -119,7 +119,7 @@ static int add_branch(const char *key, const char *branchname,
        else
                strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
                                branchname, remotename, branchname);
-       return git_config_set_multivar(key, tmp->buf, "^$", 0);
+       git_config_set_multivar(key, tmp->buf, "^$", 0);
 }
 
 static const char mirror_advice[] =
@@ -186,10 +186,7 @@ static int add(int argc, const char **argv)
        url = argv[1];
 
        remote = remote_get(name);
-       if (remote && (remote->url_nr > 1 ||
-                       (strcmp(name, remote->url[0]) &&
-                               strcmp(url, remote->url[0])) ||
-                       remote->fetch_refspec_nr))
+       if (remote_is_configured(remote))
                die(_("remote %s already exists."), name);
 
        strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
@@ -197,8 +194,7 @@ static int add(int argc, const char **argv)
                die(_("'%s' is not a valid remote name"), name);
 
        strbuf_addf(&buf, "remote.%s.url", name);
-       if (git_config_set(buf.buf, url))
-               return 1;
+       git_config_set(buf.buf, url);
 
        if (!mirror || mirror & MIRROR_FETCH) {
                strbuf_reset(&buf);
@@ -206,25 +202,22 @@ static int add(int argc, const char **argv)
                if (track.nr == 0)
                        string_list_append(&track, "*");
                for (i = 0; i < track.nr; i++) {
-                       if (add_branch(buf.buf, track.items[i].string,
-                                      name, mirror, &buf2))
-                               return 1;
+                       add_branch(buf.buf, track.items[i].string,
+                                  name, mirror, &buf2);
                }
        }
 
        if (mirror & MIRROR_PUSH) {
                strbuf_reset(&buf);
                strbuf_addf(&buf, "remote.%s.mirror", name);
-               if (git_config_set(buf.buf, "true"))
-                       return 1;
+               git_config_set(buf.buf, "true");
        }
 
        if (fetch_tags != TAGS_DEFAULT) {
                strbuf_reset(&buf);
                strbuf_addf(&buf, "remote.%s.tagopt", name);
-               if (git_config_set(buf.buf,
-                       fetch_tags == TAGS_SET ? "--tags" : "--no-tags"))
-                       return 1;
+               git_config_set(buf.buf,
+                              fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
        }
 
        if (fetch && fetch_remote(name))
@@ -592,25 +585,20 @@ static int migrate_file(struct remote *remote)
 
        strbuf_addf(&buf, "remote.%s.url", remote->name);
        for (i = 0; i < remote->url_nr; i++)
-               if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
-                       return error(_("Could not append '%s' to '%s'"),
-                                       remote->url[i], buf.buf);
+               git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
        strbuf_reset(&buf);
        strbuf_addf(&buf, "remote.%s.push", remote->name);
        for (i = 0; i < remote->push_refspec_nr; i++)
-               if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
-                       return error(_("Could not append '%s' to '%s'"),
-                                       remote->push_refspec[i], buf.buf);
+               git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0);
        strbuf_reset(&buf);
        strbuf_addf(&buf, "remote.%s.fetch", remote->name);
        for (i = 0; i < remote->fetch_refspec_nr; i++)
-               if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
-                       return error(_("Could not append '%s' to '%s'"),
-                                       remote->fetch_refspec[i], buf.buf);
+               git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0);
        if (remote->origin == REMOTE_REMOTES)
                unlink_or_warn(git_path("remotes/%s", remote->name));
        else if (remote->origin == REMOTE_BRANCHES)
                unlink_or_warn(git_path("branches/%s", remote->name));
+
        return 0;
 }
 
@@ -634,14 +622,14 @@ static int mv(int argc, const char **argv)
        rename.remote_branches = &remote_branches;
 
        oldremote = remote_get(rename.old);
-       if (!oldremote)
+       if (!remote_is_configured(oldremote))
                die(_("No such remote: %s"), rename.old);
 
        if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
                return migrate_file(oldremote);
 
        newremote = remote_get(rename.new);
-       if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
+       if (remote_is_configured(newremote))
                die(_("remote %s already exists."), rename.new);
 
        strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
@@ -657,8 +645,7 @@ static int mv(int argc, const char **argv)
 
        strbuf_reset(&buf);
        strbuf_addf(&buf, "remote.%s.fetch", rename.new);
-       if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
-               return error(_("Could not remove config section '%s'"), buf.buf);
+       git_config_set_multivar(buf.buf, NULL, NULL, 1);
        strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
        for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
                char *ptr;
@@ -678,8 +665,7 @@ static int mv(int argc, const char **argv)
                                  "\tPlease update the configuration manually if necessary."),
                                buf2.buf);
 
-               if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
-                       return error(_("Could not append '%s'"), buf.buf);
+               git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
        }
 
        read_branches();
@@ -689,9 +675,7 @@ static int mv(int argc, const char **argv)
                if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
                        strbuf_reset(&buf);
                        strbuf_addf(&buf, "branch.%s.remote", item->string);
-                       if (git_config_set(buf.buf, rename.new)) {
-                               return error(_("Could not set '%s'"), buf.buf);
-                       }
+                       git_config_set(buf.buf, rename.new);
                }
        }
 
@@ -773,7 +757,7 @@ static int rm(int argc, const char **argv)
                usage_with_options(builtin_remote_rm_usage, options);
 
        remote = remote_get(argv[1]);
-       if (!remote)
+       if (!remote_is_configured(remote))
                die(_("No such remote: %s"), argv[1]);
 
        known_remotes.to_delete = remote;
@@ -789,10 +773,7 @@ static int rm(int argc, const char **argv)
                                strbuf_reset(&buf);
                                strbuf_addf(&buf, "branch.%s.%s",
                                                item->string, *k);
-                               if (git_config_set(buf.buf, NULL)) {
-                                       strbuf_release(&buf);
-                                       return -1;
-                               }
+                               git_config_set(buf.buf, NULL);
                        }
                }
        }
@@ -1413,24 +1394,20 @@ static int update(int argc, const char **argv)
 
 static int remove_all_fetch_refspecs(const char *remote, const char *key)
 {
-       return git_config_set_multivar(key, NULL, NULL, 1);
+       return git_config_set_multivar_gently(key, NULL, NULL, 1);
 }
 
-static int add_branches(struct remote *remote, const char **branches,
-                       const char *key)
+static void add_branches(struct remote *remote, const char **branches,
+                        const char *key)
 {
        const char *remotename = remote->name;
        int mirror = remote->mirror;
        struct strbuf refspec = STRBUF_INIT;
 
        for (; *branches; branches++)
-               if (add_branch(key, *branches, remotename, mirror, &refspec)) {
-                       strbuf_release(&refspec);
-                       return 1;
-               }
+               add_branch(key, *branches, remotename, mirror, &refspec);
 
        strbuf_release(&refspec);
-       return 0;
 }
 
 static int set_remote_branches(const char *remotename, const char **branches,
@@ -1441,18 +1418,15 @@ static int set_remote_branches(const char *remotename, const char **branches,
 
        strbuf_addf(&key, "remote.%s.fetch", remotename);
 
-       if (!remote_is_configured(remotename))
-               die(_("No such remote '%s'"), remotename);
        remote = remote_get(remotename);
+       if (!remote_is_configured(remote))
+               die(_("No such remote '%s'"), remotename);
 
        if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
                strbuf_release(&key);
                return 1;
        }
-       if (add_branches(remote, branches, key.buf)) {
-               strbuf_release(&key);
-               return 1;
-       }
+       add_branches(remote, branches, key.buf);
 
        strbuf_release(&key);
        return 0;
@@ -1498,9 +1472,9 @@ static int get_url(int argc, const char **argv)
 
        remotename = argv[0];
 
-       if (!remote_is_configured(remotename))
-               die(_("No such remote '%s'"), remotename);
        remote = remote_get(remotename);
+       if (!remote_is_configured(remote))
+               die(_("No such remote '%s'"), remotename);
 
        url_nr = 0;
        if (push_mode) {
@@ -1566,9 +1540,9 @@ static int set_url(int argc, const char **argv)
        if (delete_mode)
                oldurl = newurl;
 
-       if (!remote_is_configured(remotename))
-               die(_("No such remote '%s'"), remotename);
        remote = remote_get(remotename);
+       if (!remote_is_configured(remote))
+               die(_("No such remote '%s'"), remotename);
 
        if (push_mode) {
                strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
@@ -1584,10 +1558,11 @@ static int set_url(int argc, const char **argv)
        if ((!oldurl && !delete_mode) || add_mode) {
                if (add_mode)
                        git_config_set_multivar(name_buf.buf, newurl,
-                               "^$", 0);
+                                                      "^$", 0);
                else
                        git_config_set(name_buf.buf, newurl);
                strbuf_release(&name_buf);
+
                return 0;
        }
 
index 3aa89a1a3cd951afdb97cf1fe371404fc96bca8d..275da0d647ebe147386c69683cc75f2d06105d23 100644 (file)
@@ -177,9 +177,7 @@ static void finish_commit(struct commit *commit, void *data)
        free_commit_buffer(commit);
 }
 
-static void finish_object(struct object *obj,
-                         const struct name_path *path, const char *name,
-                         void *cb_data)
+static void finish_object(struct object *obj, const char *name, void *cb_data)
 {
        struct rev_list_info *info = cb_data;
        if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid))
@@ -188,15 +186,13 @@ static void finish_object(struct object *obj,
                parse_object(obj->oid.hash);
 }
 
-static void show_object(struct object *obj,
-                       const struct name_path *path, const char *component,
-                       void *cb_data)
+static void show_object(struct object *obj, const char *name, void *cb_data)
 {
        struct rev_list_info *info = cb_data;
-       finish_object(obj, path, component, cb_data);
+       finish_object(obj, name, cb_data);
        if (info->flags & REV_LIST_QUIET)
                return;
-       show_object_with_name(stdout, obj, path, component);
+       show_object_with_name(stdout, obj, name);
 }
 
 static void show_edge(struct commit *commit)
index bd16876df5ff7bce4fbf348d84a0ebc4ea53e7d7..cf8487b3b95fcca9a8a96a1562646ee8ddd5b354 100644 (file)
@@ -763,7 +763,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--git-common-dir")) {
-                               puts(get_git_common_dir());
+                               const char *pfx = prefix ? prefix : "";
+                               puts(prefix_filename(pfx, strlen(pfx), get_git_common_dir()));
                                continue;
                        }
                        if (!strcmp(arg, "--resolve-git-dir")) {
index f4c3eff179b5f25e5d09c26348c107d0582455f2..ed764c9f0e5f76bbf8da7297315a4c29c0636271 100644 (file)
@@ -22,17 +22,12 @@ static int module_list_compute(int argc, const char **argv,
                               struct module_list *list)
 {
        int i, result = 0;
-       char *max_prefix, *ps_matched = NULL;
-       int max_prefix_len;
+       char *ps_matched = NULL;
        parse_pathspec(pathspec, 0,
                       PATHSPEC_PREFER_FULL |
                       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
                       prefix, argv);
 
-       /* Find common prefix for all pathspec's */
-       max_prefix = common_prefix(pathspec);
-       max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
-
        if (pathspec->nr)
                ps_matched = xcalloc(pathspec->nr, 1);
 
@@ -44,7 +39,7 @@ static int module_list_compute(int argc, const char **argv,
 
                if (!S_ISGITLINK(ce->ce_mode) ||
                    !match_pathspec(pathspec, ce->name, ce_namelen(ce),
-                                   max_prefix_len, ps_matched, 1))
+                                   0, ps_matched, 1))
                        continue;
 
                ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
@@ -57,7 +52,6 @@ static int module_list_compute(int argc, const char **argv,
                         */
                        i++;
        }
-       free(max_prefix);
 
        if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
                result = -1;
index 475b9581a5583166c43199f1662b510842c59c7d..38b56096bd3b914d1128e0bebb5c964a3cdd1b74 100644 (file)
@@ -52,7 +52,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)
                return 1;
        }
        len = st.st_size;
-       path = xmalloc(len + 1);
+       path = xmallocz(len);
        read_in_full(fd, path, len);
        close(fd);
        while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
@@ -201,9 +201,7 @@ static int add_worktree(const char *path, const char *refname,
                die(_("'%s' already exists"), path);
 
        /* is 'refname' a branch or commit? */
-       if (opts->force_new_branch) /* definitely a branch */
-               ;
-       else if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
+       if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
                 ref_exists(symref.buf)) { /* it's a branch */
                if (!opts->force)
                        die_if_checked_out(symref.buf);
@@ -336,9 +334,18 @@ static int add(int ac, const char **av, const char *prefix)
        branch = ac < 2 ? "HEAD" : av[1];
 
        opts.force_new_branch = !!new_branch_force;
-       if (opts.force_new_branch)
+       if (opts.force_new_branch) {
+               struct strbuf symref = STRBUF_INIT;
+
                opts.new_branch = new_branch_force;
 
+               if (!opts.force &&
+                   !strbuf_check_branch_ref(&symref, opts.new_branch) &&
+                   ref_exists(symref.buf))
+                       die_if_checked_out(symref.buf);
+               strbuf_release(&symref);
+       }
+
        if (ac < 2 && !opts.new_branch && !opts.detach) {
                int n;
                const char *s = worktree_basename(path, &n);
index 20ee7b52df0c33d4473a198270705dffead01cdc..3ebf9c3aa44eb2ab25f573192b6449f63311ffd7 100644 (file)
@@ -79,11 +79,9 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
        ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
        it->subtree_nr++;
 
-       down = xmalloc(sizeof(*down) + pathlen + 1);
+       FLEX_ALLOC_MEM(down, name, path, pathlen);
        down->cache_tree = NULL;
        down->namelen = pathlen;
-       memcpy(down->name, path, pathlen);
-       down->name[pathlen] = 0;
 
        if (pos < it->subtree_nr)
                memmove(it->down + pos + 1,
diff --git a/cache.h b/cache.h
index 26640b421d938a215236935d35c2d6b4f37fee6d..d7ff46ec4a016c6ab7d233b9d4a196ecde623528 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -229,7 +229,9 @@ struct cache_entry {
 #error "CE_EXTENDED_FLAGS out of range"
 #endif
 
+/* Forward structure decls */
 struct pathspec;
+struct child_process;
 
 /*
  * Copy the sha1 and stat state of a cache entry from one to
@@ -1487,7 +1489,7 @@ extern int update_server_info(int);
 /* git_config_parse_key() returns these negated: */
 #define CONFIG_INVALID_KEY 1
 #define CONFIG_NO_SECTION_OR_NAME 2
-/* git_config_set(), git_config_set_multivar() return the above or these: */
+/* git_config_set_gently(), git_config_set_multivar_gently() return the above or these: */
 #define CONFIG_NO_LOCK -1
 #define CONFIG_INVALID_FILE 3
 #define CONFIG_NO_WRITE 4
@@ -1506,8 +1508,8 @@ struct git_config_source {
 typedef int (*config_fn_t)(const char *, const char *, void *);
 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_buf(config_fn_t fn, const char *name,
-                              const char *buf, size_t len, void *data);
+extern int git_config_from_mem(config_fn_t fn, const char *origin_type,
+                                       const char *name, const char *buf, size_t len, 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 git_config(config_fn_t fn, void *);
@@ -1525,12 +1527,16 @@ 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(const char *, const char *, const char *);
-extern int git_config_set(const char *, const char *);
+extern int git_config_set_in_file_gently(const char *, const char *, const char *);
+extern void git_config_set_in_file(const char *, const char *, const char *);
+extern int git_config_set_gently(const char *, const char *);
+extern void git_config_set(const char *, const char *);
 extern int git_config_parse_key(const char *, char **, int *);
 extern int git_config_key_is_valid(const char *key);
-extern int git_config_set_multivar(const char *, const char *, const char *, int);
-extern int git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
+extern int git_config_set_multivar_gently(const char *, const char *, const char *, int);
+extern void git_config_set_multivar(const char *, const char *, const char *, int);
+extern int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, int);
+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 const char *git_etc_gitconfig(void);
@@ -1546,6 +1552,8 @@ extern const char *get_log_output_encoding(void);
 extern const char *get_commit_output_encoding(void);
 
 extern int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
+extern const char *current_config_origin_type(void);
+extern const char *current_config_name(void);
 
 struct config_include_data {
        int depth;
@@ -1685,6 +1693,7 @@ extern int pager_use_color;
 extern int term_columns(void);
 extern int decimal_width(uintmax_t);
 extern int check_pager_config(const char *cmd);
+extern void prepare_pager_args(struct child_process *, const char *pager);
 
 extern const char *editor_program;
 extern const char *askpass_program;
index 786abe62b0bb0f27b51218f7698c60476d12c8e8..d55ead18efeba9435bd2f2e76e2d7ac9ddc064c1 100644 (file)
--- a/column.c
+++ b/column.c
@@ -164,7 +164,7 @@ static void display_table(const struct string_list *list,
        data.colopts = colopts;
        data.opts = *opts;
 
-       data.len = xmalloc(sizeof(*data.len) * list->nr);
+       ALLOC_ARRAY(data.len, list->nr);
        for (i = 0; i < list->nr; i++)
                data.len[i] = item_length(colopts, list->items[i].string);
 
@@ -173,9 +173,8 @@ static void display_table(const struct string_list *list,
        if (colopts & COL_DENSE)
                shrink_columns(&data);
 
-       empty_cell = xmalloc(initial_width + 1);
+       empty_cell = xmallocz(initial_width);
        memset(empty_cell, ' ', initial_width);
-       empty_cell[initial_width] = '\0';
        for (y = 0; y < data.rows; y++) {
                for (x = 0; x < data.cols; x++)
                        if (display_cell(&data, initial_width, empty_cell, x, y))
index 55713049a4d6764722307e2a2c870996ff9b373b..0e1d4b0893ce0266a6d15f880bb6ad66eb2619fb 100644 (file)
@@ -189,11 +189,11 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,
         *   - Else if we have NEW, insert newend lline into base and
         *   consume newend
         */
-       lcs = xcalloc(origbaselen + 1, sizeof(int*));
-       directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
+       lcs = xcalloc(st_add(origbaselen, 1), sizeof(int*));
+       directions = xcalloc(st_add(origbaselen, 1), sizeof(enum coalesce_direction*));
        for (i = 0; i < origbaselen + 1; i++) {
-               lcs[i] = xcalloc(lennew + 1, sizeof(int));
-               directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
+               lcs[i] = xcalloc(st_add(lennew, 1), sizeof(int));
+               directions[i] = xcalloc(st_add(lennew, 1), sizeof(enum coalesce_direction));
                directions[i][0] = BASE;
        }
        for (j = 1; j < lennew + 1; j++)
@@ -319,7 +319,7 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
        if (line[len-1] == '\n')
                len--;
 
-       lline = xmalloc(sizeof(*lline) + len + 1);
+       FLEX_ALLOC_MEM(lline, line, line, len);
        lline->len = len;
        lline->next = NULL;
        lline->prev = sline->plost.lost_tail;
@@ -330,8 +330,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
        sline->plost.lost_tail = lline;
        sline->plost.len++;
        lline->parent_map = this_mask;
-       memcpy(lline->line, line, len);
-       lline->line[len] = 0;
 }
 
 struct combine_diff_state {
@@ -1043,7 +1041,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
                                elem->mode = canon_mode(S_IFLNK);
 
                        result_size = len;
-                       result = xmalloc(len + 1);
+                       result = xmallocz(len);
 
                        done = read_in_full(fd, result, len);
                        if (done < 0)
@@ -1051,8 +1049,6 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
                        else if (done < len)
                                die("early EOF '%s'", elem->path);
 
-                       result[len] = 0;
-
                        /* If not a fake symlink, apply filters, e.g. autocrlf */
                        if (is_file) {
                                struct strbuf buf = STRBUF_INIT;
@@ -1115,7 +1111,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
        if (result_size && result[result_size-1] != '\n')
                cnt++; /* incomplete line */
 
-       sline = xcalloc(cnt+2, sizeof(*sline));
+       sline = xcalloc(st_add(cnt, 2), sizeof(*sline));
        sline[0].bol = result;
        for (lno = 0, cp = result; cp < result + result_size; cp++) {
                if (*cp == '\n') {
@@ -1134,7 +1130,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
        /* Even p_lno[cnt+1] is valid -- that is for the end line number
         * for deletion hunk at the end.
         */
-       sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
+       sline[0].p_lno = xcalloc(st_mult(st_add(cnt, 2), num_parent), sizeof(unsigned long));
        for (lno = 0; lno <= cnt; lno++)
                sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
 
@@ -1266,7 +1262,7 @@ static struct diff_filepair *combined_pair(struct combine_diff_path *p,
        struct diff_filespec *pool;
 
        pair = xmalloc(sizeof(*pair));
-       pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
+       pool = xcalloc(st_add(num_parent, 1), sizeof(struct diff_filespec));
        pair->one = pool + 1;
        pair->two = pool;
 
@@ -1372,7 +1368,7 @@ static struct combine_diff_path *find_paths_multitree(
        struct combine_diff_path paths_head;
        struct strbuf base;
 
-       parents_sha1 = xmalloc(nparent * sizeof(parents_sha1[0]));
+       ALLOC_ARRAY(parents_sha1, nparent);
        for (i = 0; i < nparent; i++)
                parents_sha1[i] = parents->sha1[i];
 
@@ -1483,7 +1479,7 @@ void diff_tree_combined(const unsigned char *sha1,
        if (opt->orderfile && num_paths) {
                struct obj_order *o;
 
-               o = xmalloc(sizeof(*o) * num_paths);
+               ALLOC_ARRAY(o, num_paths);
                for (i = 0, p = paths; p; p = p->next, i++)
                        o[i].obj = p;
                order_objects(opt->orderfile, path_path, o, num_paths);
index 40388d71e754bb35e3879a73f6e8a0ad8904ca41..3f4f371e5eec41fa67345c0b9d373b541e52172a 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -147,7 +147,7 @@ struct commit_graft *read_graft_line(char *buf, int len)
        if ((len + 1) % entry_size)
                goto bad_graft_data;
        i = (len + 1) / entry_size - 1;
-       graft = xmalloc(sizeof(*graft) + GIT_SHA1_RAWSZ * i);
+       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;
@@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt)
 
        work = xcalloc(cnt, sizeof(*work));
        redundant = xcalloc(cnt, 1);
-       filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
+       ALLOC_ARRAY(filled_index, cnt - 1);
 
        for (i = 0; i < cnt; i++)
                parse_commit(array[i]);
index fbe69b874b06259e1d699572ace7dd8d2c4667c6..cfedcf9656549050c1d97ab69000d4279d1b0b5e 100644 (file)
@@ -810,7 +810,7 @@ static const char *quote_arg(const char *arg)
                return arg;
 
        /* insert \ where necessary */
-       d = q = xmalloc(len+n+3);
+       d = q = xmalloc(st_add3(len, n, 3));
        *d++ = '"';
        while (*arg) {
                if (*arg == '"')
@@ -893,7 +893,7 @@ static char **get_path_split(void)
        if (!n)
                return NULL;
 
-       path = xmalloc((n+1)*sizeof(char *));
+       ALLOC_ARRAY(path, n + 1);
        p = envpath;
        i = 0;
        do {
@@ -978,7 +978,7 @@ static wchar_t *make_environment_block(char **deltaenv)
                i++;
 
        /* copy the environment, leaving space for changes */
-       tmpenv = xmalloc((size + i) * sizeof(char*));
+       ALLOC_ARRAY(tmpenv, size + i);
        memcpy(tmpenv, environ, size * sizeof(char*));
 
        /* merge supplied environment changes into the temporary environment */
@@ -1069,7 +1069,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
                        free(quoted);
        }
 
-       wargs = xmalloc((2 * args.len + 1) * sizeof(wchar_t));
+       wargs = xmalloc_array(st_add(st_mult(2, args.len), 1), sizeof(wchar_t));
        xutftowcs(wargs, args.buf, 2 * args.len + 1);
        strbuf_release(&args);
 
@@ -1168,7 +1168,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
                int argc = 0;
                const char **argv2;
                while (argv[argc]) argc++;
-               argv2 = xmalloc(sizeof(*argv) * (argc+1));
+               ALLOC_ARRAY(argv2, argc + 1);
                argv2[0] = (char *)cmd; /* full path to the script file */
                memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
                pid = mingw_spawnv(prog, argv2, 1);
index 079070ff1d4bd629712eff5f6f6e239898640963..dfbe6d84081c8b8eea50450a4c23c9a80b3b57a7 100644 (file)
@@ -50,7 +50,8 @@ void probe_utf8_pathname_composition(void)
                close(output_fd);
                git_path_buf(&path, "%s", auml_nfd);
                precomposed_unicode = access(path.buf, R_OK) ? 0 : 1;
-               git_config_set("core.precomposeunicode", precomposed_unicode ? "true" : "false");
+               git_config_set("core.precomposeunicode",
+                              precomposed_unicode ? "true" : "false");
                git_path_buf(&path, "%s", auml_nfc);
                if (unlink(path.buf))
                        die_errno(_("failed to unlink '%s'"), path.buf);
index 9574d537bd38d3aa72ee040b106456088ee16a07..7d071afb70530ede222ed9cdb4645baf4277fb79 100644 (file)
@@ -47,7 +47,7 @@ static void msort_with_tmp(void *b, size_t n, size_t s,
 void git_qsort(void *b, size_t n, size_t s,
               int (*cmp)(const void *, const void *))
 {
-       const size_t size = n * s;
+       const size_t size = st_mult(n, s);
        char buf[1024];
 
        if (size < sizeof(buf)) {
index fc1439a6439393ff5224e98ab126607ceaa4994d..7849f258d20195ac27c9be921fb8b65ac094a408 100644 (file)
@@ -18,7 +18,7 @@ int gitsetenv(const char *name, const char *value, int replace)
 
        namelen = strlen(name);
        valuelen = strlen(value);
-       envstr = malloc((namelen + valuelen + 2));
+       envstr = malloc(st_add3(namelen, valuelen, 2));
        if (!envstr) {
                errno = ENOMEM;
                return -1;
index d015e436d57472bf9560a6f604705f52938c9f19..b905aea31bf53dc0f614d5e95c0dbd382818cbc4 100644 (file)
@@ -32,7 +32,7 @@ void syslog(int priority, const char *fmt, ...)
                return;
        }
 
-       str = malloc(str_len + 1);
+       str = malloc(st_add(str_len, 1));
        if (!str) {
                warning("malloc failed: '%s'", strerror(errno));
                return;
@@ -43,7 +43,7 @@ void syslog(int priority, const char *fmt, ...)
        va_end(ap);
 
        while ((pos = strstr(str, "%1")) != NULL) {
-               str = realloc(str, ++str_len + 1);
+               str = realloc(str, st_add(++str_len, 1));
                if (!str) {
                        warning("realloc failed: '%s'", strerror(errno));
                        return;
index b95ac3a9cd5ca4fef392a2202f0a5722d15b8dde..9ba40bc1b039b9b65425dc4fa1bd9c7f1fcb0868 100644 (file)
--- a/config.c
+++ b/config.c
@@ -24,6 +24,7 @@ struct config_source {
                        size_t pos;
                } buf;
        } u;
+       const char *origin_type;
        const char *name;
        const char *path;
        int die_on_error;
@@ -471,9 +472,9 @@ static int git_parse_source(config_fn_t fn, void *data)
                        break;
        }
        if (cf->die_on_error)
-               die(_("bad config file line %d in %s"), cf->linenr, cf->name);
+               die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
        else
-               return error(_("bad config file line %d in %s"), cf->linenr, cf->name);
+               return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
 }
 
 static int parse_unit_factor(const char *end, uintmax_t *val)
@@ -588,9 +589,9 @@ static void die_bad_number(const char *name, const char *value)
        if (!value)
                value = "";
 
-       if (cf && cf->name)
-               die(_("bad numeric config value '%s' for '%s' in %s: %s"),
-                   value, name, cf->name, reason);
+       if (cf && cf->origin_type && cf->name)
+               die(_("bad numeric config value '%s' for '%s' in %s %s: %s"),
+                   value, name, cf->origin_type, cf->name, reason);
        die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
 }
 
@@ -1061,11 +1062,13 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
 }
 
 static int do_config_from_file(config_fn_t fn,
-               const char *name, const char *path, FILE *f, void *data)
+               const char *origin_type, const char *name, const char *path, FILE *f,
+               void *data)
 {
        struct config_source top;
 
        top.u.file = f;
+       top.origin_type = origin_type;
        top.name = name;
        top.path = path;
        top.die_on_error = 1;
@@ -1078,7 +1081,7 @@ static int do_config_from_file(config_fn_t fn,
 
 static int git_config_from_stdin(config_fn_t fn, void *data)
 {
-       return do_config_from_file(fn, "<stdin>", NULL, stdin, data);
+       return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
 }
 
 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
@@ -1089,21 +1092,22 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
        f = fopen(filename, "r");
        if (f) {
                flockfile(f);
-               ret = do_config_from_file(fn, filename, filename, f, data);
+               ret = do_config_from_file(fn, "file", filename, filename, f, data);
                funlockfile(f);
                fclose(f);
        }
        return ret;
 }
 
-int git_config_from_buf(config_fn_t fn, const char *name, const char *buf,
-                       size_t len, void *data)
+int git_config_from_mem(config_fn_t fn, const char *origin_type,
+                       const char *name, const char *buf, size_t len, void *data)
 {
        struct config_source top;
 
        top.u.buf.buf = buf;
        top.u.buf.len = len;
        top.u.buf.pos = 0;
+       top.origin_type = origin_type;
        top.name = name;
        top.path = NULL;
        top.die_on_error = 0;
@@ -1132,7 +1136,7 @@ static int git_config_from_blob_sha1(config_fn_t fn,
                return error("reference '%s' does not point to a blob", name);
        }
 
-       ret = git_config_from_buf(fn, name, buf, size, data);
+       ret = git_config_from_mem(fn, "blob", name, buf, size, data);
        free(buf);
 
        return ret;
@@ -1849,15 +1853,26 @@ static ssize_t find_beginning_of_line(const char *contents, size_t size,
        return offset;
 }
 
-int git_config_set_in_file(const char *config_filename,
-                       const char *key, const char *value)
+int git_config_set_in_file_gently(const char *config_filename,
+                                 const char *key, const char *value)
 {
-       return git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
+       return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0);
 }
 
-int git_config_set(const char *key, const char *value)
+void git_config_set_in_file(const char *config_filename,
+                           const char *key, const char *value)
 {
-       return git_config_set_multivar(key, value, NULL, 0);
+       git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
+}
+
+int git_config_set_gently(const char *key, const char *value)
+{
+       return git_config_set_multivar_gently(key, value, NULL, 0);
+}
+
+void git_config_set(const char *key, const char *value)
+{
+       git_config_set_multivar(key, value, NULL, 0);
 }
 
 /*
@@ -1902,7 +1917,7 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *basele
         * Validate the key and while at it, lower case it for matching.
         */
        if (store_key)
-               *store_key = xmalloc(strlen(key) + 1);
+               *store_key = xmallocz(strlen(key));
 
        dot = 0;
        for (i = 0; key[i]; i++) {
@@ -1926,8 +1941,6 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *basele
                if (store_key)
                        (*store_key)[i] = c;
        }
-       if (store_key)
-               (*store_key)[i] = 0;
 
        return 0;
 
@@ -1974,9 +1987,10 @@ int git_config_key_is_valid(const char *key)
  * - the config file is removed and the lock file rename()d to it.
  *
  */
-int git_config_set_multivar_in_file(const char *config_filename,
-                               const char *key, const char *value,
-                               const char *value_regex, int multi_replace)
+int git_config_set_multivar_in_file_gently(const char *config_filename,
+                                          const char *key, const char *value,
+                                          const char *value_regex,
+                                          int multi_replace)
 {
        int fd = -1, in_fd = -1;
        int ret;
@@ -2203,11 +2217,27 @@ int git_config_set_multivar_in_file(const char *config_filename,
 
 }
 
-int git_config_set_multivar(const char *key, const char *value,
-                       const char *value_regex, int multi_replace)
+void git_config_set_multivar_in_file(const char *config_filename,
+                                    const char *key, const char *value,
+                                    const char *value_regex, int multi_replace)
+{
+       if (git_config_set_multivar_in_file_gently(config_filename, key, value,
+                                                  value_regex, multi_replace) < 0)
+               die(_("Could not set '%s' to '%s'"), key, value);
+}
+
+int git_config_set_multivar_gently(const char *key, const char *value,
+                                  const char *value_regex, int multi_replace)
 {
-       return git_config_set_multivar_in_file(NULL, key, value, value_regex,
-                                              multi_replace);
+       return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
+                                                     multi_replace);
+}
+
+void git_config_set_multivar(const char *key, const char *value,
+                            const char *value_regex, int multi_replace)
+{
+       git_config_set_multivar_in_file(NULL, key, value, value_regex,
+                                       multi_replace);
 }
 
 static int section_name_match (const char *buf, const char *name)
@@ -2409,3 +2439,13 @@ int parse_config_key(const char *var,
 
        return 0;
 }
+
+const char *current_config_origin_type(void)
+{
+       return cf && cf->origin_type ? cf->origin_type : "command line";
+}
+
+const char *current_config_name(void)
+{
+       return cf && cf->name ? cf->name : "";
+}
index fd7ffe1840e64417cf94d208840cf97c91e962b7..047863144f3f22fe36d178a00b116ee45c2e9e11 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -357,6 +357,10 @@ static int git_tcp_connect_sock(char *host, int flags)
                port = "<none>";
 
        memset(&hints, 0, sizeof(hints));
+       if (flags & CONNECT_IPV4)
+               hints.ai_family = AF_INET;
+       else if (flags & CONNECT_IPV6)
+               hints.ai_family = AF_INET6;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
 
@@ -783,6 +787,10 @@ struct child_process *git_connect(int fd[2], const char *url,
                        }
 
                        argv_array_push(&conn->args, ssh);
+                       if (flags & CONNECT_IPV4)
+                               argv_array_push(&conn->args, "-4");
+                       else if (flags & CONNECT_IPV6)
+                               argv_array_push(&conn->args, "-6");
                        if (tortoiseplink)
                                argv_array_push(&conn->args, "-batch");
                        if (port) {
index c41a6850f1302e4d27af1066b53b20dd303f0d13..01f14cdf3fa4e6b6c8cd3b4c9ec3c3d55e7fc04f 100644 (file)
--- a/connect.h
+++ b/connect.h
@@ -3,6 +3,8 @@
 
 #define CONNECT_VERBOSE       (1u << 0)
 #define CONNECT_DIAG_URL      (1u << 1)
+#define CONNECT_IPV4          (1u << 2)
+#define CONNECT_IPV6          (1u << 3)
 extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
 extern int finish_connect(struct child_process *conn);
 extern int git_connection_is_socket(struct child_process *conn);
index 8cd6222a263ac99a63335bf7080a3fb4dd797ff7..f524b8d7f4daf70a610a1e00af2ae6f1c23f8f62 100644 (file)
--- a/convert.c
+++ b/convert.c
 #define CONVERT_STAT_BITS_BIN       0x4
 
 enum crlf_action {
-       CRLF_GUESS = -1,
-       CRLF_BINARY = 0,
+       CRLF_UNDEFINED,
+       CRLF_BINARY,
        CRLF_TEXT,
-       CRLF_INPUT,
-       CRLF_CRLF,
-       CRLF_AUTO
+       CRLF_TEXT_INPUT,
+       CRLF_TEXT_CRLF,
+       CRLF_AUTO,
+       CRLF_AUTO_INPUT,
+       CRLF_AUTO_CRLF
 };
 
 struct text_stat {
        /* NUL, CR, LF and CRLF counts */
-       unsigned nul, cr, lf, crlf;
+       unsigned nul, lonecr, lonelf, crlf;
 
        /* These are just approximations! */
        unsigned printable, nonprintable;
@@ -44,13 +46,15 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
        for (i = 0; i < size; i++) {
                unsigned char c = buf[i];
                if (c == '\r') {
-                       stats->cr++;
-                       if (i+1 < size && buf[i+1] == '\n')
+                       if (i+1 < size && buf[i+1] == '\n') {
                                stats->crlf++;
+                               i++;
+                       } else
+                               stats->lonecr++;
                        continue;
                }
                if (c == '\n') {
-                       stats->lf++;
+                       stats->lonelf++;
                        continue;
                }
                if (c == 127)
@@ -84,7 +88,7 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
  */
 static int convert_is_binary(unsigned long size, const struct text_stat *stats)
 {
-       if (stats->cr != stats->crlf)
+       if (stats->lonecr)
                return 1;
        if (stats->nul)
                return 1;
@@ -96,19 +100,18 @@ static int convert_is_binary(unsigned long size, const struct text_stat *stats)
 static unsigned int gather_convert_stats(const char *data, unsigned long size)
 {
        struct text_stat stats;
+       int ret = 0;
        if (!data || !size)
                return 0;
        gather_stats(data, size, &stats);
        if (convert_is_binary(size, &stats))
-               return CONVERT_STAT_BITS_BIN;
-       else if (stats.crlf && stats.crlf == stats.lf)
-               return CONVERT_STAT_BITS_TXT_CRLF;
-       else if (stats.crlf && stats.lf)
-               return CONVERT_STAT_BITS_TXT_CRLF | CONVERT_STAT_BITS_TXT_LF;
-       else if (stats.lf)
-               return CONVERT_STAT_BITS_TXT_LF;
-       else
-               return 0;
+               ret |= CONVERT_STAT_BITS_BIN;
+       if (stats.crlf)
+               ret |= CONVERT_STAT_BITS_TXT_CRLF;
+       if (stats.lonelf)
+               ret |=  CONVERT_STAT_BITS_TXT_LF;
+
+       return ret;
 }
 
 static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
@@ -149,28 +152,37 @@ const char *get_wt_convert_stats_ascii(const char *path)
        return ret;
 }
 
+static int text_eol_is_crlf(void)
+{
+       if (auto_crlf == AUTO_CRLF_TRUE)
+               return 1;
+       else if (auto_crlf == AUTO_CRLF_INPUT)
+               return 0;
+       if (core_eol == EOL_CRLF)
+               return 1;
+       if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
+               return 1;
+       return 0;
+}
+
 static enum eol output_eol(enum crlf_action crlf_action)
 {
        switch (crlf_action) {
        case CRLF_BINARY:
                return EOL_UNSET;
-       case CRLF_CRLF:
+       case CRLF_TEXT_CRLF:
                return EOL_CRLF;
-       case CRLF_INPUT:
+       case CRLF_TEXT_INPUT:
                return EOL_LF;
-       case CRLF_GUESS:
-               if (!auto_crlf)
-                       return EOL_UNSET;
-               /* fall through */
+       case CRLF_UNDEFINED:
+       case CRLF_AUTO_CRLF:
+       case CRLF_AUTO_INPUT:
        case CRLF_TEXT:
        case CRLF_AUTO:
-               if (auto_crlf == AUTO_CRLF_TRUE)
-                       return EOL_CRLF;
-               else if (auto_crlf == AUTO_CRLF_INPUT)
-                       return EOL_LF;
-               else if (core_eol == EOL_UNSET)
-                       return EOL_NATIVE;
+               /* fall through */
+               return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
        }
+       warning("Illegal crlf_action %d\n", (int)crlf_action);
        return core_eol;
 }
 
@@ -196,7 +208,7 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
                 * CRLFs would be added by checkout:
                 * check if we have "naked" LFs
                 */
-               if (stats->lf != stats->crlf) {
+               if (stats->lonelf) {
                        if (checksafe == SAFE_CRLF_WARN)
                                warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
                        else /* i.e. SAFE_CRLF_FAIL */
@@ -227,7 +239,6 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
        char *dst;
 
        if (crlf_action == CRLF_BINARY ||
-           (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) ||
            (src && !len))
                return 0;
 
@@ -240,11 +251,11 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
 
        gather_stats(src, len, &stats);
 
-       if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
+       if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
                if (convert_is_binary(len, &stats))
                        return 0;
 
-               if (crlf_action == CRLF_GUESS) {
+               if (crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
                        /*
                         * If the file in the index has any CR in it, do not convert.
                         * This is the new safer autocrlf handling.
@@ -256,8 +267,8 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
 
        check_safe_crlf(path, crlf_action, &stats, checksafe);
 
-       /* Optimization: No CR? Nothing to convert, regardless. */
-       if (!stats.cr)
+       /* Optimization: No CRLF? Nothing to convert, regardless. */
+       if (!stats.crlf)
                return 0;
 
        /*
@@ -271,7 +282,7 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
        if (strbuf_avail(buf) + buf->len < len)
                strbuf_grow(buf, len - buf->len);
        dst = buf->buf;
-       if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
+       if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
                /*
                 * If we guessed, we already know we rejected a file with
                 * lone CR, and we can strip a CR without looking at what
@@ -304,19 +315,15 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
 
        gather_stats(src, len, &stats);
 
-       /* No LF? Nothing to convert, regardless. */
-       if (!stats.lf)
-               return 0;
-
-       /* Was it already in CRLF format? */
-       if (stats.lf == stats.crlf)
+       /* No "naked" LF? Nothing to convert, regardless. */
+       if (!stats.lonelf)
                return 0;
 
-       if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
-               if (crlf_action == CRLF_GUESS) {
+       if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
+               if (crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
                        /* If we have any CR or CRLF line endings, we do not touch it */
                        /* This is the new safer autocrlf-handling */
-                       if (stats.cr > 0 || stats.crlf > 0)
+                       if (stats.lonecr || stats.crlf )
                                return 0;
                }
 
@@ -328,7 +335,7 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
        if (src == buf->buf)
                to_free = strbuf_detach(buf, NULL);
 
-       strbuf_grow(buf, len + stats.lf - stats.crlf);
+       strbuf_grow(buf, len + stats.lonelf);
        for (;;) {
                const char *nl = memchr(src, '\n', len);
                if (!nl)
@@ -696,7 +703,7 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,
        return 1;
 }
 
-static enum crlf_action git_path_check_crlf(const char *path, struct git_attr_check *check)
+static enum crlf_action git_path_check_crlf(struct git_attr_check *check)
 {
        const char *value = check->value;
 
@@ -707,13 +714,13 @@ static enum crlf_action git_path_check_crlf(const char *path, struct git_attr_ch
        else if (ATTR_UNSET(value))
                ;
        else if (!strcmp(value, "input"))
-               return CRLF_INPUT;
+               return CRLF_TEXT_INPUT;
        else if (!strcmp(value, "auto"))
                return CRLF_AUTO;
-       return CRLF_GUESS;
+       return CRLF_UNDEFINED;
 }
 
-static enum eol git_path_check_eol(const char *path, struct git_attr_check *check)
+static enum eol git_path_check_eol(struct git_attr_check *check)
 {
        const char *value = check->value;
 
@@ -726,8 +733,7 @@ static enum eol git_path_check_eol(const char *path, struct git_attr_check *chec
        return EOL_UNSET;
 }
 
-static struct convert_driver *git_path_check_convert(const char *path,
-                                            struct git_attr_check *check)
+static struct convert_driver *git_path_check_convert(struct git_attr_check *check)
 {
        const char *value = check->value;
        struct convert_driver *drv;
@@ -740,28 +746,17 @@ static struct convert_driver *git_path_check_convert(const char *path,
        return NULL;
 }
 
-static int git_path_check_ident(const char *path, struct git_attr_check *check)
+static int git_path_check_ident(struct git_attr_check *check)
 {
        const char *value = check->value;
 
        return !!ATTR_TRUE(value);
 }
 
-static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol eol_attr)
-{
-       if (text_attr == CRLF_BINARY)
-               return CRLF_BINARY;
-       if (eol_attr == EOL_LF)
-               return CRLF_INPUT;
-       if (eol_attr == EOL_CRLF)
-               return CRLF_CRLF;
-       return text_attr;
-}
-
 struct conv_attrs {
        struct convert_driver *drv;
-       enum crlf_action crlf_action;
-       enum eol eol_attr;
+       enum crlf_action attr_action; /* What attr says */
+       enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
        int ident;
 };
 
@@ -783,18 +778,33 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)
        }
 
        if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
-               ca->crlf_action = git_path_check_crlf(path, ccheck + 4);
-               if (ca->crlf_action == CRLF_GUESS)
-                       ca->crlf_action = git_path_check_crlf(path, ccheck + 0);
-               ca->ident = git_path_check_ident(path, ccheck + 1);
-               ca->drv = git_path_check_convert(path, ccheck + 2);
-               ca->eol_attr = git_path_check_eol(path, ccheck + 3);
+               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) {
+                       enum eol eol_attr = git_path_check_eol(ccheck + 3);
+                       if (eol_attr == EOL_LF)
+                               ca->crlf_action = CRLF_TEXT_INPUT;
+                       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_GUESS;
-               ca->eol_attr = EOL_UNSET;
+               ca->crlf_action = CRLF_UNDEFINED;
                ca->ident = 0;
        }
+       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)
+               ca->crlf_action = CRLF_BINARY;
+       if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
+               ca->crlf_action = CRLF_AUTO_CRLF;
+       if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
+               ca->crlf_action = CRLF_AUTO_INPUT;
 }
 
 int would_convert_to_git_filter_fd(const char *path)
@@ -819,23 +829,25 @@ int would_convert_to_git_filter_fd(const char *path)
 const char *get_convert_attr_ascii(const char *path)
 {
        struct conv_attrs ca;
-       enum crlf_action crlf_action;
 
        convert_attrs(&ca, path);
-       crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
-       switch (crlf_action) {
-       case CRLF_GUESS:
+       switch (ca.attr_action) {
+       case CRLF_UNDEFINED:
                return "";
        case CRLF_BINARY:
                return "-text";
        case CRLF_TEXT:
                return "text";
-       case CRLF_INPUT:
+       case CRLF_TEXT_INPUT:
                return "text eol=lf";
-       case CRLF_CRLF:
-               return "text=auto eol=crlf";
+       case CRLF_TEXT_CRLF:
+               return "text eol=crlf";
        case CRLF_AUTO:
                return "text=auto";
+       case CRLF_AUTO_CRLF:
+               return "text=auto eol=crlf"; /* This is not supported yet */
+       case CRLF_AUTO_INPUT:
+               return "text=auto eol=lf"; /* This is not supported yet */
        }
        return "";
 }
@@ -862,7 +874,6 @@ int convert_to_git(const char *path, const char *src, size_t len,
                src = dst->buf;
                len = dst->len;
        }
-       ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
        ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
        if (ret && dst) {
                src = dst->buf;
@@ -883,7 +894,6 @@ void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
        if (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))
                die("%s: clean filter '%s' failed", path, ca.drv->name);
 
-       ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
        crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
        ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
 }
@@ -913,7 +923,6 @@ static int convert_to_working_tree_internal(const char *path, const char *src,
         * is a smudge filter.  The filter might expect CRLFs.
         */
        if (filter || !normalizing) {
-               ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
                ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
                if (ret) {
                        src = dst->buf;
@@ -1382,14 +1391,15 @@ struct stream_filter *get_stream_filter(const char *path, const unsigned char *s
        if (ca.ident)
                filter = ident_filter(sha1);
 
-       crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
+       crlf_action = ca.crlf_action;
 
-       if ((crlf_action == CRLF_BINARY) || (crlf_action == CRLF_INPUT) ||
-           (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE))
+       if ((crlf_action == CRLF_BINARY) ||
+                       crlf_action == CRLF_AUTO_INPUT ||
+                       (crlf_action == CRLF_TEXT_INPUT))
                filter = cascade_filter(filter, &null_filter_singleton);
 
        else if (output_eol(crlf_action) == EOL_CRLF &&
-                !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS))
+                !(crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_CRLF))
                filter = cascade_filter(filter, lf_to_crlf_filter());
 
        return filter;
index cc65a9c0d342aa51d4b307fe10c626d940e2f211..caef21e4fc91898f209709f723de1df5afc66a66 100644 (file)
@@ -215,7 +215,7 @@ static const char permissions_advice[] =
 "users may be able to read your cached credentials. Consider running:\n"
 "\n"
 "      chmod 0700 %s";
-static void check_socket_directory(const char *path)
+static void init_socket_directory(const char *path)
 {
        struct stat st;
        char *path_copy = xstrdup(path);
@@ -224,20 +224,27 @@ static void check_socket_directory(const char *path)
        if (!stat(dir, &st)) {
                if (st.st_mode & 077)
                        die(permissions_advice, dir);
-               free(path_copy);
-               return;
+       } else {
+               /*
+                * We must be sure to create the directory with the correct mode,
+                * not just chmod it after the fact; otherwise, there is a race
+                * condition in which somebody can chdir to it, sleep, then try to open
+                * our protected socket.
+                */
+               if (safe_create_leading_directories_const(dir) < 0)
+                       die_errno("unable to create directories for '%s'", dir);
+               if (mkdir(dir, 0700) < 0)
+                       die_errno("unable to mkdir '%s'", dir);
        }
 
-       /*
-        * We must be sure to create the directory with the correct mode,
-        * not just chmod it after the fact; otherwise, there is a race
-        * condition in which somebody can chdir to it, sleep, then try to open
-        * our protected socket.
-        */
-       if (safe_create_leading_directories_const(dir) < 0)
-               die_errno("unable to create directories for '%s'", dir);
-       if (mkdir(dir, 0700) < 0)
-               die_errno("unable to mkdir '%s'", dir);
+       if (chdir(dir))
+               /*
+                * We don't actually care what our cwd is; we chdir here just to
+                * be a friendly daemon and avoid tying up our original cwd.
+                * If this fails, it's OK to just continue without that benefit.
+                */
+               ;
+
        free(path_copy);
 }
 
@@ -264,7 +271,10 @@ int main(int argc, const char **argv)
        if (!socket_path)
                usage_with_options(usage, options);
 
-       check_socket_directory(socket_path);
+       if (!is_absolute_path(socket_path))
+               die("socket directory must be an absolute path");
+
+       init_socket_directory(socket_path);
        register_tempfile(&socket_file, socket_path);
 
        if (ignore_sighup)
index 46b411c7d941ecf862b7a7ffdce38ef8767e29e4..8d45c336f5f816768962c39e8af6b10e0bb0e21e 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -808,7 +808,7 @@ static void check_dead_children(void)
                        cradle = &blanket->next;
 }
 
-static char **cld_argv;
+static struct argv_array cld_argv = ARGV_ARRAY_INIT;
 static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
 {
        struct child_process cld = CHILD_PROCESS_INIT;
@@ -842,7 +842,7 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
 #endif
        }
 
-       cld.argv = (const char **)cld_argv;
+       cld.argv = cld_argv.argv;
        cld.in = incoming;
        cld.out = dup(incoming);
 
@@ -1374,12 +1374,10 @@ int main(int argc, char **argv)
                write_file(pid_file, "%"PRIuMAX, (uintmax_t) getpid());
 
        /* prepare argv for serving-processes */
-       cld_argv = xmalloc(sizeof (char *) * (argc + 2));
-       cld_argv[0] = argv[0];  /* git-daemon */
-       cld_argv[1] = "--serve";
+       argv_array_push(&cld_argv, argv[0]); /* git-daemon */
+       argv_array_push(&cld_argv, "--serve");
        for (i = 1; i < argc; ++i)
-               cld_argv[i+1] = argv[i];
-       cld_argv[argc+1] = NULL;
+               argv_array_push(&cld_argv, argv[i]);
 
        return serve(&listen_addr, listen_port, cred);
 }
diff --git a/diff.c b/diff.c
index 2136b6970b3a9751a3ec420bac61e9baa08df9b0..059123c5dcef4129763895b0f2ad5a54728b0c07 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -2607,12 +2607,9 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
 
 struct diff_filespec *alloc_filespec(const char *path)
 {
-       int namelen = strlen(path);
-       struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
+       struct diff_filespec *spec;
 
-       memset(spec, 0, sizeof(*spec));
-       spec->path = (char *)(spec + 1);
-       memcpy(spec->path, path, namelen+1);
+       FLEXPTR_ALLOC_STR(spec, path, path);
        spec->count = 1;
        spec->is_binary = -1;
        return spec;
@@ -2707,21 +2704,21 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
 
 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
 {
-       int len;
-       char *data = xmalloc(100), *dirty = "";
+       struct strbuf buf = STRBUF_INIT;
+       char *dirty = "";
 
        /* Are we looking at the work tree? */
        if (s->dirty_submodule)
                dirty = "-dirty";
 
-       len = snprintf(data, 100,
-                      "Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
-       s->data = data;
-       s->size = len;
-       s->should_free = 1;
+       strbuf_addf(&buf, "Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
+       s->size = buf.len;
        if (size_only) {
                s->data = NULL;
-               free(data);
+               strbuf_release(&buf);
+       } else {
+               s->data = strbuf_detach(&buf, NULL);
+               s->should_free = 1;
        }
        return 0;
 }
@@ -5085,7 +5082,7 @@ size_t fill_textconv(struct userdiff_driver *driver,
 {
        size_t size;
 
-       if (!driver || !driver->textconv) {
+       if (!driver) {
                if (!DIFF_FILE_VALID(df)) {
                        *outbuf = "";
                        return 0;
@@ -5096,6 +5093,9 @@ size_t fill_textconv(struct userdiff_driver *driver,
                return df->size;
        }
 
+       if (!driver->textconv)
+               die("BUG: fill_textconv called with non-textconv driver");
+
        if (driver->textconv_cache && df->sha1_valid) {
                *outbuf = notes_cache_get(driver->textconv_cache, df->sha1,
                                          &size);
diff --git a/diff.h b/diff.h
index 70b2d70d64e1e47da617f51b8385870a22e92f40..e7d68edaf9d4744ce3c48356e1835c5506d5322d 100644 (file)
--- a/diff.h
+++ b/diff.h
@@ -222,8 +222,8 @@ struct combine_diff_path {
        } parent[FLEX_ARRAY];
 };
 #define combine_diff_path_size(n, l) \
-       (sizeof(struct combine_diff_path) + \
-        sizeof(struct combine_diff_parent) * (n) + (l) + 1)
+       st_add4(sizeof(struct combine_diff_path), (l), 1, \
+               st_mult(sizeof(struct combine_diff_parent), (n)))
 
 extern void show_combined_diff(struct combine_diff_path *elem, int num_parent,
                              int dense, struct rev_info *);
@@ -349,10 +349,26 @@ extern void diff_no_index(struct rev_info *, int, const char **);
 
 extern int index_differs_from(const char *def, int diff_flags);
 
+/*
+ * Fill the contents of the filespec "df", respecting any textconv defined by
+ * its userdiff driver.  The "driver" parameter must come from a
+ * previous call to get_textconv(), and therefore should either be NULL or have
+ * textconv enabled.
+ *
+ * Note that the memory ownership of the resulting buffer depends on whether
+ * the driver field is NULL. If it is, then the memory belongs to the filespec
+ * struct. If it is non-NULL, then "outbuf" points to a newly allocated buffer
+ * that should be freed by the caller.
+ */
 extern size_t fill_textconv(struct userdiff_driver *driver,
                            struct diff_filespec *df,
                            char **outbuf);
 
+/*
+ * Look up the userdiff driver for the given filespec, and return it if
+ * and only if it has textconv enabled (otherwise return NULL). The result
+ * can be passed to fill_textconv().
+ */
 extern struct userdiff_driver *get_textconv(struct diff_filespec *one);
 
 extern int parse_rename_score(const char **cp_p);
index 7cf431d261f9a35679ead7c8acda15aecdb8720d..4159748a70ccc0ddee7cf2fcf82976629dfa6867 100644 (file)
@@ -53,7 +53,8 @@ static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)
        int osz = 1 << orig->alloc_log2;
        int sz = osz << 1;
 
-       new = xmalloc(sizeof(*orig) + sizeof(struct spanhash) * sz);
+       new = xmalloc(st_add(sizeof(*orig),
+                            st_mult(sizeof(struct spanhash), sz)));
        new->alloc_log2 = orig->alloc_log2 + 1;
        new->free = INITIAL_FREE(new->alloc_log2);
        memset(new->data, 0, sizeof(struct spanhash) * sz);
@@ -130,7 +131,8 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)
        int is_text = !diff_filespec_is_binary(one);
 
        i = INITIAL_HASH_SIZE;
-       hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
+       hash = xmalloc(st_add(sizeof(*hash),
+                             st_mult(sizeof(struct spanhash), 1<<i)));
        hash->alloc_log2 = i;
        hash->free = INITIAL_FREE(i);
        memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
index 97dd3d0095723c194288db0da1b9cfbf400e7e26..69d41f7a5781a10e43cf0e74f20be85a0d1fc5be 100644 (file)
@@ -52,7 +52,7 @@ static void prepare_order(const char *orderfile)
                }
                if (pass == 0) {
                        order_cnt = cnt;
-                       order = xmalloc(sizeof(*order) * cnt);
+                       ALLOC_ARRAY(order, cnt);
                }
        }
 }
@@ -120,7 +120,7 @@ void diffcore_order(const char *orderfile)
        if (!q->nr)
                return;
 
-       o = xmalloc(sizeof(*o) * q->nr);
+       ALLOC_ARRAY(o, q->nr);
        for (i = 0; i < q->nr; i++)
                o[i].obj = q->queue[i];
        order_objects(orderfile, pair_pathtwo, o, q->nr);
index af1fe08861e6862985ac8fb9311b17568cb9e547..3b3c1ed535e7c8b17947d69dbe0bd5c8f115cf2d 100644 (file)
@@ -537,7 +537,7 @@ void diffcore_rename(struct diff_options *options)
                                rename_dst_nr * rename_src_nr, 50, 1);
        }
 
-       mx = xcalloc(num_create * NUM_CANDIDATE_PER_DST, sizeof(*mx));
+       mx = xcalloc(st_mult(num_create, NUM_CANDIDATE_PER_DST), sizeof(*mx));
        for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
                struct diff_filespec *two = rename_dst[i].two;
                struct diff_score *m;
diff --git a/dir.c b/dir.c
index f0b6d0a3eab704311ed2c93a3da5db0683b3125c..69e0be6aa28d6e9bcfa3baf4f80de5bcc3158fe0 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -53,6 +53,8 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        int check_only, const struct path_simplify *simplify);
 static int get_dtype(struct dirent *de, const char *path, int len);
 
+static struct trace_key trace_exclude = TRACE_KEY_INIT(EXCLUDE);
+
 /* helper string functions with support for the ignore_case flag */
 int strcmp_icase(const char *a, const char *b)
 {
@@ -503,12 +505,7 @@ void add_exclude(const char *string, const char *base,
 
        parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
        if (flags & EXC_FLAG_MUSTBEDIR) {
-               char *s;
-               x = xmalloc(sizeof(*x) + patternlen + 1);
-               s = (char *)(x+1);
-               memcpy(s, string, patternlen);
-               s[patternlen] = '\0';
-               x->pattern = s;
+               FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen);
        } else {
                x = xmalloc(sizeof(*x));
                x->pattern = string;
@@ -519,6 +516,7 @@ void add_exclude(const char *string, const char *base,
        x->baselen = baselen;
        x->flags = flags;
        x->srcpos = srcpos;
+       string_list_init(&x->sticky_paths, 1);
        ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
        el->excludes[el->nr++] = x;
        x->el = el;
@@ -559,8 +557,10 @@ void clear_exclude_list(struct exclude_list *el)
 {
        int i;
 
-       for (i = 0; i < el->nr; i++)
+       for (i = 0; i < el->nr; i++) {
+               string_list_clear(&el->excludes[i]->sticky_paths, 0);
                free(el->excludes[i]);
+       }
        free(el->excludes);
        free(el->filebuf);
 
@@ -625,10 +625,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
        }
 
        uc->dir_created++;
-       d = xmalloc(sizeof(*d) + len + 1);
-       memset(d, 0, sizeof(*d));
-       memcpy(d->name, name, len);
-       d->name[len] = '\0';
+       FLEX_ALLOC_MEM(d, name, name, len);
 
        ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
        memmove(dir->dirs + first + 1, dir->dirs + first,
@@ -697,7 +694,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,
                        return 0;
                }
                if (buf[size-1] != '\n') {
-                       buf = xrealloc(buf, size+1);
+                       buf = xrealloc(buf, st_add(size, 1));
                        buf[size++] = '\n';
                }
        } else {
@@ -711,7 +708,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,
                        close(fd);
                        return 0;
                }
-               buf = xmalloc(size+1);
+               buf = xmallocz(size);
                if (read_in_full(fd, buf, size) != size) {
                        free(buf);
                        close(fd);
@@ -878,7 +875,7 @@ int match_pathname(const char *pathname, int pathlen,
                 * then our prefix match is all we need; we
                 * do not need to call fnmatch at all.
                 */
-               if (!patternlen && !namelen)
+               if (!patternlen && (!namelen || *name == '/'))
                        return 1;
        }
 
@@ -887,6 +884,113 @@ int match_pathname(const char *pathname, int pathlen,
                                 WM_PATHNAME) == 0;
 }
 
+static void add_sticky(struct exclude *exc, const char *pathname, int pathlen)
+{
+       struct strbuf sb = STRBUF_INIT;
+       int i;
+
+       for (i = exc->sticky_paths.nr - 1; i >= 0; i--) {
+               const char *sticky = exc->sticky_paths.items[i].string;
+               int len = strlen(sticky);
+
+               if (pathlen < len && sticky[pathlen] == '/' &&
+                   !strncmp(pathname, sticky, pathlen))
+                       return;
+       }
+
+       strbuf_add(&sb, pathname, pathlen);
+       string_list_append_nodup(&exc->sticky_paths, strbuf_detach(&sb, NULL));
+}
+
+static int match_sticky(struct exclude *exc, const char *pathname, int pathlen, int dtype)
+{
+       int i;
+
+       for (i = exc->sticky_paths.nr - 1; i >= 0; i--) {
+               const char *sticky = exc->sticky_paths.items[i].string;
+               int len = strlen(sticky);
+
+               if (pathlen == len && dtype == DT_DIR &&
+                   !strncmp(pathname, sticky, len))
+                       return 1;
+
+               if (pathlen > len && pathname[len] == '/' &&
+                   !strncmp(pathname, sticky, len))
+                       return 1;
+       }
+
+       return 0;
+}
+
+static inline int different_decisions(const struct exclude *a,
+                                     const struct exclude *b)
+{
+       return (a->flags & EXC_FLAG_NEGATIVE) != (b->flags & EXC_FLAG_NEGATIVE);
+}
+
+/*
+ * Return non-zero if pathname is a directory and an ancestor of the
+ * literal path in a pattern.
+ */
+static int match_directory_part(const char *pathname, int pathlen,
+                               int *dtype, struct exclude *x)
+{
+       const char      *base       = x->base;
+       int              baselen    = x->baselen ? x->baselen - 1 : 0;
+       const char      *pattern    = x->pattern;
+       int              prefix     = x->nowildcardlen;
+       int              patternlen = x->patternlen;
+
+       if (*dtype == DT_UNKNOWN)
+               *dtype = get_dtype(NULL, pathname, pathlen);
+       if (*dtype != DT_DIR)
+               return 0;
+
+       if (*pattern == '/') {
+               pattern++;
+               patternlen--;
+               prefix--;
+       }
+
+       if (baselen) {
+               if (((pathlen < baselen && base[pathlen] == '/') ||
+                    pathlen == baselen) &&
+                   !strncmp_icase(pathname, base, pathlen))
+                       return 1;
+               pathname += baselen + 1;
+               pathlen  -= baselen + 1;
+       }
+
+
+       if (prefix &&
+           (((pathlen < prefix && pattern[pathlen] == '/') ||
+             pathlen == prefix) &&
+            !strncmp_icase(pathname, pattern, pathlen)))
+               return 1;
+
+       return 0;
+}
+
+static struct exclude *should_descend(const char *pathname, int pathlen,
+                                     int *dtype, struct exclude_list *el,
+                                     struct exclude *exc)
+{
+       int i;
+
+       for (i = el->nr - 1; 0 <= i; i--) {
+               struct exclude *x = el->excludes[i];
+
+               if (x == exc)
+                       break;
+
+               if (!(x->flags & EXC_FLAG_NODIR) &&
+                   different_decisions(x, exc) &&
+                   match_directory_part(pathname, pathlen, dtype, x))
+                       return x;
+       }
+       return NULL;
+}
+
 /*
  * Scan the given exclude list in reverse to see whether pathname
  * should be ignored.  The first match (i.e. the last on the list), if
@@ -900,16 +1004,32 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
                                                       struct exclude_list *el)
 {
        struct exclude *exc = NULL; /* undecided */
-       int i;
+       int i, maybe_descend = 0;
 
        if (!el->nr)
                return NULL;    /* undefined */
 
+       trace_printf_key(&trace_exclude, "exclude: from %s\n", el->src);
+
        for (i = el->nr - 1; 0 <= i; i--) {
                struct exclude *x = el->excludes[i];
                const char *exclude = x->pattern;
                int prefix = x->nowildcardlen;
 
+               if (!maybe_descend && i < el->nr - 1 &&
+                   different_decisions(x, el->excludes[i+1]))
+                       maybe_descend = 1;
+
+               if (x->sticky_paths.nr) {
+                       if (*dtype == DT_UNKNOWN)
+                               *dtype = get_dtype(NULL, pathname, pathlen);
+                       if (match_sticky(x, pathname, pathlen, *dtype)) {
+                               exc = x;
+                               break;
+                       }
+                       continue;
+               }
+
                if (x->flags & EXC_FLAG_MUSTBEDIR) {
                        if (*dtype == DT_UNKNOWN)
                                *dtype = get_dtype(NULL, pathname, pathlen);
@@ -936,6 +1056,45 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
                        break;
                }
        }
+
+       if (!exc) {
+               trace_printf_key(&trace_exclude, "exclude: %.*s => n/a\n",
+                                pathlen, pathname);
+               return NULL;
+       }
+
+       /*
+        * We have found a matching pattern "exc" that may exclude whole
+        * directory. We also found that there may be a pattern that matches
+        * something inside the directory and reincludes stuff.
+        *
+        * Go through the patterns again, find that pattern and double check.
+        * If it's true, return "undecided" and keep descending in. "exc" is
+        * marked sticky so that it continues to match inside the directory.
+        */
+       if (!(exc->flags & EXC_FLAG_NEGATIVE) && maybe_descend) {
+               struct exclude *x;
+
+               if (*dtype == DT_UNKNOWN)
+                       *dtype = get_dtype(NULL, pathname, pathlen);
+
+               if (*dtype == DT_DIR &&
+                   (x = should_descend(pathname, pathlen, dtype, el, exc))) {
+                       add_sticky(exc, pathname, pathlen);
+                       trace_printf_key(&trace_exclude,
+                                        "exclude: %.*s vs %s at line %d => %s,"
+                                        " forced open by %s at line %d => n/a\n",
+                                        pathlen, pathname, exc->pattern, exc->srcpos,
+                                        exc->flags & EXC_FLAG_NEGATIVE ? "no" : "yes",
+                                        x->pattern, x->srcpos);
+                       return NULL;
+               }
+       }
+
+       trace_printf_key(&trace_exclude, "exclude: %.*s vs %s at line %d => %s%s\n",
+                        pathlen, pathname, exc->pattern, exc->srcpos,
+                        exc->flags & EXC_FLAG_NEGATIVE ? "no" : "yes",
+                        exc->sticky_paths.nr ? " (stuck)" : "");
        return exc;
 }
 
@@ -1167,10 +1326,8 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len)
 {
        struct dir_entry *ent;
 
-       ent = xmalloc(sizeof(*ent) + len + 1);
+       FLEX_ALLOC_MEM(ent, name, pathname, len);
        ent->len = len;
-       memcpy(ent->name, pathname, len);
-       ent->name[len] = 0;
        return ent;
 }
 
@@ -1683,9 +1840,13 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        struct cached_dir cdir;
        enum path_treatment state, subdir_state, dir_state = path_none;
        struct strbuf path = STRBUF_INIT;
+       static int level = 0;
 
        strbuf_add(&path, base, baselen);
 
+       trace_printf_key(&trace_exclude, "exclude: [%d] enter '%.*s'\n",
+                        level++, baselen, base);
+
        if (open_cached_dir(&cdir, dir, untracked, &path, check_only))
                goto out;
 
@@ -1749,6 +1910,8 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        }
        close_cached_dir(&cdir);
  out:
+       trace_printf_key(&trace_exclude, "exclude: [%d] leave '%.*s'\n",
+                        --level, baselen, base);
        strbuf_release(&path);
 
        return dir_state;
@@ -1985,6 +2148,25 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
        return root;
 }
 
+static void clear_sticky(struct dir_struct *dir)
+{
+       struct exclude_list_group *g;
+       struct exclude_list *el;
+       struct exclude *x;
+       int i, j, k;
+
+       for (i = EXC_CMDL; i <= EXC_FILE; i++) {
+               g = &dir->exclude_list_group[i];
+               for (j = g->nr - 1; j >= 0; j--) {
+                       el = &g->el[j];
+                       for (k = el->nr - 1; 0 <= k; k--) {
+                               x = el->excludes[k];
+                               string_list_clear(&x->sticky_paths, 0);
+                       }
+               }
+       }
+}
+
 int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec)
 {
        struct path_simplify *simplify;
@@ -2005,6 +2187,12 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru
        if (has_symlink_leading_path(path, len))
                return dir->nr;
 
+       /*
+        * Stay on the safe side. if read_directory() has run once on
+        * "dir", some sticky flag may have been left. Clear them all.
+        */
+       clear_sticky(dir);
+
        /*
         * exclude patterns are treated like positive ones in
         * create_simplify. Usually exclude patterns should be a
@@ -2370,16 +2558,15 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
        struct ondisk_untracked_cache *ouc;
        struct write_data wd;
        unsigned char varbuf[16];
-       int len = 0, varint_len;
-       if (untracked->exclude_per_dir)
-               len = strlen(untracked->exclude_per_dir);
-       ouc = xmalloc(sizeof(*ouc) + len + 1);
+       int varint_len;
+       size_t len = strlen(untracked->exclude_per_dir);
+
+       FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);
        stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
        stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);
        hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);
        hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1);
        ouc->dir_flags = htonl(untracked->dir_flags);
-       memcpy(ouc->exclude_per_dir, untracked->exclude_per_dir, len + 1);
 
        varint_len = encode_varint(untracked->ident.len, varbuf);
        strbuf_add(out, varbuf, varint_len);
@@ -2484,21 +2671,21 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
        ud.untracked_alloc = value;
        ud.untracked_nr    = value;
        if (ud.untracked_nr)
-               ud.untracked = xmalloc(sizeof(*ud.untracked) * ud.untracked_nr);
+               ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
        data = next;
 
        next = data;
        ud.dirs_alloc = ud.dirs_nr = decode_varint(&next);
        if (next > end)
                return -1;
-       ud.dirs = xmalloc(sizeof(*ud.dirs) * ud.dirs_nr);
+       ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
        data = next;
 
        len = strlen((const char *)data);
        next = data + len + 1;
        if (next > rd->end)
                return -1;
-       *untracked_ = untracked = xmalloc(sizeof(*untracked) + len);
+       *untracked_ = untracked = xmalloc(st_add(sizeof(*untracked), len));
        memcpy(untracked, &ud, sizeof(ud));
        memcpy(untracked->name, data, len + 1);
        data = next;
@@ -2611,7 +2798,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
        rd.data       = next;
        rd.end        = end;
        rd.index      = 0;
-       rd.ucd        = xmalloc(sizeof(*rd.ucd) * len);
+       ALLOC_ARRAY(rd.ucd, len);
 
        if (read_one_dir(&uc->root, &rd) || rd.index != len)
                goto done;
diff --git a/dir.h b/dir.h
index cd46f30017ce239720926afdad4301b2ac402ccf..3ec3fb0dca22164134ea0700094bf63bdc6ad329 100644 (file)
--- a/dir.h
+++ b/dir.h
@@ -4,6 +4,7 @@
 /* See Documentation/technical/api-directory-listing.txt */
 
 #include "strbuf.h"
+#include "string-list.h"
 
 struct dir_entry {
        unsigned int len;
@@ -34,6 +35,8 @@ struct exclude {
         * and from -1 decrementing for patterns from CLI args.
         */
        int srcpos;
+
+       struct string_list sticky_paths;
 };
 
 /*
diff --git a/entry.c b/entry.c
index 582c40071a3034af9a391cf438dd74ddb34e15ff..a4109574fa72e0f3f32ad7b9e1ed0e402c8aecff 100644 (file)
--- a/entry.c
+++ b/entry.c
@@ -6,7 +6,7 @@
 static void create_directories(const char *path, int path_len,
                               const struct checkout *state)
 {
-       char *buf = xmalloc(path_len + 1);
+       char *buf = xmallocz(path_len);
        int len = 0;
 
        while (len < path_len) {
index 47ad6747c415372da89fa7c88dd8ce03b2765676..7103ceefbff6768042161345565bb773ab6b8d30 100644 (file)
@@ -17,7 +17,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
-#include "git-compat-util.h"
+#include "cache.h"
 #include "ewok.h"
 
 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
@@ -25,8 +25,8 @@
 
 struct bitmap *bitmap_new(void)
 {
-       struct bitmap *bitmap = ewah_malloc(sizeof(struct bitmap));
-       bitmap->words = ewah_calloc(32, sizeof(eword_t));
+       struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
+       bitmap->words = xcalloc(32, sizeof(eword_t));
        bitmap->word_alloc = 32;
        return bitmap;
 }
@@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos)
        if (block >= self->word_alloc) {
                size_t old_size = self->word_alloc;
                self->word_alloc = block * 2;
-               self->words = ewah_realloc(self->words,
-                       self->word_alloc * sizeof(eword_t));
-
+               REALLOC_ARRAY(self->words, self->word_alloc);
                memset(self->words + old_size, 0x0,
                        (self->word_alloc - old_size) * sizeof(eword_t));
        }
@@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
        ewah_iterator_init(&it, ewah);
 
        while (ewah_iterator_next(&blowup, &it)) {
-               if (i >= bitmap->word_alloc) {
-                       bitmap->word_alloc *= 1.5;
-                       bitmap->words = ewah_realloc(
-                               bitmap->words, bitmap->word_alloc * sizeof(eword_t));
-               }
-
+               ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
                bitmap->words[i++] = blowup;
        }
 
@@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
 
        if (self->word_alloc < other_final) {
                self->word_alloc = other_final;
-               self->words = ewah_realloc(self->words,
-                       self->word_alloc * sizeof(eword_t));
+               REALLOC_ARRAY(self->words, self->word_alloc);
                memset(self->words + original_size, 0x0,
                        (self->word_alloc - original_size) * sizeof(eword_t));
        }
index b522437c0a0b1ba4906feadeb734542ec941b49a..2dc9c82ecf513ed721f290926f042b9f592221c2 100644 (file)
@@ -39,8 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)
                return;
 
        self->alloc_size = new_size;
-       self->buffer = ewah_realloc(self->buffer,
-               self->alloc_size * sizeof(eword_t));
+       REALLOC_ARRAY(self->buffer, self->alloc_size);
        self->rlw = self->buffer + (rlw_offset / sizeof(eword_t));
 }
 
@@ -282,12 +281,9 @@ struct ewah_bitmap *ewah_new(void)
 {
        struct ewah_bitmap *self;
 
-       self = ewah_malloc(sizeof(struct ewah_bitmap));
-       if (self == NULL)
-               return NULL;
-
-       self->buffer = ewah_malloc(32 * sizeof(eword_t));
+       self = xmalloc(sizeof(struct ewah_bitmap));
        self->alloc_size = 32;
+       ALLOC_ARRAY(self->buffer, self->alloc_size);
 
        ewah_clear(self);
        return self;
index 43481b9c60c8afe30d6916650dd0e765065715c9..61f6a43579f5e3fc98e562fe6f0c6d8c118e0ded 100644 (file)
@@ -134,11 +134,7 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
        self->buffer_size = self->alloc_size = get_be32(ptr);
        ptr += sizeof(uint32_t);
 
-       self->buffer = ewah_realloc(self->buffer,
-               self->alloc_size * sizeof(eword_t));
-
-       if (!self->buffer)
-               return -1;
+       REALLOC_ARRAY(self->buffer, self->alloc_size);
 
        /*
         * Copy the raw data for the bitmap as a whole chunk;
@@ -180,11 +176,7 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)
                return -1;
 
        self->buffer_size = self->alloc_size = (size_t)ntohl(word_count);
-       self->buffer = ewah_realloc(self->buffer,
-               self->alloc_size * sizeof(eword_t));
-
-       if (!self->buffer)
-               return -1;
+       REALLOC_ARRAY(self->buffer, self->alloc_size);
 
        /** 64 bit x N -- compressed words */
        buffer = self->buffer;
index 6e2c5e1e3d4475b31ba3c556b31b9d5d72e26d59..269a1a87063af0d89ab4b7569f0193e08bd98bf3 100644 (file)
 #ifndef __EWOK_BITMAP_H__
 #define __EWOK_BITMAP_H__
 
-#ifndef ewah_malloc
-#      define ewah_malloc xmalloc
-#endif
-#ifndef ewah_realloc
-#      define ewah_realloc xrealloc
-#endif
-#ifndef ewah_calloc
-#      define ewah_calloc xcalloc
-#endif
-
 struct strbuf;
 typedef uint64_t eword_t;
 #define BITS_IN_EWORD (sizeof(eword_t) * 8)
index e85f0fd8d897f4823a7c765d6b6ef929a15ca679..9d5703a157fe8e9d8396e0771798e54dc5eea6eb 100644 (file)
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "exec_cmd.h"
 #include "quote.h"
+#include "argv-array.h"
 #define MAX_ARGS       32
 
 static const char *argv_exec_path;
@@ -43,12 +44,10 @@ const char *git_extract_argv0_path(const char *argv0)
 
        if (!argv0 || !*argv0)
                return NULL;
-       slash = argv0 + strlen(argv0);
 
-       while (argv0 <= slash && !is_dir_sep(*slash))
-               slash--;
+       slash = find_last_dir_sep(argv0);
 
-       if (slash >= argv0) {
+       if (slash) {
                argv0_path = xstrndup(argv0, slash - argv0);
                return slash + 1;
        }
@@ -107,32 +106,25 @@ void setup_path(void)
        strbuf_release(&new_path);
 }
 
-const char **prepare_git_cmd(const char **argv)
+const char **prepare_git_cmd(struct argv_array *out, const char **argv)
 {
-       int argc;
-       const char **nargv;
-
-       for (argc = 0; argv[argc]; argc++)
-               ; /* just counting */
-       nargv = xmalloc(sizeof(*nargv) * (argc + 2));
-
-       nargv[0] = "git";
-       for (argc = 0; argv[argc]; argc++)
-               nargv[argc + 1] = argv[argc];
-       nargv[argc + 1] = NULL;
-       return nargv;
+       argv_array_push(out, "git");
+       argv_array_pushv(out, argv);
+       return out->argv;
 }
 
 int execv_git_cmd(const char **argv) {
-       const char **nargv = prepare_git_cmd(argv);
-       trace_argv_printf(nargv, "trace: exec:");
+       struct argv_array nargv = ARGV_ARRAY_INIT;
+
+       prepare_git_cmd(&nargv, argv);
+       trace_argv_printf(nargv.argv, "trace: exec:");
 
        /* execvp() can only ever return if it fails */
-       sane_execvp("git", (char **)nargv);
+       sane_execvp("git", (char **)nargv.argv);
 
        trace_printf("trace: exec failed: %s\n", strerror(errno));
 
-       free(nargv);
+       argv_array_clear(&nargv);
        return -1;
 }
 
index 93b0c02529a854af4ccb648ea0d0571c5495b63a..1f6b43378ba3866c07578b2222154e3a760440d1 100644 (file)
@@ -1,11 +1,13 @@
 #ifndef GIT_EXEC_CMD_H
 #define GIT_EXEC_CMD_H
 
+struct argv_array;
+
 extern void git_set_argv_exec_path(const char *exec_path);
 extern const char *git_extract_argv0_path(const char *path);
 extern const char *git_exec_path(void);
 extern void setup_path(void);
-extern const char **prepare_git_cmd(const char **argv);
+extern const char **prepare_git_cmd(struct argv_array *out, const char **argv);
 extern int execv_git_cmd(const char **argv); /* NULL terminated */
 LAST_ARG_MUST_BE_NULL
 extern int execl_git_cmd(const char *cmd, ...);
index bf01b3422142d6be204a983a9909efdb29d385d0..9fc7093406b1e2085a3b2180a8506bcd6d486e06 100644 (file)
@@ -622,7 +622,7 @@ static void *pool_alloc(size_t len)
                        return xmalloc(len);
                }
                total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
-               p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
+               p = xmalloc(st_add(sizeof(struct mem_pool), mem_pool_alloc));
                p->next_pool = mem_pool;
                p->next_free = (char *) p->space;
                p->end = p->next_free + mem_pool_alloc;
@@ -814,7 +814,8 @@ static struct tree_entry *new_tree_entry(void)
        if (!avail_tree_entry) {
                unsigned int n = tree_entry_alloc;
                total_allocd += n * sizeof(struct tree_entry);
-               avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
+               ALLOC_ARRAY(e, n);
+               avail_tree_entry = e;
                while (n-- > 1) {
                        *((void**)e) = e + 1;
                        e++;
@@ -864,15 +865,12 @@ static void start_packfile(void)
 {
        static char tmp_file[PATH_MAX];
        struct packed_git *p;
-       int namelen;
        struct pack_header hdr;
        int pack_fd;
 
        pack_fd = odb_mkstemp(tmp_file, sizeof(tmp_file),
                              "pack/tmp_pack_XXXXXX");
-       namelen = strlen(tmp_file) + 2;
-       p = xcalloc(1, sizeof(*p) + namelen);
-       xsnprintf(p->pack_name, namelen, "%s", tmp_file);
+       FLEX_ALLOC_STR(p, pack_name, tmp_file);
        p->pack_fd = pack_fd;
        p->do_not_close = 1;
        pack_file = sha1fd(pack_fd, p->pack_name);
@@ -898,7 +896,7 @@ static const char *create_index(void)
        struct object_entry_pool *o;
 
        /* Build the table of object IDs. */
-       idx = xmalloc(object_count * sizeof(*idx));
+       ALLOC_ARRAY(idx, object_count);
        c = idx;
        for (o = blocks; o; o = o->next_pool)
                for (e = o->next_free; e-- != o->entries;)
diff --git a/fsck.c b/fsck.c
index c637f6676b639beb90240e4667ed07ceb83cdbf8..ca4c68537788496dea803b559e2ffd4cf1dbeea7 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -199,7 +199,8 @@ void fsck_set_msg_type(struct fsck_options *options,
 
        if (!options->msg_type) {
                int i;
-               int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
+               int *msg_type;
+               ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
                for (i = 0; i < FSCK_MSG_MAX; i++)
                        msg_type[i] = fsck_msg_type(i, options);
                options->msg_type = msg_type;
index 693a336ff52ffc5acfaaa114829008de21b0a651..c07e0c177890639cf016eac9766d04f235dbfbcf 100644 (file)
 #define unsigned_add_overflows(a, b) \
     ((b) > maximum_unsigned_value_of_type(a) - (a))
 
+/*
+ * Returns true if the multiplication of "a" and "b" will
+ * overflow. The types of "a" and "b" must match and must be unsigned.
+ * Note that this macro evaluates "a" twice!
+ */
+#define unsigned_mult_overflows(a, b) \
+    ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
+
 #ifdef __GNUC__
 #define TYPEOF(x) (__typeof__(x))
 #else
@@ -325,10 +333,6 @@ extern char *gitdirname(char *);
 #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
 #endif
 
-#ifndef STRIP_EXTENSION
-#define STRIP_EXTENSION ""
-#endif
-
 #ifndef has_dos_drive_prefix
 static inline int git_has_dos_drive_prefix(const char *path)
 {
@@ -673,7 +677,6 @@ extern int git_vsnprintf(char *str, size_t maxsize,
 #ifdef __GLIBC_PREREQ
 #if __GLIBC_PREREQ(2, 1)
 #define HAVE_STRCHRNUL
-#define HAVE_MEMPCPY
 #endif
 #endif
 
@@ -687,14 +690,6 @@ static inline char *gitstrchrnul(const char *s, int c)
 }
 #endif
 
-#ifndef HAVE_MEMPCPY
-#define mempcpy gitmempcpy
-static inline void *gitmempcpy(void *dest, const void *src, size_t n)
-{
-       return (char *)memcpy(dest, src, n) + n;
-}
-#endif
-
 #ifdef NO_INET_PTON
 int inet_pton(int af, const char *src, void *dst);
 #endif
@@ -713,6 +708,32 @@ 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);
 
+static inline size_t st_add(size_t a, size_t b)
+{
+       if (unsigned_add_overflows(a, b))
+               die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
+                   (uintmax_t)a, (uintmax_t)b);
+       return a + b;
+}
+#define st_add3(a,b,c)   st_add((a),st_add((b),(c)))
+#define st_add4(a,b,c,d) st_add((a),st_add3((b),(c),(d)))
+
+static inline size_t st_mult(size_t a, size_t b)
+{
+       if (unsigned_mult_overflows(a, b))
+               die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
+                   (uintmax_t)a, (uintmax_t)b);
+       return a * b;
+}
+
+static inline size_t st_sub(size_t a, size_t b)
+{
+       if (a < b)
+               die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
+                   (uintmax_t)a, (uintmax_t)b);
+       return a - b;
+}
+
 #ifdef HAVE_ALLOCA_H
 # include <alloca.h>
 # define xalloca(size)      (alloca(size))
@@ -745,7 +766,70 @@ extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
 extern char *xgetcwd(void);
 extern FILE *fopen_for_writing(const char *path);
 
-#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
+#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
+#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
+
+/*
+ * These functions help you allocate structs with flex arrays, and copy
+ * the data directly into the array. For example, if you had:
+ *
+ *   struct foo {
+ *     int bar;
+ *     char name[FLEX_ARRAY];
+ *   };
+ *
+ * you can do:
+ *
+ *   struct foo *f;
+ *   FLEX_ALLOC_MEM(f, name, src, len);
+ *
+ * to allocate a "foo" with the contents of "src" in the "name" field.
+ * The resulting struct is automatically zero'd, and the flex-array field
+ * is NUL-terminated (whether the incoming src buffer was or not).
+ *
+ * The FLEXPTR_* variants operate on structs that don't use flex-arrays,
+ * but do want to store a pointer to some extra data in the same allocated
+ * block. For example, if you have:
+ *
+ *   struct foo {
+ *     char *name;
+ *     int bar;
+ *   };
+ *
+ * you can do:
+ *
+ *   struct foo *f;
+ *   FLEX_ALLOC_STR(f, name, src);
+ *
+ * and "name" will point to a block of memory after the struct, which will be
+ * freed along with the struct (but the pointer can be repointed anywhere).
+ *
+ * The *_STR variants accept a string parameter rather than a ptr/len
+ * combination.
+ *
+ * Note that these macros will evaluate the first parameter multiple
+ * times, and it must be assignable as an lvalue.
+ */
+#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
+       (x) = NULL; /* silence -Wuninitialized for offset calculation */ \
+       (x) = xalloc_flex(sizeof(*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \
+} while (0)
+#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
+       (x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \
+       (x)->ptrname = (void *)((x)+1); \
+} while(0)
+#define FLEX_ALLOC_STR(x, flexname, str) \
+       FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
+#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
+       FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
+
+static inline void *xalloc_flex(size_t base_len, size_t offset,
+                               const void *src, size_t src_len)
+{
+       unsigned char *ret = xcalloc(1, st_add3(base_len, src_len, 1));
+       memcpy(ret + offset, src, src_len);
+       return ret;
+}
 
 static inline char *xstrdup_or_null(const char *str)
 {
index cdc02af517c9d77a081d7918480f8d0a2c6a29d0..424b034e34b4b40e7c44da16ddf83a31dee6f6a0 100755 (executable)
@@ -120,8 +120,7 @@ case "${1:-.}${2:-.}${3:-.}" in
        case "$1" in
        '')
                echo "Added $4 in both, but differently."
-               orig=$(git-unpack-file $2)
-               create_virtual_base "$orig" "$src2"
+               orig=$(git-unpack-file e69de29bb2d1d6434b8b29ae775ad8c2e48c5391)
                ;;
        *)
                echo "Auto-merging $4"
index 9bc5c5f94d1d7b24dffae649cca371299540aa46..43c68deee9db2b243d39eba704878eb7551ada2d 100755 (executable)
@@ -591,6 +591,24 @@ cmd_deinit()
        done
 }
 
+is_tip_reachable () (
+       clear_local_git_env
+       cd "$1" &&
+       rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
+       test -z "$rev"
+)
+
+fetch_in_submodule () (
+       clear_local_git_env
+       cd "$1" &&
+       case "$2" in
+       '')
+               git fetch ;;
+       *)
+               git fetch $(get_default_remote) "$2" ;;
+       esac
+)
+
 #
 # Update each submodule path to correct revision, using clone and checkout as needed
 #
@@ -745,10 +763,15 @@ Maybe you want to use 'update --init'?")"
                        then
                                # Run fetch only if $sha1 isn't present or it
                                # is not reachable from a ref.
-                               (clear_local_git_env; cd "$sm_path" &&
-                                       ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
-                                        test -z "$rev") || git-fetch)) ||
+                               is_tip_reachable "$sm_path" "$sha1" ||
+                               fetch_in_submodule "$sm_path" ||
                                die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
+
+                               # Now we tried the usual fetch, but $sha1 may
+                               # not be reachable from any of the refs
+                               is_tip_reachable "$sm_path" "$sha1" ||
+                               fetch_in_submodule "$sm_path" "$sha1" ||
+                               die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain $sha1. Direct fetching of that commit failed.")"
                        fi
 
                        # Is this something we just cloned?
diff --git a/git.c b/git.c
index 6c64c9430e8e5ae3130b0c97e90bcbad29625b1e..6cc0c077f9761f9b56e5fb7e666722b437c4fae4 100644 (file)
--- a/git.c
+++ b/git.c
@@ -247,20 +247,16 @@ static int handle_alias(int *argcp, const char ***argv)
        alias_string = alias_lookup(alias_command);
        if (alias_string) {
                if (alias_string[0] == '!') {
-                       const char **alias_argv;
-                       int argc = *argcp, i;
+                       struct child_process child = CHILD_PROCESS_INIT;
 
                        commit_pager_choice();
                        restore_env(1);
 
-                       /* build alias_argv */
-                       alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1));
-                       alias_argv[0] = alias_string + 1;
-                       for (i = 1; i < argc; ++i)
-                               alias_argv[i] = (*argv)[i];
-                       alias_argv[argc] = NULL;
+                       child.use_shell = 1;
+                       argv_array_push(&child.args, alias_string + 1);
+                       argv_array_pushv(&child.args, (*argv) + 1);
 
-                       ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
+                       ret = run_command(&child);
                        if (ret >= 0)   /* normal exit */
                                exit(ret);
 
@@ -513,21 +509,25 @@ int is_builtin(const char *s)
        return !!get_builtin(s);
 }
 
+#ifdef STRIP_EXTENSION
+static void strip_extension(const char **argv)
+{
+       size_t len;
+
+       if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
+               argv[0] = xmemdupz(argv[0], len);
+}
+#else
+#define strip_extension(cmd)
+#endif
+
 static void handle_builtin(int argc, const char **argv)
 {
-       const char *cmd = argv[0];
-       int i;
-       static const char ext[] = STRIP_EXTENSION;
+       const char *cmd;
        struct cmd_struct *builtin;
 
-       if (sizeof(ext) > 1) {
-               i = strlen(argv[0]) - strlen(ext);
-               if (i > 0 && !strcmp(argv[0] + i, ext)) {
-                       char *argv0 = xstrdup(argv[0]);
-                       argv[0] = cmd = argv0;
-                       argv0[i] = '\0';
-               }
-       }
+       strip_extension(argv);
+       cmd = argv[0];
 
        /* Turn "git cmd --help" into "git help cmd" */
        if (argc > 1 && !strcmp(argv[1], "--help")) {
diff --git a/graph.c b/graph.c
index c25a09a8fdaf3e90bf7e029c78127146418ab023..1350bdde3be4346e8bc2038c57e4883d0ab12b25 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -234,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)
         * We'll automatically grow columns later if we need more room.
         */
        graph->column_capacity = 30;
-       graph->columns = xmalloc(sizeof(struct column) *
-                                graph->column_capacity);
-       graph->new_columns = xmalloc(sizeof(struct column) *
-                                    graph->column_capacity);
-       graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
-       graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
+       ALLOC_ARRAY(graph->columns, graph->column_capacity);
+       ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
+       ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
+       ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);
 
        /*
         * The diff output prefix callback, with this we can make
diff --git a/grep.c b/grep.c
index 7b2b96a4376efe51e41f9311c6fa5250a2dcd13d..528b652f713d2b6db5f48e3829448212cc3837bf 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -1741,7 +1741,7 @@ static int grep_source_load_file(struct grep_source *gs)
        i = open(filename, O_RDONLY);
        if (i < 0)
                goto err_ret;
-       data = xmalloc(size + 1);
+       data = xmallocz(size);
        if (st.st_size != read_in_full(i, data, size)) {
                error(_("'%s': short read %s"), filename, strerror(errno));
                close(i);
@@ -1749,7 +1749,6 @@ static int grep_source_load_file(struct grep_source *gs)
                return -1;
        }
        close(i);
-       data[size] = 0;
 
        gs->buf = data;
        gs->size = size;
index f693839cb4786fd5be57d878b58499273ec17f81..b10b642229ca0c3e6eb27142f080921dd5c3aece 100644 (file)
--- a/hashmap.c
+++ b/hashmap.c
@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len)
        e = hashmap_get(&map, &key, data);
        if (!e) {
                /* not found: create it */
-               e = xmallocz(sizeof(struct pool_entry) + len);
+               FLEX_ALLOC_MEM(e, data, data, len);
                hashmap_entry_init(e, key.ent.hash);
                e->len = len;
-               memcpy(e->data, data, len);
                hashmap_add(&map, e);
        }
        return e->data;
diff --git a/help.c b/help.c
index d996b340669a66d0b1619472fedf5c11b940d7c5..19328ea992299d2b66b1b8de8a4a609a4d25388b 100644 (file)
--- a/help.c
+++ b/help.c
 
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
-       struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
-
+       struct cmdname *ent;
+       FLEX_ALLOC_MEM(ent, name, name, len);
        ent->len = len;
-       memcpy(ent->name, name, len);
-       ent->name[len] = 0;
 
        ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
        cmds->names[cmds->cnt++] = ent;
index d857b131a8f7b02e3120900c5a4e525d9f8de591..bd60668707b956160edd9fb22767758023571ebb 100644 (file)
@@ -1277,9 +1277,7 @@ static struct object_list **add_one_object(struct object *obj, struct object_lis
 }
 
 static struct object_list **process_blob(struct blob *blob,
-                                        struct object_list **p,
-                                        struct name_path *path,
-                                        const char *name)
+                                        struct object_list **p)
 {
        struct object *obj = &blob->object;
 
@@ -1293,14 +1291,11 @@ static struct object_list **process_blob(struct blob *blob,
 }
 
 static struct object_list **process_tree(struct tree *tree,
-                                        struct object_list **p,
-                                        struct name_path *path,
-                                        const char *name)
+                                        struct object_list **p)
 {
        struct object *obj = &tree->object;
        struct tree_desc desc;
        struct name_entry entry;
-       struct name_path me;
 
        obj->flags |= LOCAL;
 
@@ -1310,21 +1305,17 @@ static struct object_list **process_tree(struct tree *tree,
                die("bad tree object %s", oid_to_hex(&obj->oid));
 
        obj->flags |= SEEN;
-       name = xstrdup(name);
        p = add_one_object(obj, p);
-       me.up = path;
-       me.elem = name;
-       me.elem_len = strlen(name);
 
        init_tree_desc(&desc, tree->buffer, tree->size);
 
        while (tree_entry(&desc, &entry))
                switch (object_type(entry.mode)) {
                case OBJ_TREE:
-                       p = process_tree(lookup_tree(entry.sha1), p, &me, name);
+                       p = process_tree(lookup_tree(entry.sha1), p);
                        break;
                case OBJ_BLOB:
-                       p = process_blob(lookup_blob(entry.sha1), p, &me, name);
+                       p = process_blob(lookup_blob(entry.sha1), p);
                        break;
                default:
                        /* Subproject commit - not in this repository */
@@ -1343,7 +1334,7 @@ static int get_delta(struct rev_info *revs, struct remote_lock *lock)
        int count = 0;
 
        while ((commit = get_revision(revs)) != NULL) {
-               p = process_tree(commit->tree, p, NULL, "");
+               p = process_tree(commit->tree, p);
                commit->object.flags |= LOCAL;
                if (!(commit->object.flags & UNINTERESTING))
                        count += add_send_request(&commit->object, lock);
@@ -1362,11 +1353,11 @@ static int get_delta(struct rev_info *revs, struct remote_lock *lock)
                        continue;
                }
                if (obj->type == OBJ_TREE) {
-                       p = process_tree((struct tree *)obj, p, NULL, name);
+                       p = process_tree((struct tree *)obj, p);
                        continue;
                }
                if (obj->type == OBJ_BLOB) {
-                       p = process_blob((struct blob *)obj, p, NULL, name);
+                       p = process_blob((struct blob *)obj, p);
                        continue;
                }
                die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);
diff --git a/http.c b/http.c
index dfc53c1e2554e76126459d6cb1f098facac28593..1d5e3bbd11ae0e7dc0e682d3914e07cc13a0ce14 100644 (file)
--- a/http.c
+++ b/http.c
 #include "gettext.h"
 #include "transport.h"
 
+#if LIBCURL_VERSION_NUM >= 0x070a08
+long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
+#else
+long int git_curl_ipresolve;
+#endif
 int active_requests;
 int http_is_verbose;
 size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
@@ -57,6 +62,9 @@ static const char *ssl_key;
 #if LIBCURL_VERSION_NUM >= 0x070908
 static const char *ssl_capath;
 #endif
+#if LIBCURL_VERSION_NUM >= 0x072c00
+static const char *ssl_pinnedkey;
+#endif
 static const char *ssl_cainfo;
 static long curl_low_speed_limit = -1;
 static long curl_low_speed_time = -1;
@@ -87,6 +95,7 @@ static int curl_save_cookies;
 struct credential http_auth = CREDENTIAL_INIT;
 static int http_proactive_auth;
 static const char *user_agent;
+static int curl_empty_auth;
 
 #if LIBCURL_VERSION_NUM >= 0x071700
 /* Use CURLOPT_KEYPASSWD as is */
@@ -299,14 +308,31 @@ static int http_options(const char *var, const char *value, void *cb)
        if (!strcmp("http.useragent", var))
                return git_config_string(&user_agent, var, value);
 
+       if (!strcmp("http.emptyauth", var)) {
+               curl_empty_auth = git_config_bool(var, value);
+               return 0;
+       }
+
+       if (!strcmp("http.pinnedpubkey", var)) {
+#if LIBCURL_VERSION_NUM >= 0x072c00
+               return git_config_pathname(&ssl_pinnedkey, var, value);
+#else
+               warning(_("Public key pinning not supported with cURL < 7.44.0"));
+               return 0;
+#endif
+       }
+
        /* Fall back on the default ones */
        return git_default_config(var, value, cb);
 }
 
 static void init_curl_http_auth(CURL *result)
 {
-       if (!http_auth.username)
+       if (!http_auth.username) {
+               if (curl_empty_auth)
+                       curl_easy_setopt(result, CURLOPT_USERPWD, ":");
                return;
+       }
 
        credential_fill(&http_auth);
 
@@ -498,6 +524,10 @@ static CURL *get_curl_handle(void)
 #if LIBCURL_VERSION_NUM >= 0x070908
        if (ssl_capath != NULL)
                curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
+#endif
+#if LIBCURL_VERSION_NUM >= 0x072c00
+       if (ssl_pinnedkey != NULL)
+               curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
 #endif
        if (ssl_cainfo != NULL)
                curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
@@ -824,10 +854,14 @@ struct active_request_slot *get_active_slot(void)
        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
        curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
        curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
+
+#if LIBCURL_VERSION_NUM >= 0x070a08
+       curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
+#endif
 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
        curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
 #endif
-       if (http_auth.password)
+       if (http_auth.password || curl_empty_auth)
                init_curl_http_auth(slot->curl);
 
        return slot;
diff --git a/http.h b/http.h
index f83cfa686823728587b2a803c3e84a8cd4669220..4ef4bbda7d86d8ca5238d45897b277709b30414a 100644 (file)
--- a/http.h
+++ b/http.h
@@ -107,6 +107,7 @@ extern void http_init(struct remote *remote, const char *url,
                      int proactive_auth);
 extern void http_cleanup(void);
 
+extern long int git_curl_ipresolve;
 extern int active_requests;
 extern int http_is_verbose;
 extern size_t http_post_buffer;
index 4d3b7737a99de02c42945951e91b4d7ed865611d..2c52027c84455819740bed9f53970e175b9ca133 100644 (file)
@@ -892,12 +892,11 @@ static char *cram(const char *challenge_64, const char *user, const char *pass)
        response = xstrfmt("%s %s", user, hex);
        resp_len = strlen(response) + 1;
 
-       response_64 = xmalloc(ENCODED_SIZE(resp_len) + 1);
+       response_64 = xmallocz(ENCODED_SIZE(resp_len));
        encoded_len = EVP_EncodeBlock((unsigned char *)response_64,
                                      (unsigned char *)response, resp_len);
        if (encoded_len < 0)
                die("EVP_EncodeBlock error");
-       response_64[encoded_len] = '\0';
        return (char *)response_64;
 }
 
@@ -1188,7 +1187,7 @@ static void lf_to_crlf(struct strbuf *msg)
                j++;
        }
 
-       new = xmalloc(j + 1);
+       new = xmallocz(j);
 
        /*
         * Second pass: write the new string.  Note that this loop is
diff --git a/khash.h b/khash.h
index 376475a5eaf80585311ab518d03c229a7a49a7ed..c0da40daa78f703f825208bb7d1b4b5b3cc05ae3 100644 (file)
--- a/khash.h
+++ b/khash.h
@@ -117,7 +117,7 @@ static const double __ac_HASH_UPPER = 0.77;
                        if (new_n_buckets < 4) new_n_buckets = 4;                                       \
                        if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
                        else { /* hash table size to be changed (shrink or expand); rehash */ \
-                               new_flags = (khint32_t*)xmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
+                               ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \
                                if (!new_flags) return -1;                                                              \
                                memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
                                if (h->n_buckets < new_n_buckets) {     /* expand */            \
index fc281597fd2fd5ad2299eab0848da99355e383fc..d2632690d5107b53ee8a7ac4832cd85eb8c7bfc1 100644 (file)
@@ -42,11 +42,13 @@ int levenshtein(const char *string1, const char *string2,
                int w, int s, int a, int d)
 {
        int len1 = strlen(string1), len2 = strlen(string2);
-       int *row0 = xmalloc(sizeof(int) * (len2 + 1));
-       int *row1 = xmalloc(sizeof(int) * (len2 + 1));
-       int *row2 = xmalloc(sizeof(int) * (len2 + 1));
+       int *row0, *row1, *row2;
        int i, j;
 
+       ALLOC_ARRAY(row0, len2 + 1);
+       ALLOC_ARRAY(row1, len2 + 1);
+       ALLOC_ARRAY(row2, len2 + 1);
+
        for (j = 0; j <= len2; j++)
                row1[j] = j * a;
        for (i = 0; i < len1; i++) {
index af6e2f799ec7e2447ee888854442a7cbfe7644e4..bbe31ed6fbb7c103c739f7e9a3c45cde5d36fcec 100644 (file)
@@ -14,6 +14,7 @@
 #include "graph.h"
 #include "userdiff.h"
 #include "line-log.h"
+#include "argv-array.h"
 
 static void range_set_grow(struct range_set *rs, size_t extra)
 {
@@ -521,7 +522,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
        if (diff_populate_filespec(spec, 0))
                die("Cannot read blob %s", sha1_to_hex(spec->sha1));
 
-       ends = xmalloc(size * sizeof(*ends));
+       ALLOC_ARRAY(ends, size);
        ends[cur++] = 0;
        data = spec->data;
        while (num < spec->size) {
@@ -746,22 +747,17 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_list
        add_line_range(rev, commit, range);
 
        if (!rev->diffopt.detect_rename) {
-               int i, count = 0;
-               struct line_log_data *r = range;
+               struct line_log_data *r;
+               struct argv_array array = ARGV_ARRAY_INIT;
                const char **paths;
-               while (r) {
-                       count++;
-                       r = r->next;
-               }
-               paths = xmalloc((count+1)*sizeof(char *));
-               r = range;
-               for (i = 0; i < count; i++) {
-                       paths[i] = xstrdup(r->path);
-                       r = r->next;
-               }
-               paths[count] = NULL;
+
+               for (r = range; r; r = r->next)
+                       argv_array_push(&array, r->path);
+               paths = argv_array_detach(&array);
+
                parse_pathspec(&rev->diffopt.pathspec, 0,
                               PATHSPEC_PREFER_FULL, "", paths);
+               /* strings are now owned by pathspec */
                free(paths);
        }
 }
@@ -1146,9 +1142,9 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *comm
        if (nparents > 1 && rev->first_parent_only)
                nparents = 1;
 
-       diffqueues = xmalloc(nparents * sizeof(*diffqueues));
-       cand = xmalloc(nparents * sizeof(*cand));
-       parents = xmalloc(nparents * sizeof(*parents));
+       ALLOC_ARRAY(diffqueues, nparents);
+       ALLOC_ARRAY(cand, nparents);
+       ALLOC_ARRAY(parents, nparents);
 
        p = commit->parents;
        for (i = 0; i < nparents; i++) {
index 11732d93883ee6af8990ad311d07f28028af9876..917cc5d7c9ee04d07199f18b78342b84c0375fcd 100644 (file)
 static void process_blob(struct rev_info *revs,
                         struct blob *blob,
                         show_object_fn show,
-                        struct name_path *path,
+                        struct strbuf *path,
                         const char *name,
                         void *cb_data)
 {
        struct object *obj = &blob->object;
+       size_t pathlen;
 
        if (!revs->blob_objects)
                return;
@@ -24,7 +25,11 @@ static void process_blob(struct rev_info *revs,
        if (obj->flags & (UNINTERESTING | SEEN))
                return;
        obj->flags |= SEEN;
-       show(obj, path, name, cb_data);
+
+       pathlen = path->len;
+       strbuf_addstr(path, name);
+       show(obj, path->buf, cb_data);
+       strbuf_setlen(path, pathlen);
 }
 
 /*
@@ -52,7 +57,7 @@ static void process_blob(struct rev_info *revs,
 static void process_gitlink(struct rev_info *revs,
                            const unsigned char *sha1,
                            show_object_fn show,
-                           struct name_path *path,
+                           struct strbuf *path,
                            const char *name,
                            void *cb_data)
 {
@@ -62,7 +67,6 @@ static void process_gitlink(struct rev_info *revs,
 static void process_tree(struct rev_info *revs,
                         struct tree *tree,
                         show_object_fn show,
-                        struct name_path *path,
                         struct strbuf *base,
                         const char *name,
                         void *cb_data)
@@ -70,7 +74,6 @@ static void process_tree(struct rev_info *revs,
        struct object *obj = &tree->object;
        struct tree_desc desc;
        struct name_entry entry;
-       struct name_path me;
        enum interesting match = revs->diffopt.pathspec.nr == 0 ?
                all_entries_interesting: entry_not_interesting;
        int baselen = base->len;
@@ -86,17 +89,12 @@ static void process_tree(struct rev_info *revs,
                        return;
                die("bad tree object %s", oid_to_hex(&obj->oid));
        }
+
        obj->flags |= SEEN;
-       show(obj, path, name, cb_data);
-       me.up = path;
-       me.elem = name;
-       me.elem_len = strlen(name);
-
-       if (!match) {
-               strbuf_addstr(base, name);
-               if (base->len)
-                       strbuf_addch(base, '/');
-       }
+       strbuf_addstr(base, name);
+       show(obj, base->buf, cb_data);
+       if (base->len)
+               strbuf_addch(base, '/');
 
        init_tree_desc(&desc, tree->buffer, tree->size);
 
@@ -113,16 +111,16 @@ static void process_tree(struct rev_info *revs,
                if (S_ISDIR(entry.mode))
                        process_tree(revs,
                                     lookup_tree(entry.sha1),
-                                    show, &me, base, entry.path,
+                                    show, base, entry.path,
                                     cb_data);
                else if (S_ISGITLINK(entry.mode))
                        process_gitlink(revs, entry.sha1,
-                                       show, &me, entry.path,
+                                       show, base, entry.path,
                                        cb_data);
                else
                        process_blob(revs,
                                     lookup_blob(entry.sha1),
-                                    show, &me, entry.path,
+                                    show, base, entry.path,
                                     cb_data);
        }
        strbuf_setlen(base, baselen);
@@ -213,19 +211,19 @@ void traverse_commit_list(struct rev_info *revs,
                        continue;
                if (obj->type == OBJ_TAG) {
                        obj->flags |= SEEN;
-                       show_object(obj, NULL, name, data);
+                       show_object(obj, name, data);
                        continue;
                }
                if (!path)
                        path = "";
                if (obj->type == OBJ_TREE) {
                        process_tree(revs, (struct tree *)obj, show_object,
-                                    NULL, &base, path, data);
+                                    &base, path, data);
                        continue;
                }
                if (obj->type == OBJ_BLOB) {
                        process_blob(revs, (struct blob *)obj, show_object,
-                                    NULL, path, data);
+                                    &base, path, data);
                        continue;
                }
                die("unknown pending object %s (%s)",
index 136a1da5a6f048c0f4645112cdbf29c7b0982526..0cebf8585cb179ae46fd8238eaf2b2162739bbaa 100644 (file)
@@ -2,7 +2,7 @@
 #define LIST_OBJECTS_H
 
 typedef void (*show_commit_fn)(struct commit *, void *);
-typedef void (*show_object_fn)(struct object *, const struct name_path *, const char *, void *);
+typedef void (*show_object_fn)(struct object *, const char *, void *);
 void traverse_commit_list(struct rev_info *, show_commit_fn, show_object_fn, void *);
 
 typedef void (*show_edge_fn)(struct commit *);
index 0338630fc2a5378ed33afa828db4940b34aabe82..ff4a43a982a6a67ec826fdb01153a9e35e720751 100644 (file)
@@ -205,7 +205,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
        if (fstat(fd, &st))
                goto close_bad;
        result->size = st.st_size;
-       result->ptr = xmalloc(result->size + 1);
+       result->ptr = xmallocz(result->size);
        if (read_in_full(fd, result->ptr, result->size) != result->size) {
                free(result->ptr);
                result->ptr = NULL;
index f70a30e12702fa68c77f6b2490692f3861ad32e7..60f983934d5ea2c8e276d2c493d78caf56e9a39a 100644 (file)
@@ -77,9 +77,8 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const ch
 
 void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
 {
-       int nlen = strlen(name);
-       struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
-       memcpy(res->name, name, nlen + 1);
+       struct name_decoration *res;
+       FLEX_ALLOC_STR(res, name, name);
        res->type = type;
        res->next = add_decoration(&name_decoration, obj, res);
 }
index ddca601c77fcab7d9d68362ce5c4f252ab7ccf51..9b6eac22e4256d8f2bf82961b6e4f320d89fdeba 100644 (file)
@@ -48,40 +48,6 @@ static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our
        return res.ptr;
 }
 
-static int common_outf(void *priv_, mmbuffer_t *mb, int nbuf)
-{
-       int i;
-       mmfile_t *dst = priv_;
-
-       for (i = 0; i < nbuf; i++) {
-               memcpy(dst->ptr + dst->size, mb[i].ptr, mb[i].size);
-               dst->size += mb[i].size;
-       }
-       return 0;
-}
-
-static int generate_common_file(mmfile_t *res, mmfile_t *f1, mmfile_t *f2)
-{
-       unsigned long size = f1->size < f2->size ? f1->size : f2->size;
-       void *ptr = xmalloc(size);
-       xpparam_t xpp;
-       xdemitconf_t xecfg;
-       xdemitcb_t ecb;
-
-       memset(&xpp, 0, sizeof(xpp));
-       xpp.flags = 0;
-       memset(&xecfg, 0, sizeof(xecfg));
-       xecfg.ctxlen = 3;
-       xecfg.flags = XDL_EMIT_COMMON;
-       ecb.outf = common_outf;
-
-       res->ptr = ptr;
-       res->size = 0;
-
-       ecb.priv = res;
-       return xdi_diff(f1, f2, &xpp, &xecfg, &ecb);
-}
-
 void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
 {
        void *res = NULL;
@@ -112,8 +78,8 @@ void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct
                if (fill_mmfile_blob(&common, base) < 0)
                        goto out_free_f2_f1;
        } else {
-               if (generate_common_file(&common, &f1, &f2) < 0)
-                       goto out_free_f2_f1;
+               common.ptr = xstrdup("");
+               common.size = 0;
        }
        res = three_way_filemerge(path, &common, &f1, &f2, size);
        free_mmfile(&common);
index 8eabde20fbe030a1242cf934d6e2e4cdb6ac14e7..b880ae50e7ee4f8e88bda83618529ea30462e4ef 100644 (file)
@@ -482,6 +482,9 @@ static struct string_list *get_renames(struct merge_options *o,
        struct diff_options opts;
 
        renames = xcalloc(1, sizeof(struct string_list));
+       if (!o->detect_rename)
+               return renames;
+
        diff_setup(&opts);
        DIFF_OPT_SET(&opts, RECURSIVE);
        DIFF_OPT_CLR(&opts, RENAME_EMPTY);
@@ -2039,6 +2042,7 @@ void init_merge_options(struct merge_options *o)
        o->diff_rename_limit = -1;
        o->merge_rename_limit = -1;
        o->renormalize = 0;
+       o->detect_rename = 1;
        merge_recursive_config(o);
        if (getenv("GIT_MERGE_VERBOSITY"))
                o->verbosity =
@@ -2088,9 +2092,17 @@ int parse_merge_opt(struct merge_options *o, const char *s)
                o->renormalize = 1;
        else if (!strcmp(s, "no-renormalize"))
                o->renormalize = 0;
-       else if (skip_prefix(s, "rename-threshold=", &arg)) {
+       else if (!strcmp(s, "no-renames"))
+               o->detect_rename = 0;
+       else if (!strcmp(s, "find-renames")) {
+               o->detect_rename = 1;
+               o->rename_score = 0;
+       }
+       else if (skip_prefix(s, "find-renames=", &arg) ||
+                skip_prefix(s, "rename-threshold=", &arg)) {
                if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
                        return -1;
+               o->detect_rename = 1;
        }
        else
                return -1;
index 9e090a347016ea715acf8d26ec5527f033b94ba1..52f0201f68a30114345cbfcc8b3d6204174facfd 100644 (file)
@@ -17,6 +17,7 @@ struct merge_options {
        unsigned renormalize : 1;
        long xdl_opts;
        int verbosity;
+       int detect_rename;
        int diff_rename_limit;
        int merge_rename_limit;
        int rename_score;
index 332ba956e7edec6c6fbe10f346ec08fc469b846f..6d9f23e932559c58c9ebf4679a6035889f726ed2 100644 (file)
@@ -55,10 +55,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
        dir = find_dir_entry(istate, ce->name, namelen);
        if (!dir) {
                /* not found, create it and add to hash table */
-               dir = xcalloc(1, sizeof(struct dir_entry) + namelen + 1);
+               FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
                hashmap_entry_init(dir, memihash(ce->name, namelen));
                dir->namelen = namelen;
-               strncpy(dir->name, ce->name, namelen);
                hashmap_add(&istate->dir_hash, dir);
 
                /* recursively add missing parent directories */
diff --git a/notes.c b/notes.c
index c1e503559076398b1edc5fabbaab8f3cac5ac7d6..88cf4747c16724931682e606a2c1fba4c07b2f49 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -1035,7 +1035,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
        struct string_list_item *item;
        int counter = 0;
        struct notes_tree **trees;
-       trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
+       ALLOC_ARRAY(trees, refs->nr + 1);
        for_each_string_list_item(item, refs) {
                struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
                init_notes(t, item->string, combine_notes_ignore, flags);
index 6bff970c905b6a9c8a04773d64afb164dd124cbe..c30bcd06cbd516eb04e9b55b27b5f091ede61439 100644 (file)
@@ -148,8 +148,7 @@ static uint32_t find_object_pos(const unsigned char *sha1)
        return entry->in_pack_pos;
 }
 
-static void show_object(struct object *object, const struct name_path *path,
-                       const char *last, void *data)
+static void show_object(struct object *object, const char *name, void *data)
 {
        struct bitmap *base = data;
        bitmap_set(base, find_object_pos(object->oid.hash));
index dd8dc16e676fb3e928560002c0cb7e1f36910e1a..b949e517167dbac6c4c347e0a2cbdf8ca2dabbcf 100644 (file)
@@ -414,19 +414,15 @@ static int ext_index_add_object(struct object *object, const char *name)
        return bitmap_pos + bitmap_git.pack->num_objects;
 }
 
-static void show_object(struct object *object, const struct name_path *path,
-                       const char *last, void *data)
+static void show_object(struct object *object, const char *name, void *data)
 {
        struct bitmap *base = data;
        int bitmap_pos;
 
        bitmap_pos = bitmap_position(object->oid.hash);
 
-       if (bitmap_pos < 0) {
-               char *name = path_name(path, last);
+       if (bitmap_pos < 0)
                bitmap_pos = ext_index_add_object(object, name);
-               free(name);
-       }
 
        bitmap_set(base, bitmap_pos);
 }
@@ -894,9 +890,8 @@ struct bitmap_test_data {
        size_t seen;
 };
 
-static void test_show_object(struct object *object,
-                            const struct name_path *path,
-                            const char *last, void *data)
+static void test_show_object(struct object *object, const char *name,
+                            void *data)
 {
        struct bitmap_test_data *tdata = data;
        int bitmap_pos;
index 433bd86ccd5c38ff5a4993be4e1463946aead59d..1da89a41cec9e605fc3fdfa8efaaf2489e501c88 100644 (file)
@@ -89,7 +89,7 @@ static int verify_packfile(struct packed_git *p,
         * we do not do scan-streaming check on the pack file.
         */
        nr_objects = p->num_objects;
-       entries = xmalloc((nr_objects + 1) * sizeof(*entries));
+       ALLOC_ARRAY(entries, nr_objects + 1);
        entries[nr_objects].offset = pack_sig_ofs;
        /* first sort entries by pack offset, since unpacking them is more efficient that way */
        for (i = 0; i < nr_objects; i++) {
index 155a8a3d69bf7d1c4c72aaa36ad483dae33ca9b5..96d51c3467b9ef864f62962aa3567247338d2c3a 100644 (file)
@@ -44,10 +44,14 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
         * keep track of them with alias pointers, always sorting from "from"
         * to "to".
         */
-       struct revindex_entry *tmp = xmalloc(n * sizeof(*tmp));
-       struct revindex_entry *from = entries, *to = tmp;
+       struct revindex_entry *tmp, *from, *to;
        int bits;
-       unsigned *pos = xmalloc(BUCKETS * sizeof(*pos));
+       unsigned *pos;
+
+       ALLOC_ARRAY(pos, BUCKETS);
+       ALLOC_ARRAY(tmp, n);
+       from = entries;
+       to = tmp;
 
        /*
         * If (max >> bits) is zero, then we know that the radix digit we are
@@ -121,7 +125,7 @@ static void create_pack_revindex(struct packed_git *p)
        unsigned i;
        const char *index = p->index_data;
 
-       p->revindex = xmalloc(sizeof(*p->revindex) * (num_ent + 1));
+       ALLOC_ARRAY(p->revindex, num_ent + 1);
        index += 4 * 256;
 
        if (p->index_version > 1) {
diff --git a/pager.c b/pager.c
index e425070528f4f9fbf0953c9056d1ea43686e176f..4bc048148e043eabf1315bbcbae8ea4c6363b330 100644 (file)
--- a/pager.c
+++ b/pager.c
@@ -11,7 +11,6 @@
  * something different on Windows.
  */
 
-static const char *pager_argv[] = { NULL, NULL };
 static struct child_process pager_process = CHILD_PROCESS_INIT;
 
 static void wait_for_pager(int in_signal)
@@ -64,6 +63,16 @@ const char *git_pager(int stdout_is_tty)
        return pager;
 }
 
+void prepare_pager_args(struct child_process *pager_process, const char *pager)
+{
+       argv_array_push(&pager_process->args, pager);
+       pager_process->use_shell = 1;
+       if (!getenv("LESS"))
+               argv_array_push(&pager_process->env_array, "LESS=FRX");
+       if (!getenv("LV"))
+               argv_array_push(&pager_process->env_array, "LV=-c");
+}
+
 void setup_pager(void)
 {
        const char *pager = git_pager(isatty(1));
@@ -80,14 +89,8 @@ void setup_pager(void)
        setenv("GIT_PAGER_IN_USE", "true", 1);
 
        /* spawn the pager */
-       pager_argv[0] = pager;
-       pager_process.use_shell = 1;
-       pager_process.argv = pager_argv;
+       prepare_pager_args(&pager_process, pager);
        pager_process.in = -1;
-       if (!getenv("LESS"))
-               argv_array_push(&pager_process.env_array, "LESS=FRX");
-       if (!getenv("LV"))
-               argv_array_push(&pager_process.env_array, "LV=-c");
        argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
        if (start_command(&pager_process))
                return;
index 9304ee33d75ab9de14e4a7b6dc96f5dd8028c464..c9e9b6c0778c755fcade1430cf674dfd867050f1 100644 (file)
@@ -406,7 +406,8 @@ void parse_pathspec(struct pathspec *pathspec,
                n++;
 
        pathspec->nr = n;
-       pathspec->items = item = xmalloc(sizeof(*item) * n);
+       ALLOC_ARRAY(pathspec->items, n);
+       item = pathspec->items;
        pathspec->_raw = argv;
        prefixlen = prefix ? strlen(prefix) : 0;
 
@@ -483,7 +484,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
 {
        *dst = *src;
-       dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
+       ALLOC_ARRAY(dst->items, dst->nr);
        memcpy(dst->items, src->items,
               sizeof(struct pathspec_item) * dst->nr);
 }
index 353bd37416e65f6dba733ac9d14999f1e293b4b9..76a88c573f7895bbf388ab4335faa1b86c3975d3 100644 (file)
@@ -247,7 +247,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
                size_t len = strlen(msg) + 5;
                struct throughput *tp = progress->throughput;
 
-               bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
+               bufp = (len < sizeof(buf)) ? buf : xmallocz(len);
                if (tp) {
                        unsigned int rate = !tp->avg_misecs ? 0 :
                                        tp->avg_bytes / tp->avg_misecs;
index 43616d49c7f88166d2fa2f009ca5b926d751ab0b..ed352018964ef2260f5da51239800bbcc816ad1b 100644 (file)
@@ -43,15 +43,14 @@ static int add_one_ref(const char *path, const struct object_id *oid,
  * The traversal will have already marked us as SEEN, so we
  * only need to handle any progress reporting here.
  */
-static void mark_object(struct object *obj, const struct name_path *path,
-                       const char *name, void *data)
+static void mark_object(struct object *obj, const char *name, void *data)
 {
        update_progress(data);
 }
 
 static void mark_commit(struct commit *c, void *data)
 {
-       mark_object(&c->object, NULL, NULL, data);
+       mark_object(&c->object, NULL, data);
 }
 
 struct recent_data {
index f097176ed93229fe75533e21dc60e0bbc5a4e7ef..bb79d6b9cc0f5aec16eb1dffb842910ba55e99ab 100644 (file)
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
+struct align {
+       align_type position;
+       unsigned int width;
+};
+
+/*
+ * An atom is a valid field atom listed below, possibly prefixed with
+ * a "*" to denote deref_tag().
+ *
+ * We parse given format string and sort specifiers, and make a list
+ * of properties that we need to extract out of objects.  ref_array_item
+ * structure will hold an array of values extracted that can be
+ * indexed with the "atom number", which is an index into this
+ * array.
+ */
+static struct used_atom {
+       const char *name;
+       cmp_type type;
+       union {
+               char color[COLOR_MAXLEN];
+               struct align align;
+               enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT }
+                       remote_ref;
+               struct {
+                       enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB } option;
+                       unsigned int nlines;
+               } contents;
+               enum { O_FULL, O_SHORT } objectname;
+       } u;
+} *used_atom;
+static int used_atom_cnt, need_tagged, need_symref;
+static int need_color_reset_at_eol;
+
+static void color_atom_parser(struct used_atom *atom, const char *color_value)
+{
+       if (!color_value)
+               die(_("expected format: %%(color:<color>)"));
+       if (color_parse(color_value, atom->u.color) < 0)
+               die(_("unrecognized color: %%(color:%s)"), color_value);
+}
+
+static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (!arg)
+               atom->u.remote_ref = RR_NORMAL;
+       else if (!strcmp(arg, "short"))
+               atom->u.remote_ref = RR_SHORTEN;
+       else if (!strcmp(arg, "track"))
+               atom->u.remote_ref = RR_TRACK;
+       else if (!strcmp(arg, "trackshort"))
+               atom->u.remote_ref = RR_TRACKSHORT;
+       else
+               die(_("unrecognized format: %%(%s)"), atom->name);
+}
+
+static void body_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (arg)
+               die("%%(body) does not take arguments");
+       atom->u.contents.option = C_BODY_DEP;
+}
+
+static void subject_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (arg)
+               die("%%(subject) does not take arguments");
+       atom->u.contents.option = C_SUB;
+}
+
+static void contents_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (!arg)
+               atom->u.contents.option = C_BARE;
+       else if (!strcmp(arg, "body"))
+               atom->u.contents.option = C_BODY;
+       else if (!strcmp(arg, "signature"))
+               atom->u.contents.option = C_SIG;
+       else if (!strcmp(arg, "subject"))
+               atom->u.contents.option = C_SUB;
+       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);
+       } else
+               die(_("unrecognized %%(contents) argument: %s"), arg);
+}
+
+static void objectname_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (!arg)
+               atom->u.objectname = O_FULL;
+       else if (!strcmp(arg, "short"))
+               atom->u.objectname = O_SHORT;
+       else
+               die(_("unrecognized %%(objectname) argument: %s"), arg);
+}
+
+static align_type parse_align_position(const char *s)
+{
+       if (!strcmp(s, "right"))
+               return ALIGN_RIGHT;
+       else if (!strcmp(s, "middle"))
+               return ALIGN_MIDDLE;
+       else if (!strcmp(s, "left"))
+               return ALIGN_LEFT;
+       return -1;
+}
+
+static void align_atom_parser(struct used_atom *atom, const char *arg)
+{
+       struct align *align = &atom->u.align;
+       struct string_list params = STRING_LIST_INIT_DUP;
+       int i;
+       unsigned int width = ~0U;
+
+       if (!arg)
+               die(_("expected format: %%(align:<width>,<position>)"));
+
+       align->position = ALIGN_LEFT;
+
+       string_list_split(&params, arg, ',', -1);
+       for (i = 0; i < params.nr; i++) {
+               const char *s = params.items[i].string;
+               int position;
+
+               if (skip_prefix(s, "position=", &s)) {
+                       position = parse_align_position(s);
+                       if (position < 0)
+                               die(_("unrecognized position:%s"), s);
+                       align->position = position;
+               } else if (skip_prefix(s, "width=", &s)) {
+                       if (strtoul_ui(s, 10, &width))
+                               die(_("unrecognized width:%s"), s);
+               } else if (!strtoul_ui(s, 10, &width))
+                       ;
+               else if ((position = parse_align_position(s)) >= 0)
+                       align->position = position;
+               else
+                       die(_("unrecognized %%(align) argument: %s"), s);
+       }
+
+       if (width == ~0U)
+               die(_("positive width expected with the %%(align) atom"));
+       align->width = width;
+       string_list_clear(&params, 0);
+}
+
 static struct {
        const char *name;
        cmp_type cmp_type;
+       void (*parser)(struct used_atom *atom, const char *arg);
 } valid_atom[] = {
        { "refname" },
        { "objecttype" },
        { "objectsize", FIELD_ULONG },
-       { "objectname" },
+       { "objectname", FIELD_STR, objectname_atom_parser },
        { "tree" },
        { "parent" },
        { "numparent", FIELD_ULONG },
@@ -44,31 +192,21 @@ static struct {
        { "taggerdate", FIELD_TIME },
        { "creator" },
        { "creatordate", FIELD_TIME },
-       { "subject" },
-       { "body" },
-       { "contents" },
-       { "upstream" },
-       { "push" },
+       { "subject", FIELD_STR, subject_atom_parser },
+       { "body", FIELD_STR, body_atom_parser },
+       { "contents", FIELD_STR, contents_atom_parser },
+       { "upstream", FIELD_STR, remote_ref_atom_parser },
+       { "push", FIELD_STR, remote_ref_atom_parser },
        { "symref" },
        { "flag" },
        { "HEAD" },
-       { "color" },
-       { "align" },
+       { "color", FIELD_STR, color_atom_parser },
+       { "align", FIELD_STR, align_atom_parser },
        { "end" },
 };
 
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
 
-struct align {
-       align_type position;
-       unsigned int width;
-};
-
-struct contents {
-       unsigned int lines;
-       struct object_id oid;
-};
-
 struct ref_formatting_stack {
        struct ref_formatting_stack *prev;
        struct strbuf output;
@@ -85,33 +223,18 @@ struct atom_value {
        const char *s;
        union {
                struct align align;
-               struct contents contents;
        } u;
        void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
        unsigned long ul; /* used for sorting when not FIELD_STR */
 };
 
-/*
- * An atom is a valid field atom listed above, possibly prefixed with
- * a "*" to denote deref_tag().
- *
- * We parse given format string and sort specifiers, and make a list
- * of properties that we need to extract out of objects.  ref_array_item
- * structure will hold an array of values extracted that can be
- * indexed with the "atom number", which is an index into this
- * array.
- */
-static const char **used_atom;
-static cmp_type *used_atom_type;
-static int used_atom_cnt, need_tagged, need_symref;
-static int need_color_reset_at_eol;
-
 /*
  * Used to parse format string and sort specifiers
  */
 int parse_ref_filter_atom(const char *atom, const char *ep)
 {
        const char *sp;
+       const char *arg;
        int i, at;
 
        sp = atom;
@@ -122,24 +245,24 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
 
        /* Do we have the atom already used elsewhere? */
        for (i = 0; i < used_atom_cnt; i++) {
-               int len = strlen(used_atom[i]);
-               if (len == ep - atom && !memcmp(used_atom[i], atom, len))
+               int len = strlen(used_atom[i].name);
+               if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
                        return i;
        }
 
        /* Is the atom a valid one? */
        for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
                int len = strlen(valid_atom[i].name);
+
                /*
                 * If the atom name has a colon, strip it and everything after
                 * it off - it specifies the format for this entry, and
                 * shouldn't be used for checking against the valid_atom
                 * table.
                 */
-               const char *formatp = strchr(sp, ':');
-               if (!formatp || ep < formatp)
-                       formatp = ep;
-               if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len))
+               arg = memchr(sp, ':', ep - sp);
+               if (len == (arg ? arg : ep) - sp &&
+                   !memcmp(valid_atom[i].name, sp, len))
                        break;
        }
 
@@ -150,12 +273,16 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        at = used_atom_cnt;
        used_atom_cnt++;
        REALLOC_ARRAY(used_atom, used_atom_cnt);
-       REALLOC_ARRAY(used_atom_type, used_atom_cnt);
-       used_atom[at] = xmemdupz(atom, ep - atom);
-       used_atom_type[at] = valid_atom[i].cmp_type;
+       used_atom[at].name = xmemdupz(atom, ep - atom);
+       used_atom[at].type = valid_atom[i].cmp_type;
+       if (arg)
+               arg = used_atom[at].name + (arg - atom) + 1;
+       memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
+       if (valid_atom[i].parser)
+               valid_atom[i].parser(&used_atom[at], arg);
        if (*atom == '*')
                need_tagged = 1;
-       if (!strcmp(used_atom[at], "symref"))
+       if (!strcmp(used_atom[at].name, "symref"))
                need_symref = 1;
        return at;
 }
@@ -258,22 +385,6 @@ static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_sta
        pop_stack_element(&state->stack);
 }
 
-static int match_atom_name(const char *name, const char *atom_name, const char **val)
-{
-       const char *body;
-
-       if (!skip_prefix(name, atom_name, &body))
-               return 0; /* doesn't even begin with "atom_name" */
-       if (!body[0]) {
-               *val = NULL; /* %(atom_name) and no customization */
-               return 1;
-       }
-       if (body[0] != ':')
-               return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
-       *val = body + 1; /* "atom_name:val" */
-       return 1;
-}
-
 /*
  * In a format string, find the next occurrence of %(atom).
  */
@@ -315,7 +426,7 @@ int verify_ref_format(const char *format)
                at = parse_ref_filter_atom(sp + 2, ep);
                cp = ep + 1;
 
-               if (skip_prefix(used_atom[at], "color:", &color))
+               if (skip_prefix(used_atom[at].name, "color:", &color))
                        need_color_reset_at_eol = !!strcmp(color, "reset");
        }
        return 0;
@@ -340,15 +451,17 @@ static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned lo
 }
 
 static int grab_objectname(const char *name, const unsigned char *sha1,
-                           struct atom_value *v)
+                          struct atom_value *v, struct used_atom *atom)
 {
-       if (!strcmp(name, "objectname")) {
-               v->s = xstrdup(sha1_to_hex(sha1));
-               return 1;
-       }
-       if (!strcmp(name, "objectname:short")) {
-               v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
-               return 1;
+       if (starts_with(name, "objectname")) {
+               if (atom->u.objectname == O_SHORT) {
+                       v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
+                       return 1;
+               } else if (atom->u.objectname == O_FULL) {
+                       v->s = xstrdup(sha1_to_hex(sha1));
+                       return 1;
+               } else
+                       die("BUG: unknown %%(objectname) option");
        }
        return 0;
 }
@@ -359,7 +472,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
        int i;
 
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i];
+               const char *name = used_atom[i].name;
                struct atom_value *v = &val[i];
                if (!!deref != (*name == '*'))
                        continue;
@@ -372,7 +485,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
                        v->s = xstrfmt("%lu", sz);
                }
                else if (deref)
-                       grab_objectname(name, obj->oid.hash, v);
+                       grab_objectname(name, obj->oid.hash, v, &used_atom[i]);
        }
 }
 
@@ -383,7 +496,7 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob
        struct tag *tag = (struct tag *) obj;
 
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i];
+               const char *name = used_atom[i].name;
                struct atom_value *v = &val[i];
                if (!!deref != (*name == '*'))
                        continue;
@@ -405,7 +518,7 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
        struct commit *commit = (struct commit *) obj;
 
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i];
+               const char *name = used_atom[i].name;
                struct atom_value *v = &val[i];
                if (!!deref != (*name == '*'))
                        continue;
@@ -535,7 +648,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
        const char *wholine = NULL;
 
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i];
+               const char *name = used_atom[i].name;
                struct atom_value *v = &val[i];
                if (!!deref != (*name == '*'))
                        continue;
@@ -573,7 +686,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
        if (!wholine)
                return;
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i];
+               const char *name = used_atom[i].name;
                struct atom_value *v = &val[i];
                if (!!deref != (*name == '*'))
                        continue;
@@ -663,20 +776,16 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
        unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
 
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i];
+               struct used_atom *atom = &used_atom[i];
+               const char *name = atom->name;
                struct atom_value *v = &val[i];
-               const char *valp = NULL;
                if (!!deref != (*name == '*'))
                        continue;
                if (deref)
                        name++;
                if (strcmp(name, "subject") &&
                    strcmp(name, "body") &&
-                   strcmp(name, "contents") &&
-                   strcmp(name, "contents:subject") &&
-                   strcmp(name, "contents:body") &&
-                   strcmp(name, "contents:signature") &&
-                   !starts_with(name, "contents:lines="))
+                   !starts_with(name, "contents"))
                        continue;
                if (!subpos)
                        find_subpos(buf, sz,
@@ -684,28 +793,23 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                                    &bodypos, &bodylen, &nonsiglen,
                                    &sigpos, &siglen);
 
-               if (!strcmp(name, "subject"))
-                       v->s = copy_subject(subpos, sublen);
-               else if (!strcmp(name, "contents:subject"))
+               if (atom->u.contents.option == C_SUB)
                        v->s = copy_subject(subpos, sublen);
-               else if (!strcmp(name, "body"))
+               else if (atom->u.contents.option == C_BODY_DEP)
                        v->s = xmemdupz(bodypos, bodylen);
-               else if (!strcmp(name, "contents:body"))
+               else if (atom->u.contents.option == C_BODY)
                        v->s = xmemdupz(bodypos, nonsiglen);
-               else if (!strcmp(name, "contents:signature"))
+               else if (atom->u.contents.option == C_SIG)
                        v->s = xmemdupz(sigpos, siglen);
-               else if (!strcmp(name, "contents"))
-                       v->s = xstrdup(subpos);
-               else if (skip_prefix(name, "contents:lines=", &valp)) {
+               else if (atom->u.contents.option == C_LINES) {
                        struct strbuf s = STRBUF_INIT;
                        const char *contents_end = bodylen + bodypos - siglen;
 
-                       if (strtoul_ui(valp, 10, &v->u.contents.lines))
-                               die(_("positive value expected contents:lines=%s"), valp);
                        /*  Size is the length of the message after removing the signature */
-                       append_lines(&s, subpos, contents_end - subpos, v->u.contents.lines);
+                       append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
                        v->s = strbuf_detach(&s, NULL);
-               }
+               } else if (atom->u.contents.option == C_BARE)
+                       v->s = xstrdup(subpos);
        }
 }
 
@@ -786,6 +890,43 @@ static const char *strip_ref_components(const char *refname, const char *nr_arg)
        return start;
 }
 
+static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
+                                   struct branch *branch, const char **s)
+{
+       int num_ours, num_theirs;
+       if (atom->u.remote_ref == RR_SHORTEN)
+               *s = shorten_unambiguous_ref(refname, warn_ambiguous_refs);
+       else if (atom->u.remote_ref == RR_TRACK) {
+               if (stat_tracking_info(branch, &num_ours,
+                                      &num_theirs, NULL))
+                       return;
+
+               if (!num_ours && !num_theirs)
+                       *s = "";
+               else if (!num_ours)
+                       *s = xstrfmt("[behind %d]", num_theirs);
+               else if (!num_theirs)
+                       *s = xstrfmt("[ahead %d]", num_ours);
+               else
+                       *s = xstrfmt("[ahead %d, behind %d]",
+                                    num_ours, num_theirs);
+       } else if (atom->u.remote_ref == RR_TRACKSHORT) {
+               if (stat_tracking_info(branch, &num_ours,
+                                      &num_theirs, NULL))
+                       return;
+
+               if (!num_ours && !num_theirs)
+                       *s = "=";
+               else if (!num_ours)
+                       *s = "<";
+               else if (!num_theirs)
+                       *s = ">";
+               else
+                       *s = "<>";
+       } else /* RR_NORMAL */
+               *s = refname;
+}
+
 /*
  * Parse the object referred by ref, and grab needed value.
  */
@@ -809,12 +950,12 @@ static void populate_value(struct ref_array_item *ref)
 
        /* Fill in specials first */
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i];
+               struct used_atom *atom = &used_atom[i];
+               const char *name = used_atom[i].name;
                struct atom_value *v = &ref->value[i];
                int deref = 0;
                const char *refname;
                const char *formatp;
-               const char *valp;
                struct branch *branch = NULL;
 
                v->handler = append_atom;
@@ -837,8 +978,9 @@ static void populate_value(struct ref_array_item *ref)
                        branch = branch_get(branch_name);
 
                        refname = branch_get_upstream(branch, NULL);
-                       if (!refname)
-                               continue;
+                       if (refname)
+                               fill_remote_ref_details(atom, refname, branch, &v->s);
+                       continue;
                } else if (starts_with(name, "push")) {
                        const char *branch_name;
                        if (!skip_prefix(ref->refname, "refs/heads/",
@@ -849,14 +991,10 @@ static void populate_value(struct ref_array_item *ref)
                        refname = branch_get_push(branch, NULL);
                        if (!refname)
                                continue;
-               } else if (match_atom_name(name, "color", &valp)) {
-                       char color[COLOR_MAXLEN] = "";
-
-                       if (!valp)
-                               die(_("expected format: %%(color:<color>)"));
-                       if (color_parse(valp, color) < 0)
-                               die(_("unable to parse format"));
-                       v->s = xstrdup(color);
+                       fill_remote_ref_details(atom, refname, branch, &v->s);
+                       continue;
+               } else if (starts_with(name, "color:")) {
+                       v->s = atom->u.color;
                        continue;
                } else if (!strcmp(name, "flag")) {
                        char buf[256], *cp = buf;
@@ -871,7 +1009,7 @@ static void populate_value(struct ref_array_item *ref)
                                v->s = xstrdup(buf + 1);
                        }
                        continue;
-               } else if (!deref && grab_objectname(name, ref->objectname, v)) {
+               } else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
                        continue;
                } else if (!strcmp(name, "HEAD")) {
                        const char *head;
@@ -884,43 +1022,8 @@ static void populate_value(struct ref_array_item *ref)
                        else
                                v->s = " ";
                        continue;
-               } else if (match_atom_name(name, "align", &valp)) {
-                       struct align *align = &v->u.align;
-                       struct strbuf **s, **to_free;
-                       int width = -1;
-
-                       if (!valp)
-                               die(_("expected format: %%(align:<width>,<position>)"));
-
-                       /*
-                        * TODO: Implement a function similar to strbuf_split_str()
-                        * which would omit the separator from the end of each value.
-                        */
-                       s = to_free = strbuf_split_str(valp, ',', 0);
-
-                       align->position = ALIGN_LEFT;
-
-                       while (*s) {
-                               /*  Strip trailing comma */
-                               if (s[1])
-                                       strbuf_setlen(s[0], s[0]->len - 1);
-                               if (!strtoul_ui(s[0]->buf, 10, (unsigned int *)&width))
-                                       ;
-                               else if (!strcmp(s[0]->buf, "left"))
-                                       align->position = ALIGN_LEFT;
-                               else if (!strcmp(s[0]->buf, "right"))
-                                       align->position = ALIGN_RIGHT;
-                               else if (!strcmp(s[0]->buf, "middle"))
-                                       align->position = ALIGN_MIDDLE;
-                               else
-                                       die(_("improper format entered align:%s"), s[0]->buf);
-                               s++;
-                       }
-
-                       if (width < 0)
-                               die(_("positive width expected with the %%(align) atom"));
-                       align->width = width;
-                       strbuf_list_free(to_free);
+               } else if (starts_with(name, "align")) {
+                       v->u.align = atom->u.align;
                        v->handler = align_atom_handler;
                        continue;
                } else if (!strcmp(name, "end")) {
@@ -931,7 +1034,6 @@ static void populate_value(struct ref_array_item *ref)
 
                formatp = strchr(name, ':');
                if (formatp) {
-                       int num_ours, num_theirs;
                        const char *arg;
 
                        formatp++;
@@ -940,43 +1042,7 @@ static void populate_value(struct ref_array_item *ref)
                                                      warn_ambiguous_refs);
                        else if (skip_prefix(formatp, "strip=", &arg))
                                refname = strip_ref_components(refname, arg);
-                       else if (!strcmp(formatp, "track") &&
-                                (starts_with(name, "upstream") ||
-                                 starts_with(name, "push"))) {
-
-                               if (stat_tracking_info(branch, &num_ours,
-                                                      &num_theirs, NULL))
-                                       continue;
-
-                               if (!num_ours && !num_theirs)
-                                       v->s = "";
-                               else if (!num_ours)
-                                       v->s = xstrfmt("[behind %d]", num_theirs);
-                               else if (!num_theirs)
-                                       v->s = xstrfmt("[ahead %d]", num_ours);
-                               else
-                                       v->s = xstrfmt("[ahead %d, behind %d]",
-                                                      num_ours, num_theirs);
-                               continue;
-                       } else if (!strcmp(formatp, "trackshort") &&
-                                  (starts_with(name, "upstream") ||
-                                   starts_with(name, "push"))) {
-                               assert(branch);
-
-                               if (stat_tracking_info(branch, &num_ours,
-                                                       &num_theirs, NULL))
-                                       continue;
-
-                               if (!num_ours && !num_theirs)
-                                       v->s = "=";
-                               else if (!num_ours)
-                                       v->s = "<";
-                               else if (!num_theirs)
-                                       v->s = ">";
-                               else
-                                       v->s = "<>";
-                               continue;
-                       } else
+                       else
                                die("unknown %.*s format %s",
                                    (int)(formatp - name), name, formatp);
                }
@@ -1255,10 +1321,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
                                                 const unsigned char *objectname,
                                                 int flag)
 {
-       size_t len = strlen(refname);
-       struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
-       memcpy(ref->refname, refname, len);
-       ref->refname[len] = '\0';
+       struct ref_array_item *ref;
+       FLEX_ALLOC_STR(ref, refname, refname);
        hashcpy(ref->objectname, objectname);
        ref->flag = flag;
 
@@ -1471,7 +1535,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
 {
        struct atom_value *va, *vb;
        int cmp;
-       cmp_type cmp_type = used_atom_type[s->atom];
+       cmp_type cmp_type = used_atom[s->atom].type;
 
        get_ref_atom_value(a, s->atom, &va);
        get_ref_atom_value(b, s->atom, &vb);
diff --git a/refs.c b/refs.c
index e2d34b253e4c9c4efcb30e88d11ccd3ce9e14deb..b0e6ece6f43437bb7d59490d64802baa3b277090 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -124,7 +124,7 @@ int refname_is_safe(const char *refname)
                char *buf;
                int result;
 
-               buf = xmalloc(strlen(refname) + 1);
+               buf = xmallocz(strlen(refname));
                /*
                 * Does the refname try to escape refs/?
                 * For example: refs/foo/../bar is safe but refs/foo/../../bar
@@ -761,10 +761,8 @@ void ref_transaction_free(struct ref_transaction *transaction)
 static struct ref_update *add_update(struct ref_transaction *transaction,
                                     const char *refname)
 {
-       size_t len = strlen(refname) + 1;
-       struct ref_update *update = xcalloc(1, sizeof(*update) + len);
-
-       memcpy((char *)update->refname, refname, len); /* includes NUL */
+       struct ref_update *update;
+       FLEX_ALLOC_STR(update, refname, refname);
        ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
        transaction->updates[transaction->nr++] = update;
        return update;
@@ -908,7 +906,7 @@ char *shorten_unambiguous_ref(const char *refname, int strict)
                        /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
                        total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
 
-               scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len);
+               scanf_fmts = xmalloc(st_add(st_mult(nr_rules, sizeof(char *)), total_len));
 
                offset = 0;
                for (i = 0; i < nr_rules; i++) {
index b56976288819c87307c03e4e5abf9b9458d09970..81f68f846b69af65badfbe8c25d4a03601fb23c8 100644 (file)
@@ -199,17 +199,14 @@ static struct ref_entry *create_ref_entry(const char *refname,
                                          const unsigned char *sha1, int flag,
                                          int check_name)
 {
-       int len;
        struct ref_entry *ref;
 
        if (check_name &&
            check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
                die("Reference has invalid format: '%s'", refname);
-       len = strlen(refname) + 1;
-       ref = xmalloc(sizeof(struct ref_entry) + len);
+       FLEX_ALLOC_STR(ref, name, refname);
        hashcpy(ref->u.value.oid.hash, sha1);
        oidclr(&ref->u.value.peeled);
-       memcpy(ref->name, refname, len);
        ref->flag = flag;
        return ref;
 }
@@ -268,9 +265,7 @@ static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
                                          int incomplete)
 {
        struct ref_entry *direntry;
-       direntry = xcalloc(1, sizeof(struct ref_entry) + len + 1);
-       memcpy(direntry->name, dirname, len);
-       direntry->name[len] = '\0';
+       FLEX_ALLOC_MEM(direntry, name, dirname, len);
        direntry->u.subdir.ref_cache = ref_cache;
        direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
        return direntry;
@@ -939,13 +934,10 @@ static void clear_loose_ref_cache(struct ref_cache *refs)
  */
 static struct ref_cache *create_ref_cache(const char *submodule)
 {
-       int len;
        struct ref_cache *refs;
        if (!submodule)
                submodule = "";
-       len = strlen(submodule) + 1;
-       refs = xcalloc(1, sizeof(struct ref_cache) + len);
-       memcpy(refs->name, submodule, len);
+       FLEX_ALLOC_STR(refs, name, submodule);
        refs->next = submodule_ref_caches;
        submodule_ref_caches = refs;
        return refs;
@@ -2197,10 +2189,9 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
 
        /* Schedule the loose reference for pruning if requested. */
        if ((cb->flags & PACK_REFS_PRUNE)) {
-               int namelen = strlen(entry->name) + 1;
-               struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
+               struct ref_to_prune *n;
+               FLEX_ALLOC_STR(n, name, entry->name);
                hashcpy(n->sha1, entry->u.value.oid.hash);
-               memcpy(n->name, entry->name, namelen); /* includes NUL */
                n->next = cb->ref_to_prune;
                cb->ref_to_prune = n;
        }
index c7048575fbdca1bd8dc57c114d0cfdc031e63039..15e48e25fb9fb8cd2e9e3e7a63cf08d2f9483ea2 100644 (file)
@@ -119,6 +119,19 @@ static int set_option(const char *name, const char *value)
                else
                        return -1;
                return 0;
+
+#if LIBCURL_VERSION_NUM >= 0x070a08
+       } else if (!strcmp(name, "family")) {
+               if (!strcmp(value, "ipv4"))
+                       git_curl_ipresolve = CURL_IPRESOLVE_V4;
+               else if (!strcmp(value, "ipv6"))
+                       git_curl_ipresolve = CURL_IPRESOLVE_V6;
+               else if (!strcmp(value, "all"))
+                       git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
+               else
+                       return -1;
+               return 0;
+#endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
        } else {
                return 1 /* unsupported */;
        }
@@ -439,8 +452,20 @@ static int run_slot(struct active_request_slot *slot,
        err = run_one_slot(slot, results);
 
        if (err != HTTP_OK && err != HTTP_REAUTH) {
-               error("RPC failed; result=%d, HTTP code = %ld",
-                     results->curl_result, results->http_code);
+               struct strbuf msg = STRBUF_INIT;
+               if (results->http_code && results->http_code != 200)
+                       strbuf_addf(&msg, "HTTP %ld", results->http_code);
+               if (results->curl_result != CURLE_OK) {
+                       if (msg.len)
+                               strbuf_addch(&msg, ' ');
+                       strbuf_addf(&msg, "curl %d", results->curl_result);
+                       if (curl_errorstr[0]) {
+                               strbuf_addch(&msg, ' ');
+                               strbuf_addstr(&msg, curl_errorstr);
+                       }
+               }
+               error("RPC failed; %s", msg.buf);
+               strbuf_release(&msg);
        }
 
        return err;
@@ -696,9 +721,10 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
 {
        struct walker *walker;
-       char **targets = xmalloc(nr_heads * sizeof(char*));
+       char **targets;
        int ret, i;
 
+       ALLOC_ARRAY(targets, nr_heads);
        if (options.depth)
                die("dumb http transport does not support --depth");
        for (i = 0; i < nr_heads; i++)
@@ -845,23 +871,22 @@ static void parse_fetch(struct strbuf *buf)
 
 static int push_dav(int nr_spec, char **specs)
 {
-       const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
-       int argc = 0, i;
+       struct child_process child = CHILD_PROCESS_INIT;
+       size_t i;
 
-       argv[argc++] = "http-push";
-       argv[argc++] = "--helper-status";
+       child.git_cmd = 1;
+       argv_array_push(&child.args, "http-push");
+       argv_array_push(&child.args, "--helper-status");
        if (options.dry_run)
-               argv[argc++] = "--dry-run";
+               argv_array_push(&child.args, "--dry-run");
        if (options.verbosity > 1)
-               argv[argc++] = "--verbose";
-       argv[argc++] = url.buf;
+               argv_array_push(&child.args, "--verbose");
+       argv_array_push(&child.args, url.buf);
        for (i = 0; i < nr_spec; i++)
-               argv[argc++] = specs[i];
-       argv[argc++] = NULL;
+               argv_array_push(&child.args, specs[i]);
 
-       if (run_command_v_opt(argv, RUN_GIT_CMD))
-               die("git-%s failed", argv[0]);
-       free(argv);
+       if (run_command(&child))
+               die("git-http-push failed");
        return 0;
 }
 
index 02e698a5e05a02f8905ec511851eea15e86d746f..fc02698587c61d230200272ce4704403e1a6741f 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -318,93 +318,88 @@ static void read_branches_file(struct remote *remote)
 static int handle_config(const char *key, const char *value, void *cb)
 {
        const char *name;
+       int namelen;
        const char *subkey;
        struct remote *remote;
        struct branch *branch;
-       if (starts_with(key, "branch.")) {
-               name = key + 7;
-               subkey = strrchr(name, '.');
-               if (!subkey)
+       if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
+               if (!name)
                        return 0;
-               branch = make_branch(name, subkey - name);
-               if (!strcmp(subkey, ".remote")) {
+               branch = make_branch(name, namelen);
+               if (!strcmp(subkey, "remote")) {
                        return git_config_string(&branch->remote_name, key, value);
-               } else if (!strcmp(subkey, ".pushremote")) {
+               } else if (!strcmp(subkey, "pushremote")) {
                        return git_config_string(&branch->pushremote_name, key, value);
-               } else if (!strcmp(subkey, ".merge")) {
+               } else if (!strcmp(subkey, "merge")) {
                        if (!value)
                                return config_error_nonbool(key);
                        add_merge(branch, xstrdup(value));
                }
                return 0;
        }
-       if (starts_with(key, "url.")) {
+       if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
                struct rewrite *rewrite;
-               name = key + 4;
-               subkey = strrchr(name, '.');
-               if (!subkey)
+               if (!name)
                        return 0;
-               if (!strcmp(subkey, ".insteadof")) {
-                       rewrite = make_rewrite(&rewrites, name, subkey - name);
+               if (!strcmp(subkey, "insteadof")) {
+                       rewrite = make_rewrite(&rewrites, name, namelen);
                        if (!value)
                                return config_error_nonbool(key);
                        add_instead_of(rewrite, xstrdup(value));
-               } else if (!strcmp(subkey, ".pushinsteadof")) {
-                       rewrite = make_rewrite(&rewrites_push, name, subkey - name);
+               } else if (!strcmp(subkey, "pushinsteadof")) {
+                       rewrite = make_rewrite(&rewrites_push, name, namelen);
                        if (!value)
                                return config_error_nonbool(key);
                        add_instead_of(rewrite, xstrdup(value));
                }
        }
 
-       if (!starts_with(key,  "remote."))
+       if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
                return 0;
-       name = key + 7;
 
        /* Handle remote.* variables */
-       if (!strcmp(name, "pushdefault"))
+       if (!name && !strcmp(subkey, "pushdefault"))
                return git_config_string(&pushremote_name, key, value);
 
+       if (!name)
+               return 0;
        /* Handle remote.<name>.* variables */
        if (*name == '/') {
                warning("Config remote shorthand cannot begin with '/': %s",
                        name);
                return 0;
        }
-       subkey = strrchr(name, '.');
-       if (!subkey)
-               return 0;
-       remote = make_remote(name, subkey - name);
+       remote = make_remote(name, namelen);
        remote->origin = REMOTE_CONFIG;
-       if (!strcmp(subkey, ".mirror"))
+       if (!strcmp(subkey, "mirror"))
                remote->mirror = git_config_bool(key, value);
-       else if (!strcmp(subkey, ".skipdefaultupdate"))
+       else if (!strcmp(subkey, "skipdefaultupdate"))
                remote->skip_default_update = git_config_bool(key, value);
-       else if (!strcmp(subkey, ".skipfetchall"))
+       else if (!strcmp(subkey, "skipfetchall"))
                remote->skip_default_update = git_config_bool(key, value);
-       else if (!strcmp(subkey, ".prune"))
+       else if (!strcmp(subkey, "prune"))
                remote->prune = git_config_bool(key, value);
-       else if (!strcmp(subkey, ".url")) {
+       else if (!strcmp(subkey, "url")) {
                const char *v;
                if (git_config_string(&v, key, value))
                        return -1;
                add_url(remote, v);
-       } else if (!strcmp(subkey, ".pushurl")) {
+       } else if (!strcmp(subkey, "pushurl")) {
                const char *v;
                if (git_config_string(&v, key, value))
                        return -1;
                add_pushurl(remote, v);
-       } else if (!strcmp(subkey, ".push")) {
+       } else if (!strcmp(subkey, "push")) {
                const char *v;
                if (git_config_string(&v, key, value))
                        return -1;
                add_push_refspec(remote, v);
-       } else if (!strcmp(subkey, ".fetch")) {
+       } else if (!strcmp(subkey, "fetch")) {
                const char *v;
                if (git_config_string(&v, key, value))
                        return -1;
                add_fetch_refspec(remote, v);
-       } else if (!strcmp(subkey, ".receivepack")) {
+       } else if (!strcmp(subkey, "receivepack")) {
                const char *v;
                if (git_config_string(&v, key, value))
                        return -1;
@@ -412,7 +407,7 @@ static int handle_config(const char *key, const char *value, void *cb)
                        remote->receivepack = v;
                else
                        error("more than one receivepack given, using the first");
-       } else if (!strcmp(subkey, ".uploadpack")) {
+       } else if (!strcmp(subkey, "uploadpack")) {
                const char *v;
                if (git_config_string(&v, key, value))
                        return -1;
@@ -420,18 +415,18 @@ static int handle_config(const char *key, const char *value, void *cb)
                        remote->uploadpack = v;
                else
                        error("more than one uploadpack given, using the first");
-       } else if (!strcmp(subkey, ".tagopt")) {
+       } else if (!strcmp(subkey, "tagopt")) {
                if (!strcmp(value, "--no-tags"))
                        remote->fetch_tags = -1;
                else if (!strcmp(value, "--tags"))
                        remote->fetch_tags = 2;
-       } else if (!strcmp(subkey, ".proxy")) {
+       } else if (!strcmp(subkey, "proxy")) {
                return git_config_string((const char **)&remote->http_proxy,
                                         key, value);
-       } else if (!strcmp(subkey, ".proxyauthmethod")) {
+       } else if (!strcmp(subkey, "proxyauthmethod")) {
                return git_config_string((const char **)&remote->http_proxy_authmethod,
                                         key, value);
-       } else if (!strcmp(subkey, ".vcs")) {
+       } else if (!strcmp(subkey, "vcs")) {
                return git_config_string(&remote->foreign_vcs, key, value);
        }
        return 0;
@@ -718,18 +713,9 @@ struct remote *pushremote_get(const char *name)
        return remote_get_1(name, pushremote_for_branch);
 }
 
-int remote_is_configured(const char *name)
+int remote_is_configured(struct remote *remote)
 {
-       struct remotes_hash_key lookup;
-       struct hashmap_entry lookup_entry;
-       read_config();
-
-       init_remotes_hash();
-       lookup.str = name;
-       lookup.len = strlen(name);
-       hashmap_entry_init(&lookup_entry, memhash(name, lookup.len));
-
-       return hashmap_get(&remotes_hash, &lookup_entry, &lookup) != NULL;
+       return remote && remote->origin;
 }
 
 int for_each_remote(each_remote_fn fn, void *priv)
@@ -931,7 +917,7 @@ static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
                const char *name)
 {
        size_t len = strlen(name);
-       struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);
+       struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1));
        memcpy(ref->name, prefix, prefixlen);
        memcpy(ref->name + prefixlen, name, len);
        return ref;
@@ -948,9 +934,9 @@ struct ref *copy_ref(const struct ref *ref)
        size_t len;
        if (!ref)
                return NULL;
-       len = strlen(ref->name);
-       cpy = xmalloc(sizeof(struct ref) + len + 1);
-       memcpy(cpy, ref, sizeof(struct ref) + len + 1);
+       len = st_add3(sizeof(struct ref), strlen(ref->name), 1);
+       cpy = xmalloc(len);
+       memcpy(cpy, ref, len);
        cpy->next = NULL;
        cpy->symref = xstrdup_or_null(ref->symref);
        cpy->remote_status = xstrdup_or_null(ref->remote_status);
@@ -2136,16 +2122,13 @@ static int one_local_ref(const char *refname, const struct object_id *oid,
 {
        struct ref ***local_tail = cb_data;
        struct ref *ref;
-       int len;
 
        /* we already know it starts with refs/ to get here */
        if (check_refname_format(refname + 5, 0))
                return 0;
 
-       len = strlen(refname) + 1;
-       ref = xcalloc(1, sizeof(*ref) + len);
+       ref = alloc_ref(refname);
        oidcpy(&ref->new_oid, oid);
-       memcpy(ref->name, refname, len);
        **local_tail = ref;
        *local_tail = &ref->next;
        return 0;
index 4fd7a0f9b2764d3de935f6387f6a93bfd7ab13b0..c21fd3788c78f28d57e7d76d472dc411986b34d1 100644 (file)
--- a/remote.h
+++ b/remote.h
@@ -5,6 +5,7 @@
 #include "hashmap.h"
 
 enum {
+       REMOTE_UNCONFIGURED = 0,
        REMOTE_CONFIG,
        REMOTE_REMOTES,
        REMOTE_BRANCHES
@@ -59,7 +60,7 @@ struct remote {
 
 struct remote *remote_get(const char *name);
 struct remote *pushremote_get(const char *name);
-int remote_is_configured(const char *name);
+int remote_is_configured(struct remote *remote);
 
 typedef int each_remote_fn(struct remote *remote, void *priv);
 int for_each_remote(each_remote_fn fn, void *priv);
index f24ead53d537b05d7819ce53b45b05a15ec60a96..8b2dfe3160784f9780cf541a674fe89100d38a93 100644 (file)
@@ -25,69 +25,13 @@ volatile show_early_output_fn_t show_early_output;
 static const char *term_bad;
 static const char *term_good;
 
-char *path_name(const struct name_path *path, const char *name)
-{
-       const struct name_path *p;
-       char *n, *m;
-       int nlen = strlen(name);
-       int len = nlen + 1;
-
-       for (p = path; p; p = p->up) {
-               if (p->elem_len)
-                       len += p->elem_len + 1;
-       }
-       n = xmalloc(len);
-       m = n + len - (nlen + 1);
-       memcpy(m, name, nlen + 1);
-       for (p = path; p; p = p->up) {
-               if (p->elem_len) {
-                       m -= p->elem_len + 1;
-                       memcpy(m, p->elem, p->elem_len);
-                       m[p->elem_len] = '/';
-               }
-       }
-       return n;
-}
-
-static int show_path_component_truncated(FILE *out, const char *name, int len)
-{
-       int cnt;
-       for (cnt = 0; cnt < len; cnt++) {
-               int ch = name[cnt];
-               if (!ch || ch == '\n')
-                       return -1;
-               fputc(ch, out);
-       }
-       return len;
-}
-
-static int show_path_truncated(FILE *out, const struct name_path *path)
-{
-       int emitted, ours;
-
-       if (!path)
-               return 0;
-       emitted = show_path_truncated(out, path->up);
-       if (emitted < 0)
-               return emitted;
-       if (emitted)
-               fputc('/', out);
-       ours = show_path_component_truncated(out, path->elem, path->elem_len);
-       if (ours < 0)
-               return ours;
-       return ours || emitted;
-}
-
-void show_object_with_name(FILE *out, struct object *obj,
-                          const struct name_path *path, const char *component)
+void show_object_with_name(FILE *out, struct object *obj, const char *name)
 {
-       struct name_path leaf;
-       leaf.up = (struct name_path *)path;
-       leaf.elem = component;
-       leaf.elem_len = strlen(component);
+       const char *p;
 
        fprintf(out, "%s ", oid_to_hex(&obj->oid));
-       show_path_truncated(out, &leaf);
+       for (p = name; *p && *p != '\n'; p++)
+               fputc(*p, out);
        fputc('\n', out);
 }
 
@@ -540,7 +484,7 @@ struct treesame_state {
 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
 {
        unsigned n = commit_list_count(commit->parents);
-       struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
+       struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
        st->nparents = n;
        add_decoration(&revs->treesame, &commit->object, st);
        return st;
index 23857c0ed1d7a304c848842711a920e678fd506d..dca0d381715cf5bc6888587feaec989012f51252 100644 (file)
@@ -257,16 +257,9 @@ extern void put_revision_mark(const struct rev_info *revs,
 extern void mark_parents_uninteresting(struct commit *commit);
 extern void mark_tree_uninteresting(struct tree *tree);
 
-struct name_path {
-       struct name_path *up;
-       int elem_len;
-       const char *elem;
-};
-
-char *path_name(const struct name_path *path, const char *name);
+char *path_name(struct strbuf *path, const char *name);
 
-extern void show_object_with_name(FILE *, struct object *,
-                                 const struct name_path *, const char *);
+extern void show_object_with_name(FILE *, struct object *, const char *);
 
 extern void add_pending_object(struct rev_info *revs,
                               struct object *obj, const char *name);
index cdf01845790849f863aef4195a68e12fa19ca543..019f6d19a5a10718bd4aa94cf1076312e0297016 100644 (file)
@@ -160,50 +160,41 @@ int sane_execvp(const char *file, char * const argv[])
        return -1;
 }
 
-static const char **prepare_shell_cmd(const char **argv)
+static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
 {
-       int argc, nargc = 0;
-       const char **nargv;
-
-       for (argc = 0; argv[argc]; argc++)
-               ; /* just counting */
-       /* +1 for NULL, +3 for "sh -c" plus extra $0 */
-       nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
-
-       if (argc < 1)
+       if (!argv[0])
                die("BUG: shell command is empty");
 
        if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
 #ifndef GIT_WINDOWS_NATIVE
-               nargv[nargc++] = SHELL_PATH;
+               argv_array_push(out, SHELL_PATH);
 #else
-               nargv[nargc++] = "sh";
+               argv_array_push(out, "sh");
 #endif
-               nargv[nargc++] = "-c";
-
-               if (argc < 2)
-                       nargv[nargc++] = argv[0];
-               else {
-                       struct strbuf arg0 = STRBUF_INIT;
-                       strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
-                       nargv[nargc++] = strbuf_detach(&arg0, NULL);
-               }
-       }
+               argv_array_push(out, "-c");
 
-       for (argc = 0; argv[argc]; argc++)
-               nargv[nargc++] = argv[argc];
-       nargv[nargc] = NULL;
+               /*
+                * If we have no extra arguments, we do not even need to
+                * bother with the "$@" magic.
+                */
+               if (!argv[1])
+                       argv_array_push(out, argv[0]);
+               else
+                       argv_array_pushf(out, "%s \"$@\"", argv[0]);
+       }
 
-       return nargv;
+       argv_array_pushv(out, argv);
+       return out->argv;
 }
 
 #ifndef GIT_WINDOWS_NATIVE
 static int execv_shell_cmd(const char **argv)
 {
-       const char **nargv = prepare_shell_cmd(argv);
-       trace_argv_printf(nargv, "trace: exec:");
-       sane_execvp(nargv[0], (char **)nargv);
-       free(nargv);
+       struct argv_array nargv = ARGV_ARRAY_INIT;
+       prepare_shell_cmd(&nargv, argv);
+       trace_argv_printf(nargv.argv, "trace: exec:");
+       sane_execvp(nargv.argv[0], (char **)nargv.argv);
+       argv_array_clear(&nargv);
        return -1;
 }
 #endif
@@ -457,6 +448,7 @@ int start_command(struct child_process *cmd)
 {
        int fhin = 0, fhout = 1, fherr = 2;
        const char **sargv = cmd->argv;
+       struct argv_array nargv = ARGV_ARRAY_INIT;
 
        if (cmd->no_stdin)
                fhin = open("/dev/null", O_RDWR);
@@ -482,9 +474,9 @@ int start_command(struct child_process *cmd)
                fhout = dup(cmd->out);
 
        if (cmd->git_cmd)
-               cmd->argv = prepare_git_cmd(cmd->argv);
+               cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
        else if (cmd->use_shell)
-               cmd->argv = prepare_shell_cmd(cmd->argv);
+               cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
 
        cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
                        cmd->dir, fhin, fhout, fherr);
@@ -494,9 +486,7 @@ int start_command(struct child_process *cmd)
        if (cmd->clean_on_exit && cmd->pid >= 0)
                mark_child_for_cleanup(cmd->pid);
 
-       if (cmd->git_cmd)
-               free(cmd->argv);
-
+       argv_array_clear(&nargv);
        cmd->argv = sargv;
        if (fhin != 0)
                close(fhin);
index 80487860c8fc5c79e3ffd4fbe21a0a0ea7e385ca..e66f2fe0f0409cccf1647f7851044f91d68252a6 100644 (file)
@@ -124,42 +124,33 @@ static const char *action_name(const struct replay_opts *opts)
 
 struct commit_message {
        char *parent_label;
-       const char *label;
-       const char *subject;
+       char *label;
+       char *subject;
        const char *message;
 };
 
 static int get_message(struct commit *commit, struct commit_message *out)
 {
        const char *abbrev, *subject;
-       int abbrev_len, subject_len;
-       char *q;
-
-       if (!git_commit_encoding)
-               git_commit_encoding = "UTF-8";
+       int subject_len;
 
-       out->message = logmsg_reencode(commit, NULL, git_commit_encoding);
+       out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding());
        abbrev = find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV);
-       abbrev_len = strlen(abbrev);
 
        subject_len = find_commit_subject(out->message, &subject);
 
-       out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
-                             strlen("... ") + subject_len + 1);
-       q = out->parent_label;
-       q = mempcpy(q, "parent of ", strlen("parent of "));
-       out->label = q;
-       q = mempcpy(q, abbrev, abbrev_len);
-       q = mempcpy(q, "... ", strlen("... "));
-       out->subject = q;
-       q = mempcpy(q, subject, subject_len);
-       *q = '\0';
+       out->subject = xmemdupz(subject, subject_len);
+       out->label = xstrfmt("%s... %s", abbrev, out->subject);
+       out->parent_label = xstrfmt("parent of %s", out->label);
+
        return 0;
 }
 
 static void free_message(struct commit *commit, struct commit_message *msg)
 {
        free(msg->parent_label);
+       free(msg->label);
+       free(msg->subject);
        unuse_commit_buffer(commit, msg->message);
 }
 
diff --git a/setup.c b/setup.c
index 0deb02238ba426144e0b7b077c9f2ad5c7993d23..de1a2a7ea5973fef256328a26730117466c15172 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -88,7 +88,7 @@ char *prefix_path_gently(const char *prefix, int len,
        const char *orig = path;
        char *sanitized;
        if (is_absolute_path(orig)) {
-               sanitized = xmalloc(strlen(path) + 1);
+               sanitized = xmallocz(strlen(path));
                if (remaining_prefix)
                        *remaining_prefix = 0;
                if (normalize_path_copy_len(sanitized, path, remaining_prefix)) {
@@ -139,9 +139,7 @@ int check_filename(const char *prefix, const char *arg)
                if (arg[2] == '\0') /* ":/" is root dir, always exists */
                        return 1;
                name = arg + 2;
-       } else if (!no_wildcard(arg))
-               return 1;
-       else if (prefix)
+       } else if (prefix)
                name = prefix_filename(prefix, strlen(prefix), arg);
        else
                name = arg;
@@ -202,7 +200,7 @@ void verify_filename(const char *prefix,
 {
        if (*arg == '-')
                die("bad flag '%s' used after filename", arg);
-       if (check_filename(prefix, arg))
+       if (check_filename(prefix, arg) || !no_wildcard(arg))
                return;
        die_verify_filename(prefix, arg, diagnose_misspelt_rev);
 }
@@ -488,14 +486,13 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
                error_code = READ_GITFILE_ERR_OPEN_FAILED;
                goto cleanup_return;
        }
-       buf = xmalloc(st.st_size + 1);
+       buf = xmallocz(st.st_size);
        len = read_in_full(fd, buf, st.st_size);
        close(fd);
        if (len != st.st_size) {
                error_code = READ_GITFILE_ERR_READ_FAILED;
                goto cleanup_return;
        }
-       buf[len] = '\0';
        if (!starts_with(buf, "gitdir: ")) {
                error_code = READ_GITFILE_ERR_INVALID_FORMAT;
                goto cleanup_return;
index aab1872b4c95416fc7c8ae9c93cd25f341e2d4c1..02517009c15ffe5ef8bc8174805a9746e809eeab 100644 (file)
@@ -253,7 +253,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
 {
        struct alternate_object_database *ent;
        struct alternate_object_database *alt;
-       int pfxlen, entlen;
+       size_t pfxlen, entlen;
        struct strbuf pathbuf = STRBUF_INIT;
 
        if (!is_absolute_path(entry) && relative_base) {
@@ -273,8 +273,8 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
        while (pfxlen && pathbuf.buf[pfxlen-1] == '/')
                pfxlen -= 1;
 
-       entlen = pfxlen + 43; /* '/' + 2 hex + '/' + 38 hex + NUL */
-       ent = xmalloc(sizeof(*ent) + entlen);
+       entlen = st_add(pfxlen, 43); /* '/' + 2 hex + '/' + 38 hex + NUL */
+       ent = xmalloc(st_add(sizeof(*ent), entlen));
        memcpy(ent->base, pathbuf.buf, pfxlen);
        strbuf_release(&pathbuf);
 
@@ -1134,7 +1134,7 @@ unsigned char *use_pack(struct packed_git *p,
 
 static struct packed_git *alloc_packed_git(int extra)
 {
-       struct packed_git *p = xmalloc(sizeof(*p) + extra);
+       struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
        memset(p, 0, sizeof(*p));
        p->pack_fd = -1;
        return p;
@@ -1168,7 +1168,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
         * ".pack" is long enough to hold any suffix we're adding (and
         * the use xsnprintf double-checks that)
         */
-       alloc = path_len + strlen(".pack") + 1;
+       alloc = st_add3(path_len, strlen(".pack"), 1);
        p = alloc_packed_git(alloc);
        memcpy(p->pack_name, path, path_len);
 
@@ -1196,7 +1196,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
 struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
 {
        const char *path = sha1_pack_name(sha1);
-       int alloc = strlen(path) + 1;
+       size_t alloc = st_add(strlen(path), 1);
        struct packed_git *p = alloc_packed_git(alloc);
 
        memcpy(p->pack_name, path, alloc); /* includes NUL */
@@ -1413,10 +1413,12 @@ static void mark_bad_packed_object(struct packed_git *p,
 {
        unsigned i;
        for (i = 0; i < p->num_bad_objects; i++)
-               if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
+               if (!hashcmp(sha1, p->bad_object_sha1 + GIT_SHA1_RAWSZ * i))
                        return;
-       p->bad_object_sha1 = xrealloc(p->bad_object_sha1, 20 * (p->num_bad_objects + 1));
-       hashcpy(p->bad_object_sha1 + 20 * p->num_bad_objects, sha1);
+       p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
+                                     st_mult(GIT_SHA1_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++;
 }
 
@@ -1942,7 +1944,7 @@ static enum object_type packed_to_object_type(struct packed_git *p,
                /* 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);
-                       poi_stack = xmalloc(sizeof(off_t)*poi_stack_alloc);
+                       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);
@@ -2308,7 +2310,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
                if (delta_stack_nr >= delta_stack_alloc
                    && delta_stack == small_delta_stack) {
                        delta_stack_alloc = alloc_nr(delta_stack_nr);
-                       delta_stack = xmalloc(sizeof(*delta_stack)*delta_stack_alloc);
+                       ALLOC_ARRAY(delta_stack, delta_stack_alloc);
                        memcpy(delta_stack, small_delta_stack,
                               sizeof(*delta_stack)*delta_stack_nr);
                } else {
index 89918ca158379a02368b0604b14465fa14855c8e..3acf221f92f7a0857f17c0210044cacc325c8ce0 100644 (file)
@@ -87,9 +87,8 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa
                 * object databases including our own.
                 */
                const char *objdir = get_object_directory();
-               int objdir_len = strlen(objdir);
-               int entlen = objdir_len + 43;
-               fakeent = xmalloc(sizeof(*fakeent) + entlen);
+               size_t objdir_len = strlen(objdir);
+               fakeent = xmalloc(st_add3(sizeof(*fakeent), objdir_len, 43));
                memcpy(fakeent->base, objdir, objdir_len);
                fakeent->name = fakeent->base + objdir_len + 1;
                fakeent->name[-1] = '/';
@@ -892,12 +891,12 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
                        prefix++;
                        negative = 1;
                } else if (prefix[0] != '!') {
-                       die ("Invalid search pattern: %s", prefix);
+                       return -1;
                }
        }
 
        if (regcomp(&regex, prefix, REG_EXTENDED))
-               die("Invalid search pattern: %s", prefix);
+               return -1;
 
        for (l = list; l; l = l->next) {
                l->item->object.flags |= ONELINE_SEEN;
index 60f1505d9791bc92a283d02183da35e7542ad2dc..4d554caf8d751d7a7c8a3a4286a208e0959ab513 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -315,8 +315,8 @@ void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
        info->shallow = sa;
        if (!sa)
                return;
-       info->ours = xmalloc(sizeof(*info->ours) * sa->nr);
-       info->theirs = xmalloc(sizeof(*info->theirs) * sa->nr);
+       ALLOC_ARRAY(info->ours, sa->nr);
+       ALLOC_ARRAY(info->theirs, sa->nr);
        for (i = 0; i < sa->nr; i++) {
                if (has_sha1_file(sa->sha1[i])) {
                        struct commit_graft *graft;
@@ -389,7 +389,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
        unsigned int i, nr;
        struct commit_list *head = NULL;
        int bitmap_nr = (info->nr_bits + 31) / 32;
-       int bitmap_size = bitmap_nr * sizeof(uint32_t);
+       size_t bitmap_size = st_mult(bitmap_nr, sizeof(uint32_t));
        uint32_t *tmp = xmalloc(bitmap_size); /* to be freed before return */
        uint32_t *bitmap = paint_alloc(info);
        struct commit *c = lookup_commit_reference_gently(sha1, 1);
@@ -487,7 +487,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
        struct paint_info pi;
 
        trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
-       shallow = xmalloc(sizeof(*shallow) * (info->nr_ours + info->nr_theirs));
+       ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
        for (i = 0; i < info->nr_ours; i++)
                shallow[nr_shallow++] = info->ours[i];
        for (i = 0; i < info->nr_theirs; i++)
index d9e4903fed24f5916962f8d315eb680f9b4013ff..acf8d5445ad96aacf6d2c3ccc2d77a3815291228 100644 (file)
@@ -50,7 +50,8 @@ int main(int argc, char **argv)
                        unsigned char sha1[20];
                        uint32_t crc;
                        uint32_t off;
-               } *entries = xmalloc(nr * sizeof(entries[0]));
+               } *entries;
+               ALLOC_ARRAY(entries, nr);
                for (i = 0; i < nr; i++)
                        if (fread(entries[i].sha1, 20, 1, stdin) != 1)
                                die("unable to read sha1 %u/%u", i, nr);
index bab316dda8f4946efb9a908f17c98b4db3d21e98..f60e2ee72ba86cbd6c66622366af4a7faf25e1a0 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -718,7 +718,7 @@ char *xstrdup_tolower(const char *string)
        size_t len, i;
 
        len = strlen(string);
-       result = xmalloc(len + 1);
+       result = xmallocz(len);
        for (i = 0; i < len; i++)
                result[i] = tolower(string[i]);
        result[i] = '\0';
index fe8ceabf3009db672d4d2dc5121123961c298212..92502b594d055bbf99fd203175d7dffaf8dfb732 100644 (file)
@@ -427,8 +427,8 @@ static const struct submodule *config_from(struct submodule_cache *cache,
        parameter.commit_sha1 = commit_sha1;
        parameter.gitmodules_sha1 = sha1;
        parameter.overwrite = 0;
-       git_config_from_buf(parse_config, rev.buf, config, config_size,
-                       &parameter);
+       git_config_from_mem(parse_config, "submodule-blob", rev.buf,
+                       config, config_size, &parameter);
        free(config);
 
        switch (lookup_type) {
index b83939c294782ac59e5c78c8882064a1e272e309..24fb81ac62e2161e79cbd5ab1f2a241663c2a3a1 100644 (file)
@@ -69,7 +69,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(".gitmodules", entry.buf, newpath) < 0) {
+       if (git_config_set_in_file_gently(".gitmodules", 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);
@@ -123,7 +123,7 @@ static int add_submodule_odb(const char *path)
        struct strbuf objects_directory = STRBUF_INIT;
        struct alternate_object_database *alt_odb;
        int ret = 0;
-       int alloc;
+       size_t alloc;
 
        strbuf_git_path_submodule(&objects_directory, path, "objects/");
        if (!is_directory(objects_directory.buf)) {
@@ -138,8 +138,8 @@ static int add_submodule_odb(const char *path)
                                        objects_directory.len))
                        goto done;
 
-       alloc = objects_directory.len + 42; /* for "12/345..." sha1 */
-       alt_odb = xmalloc(sizeof(*alt_odb) + alloc);
+       alloc = st_add(objects_directory.len, 42); /* for "12/345..." sha1 */
+       alt_odb = xmalloc(st_add(sizeof(*alt_odb), alloc));
        alt_odb->next = alt_odb_list;
        xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf);
        alt_odb->name = alt_odb->base + objects_directory.len;
@@ -1087,11 +1087,9 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
        /* Update core.worktree setting */
        strbuf_reset(&file_name);
        strbuf_addf(&file_name, "%s/config", git_dir);
-       if (git_config_set_in_file(file_name.buf, "core.worktree",
-                                  relative_path(real_work_tree, git_dir,
-                                                &rel_path)))
-               die(_("Could not set core.worktree in %s"),
-                   file_name.buf);
+       git_config_set_in_file(file_name.buf, "core.worktree",
+                              relative_path(real_work_tree, git_dir,
+                                            &rel_path));
 
        strbuf_release(&file_name);
        strbuf_release(&rel_path);
index 504e5a02a1d930238023bc2fc7a582183f237674..f33962b178238126d4cbe88cfbda7dd7bb747490 100755 (executable)
@@ -21,38 +21,32 @@ compare_ws_file () {
        pfx=$1
        exp=$2.expect
        act=$pfx.actual.$3
-       tr '\015\000' QN <"$2" >"$exp" &&
-       tr '\015\000' QN <"$3" >"$act" &&
-       test_cmp $exp $act &&
-       rm $exp $act
+       tr '\015\000abcdef0123456789' QN00000000000000000 <"$2" >"$exp" &&
+       tr '\015\000abcdef0123456789' QN00000000000000000 <"$3" >"$act" &&
+       test_cmp "$exp" "$act" &&
+       rm "$exp" "$act"
 }
 
 create_gitattributes () {
-       attr=$1
-       case "$attr" in
-               auto)
-               echo "*.txt text=auto" >.gitattributes
-               ;;
-               text)
-               echo "*.txt text" >.gitattributes
-               ;;
-               -text)
-               echo "*.txt -text" >.gitattributes
-               ;;
-               crlf)
-               echo "*.txt eol=crlf" >.gitattributes
-               ;;
-               lf)
-               echo "*.txt eol=lf" >.gitattributes
-               ;;
-               "")
-               echo >.gitattributes
-               ;;
-               *)
-               echo >&2 invalid attribute: $attr
-               exit 1
-               ;;
-       esac
+       {
+               while test "$#" != 0
+               do
+                       case "$1" in
+                       auto)    echo '*.txt text=auto' ;;
+                       ident) echo '*.txt ident' ;;
+                       text)    echo '*.txt text' ;;
+                       -text) echo '*.txt -text' ;;
+                       crlf)  echo '*.txt eol=crlf' ;;
+                       lf)    echo '*.txt eol=lf' ;;
+                       "") ;;
+                       *)
+                               echo >&2 invalid attribute: "$1"
+                               exit 1
+                               ;;
+                       esac &&
+                       shift
+               done
+       } >.gitattributes
 }
 
 create_NNO_files () {
@@ -165,6 +159,25 @@ stats_ascii () {
 
 }
 
+
+# contruct the attr/ returned by git ls-files --eol
+# Take none (=empty), one or two args
+attr_ascii () {
+       case $1,$2 in
+       -text,*)   echo "-text" ;;
+       text,)     echo "text" ;;
+       text,lf)   echo "text eol=lf" ;;
+       text,crlf) echo "text eol=crlf" ;;
+       auto,)     echo "text=auto" ;;
+       auto,lf)   echo "text=auto eol=lf" ;;
+       auto,crlf) echo "text=auto eol=crlf" ;;
+       lf,)       echo "text eol=lf" ;;
+       crlf,)     echo "text eol=crlf" ;;
+       ,) echo "" ;;
+       *) echo invalid_attr "$1,$2" ;;
+       esac
+}
+
 check_files_in_repo () {
        crlf=$1
        attr=$2
@@ -208,55 +221,57 @@ check_in_repo_NNO () {
 }
 
 checkout_files () {
-       eol=$1
-       crlf=$2
-       attr=$3
-       lfname=$4
-       crlfname=$5
-       lfmixcrlf=$6
-       lfmixcr=$7
-       crlfnul=$8
-       create_gitattributes $attr &&
+       attr=$1 ; shift
+       ident=$1; shift
+       aeol=$1 ; shift
+       crlf=$1 ; shift
+       ceol=$1 ; shift
+       lfname=$1 ; shift
+       crlfname=$1 ; shift
+       lfmixcrlf=$1 ; shift
+       lfmixcr=$1 ; shift
+       crlfnul=$1 ; shift
+       create_gitattributes "$attr" "$ident" &&
        git config core.autocrlf $crlf &&
-       pfx=eol_${eol}_crlf_${crlf}_attr_${attr}_ &&
+       pfx=eol_${ceol}_crlf_${crlf}_attr_${attr}_ &&
        for f in LF CRLF LF_mix_CR CRLF_mix_LF LF_nul
        do
                rm crlf_false_attr__$f.txt &&
-               if test -z "$eol"; then
+               if test -z "$ceol"; then
                        git checkout crlf_false_attr__$f.txt
                else
-                       git -c core.eol=$eol checkout crlf_false_attr__$f.txt
+                       git -c core.eol=$ceol checkout crlf_false_attr__$f.txt
                fi
        done
 
-       test_expect_success "ls-files --eol $lfname ${pfx}LF.txt" '
+       test_expect_success "ls-files --eol attr=$attr $ident $aeol core.autocrlf=$crlf core.eol=$ceol" '
                test_when_finished "rm expect actual" &&
                sort <<-EOF >expect &&
-               i/crlf w/$(stats_ascii $crlfname) crlf_false_attr__CRLF.txt
-               i/mixed w/$(stats_ascii $lfmixcrlf) crlf_false_attr__CRLF_mix_LF.txt
-               i/lf w/$(stats_ascii $lfname) crlf_false_attr__LF.txt
-               i/-text w/$(stats_ascii $lfmixcr) crlf_false_attr__LF_mix_CR.txt
-               i/-text w/$(stats_ascii $crlfnul) crlf_false_attr__CRLF_nul.txt
-               i/-text w/$(stats_ascii $crlfnul) crlf_false_attr__LF_nul.txt
+               i/crlf w/$(stats_ascii $crlfname) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF.txt
+               i/mixed w/$(stats_ascii $lfmixcrlf) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF_mix_LF.txt
+               i/lf w/$(stats_ascii $lfname) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF.txt
+               i/-text w/$(stats_ascii $lfmixcr) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF_mix_CR.txt
+               i/-text w/$(stats_ascii $crlfnul) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF_nul.txt
+               i/-text w/$(stats_ascii $crlfnul) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF_nul.txt
                EOF
                git ls-files --eol crlf_false_attr__* |
-               sed -e "s!attr/[^       ]*!!g" -e "s/   / /g" -e "s/  */ /g" |
+               sed -e "s/      / /g" -e "s/  */ /g" |
                sort >actual &&
                test_cmp expect actual
        '
-       test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF" "
+       test_expect_success "checkout $ident $attr $aeol core.autocrlf=$crlf core.eol=$ceol file=LF" "
                compare_ws_file $pfx $lfname    crlf_false_attr__LF.txt
        "
-       test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF" "
+       test_expect_success "checkout $ident $attr $aeol core.autocrlf=$crlf core.eol=$ceol file=CRLF" "
                compare_ws_file $pfx $crlfname  crlf_false_attr__CRLF.txt
        "
-       test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=CRLF_mix_LF" "
+       test_expect_success "checkout $ident $attr $aeol core.autocrlf=$crlf core.eol=$ceol file=CRLF_mix_LF" "
                compare_ws_file $pfx $lfmixcrlf crlf_false_attr__CRLF_mix_LF.txt
        "
-       test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF_mix_CR" "
+       test_expect_success "checkout $ident $attr $aeol core.autocrlf=$crlf core.eol=$ceol file=LF_mix_CR" "
                compare_ws_file $pfx $lfmixcr   crlf_false_attr__LF_mix_CR.txt
        "
-       test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF_nul" "
+       test_expect_success "checkout $ident $attr $aeol core.autocrlf=$crlf core.eol=$ceol file=LF_nul" "
                compare_ws_file $pfx $crlfnul   crlf_false_attr__LF_nul.txt
        "
 }
@@ -301,14 +316,13 @@ test_expect_success 'setup master' '
        git checkout -b master &&
        git add .gitattributes &&
        git commit -m "add .gitattributes" "" &&
-       printf "line1\nline2\nline3"     >LF &&
-       printf "line1\r\nline2\r\nline3" >CRLF &&
-       printf "line1\r\nline2\nline3"   >repoMIX &&
-       printf "line1\r\nline2\nline3"   >CRLF_mix_LF &&
-       printf "line1\nline2\rline3"     >LF_mix_CR &&
-       printf "line1\r\nline2\rline3"   >CRLF_mix_CR &&
-       printf "line1Q\r\nline2\r\nline3" | q_to_nul >CRLF_nul &&
-       printf "line1Q\nline2\nline3" | q_to_nul >LF_nul &&
+       printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\nLINETWO\nLINETHREE"     >LF &&
+       printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\r\nLINETHREE" >CRLF &&
+       printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\r\nLINETWO\nLINETHREE"   >CRLF_mix_LF &&
+       printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\nLINETWO\rLINETHREE"     >LF_mix_CR &&
+       printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\rLINETHREE"   >CRLF_mix_CR &&
+       printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONEQ\r\nLINETWO\r\nLINETHREE" | q_to_nul >CRLF_nul &&
+       printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONEQ\nLINETWO\nLINETHREE" | q_to_nul >LF_nul &&
        create_NNO_files CRLF_mix_LF CRLF_mix_LF CRLF_mix_LF CRLF_mix_LF CRLF_mix_LF &&
        git -c core.autocrlf=false add NNO_*.txt &&
        git commit -m "mixed line endings" &&
@@ -449,23 +463,18 @@ check_in_repo_NNO input "-text" LF        CRLF      CRLF_mix_LF  LF_mix_CR        CRLF
 # How to read the table below:
 # - checkout_files will check multiple files with a combination of settings
 #   and attributes (core.autocrlf=input is forbidden with core.eol=crlf)
-# - parameter $1 : core.eol               lf | crlf
-# - parameter $2 : core.autocrlf          false | true | input
-# - parameter $3 : text in .gitattributs  "" (empty) | auto | text | -text
-# - parameter $4 : reference for a file with only LF in the repo
-# - parameter $5 : reference for a file with only CRLF in the repo
-# - parameter $6 : reference for a file with mixed LF and CRLF in the repo
-# - parameter $7 : reference for a file with LF and CR in the repo (does somebody uses this ?)
-# - parameter $8 : reference for a file with CRLF and a NUL (should be handled as binary when auto)
-
-#                                            What we have in the repo:
-#                                            ----------------- EOL in repo ----------------
-#                                            LF    CRLF  CRLF_mix_LF  LF_mix_CR    CRLF_nul
-#                   settings with checkout:
-#                   core.   core.   .gitattr
-#                    eol     acrlf
-#                                            ----------------------------------------------
-#                                            What we want to have in the working tree:
+#
+# - parameter $1       : text in .gitattributs  "" (empty) | auto | text | -text
+# - parameter $2       : ident                  "" | i (i == ident)
+# - parameter $3       : eol in .gitattributs   "" (empty) | lf | crlf
+# - parameter $4       : core.autocrlf          false | true | input
+# - parameter $5       : core.eol               "" | lf | crlf | "native"
+# - parameter $6       : reference for a file with only LF in the repo
+# - parameter $7       : reference for a file with only CRLF in the repo
+# - parameter $8       : reference for a file with mixed LF and CRLF in the repo
+# - parameter $9       : reference for a file with LF and CR in the repo
+# - parameter $10 : reference for a file with CRLF and a NUL (should be handled as binary when auto)
+
 if test_have_prereq NATIVE_CRLF
 then
 MIX_CRLF_LF=CRLF
@@ -480,69 +489,90 @@ LFNUL=LF_nul
 fi
 export CRLF_MIX_LF_CR MIX NL
 
-checkout_files    lf      false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      false "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
-checkout_files    lf      input "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      false "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    lf      input "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      input "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      input "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    lf      false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    lf      true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    lf      input "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-
-checkout_files    crlf    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    crlf    true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    crlf    false "auto"    CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
-checkout_files    crlf    true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
-checkout_files    crlf    false "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    crlf    true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    crlf    false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    crlf    true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    crlf    false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    crlf    true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    crlf    false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    crlf    true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-
-checkout_files    ""      false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      false "auto"    $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    LF_nul
-checkout_files    ""      true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
-checkout_files    ""      input "auto"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      false "text"    $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   $LFNUL
-checkout_files    ""      true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    ""      input "text"    LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      input "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      input "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    ""      false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    ""      true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    ""      input "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-
-checkout_files    native  false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    native  true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    native  false "auto"    $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    LF_nul
-checkout_files    native  true  "auto"    CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
-checkout_files    native  false "text"    $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   $LFNUL
-checkout_files    native  true  "text"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    native  false "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    native  true  "-text"   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    native  false "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    native  true  "lf"      LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
-checkout_files    native  false "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
-checkout_files    native  true  "crlf"    CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+checkout_files ""      ""       ""    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    false  crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    false  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    false  native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    input  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    true   ""       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    true   crlf     CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    true   lf       CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ""       ""    true   native   CRLF  CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    false  crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    false  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    false  native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    input  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    true   ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    true   crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    true   lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files ""      ident ""    true   native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    false  ""       $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    false  crlf     CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    false  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    false  native   $NL   CRLF  $MIX_CRLF_LF LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    input  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    true   ""       CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    true   crlf     CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    true   lf       CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
+checkout_files "auto"  ""       ""    true   native   CRLF  CRLF  CRLF         LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    false  crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    false  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    false  native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    input  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    true   ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    true   crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    true   lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+checkout_files "auto"  ident ""    true   native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+
+for id in "" ident;
+do
+       checkout_files "crlf"  "$id" ""    false  ""       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    false  crlf     CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    false  lf       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    false  native   CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    input  ""       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    input  lf       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    true   ""       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    true   crlf     CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    true   lf       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "crlf"  "$id" ""    true   native   CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "lf"    "$id" ""    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    false  crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    false  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    false  native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    input  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    true   ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    true   crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    true   lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "lf"    "$id" ""    true   native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "text"  "$id" ""    false  ""       $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   $LFNUL
+       checkout_files "text"  "$id" ""    false  crlf     CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "text"  "$id" ""    false  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "text"  "$id" ""    false  native   $NL   CRLF  $MIX_CRLF_LF $MIX_LF_CR   $LFNUL
+       checkout_files "text"  "$id" ""    input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "text"  "$id" ""    input  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "text"  "$id" ""    true   ""       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "text"  "$id" ""    true   crlf     CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "text"  "$id" ""    true   lf       CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "text"  "$id" ""    true   native   CRLF  CRLF  CRLF         CRLF_mix_CR  CRLF_nul
+       checkout_files "-text" "$id" ""    false  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    false  crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    false  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    false  native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    input  ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    input  lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    true   ""       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    true   crlf     LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    true   lf       LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+       checkout_files "-text" "$id" ""    true   native   LF    CRLF  CRLF_mix_LF  LF_mix_CR    LF_nul
+done
 
 # Should be the last test case: remove some files from the worktree
 test_expect_success 'ls-files --eol -d -z' '
index 52678e7d0ac754bb678eb7c7a9203bc253085366..8867ce10f8c40cd72c9395c2b36b8b40b5e91fcf 100755 (executable)
@@ -700,12 +700,18 @@ test_expect_success 'invalid unit' '
        git config aninvalid.unit >actual &&
        test_cmp expect actual &&
        cat >expect <<-\EOF &&
-       fatal: bad numeric config value '\''1auto'\'' for '\''aninvalid.unit'\'' in .git/config: invalid unit
+       fatal: bad numeric config value '\''1auto'\'' for '\''aninvalid.unit'\'' in file .git/config: invalid unit
        EOF
        test_must_fail git config --int --get aninvalid.unit 2>actual &&
        test_i18ncmp expect actual
 '
 
+test_expect_success 'invalid stdin config' '
+       echo "fatal: bad config line 1 in standard input " >expect &&
+       echo "[broken" | test_must_fail git config --list --file - >output 2>&1 &&
+       test_cmp expect output
+'
+
 cat > expect << EOF
 true
 false
@@ -957,13 +963,15 @@ Qsection.sub=section.val4
 Qsection.sub=section.val5Q
 EOF
 test_expect_success '--null --list' '
-       git config --null --list | nul_to_q >result &&
+       git config --null --list >result.raw &&
+       nul_to_q <result.raw >result &&
        echo >>result &&
        test_cmp expect result
 '
 
 test_expect_success '--null --get-regexp' '
-       git config --null --get-regexp "val[0-9]" | nul_to_q >result &&
+       git config --null --get-regexp "val[0-9]" >result.raw &&
+       nul_to_q <result.raw >result &&
        echo >>result &&
        test_cmp expect result
 '
@@ -1201,4 +1209,151 @@ test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
          "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
 '
 
+test_expect_success 'set up --show-origin tests' '
+       INCLUDE_DIR="$HOME/include" &&
+       mkdir -p "$INCLUDE_DIR" &&
+       cat >"$INCLUDE_DIR"/absolute.include <<-\EOF &&
+               [user]
+                       absolute = include
+       EOF
+       cat >"$INCLUDE_DIR"/relative.include <<-\EOF &&
+               [user]
+                       relative = include
+       EOF
+       cat >"$HOME"/.gitconfig <<-EOF &&
+               [user]
+                       global = true
+                       override = global
+               [include]
+                       path = "$INCLUDE_DIR/absolute.include"
+       EOF
+       cat >.git/config <<-\EOF
+               [user]
+                       local = true
+                       override = local
+               [include]
+                       path = ../include/relative.include
+       EOF
+'
+
+test_expect_success '--show-origin with --list' '
+       cat >expect <<-EOF &&
+               file:$HOME/.gitconfig   user.global=true
+               file:$HOME/.gitconfig   user.override=global
+               file:$HOME/.gitconfig   include.path=$INCLUDE_DIR/absolute.include
+               file:$INCLUDE_DIR/absolute.include      user.absolute=include
+               file:.git/config        user.local=true
+               file:.git/config        user.override=local
+               file:.git/config        include.path=../include/relative.include
+               file:.git/../include/relative.include   user.relative=include
+               command line:   user.cmdline=true
+       EOF
+       git -c user.cmdline=true config --list --show-origin >output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin with --list --null' '
+       cat >expect <<-EOF &&
+               file:$HOME/.gitconfigQuser.global
+               trueQfile:$HOME/.gitconfigQuser.override
+               globalQfile:$HOME/.gitconfigQinclude.path
+               $INCLUDE_DIR/absolute.includeQfile:$INCLUDE_DIR/absolute.includeQuser.absolute
+               includeQfile:.git/configQuser.local
+               trueQfile:.git/configQuser.override
+               localQfile:.git/configQinclude.path
+               ../include/relative.includeQfile:.git/../include/relative.includeQuser.relative
+               includeQcommand line:Quser.cmdline
+               trueQ
+       EOF
+       git -c user.cmdline=true config --null --list --show-origin >output.raw &&
+       nul_to_q <output.raw >output &&
+       # The here-doc above adds a newline that the --null output would not
+       # include. Add it here to make the two comparable.
+       echo >>output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin with single file' '
+       cat >expect <<-\EOF &&
+               file:.git/config        user.local=true
+               file:.git/config        user.override=local
+               file:.git/config        include.path=../include/relative.include
+       EOF
+       git config --local --list --show-origin >output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin with --get-regexp' '
+       cat >expect <<-EOF &&
+               file:$HOME/.gitconfig   user.global true
+               file:.git/config        user.local true
+       EOF
+       git config --show-origin --get-regexp "user\.[g|l].*" >output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin getting a single key' '
+       cat >expect <<-\EOF &&
+               file:.git/config        local
+       EOF
+       git config --show-origin user.override >output &&
+       test_cmp expect output
+'
+
+test_expect_success 'set up custom config file' '
+       CUSTOM_CONFIG_FILE="file\" (dq) and spaces.conf" &&
+       cat >"$CUSTOM_CONFIG_FILE" <<-\EOF
+               [user]
+                       custom = true
+       EOF
+'
+
+test_expect_success '--show-origin escape special file name characters' '
+       cat >expect <<-\EOF &&
+               file:"file\" (dq) and spaces.conf"      user.custom=true
+       EOF
+       git config --file "$CUSTOM_CONFIG_FILE" --show-origin --list >output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin stdin' '
+       cat >expect <<-\EOF &&
+               standard input: user.custom=true
+       EOF
+       git config --file - --show-origin --list <"$CUSTOM_CONFIG_FILE" >output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin stdin with file include' '
+       cat >"$INCLUDE_DIR"/stdin.include <<-EOF &&
+               [user]
+                       stdin = include
+       EOF
+       cat >expect <<-EOF &&
+               file:$INCLUDE_DIR/stdin.include include
+       EOF
+       echo "[include]path=\"$INCLUDE_DIR\"/stdin.include" \
+               | git config --show-origin --includes --file - user.stdin >output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin blob' '
+       cat >expect <<-\EOF &&
+               blob:a9d9f9e555b5c6f07cbe09d3f06fe3df11e09c08   user.custom=true
+       EOF
+       blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
+       git config --blob=$blob --show-origin --list >output &&
+       test_cmp expect output
+'
+
+test_expect_success '--show-origin blob ref' '
+       cat >expect <<-\EOF &&
+               blob:"master:file\" (dq) and spaces.conf"       user.custom=true
+       EOF
+       git add "$CUSTOM_CONFIG_FILE" &&
+       git commit -m "new config file" &&
+       git config --blob=master:"$CUSTOM_CONFIG_FILE" --show-origin --list >output &&
+       test_cmp expect output
+'
+
 test_done
index 91235b76ba76f7e7e9338e37da7106eb550969e6..82f82a16d961486b7a8f3d68229436393baa64e3 100755 (executable)
@@ -195,14 +195,14 @@ test_expect_success 'proper error on error in default config files' '
        cp .git/config .git/config.old &&
        test_when_finished "mv .git/config.old .git/config" &&
        echo "[" >>.git/config &&
-       echo "fatal: bad config file line 34 in .git/config" >expect &&
+       echo "fatal: bad config line 34 in file .git/config" >expect &&
        test_expect_code 128 test-config get_value foo.bar 2>actual &&
        test_cmp expect actual
 '
 
 test_expect_success 'proper error on error in custom config files' '
        echo "[" >>syntax-error &&
-       echo "fatal: bad config file line 1 in syntax-error" >expect &&
+       echo "fatal: bad config line 1 in file syntax-error" >expect &&
        test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
        test_cmp expect actual
 '
diff --git a/t/t1501-work-tree.sh b/t/t1501-work-tree.sh
new file mode 100755 (executable)
index 0000000..cc5b870
--- /dev/null
@@ -0,0 +1,426 @@
+#!/bin/sh
+
+test_description='test separate work tree'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+       EMPTY_TREE=$(git write-tree) &&
+       EMPTY_BLOB=$(git hash-object -t blob --stdin </dev/null) &&
+       CHANGED_BLOB=$(echo changed | git hash-object -t blob --stdin) &&
+       EMPTY_BLOB7=$(echo $EMPTY_BLOB | sed "s/\(.......\).*/\1/") &&
+       CHANGED_BLOB7=$(echo $CHANGED_BLOB | sed "s/\(.......\).*/\1/") &&
+
+       mkdir -p work/sub/dir &&
+       mkdir -p work2 &&
+       mv .git repo.git
+'
+
+test_expect_success 'setup: helper for testing rev-parse' '
+       test_rev_parse() {
+               echo $1 >expected.bare &&
+               echo $2 >expected.inside-git &&
+               echo $3 >expected.inside-worktree &&
+               if test $# -ge 4
+               then
+                       echo $4 >expected.prefix
+               fi &&
+
+               git rev-parse --is-bare-repository >actual.bare &&
+               git rev-parse --is-inside-git-dir >actual.inside-git &&
+               git rev-parse --is-inside-work-tree >actual.inside-worktree &&
+               if test $# -ge 4
+               then
+                       git rev-parse --show-prefix >actual.prefix
+               fi &&
+
+               test_cmp expected.bare actual.bare &&
+               test_cmp expected.inside-git actual.inside-git &&
+               test_cmp expected.inside-worktree actual.inside-worktree &&
+               if test $# -ge 4
+               then
+                       # rev-parse --show-prefix should output
+                       # a single newline when at the top of the work tree,
+                       # but we test for that separately.
+                       test -z "$4" && ! test -s actual.prefix ||
+                       test_cmp expected.prefix actual.prefix
+               fi
+       }
+'
+
+test_expect_success 'setup: core.worktree = relative path' '
+       sane_unset GIT_WORK_TREE &&
+       GIT_DIR=repo.git &&
+       GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+       export GIT_DIR GIT_CONFIG &&
+       git config core.worktree ../work
+'
+
+test_expect_success 'outside' '
+       test_rev_parse false false false
+'
+
+test_expect_success 'inside work tree' '
+       (
+               cd work &&
+               GIT_DIR=../repo.git &&
+               GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+               test_rev_parse false false true ""
+       )
+'
+
+test_expect_success 'empty prefix is actually written out' '
+       echo >expected &&
+       (
+               cd work &&
+               GIT_DIR=../repo.git &&
+               GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+               git rev-parse --show-prefix >../actual
+       ) &&
+       test_cmp expected actual
+'
+
+test_expect_success 'subdir of work tree' '
+       (
+               cd work/sub/dir &&
+               GIT_DIR=../../../repo.git &&
+               GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+               test_rev_parse false false true sub/dir/
+       )
+'
+
+test_expect_success 'setup: core.worktree = absolute path' '
+       sane_unset GIT_WORK_TREE &&
+       GIT_DIR=$(pwd)/repo.git &&
+       GIT_CONFIG=$GIT_DIR/config &&
+       export GIT_DIR GIT_CONFIG &&
+       git config core.worktree "$(pwd)/work"
+'
+
+test_expect_success 'outside' '
+       test_rev_parse false false false &&
+       (
+               cd work2 &&
+               test_rev_parse false false false
+       )
+'
+
+test_expect_success 'inside work tree' '
+       (
+               cd work &&
+               test_rev_parse false false true ""
+       )
+'
+
+test_expect_success 'subdir of work tree' '
+       (
+               cd work/sub/dir &&
+               test_rev_parse false false true sub/dir/
+       )
+'
+
+test_expect_success 'setup: GIT_WORK_TREE=relative (override core.worktree)' '
+       GIT_DIR=$(pwd)/repo.git &&
+       GIT_CONFIG=$GIT_DIR/config &&
+       git config core.worktree non-existent &&
+       GIT_WORK_TREE=work &&
+       export GIT_DIR GIT_CONFIG GIT_WORK_TREE
+'
+
+test_expect_success 'outside' '
+       test_rev_parse false false false &&
+       (
+               cd work2 &&
+               test_rev_parse false false false
+       )
+'
+
+test_expect_success 'inside work tree' '
+       (
+               cd work &&
+               GIT_WORK_TREE=. &&
+               test_rev_parse false false true ""
+       )
+'
+
+test_expect_success 'subdir of work tree' '
+       (
+               cd work/sub/dir &&
+               GIT_WORK_TREE=../.. &&
+               test_rev_parse false false true sub/dir/
+       )
+'
+
+test_expect_success 'setup: GIT_WORK_TREE=absolute, below git dir' '
+       mv work repo.git/work &&
+       mv work2 repo.git/work2 &&
+       GIT_DIR=$(pwd)/repo.git &&
+       GIT_CONFIG=$GIT_DIR/config &&
+       GIT_WORK_TREE=$(pwd)/repo.git/work &&
+       export GIT_DIR GIT_CONFIG GIT_WORK_TREE
+'
+
+test_expect_success 'outside' '
+       echo outside &&
+       test_rev_parse false false false
+'
+
+test_expect_success 'in repo.git' '
+       (
+               cd repo.git &&
+               test_rev_parse false true false
+       ) &&
+       (
+               cd repo.git/objects &&
+               test_rev_parse false true false
+       ) &&
+       (
+               cd repo.git/work2 &&
+               test_rev_parse false true false
+       )
+'
+
+test_expect_success 'inside work tree' '
+       (
+               cd repo.git/work &&
+               test_rev_parse false true true ""
+       )
+'
+
+test_expect_success 'subdir of work tree' '
+       (
+               cd repo.git/work/sub/dir &&
+               test_rev_parse false true true sub/dir/
+       )
+'
+
+test_expect_success 'find work tree from repo' '
+       echo sub/dir/untracked >expected &&
+       cat <<-\EOF >repo.git/work/.gitignore &&
+       expected.*
+       actual.*
+       .gitignore
+       EOF
+       >repo.git/work/sub/dir/untracked &&
+       (
+               cd repo.git &&
+               git ls-files --others --exclude-standard >../actual
+       ) &&
+       test_cmp expected actual
+'
+
+test_expect_success 'find work tree from work tree' '
+       echo sub/dir/tracked >expected &&
+       >repo.git/work/sub/dir/tracked &&
+       (
+               cd repo.git/work/sub/dir &&
+               git --git-dir=../../.. add tracked
+       ) &&
+       (
+               cd repo.git &&
+               git ls-files >../actual
+       ) &&
+       test_cmp expected actual
+'
+
+test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
+       (
+               cd repo.git/work/sub/dir &&
+               GIT_DIR=../../.. &&
+               GIT_WORK_TREE=../.. &&
+               GIT_PAGER= &&
+               export GIT_DIR GIT_WORK_TREE GIT_PAGER &&
+
+               git diff --exit-code tracked &&
+               echo changed >tracked &&
+               test_must_fail git diff --exit-code tracked
+       )
+'
+
+test_expect_success 'diff-index respects work tree under .git dir' '
+       cat >diff-index-cached.expected <<-EOF &&
+       :000000 100644 $_z40 $EMPTY_BLOB A      sub/dir/tracked
+       EOF
+       cat >diff-index.expected <<-EOF &&
+       :000000 100644 $_z40 $_z40 A    sub/dir/tracked
+       EOF
+
+       (
+               GIT_DIR=repo.git &&
+               GIT_WORK_TREE=repo.git/work &&
+               export GIT_DIR GIT_WORK_TREE &&
+               git diff-index $EMPTY_TREE >diff-index.actual &&
+               git diff-index --cached $EMPTY_TREE >diff-index-cached.actual
+       ) &&
+       test_cmp diff-index.expected diff-index.actual &&
+       test_cmp diff-index-cached.expected diff-index-cached.actual
+'
+
+test_expect_success 'diff-files respects work tree under .git dir' '
+       cat >diff-files.expected <<-EOF &&
+       :100644 100644 $EMPTY_BLOB $_z40 M      sub/dir/tracked
+       EOF
+
+       (
+               GIT_DIR=repo.git &&
+               GIT_WORK_TREE=repo.git/work &&
+               export GIT_DIR GIT_WORK_TREE &&
+               git diff-files >diff-files.actual
+       ) &&
+       test_cmp diff-files.expected diff-files.actual
+'
+
+test_expect_success 'git diff respects work tree under .git dir' '
+       cat >diff-TREE.expected <<-EOF &&
+       diff --git a/sub/dir/tracked b/sub/dir/tracked
+       new file mode 100644
+       index 0000000..$CHANGED_BLOB7
+       --- /dev/null
+       +++ b/sub/dir/tracked
+       @@ -0,0 +1 @@
+       +changed
+       EOF
+       cat >diff-TREE-cached.expected <<-EOF &&
+       diff --git a/sub/dir/tracked b/sub/dir/tracked
+       new file mode 100644
+       index 0000000..$EMPTY_BLOB7
+       EOF
+       cat >diff-FILES.expected <<-EOF &&
+       diff --git a/sub/dir/tracked b/sub/dir/tracked
+       index $EMPTY_BLOB7..$CHANGED_BLOB7 100644
+       --- a/sub/dir/tracked
+       +++ b/sub/dir/tracked
+       @@ -0,0 +1 @@
+       +changed
+       EOF
+
+       (
+               GIT_DIR=repo.git &&
+               GIT_WORK_TREE=repo.git/work &&
+               export GIT_DIR GIT_WORK_TREE &&
+               git diff $EMPTY_TREE >diff-TREE.actual &&
+               git diff --cached $EMPTY_TREE >diff-TREE-cached.actual &&
+               git diff >diff-FILES.actual
+       ) &&
+       test_cmp diff-TREE.expected diff-TREE.actual &&
+       test_cmp diff-TREE-cached.expected diff-TREE-cached.actual &&
+       test_cmp diff-FILES.expected diff-FILES.actual
+'
+
+test_expect_success 'git grep' '
+       echo dir/tracked >expected.grep &&
+       (
+               cd repo.git/work/sub &&
+               GIT_DIR=../.. &&
+               GIT_WORK_TREE=.. &&
+               export GIT_DIR GIT_WORK_TREE &&
+               git grep -l changed >../../../actual.grep
+       ) &&
+       test_cmp expected.grep actual.grep
+'
+
+test_expect_success 'git commit' '
+       (
+               cd repo.git &&
+               GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done
+       )
+'
+
+test_expect_success 'absolute pathspec should fail gracefully' '
+       (
+               cd repo.git &&
+               test_might_fail git config --unset core.worktree &&
+               test_must_fail git log HEAD -- /home
+       )
+'
+
+test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
+       >dummy_file &&
+       echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
+       git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
+'
+
+test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '
+       GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \
+       test-subprocess --setup-work-tree rev-parse --show-toplevel >actual &&
+       echo "$(pwd)/repo.git/work" >expected &&
+       test_cmp expected actual
+'
+
+test_expect_success 'Multi-worktree setup' '
+       mkdir work &&
+       mkdir -p repo.git/repos/foo &&
+       cp repo.git/HEAD repo.git/index repo.git/repos/foo &&
+       test_might_fail cp repo.git/sharedindex.* repo.git/repos/foo &&
+       sane_unset GIT_DIR GIT_CONFIG GIT_WORK_TREE
+'
+
+test_expect_success 'GIT_DIR set (1)' '
+       echo "gitdir: repo.git/repos/foo" >gitfile &&
+       echo ../.. >repo.git/repos/foo/commondir &&
+       (
+               cd work &&
+               GIT_DIR=../gitfile git rev-parse --git-common-dir >actual &&
+               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'GIT_DIR set (2)' '
+       echo "gitdir: repo.git/repos/foo" >gitfile &&
+       echo "$(pwd)/repo.git" >repo.git/repos/foo/commondir &&
+       (
+               cd work &&
+               GIT_DIR=../gitfile git rev-parse --git-common-dir >actual &&
+               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'Auto discovery' '
+       echo "gitdir: repo.git/repos/foo" >.git &&
+       echo ../.. >repo.git/repos/foo/commondir &&
+       (
+               cd work &&
+               git rev-parse --git-common-dir >actual &&
+               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+               test_cmp expect actual &&
+               echo haha >data1 &&
+               git add data1 &&
+               git ls-files --full-name :/ | grep data1 >actual &&
+               echo work/data1 >expect &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success '$GIT_DIR/common overrides core.worktree' '
+       mkdir elsewhere &&
+       git --git-dir=repo.git config core.worktree "$TRASH_DIRECTORY/elsewhere" &&
+       echo "gitdir: repo.git/repos/foo" >.git &&
+       echo ../.. >repo.git/repos/foo/commondir &&
+       (
+               cd work &&
+               git rev-parse --git-common-dir >actual &&
+               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+               test_cmp expect actual &&
+               echo haha >data2 &&
+               git add data2 &&
+               git ls-files --full-name :/ | grep data2 >actual &&
+               echo work/data2 >expect &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success '$GIT_WORK_TREE overrides $GIT_DIR/common' '
+       echo "gitdir: repo.git/repos/foo" >.git &&
+       echo ../.. >repo.git/repos/foo/commondir &&
+       (
+               cd work &&
+               echo haha >data3 &&
+               git --git-dir=../.git --work-tree=. add data3 &&
+               git ls-files --full-name -- :/ | grep data3 >actual &&
+               echo data3 >expect &&
+               test_cmp expect actual
+       )
+'
+
+test_done
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
deleted file mode 100755 (executable)
index cc5b870..0000000
+++ /dev/null
@@ -1,426 +0,0 @@
-#!/bin/sh
-
-test_description='test separate work tree'
-. ./test-lib.sh
-
-test_expect_success 'setup' '
-       EMPTY_TREE=$(git write-tree) &&
-       EMPTY_BLOB=$(git hash-object -t blob --stdin </dev/null) &&
-       CHANGED_BLOB=$(echo changed | git hash-object -t blob --stdin) &&
-       EMPTY_BLOB7=$(echo $EMPTY_BLOB | sed "s/\(.......\).*/\1/") &&
-       CHANGED_BLOB7=$(echo $CHANGED_BLOB | sed "s/\(.......\).*/\1/") &&
-
-       mkdir -p work/sub/dir &&
-       mkdir -p work2 &&
-       mv .git repo.git
-'
-
-test_expect_success 'setup: helper for testing rev-parse' '
-       test_rev_parse() {
-               echo $1 >expected.bare &&
-               echo $2 >expected.inside-git &&
-               echo $3 >expected.inside-worktree &&
-               if test $# -ge 4
-               then
-                       echo $4 >expected.prefix
-               fi &&
-
-               git rev-parse --is-bare-repository >actual.bare &&
-               git rev-parse --is-inside-git-dir >actual.inside-git &&
-               git rev-parse --is-inside-work-tree >actual.inside-worktree &&
-               if test $# -ge 4
-               then
-                       git rev-parse --show-prefix >actual.prefix
-               fi &&
-
-               test_cmp expected.bare actual.bare &&
-               test_cmp expected.inside-git actual.inside-git &&
-               test_cmp expected.inside-worktree actual.inside-worktree &&
-               if test $# -ge 4
-               then
-                       # rev-parse --show-prefix should output
-                       # a single newline when at the top of the work tree,
-                       # but we test for that separately.
-                       test -z "$4" && ! test -s actual.prefix ||
-                       test_cmp expected.prefix actual.prefix
-               fi
-       }
-'
-
-test_expect_success 'setup: core.worktree = relative path' '
-       sane_unset GIT_WORK_TREE &&
-       GIT_DIR=repo.git &&
-       GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
-       export GIT_DIR GIT_CONFIG &&
-       git config core.worktree ../work
-'
-
-test_expect_success 'outside' '
-       test_rev_parse false false false
-'
-
-test_expect_success 'inside work tree' '
-       (
-               cd work &&
-               GIT_DIR=../repo.git &&
-               GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
-               test_rev_parse false false true ""
-       )
-'
-
-test_expect_success 'empty prefix is actually written out' '
-       echo >expected &&
-       (
-               cd work &&
-               GIT_DIR=../repo.git &&
-               GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
-               git rev-parse --show-prefix >../actual
-       ) &&
-       test_cmp expected actual
-'
-
-test_expect_success 'subdir of work tree' '
-       (
-               cd work/sub/dir &&
-               GIT_DIR=../../../repo.git &&
-               GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
-               test_rev_parse false false true sub/dir/
-       )
-'
-
-test_expect_success 'setup: core.worktree = absolute path' '
-       sane_unset GIT_WORK_TREE &&
-       GIT_DIR=$(pwd)/repo.git &&
-       GIT_CONFIG=$GIT_DIR/config &&
-       export GIT_DIR GIT_CONFIG &&
-       git config core.worktree "$(pwd)/work"
-'
-
-test_expect_success 'outside' '
-       test_rev_parse false false false &&
-       (
-               cd work2 &&
-               test_rev_parse false false false
-       )
-'
-
-test_expect_success 'inside work tree' '
-       (
-               cd work &&
-               test_rev_parse false false true ""
-       )
-'
-
-test_expect_success 'subdir of work tree' '
-       (
-               cd work/sub/dir &&
-               test_rev_parse false false true sub/dir/
-       )
-'
-
-test_expect_success 'setup: GIT_WORK_TREE=relative (override core.worktree)' '
-       GIT_DIR=$(pwd)/repo.git &&
-       GIT_CONFIG=$GIT_DIR/config &&
-       git config core.worktree non-existent &&
-       GIT_WORK_TREE=work &&
-       export GIT_DIR GIT_CONFIG GIT_WORK_TREE
-'
-
-test_expect_success 'outside' '
-       test_rev_parse false false false &&
-       (
-               cd work2 &&
-               test_rev_parse false false false
-       )
-'
-
-test_expect_success 'inside work tree' '
-       (
-               cd work &&
-               GIT_WORK_TREE=. &&
-               test_rev_parse false false true ""
-       )
-'
-
-test_expect_success 'subdir of work tree' '
-       (
-               cd work/sub/dir &&
-               GIT_WORK_TREE=../.. &&
-               test_rev_parse false false true sub/dir/
-       )
-'
-
-test_expect_success 'setup: GIT_WORK_TREE=absolute, below git dir' '
-       mv work repo.git/work &&
-       mv work2 repo.git/work2 &&
-       GIT_DIR=$(pwd)/repo.git &&
-       GIT_CONFIG=$GIT_DIR/config &&
-       GIT_WORK_TREE=$(pwd)/repo.git/work &&
-       export GIT_DIR GIT_CONFIG GIT_WORK_TREE
-'
-
-test_expect_success 'outside' '
-       echo outside &&
-       test_rev_parse false false false
-'
-
-test_expect_success 'in repo.git' '
-       (
-               cd repo.git &&
-               test_rev_parse false true false
-       ) &&
-       (
-               cd repo.git/objects &&
-               test_rev_parse false true false
-       ) &&
-       (
-               cd repo.git/work2 &&
-               test_rev_parse false true false
-       )
-'
-
-test_expect_success 'inside work tree' '
-       (
-               cd repo.git/work &&
-               test_rev_parse false true true ""
-       )
-'
-
-test_expect_success 'subdir of work tree' '
-       (
-               cd repo.git/work/sub/dir &&
-               test_rev_parse false true true sub/dir/
-       )
-'
-
-test_expect_success 'find work tree from repo' '
-       echo sub/dir/untracked >expected &&
-       cat <<-\EOF >repo.git/work/.gitignore &&
-       expected.*
-       actual.*
-       .gitignore
-       EOF
-       >repo.git/work/sub/dir/untracked &&
-       (
-               cd repo.git &&
-               git ls-files --others --exclude-standard >../actual
-       ) &&
-       test_cmp expected actual
-'
-
-test_expect_success 'find work tree from work tree' '
-       echo sub/dir/tracked >expected &&
-       >repo.git/work/sub/dir/tracked &&
-       (
-               cd repo.git/work/sub/dir &&
-               git --git-dir=../../.. add tracked
-       ) &&
-       (
-               cd repo.git &&
-               git ls-files >../actual
-       ) &&
-       test_cmp expected actual
-'
-
-test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
-       (
-               cd repo.git/work/sub/dir &&
-               GIT_DIR=../../.. &&
-               GIT_WORK_TREE=../.. &&
-               GIT_PAGER= &&
-               export GIT_DIR GIT_WORK_TREE GIT_PAGER &&
-
-               git diff --exit-code tracked &&
-               echo changed >tracked &&
-               test_must_fail git diff --exit-code tracked
-       )
-'
-
-test_expect_success 'diff-index respects work tree under .git dir' '
-       cat >diff-index-cached.expected <<-EOF &&
-       :000000 100644 $_z40 $EMPTY_BLOB A      sub/dir/tracked
-       EOF
-       cat >diff-index.expected <<-EOF &&
-       :000000 100644 $_z40 $_z40 A    sub/dir/tracked
-       EOF
-
-       (
-               GIT_DIR=repo.git &&
-               GIT_WORK_TREE=repo.git/work &&
-               export GIT_DIR GIT_WORK_TREE &&
-               git diff-index $EMPTY_TREE >diff-index.actual &&
-               git diff-index --cached $EMPTY_TREE >diff-index-cached.actual
-       ) &&
-       test_cmp diff-index.expected diff-index.actual &&
-       test_cmp diff-index-cached.expected diff-index-cached.actual
-'
-
-test_expect_success 'diff-files respects work tree under .git dir' '
-       cat >diff-files.expected <<-EOF &&
-       :100644 100644 $EMPTY_BLOB $_z40 M      sub/dir/tracked
-       EOF
-
-       (
-               GIT_DIR=repo.git &&
-               GIT_WORK_TREE=repo.git/work &&
-               export GIT_DIR GIT_WORK_TREE &&
-               git diff-files >diff-files.actual
-       ) &&
-       test_cmp diff-files.expected diff-files.actual
-'
-
-test_expect_success 'git diff respects work tree under .git dir' '
-       cat >diff-TREE.expected <<-EOF &&
-       diff --git a/sub/dir/tracked b/sub/dir/tracked
-       new file mode 100644
-       index 0000000..$CHANGED_BLOB7
-       --- /dev/null
-       +++ b/sub/dir/tracked
-       @@ -0,0 +1 @@
-       +changed
-       EOF
-       cat >diff-TREE-cached.expected <<-EOF &&
-       diff --git a/sub/dir/tracked b/sub/dir/tracked
-       new file mode 100644
-       index 0000000..$EMPTY_BLOB7
-       EOF
-       cat >diff-FILES.expected <<-EOF &&
-       diff --git a/sub/dir/tracked b/sub/dir/tracked
-       index $EMPTY_BLOB7..$CHANGED_BLOB7 100644
-       --- a/sub/dir/tracked
-       +++ b/sub/dir/tracked
-       @@ -0,0 +1 @@
-       +changed
-       EOF
-
-       (
-               GIT_DIR=repo.git &&
-               GIT_WORK_TREE=repo.git/work &&
-               export GIT_DIR GIT_WORK_TREE &&
-               git diff $EMPTY_TREE >diff-TREE.actual &&
-               git diff --cached $EMPTY_TREE >diff-TREE-cached.actual &&
-               git diff >diff-FILES.actual
-       ) &&
-       test_cmp diff-TREE.expected diff-TREE.actual &&
-       test_cmp diff-TREE-cached.expected diff-TREE-cached.actual &&
-       test_cmp diff-FILES.expected diff-FILES.actual
-'
-
-test_expect_success 'git grep' '
-       echo dir/tracked >expected.grep &&
-       (
-               cd repo.git/work/sub &&
-               GIT_DIR=../.. &&
-               GIT_WORK_TREE=.. &&
-               export GIT_DIR GIT_WORK_TREE &&
-               git grep -l changed >../../../actual.grep
-       ) &&
-       test_cmp expected.grep actual.grep
-'
-
-test_expect_success 'git commit' '
-       (
-               cd repo.git &&
-               GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done
-       )
-'
-
-test_expect_success 'absolute pathspec should fail gracefully' '
-       (
-               cd repo.git &&
-               test_might_fail git config --unset core.worktree &&
-               test_must_fail git log HEAD -- /home
-       )
-'
-
-test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
-       >dummy_file &&
-       echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
-       git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
-'
-
-test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '
-       GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \
-       test-subprocess --setup-work-tree rev-parse --show-toplevel >actual &&
-       echo "$(pwd)/repo.git/work" >expected &&
-       test_cmp expected actual
-'
-
-test_expect_success 'Multi-worktree setup' '
-       mkdir work &&
-       mkdir -p repo.git/repos/foo &&
-       cp repo.git/HEAD repo.git/index repo.git/repos/foo &&
-       test_might_fail cp repo.git/sharedindex.* repo.git/repos/foo &&
-       sane_unset GIT_DIR GIT_CONFIG GIT_WORK_TREE
-'
-
-test_expect_success 'GIT_DIR set (1)' '
-       echo "gitdir: repo.git/repos/foo" >gitfile &&
-       echo ../.. >repo.git/repos/foo/commondir &&
-       (
-               cd work &&
-               GIT_DIR=../gitfile git rev-parse --git-common-dir >actual &&
-               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
-               test_cmp expect actual
-       )
-'
-
-test_expect_success 'GIT_DIR set (2)' '
-       echo "gitdir: repo.git/repos/foo" >gitfile &&
-       echo "$(pwd)/repo.git" >repo.git/repos/foo/commondir &&
-       (
-               cd work &&
-               GIT_DIR=../gitfile git rev-parse --git-common-dir >actual &&
-               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
-               test_cmp expect actual
-       )
-'
-
-test_expect_success 'Auto discovery' '
-       echo "gitdir: repo.git/repos/foo" >.git &&
-       echo ../.. >repo.git/repos/foo/commondir &&
-       (
-               cd work &&
-               git rev-parse --git-common-dir >actual &&
-               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
-               test_cmp expect actual &&
-               echo haha >data1 &&
-               git add data1 &&
-               git ls-files --full-name :/ | grep data1 >actual &&
-               echo work/data1 >expect &&
-               test_cmp expect actual
-       )
-'
-
-test_expect_success '$GIT_DIR/common overrides core.worktree' '
-       mkdir elsewhere &&
-       git --git-dir=repo.git config core.worktree "$TRASH_DIRECTORY/elsewhere" &&
-       echo "gitdir: repo.git/repos/foo" >.git &&
-       echo ../.. >repo.git/repos/foo/commondir &&
-       (
-               cd work &&
-               git rev-parse --git-common-dir >actual &&
-               test-path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
-               test_cmp expect actual &&
-               echo haha >data2 &&
-               git add data2 &&
-               git ls-files --full-name :/ | grep data2 >actual &&
-               echo work/data2 >expect &&
-               test_cmp expect actual
-       )
-'
-
-test_expect_success '$GIT_WORK_TREE overrides $GIT_DIR/common' '
-       echo "gitdir: repo.git/repos/foo" >.git &&
-       echo ../.. >repo.git/repos/foo/commondir &&
-       (
-               cd work &&
-               echo haha >data3 &&
-               git --git-dir=../.git --work-tree=. add data3 &&
-               git ls-files --full-name -- :/ | grep data3 >actual &&
-               echo data3 >expect &&
-               test_cmp expect actual
-       )
-'
-
-test_done
diff --git a/t/t1509-root-work-tree.sh b/t/t1509-root-work-tree.sh
new file mode 100755 (executable)
index 0000000..553a3f6
--- /dev/null
@@ -0,0 +1,258 @@
+#!/bin/sh
+
+test_description='Test Git when git repository is located at root
+
+This test requires write access in root. Do not bother if you do not
+have a throwaway chroot or VM.
+
+Script t1509/prepare-chroot.sh may help you setup chroot, then you
+can chroot in and execute this test from there.
+'
+
+. ./test-lib.sh
+
+test_cmp_val() {
+       echo "$1" > expected
+       echo "$2" > result
+       test_cmp expected result
+}
+
+test_vars() {
+       test_expect_success "$1: gitdir" '
+               test_cmp_val "'"$2"'" "$(git rev-parse --git-dir)"
+       '
+
+       test_expect_success "$1: worktree" '
+               test_cmp_val "'"$3"'" "$(git rev-parse --show-toplevel)"
+       '
+
+       test_expect_success "$1: prefix" '
+               test_cmp_val "'"$4"'" "$(git rev-parse --show-prefix)"
+       '
+}
+
+test_foobar_root() {
+       test_expect_success 'add relative' '
+               test -z "$(cd / && git ls-files)" &&
+               git add foo/foome &&
+               git add foo/bar/barme &&
+               git add me &&
+               ( cd / && git ls-files --stage ) > result &&
+               test_cmp /ls.expected result &&
+               rm "$(git rev-parse --git-dir)/index"
+       '
+
+       test_expect_success 'add absolute' '
+               test -z "$(cd / && git ls-files)" &&
+               git add /foo/foome &&
+               git add /foo/bar/barme &&
+               git add /me &&
+               ( cd / && git ls-files --stage ) > result &&
+               test_cmp /ls.expected result &&
+               rm "$(git rev-parse --git-dir)/index"
+       '
+
+}
+
+test_foobar_foo() {
+       test_expect_success 'add relative' '
+               test -z "$(cd / && git ls-files)" &&
+               git add foome &&
+               git add bar/barme &&
+               git add ../me &&
+               ( cd / && git ls-files --stage ) > result &&
+               test_cmp /ls.expected result &&
+               rm "$(git rev-parse --git-dir)/index"
+       '
+
+       test_expect_success 'add absolute' '
+               test -z "$(cd / && git ls-files)" &&
+               git add /foo/foome &&
+               git add /foo/bar/barme &&
+               git add /me &&
+               ( cd / && git ls-files --stage ) > result &&
+               test_cmp /ls.expected result &&
+               rm "$(git rev-parse --git-dir)/index"
+       '
+}
+
+test_foobar_foobar() {
+       test_expect_success 'add relative' '
+               test -z "$(cd / && git ls-files)" &&
+               git add ../foome &&
+               git add barme &&
+               git add ../../me &&
+               ( cd / && git ls-files --stage ) > result &&
+               test_cmp /ls.expected result &&
+               rm "$(git rev-parse --git-dir)/index"
+       '
+
+       test_expect_success 'add absolute' '
+               test -z "$(cd / && git ls-files)" &&
+               git add /foo/foome &&
+               git add /foo/bar/barme &&
+               git add /me &&
+               ( cd / && git ls-files --stage ) > result &&
+               test_cmp /ls.expected result &&
+               rm "$(git rev-parse --git-dir)/index"
+       '
+}
+
+if ! test -w /
+then
+       skip_all="Test requiring writable / skipped. Read this test if you want to run it"
+       test_done
+fi
+
+if  test -e /refs || test -e /objects || test -e /info || test -e /hooks ||
+    test -e /.git || test -e /foo || test -e /me
+then
+       skip_all="Skip test that clobbers existing files in /"
+       test_done
+fi
+
+if [ "$IKNOWWHATIAMDOING" != "YES" ]; then
+       skip_all="You must set env var IKNOWWHATIAMDOING=YES in order to run this test"
+       test_done
+fi
+
+if ! test_have_prereq NOT_ROOT
+then
+       skip_all="No you can't run this as root"
+       test_done
+fi
+
+ONE_SHA1=d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
+
+test_expect_success 'setup' '
+       rm -rf /foo &&
+       mkdir /foo &&
+       mkdir /foo/bar &&
+       echo 1 > /foo/foome &&
+       echo 1 > /foo/bar/barme &&
+       echo 1 > /me
+'
+
+say "GIT_DIR absolute, GIT_WORK_TREE set"
+
+test_expect_success 'go to /' 'cd /'
+
+cat >ls.expected <<EOF
+100644 $ONE_SHA1 0     foo/bar/barme
+100644 $ONE_SHA1 0     foo/foome
+100644 $ONE_SHA1 0     me
+EOF
+
+GIT_DIR="$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'abs gitdir, root' "$GIT_DIR" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /foo' 'cd /foo'
+
+test_vars 'abs gitdir, foo' "$GIT_DIR" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+
+test_vars 'abs gitdir, foo/bar' "$GIT_DIR" "/" "foo/bar/"
+test_foobar_foobar
+
+say "GIT_DIR relative, GIT_WORK_TREE set"
+
+test_expect_success 'go to /' 'cd /'
+
+GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /foo' 'cd /foo'
+
+GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+
+GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
+test_foobar_foobar
+
+say "GIT_DIR relative, GIT_WORK_TREE relative"
+
+test_expect_success 'go to /' 'cd /'
+
+GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
+GIT_WORK_TREE=. && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /' 'cd /foo'
+
+GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=.. && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+
+GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=../.. && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
+test_foobar_foobar
+
+say ".git at root"
+
+unset GIT_DIR
+unset GIT_WORK_TREE
+
+test_expect_success 'go to /' 'cd /'
+test_expect_success 'setup' '
+       rm -rf /.git &&
+       echo "Initialized empty Git repository in /.git/" > expected &&
+       git init > result &&
+       test_cmp expected result
+'
+
+test_vars 'auto gitdir, root' ".git" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /foo' 'cd /foo'
+test_vars 'auto gitdir, foo' "/.git" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+test_vars 'auto gitdir, foo/bar' "/.git" "/" "foo/bar/"
+test_foobar_foobar
+
+test_expect_success 'cleanup' 'rm -rf /.git'
+
+say "auto bare gitdir"
+
+# DESTROYYYYY!!!!!
+test_expect_success 'setup' '
+       rm -rf /refs /objects /info /hooks &&
+       rm -f /expected /ls.expected /me /result &&
+       cd / &&
+       echo "Initialized empty Git repository in /" > expected &&
+       git init --bare > result &&
+       test_cmp expected result
+'
+
+test_vars 'auto gitdir, root' "." "" ""
+
+test_expect_success 'go to /foo' 'cd /foo'
+
+test_vars 'auto gitdir, root' "/" "" ""
+
+test_done
diff --git a/t/t1509-root-worktree.sh b/t/t1509-root-worktree.sh
deleted file mode 100755 (executable)
index 553a3f6..0000000
+++ /dev/null
@@ -1,258 +0,0 @@
-#!/bin/sh
-
-test_description='Test Git when git repository is located at root
-
-This test requires write access in root. Do not bother if you do not
-have a throwaway chroot or VM.
-
-Script t1509/prepare-chroot.sh may help you setup chroot, then you
-can chroot in and execute this test from there.
-'
-
-. ./test-lib.sh
-
-test_cmp_val() {
-       echo "$1" > expected
-       echo "$2" > result
-       test_cmp expected result
-}
-
-test_vars() {
-       test_expect_success "$1: gitdir" '
-               test_cmp_val "'"$2"'" "$(git rev-parse --git-dir)"
-       '
-
-       test_expect_success "$1: worktree" '
-               test_cmp_val "'"$3"'" "$(git rev-parse --show-toplevel)"
-       '
-
-       test_expect_success "$1: prefix" '
-               test_cmp_val "'"$4"'" "$(git rev-parse --show-prefix)"
-       '
-}
-
-test_foobar_root() {
-       test_expect_success 'add relative' '
-               test -z "$(cd / && git ls-files)" &&
-               git add foo/foome &&
-               git add foo/bar/barme &&
-               git add me &&
-               ( cd / && git ls-files --stage ) > result &&
-               test_cmp /ls.expected result &&
-               rm "$(git rev-parse --git-dir)/index"
-       '
-
-       test_expect_success 'add absolute' '
-               test -z "$(cd / && git ls-files)" &&
-               git add /foo/foome &&
-               git add /foo/bar/barme &&
-               git add /me &&
-               ( cd / && git ls-files --stage ) > result &&
-               test_cmp /ls.expected result &&
-               rm "$(git rev-parse --git-dir)/index"
-       '
-
-}
-
-test_foobar_foo() {
-       test_expect_success 'add relative' '
-               test -z "$(cd / && git ls-files)" &&
-               git add foome &&
-               git add bar/barme &&
-               git add ../me &&
-               ( cd / && git ls-files --stage ) > result &&
-               test_cmp /ls.expected result &&
-               rm "$(git rev-parse --git-dir)/index"
-       '
-
-       test_expect_success 'add absolute' '
-               test -z "$(cd / && git ls-files)" &&
-               git add /foo/foome &&
-               git add /foo/bar/barme &&
-               git add /me &&
-               ( cd / && git ls-files --stage ) > result &&
-               test_cmp /ls.expected result &&
-               rm "$(git rev-parse --git-dir)/index"
-       '
-}
-
-test_foobar_foobar() {
-       test_expect_success 'add relative' '
-               test -z "$(cd / && git ls-files)" &&
-               git add ../foome &&
-               git add barme &&
-               git add ../../me &&
-               ( cd / && git ls-files --stage ) > result &&
-               test_cmp /ls.expected result &&
-               rm "$(git rev-parse --git-dir)/index"
-       '
-
-       test_expect_success 'add absolute' '
-               test -z "$(cd / && git ls-files)" &&
-               git add /foo/foome &&
-               git add /foo/bar/barme &&
-               git add /me &&
-               ( cd / && git ls-files --stage ) > result &&
-               test_cmp /ls.expected result &&
-               rm "$(git rev-parse --git-dir)/index"
-       '
-}
-
-if ! test -w /
-then
-       skip_all="Test requiring writable / skipped. Read this test if you want to run it"
-       test_done
-fi
-
-if  test -e /refs || test -e /objects || test -e /info || test -e /hooks ||
-    test -e /.git || test -e /foo || test -e /me
-then
-       skip_all="Skip test that clobbers existing files in /"
-       test_done
-fi
-
-if [ "$IKNOWWHATIAMDOING" != "YES" ]; then
-       skip_all="You must set env var IKNOWWHATIAMDOING=YES in order to run this test"
-       test_done
-fi
-
-if ! test_have_prereq NOT_ROOT
-then
-       skip_all="No you can't run this as root"
-       test_done
-fi
-
-ONE_SHA1=d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
-
-test_expect_success 'setup' '
-       rm -rf /foo &&
-       mkdir /foo &&
-       mkdir /foo/bar &&
-       echo 1 > /foo/foome &&
-       echo 1 > /foo/bar/barme &&
-       echo 1 > /me
-'
-
-say "GIT_DIR absolute, GIT_WORK_TREE set"
-
-test_expect_success 'go to /' 'cd /'
-
-cat >ls.expected <<EOF
-100644 $ONE_SHA1 0     foo/bar/barme
-100644 $ONE_SHA1 0     foo/foome
-100644 $ONE_SHA1 0     me
-EOF
-
-GIT_DIR="$TRASH_DIRECTORY/.git" && export GIT_DIR
-GIT_WORK_TREE=/ && export GIT_WORK_TREE
-
-test_vars 'abs gitdir, root' "$GIT_DIR" "/" ""
-test_foobar_root
-
-test_expect_success 'go to /foo' 'cd /foo'
-
-test_vars 'abs gitdir, foo' "$GIT_DIR" "/" "foo/"
-test_foobar_foo
-
-test_expect_success 'go to /foo/bar' 'cd /foo/bar'
-
-test_vars 'abs gitdir, foo/bar' "$GIT_DIR" "/" "foo/bar/"
-test_foobar_foobar
-
-say "GIT_DIR relative, GIT_WORK_TREE set"
-
-test_expect_success 'go to /' 'cd /'
-
-GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
-GIT_WORK_TREE=/ && export GIT_WORK_TREE
-
-test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
-test_foobar_root
-
-test_expect_success 'go to /foo' 'cd /foo'
-
-GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
-GIT_WORK_TREE=/ && export GIT_WORK_TREE
-
-test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
-test_foobar_foo
-
-test_expect_success 'go to /foo/bar' 'cd /foo/bar'
-
-GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
-GIT_WORK_TREE=/ && export GIT_WORK_TREE
-
-test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
-test_foobar_foobar
-
-say "GIT_DIR relative, GIT_WORK_TREE relative"
-
-test_expect_success 'go to /' 'cd /'
-
-GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
-GIT_WORK_TREE=. && export GIT_WORK_TREE
-
-test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
-test_foobar_root
-
-test_expect_success 'go to /' 'cd /foo'
-
-GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
-GIT_WORK_TREE=.. && export GIT_WORK_TREE
-
-test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
-test_foobar_foo
-
-test_expect_success 'go to /foo/bar' 'cd /foo/bar'
-
-GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
-GIT_WORK_TREE=../.. && export GIT_WORK_TREE
-
-test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
-test_foobar_foobar
-
-say ".git at root"
-
-unset GIT_DIR
-unset GIT_WORK_TREE
-
-test_expect_success 'go to /' 'cd /'
-test_expect_success 'setup' '
-       rm -rf /.git &&
-       echo "Initialized empty Git repository in /.git/" > expected &&
-       git init > result &&
-       test_cmp expected result
-'
-
-test_vars 'auto gitdir, root' ".git" "/" ""
-test_foobar_root
-
-test_expect_success 'go to /foo' 'cd /foo'
-test_vars 'auto gitdir, foo' "/.git" "/" "foo/"
-test_foobar_foo
-
-test_expect_success 'go to /foo/bar' 'cd /foo/bar'
-test_vars 'auto gitdir, foo/bar' "/.git" "/" "foo/bar/"
-test_foobar_foobar
-
-test_expect_success 'cleanup' 'rm -rf /.git'
-
-say "auto bare gitdir"
-
-# DESTROYYYYY!!!!!
-test_expect_success 'setup' '
-       rm -rf /refs /objects /info /hooks &&
-       rm -f /expected /ls.expected /me /result &&
-       cd / &&
-       echo "Initialized empty Git repository in /" > expected &&
-       git init --bare > result &&
-       test_cmp expected result
-'
-
-test_vars 'auto gitdir, root' "." "" ""
-
-test_expect_success 'go to /foo' 'cd /foo'
-
-test_vars 'auto gitdir, root' "/" "" ""
-
-test_done
index 199b22d85e92535f148eaaca4d3856dbdfb6bcec..b99d5192a96ec77ef6a99cb86518375c447b48a9 100755 (executable)
@@ -56,30 +56,4 @@ test_expect_success VAGUENESS_SUCCESS 'checkout reports switch to branch' '
        test_i18ngrep ! "^HEAD is now at" stderr
 '
 
-test_expect_success 'wildcard ambiguation, paths win' '
-       git init ambi &&
-       (
-               cd ambi &&
-               echo a >a.c &&
-               git add a.c &&
-               echo b >a.c &&
-               git checkout "*.c" &&
-               echo a >expect &&
-               test_cmp expect a.c
-       )
-'
-
-test_expect_success !MINGW 'wildcard ambiguation, refs lose' '
-       git init ambi2 &&
-       (
-               cd ambi2 &&
-               echo a >"*.c" &&
-               git add . &&
-               test_must_fail git show :"*.c" &&
-               git show :"*.c" -- >actual &&
-               echo a >expect &&
-               test_cmp expect actual
-       )
-'
-
 test_done
index 0a804dab634d0a72bfabd8826d0cb38e1b4f7dbc..cbfa41ec61b9f893b4df441e0289a5313ccaf413 100755 (executable)
@@ -193,6 +193,21 @@ test_expect_success '"add" -B/--detach mutually exclusive' '
        test_must_fail git worktree add -B poodle --detach bamboo master
 '
 
+test_expect_success '"add -B" fails if the branch is checked out' '
+       git rev-parse newmaster >before &&
+       test_must_fail git worktree add -B newmaster bamboo master &&
+       git rev-parse newmaster >after &&
+       test_cmp before after
+'
+
+test_expect_success 'add -B' '
+       git worktree add -B poodle bamboo2 master^ &&
+       git -C bamboo2 symbolic-ref HEAD >actual &&
+       echo refs/heads/poodle >expected &&
+       test_cmp expected actual &&
+       test_cmp_rev master^ poodle
+'
+
 test_expect_success 'local clone from linked checkout' '
        git clone --local here here-clone &&
        ( cd here-clone && git fsck )
index 75ebb1b4a0f79faa6f92cc9575eeef6b10ae27d3..1b1b65a6b0fc7ace4e18e8642a6c47631d7e6a43 100755 (executable)
@@ -8,6 +8,16 @@ test_expect_success 'setup' '
        test_commit init
 '
 
+test_expect_success 'rev-parse --git-common-dir on main worktree' '
+       git rev-parse --git-common-dir >actual &&
+       echo .git >expected &&
+       test_cmp expected actual &&
+       mkdir sub &&
+       git -C sub rev-parse --git-common-dir >actual2 &&
+       echo sub/.git >expected2 &&
+       test_cmp expected2 actual2
+'
+
 test_expect_success '"list" all worktrees from main' '
        echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
        test_when_finished "rm -rf here && git worktree prune" &&
index 3fc484e8c3f8d910f02e409baf7bf1d5682b4f2f..d043078da526275c3b1505297f5300cf1ad26a22 100755 (executable)
@@ -175,13 +175,10 @@ test_expect_success 'negated exclude matches can override previous ones' '
        grep "^a.1" output
 '
 
-test_expect_success 'excluded directory overrides content patterns' '
+test_expect_success 'excluded directory does not override content patterns' '
 
        git ls-files --others --exclude="one" --exclude="!one/a.1" >output &&
-       if grep "^one/a.1" output
-       then
-               false
-       fi
+       grep "^one/a.1" output
 '
 
 test_expect_success 'negated directory doesn'\''t affect content patterns' '
diff --git a/t/t3007-ls-files-other-negative.sh b/t/t3007-ls-files-other-negative.sh
new file mode 100755 (executable)
index 0000000..0797b86
--- /dev/null
@@ -0,0 +1,153 @@
+#!/bin/sh
+
+test_description='test re-include patterns'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+       mkdir -p fooo foo/bar tmp &&
+       touch abc foo/def foo/bar/ghi foo/bar/bar
+'
+
+test_expect_success 'no match, do not enter subdir and waste cycles' '
+       cat >.gitignore <<-\EOF &&
+       /tmp
+       /foo
+       !fooo/bar/bar
+       EOF
+       GIT_TRACE_EXCLUDE="$(pwd)/tmp/trace" git ls-files -o --exclude-standard >tmp/actual &&
+       ! grep "enter .foo/.\$" tmp/trace &&
+       cat >tmp/expected <<-\EOF &&
+       .gitignore
+       abc
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_expect_success 'match, excluded by literal pathname pattern' '
+       cat >.gitignore <<-\EOF &&
+       /tmp
+       /fooo
+       /foo
+       !foo/bar/bar
+       EOF
+       cat >fooo/.gitignore <<-\EOF &&
+       !/*
+       EOF     git ls-files -o --exclude-standard >tmp/actual &&
+       cat >tmp/expected <<-\EOF &&
+       .gitignore
+       abc
+       foo/bar/bar
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_expect_success 'match, excluded by wildcard pathname pattern' '
+       cat >.gitignore <<-\EOF &&
+       /tmp
+       /fooo
+       /fo?
+       !foo/bar/bar
+       EOF
+       git ls-files -o --exclude-standard >tmp/actual &&
+       cat >tmp/expected <<-\EOF &&
+       .gitignore
+       abc
+       foo/bar/bar
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_expect_success 'match, excluded by literal basename pattern' '
+       cat >.gitignore <<-\EOF &&
+       /tmp
+       /fooo
+       foo
+       !foo/bar/bar
+       EOF
+       git ls-files -o --exclude-standard >tmp/actual &&
+       cat >tmp/expected <<-\EOF &&
+       .gitignore
+       abc
+       foo/bar/bar
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_expect_success 'match, excluded by wildcard basename pattern' '
+       cat >.gitignore <<-\EOF &&
+       /tmp
+       /fooo
+       fo?
+       !foo/bar/bar
+       EOF
+       git ls-files -o --exclude-standard >tmp/actual &&
+       cat >tmp/expected <<-\EOF &&
+       .gitignore
+       abc
+       foo/bar/bar
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_expect_success 'match, excluded by literal mustbedir, basename pattern' '
+       cat >.gitignore <<-\EOF &&
+       /tmp
+       /fooo
+       foo/
+       !foo/bar/bar
+       EOF
+       git ls-files -o --exclude-standard >tmp/actual &&
+       cat >tmp/expected <<-\EOF &&
+       .gitignore
+       abc
+       foo/bar/bar
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_expect_success 'match, excluded by literal mustbedir, pathname pattern' '
+       cat >.gitignore <<-\EOF &&
+       /tmp
+       /fooo
+       /foo/
+       !foo/bar/bar
+       EOF
+       git ls-files -o --exclude-standard >tmp/actual &&
+       cat >tmp/expected <<-\EOF &&
+       .gitignore
+       abc
+       foo/bar/bar
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_expect_success 'prepare for nested negatives' '
+       cat >.git/info/exclude <<-\EOF &&
+       /.gitignore
+       /tmp
+       /foo
+       /abc
+       EOF
+       git ls-files -o --exclude-standard >tmp/actual &&
+       test_must_be_empty tmp/actual &&
+       mkdir -p 1/2/3/4 &&
+       touch 1/f 1/2/f 1/2/3/f 1/2/3/4/f
+'
+
+test_expect_success 'match, literal pathname, nested negatives' '
+       cat >.gitignore <<-\EOF &&
+       /1
+       !1/2
+       1/2/3
+       !1/2/3/4
+       EOF
+       git ls-files -o --exclude-standard >tmp/actual &&
+       cat >tmp/expected <<-\EOF &&
+       1/2/3/4/f
+       1/2/f
+       EOF
+       test_cmp tmp/expected tmp/actual
+'
+
+test_done
diff --git a/t/t3032-merge-recursive-options.sh b/t/t3032-merge-recursive-options.sh
deleted file mode 100755 (executable)
index 4029c9c..0000000
+++ /dev/null
@@ -1,207 +0,0 @@
-#!/bin/sh
-
-test_description='merge-recursive options
-
-* [master] Clarify
- ! [remote] Remove cruft
---
- + [remote] Remove cruft
-*  [master] Clarify
-*+ [remote^] Initial revision
-*   ok 1: setup
-'
-
-. ./test-lib.sh
-
-test_have_prereq SED_STRIPS_CR && SED_OPTIONS=-b
-if test_have_prereq GREP_STRIPS_CR
-then
-       GREP_OPTIONS=-U
-       export GREP_OPTIONS
-fi
-
-test_expect_success 'setup' '
-       conflict_hunks () {
-               sed $SED_OPTIONS -n -e "
-                       /^<<<</ b conflict
-                       b
-                       : conflict
-                       p
-                       /^>>>>/ b
-                       n
-                       b conflict
-               " "$@"
-       } &&
-
-       cat <<-\EOF >text.txt &&
-           Hope, he says, cherishes the soul of him who lives in
-           justice and holiness and is the nurse of his age and the
-           companion of his journey;--hope which is mightiest to sway
-           the restless soul of man.
-
-       How admirable are his words!  And the great blessing of riches, I do
-       not say to every man, but to a good man, is, that he has had no
-       occasion to deceive or to defraud others, either intentionally or
-       unintentionally; and when he departs to the world below he is not in
-       any apprehension about offerings due to the gods or debts which he owes
-       to men.  Now to this peace of mind the possession of wealth greatly
-       contributes; and therefore I say, that, setting one thing against
-       another, of the many advantages which wealth has to give, to a man of
-       sense this is in my opinion the greatest.
-
-       Well said, Cephalus, I replied; but as concerning justice, what is
-       it?--to speak the truth and to pay your debts--no more than this?  And
-       even to this are there not exceptions?  Suppose that a friend when in
-       his right mind has deposited arms with me and he asks for them when he
-       is not in his right mind, ought I to give them back to him?  No one
-       would say that I ought or that I should be right in doing so, any more
-       than they would say that I ought always to speak the truth to one who
-       is in his condition.
-
-       You are quite right, he replied.
-
-       But then, I said, speaking the truth and paying your debts is not a
-       correct definition of justice.
-
-       CEPHALUS - SOCRATES - POLEMARCHUS
-
-       Quite correct, Socrates, if Simonides is to be believed, said
-       Polemarchus interposing.
-
-       I fear, said Cephalus, that I must go now, for I have to look after the
-       sacrifices, and I hand over the argument to Polemarchus and the company.
-       EOF
-       git add text.txt &&
-       test_tick &&
-       git commit -m "Initial revision" &&
-
-       git checkout -b remote &&
-       sed -e "
-                       s/\.  /\. /g
-                       s/[?]  /? /g
-                       s/    / /g
-                       s/--/---/g
-                       s/but as concerning/but as con cerning/
-                       /CEPHALUS - SOCRATES - POLEMARCHUS/ d
-               " text.txt >text.txt+ &&
-       mv text.txt+ text.txt &&
-       git commit -a -m "Remove cruft" &&
-
-       git checkout master &&
-       sed -e "
-                       s/\(not in his right mind\),\(.*\)/\1;\2Q/
-                       s/Quite correct\(.*\)/It is too correct\1Q/
-                       s/unintentionally/un intentionally/
-                       /un intentionally/ s/$/Q/
-                       s/Polemarchus interposing./Polemarchus, interposing.Q/
-                       /justice and holiness/ s/$/Q/
-                       /pay your debts/ s/$/Q/
-               " text.txt | q_to_cr >text.txt+ &&
-       mv text.txt+ text.txt &&
-       git commit -a -m "Clarify" &&
-       git show-branch --all
-'
-
-test_expect_success 'naive merge fails' '
-       git read-tree --reset -u HEAD &&
-       test_must_fail git merge-recursive HEAD^ -- HEAD remote &&
-       test_must_fail git update-index --refresh &&
-       grep "<<<<<<" text.txt
-'
-
-test_expect_success '--ignore-space-change makes merge succeed' '
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote
-'
-
-test_expect_success 'naive cherry-pick fails' '
-       git read-tree --reset -u HEAD &&
-       test_must_fail git cherry-pick --no-commit remote &&
-       git read-tree --reset -u HEAD &&
-       test_must_fail git cherry-pick remote &&
-       test_must_fail git update-index --refresh &&
-       grep "<<<<<<" text.txt
-'
-
-test_expect_success '-Xignore-space-change makes cherry-pick succeed' '
-       git read-tree --reset -u HEAD &&
-       git cherry-pick --no-commit -Xignore-space-change remote
-'
-
-test_expect_success '--ignore-space-change: our w/s-only change wins' '
-       q_to_cr <<-\EOF >expected &&
-           justice and holiness and is the nurse of his age and theQ
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
-       grep "justice and holiness" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-space-change: their real change wins over w/s' '
-       cat <<-\EOF >expected &&
-       it?---to speak the truth and to pay your debts---no more than this? And
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
-       grep "pay your debts" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-space-change: does not ignore new spaces' '
-       cat <<-\EOF >expected1 &&
-       Well said, Cephalus, I replied; but as con cerning justice, what is
-       EOF
-       q_to_cr <<-\EOF >expected2 &&
-       un intentionally; and when he departs to the world below he is not inQ
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
-       grep "Well said" text.txt >actual1 &&
-       grep "when he departs" text.txt >actual2 &&
-       test_cmp expected1 actual1 &&
-       test_cmp expected2 actual2
-'
-
-test_expect_success '--ignore-all-space drops their new spaces' '
-       cat <<-\EOF >expected &&
-       Well said, Cephalus, I replied; but as concerning justice, what is
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
-       grep "Well said" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-all-space keeps our new spaces' '
-       q_to_cr <<-\EOF >expected &&
-       un intentionally; and when he departs to the world below he is not inQ
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
-       grep "when he departs" text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_expect_success '--ignore-space-at-eol' '
-       q_to_cr <<-\EOF >expected &&
-       <<<<<<< HEAD
-       is not in his right mind; ought I to give them back to him?  No oneQ
-       =======
-       is not in his right mind, ought I to give them back to him? No one
-       >>>>>>> remote
-       EOF
-
-       git read-tree --reset -u HEAD &&
-       test_must_fail git merge-recursive --ignore-space-at-eol \
-                                                HEAD^ -- HEAD remote &&
-       conflict_hunks text.txt >actual &&
-       test_cmp expected actual
-'
-
-test_done
diff --git a/t/t3032-merge-recursive-space-options.sh b/t/t3032-merge-recursive-space-options.sh
new file mode 100755 (executable)
index 0000000..b56180e
--- /dev/null
@@ -0,0 +1,207 @@
+#!/bin/sh
+
+test_description='merge-recursive space options
+
+* [master] Clarify
+ ! [remote] Remove cruft
+--
+ + [remote] Remove cruft
+*  [master] Clarify
+*+ [remote^] Initial revision
+*   ok 1: setup
+'
+
+. ./test-lib.sh
+
+test_have_prereq SED_STRIPS_CR && SED_OPTIONS=-b
+if test_have_prereq GREP_STRIPS_CR
+then
+       GREP_OPTIONS=-U
+       export GREP_OPTIONS
+fi
+
+test_expect_success 'setup' '
+       conflict_hunks () {
+               sed $SED_OPTIONS -n -e "
+                       /^<<<</ b conflict
+                       b
+                       : conflict
+                       p
+                       /^>>>>/ b
+                       n
+                       b conflict
+               " "$@"
+       } &&
+
+       cat <<-\EOF >text.txt &&
+           Hope, he says, cherishes the soul of him who lives in
+           justice and holiness and is the nurse of his age and the
+           companion of his journey;--hope which is mightiest to sway
+           the restless soul of man.
+
+       How admirable are his words!  And the great blessing of riches, I do
+       not say to every man, but to a good man, is, that he has had no
+       occasion to deceive or to defraud others, either intentionally or
+       unintentionally; and when he departs to the world below he is not in
+       any apprehension about offerings due to the gods or debts which he owes
+       to men.  Now to this peace of mind the possession of wealth greatly
+       contributes; and therefore I say, that, setting one thing against
+       another, of the many advantages which wealth has to give, to a man of
+       sense this is in my opinion the greatest.
+
+       Well said, Cephalus, I replied; but as concerning justice, what is
+       it?--to speak the truth and to pay your debts--no more than this?  And
+       even to this are there not exceptions?  Suppose that a friend when in
+       his right mind has deposited arms with me and he asks for them when he
+       is not in his right mind, ought I to give them back to him?  No one
+       would say that I ought or that I should be right in doing so, any more
+       than they would say that I ought always to speak the truth to one who
+       is in his condition.
+
+       You are quite right, he replied.
+
+       But then, I said, speaking the truth and paying your debts is not a
+       correct definition of justice.
+
+       CEPHALUS - SOCRATES - POLEMARCHUS
+
+       Quite correct, Socrates, if Simonides is to be believed, said
+       Polemarchus interposing.
+
+       I fear, said Cephalus, that I must go now, for I have to look after the
+       sacrifices, and I hand over the argument to Polemarchus and the company.
+       EOF
+       git add text.txt &&
+       test_tick &&
+       git commit -m "Initial revision" &&
+
+       git checkout -b remote &&
+       sed -e "
+                       s/\.  /\. /g
+                       s/[?]  /? /g
+                       s/    / /g
+                       s/--/---/g
+                       s/but as concerning/but as con cerning/
+                       /CEPHALUS - SOCRATES - POLEMARCHUS/ d
+               " text.txt >text.txt+ &&
+       mv text.txt+ text.txt &&
+       git commit -a -m "Remove cruft" &&
+
+       git checkout master &&
+       sed -e "
+                       s/\(not in his right mind\),\(.*\)/\1;\2Q/
+                       s/Quite correct\(.*\)/It is too correct\1Q/
+                       s/unintentionally/un intentionally/
+                       /un intentionally/ s/$/Q/
+                       s/Polemarchus interposing./Polemarchus, interposing.Q/
+                       /justice and holiness/ s/$/Q/
+                       /pay your debts/ s/$/Q/
+               " text.txt | q_to_cr >text.txt+ &&
+       mv text.txt+ text.txt &&
+       git commit -a -m "Clarify" &&
+       git show-branch --all
+'
+
+test_expect_success 'naive merge fails' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive HEAD^ -- HEAD remote &&
+       test_must_fail git update-index --refresh &&
+       grep "<<<<<<" text.txt
+'
+
+test_expect_success '--ignore-space-change makes merge succeed' '
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote
+'
+
+test_expect_success 'naive cherry-pick fails' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git cherry-pick --no-commit remote &&
+       git read-tree --reset -u HEAD &&
+       test_must_fail git cherry-pick remote &&
+       test_must_fail git update-index --refresh &&
+       grep "<<<<<<" text.txt
+'
+
+test_expect_success '-Xignore-space-change makes cherry-pick succeed' '
+       git read-tree --reset -u HEAD &&
+       git cherry-pick --no-commit -Xignore-space-change remote
+'
+
+test_expect_success '--ignore-space-change: our w/s-only change wins' '
+       q_to_cr <<-\EOF >expected &&
+           justice and holiness and is the nurse of his age and theQ
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+       grep "justice and holiness" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-change: their real change wins over w/s' '
+       cat <<-\EOF >expected &&
+       it?---to speak the truth and to pay your debts---no more than this? And
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+       grep "pay your debts" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-change: does not ignore new spaces' '
+       cat <<-\EOF >expected1 &&
+       Well said, Cephalus, I replied; but as con cerning justice, what is
+       EOF
+       q_to_cr <<-\EOF >expected2 &&
+       un intentionally; and when he departs to the world below he is not inQ
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+       grep "Well said" text.txt >actual1 &&
+       grep "when he departs" text.txt >actual2 &&
+       test_cmp expected1 actual1 &&
+       test_cmp expected2 actual2
+'
+
+test_expect_success '--ignore-all-space drops their new spaces' '
+       cat <<-\EOF >expected &&
+       Well said, Cephalus, I replied; but as concerning justice, what is
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
+       grep "Well said" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-all-space keeps our new spaces' '
+       q_to_cr <<-\EOF >expected &&
+       un intentionally; and when he departs to the world below he is not inQ
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
+       grep "when he departs" text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-at-eol' '
+       q_to_cr <<-\EOF >expected &&
+       <<<<<<< HEAD
+       is not in his right mind; ought I to give them back to him?  No oneQ
+       =======
+       is not in his right mind, ought I to give them back to him? No one
+       >>>>>>> remote
+       EOF
+
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --ignore-space-at-eol \
+                                                HEAD^ -- HEAD remote &&
+       conflict_hunks text.txt >actual &&
+       test_cmp expected actual
+'
+
+test_done
diff --git a/t/t3034-merge-recursive-rename-options.sh b/t/t3034-merge-recursive-rename-options.sh
new file mode 100755 (executable)
index 0000000..b9c4028
--- /dev/null
@@ -0,0 +1,312 @@
+#!/bin/sh
+
+test_description='merge-recursive rename options
+
+Test rename detection by examining rename/delete conflicts.
+
+* (HEAD -> rename) rename
+| * (master) delete
+|/
+* base
+
+git diff --name-status base master
+D      0-old
+D      1-old
+D      2-old
+D      3-old
+
+git diff --name-status -M01 base rename
+R025    0-old   0-new
+R050    1-old   1-new
+R075    2-old   2-new
+R100    3-old   3-new
+
+Actual similarity indices are parsed from diff output. We rely on the fact that
+they are rounded down (see, e.g., Documentation/diff-generate-patch.txt, which
+mentions this in a different context).
+'
+
+. ./test-lib.sh
+
+get_expected_stages () {
+       git checkout rename -- $1-new &&
+       git ls-files --stage $1-new >expected-stages-undetected-$1 &&
+       sed "s/ 0       / 2     /" <expected-stages-undetected-$1 \
+               >expected-stages-detected-$1 &&
+       git read-tree -u --reset HEAD
+}
+
+rename_detected () {
+       git ls-files --stage $1-old $1-new >stages-actual-$1 &&
+       test_cmp expected-stages-detected-$1 stages-actual-$1
+}
+
+rename_undetected () {
+       git ls-files --stage $1-old $1-new >stages-actual-$1 &&
+       test_cmp expected-stages-undetected-$1 stages-actual-$1
+}
+
+check_common () {
+       git ls-files --stage >stages-actual &&
+       test_line_count = 4 stages-actual
+}
+
+check_threshold_0 () {
+       check_common &&
+       rename_detected 0 &&
+       rename_detected 1 &&
+       rename_detected 2 &&
+       rename_detected 3
+}
+
+check_threshold_1 () {
+       check_common &&
+       rename_undetected 0 &&
+       rename_detected 1 &&
+       rename_detected 2 &&
+       rename_detected 3
+}
+
+check_threshold_2 () {
+       check_common &&
+       rename_undetected 0 &&
+       rename_undetected 1 &&
+       rename_detected 2 &&
+       rename_detected 3
+}
+
+check_exact_renames () {
+       check_common &&
+       rename_undetected 0 &&
+       rename_undetected 1 &&
+       rename_undetected 2 &&
+       rename_detected 3
+}
+
+check_no_renames () {
+       check_common &&
+       rename_undetected 0 &&
+       rename_undetected 1 &&
+       rename_undetected 2 &&
+       rename_undetected 3
+}
+
+test_expect_success 'setup repo' '
+       cat <<-\EOF >3-old &&
+       33a
+       33b
+       33c
+       33d
+       EOF
+       sed s/33/22/ <3-old >2-old &&
+       sed s/33/11/ <3-old >1-old &&
+       sed s/33/00/ <3-old >0-old &&
+       git add [0-3]-old &&
+       git commit -m base &&
+       git rm [0-3]-old &&
+       git commit -m delete &&
+       git checkout -b rename HEAD^ &&
+       cp 3-old 3-new &&
+       sed 1,1s/./x/ <2-old >2-new &&
+       sed 1,2s/./x/ <1-old >1-new &&
+       sed 1,3s/./x/ <0-old >0-new &&
+       git add [0-3]-new &&
+       git rm [0-3]-old &&
+       git commit -m rename &&
+       get_expected_stages 0 &&
+       get_expected_stages 1 &&
+       get_expected_stages 2 &&
+       get_expected_stages 3 &&
+       check_50="false" &&
+       tail="HEAD^ -- HEAD master"
+'
+
+test_expect_success 'setup thresholds' '
+       git diff --name-status -M01 HEAD^ HEAD >diff-output &&
+       test_debug "cat diff-output" &&
+       test_line_count = 4 diff-output &&
+       grep "R[0-9][0-9][0-9]  \([0-3]\)-old   \1-new" diff-output \
+               >grep-output &&
+       test_cmp diff-output grep-output &&
+       th0=$(sed -n "s/R\(...\)        0-old   0-new/\1/p" <diff-output) &&
+       th1=$(sed -n "s/R\(...\)        1-old   1-new/\1/p" <diff-output) &&
+       th2=$(sed -n "s/R\(...\)        2-old   2-new/\1/p" <diff-output) &&
+       th3=$(sed -n "s/R\(...\)        3-old   3-new/\1/p" <diff-output) &&
+       test "$th0" -lt "$th1" &&
+       test "$th1" -lt "$th2" &&
+       test "$th2" -lt "$th3" &&
+       test "$th3" = 100 &&
+       if test 50 -le "$th0"
+       then
+               check_50=check_threshold_0
+       elif test 50 -le "$th1"
+       then
+               check_50=check_threshold_1
+       elif test 50 -le "$th2"
+       then
+               check_50=check_threshold_2
+       fi &&
+       th0="$th0%" &&
+       th1="$th1%" &&
+       th2="$th2%" &&
+       th3="$th3%"
+'
+
+test_expect_success 'assumption for tests: rename detection with diff' '
+       git diff --name-status -M$th0 --diff-filter=R HEAD^ HEAD \
+               >diff-output-0 &&
+       git diff --name-status -M$th1 --diff-filter=R HEAD^ HEAD \
+               >diff-output-1 &&
+       git diff --name-status -M$th2 --diff-filter=R HEAD^ HEAD \
+               >diff-output-2 &&
+       git diff --name-status -M100% --diff-filter=R HEAD^ HEAD \
+               >diff-output-3 &&
+       test_line_count = 4 diff-output-0 &&
+       test_line_count = 3 diff-output-1 &&
+       test_line_count = 2 diff-output-2 &&
+       test_line_count = 1 diff-output-3
+'
+
+test_expect_success 'default similarity threshold is 50%' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive $tail &&
+       $check_50
+'
+
+test_expect_success 'low rename threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=$th0 $tail &&
+       check_threshold_0
+'
+
+test_expect_success 'medium rename threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=$th1 $tail &&
+       check_threshold_1
+'
+
+test_expect_success 'high rename threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=$th2 $tail &&
+       check_threshold_2
+'
+
+test_expect_success 'exact renames only' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=100% $tail &&
+       check_exact_renames
+'
+
+test_expect_success 'rename threshold is truncated' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=200% $tail &&
+       check_exact_renames
+'
+
+test_expect_success 'disabled rename detection' '
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --no-renames $tail &&
+       check_no_renames
+'
+
+test_expect_success 'last wins in --find-renames=<m> --find-renames=<n>' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive \
+               --find-renames=$th0 --find-renames=$th2 $tail &&
+       check_threshold_2
+'
+
+test_expect_success '--find-renames resets threshold' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive \
+               --find-renames=$th0 --find-renames $tail &&
+       $check_50
+'
+
+test_expect_success 'last wins in --no-renames --find-renames' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --no-renames --find-renames $tail &&
+       $check_50
+'
+
+test_expect_success 'last wins in --find-renames --no-renames' '
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --find-renames --no-renames $tail &&
+       check_no_renames
+'
+
+test_expect_success 'assumption for further tests: trivial merge succeeds' '
+       git read-tree --reset -u HEAD &&
+       git merge-recursive HEAD -- HEAD HEAD &&
+       git diff --quiet --cached &&
+       git merge-recursive --find-renames=$th0 HEAD -- HEAD HEAD &&
+       git diff --quiet --cached &&
+       git merge-recursive --find-renames=$th2 HEAD -- HEAD HEAD &&
+       git diff --quiet --cached &&
+       git merge-recursive --find-renames=100% HEAD -- HEAD HEAD &&
+       git diff --quiet --cached &&
+       git merge-recursive --no-renames HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_expect_success '--find-renames rejects negative argument' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=-25 \
+               HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_expect_success '--find-renames rejects non-numbers' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --find-renames=0xf \
+               HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_expect_success 'rename-threshold=<n> is a synonym for find-renames=<n>' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --rename-threshold=$th0 $tail &&
+       check_threshold_0
+'
+
+test_expect_success 'last wins in --no-renames --rename-threshold=<n>' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --no-renames --rename-threshold=$th0 $tail &&
+       check_threshold_0
+'
+
+test_expect_success 'last wins in --rename-threshold=<n> --no-renames' '
+       git read-tree --reset -u HEAD &&
+       git merge-recursive --rename-threshold=$th0 --no-renames $tail &&
+       check_no_renames
+'
+
+test_expect_success '--rename-threshold=<n> rejects negative argument' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --rename-threshold=-25 \
+               HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_expect_success '--rename-threshold=<n> rejects non-numbers' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive --rename-threshold=0xf \
+               HEAD -- HEAD HEAD &&
+       git diff --quiet --cached
+'
+
+test_expect_success 'last wins in --rename-threshold=<m> --find-renames=<n>' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive \
+               --rename-threshold=$th0 --find-renames=$th2 $tail &&
+       check_threshold_2
+'
+
+test_expect_success 'last wins in --find-renames=<m> --rename-threshold=<n>' '
+       git read-tree --reset -u HEAD &&
+       test_must_fail git merge-recursive \
+               --find-renames=$th2 --rename-threshold=$th0 $tail &&
+       check_threshold_0
+'
+
+test_done
index cdaf6f64ec78830def2f082b72604075e15496f6..a897248490650ebef1a33a60a1aa9cf4431f06ff 100755 (executable)
@@ -446,6 +446,13 @@ test_expect_success '--set-upstream-to fails on a non-ref' '
        test_must_fail git branch --set-upstream-to HEAD^{}
 '
 
+test_expect_success '--set-upstream-to fails on locked config' '
+       test_when_finished "rm -f .git/config.lock" &&
+       >.git/config.lock &&
+       git branch locked &&
+       test_must_fail git branch --set-upstream-to locked
+'
+
 test_expect_success 'use --set-upstream-to modify HEAD' '
        test_config branch.master.remote foo &&
        test_config branch.master.merge foo &&
@@ -466,6 +473,13 @@ test_expect_success '--unset-upstream should fail if given a non-existent branch
        test_must_fail git branch --unset-upstream i-dont-exist
 '
 
+test_expect_success '--unset-upstream should fail if config is locked' '
+       test_when_finished "rm -f .git/config.lock" &&
+       git branch --set-upstream-to locked &&
+       >.git/config.lock &&
+       test_must_fail git branch --unset-upstream
+'
+
 test_expect_success 'test --unset-upstream on HEAD' '
        git branch my14 &&
        test_config branch.master.remote foo &&
@@ -579,7 +593,7 @@ test_expect_success 'avoid ambiguous track' '
        git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
        git config remote.ambi2.url lilili &&
        git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
-       git branch all1 master &&
+       test_must_fail git branch all1 master &&
        test -z "$(git config branch.all1.merge)"
 '
 
index 0dcc752076a9e42b0cff982d7b0f42c63f85dee2..dd2e6ce34e7dff0e59e917fd4e3aefa01181778c 100755 (executable)
@@ -144,6 +144,39 @@ test_expect_success 'remove remote protects local branches' '
        )
 '
 
+test_expect_success 'remove errors out early when deleting non-existent branch' '
+       (
+               cd test &&
+               echo "fatal: No such remote: foo" >expect &&
+               test_must_fail git remote rm foo 2>actual &&
+               test_i18ncmp expect actual
+       )
+'
+
+test_expect_success 'rename errors out early when deleting non-existent branch' '
+       (
+               cd test &&
+               echo "fatal: No such remote: foo" >expect &&
+               test_must_fail git remote rename foo bar 2>actual &&
+               test_i18ncmp expect actual
+       )
+'
+
+test_expect_success 'add existing foreign_vcs remote' '
+       test_config remote.foo.vcs bar &&
+       echo "fatal: remote foo already exists." >expect &&
+       test_must_fail git remote add foo bar 2>actual &&
+       test_i18ncmp expect actual
+'
+
+test_expect_success 'add existing foreign_vcs remote' '
+       test_config remote.foo.vcs bar &&
+       test_config remote.bar.vcs bar &&
+       echo "fatal: remote bar already exists." >expect &&
+       test_must_fail git remote rename foo bar 2>actual &&
+       test_i18ncmp expect actual
+'
+
 cat >test/expect <<EOF
 * remote origin
   Fetch URL: $(pwd)/one
@@ -937,6 +970,15 @@ test_expect_success 'get-url on new remote' '
        echo foo | get_url_test --push --all someremote
 '
 
+test_expect_success 'remote set-url with locked config' '
+       test_when_finished "rm -f .git/config.lock" &&
+       git config --get-all remote.someremote.url >expect &&
+       >.git/config.lock &&
+       test_must_fail git remote set-url someremote baz &&
+       git config --get-all remote.someremote.url >actual &&
+       cmp expect actual
+'
+
 test_expect_success 'remote set-url bar' '
        git remote set-url someremote bar &&
        echo bar >expect &&
diff --git a/t/t6133-pathspec-rev-dwim.sh b/t/t6133-pathspec-rev-dwim.sh
new file mode 100755 (executable)
index 0000000..a290ffc
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+test_description='test dwim of revs versus pathspecs in revision parser'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+       test_commit base &&
+       echo content >"br[ack]ets" &&
+       git add . &&
+       test_tick &&
+       git commit -m brackets
+'
+
+test_expect_success 'non-rev wildcard dwims to pathspec' '
+       git log -- "*.t" >expect &&
+       git log    "*.t" >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'tree:path with metacharacters dwims to rev' '
+       git show "HEAD:br[ack]ets" -- >expect &&
+       git show "HEAD:br[ack]ets"    >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success '^{foo} with metacharacters dwims to rev' '
+       git log "HEAD^{/b.*}" -- >expect &&
+       git log "HEAD^{/b.*}"    >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success '@{foo} with metacharacters dwims to rev' '
+       git log "HEAD@{now [or thereabouts]}" -- >expect &&
+       git log "HEAD@{now [or thereabouts]}"    >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success ':/*.t from a subdir dwims to a pathspec' '
+       mkdir subdir &&
+       (
+               cd subdir &&
+               git log -- ":/*.t" >expect &&
+               git log    ":/*.t" >actual &&
+               test_cmp expect actual
+       )
+'
+
+test_done
index fe4796cc9c8b91210f98af8703f1b58e32e757c3..bcf472bf51999f3fd1a043b5d44601156d5c8049 100755 (executable)
@@ -133,6 +133,48 @@ test_expect_success 'right alignment' '
        test_cmp expect actual
 '
 
+cat >expect <<-\EOF
+|       refname is refs/heads/master       |refs/heads/master
+|        refname is refs/heads/side        |refs/heads/side
+|         refname is refs/odd/spot         |refs/odd/spot
+|     refname is refs/tags/double-tag      |refs/tags/double-tag
+|        refname is refs/tags/four         |refs/tags/four
+|         refname is refs/tags/one         |refs/tags/one
+|     refname is refs/tags/signed-tag      |refs/tags/signed-tag
+|        refname is refs/tags/three        |refs/tags/three
+|         refname is refs/tags/two         |refs/tags/two
+EOF
+
+test_align_permutations() {
+       while read -r option
+       do
+               test_expect_success "align:$option" '
+                       git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
+                       test_cmp expect actual
+               '
+       done
+}
+
+test_align_permutations <<-\EOF
+       middle,42
+       42,middle
+       position=middle,42
+       42,position=middle
+       middle,width=42
+       width=42,middle
+       position=middle,width=42
+       width=42,position=middle
+EOF
+
+# Last one wins (silently) when multiple arguments of the same type are given
+
+test_align_permutations <<-\EOF
+       32,width=42,middle
+       width=30,42,middle
+       width=42,position=right,middle
+       42,right,position=middle
+EOF
+
 # Individual atoms inside %(align:...) and %(end) must not be quoted.
 
 test_expect_success 'alignment with format quote' "
index b146406e9c0912cdeb1a9c5bc93d41b981e971ca..9c9c378119defa7884a21beb27e806ddf1171497 100755 (executable)
@@ -141,7 +141,8 @@ test_expect_success 'grep respects not-binary diff attribute' '
        test_cmp expect actual &&
        echo "b diff" >.gitattributes &&
        echo "b:binQary" >expect &&
-       git grep bin b | nul_to_q >actual &&
+       git grep bin b >actual.raw &&
+       nul_to_q <actual.raw >actual &&
        test_cmp expect actual
 '
 
index 540771ca419b82e4dd32adf986545515fa1db66e..be82a75e542a7b7d8dab259da4e64dd265019dd1 100755 (executable)
@@ -999,5 +999,30 @@ test_expect_success 'submodule add clone shallow submodule' '
        )
 '
 
+test_expect_success 'submodule helper list is not confused by common prefixes' '
+       mkdir -p dir1/b &&
+       (
+               cd dir1/b &&
+               git init &&
+               echo hi >testfile2 &&
+               git add . &&
+               git commit -m "test1"
+       ) &&
+       mkdir -p dir2/b &&
+       (
+               cd dir2/b &&
+               git init &&
+               echo hello >testfile1 &&
+               git add .  &&
+               git commit -m "test2"
+       ) &&
+       git submodule add /dir1/b dir1/b &&
+       git submodule add /dir2/b dir2/b &&
+       git commit -m "first submodule commit" &&
+       git submodule--helper list dir1/b |cut -c51- >actual &&
+       echo "dir1/b" >expect &&
+       test_cmp expect actual
+'
+
 
 test_done
diff --git a/t/t7409-submodule-detached-work-tree.sh b/t/t7409-submodule-detached-work-tree.sh
new file mode 100755 (executable)
index 0000000..c207171
--- /dev/null
@@ -0,0 +1,84 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Daniel GraƱa
+#
+
+test_description='Test submodules on detached working tree
+
+This test verifies that "git submodule" initialization, update and addition works
+on detahced working trees
+'
+
+TEST_NO_CREATE_REPO=1
+. ./test-lib.sh
+
+test_expect_success 'submodule on detached working tree' '
+       git init --bare remote &&
+       test_create_repo bundle1 &&
+       (
+               cd bundle1 &&
+               test_commit "shoot" &&
+               git rev-parse --verify HEAD >../expect
+       ) &&
+       mkdir home &&
+       (
+               cd home &&
+               GIT_WORK_TREE="$(pwd)" &&
+               GIT_DIR="$(pwd)/.dotfiles" &&
+               export GIT_WORK_TREE GIT_DIR &&
+               git clone --bare ../remote .dotfiles &&
+               git submodule add ../bundle1 .vim/bundle/sogood &&
+               test_commit "sogood" &&
+               (
+                       unset GIT_WORK_TREE GIT_DIR &&
+                       cd .vim/bundle/sogood &&
+                       git rev-parse --verify HEAD >actual &&
+                       test_cmp ../../../../expect actual
+               ) &&
+               git push origin master
+       ) &&
+       mkdir home2 &&
+       (
+               cd home2 &&
+               git clone --bare ../remote .dotfiles &&
+               GIT_WORK_TREE="$(pwd)" &&
+               GIT_DIR="$(pwd)/.dotfiles" &&
+               export GIT_WORK_TREE GIT_DIR &&
+               git checkout master &&
+               git submodule update --init &&
+               (
+                       unset GIT_WORK_TREE GIT_DIR &&
+                       cd .vim/bundle/sogood &&
+                       git rev-parse --verify HEAD >actual &&
+                       test_cmp ../../../../expect actual
+               )
+       )
+'
+
+test_expect_success 'submodule on detached working pointed by core.worktree' '
+       mkdir home3 &&
+       (
+               cd home3 &&
+               GIT_DIR="$(pwd)/.dotfiles" &&
+               export GIT_DIR &&
+               git clone --bare ../remote "$GIT_DIR" &&
+               git config core.bare false &&
+               git config core.worktree .. &&
+               git checkout master &&
+               git submodule add ../bundle1 .vim/bundle/dupe &&
+               test_commit "dupe" &&
+               git push origin master
+       ) &&
+       (
+               cd home &&
+               GIT_DIR="$(pwd)/.dotfiles" &&
+               export GIT_DIR &&
+               git config core.bare false &&
+               git config core.worktree .. &&
+               git pull &&
+               git submodule update --init &&
+               test -f .vim/bundle/dupe/shoot.t
+       )
+'
+
+test_done
diff --git a/t/t7409-submodule-detached-worktree.sh b/t/t7409-submodule-detached-worktree.sh
deleted file mode 100755 (executable)
index c207171..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2012 Daniel GraƱa
-#
-
-test_description='Test submodules on detached working tree
-
-This test verifies that "git submodule" initialization, update and addition works
-on detahced working trees
-'
-
-TEST_NO_CREATE_REPO=1
-. ./test-lib.sh
-
-test_expect_success 'submodule on detached working tree' '
-       git init --bare remote &&
-       test_create_repo bundle1 &&
-       (
-               cd bundle1 &&
-               test_commit "shoot" &&
-               git rev-parse --verify HEAD >../expect
-       ) &&
-       mkdir home &&
-       (
-               cd home &&
-               GIT_WORK_TREE="$(pwd)" &&
-               GIT_DIR="$(pwd)/.dotfiles" &&
-               export GIT_WORK_TREE GIT_DIR &&
-               git clone --bare ../remote .dotfiles &&
-               git submodule add ../bundle1 .vim/bundle/sogood &&
-               test_commit "sogood" &&
-               (
-                       unset GIT_WORK_TREE GIT_DIR &&
-                       cd .vim/bundle/sogood &&
-                       git rev-parse --verify HEAD >actual &&
-                       test_cmp ../../../../expect actual
-               ) &&
-               git push origin master
-       ) &&
-       mkdir home2 &&
-       (
-               cd home2 &&
-               git clone --bare ../remote .dotfiles &&
-               GIT_WORK_TREE="$(pwd)" &&
-               GIT_DIR="$(pwd)/.dotfiles" &&
-               export GIT_WORK_TREE GIT_DIR &&
-               git checkout master &&
-               git submodule update --init &&
-               (
-                       unset GIT_WORK_TREE GIT_DIR &&
-                       cd .vim/bundle/sogood &&
-                       git rev-parse --verify HEAD >actual &&
-                       test_cmp ../../../../expect actual
-               )
-       )
-'
-
-test_expect_success 'submodule on detached working pointed by core.worktree' '
-       mkdir home3 &&
-       (
-               cd home3 &&
-               GIT_DIR="$(pwd)/.dotfiles" &&
-               export GIT_DIR &&
-               git clone --bare ../remote "$GIT_DIR" &&
-               git config core.bare false &&
-               git config core.worktree .. &&
-               git checkout master &&
-               git submodule add ../bundle1 .vim/bundle/dupe &&
-               test_commit "dupe" &&
-               git push origin master
-       ) &&
-       (
-               cd home &&
-               GIT_DIR="$(pwd)/.dotfiles" &&
-               export GIT_DIR &&
-               git config core.bare false &&
-               git config core.worktree .. &&
-               git pull &&
-               git submodule update --init &&
-               test -f .vim/bundle/dupe/shoot.t
-       )
-'
-
-test_done
index 847d098c094566aafd8dd05fd3962e28f113360e..75da219ed1be95c8279b363816d46bc3e2686fc3 100755 (executable)
@@ -33,11 +33,15 @@ author $SJIS_NAME
 summary $SJIS_MSG
 EOF
 
+filter_author_summary () {
+       sed -n -e '/^author /p' -e '/^summary /p' "$@"
+}
+
 test_expect_success !MINGW \
        'blame respects i18n.commitencoding' '
-       git blame --incremental file | \
-               egrep "^(author|summary) " > actual &&
-       test_cmp actual expected
+       git blame --incremental file >output &&
+       filter_author_summary output >actual &&
+       test_cmp expected actual
 '
 
 cat >expected <<EOF
@@ -52,9 +56,9 @@ EOF
 test_expect_success !MINGW \
        'blame respects i18n.logoutputencoding' '
        git config i18n.logoutputencoding eucJP &&
-       git blame --incremental file | \
-               egrep "^(author|summary) " > actual &&
-       test_cmp actual expected
+       git blame --incremental file >output &&
+       filter_author_summary output >actual &&
+       test_cmp expected actual
 '
 
 cat >expected <<EOF
@@ -68,9 +72,9 @@ EOF
 
 test_expect_success !MINGW \
        'blame respects --encoding=UTF-8' '
-       git blame --incremental --encoding=UTF-8 file | \
-               egrep "^(author|summary) " > actual &&
-       test_cmp actual expected
+       git blame --incremental --encoding=UTF-8 file >output &&
+       filter_author_summary output >actual &&
+       test_cmp expected actual
 '
 
 cat >expected <<EOF
@@ -84,9 +88,9 @@ EOF
 
 test_expect_success !MINGW \
        'blame respects --encoding=none' '
-       git blame --incremental --encoding=none file | \
-               egrep "^(author|summary) " > actual &&
-       test_cmp actual expected
+       git blame --incremental --encoding=none file >output &&
+       filter_author_summary output >actual &&
+       test_cmp expected actual
 '
 
 test_done
index 5cfb9cfc52e715c916f3ebe922e57263d7c4d7da..bb879a527d94e7329dccef15525421917676f56b 100755 (executable)
@@ -35,7 +35,7 @@ exit 1
 
 check_entries () {
        # $1 == directory, $2 == expected
-       grep '^/' "$1/CVS/Entries" | sort | cut -d/ -f2,3,5 >actual
+       sed -ne '/^\//p' "$1/CVS/Entries" | sort | cut -d/ -f2,3,5 >actual
        if test -z "$2"
        then
                >expected
index 6232dfe661a53d1eaf17ead5133da5792ae2413f..ba805b374c57a4e5ad2e6e4a4b9071a11c165afa 100644 (file)
@@ -8,21 +8,14 @@
  */
 static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
 {
-       const char *ceil = item->string;
-       int len = strlen(ceil);
-       char buf[PATH_MAX+1];
+       char *ceil = item->string;
 
-       if (len == 0)
+       if (!*ceil)
                die("Empty path is not supported");
-       if (len > PATH_MAX)
-               die("Path \"%s\" is too long", ceil);
        if (!is_absolute_path(ceil))
                die("Path \"%s\" is not absolute", ceil);
-       if (normalize_path_copy(buf, ceil) < 0)
+       if (normalize_path_copy(ceil, ceil) < 0)
                die("Path \"%s\" could not be normalized", ceil);
-       len = strlen(buf);
-       free(item->string);
-       item->string = xstrdup(buf);
        return 1;
 }
 
@@ -166,7 +159,7 @@ static struct test_data dirname_data[] = {
 int main(int argc, char **argv)
 {
        if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
-               char *buf = xmalloc(PATH_MAX + 1);
+               char *buf = xmallocz(strlen(argv[2]));
                int rv = normalize_path_copy(buf, argv[2]);
                if (rv)
                        buf = "++failed++";
index a6bff8b30811c649c82afcdc9e018af1a34ce13c..b934183236ca571093f55818f5018c278150e274 100644 (file)
@@ -321,6 +321,21 @@ static void standard_options(struct transport *t)
        if (n >= sizeof(buf))
                die("impossibly large verbosity value");
        set_helper_option(t, "verbosity", buf);
+
+       switch (t->family) {
+       case TRANSPORT_FAMILY_ALL:
+               /*
+                * this is already the default,
+                * do not break old remote helpers by setting "all" here
+                */
+               break;
+       case TRANSPORT_FAMILY_IPV4:
+               set_helper_option(t, "family", "ipv4");
+               break;
+       case TRANSPORT_FAMILY_IPV6:
+               set_helper_option(t, "family", "ipv6");
+               break;
+       }
 }
 
 static int release_helper(struct transport *transport)
index c92f8ae7054e3340cf1a40c3f74c70b7aab7303a..ca3cfa4b00d857603e6c72536fa6905a7ee2905f 100644 (file)
@@ -163,6 +163,12 @@ static int connect_setup(struct transport *transport, int for_push)
        if (data->conn)
                return 0;
 
+       switch (transport->family) {
+       case TRANSPORT_FAMILY_ALL: break;
+       case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
+       case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
+       }
+
        data->conn = git_connect(data->fd, transport->url,
                                 for_push ? data->options.receivepack :
                                 data->options.uploadpack,
@@ -977,7 +983,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
                 * This condition shouldn't be met in a non-deepening fetch
                 * (see builtin/fetch.c:quickfetch()).
                 */
-               heads = xmalloc(nr_refs * sizeof(*heads));
+               ALLOC_ARRAY(heads, nr_refs);
                for (rm = refs; rm; rm = rm->next)
                        heads[nr_heads++] = rm;
        }
@@ -1021,7 +1027,7 @@ int transport_disconnect(struct transport *transport)
  */
 char *transport_anonymize_url(const char *url)
 {
-       char *anon_url, *scheme_prefix, *anon_part;
+       char *scheme_prefix, *anon_part;
        size_t anon_len, prefix_len = 0;
 
        anon_part = strchr(url, '@');
@@ -1055,10 +1061,8 @@ char *transport_anonymize_url(const char *url)
                        goto literal_copy;
                prefix_len = scheme_prefix - url + 3;
        }
-       anon_url = xcalloc(1, 1 + prefix_len + anon_len);
-       memcpy(anon_url, url, prefix_len);
-       memcpy(anon_url + prefix_len, anon_part, anon_len);
-       return anon_url;
+       return xstrfmt("%.*s%.*s", (int)prefix_len, url,
+                      (int)anon_len, anon_part);
 literal_copy:
        return xstrdup(url);
 }
index 8ebaaf2cae054bcdfea491fc464f8bf3180b1e03..c68140892c6258925104f7dda385e635fcf95e20 100644 (file)
@@ -18,6 +18,12 @@ struct git_transport_options {
        struct push_cas_option *cas;
 };
 
+enum transport_family {
+       TRANSPORT_FAMILY_ALL = 0,
+       TRANSPORT_FAMILY_IPV4,
+       TRANSPORT_FAMILY_IPV6
+};
+
 struct transport {
        struct remote *remote;
        const char *url;
@@ -110,6 +116,8 @@ struct transport {
         * actually turns out to be smart.
         */
        struct git_transport_options *smart_options;
+
+       enum transport_family family;
 };
 
 #define TRANSPORT_PUSH_ALL 1
index 290a1da4ce5c3504dd75e584030a4a00fbdbe9a2..4dda9a14abacbf38ba19fcf89b2ef54576a26538 100644 (file)
@@ -124,8 +124,8 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
        unsigned mode, const unsigned char *sha1)
 {
        struct combine_diff_path *p;
-       int len = base->len + pathlen;
-       int alloclen = combine_diff_path_size(nparent, len);
+       size_t len = st_add(base->len, pathlen);
+       size_t alloclen = combine_diff_path_size(nparent, len);
 
        /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
        p = last->next;
index 4a7e78ffbcc6d552a39dcccd9008d6c11919e432..2ef0ce545278ea2f40526c42303c541736643bd5 100644 (file)
@@ -23,6 +23,10 @@ int userdiff_config(const char *k, const char *v);
 struct userdiff_driver *userdiff_find_by_name(const char *name);
 struct userdiff_driver *userdiff_find_by_path(const char *path);
 
+/*
+ * Initialize any textconv-related fields in the driver and return it, or NULL
+ * if it does not have textconv enabled at all.
+ */
 struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver);
 
 #endif /* USERDIFF */
index 29a45d2654e982dbbb3fc328e16c01713d8a7fb5..9afc1a021c224d4ff2230fffeb0e4f0882d4a99e 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -152,6 +152,9 @@ void *xcalloc(size_t nmemb, size_t size)
 {
        void *ret;
 
+       if (unsigned_mult_overflows(nmemb, size))
+               die("data too large to fit into virtual memory space");
+
        memory_limit_check(size * nmemb, 0);
        ret = calloc(nmemb, size);
        if (!ret && (!nmemb || !size))
index cb67c1c42b35e412dccf9a13ad18dde727ab8ce6..54236f24b9786710f91650ac63f6004cdeb012e6 100644 (file)
@@ -265,7 +265,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
        for (i = 0, regs->nr = 1; value[i]; i++)
                if (value[i] == '\n')
                        regs->nr++;
-       regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
+       ALLOC_ARRAY(regs->array, regs->nr);
        for (i = 0; i < regs->nr; i++) {
                struct ff_reg *reg = regs->array + i;
                const char *ep = strchr(value, '\n'), *expression;
index c0339919ccf78c4fe7d78123f9c3e891075d0602..4fb7e79410c22fba1fb390af2e09008e932f5ea8 100644 (file)
@@ -42,7 +42,6 @@ extern "C" {
 #define XDF_IGNORE_BLANK_LINES (1 << 7)
 
 #define XDL_EMIT_FUNCNAMES (1 << 0)
-#define XDL_EMIT_COMMON (1 << 1)
 #define XDL_EMIT_FUNCCONTEXT (1 << 2)
 
 #define XDL_MMB_READONLY (1 << 0)
index 4266ada23f3c37c1989c7fa4283b423d16c0891b..993724b11c40bacffee8df927018e5790a265bd4 100644 (file)
@@ -120,21 +120,6 @@ static long def_ff(const char *rec, long len, char *buf, long sz, void *priv)
        return -1;
 }
 
-static int xdl_emit_common(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
-                           xdemitconf_t const *xecfg) {
-       xdfile_t *xdf = &xe->xdf2;
-       const char *rchg = xdf->rchg;
-       long ix;
-
-       for (ix = 0; ix < xdf->nrec; ix++) {
-               if (rchg[ix])
-                       continue;
-               if (xdl_emit_record(xdf, ix, "", ecb))
-                       return -1;
-       }
-       return 0;
-}
-
 struct func_line {
        long len;
        char buf[80];
@@ -170,9 +155,6 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
        long funclineprev = -1;
        struct func_line func_line = { 0 };
 
-       if (xecfg->flags & XDL_EMIT_COMMON)
-               return xdl_emit_common(xe, xscr, ecb, xecfg);
-
        for (xch = xscr; xch; xch = xche->next) {
                xche = xdl_get_hunk(&xch, xecfg);
                if (!xch)
index d98f430c912bead3ee5381eafd195dde4d709c47..f338ad6c757cda29a052960a504715c062ab5dda 100644 (file)
@@ -641,8 +641,11 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
        result->ptr = NULL;
        result->size = 0;
 
-       if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 ||
-                       xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
+       if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0) {
+               return -1;
+       }
+       if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
+               xdl_free_env(&xe1);
                return -1;
        }
        if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 ||
@@ -654,6 +657,8 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
        if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 ||
            xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 ||
            xdl_build_script(&xe2, &xscr2) < 0) {
+               xdl_free_script(xscr1);
+               xdl_free_env(&xe1);
                xdl_free_env(&xe2);
                return -1;
        }