-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitTimeLapseNodeGit.js
171 lines (138 loc) · 3.67 KB
/
GitTimeLapseNodeGit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict";
var open = require("nodegit").Repository.open;
var timer = require("./Timer.js");
var exec = require('child_process').exec;
var fs = require('fs');
var Promise = require('es6-promise').Promise;
/*
*
* Object:
* [{ //commit
* time: ,
* files:[
* {
* name:
* fileContents:
* }]
* }]
*/
var Commit = function(){
this.time = 0;
this.sha = "";
this.msg = "";
this.files = [];
};
var File = function(){
this.name = "";
this.fileContents = "";
};
var getCommitListFromRepo = function(repoPath){
console.log("Get commit list")
return new Promise(function(resolve, reject){
var commitList = [];
console.log("In promise")
open(repoPath).then(function(repo) {
return repo.getMasterCommit();
})
.then(function(firstCommitOnMaster) {
// Create a new history event emitter.
var history = firstCommitOnMaster.history();
history.on("end",function(_commitObjs){
resolve(commitList);
});
history.on("error",function(error){
reject(error);
});
history.on("commit", function(commit) {
commitList.push(commit.sha());
});
// Start emitting events.
history.start();
});
});
};
var getCommitsFromRepo = function(repoPath){
return new Promise(function(resolve, reject){
var watch = timer.create("getCommits");
watch.start();
getFileListFromGitRepo(repoPath).then(function(files){
getFileCommits(repoPath, files).then(function(commits){
watch.stop();
console.log(watch.average(), "ms <- Generated commits for ", repoPath, commits.length);
resolve(commits);
}, function(error){reject(error); });
}, function(error){reject(error); });
});
};
// ls-files is not available through nodegit, so it must be fetched manually
var getFileListFromGitRepo = function(dir){
return new Promise(function(resolve,reject){
exec("git ls-files",{cwd:dir},function(error,stdout,stderr){
if(error !== null || stderr !== ""){
reject(error+" \nstderr: "+stderr);
return;
}
var files = stdout.split("\n");
//Remove empty element
files = files.filter(function(el) {return el.length !== 0;});
resolve(files);
});
});
};
var getFileCommits = function(repoPath,files){
return new Promise(function(resolve,reject){
var commits = [];
var filesInRepo = files;
// Open the repository directory.
open(repoPath)
// Open the master branch.
.then(function(repo) {
return repo.getMasterCommit();
})
// Display information about commits on master.
.then(function(firstCommitOnMaster) {
// Create a new history event emitter.
var history = firstCommitOnMaster.history();
history.on("end",function(_commitObjs){
resolve(commits);
});
history.on("error",function(error){
reject(error);
});
history.on("commit", function(commit) {
generateCommitObject(commit,filesInRepo).then(function(_commit){
commits.push(_commit);
},function(error){
console.log("Could not parse commit: "+error);
});
});
// Start emitting events.
history.start();
});
});
};
var generateCommitObject = function(_commit, filesInRepo){
return new Promise(function(resolve, reject){
var commit = new Commit();
commit.time = _commit.date();
commit.sha = _commit.sha();
commit.msg = _commit.message();
commit.files = [];
//Get file contents from commit
filesInRepo.map(function(filename){
_commit.getEntry(filename).then(function(entry){
entry.getBlob().then(function(blob){
var file = new File();
file.name = filename;
file.fileContents = String(blob);
commit.files.push(file);
resolve(commit);
});
});
});
});
};
module.exports = {
getCommitsFromRepo: getCommitsFromRepo,
getCommitListFromRepo: getCommitListFromRepo
};