Search code examples
node.jsgitmergetree-conflict

how to get list of conflicted files before merging?


I am making a routine to be executed before every merge. It will run unit test and check other config files. FYI, it is coded in node.

At the end, if all is allright, it will check if there is any conflict with develop, and just output yes or no to user. Pull request should be manage on gitlab, not on local branch. So there shouldn't be a 'real' merge on the local branch.

Is there any git command available like git -conflitcs-with-this-branch ? And a way to retrive it from nodejs?

Thanks,

Stéphane.


Solution

  • The command that finds merge conflicts is git merge. Run git merge, optionally with --no-commit and optionally on a detached HEAD.

    If there are conflicts, git merge exits with a nonzero status. If there are no conflicts, git merge exits with a zero status. If run with --no-commit Git makes no commit in either case; if run without it, Git makes a merge commit on the current HEAD. If HEAD is detached, this commit affects only HEAD.

    If you used a detached HEAD, you can now re-attach HEAD to its original branch or commit.

    If you used --no-commit, you can simply run git merge --abort regardless of whether the merge succeeded or failed.

    Hence, the simplest command sequence is to run git merge --no-commit, save exit status, then run git merge --abort.

    Note that in all cases, Git will use the index and work-tree during the merge process, so you must make sure they are safe for Git to use for that entire period. (In particular the index and work-tree must be "clean" at the start of the process. They will be clean at the end as well, provided there are no merge drivers that leave messes behind.)

    (How to get the exit status of git merge from node.js, I have no idea.)