forked from lerna/lerna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe-ref.js
71 lines (54 loc) · 1.94 KB
/
describe-ref.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
"use strict";
const log = require("npmlog");
const childProcess = require("@lerna/child-process");
module.exports = describeRef;
module.exports.parse = parse;
module.exports.sync = sync;
function getArgs(options) {
const args = [
"describe",
// explicitly get tags
"--tags",
// fallback to short sha if no tags located
"--always",
// always return full result, helps identify existing release
"--long",
// annotate if uncommitted changes present
"--dirty",
// prefer tags originating on upstream branch
"--first-parent",
];
if (options.match) {
args.push("--match", options.match);
}
return args;
}
function describeRef(options = {}) {
const promise = childProcess.exec("git", getArgs(options), options);
return promise.then(({ stdout }) => {
const result = parse(stdout, options);
log.verbose("git-describe", "%j => %j", options && options.match, stdout);
log.silly("git-describe", "parsed => %j", result);
return result;
});
}
function sync(options = {}) {
const stdout = childProcess.execSync("git", getArgs(options), options);
const result = parse(stdout, options);
// only called by collect-updates with no matcher
log.silly("git-describe.sync", "%j => %j", stdout, result);
return result;
}
function parse(stdout, options = {}) {
// when git describe fails to locate tags, it returns only the minimal sha
if (/^[0-9a-f]{7,40}/.test(stdout)) {
// repo might still be dirty
const [, sha, isDirty] = /^([0-9a-f]{7,40})(-dirty)?/.exec(stdout);
// count number of commits since beginning of time
const refCount = childProcess.execSync("git", ["rev-list", "--count", sha], options);
return { refCount, sha, isDirty: Boolean(isDirty) };
}
const [, lastTagName, lastVersion, refCount, sha, isDirty] =
/^((?:.*@)?(.*))-(\d+)-g([0-9a-f]+)(-dirty)?$/.exec(stdout) || [];
return { lastTagName, lastVersion, refCount, sha, isDirty: Boolean(isDirty) };
}