Skip to content

Commit

Permalink
Git - do not show "Open on GitHub" action for commits that have not b…
Browse files Browse the repository at this point in the history
…een pushed to the remote (#237605)
  • Loading branch information
lszomoru authored Jan 9, 2025
1 parent ddf3841 commit a26fe3e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion extensions/git/src/blame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ export class GitBlameController {

// Remote commands
const defaultRemote = repository.getDefaultRemote();
if (defaultRemote?.fetchUrl) {
const unpublishedCommits = await repository.getUnpublishedCommits();

if (defaultRemote?.fetchUrl && !unpublishedCommits.has(blameInformation.hash)) {
remoteSourceCommands.push(...await getRemoteSourceControlHistoryItemCommands(defaultRemote.fetchUrl));
}
}
Expand Down
9 changes: 9 additions & 0 deletions extensions/git/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,15 @@ export class Repository {
return commits[0];
}

async revList(ref1: string, ref2: string): Promise<string[]> {
const result = await this.exec(['rev-list', `${ref1}..${ref2}`]);
if (result.stderr) {
return [];
}

return result.stdout.trim().split('\n');
}

async revParse(ref: string): Promise<string | undefined> {
try {
const result = await fs.readFile(path.join(this.dotGit.path, ref), 'utf8');
Expand Down
30 changes: 30 additions & 0 deletions extensions/git/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ export class Repository implements Disposable {
private isRepositoryHuge: false | { limit: number } = false;
private didWarnAboutLimit = false;

private unpublishedCommits: Set<string> | undefined = undefined;
private branchProtection = new Map<string, BranchProtectionMatcher[]>();
private commitCommandCenter: CommitCommandsCenter;
private resourceCommandResolver = new ResourceCommandResolver(this);
Expand Down Expand Up @@ -2270,6 +2271,17 @@ export class Repository implements Disposable {
this.isCherryPickInProgress(),
this.getInputTemplate()]);

// Reset the list of unpublished commits if HEAD has
// changed (ex: checkout, fetch, pull, push, publish, etc.).
// The list of unpublished commits will be computed lazily
// on demand.
if (this.HEAD?.name !== HEAD?.name ||
this.HEAD?.commit !== HEAD?.commit ||
this.HEAD?.ahead !== HEAD?.ahead ||
this.HEAD?.upstream !== HEAD?.upstream) {
this.unpublishedCommits = undefined;
}

this._HEAD = HEAD;
this._remotes = remotes!;
this._submodules = submodules!;
Expand Down Expand Up @@ -2746,6 +2758,24 @@ export class Repository implements Disposable {
return false;
}

async getUnpublishedCommits(): Promise<Set<string>> {
if (this.unpublishedCommits) {
return this.unpublishedCommits;
}

if (this.HEAD && this.HEAD.name && this.HEAD.upstream && this.HEAD.ahead && this.HEAD.ahead > 0) {
const ref1 = `${this.HEAD.upstream.remote}/${this.HEAD.upstream.name}`;
const ref2 = this.HEAD.name;

const revList = await this.repository.revList(ref1, ref2);
this.unpublishedCommits = new Set<string>(revList);
} else {
this.unpublishedCommits = new Set<string>();
}

return this.unpublishedCommits;
}

dispose(): void {
this.disposables = dispose(this.disposables);
}
Expand Down
4 changes: 3 additions & 1 deletion extensions/git/src/timelineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class GitTimelineProvider implements TimelineProvider {
const openComparison = l10n.t('Open Comparison');

const defaultRemote = repo.getDefaultRemote();
const unpublishedCommits = await repo.getUnpublishedCommits();
const remoteSourceCommands: Command[] = defaultRemote?.fetchUrl
? await getRemoteSourceControlHistoryItemCommands(defaultRemote.fetchUrl)
: [];
Expand All @@ -230,7 +231,8 @@ export class GitTimelineProvider implements TimelineProvider {
item.description = c.authorName;
}

item.setItemDetails(uri, c.hash, c.authorName!, c.authorEmail, dateFormatter.format(date), message, c.shortStat, remoteSourceCommands);
const commitRemoteSourceCommands = !unpublishedCommits.has(c.hash) ? remoteSourceCommands : [];
item.setItemDetails(uri, c.hash, c.authorName!, c.authorEmail, dateFormatter.format(date), message, c.shortStat, commitRemoteSourceCommands);

const cmd = this.commands.resolveTimelineOpenDiffCommand(item, uri);
if (cmd) {
Expand Down

0 comments on commit a26fe3e

Please sign in to comment.