@hyperdrive.bot/gut 0.1.14-alpha.0 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/worktree/create.js +0 -8
- package/dist/services/worktree.service.d.ts +0 -24
- package/dist/services/worktree.service.js +0 -90
- package/package.json +2 -2
- package/dist/commands/worktree/gc.d.ts +0 -13
- package/dist/commands/worktree/gc.js +0 -126
- package/dist/commands/worktree/list.d.ts +0 -12
- package/dist/commands/worktree/list.js +0 -63
- package/dist/commands/worktree/list.test.d.ts +0 -1
- package/dist/commands/worktree/list.test.js +0 -182
- package/dist/commands/worktree/prune.d.ts +0 -6
- package/dist/commands/worktree/prune.js +0 -28
- package/dist/commands/worktree/prune.test.d.ts +0 -1
- package/dist/commands/worktree/prune.test.js +0 -105
- package/dist/commands/worktree/remove.d.ts +0 -12
- package/dist/commands/worktree/remove.js +0 -48
- package/dist/commands/worktree/status.d.ts +0 -9
- package/dist/commands/worktree/status.js +0 -58
- package/dist/utils/duration.d.ts +0 -1
- package/dist/utils/duration.js +0 -26
- package/oclif.manifest.json +0 -2418
|
@@ -33,14 +33,6 @@ export default class WorktreeCreate extends BaseCommand {
|
|
|
33
33
|
};
|
|
34
34
|
async run() {
|
|
35
35
|
const { args, flags } = await this.parse(WorktreeCreate);
|
|
36
|
-
const stale = this.worktreeService.findStale();
|
|
37
|
-
if (stale.length > 0) {
|
|
38
|
-
this.log(chalk.yellow('⚠️ Stale worktree records detected (path does not exist on disk):'));
|
|
39
|
-
for (const record of stale) {
|
|
40
|
-
this.log(chalk.yellow(` - ${record.name} ${record.path}`));
|
|
41
|
-
}
|
|
42
|
-
this.log(chalk.yellow('Run `gut worktree prune` to remove them.'));
|
|
43
|
-
}
|
|
44
36
|
const workspaceRoot = this.configService.getWorkspaceRoot();
|
|
45
37
|
const slug = args.name.replace(/\//g, '-');
|
|
46
38
|
const wtPath = path.join(flags['base-dir'], slug);
|
|
@@ -2,20 +2,6 @@ import type { WorktreeRecord, WorktreeState } from '../models/entity.model.js';
|
|
|
2
2
|
import type { ConfigService } from './config.service.js';
|
|
3
3
|
import type { FocusService } from './focus.service.js';
|
|
4
4
|
import type { GitService } from './git.service.js';
|
|
5
|
-
export interface RemoveWorktreeOptions {
|
|
6
|
-
force: boolean;
|
|
7
|
-
logger?: (message: string) => void;
|
|
8
|
-
}
|
|
9
|
-
export interface RemoveWorktreeResult {
|
|
10
|
-
dirtyEntities: Array<{
|
|
11
|
-
changeCount: number;
|
|
12
|
-
entityName: string;
|
|
13
|
-
}>;
|
|
14
|
-
removed: boolean;
|
|
15
|
-
removedEntities: number;
|
|
16
|
-
skippedDueToDirty: boolean;
|
|
17
|
-
skippedEntities: number;
|
|
18
|
-
}
|
|
19
5
|
export declare class WorktreeService {
|
|
20
6
|
private readonly configService;
|
|
21
7
|
private readonly gitService;
|
|
@@ -25,16 +11,6 @@ export declare class WorktreeService {
|
|
|
25
11
|
constructor(configService: ConfigService, gitService: GitService, focusService: FocusService);
|
|
26
12
|
get(name: string): WorktreeRecord | null;
|
|
27
13
|
list(): WorktreeRecord[];
|
|
28
|
-
findStale(): WorktreeRecord[];
|
|
29
14
|
loadState(): WorktreeState;
|
|
30
|
-
/**
|
|
31
|
-
* Remove a worktree record: all entity worktrees in reverse order, then the
|
|
32
|
-
* super-repo worktree, pruning refs along the way, then splice the record
|
|
33
|
-
* out of state. Shared between `worktree remove` and `worktree gc`.
|
|
34
|
-
*
|
|
35
|
-
* If any entity has uncommitted changes and `force` is false, nothing is
|
|
36
|
-
* removed and `skippedDueToDirty` is returned true.
|
|
37
|
-
*/
|
|
38
|
-
removeWorktreeRecord(name: string, options: RemoveWorktreeOptions): Promise<RemoveWorktreeResult>;
|
|
39
15
|
saveState(state: WorktreeState): void;
|
|
40
16
|
}
|
|
@@ -20,9 +20,6 @@ export class WorktreeService {
|
|
|
20
20
|
list() {
|
|
21
21
|
return this.loadState().worktrees;
|
|
22
22
|
}
|
|
23
|
-
findStale() {
|
|
24
|
-
return this.loadState().worktrees.filter(w => !fs.existsSync(w.path));
|
|
25
|
-
}
|
|
26
23
|
loadState() {
|
|
27
24
|
if (!fs.existsSync(this.stateFile)) {
|
|
28
25
|
return { worktrees: [] };
|
|
@@ -40,93 +37,6 @@ export class WorktreeService {
|
|
|
40
37
|
return { worktrees: [] };
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
|
-
/**
|
|
44
|
-
* Remove a worktree record: all entity worktrees in reverse order, then the
|
|
45
|
-
* super-repo worktree, pruning refs along the way, then splice the record
|
|
46
|
-
* out of state. Shared between `worktree remove` and `worktree gc`.
|
|
47
|
-
*
|
|
48
|
-
* If any entity has uncommitted changes and `force` is false, nothing is
|
|
49
|
-
* removed and `skippedDueToDirty` is returned true.
|
|
50
|
-
*/
|
|
51
|
-
async removeWorktreeRecord(name, options) {
|
|
52
|
-
const { force, logger } = options;
|
|
53
|
-
const log = (message) => {
|
|
54
|
-
if (logger)
|
|
55
|
-
logger(message);
|
|
56
|
-
};
|
|
57
|
-
const record = this.get(name);
|
|
58
|
-
if (!record) {
|
|
59
|
-
throw new Error(`Worktree '${name}' not found`);
|
|
60
|
-
}
|
|
61
|
-
const workspaceRoot = this.configService.getWorkspaceRoot();
|
|
62
|
-
// Dirty-state pre-check (mirrors remove.ts:39-50)
|
|
63
|
-
const dirtyEntities = [];
|
|
64
|
-
for (const entity of record.entities) {
|
|
65
|
-
if (!fs.existsSync(entity.worktreePath))
|
|
66
|
-
continue;
|
|
67
|
-
// eslint-disable-next-line no-await-in-loop
|
|
68
|
-
const status = await this.gitService.getStatus(entity.worktreePath);
|
|
69
|
-
if (status.hasChanges) {
|
|
70
|
-
dirtyEntities.push({
|
|
71
|
-
changeCount: status.changes.length + status.untracked.length,
|
|
72
|
-
entityName: entity.entityName,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
if (dirtyEntities.length > 0 && !force) {
|
|
77
|
-
return {
|
|
78
|
-
dirtyEntities,
|
|
79
|
-
removed: false,
|
|
80
|
-
removedEntities: 0,
|
|
81
|
-
skippedDueToDirty: true,
|
|
82
|
-
skippedEntities: 0,
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
if (dirtyEntities.length > 0 && force) {
|
|
86
|
-
for (const d of dirtyEntities) {
|
|
87
|
-
log(` ⚠ ${d.entityName}: ${d.changeCount} uncommitted changes will be lost`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
// Entity worktrees in reverse order
|
|
91
|
-
let removedEntities = 0;
|
|
92
|
-
let skippedEntities = 0;
|
|
93
|
-
const reversed = [...record.entities].reverse();
|
|
94
|
-
for (const entity of reversed) {
|
|
95
|
-
const mainRepoPath = path.join(workspaceRoot, entity.entityPath);
|
|
96
|
-
if (!fs.existsSync(entity.worktreePath)) {
|
|
97
|
-
log(` ○ ${entity.entityName}: already removed (skipped)`);
|
|
98
|
-
skippedEntities++;
|
|
99
|
-
// eslint-disable-next-line no-await-in-loop
|
|
100
|
-
await this.gitService.worktreePrune(mainRepoPath).catch(() => { });
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
// eslint-disable-next-line no-await-in-loop
|
|
104
|
-
await this.gitService.worktreeRemove(mainRepoPath, entity.worktreePath, force);
|
|
105
|
-
// eslint-disable-next-line no-await-in-loop
|
|
106
|
-
await this.gitService.worktreePrune(mainRepoPath);
|
|
107
|
-
log(` ✓ ${entity.entityName}: worktree removed`);
|
|
108
|
-
removedEntities++;
|
|
109
|
-
}
|
|
110
|
-
// Super-repo worktree
|
|
111
|
-
if (fs.existsSync(record.path)) {
|
|
112
|
-
await this.gitService.worktreeRemove(workspaceRoot, record.path, force);
|
|
113
|
-
}
|
|
114
|
-
await this.gitService.worktreePrune(workspaceRoot);
|
|
115
|
-
// Splice the record out of state
|
|
116
|
-
const state = this.loadState();
|
|
117
|
-
const idx = state.worktrees.findIndex(w => w.name === name);
|
|
118
|
-
if (idx !== -1) {
|
|
119
|
-
state.worktrees.splice(idx, 1);
|
|
120
|
-
}
|
|
121
|
-
this.saveState(state);
|
|
122
|
-
return {
|
|
123
|
-
dirtyEntities,
|
|
124
|
-
removed: true,
|
|
125
|
-
removedEntities,
|
|
126
|
-
skippedDueToDirty: false,
|
|
127
|
-
skippedEntities,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
40
|
saveState(state) {
|
|
131
41
|
const serialized = JSON.stringify(state, null, 2);
|
|
132
42
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperdrive.bot/gut",
|
|
3
|
-
"version": "0.1.14
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "Git Unified Tooling - Enhanced git with workspace intelligence for entity-based organization",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -93,4 +93,4 @@
|
|
|
93
93
|
"access": "public",
|
|
94
94
|
"registry": "https://registry.npmjs.org/"
|
|
95
95
|
}
|
|
96
|
-
}
|
|
96
|
+
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { BaseCommand } from '../../base-command.js';
|
|
2
|
-
export default class WorktreeGc extends BaseCommand {
|
|
3
|
-
static description: string;
|
|
4
|
-
static examples: string[];
|
|
5
|
-
static flags: {
|
|
6
|
-
'dry-run': import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
7
|
-
force: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
8
|
-
'older-than': import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
-
};
|
|
10
|
-
run(): Promise<void>;
|
|
11
|
-
private countDirtyChanges;
|
|
12
|
-
private hasDirtyEntity;
|
|
13
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { Flags } from '@oclif/core';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import { BaseCommand } from '../../base-command.js';
|
|
5
|
-
import { parseDuration } from '../../utils/duration.js';
|
|
6
|
-
const DAY_MS = 86_400_000;
|
|
7
|
-
export default class WorktreeGc extends BaseCommand {
|
|
8
|
-
static description = 'Remove worktrees older than a given duration (garbage collection)';
|
|
9
|
-
static examples = [
|
|
10
|
-
'<%= config.bin %> worktree gc --older-than 7d',
|
|
11
|
-
'<%= config.bin %> worktree gc --older-than 14d --force',
|
|
12
|
-
'<%= config.bin %> worktree gc --older-than 7d --dry-run',
|
|
13
|
-
];
|
|
14
|
-
static flags = {
|
|
15
|
-
'dry-run': Flags.boolean({
|
|
16
|
-
char: 'n',
|
|
17
|
-
default: false,
|
|
18
|
-
description: 'List candidates without removing anything',
|
|
19
|
-
}),
|
|
20
|
-
force: Flags.boolean({
|
|
21
|
-
char: 'f',
|
|
22
|
-
default: false,
|
|
23
|
-
description: 'Remove even worktrees with uncommitted changes',
|
|
24
|
-
}),
|
|
25
|
-
'older-than': Flags.string({
|
|
26
|
-
char: 'o',
|
|
27
|
-
description: "Duration threshold (e.g. '7d', '24h', '30m', '90s')",
|
|
28
|
-
required: true,
|
|
29
|
-
}),
|
|
30
|
-
};
|
|
31
|
-
async run() {
|
|
32
|
-
const { flags } = await this.parse(WorktreeGc);
|
|
33
|
-
const olderThanInput = flags['older-than'];
|
|
34
|
-
let thresholdMs;
|
|
35
|
-
try {
|
|
36
|
-
thresholdMs = parseDuration(olderThanInput);
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
this.error(error instanceof Error ? error.message : String(error));
|
|
40
|
-
}
|
|
41
|
-
const state = this.worktreeService.loadState();
|
|
42
|
-
const now = Date.now();
|
|
43
|
-
const candidates = state.worktrees.filter(w => now - w.createdAt > thresholdMs);
|
|
44
|
-
if (candidates.length === 0) {
|
|
45
|
-
this.log(chalk.dim(`No worktrees older than ${olderThanInput}`));
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
// Collect per-candidate dirty state up front (used by both dry-run and real run)
|
|
49
|
-
const dirtyFlags = await Promise.all(candidates.map(async (record) => this.hasDirtyEntity(record)));
|
|
50
|
-
if (flags['dry-run']) {
|
|
51
|
-
this.log(chalk.bold(`\nWorktrees older than ${olderThanInput}:`));
|
|
52
|
-
for (const [i, record] of candidates.entries()) {
|
|
53
|
-
const ageDays = Math.floor((now - record.createdAt) / DAY_MS);
|
|
54
|
-
const dirtyLabel = dirtyFlags[i] ? chalk.yellow(' [dirty]') : '';
|
|
55
|
-
this.log(` ${record.name} ${ageDays}d old${dirtyLabel}`);
|
|
56
|
-
}
|
|
57
|
-
this.log(chalk.dim(`\n${candidates.length} candidate(s)`));
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
this.log(chalk.bold(`\n🧹 Garbage collecting worktrees older than ${olderThanInput}...`));
|
|
61
|
-
let removed = 0;
|
|
62
|
-
let skippedDirty = 0;
|
|
63
|
-
let skippedMissing = 0;
|
|
64
|
-
for (const [i, record] of candidates.entries()) {
|
|
65
|
-
if (dirtyFlags[i] && !flags.force) {
|
|
66
|
-
// eslint-disable-next-line no-await-in-loop
|
|
67
|
-
const changeCount = await this.countDirtyChanges(record);
|
|
68
|
-
this.log(chalk.yellow(` ⚠ ${record.name}: skipped (${changeCount} uncommitted changes)`));
|
|
69
|
-
skippedDirty++;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
// eslint-disable-next-line no-await-in-loop
|
|
73
|
-
const result = await this.worktreeService.removeWorktreeRecord(record.name, {
|
|
74
|
-
force: flags.force,
|
|
75
|
-
logger: (msg) => {
|
|
76
|
-
if (msg.includes('⚠'))
|
|
77
|
-
this.log(chalk.yellow(msg));
|
|
78
|
-
else if (msg.includes('○'))
|
|
79
|
-
this.log(chalk.dim(msg));
|
|
80
|
-
else if (msg.includes('✓'))
|
|
81
|
-
this.log(chalk.green(msg));
|
|
82
|
-
else
|
|
83
|
-
this.log(msg);
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
if (result.skippedDueToDirty) {
|
|
87
|
-
skippedDirty++;
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
removed++;
|
|
91
|
-
if (result.removedEntities === 0 && result.skippedEntities > 0) {
|
|
92
|
-
skippedMissing++;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
this.log(chalk.dim('─'.repeat(50)));
|
|
96
|
-
this.log(chalk.green(`✓ Garbage collected ${removed} worktree(s)`));
|
|
97
|
-
this.log(chalk.dim(` Removed: ${removed}`));
|
|
98
|
-
this.log(chalk.dim(` Skipped (dirty): ${skippedDirty}`));
|
|
99
|
-
this.log(chalk.dim(` Skipped (missing):${skippedMissing}`));
|
|
100
|
-
this.log(chalk.dim(` Candidates: ${candidates.length}`));
|
|
101
|
-
}
|
|
102
|
-
async countDirtyChanges(record) {
|
|
103
|
-
let total = 0;
|
|
104
|
-
for (const entity of record.entities) {
|
|
105
|
-
if (!fs.existsSync(entity.worktreePath))
|
|
106
|
-
continue;
|
|
107
|
-
// eslint-disable-next-line no-await-in-loop
|
|
108
|
-
const status = await this.gitService.getStatus(entity.worktreePath);
|
|
109
|
-
if (status.hasChanges) {
|
|
110
|
-
total += status.changes.length + status.untracked.length;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return total;
|
|
114
|
-
}
|
|
115
|
-
async hasDirtyEntity(record) {
|
|
116
|
-
for (const entity of record.entities) {
|
|
117
|
-
if (!fs.existsSync(entity.worktreePath))
|
|
118
|
-
continue;
|
|
119
|
-
// eslint-disable-next-line no-await-in-loop
|
|
120
|
-
const status = await this.gitService.getStatus(entity.worktreePath);
|
|
121
|
-
if (status.hasChanges)
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { BaseCommand } from '../../base-command.js';
|
|
2
|
-
export declare const MS_PER_DAY: number;
|
|
3
|
-
export declare const STALE_DAYS = 7;
|
|
4
|
-
export declare function formatWorktreeAge(createdAt: number, now: number): {
|
|
5
|
-
isStale: boolean;
|
|
6
|
-
text: string;
|
|
7
|
-
};
|
|
8
|
-
export default class WorktreeList extends BaseCommand {
|
|
9
|
-
static description: string;
|
|
10
|
-
static examples: string[];
|
|
11
|
-
run(): Promise<void>;
|
|
12
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import { BaseCommand } from '../../base-command.js';
|
|
4
|
-
export const MS_PER_DAY = 1000 * 60 * 60 * 24;
|
|
5
|
-
export const STALE_DAYS = 7;
|
|
6
|
-
export function formatWorktreeAge(createdAt, now) {
|
|
7
|
-
if (typeof createdAt !== 'number' || Number.isNaN(createdAt)) {
|
|
8
|
-
return { isStale: false, text: 'unknown age' };
|
|
9
|
-
}
|
|
10
|
-
const ageMs = now - createdAt;
|
|
11
|
-
if (ageMs <= 0) {
|
|
12
|
-
return { isStale: false, text: 'less than a day old' };
|
|
13
|
-
}
|
|
14
|
-
const ageDays = Math.floor(ageMs / MS_PER_DAY);
|
|
15
|
-
const isStale = ageMs / MS_PER_DAY > STALE_DAYS;
|
|
16
|
-
let text;
|
|
17
|
-
if (ageDays === 0) {
|
|
18
|
-
text = 'less than a day old';
|
|
19
|
-
}
|
|
20
|
-
else if (ageDays === 1) {
|
|
21
|
-
text = '1 day old';
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
text = `${ageDays} days old`;
|
|
25
|
-
}
|
|
26
|
-
return { isStale, text };
|
|
27
|
-
}
|
|
28
|
-
export default class WorktreeList extends BaseCommand {
|
|
29
|
-
static description = 'List all active worktrees with metadata and staleness detection';
|
|
30
|
-
static examples = ['<%= config.bin %> <%= command.id %>'];
|
|
31
|
-
async run() {
|
|
32
|
-
const stale = this.worktreeService.findStale();
|
|
33
|
-
if (stale.length > 0) {
|
|
34
|
-
this.log(chalk.yellow('⚠️ Stale worktree records detected (path does not exist on disk):'));
|
|
35
|
-
for (const record of stale) {
|
|
36
|
-
this.log(chalk.yellow(` - ${record.name} ${record.path}`));
|
|
37
|
-
}
|
|
38
|
-
this.log(chalk.yellow('Run `gut worktree prune` to remove them.'));
|
|
39
|
-
}
|
|
40
|
-
const worktrees = this.worktreeService.list();
|
|
41
|
-
if (worktrees.length === 0) {
|
|
42
|
-
this.log(`No active worktrees. Create one with: ${chalk.cyan('gut worktree create <branch-name>')}`);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
this.log(chalk.bold('\nActive worktrees:\n'));
|
|
46
|
-
for (const record of worktrees) {
|
|
47
|
-
const isStale = !fs.existsSync(record.path);
|
|
48
|
-
const formattedDate = new Date(record.createdAt).toISOString().replace('T', ' ').slice(0, 16);
|
|
49
|
-
const entityNames = record.entities.map(e => e.entityName).join(', ');
|
|
50
|
-
const age = formatWorktreeAge(record.createdAt, Date.now());
|
|
51
|
-
const ageLine = age.isStale
|
|
52
|
-
? chalk.yellow(` Age: ⚠ (${age.text}) — consider removing`)
|
|
53
|
-
: ` Age: (${age.text})`;
|
|
54
|
-
this.log(` ${chalk.bold(record.name)}`);
|
|
55
|
-
this.log(` Path: ${record.path}${isStale ? chalk.yellow(' [stale]') : ''}`);
|
|
56
|
-
this.log(` Branch: ${chalk.cyan(record.name)}`);
|
|
57
|
-
this.log(` Created: ${chalk.dim(formattedDate)}`);
|
|
58
|
-
this.log(ageLine);
|
|
59
|
-
this.log(` Entities: ${entityNames}`);
|
|
60
|
-
this.log('');
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import { MS_PER_DAY, formatWorktreeAge } from './list.js';
|
|
4
|
-
// Mock fs.existsSync
|
|
5
|
-
vi.mock('node:fs', () => ({
|
|
6
|
-
default: {
|
|
7
|
-
existsSync: vi.fn(() => true),
|
|
8
|
-
},
|
|
9
|
-
existsSync: vi.fn(() => true),
|
|
10
|
-
}));
|
|
11
|
-
// Mock chalk to return raw strings for easy assertion
|
|
12
|
-
vi.mock('chalk', () => ({
|
|
13
|
-
default: {
|
|
14
|
-
bold: (s) => s,
|
|
15
|
-
cyan: (s) => s,
|
|
16
|
-
dim: (s) => s,
|
|
17
|
-
yellow: (s) => s,
|
|
18
|
-
},
|
|
19
|
-
}));
|
|
20
|
-
function makeRecord(overrides = {}) {
|
|
21
|
-
return {
|
|
22
|
-
baseBranch: 'master',
|
|
23
|
-
createdAt: new Date('2026-03-19T13:22:00Z').getTime(),
|
|
24
|
-
entities: [
|
|
25
|
-
{ branch: 'workflow/deploy-batch', entityName: 'api', entityPath: 'packages/serverless/api', worktreePath: '/tmp/gut-worktrees/workflow-deploy-batch/packages/serverless/api' },
|
|
26
|
-
{ branch: 'workflow/deploy-batch', entityName: 'sign', entityPath: 'packages/web-apps/sign', worktreePath: '/tmp/gut-worktrees/workflow-deploy-batch/packages/web-apps/sign' },
|
|
27
|
-
],
|
|
28
|
-
name: 'workflow/deploy-batch',
|
|
29
|
-
path: '/tmp/gut-worktrees/workflow-deploy-batch',
|
|
30
|
-
...overrides,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
describe('WorktreeList', () => {
|
|
34
|
-
it('displays populated list with names, paths, branches, dates, entity names', async () => {
|
|
35
|
-
const record1 = makeRecord();
|
|
36
|
-
const record2 = makeRecord({
|
|
37
|
-
baseBranch: 'main',
|
|
38
|
-
createdAt: new Date('2026-03-19T14:05:00Z').getTime(),
|
|
39
|
-
entities: [{ branch: 'workflow/story-042', entityName: 'api', entityPath: 'packages/serverless/api', worktreePath: '/tmp/gut-worktrees/workflow-story-042/packages/serverless/api' }],
|
|
40
|
-
name: 'workflow/story-042',
|
|
41
|
-
path: '/tmp/gut-worktrees/workflow-story-042',
|
|
42
|
-
});
|
|
43
|
-
const logs = [];
|
|
44
|
-
// Import the command class
|
|
45
|
-
const { default: WorktreeList } = await import('./list.js');
|
|
46
|
-
// Create instance and override dependencies
|
|
47
|
-
const cmd = Object.create(WorktreeList.prototype);
|
|
48
|
-
cmd.log = (msg = '') => { logs.push(msg); };
|
|
49
|
-
cmd.worktreeService = { findStale: () => [], list: () => [record1, record2] };
|
|
50
|
-
vi.mocked(fs.existsSync).mockReturnValue(true);
|
|
51
|
-
await cmd.run();
|
|
52
|
-
const output = logs.join('\n');
|
|
53
|
-
expect(output).toContain('Active worktrees:');
|
|
54
|
-
expect(output).toContain('workflow/deploy-batch');
|
|
55
|
-
expect(output).toContain('workflow/story-042');
|
|
56
|
-
expect(output).toContain('/tmp/gut-worktrees/workflow-deploy-batch');
|
|
57
|
-
expect(output).toContain('/tmp/gut-worktrees/workflow-story-042');
|
|
58
|
-
expect(output).toContain('2026-03-19 13:22');
|
|
59
|
-
expect(output).toContain('2026-03-19 14:05');
|
|
60
|
-
expect(output).toContain('api, sign');
|
|
61
|
-
expect(output).toContain('api');
|
|
62
|
-
expect(output).not.toContain('[stale]');
|
|
63
|
-
});
|
|
64
|
-
it('displays empty state message when no worktrees exist', async () => {
|
|
65
|
-
const logs = [];
|
|
66
|
-
const { default: WorktreeList } = await import('./list.js');
|
|
67
|
-
const cmd = Object.create(WorktreeList.prototype);
|
|
68
|
-
cmd.log = (msg = '') => { logs.push(msg); };
|
|
69
|
-
cmd.worktreeService = { findStale: () => [], list: () => [] };
|
|
70
|
-
await cmd.run();
|
|
71
|
-
const output = logs.join('\n');
|
|
72
|
-
expect(output).toContain('No active worktrees');
|
|
73
|
-
expect(output).toContain('gut worktree create <branch-name>');
|
|
74
|
-
expect(output).not.toContain('Active worktrees:');
|
|
75
|
-
});
|
|
76
|
-
it('shows [stale] marker when worktree path does not exist on disk', async () => {
|
|
77
|
-
const record = makeRecord({
|
|
78
|
-
path: '/tmp/gut-worktrees/nonexistent-worktree',
|
|
79
|
-
});
|
|
80
|
-
const logs = [];
|
|
81
|
-
const { default: WorktreeList } = await import('./list.js');
|
|
82
|
-
const cmd = Object.create(WorktreeList.prototype);
|
|
83
|
-
cmd.log = (msg = '') => { logs.push(msg); };
|
|
84
|
-
cmd.worktreeService = { findStale: () => [record], list: () => [record] };
|
|
85
|
-
vi.mocked(fs.existsSync).mockReturnValue(false);
|
|
86
|
-
await cmd.run();
|
|
87
|
-
const output = logs.join('\n');
|
|
88
|
-
expect(output).toContain('[stale]');
|
|
89
|
-
expect(output).toContain('/tmp/gut-worktrees/nonexistent-worktree');
|
|
90
|
-
});
|
|
91
|
-
describe('stale warning block (Story 2.1)', () => {
|
|
92
|
-
it('prints warning block before the list when findStale returns records', async () => {
|
|
93
|
-
const staleA = makeRecord({ name: 'workflow/dead-a', path: '/tmp/gone-a' });
|
|
94
|
-
const staleB = makeRecord({ name: 'workflow/dead-b', path: '/tmp/gone-b' });
|
|
95
|
-
const alive = makeRecord({ name: 'workflow/alive', path: '/tmp/alive' });
|
|
96
|
-
const logs = [];
|
|
97
|
-
const { default: WorktreeList } = await import('./list.js');
|
|
98
|
-
const cmd = Object.create(WorktreeList.prototype);
|
|
99
|
-
cmd.log = (msg = '') => { logs.push(msg); };
|
|
100
|
-
cmd.worktreeService = {
|
|
101
|
-
findStale: () => [staleA, staleB],
|
|
102
|
-
list: () => [staleA, staleB, alive],
|
|
103
|
-
};
|
|
104
|
-
vi.mocked(fs.existsSync).mockImplementation((p) => p === '/tmp/alive');
|
|
105
|
-
await cmd.run();
|
|
106
|
-
const warningIdx = logs.findIndex(l => l.includes('⚠️ Stale worktree records detected (path does not exist on disk):'));
|
|
107
|
-
const activeIdx = logs.findIndex(l => l.includes('Active worktrees:'));
|
|
108
|
-
expect(warningIdx).toBeGreaterThanOrEqual(0);
|
|
109
|
-
expect(warningIdx).toBeLessThan(activeIdx);
|
|
110
|
-
expect(logs).toContain(' - workflow/dead-a /tmp/gone-a');
|
|
111
|
-
expect(logs).toContain(' - workflow/dead-b /tmp/gone-b');
|
|
112
|
-
expect(logs).toContain('Run `gut worktree prune` to remove them.');
|
|
113
|
-
});
|
|
114
|
-
it('omits the warning block entirely when findStale returns empty', async () => {
|
|
115
|
-
const alive = makeRecord({ name: 'workflow/alive', path: '/tmp/alive' });
|
|
116
|
-
const logs = [];
|
|
117
|
-
const { default: WorktreeList } = await import('./list.js');
|
|
118
|
-
const cmd = Object.create(WorktreeList.prototype);
|
|
119
|
-
cmd.log = (msg = '') => { logs.push(msg); };
|
|
120
|
-
cmd.worktreeService = { findStale: () => [], list: () => [alive] };
|
|
121
|
-
vi.mocked(fs.existsSync).mockReturnValue(true);
|
|
122
|
-
await cmd.run();
|
|
123
|
-
const output = logs.join('\n');
|
|
124
|
-
expect(output).not.toContain('⚠️');
|
|
125
|
-
expect(output).not.toContain('Stale worktree records detected');
|
|
126
|
-
expect(output).not.toContain('gut worktree prune');
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
describe('formatWorktreeAge (Story 3.1)', () => {
|
|
130
|
-
const NOW = new Date('2026-04-11T12:00:00Z').getTime();
|
|
131
|
-
it('returns "less than a day old", not stale, when created right now', () => {
|
|
132
|
-
const result = formatWorktreeAge(NOW, NOW);
|
|
133
|
-
expect(result.text).toContain('less than a day old');
|
|
134
|
-
expect(result.isStale).toBe(false);
|
|
135
|
-
});
|
|
136
|
-
it('returns "1 day old" at exactly 1 day', () => {
|
|
137
|
-
const result = formatWorktreeAge(NOW - MS_PER_DAY, NOW);
|
|
138
|
-
expect(result.text).toContain('1 day old');
|
|
139
|
-
expect(result.isStale).toBe(false);
|
|
140
|
-
});
|
|
141
|
-
it('returns "3 days old", not stale, at 3 days', () => {
|
|
142
|
-
const result = formatWorktreeAge(NOW - 3 * MS_PER_DAY, NOW);
|
|
143
|
-
expect(result.text).toContain('3 days old');
|
|
144
|
-
expect(result.isStale).toBe(false);
|
|
145
|
-
});
|
|
146
|
-
it('is NOT stale at exactly 7 days (strict >)', () => {
|
|
147
|
-
const result = formatWorktreeAge(NOW - 7 * MS_PER_DAY, NOW);
|
|
148
|
-
expect(result.isStale).toBe(false);
|
|
149
|
-
expect(result.text).toContain('7 days old');
|
|
150
|
-
});
|
|
151
|
-
it('IS stale at 7 days + 1ms', () => {
|
|
152
|
-
const result = formatWorktreeAge(NOW - (7 * MS_PER_DAY + 1), NOW);
|
|
153
|
-
expect(result.isStale).toBe(true);
|
|
154
|
-
});
|
|
155
|
-
it('returns "30 days old" and is stale at 30 days', () => {
|
|
156
|
-
const result = formatWorktreeAge(NOW - 30 * MS_PER_DAY, NOW);
|
|
157
|
-
expect(result.text).toContain('30 days old');
|
|
158
|
-
expect(result.isStale).toBe(true);
|
|
159
|
-
});
|
|
160
|
-
it('handles future createdAt (clock skew) as "less than a day old", not stale', () => {
|
|
161
|
-
const result = formatWorktreeAge(NOW + 5 * MS_PER_DAY, NOW);
|
|
162
|
-
expect(result.text).toContain('less than a day old');
|
|
163
|
-
expect(result.isStale).toBe(false);
|
|
164
|
-
});
|
|
165
|
-
it('handles NaN createdAt as "unknown age", not stale', () => {
|
|
166
|
-
const result = formatWorktreeAge(Number.NaN, NOW);
|
|
167
|
-
expect(result.text).toContain('unknown age');
|
|
168
|
-
expect(result.isStale).toBe(false);
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
it('handles missing worktrees.json (first-run) — same as empty state', async () => {
|
|
172
|
-
const logs = [];
|
|
173
|
-
const { default: WorktreeList } = await import('./list.js');
|
|
174
|
-
const cmd = Object.create(WorktreeList.prototype);
|
|
175
|
-
cmd.log = (msg = '') => { logs.push(msg); };
|
|
176
|
-
cmd.worktreeService = { findStale: () => [], list: () => [] };
|
|
177
|
-
await cmd.run();
|
|
178
|
-
const output = logs.join('\n');
|
|
179
|
-
expect(output).toContain('No active worktrees');
|
|
180
|
-
expect(output).toContain('gut worktree create <branch-name>');
|
|
181
|
-
});
|
|
182
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import { BaseCommand } from '../../base-command.js';
|
|
4
|
-
export default class WorktreePrune extends BaseCommand {
|
|
5
|
-
static description = 'Remove worktree records whose paths no longer exist on disk';
|
|
6
|
-
static examples = ['<%= config.bin %> worktree prune'];
|
|
7
|
-
async run() {
|
|
8
|
-
const state = this.worktreeService.loadState();
|
|
9
|
-
const stale = [];
|
|
10
|
-
const kept = [];
|
|
11
|
-
for (const w of state.worktrees) {
|
|
12
|
-
if (fs.existsSync(w.path))
|
|
13
|
-
kept.push(w);
|
|
14
|
-
else
|
|
15
|
-
stale.push(w);
|
|
16
|
-
}
|
|
17
|
-
if (stale.length === 0) {
|
|
18
|
-
this.log(chalk.dim('No stale worktree records found'));
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
for (const record of stale) {
|
|
22
|
-
this.log(` ${chalk.yellow('✗')} ${record.name} ${chalk.dim('→')} ${record.path}`);
|
|
23
|
-
}
|
|
24
|
-
this.worktreeService.saveState({ worktrees: kept });
|
|
25
|
-
await this.gitService.worktreePrune(this.configService.getWorkspaceRoot());
|
|
26
|
-
this.log(chalk.green(`✓ Pruned ${stale.length} stale worktree record(s)`));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|