From 52a105475799d3104ecb38d1977ec30daef3c233 Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Thu, 22 Jan 2026 19:14:22 -0500 Subject: [PATCH] Sort branches by commit date like lazygit Use git for-each-ref with --sort=-committerdate to order branches by last commit date (newest first), matching lazygit's default behavior. Added e2e test with controlled commit dates to verify ordering. Co-Authored-By: Claude Opus 4.5 --- bb.edn | 6 ++- src/lazygitclj/git.clj | 18 +++++++-- test/e2e.clj | 75 ++++++++++++++++++++++++++++++++++++++ test/e2e/branch-order.tape | 66 +++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 test/e2e/branch-order.tape diff --git a/bb.edn b/bb.edn index b12b41e..e549a51 100644 --- a/bb.edn +++ b/bb.edn @@ -12,4 +12,8 @@ e2e:setup {:doc "Setup standard e2e test repo" :task (e2e/setup-test-repo)} e2e:setup-cursor {:doc "Setup cursor navigation test repo" - :task (e2e/setup-cursor-test-repo)}}} + :task (e2e/setup-cursor-test-repo)} + e2e:setup-branch-order {:doc "Setup branch order test repo" + :task (e2e/setup-branch-order-test-repo)} + e2e:cleanup-branch-order {:doc "Cleanup branch order test repo" + :task (e2e/cleanup-branch-order-test-repo)}}} diff --git a/src/lazygitclj/git.clj b/src/lazygitclj/git.clj index cc3be4d..ef578b5 100644 --- a/src/lazygitclj/git.clj +++ b/src/lazygitclj/git.clj @@ -86,13 +86,23 @@ ;; === Branches === (defn branches - "Returns list of local branches." + "Returns list of local branches, sorted by last commit date (newest first). + This matches lazygit's default branch ordering." [] - (->> (sh "git" "branch" "--format=%(refname:short)") + (->> (sh "git" "for-each-ref" + "--sort=-committerdate" + "--format=%(refname:short)" + "refs/heads/") lines)) -(defn remote-branches [] - (->> (sh "git" "branch" "-r" "--format=%(refname:short)") +(defn remote-branches + "Returns list of remote branches, sorted by last commit date (newest first). + This matches lazygit's default remote branch ordering." + [] + (->> (sh "git" "for-each-ref" + "--sort=-committerdate" + "--format=%(refname:short)" + "refs/remotes/") lines)) ;; === Actions === diff --git a/test/e2e.clj b/test/e2e.clj index 0e5b30f..349aa7e 100644 --- a/test/e2e.clj +++ b/test/e2e.clj @@ -75,6 +75,78 @@ (println "Cursor test repo created at" repo-dir) repo-dir)) +(defn setup-branch-order-test-repo + "Setup test repo with branches having different commit dates to test sorting. + Creates branches in alphabetical order but with commits in reverse order, + so date-sorted results should differ from alphabetical. + + Commit dates: + - main: 2024-01-04 (newest, so it appears first) + - gamma-branch: 2024-01-03 + - beta-branch: 2024-01-02 + - alpha-branch: 2024-01-01 (oldest) + + Expected date-sorted order: main, gamma-branch, beta-branch, alpha-branch + Alphabetical order would be: alpha-branch, beta-branch, gamma-branch, main" + ([] (setup-branch-order-test-repo "/tmp/lazygitclj-e2e-branch-order")) + ([repo-dir] + (fs/delete-tree repo-dir) + (fs/create-dirs repo-dir) + + ;; Initialize repo + (shell {:dir repo-dir} "git" "init" "-b" "main") + (shell {:dir repo-dir} "git" "config" "user.email" "test@example.com") + (shell {:dir repo-dir} "git" "config" "user.name" "Test User") + + ;; Initial commit on main (will be updated later with a specific date) + (spit (fs/file repo-dir "README.md") "# Branch Order Test\n") + (shell {:dir repo-dir} "git" "add" ".") + (shell {:dir repo-dir :extra-env {"GIT_COMMITTER_DATE" "2024-01-04T10:00:00"}} + "git" "commit" "-m" "Initial commit" "--date=2024-01-04T10:00:00") + + ;; Create branches with commits in specific order (oldest to newest): + ;; alpha-branch (oldest) -> beta-branch -> gamma-branch -> main (newest) + ;; This way, date-sorted order should be: main, gamma, beta, alpha + ;; while alphabetical would be: alpha, beta, gamma, main + + ;; Create alpha-branch (oldest) + (shell {:dir repo-dir} "git" "checkout" "-b" "alpha-branch") + (spit (fs/file repo-dir "alpha.txt") "alpha content\n") + (shell {:dir repo-dir} "git" "add" ".") + ;; Use GIT_COMMITTER_DATE to set specific commit times + (shell {:dir repo-dir :extra-env {"GIT_COMMITTER_DATE" "2024-01-01T10:00:00"}} + "git" "commit" "-m" "Add alpha" "--date=2024-01-01T10:00:00") + + ;; Create beta-branch (middle) + (shell {:dir repo-dir} "git" "checkout" "main") + (shell {:dir repo-dir} "git" "checkout" "-b" "beta-branch") + (spit (fs/file repo-dir "beta.txt") "beta content\n") + (shell {:dir repo-dir} "git" "add" ".") + (shell {:dir repo-dir :extra-env {"GIT_COMMITTER_DATE" "2024-01-02T10:00:00"}} + "git" "commit" "-m" "Add beta" "--date=2024-01-02T10:00:00") + + ;; Create gamma-branch + (shell {:dir repo-dir} "git" "checkout" "main") + (shell {:dir repo-dir} "git" "checkout" "-b" "gamma-branch") + (spit (fs/file repo-dir "gamma.txt") "gamma content\n") + (shell {:dir repo-dir} "git" "add" ".") + (shell {:dir repo-dir :extra-env {"GIT_COMMITTER_DATE" "2024-01-03T10:00:00"}} + "git" "commit" "-m" "Add gamma" "--date=2024-01-03T10:00:00") + + ;; Return to main + (shell {:dir repo-dir} "git" "checkout" "main") + + (println "Branch order test repo created at" repo-dir) + (println "Expected date-sorted order: main, gamma-branch, beta-branch, alpha-branch") + repo-dir)) + +(defn cleanup-branch-order-test-repo + "Clean up the branch order test repo." + ([] (cleanup-branch-order-test-repo "/tmp/lazygitclj-e2e-branch-order")) + ([repo-dir] + (fs/delete-tree repo-dir) + (println "Cleaned up" repo-dir))) + (def tests "List of e2e test tapes." ["test/e2e/navigation.tape" @@ -85,6 +157,7 @@ "test/e2e/branches.tape" "test/e2e/branch-operations.tape" "test/e2e/branches-tabs.tape" + "test/e2e/branch-order.tape" "test/e2e/commits-tabs.tape" "test/e2e/stash-operations.tape" "test/e2e/stash-menu.tape" @@ -127,5 +200,7 @@ (case (first args) "setup" (setup-test-repo (or (second args) "/tmp/lazygitclj-e2e-test")) "setup-cursor" (setup-cursor-test-repo (or (second args) "/tmp/lazygitclj-e2e-cursor")) + "setup-branch-order" (setup-branch-order-test-repo (or (second args) "/tmp/lazygitclj-e2e-branch-order")) + "cleanup-branch-order" (cleanup-branch-order-test-repo (or (second args) "/tmp/lazygitclj-e2e-branch-order")) "run" (run-all) (run-all)))) diff --git a/test/e2e/branch-order.tape b/test/e2e/branch-order.tape new file mode 100644 index 0000000..92e9dd2 --- /dev/null +++ b/test/e2e/branch-order.tape @@ -0,0 +1,66 @@ +# VHS E2E test for lazygitclj - Branch ordering by commit date +# Tests that branches are sorted by commit date (newest first) like lazygit +# +# Setup creates branches with specific commit dates: +# - main: 2024-01-04 (newest) +# - gamma-branch: 2024-01-03 +# - beta-branch: 2024-01-02 +# - alpha-branch: 2024-01-01 (oldest) +# +# Expected order in UI (by date): main, gamma-branch, beta-branch, alpha-branch +# Alphabetical order would be: alpha-branch, beta-branch, gamma-branch, main + +Output test/e2e/output/branch-order.gif +Output test/e2e/output/branch-order.ascii + +Require bb + +Set Shell "bash" +Set FontSize 14 +Set Width 1000 +Set Height 600 +Set Framerate 10 + +# Setup test repo with branches having different commit dates +Type "cd /home/ajet/repos/lazygitclj && bb e2e:setup-branch-order" +Enter +Sleep 1s + +# Run lazygitclj in the test repo +Type "cd /tmp/lazygitclj-e2e-branch-order && bb --config /home/ajet/repos/lazygitclj/bb.edn start" +Enter +Sleep 2s + +# Switch to Branches panel (panel 3) +Type "3" +Sleep 500ms + +# The branches panel should show branches in date order: +# gamma-branch (newest), beta-branch, alpha-branch, main (oldest) +# Take a moment to verify the order visually +Sleep 2s + +# Navigate down through the branches to verify order +Type "j" +Sleep 300ms +Type "j" +Sleep 300ms +Type "j" +Sleep 300ms + +# Navigate back up +Type "k" +Sleep 300ms +Type "k" +Sleep 300ms +Type "k" +Sleep 300ms + +# Quit +Type "q" +Sleep 1s + +# Cleanup test repo +Type "cd /home/ajet/repos/lazygitclj && bb e2e:cleanup-branch-order" +Enter +Sleep 500ms