@@ -39,6 +39,14 @@ function safe_filename() {
39
39
echo " $1 " | sed ' s/[^a-zA-Z0-9.]/_/g'
40
40
}
41
41
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
+
42
50
function build_ref {
43
51
local ref=" $1 "
44
52
local output_dir=" $2 "
@@ -63,28 +71,65 @@ function main {
63
71
validate_root_dir
64
72
validate_max_depth
65
73
66
- git config --global --add safe.directory " $GITHUB_WORKSPACE "
74
+ git_with_error_handling git config --global --add safe.directory " $GITHUB_WORKSPACE "
67
75
68
76
# Save current state to restore later
69
77
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 )"
71
111
72
112
# Build BASE output
73
113
local safe_base_ref=$( safe_filename " $INPUT_BASE_REF " )
74
114
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 )"
76
121
77
122
# Create a temporary merge branch
78
123
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
80
125
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 ) "
82
127
83
128
# 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
88
133
exit 1
89
134
fi
90
135
@@ -94,17 +139,18 @@ function main {
94
139
95
140
# Compare outputs
96
141
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 )
98
143
local diff_exit_code=$?
99
144
145
+ debug_log " Git diff exit code: $diff_exit_code "
100
146
debug_log " Git diff output:"
101
147
debug_log " $diff "
102
148
debug_log " End of git diff output"
103
149
debug_log " ------------------------------------"
104
150
105
151
# 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
108
154
109
155
if [[ $diff_exit_code -eq 0 ]]; then
110
156
output=" No differences found in kustomize output after merging $INPUT_HEAD_REF into $INPUT_BASE_REF "
0 commit comments