I am writing a shell script to add a "review" sub command to git, to make it easier to do code reviews. It's basically a fancy wrapper for git diff
and git difftool
commands, but requires less typing.
Some example usages:
# Lists added, deleted, renamed and modified files between master and current branch
git review master -l
# Opens difftool for files modified between master and current branch
git review -m
I would like to enable branch auto completion in my shell script, but cannot for the life of me figure out how to do it. Here is what I'm really after:
git review ma<tab><tab>
And then it should behave like git checkout ma<tab><tab>
.
My shell script:
#!/bin/env bash
function print_usage {
echo "Usage: git review <branch> <-a|-d|-m|-l> [-- paths/to filter/by]"
echo ""
echo " -a: Only review added files"
echo " -d: Only review delete files"
echo " -m: Only review modified files"
echo " -l: List files and and type of modification"
}
if [ -z "$1" ] || [ -z "$2" ]; then
print_usage
exit 1
fi
git_branch=$1
review_command=$2
path_filters=""
shift
shift
if [ "$1" = "--" ]; then
path_filters="$@"
fi
case $review_command in
"-a")
echo "Reviewing added files..."
if [ -z "$path_filters" ]; then
git difftool $git_branch -- $(git diff --name-status $git_branch | grep -E '^A' | awk '{print $2}')
else
git difftool $git_branch -- $(git diff --name-status $git_branch -- $path_filters | grep -E '^A' | awk '{print $2}')
fi
;;
"-d")
echo "Reviewing deleted files..."
if [ -z "$path_filters" ]; then
git difftool $git_branch -- $(git diff --name-status $git_branch | grep -E '^D' | awk '{print $2}')
else
git difftool $git_branch -- $(git diff --name-status $git_branch -- $path_filters | grep -E '^D' | awk '{print $2}')
fi
;;
"-m")
echo "Reviewing modified files..."
if [ -z "$path_filters" ]; then
git difftool $git_branch -- $(git diff --name-status $git_branch | grep -E '^M' | awk '{print $2}')
else
git difftool $git_branch -- $(git diff --name-status $git_branch -- $path_filters | grep -E '^M' | awk '{print $2}')
fi
;;
"-l")
echo "Differences between $git_branch and $(git mybranch)..."
if [ -z "$path_filters" ]; then
git diff --name-status $git_branch
else
git diff --name-status $git_branch -- $path_filters
fi
;;
esac
echo ""
echo "Review finished."
Really, since I'm in the process of typing the command in, I doubt my shell script will have anything to do with the solution.
Some other useful info:
- Windows 10
- Git v2.18.0.windows.1
- Shell: GNU bash, version 4.4.19(2)-release (x86_64-pc-msys)
Is there a way to add git branch auto completion to a custom git extension command?
An interesting related question for Linux: custom git command autocompletion.