Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: set diff quiet and switch isdirty command order #627

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ function isEven(git, branch1, branch2) {
!(yield isBehind(git, branch1, branch2)));
});
}
function hasDiff(git, branch1, branch2) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield git.diff([`${branch1}..${branch2}`]);
return result.length > 0;
});
}
function splitLines(multilineString) {
return multilineString
.split('\n')
Expand Down Expand Up @@ -192,7 +186,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
// squash merged but not deleted. We need to reset to make sure it doesn't appear
// to have a diff with the base due to different commits for the same changes.
// For changes on base this reset is equivalent to a rebase of the pull request branch.
if ((yield hasDiff(git, branch, tempBranch)) ||
if ((yield git.hasDiff([`${branch}..${tempBranch}`])) ||
!(yield isAhead(git, base, tempBranch))) {
core.info(`Resetting '${branch}'`);
// Alternatively, git switch -C branch tempBranch
Expand Down Expand Up @@ -662,16 +656,6 @@ class GitCommandManager {
return output.exitCode === 0;
});
}
diff(options) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['-c', 'core.pager=cat', 'diff'];
if (options) {
args.push(...options);
}
const output = yield this.exec(args);
return output.stdout.trim();
});
}
fetch(refSpec, remoteName, options) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['-c', 'protocol.version=2', 'fetch'];
Expand Down Expand Up @@ -712,19 +696,28 @@ class GitCommandManager {
getWorkingDirectory() {
return this.workingDirectory;
}
hasDiff(options) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['diff', '--quiet'];
if (options) {
args.push(...options);
}
const output = yield this.exec(args, true);
return output.exitCode === 1;
});
}
isDirty(untracked) {
return __awaiter(this, void 0, void 0, function* () {
const diffArgs = ['--abbrev=40', '--full-index', '--raw'];
// Check staged changes
if (yield this.diff([...diffArgs, '--staged'])) {
// Check untracked changes
if (untracked && (yield this.status(['--porcelain', '-unormal']))) {
return true;
}
// Check working index changes
if (yield this.diff(diffArgs)) {
if (yield this.hasDiff()) {
return true;
}
// Check untracked changes
if (untracked && (yield this.status(['--porcelain', '-unormal']))) {
// Check staged changes
if (yield this.hasDiff(['--staged'])) {
return true;
}
return false;
Expand Down
11 changes: 1 addition & 10 deletions src/create-or-update-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@ async function isEven(
)
}

async function hasDiff(
git: GitCommandManager,
branch1: string,
branch2: string
): Promise<boolean> {
const result = await git.diff([`${branch1}..${branch2}`])
return result.length > 0
}

function splitLines(multilineString: string): string[] {
return multilineString
.split('\n')
Expand Down Expand Up @@ -205,7 +196,7 @@ export async function createOrUpdateBranch(
// to have a diff with the base due to different commits for the same changes.
// For changes on base this reset is equivalent to a rebase of the pull request branch.
if (
(await hasDiff(git, branch, tempBranch)) ||
(await git.hasDiff([`${branch}..${tempBranch}`])) ||
!(await isAhead(git, base, tempBranch))
) {
core.info(`Resetting '${branch}'`)
Expand Down
29 changes: 14 additions & 15 deletions src/git-command-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ export class GitCommandManager {
return output.exitCode === 0
}

async diff(options?: string[]): Promise<string> {
const args = ['-c', 'core.pager=cat', 'diff']
if (options) {
args.push(...options)
}
const output = await this.exec(args)
return output.stdout.trim()
}

async fetch(
refSpec: string[],
remoteName?: string,
Expand Down Expand Up @@ -153,18 +144,26 @@ export class GitCommandManager {
return this.workingDirectory
}

async hasDiff(options?: string[]): Promise<boolean> {
const args = ['diff', '--quiet']
if (options) {
args.push(...options)
}
const output = await this.exec(args, true)
return output.exitCode === 1
}

async isDirty(untracked: boolean): Promise<boolean> {
const diffArgs = ['--abbrev=40', '--full-index', '--raw']
// Check staged changes
if (await this.diff([...diffArgs, '--staged'])) {
// Check untracked changes
if (untracked && (await this.status(['--porcelain', '-unormal']))) {
return true
}
// Check working index changes
if (await this.diff(diffArgs)) {
if (await this.hasDiff()) {
return true
}
// Check untracked changes
if (untracked && (await this.status(['--porcelain', '-unormal']))) {
// Check staged changes
if (await this.hasDiff(['--staged'])) {
return true
}
return false
Expand Down