Debian Codegraph @

cli/docs/conflicts.md

raw
1 # First-class conflicts
2
3 ## Introduction
4
5 Conflicts happen when Jujutsu can't figure out how to merge different changes
6 made to the same file. For instance, this can happen if two people are working
7 on the same file and make different changes to the same part of the file, and
8 then their commits are merged together with `jj new` (or one is rebased onto the
9 other with `jj rebase`).
10
11 Unlike most other VCSs, Jujutsu can record conflicted states in commits. For
12 example, if you rebase a commit and it results in a conflict, the conflict will
13 be recorded in the rebased commit and the rebase operation will succeed. You can
14 then resolve the conflict whenever you want. Conflicted states can be further
15 rebased, merged, or backed out. Note that what's stored in the commit is a
16 logical representation of the conflict, not conflict *markers*; rebasing a
17 conflict doesn't result in a nested conflict markers (see
18 [technical doc](technical/conflicts.md) for how this works).
19
20 ## Advantages
21
22 The deeper understanding of conflicts has many advantages:
23
24 * Removes the need for things like
25 `git rebase/merge/cherry-pick/etc --continue`. Instead, you get a single
26 workflow for resolving conflicts: check out the conflicted commit, resolve
27 conflicts, and amend.
28 * Enables the "auto-rebase" feature, where descendants of rewritten commits
29 automatically get rewritten. This feature mostly replaces Mercurial's
30 [Changeset Evolution](https://www.mercurial-scm.org/wiki/ChangesetEvolution).
31 * Lets us define the change in a merge commit as being compared to the merged
32 parents. That way, we can rebase merge commits correctly (unlike both Git and
33 Mercurial). That includes conflict resolutions done in the merge commit,
34 addressing a common use case for
35 [git rerere](https://git-scm.com/docs/git-rerere).
36 Since the changes in a merge commit are displayed and rebased as expected,
37 [evil merges](https://git-scm.com/docs/gitglossary/2.22.0#Documentation/gitglossary.txt-aiddefevilmergeaevilmerge)
38 are arguably not as evil anymore.
39 * Allows you to postpone conflict resolution until you're ready for it. You
40 can easily keep all your work-in-progress commits rebased onto upstream's head
41 if you like.
42 * [Criss-cross merges](https://stackoverflow.com/questions/26370185/how-do-criss-cross-merges-arise-in-git)
43 and [octopus merges](https://git-scm.com/docs/git-merge#Documentation/git-merge.txt-octopus)
44 become trivial (implementation-wise); some cases that Git can't currently
45 handle, or that would result in nested conflict markers, can be automatically
46 resolved.
47 * Enables collaborative conflict resolution. (This assumes that you can share
48 the conflicts with others, which you probably shouldn't do if some people
49 interact with your project using Git.)
50
51 For information about how conflicts are handled in the working copy, see
52 [here](working-copy.md#conflicts).
53
54 ## Conflict markers
55
56 Conflicts are "materialized" using *conflict markers* in various contexts. For
57 example, when you run `jj new` or `jj edit` on a commit with a conflict, it will
58 be materialized in the working copy. Conflicts are also materialized when they
59 are part of diff output (e.g. `jj show` on a commit that introduces or resolves
60 a conflict).
61
62 As an example, imagine that you have a file which contains the following text,
63 all in lowercase:
64
65 ```text
66 apple
67 grape
68 orange
69 ```
70
71 One person replaces the word "grape" with "grapefruit" in commit A, while
72 another person changes every line to uppercase in commit B. If you merge the
73 changes together with `jj new A B`, the resulting commit will have a conflict
74 since Jujutsu can't figure out how to combine these changes. Therefore, Jujutsu
75 will materialize the conflict in the working copy using conflict markers, which
76 would look like this:
77
78 ```text
79 <<<<<<< conflict 1 of 1
80 %%%%%%% diff from: vpxusssl 38d49363 "merge base"
81 \\\\\\\ to: rtsqusxu 2768b0b9 "commit A"
82 apple
83 -grape
84 +grapefruit
85 orange
86 +++++++ ysrnknol 7a20f389 "commit B"
87 APPLE
88 GRAPE
89 ORANGE
90 >>>>>>> conflict 1 of 1 ends
91 ```
92
93 The markers `<<<<<<<` and `>>>>>>>` indicate the start and end of a conflict
94 respectively. The marker `+++++++` indicates the start of a snapshot, while the
95 marker `%%%%%%%` indicates the start of a diff to apply to the snapshot. The
96 `\\\\\\\` marker just allows the label to be split across two lines to make it
97 more readable.
98
99 Therefore, to resolve this conflict, you would apply the diff (changing "grape"
100 to "grapefruit") to the snapshot (the side with every line in uppercase),
101 editing the file to look like this:
102
103 ```text
104 APPLE
105 GRAPEFRUIT
106 ORANGE
107 ```
108
109 In practice, conflicts are usually 2-sided, meaning that there's only 2
110 conflicting changes being merged together at a time, but Jujutsu supports
111 conflicts with arbitrarily many sides, which can happen when merging 3 or more
112 commits at once. In that case, you would see a single snapshot section and
113 multiple diff sections.
114
115 Compared to just showing the content of each side of the conflict, the main
116 benefit of Jujutsu's style of conflict markers is that you don't need to spend
117 time manually comparing the sides to spot the differences between them. This is
118 especially beneficial for many-sided conflicts, since resolving them just
119 requires applying each diff to the snapshot one-by-one.
120
121 ## Alternative conflict marker styles
122
123 If you prefer to just see the contents of each side of the conflict without the
124 diff, Jujutsu also supports a "snapshot" style, which can be enabled by setting
125 the `ui.conflict-marker-style` config option to "snapshot":
126
127 ```text
128 <<<<<<< conflict 1 of 1
129 +++++++ rtsqusxu 2768b0b9 "commit A"
130 apple
131 grapefruit
132 orange
133 ------- vpxusssl 38d49363 "merge base"
134 apple
135 grape
136 orange
137 +++++++ ysrnknol 7a20f389 "commit B"
138 APPLE
139 GRAPE
140 ORANGE
141 >>>>>>> conflict 1 of 1 ends
142 ```
143
144 Some tools expect Git-style conflict markers, so Jujutsu also supports [Git's
145 "diff3" style](https://git-scm.com/docs/git-merge#_how_conflicts_are_presented)
146 conflict markers by setting the `ui.conflict-marker-style` config option to
147 "git":
148
149 ```text
150 <<<<<<< rtsqusxu 2768b0b9 "commit A"
151 apple
152 grapefruit
153 orange
154 ||||||| vpxusssl 38d49363 "merge base"
155 apple
156 grape
157 orange
158 =======
159 APPLE
160 GRAPE
161 ORANGE
162 >>>>>>> ysrnknol 7a20f389 "commit B"
163 ```
164
165 This conflict marker style only supports 2-sided conflicts though, so it falls
166 back to the similar "snapshot" conflict markers if there are more than 2 sides
167 to the conflict.
168
169 ## Long conflict markers
170
171 Some files may contain lines which could be confused for conflict markers. For
172 instance, a line could start with `=======`, which looks like a Git-style
173 conflict marker. To ensure that it's always unambiguous which lines are conflict
174 markers and which are just part of the file contents, `jj` sometimes uses
175 conflict markers which are longer than normal:
176
177 ```text
178 <<<<<<<<<<<<<<< conflict 1 of 1
179 %%%%%%%%%%%%%%% diff from: wqvuxsty cb9217d5 "merge base"
180 \\\\\\\\\\\\\\\ to: kwntsput 0e15b770 "commit A"
181 -Heading
182 +HEADING
183 =======
184 +++++++++++++++ mpnwrytz 52020ed6 "commit B"
185 New Heading
186 ===========
187 >>>>>>>>>>>>>>> conflict 1 of 1 ends
188 ```
189
190 ## Conflicts with missing terminating newline
191
192 When materializing conflicts, `jj` outputs them in a line-based format, with
193 each conflict marker line terminated by a newline character (`\n`). This format
194 works well for text files that consist of a series of lines, with each line
195 terminated by a newline character as well.
196
197 While most text files follow this convention, some do not. Since conflict
198 markers must appear on their own line, when `jj` encounters a missing
199 terminating newline character in a conflict, it still needs to ensure that
200 there is a newline before the next conflict marker. To do this, it adds an
201 extra newline to each term of the conflict. Then, to compensate for this extra
202 newline, it also omits the terminating newline from the "end of conflict"
203 marker (`>>>>>>>`).
204
205 For instance, if a file originally contained `grape` with no terminating newline
206 character, and one person changed `grape` to `grapefruit`, while another person
207 added the missing newline character to make `grape\n`, the resulting conflict
208 would look like this (`␊` demonstrates the `\n` character explicitly; note that
209 the last line doesn't have the terminating `\n` character):
210
211 ```text
212 <<<<<<< conflict 1 of 1␊
213 +++++++ tlwwkqxk d121763d "commit A" (no terminating newline)␊
214 grapefruit␊
215 %%%%%%% diff from: qwpqssno fe561d93 "merge base" (no terminating newline)␊
216 \\\\\\\ to: poxkmrxy c735fe02 "commit B"␊
217 grape␊
218 +␊
219 >>>>>>> conflict 1 of 1 ends
220 ```
221
222 Therefore, a resolution of this conflict could be `grapefruit\n`, with the
223 terminating newline character added.