1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(Git)行末にスペースを追加・削除したコミットを探す

Posted at

秀丸のオプションとかで勝手に消された行末スペースをGitリポジトリから探す方法。

結論

以下のonelinerで表形式の結果が取得できる。

git log -S" $" --pickaxe-regex --name-only --pretty="__NEWLINE__%h%x09%an%x09%ae%x09%s" | tr "\n" "\t" | sed 's/__NEWLINE__/\n/g'

注意:Mac OSのsedではうまく動きません。回避策は後述。

解説

step 1: git-logでログ検索

git log -S" $" --pickaxe-regex --name-only --pretty="__NEWLINE__%h%x09%an%x09%ae%x09%s"
  • -S--pickaxe-regex: 文末スペースが追加・削除されたコミットを探しています。
  • --name-only: 該当ファイルのパスを出力します。
  • --pretty: 出力形式を指定します。後述。

出力形式の補足

__NEWLINE__: 後で改行するための印。
%x09: タブ文字

step 2: ひとまず1行にまとめる

tr "\n" "\t"

step 3: 印の付いたところで改行

sed 's/__NEWLINE__/\n/g'

注意:Mac OSのsedでは改行文字がうまく動きません。homebrew等でGNU sed(gsed)を入れて使うか、\の後に実際に改行を入力してください。

事例

githubのgithubtraining/hellogitworldで試してみる。

git clone https://github.com/githubtraining/hellogitworld
git log -S" $" --pickaxe-regex --name-only --pretty="__NEWLINE__%h%x09%an%x09%ae%x09%s" | tr "\n" "\t" | sed 's/__NEWLINE__/\n/g'

ebbbf77	Jordan McCullough	jordan@github.com	Update package name, directory		src/test/java/com/ambientideas/AppTest.java	src/test/java/com/github/AppTest.java	
45a30ea	Jordan McCullough	jordan@github.com	Update package name, directory		src/main/java/com/ambientideas/App.java	src/main/java/com/gith![kobito.1446274215.865095.png](https://qiita-image-store.s3.amazonaws.com/0/98701/9de8bef4-7ab8-c7f6-244a-20efd679ed9f.png "kobito.1446274215.865095.png")
ub/App.java
d2280d0	Matthew McCullough	matthew@github.com	Adding maven build script		src/main/java/com/ambientideas/App.java	src/test/java/com/ambientideas/AppTest.java	

TSVファイルとかに吐いてスプレッドシートとして開けば、こんな感じで見られる。
kobito.1446274319.454548.png

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?