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 documentation on how to get it working with git show #334

Closed
dylan-chong opened this issue Oct 6, 2018 · 8 comments
Closed

Add documentation on how to get it working with git show #334

dylan-chong opened this issue Oct 6, 2018 · 8 comments
Labels
question Further information is requested

Comments

@dylan-chong
Copy link

My main use case for this program is to add syntax highlighting to git show somebranch:somefile. Would be possible to add some documentation on how to use bat as the default git show viewer.

One thing to may be consider is a possible conflict with https://github.com/so-fancy/diff-so-fancy

@dylan-chong
Copy link
Author

This is a really awesome looking tool by the way !

@sharkdp
Copy link
Owner

sharkdp commented Oct 6, 2018

Thank you for the feedback.

You can configure git show to use bat as a pager like this:

> git config --global pager.show bat

However, git will only pipe its output into bat, so there is no way of determining the syntax of the file (if #205 would be implemented, we could determine the syntax for a few files).

Unfortunately, I don't know if there is any way to get more information from git, since - in principle - we also have the filename.

Note that you can always just use a pipe yourself. This way, you can explicitly set the syntax:

> git show HEAD~3:src/main.rs | bat -lrs

I guess it would also be easy to write a simple git-show bash function that would take something like somebranch:somefile.ext as an argument and then call git show somebranch:somefile.ext | bat -l ext.

@sharkdp sharkdp added the question Further information is requested label Oct 6, 2018
@sharkdp
Copy link
Owner

sharkdp commented Oct 7, 2018

Small update: #205 is now implemented, so just configuring bat as a pager works for cases where the syntax of the file can be determined from the first line (typically via shebangs in Python, Bash, etc).

@sharkdp
Copy link
Owner

sharkdp commented Oct 11, 2018

@dylan-chong Any update on this?

@dylan-chong
Copy link
Author

dylan-chong commented Oct 11, 2018 via email

@sharkdp
Copy link
Owner

sharkdp commented Oct 11, 2018

git show doesn't provide any information about the filename to the pager, so I guess an external tool or shell function will be the only option.

This works for me (bash or zsh):

function git-show() {
    local ref="$1"
    local extension="${ref##*.}"

    git show "$ref" | bat --language "$extension"
}

@dylan-chong
Copy link
Author

dylan-chong commented Oct 11, 2018 via email

@sharkdp
Copy link
Owner

sharkdp commented Oct 11, 2018

Yeah, sorry. I really don't think there is a better way unless someone wants to propose a change in Git such that it publishes the filename via some environment variable, for example 😄

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

No branches or pull requests

2 participants