Skip to content

Commit 4ceac70

Browse files
authored
fix: improve branch reference handling and compare with merged state
2 parents a2444f0 + 643ac8d commit 4ceac70

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

kustdiff

+58-12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ function safe_filename() {
3939
echo "$1" | sed 's/[^a-zA-Z0-9.]/_/g'
4040
}
4141

42+
function git_with_error_handling() {
43+
if ! "$@"; then
44+
echo "Error executing git command: $*"
45+
return 1
46+
fi
47+
return 0
48+
}
49+
4250
function build_ref {
4351
local ref="$1"
4452
local output_dir="$2"
@@ -63,28 +71,65 @@ function main {
6371
validate_root_dir
6472
validate_max_depth
6573

66-
git config --global --add safe.directory "$GITHUB_WORKSPACE"
74+
git_with_error_handling git config --global --add safe.directory "$GITHUB_WORKSPACE"
6775

6876
# Save current state to restore later
6977
local current_branch
70-
current_branch=$(git rev-parse --abbrev-ref HEAD)
78+
current_branch=$(git rev-parse --abbrev-ref HEAD || echo "detached")
79+
80+
# Check if the branch exists locally or as a remote reference
81+
function resolve_branch_ref() {
82+
local branch_name="$1"
83+
84+
# First check if it's a local branch
85+
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
86+
echo "$branch_name"
87+
return 0
88+
fi
89+
90+
# Next check if it's a remote branch
91+
if git show-ref --verify --quiet "refs/remotes/origin/$branch_name"; then
92+
echo "origin/$branch_name"
93+
return 0
94+
fi
95+
96+
# Finally check if it's a valid commit SHA
97+
if git cat-file -e "$branch_name^{commit}" 2>/dev/null; then
98+
echo "$branch_name"
99+
return 0
100+
fi
101+
102+
# If we get here, we couldn't resolve the reference
103+
echo "Error: Could not resolve reference: $branch_name"
104+
return 1
105+
}
106+
107+
# Resolve the BASE and HEAD references
108+
local base_ref_resolved
109+
base_ref_resolved=$(resolve_branch_ref "$INPUT_BASE_REF") || exit 1
110+
debug_log "Resolved base reference: $base_ref_resolved (from $INPUT_BASE_REF)"
71111

72112
# Build BASE output
73113
local safe_base_ref=$(safe_filename "$INPUT_BASE_REF")
74114
local base_output_dir="$TMP_DIR/base"
75-
build_ref "$INPUT_BASE_REF" "$base_output_dir"
115+
build_ref "$base_ref_resolved" "$base_output_dir"
116+
117+
# Resolve HEAD reference
118+
local head_ref_resolved
119+
head_ref_resolved=$(resolve_branch_ref "$INPUT_HEAD_REF") || exit 1
120+
debug_log "Resolved head reference: $head_ref_resolved (from $INPUT_HEAD_REF)"
76121

77122
# Create a temporary merge branch
78123
local merge_branch="temp-merge-$RANDOM"
79-
git checkout -b "$merge_branch" "$INPUT_BASE_REF" --quiet
124+
git checkout -b "$merge_branch" "$base_ref_resolved" --quiet
80125

81-
debug_log "Creating temporary merge of $INPUT_HEAD_REF into $INPUT_BASE_REF"
126+
debug_log "Creating temporary merge of $head_ref_to_use into $base_ref_to_use (via $merge_branch)"
82127

83128
# Attempt to merge HEAD into BASE
84-
if ! git merge "$INPUT_HEAD_REF" --quiet; then
85-
echo "Merge conflict detected. Cannot automatically merge $INPUT_HEAD_REF into $INPUT_BASE_REF."
86-
git merge --abort
87-
git checkout "$current_branch" --quiet
129+
if ! git merge "$head_ref_to_use" --quiet; then
130+
echo "Merge conflict detected. Cannot automatically merge $head_ref_to_use into $base_ref_to_use."
131+
git merge --abort || true
132+
git checkout "$current_branch" --quiet || git checkout "$base_ref_to_use" --quiet || true
88133
exit 1
89134
fi
90135

@@ -94,17 +139,18 @@ function main {
94139

95140
# Compare outputs
96141
set +e
97-
diff=$(git diff --no-index "$base_output_dir" "$merged_output_dir")
142+
diff=$(git diff --no-index "$base_output_dir" "$merged_output_dir" 2>&1)
98143
local diff_exit_code=$?
99144

145+
debug_log "Git diff exit code: $diff_exit_code"
100146
debug_log "Git diff output:"
101147
debug_log "$diff"
102148
debug_log "End of git diff output"
103149
debug_log "------------------------------------"
104150

105151
# Clean up temporary branches
106-
git checkout "$current_branch" --quiet
107-
git branch -D "$merge_branch" --quiet
152+
git checkout "$current_branch" --quiet || git checkout "$base_ref_to_use" --quiet || true
153+
git branch -D "$merge_branch" --quiet || true
108154

109155
if [[ $diff_exit_code -eq 0 ]]; then
110156
output="No differences found in kustomize output after merging $INPUT_HEAD_REF into $INPUT_BASE_REF"

0 commit comments

Comments
 (0)