Skip to content

Commit

Permalink
Merge pull request #627 from peter-evans/git-diff-perf
Browse files Browse the repository at this point in the history
perf: set diff quiet and switch isdirty command order
  • Loading branch information
peter-evans authored Nov 17, 2020
2 parents 0fd77ba + ddeca94 commit ff0beed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
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

0 comments on commit ff0beed

Please sign in to comment.