Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/git/src/worktree-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { generateHumanReadableName } from "./worktree-name";

describe("generateHumanReadableName", () => {
it("returns a string matching adjective-noun-NN", () => {
const name = generateHumanReadableName();
expect(name).toMatch(/^[a-z]+-[a-z]+-\d{2}$/);
});

it("produces varied names over multiple calls", () => {
const names = new Set<string>();
for (let i = 0; i < 50; i++) {
names.add(generateHumanReadableName());
}
// With 36 * 36 * 90 = ~116k combinations, 50 draws should yield
// many unique values. Allow generous slack for randomness.
expect(names.size).toBeGreaterThan(20);
});

it("uses only filesystem-safe characters", () => {
for (let i = 0; i < 25; i++) {
const name = generateHumanReadableName();
expect(name).toMatch(/^[a-z0-9-]+$/);
}
});

it("stays compact (under 32 chars)", () => {
for (let i = 0; i < 25; i++) {
expect(generateHumanReadableName().length).toBeLessThanOrEqual(32);
}
});
Comment on lines +10 to +31
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The team's standard is to prefer parameterised tests over manual loops. The three loop-based cases ("varies", "filesystem-safe", "compact") can each be expressed as it.each calls, making each individual sample a named test case and avoiding an imperative loop that obscures the intent.

Suggested change
it("produces varied names over multiple calls", () => {
const names = new Set<string>();
for (let i = 0; i < 50; i++) {
names.add(generateHumanReadableName());
}
// With 36 * 36 * 90 = ~116k combinations, 50 draws should yield
// many unique values. Allow generous slack for randomness.
expect(names.size).toBeGreaterThan(20);
});
it("uses only filesystem-safe characters", () => {
for (let i = 0; i < 25; i++) {
const name = generateHumanReadableName();
expect(name).toMatch(/^[a-z0-9-]+$/);
}
});
it("stays compact (under 32 chars)", () => {
for (let i = 0; i < 25; i++) {
expect(generateHumanReadableName().length).toBeLessThanOrEqual(32);
}
});
it("produces varied names over multiple calls", () => {
const names = new Set(Array.from({ length: 50 }, generateHumanReadableName));
// With 36 * 36 * 90 = ~116k combinations, 50 draws should yield
// many unique values. Allow generous slack for randomness.
expect(names.size).toBeGreaterThan(20);
});
it.each(Array.from({ length: 25 }, generateHumanReadableName))(
"uses only filesystem-safe characters: %s",
(name) => {
expect(name).toMatch(/^[a-z0-9-]+$/);
},
);
it.each(Array.from({ length: 25 }, generateHumanReadableName))(
"stays compact (under 32 chars): %s",
(name) => {
expect(name.length).toBeLessThanOrEqual(32);
},
);

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/git/src/worktree-name.test.ts
Line: 10-31

Comment:
The team's standard is to prefer parameterised tests over manual loops. The three loop-based cases ("varies", "filesystem-safe", "compact") can each be expressed as `it.each` calls, making each individual sample a named test case and avoiding an imperative loop that obscures the intent.

```suggestion
  it("produces varied names over multiple calls", () => {
    const names = new Set(Array.from({ length: 50 }, generateHumanReadableName));
    // With 36 * 36 * 90 = ~116k combinations, 50 draws should yield
    // many unique values. Allow generous slack for randomness.
    expect(names.size).toBeGreaterThan(20);
  });

  it.each(Array.from({ length: 25 }, generateHumanReadableName))(
    "uses only filesystem-safe characters: %s",
    (name) => {
      expect(name).toMatch(/^[a-z0-9-]+$/);
    },
  );

  it.each(Array.from({ length: 25 }, generateHumanReadableName))(
    "stays compact (under 32 chars): %s",
    (name) => {
      expect(name.length).toBeLessThanOrEqual(32);
    },
  );
```

**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

});
34 changes: 34 additions & 0 deletions packages/git/src/worktree-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as crypto from "node:crypto";

/**
* Curated adjective-noun word list used to label worktrees. Words are short,
* filesystem-safe (lowercase a-z only), and chosen to be inoffensive.
*/
// biome-ignore format: keep word lists compact
const ADJECTIVES = [
"amber", "brave", "calm", "clever", "cosmic", "crisp", "dapper", "dusty",
"eager", "fancy", "fluffy", "gentle", "happy", "jolly", "lively", "lucky",
"merry", "mighty", "nimble", "plucky", "proud", "quick", "quiet", "rapid",
"shiny", "silver", "smooth", "snappy", "spry", "sturdy", "sunny", "swift",
"tidy", "vivid", "witty", "zesty",
];

// biome-ignore format: keep word lists compact
const NOUNS = [
"badger", "beetle", "bison", "cedar", "comet", "cricket", "delta", "ember",
"falcon", "ferret", "finch", "fjord", "glade", "harbor", "heron", "ibex",
"lemur", "lynx", "marlin", "meadow", "mountain", "otter", "panda", "petal",
"pebble", "puffin", "quokka", "raven", "river", "robin", "sparrow", "summit",
"tiger", "valley", "willow", "wombat",
];

/**
* Generates a short, human-readable random name (e.g. "swift-otter-42").
* Suffix is a 2-digit number to reduce collisions while keeping names compact.
*/
export function generateHumanReadableName(): string {
const adjective = ADJECTIVES[crypto.randomInt(0, ADJECTIVES.length)];
const noun = NOUNS[crypto.randomInt(0, NOUNS.length)];
const suffix = crypto.randomInt(10, 100).toString();
return `${adjective}-${noun}-${suffix}`;
}
4 changes: 2 additions & 2 deletions packages/git/src/worktree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { execFile, spawn } from "node:child_process";
import * as crypto from "node:crypto";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { getCleanEnv, getGitOperationManager } from "./operation-manager";
Expand All @@ -11,6 +10,7 @@ import {
listWorktrees as listWorktreesRaw,
} from "./queries";
import { clonePath, forceRemove, safeSymlink } from "./utils";
import { generateHumanReadableName } from "./worktree-name";

export interface WorktreeInfo {
worktreePath: string;
Expand Down Expand Up @@ -44,7 +44,7 @@ export class WorktreeManager {
}

generateWorktreeName(): string {
return crypto.randomInt(1000, 10000).toString();
return generateHumanReadableName();
}

private getWorktreeBaseFolderPath(): string {
Expand Down