LoginSignup
0
2

More than 5 years have passed since last update.

日本語でtexを書く時に使ってるpre-commit

Last updated at Posted at 2018-01-14

pre-commitとは

gitでcommit処理をする直前に呼び出す処理のことです

texでpre-commitを使う理由

よく論文作成などで、句読点の「。」を「.」に「、」を「,」にするように注意されます
しかし普段使いを考えると設定などで句読点を変更したくない

そんな時にせっかくだからtexの論文をgitで管理してるのだったら、git hooksをうまく使ってこの辺を解消しようと思いやってみました

やり方

以下のコードを、texのレポジトリ配下の.git/hooks/pre-commitに追加し、chmod +x .git/hooks/pre-commitとするだけです

pre-commit

#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

LIST=$(git diff-index --name-status $against -- | grep -E '^[AUM].*\.tex$'| cut -c3-)
echo $LIST
for file in $LIST
do
        sed -i -e "s/。/./g" $file
        sed -i -e "s/、/,/g" $file
        git add $file
done

コード内で行なっている処理は
コミット対象に.texファイルがある場合、sed -i -e "s/。/./g"sed -i -e "s/、/,/g"で句読点を書き換えているだけです

ちなみに4b825dc642cb6eb9a060e54bf8d69288fbee4904というハッシュ値はempty treeを表す特殊なハッシュ値だそうです

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2