I’ve been sharing GitHub and GitLab links a lot lately and thought it might be worthwhile to automate generating them.
Most IDEs and GUI-based git clients have these features already, but I’ve found my workflow to involve (1) tidying up commits in the terminal (lots of interactive rebasing) and then sharing them in comments, messages, and Jira tickets.
The Problem
Basically, I wanted to generate commit and compare URLs in the terminal. And then copy the output into a browser, chat, or ticket.
The Solution
Here’s the solution:
cowboy-bebug/dotfiles@bdaf26e.
I’ve set up gurl
(git URL) as a shell function in my dotfiles. It’s symlinked
to ~/.local/bin
, so you can install it by copying the script there or adding a
symlink yourself.
Here’s the help output:
❯ gurl --help
Usage:
gurl [<commit>|<range>] [--output <format>]
Options:
-o, --output <format> Output format: url (default), markdown|md, org|orgmode
-h, --help Show this help message
Examples:
gurl → https://github.com/org/repo/commit/abc1234
gurl abc..def → https://github.com/org/repo/compare/abc...def
gurl -o markdown → [abc1234](https://github.com/org/repo/commit/abc1234)
gurl -o org → [[https://github.com/org/repo/commit/abc1234][abc1234]]
And a few snippets as examples:
-
latest commit link:
❯ gurl https://github.com/cowboy-bebug/dotfiles/commit/b691bce
-
latest commit link in markdown (also works for org syntax):
❯ gurl -o md [cowboy-bebug/dotfiles@b691bce](https://github.com/cowboy-bebug/dotfiles/commit/b691bce)
-
you can also pipe a commit hash:
❯ gurl 8a3f803 https://github.com/cowboy-bebug/dotfiles/commit/8a3f803 # 4th commit from the latest ❯ git log --skip=3 -1 --format="%h" | gurl https://github.com/cowboy-bebug/dotfiles/commit/8a3f803
-
the most useful feature in my opinion is to print a compare link:
❯ gurl cc21754..60ce1bf https://github.com/cowboy-bebug/dotfiles/compare/cc21754...60ce1bf ❯ echo $(git log --skip=33 -1 --format="%h")..$(git log --skip=30 -1 --format="%h") | gurl https://github.com/cowboy-bebug/dotfiles/compare/cc21754...60ce1bf
One thing to keep in mind about compare links is that the starting commit is exclusive (ending commit is inclusive). So if you want to print the last 3 commits, the starting commit has to be the 4th commit from the latest. This is a limitation (or feature) of how GitHub and GitLab structure their compare link paths.
TL;DR
Use the script from cowboy-bebug/dotfiles@bdaf26e to generate commit and compare URLs directly from the terminal.