bat
bat copied to clipboard
Add a flag to show `git blame`
It would be awesome to see the syntax highlighted output of bat
with the info from git blame
included. Bonus points for supporting git blame -w
Thank you for your request.
I think I like this idea! We could implement this as a new --style
component (blame
). Alternatively (or in addition), we could provide a --blame
option for easy access.
Layout-wise, we could probably include a second side-bar to the right of the line-number+git-changes side bar. Another idea could be to include it on the right hand side of the screen. Having the source code way out on the right is actually something I don't really like about the default git blame
output.
The git2
crate (that we already use) should provide us with all the necessary information.
-
blame.rs
example application -
git2::Blame
struct
What I really like about this idea is that I typically use git blame
when I'm reading source code. If I use bat
for this already, I can simply append --blame
when I need it.
To add a counter-argument as well: this leads us further away from "do one thing and do it well". It sounds like a rather easy to implement feature, but it might also be quite complex to get the layout right - something which would further complicate our "rendering" logic.
@eth-p @keith-hall @Enselic: What's your opinion on this?
Since it already is within scope of bat to integrate with git, I'd say this is a perfectly reasonable feature request.
However, in my opinion, a good git blame tool needs to enable easy navigation back and forth in the git log, i.e. have e.g. a "blame parent commit of the commit that last changed this line" feature, like git gui blame
and many IDE integrations have. I don't see how such a feature could be implemented in a good way with bat
.
So for me the question boils down to "Is it ok for bat to only be a mediocre git blame tool?", and the answer I'm currently leaning towards is "If you can't do it really well, don't do it at all". There could of course be a clever way to implement "blame parent commit" that I'm not thinking about. That would perhaps change my mind.
FWIW, my original reason for requesting this feature is because I was looking through a bunch of code with git blame
, and its lack of syntax highlighting was frustrating, but I just needed to see when the last commit of certain lines were. To me, that alone is good enough. I never considered this as being a blame history explorer, and I wouldn't expect such a feature from bat
. I see it as being similar to this one feature, https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens#current-line-blame, which just gives hints to the reader about the current code.
Thanks for elaborating on your use case! That particular VS Code extension is what I personally use. For the record, I would like to point out that it has a "Blame Previous Revision" button if you hover over its annotation, which works the same as "blame parent commit" I described above.
I would like to dig a bit deeper into your use case if I may?
Since you know about that extension, I suspect you are a VS Code user. So why not use that extension with VS Code? Then you get git blame information together with syntax highlightning. Sometimes, one is of course in a situation where bat
is available but not VS Code
. But typically, if you are browsing around a git versioned repository, VS Code is also available.
Let me clarify that I am not trying to tell you what to do and not do here, I'm just curious about more details on your particular use case :)
I don't do much code exploration in vscode. I tend to go into a repo and git grep
or go into a git workspace and rg -iu
, then when I find whatever content I am searching for, I bat
the file to see more context. Often times these are on remote machines accessed over ssh+tmux.
would be great to see this
One way this could be implemented is if bat could ignore the first N characters of every line when reading the input. EG: my git blame
has 60 chars left of the file contents, so i could do something like cat whatever.py | bat --ignore-left=60 -l py
Reading the current issue I am lead to believe that adding git-blame
functionality into bat
is not a favorable course.
Nonetheless I found this issue because it seems to be one requested once again in the newer #2810 issue. So I choose to have a stab at it, taking into account the suggestions in the latest issue.
Currently I have implemented a new decorator (following the example of the git status
one) that adds an additional column (before the line-number column, no specific reason, can be changed) and also an optional --blame-format
argument that has a few of the pretty-format options.
Due to the "formatting" happening outside/before the decorator, in the current draft, there is no ability to highlight different parts of the "blame". This might be implemented in case you think I'm moving in the right direction at the moment.
Another thing which I believe would be beneficial but looked like too much change in existing functionality is getting the blame for a line directly in the decorator (as it is now passed to the printer
which then supplies it to the decorator
). But that will require me to have the file's path in the decorator and seems like a bad idea.
Feel free to inform me if you think that this is pointless, in other case, I might set some more time aside to see if I can polish it a bit more.