Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tig-pick script #580

Merged
merged 1 commit into from
Mar 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add tig-pick script
Signed-off-by: Konrad Gräfe <[email protected]>
kgraefe committed Mar 24, 2017
commit b2bbca0b3f81d7fa943020179ad4ed75c1e1780e
48 changes: 48 additions & 0 deletions contrib/tig-pick
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh
#
# 'tig-pick' is a wrapper script that uses 'tig' to pick a Git commit from the
# history. On success, The script prints the ID of the commit to standard
# output, so that it can be used as a parameter for subsequent commands, e.g.
# 'git rebase -i $(tig-pick)'
#
# In order to be able to display the interface and to catch the commit ID
# (easily) at the same time, 'tig' has to be run with the standard and error
# output channels swapped.
#
# All parameters passed to the script will be forwarded to 'tig'.
#


set -e

CONFIG=$(mktemp)
trap "rm -f '$CONFIG'" EXIT

# Prepare config file: source user config, if present
if [ ! -z "$TIGRC_USER" ]; then
echo "source $TIGRC_USER" >> $CONFIG
elif [ -f "$HOME/.tigrc" ]; then
echo "source $HOME/.tigrc" >> $CONFIG
fi

# Bind Enter to print the selected commit ID to error output and exit after
# that.
echo 'bind main <Enter> <sh -c "echo %(commit) >&2"' >> $CONFIG


# Run tig with the standard and error output channels swapped.
export TIGRC_USER=$CONFIG
stderr=$(tig "$@" 3>&2 2>&1 1>&3 3>&-) || {
status=$?
echo "$stderr" >&2
exit $status
}
commit=$(echo "$stderr" | tail -n1)

# Check return value for valid commit ID
if ! echo -n "$commit" | grep -iqE '^[0-9a-f]{40}$'; then
echo "$stderr" >&2
exit 1
fi

echo "$commit"