File: pr2wt.sh

package info (click to toggle)
llama.cpp 8064%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 76,488 kB
  • sloc: cpp: 353,828; ansic: 51,268; python: 30,090; lisp: 11,788; sh: 6,290; objc: 1,395; javascript: 924; xml: 384; makefile: 233
file content (85 lines) | stat: -rwxr-xr-x 2,199 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env bash

# intialize a new worktree from a PR number:
#
# - creates a new remote using the fork's clone URL
# - creates a local branch tracking the remote branch
# - creates a new worktree in a parent folder, suffixed with "-pr-$PR"
#
# sample usage:
#   ./scripts/pr2wt.sh 12345
#   ./scripts/pr2wt.sh 12345 opencode
#   ./scripts/pr2wt.sh 12345 "cmake -B build && cmake --build build"
#   ./scripts/pr2wt.sh 12345 "bash -l"

function usage() {
    echo "usage: $0 <pr_number> [cmd]"
    exit 1
}

# check we are in the right directory
if [[ ! -f "scripts/pr2wt.sh" ]]; then
    echo "error: this script must be run from the root of the repository"
    exit 1
fi

if [[ $# -lt 1 || $# -gt 2 ]]; then
    usage
fi

PR=$1
[[ "$PR" =~ ^[0-9]+$ ]] || { echo "error: PR number must be numeric"; exit 1; }

url_origin=$(git config --get remote.upstream.url 2>/dev/null) || \
url_origin=$(git config --get remote.origin.url) || {
    echo "error: no remote named 'upstream' or 'origin' in this repository"
    exit 1
}

# Extract org/repo from either https or ssh format.
if [[ $url_origin =~ ^git@ ]]; then
    org_repo=$(echo $url_origin | cut -d: -f2)
else
    org_repo=$(echo $url_origin | cut -d/ -f4-)
fi
org_repo=${org_repo%.git}

echo "org/repo: $org_repo"

meta=$(curl -sSLf -H "Accept: application/vnd.github+json" "https://api.github.com/repos/$org_repo/pulls/$PR")

url_remote=$(echo "$meta" | jq -r '.head.repo.clone_url')
head_ref=$(echo "$meta" | jq -r '.head.ref')

echo "url:      $url_remote"
echo "head_ref: $head_ref"

url_remote_cur=$(git config --get "remote.pr/$PR.url" 2>/dev/null || true)

if [[ "$url_remote_cur" != "$url_remote" ]]; then
    git remote rm  pr/$PR 2> /dev/null
    git remote add pr/$PR "$url_remote"
fi

git fetch "pr/$PR" "$head_ref"

dir=$(basename $(pwd))

git branch -D pr/$PR 2> /dev/null
git worktree add -b pr/$PR ../$dir-pr-$PR pr/$PR/$head_ref 2> /dev/null

wt_path=$(cd ../$dir-pr-$PR && pwd)

echo "git worktree created in $wt_path"

cd $wt_path
git branch --set-upstream-to=pr/$PR/$head_ref
git pull   --ff-only || {
    echo "error: failed to pull pr/$PR"
    exit 1
}

if [[ $# -eq 2 ]]; then
    echo "executing: $2"
    eval "$2"
fi