Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1428,8 +1428,8 @@ $ git reset-file .htaccess dc82b19

## git mr

Checks out a merge request from GitLab. Usage: `git mr <ID|URL> [REMOTE]`.
Default remote is `origin`.
Checks out a merge request from GitLab, or a pull request from Forgejo/Codeberg.
Usage: `git mr <ID|URL> [REMOTE]`. Default remote is `origin`.

``` bash
$ git mr 51
Expand All @@ -1447,6 +1447,15 @@ From gitlab.com:owner/repository
Switched to branch 'mr/51'
```

A Forgejo/Codeberg pull request URL is also supported:

``` bash
$ git mr https://codeberg.org/owner/repository/pulls/51
From codeberg.org:owner/repository
* [new ref] refs/pull/51/head -> mr/51
Switched to branch 'mr/51'
```

Just like [git pr](#git-pr), `git mr` accepts a `clean` argument to trash all
`mr/` branches. Ensure current branch is not one.

Expand Down
10 changes: 9 additions & 1 deletion bin/git-mr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if [ -z "${1-}" ] ; then
exit 1
fi

remote_ref_kind=merge-requests

if test "$1" = "clean"; then
git for-each-ref refs/heads/mr/* --format='%(refname)' | while read -r ref; do
git branch -D "${ref#refs/heads/}"
Expand All @@ -15,13 +17,19 @@ if test "$1" = "clean"; then
elif [[ $1 =~ ^(https?://[^/]+/(.+))/merge_requests/([0-9]+).*$ ]]; then
remote=${BASH_REMATCH[1]}.git
id=${BASH_REMATCH[3]}
elif [[ $1 =~ ^(https?://[^/]+/(.+))/pulls/([0-9]+).*$ ]]; then
# Forgejo/Codeberg pull request URL, e.g.
# https://codeberg.org/owner/repository/pulls/453
remote=${BASH_REMATCH[1]}.git
id=${BASH_REMATCH[3]}
remote_ref_kind=pull
else
id=$1
remote=${2:-origin}
fi

branch=mr/$id
remote_ref=refs/merge-requests/$id/head
remote_ref=refs/$remote_ref_kind/$id/head
git fetch -fu "$remote" "$remote_ref:$branch"
git checkout "$branch"
git config --local --replace "branch.$branch.merge" "$remote_ref"
Expand Down
18 changes: 17 additions & 1 deletion man/git-mr.1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The name of the remote to fetch from\. Defaults to \fBorigin\fR\.
<url>
.
.P
GitLab merge request URL in the format \fBhttps://gitlab\.tld/owner/repository/merge_requests/453\fR\.
GitLab merge request URL in the format \fBhttps://gitlab\.tld/owner/repository/merge_requests/453\fR, or a Forgejo/Codeberg pull request URL in the format \fBhttps://codeberg\.tld/owner/repository/pulls/453\fR\.
.
.SH "EXAMPLES"
This checks out merge request \fB!51\fR from remote \fBorigin\fR to branch \fBmr/51\fR\.
Expand All @@ -46,6 +46,22 @@ Switched to branch \'mr/51\'
.
.IP "" 0
.
.P
This checks out pull request \fB#51\fR from a Forgejo/Codeberg URL to branch \fBmr/51\fR\.
.
.IP "" 4
.
.nf

$ git mr https://codeberg\.org/owner/repository/pulls/51
From codeberg\.org:owner/repository
* [new ref] refs/pull/51/head \-> mr/51
Switched to branch \'mr/51\'
.
.fi
.
.IP "" 0
.
.SH "AUTHOR"
Written by Étienne BERSAC \fIbersace03@gmail\.com\fR from git\-pr(1)\.
.
Expand Down
12 changes: 11 additions & 1 deletion man/git-mr.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion man/git-mr.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ git-mr(1) -- Checks out a merge request locally
&lt;url&gt;

GitLab merge request URL in the format
`https://gitlab.tld/owner/repository/merge_requests/453`.
`https://gitlab.tld/owner/repository/merge_requests/453`, or a

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regenerated git-mr.1 and git-mr.html in 6d65f6b to match the .md changes. ronn-ng crashed in my local ruby/windows setup (style_files bug unrelated to this change), so I hand-matched the existing generated format for the new OPTIONS line and EXAMPLES block - flagging in case you'd rather re-run make docs on a working toolchain to double check byte-for-byte output

Forgejo/Codeberg pull request URL in the format
`https://codeberg.tld/owner/repository/pulls/453`.


## EXAMPLES
Expand All @@ -33,6 +35,13 @@ This checks out merge request `!51` from remote `origin` to branch `mr/51`.
* [new ref] refs/merge-requests/51/head -> mr/51
Switched to branch 'mr/51'

This checks out pull request `#51` from a Forgejo/Codeberg URL to branch `mr/51`.

$ git mr https://codeberg.org/owner/repository/pulls/51
From codeberg.org:owner/repository
* [new ref] refs/pull/51/head -> mr/51
Switched to branch 'mr/51'

## AUTHOR

Written by Étienne BERSAC <bersace03@gmail.com> from git-pr(1).
Expand Down
68 changes: 68 additions & 0 deletions tests/git-mr.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# shellcheck shell=bash

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test to cover Forgejo/Codeberg

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in 6d65f6b - checks out a codeberg-style pulls URL against a local remote using url.insteadOf so it doesn't need a real HTTP endpoint


source "$BATS_TEST_DIRNAME/test_util.sh"

setup_file() {
test_util.setup_file

PATH="$BATS_TEST_DIRNAME/bin:$PATH"
}

setup() {
test_util.cd_test

test_util.git_init
touch ./tracked
git add ./tracked
git commit -m 'Initial commit'

# Local bare "remote" standing in for a real GitLab/Forgejo instance,
# with a merge-requests ref and a pull ref pointing at the same commit.
remote_dir="$BATS_TEST_TMPDIR/remote.git"
git init --bare --initial-branch main "$remote_dir"
git push "$remote_dir" HEAD:refs/merge-requests/51/head
git push "$remote_dir" HEAD:refs/pull/51/head

git remote add origin "$remote_dir"
}

@test "checks out a merge request by numeric id" {
run git mr 51
assert_success
assert_output --partial "refs/merge-requests/51/head"

run git rev-parse --abbrev-ref HEAD
assert_output 'mr/51'

run git config --get branch.mr/51.merge
assert_output 'refs/merge-requests/51/head'
}

@test "checks out a merge request by numeric id and explicit remote" {
run git mr 51 "$remote_dir"
assert_success
assert_output --partial "refs/merge-requests/51/head"

run git rev-parse --abbrev-ref HEAD
assert_output 'mr/51'
}

@test "checks out a Forgejo/Codeberg pull request by URL" {
# git-mr only recognizes http(s) URLs, so rewrite the fake Codeberg URL
# to our local bare "remote" via insteadOf rather than needing a real
# HTTP(S) endpoint.
git config --local url."$remote_dir".insteadOf "https://codeberg.org/owner/repository.git"

run git mr "https://codeberg.org/owner/repository/pulls/51"
assert_success
assert_output --partial "refs/pull/51/head"

run git rev-parse --abbrev-ref HEAD
assert_output 'mr/51'

run git config --get branch.mr/51.merge
assert_output 'refs/pull/51/head'

run git config --get branch.mr/51.remote
assert_output 'https://codeberg.org/owner/repository.git'
}
Loading