Skip to content

Commit

Permalink
Handle exit code in resolve-import-conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst committed Sep 25, 2023
1 parent a76a1a6 commit 32a4d41
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/scripts/merge_tools/gitmerge.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
#!/usr/bin/env sh

# usage: ./gitmerge.sh <clone_dir> <branch-1> <branch-2> <strategy>
# usage: ./gitmerge.sh <clone_dir> <branch-1> <branch-2> <strategy> [no-git-merge-abort]
# <clone_dir> must contain a clone of a repository.
# <strategy> is arguments to `git merge`, including -s and possibly -X.
# Merges branch2 into branch1, in <clone_dir>, using merge strategy <strategy>.
# Return code is 0 for merge success, 1 for merge failure.
# For merge failure, also outputs "Conflict" and aborts the merge.
# For merge success, return code is 0.
# For merge failure:
# * return code is 1.
# * outputs "Conflict".
# * aborts the merge, unless a 5th command-line argument is provided.

set -o nounset

if [ "$#" -ne 4 ]; then
echo "Usage: $0 CLONE_DIR BRANCH1 BRANCH2 STRATEGY" >&2
if [ "$#" -ne 4 ] && [ "$#" -ne 5 ]; then
echo "Usage: $0 CLONE_DIR BRANCH1 BRANCH2 STRATEGY [no-git-merge-abort]" >&2
exit 1
fi

clone_dir=$1
branch1=$2
branch2=$3
strategy=$4
# If this variable is non-empty, don't run `git merge --abort`.
no_git_merge_abort=$5

# perform merge
cd "$clone_dir" || exit 1
Expand All @@ -30,7 +35,9 @@ retVal=$?
# report conflicts
if [ $retVal -ne 0 ]; then
echo "Conflict"
git merge --abort
if [ -z "$no_git_merge_abort" ] ; then
git merge --abort
fi
fi

exit $retVal
14 changes: 12 additions & 2 deletions src/scripts/merge_tools/gitmerge_ort_imports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ clone_dir=$1
branch1=$2
branch2=$3
strategy="-s ort"
"$MERGE_SCRIPTS_DIR"/gitmerge.sh "$clone_dir" "$branch1" "$branch2" "$strategy"
"$MERGE_SCRIPTS_DIR"/gitmerge.sh "$clone_dir" "$branch1" "$branch2" "$strategy" "no-git-merge-abort"
status=$?

(cd "$clone_dir" && "$MERGE_SCRIPTS_DIR"/resolve-import-conflicts)
if ! "$status" ; then
(cd "$clone_dir" && "$MERGE_SCRIPTS_DIR"/resolve-import-conflicts)

# From https://stackoverflow.com/questions/41246415/
status=$(git diff --exit-code -S '<<<<<<< HEAD' -S "=======" -S ">>>>>>> $(git name-rev --name-only MERGE_HEAD)" HEAD)

git merge --abort
fi

exit "$status"
3 changes: 3 additions & 0 deletions src/scripts/merge_tools/resolve-import-conflicts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# parent 2 files, all without conflict markers.
# However, this script can be run instead of a git mergetool, or after a git mergetool.

# Exit status is 1 if merging some file halted exceptionally.
# With an exit status of 0, some conflicts may still exist in the current repository.

if [ "$#" -eq 0 ] ; then
readarray -t files < <(grep -l -r '^<<<<<<< HEAD' .)
else
Expand Down
6 changes: 5 additions & 1 deletion src/scripts/merge_tools/resolve-import-conflicts-in-file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#! /usr/bin/env python

"""Edits a file in place to remove conflict markers related to Java imports.
It simplistically leaves all the imports that appear in either parent.
The merged version contains all the imports that appear in either parent.
This is simplistic, but is often adequate.
Exit status is 1 only if the program halts exceptionally.
With an exit status of 0, some conflicts may still exist in the file.
"""


Expand Down

0 comments on commit 32a4d41

Please sign in to comment.