Skip to content

Commit 48c3362

Browse files
committed
[WIP] Scripts: Add smarter merge conflicts logic
Signed-off-by: timflannagan <[email protected]>
1 parent 4313725 commit 48c3362

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

scripts/common.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#! /bin/bash
22

33
export KNOWN_GENERATED_PATHS=(':!vendor' ':!pkg/manifests' ':!manifests' ':!go.sum' ':!go.mod')
4+
export ROOT_GENERATED_PATHS=( "vendor" "pkg/manifests" "manifests" "go.mod" "go.sum" )
45
export UPSTREAM_REMOTES=("api" "operator-registry" "operator-lifecycle-manager")

scripts/sync.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fetch_remote() {
2929

3030
new_candidate_branch() {
3131
echo "Creating a sync branch if it doesn't already exist"
32-
git checkout -b "$SYNC_BRANCH_NAME" upstream/master 2>/dev/null || git checkout "$SYNC_BRANCH_NAME"
32+
git checkout -b "$SYNC_BRANCH_NAME" 2>/dev/null || git checkout "$SYNC_BRANCH_NAME"
3333
}
3434

3535
candidates() {

scripts/sync_pop_candidate.sh

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env bash
22

3-
cph=$(git rev-list -n 1 CHERRY_PICK_HEAD 2> /dev/null)
4-
53
set -o errexit
64
set -o pipefail
75

@@ -36,20 +34,44 @@ function pop() {
3634
fi
3735
printf 'popping: %s\n' "${rc}"
3836

39-
if [[ ! $cph ]]; then
40-
git cherry-pick --allow-empty --keep-redundant-commits -Xsubtree="${subtree_dir}" "${rc}"
41-
else
42-
if [[ $cph != "${rc}" ]]; then
43-
printf 'unexpected CHERRY_PICK_HEAD:\ngot %s\nexpected: %s\n' "${cph}" "${rc}"
44-
exit
45-
fi
46-
printf 'cherry-pick in progress for %s\n' "${cph}"
47-
git add .
37+
# Note: this may require a more up-to-date git version
38+
if ! git cherry-pick --allow-empty --keep-redundant-commits -Xsubtree="${subtree_dir}" "${rc}"; then
39+
# Always blast away the vendor directory given OLM/registry still commit it into source control.
40+
git rm -rf "${subtree_dir}"/vendor 2>/dev/null || true
41+
42+
# TODO: handle the case where there's a single merge conflict.
43+
# TODO: handle root go.mod merge conflicts!
44+
# TODO: maybe use a while loop here?
45+
# TODO: verify there's no remaining merge conflicts
46+
num_conflicts=$(git diff --name-only --diff-filter=U --relative | wc -l)
47+
while [[ $num_conflicts != 0 ]] ; do
48+
file=$(git diff --name-only --diff-filter=U --relative)
49+
50+
if [[ $file == *"go.mod"* ]]; then
51+
git diff "${subtree_dir}"/go.mod
52+
53+
git checkout --theirs "${subtree_dir}"/go.mod
54+
pushd "${subtree_dir}"
55+
# Note: handle case where this fails. does this update the root module too?
56+
go mod tidy
57+
# TODO: double-check there's no remaining markers via git diff --check: <https://stackoverflow.com/questions/3065650/whats-the-simplest-way-to-list-conflicted-files-in-git>?
58+
git add go.mod go.sum
59+
popd
60+
else
61+
git checkout --theirs "$file"
62+
git diff "$file"
63+
git add "$file"
64+
fi
65+
66+
num_conflicts=$(git diff --name-only --diff-filter=U --relative | wc -l)
67+
echo "number of merge conflicts remaining: $num_conflicts"
68+
done
4869

4970
if [[ -z $(git status --porcelain) ]]; then
5071
git commit --allow-empty
5172
else
52-
git cherry-pick --continue
73+
echo "Current cherry pick status: $(git status --porcelain)"
74+
git -c core.editor=true cherry-pick --continue
5375
fi
5476
fi
5577

@@ -59,8 +81,9 @@ function pop() {
5981
# 3. Ammend commit
6082
# 4. Remove from cherrypick set
6183
make vendor
84+
# TODO: make this check smarter and avoid manifest generation when it's not needed.
6285
make manifests
63-
git add "${subtree_dir}" "${KNOWN_GENERATED_PATHS[@]}"
86+
git add "${subtree_dir}" "${ROOT_GENERATED_PATHS[@]}"
6487
git status
6588
git commit --amend --allow-empty --no-edit --trailer "Upstream-repository: ${remote}" --trailer "Upstream-commit: ${rc}"
6689

@@ -72,9 +95,19 @@ function pop() {
7295
(( --remaining )) || true
7396
printf '%d picks remaining (pop_all=%s)\n' "${remaining}" "${pop_all}"
7497

98+
# no changes added to commit (use "git add" and/or "git commit -a")
99+
# [sync-2022-08-09 11f50a83b] add grokspawn to OWNERS (#2833)
100+
# Author: grokspawn <[email protected]>
101+
# Date: Tue Aug 9 13:44:11 2022 -0500
102+
# 1 file changed, 4 insertions(+), 2 deletions(-)
103+
# 0 picks remaining (pop_all=true)
104+
# ./scripts/../scripts/sync_pop_candidate.sh: line 108: unexpected EOF while looking for matching `''
105+
# make: *** [Makefile:191: sync] Error 2
106+
echo "Checking whether there's any remaining cherry picks"
75107
if [[ $pop_all == 'true' ]] && (( remaining > 0 )); then
76108
pop
77109
fi
110+
echo "Done"
78111
}
79112

80113
pop

0 commit comments

Comments
 (0)