Browse Source

Grab dates from git repository

Grab creation and modification date from git repository. To use this
plugin, `created` and `modified` meta data should be set to `git`.
They will be replaced by the proper date.
main
Vincent Bernat 13 years ago
parent
commit
99b81267b3
1 changed files with 54 additions and 0 deletions
  1. +54
    -0
      hyde/ext/plugins/git.py

+ 54
- 0
hyde/ext/plugins/git.py View File

@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""
Contains classes and utilities to extract information from git repository
"""

from hyde.plugin import Plugin

import subprocess
import traceback
from dateutil.parser import parse

class GitDatesPlugin(Plugin):
"""
Extract creation and last modification date from git and include
them in the meta data if they are set to "git". Creation date
is put in `created` and last modification date in `modified`.
"""

def __init__(self, site):
super(GitDatesPlugin, self).__init__(site)

def begin_site(self):
"""
Initialize plugin. Retrieve dates from git
"""
for node in self.site.content.walk():
for resource in node.resources:
created = None
modified = None
try:
created = resource.meta.created
modified = resource.meta.modified
except AttributeError:
pass
# Everything is already overrided
if created != "git" and modified != "git":
continue
# Run git log --pretty=%ai
try:
commits = subprocess.check_output(["git", "log", "--pretty=%ai",
resource.path]).split("\n")
except subprocess.CalledProcessError:
self.logger.warning("Unable to get git history for [%s]" % resource)
continue
commits = commits[:-1]
if not commits:
self.logger.warning("No git history for [%s]" % resource)
continue
if created == "git":
created = parse(commits[-1].strip())
resource.meta.created = created
if modified == "git":
modified = parse(commits[0].strip())
resource.meta.modified = created

Loading…
Cancel
Save