@atomist/automation-client
Version:
Atomist API for software low-level client
90 lines • 4.32 kB
JavaScript
;
/*
* Copyright © 2018 Atomist, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("../../util/child_process");
function isFullyClean(gs) {
return gs.isClean && gs.ignoredChanges.length === 0;
}
exports.isFullyClean = isFullyClean;
function runStatusIn(baseDir) {
return __awaiter(this, void 0, void 0, function* () {
const branch = yield determineBranch(baseDir);
const upstreamData = yield collectUpstream(baseDir, branch);
const shaData = yield collectFullSha(baseDir);
const cleanlinessData = yield collectCleanliness(baseDir);
const ignoredChangeData = yield collectIgnoredChanges(baseDir);
return Object.assign(Object.assign(Object.assign(Object.assign({ branch }, ignoredChangeData), cleanlinessData), shaData), upstreamData);
});
}
exports.runStatusIn = runStatusIn;
function determineBranch(baseDir) {
return __awaiter(this, void 0, void 0, function* () {
const branchNameResult = yield child_process_1.execPromise("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd: baseDir });
return branchNameResult.stdout.trim();
});
}
function collectCleanliness(baseDir) {
return __awaiter(this, void 0, void 0, function* () {
const porcelainStatusResult = yield child_process_1.execPromise("git", ["status", "--porcelain"], { cwd: baseDir });
const raw = porcelainStatusResult.stdout;
return { isClean: (raw.length) === 0 };
});
}
function collectIgnoredChanges(baseDir) {
return __awaiter(this, void 0, void 0, function* () {
const porcelainStatusResult = yield child_process_1.execPromise("git", ["status", "--porcelain", "--ignored"], { cwd: baseDir });
const raw = porcelainStatusResult.stdout;
const ignored = raw.trim()
.split("\n")
.filter(s => s.startsWith("!"))
.map(s => s.substring(3));
return {
raw,
ignoredChanges: ignored,
};
});
}
function collectFullSha(baseDir, commit = "HEAD") {
return __awaiter(this, void 0, void 0, function* () {
const result = yield child_process_1.execPromise("git", ["rev-list", "-1", commit, "--"], { cwd: baseDir });
return {
sha: result.stdout.trim(),
};
});
}
function collectUpstream(baseDir, branch) {
return __awaiter(this, void 0, void 0, function* () {
const branchArgs = ["for-each-ref", "--format", "%(upstream:short) %(upstream:trackshort)", `refs/heads/${branch}`];
const branchResult = yield child_process_1.execPromise("git", branchArgs, { cwd: baseDir });
const branchResultParts = branchResult.stdout.trim().split(" ");
const upstream = branchResultParts.length > 0 ?
{ branch: branchResultParts[0], inSync: branchResultParts[1] === "=" }
: undefined;
return { upstream };
});
}
//# sourceMappingURL=gitStatus.js.map