Skip to content
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ release
*.local

# Editor directories and files
.vscode/.debug.env
.vscode/settings.json
.vscode
.idea
.DS_Store
*.suo
Expand Down
7 changes: 0 additions & 7 deletions apps/desktop/.vscode/extensions.json

This file was deleted.

35 changes: 0 additions & 35 deletions apps/desktop/.vscode/settings.json

This file was deleted.

159 changes: 159 additions & 0 deletions apps/desktop/src/lib/trpc/routers/changes/branches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { db } from "main/lib/db";
import simpleGit from "simple-git";
import { z } from "zod";
import { publicProcedure, router } from "../..";

export const createBranchesRouter = () => {
return router({
getBranches: publicProcedure
.input(z.object({ worktreePath: z.string() }))
.query(
async ({
input,
}): Promise<{
local: Array<{ branch: string; lastCommitDate: number }>;
remote: string[];
defaultBranch: string;
checkedOutBranches: Record<string, string>;
}> => {
const git = simpleGit(input.worktreePath);

const branchSummary = await git.branch(["-a"]);

const localBranches: string[] = [];
const remote: string[] = [];

for (const name of Object.keys(branchSummary.branches)) {
if (name.startsWith("remotes/origin/")) {
if (name === "remotes/origin/HEAD") continue;
const remoteName = name.replace("remotes/origin/", "");
remote.push(remoteName);
} else {
localBranches.push(name);
}
}

const local = await getLocalBranchesWithDates(git, localBranches);
const defaultBranch = await getDefaultBranch(git, remote);
const checkedOutBranches = await getCheckedOutBranches(
git,
input.worktreePath,
);

return {
local,
remote: remote.sort(),
defaultBranch,
checkedOutBranches,
};
},
),

switchBranch: publicProcedure
.input(
z.object({
worktreePath: z.string(),
branch: z.string(),
}),
)
.mutation(async ({ input }): Promise<{ success: boolean }> => {
const git = simpleGit(input.worktreePath);

const worktree = db.data.worktrees.find(
(wt) => wt.path === input.worktreePath,
);
if (!worktree) {
throw new Error(`No worktree found at path "${input.worktreePath}"`);
}

await git.checkout(input.branch);

await db.update((data) => {
const wt = data.worktrees.find((w) => w.path === input.worktreePath);
if (wt) {
wt.branch = input.branch;
if (wt.gitStatus) {
wt.gitStatus.branch = input.branch;
}
}
});

return { success: true };
}),
});
};

async function getLocalBranchesWithDates(
git: ReturnType<typeof simpleGit>,
localBranches: string[],
): Promise<Array<{ branch: string; lastCommitDate: number }>> {
try {
const branchInfo = await git.raw([
"for-each-ref",
"--sort=-committerdate",
"--format=%(refname:short) %(committerdate:unix)",
"refs/heads/",
]);

const local: Array<{ branch: string; lastCommitDate: number }> = [];
for (const line of branchInfo.trim().split("\n")) {
if (!line) continue;
const lastSpaceIdx = line.lastIndexOf(" ");
const branch = line.substring(0, lastSpaceIdx);
const timestamp = Number.parseInt(line.substring(lastSpaceIdx + 1), 10);
if (localBranches.includes(branch)) {
local.push({
branch,
lastCommitDate: timestamp * 1000,
});
}
}
return local;
} catch {
return localBranches.map((branch) => ({ branch, lastCommitDate: 0 }));
}
}

async function getDefaultBranch(
git: ReturnType<typeof simpleGit>,
remoteBranches: string[],
): Promise<string> {
try {
const headRef = await git.raw(["symbolic-ref", "refs/remotes/origin/HEAD"]);
const match = headRef.match(/refs\/remotes\/origin\/(.+)/);
if (match) {
return match[1].trim();
}
} catch {
if (remoteBranches.includes("master") && !remoteBranches.includes("main")) {
return "master";
}
}
return "main";
}

async function getCheckedOutBranches(
git: ReturnType<typeof simpleGit>,
currentWorktreePath: string,
): Promise<Record<string, string>> {
const checkedOutBranches: Record<string, string> = {};

try {
const worktreeList = await git.raw(["worktree", "list", "--porcelain"]);
const lines = worktreeList.split("\n");
let currentPath: string | null = null;

for (const line of lines) {
if (line.startsWith("worktree ")) {
currentPath = line.substring(9).trim();
} else if (line.startsWith("branch ")) {
const branch = line.substring(7).trim().replace("refs/heads/", "");
if (currentPath && currentPath !== currentWorktreePath) {
checkedOutBranches[branch] = currentPath;
}
}
}
} catch {}

return checkedOutBranches;
}
Loading