Skip to content

Instantly share code, notes, and snippets.

@devlights
Last active October 9, 2017 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devlights/329219ea26edbd5037064671d258e6a4 to your computer and use it in GitHub Desktop.
Save devlights/329219ea26edbd5037064671d258e6a4 to your computer and use it in GitHub Desktop.
本スクリプトは、予め pull を実施後に実行されることを想定しています。 なので、リモート周りのコマンドは使っていません。コミット履歴もローカルコミット履歴をみます。 (つまり、ls-filesを使って、ls-remoteは利用しません。) 動作には、python-dateutilとgitpythonモジュールが必要です。
# coding: utf-8
"""
ファイルの更新日付をコミット日時に調整します。
注意)本スクリプトは、予め pull を実施後に実行されることを想定しています。
なので、リモート周りのコマンドは使っていません。コミット履歴もローカルコミット履歴をみます。
(つまり、ls-filesを使って、ls-remoteは利用しません。)
動作には、python-dateutilとgitpythonが必要です。
$ pip intall python-dateutil gitpython
"""
import datetime
import logging
import os
import pathlib
import dateutil.parser
import git
logging.basicConfig(
level=logging.INFO,
format=' %(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('log.txt', mode='w', encoding='utf-8')
])
try:
base_dir = pathlib.Path('/path/to/your/repo')
os.chdir(base_dir)
logging.info(f'ベースディレクトリ: {base_dir.absolute()}')
g = git.cmd.Git()
for f in g.ls_files().splitlines():
commit_time = g.log('-n1', f'{f}', pretty='format:%ci')
commit_time_dt = dateutil.parser.parse(commit_time)
commit_timestamp = commit_time_dt.timestamp()
cur_dt = datetime.datetime.fromtimestamp(os.stat(f).st_mtime)
logging.info(f'\tファイル  : {f}')
logging.info(f'\tコミット日時: {commit_time}')
logging.info(f'\t  日付更新: {str(cur_dt)} --> {str(commit_time_dt)}')
os.utime(f, (commit_timestamp, commit_timestamp))
except Exception as e:
logging.error(f'エラー発生:{e}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment