apply: accept -3/--3way command line option

This is the beginning of teaching the three-way merge fallback logic "git
am -3" uses to the underlying "git apply".  It only implements the command
line parsing part, and does not do anything interesting yet, other than
making sure that "--reject" and "--3way" are not given together and
making "--3way" imply "--index".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: fall back on three-way merge

Grab the preimage blob the patch claims to be based on out of the object
store, apply the patch, and then call three-way-merge function.  This step
still does not plug the actual three-way merge logic yet, but we are
getting there.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: plug the three-way merge logic in

When a patch does not apply to what we have, but we know the preimage the
patch was made against, we apply the patch to the preimage to compute what
the patch author wanted the result to look like, and attempt a three-way
merge between the result and our version, using the intended preimage as
the base version.

When we are applying the patch using the index, we would additionally need
to add the object names of these three blobs involved in the merge, which
is not yet done in this step, but we add a field to "struct patch" so that
later write-out step can use it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: register conflicted stages to the index

Now we have all the necessary logic to fall back on three-way merge when
the patch does not cleanly apply, insert the conflicted entries to the
index as appropriate.  This obviously triggers only when the "--index"
option is used.

When we fall back to three-way merge and some of the merges fail, just
like the case where the "--reject" option was specified and we had to
write some "*.rej" files out for unapplicable patches, exit the command
with non-zero status without showing the diffstat and summary.  Otherwise
they would make the list of problematic paths scroll off the display.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: allow rerere() upon --3way results

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: document --3way option

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: a bit more comments on PATH_TO_BE_DELETED

The code is littered with to_be_deleted() whose purpose is not so clear.
Describe where it matters.  Also remove an extra space before "#define"
that snuck in by mistake at 7fac0ee (builtin-apply: keep information about
files to be deleted, 2009-04-11).

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: refactor "previous patch" logic

The code to grab the result of application of a previous patch in the
input was mixed with error message generation for a case where a later
patch tries to modify contents of a path that has been removed.

The same code is duplicated elsewhere in the code.  Introduce a helper
to clarify what is going on.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply: fix an incomplete comment in check_patch()

This check is not only about type-change (for which it would be
sufficient to check only was_deleted()) but is also about a swap
rename.  Otherwise to_be_deleted() check is not justified.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply -3: add failing test for add/add conflicts

This currently triggers a check in check_patch() that prevents a
path from getting added to a preimage that already has it; instead
we should be crafting add/add conflict and letting the 3-way merge
trigger, after making sure that there is no local change to the
path.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply.c: move check_to_create_blob() closer to its sole caller

Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply.c::check_patch(): move "already exists" logic to check_to_create()

The check_to_create_blob() function used to check only the case
where we are applying to the working tree.  Rename the function to
check_to_create() and make it also responsible for checking the case
where we apply to the index.  Also make its caller responsible for
issuing an error message.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
WIP: apply --3way with add/add conflict

load_preimage() needs to be refactored so that we can read from the
patch->new_name before this change can work.

Also there currently is no check to avoid overwriting local changes, as
check_preimage() will not check anything for a patch that creates a new
path (the check is done in check_to_create() for the old name, but not
for the new name).
wip