|
@@ -1,18 +1,11 @@
|
|
-#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
import os
|
|
import os
|
|
-from git import Git, Repo, InvalidGitRepositoryError
|
|
|
|
from pelican import signals, contents
|
|
from pelican import signals, contents
|
|
-from datetime import datetime
|
|
|
|
-from time import mktime, altzone
|
|
|
|
from pelican.utils import strftime, set_date_tzinfo
|
|
from pelican.utils import strftime, set_date_tzinfo
|
|
|
|
+from datetime import datetime
|
|
|
|
+from git_wrapper import git_wrapper
|
|
|
|
|
|
-try:
|
|
|
|
- repo = Repo(os.path.abspath('.'))
|
|
|
|
- git = Git(os.path.abspath('.'))
|
|
|
|
-except InvalidGitRepositoryError as e:
|
|
|
|
- repo = None
|
|
|
|
|
|
|
|
def datetime_from_timestamp(timestamp, content):
|
|
def datetime_from_timestamp(timestamp, content):
|
|
"""
|
|
"""
|
|
@@ -20,15 +13,23 @@ def datetime_from_timestamp(timestamp, content):
|
|
so that datetime is comparable to other datetime objects in recent versions
|
|
so that datetime is comparable to other datetime objects in recent versions
|
|
that now also have timezone information.
|
|
that now also have timezone information.
|
|
"""
|
|
"""
|
|
- return set_date_tzinfo(datetime.fromtimestamp(timestamp), tz_name=content.settings.get('TIMEZONE', None))
|
|
|
|
|
|
+ return set_date_tzinfo(
|
|
|
|
+ datetime.fromtimestamp(timestamp),
|
|
|
|
+ tz_name=content.settings.get('TIMEZONE', None))
|
|
|
|
+
|
|
|
|
|
|
def filetime_from_git(content):
|
|
def filetime_from_git(content):
|
|
- if isinstance(content, contents.Static) or repo is None:
|
|
|
|
|
|
+ if isinstance(content, contents.Static):
|
|
return
|
|
return
|
|
|
|
+
|
|
|
|
+ git = git_wrapper('.')
|
|
|
|
+ tz_name = content.settings.get('TIMEZONE', None)
|
|
|
|
+
|
|
gittime = content.metadata.get('gittime', 'yes').lower()
|
|
gittime = content.metadata.get('gittime', 'yes').lower()
|
|
gittime = gittime.replace("false", "no").replace("off", "no")
|
|
gittime = gittime.replace("false", "no").replace("off", "no")
|
|
if gittime == "no":
|
|
if gittime == "no":
|
|
return
|
|
return
|
|
|
|
+
|
|
# 1. file is not managed by git
|
|
# 1. file is not managed by git
|
|
# date: fs time
|
|
# date: fs time
|
|
# 2. file is staged, but has no commits
|
|
# 2. file is staged, but has no commits
|
|
@@ -38,36 +39,42 @@ def filetime_from_git(content):
|
|
# 4. file is managed, but dirty
|
|
# 4. file is managed, but dirty
|
|
# date: first commit time, update: fs time
|
|
# date: first commit time, update: fs time
|
|
path = content.source_path
|
|
path = content.source_path
|
|
- status, stdout, stderr = git.execute(['git', 'ls-files', path, '--error-unmatch'],
|
|
|
|
- with_extended_output=True, with_exceptions=False)
|
|
|
|
- if status != 0:
|
|
|
|
- # file is not managed by git
|
|
|
|
- content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)
|
|
|
|
- else:
|
|
|
|
- # file is managed by git
|
|
|
|
- commits = repo.commits(path=path)
|
|
|
|
|
|
+ if git.is_file_managed_by_git(path):
|
|
|
|
+ commits = git.get_commits(
|
|
|
|
+ path, follow=content.settings.get('GIT_FILETIME_FOLLOW', False))
|
|
|
|
+
|
|
if len(commits) == 0:
|
|
if len(commits) == 0:
|
|
# never commited, but staged
|
|
# never commited, but staged
|
|
- content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)
|
|
|
|
|
|
+ content.date = git.datetime_from_timestamp(
|
|
|
|
+ os.stat(path).st_ctime, content)
|
|
else:
|
|
else:
|
|
# has commited
|
|
# has commited
|
|
- content.date = datetime_from_timestamp(mktime(commits[-1].committed_date) - altzone, content)
|
|
|
|
|
|
+ content.date = git.get_commit_date(
|
|
|
|
+ commits[-1], tz_name)
|
|
|
|
|
|
- status, stdout, stderr = git.execute(['git', 'diff', '--quiet', 'HEAD', path],
|
|
|
|
- with_extended_output=True, with_exceptions=False)
|
|
|
|
- if status != 0:
|
|
|
|
|
|
+ if git.is_file_modified(path):
|
|
# file has changed
|
|
# file has changed
|
|
- content.modified = datetime_from_timestamp(os.stat(path).st_ctime, content)
|
|
|
|
|
|
+ content.modified = datetime_from_timestamp(
|
|
|
|
+ os.stat(path).st_ctime, content)
|
|
else:
|
|
else:
|
|
# file is not changed
|
|
# file is not changed
|
|
if len(commits) > 1:
|
|
if len(commits) > 1:
|
|
- content.modified = datetime_from_timestamp(mktime(commits[0].committed_date) - altzone, content)
|
|
|
|
|
|
+ content.modified = git.get_commit_date(
|
|
|
|
+ commits[0], tz_name)
|
|
|
|
+ else:
|
|
|
|
+ # file is not managed by git
|
|
|
|
+ content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)
|
|
|
|
+
|
|
if not hasattr(content, 'modified'):
|
|
if not hasattr(content, 'modified'):
|
|
content.modified = content.date
|
|
content.modified = content.date
|
|
|
|
+
|
|
if hasattr(content, 'date'):
|
|
if hasattr(content, 'date'):
|
|
content.locale_date = strftime(content.date, content.date_format)
|
|
content.locale_date = strftime(content.date, content.date_format)
|
|
|
|
+
|
|
if hasattr(content, 'modified'):
|
|
if hasattr(content, 'modified'):
|
|
- content.locale_modified = strftime(content.modified, content.date_format)
|
|
|
|
|
|
+ content.locale_modified = strftime(
|
|
|
|
+ content.modified, content.date_format)
|
|
|
|
+
|
|
|
|
|
|
def register():
|
|
def register():
|
|
signals.content_object_init.connect(filetime_from_git)
|
|
signals.content_object_init.connect(filetime_from_git)
|