17

Ediff interface has + button, but it doesn't seem to do anything (the minibuffer invariably shows "nil" when I press it), I thought this button was meant for using both variants in the result buffer.

Anyhow, the original question is: how do I make Ediff resolve a merge conflict by putting the content from both buffers A and B into buffer C (I know I can just edit C, but would hope this can be done more easily).

Suppose file A is this:

Same line

Different line

Same line

while file B is:

Same line

Another line

Same line

I want the file C, the result of merging A with B to look like this:

Same line

Different line

Another line

Same line

  • I'm not sure I entirely understand the question, but this Ediff manual section on merging and diff3 may help. – Tianxiang Xiong Dec 14 '16 at 1:29
  • @TianxiangXiong I'll add an example of what I mean. I don't think the linked manual page covers it. – wvxvw Dec 14 '16 at 9:48
7

See this StackOverflow answer.

From @killdash9:

Pressing d will copy both A and B to buffer C.

(defun ediff-copy-both-to-C ()
  (interactive)
  (ediff-copy-diff ediff-current-difference nil 'C nil
                   (concat
                    (ediff-get-region-contents ediff-current-difference 'A ediff-control-buffer)
                    (ediff-get-region-contents ediff-current-difference 'B ediff-control-buffer))))
(defun add-d-to-ediff-mode-map () (define-key ediff-mode-map "d" 'ediff-copy-both-to-C))
(add-hook 'ediff-keymap-setup-hook 'add-d-to-ediff-mode-map)
  • Because the duplicate is also on the StackExchange network (and consequently if people can get to this site they will be able to get to that one as well), I'm in favour of not copying the code, but purely linking to the answer (as a general approach to handling cross-site duplicates). That way if the original answer is improved, there aren't outdated copies floating around on other sites. Up-voted, regardless. – phils Dec 15 '16 at 21:28
  • I was under the impression that StackExchange sites usually prefer to have an answer on the site itself, rather than simply a link. But maybe it's different if the link is another StackExchange site? /shrug – Tianxiang Xiong Dec 15 '16 at 22:27
  • I actually don't know what the official policy is, but I certainly think intra-network links should be treated differently to out-of-network links. Not using links in general means that the answer is accessible even if the remote source is unavailable, but I think it would be highly unlikely for only part of the StackExchange network to be available at any given time, so I think that argument goes away; at which point the question is whether it's sensible to divide the answers between two different duplicate questions, and I believe that's almost never helpful. IMO, anyhow. – phils Dec 16 '16 at 1:27
3

Edit: The duplicate Q&A on StackOverflow has answers to this question (as pointed out by Tianxiang Xiong in the comments).

My answer below is no use, as you evidentially can't get the desired result that way. I'm only not deleting it because the information it does give is somewhat relevant, and might still prove interesting information to someone.


Personally I do just edit the merge buffer directly, but you can teach Emacs what to do if you have a standard requirement.

+ calls ediff-combine-diffs:

Combine Nth diff regions of buffers A and B and place the combination in C. N is a prefix argument. If nil, combine the current difference regions. Combining is done according to the specifications in variable ediff-combination-pattern.

which is:

Pattern to be used for combining difference regions in buffers A and B. The value must be a list of the form (STRING1 bufspec1 STRING2 bufspec2 STRING3 bufspec3 STRING4) where bufspec is the symbol A, B, or Ancestor. For instance, if the value is '(STRING1 A STRING2 Ancestor STRING3 B STRING4) then the combined text will look like this:

STRING1
diff region from variant A
STRING2
diff region from the ancestor
STRING3
diff region from variant B
STRING4

Note that when you think it's doing nothing, it's actually combining the variants using the normal conflict markers. i.e. If you had already selected A or B, then by default + is a way to restore the original conflict.

  • Wouldn't my altering of ediff-combination-pattern remove the traditional conflict markings used by default? I'd really like to keep the conflict that way until I resolve it by using whichever combination of diffs from merged files. – wvxvw Dec 14 '16 at 11:16
  • The conflict markers originate outside of Emacs, so I doubt that's the case, but I don't actually know. Try it? – phils Dec 14 '16 at 11:22
  • I will, but later today. I'll update once I know. – wvxvw Dec 14 '16 at 11:25
  • In fact you're right -- ediff processes and converts all the conflict regions according to that pattern before presenting the buffers. You might, however, add a custom binding for a command which let-binds ediff-combination-pattern to your custom pattern and then calls ediff-combine-diffs. – phils Dec 14 '16 at 11:40
  • 2
    This solution seems to be what you want. – Tianxiang Xiong Dec 14 '16 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.