Skip to content

Commit 54f3988

Browse files
committed
Extracts getPseudoCommits into reusable helper
1 parent fb18b2b commit 54f3988

File tree

2 files changed

+129
-69
lines changed

2 files changed

+129
-69
lines changed

src/git/models/statusFile.ts

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import type { Uri } from 'vscode';
33
import type { Container } from '../../container';
44
import { memoize } from '../../system/decorators/-webview/memoize';
5-
import { createUncommittedChangesCommit } from '../utils/-webview/commit.utils';
65
import { getGitFileFormattedDirectory, getGitFileFormattedPath } from '../utils/-webview/file.utils';
6+
import { getPseudoCommits } from '../utils/-webview/statusFile.utils';
77
import { getGitFileStatusText } from '../utils/fileStatus.utils';
88
import type { GitCommit } from './commit';
99
import type { GitFile } from './file';
1010
import { GitFileChange } from './fileChange';
1111
import type { GitFileStatus } from './fileStatus';
1212
import { GitFileConflictStatus, GitFileIndexStatus, GitFileWorkingTreeStatus } from './fileStatus';
13-
import { uncommitted, uncommittedStaged } from './revision';
13+
import { uncommittedStaged } from './revision';
1414
import type { GitUser } from './user';
1515

1616
export class GitStatusFile implements GitFile {
@@ -127,73 +127,7 @@ export class GitStatusFile implements GitFile {
127127
}
128128

129129
getPseudoCommits(container: Container, user: GitUser | undefined): GitCommit[] {
130-
let now = new Date();
131-
132-
if (this.conflicted) {
133-
const file = new GitFileChange(
134-
container,
135-
this.repoPath,
136-
this.path,
137-
this.status,
138-
this.originalPath,
139-
'HEAD',
140-
undefined,
141-
false,
142-
);
143-
return [
144-
createUncommittedChangesCommit(container, this.repoPath, uncommitted, now, user, {
145-
files: { file: file, files: [file] },
146-
parents: ['HEAD'],
147-
}),
148-
];
149-
}
150-
151-
const commits: GitCommit[] = [];
152-
const staged = this.staged;
153-
154-
if (this.wip) {
155-
const previousSha = staged ? uncommittedStaged : 'HEAD';
156-
const file = new GitFileChange(
157-
this.container,
158-
this.repoPath,
159-
this.path,
160-
this.workingTreeStatus ?? this.status,
161-
this.originalPath,
162-
previousSha,
163-
undefined,
164-
false,
165-
);
166-
commits.push(
167-
createUncommittedChangesCommit(container, this.repoPath, uncommitted, now, user, {
168-
files: { file: file, files: [file] },
169-
parents: [previousSha],
170-
}),
171-
);
172-
173-
// Decrements the date to guarantee the staged entry (if exists) will be sorted after the working entry (most recent first)
174-
now = new Date(now.getTime() - 60000);
175-
}
176-
177-
if (staged) {
178-
const file = new GitFileChange(
179-
this.container,
180-
this.repoPath,
181-
this.path,
182-
this.indexStatus ?? this.status,
183-
this.originalPath,
184-
'HEAD',
185-
undefined,
186-
true,
187-
);
188-
commits.push(
189-
createUncommittedChangesCommit(container, this.repoPath, uncommittedStaged, now, user, {
190-
files: { file: file, files: [file] },
191-
parents: ['HEAD'],
192-
}),
193-
);
194-
}
195-
196-
return commits;
130+
return getPseudoCommits(container, [this], user);
197131
}
198132

199133
getPseudoFileChanges(): GitFileChange[] {
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import type { Container } from '../../../container';
2+
import type { GitCommit } from '../../models/commit';
3+
import { GitFileChange } from '../../models/fileChange';
4+
import { uncommitted, uncommittedStaged } from '../../models/revision';
5+
import type { GitStatusFile } from '../../models/statusFile';
6+
import type { GitUser } from '../../models/user';
7+
import { createUncommittedChangesCommit } from './commit.utils';
8+
9+
export function getPseudoCommits(
10+
container: Container,
11+
files: GitStatusFile[] | undefined,
12+
user: GitUser | undefined,
13+
): GitCommit[] {
14+
if (!files?.length) return [];
15+
16+
let now = new Date();
17+
const repoPath = files[0].repoPath;
18+
19+
let conflicted: GitFileChange[] | undefined;
20+
let staged: GitFileChange[] | undefined;
21+
let wip: GitFileChange[] | undefined;
22+
23+
for (const file of files) {
24+
if (file.conflicted) {
25+
conflicted ??= [];
26+
conflicted.push(
27+
new GitFileChange(
28+
container,
29+
repoPath,
30+
file.path,
31+
file.status,
32+
file.originalPath,
33+
'HEAD',
34+
undefined,
35+
false,
36+
),
37+
);
38+
} else {
39+
if (file.wip) {
40+
wip ??= [];
41+
wip.push(
42+
new GitFileChange(
43+
container,
44+
repoPath,
45+
file.path,
46+
file.workingTreeStatus ?? file.status,
47+
file.originalPath,
48+
file.staged ? uncommittedStaged : 'HEAD',
49+
undefined,
50+
false,
51+
),
52+
);
53+
}
54+
55+
if (file.staged) {
56+
staged ??= [];
57+
staged.push(
58+
new GitFileChange(
59+
container,
60+
repoPath,
61+
file.path,
62+
file.indexStatus ?? file.status,
63+
file.originalPath,
64+
'HEAD',
65+
undefined,
66+
true,
67+
),
68+
);
69+
}
70+
}
71+
}
72+
73+
const commits: GitCommit[] = [];
74+
75+
if (conflicted?.length || wip?.length) {
76+
const conflictedAndWipFiles = [...(conflicted ?? []), ...(wip ?? [])];
77+
commits.push(
78+
createUncommittedChangesCommit(container, repoPath, uncommitted, now, user, {
79+
files: {
80+
file: conflictedAndWipFiles.length === 1 ? conflictedAndWipFiles[0] : undefined,
81+
files: conflictedAndWipFiles,
82+
},
83+
parents: [staged?.length ? uncommittedStaged : 'HEAD'],
84+
}),
85+
);
86+
87+
// Decrements the date to guarantee the staged entry (if exists) will be sorted before the working entry (most recent first)
88+
now = new Date(now.getTime() - 60000);
89+
}
90+
91+
if (staged?.length) {
92+
commits.push(
93+
createUncommittedChangesCommit(container, repoPath, uncommittedStaged, now, user, {
94+
files: { file: staged.length === 1 ? staged[0] : undefined, files: staged },
95+
parents: ['HEAD'],
96+
}),
97+
);
98+
}
99+
100+
return commits;
101+
}
102+
103+
export async function getPseudoCommitsWithStats(
104+
container: Container,
105+
files: GitStatusFile[] | undefined,
106+
user: GitUser | undefined,
107+
): Promise<GitCommit[]> {
108+
const pseudoCommits = getPseudoCommits(container, files, user);
109+
if (!pseudoCommits.length) return pseudoCommits;
110+
111+
const diffProvider = container.git.diff(pseudoCommits[0].repoPath);
112+
113+
const commits: GitCommit[] = [];
114+
115+
for (const commit of pseudoCommits) {
116+
commits.push(
117+
commit.with({
118+
stats: await diffProvider.getChangedFilesCount(commit.sha, 'HEAD', {
119+
uris: commit.files?.map(f => f.uri),
120+
}),
121+
}),
122+
);
123+
}
124+
125+
return commits;
126+
}

0 commit comments

Comments
 (0)