gitweb.git
write_shared_index(): use tempfile moduleMichael Haggerty Mon, 10 Aug 2015 09:47:45 +0000 (11:47 +0200)

write_shared_index(): use tempfile module

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

register_tempfile(): new function to handle an existing... Michael Haggerty Mon, 10 Aug 2015 09:47:44 +0000 (11:47 +0200)

register_tempfile(): new function to handle an existing temporary file

Allow an existing file to be registered with the tempfile-handling
infrastructure; in particular, arrange for it to be deleted on program
exit. This can be used if the temporary file has to be created in a
more complicated way than just open(). For example:

* If the file itself needs to be created via the lockfile API
* If it is not a regular file (e.g., a socket)

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

tempfile: add several functions for creating temporary... Michael Haggerty Mon, 10 Aug 2015 09:47:43 +0000 (11:47 +0200)

tempfile: add several functions for creating temporary files

Add several functions for creating temporary files with
automatically-generated names, analogous to mkstemps(), but also
arranging for the files to be deleted on program exit.

The functions are named according to a pattern depending how they
operate. They will be used to replace many places in the code where
temporary files are created and cleaned up ad-hoc.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

prepare_tempfile_object(): new function, extracted... Michael Haggerty Mon, 10 Aug 2015 09:47:42 +0000 (11:47 +0200)

prepare_tempfile_object(): new function, extracted from create_tempfile()

This makes the next step easier.

The old code used to use "path" to set the initial length of
tempfile->filename. This was not helpful because path was usually
relative whereas the value stored to filename will be absolute. So
just initialize the length to 0.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

tempfile: a new module for handling temporary filesMichael Haggerty Mon, 10 Aug 2015 09:47:41 +0000 (11:47 +0200)

tempfile: a new module for handling temporary files

A lot of work went into defining the state diagram for lockfiles and
ensuring correct, race-resistant cleanup in all circumstances.

Most of that infrastructure can be applied directly to *any* temporary
file. So extract a new "tempfile" module from the "lockfile" module.
Reimplement lockfile on top of tempfile.

Subsequent commits will add more users of the new module.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

commit_lock_file(): use get_locked_file_path()Michael Haggerty Mon, 10 Aug 2015 09:47:40 +0000 (11:47 +0200)

commit_lock_file(): use get_locked_file_path()

First beef up the sanity checking in get_locked_file_path() to match
that in commit_lock_file(). Then rewrite commit_lock_file() to use
get_locked_file_path() for its pathname computation.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

lockfile: add accessor get_lock_file_path()Michael Haggerty Mon, 10 Aug 2015 09:47:39 +0000 (11:47 +0200)

lockfile: add accessor get_lock_file_path()

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

lockfile: add accessors get_lock_file_fd() and get_lock... Michael Haggerty Mon, 10 Aug 2015 09:47:38 +0000 (11:47 +0200)

lockfile: add accessors get_lock_file_fd() and get_lock_file_fp()

We are about to move those members, so change client code to read them
through accessor functions.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

create_bundle(): duplicate file descriptor to avoid... Michael Haggerty Mon, 10 Aug 2015 09:47:37 +0000 (11:47 +0200)

create_bundle(): duplicate file descriptor to avoid closing it twice

write_pack_data() passes bundle_fd to start_command() to be used as
the stdout of pack-objects. But start_command() closes its stdout if
it is > 1. This is a problem if bundle_fd is the fd of a lock_file,
because commit_lock_file() will also try to close the fd.

So the old code suppressed commit_lock_file()'s usual behavior of
closing the file descriptor by setting the lock_file object's fd field
to -1.

But this is not really kosher. Code here shouldn't be mutating fields
within the lock_file object.

Instead, duplicate the file descriptor before passing it to
write_pack_data(). Then that function can close its copy without
closing the copy held in the lock_file object.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

lockfile: move documentation to lockfile.h and lockfile.cMichael Haggerty Mon, 10 Aug 2015 09:47:36 +0000 (11:47 +0200)

lockfile: move documentation to lockfile.h and lockfile.c

Rearrange/rewrite it somewhat to fit its new environment.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

strbuf_read(): skip unnecessary strbuf_grow() at eofJim Hill Sun, 31 May 2015 18:16:45 +0000 (11:16 -0700)

strbuf_read(): skip unnecessary strbuf_grow() at eof

The loop in strbuf_read() uses xread() repeatedly while extending
the strbuf until the call returns zero. If the buffer is
sufficiently large to begin with, this results in xread()
returning the remainder of the file to the end (returning
non-zero), the loop extending the strbuf, and then making another
call to xread() to have it return zero.

By using read_in_full(), we can tell when the read reached the end
of file: when it returns less than was requested, it's eof. This
way we can avoid an extra iteration that allocates an extra 8kB
that is never used.

Signed-off-by: Jim Hill <gjthill@gmail.com>
Reviewed-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

sha1_file.c: rename move_temp_to_file() to finalize_obj... Junio C Hamano Fri, 7 Aug 2015 21:40:24 +0000 (14:40 -0700)

sha1_file.c: rename move_temp_to_file() to finalize_object_file()

Since 5a688fe4 ("core.sharedrepository = 0mode" should set, not
loosen, 2009-03-25), we kept reminding ourselves:

NEEDSWORK: this should be renamed to finalize_temp_file() as
"moving" is only a part of what it does, when no patch between
master to pu changes the call sites of this function.

without doing anything about it. Let's do so.

The purpose of this function was not to move but to finalize. The
detail of the primarily implementation of finalizing was to link the
temporary file to its final name and then to unlink, which wasn't
even "moving". The alternative implementation did "move" by calling
rename(2), which is a fun tangent.

Signed-off-by: Junio C Hamano <gitster@pobox.com>

clone: abort if no dir name could be guessedPatrick Steinhardt Mon, 10 Aug 2015 15:48:25 +0000 (17:48 +0200)

clone: abort if no dir name could be guessed

Due to various components of the URI being stripped off it may
happen that we fail to guess a directory name. We currently error
out with a message that it is impossible to create the working
tree '' in such cases. Instead, error out early with a sensible
error message hinting that a directory name should be specified
manually on the command line.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

clone: do not use port number as dir namePatrick Steinhardt Mon, 10 Aug 2015 15:48:24 +0000 (17:48 +0200)

clone: do not use port number as dir name

If the URI contains a port number and the URI's path component is
empty we fail to guess a sensible directory name. E.g. cloning a
repository 'ssh://example.com:2222/' we guess a directory name
'2222' where we would want the hostname only, e.g. 'example.com'.

We need to take care to not drop trailing port-like numbers in
certain cases. E.g. when cloning a repository 'foo/bar:2222.git'
we want to guess the directory name '2222' instead of 'bar'.
Thus, we have to first check the stripped URI for path separators
and only strip port numbers if there are path separators present.
This heuristic breaks when cloning a repository 'bar:2222.git',
though.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

clone: do not include authentication data in guessed dirPatrick Steinhardt Mon, 10 Aug 2015 15:48:23 +0000 (17:48 +0200)

clone: do not include authentication data in guessed dir

If the URI contains authentication data and the URI's path
component is empty, we fail to guess a sensible directory name.
E.g. cloning a repository 'ssh://user:password@example.com/' we
guess a directory name 'password@example.com' where we would want
the hostname only, e.g. 'example.com'.

The naive way of just adding '@' as a path separator would break
cloning repositories like 'foo/bar@baz.git' (which would
currently become 'bar@baz' but would then become 'baz' only).
Instead fix this by first dropping the scheme and then greedily
scanning for an '@' sign until we find the first path separator.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

clone: use computed length in guess_dir_nameJeff King Mon, 10 Aug 2015 15:48:22 +0000 (17:48 +0200)

clone: use computed length in guess_dir_name

Commit 7e837c6 (clone: simplify string handling in
guess_dir_name(), 2015-07-09) changed clone to use
strip_suffix instead of hand-rolled pointer manipulation.
However, strip_suffix will strip from the end of a
NUL-terminated string, and we may have already stripped some
characters (like directory separators, or "/.git"). This
leads to commands like:

git clone host:foo.git/

failing to strip the ".git".

We must instead convert our pointer arithmetic into a
computed length and feed that to strip_suffix_mem, which will
then reduce the length further for us.

It would be nicer if we could drop the pointer manipulation
entirely, and just continually strip using strip_suffix. But
that doesn't quite work for two reasons:

1. The early suffixes we're stripping are not constant; we
need to look for is_dir_sep, which could be one of
several characters.

2. Mid-way through the stripping we compute the pointer
"start", which shows us the beginning of the pathname.
Which really give us two lengths to work with: the
offset from the start of the string, and from the start
of the path. By using pointers for the early part, we
can just compute the length from "start" when we need
it.

Signed-off-by: Jeff King <peff@peff.net>
Acked-by: Sebastian Schuberth <sschuberth@gmail.com>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

clone: add tests for output directoryJeff King Mon, 10 Aug 2015 15:48:21 +0000 (17:48 +0200)

clone: add tests for output directory

When we run "git clone $url", clone guesses from the $url
what to name the local output directory. We don't have any
test coverage of this, so let's add some basic tests.

This reveals a few problems:

- cloning "foo.git/" does not properly remove the ".git";
this is a recent regression from 7e837c6 (clone:
simplify string handling in guess_dir_name(), 2015-07-09)

- likewise, cloning foo/.git does not seem to handle the
bare case (we should end up in foo.git, but we try to
use foo/.git on the local end), which also comes from
7e837c6.

- cloning the root is not very smart about URL parsing,
and usernames and port numbers may end up in the
directory name

All of these tests are marked as failures.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

test-lib: disable trace when test is not verboseJeff King Thu, 6 Aug 2015 05:33:57 +0000 (01:33 -0400)

test-lib: disable trace when test is not verbose

The "-x" test-script option turns on the shell's "-x"
tracing, which can help show why a particular test is
failing. Unfortunately, this can create false negatives in
some tests if they invoke a shell function with its stderr
redirected. t5512.10 is such a test, as it does:

test_must_fail git ls-remote refs*master >actual 2>&1 &&
test_cmp exp actual

The "actual" file gets the "-x" trace for the test_must_fail
function, which prevents it from matching the expected
output.

There's no way to avoid this without managing the
trace flag inside each sub-function, which isn't really a
workable solution. But unless you specifically care about
t5512.10, we can work around it by enabling tracing only for
the specific tests we want.

You can already do:

./t5512-ls-remote.sh -x --verbose-only=16

to see the trace only for a specific test. But that doesn't
_disable_ the tracing in the other tests; it just sends it
to /dev/null. However, there's no point in generating a
trace that the user won't see, so we can simply disable
tracing whenever it doesn't have a matching verbose flag.

The normal case of just "./t5512-ls-remote.sh -x" stays the
same, as "-x" already implies "--verbose" (and
"--verbose-only" overrides "--verbose", which is why this
works at all). And for our test, we need only check
$verbose, as maybe_setup_verbose will have already
set that flag based on the $verbose_only list).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

test-lib: turn off "-x" tracing during chain-lint checkJeff King Thu, 6 Aug 2015 05:31:47 +0000 (01:31 -0400)

test-lib: turn off "-x" tracing during chain-lint check

Now that GIT_TEST_CHAIN_LINT is on by default, running:

./t0000-basic.sh -x --verbose-only=1

starts with:

expecting success:
find .git/objects -type f -print >should-be-empty &&
test_line_count = 0 should-be-empty

+ exit 117
error: last command exited with $?=117
+ find .git/objects -type f -print
+ test_line_count = 0 should-be-empty
+ test 3 != 3
+ wc -l
+ test 0 = 0
ok 1 - .git/objects should be empty after git init in an empty repo

This is confusing, as the "exit 117" line and the error line
(which is printed in red, no less!) are not part of the test
at all, but are rather in the separate chain-lint test_eval.
Let's unset the "trace" variable when eval-ing the chain
lint check, which avoids this.

Note that we cannot just do a one-shot variable like:

trace= test_eval ...

as the behavior of one-shot variables for function calls
is not portable.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

refs: support negative transfer.hideRefsJeff King Tue, 28 Jul 2015 20:23:26 +0000 (16:23 -0400)

refs: support negative transfer.hideRefs

If you hide a hierarchy of refs using the transfer.hideRefs
config, there is no way to later override that config to
"unhide" it. This patch implements a "negative" hide which
causes matches to immediately be marked as unhidden, even if
another match would hide it. We take care to apply the
matches in reverse-order from how they are fed to us by the
config machinery, as that lets our usual "last one wins"
config precedence work (and entries in .git/config, for
example, will override /etc/gitconfig).

So you can now do:

$ git config --system transfer.hideRefs refs/secret
$ git config transfer.hideRefs '!refs/secret/not-so-secret'

to hide refs/secret in all repos, except for one public bit
in one specific repo. Or you can even do:

$ git clone \
-u "git -c transfer.hiderefs="!refs/foo" upload-pack" \
remote:repo.git

to clone remote:repo.git, overriding any hiding it has
configured.

There are two alternatives that were considered and
rejected:

1. A generic config mechanism for removing an item from a
list. E.g.: (e.g., "[transfer] hideRefs -= refs/foo").

This is nice because it could apply to other
multi-valued config, as well. But it is not nearly as
flexible. There is no way to say:

[transfer]
hideRefs = refs/secret
hideRefs = refs/secret/not-so-secret

Having explicit negative specifications means we can
override previous entries, even if they are not the
same literal string.

2. Adding another variable to override some parts of
hideRefs (e.g., "exposeRefs").

This solves the problem from alternative (1), but it
cannot easily obey the normal config precedence,
because it would use two separate lists. For example:

[transfer]
hideRefs = refs/secret
exposeRefs = refs/secret/not-so-secret
hideRefs = refs/secret/not-so-secret/no-really-its-secret

With two lists, we have to apply the "expose" rules
first, and only then apply the "hide" rules. But that
does not match what the above config intends.

Of course we could internally parse that to a single
list, respecting the ordering, which saves us having to
invent the new "!" syntax. But using a single name
communicates to the user that the ordering _is_
important. And "!" is well-known for negation, and
should not appear at the beginning of a ref (it is
actually valid in a ref-name, but all entries here
should be fully-qualified, starting with "refs/").

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

completion: offer '--edit-todo' during interactive... Thomas Braun Wed, 5 Aug 2015 13:40:00 +0000 (15:40 +0200)

completion: offer '--edit-todo' during interactive rebase

Helped-by: John Keeping <john@keeping.me.uk>
Helped-by: SZEDER Gábor <szeder@ira.uka.de>
Signed-off-by: Thomas Braun <thomas.braun@virtuell-zuhause.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

tests: fix cleanup after tests in t1509-root-worktreePatrick Steinhardt Wed, 5 Aug 2015 09:43:51 +0000 (11:43 +0200)

tests: fix cleanup after tests in t1509-root-worktree

During cleanup we do a simple 'rm /*' to remove leftover files
from previous tests. As 'rm' errors out when there is anything it
cannot delete and there are directories present at '/' it will
throw an error, causing the '&&' chain to fail.

Fix this by explicitly removing the files.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

tests: fix broken && chains in t1509-root-worktreePatrick Steinhardt Wed, 5 Aug 2015 09:43:50 +0000 (11:43 +0200)

tests: fix broken && chains in t1509-root-worktree

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Documentation/git-worktree: fix duplicated 'from'Patrick Steinhardt Tue, 4 Aug 2015 12:27:57 +0000 (14:27 +0200)

Documentation/git-worktree: fix duplicated 'from'

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Acked-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

git-am: add am.threeWay config variableRemi Lespinet Tue, 4 Aug 2015 14:19:26 +0000 (22:19 +0800)

git-am: add am.threeWay config variable

Add the am.threeWay configuration variable to use the -3 or --3way
option of git am by default. When am.threeway is set and not desired
for a specific git am command, the --no-3way option can be used to
override it.

Signed-off-by: Remi Lespinet <remi.lespinet@ensimag.grenoble-inp.fr>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: remove redirection to git-am.shPaul Tan Tue, 4 Aug 2015 13:52:06 +0000 (21:52 +0800)

builtin-am: remove redirection to git-am.sh

At the beginning of the rewrite of git-am.sh to C, in order to not break
existing test scripts that depended on a functional git-am, a
redirection to git-am.sh was introduced that would activate if the
environment variable _GIT_USE_BUILTIN_AM was not defined.

Now that all of git-am.sh's functionality has been re-implemented in
builtin/am.c, remove this redirection, and retire git-am.sh into
contrib/examples/.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: check for valid committer identPaul Tan Tue, 4 Aug 2015 13:52:05 +0000 (21:52 +0800)

builtin-am: check for valid committer ident

When commit_tree() is called, if the user does not have an explicit
committer ident configured, it will attempt to construct a default
committer ident based on the user's and system's info (e.g. gecos field,
hostname etc.) However, if a default committer ident is unable to be
constructed, commit_tree() will die(), but at this point of git-am's
execution, there will already be changes made to the index and work
tree.

This can be confusing to new users, and as such since d64e6b0 (Keep
Porcelainish from failing by broken ident after making changes.,
2006-02-18) git-am.sh will check to see if the committer ident has been
configured, or a default one can be constructed, before even starting to
apply patches.

Re-implement this in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement legacy -b/--binary optionPaul Tan Tue, 4 Aug 2015 13:52:04 +0000 (21:52 +0800)

builtin-am: implement legacy -b/--binary option

The -b/--binary option was initially implemented in 087b674 (git-am:
--binary; document --resume and --binary., 2005-11-16). The option will
pass the --binary flag to git-apply to allow it to apply binary patches.

However, in 2b6eef9 (Make apply --binary a no-op., 2006-09-06), --binary
was been made a no-op in git-apply. Following that, since cb3a160
(git-am: ignore --binary option, 2008-08-09), the --binary option in
git-am is ignored as well.

In 6c15a1c (am: officially deprecate -b/--binary option, 2012-03-13),
the --binary option was tweaked to its present behavior: when set, the
message:

The -b/--binary option has been a no-op for long time, and it
will be removed. Please do not use it anymore.

will be printed.

Re-implement this in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement -i/--interactivePaul Tan Tue, 4 Aug 2015 13:52:03 +0000 (21:52 +0800)

builtin-am: implement -i/--interactive

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sh supported the --interactive mode. After parsing the patch mail
and extracting the patch, commit message and authorship info, an
interactive session will begin that allows the user to choose between:

* applying the patch

* applying the patch and all subsequent patches (by disabling
interactive mode in subsequent patches)

* skipping the patch

* editing the commit message

Since f89ad67 (Add [v]iew patch in git-am interactive., 2005-10-25),
git-am.sh --interactive also supported viewing the patch to be applied.

When --resolved-ing in --interactive mode, we need to take care to
update the patch with the contents of the index, such that the correct
patch will be displayed when the patch is viewed in interactive mode.

Re-implement the above in builtin/am.c

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: support and auto-detect mercurial patchesPaul Tan Tue, 4 Aug 2015 13:52:02 +0000 (21:52 +0800)

builtin-am: support and auto-detect mercurial patches

Since 0cfd112 (am: preliminary support for hg patches, 2011-08-29),
git-am.sh could convert mercurial patches to an RFC2822 mail patch
suitable for parsing with git-mailinfo, and queue them in the state
directory for application.

Since 15ced75 (git-am foreign patch support: autodetect some patch
formats, 2009-05-27), git-am.sh was able to auto-detect mercurial
patches by checking if the file begins with the line:

# HG changeset patch

Re-implement the above in builtin/am.c.

Helped-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: support and auto-detect StGit series filesPaul Tan Tue, 4 Aug 2015 13:52:01 +0000 (21:52 +0800)

builtin-am: support and auto-detect StGit series files

Since c574e68 (git-am foreign patch support: StGIT support, 2009-05-27),
git-am.sh is able to read a single StGit series file and, for each StGit
patch listed in the file, convert the StGit patch into a RFC2822 mail
patch suitable for parsing with git-mailinfo, and queue them in the
state directory for applying.

Since 15ced75 (git-am foreign patch support: autodetect some patch
formats, 2009-05-27), git-am.sh is able to auto-detect StGit series
files by checking to see if the file starts with the string:

# This series applies on GIT commit

Re-implement the above in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: support and auto-detect StGit patchesPaul Tan Tue, 4 Aug 2015 13:52:00 +0000 (21:52 +0800)

builtin-am: support and auto-detect StGit patches

Since c574e68 (git-am foreign patch support: StGIT support, 2009-05-27),
git-am.sh supported converting StGit patches into RFC2822 mail patches
that can be parsed with git-mailinfo.

Implement this by introducing two functions in builtin/am.c:
stgit_patch_to_mail() and split_mail_conv().

stgit_patch_to_mail() is a callback function for split_mail_conv(), and
contains the logic for converting an StGit patch into an RFC2822 mail
patch.

split_mail_conv() implements the logic to go through each file in the
`paths` list, reading from stdin where specified, and calls the callback
function to write the converted patch to the corresponding output file
in the state directory. This interface should be generic enough to
support other foreign patch formats in the future.

Since 15ced75 (git-am foreign patch support: autodetect some patch
formats, 2009-05-27), git-am.sh is able to auto-detect StGit patches.
Re-implement this in builtin/am.c.

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: rerere supportPaul Tan Tue, 4 Aug 2015 13:51:59 +0000 (21:51 +0800)

builtin-am: rerere support

git-am.sh will call git-rerere at the following events:

* "git rerere" when a three-way merge fails to record the conflicted
automerge results. Since 8389b52 (git-rerere: reuse recorded resolve.,
2006-01-28)

* Since cb6020b (Teach --[no-]rerere-autoupdate option to merge,
revert and friends, 2009-12-04), git-am.sh supports the
--[no-]rerere-autoupdate option as well, and would pass it to
git-rerere.

* "git rerere" when --resolved, to record the hand resolution. Since
f131dd4 (rerere: record (or avoid misrecording) resolved, skipped or
aborted rebase/am, 2006-12-08)

* "git rerere clear" when --skip-ing. Since f131dd4 (rerere: record (or
avoid misrecording) resolved, skipped or aborted rebase/am,
2006-12-08)

* "git rerere clear" when --abort-ing. Since 3e5057a (git am --abort,
2008-07-16)

Re-implement the above in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: invoke post-applypatch hookPaul Tan Tue, 4 Aug 2015 13:51:58 +0000 (21:51 +0800)

builtin-am: invoke post-applypatch hook

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sh will invoke the post-applypatch hook after the patch is
applied and a commit is made. The exit code of the hook is ignored.

Re-implement this in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: invoke pre-applypatch hookPaul Tan Tue, 4 Aug 2015 13:51:57 +0000 (21:51 +0800)

builtin-am: invoke pre-applypatch hook

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sg will invoke the pre-applypatch hook after applying the patch
to the index, but before a commit is made. Should the hook exit with a
non-zero status, git am will exit.

Re-implement this in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: invoke applypatch-msg hookPaul Tan Tue, 4 Aug 2015 13:51:56 +0000 (21:51 +0800)

builtin-am: invoke applypatch-msg hook

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sh will invoke the applypatch-msg hooks just after extracting the
patch message. If the applypatch-msg hook exits with a non-zero status,
git-am.sh abort before even applying the patch to the index.

Re-implement this in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: support automatic notes copyingPaul Tan Tue, 4 Aug 2015 13:51:55 +0000 (21:51 +0800)

builtin-am: support automatic notes copying

Since eb2151b (rebase: support automatic notes copying, 2010-03-12),
git-am.sh supported automatic notes copying in --rebasing mode by
invoking "git notes copy" once it has finished applying all the patches.

Re-implement this feature in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: invoke post-rewrite hookPaul Tan Tue, 4 Aug 2015 13:51:54 +0000 (21:51 +0800)

builtin-am: invoke post-rewrite hook

Since 96e1948 (rebase: invoke post-rewrite hook, 2010-03-12), git-am.sh
will invoke the post-rewrite hook after it successfully finishes
applying all the queued patches.

To do this, when parsing a mail to extract its patch and metadata, in
--rebasing mode git-am.sh will also store the original commit ID in the
$state_dir/original-commit file. Once it applies and commits the patch,
the original commit ID, and the new commit ID, will be appended to the
$state_dir/rewritten file.

Once all of the queued mail have been processed, git-am.sh will then
invoke the post-rewrite hook with the contents of the
$state_dir/rewritten file.

Re-implement this in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement -S/--gpg-sign, commit.gpgsignPaul Tan Tue, 4 Aug 2015 13:51:53 +0000 (21:51 +0800)

builtin-am: implement -S/--gpg-sign, commit.gpgsign

Since 3b4e395 (am: add the --gpg-sign option, 2014-02-01), git-am.sh
supported the --gpg-sign option, and would pass it to git-commit-tree,
thus GPG-signing the commit object.

Re-implement this option in builtin/am.c.

git-commit-tree would also sign the commit by default if the
commit.gpgsign setting is true. Since we do not run commit-tree, we
re-implement this behavior by handling the commit.gpgsign setting
ourselves.

Helped-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --committer-date-is-author-datePaul Tan Tue, 4 Aug 2015 13:51:52 +0000 (21:51 +0800)

builtin-am: implement --committer-date-is-author-date

Since 3f01ad6 (am: Add --committer-date-is-author-date option,
2009-01-22), git-am.sh implemented the --committer-date-is-author-date
option, which tells git-am to use the timestamp recorded in the email
message as both author and committer date.

Re-implement this option in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --ignore-datePaul Tan Tue, 4 Aug 2015 13:51:51 +0000 (21:51 +0800)

builtin-am: implement --ignore-date

Since a79ec62 (git-am: Add --ignore-date option, 2009-01-24), git-am.sh
supported the --ignore-date option, and would use the current timestamp
instead of the one provided in the patch if the option was set.

Re-implement this option in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: pass git-apply's options to git-applyPaul Tan Tue, 4 Aug 2015 13:51:50 +0000 (21:51 +0800)

builtin-am: pass git-apply's options to git-apply

git-am.sh recognizes some of git-apply's options, and would pass them to
git-apply:

* --whitespace, since 8c31cb8 (git-am: --whitespace=x option.,
2006-02-28)

* -C, since 67dad68 (add -C[NUM] to git-am, 2007-02-08)

* -p, since 2092a1f (Teach git-am to pass -p option down to git-apply,
2007-02-11)

* --directory, since b47dfe9 (git-am: add --directory=<dir> option,
2009-01-11)

* --reject, since b80da42 (git-am: implement --reject option passed to
git-apply, 2009-01-23)

* --ignore-space-change, --ignore-whitespace, since 86c91f9 (git apply:
option to ignore whitespace differences, 2009-08-04)

* --exclude, since 77e9e49 (am: pass exclude down to apply, 2011-08-03)

* --include, since 58725ef (am: support --include option, 2012-03-28)

* --reject, since b80da42 (git-am: implement --reject option passed to
git-apply, 2009-01-23)

Re-implement support for these options in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --[no-]scissorsPaul Tan Tue, 4 Aug 2015 13:51:49 +0000 (21:51 +0800)

builtin-am: implement --[no-]scissors

Since 017678b (am/mailinfo: Disable scissors processing by default,
2009-08-26), git-am supported the --[no-]scissors option, passing it to
git-mailinfo.

Re-implement support for this option in builtin/am.c.

Since the default setting of --scissors in git-mailinfo can be
configured with mailinfo.scissors (and perhaps through other settings in
the future), to be safe we make an explicit distinction between
SCISSORS_UNSET, SCISSORS_TRUE and SCISSORS_FALSE.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: support --keep-cr, am.keepcrPaul Tan Tue, 4 Aug 2015 13:51:48 +0000 (21:51 +0800)

builtin-am: support --keep-cr, am.keepcr

Since ad2c928 (git-am: Add command line parameter `--keep-cr` passing it
to git-mailsplit, 2010-02-27), git-am.sh supported the --keep-cr option
and would pass it to git-mailsplit.

Since e80d4cb (git-am: Add am.keepcr and --no-keep-cr to override it,
2010-02-27), git-am.sh supported the am.keepcr config setting, which
controls whether --keep-cr is on by default.

Re-implement the above in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --[no-]message-id, am.messageidPaul Tan Tue, 4 Aug 2015 13:51:47 +0000 (21:51 +0800)

builtin-am: implement --[no-]message-id, am.messageid

Since a078f73 (git-am: add --message-id/--no-message-id, 2014-11-25),
git-am.sh supported the --[no-]message-id options, and the
"am.messageid" setting which specifies the default option.

--[no-]message-id tells git-am whether or not the -m option should be
passed to git-mailinfo.

Re-implement this option in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement -k/--keep, --keep-non-patchPaul Tan Tue, 4 Aug 2015 13:51:46 +0000 (21:51 +0800)

builtin-am: implement -k/--keep, --keep-non-patch

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sh supported the -k/--keep option to pass the -k option to
git-mailsplit.

Since f7e5ea1 (am: learn passing -b to mailinfo, 2012-01-16), git-am.sh
supported the --keep-non-patch option to pass the -b option to
git-mailsplit.

Re-implement these two options in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement -u/--utf8Paul Tan Tue, 4 Aug 2015 13:51:45 +0000 (21:51 +0800)

builtin-am: implement -u/--utf8

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sh supported the -u,--utf8 option. If set, the -u option will be
passed to git-mailinfo to re-code the commit log message and authorship
in the charset specified by i18n.commitencoding. If unset, the -n option
will be passed to git-mailinfo, which disables the re-encoding.

Since d84029b (--utf8 is now default for 'git-am', 2007-01-08), --utf8
is specified by default in git-am.sh.

Re-implement the above in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: handle stray state directoryPaul Tan Tue, 4 Aug 2015 13:51:44 +0000 (21:51 +0800)

builtin-am: handle stray state directory

Should git-am terminate unexpectedly between the point where the state
directory is created, but the "next" and "last" files are not written
yet, a stray state directory will be left behind.

As such, since b141f3c (am: handle stray $dotest directory, 2013-06-15),
git-am.sh explicitly recognizes such a stray directory, and allows the
user to remove it with am --abort.

Re-implement this feature in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: bypass git-mailinfo when --rebasingPaul Tan Tue, 4 Aug 2015 13:51:43 +0000 (21:51 +0800)

builtin-am: bypass git-mailinfo when --rebasing

Since 5e835ca (rebase: do not munge commit log message, 2008-04-16),
git am --rebasing no longer gets the commit log message from the patch,
but reads it directly from the commit identified by the "From " header
line.

Since 43c2325 (am: use get_author_ident_from_commit instead of mailinfo
when rebasing, 2010-06-16), git am --rebasing also gets the author name,
email and date directly from the commit.

Since 0fbb95d (am: don't call mailinfo if $rebasing, 2012-06-26), git am
--rebasing does not use git-mailinfo to get the patch body, but rather
generates it directly from the commit itself.

The above 3 commits introduced a separate parse_mail() code path in
git-am.sh's --rebasing mode that bypasses git-mailinfo. Re-implement
this code path in builtin/am.c as parse_mail_rebase().

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --rebasing modePaul Tan Tue, 4 Aug 2015 13:51:42 +0000 (21:51 +0800)

builtin-am: implement --rebasing mode

Since 3041c32 (am: --rebasing, 2008-03-04), git-am.sh supported the
--rebasing option, which is used internally by git-rebase to tell git-am
that it is being used for its purpose. It would create the empty file
$state_dir/rebasing to help "completion" scripts tell if the ongoing
operation is am or rebase.

As of 0fbb95d (am: don't call mailinfo if $rebasing, 2012-06-26),
--rebasing also implies --3way as well.

Since a1549e1 (am: return control to caller, for housekeeping,
2013-05-12), git-am.sh would only clean up the state directory when it
is not --rebasing, instead deferring cleanup to git-rebase.sh.

Re-implement the above in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --3wayPaul Tan Tue, 4 Aug 2015 13:51:41 +0000 (21:51 +0800)

builtin-am: implement --3way

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07),
git-am.sh supported the --3way option, and if set, would attempt to do a
3-way merge if the initial patch application fails.

Since 5d86861 (am -3: list the paths that needed 3-way fallback,
2012-03-28), in a 3-way merge git-am.sh would list the paths that needed
3-way fallback, so that the user can review them more carefully to spot
mismerges.

Re-implement the above in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

cache-tree: introduce write_index_as_tree()Paul Tan Tue, 4 Aug 2015 13:51:40 +0000 (21:51 +0800)

cache-tree: introduce write_index_as_tree()

A caller may wish to write a temporary index as a tree. However,
write_cache_as_tree() assumes that the index was read from, and will
write to, the default index file path. Introduce write_index_as_tree()
which removes this limitation by allowing the caller to specify its own
index_state and index file path.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement -s/--signoffPaul Tan Tue, 4 Aug 2015 13:51:39 +0000 (21:51 +0800)

builtin-am: implement -s/--signoff

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am
supported the --signoff option which will append a signoff at the end of
the commit messsage. Re-implement this feature in parse_mail() by
calling append_signoff() if the option is set.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: exit with user friendly message on failurePaul Tan Tue, 4 Aug 2015 13:51:38 +0000 (21:51 +0800)

builtin-am: exit with user friendly message on failure

Since ced9456 (Give the user a hint for how to continue in the case that
git-am fails because it requires user intervention, 2006-05-02), git-am
prints additional information on how the user can re-invoke git-am to
resume patch application after resolving the failure. Re-implement this
through the die_user_resolve() function.

Since cc12005 (Make git rebase interactive help match documentation.,
2006-05-13), git-am supports the --resolvemsg option which is used by
git-rebase to override the message printed out when git-am fails.
Re-implement this option.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement -q/--quietPaul Tan Tue, 4 Aug 2015 13:51:37 +0000 (21:51 +0800)

builtin-am: implement -q/--quiet

Since 0e987a1 (am, rebase: teach quiet option, 2009-06-16), git-am
supported the --quiet option, and when told to be quiet, would only
speak on failure. Re-implement this by introducing the say() function,
which works like fprintf_ln(), but would only write to the stream when
state->quiet is false.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: reject patches when there's a session in... Paul Tan Tue, 4 Aug 2015 13:51:36 +0000 (21:51 +0800)

builtin-am: reject patches when there's a session in progress

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am
would error out if the user gave it mbox(s) on the command-line, but
there was a session in progress.

Since c95b138 (Fix git-am safety checks, 2006-09-15), git-am would
detect if the user attempted to feed it a mbox via stdin, by checking if
stdin is not a tty and there is no resume command given.

Re-implement the above two safety checks.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --abortPaul Tan Tue, 4 Aug 2015 13:51:35 +0000 (21:51 +0800)

builtin-am: implement --abort

Since 3e5057a (git am --abort, 2008-07-16), git-am supported the --abort
option that will rewind HEAD back to the original commit. Re-implement
this through am_abort().

Since 7b3b7e3 (am --abort: keep unrelated commits since the last failure
and warn, 2010-12-21), to prevent commits made since the last failure
from being lost, git-am will not rewind HEAD back to the original
commit if HEAD moved since the last failure. Re-implement this through
safe_to_abort().

Helped-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --skipPaul Tan Tue, 4 Aug 2015 13:51:34 +0000 (21:51 +0800)

builtin-am: implement --skip

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am
supported resuming from a failed patch application by skipping the
current patch. Re-implement this feature by introducing am_skip().

Helped-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: don't parse mail when resumingPaul Tan Tue, 4 Aug 2015 13:51:33 +0000 (21:51 +0800)

builtin-am: don't parse mail when resuming

Since 271440e (git-am: make it easier after fixing up an unapplicable
patch., 2005-10-25), when "git am" is run again after being paused, the
current mail message will not be re-parsed, but instead the contents of
the state directory's patch, msg and author-script files will be used
as-is instead.

Re-implement this in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement --resolved/--continuePaul Tan Tue, 4 Aug 2015 13:51:32 +0000 (21:51 +0800)

builtin-am: implement --resolved/--continue

Since 0c15cc9 (git-am: --resolved., 2005-11-16), git-am supported
resuming from a failed patch application. The user will manually apply
the patch, and the run git am --resolved which will then commit the
resulting index. Re-implement this feature by introducing am_resolve().

Since it makes no sense for the user to run am --resolved when there is
no session in progress, we error out in this case.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: refuse to apply patches if index is dirtyPaul Tan Tue, 4 Aug 2015 13:51:31 +0000 (21:51 +0800)

builtin-am: refuse to apply patches if index is dirty

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am
will refuse to apply patches if the index is dirty. Re-implement this
behavior in builtin/am.c.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement committing applied patchPaul Tan Tue, 4 Aug 2015 13:51:30 +0000 (21:51 +0800)

builtin-am: implement committing applied patch

Implement do_commit(), which commits the index which contains the
results of applying the patch, along with the extracted commit message
and authorship information.

Since 29b6754 (am: remove rebase-apply directory before gc, 2010-02-22),
git gc --auto is also invoked to pack the loose objects that are created
from making the commits.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: apply patch with git-applyPaul Tan Tue, 4 Aug 2015 13:51:29 +0000 (21:51 +0800)

builtin-am: apply patch with git-apply

Implement applying the patch to the index using git-apply.

If a file is unchanged but stat-dirty, git-apply may erroneously fail to
apply patches, thinking that they conflict with a dirty working tree.

As such, since 2a6f08a (am: refresh the index at start and --resolved,
2011-08-15), git-am will refresh the index before applying patches.
Re-implement this behavior.

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: extract patch and commit info with git... Paul Tan Tue, 4 Aug 2015 13:51:28 +0000 (21:51 +0800)

builtin-am: extract patch and commit info with git-mailinfo

For the purpose of applying the patch and committing the results,
implement extracting the patch data, commit message and authorship from
an e-mail message using git-mailinfo.

git-mailinfo is run as a separate process, but ideally in the future,
we should be be able to access its functionality directly without
spawning a new process.

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Jeff King <peff@peff.net>
Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: auto-detect mbox patchesPaul Tan Tue, 4 Aug 2015 13:51:27 +0000 (21:51 +0800)

builtin-am: auto-detect mbox patches

Since 15ced75 (git-am foreign patch support: autodetect some patch
formats, 2009-05-27), git-am.sh is able to autodetect mbox, stgit and
mercurial patches through heuristics.

Re-implement support for autodetecting mbox/maildir files in
builtin/am.c.

RFC 2822 requires that lines are terminated by "\r\n". To support this,
implement strbuf_getline_crlf(), which will remove both '\n' and "\r\n"
from the end of the line.

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: split out mbox/maildir patches with git... Paul Tan Tue, 4 Aug 2015 13:51:26 +0000 (21:51 +0800)

builtin-am: split out mbox/maildir patches with git-mailsplit

git-am.sh supports mbox, stgit and mercurial patches. Re-implement
support for splitting out mbox/maildirs using git-mailsplit, while also
implementing the framework required to support other patch formats in
the future.

Re-implement support for the --patch-format option (since a5a6755
(git-am foreign patch support: introduce patch_format, 2009-05-27)) to
allow the user to choose between the different patch formats.

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement patch queue mechanismPaul Tan Tue, 4 Aug 2015 13:51:25 +0000 (21:51 +0800)

builtin-am: implement patch queue mechanism

git-am applies a series of patches. If the process terminates
abnormally, we want to be able to resume applying the series of patches.
This requires the session state to be saved in a persistent location.

Implement the mechanism of a "patch queue", represented by 2 integers --
the index of the current patch we are applying and the index of the last
patch, as well as its lifecycle through the following functions:

* am_setup(), which will set up the state directory
$GIT_DIR/rebase-apply. As such, even if the process exits abnormally,
the last-known state will still persist.

* am_load(), which is called if there is an am session in
progress, to load the last known state from the state directory so we
can resume applying patches.

* am_run(), which will do the actual patch application. After applying a
patch, it calls am_next() to increment the current patch index. The
logic for applying and committing a patch is not implemented yet.

* am_destroy(), which is finally called when we successfully applied all
the patches in the queue, to clean up by removing the state directory
and its contents.

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Stefan Beller <sbeller@google.com>
Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

builtin-am: implement skeletal builtin amPaul Tan Tue, 4 Aug 2015 13:51:24 +0000 (21:51 +0800)

builtin-am: implement skeletal builtin am

For the purpose of rewriting git-am.sh into a C builtin, implement a
skeletal builtin/am.c that redirects to $GIT_EXEC_PATH/git-am if the
environment variable _GIT_USE_BUILTIN_AM is not defined. Since in the
Makefile git-am.sh takes precedence over builtin/am.c,
$GIT_EXEC_PATH/git-am will contain the shell script git-am.sh, and thus
this allows us to fall back on the functional git-am.sh when running the
test suite for tests that depend on a working git-am implementation.

Since git-am.sh cannot handle any environment modifications by
setup_git_directory(), "am" is declared with no setup flags in git.c. On
the other hand, to re-implement git-am.sh in builtin/am.c, we need to
run all the git dir and work tree setup logic that git.c typically does
for us. As such, we work around this temporarily by copying the logic in
git.c's run_builtin(), which is roughly:

prefix = setup_git_directory();
trace_repo_setup(prefix);
setup_work_tree();

This redirection should be removed when all the features of git-am.sh
have been re-implemented in builtin/am.c.

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

wrapper: implement xfopen()Paul Tan Tue, 4 Aug 2015 13:51:23 +0000 (21:51 +0800)

wrapper: implement xfopen()

A common usage pattern of fopen() is to check if it succeeded, and die()
if it failed:

FILE *fp = fopen(path, "w");
if (!fp)
die_errno(_("could not open '%s' for writing"), path);

Implement a wrapper function xfopen() for the above, so that we can save
a few lines of code and make the die() messages consistent.

Helped-by: Jeff King <peff@peff.net>
Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

wrapper: implement xopen()Paul Tan Tue, 4 Aug 2015 13:51:22 +0000 (21:51 +0800)

wrapper: implement xopen()

A common usage pattern of open() is to check if it was successful, and
die() if it was not:

int fd = open(path, O_WRONLY | O_CREAT, 0777);
if (fd < 0)
die_errno(_("Could not open '%s' for writing."), path);

Implement a wrapper function xopen() that does the above so that we can
save a few lines of code, and make the die() messages consistent.

Helped-by: Torsten Bögershausen <tboegi@web.de>
Helped-by: Jeff King <peff@peff.net>
Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

transport-helper: die on errors reading refs.Stefan Beller Fri, 31 Jul 2015 23:57:57 +0000 (16:57 -0700)

transport-helper: die on errors reading refs.

We check the return value of read_ref in 19 out of 21 cases.
This adds checks to the missing cases.

Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

bisect: don't mix option parsing and non-trivial codeMatthieu Moy Mon, 29 Jun 2015 15:40:31 +0000 (17:40 +0200)

bisect: don't mix option parsing and non-trivial code

As-is, the revisions that appear on the command-line are processed in
order. This would mix badly with code that changes the configuration
(e.g. change $TERM_GOOD and $TERM_BAD) while processing the options.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

bisect: simplify the addition of new bisect termsAntoine Delaite Mon, 29 Jun 2015 15:40:30 +0000 (17:40 +0200)

bisect: simplify the addition of new bisect terms

We create a file BISECT_TERMS in the repository .git to be read during a
bisection. There's no user-interface yet, but "git bisect" works if terms
other than old/new or bad/good are set in .git/BISECT_TERMS. The
fonctions to be changed if we add new terms are quite few.

In git-bisect.sh:
check_and_set_terms
bisect_voc

Co-authored-by: Louis Stuber <stuberl@ensimag.grenoble-inp.fr>
Tweaked-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Antoine Delaite <antoine.delaite@ensimag.grenoble-inp.fr>
Signed-off-by: Louis Stuber <stuberl@ensimag.grenoble-inp.fr>
Signed-off-by: Valentin Duperray <Valentin.Duperray@ensimag.imag.fr>
Signed-off-by: Franck Jonas <Franck.Jonas@ensimag.imag.fr>
Signed-off-by: Lucien Kong <Lucien.Kong@ensimag.imag.fr>
Signed-off-by: Thomas Nguy <Thomas.Nguy@ensimag.imag.fr>
Signed-off-by: Huynh Khoi Nguyen Nguyen <Huynh-Khoi-Nguyen.Nguyen@ensimag.imag.fr>
Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sync with maintJunio C Hamano Mon, 3 Aug 2015 18:20:07 +0000 (11:20 -0700)

Sync with maint

* maint:
Git 2.4.8

First batch for 2.6Junio C Hamano Mon, 3 Aug 2015 18:19:36 +0000 (11:19 -0700)

First batch for 2.6

Signed-off-by: Junio C Hamano <gitster@pobox.com>

Merge branch 'es/doc-clean-outdated-tools'Junio C Hamano Mon, 3 Aug 2015 18:01:32 +0000 (11:01 -0700)

Merge branch 'es/doc-clean-outdated-tools'

* es/doc-clean-outdated-tools:
Documentation/git-tools: retire manually-maintained list
Documentation/git-tools: drop references to defunct tools
Documentation/git-tools: fix item text formatting
Documentation/git-tools: improve discoverability of Git wiki
Documentation/git: drop outdated Cogito reference

Merge branch 'jk/refspec-parse-wildcard'Junio C Hamano Mon, 3 Aug 2015 18:01:31 +0000 (11:01 -0700)

Merge branch 'jk/refspec-parse-wildcard'

Allow an asterisk as a substring (as opposed to the entirety) of
a path component for both side of a refspec, e.g.
"refs/heads/o*:refs/remotes/heads/i*".

* jk/refspec-parse-wildcard:
refs: loosen restriction on wildcard "*" refspecs
refs: cleanup comments regarding check_refname_component()

Merge branch 'da/subtree-date-confusion'Junio C Hamano Mon, 3 Aug 2015 18:01:31 +0000 (11:01 -0700)

Merge branch 'da/subtree-date-confusion'

"git subtree" (in contrib/) depended on "git log" output to be
stable, which was a no-no. Apply a workaround to force a
particular date format.

* da/subtree-date-confusion:
contrib/subtree: ignore log.date configuration

Merge branch 'jx/do-not-crash-receive-pack-wo-head'Junio C Hamano Mon, 3 Aug 2015 18:01:31 +0000 (11:01 -0700)

Merge branch 'jx/do-not-crash-receive-pack-wo-head'

An attempt to delete a ref by pushing into a repositorywhose HEAD
symbolic reference points at an unborn branch that cannot be
created due to ref D/F conflict (e.g. refs/heads/a/b exists, HEAD
points at refs/heads/a) failed.

* jx/do-not-crash-receive-pack-wo-head:
receive-pack: crash when checking with non-exist HEAD

Merge branch 'db/send-pack-user-signingkey'Junio C Hamano Mon, 3 Aug 2015 18:01:30 +0000 (11:01 -0700)

Merge branch 'db/send-pack-user-signingkey'

The low-level "git send-pack" did not honor 'user.signingkey'
configuration variable when sending a signed-push.

* db/send-pack-user-signingkey:
builtin/send-pack.c: respect user.signingkey

Merge branch 'zb/userdiff-fountain'Junio C Hamano Mon, 3 Aug 2015 18:01:29 +0000 (11:01 -0700)

Merge branch 'zb/userdiff-fountain'

New userdiff pattern definition for fountain screenwriting markup
format.

* zb/userdiff-fountain:
userdiff: add support for Fountain documents

Merge branch 'dt/refs-backend-preamble'Junio C Hamano Mon, 3 Aug 2015 18:01:29 +0000 (11:01 -0700)

Merge branch 'dt/refs-backend-preamble'

In preparation for allowing different "backends" to store the refs
in a way different from the traditional "one ref per file in $GIT_DIR
or in a $GIT_DIR/packed-refs file" filesystem storage, reduce
direct filesystem access to ref-like things like CHERRY_PICK_HEAD
from scripts and programs.

* dt/refs-backend-preamble:
git-stash: use update-ref --create-reflog instead of creating files
update-ref and tag: add --create-reflog arg
refs: add REF_FORCE_CREATE_REFLOG flag
git-reflog: add exists command
refs: new public ref function: safe_create_reflog
refs: break out check for reflog autocreation
refs.c: add err arguments to reflog functions

Merge branch 'as/sparse-checkout-removal'Junio C Hamano Mon, 3 Aug 2015 18:01:28 +0000 (11:01 -0700)

Merge branch 'as/sparse-checkout-removal'

"sparse checkout" misbehaved for a path that is excluded from the
checkout when switching between branches that differ at the path.

* as/sparse-checkout-removal:
unpack-trees: don't update files with CE_WT_REMOVE set

Merge branch 'jk/date-mode-format'Junio C Hamano Mon, 3 Aug 2015 18:01:27 +0000 (11:01 -0700)

Merge branch 'jk/date-mode-format'

Teach "git log" and friends a new "--date=format:..." option to
format timestamps using system's strftime(3).

* jk/date-mode-format:
strbuf: make strbuf_addftime more robust
introduce "format" date-mode
convert "enum date_mode" into a struct
show-branch: use DATE_RELATIVE instead of magic number

Merge branch 'pt/am-tests'Junio C Hamano Mon, 3 Aug 2015 18:01:26 +0000 (11:01 -0700)

Merge branch 'pt/am-tests'

* pt/am-tests:
t3901: test git-am encoding conversion
t3418: non-interactive rebase --continue with rerere enabled
t4150: tests for am --[no-]scissors
t4150: am with post-applypatch hook
t4150: am with pre-applypatch hook
t4150: am with applypatch-msg hook
t4150: am --resolved fails if index has unmerged entries
t4150: am --resolved fails if index has no changes
t4150: am refuses patches when paused
t4151: am --abort will keep dirty index intact
t4150: am fails if index is dirty
t4150: am.messageid really adds the message id

Merge branch 'sg/bash-prompt-untracked-optim'Junio C Hamano Mon, 3 Aug 2015 18:01:26 +0000 (11:01 -0700)

Merge branch 'sg/bash-prompt-untracked-optim'

Optimize computation of untracked status indicator by bash prompt
script (in contrib/).

* sg/bash-prompt-untracked-optim:
bash prompt: faster untracked status indicator with untracked directories
bash prompt: test untracked files status indicator with untracked dirs

Merge branch 'cb/uname-in-untracked'Junio C Hamano Mon, 3 Aug 2015 18:01:26 +0000 (11:01 -0700)

Merge branch 'cb/uname-in-untracked'

An experimental "untracked cache" feature used uname(2) in a
slightly unportable way.

* cb/uname-in-untracked:
untracked: fix detection of uname(2) failure

Merge branch 'se/doc-checkout-ours-theirs'Junio C Hamano Mon, 3 Aug 2015 18:01:25 +0000 (11:01 -0700)

Merge branch 'se/doc-checkout-ours-theirs'

A "rebase" replays changes of the local branch on top of something
else, as such they are placed in stage #3 and referred to as
"theirs", while the changes in the new base, typically a foreign
work, are placed in stage #2 and referred to as "ours". Clarify
the "checkout --ours/--theirs".

* se/doc-checkout-ours-theirs:
checkout: document subtlety around --ours/--theirs

Merge branch 'ib/scripted-parse-opt-better-hint-string'Junio C Hamano Mon, 3 Aug 2015 18:01:24 +0000 (11:01 -0700)

Merge branch 'ib/scripted-parse-opt-better-hint-string'

The "rev-parse --parseopt" mode parsed the option specification
and the argument hint in a strange way to allow '=' and other
special characters in the option name while forbidding them from
the argument hint. This made it impossible to define an option
like "--pair <key>=<value>" with "pair=key=value" specification,
which instead would have defined a "--pair=key <value>" option.

* ib/scripted-parse-opt-better-hint-string:
rev-parse --parseopt: allow [*=?!] in argument hints

Merge branch 'mh/fast-import-optimize-current-from'Junio C Hamano Mon, 3 Aug 2015 18:01:24 +0000 (11:01 -0700)

Merge branch 'mh/fast-import-optimize-current-from'

Often a fast-import stream builds a new commit on top of the
previous commit it built, and it often unconditionally emits a
"from" command to specify the first parent, which can be omitted in
such a case. This caused fast-import to forget the tree of the
previous commit and then re-read it from scratch, which was
inefficient. Optimize for this common case.

* mh/fast-import-optimize-current-from:
fast-import: do less work when given "from" matches current branch head

Merge branch 'kn/tag-doc-fix'Junio C Hamano Mon, 3 Aug 2015 18:01:23 +0000 (11:01 -0700)

Merge branch 'kn/tag-doc-fix'

* kn/tag-doc-fix:
Documentation/tag: remove double occurance of "<pattern>"

Merge branch 'mh/fast-import-get-mark'Junio C Hamano Mon, 3 Aug 2015 18:01:23 +0000 (11:01 -0700)

Merge branch 'mh/fast-import-get-mark'

"git fast-import" learned to respond to the get-mark command via
its cat-blob-fd interface.

* mh/fast-import-get-mark:
fast-import: add a get-mark command

Merge branch 'gr/rebase-i-drop-warn'Junio C Hamano Mon, 3 Aug 2015 18:01:22 +0000 (11:01 -0700)

Merge branch 'gr/rebase-i-drop-warn'

Add "drop commit-object-name subject" command as another way to
skip replaying of a commit in "rebase -i", and then punish those
who do not use it (and instead just remove the lines) by throwing
a warning.

* gr/rebase-i-drop-warn:
git rebase -i: add static check for commands and SHA-1
git rebase -i: warn about removed commits
git-rebase -i: add command "drop" to remove a commit

Merge branch 'jc/commit-slab'Junio C Hamano Mon, 3 Aug 2015 18:01:21 +0000 (11:01 -0700)

Merge branch 'jc/commit-slab'

Memory use reduction when commit-slab facility is used to annotate
sparsely (which is not recommended in the first place).

* jc/commit-slab:
commit-slab: introduce slabname##_peek() function

Merge branch 'dt/log-follow-config'Junio C Hamano Mon, 3 Aug 2015 18:01:20 +0000 (11:01 -0700)

Merge branch 'dt/log-follow-config'

Add a new configuration variable to enable "--follow" automatically
when "git log" is run with one pathspec argument.

* dt/log-follow-config:
log: add "log.follow" configuration variable

Merge branch 'gp/status-rebase-i-info'Junio C Hamano Mon, 3 Aug 2015 18:01:19 +0000 (11:01 -0700)

Merge branch 'gp/status-rebase-i-info'

Teach "git status" to show a more detailed information regarding
the "rebase -i" session in progress.

* gp/status-rebase-i-info:
status: add new tests for status during rebase -i
status: give more information during rebase -i
status: differentiate interactive from non-interactive rebases
status: factor two rebase-related messages together

Merge branch 'jk/cat-file-batch-all'Junio C Hamano Mon, 3 Aug 2015 18:01:19 +0000 (11:01 -0700)

Merge branch 'jk/cat-file-batch-all'

"cat-file" learned "--batch-all-objects" option to enumerate all
available objects in the repository more quickly than "rev-list
--all --objects" (the output includes unreachable objects, though).

* jk/cat-file-batch-all:
cat-file: sort and de-dup output of --batch-all-objects
cat-file: add --batch-all-objects option
cat-file: split batch_one_object into two stages
cat-file: stop returning value from batch_one_object
cat-file: add --buffer option
cat-file: move batch_options definition to top of file
cat-file: minor style fix in options list

Merge branch 'js/fsck-opt'Junio C Hamano Mon, 3 Aug 2015 18:01:18 +0000 (11:01 -0700)

Merge branch 'js/fsck-opt'

Allow ignoring fsck errors on specific set of known-to-be-bad
objects, and also tweaking warning level of various kinds of non
critical breakages reported.

* js/fsck-opt:
fsck: support ignoring objects in `git fsck` via fsck.skiplist
fsck: git receive-pack: support excluding objects from fsck'ing
fsck: introduce `git fsck --connectivity-only`
fsck: support demoting errors to warnings
fsck: document the new receive.fsck.<msg-id> options
fsck: allow upgrading fsck warnings to errors
fsck: optionally ignore specific fsck issues completely
fsck: disallow demoting grave fsck errors to warnings
fsck: add a simple test for receive.fsck.<msg-id>
fsck: make fsck_tag() warn-friendly
fsck: handle multiple authors in commits specially
fsck: make fsck_commit() warn-friendly
fsck: make fsck_ident() warn-friendly
fsck: report the ID of the error/warning
fsck (receive-pack): allow demoting errors to warnings
fsck: offer a function to demote fsck errors to warnings
fsck: provide a function to parse fsck message IDs
fsck: introduce identifiers for fsck messages
fsck: introduce fsck options

Merge branch 'mh/init-delete-refs-api'Junio C Hamano Mon, 3 Aug 2015 18:01:17 +0000 (11:01 -0700)

Merge branch 'mh/init-delete-refs-api'

Clean up refs API and make "git clone" less intimate with the
implementation detail.

* mh/init-delete-refs-api:
delete_ref(): use the usual convention for old_sha1
cmd_update_ref(): make logic more straightforward
update_ref(): don't read old reference value before delete
check_branch_commit(): make first parameter const
refs.h: add some parameter names to function declarations
refs: move the remaining ref module declarations to refs.h
initial_ref_transaction_commit(): check for ref D/F conflicts
initial_ref_transaction_commit(): check for duplicate refs
refs: remove some functions from the module's public interface
initial_ref_transaction_commit(): function for initial ref creation
repack_without_refs(): make function private
prune_refs(): use delete_refs()
prune_remote(): use delete_refs()
delete_refs(): bail early if the packed-refs file cannot be rewritten
delete_refs(): make error message more generic
delete_refs(): new function for the refs API
delete_ref(): handle special case more explicitly
remove_branches(): remove temporary
delete_ref(): move declaration to refs.h

Merge branch 'pt/pull-builtin'Junio C Hamano Mon, 3 Aug 2015 18:01:17 +0000 (11:01 -0700)

Merge branch 'pt/pull-builtin'

Reimplement 'git pull' in C.

* pt/pull-builtin:
pull: remove redirection to git-pull.sh
pull --rebase: error on no merge candidate cases
pull --rebase: exit early when the working directory is dirty
pull: configure --rebase via branch.<name>.rebase or pull.rebase
pull: teach git pull about --rebase
pull: set reflog message
pull: implement pulling into an unborn branch
pull: fast-forward working tree if head is updated
pull: check if in unresolved merge state
pull: support pull.ff config
pull: error on no merge candidates
pull: pass git-fetch's options to git-fetch
pull: pass git-merge's options to git-merge
pull: pass verbosity, --progress flags to fetch and merge
pull: implement fetch + merge
pull: implement skeletal builtin pull
argv-array: implement argv_array_pushv()
parse-options-cb: implement parse_opt_passthru_argv()
parse-options-cb: implement parse_opt_passthru()