navid/veins / 89aa02ed6b6660b6cf09e1a7700cf3b020a1b1e2bd33fd14dace8eb1a92db071 / docs/MERGE_CONFLICTS.adoc

MERGE_CONFLICTS.adoc

← docs · 7366 bytes · raw · download

Showing source. View rendered.

= Understanding Merge Conflicts in veins

This guide shows, step by step, how veins represents, materializes, and
resolves merge conflicts.

The examples use the command `vn`. If you built the development binary in this
repository, define an alias in your interactive shell first:

[source,sh]
----
# Run this from the veins checkout.
alias vn="$PWD/vn-dev"
vn version
----

If you already installed a release build, just use `vn` directly.

== The short version

veins never inserts `<<<<<<<` markers into your files. A conflict is stored as
a value in history. In the working copy it looks like this:

[source,text]
----
story.txt          clean file, conflicting regions use "ours"
story.txt.base     the common ancestor version
story.txt.ours     the current working-copy side
story.txt.theirs   the other side
----

`vn xs` lists conflicted paths. `vn resolve` records the resolution.

== Step 1: Create a repository and a base change

[source,sh]
----
mkdir conflict-demo
cd conflict-demo

vn init .
printf 'line 1\nline 2\nline 3\n' > story.txt

vn new
vn desc -m "base"
vn bmk set main
----

`vn new` snapshots the files and starts a new change. `vn desc` names that
change. `vn bmk set main` gives the current commit the bookmark `main`.

== Step 2: Create the "ours" change

[source,sh]
----
vn new
printf 'ours: line 1\nline 2\nline 3\n' > story.txt
vn desc -m "ours"
vn bmk set feature
----

The working copy now contains the `ours` version of the file.

== Step 3: Create the "theirs" change from the same base

[source,sh]
----
vn co main
vn new main
printf 'theirs: line 1\nline 2\nline 3\n' > story.txt
vn desc -m "theirs"
----

We are now on a different commit. The two changes `ours` and `theirs` both
modified the first line of `story.txt` from the same base.

== Step 4: Merge

[source,sh]
----
vn merge feature
----

The merge succeeds. It does not stop with an error. Check the result:

[source,sh]
----
vn status
vn xs
----

You should see something like:

[source,text]
----
C story.txt
1 conflict(s)
1 conflicted commit(s) in ancestry
story.txt: text conflict (base 1-1, ours 1-1, theirs 1-1)
----

The `C` means the path is conflicted in the working-copy commit.

For machine-readable output:

[source,sh]
----
vn xs -porcelain
----

The columns are tab-separated:

[source,text]
----
path<TAB>kind<TAB>base-ranges<TAB>ours-ranges<TAB>theirs-ranges
----

Example:

[source,text]
----
story.txt	text	1-1	1-1	1-1
----

== Step 5: Look at the files on disk

[source,sh]
----
ls story.txt*
cat story.txt
cat story.txt.base
cat story.txt.ours
cat story.txt.theirs
----

You should see something like:

[source,text]
----
story.txt          -> theirs: line 1\nline 2\nline 3
story.txt.base     -> line 1\nline 2\nline 3
story.txt.ours     -> theirs: line 1\nline 2\nline 3
story.txt.theirs   -> ours: line 1\nline 2\nline 3
----

Important: "ours" in a merge always means the current working-copy side, not a
branch name. Here the working-copy side is the change named `theirs`, so
`story.txt.ours` contains the `theirs:` line.

`story.txt` itself is a normal clean file. Conflicting regions are resolved to
the `ours` side, but no conflict markers are inserted.

Sidecar files are never snapshotted while the path is conflicted. They are
working-copy materialization only.

== Step 6: Resolve the conflict

There are several ways to resolve it.

=== Take one side

Take the current working-copy side:

[source,sh]
----
vn resolve -take ours story.txt
cat story.txt
----

Take the other side:

[source,sh]
----
vn resolve -take theirs story.txt
cat story.txt
----

Take the common base version:

[source,sh]
----
vn resolve -take base story.txt
cat story.txt
----

=== Resolve manually

Edit the file to exactly what you want, then tell veins to accept it:

[source,sh]
----
printf 'resolved: line 1\nline 2\nline 3\n' > story.txt
vn resolve story.txt
cat story.txt
----

=== Use an external merge tool

`vn resolve -tool` substitutes `$BASE`, `$OURS`, `$THEIRS`, and `$OUTPUT` in
the command. For example, to take the `ours` side using `cp`:

[source,sh]
----
vn resolve -tool 'cp $OURS $OUTPUT' story.txt
----

== Step 7: Verify the resolution

[source,sh]
----
vn xs
ls story.txt*
vn status
----

`vn xs` should now report:

[source,text]
----
no conflicts
----

The sidecar files should be gone. This command reports them if they still
exist, or prints a confirmation otherwise:

[source,sh]
----
ls story.txt.base story.txt.ours story.txt.theirs 2>/dev/null || echo "sidecars removed"
----

The resolution is a new operation. You can inspect the operation log:

[source,sh]
----
vn op log
----

You can also inspect the history and the merge commit:

[source,sh]
----
vn log -no-col-titles
vn diff
----

After resolution, `vn status` may still show a change relative to the merge
commit's first parent. That is normal. The important part is that the path is no
longer conflicted.

== Step 8: Undo if needed

Every state-changing command, including a merge and a resolution, is an
operation:

[source,sh]
----
vn op log
vn undo
----

`vn undo` restores the previous operation view. Resolution does not require you
to get it right the first time.

== Optional: resolution memory

veins remembers exact conflict resolutions in `.veins/resolutions/`:

[source,sh]
----
ls .veins/resolutions
----

If the exact same conflict appears again later, veins can reuse the recorded
resolution automatically. The operation description then includes a note such
as:

[source,text]
----
merge feature (rerere 1)
----

This is called resolution memory (`merge.rerere`, enabled by default).

== What is happening internally

For a path, a three-way merge is represented as:

[source,text]
----
ours + theirs - base
----

For `story.txt`, that is:

[source,text]
----
(theirs version) + (ours version) - (base version)
----

Because both sides changed the same line differently, the values do not cancel.
veins stores the unresolved sum as a conflict object and puts a conflict entry
in the merged tree. A commit containing such an entry is a normal commit; it can
be described, rebased, or built upon.

When you run `vn resolve`, veins replaces the conflict entry with the blob you
chose or edited. The conflict object remains in history, but the new working-copy
commit no longer points at it for that path.

== Common questions

=== Why is there no marker in the file?

veins stores conflicts as values, not as text inserted into files. The clean
file plus `.base`, `.ours`, and `.theirs` sidecars give you the same information
without corrupting the file content.

=== How do I list conflicts?

[source,sh]
----
vn xs
vn xs -porcelain
----

`vn resolve -list` is an alias for `vn xs`.

=== Why did the merge not fail?

A merge in veins never fails. It always produces a commit. If the values cannot
be merged, the commit contains conflict values and the working copy shows
sidecars.

=== What does "ours" mean?

In a merge, `ours` is the current working-copy side. If you are on commit `A`
and run `vn merge B`, then `ours` is `A` and `theirs` is `B`, regardless of
branch or bookmark names.

=== What happens to the sidecars when I edit the conflicted file?

While the path is conflicted, the file on disk is a draft. Running
`vn resolve <path>` accepts the current file content as the resolution. The
sidecars are removed once the conflict is resolved.