|
28 | 28 | fi |
29 | 29 |
|
30 | 30 | if [ -z "$DIFF" ]; then |
| 31 | + # If HEAD was set from gh pr view but INPUT_HEAD is empty, use HEAD instead |
| 32 | + if [ -z "$INPUT_HEAD" ] && [ -n "$HEAD" ]; then |
| 33 | + INPUT_HEAD=$HEAD |
| 34 | + fi |
31 | 35 | echo "__ using branch name $INPUT_HEAD __" |
32 | | - git fetch origin main --depth 1 |
| 36 | + git fetch origin $INPUT_HEAD:refs/remotes/origin/$INPUT_HEAD |
33 | 37 | echo "__ running git diff __" |
34 | 38 |
|
35 | | - temp_file=$(mktemp) |
36 | | - git diff --name-only origin/main $INPUT_HEAD > "$temp_file" 2>/dev/null |
37 | | - GIT_EXIT_CODE=$? |
38 | | - |
39 | | - DIFF=$(cat "$temp_file") |
40 | | - rm -f "$temp_file" |
41 | | - |
42 | | - if [ $GIT_EXIT_CODE -ne 0 ]; then |
43 | | - echo "__ git diff failed with exit code $GIT_EXIT_CODE, fetching unshallow __" |
44 | | - git fetch --depth=100 origin $INPUT_HEAD |
45 | | - git diff --name-only origin/main $INPUT_HEAD > "$temp_file" 2>/dev/null |
46 | | - DIFF=$(cat "$temp_file") |
47 | | - rm -f "$temp_file" |
48 | | - else |
49 | | - echo "__ git diff succeeded __" |
50 | | - fi |
| 39 | + DIFF=$(git diff --name-only origin/main origin/$INPUT_HEAD) |
51 | 40 | fi |
52 | 41 |
|
53 | 42 | # So we can inspect the output |
@@ -79,20 +68,36 @@ FORMATTED_DIFF=$(echo "$DIFF" | tr '\n' ' ' | tr -s ' ' | sed 's/^ *//' | sed 's |
79 | 68 | echo "Formatted diff: '$FORMATTED_DIFF'" |
80 | 69 |
|
81 | 70 | # Set the output for GitHub Actions |
82 | | -if [[ -n "$INPUT_OUTPUT_FILE" ]]; then |
83 | | - ALL_FORMATTED=$(echo "$DIFF" | tr '\n' ' ' | tr -s ' ' | sed 's/^ *//' | sed 's/ *$//') |
| 71 | +ALL_FORMATTED=$(echo "$DIFF" | tr '\n' ' ' | tr -s ' ' | sed 's/^ *//' | sed 's/ *$//') |
| 72 | +HAS_CHANGES=true |
| 73 | +if [[ -z "$ALL_FORMATTED" ]]; then |
| 74 | + echo "No changed files detected" |
| 75 | + HAS_CHANGES=false |
| 76 | +fi |
| 77 | + |
| 78 | +# Function to set outputs either to a file or GITHUB_OUTPUT |
| 79 | +set_outputs() { |
| 80 | + local target=$1 |
84 | 81 |
|
85 | | - # Only set outputs if there are actually changed files |
86 | | - if [[ -z "$ALL_FORMATTED" ]]; then |
87 | | - echo "No changed files detected, setting empty outputs" |
88 | | - echo "all_changed_files=" >> "$INPUT_OUTPUT_FILE" |
89 | | - echo "filtered_changed_files=" >> "$INPUT_OUTPUT_FILE" |
| 82 | + if [[ "$HAS_CHANGES" == "false" ]]; then |
| 83 | + echo "Setting empty outputs to $target" |
| 84 | + echo "all_changed_files=" >> "$target" |
| 85 | + echo "filtered_changed_files=" >> "$target" |
90 | 86 | else |
91 | | - echo "Setting non-empty outputs" |
92 | | - echo "all_changed_files=$ALL_FORMATTED" >> "$INPUT_OUTPUT_FILE" |
93 | | - echo "filtered_changed_files=$FORMATTED_DIFF" >> "$INPUT_OUTPUT_FILE" |
| 87 | + echo "Setting non-empty outputs to $target" |
| 88 | + echo "all_changed_files<<EOF" >> "$target" |
| 89 | + echo "$ALL_FORMATTED" >> "$target" |
| 90 | + echo "EOF" >> "$target" |
| 91 | + |
| 92 | + echo "filtered_changed_files<<EOF" >> "$target" |
| 93 | + echo "$FORMATTED_DIFF" >> "$target" |
| 94 | + echo "EOF" >> "$target" |
94 | 95 | fi |
| 96 | +} |
| 97 | + |
| 98 | +# Set outputs to the appropriate target |
| 99 | +if [[ -n "$INPUT_OUTPUT_FILE" ]]; then |
| 100 | + set_outputs "$INPUT_OUTPUT_FILE" |
95 | 101 | else |
96 | | - echo "all_changed_files='$DIFF'" |
97 | | - echo "filtered_changed_files='$FORMATTED_DIFF'" |
| 102 | + set_outputs "$GITHUB_OUTPUT" |
98 | 103 | fi |
0 commit comments