Git pull request fetching & searching

Published

Working with GitHub and GitLab pull/merge requests can be improved greatly by adding an alias for fetching all requests. Fetched PRs can then easily be searched or checked out.

Adam Jackson created the script add-gitlab-merge-requests.sh which is the basis of this workflow.

git pr

The idea is to provide local access to all of the PRs that exist upstream. This both provides a better general overview of which PRs that have been pulled into the branch you're working on, but also enables you to search the contents of all PRs.

This function automagically detects if your remote is hosted on GitHub or GitLab and makes the necessary adjustments to work on either platform.

[alias]
  mr = pr
  pr = "!f() { \
    REMOTES=$(git remote); \
    REMOTE=\"origin\"; \
    case \"$REMOTES\" in \
    *upstream*) \
      REMOTE=\"upstream\"; \
    ;; \
    esac; \
    \
    ORIGIN=${1:-${REMOTE}}; \
    URL=$(git remote get-url ${ORIGIN}); \
    \
    case \"$URL\" in \
    *gitlab*) \
      FORGE_EXT="mr" \
      FORGE_PATH="merge-requests" \
    ;; \
    *github*) \
      FORGE_EXT="pr" \
      FORGE_PATH="pull" \
    ;; \
    esac; \
    ORIGIN_NAME=${ORIGIN}-${FORGE_EXT}; \
    git remote add ${ORIGIN_NAME} ${URL}; \
    git config remote.${ORIGIN_NAME}.fetch "+refs/${FORGE_PATH}/*/head:refs/remotes/${ORIGIN_NAME}/*"; \
    git fetch ${ORIGIN_NAME}; \
  }; f"

The syntax is git pr [remote].

[remote] will default to the upstream remote if available or origin if it isn't available.

$ git pr
remote: Enumerating objects: 886, done.
remote: Counting objects: 100% (827/827), done.
remote: Compressing objects: 100% (273/273), done.
remote: Total 886 (delta 637), reused 692 (delta 551), pack-reused 59
Receiving objects: 100% (886/886), 313.79 KiB | 918.00 KiB/s, done.
Resolving deltas: 100% (647/647), completed with 228 local objects.
From https://gitlab.freedesktop.org/wayland/wayland
 * [new ref]         refs/merge-requests/102/head -> upstream-mr/102
 * [new ref]         refs/merge-requests/104/head -> upstream-mr/104
 * [new ref]         refs/merge-requests/105/head -> upstream-mr/105
 * [new ref]         refs/merge-requests/106/head -> upstream-mr/106
 * [new ref]         refs/merge-requests/107/head -> upstream-mr/107

git search-all

git search-all or git sa is used for search all commits in a repository. When used in conjunction with git pr this enables searching through all PRs to look for previous implementations of functionality for example.

[alias]
    search-all = sa
    sa = "!f() { git log --all --color --pretty=log --abbrev-commit -S\"$1\" $2; }; f"

[pretty]
    log = %Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset

With both of these additions you'll be able to search PR commits for strings and have the relevant PR id listed in the search output.

$ git sa "~/.local/share"
91a92e5 - (upstream-mr/192) cursor: add XDG_DATA_DIRS to xcursor search path (10 months ago) <catsout>
0aebb5b - (upstream-mr/112) cursor: add one more directory to XCURSORPATH (2 years, 2 months ago) <Alexander Dunaev>

Tags: shell · git · alias · pr · mr · pull · merge · request · gitlab · github · gitconfig