Tig: [Feature request]: Use tig as commit picker

Created on 12 Mar 2017  路  8Comments  路  Source: jonas/tig

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).

Most helpful comment

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"

All 8 comments

@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)

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.

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

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

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

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"

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

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)

Done

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rominf picture rominf  路  6Comments

Anrock picture Anrock  路  8Comments

dmalikov picture dmalikov  路  4Comments

hSATAC picture hSATAC  路  8Comments

danielb2 picture danielb2  路  3Comments