|
| 1 | +#!/bin/zsh |
| 2 | +set -e |
| 3 | + |
| 4 | +# gwf -- git workflow |
| 5 | +# |
| 6 | +# Very much a WORK IN PROGRESS |
| 7 | +# for my main/dev/grit branches workflow |
| 8 | + |
| 9 | +usage=' |
| 10 | +Usage: gwf [pr|release|reset|save|sync|delete|watch] |
| 11 | +
|
| 12 | +Work is done on `dev`, which is a short-lived branch off of `main`. |
| 13 | +The `main` branch is only modified through pull requests from `dev`. |
| 14 | +The commits from `dev` are cherry-picked onto `grit`, and then |
| 15 | +`dev` is deleted, and recreated as a new branch off of `main`. |
| 16 | +
|
| 17 | +When ready to add the changes to `main` (and create a new release) |
| 18 | +I run this script once for each of these primary steps: |
| 19 | +
|
| 20 | + pr Push dev, then open the github repo in a browser. |
| 21 | + This does not create a pull request automatically. |
| 22 | + The intent is to make the pull request in the browser, |
| 23 | + as well as squash-merge it into main. |
| 24 | + release Tag the main branch, triggering a release, |
| 25 | + then watch the latest github action |
| 26 | + reset This performs the sub-steps: save, sync, and |
| 27 | + then delete; aborting if any step fails. |
| 28 | +
|
| 29 | +These sub-steps can also be run independently: |
| 30 | +
|
| 31 | + save Save commits on dev branch to the grit branch. |
| 32 | + sync Sync-up the grit branch with the main branch. |
| 33 | + delete Delete the dev branch, then recreate it |
| 34 | + as a new branch off of the main branch. |
| 35 | +
|
| 36 | + watch Watch the latest github action. |
| 37 | + This step is automaically run by the release step, |
| 38 | + but it can also be used at any time by itself. |
| 39 | +
|
| 40 | +' |
| 41 | + |
| 42 | +main(){ |
| 43 | + [ -z "$(git status --porcelain)" ] || { |
| 44 | + print -u2 '❌ Repo must be in a clean state. It is not:' |
| 45 | + git status |
| 46 | + return 1 |
| 47 | + } |
| 48 | + local starting_branch |
| 49 | + starting_branch=$(git branch --show-current 2>/dev/null) || return 1 |
| 50 | + |
| 51 | + # ensure the script ends in the same branch it started in, if possible |
| 52 | + TRAPEXIT() { |
| 53 | + local exit_status=$? |
| 54 | + [[ -z "$starting_branch" ]] && return $exit_status |
| 55 | + git show-ref --verify --quiet "refs/heads/$starting_branch" && { |
| 56 | + git checkout "$starting_branch" &>/dev/null |
| 57 | + return $exit_status |
| 58 | + } |
| 59 | + print -u2 "\n⚠️ Starting branch '$starting_branch' no longer exists. Switching to main." |
| 60 | + git checkout main &>/dev/null || |
| 61 | + print -u2 "❌ Failed to switch to main. Check git status." |
| 62 | + return $exit_status |
| 63 | + } |
| 64 | + |
| 65 | + case ${1-} { |
| 66 | + pr) |
| 67 | + git push origin dev || return 1 |
| 68 | + gh browse |
| 69 | + ;; |
| 70 | + watch) watch_latest ;; |
| 71 | + release) |
| 72 | + tag_main ${@:2} || return 1 |
| 73 | + watch_latest |
| 74 | + ;; |
| 75 | + save) apply_dev_to_grit ;; |
| 76 | + sync) sync_grit_with_main ;; |
| 77 | + delete) delete_dev ;; |
| 78 | + reset) |
| 79 | + apply_dev_to_grit && |
| 80 | + sync_grit_with_main && |
| 81 | + delete_dev |
| 82 | + ;; |
| 83 | + *) |
| 84 | + print -u2 $usage |
| 85 | + return 2 |
| 86 | + ;; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +# On github: - make pull request from dev |
| 91 | +# - squash-merge it into main |
| 92 | +watch_latest(){ |
| 93 | + local latest_run |
| 94 | + latest_run="$( |
| 95 | + gh run list -L 1 --json databaseId -q '.[0].databaseId' |
| 96 | + )" && [[ -n $latest_run ]] || { |
| 97 | + print -u2 "❌ No GitHub actions found." |
| 98 | + return 1 |
| 99 | + } |
| 100 | + gh run watch $latest_run |
| 101 | +} |
| 102 | + |
| 103 | +tag_main(){ |
| 104 | + local patches |
| 105 | + patches="$( |
| 106 | + git tag --sort=-v:refname -n --list "v[0-9]*" | |
| 107 | + awk -F. 'NR=1{major=$1;minor=$2}$1==major&&$2==minor{print;next}{exit}' | |
| 108 | + tac |
| 109 | + )" |
| 110 | + local next_patch |
| 111 | + next_patch="$( |
| 112 | + <<<$patches awk -F'[.[:space:]]' ' |
| 113 | + {printf("%s.%d.%d",$1,$2,$3+1); exit} |
| 114 | + ' |
| 115 | + )" |
| 116 | + local version |
| 117 | + if [[ -n $1 ]] |
| 118 | + then version=$1 |
| 119 | + else |
| 120 | + print 'Patches for this minor version:' |
| 121 | + tac <<< $patches |
| 122 | + version=$next_patch |
| 123 | + vared -p "Enter version number: " version |
| 124 | + fi |
| 125 | + version=v${version#v} |
| 126 | + autoload -Uz is-at-least |
| 127 | + is-at-least $next_patch $version || { |
| 128 | + print -u2 "❌ Aborting: version must be at least $next_patch" |
| 129 | + return 2 |
| 130 | + } |
| 131 | + # get the latest main, but don't do any merging within the script |
| 132 | + git checkout main && git pull --ff-only origin main || return 1 |
| 133 | + git tag $version && |
| 134 | + git push origin main --tags |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +apply_dev_to_grit(){ |
| 139 | + # ensure we have the lastest version of the two branches |
| 140 | + # but don't do any merging within the script, just bail instead |
| 141 | + git checkout dev && git pull --ff-only origin dev || return 1 |
| 142 | + git checkout grit && git pull --ff-only origin grit || return 1 |
| 143 | + |
| 144 | + local fork_point="$(git merge-base main dev)" || return 1 |
| 145 | + |
| 146 | + print "🍒 Cherry-picking new commits from dev onto grit..." |
| 147 | + git cherry-pick "${fork_point}..dev" || { |
| 148 | + print -u2 '❌ Cherry-pick conflict detected!' |
| 149 | + print -u2 'Fix the conflicting files, run "git add .", and type "git cherry-pick --continue"' |
| 150 | + return 1 |
| 151 | + } |
| 152 | + git push origin grit |
| 153 | +} |
| 154 | + |
| 155 | +sync_grit_with_main(){ |
| 156 | + # ensure we have the lastest version of the two branches, |
| 157 | + # but don't do any merging within the script, just bail instead |
| 158 | + git checkout main && git pull --ff-only origin main || return 1 |
| 159 | + git checkout grit && git pull --ff-only origin grit || return 1 |
| 160 | + git merge main -s ours -m "Sync with 'main'" && |
| 161 | + git push origin grit |
| 162 | +} |
| 163 | + |
| 164 | +# Delete the dev branch and create a new one from main |
| 165 | +delete_dev(){ |
| 166 | + print -n "Sure you want to ERASE the dev branch? [y/N] " |
| 167 | + read -q || { |
| 168 | + print -u2 'Aborting.' |
| 169 | + return 1 |
| 170 | + } |
| 171 | + print "" |
| 172 | + git checkout main && |
| 173 | + git branch -D dev && |
| 174 | + git push origin --delete dev && |
| 175 | + git checkout -b dev && |
| 176 | + git push -u origin dev |
| 177 | +} |
| 178 | + |
| 179 | +main $@ |
0 commit comments