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
revision: "simplify" options imply topo-order sort

The code internally runs sort_in_topo_order() already; it is more clear
to spell it out in the option parsing phase, instead of adding a special
case in simplify_merges() function.
revision: note the lack of free() in simplify_merges()

Among the three similar-looking loops that walk singly linked
commit_list, the first one is only peeking and the same list is
later used for real work.  Leave a comment not to mistakenly
free its elements there.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
completion: warn people about duplicated function

The __gitdir function is duplicated between completion and prompt
scripts, and these definitions should not diverge; otherwise one of
them can be subtly broken depending on the order the user's shell
dot-sources them.

Leave a note to people who may want to touch one copy to make sure
they update the other one in sync.  Hopefully this line would also
appear in the context of the patch to allow reviewers to notice a
patch that attempts to update only one of them.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
*WRONG* revision: cull side parents before running simplify-merges

The simplify_merges() function needs to look at all history chain to
find the closest ancestor that is relevant after the simplification,
but after --first-parent traversal, side parents haven't been marked
for relevance (they are irrelevant by definition due to the nature
of first-parent-only traversal) nor culled from the parents list of
resulting commits.

Remove these side parents from parents list before starting to
further simplifying the result.

This patch is wrong, I think, as the output phase still wants to see
the parents (i.e. "git log" will no longer show "Merge X Y", I
suspect, but I didn't check). Instead, simplify_one() and its callees
need to learn to ignore the later parents.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Merge branch 'nd/stream-index-pack' into pu

Use streaming API to read from the object store to avoid having to hold
a large blob object in-core while running index-pack.

Not urgent; probably early post 1.7.11.

# By Nguyễn Thái Ngọc Duy
* nd/stream-index-pack:
  index-pack: use streaming interface for collision test on large blobs
  index-pack: factor out unpack core from get_data_from_pack
  index-pack: use streaming interface on large blobs (most of the time)
  index-pack: hash non-delta objects while reading from stream
Merge branch 'nd/stream-pack-objects' into pu

Not urgent; probably early post 1.7.11.

# By Nguyễn Thái Ngọc Duy
* nd/stream-pack-objects:
  pack-objects: use streaming interface for reading large loose blobs