sharkdp / bat

A cat(1) clone with wings.
Apache License 2.0
49.28k stars 1.25k forks source link

`git log` broken when using bat as pager #835

Closed tbrodbeck closed 4 years ago

tbrodbeck commented 4 years ago

I like using bat as my pager of choice with syntax highlighting! :). Somewhere across the internet I found the command export PAGER='bat --paging=always' to do exactly that.

Now I realized that my git log did not work anymore (did not show anything) and I found out that it was because of that setting. Setting export PAGER=less and my git log is back.

Is there a way how to make bat-pager and the git log compatible?

FrogBomb commented 4 years ago

Do you have more information about your environment and the version of bat you are using? What you did works fine for me... My system is:

$ uname -srm
Linux 5.5.4-arch1-1 x86_64

I'm using version:

$ yay -Q bat
bat 0.12.1-1
tbrodbeck commented 4 years ago

Thanks for asking! So my system (macOS Catalina) information is

$ uname -srm
Darwin 19.3.0 x86_64

And the bat version is:

$ brew info bat
bat: stable 0.12.1 (bottled)
[...]
eth-p commented 4 years ago

Trying this on my system (macOS Mojave) with git -c core.pager='bat --paging=always --pager="less"' log (I can't use PAGER because I have a global core.pager setting), it works for me.

$ uname -srm
Darwin 18.7.0 x86_64
$ brew info bat
bat: stable 0.12.1 (bottled)

Can you run the following script and paste the output?

#!/usr/bin/env bash
_bat_config="$(bat --config-file)"
_bat_binary="$(which bat 2>&1)"

printf "\n--- System ---\n"
uname -srm
command -v sw_vers &>/dev/null && sw_vers
command -v lsb_release &>/dev/null && lsb_release -a

printf "\n--- Software ---\n"
printf "less: %s\n" "$(less --version | head -n1)"

printf "\n--- Bat ---\n"
bat --version
printf "\n"

printf "Environment:\n"
env | grep '^BAT\|^PAGER=' && printf "\n"

printf "Config:\n"
[[ -f "$_bat_config" ]] && cat "$(bat --config-file)" && printf "\n"

printf "Wrapper:\n"
file "$_bat_binary" | grep "text executable" &>/dev/null && bat "$_bat_binary" && printf "\n"

Also, how big is your git log? Maybe you're being affected by the less --quit-if-one-screen bug?

tbrodbeck commented 4 years ago

Sure:

$ sh batScript.sh

--- System ---
Darwin 19.3.0 x86_64
ProductName:    Mac OS X
ProductVersion: 10.15.3
BuildVersion:   19D76

--- Software ---
less: less 487 (POSIX regular expressions)

--- Bat ---
bat 0.12.1

Environment:
PAGER=bat --paging=always

Config:
# Show line numbers, Git modifications and file header (but no grid)
--style="numbers,changes"
--theme='Monokai Extended'

Wrapper:

Also, how big is your git log? Maybe you're being affected by the less --quit-if-one-screen bug?

The logs are definitively larger than my terminal screen. That should not be the issue.

git -c core.pager='bat --paging=always --pager="less"' log is not working for me as well (just in case thats somehow important)

eth-p commented 4 years ago

I tried to change my environment to be more like yours, but I still can't manage to reproduce this issue. I have a feeling this might be tricky to diagnose.

$ env
...
PAGER=/usr/local/bin/bat --paging=always --style=numbers,changes

$ less --version
less 487 (POSIX regular expressions)

$ git config --global --get core.pager
[unset]

A couple more questions, if you don't mind answering:

tbrodbeck commented 4 years ago
$ env
...
PAGER=/usr/local/bin/bat --paging=always --style=numbers,changes

$ less --version
less 487 (POSIX regular expressions)

$ git config --global --get core.pager
[unset]

I have the same setup and the same version of less (it is also the newest on brew).

This is what I get when I ask for my git versions. 🤔

$ brew info git
git: stable 2.25.0 (bottled), HEAD
[...]
$ git --version
git version 2.21.0 (Apple Git-122)
  • Does this happen with other repositories?

Yes. With all of them.

  • Does it work if you set export BAT_PAGER="cat"?

Nothing changes.

sharkdp commented 4 years ago

Could this be related to #680? Does bat work at all (if you simply use it to display a file)?

tbrodbeck commented 4 years ago

No. Bat works perfectly fine as a pager in general.

sharkdp commented 4 years ago

$_bat_binary is not set.

Hm, why is that? What does which bat output?

tbrodbeck commented 4 years ago

Oh im sorry, it actually was set. I was just trying to explain why the output of the "wrapper" was empty.

Here you go:

$ file $(which bat)
/usr/local/bin/bat: Mach-O 64-bit executable x86_64
eth-p commented 4 years ago

I have the same setup and the same version of less (it is also the newest on brew).

I think 530 is the newest version. Do they not have less 530 for Catalina?

$ brew info less

less: stable 530 (bottled)
Pager program similar to more
http://www.greenwoodsoftware.com/less/index.html
/usr/local/Cellar/less/530_1 (12 files, 336.2KB) *
  Poured from bottle on 2019-10-08 at 16:13:26
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/less.rb
tbrodbeck commented 4 years ago
$ brew info less
less: stable 530 (bottled)
Pager program similar to more
http://www.greenwoodsoftware.com/less/index.html
/usr/local/Cellar/less/530_1 (12 files, 348.3KB) *
  Poured from bottle on 2020-02-24 at 09:35:55
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/less.rb

Oh your right, I don't know why I did not get the update earlier. Does not fix the issue though.

sharkdp commented 4 years ago

How does the output look like? There is just no output at all? Does it exit with a non-zero exit code (echo $?)?

One more idea. Could you please try to run it with --no-config:

git -c core.pager="bat --no-config --paging=always" log
tbrodbeck commented 4 years ago

Screenshot 2020-02-26 at 08 58 13

No output. No exit code.

eth-p commented 4 years ago

#838 was just opened (certain files exit with zero on Catalina). If these two are related and it's something specific to Catalina, that would explain why I can't reproduce it.

eth-p commented 4 years ago

Sorry to ask, but could you try another couple of variations?

$ git -c core.pager="bat --no-config --paging=never" log
$ BAT_PAGER="" git -c core.pager="bat --no-config --paging=always" log
$ git -c core.pager="bat --no-config --color=never --decorations=never --paging=always" log
tbrodbeck commented 4 years ago

Sure. No outputs on that:

sh-3.2$ git -c core.pager="bat --no-config --paging=never" log
sh-3.2$ BAT_PAGER="" git -c core.pager="bat --no-config --paging=always" log
sh-3.2$ git -c core.pager="bat --no-config --color=never --decorations=never --paging=always" log
sharkdp commented 4 years ago

This is a tough one :smile:. But I think we are getting closer.

sh-3.2$ git -c core.pager="bat --no-config --paging=never" log

Interesting. So that rules out any problems related to the pager (that bat calls).

tbrodbeck commented 4 years ago

Ok sound good :D

  • Does git -c core.pager="cat" log work? How about git -c core.pager="cat -n" log?

Both work as expected.

  • What does git -c core.pager="some-non-existing-command" log output?

That leads to interesting behavior: git -c core.pager="asdf" log leads to the same output as git -c core.pager="cat" log. But:

sh-3.2$ git -c core.pager="asdf -n" log
asdf -n: asdf: command not found
sharkdp commented 4 years ago

Okay.

What if you pipe the output of git log into bat explicitly?

git log | bat --no-config
tbrodbeck commented 4 years ago
sh-3.2$ git log | bat --no-config
Segmentation fault: 11
sharkdp commented 4 years ago

There we have it. Ouch :smile:

sharkdp commented 4 years ago

This is why I initially suspected #680 in https://github.com/sharkdp/bat/issues/835#issuecomment-590733724

Could you please make sure that #680 is fixed for you (see https://github.com/sharkdp/bat/issues/680#issuecomment-552219808)

tbrodbeck commented 4 years ago

This is why I initially suspected #680 in #835 (comment)

Could you please make sure that #680 is fixed for you (see #680 (comment))

Oh I am sorry maybe I used wrong terminology in my statement that the pager works fine. It works fine when calling bat on large files. But I just tested bat -p and it always lead to the segmentation fault.

I did reinstall brew as mentioned (#680 (comment)). But I still have the same issue

eth-p commented 4 years ago

Would you be able to send a SHA1 hash of your bat binary so I can compare it with my homebrew-installed binary on Mojave. This will help me figure out where to start looking (bad binary? Catalina-specific issue?)

I was able to reproduce this by downloading one of the older homebrew Catalina binaries from bintray.

Archive SHA1 Affected?
bat-0.12.1.catalina.bottle.tar.gz 40aca416ce1d7dd24dcd423149b169251fc0da2f Yes
bat-0.12.1.catalina.bottle.1.tar.gz 6dd8877a398cb97c4813c227c117855e837bbb45 Yes
bat-0.12.1.catalina.bottle.2.tar.gz a0fdd68eb8a3fc38bf2e7987e43524e27229472e No

The latest one shouldn't be crashing, though. Maybe @tbrodbeck was using one of the older builds?

eth-p commented 4 years ago

@sharkdp Trying the first two binaries, it looks like #680.

Process 80285 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x00000001000d6852 bat`match_at + 1250
bat`match_at:
->  0x1000d6852 <+1250>: jmpq   *(%r10)
    0x1000d6855 <+1253>: movq   -0xb8(%rbp), %r12
    0x1000d685c <+1260>: movl   %r12d, %r14d
    0x1000d685f <+1263>: movq   -0x168(%rbp), %rcx
Target 0: (bat) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  * frame #0: 0x00000001000d6852 bat`match_at + 1250
    frame #1: 0x00000001000dc917 bat`onig_search_with_param + 1687
    frame #2: 0x00000001000d4e78 bat`onig::Regex::search_with_encoding::he36d31797caa25d1 (.llvm.15521931234845958414) + 184
    frame #3: 0x00000001000d51f5 bat`onig::Regex::find::h445391cbfda2acba + 53
    frame #4: 0x000000010008dbb6 bat`syntect::parsing::syntax_set::SyntaxSet::find_syntax_by_first_line::heed929af5bec9705 + 758
    frame #5: 0x0000000100078125 bat`bat::assets::HighlightingAssets::get_syntax::h7a142593e27fa892 + 389
    frame #6: 0x0000000100033748 bat`bat::printer::InteractivePrinter::new::h058277bf3bb0adf5 + 2088
    frame #7: 0x00000001000236cf bat`bat::controller::Controller::run::h39171a5b8f4fea15 + 1823
    frame #8: 0x000000010003e065 bat`bat::main::h07864aaab984eefc + 8197
    frame #9: 0x000000010006f356 bat`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h36e77430ad96293d + 6
    frame #10: 0x000000010027f0d8 bat`std::panicking::try::do_call::had5184326ada574e + 24
    frame #11: 0x000000010028153f bat`__rust_maybe_catch_panic + 31
    frame #12: 0x000000010027fa0b bat`std::rt::lang_start_internal::hd00d0467a3a483f4 + 363
    frame #13: 0x000000010003ed69 bat`main + 41
    frame #14: 0x00007fff681733d5 libdyld.dylib`start + 1

Disassembling one of the affected binaries shows that it's using ____chkstk_darwin:

$ otool -tvV bat | rg '_onig_search_with_param:|_match_at:' -C 1024 | rg 'chkstk'
00000001000d65b3    callq   0x1002a0402 ## symbol stub for: ____chkstk_darwin

While disassembling the one that works, does not:

$ otool -tvV bat | rg 'chkstk'
eth-p commented 4 years ago

@tbrodbeck Could you run the following for me:

$ which bat
$ sha1sum "$(which bat)"
$ otool -tvV "$(which bat)" | grep 'chkstk'
$ pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
$ clang --version

The fix mentioned in #680 has recently been removed from the homebrew formula, and you might be using a version of Xcode that emits broken binaries (11.0 <= ? < 11.3).

tbrodbeck commented 4 years ago
sh-3.2$ which bat
/usr/local/bin/bat
sh-3.2$ sha1sum "$(which bat)"
7eafc98be527af700a0606f9b88b90e3c8f2a9da  /usr/local/bin/bat
sh-3.2$ otool -tvV "$(which bat)" | grep 'chkstk'
00000001000b1ec3    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
00000001000cf223    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
00000001000e9f7a    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
00000001001231f3    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
0000000100125813    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
0000000100129d13    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
00000001001437c3    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
0000000100144cbf    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
000000010014577b    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
000000010014a083    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
0000000100160333    callq   0x100290560 ## symbol stub for: ____chkstk_darwin
sh-3.2$ pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.
sh-3.2$ clang --version
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

My Xcode version is 11.3.1. But I am not sure if this corresponds to my command line tools.

Btw, when I reinstalled bat I got the message

Warning: A newer Command Line Tools release is available.
Update them from Software Update in System Preferences or
https://developer.apple.com/download/more/.

but in my software updates there is no newer version.

I am not sure how to check my CL Tools version. Do you think this is related to the problem?

eth-p commented 4 years ago

Based on the otool result, your build from source was after they removed the workaround. Since the command failed, I can't tell what version your command line tools are though. I also don't know which version of clang is considered fixed either, unfortunately.

There's this stackoverflow thread that might help you figure out how to update your xcode command line tools. Once you figure that out, rebuilding bat again should work.

Or you could try reinstalling bat without -s to see if you get the correct .2 revision bottle?

tbrodbeck commented 4 years ago

Or you could try reinstalling bat without -s to see if you get the correct .2 revision bottle?

That already fixed it. Thank you! :)

Thanks for the awesome support guys. You were really persistent about solving this problem. 👍