Skip to content

Commit 1c989b0

Browse files
committed
Adds stashes & worktrees to Graph model
1 parent 5f869fd commit 1c989b0

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

src/env/node/git/localGitProvider.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ import type { GitTreeEntry } from '../../../git/models/tree';
128128
import type { GitUser } from '../../../git/models/user';
129129
import { isUserMatch } from '../../../git/models/user';
130130
import type { GitWorktree } from '../../../git/models/worktree';
131-
import { getWorktreeId, getWorktreesByBranch } from '../../../git/models/worktree';
131+
import { getWorktreeId, groupWorktreesByBranch } from '../../../git/models/worktree';
132132
import { parseGitBlame } from '../../../git/parsers/blameParser';
133133
import { parseGitBranches } from '../../../git/parsers/branchParser';
134134
import {
@@ -2319,21 +2319,25 @@ export class LocalGitProvider implements GitProvider, Disposable {
23192319
const refParser = getRefParser();
23202320
const statsParser = getGraphStatsParser();
23212321

2322-
const [refResult, stashResult, branchesResult, remotesResult, currentUserResult, worktreesByBranchResult] =
2322+
const [refResult, stashResult, branchesResult, remotesResult, currentUserResult, worktreesResult] =
23232323
await Promise.allSettled([
23242324
this.git.log(repoPath, undefined, ...refParser.arguments, '-n1', options?.ref ?? 'HEAD'),
23252325
this.getStash(repoPath),
23262326
this.getBranches(repoPath),
23272327
this.getRemotes(repoPath),
23282328
this.getCurrentUser(repoPath),
2329-
getWorktreesByBranch(this.container.git.getRepository(repoPath), { includeMainWorktree: true }),
2329+
this.container.git
2330+
.getWorktrees(repoPath)
2331+
.then(w => [w, groupWorktreesByBranch(w, { includeMainWorktree: true })]) satisfies Promise<
2332+
[GitWorktree[], Map<string, GitWorktree>]
2333+
>,
23302334
]);
23312335

23322336
const branches = getSettledValue(branchesResult)?.values;
23332337
const branchMap = branches != null ? new Map(branches.map(r => [r.name, r])) : new Map<string, GitBranch>();
23342338
const headBranch = branches?.find(b => b.current);
23352339
const headRefUpstreamName = headBranch?.upstream?.name;
2336-
const worktreesByBranch = getSettledValue(worktreesByBranchResult);
2340+
const [worktrees, worktreesByBranch] = getSettledValue(worktreesResult) ?? [[], new Map<string, GitWorktree>()];
23372341

23382342
let branchIdOfMainWorktree: string | undefined;
23392343
if (worktreesByBranch != null) {
@@ -2351,13 +2355,15 @@ export class LocalGitProvider implements GitProvider, Disposable {
23512355

23522356
const downstreamMap = new Map<string, string[]>();
23532357

2358+
let stashes: Map<string, GitStashCommit> | undefined;
23542359
let stdin: string | undefined;
23552360

23562361
// TODO@eamodio this is insanity -- there *HAS* to be a better way to get git log to return stashes
23572362
const stash = getSettledValue(stashResult);
23582363
if (stash?.commits.size) {
2364+
stashes = new Map(stash.commits);
23592365
stdin = join(
2360-
map(stash.commits.values(), c => c.sha.substring(0, 9)),
2366+
map(stashes.values(), c => c.sha.substring(0, 9)),
23612367
'\n',
23622368
);
23632369
}
@@ -2421,6 +2427,8 @@ export class LocalGitProvider implements GitProvider, Disposable {
24212427
branches: branchMap,
24222428
remotes: remoteMap,
24232429
downstreams: downstreamMap,
2430+
stashes: stashes,
2431+
worktrees: worktrees,
24242432
worktreesByBranch: worktreesByBranch,
24252433
rows: [],
24262434
};
@@ -2444,6 +2452,8 @@ export class LocalGitProvider implements GitProvider, Disposable {
24442452
branches: branchMap,
24452453
remotes: remoteMap,
24462454
downstreams: downstreamMap,
2455+
stashes: stashes,
2456+
worktrees: worktrees,
24472457
worktreesByBranch: worktreesByBranch,
24482458
rows: [],
24492459
};
@@ -2838,6 +2848,8 @@ export class LocalGitProvider implements GitProvider, Disposable {
28382848
branches: branchMap,
28392849
remotes: remoteMap,
28402850
downstreams: downstreamMap,
2851+
stashes: stashes,
2852+
worktrees: worktrees,
28412853
worktreesByBranch: worktreesByBranch,
28422854
rows: rows,
28432855
id: sha,

src/git/models/graph.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
import type { GkProviderId } from '../../gk/models/repositoryIdentities';
1111
import type { Brand, Unbrand } from '../../system/brand';
1212
import type { GitBranch } from './branch';
13+
import type { GitStashCommit } from './commit';
1314
import type { GitRemote } from './remote';
1415
import type { GitWorktree } from './worktree';
1516

@@ -50,6 +51,8 @@ export interface GitGraph {
5051
readonly branches: Map<string, GitBranch>;
5152
readonly remotes: Map<string, GitRemote>;
5253
readonly downstreams: Map<string, string[]>;
54+
readonly stashes: Map<string, GitStashCommit> | undefined;
55+
readonly worktrees: GitWorktree[] | undefined;
5356
readonly worktreesByBranch: Map<string, GitWorktree> | undefined;
5457

5558
/** The rows for the set of commits requested */

src/git/models/worktree.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,10 @@ export async function getWorktreesByBranch(
345345
if (repos == null) return worktreesByBranch;
346346

347347
async function addWorktrees(repo: Repository) {
348-
const worktrees = await repo.getWorktrees();
349-
for (const wt of worktrees) {
350-
if (wt.branch == null || (!options?.includeMainWorktree && wt.main)) continue;
351-
352-
worktreesByBranch.set(wt.branch.id, wt);
353-
}
348+
groupWorktreesByBranch(await repo.getWorktrees(), {
349+
includeMainWorktree: options?.includeMainWorktree,
350+
worktreesByBranch: worktreesByBranch,
351+
});
354352
}
355353

356354
if (!Array.isArray(repos)) {
@@ -362,6 +360,22 @@ export async function getWorktreesByBranch(
362360
return worktreesByBranch;
363361
}
364362

363+
export function groupWorktreesByBranch(
364+
worktrees: GitWorktree[],
365+
options?: { includeMainWorktree?: boolean; worktreesByBranch?: Map<string, GitWorktree> },
366+
) {
367+
const worktreesByBranch = options?.worktreesByBranch ?? new Map<string, GitWorktree>();
368+
if (worktrees == null) return worktreesByBranch;
369+
370+
for (const wt of worktrees) {
371+
if (wt.branch == null || (!options?.includeMainWorktree && wt.main)) continue;
372+
373+
worktreesByBranch.set(wt.branch.id, wt);
374+
}
375+
376+
return worktreesByBranch;
377+
}
378+
365379
export function getOpenedWorktreesByBranch(
366380
worktreesByBranch: Map<string, GitWorktree> | undefined,
367381
): Set<string> | undefined {

src/plus/integrations/providers/github/githubGitProvider.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { GitUri } from '../../../../git/gitUri';
4242
import type { GitBlame, GitBlameAuthor, GitBlameLine, GitBlameLines } from '../../../../git/models/blame';
4343
import type { BranchSortOptions } from '../../../../git/models/branch';
4444
import { getBranchId, getBranchNameWithoutRemote, GitBranch, sortBranches } from '../../../../git/models/branch';
45-
import type { GitCommitLine } from '../../../../git/models/commit';
45+
import type { GitCommitLine, GitStashCommit } from '../../../../git/models/commit';
4646
import { getChangedFilesCount, GitCommit, GitCommitIdentity } from '../../../../git/models/commit';
4747
import { deletedOrMissing, uncommitted } from '../../../../git/models/constants';
4848
import { GitContributor } from '../../../../git/models/contributor';
@@ -84,6 +84,7 @@ import { getTagId, GitTag, sortTags } from '../../../../git/models/tag';
8484
import type { GitTreeEntry } from '../../../../git/models/tree';
8585
import type { GitUser } from '../../../../git/models/user';
8686
import { isUserMatch } from '../../../../git/models/user';
87+
import type { GitWorktree } from '../../../../git/models/worktree';
8788
import { getRemoteProviderMatcher, loadRemoteProviders } from '../../../../git/remotes/remoteProviders';
8889
import type { GitSearch, GitSearchResultData, GitSearchResults } from '../../../../git/search';
8990
import { getSearchQueryComparisonKey, parseSearchQuery } from '../../../../git/search';
@@ -1329,6 +1330,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
13291330
getSettledValue(currentUserResult),
13301331
avatars,
13311332
ids,
1333+
undefined,
1334+
undefined,
1335+
undefined,
13321336
{ ...options, useAvatars: useAvatars },
13331337
);
13341338
}
@@ -1346,6 +1350,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
13461350
currentUser: GitUser | undefined,
13471351
avatars: Map<string, string>,
13481352
ids: Set<string>,
1353+
stashes: Map<string, GitStashCommit> | undefined,
1354+
worktrees: GitWorktree[] | undefined,
1355+
worktreesByBranch: Map<string, GitWorktree> | undefined,
13491356
options?: {
13501357
branch?: string;
13511358
include?: { stats?: boolean };
@@ -1365,7 +1372,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
13651372
branches: branchMap,
13661373
remotes: remoteMap,
13671374
downstreams: downstreamMap,
1368-
worktreesByBranch: undefined,
1375+
stashes: stashes,
1376+
worktrees: worktrees,
1377+
worktreesByBranch: worktreesByBranch,
13691378
rows: [],
13701379
};
13711380
}
@@ -1380,7 +1389,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
13801389
branches: branchMap,
13811390
remotes: remoteMap,
13821391
downstreams: downstreamMap,
1383-
worktreesByBranch: undefined,
1392+
stashes: stashes,
1393+
worktrees: worktrees,
1394+
worktreesByBranch: worktreesByBranch,
13841395
rows: [],
13851396
};
13861397
}
@@ -1629,7 +1640,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
16291640
branches: branchMap,
16301641
remotes: remoteMap,
16311642
downstreams: downstreamMap,
1632-
worktreesByBranch: undefined,
1643+
stashes: stashes,
1644+
worktrees: worktrees,
1645+
worktreesByBranch: worktreesByBranch,
16331646
rows: rows,
16341647
id: options?.ref,
16351648

@@ -1653,6 +1666,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
16531666
currentUser,
16541667
avatars,
16551668
ids,
1669+
stashes,
1670+
worktrees,
1671+
worktreesByBranch,
16561672
options,
16571673
);
16581674
},

0 commit comments

Comments
 (0)