Skip to content

Commit

Permalink
fix(git): filter out detached state from branch list (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
tugrulates authored Feb 28, 2025
1 parent 3b2aee0 commit 65e1c6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/git/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ Deno.test("git().branches.list() returns all branches", async () => {
assertEquals(await repo.branches.list(), ["branch", "main"]);
});

Deno.test("git().branches.list() returns all branches in detached HEAD", async () => {
await using repo = await tempRepository({
config: { init: { defaultBranch: "main" } },
});
await repo.commits.create("commit", { allowEmpty: true });
await repo.branches.checkout({ detach: true });
await repo.branches.create("branch");
assertEquals(await repo.branches.list(), ["branch", "main"]);
});

Deno.test("git().branches.list() matches branch name", async () => {
await using repo = await tempRepository();
await repo.commits.create("commit", { allowEmpty: true });
Expand Down Expand Up @@ -306,11 +316,22 @@ Deno.test("git().branches.create() creates a branch", async () => {

Deno.test("git().branches.delete() fails on current branch", async () => {
await using repo = await tempRepository();
await repo.commits.create("commit", { allowEmpty: true });
const current = await repo.branches.current();
assert(current);
await assertRejects(() => repo.branches.delete(current), GitError);
});

Deno.test("git().branches.delete() can delete from detached HEAD", async () => {
await using repo = await tempRepository();
await repo.commits.create("commit", { allowEmpty: true });
const current = await repo.branches.current();
assert(current);
await repo.branches.checkout({ detach: true });
await repo.branches.delete(current);
assertEquals(await repo.branches.list(), []);
});

Deno.test("git().branches.delete() can delete branch", async () => {
await using repo = await tempRepository({
config: { init: { defaultBranch: "main" } },
Expand Down
1 change: 1 addition & 0 deletions core/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ export function git(options?: GitOptions): Git {
.split("\n")
.filter((x) => x)
.filter((x) => basename(x) !== "HEAD")
.filter((x) => !x.includes(" "))
.map((x) => x.replace(/^refs\/heads\//, ""))
.map((x) => x.replace(/^refs\/remotes\//, ""));
},
Expand Down

0 comments on commit 65e1c6d

Please sign in to comment.