Skip to content

Commit 7f24ce5

Browse files
committed
Removes unneeded stash methods
1 parent 69734cf commit 7f24ce5

File tree

4 files changed

+71
-87
lines changed

4 files changed

+71
-87
lines changed

src/commands/git/stash.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export class StashGitCommand extends QuickCommand<State> {
345345
endSteps(state);
346346

347347
try {
348-
await state.repo.stashApply(
348+
await state.repo.git.stash()?.applyStash(
349349
// pop can only take a stash index, e.g. `stash@{1}`
350350
state.subcommand === 'pop' ? `stash@{${state.reference.number}}` : state.reference.ref,
351351
{ deleteAfter: state.subcommand === 'pop' },
@@ -447,7 +447,7 @@ export class StashGitCommand extends QuickCommand<State> {
447447
for (const ref of state.references) {
448448
try {
449449
// drop can only take a stash index, e.g. `stash@{1}`
450-
await state.repo.stashDelete(`stash@{${ref.number}}`, ref.ref);
450+
await state.repo.git.stash()?.deleteStash(`stash@{${ref.number}}`, ref.ref);
451451
} catch (ex) {
452452
Logger.error(ex, context.title);
453453

@@ -540,9 +540,9 @@ export class StashGitCommand extends QuickCommand<State> {
540540

541541
try {
542542
if (state.flags.includes('--snapshot')) {
543-
await state.repo.stashSaveSnapshot(state.message);
543+
await state.repo.git.stash()?.saveSnapshot(state.message);
544544
} else {
545-
await state.repo.stashSave(state.message, state.uris, {
545+
await state.repo.git.stash()?.saveStash(state.message, state.uris, {
546546
includeUntracked: state.flags.includes('--include-untracked'),
547547
keepIndex: state.flags.includes('--keep-index'),
548548
onlyStaged: state.flags.includes('--staged'),
@@ -745,12 +745,9 @@ export class StashGitCommand extends QuickCommand<State> {
745745
endSteps(state);
746746

747747
try {
748-
await state.repo.stashRename(
749-
state.reference.name,
750-
state.reference.ref,
751-
state.message,
752-
state.reference.stashOnRef,
753-
);
748+
await state.repo.git
749+
.stash()
750+
?.renameStash(state.reference.name, state.reference.ref, state.message, state.reference.stashOnRef);
754751
} catch (ex) {
755752
Logger.error(ex, context.title);
756753
void showGenericErrorMessage(ex.message);

src/env/node/git/sub-providers/stash.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { GitStashCommit } from '../../../../git/models/commit';
88
import { GitCommit, GitCommitIdentity } from '../../../../git/models/commit';
99
import type { GitFileStatus } from '../../../../git/models/file';
1010
import { GitFileChange, GitFileWorkingTreeStatus, mapFilesWithStats } from '../../../../git/models/file';
11+
import { RepositoryChange } from '../../../../git/models/repository';
1112
import type { GitStash } from '../../../../git/models/stash';
1213
import type { ParserWithFilesAndMaybeStats } from '../../../../git/parsers/logParser';
1314
import { createLogParserWithFiles, createLogParserWithFilesAndStats } from '../../../../git/parsers/logParser';
@@ -33,10 +34,12 @@ export class StashGitSubProvider implements GitStashSubProvider {
3334
private readonly cache: GitCache,
3435
) {}
3536

37+
@gate()
3638
@log()
3739
async applyStash(repoPath: string, stashName: string, options?: { deleteAfter?: boolean }): Promise<void> {
3840
try {
3941
await this.git.stash__apply(repoPath, stashName, Boolean(options?.deleteAfter));
42+
this.container.events.fire('git:repo:change', { repoPath: repoPath, changes: [RepositoryChange.Stash] });
4043
} catch (ex) {
4144
if (ex instanceof Error) {
4245
const msg: string = ex.message ?? '';
@@ -188,6 +191,7 @@ export class StashGitSubProvider implements GitStashSubProvider {
188191
return gitStash ?? undefined;
189192
}
190193

194+
@gate()
191195
@log()
192196
async getStashCommitFiles(
193197
repoPath: string,
@@ -270,12 +274,15 @@ export class StashGitSubProvider implements GitStashSubProvider {
270274
return undefined;
271275
}
272276

277+
@gate()
273278
@log()
274279
async deleteStash(repoPath: string, stashName: string, ref?: string): Promise<void> {
275280
await this.git.stash__delete(repoPath, stashName, ref);
281+
this.container.events.fire('git:repo:change', { repoPath: repoPath, changes: [RepositoryChange.Stash] });
276282
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['stashes'] });
277283
}
278284

285+
@gate()
279286
@log()
280287
async renameStash(
281288
repoPath: string,
@@ -285,6 +292,7 @@ export class StashGitSubProvider implements GitStashSubProvider {
285292
stashOnRef?: string,
286293
): Promise<void> {
287294
await this.git.stash__rename(repoPath, stashName, ref, message, stashOnRef);
295+
this.container.events.fire('git:repo:change', { repoPath: repoPath, changes: [RepositoryChange.Stash] });
288296
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['stashes'] });
289297
}
290298

@@ -295,52 +303,53 @@ export class StashGitSubProvider implements GitStashSubProvider {
295303
uris?: Uri[],
296304
options?: { includeUntracked?: boolean; keepIndex?: boolean; onlyStaged?: boolean },
297305
): Promise<void> {
298-
try {
299-
if (!uris?.length) {
300-
await this.git.stash__push(repoPath, message, options);
301-
return;
302-
}
306+
if (!uris?.length) {
307+
await this.git.stash__push(repoPath, message, options);
308+
this.container.events.fire('git:repo:change', { repoPath: repoPath, changes: [RepositoryChange.Stash] });
309+
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['stashes', 'status'] });
310+
return;
311+
}
303312

304-
await this.git.ensureGitVersion(
305-
'2.13.2',
306-
'Stashing individual files',
307-
' Please retry by stashing everything or install a more recent version of Git and try again.',
308-
);
313+
await this.git.ensureGitVersion(
314+
'2.13.2',
315+
'Stashing individual files',
316+
' Please retry by stashing everything or install a more recent version of Git and try again.',
317+
);
309318

310-
const pathspecs = uris.map(u => `./${splitPath(u, repoPath)[0]}`);
319+
const pathspecs = uris.map(u => `./${splitPath(u, repoPath)[0]}`);
311320

312-
const stdinVersion = '2.30.0';
313-
let stdin = await this.git.isAtLeastVersion(stdinVersion);
314-
if (stdin && options?.onlyStaged && uris.length) {
315-
// Since Git doesn't support --staged with --pathspec-from-file try to pass them in directly
316-
stdin = false;
317-
}
318-
319-
// If we don't support stdin, then error out if we are over the maximum allowed git cli length
320-
if (!stdin && countStringLength(pathspecs) > maxGitCliLength) {
321-
await this.git.ensureGitVersion(
322-
stdinVersion,
323-
`Stashing so many files (${pathspecs.length}) at once`,
324-
' Please retry by stashing fewer files or install a more recent version of Git and try again.',
325-
);
326-
}
321+
const stdinVersion = '2.30.0';
322+
let stdin = await this.git.isAtLeastVersion(stdinVersion);
323+
if (stdin && options?.onlyStaged && uris.length) {
324+
// Since Git doesn't support --staged with --pathspec-from-file try to pass them in directly
325+
stdin = false;
326+
}
327327

328-
await this.git.stash__push(repoPath, message, {
329-
...options,
330-
pathspecs: pathspecs,
331-
stdin: stdin,
332-
});
333-
} finally {
334-
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['stashes', 'status'] });
328+
// If we don't support stdin, then error out if we are over the maximum allowed git cli length
329+
if (!stdin && countStringLength(pathspecs) > maxGitCliLength) {
330+
await this.git.ensureGitVersion(
331+
stdinVersion,
332+
`Stashing so many files (${pathspecs.length}) at once`,
333+
' Please retry by stashing fewer files or install a more recent version of Git and try again.',
334+
);
335335
}
336+
337+
await this.git.stash__push(repoPath, message, {
338+
...options,
339+
pathspecs: pathspecs,
340+
stdin: stdin,
341+
});
342+
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['stashes', 'status'] });
336343
}
337344

345+
@gate()
338346
@log()
339347
async saveSnapshot(repoPath: string, message?: string): Promise<void> {
340348
const id = await this.git.stash__create(repoPath);
341349
if (id == null) return;
342350

343351
await this.git.stash__store(repoPath, id, message);
352+
this.container.events.fire('git:repo:change', { repoPath: repoPath, changes: [RepositoryChange.Stash] });
344353
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['stashes'] });
345354
}
346355
}

src/eventBus.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { CustomEditorIds, ViewIds, WebviewIds } from './constants.views';
44
import type { GitCaches } from './git/gitProvider';
55
import type { GitCommit } from './git/models/commit';
66
import type { GitRevisionReference } from './git/models/reference';
7+
import type { RepositoryChange } from './git/models/repository';
78
import type { Draft, LocalDraft } from './gk/models/drafts';
89

910
export type CommitSelectedEvent = EventBusEvent<'commit:selected'>;
@@ -35,11 +36,27 @@ interface GitCacheResetEventArgs {
3536
readonly caches?: GitCaches[];
3637
}
3738

39+
/**
40+
* Out-of-band event to ensure @type {import('./git/models/repository').Repository} fires its change event
41+
* Should only be listened to by @type {import('./git/models/repository').Repository}
42+
*/
43+
export type GitRepoChangeEvent = EventBusEvent<'git:repo:change'>;
44+
interface GitRepoChangeEventArgs {
45+
readonly repoPath: string;
46+
readonly changes: RepositoryChange[];
47+
}
48+
3849
type EventsMapping = {
3950
'commit:selected': CommitSelectedEventArgs;
4051
'draft:selected': DraftSelectedEventArgs;
4152
'file:selected': FileSelectedEventArgs;
53+
4254
'git:cache:reset': GitCacheResetEventArgs;
55+
/**
56+
* Out-of-band event to ensure @type {import('./git/models/repository').Repository} fires its change event
57+
* Should only be listened to by @type {import('./git/models/repository').Repository}
58+
*/
59+
'git:repo:change': GitRepoChangeEventArgs;
4360
};
4461

4562
interface EventBusEvent<T extends keyof EventsMapping = keyof EventsMapping> {

src/git/models/repository.ts

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ export class Repository implements Disposable {
323323
}
324324
}
325325
}),
326+
this.container.events.on('git:repo:change', e => {
327+
if (e.data.repoPath === this.path) {
328+
this.fireChange(...e.data.changes);
329+
}
330+
}),
326331
);
327332

328333
const watcher = workspace.createFileSystemWatcher(new RelativePattern(this.uri, '**/.gitignore'));
@@ -848,50 +853,6 @@ export class Repository implements Disposable {
848853
return this.updateStarred(true, branch);
849854
}
850855

851-
@gate()
852-
@log()
853-
async stashApply(stashName: string, options?: { deleteAfter?: boolean }) {
854-
await this.git.stash()?.applyStash(stashName, options);
855-
856-
this.fireChange(RepositoryChange.Stash);
857-
}
858-
859-
@gate()
860-
@log()
861-
async stashDelete(stashName: string, ref?: string) {
862-
await this.git.stash()?.deleteStash(stashName, ref);
863-
864-
this.fireChange(RepositoryChange.Stash);
865-
}
866-
867-
@gate()
868-
@log()
869-
async stashRename(stashName: string, ref: string, message: string, stashOnRef?: string) {
870-
await this.git.stash()?.renameStash(stashName, ref, message, stashOnRef);
871-
872-
this.fireChange(RepositoryChange.Stash);
873-
}
874-
875-
@gate()
876-
@log()
877-
async stashSave(
878-
message?: string,
879-
uris?: Uri[],
880-
options?: { includeUntracked?: boolean; keepIndex?: boolean; onlyStaged?: boolean },
881-
): Promise<void> {
882-
await this.git.stash()?.saveStash(message, uris, options);
883-
884-
this.fireChange(RepositoryChange.Stash);
885-
}
886-
887-
@gate()
888-
@log()
889-
async stashSaveSnapshot(message?: string): Promise<void> {
890-
await this.git.stash()?.saveSnapshot(message);
891-
892-
this.fireChange(RepositoryChange.Stash);
893-
}
894-
895856
@gate()
896857
@log()
897858
async switch(ref: string, options?: { createBranch?: string | undefined; progress?: boolean }) {

0 commit comments

Comments
 (0)