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

[Feature request]: Use tig as commit picker #575

Closed
kgraefe opened this issue Mar 12, 2017 · 8 comments
Closed

[Feature request]: Use tig as commit picker #575

kgraefe opened this issue Mar 12, 2017 · 8 comments
Assignees

Comments

@kgraefe
Copy link
Contributor

kgraefe commented Mar 12, 2017

I'd like to use the tig main view for selecting a specific commit and use its commit ID as a parameter for a script, e.g.: git rebase -i $(tig select).

@Avinash-Bhat
Copy link
Contributor

@kgraefe you can bind a key to do that.

Adding the following to ~/.tigrc will do that.

bind main <Ctrl+b> !?git rebase -i %(commit)

@kgraefe
Copy link
Contributor Author

kgraefe commented Mar 16, 2017

Yes, I know. But I have multiple use cases where I need a Git ID and I don't want to create (and remember) a binding for each of them. The git rebase was just an example.

Right now I often find me opening tig, select a commit, copy its ID, close tig, paste the ID and run the command.

@Avinash-Bhat
Copy link
Contributor

@kgraefe you can replace the command for rebase here with your script.

bind main <Ctrl+b> !?/path/to/script %(commit)

@kgraefe
Copy link
Contributor Author

kgraefe commented Mar 16, 2017

Sure. Still that does not help me with what I want to achieve (please see above).

@kgraefe
Copy link
Contributor Author

kgraefe commented Mar 17, 2017

I managed to write a wrapper around tig that does that for me. While implementing, I realized that the output channels need to be swapped in order to see the interface and catch the commit ID it spits out. I believe that you wouldn't want that in the mainline code, as I am not sure about the consequences (though it's working well for me).

Find attached my wrapper script tig-pick, that:

  • loads the user config and remaps <Enter> to print the selected commit ID to stderr before closing tig and
  • runs tig with its output channels swapped.
#!/bin/bash

set -e

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

# Prepare config file
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 commit ID and exit
echo 'bind main <Enter> <sh -c "echo %(commit) >&2"' >> $CONFIG


# Run tig
export TIGRC_USER=$CONFIG
commit=$(tig "$@" 3>&2 2>&1 1>&3-)

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

echo "$commit"

@kgraefe kgraefe closed this as completed Mar 17, 2017
@jonas
Copy link
Owner

jonas commented Mar 18, 2017

We can add the script to the contrib directory if you want to make a PR.

@kgraefe kgraefe reopened this Mar 20, 2017
@kgraefe
Copy link
Contributor Author

kgraefe commented Mar 20, 2017

Sure. I will do some clean-ups and documentation as soon as I can find the time and make a PR after that.

Can you assign me to this ticket? (I can't, apparently)

@jonas
Copy link
Owner

jonas commented Mar 21, 2017

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants