Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3fd9f4

Browse files
authored
Merge pull request atom#2246 from atom/aw/is-commit-pushed-stall
Improve performance of isCommitPushed
2 parents eaf0e4a + c30bf6a commit b3fd9f4

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

lib/git-shell-out-strategy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,9 @@ export default class GitShellOutStrategy {
985985
} else if (option.showRemote) {
986986
args.splice(1, 0, '--remotes');
987987
}
988+
if (option.pattern) {
989+
args.push(option.pattern);
990+
}
988991
return (await this.exec(args)).trim().split(LINE_ENDING_REGEX);
989992
}
990993

lib/models/repository-states/present.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,18 @@ export default class Present extends State {
770770
}
771771

772772
async isCommitPushed(sha) {
773-
const remoteBranchesWithCommit = await this.git().getBranchesWithCommit(sha, {showLocal: false, showRemote: true});
774-
const currentRemote = (await this.repository.getCurrentBranch()).getUpstream();
775-
return remoteBranchesWithCommit.includes(currentRemote.getFullRef());
773+
const currentBranch = await this.repository.getCurrentBranch();
774+
const upstream = currentBranch.getPush();
775+
if (!upstream.isPresent()) {
776+
return false;
777+
}
778+
779+
const contained = await this.git().getBranchesWithCommit(sha, {
780+
showLocal: false,
781+
showRemote: true,
782+
pattern: upstream.getShortRef(),
783+
});
784+
return contained.some(ref => ref.length > 0);
776785
}
777786

778787
// Author information

test/git-strategies.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,13 @@ import * as reporterProxy from '../lib/reporter-proxy';
10801080
['refs/remotes/origin/HEAD', 'refs/remotes/origin/master'],
10811081
);
10821082
});
1083+
1084+
it('includes only refs matching a pattern', async function() {
1085+
assert.sameMembers(
1086+
await git.getBranchesWithCommit(SHA, {showLocal: true, showRemote: true, pattern: 'origin/master'}),
1087+
['refs/remotes/origin/master'],
1088+
);
1089+
});
10831090
});
10841091

10851092
describe('getRemotes()', function() {

test/models/repository.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,40 @@ describe('Repository', function() {
882882
});
883883
});
884884

885+
describe('isCommitPushed(sha)', function() {
886+
it('returns true if SHA is reachable from the upstream ref', async function() {
887+
const {localRepoPath} = await setUpLocalAndRemoteRepositories('multiple-commits');
888+
const repository = new Repository(localRepoPath);
889+
await repository.getLoadPromise();
890+
891+
const sha = (await repository.getLastCommit()).getSha();
892+
assert.isTrue(await repository.isCommitPushed(sha));
893+
});
894+
895+
it('returns false if SHA is not reachable from upstream', async function() {
896+
const {localRepoPath} = await setUpLocalAndRemoteRepositories('multiple-commits');
897+
const repository = new Repository(localRepoPath);
898+
await repository.getLoadPromise();
899+
900+
await repository.git.commit('unpushed', {allowEmpty: true});
901+
repository.refresh();
902+
903+
const sha = (await repository.getLastCommit()).getSha();
904+
assert.isFalse(await repository.isCommitPushed(sha));
905+
});
906+
907+
it('returns false on a detached HEAD', async function() {
908+
const workdir = await cloneRepository('multiple-commits');
909+
const repository = new Repository(workdir);
910+
await repository.getLoadPromise();
911+
912+
await repository.checkout('HEAD~2');
913+
914+
const sha = (await repository.getLastCommit()).getSha();
915+
assert.isFalse(await repository.isCommitPushed(sha));
916+
});
917+
});
918+
885919
describe('undoLastCommit()', function() {
886920
it('performs a soft reset', async function() {
887921
const workingDirPath = await cloneRepository('multiple-commits');

0 commit comments

Comments
 (0)