├── git-add-sob ├── git-sed ├── git-list-unmerged ├── git-exgrep ├── git-add-unmerged ├── git-enable-sob ├── git-user-remote-add ├── git-add-tag ├── git-discard-branch ├── git-delete-merged-branches ├── git-post-checkout-master-main-hook ├── git-try-delete-branch ├── git-squash ├── git-hooks ├── git-post-checkout-nagging-hook ├── git-stashed ├── git-patch-set ├── git-split-commit ├── git-branch-description ├── README.md ├── git-for-each ├── git-archive-branch ├── git-recent-branches └── COPYING /git-add-sob: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Add a Signed-off-by tag to each commit 4 | # 5 | # Usage: git add-sob HEAD~4 6 | 7 | git rebase --exec 'git commit --amend --no-edit -n -s' -t 8 | -------------------------------------------------------------------------------- /git-sed: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Usage: git sed 's/abc/def/' **/*.c 4 | 5 | expr=$1 6 | shift 7 | 8 | set -e 9 | git ls-files -z $@ | xargs -0 sed -i "$expr" 10 | -------------------------------------------------------------------------------- /git-list-unmerged: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # List any files needing changes during a rebase 4 | # 5 | # This provides the list of files needing merges after you get after a git 6 | # rebase that triggers conflicts 7 | 8 | git diff --name-only --diff-filter=U 9 | -------------------------------------------------------------------------------- /git-exgrep: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | # 3 | # git grep wrapper that ignores a bunch of annoying files that mess up git grep's output 4 | 5 | # NEWS and Changelog are fake-ish globs so avoid failing on nonexisting files 6 | excludes=("*.po" "NEWS" "*ChangeLog*") 7 | 8 | exec git grep $@ ${excludes[@]/#/:!} 9 | -------------------------------------------------------------------------------- /git-add-unmerged: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Add any files needing changes during a rebase 4 | # 5 | # Usage: 6 | # $ git rebase origin/master 7 | # # CONFLICT in a thousand files 8 | # # fix them one-by one 9 | # $ git add-unmerged 10 | # $ git rebase --continue 11 | 12 | git add $(git diff --name-only --diff-filter=U) 13 | -------------------------------------------------------------------------------- /git-enable-sob: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Usage: git enable-sob 4 | # 5 | # in the git repo that requires signed-off commits 6 | 7 | author=$(git config user.name) 8 | email=$(git config user.email) 9 | 10 | cat > .git/git-sob-commit-template < 14 | EOF 15 | git config --local commit.template .git/git-sob-commit-template 16 | -------------------------------------------------------------------------------- /git-user-remote-add: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | user="$1" 4 | repo="$( basename $PWD)" 5 | 6 | if [[ -z "$user" ]]; then 7 | echo "Usage: $(basename $0) " 8 | exit 1 9 | fi 10 | 11 | url=$(git remote get-url origin | sed -e 's|https://||' -e 's|ssh://git@||' -e 's|:.*||' -e 's|/.*||') 12 | 13 | case $url in 14 | *gitlab*) 15 | ;; 16 | *github*) 17 | ;; 18 | *) 19 | echo "Don't know how to name remote for $url" 20 | exit 1 21 | ;; 22 | esac 23 | 24 | git remote add "$user" "http://$url/$user/$repo" 25 | git fetch "$user" 26 | -------------------------------------------------------------------------------- /git-add-tag: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # fake some name that looks like a git commit to vim 4 | if [ $# -ne 1 ]; then 5 | echo "usage: $0 " 6 | fi 7 | 8 | range="$1" 9 | if [[ "$range" != *..* ]]; then 10 | range="$range.." 11 | fi 12 | 13 | readonly FAKE_GIT_DIR=$(mktemp -d) 14 | readonly FILE="$FAKE_GIT_DIR/.git/COMMIT_EDITMSG" 15 | mkdir -p "$FAKE_GIT_DIR/.git/" 16 | 17 | $EDITOR "$FILE" 18 | export FILTER_BRANCH_SQUELCH_WARNING=1 19 | git filter-branch -f --msg-filter "sed '\$a $(cat "$FILE")'" "$range" 20 | rm -rf "$FAKE_GIT_DIR" 21 | -------------------------------------------------------------------------------- /git-discard-branch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | branch="$1" 4 | case $branch in 5 | --help) 6 | echo "Usage: git discard-branch " 7 | exit 0 8 | ;; 9 | **) 10 | ;; 11 | esac 12 | 13 | if [[ -z "$branch" ]]; then 14 | branch=$(git symbolic-ref --short HEAD) 15 | fi 16 | 17 | case $branch in 18 | master | main) 19 | echo "master and main branches cannot be discarded" 20 | exit 1 21 | ;; 22 | **) 23 | ;; 24 | esac 25 | 26 | date=$(date +%F) 27 | # Drop the wip/ prefix, if any 28 | name="${branch/wip\//}" 29 | 30 | git branch -m "$branch" "discarded/$date/$name" 31 | -------------------------------------------------------------------------------- /git-delete-merged-branches: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | branch="$1" 4 | remotes="origin gitlab github" 5 | 6 | if [[ -z "$branch" ]]; then 7 | for remote in $remotes; do 8 | if $(git remote get-url $remote &> /dev/null); then 9 | branch=$(git remote show $remote | grep "HEAD branch:" | awk '{print $3;}') 10 | break 11 | fi 12 | done 13 | fi 14 | 15 | [ -z "$branch" ] && echo "Unable to find default branch" && exit 1 16 | 17 | echo "Deleting branches merged into $branch" 18 | 19 | for to_delete in $(git branch --merged $branch | grep -v "^\*" | grep -v "$branch"); do 20 | echo "Deleting $to_delete ($(git log -1 --pretty=reference $to_delete)" 21 | git branch --quiet -d "$to_delete" 22 | done 23 | -------------------------------------------------------------------------------- /git-post-checkout-master-main-hook: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Exits with status 1 when checking out the master branch and a "main" branch 3 | # is available 4 | 5 | prev_head=$1 6 | new_head=$2 7 | branch_checkout=$3 8 | 9 | # Ignore any non-branch checkouts 10 | if [[ "$branch_checkout" -ne 1 ]]; then 11 | exit 0 12 | fi 13 | 14 | # if we don't have a main branch, exit early 15 | if ! git describe main > /dev/null; then 16 | exit 0 17 | fi 18 | 19 | remotes="origin gitlab github" 20 | master_branch="master" 21 | for remote in $remotes; do 22 | git remote | grep --quiet "$remote" 23 | if [[ $? -eq 0 ]]; then 24 | master_branch="$remote/$master_branch" 25 | break 26 | fi 27 | done 28 | 29 | 30 | master_commit=$(git rev-list -1 $master_branch) 31 | 32 | if [[ "$new_head" == "$master_commit" ]]; then 33 | echo "WARNING: the master branch is no longer in use" 34 | exit 1 35 | fi 36 | 37 | -------------------------------------------------------------------------------- /git-try-delete-branch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # github always rebases even for FF so you can't easily delete a branch 4 | # without it complaining about being non merged. 5 | # 6 | 7 | arg="$1" 8 | test -z "$arg" && echo "Usage: $(basename "$0") [--all|]" && exit 1 9 | 10 | delete_branch () { 11 | branch="$1" 12 | 13 | sha=$(git log -n1 --pretty=format:%H) 14 | 15 | set -e 16 | git log -n1 --format=short 17 | git checkout --quiet "$branch" 18 | set +e 19 | if ! git -c advice.skippedCherryPicks=false rebase --quiet "$sha"; then 20 | git rebase --abort 21 | git checkout -f - 22 | echo "Failed to rebase, aborting" 23 | return 1 24 | fi 25 | git branch --unset-upstream > /dev/null 2>&1 26 | git checkout --quiet - 27 | git branch -d "$branch" 28 | set -e 29 | } 30 | 31 | if [ "$arg" = "--all" ]; then 32 | for branch in $(git branch | grep wip); do 33 | delete_branch "$branch" 34 | done 35 | else 36 | delete_branch "$arg" 37 | exit $? 38 | fi 39 | -------------------------------------------------------------------------------- /git-squash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Usage: like git commit, but it will provide a short list of squash-able 4 | # commits[ 5 | # 6 | 7 | nrefs=15 8 | 9 | case "$1" in 10 | --help) 11 | echo "Usage: git squash [-n N] [git commit options]" 12 | echo "Displays a numbered list of N recent commits." 13 | echo "" 14 | echo "Chosing a number will commit the staging area with" 15 | echo "a subject line matching that commit, so that on rebase it" 16 | echo "will be squashed in automatically." 17 | exit 0 18 | ;; 19 | -n) 20 | shift 21 | nrefs=$1 22 | shift 23 | ;; 24 | -n*) 25 | nrefs=${1/-n/} 26 | shift 27 | ;; 28 | *) 29 | ;; 30 | esac 31 | 32 | declare -a ref=() 33 | 34 | IFS=' 35 | ' 36 | refs=($(git log --format="%s" -n $nrefs)) 37 | 38 | i=0 39 | for ref in "${refs[@]}"; do 40 | i=$(($i + 1)) 41 | echo "[$i] $ref" 42 | done 43 | 44 | echo -n "Number to squash in: " 45 | read r 46 | 47 | ref=${refs[$r-1]} 48 | 49 | git commit -m "fixup! $ref" $@ 50 | 51 | -------------------------------------------------------------------------------- /git-hooks: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | usage() { 4 | echo "`basename $0` enable|disable " 5 | } 6 | 7 | if [[ $# < 1 ]]; then 8 | usage 9 | exit 1 10 | fi 11 | 12 | cmd=$1 13 | hook=$2 14 | hook_disabled=$hook.disabled 15 | case $cmd in 16 | enable) 17 | ;; 18 | disable) 19 | ;; 20 | *) 21 | usage 22 | exit 1 23 | ;; 24 | esac 25 | 26 | git rev-list HEAD~..HEAD > /dev/null 27 | if [ $? -ne 0 ]; then 28 | exit 1 29 | fi 30 | 31 | # git rev-list will take care of searching for the git-repo 32 | # so if we get here, we know we're inside a git repo. 33 | while ! [ -e ".git" ]; do 34 | cd .. 35 | done 36 | 37 | pushd .git/hooks > /dev/null || exit 1 38 | 39 | case $cmd in 40 | enable) 41 | if ! [[ -e "$hook_disabled" ]]; then 42 | echo "Hook $hook_disabled does not exist" 43 | exit 1 44 | fi 45 | mv $hook_disabled $hook 46 | ;; 47 | disable) 48 | if ! [[ -e "$hook" ]]; then 49 | echo "Hook $hook does not exist" 50 | exit 1 51 | fi 52 | mv $hook $hook_disabled 53 | ;; 54 | *) 55 | ;; 56 | esac 57 | 58 | popd > /dev/null || exit 1 59 | -------------------------------------------------------------------------------- /git-post-checkout-nagging-hook: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # post-checkout hook that reminds you to add a branch description whenever 4 | # you checkout that branch. 5 | # 6 | # save as: .git/hooks/post-checkout 7 | # don't forget to chmod +x 8 | # 9 | # add list of branches you don't want to be nagged about in your git config 10 | # 11 | # [branch-tools] 12 | # no-description-branches = "master" 13 | 14 | 15 | whitelist=`git config --get-all branch-tools.no-description-branches` 16 | 17 | if [ -z "$whitelist" ]; then 18 | whitelist="master" 19 | fi 20 | 21 | prev="$1" 22 | new="$2" 23 | branch_switch="$3" 24 | 25 | if [ -z "$branch_switch" ] || [ $branch_switch -eq 0 ]; then 26 | exit 0 # this was a file checkout 27 | fi 28 | 29 | zero="0000000000000000000000000000000000000000" 30 | if [ "$prev" = "$zero" ]; then 31 | exit 0 # this was a clone 32 | fi 33 | 34 | branch=`git rev-parse --abbrev-ref HEAD` 35 | git show-ref --verify --quiet refs/heads/$branch 36 | if [ $? -ne 0 ]; then 37 | # not a branch 38 | exit 0 39 | fi 40 | 41 | for b in $whitelist; do 42 | if [ $b = $branch ]; then 43 | exit 0 44 | fi 45 | done 46 | 47 | description=`git config branch.$branch.description` 48 | if [ -z "$description" ]; then 49 | echo "This branch has no description. Use: git branch --edit-description " 50 | fi 51 | 52 | 53 | -------------------------------------------------------------------------------- /git-stashed: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | usage() { 3 | echo "usage: `basename $0` [arg ...]" 4 | echo "" 5 | echo "Executes 'git [arg ...]' bracketed by 'git stash' and" 6 | echo "'git stash pop'. If the subcommand fails, exits with its return code" 7 | echo "(without running 'git stash pop')." 8 | echo "" 9 | echo "Example:" 10 | echo "" 11 | echo " $ git pull -r" 12 | echo " Cannot pull with rebase: You have unstaged changes." 13 | echo " Please commit or stash them." 14 | echo "" 15 | echo " $ git stashed pull -r" 16 | echo " Saved working directory and index state WIP on master: ..." 17 | echo " [ ... git pull -r output ... ]" 18 | echo " Dropped refs/stash@{0} (...)" 19 | } 20 | 21 | if [ $# -eq 0 ]; then 22 | usage 23 | exit 1 24 | fi 25 | 26 | git status --porcelain | grep -qv '^??' 27 | dirty_tree=$? 28 | 29 | if [ $dirty_tree -eq 1 ]; then 30 | git "$@" || exit $? 31 | else 32 | git stash save "stashed while running $@" || exit $? 33 | 34 | git "$@" 35 | rc=$? 36 | if [ $rc -eq 0 ]; then 37 | git stash pop || exit $? 38 | else 39 | echo "git $1 failed; resolve the issue then run 'git stash pop' to restore your tree." 40 | exit $rc 41 | fi 42 | fi 43 | -------------------------------------------------------------------------------- /git-patch-set: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Create a patch set, save it in patches/patches-- 4 | # Points patches/latest to the last created directory, so it's easy to 5 | # git-send-email from there 6 | 7 | readonly pre_patchset_hook=.git/hooks/pre-patch-set 8 | readonly post_patchset_hook=.git/hooks/post-patch-set 9 | 10 | range=${!#} 11 | 12 | git rev-list $range > /dev/null 13 | if [ $? -ne 0 ]; then 14 | echo "Failed to look up refs" 15 | exit 1 16 | fi 17 | 18 | # git rev-list will take care of searching for the git-repo 19 | # so if we get here, we know we're inside a git repo. 20 | while ! [ -e ".git" ]; do 21 | cd .. 22 | done 23 | 24 | readonly revs="${range/\//:}" 25 | readonly date=`date +%Y%m%d%H%M` 26 | readonly basedir=patches 27 | readonly dir="patches-$date-$revs" 28 | readonly tmplink="$basedir/latest" 29 | readonly odir="$basedir/$dir" 30 | 31 | if [ -e "$odir" ]; then 32 | echo "Directory $dir already exists. Oops." 33 | exit 1 34 | fi 35 | 36 | readonly first=`git rev-list --reverse $range | head -n 1` 37 | readonly last=`git rev-list -n 1 $range` 38 | 39 | if [ -x "$pre_patchset_hook" ]; then 40 | set -e 41 | ./$pre_patchset_hook $first $last 42 | set +e 43 | fi 44 | 45 | mkdir -p "$odir" 46 | rm -f $tmplink 47 | ln -sf $dir $tmplink 48 | 49 | git format-patch -M -n -o $odir "$@" 50 | 51 | if [ -x "$post_patchset_hook" ]; then 52 | set -e 53 | ./$post_patchset_hook $first $last 54 | set +e 55 | fi 56 | -------------------------------------------------------------------------------- /git-split-commit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Split the last commit into multiple patches. Usually used during 4 | # re-basing. 5 | # 6 | # 7 | 8 | usage() { 9 | cat <<- EOF 10 | Usage: $(basename $0) [--before|--after] 11 | --before ... re-commit the patch before any leftovers (default) 12 | --after ... commit everything else first, then the leftover 13 | EOF 14 | } 15 | 16 | mode="before" 17 | case "$1" in 18 | --after) 19 | mode="after" 20 | ;; 21 | --before) 22 | mode="before" 23 | ;; 24 | --help) 25 | usage 26 | exit 0 27 | ;; 28 | "") 29 | ;; 30 | *) 31 | echo "Invalid argument: $1" 32 | usage 33 | exit 1 34 | ;; 35 | esac 36 | 37 | git diff > /dev/null 38 | if [ $? -ne 0 ]; then 39 | echo "Unstaged changes, please commit them first" 40 | exit 1 41 | fi 42 | 43 | readonly sha=$(git rev-list -n 1 HEAD) 44 | 45 | git reset HEAD~1 46 | if [ $mode = "before" ]; then 47 | git add -p 48 | 49 | if [ $? -ne 0 ]; then 50 | echo "---------------------------------------------" 51 | echo "git add -p failed, dropping you into a shell." 52 | echo "Exiting the shell will commit" 53 | $SHELL 54 | fi 55 | 56 | echo -n "Need to add any files? [y/N] " 57 | read addfiles 58 | 59 | if [ "$addfiles" = "y" -o "$addfiles" = "Y" ]; then 60 | echo "------------------------------" 61 | echo "Dropping you into a shell now." 62 | echo "Exiting the shell will commit." 63 | $SHELL 64 | fi 65 | 66 | git commit -c $sha 67 | 68 | echo "--------------------------------------" 69 | echo "Add remaining hunks now. To abort, run" 70 | echo " git reset --hard $sha" 71 | 72 | else # mode == after 73 | echo "---------------------------------------------------" 74 | echo "Starting a shell. Commit everything unrelated first" 75 | echo "Exiting the shell will re-commit" 76 | $SHELL 77 | git commit -ac $sha 78 | 79 | echo -n "Happy with the split? N will reset [Y/n] " 80 | read happy 81 | if [ "$happy" = "n" -o "$happy" = "N" ]; then 82 | git reset --hard $sha 83 | fi 84 | fi 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /git-branch-description: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Show a bunch of information about a given branch. 4 | # 5 | 6 | if [ -t 1 ]; then 7 | readonly BOLD=`tput bold` 8 | readonly YELLOW=`tput setaf 3` 9 | readonly RESET=`tput sgr0` 10 | readonly COLS=`tput cols` 11 | else 12 | readonly COLS=80 13 | fi 14 | 15 | if [ "$1" = "--help" ]; then 16 | echo "Usage: `basename $0` " 17 | exit 0 18 | fi 19 | 20 | branch=$1 21 | if [ -z "$branch" ]; then 22 | branch=`git symbolic-ref --short HEAD` 23 | fi 24 | 25 | set -e 26 | git log -n1 $branch > /dev/null 27 | set +e 28 | 29 | readonly first_date=`git reflog --format=format:%cd $branch | tail -n 2 | head -n 1` 30 | readonly last_date=`git reflog -n 1 --format=format:%cd $branch` 31 | 32 | echo "Branch $BOLD$branch$RESET" 33 | echo "Branched: $YELLOW$first_date$RESET" 34 | echo "Last commit: $YELLOW$last_date$RESET" 35 | readonly description=`git config branch.$branch.description` 36 | if [ -z "$description" ]; then 37 | echo "" 38 | echo "This branch has no description. Use:" 39 | echo " git branch --edit-description " 40 | else 41 | echo "" 42 | echo $description | fold -s 43 | fi 44 | echo "" 45 | 46 | upstream=$2 47 | if [ -z "$upstream" ]; then 48 | upstream=`git symbolic-ref --short HEAD` 49 | fi 50 | if [ "$upstream" != "$branch" ]; then 51 | printf '%*s' "${C:-$(($COLS/2-9))}" | tr ' ' = 52 | printf "$YELLOW Unmerged commits$RESET " 53 | printf '%*s\n' "${C:-$(($COLS/2-9))}" | tr ' ' = 54 | 55 | readonly mergebase=`git merge-base $branch $upstream` 56 | readonly branchsha=`git rev-list -n1 $branch` 57 | readonly upstreamsha=`git rev-list -n1 $upstream` 58 | 59 | if [ "$mergebase" = "$upstreamsha" ] || [ "$mergebase" = "$branchsha" ]; then 60 | echo "" 61 | echo "Branch $branch already merged into $upstream" 62 | echo "" 63 | else 64 | echo "Commits on $RESET$BOLD$branch$RESET not in $BOLD$upstream$RESET:" 65 | git cherry -v $upstream $branch | grep "^+" | sed -e "s/^+ /$YELLOW/" | sed -e "s/ /$RESET /" 66 | echo "" 67 | 68 | echo "Commits on $BOLD$branch$RESET already merged to $BOLD$upstream$RESET:" 69 | git cherry -v $upstream $branch | grep "^-" | sed -e "s/^- /$YELLOW/" | sed -e "s/ /$RESET /" 70 | echo "" 71 | fi 72 | fi 73 | 74 | printf '%*s' "${C:-$(($COLS/2-5))}" | tr ' ' = 75 | printf "${YELLOW} Activity $RESET" 76 | printf '%*s\n' "${C:-$(($COLS/2-5))}" | tr ' ' = 77 | 78 | git --no-pager reflog -v -n40 --date=relative --grep-reflog="from $branch" --grep-reflog="to $branch" HEAD 79 | 80 | 81 | #git --no-pager reflog show --pretty --no-abbrev-commit $branch 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | git-branch-tools 2 | ================ 3 | 4 | A set of git tools to help manage branches better. Some of the tools use 5 | git-config hooks for local customization, have a look at the tools directly. 6 | 7 | Archiving branches 8 | ------------------ 9 | Sometimes branches are not actively developed anymore but need to be 10 | preserved for posterity. These branches are clogging up the branch view. 11 | 12 | git archive-branch mybranch 13 | 14 | moves `mybranch` to `archive/2013/mybranch` and tags the current top commit 15 | with a message containing bits of the branch history. 16 | 17 | Showing recent branches 18 | ----------------------- 19 | Working on many branches can mean you forget which branch you worked on last 20 | week, or the week before. 21 | 22 | git recent-branches 23 | 24 | lists the various branches checked out over the history, including the date 25 | and last commit date on that branch. 26 | 27 | Example: 28 | 29 | next 4 hours ago last commit 6 days ago 30 | server-1.13-branch 4 hours ago last commit 2 weeks ago 31 | touch-grab-race-condition-56578-v2 3 days ago last commit 3 days ago 32 | touch-grab-race-condition-56578 4 days ago last commit 6 days ago † 33 | bug/xts-segfaults 6 days ago last commit 6 days ago † 34 | master 6 days ago last commit 3 weeks ago 35 | for-keith 10 days ago last commit 2 weeks ago 36 | memleak 13 days ago last commit 2 weeks ago 37 | 38 | 39 | ### Dependencies 40 | 41 | git-recent-branches requires GitPython: http://pythonhosted.org/GitPython/ 42 | 43 | Better branch descriptions 44 | -------------------------- 45 | Can't remember what branch "fix-race-condition" was? Me neither. That's what 46 | 47 | git branch-description [branchname] [upstream] 48 | 49 | will tell you. If upstream is given, it'll also show you what has been 50 | merged into upstream already (by patch, not by commit). 51 | 52 | And install the `git-post-checkout-nagging-hook` as your 53 | `.git/hooks/post-checkout` to make sure you get reminded to set the branch 54 | description. 55 | 56 | Creating patch sets 57 | ------------------- 58 | A wrapper around git-format-patch, that drops the patches into a directory 59 | named after the patch set and date. 60 | 61 | $> git patch-set origin/master..HEAD~2 62 | $> ls patches 63 | patches/patches-2013-08-30-10:47-origin\master..HEAD~2/ 64 | patches/latest -> patches/patches-2013-08-30-10:47-origin\master..HEAD~2/ 65 | 66 | patches/latest always links to the last generated patcheset. 67 | 68 | Note: git-patch-set will run the pre-push hook (if any). Ideally, you'll 69 | have a pre-push hook that runs make check, so you never send out patches 70 | that haven't at least be built and tested. 71 | 72 | $> cat .git/hooks/pre-push 73 | cat ~/code/libevdev/.git/hooks/pre-push 74 | #!/bin/bash 75 | set -e 76 | echo "running make check" 77 | make check 78 | 79 | Splitting commits 80 | ----------------- 81 | During rebase, when you realise that a commit needs to be broken up into 82 | two. 83 | 84 | $> git rebase -i 85 | # mark commit to split as "edit" 86 | $> git split-commit 87 | 88 | The tool now calls git add -p, letting you add the hunks needed (and/or missing 89 | files). Finish up, it will re-commit with the same message and drop you 90 | back to the shell to finish up and continue. Alternatively, 91 | 92 | $> git split-commit --after 93 | 94 | will drop you into a shell immediately, letting you git add and commit 95 | anything you want committed _before_ the current commit. Exiting the shell 96 | will commit the rest. 97 | 98 | Squashing commits 99 | ----------------- 100 | git config's rebase.autosquash enables automatic re-ordering and squashing 101 | of commits starting with "squash!" or "fixup!", followed by a subject line 102 | that matches an earlier commit. 103 | 104 | git-squash is a wrapper script around that to make it easier to remember. 105 | 106 | $> git add myfile yourfile 107 | $> git squash 108 | [1] this is the most recent commit 109 | [2] previos commit 110 | [3] some other commit 111 | [4] a commit message 112 | Number to squash in: 3 113 | 114 | Arguments passed to git-squash will be passed to git-commit. 115 | 116 | 117 | Adding tags to commit messages 118 | ------------------------------ 119 | Some projects request Reviewed-by, Signed-off-by or bugzilla references to 120 | be adeed to a commit message. git-add-tag is a script to add these to a set 121 | of commit messages. 122 | 123 | $> git add-tag HEAD~6.. 124 | 125 | This will open up an editor window. Write the line you need to add to the 126 | commit range and exit. The line will be appended to all commits in the 127 | range. 128 | 129 | 130 | Testing commits in sequence 131 | --------------------------- 132 | After merging a set of patches, sometimes it's necessary to make sure that 133 | each patch fulfills certain conditions (e.g. pass a test suite). 134 | 135 | $> git for-each origin/master.. make check 136 | 137 | Runs make check on each commit from origin/master to HEAD and prints a log. 138 | 139 | -------------------------------------------------------------------------------- /git-for-each: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # git for-each ... command ... 4 | # 5 | # For each commit, run the command and collect the exit code. 6 | # Works somewhat similar to git-bisect. 7 | # 8 | # Uses a temporary branch FOREACH that is deleted on success. 9 | 10 | set -e 11 | 12 | usage() { 13 | echo "usage: `basename $0` [--ignore-error] ... [command] [arg arg ...]" 14 | echo " `basename $0` --abort|--continue|--skip|--show-log" 15 | echo " `basename $0` --help" 16 | echo "" 17 | echo "Check out each commit in the given range and runs the" 18 | echo "given command. If the command exists with a non-zero exit code," 19 | echo "stop and drop back to the shell, otherwise continue" 20 | echo "with the next commit." 21 | echo "" 22 | echo "Options" 23 | echo " --abort ... clean up from a previous for-each run" 24 | echo " --continue ... continue from an interrupted run, starting" 25 | echo " with the current commit" 26 | echo " --ignore-error ... ignore non-zero exit code from command" 27 | echo " and continue anyway" 28 | echo " --show-log ... show the current log" 29 | echo " --skip ... skip this commit, continue with the next" 30 | } 31 | 32 | cleanup() { 33 | set +e 34 | branch=`git rev-parse --abbrev-ref HEAD` 35 | if test "$branch" = "FOREACH"; then 36 | prev_branch="-" 37 | if test -e .git/FOREACH_START; then 38 | prev_branch=`cat .git/FOREACH_START` 39 | fi 40 | git checkout --quiet $prev_branch 41 | fi 42 | git branch --quiet -D FOREACH > /dev/null 2>&1 43 | rm -f .git/FOREACH_START 44 | rm -f .git/FOREACH_CMD 45 | rm -f .git/FOREACH_LOG 46 | set -e 47 | } 48 | 49 | exit_if_local_changes() { 50 | if ! git diff-files --quiet; then 51 | echo >&2 "Your index contains uncommitted changes, aborting." 52 | exit 1 53 | fi 54 | } 55 | 56 | check_in_progress() { 57 | branch=`git rev-parse --abbrev-ref HEAD` 58 | if test "$branch" != "FOREACH" || 59 | ! test -e .git/FOREACH_START || 60 | ! test -e .git/FOREACH_CMD || 61 | ! test -e .git/FOREACH_LOG; then 62 | echo >&2 "No foreach in progress?" 63 | exit 1 64 | fi 65 | } 66 | 67 | show() { 68 | check_in_progress 69 | 70 | cat .git/FOREACH_LOG 71 | exit 0 72 | } 73 | 74 | cont() { 75 | check_in_progress 76 | 77 | command=`cat .git/FOREACH_CMD` 78 | refspec=`head -n 1 .git/FOREACH_LOG` 79 | 80 | if test "x$command" = "x" || 81 | test "x$refspec" = "x"; then 82 | echo >&2 "Invalid or corrupted for-each files. Aborting" 83 | exit 1 84 | fi 85 | 86 | revlist=`git rev-list --reverse $refspec ^HEAD~` 87 | 88 | process_revlist $revlist 89 | } 90 | 91 | process_revlist() { 92 | if test 0$skip -ne 0; then 93 | echo "# skip: `git log -n1 --abbrev-commit --format=oneline`" >> .git/FOREACH_LOG 94 | shift $skip 95 | fi 96 | 97 | echo "# continued" >> .git/FOREACH_LOG 98 | 99 | local _revlist=$@ 100 | for rev in ${_revlist}; do 101 | git reset --quiet --hard $rev 102 | echo "Processing commit `git log -n1 --abbrev-commit --format=oneline`" 103 | set +e 104 | ($command) 105 | exitcode=$? 106 | set -e 107 | if test $exitcode -eq 0; then 108 | echo -n "success: " >> .git/FOREACH_LOG 109 | else 110 | rc=1 111 | echo -n "fail $exitcode: " >> .git/FOREACH_LOG 112 | fi 113 | git log -n1 --abbrev-commit --format=oneline >> .git/FOREACH_LOG 114 | if test 0$rc -ne 0 -a 0$ignore_errors -eq 0; then 115 | exit $rc 116 | fi 117 | done 118 | 119 | echo "`basename $0` finished. Log:" 120 | echo -n "commit range: " 121 | 122 | cat .git/FOREACH_LOG 123 | 124 | cleanup 125 | } 126 | 127 | sigint () { 128 | kill 0 -SINT 129 | exit 1 130 | } 131 | 132 | abort=0 133 | cont=0 134 | show=0 135 | skip=0 136 | 137 | while true; do 138 | case $1 in 139 | --abort) 140 | abort=1 141 | break 142 | ;; 143 | --continue) 144 | cont=1 145 | ;; 146 | --skip) 147 | skip=1 148 | ;; 149 | --ignore-errors) 150 | ignore_errors=1 151 | ;; 152 | --show-log) 153 | show=1 154 | ;; 155 | *) 156 | break 157 | ;; 158 | esac; 159 | shift 160 | done 161 | 162 | trap 'sigint' INT 163 | 164 | modes=$((0$abort + 0$cont + 0$skip + 0$show)) 165 | if test $modes -gt 1; then 166 | usage 167 | exit 1 168 | fi 169 | 170 | if test 0$show -ne 0; then 171 | show 172 | elif test 0$abort -ne 0; then 173 | cleanup 174 | exit 0 175 | elif test 0$cont -ne 0 -o 0$skip -ne 0; then 176 | cont 177 | exit 0 178 | fi 179 | 180 | if test $# -lt 2; then 181 | usage 182 | exit 1 183 | fi 184 | 185 | exit_if_local_changes 186 | 187 | # git diff will take care of searching for the git-repo 188 | # so if we get here, we know we're inside a git repo. 189 | while ! test -e .git; do 190 | cd .. 191 | done 192 | 193 | if test -e ".git/FOREACH_START" || 194 | test -e ".git/FOREACH_CMD" || 195 | test -e ".git/FOREACH_LOG"; then 196 | echo >&2 "Leftovers from previous for-each detected. aborting." 197 | exit 1 198 | fi 199 | 200 | refspec=$1 201 | 202 | shift 203 | command="" 204 | while test $# -gt 0; do 205 | arg=$1 206 | command="$command $arg" 207 | shift 208 | done 209 | 210 | ref_start=`git rev-list --abbrev-commit $refspec | tail -n 1` 211 | ref_end=`git rev-list --max-count=1 --abbrev-commit $refspec` 212 | revlist=`git rev-list --reverse $refspec` 213 | 214 | rc=0 215 | # Note: we need to start at ref_start~, otherwise the revlist generated on 216 | # continue/skip doesn't include the first real commit we care about 217 | echo "$ref_start~..$ref_end" > .git/FOREACH_LOG 218 | echo "$command" > .git/FOREACH_CMD 219 | git rev-parse --abbrev-ref HEAD > .git/FOREACH_START 220 | git checkout --quiet -b FOREACH 221 | 222 | process_revlist $revlist 223 | 224 | exit $rc 225 | -------------------------------------------------------------------------------- /git-archive-branch: -------------------------------------------------------------------------------- 1 | #!/bin/bash -u 2 | # 3 | # git-archive-branch: move branchname to archive/2013/branchname and tag it 4 | # as archive-2013-03-04-branchname. 5 | # 6 | # optional git-config hooks: 7 | # branch-tools.archive ... prefix for branch names, default "archive" 8 | # branch-tools.no-description-branches ... list of branches that do 9 | # not need descriptions 10 | 11 | readonly DEFAULT_ARCHIVE="archive" 12 | ARCHIVE=`git config branch-tools.archive` 13 | if [ -z "$ARCHIVE" ]; then 14 | ARCHIVE=$DEFAULT_ARCHIVE 15 | fi 16 | 17 | # Strip archive off the branch name, store in $2 18 | normalize_branch() { 19 | local branch=$1 20 | local __normalized_name=$2 21 | 22 | branch=${branch/$ARCHIVE\/20*\//} 23 | eval $__normalized_name="'$branch'" 24 | } 25 | 26 | # return 0 if $branch exists, or non-zero otherwise 27 | branch_exists() { 28 | local readonly branch=$1 29 | git log -n1 $branch -- > /dev/null 2>&1 30 | [[ $? -eq 0 ]] && return 0 || return 1 31 | } 32 | 33 | # return 0 if $tag exists, or non-zero otherwise 34 | tag_exists() { 35 | local readonly tag=$1 36 | git log -n1 $tag -- > /dev/null 2>&1 37 | [[ $? -eq 0 ]] && return 0 || return 1 38 | } 39 | 40 | # Create archived branch name from $branch and store in $2 41 | make_archive_branch_name() { 42 | local readonly branch=$1 43 | local __archived_branch=$2 44 | 45 | local normalized_branch='' 46 | local readonly year=`date +%Y` 47 | 48 | normalize_branch $branch normalized_branch 49 | 50 | local archive_branch_name="$ARCHIVE/$year/$normalized_branch" 51 | 52 | eval $__archived_branch="'$archive_branch_name'" 53 | } 54 | 55 | # return tag name for $branch and store it in $2 56 | make_tag() { 57 | local readonly branch=$1 58 | local readonly date=`date +%Y-%m-%d` 59 | local normalized_branch='' 60 | local __tag=$2 61 | 62 | normalize_branch $branch normalized_branch 63 | 64 | tagname=archive-$date-$normalized_branch 65 | 66 | eval $__tag="'$tagname'" 67 | } 68 | 69 | # Archive $branch to $ARCHIVES and tag last commit 70 | archive() { 71 | local readonly branch=$1 72 | local archive_branch='' 73 | local tag='' 74 | 75 | case $branch in 76 | $ARCHIVE*) 77 | echo "Branch $branch appears to be archived already. Aborting." 78 | exit 1 79 | ;; 80 | *) 81 | if ! ( branch_exists $branch ); then 82 | echo "Branch $branch does not exist." 83 | exit 1 84 | fi 85 | ;; 86 | esac 87 | 88 | make_archive_branch_name $branch archive_branch 89 | if ( branch_exists $archive_branch ); then 90 | echo "Error: Branch $archive_branch already exists." 91 | echo "Move out of the way first." 92 | exit 1 93 | fi 94 | 95 | make_tag $branch tag 96 | if ( tag_exists $tag ); then 97 | echo "Error: Tag '$tag' already exists." 98 | echo "Move out of the way first." 99 | exit 1 100 | fi 101 | 102 | echo -n "Move branch $branch to $archive_branch? [y/n] " 103 | read confirm 104 | if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then 105 | exit 0; 106 | fi 107 | 108 | # add description unless branch is in no-description-branches 109 | local description=`git config branch.$branch.description` 110 | if [ -z "$description" ]; then 111 | local readonly no_desc_branches=`git config branch-tools.no-description-branches` 112 | local skip_desc=0 113 | for ndb in $no_desc_branches; do 114 | if [ "$ndb" = "$branch" ]; then 115 | skip_desc=1 116 | break 117 | fi 118 | done 119 | if [ $skip_desc -eq 0 ]; then 120 | echo "Archiving a branch without a description is not recommended." 121 | echo "Use git branch --edit-description before archiving." 122 | echo -n "Continue anyway? [y/n] " 123 | read confirm 124 | if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then 125 | exit 0; 126 | fi 127 | fi 128 | fi 129 | 130 | local readonly tmpfile=`mktemp` 131 | cat > $tmpfile << EOF 132 | Branch: $branch. 133 | Archived: `date`. 134 | 135 | $description 136 | 137 | Top commits: 138 | `git log -n 10 --pretty=oneline $branch --` 139 | 140 | EOF 141 | 142 | echo "Tag message:" 143 | echo "------------------------------------------------------------------------" 144 | 145 | cat $tmpfile 146 | echo "------------------------------------------------------------------------" 147 | echo -n "Edit message? [y/n] " 148 | read confirm 149 | if test "$confirm" = "y" || test "$confirm" = "Y"; then 150 | $EDITOR $tmpfile 151 | fi 152 | 153 | git branch -m $branch $archive_branch 154 | git tag -a -F $tmpfile $tag $archive_branch 155 | rm $tmpfile 156 | 157 | echo "Archived and tagged as $tag." 158 | } 159 | 160 | # Search for $branch in the archive and move it back, leaving the tags 161 | # intact. 162 | restore() { 163 | branch=$1 164 | 165 | case $branch in 166 | $ARCHIVE*) 167 | if ! ( branch_exists $branch ); then 168 | echo "Branch $branch does not exist in the archives" 169 | exit 1 170 | fi 171 | 172 | branch_to_restore=$branch 173 | ;; 174 | *) 175 | if ( branch_exists $branch ); then 176 | echo "Branch $branch looks like a normal, existing branch" 177 | exit 1 178 | fi 179 | 180 | echo "Searching for $branch in the archives" 181 | for b in `git branch | grep $ARCHIVE | sed -e "s/\*\? \+//"`; do 182 | local normalized_b='' 183 | normalize_branch $b normalized_b 184 | 185 | if [ "$normalized_b" = "$branch" ]; then 186 | echo "Found matching archived branch $b" 187 | branch_to_restore=$b 188 | break; 189 | fi 190 | done 191 | ;; 192 | esac 193 | 194 | if [ -z "$branch_to_restore" ]; then 195 | echo "Failed to find branch to restore" 196 | exit 1 197 | fi 198 | 199 | local normalized_branch='' 200 | normalize_branch $branch_to_restore normalized_branch 201 | if ( branch_exists $normalized_branch ); then 202 | echo "Attempting to restore to branch $normalized_branch, but it already exists." 203 | echo "Move out of the way first" 204 | exit 1 205 | fi 206 | 207 | echo -n "Restoring archived branch $branch_to_restore as $normalized_branch. [y/n] " 208 | read confirm 209 | if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then 210 | exit 0; 211 | fi 212 | 213 | git branch -m $branch_to_restore $normalized_branch 214 | } 215 | 216 | # restore $branch 217 | undo() { 218 | local branch=$branch 219 | case $branch in 220 | $ARCHIVE*) 221 | if ! ( branch_exists $branch ); then 222 | echo "Branch $branch does not exist in the archives." 223 | exit 1 224 | fi 225 | ;; 226 | *) 227 | echo "Specify full archived branch name for undo." 228 | exit 1 229 | ;; 230 | esac 231 | 232 | local normalized_branch='' 233 | normalize_branch $branch normalized_branch 234 | 235 | if ( branch_exists $normalized_branch ); then 236 | echo "Branch $normalized_branch already exists, cannot undo." 237 | exit 1 238 | fi 239 | 240 | local tag='' 241 | make_tag $branch tag 242 | 243 | if ! ( tag_exists $tag ); then 244 | echo "Tag $tag does not exist, cannot undo." 245 | exit 1 246 | fi 247 | 248 | echo -n "Undoing archive of branch $branch? [y/n] " 249 | read confirm 250 | if ! test "$confirm" = "y" && ! test "$confirm" = "Y"; then 251 | exit 0; 252 | fi 253 | 254 | git tag -d $tag 255 | git branch -m $branch $normalized_branch 256 | } 257 | 258 | usage() { 259 | cat <<- EOF 260 | `basename $0` [--restore|--undo] [branch] 261 | 262 | `basename $0` [branch] 263 | moves the current or given branch to $ARCHIVE 264 | 265 | `basename $0` --restore [branch] 266 | restores the branch from the archive 267 | 268 | `basename $0` --undo $ARCHIVE//branch 269 | restores the branch and deletes archived tags 270 | This will only work immediately after a archive operation 271 | EOF 272 | } 273 | 274 | restore='' 275 | undo='' 276 | branch='' 277 | 278 | while [ $# -gt 0 ]; do 279 | case $1 in 280 | --restore) 281 | restore=1 282 | shift 283 | ;; 284 | --undo) 285 | undo=1 286 | shift 287 | ;; 288 | -h|--help) 289 | usage 290 | exit 291 | ;; 292 | *) 293 | branch=$1 294 | shift 295 | ;; 296 | esac 297 | done 298 | 299 | if [ -n "$restore" ] && [ -n "$undo" ]; then 300 | echo "Undo or restore? Pick one only." 301 | exit 1 302 | fi 303 | 304 | if test -z "$branch"; then 305 | branch=`git symbolic-ref --short HEAD` 306 | fi 307 | 308 | if [ -n "$restore" ]; then 309 | restore $branch 310 | elif [ -n "$undo" ]; then 311 | undo $branch 312 | else 313 | archive $branch 314 | fi 315 | -------------------------------------------------------------------------------- /git-recent-branches: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # coding: utf-8 3 | # 4 | # git recent-branches shows a list of recently checked-out branches. 5 | # 6 | 7 | import git 8 | import re 9 | import os 10 | import sys 11 | import time 12 | import logging 13 | import gettext 14 | import argparse 15 | import configparser 16 | import termcolor 17 | 18 | class reflog(dict): 19 | pass 20 | 21 | def git_config_noauto(setting): 22 | """Convert true/false/"auto" state into a boolean""" 23 | return setting is True or (setting == "auto" and sys.stdout.isatty()) 24 | 25 | def error(msg): 26 | print(msg, file=sys.stderr) 27 | 28 | def reltime_to_now(t, tzoff): 29 | utc_sec = t - tzoff; 30 | now_utc_sec = time.time() - time.timezone 31 | 32 | delta = int(now_utc_sec - utc_sec) 33 | 34 | if delta < 0: 35 | logging.error("Date is in the future?") 36 | return "in the future?" 37 | 38 | datewords = ( 39 | (60 * 60 * 24 * 365, lambda n : gettext.ngettext("year", "years", n)), 40 | (60 * 60 * 24 * 30, lambda n : gettext.ngettext("month", "months", n)), 41 | (60 * 60 * 24 * 7, lambda n : gettext.ngettext("week", "weeks", n)), 42 | (60 * 60 * 24, lambda n : gettext.ngettext("day", "days", n)), 43 | (60 * 60, lambda n : gettext.ngettext("hour", "hours", n)), 44 | (60, lambda n : gettext.ngettext("minute", "minutes", n)), 45 | ) 46 | 47 | count = 0 48 | for (secs, word) in datewords: 49 | count = delta//secs 50 | if count > 1: # better to have 8 days than 1 week 51 | break 52 | 53 | return "%d %s ago" % (count, word(count)) 54 | 55 | def find_repo(): 56 | """Run up from $PWD until the end of the filesystem, trying to find the 57 | git repo. This emulates what git does by default.""" 58 | 59 | across_fs = os.environ.get("GIT_DISCOVERY_ACROSS_FILESYSTEM", False) 60 | cwd = os.getcwd() 61 | dev = os.stat(cwd).st_dev 62 | gitdir = os.path.join(cwd, ".git") 63 | while not git.repo.fun.is_git_dir(gitdir): 64 | oldcwd = cwd 65 | cwd = os.path.abspath(os.path.join(cwd, os.pardir)) 66 | if cwd == oldcwd: 67 | break; 68 | 69 | gitdir = os.path.join(cwd, ".git") 70 | stat = os.stat(cwd) 71 | if not across_fs and stat.st_dev != dev: 72 | error("Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).") 73 | break; 74 | 75 | return gitdir 76 | 77 | def init_repo(): 78 | gitdir = os.environ.get("GIT_DIR", find_repo()) 79 | if not git.repo.fun.is_git_dir(gitdir): 80 | error("fatal: Not a git repository") 81 | sys.exit(1) 82 | 83 | repo = git.Repo(path = gitdir) 84 | repo.branches_set = { head.name for head in repo.branches } 85 | return repo 86 | 87 | def is_sha1sum(s): 88 | # 40-byte sha1sum, a direct checkout. This is safe 89 | # there are no 40-char english words with a-f 90 | m = re.match(r"[a-f0-9]{40}", s) 91 | if m != None: 92 | logging.debug("%s is a sha1sum" % s) 93 | return m != None 94 | 95 | def is_sha1sum_short(s): 96 | m = re.match(r"[a-f0-9]{7}", s) 97 | if m == None: 98 | return False 99 | 100 | # could be a word. check for at least two numbers, not as the first or 101 | # last char. if you name your 7-letter branches a23dead, your fault. 102 | # second condition: digit + letter + digit + letter 103 | m = re.match(r".+[0-9]+.*[0-9]+.+", s) 104 | if m == None: 105 | m = re.match(r".?[0-9]+.+[0-9]+", s) 106 | 107 | if m != None: 108 | logging.debug("%s is a short sha1sum" % s) 109 | return m != None 110 | 111 | def is_tag(repo, s): 112 | if s in repo.tags: 113 | logging.debug("%s is a tag" % s) 114 | return True 115 | return False 116 | 117 | def is_removed(repo, s): 118 | if s not in repo.branches_set: 119 | logging.debug("%s is removed" % s) 120 | return True 121 | return False 122 | 123 | def is_remote(repo, s): 124 | for r in git.remote.Remote.iter_items(repo): 125 | m = re.match(r"%s/.*" % r.name, s) 126 | if m != None: 127 | logging.debug("%s is a remote branch" % s) 128 | return True 129 | return False 130 | 131 | def is_headN(s): 132 | m = re.match(r"HEAD@{[0-9]+}", s) 133 | if m != None: 134 | logging.debug("%s is direct HEAD checkout" % s) 135 | return m != None 136 | 137 | # Drop direct checkouts, tag names, etc. 138 | def filter_logs(args, repo, log_entries): 139 | flogs = [] # filtered logs as dict(from, to, log) 140 | uniq = {} 141 | logged_count = 0 142 | for l in log_entries: 143 | m = re.match(r".*checkout: moving from (.*) to (.*)", l.message) 144 | if m == None: 145 | continue 146 | 147 | log = reflog() 148 | log.fro = m.group(1) 149 | log.to = m.group(2) 150 | log.log = l 151 | 152 | if log.to in uniq: 153 | continue 154 | uniq[log.to] = 1 155 | 156 | if is_headN(log.to): 157 | continue 158 | 159 | # skip direct checkouts 160 | if is_sha1sum(log.to): 161 | continue 162 | 163 | if is_sha1sum_short(log.to): 164 | continue 165 | 166 | if is_remote(repo, log.to): 167 | continue 168 | 169 | if is_tag(repo, log.to): 170 | continue 171 | 172 | if is_removed(repo, log.to): 173 | continue 174 | 175 | flogs.append(log) 176 | 177 | logged_count += 1 178 | if args.limit > 0 and logged_count >= args.limit: 179 | break 180 | 181 | return flogs 182 | 183 | def last_commit(repo, reflog): 184 | commit = repo.commit(reflog.log.newhexsha) 185 | reflog.reltime_commit = reltime_to_now(commit.committed_date, commit.committer_tz_offset) 186 | reflog.commit_msg = commit.message.split("\n")[0] 187 | reflog.commit = commit 188 | 189 | 190 | def print_commit(args, commit): 191 | g = git.cmd.Git() 192 | 193 | opts = ["-n 1"] # git handles -n 1 -n 4 correctly so the user can override this 194 | 195 | try: 196 | config = repo.config_reader() 197 | if git_config_noauto(config.get_value("color", "pager", sys.stdout.isatty())): 198 | opts.append(["--color=always"]) 199 | except configparser.ParsingError: 200 | logging.debug("Parsing error in config") 201 | 202 | opts.append(args.log_options) 203 | 204 | try: 205 | print("") 206 | print(g.log(opts, commit.hexsha)) 207 | print("") 208 | except git.exc.GitCommandError as e: 209 | print(e) 210 | sys.exit(1) 211 | 212 | def print_description(args, repo, reflog): 213 | config = repo.config_reader() 214 | 215 | try: 216 | section = "branch \"%s\"" % reflog.to 217 | desc = config.get_value(section, "description", None) 218 | if desc: 219 | print("") 220 | for line in desc.split("\\n"): 221 | print(" " + line) 222 | except configparser.ParsingError: 223 | logging.debug("Parsing error in config") 224 | pass 225 | 226 | def walk_reflog(args, repo): 227 | head = repo.head 228 | log = head.log() 229 | log.reverse()# sort as most recent first 230 | log = filter_logs(args, repo, log) 231 | 232 | config = repo.config_reader() 233 | 234 | for l in log: 235 | l.reltime = reltime_to_now(*l.log.time) 236 | last_commit(repo, l) 237 | 238 | branch = l.to 239 | try: 240 | if git_config_noauto(config.get_value("color", "branch", sys.stdout.isatty())): 241 | branch = termcolor.colored(branch, "yellow") 242 | except configparser.ParsingError: 243 | logging.debug("Parsing error in config") 244 | 245 | print("{:<45} {:<20} last commit {:<15}".format(branch, l.reltime, l.reltime_commit)) 246 | 247 | if args.print_description: 248 | print_description(args, repo, l) 249 | 250 | if args.last_commit: 251 | print_commit(args, l.commit) 252 | 253 | def get_configured_limit(repo): 254 | config = repo.config_reader() 255 | value = config.get_value("recent-branches", "limit", 20) 256 | return value 257 | 258 | if __name__ == "__main__": 259 | logging.basicConfig(level=logging.ERROR) 260 | 261 | repo = init_repo() 262 | default_limit = get_configured_limit(repo) 263 | 264 | parser = argparse.ArgumentParser(description="Show a list of recently used branches", 265 | formatter_class=argparse.RawDescriptionHelpFormatter, 266 | epilog="Output is in the form of\n\n" 267 | " branchname most recent checkout most recent commit date\n\n" 268 | "Branches are filtered\n" 269 | " * if it looks like a sha1sum\n" 270 | " * if it is a remote branch\n" 271 | " * if it is a HEAD@{3}-like reflog\n" 272 | " * if it is a tag name\n" 273 | "Branches that do not exist anymore in the local repository are not shown") 274 | parser.add_argument("--print-description", 275 | action="store_true", 276 | default=False, 277 | help="Show branch description (if available)") 278 | parser.add_argument("--last-commit", 279 | action="store_true", 280 | default=False, 281 | help="Show top commit on each branch. Uses git log, use git log options for formatting the output") 282 | parser.add_argument("--limit", 283 | type=int, 284 | default=default_limit, 285 | help="Limit output to N branches") 286 | parser.add_argument("log_options", 287 | nargs="*", 288 | metavar="-- --pretty=oneline ...", 289 | help="Options passed to git log. Separate these with " 290 | "a -- from the other options") 291 | 292 | args = parser.parse_args(sys.argv[1:]) 293 | 294 | walk_reflog(args, repo) 295 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------