Skip to content

Commit

Permalink
fix(describe-ref): Fallback refCount is the number of commits since b…
Browse files Browse the repository at this point in the history
…eginning of repository
  • Loading branch information
evocateur committed Sep 4, 2018
1 parent a289ac4 commit 6dfea52
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
3 changes: 2 additions & 1 deletion commands/publish/__tests__/publish-canary.test.js
Expand Up @@ -163,9 +163,10 @@ test("publish --canary addresses unpublished package", async () => {
);
await lernaPublish(cwd)("--canary", "premajor");

// there have been two commits since the beginning of the repo
expect(writePkg.updatedVersions()).toMatchInlineSnapshot(`
Object {
"package-6": 1.0.0-alpha.0+SHA,
"package-6": 1.0.0-alpha.1+SHA,
}
`);
});
Expand Down
2 changes: 1 addition & 1 deletion commands/publish/index.js
Expand Up @@ -230,7 +230,7 @@ class PublishCommand extends Command {
match: `${pkg.name}@*`,
cwd: this.execOpts.cwd,
})
.then(({ lastVersion = pkg.version, refCount = 1, sha }) =>
.then(({ lastVersion = pkg.version, refCount, sha }) =>
// an unpublished package will have no reachable git tag
makeVersion({ lastVersion, refCount, sha })
)
Expand Down
25 changes: 22 additions & 3 deletions utils/describe-ref/__tests__/describe-ref.test.js
Expand Up @@ -14,7 +14,7 @@ describe("describeRef()", () => {
it("resolves parsed metadata", async () => {
const result = await describeRef();

expect(childProcess.exec).lastCalledWith("git", DEFAULT_ARGS, undefined);
expect(childProcess.exec).lastCalledWith("git", DEFAULT_ARGS, {});
expect(result).toEqual({
isDirty: false,
lastTagName: "v1.2.3",
Expand Down Expand Up @@ -43,7 +43,7 @@ describe("describeRef.sync()", () => {
it("returns parsed metadata", () => {
const result = describeRef.sync();

expect(childProcess.execSync).lastCalledWith("git", DEFAULT_ARGS, undefined);
expect(childProcess.execSync).lastCalledWith("git", DEFAULT_ARGS, {});
expect(result).toEqual({
isDirty: false,
lastTagName: "v1.2.3",
Expand Down Expand Up @@ -102,9 +102,28 @@ describe("describeRef.parse()", () => {
});

it("detects fallback and returns partial metadata", () => {
const result = describeRef.parse("a1b2c3d");
childProcess.execSync.mockReturnValueOnce("123");

const options = { cwd: "bar" };
const result = describeRef.parse("a1b2c3d", options);

expect(childProcess.execSync).lastCalledWith("git", ["rev-list", "--count", "a1b2c3d"], options);
expect(result).toEqual({
isDirty: false,
refCount: "123",
sha: "a1b2c3d",
});
});

it("detects dirty fallback and returns partial metadata", () => {
childProcess.execSync.mockReturnValueOnce("456");

const result = describeRef.parse("a1b2c3d-dirty");

expect(childProcess.execSync).lastCalledWith("git", ["rev-list", "--count", "a1b2c3d"], {});
expect(result).toEqual({
isDirty: true,
refCount: "456",
sha: "a1b2c3d",
});
});
Expand Down
24 changes: 15 additions & 9 deletions utils/describe-ref/lib/describe-ref.js
Expand Up @@ -7,7 +7,7 @@ module.exports = describeRef;
module.exports.parse = parse;
module.exports.sync = sync;

function getArgs(options = {}) {
function getArgs(options) {
const args = [
"describe",
// fallback to short sha if no tags located
Expand All @@ -27,11 +27,11 @@ function getArgs(options = {}) {
return args;
}

function describeRef(options) {
function describeRef(options = {}) {
const promise = childProcess.exec("git", getArgs(options), options);

return promise.then(({ stdout }) => {
const result = parse(stdout);
const result = parse(stdout, options);

log.verbose("git-describe", "%j => %j", options && options.match, stdout);
log.silly("git-describe", "parsed => %j", result);
Expand All @@ -40,20 +40,26 @@ function describeRef(options) {
});
}

function sync(options) {
function sync(options = {}) {
const stdout = childProcess.execSync("git", getArgs(options), options);
const result = parse(stdout);
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) {
if (/^[0-9a-f]{7,40}$/.test(stdout)) {
// fallback received, can't provide full metadata
return { sha: stdout };
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] =
Expand Down

0 comments on commit 6dfea52

Please sign in to comment.