From 99b81267b3d3c008ce351e07d3d81933a817cbdc Mon Sep 17 00:00:00 2001
From: Vincent Bernat <bernat@luffy.cx>
Date: Sat, 7 May 2011 19:09:02 +0200
Subject: [PATCH 1/2] 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.
---
 hyde/ext/plugins/git.py | 54 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 hyde/ext/plugins/git.py

diff --git a/hyde/ext/plugins/git.py b/hyde/ext/plugins/git.py
new file mode 100644
index 0000000..3c33869
--- /dev/null
+++ b/hyde/ext/plugins/git.py
@@ -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

From 4a43492b5924531388552fee39c5d4a65b7ab42a Mon Sep 17 00:00:00 2001
From: Vincent Bernat <bernat@luffy.cx>
Date: Sun, 8 May 2011 14:25:10 +0200
Subject: [PATCH 2/2] Fix a typo

---
 hyde/ext/plugins/git.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hyde/ext/plugins/git.py b/hyde/ext/plugins/git.py
index 3c33869..996a865 100644
--- a/hyde/ext/plugins/git.py
+++ b/hyde/ext/plugins/git.py
@@ -51,4 +51,4 @@ class GitDatesPlugin(Plugin):
                     resource.meta.created = created
                 if modified == "git":
                     modified = parse(commits[0].strip())
-                    resource.meta.modified = created
+                    resource.meta.modified = modified