From 12182f1537c09a7fa43c951d201618f20e954624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionic=C4=83=20Biz=C4=83u?= Date: Mon, 13 Jul 2015 08:26:51 +0300 Subject: [PATCH] Fixed #38. Implemented the new schema. Now we only store the date days and the commit hashes. The remote url is not used anymore. Fixes #29. After this change, the data file is simplified a lot. --- lib/index.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8eac776..bcfeb85 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,12 +14,14 @@ var Ul = require("ul") , Typpy = require("typpy") , Exec = ChildProcess.exec , Spawn = ChildProcess.spawn - , LowDb = require("lowdb") ; // Constants const DATE_FORMAT = "MMM D, YYYY" , DEFAULT_STORE = Abs("~/.git-stats") + , DEFAULT_DATA = { + commits: {} + } ; function GitStats(dataPath) { @@ -65,20 +67,18 @@ GitStats.prototype.record = function (data, callback) { } if (typeof data.url !== "string" || !data.url) { - callback(new Error("Invalid url field. This commit is not recorded into the git-stats history since you haven't added the remote url. You can import the previous commits using the git-stats-importer tool.")); - return GitStats; + delete data.url; } // Get stats self.get(function (err, stats) { - stats = stats || {}; - var day = data.date.format(DATE_FORMAT) - , today = stats[day] = Object(stats[day]) - , repo = today[data.url] = Object(today[data.url]) + var commits = stats.commits + , day = data.date.format(DATE_FORMAT) + , today = commits[day] = Object(commits[day]) ; - repo[data.hash] = { date: data.date }; + today[data.hash] = 1; self.save(stats, callback); }); @@ -100,8 +100,8 @@ GitStats.prototype.get = function (callback) { ReadJson(self.path, function (err, data) { if (err && err.code === "ENOENT") { - return self.save({}, function (err) { - callback(err, {}); + return self.save(DEFAULT_DATA, function (err) { + callback(err, DEFAULT_DATA); }); } @@ -198,14 +198,11 @@ GitStats.prototype.graph = function (data, callback) { // Iterate days self.iterateDays(data, function (cDay) { + cDayObj = Object(stats.commits[cDay]); cDayObj = year[cDay] = { - _: stats[cDay] || {} - , c: 0 + _: cDayObj + , c: Object.keys(cDayObj).length }; - - Object.keys(cDayObj._).forEach(function (c) { - cDayObj.c += Object.keys(cDayObj._[c]).length; - }); }); callback(null, year);