-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (78 loc) · 2.46 KB
/
app.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
var express = require('express');
var watch = require('node-watch');
var exec_path = require('path').dirname(require.main.filename);
var events = require('events');
var exec = require('child_process').exec;
var gitLog = {
log: "",
ncommits : 0,
commits : [],
parse: function(log) {
this.log = log.split("\n");
this.ncommits = this.log.length;
this.commits = [];
for (var i=0; i<this.ncommits; i++){
var commit = {
// Commit hash line -> {hash_commit}
hash : this.log[i].split(" ")[0],
// Author line -> Author: {name} <{email}>
authorInfo : { name: this.log[i].split(" ")[1], email: this.log[i].split(" ")[2] },
// Date line -> Date: {date}
date : this.log[i].split(" ")[3],
// Comment line -> {comment}
comment : this.log[i].split(" ")[4],
index : this.ncommits - i
}
this.commits.push( commit );
}
return this.commits.reverse();
},
toString: function() {
return log;
}
};
var currentBranch = {
get: function(input) {
var lines = input.split("\n");
for (var i=0; i< lines.length; i++){
if (lines[i].indexOf("*") !=-1){
return lines[i].split("* ")[1];
}
}
}
}
var Git = function () {};
Git.prototype = new events.EventEmitter;
var gitlog = new Git();
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
app.configure(function(){
app.use('/media', express.static(__dirname + '/media'));
app.use(express.static(__dirname + '/public'));
});
server.listen(3000);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
exec('git log --pretty=format:"%h %an %ae %ad %s"', function (error, gitLogOutput) {
// Send the full git log
socket.emit("full-git-log",gitLog.parse(gitLogOutput));
});
exec('git branch', function (error, output) {
// Send the full git log
socket.emit("current-branch",currentBranch.get(output));
});
gitlog.on('commit', function (commit) {
socket.emit('update-git-log', commit);
});
});
watch(exec_path +'/.git/refs/heads', function(filename) {
exec('git log --pretty=format:"%h %an %ae %ad %s"', function (error, gitLogOutput) {
var log = gitLog.parse(gitLogOutput);
// gitlog.emit('commit', log[0]);
gitlog.emit('commit', log);
});
});