Skip to content

Commit ff0beed

Browse files
authored
Merge pull request #627 from peter-evans/git-diff-perf
perf: set diff quiet and switch isdirty command order
2 parents 0fd77ba + ddeca94 commit ff0beed

File tree

3 files changed

+31
-48
lines changed

3 files changed

+31
-48
lines changed

dist/index.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ function isEven(git, branch1, branch2) {
9393
!(yield isBehind(git, branch1, branch2)));
9494
});
9595
}
96-
function hasDiff(git, branch1, branch2) {
97-
return __awaiter(this, void 0, void 0, function* () {
98-
const result = yield git.diff([`${branch1}..${branch2}`]);
99-
return result.length > 0;
100-
});
101-
}
10296
function splitLines(multilineString) {
10397
return multilineString
10498
.split('\n')
@@ -192,7 +186,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
192186
// squash merged but not deleted. We need to reset to make sure it doesn't appear
193187
// to have a diff with the base due to different commits for the same changes.
194188
// For changes on base this reset is equivalent to a rebase of the pull request branch.
195-
if ((yield hasDiff(git, branch, tempBranch)) ||
189+
if ((yield git.hasDiff([`${branch}..${tempBranch}`])) ||
196190
!(yield isAhead(git, base, tempBranch))) {
197191
core.info(`Resetting '${branch}'`);
198192
// Alternatively, git switch -C branch tempBranch
@@ -662,16 +656,6 @@ class GitCommandManager {
662656
return output.exitCode === 0;
663657
});
664658
}
665-
diff(options) {
666-
return __awaiter(this, void 0, void 0, function* () {
667-
const args = ['-c', 'core.pager=cat', 'diff'];
668-
if (options) {
669-
args.push(...options);
670-
}
671-
const output = yield this.exec(args);
672-
return output.stdout.trim();
673-
});
674-
}
675659
fetch(refSpec, remoteName, options) {
676660
return __awaiter(this, void 0, void 0, function* () {
677661
const args = ['-c', 'protocol.version=2', 'fetch'];
@@ -712,19 +696,28 @@ class GitCommandManager {
712696
getWorkingDirectory() {
713697
return this.workingDirectory;
714698
}
699+
hasDiff(options) {
700+
return __awaiter(this, void 0, void 0, function* () {
701+
const args = ['diff', '--quiet'];
702+
if (options) {
703+
args.push(...options);
704+
}
705+
const output = yield this.exec(args, true);
706+
return output.exitCode === 1;
707+
});
708+
}
715709
isDirty(untracked) {
716710
return __awaiter(this, void 0, void 0, function* () {
717-
const diffArgs = ['--abbrev=40', '--full-index', '--raw'];
718-
// Check staged changes
719-
if (yield this.diff([...diffArgs, '--staged'])) {
711+
// Check untracked changes
712+
if (untracked && (yield this.status(['--porcelain', '-unormal']))) {
720713
return true;
721714
}
722715
// Check working index changes
723-
if (yield this.diff(diffArgs)) {
716+
if (yield this.hasDiff()) {
724717
return true;
725718
}
726-
// Check untracked changes
727-
if (untracked && (yield this.status(['--porcelain', '-unormal']))) {
719+
// Check staged changes
720+
if (yield this.hasDiff(['--staged'])) {
728721
return true;
729722
}
730723
return false;

src/create-or-update-branch.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ async function isEven(
7878
)
7979
}
8080

81-
async function hasDiff(
82-
git: GitCommandManager,
83-
branch1: string,
84-
branch2: string
85-
): Promise<boolean> {
86-
const result = await git.diff([`${branch1}..${branch2}`])
87-
return result.length > 0
88-
}
89-
9081
function splitLines(multilineString: string): string[] {
9182
return multilineString
9283
.split('\n')
@@ -205,7 +196,7 @@ export async function createOrUpdateBranch(
205196
// to have a diff with the base due to different commits for the same changes.
206197
// For changes on base this reset is equivalent to a rebase of the pull request branch.
207198
if (
208-
(await hasDiff(git, branch, tempBranch)) ||
199+
(await git.hasDiff([`${branch}..${tempBranch}`])) ||
209200
!(await isAhead(git, base, tempBranch))
210201
) {
211202
core.info(`Resetting '${branch}'`)

src/git-command-manager.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@ export class GitCommandManager {
9696
return output.exitCode === 0
9797
}
9898

99-
async diff(options?: string[]): Promise<string> {
100-
const args = ['-c', 'core.pager=cat', 'diff']
101-
if (options) {
102-
args.push(...options)
103-
}
104-
const output = await this.exec(args)
105-
return output.stdout.trim()
106-
}
107-
10899
async fetch(
109100
refSpec: string[],
110101
remoteName?: string,
@@ -153,18 +144,26 @@ export class GitCommandManager {
153144
return this.workingDirectory
154145
}
155146

147+
async hasDiff(options?: string[]): Promise<boolean> {
148+
const args = ['diff', '--quiet']
149+
if (options) {
150+
args.push(...options)
151+
}
152+
const output = await this.exec(args, true)
153+
return output.exitCode === 1
154+
}
155+
156156
async isDirty(untracked: boolean): Promise<boolean> {
157-
const diffArgs = ['--abbrev=40', '--full-index', '--raw']
158-
// Check staged changes
159-
if (await this.diff([...diffArgs, '--staged'])) {
157+
// Check untracked changes
158+
if (untracked && (await this.status(['--porcelain', '-unormal']))) {
160159
return true
161160
}
162161
// Check working index changes
163-
if (await this.diff(diffArgs)) {
162+
if (await this.hasDiff()) {
164163
return true
165164
}
166-
// Check untracked changes
167-
if (untracked && (await this.status(['--porcelain', '-unormal']))) {
165+
// Check staged changes
166+
if (await this.hasDiff(['--staged'])) {
168167
return true
169168
}
170169
return false

0 commit comments

Comments
 (0)