File: git-pr

package info (click to toggle)
git-extras 7.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,120 kB
  • sloc: sh: 4,312; python: 994; makefile: 146
file content (77 lines) | stat: -rwxr-xr-x 1,674 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env bash
# Based on https://gist.github.com/gnarf/5406589 and https://gist.github.com/jhnns/d654d9d6da6d3b749986
TO_MERGE=

pull() {
  local remote="$1"
  local id="$2"
  local branch="$3"
  local ref="refs/pull/$id/head"

  if [ -n "$TO_MERGE" ]; then
    ref="refs/pull/$id/merge"
  fi

  git fetch -fu "$remote" "$ref:$branch" && \
  git checkout "$branch" && \
  git config --local --replace "branch.$branch.merge" "$ref" && \
  git config --local --replace "branch.$branch.remote" "$remote"
}

pull_pr_if_matched() {
  if [[ $1 =~ ^(.*):([0-9]+)|(https?://[^/]+/.+)/pull/([0-9]+).*$ ]]; then
    if [[ -n ${BASH_REMATCH[2]} ]]; then
        remote="${BASH_REMATCH[1]:-origin}"
        id="${BASH_REMATCH[2]}"
    else
        remote="${BASH_REMATCH[3]}.git"
        id="${BASH_REMATCH[4]}"
    fi

    branch=pr/$id
    pull "$remote" "$id" "$branch"
    return $?
  fi

  echo "$1 doesn't match the pr id pattern."
  return 1
}

for arg in "$@"; do
    case "$arg" in
        -m|--merge)
            TO_MERGE=1
            ;;
        *)
            # set the argument back
            set -- "$@" "$arg"
            ;;
    esac

    shift
done

test -z "$1" && echo "pr number required." 1>&2 && exit 1

if test "$1" = "clean"; then
  git for-each-ref refs/heads/pr/* --format='%(refname)' | while read -r ref; do
    git branch -D "${ref#refs/heads/}"
  done

elif [[ "$1" =~ ^[0-9]+$ ]]; then
  remote_pref=${2:-$(git config --get git-extras.pr.remote)}
  remote=${remote_pref:-origin}
  id=$1
  branch=pr/$id
  pull "$remote" "$id" "$branch"

else
  rc=1
  while [ "$1" != "" ]; do
    pull_pr_if_matched "$1"
    rc=$?
    shift
  done

  exit "$rc"
fi