mirror of
https://github.com/IonicaBizau/git-stats.git
synced 2024-12-22 05:12:11 +01:00
Adding new functions to control the commit data file
This commit is contained in:
parent
66d51ae7b0
commit
fa2509dd81
1 changed files with 73 additions and 0 deletions
73
lib/index.js
73
lib/index.js
|
@ -161,6 +161,8 @@ GitStats.prototype.record = function (data, callback) {
|
|||
if (!/^moment|date$/.test(Typpy(data.date))) {
|
||||
callback(new Error("The date field should be a string or a date object."));
|
||||
return GitStats;
|
||||
} else if (Typpy(data.date, Date)) {
|
||||
data.date = Moment(data.date);
|
||||
}
|
||||
|
||||
if (typeof data.hash !== "string" || !data.hash) {
|
||||
|
@ -203,6 +205,77 @@ GitStats.prototype.record = function (data, callback) {
|
|||
return self;
|
||||
};
|
||||
|
||||
/**
|
||||
* removeCommit
|
||||
* Deletes a specifc commit from the history.
|
||||
*
|
||||
* @name record
|
||||
* @function
|
||||
* @param {Object} data The commit data containing:
|
||||
*
|
||||
* - `date` (String|Date): The date object or a string in a format that can be parsed.
|
||||
* - `hash` (String): The commit hash.
|
||||
* - `_data` (Object): If this field is provided, it should be the content of the git-stats data file as object. It will be modified in-memory and then returned.
|
||||
* - `save` (Boolean): If `false`, the result will *not* be saved in the file.
|
||||
*
|
||||
* @param {Function} callback The callback function.
|
||||
* @return {GitStats} The `GitStats` instance.
|
||||
*/
|
||||
GitStats.prototype.removeCommit = function (data, callback) {
|
||||
|
||||
var self = this;
|
||||
|
||||
// Validate data
|
||||
callback = callback || function (err) { if (err) throw err; };
|
||||
data = Object(data);
|
||||
|
||||
if (typeof data.date === "string") {
|
||||
data.date = new Moment(new Date(data.date));
|
||||
}
|
||||
|
||||
if (!/^moment|date$/.test(Typpy(data.date))) {
|
||||
callback(new Error("The date field should be a string or a date object."));
|
||||
return GitStats;
|
||||
} else if (Typpy(data.date, Date)) {
|
||||
data.date = Moment(data.date);
|
||||
}
|
||||
|
||||
if (typeof data.hash !== "string" || !data.hash) {
|
||||
callback(new Error("Invalid hash."));
|
||||
return GitStats;
|
||||
}
|
||||
|
||||
function modify (err, stats) {
|
||||
|
||||
if (err) { return callback(err); }
|
||||
|
||||
var commits = stats.commits
|
||||
, day = data.date.format(DATE_FORMAT)
|
||||
, today = commits[day] = Object(commits[day])
|
||||
;
|
||||
|
||||
delete today[data.hash];
|
||||
|
||||
if (data.save === false) {
|
||||
callback(null, stats);
|
||||
} else {
|
||||
self.save(stats, callback);
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
// Check if we have input data
|
||||
if (data._data) {
|
||||
return modify(null, data._data);
|
||||
} else {
|
||||
// Get stats
|
||||
self.get(modify);
|
||||
}
|
||||
|
||||
return self;
|
||||
};
|
||||
|
||||
/**
|
||||
* get
|
||||
* Gets the git stats.
|
||||
|
|
Loading…
Reference in a new issue