0

I'm automating changing files in github using pygit2. Sometimes the files have changed in github while I am processing a repo so I want to pull() before I push().

Since this is automated I would like to avoid conflicts by either having my local changes always override the remote, or vice-versa. This seems like a very simple scenario but after hours of scouring the internet for examples I have found 0 examples of someone doing this. The pygit source itself has some examples that get close but the "handle conflicts" portion is just a "TODO" comment.

It looks like pygit2 should support it, but none of the APIs seem to do this.

For example,

Repository.merge_commits(ours, theirs, favor='normal', flags={}, file_flags={})

When I set favor="theirs" or favor="ours" and purposely force a conflict I still get conflicts.

I tried this:

ancestor_id = repo.merge_base(repo.head.target,remote_master_id)
repo.merge_trees(ancestor_id,repo.head,remote_master_id,favor="theirs")

No conflict now, but now I somehow end up with the repo in a state where both (ours and theirs) changes are in the commit history but the file itself is missing either change.

I'm just guessing here since I have no clue what merge_trees does (except "merge trees") and experimenting with values of ancestor_id.

Is there a way to get pygit2 to get it to do what I want?

  • 1
    What did you do to "purposely force a conflict"? Certain kinds of conflicts cannot be resolved by ours/theirs strategies. For example, an update to both sides can be auto-resolved, but an update vs a delete cannot. – TTT Aug 02 '22 at 03:54
  • I mean a local file contains a single line x=1, the remote contains the same file with a single line x=2. I want to either accept the remote or local version. – Eric Van Bezooijen Aug 02 '22 at 05:29
  • Let me put it another way. I don't mind having conflicts but then I want an easy way to overwrite the file with the conflict with either the local or remote version and I would like to not have to keep track of multiple cloned repos. I could also not see any easy way to tell pygit2 for each conflicted file - grab the local or remote version, add it to build a commit and then commit all those changes. checkout seems to be for the entire repo not a single file. – Eric Van Bezooijen Aug 02 '22 at 05:51

0 Answers0