forked from ytdl-org/ytdl-patched-rebase-upstream-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
71 lines (68 loc) · 2.63 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
name: Reverse Rebase from Upstream
description: Use git rebase to append new upstream commits
# inverted logic from imba-tjd/rebase-on-upstream
branding:
icon: git-pull-request
color: blue
inputs:
upstream:
type: string
description: <user>/<repo> or full HTTP URL; default is parent repo
required: false
branch:
type: string
description: The upstream branch that is rebased from
required: false
default: master
push:
type: boolean
description: Do the force push in this action
required: false
default: true
token:
type: string
description: GitHub Access Token
required: true
default: ${{ github.token }}
permissions:
contents: write
runs:
using: composite
steps:
- name: Main
shell: bash
run: |
set -ex
# Starting with current git branch (TARGET), get the UPSTREAM branch
# as a temporary branch; rebase TARGET onto it; if that differs from TARGET,
# make it the TARGET and push the result, if the remote hasn't changed.
# Effect: commits specific to the TARGET repo stay in place; new UPSTREAM
# commits are appended
TARGET_SHA=$(git rev-parse HEAD)
TARGET=$(git name-rev --name-only --exclude='tags/*' "$TARGET_SHA")
if [ -z "$TARGET" ]; then echo "Can't find current branch" >&2; exit 1; fi
UPSTREAM=${{ inputs.upstream }}
if [ -z "$UPSTREAM" ]; then
echo "${{ inputs.token }}" | gh auth login --with-token
UPSTREAM=$(gh api repos/:owner/:repo --jq .parent.full_name)
if [ -z "$UPSTREAM" ]; then echo "Can't find upstream" >&2 && exit 1; fi
fi
if ! echo "$UPSTREAM" | grep -E '^(http|git@)'; then
UPSTREAM=https://github.com/$UPSTREAM.git
fi
git remote add -f -t "${{ inputs.branch }}" --tags upstream "$UPSTREAM"
REBASE=${TARGET}-rebase
# avoid leaving an unwanted branch "$REBASE"
trap '{ git checkout "$TARGET"; git branch -D "$REBASE" || true; }' EXIT
git checkout -B "$REBASE" --no-track "upstream/${{ inputs.branch }}"
git config --local user.email "rebaser+github-actions[bot]@users.noreply.github.com"
git config --local user.name "GitHub Actions"
# rebase new UPSTREAM commits onto TARGET
git rebase "refs/heads/${TARGET}"
if [ -n "$(git log --max-count 1 --oneline "HEAD...${TARGET}")" ]; then
git branch -M "$REBASE" "$TARGET"
if [ "${{ inputs.push }}" = true ]; then
# rebase changed TARGET: push if remote TARGET unchanged
git push origin --force-with-lease="${TARGET}"
fi
fi