Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chaperon with worktree #147

Merged
merged 5 commits into from
Sep 24, 2024
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ jobs:
- run: git config --global core.autocrlf false
if: runner.os == 'Windows'

- run: |
git config --global user.email "github-action@example.com"
git config --global user.name "GitHub Action"
git version

- uses: actions/checkout@v4

- uses: denoland/setup-deno@v1.1.4
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"tasks": {
"check": "deno check ./**/*.ts",
"test": "deno test -A --parallel --shuffle --doc",
"test": "deno test -A --shuffle --doc",
"test:coverage": "deno task test --coverage=.coverage",
"coverage": "deno coverage .coverage",
"update": "deno run --allow-env --allow-read --allow-write --allow-run=git,deno --allow-net=jsr.io,registry.npmjs.org jsr:@molt/cli **/*.ts",
Expand Down
4 changes: 3 additions & 1 deletion denops/gin/command/chaperon/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "jsr:@std/fs@^1.0.0";
import * as path from "jsr:@std/path@^1.0.0";
import { findGitdir } from "../../git/finder.ts";

const beginMarker = `${"<".repeat(7)} `;
const endMarker = `${">".repeat(7)} `;
Expand Down Expand Up @@ -29,8 +30,9 @@
export async function getInProgressAliasHead(
worktree: string,
): Promise<AliasHead | undefined> {
const gitdir = await findGitdir(worktree);

Check warning on line 33 in denops/gin/command/chaperon/util.ts

View check run for this annotation

Codecov / codecov/patch

denops/gin/command/chaperon/util.ts#L33

Added line #L33 was not covered by tests
for (const head of validAliasHeads) {
if (await fs.exists(path.join(worktree, ".git", head))) {
if (await fs.exists(path.join(gitdir, head))) {

Check warning on line 35 in denops/gin/command/chaperon/util.ts

View check run for this annotation

Codecov / codecov/patch

denops/gin/command/chaperon/util.ts#L35

Added line #L35 was not covered by tests
return head;
}
}
Expand Down
4 changes: 2 additions & 2 deletions denops/gin/component/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Denops } from "jsr:@denops/std@^7.0.0";
import { Cache } from "jsr:@lambdalisue/ttl-cache@^1.0.0";
import * as path from "jsr:@std/path@^1.0.0";
import { findWorktreeFromDenops } from "../git/worktree.ts";
import { find } from "../git/finder.ts";
import { findWorktree } from "../git/finder.ts";

type Data = string;

Expand All @@ -13,7 +13,7 @@ async function getData(
): Promise<Data> {
return cache.get("data") ?? await (async () => {
const worktree = await findWorktreeFromDenops(denops);
const result = await find(worktree);
const result = await findWorktree(worktree);
cache.set("data", result);
return result;
})();
Expand Down
58 changes: 42 additions & 16 deletions denops/gin/git/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@ import { execute } from "./process.ts";
import { decodeUtf8 } from "../util/text.ts";

lambdalisue marked this conversation as resolved.
Show resolved Hide resolved
const ttl = 30000; // seconds
const cache = new Cache<string, string | Error>(ttl);
const cacheWorktree = new Cache<string, string | Error>(ttl);
const cacheGitdir = new Cache<string, string | Error>(ttl);

export async function find(cwd: string): Promise<string> {
const result = cache.get(cwd) ?? await (async () => {
/**
* Find a root path of a git working directory.
*
* @param cwd - A current working directory.
* @returns A root path of a git working directory.
*/
export async function findWorktree(cwd: string): Promise<string> {
const path = await Deno.realPath(cwd);
const result = cacheWorktree.get(path) ?? await (async () => {
let result: string | Error;
try {
result = await findInternal(cwd);
result = await revParse(path, [
"--show-toplevel",
"--show-superproject-working-tree",
]);
} catch (e) {
result = e;
}
cache.set(cwd, result);
cacheWorktree.set(path, result);
lambdalisue marked this conversation as resolved.
Show resolved Hide resolved
return result;
})();
if (result instanceof Error) {
Expand All @@ -23,24 +34,39 @@ export async function find(cwd: string): Promise<string> {
return result;
}

async function findInternal(cwd: string): Promise<string> {
/**
* Find a .git directory of a git working directory.
*
* @param cwd - A current working directory.
* @returns A root path of a git working directory.
*/
export async function findGitdir(cwd: string): Promise<string> {
const path = await Deno.realPath(cwd);
const result = cacheGitdir.get(path) ?? await (async () => {
let result: string | Error;
try {
result = await revParse(path, ["--git-dir"]);
} catch (e) {
result = e;
}
cacheGitdir.set(path, result);
return result;
})();
lambdalisue marked this conversation as resolved.
Show resolved Hide resolved
if (result instanceof Error) {
throw result;
}
return result;
}

async function revParse(cwd: string, args: string[]): Promise<string> {
const terms = cwd.split(SEPARATOR);
if (terms.includes(".git")) {
// `git rev-parse` does not work in a ".git" directory
// so use a parent directory of it instead.
const index = terms.indexOf(".git");
cwd = terms.slice(0, index).join(SEPARATOR);
}
const stdout = await execute(
[
"rev-parse",
"--show-toplevel",
"--show-superproject-working-tree",
],
{
cwd,
},
);
const stdout = await execute(["rev-parse", ...args], { cwd });
lambdalisue marked this conversation as resolved.
Show resolved Hide resolved
const output = decodeUtf8(stdout);
return resolve(output.split(/\n/, 2)[0]);
}
81 changes: 70 additions & 11 deletions denops/gin/git/finder_test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,93 @@
import { assertEquals, assertRejects } from "jsr:@std/assert@^1.0.0";
import * as path from "jsr:@std/path@^1.0.0";
import { find } from "./finder.ts";
import { sandbox } from "jsr:@lambdalisue/sandbox@^2.0.0";
import $ from "jsr:@david/dax@^0.42.0";
import { join } from "jsr:@std/path@^1.0.0";
import { findGitdir, findWorktree } from "./finder.ts";
import { ExecuteError } from "./process.ts";

Deno.test({
name: "find() returns a root path of a git working directory",
name: "findWorktree() returns a root path of a git working directory",
fn: async () => {
const exp = path.resolve(
path.fromFileUrl(import.meta.url),
"../../../../",
await using sbox = await prepare();
Deno.chdir(join("a", "b", "c"));
assertEquals(await findWorktree("."), sbox.path);
// An internal cache will be used for the following call
assertEquals(await findWorktree("."), sbox.path);
},
sanitizeResources: false,
sanitizeOps: false,
});

Deno.test({
name:
"findWorktree() throws an error if the path is not in a git working directory",
fn: async () => {
await assertRejects(async () => {
await findWorktree("/");
}, ExecuteError);
// An internal cache will be used for the following call
await assertRejects(async () => {
await findWorktree("/");
}, ExecuteError);
},
sanitizeResources: false,
sanitizeOps: false,
});

Deno.test({
name: "findGitdir() returns a root path of a git working directory",
fn: async () => {
await using sbox = await prepare();
Deno.chdir(join("a", "b", "c"));
assertEquals(await findGitdir("."), join(sbox.path, ".git"));
// An internal cache will be used for the following call
assertEquals(await findGitdir("."), join(sbox.path, ".git"));
},
sanitizeResources: false,
sanitizeOps: false,
});

Deno.test({
name: "findGitdir() returns a worktree path of a git working directory",
fn: async () => {
await using sbox = await prepare();
await $`git worktree add -b test test main`;
Deno.chdir("test");
lambdalisue marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(
await findGitdir("."),
join(sbox.path, ".git", "worktrees", "test"),
);
assertEquals(await find("."), exp);
// An internal cache will be used for the following call
assertEquals(await find("."), exp);
assertEquals(
await findGitdir("."),
join(sbox.path, ".git", "worktrees", "test"),
);
},
sanitizeResources: false,
sanitizeOps: false,
});

Deno.test({
name: "find() throws an error if the path is not in a git working directory",
name:
"findGitdir() throws an error if the path is not in a git working directory",
fn: async () => {
await assertRejects(async () => {
await find("/");
await findGitdir("/");
}, ExecuteError);
// An internal cache will be used for the following call
await assertRejects(async () => {
await find("/");
await findGitdir("/");
}, ExecuteError);
},
sanitizeResources: false,
sanitizeOps: false,
});

async function prepare(): ReturnType<typeof sandbox> {
const sbox = await sandbox();
await $`git init`;
await $`git commit --allow-empty -m 'Initial commit' --no-gpg-sign`;
await $`git switch -c main`;
await Deno.mkdir(join("a", "b", "c"), { recursive: true });
return sbox;
}
4 changes: 2 additions & 2 deletions denops/gin/git/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as path from "jsr:@std/path@^1.0.0";
import { GIN_BUFFER_PROTOCOLS } from "../global.ts";
import { expand } from "../util/expand.ts";
import { find } from "./finder.ts";
import { findWorktree } from "./finder.ts";

/**
* Find a git worktree from a suspected directory
Expand All @@ -32,7 +32,7 @@
console.debug(`Trying to find a git repository from '${c}'`);
}
try {
return await find(c);
return await findWorktree(c);

Check warning on line 35 in denops/gin/git/worktree.ts

View check run for this annotation

Codecov / codecov/patch

denops/gin/git/worktree.ts#L35

Added line #L35 was not covered by tests
} catch {
// Fail silently
}
Expand Down