Skip to content

Commit 3fa732d

Browse files
authored
Merge pull request #195 from unixorn/update-fetch-prs
Update fetch prs
2 parents 270a2c6 + 935b602 commit 3fa732d

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ If you wrote one of these scripts and want it removed from this collection, plea
7979
| `git-diff-last` | [Sebastian Schuberth](https://github.com/sschuberth) | Show the last change made to a file in the repository. |
8080
| `git-divergence` | Gary Bernhardt's [dotfiles](https://github.com/garybernhardt/dotfiles/blob/master/bin/git-churn) | Shows differences between local branch and its tracking branch. |
8181
| `git-edit-conflicts` | Joe Block <jpb@unixorn.net> | Edit the files that are marked as conflicted during a merge/rebase in your `$EDITOR/$VISUAL`. |
82-
| `git-fetch-prs` | Pretty sure I saw this on slack, but can't recall which one | Get all Pull Request branches as local remote branches by refspec |
82+
| `git-fetch-prs` | Pretty sure I saw this on slack, but can't recall which one | Get all Pull Request branches from a given remote by refspec |
8383
| `git-files` | Jake Zimmerman's [blog](https://blog.jez.io/cli-code-review/) | List the files different between the current branch and `$REVIEW_BRANCH`, which if unset defaults to the repository's default branch |
8484
| `git-find-dirty` | Matthew McCullough's [scripts](https://github.com/matthewmccullough/scripts/) repository | |
8585
| `git-flush` | John Wiegley's [git-scripts](https://github.com/jwiegley/git-scripts) | Compact your repository by dropping all reflogs, stashes, and other cruft that may be bloating your pack files. |

bin/git-fetch-prs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
11
#!/usr/bin/env bash
22
#
3-
# Fetch PR branches by refspec
3+
# Fetch PR branches by refspec from one of a repository's remotes.
44
#
5-
# Copyright 2020, Joe Block <jpb@unixorn.net>
5+
# Copyright 2020-2022, Joe Block <jpb@unixorn.net>
66

77
set -o pipefail
88

9+
DEFAULT_REMOTE=${DEFAULT_REMOTE:-"origin"}
10+
11+
function debug() {
12+
if [[ -n "$DEBUG" ]]; then
13+
echo "$@"
14+
fi
15+
}
16+
17+
function usage() {
18+
local myname
19+
myname=$(basename "$0")
20+
echo "$myname [remote]"
21+
echo
22+
echo "Pulls all the PR branches from one of your remotes. If remote is not specified, defaults to $DEFAULT_REMOTE."
23+
}
24+
25+
function fail() {
26+
printf '%s\n' "$1" >&2 ## Send message to stderr. Exclude >&2 if you don't want it that way.
27+
exit "${2-1}" ## Return a code specified by $2 or 1 by default.
28+
}
29+
30+
function has() {
31+
# Check if a command is in $PATH
32+
which "$@" > /dev/null 2>&1
33+
}
34+
35+
if [[ "$1" == '--help' ]]; then
36+
usage
37+
exit 0
38+
fi
39+
40+
debug "DEFAULT_REMOTE: $DEFAULT_REMOTE"
41+
42+
if ! has git; then
43+
fail "Couldn't find git in your PATH"
44+
fi
45+
46+
if [ -z "$1" ]; then
47+
echo "Remote not specified, using $DEFAULT_REMOTE."
48+
git_remote="$DEFAULT_REMOTE"
49+
else
50+
git_remote="$1"
51+
fi
52+
953
# Get all Pull Request branches as local remote branches by refspec.
10-
exec git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'
54+
# shellcheck disable=SC2086
55+
git fetch "$git_remote" +refs/heads/\*:refs/remotes/$git_remote/\* +refs/pull/\*/head:refs/remotes/$git_remote/pr/\*

bin/git-pr-fetch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git-fetch-prs

0 commit comments

Comments
 (0)