Repository navigation
create commit in change tab #409
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f081bba
merge main
Kitenite 2cfec24
update git ops
Kitenite 872a35d
feat(desktop): add VSCode-style commit/push/pull UI in Changes tab
Kitenite a7f376e
remove redundant comments
Kitenite cf05921
add back some comments
Kitenite cb73ba2
deslop
Kitenite 913eebf
fix tests
Kitenite 1ff472a
fix traversal
Kitenite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
apps/desktop/src/lib/trpc/routers/changes/git-operations.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { isUpstreamMissingError } from "./git-utils"; | ||
|
|
||
| describe("git-operations error handling", () => { | ||
| describe("isUpstreamMissingError", () => { | ||
| const upstreamDeletedMessages = [ | ||
| "Your configuration specifies to merge with the ref 'refs/heads/feature-branch' from the remote, but no such ref was fetched.", | ||
| "fatal: couldn't find remote ref refs/heads/deleted-branch", | ||
| "There is no tracking information for the current branch", | ||
| ]; | ||
|
|
||
| const otherErrorMessages = [ | ||
| "fatal: not a git repository", | ||
| "error: failed to push some refs", | ||
| "CONFLICT (content): Merge conflict in file.ts", | ||
| "fatal: refusing to merge unrelated histories", | ||
| ]; | ||
|
|
||
| test("detects upstream deleted errors", () => { | ||
| for (const message of upstreamDeletedMessages) { | ||
| expect(isUpstreamMissingError(message)).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| test("does not falsely detect other errors as upstream deleted", () => { | ||
| for (const message of otherErrorMessages) { | ||
| expect(isUpstreamMissingError(message)).toBe(false); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("error message patterns", () => { | ||
| test("commit with no staged changes", () => { | ||
| const message = "nothing to commit, working tree clean"; | ||
| expect(message.includes("nothing to commit")).toBe(true); | ||
| }); | ||
|
|
||
| test("push rejected - needs pull first", () => { | ||
| const message = | ||
| "error: failed to push some refs to 'origin'\nhint: Updates were rejected because the remote contains work"; | ||
| expect(message.includes("failed to push")).toBe(true); | ||
| expect(message.includes("rejected")).toBe(true); | ||
| }); | ||
|
|
||
| test("push rejected - no permission", () => { | ||
| const message = "remote: Permission to user/repo.git denied to otheruser"; | ||
| expect(message.includes("Permission")).toBe(true); | ||
| expect(message.includes("denied")).toBe(true); | ||
| }); | ||
|
|
||
| test("merge conflict during pull", () => { | ||
| const message = | ||
| "CONFLICT (content): Merge conflict in src/file.ts\nAutomatic merge failed"; | ||
| expect(message.includes("CONFLICT")).toBe(true); | ||
| expect(message.includes("Merge conflict")).toBe(true); | ||
| }); | ||
|
|
||
| test("detached HEAD state", () => { | ||
| const message = "HEAD detached at abc1234"; | ||
| expect(message.includes("detached")).toBe(true); | ||
| }); | ||
|
|
||
| test("no remote configured", () => { | ||
| const message = "fatal: 'origin' does not appear to be a git repository"; | ||
| expect(message.includes("does not appear to be a git repository")).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("sync operation logic", () => { | ||
| test("should push with set-upstream when pull fails due to deleted upstream", () => { | ||
| // This tests the logic flow: | ||
| // 1. Pull fails with "no such ref was fetched" | ||
| // 2. Should fall back to push with --set-upstream | ||
|
|
||
| const pullError = new Error( | ||
| "Your configuration specifies to merge with the ref 'refs/heads/feature' from the remote, but no such ref was fetched.", | ||
| ); | ||
|
|
||
| expect(isUpstreamMissingError(pullError.message)).toBe(true); | ||
| }); | ||
|
|
||
| test("should re-throw other pull errors", () => { | ||
| const pullError = new Error( | ||
| "CONFLICT (content): Merge conflict in file.ts", | ||
| ); | ||
|
|
||
| expect(isUpstreamMissingError(pullError.message)).toBe(false); | ||
| }); | ||
| }); | ||
169 changes: 169 additions & 0 deletions
169
apps/desktop/src/lib/trpc/routers/changes/git-operations.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { writeFile } from "node:fs/promises"; | ||
| import { resolve } from "node:path"; | ||
| import { shell } from "electron"; | ||
| import simpleGit from "simple-git"; | ||
| import { z } from "zod"; | ||
| import { publicProcedure, router } from "../.."; | ||
| import { isUpstreamMissingError } from "./git-utils"; | ||
|
|
||
| export { isUpstreamMissingError }; | ||
|
|
||
| async function hasUpstreamBranch( | ||
| git: ReturnType<typeof simpleGit>, | ||
| ): Promise<boolean> { | ||
| try { | ||
| await git.raw(["rev-parse", "--abbrev-ref", "@{upstream}"]); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export const createGitOperationsRouter = () => { | ||
| return router({ | ||
| saveFile: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| worktreePath: z.string(), | ||
| filePath: z.string(), | ||
| content: z.string(), | ||
| }), | ||
| ) | ||
| .mutation(async ({ input }): Promise<{ success: boolean }> => { | ||
| const resolvedWorktree = resolve(input.worktreePath); | ||
| const fullPath = resolve(resolvedWorktree, input.filePath); | ||
|
|
||
| if (!fullPath.startsWith(`${resolvedWorktree}/`)) { | ||
| throw new Error("Invalid file path: path traversal detected"); | ||
| } | ||
|
|
||
| await writeFile(fullPath, input.content, "utf-8"); | ||
| return { success: true }; | ||
| }), | ||
|
Kitenite marked this conversation as resolved.
Resolved
|
||
|
|
||
| commit: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| worktreePath: z.string(), | ||
| message: z.string(), | ||
| }), | ||
| ) | ||
| .mutation( | ||
| async ({ input }): Promise<{ success: boolean; hash: string }> => { | ||
| const git = simpleGit(input.worktreePath); | ||
| const result = await git.commit(input.message); | ||
| return { success: true, hash: result.commit }; | ||
| }, | ||
| ), | ||
|
|
||
| push: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| worktreePath: z.string(), | ||
| setUpstream: z.boolean().optional(), | ||
| }), | ||
| ) | ||
| .mutation(async ({ input }): Promise<{ success: boolean }> => { | ||
| const git = simpleGit(input.worktreePath); | ||
| const hasUpstream = await hasUpstreamBranch(git); | ||
|
|
||
| if (input.setUpstream && !hasUpstream) { | ||
| const branch = await git.revparse(["--abbrev-ref", "HEAD"]); | ||
| await git.push(["--set-upstream", "origin", branch.trim()]); | ||
| } else { | ||
| await git.push(); | ||
| } | ||
| await git.fetch(); | ||
| return { success: true }; | ||
| }), | ||
|
|
||
| pull: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| worktreePath: z.string(), | ||
| }), | ||
| ) | ||
| .mutation(async ({ input }): Promise<{ success: boolean }> => { | ||
| const git = simpleGit(input.worktreePath); | ||
| try { | ||
| await git.pull(["--rebase"]); | ||
| } catch (error) { | ||
| const message = | ||
| error instanceof Error ? error.message : String(error); | ||
| if (isUpstreamMissingError(message)) { | ||
| throw new Error( | ||
| "No upstream branch to pull from. The remote branch may have been deleted.", | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| return { success: true }; | ||
| }), | ||
|
|
||
| sync: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| worktreePath: z.string(), | ||
| }), | ||
| ) | ||
| .mutation(async ({ input }): Promise<{ success: boolean }> => { | ||
| const git = simpleGit(input.worktreePath); | ||
| try { | ||
| await git.pull(["--rebase"]); | ||
| } catch (error) { | ||
| const message = | ||
| error instanceof Error ? error.message : String(error); | ||
| if (isUpstreamMissingError(message)) { | ||
| const branch = await git.revparse(["--abbrev-ref", "HEAD"]); | ||
| await git.push(["--set-upstream", "origin", branch.trim()]); | ||
| await git.fetch(); | ||
| return { success: true }; | ||
| } | ||
| throw error; | ||
| } | ||
| await git.push(); | ||
| await git.fetch(); | ||
| return { success: true }; | ||
| }), | ||
|
|
||
| createPR: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| worktreePath: z.string(), | ||
| }), | ||
| ) | ||
| .mutation( | ||
| async ({ input }): Promise<{ success: boolean; url: string }> => { | ||
| const git = simpleGit(input.worktreePath); | ||
| const branch = (await git.revparse(["--abbrev-ref", "HEAD"])).trim(); | ||
| const hasUpstream = await hasUpstreamBranch(git); | ||
|
|
||
| // Ensure branch is pushed first | ||
| if (!hasUpstream) { | ||
| await git.push(["--set-upstream", "origin", branch]); | ||
| } else { | ||
| // Push any unpushed commits | ||
| await git.push(); | ||
| } | ||
|
|
||
| // Get the remote URL to construct the GitHub compare URL | ||
| const remoteUrl = (await git.remote(["get-url", "origin"])) || ""; | ||
| const repoMatch = remoteUrl | ||
| .trim() | ||
| .match(/github\.com[:/](.+?)(?:\.git)?$/); | ||
|
|
||
| if (!repoMatch) { | ||
| throw new Error("Could not determine GitHub repository URL"); | ||
| } | ||
|
|
||
| const repo = repoMatch[1].replace(/\.git$/, ""); | ||
| const url = `https://github.com/${repo}/compare/${branch}?expand=1`; | ||
|
|
||
| await shell.openExternal(url); | ||
| await git.fetch(); | ||
|
|
||
| return { success: true, url }; | ||
| }, | ||
| ), | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /** | ||
| * Check if the error message indicates the upstream branch is missing/deleted | ||
| */ | ||
| export function isUpstreamMissingError(message: string): boolean { | ||
| return ( | ||
| message.includes("no such ref was fetched") || | ||
| message.includes("no tracking information") || | ||
| message.includes("couldn't find remote ref") | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Remove trivial string-matching tests.
These tests check obvious string containment (e.g., verifying that
"nothing to commit, working tree clean"includes"nothing to commit"). They don't test business logic or behavior and would always pass, providing no value.Consider removing this entire section or replacing with meaningful integration tests
If you want to test error handling for these scenarios, test the actual git operation procedures (commit, push, pull) with mocked git responses, rather than testing string literals.
Example:
Based on learnings, tests should be meaningful and test actual behavior rather than trivial assertions.
🤖 Prompt for AI Agents