4
7

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でcommit時にファイルのタイムスタンプをcommiter dateに変更する

Posted at

背景

諸事情でファイルのタイムスタンプを厳密に管理する必要があるプロジェクトで
ファイルのタイムスタンプ = commiter date
としたい状況になった

最初は既にコミット済みのファイルについて、まとめてタイムスタンプを変更したり、
個別にシェルを実行してタイムスタンプを変更していたが、
タイムスタンプを変更するシェルを実行し忘れるのでhookを利用する
なお、Windows環境

今まで

  1. ファイル編集…A
  2. add & commit…B
  3. ファイルのタイムスタンプはA
  4. 個別に(まとめて)手動でファイルのタイムスタンプをB時点に変更するシェルを実行(←これ面倒で忘れる)

hook仕込み後

  1. ファイル編集…A
  2. add & commit…B
  3. ファイルのタイムスタンプはA
  4. post-commitでcommitしたファイルのタイムスタンプをB時点に都度変更

コード

post-commitに以下のコードを記載する

.git\hooks\post-commit
# !/bin/bash
# for post-commit
# ファイルのタイムスタンプをcommiter dateに変更する

for FILE in $(git diff --name-only HEAD HEAD^); do
	TIME=$(git log --pretty=format:%ci -n1 $FILE)
	STAMP=`date  +%y%m%d%H%M.%S --date "$TIME"`
	touch -t $STAMP $FILE
done

git使ってるくせに、ファイルのタイムスタンプなんか気にするな
ってとても思いますが諸事情、諸事情

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?