Search code examples
clibgit2

How to get the file name of the failed switch branch in libgit2?


I can call the switch branch interface normally, but when the switch branch fails, I cannot get the specific file of the current branch failure. Viewing error info only shows "one or more conflict prevents checkout", if I want to get the detailed error file name, How to get detailed error information from the callback function or return value? (also include:Merge、Reset...)

// code

git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE;

git_branch_lookup(&lookup, repo, branchname, GIT_BRANCH_LOCAL);

git_revparse_single(&treeish, repo, branchbane);

if(git_checkout_tree(repo, treeish, &opts)<0)

{
   /*
     just return "1 conflict prevents checkout",
     But I want to know which files is wrong
   */
   const git_error* error = giterr_last(); 
}

Solution

  • You'll need to set a notify_cb in your git_checkout_options, and set your notify_flags to include GIT_CHECKOUT_NOTIFY_CONFLICT .

    Your notify callback that you provide will be invoked with the files that are changed in your working directory and preventing the checkout from occurring.