From 6bd7ff3fdf92e3e12b714ca7d506878483098b83 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Tue, 11 Feb 2025 07:59:29 -0800 Subject: [PATCH] grab HEAD branch of origin (main, master) more reliably --- git-recent | 34 +++++++++++++++++++++++---------- homebrew/git-recent.rb | 43 ++++++++++++++++++++++++++++++++++++++++++ man/man1/git-recent.1 | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 homebrew/git-recent.rb create mode 100644 man/man1/git-recent.1 diff --git a/git-recent b/git-recent index 1e5dd34..1080931 100755 --- a/git-recent +++ b/git-recent @@ -9,17 +9,31 @@ # - text filtering against branch names, too. # +if ! command -v fzf >/dev/null 2>&1; then + echo "Error: fzf is not installed. Please install fzf to use git-recent." >&2 + exit 1 +fi +if [[ "$1" == "--help" ]]; then + echo "git-recent: Browse and checkout recently used Git branches." + echo + echo "Keybindings:" + echo " Enter: Checkout the selected branch" + echo " Ctrl-O: Show the diff of the selected branch against the main/master branch" + echo " Ctrl-C: Exit" + exit 0 +fi + +# --------------------------------------------------------------------------------------- + branches_format="%(color:yellow)%(refname:short)%(color:reset)" commits_format="%C(red bold)%h %C(bold blue)%an %C(bold green)%ad %Creset%s" -# The primary branch -mainormaster() { - (git branch --format '%(refname:short)' --sort=-committerdate --list master main; echo main) | head -n1 || echo main -} +# The primary branch (eg main or master or w/e) +origin_HEAD=$(cat $(git rev-parse --show-cdup).git/refs/remotes/origin/HEAD | awk '{print $2}') -uniqcommits_cmd="git log --date=human --color=always --format='$commits_format' --no-merges origin/$(mainormaster)..{1}" +uniqcommits_cmd="git log --date=human --color=always --format='$commits_format' --no-merges $origin_HEAD..{1}" -diffbranch_cmd="git diff --color=always origin/$(mainormaster)...{}" +diffbranch_cmd="git diff --color=always $origin_HEAD...{}" # Progressive enhancement if you have delta or diff-so-fancy if command -v delta >/dev/null 2>&1; then diffbranch_cmd="$diffbranch_cmd | delta" @@ -27,10 +41,10 @@ elif command -v diff-so-fancy >/dev/null 2>&1; then diffbranch_cmd="$diffbranch_cmd | diff-so-fancy" fi -# fzf basic inspiration: -# - https://github.com/junegunn/fzf/wiki/Examples#git, https://github.com/junegunn/fzf/wiki/Examples-(fish)#git -# Hardcore inspiration: -# - https://github.com/junegunn/fzf-git.sh +# fzf git inspiration: +# - https://github.com/junegunn/fzf/wiki/Examples#git +# - https://github.com/junegunn/fzf/wiki/Examples-(fish)#git +# - https://github.com/junegunn/fzf-git.sh (intense.) _browse_branches() { git for-each-ref --color=always --sort=-authordate "refs/heads/" --format="$branches_format" \ diff --git a/homebrew/git-recent.rb b/homebrew/git-recent.rb new file mode 100644 index 0000000..d6b1684 --- /dev/null +++ b/homebrew/git-recent.rb @@ -0,0 +1,43 @@ +class GitRecent < Formula + desc "See your latest local git branches, formatted real fancy" + homepage "https://github.com/paulirish/git-recent" + url "https://github.com/paulirish/git-recent/archive/refs/tags/v2.0.1.tar.gz" + sha256 "ab9c3f5da92747f7b53f1a301b22433116ee8d204562cc8f0364f70f4a79d318" + license "MIT" + + bottle do + rebuild 1 + sha256 cellar: :any_skip_relocation, all: "3a4143920243a863447daa6f2b17b3cda4e0a163e8502c6c36a910eee4ee7450" + end + + # depends_on "fzf" + + depends_on macos: :sierra + + on_linux do + depends_on "util-linux" # for `column` + end + + conflicts_with "git-plus", because: "both install `git-recent` binaries" + + def install + man1.install "man/man1/git-recent.1" + bin.install "git-recent" + bin.install "git-recent-og" + end + + test do + system "git", "init", "--initial-branch=main" + # User will be 'BrewTestBot' on CI, needs to be set here to work locally + system "git", "config", "user.name", "BrewTestBot" + system "git", "config", "user.email", "brew@test.bot" + system "git", "commit", "--allow-empty", "-m", "test_commit" + # assert_match(/.*main.*seconds? ago.*BrewTestBot.*\n.*test_commit/, shell_output("git recent")) + # output = shell_output("git log") + output = pipe_output("#{bin}/git-recent") + puts output + assert_equal "main", output.strip + # assert_equal "test_commit", output.strip + assert_equal "Branches", output.strip + end +end diff --git a/man/man1/git-recent.1 b/man/man1/git-recent.1 new file mode 100644 index 0000000..0c26e63 --- /dev/null +++ b/man/man1/git-recent.1 @@ -0,0 +1,41 @@ +.TH GIT-RECENT 1 "February 8, 2025" "git-recent 2.0" +.SH NAME +git recent \- Browse and checkout recently used Git branches. +.SH SYNOPSIS +.B git recent +.SH DESCRIPTION +.B git recent +provides an interactive interface (using +.I fzf +) to browse your latest local git branches. It displays a list of branches sorted by recency, shows the unique commits for each branch, and allows you to checkout a branch with +.B Enter\fR. +You can also view the diff of a branch against the main/master branch. +.SH OPTIONS +.TP +.B --help +Display a help message. +.SH KEYBINDINGS +.TP +.B Enter +Checkout the selected branch. +.TP +.B Up/Down arrow keys +Navigate the branch list. +.TP +.B Type +Filter branch list with fuzzy text matching +.TP +.B Ctrl-o +Show the diff of the selected branch against the main/master branch. +.TP +.B Ctrl-C +Exit. +.SH INSTALLATION +.I fzf +is required. https://junegunn.github.io/fzf/installation/ + +.SH SEE ALSO +.I fzf(1), git(1), git-open(1), diff-so-fancy(1), delta(1) +.br +.I git-recent-og +(for the non-fzf version) \ No newline at end of file