From 664b1330476cb6bd3b71af6ea33ec2ae4dc52841 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Sko=C5=99epa?= <jakub@skorepa.info>
Date: Mon, 11 Apr 2016 00:04:03 +0200
Subject: [PATCH] Fix build without repository in content

Fixes CI builds
---
 sitegin/gitInfo.js | 77 ++++++++++++++++++++++++++--------------------
 1 file changed, 43 insertions(+), 34 deletions(-)

diff --git a/sitegin/gitInfo.js b/sitegin/gitInfo.js
index e27c4721..b3f085b9 100644
--- a/sitegin/gitInfo.js
+++ b/sitegin/gitInfo.js
@@ -1,3 +1,4 @@
+'use strict';
 /*
  * Add date: {creation, modification, modified} and author: {name, email}
  * to metadata if not already present
@@ -7,8 +8,6 @@ var nodegit = require('nodegit');
 var path = require('path');
 var moment = require('moment');
 
-var pathToRepo;
-
 // Returns promise for patch of single commit
 var gitShow = function(repo, commitData) {
   var commit, tree, parentTree;
@@ -83,11 +82,9 @@ var gitlog = function(repo) {
   });
 };
 
-var getFilesHistory = function() {
-  var repo;
-  return nodegit.Repository.open(pathToRepo)
-  .then(function(_repo) {
-    repo = _repo;
+var getFilesHistory = function(repo) {
+  return Promise.resolve()
+  .then(function() {
     return gitlog(repo)
   })
   .then(function(log){
@@ -117,38 +114,50 @@ var getFilesHistory = function() {
 
 module.exports = function(obj) {
   console.log('Build step: GitInfo');
-  pathToRepo = path.resolve(obj.config.sourceDir);
-  return getFilesHistory()
-  .then((filesHistory) => {
-    obj.pages.forEach(function(article) {
-      var file = path.relative(pathToRepo, article.filename);
-      var commits = filesHistory[file];
-      if(commits !== undefined) {
-        commits.sort(function(a,b){
-          return b.date - a.date;
-        });
-        var newestCommit = commits[0];
-        var oldestCommit = commits[commits.length-1];
+  let pathToRepo = path.resolve(obj.config.sourceDir);
+  return new Promise(function(resolve,reject) {
+    return nodegit.Repository.open(pathToRepo)
+    .catch(function() {
+      resolve(obj);
+    })
+    .then(getFilesHistory)
+    .then((filesHistory) => {
+      obj.pages.forEach(function(article) {
+        var file = path.relative(pathToRepo, article.filename);
+        var commits = filesHistory[file];
+        if(commits !== undefined) {
+          commits.sort(function(a,b){
+            return b.date - a.date;
+          });
+          var newestCommit = commits[0];
+          var oldestCommit = commits[commits.length-1];
 
-        if(article.metadata.date === undefined)
-          article.metadata.date = {};
+          if(article.metadata.date === undefined)
+            article.metadata.date = {};
 
-        if(article.metadata.date.creation === undefined)
-          article.metadata.date.creation = oldestCommit.date;
+          if(article.metadata.date.creation === undefined)
+            article.metadata.date.creation = oldestCommit.date;
 
-        article.metadata.date.modification = newestCommit.date;
-        article.metadata.date.modified = (newestCommit !== oldestCommit);
+          article.metadata.date.modification = newestCommit.date;
+          article.metadata.date.modified = (newestCommit !== oldestCommit);
 
-        if(article.metadata.author === undefined)
-          article.metadata.author = {}
-        if(article.metadata.author.name === undefined)
-          article.metadata.author.name = oldestCommit.author.name
-        if(article.metadata.author.email === undefined)
-          article.metadata.author.email = oldestCommit.author.email
+          if(article.metadata.author === undefined)
+            article.metadata.author = {}
+          if(article.metadata.author.name === undefined)
+            article.metadata.author.name = oldestCommit.author.name
+          if(article.metadata.author.email === undefined)
+            article.metadata.author.email = oldestCommit.author.email
 
-        article.commits = commits;
-      }
+          article.commits = commits;
+        }
+      });
+      return obj;
+    })
+    .then(function(o) {
+      resolve(o);
+    })
+    .catch(function(e){
+      reject(e);
     });
-    return obj;
   })
 }
-- 
GitLab