1+ #! /bin/bash
2+
3+ # Script to resolve searchindex.js conflicts by accepting master branch version
4+ # This script is designed to handle merge conflicts that occur when PRs become
5+ # desynchronized due to the auto-generated searchindex.js file.
6+ #
7+ # The searchindex.js file is automatically generated by the build process and
8+ # frequently causes conflicts when multiple PRs are waiting to be merged.
9+ # This script automatically resolves those conflicts by accepting the master
10+ # branch version of the file.
11+ #
12+ # Usage: resolve_searchindex_conflicts.sh <pr_number> <head_branch> <base_branch>
13+
14+ set -euo pipefail
15+
16+ # Validate arguments
17+ if [ $# -ne 3 ]; then
18+ echo " Usage: $0 <pr_number> <head_branch> <base_branch>"
19+ exit 1
20+ fi
21+
22+ PR_NUMBER=" $1 "
23+ HEAD_BRANCH=" $2 "
24+ BASE_BRANCH=" $3 "
25+
26+ # Validate required environment variables
27+ if [ -z " ${GITHUB_REPOSITORY:- } " ]; then
28+ echo " Error: GITHUB_REPOSITORY environment variable is required"
29+ exit 1
30+ fi
31+
32+ if [ -z " ${GH_TOKEN:- } " ]; then
33+ echo " Error: GH_TOKEN environment variable is required"
34+ exit 1
35+ fi
36+
37+ echo " Resolving conflicts for PR #$PR_NUMBER (branch: $HEAD_BRANCH -> $BASE_BRANCH )"
38+
39+ # Get current directory for safety
40+ ORIGINAL_DIR=$( pwd)
41+
42+ # Create a temporary directory for the operation
43+ TEMP_DIR=$( mktemp -d)
44+ echo " Working in temporary directory: $TEMP_DIR "
45+
46+ cleanup () {
47+ echo " Cleaning up..."
48+ cd " $ORIGINAL_DIR "
49+ rm -rf " $TEMP_DIR "
50+ }
51+ trap cleanup EXIT
52+
53+ # Clone the repository to the temp directory
54+ echo " Cloning repository..."
55+ cd " $TEMP_DIR "
56+ gh repo clone " $GITHUB_REPOSITORY " . --branch " $HEAD_BRANCH "
57+
58+ # Configure git
59+ git config user.email
" [email protected] " 60+ git config user.name " GitHub Action"
61+
62+ # Fetch all branches
63+ git fetch origin
64+
65+ # Make sure we're on the correct branch
66+ git checkout " $HEAD_BRANCH "
67+
68+ # Try to merge the base branch
69+ echo " Attempting to merge $BASE_BRANCH into $HEAD_BRANCH ..."
70+ if git merge " origin/$BASE_BRANCH " --no-edit; then
71+ echo " No conflicts found, merge successful"
72+
73+ # Push the updated branch
74+ echo " Pushing merged branch..."
75+ git push origin " $HEAD_BRANCH "
76+ exit 0
77+ fi
78+
79+ # Check what files have conflicts
80+ echo " Checking for conflicts..."
81+ conflicted_files=$( git diff --name-only --diff-filter=U)
82+ echo " Conflicted files: $conflicted_files "
83+
84+ # Check if searchindex.js is the only conflict or if conflicts are only in acceptable files
85+ acceptable_conflicts=true
86+ searchindex_conflict=false
87+
88+ for file in $conflicted_files ; do
89+ case " $file " in
90+ " searchindex.js" )
91+ searchindex_conflict=true
92+ echo " Found searchindex.js conflict (acceptable)"
93+ ;;
94+ * )
95+ echo " Found unacceptable conflict in: $file "
96+ acceptable_conflicts=false
97+ ;;
98+ esac
99+ done
100+
101+ if [ " $acceptable_conflicts " = false ]; then
102+ echo " Cannot auto-resolve: conflicts found in files other than searchindex.js"
103+ git merge --abort
104+ exit 1
105+ fi
106+
107+ if [ " $searchindex_conflict " = false ]; then
108+ echo " No searchindex.js conflicts found, but merge failed for unknown reason"
109+ git merge --abort
110+ exit 1
111+ fi
112+
113+ echo " Resolving searchindex.js conflict by accepting $BASE_BRANCH version..."
114+
115+ # Accept the base branch version of searchindex.js (--theirs refers to the branch being merged in)
116+ git checkout --theirs searchindex.js
117+ git add searchindex.js
118+
119+ # Check if there are any other staged changes from the merge
120+ staged_files=$( git diff --cached --name-only || true)
121+ echo " Staged files after resolution: $staged_files "
122+
123+ # Complete the merge
124+ if git commit --no-edit; then
125+ echo " Successfully resolved merge conflicts"
126+
127+ # Push the updated branch
128+ echo " Pushing resolved branch..."
129+ if git push origin " $HEAD_BRANCH " ; then
130+ echo " Successfully pushed resolved branch"
131+ exit 0
132+ else
133+ echo " Failed to push resolved branch"
134+ exit 1
135+ fi
136+ else
137+ echo " Failed to commit merge resolution"
138+ exit 1
139+ fi
0 commit comments