aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-02-20 22:56:23 +0100
committerTeddy Wing2018-02-20 22:56:23 +0100
commit63c4a1a2d0037688f89416bf76127ed2120761cc (patch)
tree9bf58701f718563e3bfb766da1ff259c924505f0
downloadgit-branch-list-63c4a1a2d0037688f89416bf76127ed2120761cc.tar.bz2
Add initial draft of `git-branch-list`
A Git add-on that stores a list of branch names and allows you to quickly switch to any branch in the list. This enables you to more quickly switch between branches that you're working on or reviewing without having to either remember or look up the beginnings of the names of the branches. Currently provides basic functionality for saving and dropping branches from the list, printing the list, and checking out a branch in the list. The branch list "database" is stored in a text file in the `.git/info/` directory local to the project. Perhaps worrisome to store an external file in the internal Git directory, but this allows us to keep the database local to the current project and maintain different databases for different projects.
-rwxr-xr-xgit-branch-list96
1 files changed, 96 insertions, 0 deletions
diff --git a/git-branch-list b/git-branch-list
new file mode 100755
index 0000000..2569dc0
--- /dev/null
+++ b/git-branch-list
@@ -0,0 +1,96 @@
+#!/bin/bash
+
+# git branch-list/bl [save|drop|#id]
+
+# database=
+# if -f .git then database= .git/info/git-branch-list
+DATABASE=.git/info/git-branch-list
+
+function initialise_database () {
+ if [ -d .git ]; then
+ touch "$DATABASE"
+ fi
+}
+
+function save_branch () {
+ local branch=$(branch_or_current_branch "$1")
+
+# sed -i '.bak' "0i\\
+# $branch
+# " "$DATABASE"
+#
+# rm "${DATABASE}.bak"
+
+# # echo "$branch" >> "$DATABASE"
+
+ cat <(echo "$branch") "$DATABASE" > "${DATABASE}.bak"
+ mv "${DATABASE}.bak" "$DATABASE"
+
+ # TODO: don't save if already added
+ # TODO: append instead of prepend so IDs stay the same. Maybe?
+}
+
+function drop_branch () {
+ local branch=$(branch_or_current_branch "$1")
+
+ sed -i '.bak' "/$branch/d" "$DATABASE"
+
+ # TODO: Allow dropping by ID or branch name
+ # if [ "$branch" = '' ] use current branch
+ # if is_a_branch "$branch"; then do what happens now
+ # try to delete branch at ID
+}
+
+function list_branches () {
+ nl "$DATABASE"
+}
+
+function checkout_branch () {
+ local id="$1"
+
+ local branch=$(list_branches |
+ tr -d ' ' |
+ fgrep "$id " |
+ cut -d ' ' -f 2)
+
+ git checkout "$branch"
+}
+
+function branch_or_current_branch () {
+ local branch="$1"
+
+ if [ "$branch" = '' ]; then
+ git rev-parse --abbrev-ref HEAD
+ else
+ echo "$branch"
+ fi
+}
+
+function is_a_branch () {
+ local branch="$1"
+
+ git rev-parse --verify "$branch"
+ return $?
+}
+
+command="$1"
+
+initialise_database
+
+case "$command" in
+ save)
+ save_branch "$2"
+ ;;
+ drop)
+ drop_branch "$2"
+ ;;
+ "")
+ list_branches
+
+ exit $?
+ ;;
+ *)
+ # if $1 is an integer
+ checkout_branch "$1"
+ ;;
+esac