Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain merge #3279

Closed
3 tasks done
philippludwig opened this issue Oct 2, 2023 · 3 comments
Closed
3 tasks done

Explain merge #3279

philippludwig opened this issue Oct 2, 2023 · 3 comments
Labels
support Support request

Comments

@philippludwig
Copy link

philippludwig commented Oct 2, 2023

What exactly are you trying to do?

Merge a file that has been edited on two machines.

What have you tried so far?

chezmoi merge

Where else have you checked for solutions?

Output of any commands you've tried with --verbose flag

$ chezmoi --merge ~/.emacs

<Copying vim output is a bit too much I think>

Output of chezmoi doctor

RESULT    CHECK                       MESSAGE
ok        version                     v2.40.0, commit 6a8ca1634654734bb33a036ffb9c21e6b9f4d28d, built at 2023-09-19T09:56:08Z, built by goreleaser
ok        latest-version              v2.40.0
ok        os-arch                     linux/amd64 (Debian GNU/Linux 12 (bookworm))
ok        uname                       Linux lenovo 6.5.5-2-liquorix-amd64 #1 ZEN SMP PREEMPT liquorix 6.5-6.1~bookworm (2023-09-30) x86_64 GNU/Linux
ok        go-version                  go1.21.1 (gc)
ok        executable                  ~/.local/bin/chezmoi
ok        upgrade-method              replace-executable
ok        config-file                 ~/.config/chezmoi/chezmoi.toml, last modified 2023-06-20T14:59:00+02:00
warning   source-dir                  ~/.local/share/chezmoi is a git working tree (dirty)
ok        suspicious-entries          no suspicious entries
warning   working-tree                ~/.local/share/chezmoi is a git working tree (dirty)
ok        dest-dir                    ~ is a directory
ok        umask                       022
ok        cd-command                  found /bin/bash
ok        cd-args                     /bin/bash
info      diff-command                not set
ok        edit-command                found /usr/bin/emacsclient
ok        edit-args                   /usr/bin/emacsclient
ok        git-command                 found /usr/bin/git, version 2.39.2
ok        merge-command               found /usr/bin/vimdiff
ok        shell-command               found /bin/bash
ok        shell-args                  /bin/bash
info      age-command                 age not found in $PATH
ok        gpg-command                 found /usr/bin/gpg, version 2.2.40
info      pinentry-command            not set
info      1password-command           op not found in $PATH
info      bitwarden-command           bw not found in $PATH
info      bitwarden-secrets-command   bws not found in $PATH
info      dashlane-command            dcli not found in $PATH
info      doppler-command             doppler not found in $PATH
info      gopass-command              gopass not found in $PATH
info      keepassxc-command           keepassxc-cli not found in $PATH
info      keepassxc-db                not set
info      keeper-command              keeper not found in $PATH
info      lastpass-command            lpass not found in $PATH
ok        pass-command                found /usr/bin/pass, version 1.7.4
info      passhole-command            ph not found in $PATH
info      rbw-command                 rbw not found in $PATH
info      vault-command               vault not found in $PATH
info      vlt-command                 vlt not found in $PATH
info      secret-command              not set

Additional context

So "now" (since 2019 it seems) chezmoi merge is here and I tried to merge my .emacs file that I modified on machine A, pushed and then modified on machine B before pulling there. Silly me, I know.

Therefore I ran chezmoi merge, then vimdiff opens and I have no clue what to do. Consulting the chezmoi user guide does not help, it just states that I could use other tools, but I don’t know which are easier. I know my way around Vim, so I tried the vimdiff integrated help, but that did not clear things up. I also have a really hard time to wrap my head around "destination"/"target"/"source", what does that even mean? I was expecting a "normal" git-like merge, i.e. when I pull some repository and have a merge conflict.

I have been using git for 10 years, but never had to do a 3-way merge. I usually get away by running git stash, git pull, git stash pop and then resolve the conflicts manually, which is something I would like to do here.

Since I could not find any satisfying tutorial to vimdiff online, I just tried something and of course it destroyed my file completely.

So here is my request: Could there be some noob-friendly explanation on how to use merge in the user guide? Or a stash feature?

Because right now, all I want to do is have the changes in my ~/.emacs file that I made on both machines.

@philippludwig philippludwig added the support Support request label Oct 2, 2023
@halostatue
Copy link
Collaborator

merge is a fairly advanced tool and providing a specific vimdiff tutorial would be inappropriate for systems where vimdiff is not by default available.

The following tutorial is reasonable but not entirely useful for chezmoi: https://www.rosipov.com/blog/use-vimdiff-as-git-mergetool/

Chezmoi does not have four panes (the "merge result" on the bottom, usually), only three (it does not have the merge result). What shows up in the three panes for chezmoi merge[-all] is:

  1. Destination file (the version you edited outside of chezmoi)
  2. Source file (the version in $CHEZMOISOURCE)
  3. Target file (the version that chezmoi would like to write to the Destination file)

You are viewing a simultaneous diff with all three versions. The one where you need to make changes is the middle one (Source), because if the source file is a template, this is the template. I usually do :bd 3 followed by <C-w>= to delete the target version since I don't need to see what that would be. With a template that pulls all of its shape from an external source, the Target file is useful to compare.

You can navigate diffs (]c next change and [c previous change) in any of the three windows, but I recommend doing so either in the Destination or Source files only. Which one you will be doing this in will vary and you can only get that feeling through experience. Remember, your goal is to modify the middle window, the Source file, so you are most likely going to be navigating in buffer 2.

You can pull changes from a buffer to the source by being in buffer 2 (Source) and doing d1o (diff, buffer 1, obtain). You can push changes from a buffer to the source by being in the other buffer (Target, usually) and doing d2p (diff, buffer 2, push). You may need to use both, because diffs at the top or the bottom may require you be in the buffer where the chunk is that you're replacing.

Sometimes, and this is especially true with templates, you will need to manually edit the Source template — especially if it's a template formatting error (you forgot a | quote or something).

By setting the chezmoi.$FORMAT.merge.command configuration you can change your merge tool as well. You may need to set chezmoi.$FORMAT.merge.args, this seems to be especially necessary if you were to use VSCode.

I don’t use emacs, but you should be able to configure it as the merge command (the following links are for git merge tool &c., but it would be similar for chezmoi.$FORMAT.merge.command):

@benmezger has written some tooling to help use chezmoi with emacs: https://seds.nl/notes/chezmoi-and-emacs/; he may be able to provide more help on that. (There is also https://github.com/tuh8888/chezmoi.el.)

Here is a video on 3-way merges — but be warned that it's basically the first video on such I found via DDG: https://www.youtube.com/watch?v=OYhAG-_PY6I

I don’t see us adding this to chezmoi documentation, but I will leave this open to see if (a) what I have provided helps and (b) if anyone else can provide more specific assistance.

@twpayne
Copy link
Owner

twpayne commented Oct 4, 2023

Thank you for a very kind and complete answer @halostatue.

@philippludwig please re-open this issue if you have further questions.

@twpayne twpayne closed this as completed Oct 4, 2023
@philippludwig
Copy link
Author

Everything is clear now, thanks @halostatue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support Support request
Projects
None yet
Development

No branches or pull requests

3 participants