0
votes

i try to implement a UI for Git in C#. I use the Libgit2Sharp library version 0.25.2. At the moment, I keep deliberately creating conflicts so that I can construct the user interface in such a way that they are easy to resolve. So now if i try to make a pull with Libgit2Sharp it throws a CheckoutConflictException. The question Show conflicts when pulling using libgit2sharp helped me to get detailed information, which file is conflicted, my code for a checkout is:

public void Checkout()
{
    try
    {
        Debug.Log("starting checkout");
        CheckoutOptions opts = new CheckoutOptions();
        opts.OnCheckoutNotify = (string path, CheckoutNotifyFlags notify) =>
        {
            switch (notify)
            {
                case CheckoutNotifyFlags.Conflict:
                    Debug.Log("conflicted file found at " + path);
                    return true;
                case CheckoutNotifyFlags.Ignored:
                    Debug.Log("ignored file found at: " + path);
                    return true;
                case CheckoutNotifyFlags.None:
                    Debug.Log("unchanged file found at: " + path);
                    return true;
                case CheckoutNotifyFlags.Untracked:
                    Debug.Log("untracked file found at: " + path);
                    return true;
                case CheckoutNotifyFlags.Updated:
                    Debug.Log("updated file found at: " + path);
                    return true;
                default:
                    Debug.Log("dirty file at: " + path);
                    return true;
            }
        };
        opts.CheckoutNotifyFlags = CheckoutNotifyFlags.None |
                                   CheckoutNotifyFlags.Dirty |
                                   CheckoutNotifyFlags.Ignored |
                                   CheckoutNotifyFlags.Conflict |
                                   CheckoutNotifyFlags.Untracked |
                                   CheckoutNotifyFlags.Updated;


        Branch actualRemote = Repo.Branches["origin/" + Repo.Head.FriendlyName];
        Commands.Checkout(Repo, actualRemote, opts);
    }
    catch (CheckoutConflictException e)
    {
        if (e.InnerException != null) Debug.Log(e.InnerException.Message);
        Debug.Log(e.Message);
    }
}

Unfortunately the catch-clause will be still triggered. So what can i do with the conflicted files? The pull command can't merge them, can i somehow make a git stash? I can't find anything about it in the documentation, or at least an example at https://github.com/libgit2/libgit2sharp/wiki/LibGit2Sharp-Hitchhiker%27s-Guide-to-Git.

Since if there is a conflicted file and it will be deleted, the git pull command in the console works, so i have also tried this workaround in my code: At the point, where a conflict is detected with OnCheckoutNotify, i just delete the file in my workdir after its content is saved. But then if i perform a pull i still get the CheckoutConflictException with the same message: "1 conflict prevents the checkout". The pull-command works fine, if there are no conflicts.

Any ideas?

1

1 Answers

1
votes

The stash option works, and you can put the pending changes to the stash like this:

repo.Stashes.Add(new Signature("Your Name", "[email protected]", DateTimeOffset.Now), "Optional stash comment here"));

Then pull and then pop the changes off the stash:

repo.Stashes.Pop(0);

You could also do a hard reset, if the user is ok with it:

repo.Reset(ResetMode.Hard, repo.Branches.First(b => b.IsCurrentRepositoryHead).Tip);