「git show」で「source-highlight」を使う方法は?

「git show」で「source-highlight」を使う方法は?

使用lessで使用されるソースの強調表示うまく動作しますが、git出力に使用されている言語をgit show見つけることができるファイル拡張子がないので、使用方法を見つけるのが難しいです...source-highlight

less somefile.rb # result is syntax colourised
git show master:somefile.rb | less # no colouring

call の使用git show master:somefile.rb | lessは実際に呼び出すのと同じですless somefile(つまり、 not .rb)。拡張子がないため、ソースハイライトは構文を推測できません。

ソースハイライトが推測するようにする拡張されていない方法はありますか、それとも何らかの方法で--lang-defオプションをLESSOPEN変数に渡すことはできますか?

編集1 ああ、そうです。ソースハイライトは他の方法を使用することができます言語は推論されますが、ソースファイルにはこれらの言語はありません。

ベストアンサー1

パイプファイルと連携するようにgnu source-highlightのsrc-hilite-lesspipe.shを修正しました。https://gist.github.com/jaygooby/9494858d3d481a64819d227a9318f6c7

これはまた、一般的な方法で呼び出すことを意味します。

less code.py

拡張子のないファイルに対してソースコードを強調表示することもできます。言語的推論機能ソースのハイライトは次のように使用します。

less /tmp/mycode

そして(これを行う元の同期)パイプラインファイルは次のようになります。

cat /tmp/file.rb | less
git show master:obfusicated.perl # implicit pipe to less via git's pager

コードがsource-highlight構文に定義ファイルがないことがわかっている場合は、同様の言語を設定して推測を無視できます。 cは一般的に良い選択肢です。

SRCLANG=c git show master:app/views/layouts/application.html.erb

以下はその要旨の原本です。https://gist.github.com/jaygooby/9494858d3d481a64819d227a9318f6c7

#! /bin/bash
#
# Based on http://git.savannah.gnu.org/cgit/src-highlite.git/tree/src/src-hilite-lesspipe.sh.in
# by Lorenzo Bettini
#
# Modified by Jay Caines-Gooby to support piped files
# [email protected]
# @jaygooby
#
# Typically called by setting:
#
# export LESSOPEN="|-/path/to/src-hilite-lesspipe.sh %s"
# export LESS=-R
#
# If we're less-ing a file, %s will be replaced by the name of the file. If
# there's no file and we're reading from a pipe, then %s is set to -
#
# This script differs from the original src-hilite-lesspipe.sh
# in that it can handle pipes and files with no extensions and will
# attempt to guess their language using the file command.
#
# So as well as invoking on regular files:
#
# less some.rb
# less some.py
#
# It will should be able to work on:
#
# less no-extension-but-contains-perl
#
# and even with more complex examples (my original motivation
# https://unix.stackexchange.com/questions/469982/how-can-i-use-source-highlight-with-git-show)
#
# git show master:some.rb
#
# It uses bashisms to do this, so is no longer a pure POSIX sh script.
set -eu

# Users can override the guessed language by setting SRCLANG:
# SRCLANG=c git show master:app/views/layouts/application.html.erb
SRCLANG=${SRCLANG:-}

guess_language() {
  lang=$(echo -e ${1:-} | file - | cut -d" " -f2)
  echo $(tr [A-Z] [a-z] <<< "$lang")
}

# check if the language passed as $1 is known to source-highlight
# In an earlier version of this script I set a fallback (c.lang)
# but this causes issues with paging man pages etc
check_language_is_known() {
  fallback=""
  lang=$(source-highlight --lang-list | cut -d' ' -f1 | grep "${1:-}" || true)
  lang=${lang:-$fallback}
  echo $lang
}

for source in "$@"; do
  case $source in
    *ChangeLog|*changelog)
      source-highlight --failsafe -f esc --lang-def=changelog.lang --style-file=esc.style -i "$source" ;;
    *Makefile|*makefile)
      source-highlight --failsafe -f esc --lang-def=makefile.lang --style-file=esc.style -i "$source" ;;
    *.tar|*.tgz|*.gz|*.bz2|*.xz)
      lesspipe "$source" ;;
    *)

      # naive check for a file extension; let source-highlight infer language
      # but only when source isn't - (ie. from a piped file)
      if [[ "$source" != "-" && $(basename "$source") =~ \. ]]; then
        source-highlight --failsafe --infer-lang -f esc --style-file=esc.style -i "$source"
      else
        # We're being piped to, or the filename doesn't have an extension
        # so guess the language.

        # When we're being piped to, we cat stdin, but when it's a file
        # without an extension, we cat the file instead.

        # unset IFS so line breaks are preserved and capture the file's contents
        # (will only work for files up to bash's available memory). There should
        # be a better way to replicate this with tee or process substitution...
        IFS= file=$([ "source" = "-" ] && cat || cat "$source")
        lang=$(guess_language $file)
        lang=$(check_language_is_known $lang)

        # Don't call if source-highlight doesn't know the language
        # BUT also let users override the guessed lang if the environment
        # variable SRCLANG is set. This can help where you know e.g. your
        # source code is c-like, but source-highlight has no specific syntax
        # definition for your code
        [ -n "$SRCLANG" ] && lang="$SRCLANG"

        if [ -n "$lang" ]; then
          echo $file | source-highlight --failsafe -f esc --src-lang=$lang --style-file=esc.style
        else
          echo $file
        fi
      fi

      ;;
  esac
done

おすすめ記事