rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.74k stars 2.42k forks source link

Add a custom Git merge tool for Cargo.locks and Cargo.tomls #1818

Open pcwalton opened 9 years ago

pcwalton commented 9 years ago

It's terribly annoying to have tons of Cargo.lock and Cargo.toml merge conflicts when rebasing pretty much any Rust PR on any large repo. It would be nice to have custom merge tools that automatically do the right thing here.

dwijnand commented 6 years ago

see "Defining a custom merge driver": https://git-scm.com/docs/gitattributes#_defining_a_custom_merge_driver

ishitatsuyuki commented 6 years ago

To add to this, NPM/Yarn have already implemented this feature officially.

Eh2406 commented 5 years ago

cc #5831

wycats commented 5 years ago

I really like this feature, and enjoy it a lot in yarn.

dwijnand commented 5 years ago

So there are actually two different solutions here, and I think we should follow npm's lead and implement both:

Here are some references:

thomaseizinger commented 5 years ago

From reading the documentation/PRs linked in this ticket, I get the following understanding (please correct at any stage if I am wrong):

Thoughts? If my understanding is correct, then I would start working on such a merge driver :)

mightyiam commented 2 years ago

It's unfortunate you only got reactions for this and not a real response, @thomaseizinger.

  • Custom git merge drivers are automatically invoked as soon as the conflict occurs.

I can confirm this from experience. Here's an example from npm: https://github.com/npm/npm-merge-driver

Hence we could start implementing this as a totally separate binary without needing any support for it in cargo.

Sounds like standard practice in Rust-land.

If the custom merge driver is activated in a repository, cargo would practically never see a lock file with merge conflicts again, unless the driver fails to resolve the conflicts.

Right. Emphasis on "unless". I don't expect this to always be able to resolve them.

  • Once the tool is mature enough, it could be included in cargo as a subcommand and cargo could automatically register the merge driver when it initializes a project.

That would be super sweet.

thomaseizinger commented 2 years ago

Thanks @mightyiam !

I have to admit, this was no longer on my radar at all. The latest lockfile version only rarely produces conflicts in my experience.

For Cargo.toml, I am not sure we can automatically resolve conflicts at all?

mightyiam commented 2 years ago

I haven't thought this through. I hope some smart people will!

epage commented 2 years ago

The latest lockfile version only rarely produces conflicts in my experience.

In case anyone else wants context on this, https://github.com/rust-lang/cargo/pull/7070 changed the lockfile to make it produce fewer conflicts

afranchuk commented 8 months ago

Note that https://github.com/relrelb/cargo-merge-driver exists, which seems to work (though it could maybe do with some updating of e.g. the beta clap version it's using).

Edit: updated deps and README at https://github.com/afranchuk/cargo-merge-driver

Kixunil commented 3 months ago

@afranchuk sadly, that's not a real merge driver, just a hack that always updates all packages to their latest versions. A real merger should parse all lock files, identify the changes and merge them roughly according to these rules:

Also FYI I made a merge tool to resolve use statements. It's quite sketchy so far but does work in some cases. I've since learned about merge drivers and I think it'd be better suitable as a driver.