forked from kurttheviking/git-rev-sync-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·193 lines (147 loc) · 4.64 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
var childProcess = require('child_process');
var escapeStringRegexp = require('escape-string-regexp');
var fs = require('graceful-fs');
var path = require('path');
var shell = require('shelljs');
var HAS_NATIVE_EXECSYNC = childProcess.hasOwnProperty('spawnSync');
var PATH_SEP = path.sep;
var RE_BRANCH = /^ref: refs\/heads\/(.*)\n/;
function _command(cmd, args) {
var result;
if (HAS_NATIVE_EXECSYNC) {
result = childProcess.spawnSync(cmd, args);
if (result.status !== 0) {
throw new Error('[git-rev-sync] failed to execute command: ' + result.stderr);
}
return result.stdout.toString('utf8').replace(/^\s+|\s+$/g, '');
}
result = shell.exec(cmd + ' ' + args.join(' '), {silent: true});
if (result.code !== 0) {
throw new Error('[git-rev-sync] failed to execute command: ' + result.stdout);
}
return result.stdout.toString('utf8').replace(/^\s+|\s+$/g, '');
}
function _getGitDirectory(start) {
if (start === undefined || start === null) {
start = module.parent.filename;
}
if (typeof start === 'string') {
start = start.split(PATH_SEP);
}
var testPath = start.join(PATH_SEP);
if (!testPath.length) {
throw new Error('[git-rev-sync] no git repository found');
}
testPath = path.resolve(testPath, '.git');
if (fs.existsSync(testPath)) {
if (!fs.statSync(testPath).isDirectory()) {
var parentRepoPath = fs.readFileSync(testPath, 'utf8').trim().split(' ').pop();
if (fs.existsSync(parentRepoPath)) {
return path.resolve(parentRepoPath);
}
var submoduleName = parentRepoPath.split('/').pop();
var submodulePath = '../.git/modules/' + submoduleName;
if (fs.existsSync(submodulePath)) {
return path.resolve(submodulePath);
}
throw new Error('[git-rev-sync] could not find repository from path' + parentRepoPath);
}
return testPath;
}
start.pop();
return _getGitDirectory(start);
}
function branch(dir) {
var gitDir = _getGitDirectory(dir);
var head = fs.readFileSync(path.resolve(gitDir, 'HEAD'), 'utf8');
var b = head.match(RE_BRANCH);
if (b) {
return b[1];
}
return 'Detached: ' + head.trim();
}
function long(dir) {
var b = branch(dir);
if (/Detached: /.test(b)) {
return b.substr(10);
}
var gitDir = _getGitDirectory(dir);
var refsFilePath = path.resolve(gitDir, 'refs', 'heads', b);
var ref;
if (fs.existsSync(refsFilePath)) {
ref = fs.readFileSync(refsFilePath, 'utf8');
} else {
// If there isn't an entry in /refs/heads for this branch, it may be that
// the ref is stored in the packfile (.git/packed-refs). Fall back to
// looking up the hash here.
var refToFind = ['refs', 'heads', b].join('/');
var packfileContents = fs.readFileSync(path.resolve(gitDir, 'packed-refs'), 'utf8');
var packfileRegex = new RegExp('(.*) ' + escapeStringRegexp(refToFind));
ref = packfileRegex.exec(packfileContents)[1];
}
return ref.trim();
}
function short(dir, len) {
return long(dir).substr(0, len || 7);
}
function message() {
return _command('git', ['log', '-1', '--pretty=%B']);
}
function tag(markDirty) {
if (markDirty) {
return _command('git', ['describe', '--always', '--tag', '--dirty', '--abbrev=0']);
}
return _command('git', ['describe', '--always', '--tag', '--abbrev=0']);
}
function tagFirstParent(markDirty) {
if (markDirty) {
return _command('git', ['describe', '--always', '--tag', '--dirty', '--abbrev=0', '--first-parent']);
}
return _command('git', ['describe', '--always', '--tag', '--abbrev=0', '--first-parent']);
}
function hasUnstagedChanges() {
var writeTree = _command('git', ['write-tree']);
return _command('git', ['diff-index', writeTree, '--']).length > 0;
}
function isDirty() {
return _command('git', ['diff-index', 'HEAD', '--']).length > 0;
}
function isTagDirty() {
try {
_command('git', ['describe', '--exact-match', '--tags']);
} catch (e) {
if (e.message.indexOf('no tag exactly matches')) {
return true;
}
throw e;
}
return false;
}
function remoteUrl() {
return _command('git', ['ls-remote', '--get-url']);
}
function date() {
return new Date(_command('git', ['log', '--no-color', '-n', '1', '--pretty=format:"%ad"']));
}
function count() {
return parseInt(_command('git', ['rev-list', '--all', '--count']), 10);
}
function log() {
throw new Error('not implemented');
}
module.exports = {
branch : branch,
count: count,
date: date,
hasUnstagedChanges: hasUnstagedChanges,
isDirty: isDirty,
isTagDirty: isTagDirty,
log: log,
long: long,
message: message,
remoteUrl: remoteUrl,
short: short,
tag: tag,
tagFirstParent: tagFirstParent
};