Skip to content

Commit

Permalink
refactor: Reason type
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed Jul 31, 2024
1 parent 45bc292 commit 84c7da0
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import * as humanise from './humanise';
import { partition } from './util';
import { toFossilUri } from './uri';
import { FossilPreviewManager } from './preview';
import type { FossilCWD, FossilExecutable } from './fossilExecutable';
import type { FossilCWD, FossilExecutable, Reason } from './fossilExecutable';

import { localize } from './main';
import type { Credentials } from './gitExport';
Expand Down Expand Up @@ -196,7 +196,7 @@ export class CommandCenter {

@command(Inline.Repository)
async refresh(repository: Repository): Promise<void> {
await repository.status('forced refresh');
await repository.status('forced refresh' as Reason);
}

@command()
Expand Down
29 changes: 17 additions & 12 deletions src/fossilExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type FossilStdOut = Distinct<
'raw fossil stdout' | 'fossil status stdout' | 'RenderedHTML'
>;
export type FossilStdErr = Distinct<string, 'raw fossil stderr'>;
export type Reason = Distinct<string, 'exec reason'> | undefined;

/** cwd for executing fossil */
export type FossilCWD =
Expand Down Expand Up @@ -185,7 +186,7 @@ export class FossilExecutable {
fossilCwd: FossilCWD,
args: FossilArgs
): Promise<Buffer | undefined> {
const res = await this._loggingExec(args, '', { cwd: fossilCwd });
const res = await this._loggingExec(args, { cwd: fossilCwd });
if (!res.exitCode) {
return res.stdout;
}
Expand Down Expand Up @@ -295,13 +296,13 @@ export class FossilExecutable {

private async _loggingExec(
args: FossilArgs,
reason: string,
options: FossilSpawnOptions
options: FossilSpawnOptions,
reason?: Reason
) {
const startTimeHR = process.hrtime();
const waitAndLog = (timeout: number): ReturnType<typeof setTimeout> => {
return setTimeout(() => {
this.logArgs(args, reason, 'still running');
this.logArgs(args, 'still running', reason);
logTimeout = waitAndLog(timeout * 4);
}, timeout);
};
Expand All @@ -312,22 +313,26 @@ export class FossilExecutable {
const durationHR = process.hrtime(startTimeHR);
this.logArgs(
args,
reason,
`${Math.floor(msFromHighResTime(durationHR))}ms`
`${Math.floor(msFromHighResTime(durationHR))}ms`,
reason
);
return resultRaw;
}

public async exec(
cwd: FossilCWD,
args: FossilArgs,
reason = '',
reason?: Reason,
options: Omit<FossilSpawnOptions, 'cwd'> = {} as const
): Promise<ExecResult> {
const resultRaw = await this._loggingExec(args, reason, {
cwd,
...options,
});
const resultRaw = await this._loggingExec(
args,
{
cwd,
...options,
},
reason
);
const result: ExecResult = {
...resultRaw,
stdout: resultRaw.stdout.toString('utf8') as FossilStdOut,
Expand Down Expand Up @@ -388,7 +393,7 @@ export class FossilExecutable {
private log(output: string): void {
this.outputChannel.info(output);
}
private logArgs(args: FossilArgs, reason: string, info: string): void {
private logArgs(args: FossilArgs, info: string, reason: Reason): void {
if (args[0] == 'clone') {
// replace password with 9 asterisks
args = [...args];
Expand Down
6 changes: 4 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
commands,
ConfigurationChangeEvent,
} from 'vscode';
import type { FossilExecutable } from './fossilExecutable';
import type { FossilExecutable, Reason } from './fossilExecutable';
import { anyEvent, filterEvent, dispose, eventToPromise } from './util';
import { memoize, debounce, sequentialize } from './decorators';
import * as path from 'path';
Expand Down Expand Up @@ -343,7 +343,9 @@ export class Model implements Disposable {
for (const { oldUri, newUri } of e.files) {
const repository = this.getRepository(oldUri);
if (repository) {
await repository.updateModelState('file rename event');
await repository.updateModelState(
'file rename event' as Reason
);
if (
repository.isInAnyGroup(oldUri) ||
repository.isDirInAnyGroup(oldUri)
Expand Down
9 changes: 5 additions & 4 deletions src/openedRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
FossilSpawnOptions,
FossilArgs,
ExecResult,
Reason,
} from './fossilExecutable';
import { NewBranchOptions } from './interaction';
import { FossilCWD } from './fossilExecutable';
Expand Down Expand Up @@ -205,7 +206,7 @@ export class OpenedRepository {
const result = await executable.exec(
cwd,
['info'],
`getting root for '${anypath}'`
`getting root for '${anypath}'` as Reason
);
const root = result.stdout.match(/local-root:\s*(.+)\/\s/);
if (root) {
Expand All @@ -216,7 +217,7 @@ export class OpenedRepository {

async exec(
args: FossilArgs,
reason = '',
reason?: Reason,
options: Omit<FossilSpawnOptions, 'cwd'> = {} as const
): Promise<ExecResult> {
return this.executable.exec(this.root, args, reason, options);
Expand Down Expand Up @@ -539,7 +540,7 @@ export class OpenedRepository {

/** Report the change status of files in the current checkout */
@throttle
async getStatus(reason: string): Promise<ExecResult> {
async getStatus(reason: Reason): Promise<ExecResult> {
return this.exec(['status', '--differ', '--merge'], reason);
}

Expand Down Expand Up @@ -655,7 +656,7 @@ export class OpenedRepository {
async config<T extends ConfigKey>(keys: T[]): Promise<Map<T, string>> {
const result = await this.exec(
['sqlite', '--readonly'],
'reading configuration for github',
'reading configuration for github' as Reason,
{
stdin_data:
'.mode json\n' +
Expand Down
22 changes: 16 additions & 6 deletions src/preview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { FossilExecutable, FossilCWD, FossilArgs } from './fossilExecutable';
import {
FossilExecutable,
FossilCWD,
FossilArgs,
Reason,
} from './fossilExecutable';
import * as path from 'path';
import {
Uri,
Expand Down Expand Up @@ -271,7 +276,7 @@ class FossilPreview implements IDisposable {
mimetype,
...(where == 'Technote' ? ['--technote', 'now'] : []),
],
'',
'' as Reason,
{
stdin_data: source,
}
Expand Down Expand Up @@ -303,10 +308,15 @@ class FossilPreview implements IDisposable {
];
}

const result = await this.executable.exec(this.dirname, args, '', {
stdin_data: this.current_content[0],
logErrors: false,
});
const result = await this.executable.exec(
this.dirname,
args,
'preview' as Reason,
{
stdin_data: this.current_content[0],
logErrors: false,
}
);
let html: RenderedHTML;
if (!result.exitCode) {
html = result.stdout as RenderedHTML;
Expand Down
10 changes: 5 additions & 5 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import type { InteractionAPI, NewBranchOptions } from './interaction';
import { FossilUriParams, toFossilUri } from './uri';

import { localize } from './main';
import type { ExecFailure, ExecResult } from './fossilExecutable';
import type { ExecFailure, ExecResult, Reason } from './fossilExecutable';
const iconsRootPath = path.join(path.dirname(__dirname), 'resources', 'icons');

type AvailableIcons =
Expand Down Expand Up @@ -427,7 +427,7 @@ export class Repository implements IDisposable, InteractionAPI {
);
this._sourceControl.statusBarCommands = statusBar.commands;

this.updateModelState('opening repository');
this.updateModelState('opening repository' as Reason);

this.disposables.push(new AutoIncomingOutgoing(this));
}
Expand All @@ -440,7 +440,7 @@ export class Repository implements IDisposable, InteractionAPI {
}

@throttle
async status(reason: string): Promise<ExecResult> {
async status(reason: Reason): Promise<ExecResult> {
const statusPromise = this.repository.getStatus(reason);
await this.runWithProgress(Operation.Status, () => statusPromise);
this.updateInputBoxPlaceholder();
Expand All @@ -467,7 +467,7 @@ export class Repository implements IDisposable, InteractionAPI {
@throttle
private async updateWhenIdleAndWait(): Promise<void> {
await this.whenIdleAndFocused();
await this.updateModelState('idle update');
await this.updateModelState('idle update' as Reason);
await delay(5000);
}

Expand Down Expand Up @@ -1065,7 +1065,7 @@ export class Repository implements IDisposable, InteractionAPI {
*/
@throttle
public async updateModelState(
reason = 'model state is updating'
reason: Reason = 'model state is updating' as Reason
): Promise<ExecFailure | undefined> {
const result = await this.repository.getStatus(reason);
if (result.exitCode) {
Expand Down
3 changes: 2 additions & 1 deletion src/test/suite/mergeSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as assert from 'assert/strict';
import * as fs from 'fs/promises';
import { FossilBranch, OpenedRepository } from '../../openedRepository';
import { Suite } from 'mocha';
import { Reason } from '../../fossilExecutable';

export function MergeSuite(this: Suite): void {
test('Merge error is shown', async () => {
Expand Down Expand Up @@ -112,7 +113,7 @@ export function MergeSuite(this: Suite): void {
sinon.assert.calledOnce(sqp);
sinon.assert.calledOnce(sib);

await repository.updateModelState('test');
await repository.updateModelState('test' as Reason);
assertGroups(repository, new Map(), new Map());
}).timeout(5000);

Expand Down
3 changes: 2 additions & 1 deletion src/test/suite/renameSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as fs from 'fs/promises';
import { OpenedRepository, ResourceStatus } from '../../openedRepository';
import { delay, eventToPromise } from '../../util';
import { Suite, before } from 'mocha';
import { Reason } from '../../fossilExecutable';

export function RenameSuite(this: Suite): void {
before(async () => {
Expand Down Expand Up @@ -81,7 +82,7 @@ export function RenameSuite(this: Suite): void {
sinon.assert.calledOnceWithExactly(
status,
['status', '--differ', '--merge'],
'file rename event'
'file rename event' as Reason
);
sinon.assert.calledOnceWithExactly(
sim,
Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/resourceActionsSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TextDocumentShowOptions,
} from 'vscode';
import * as sinon from 'sinon';
import { FossilCWD } from '../../fossilExecutable';
import { FossilCWD, Reason } from '../../fossilExecutable';
import {
assertGroups,
cleanupFossil,
Expand Down Expand Up @@ -76,7 +76,7 @@ export function resourceActionsSuite(this: Suite): void {
let statusStub = fakeFossilStatus(execStub, 'EXTRA a.txt\nEXTRA b.txt');

const repository = getRepository();
await repository.updateModelState('test');
await repository.updateModelState('test' as Reason);
sinon.assert.calledOnce(statusStub);
assert.equal(repository.untrackedGroup.resourceStates.length, 2);
assertGroups(repository, new Map(), new Map());
Expand Down Expand Up @@ -105,7 +105,7 @@ export function resourceActionsSuite(this: Suite): void {
await cleanupFossil(repository);
const execStub = getExecStub(this.ctx.sandbox);
const statusStub = fakeFossilStatus(execStub, 'ADDED a\nADDED b');
await repository.updateModelState('test');
await repository.updateModelState('test' as Reason);
sinon.assert.calledOnce(statusStub);
assert.equal(repository.workingGroup.resourceStates.length, 2);
assert.equal(repository.stagingGroup.resourceStates.length, 0);
Expand Down
3 changes: 2 additions & 1 deletion src/test/suite/stateSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as assert from 'assert/strict';
import * as fs from 'fs/promises';
import { OpenedRepository } from '../../openedRepository';
import { Suite, before } from 'mocha';
import { Reason } from '../../fossilExecutable';

function PullAndPushSuite(this: Suite): void {
const noRemotes = async (
Expand Down Expand Up @@ -432,7 +433,7 @@ export function StageSuite(this: Suite): void {
const execStub = getExecStub(this.ctx.sandbox);
await fakeFossilStatus(execStub, status);
const repository = getRepository();
await repository.updateModelState('test');
await repository.updateModelState('test' as Reason);
};

test('Stage from working group', async () => {
Expand Down

0 comments on commit 84c7da0

Please sign in to comment.