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
5 changes: 5 additions & 0 deletions e2e/file-explorer-sidebar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ test('git sidebar pane shows no changes for a clean repository', async ({ create
await expect(worktree.locator('.worktrees-panel__worktree-name')).toContainText('git-pane-clean', {
timeout: 6000,
})

await worktree.locator('.worktrees-panel__worktree-header').click({ button: 'right' })
await expect(contextMenuItem(mainWindow, 'Pull from origin')).toBeVisible()
await mainWindow.keyboard.press('Escape')

await worktree.locator('.worktrees-panel__worktree-toggle').click()

await expect(worktree.locator('.git-panel__message')).toHaveText('No changes', { timeout: 6000 })
Expand Down
12 changes: 12 additions & 0 deletions electron/fileViewer/gitDiffService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ export class GitDiffService {
}
}

async pullWorktreeFromOrigin(rawWorktreePath: string): Promise<void> {
const cwd = await this.resolveGitCommandCwd(rawWorktreePath)
const { stdout } = await execFileAsync('git', ['symbolic-ref', '--quiet', '--short', 'HEAD'], { cwd })
const branch = stdout.trim()

if (!branch) {
throw new Error('Cannot pull into a detached HEAD worktree.')
}

await execFileAsync('git', ['pull', 'origin', branch], { cwd })
}

private async resolveGitCommandCwd(rawPath: string): Promise<string> {
const info = await this.fileBufferService.getFileInfo(rawPath)
return getGitWorkingDirectory(info.path, info.isDirectory)
Expand Down
4 changes: 4 additions & 0 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,10 @@ ipcMain.handle(
},
)

ipcMain.handle('fs:pull-git-worktree-from-origin', async (_event, payload: { worktreePath: string }) => {
await gitDiffService.pullWorktreeFromOrigin(payload.worktreePath)
})

ipcMain.handle('fs:rename', async (_event, { oldPath, newPath }: { oldPath: string; newPath: string }) => {
await rename(oldPath, newPath)
})
Expand Down
2 changes: 2 additions & 0 deletions electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ contextBridge.exposeInMainWorld('terminay', {
ipcRenderer.invoke('fs:move-git-worktree', payload) as Promise<void>,
removeGitWorktree: (payload: { force?: boolean; repoPath: string; worktreePath: string }) =>
ipcRenderer.invoke('fs:remove-git-worktree', payload) as Promise<void>,
pullGitWorktreeFromOrigin: (worktreePath: string) =>
ipcRenderer.invoke('fs:pull-git-worktree-from-origin', { worktreePath }) as Promise<void>,
getFileInfo: (filePath: string) => ipcRenderer.invoke('file:get-info', { path: filePath }) as Promise<FileViewerFileInfo>,
readFileBytes: (options: { path: string; start: number; length: number }) =>
ipcRenderer.invoke('file:read-bytes', options) as Promise<FileViewerByteRange>,
Expand Down
39 changes: 38 additions & 1 deletion scripts/git-worktree-status.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { execFile } from 'node:child_process'
import { lstat, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { lstat, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { basename, join, resolve } from 'node:path'
import { promisify } from 'node:util'
Expand Down Expand Up @@ -76,6 +76,43 @@ test('worktree status treats squash-merged commits as clean', async () => {
}
})

test('pulling a worktree from origin fast-forwards its current branch', async () => {
const { GitDiffService } = await importBundled('../electron/fileViewer/gitDiffService.ts')
const root = await mkdtemp(join(tmpdir(), 'terminay-git-worktree-pull-test-'))
const remote = join(root, 'remote.git')
const project = join(root, 'project')
const contributor = join(root, 'contributor')

try {
await git(['init', '--bare', remote], root)
await mkdir(project)
await git(['init', '-b', 'main'], project)
await git(['config', 'user.email', 'test@example.invalid'], project)
await git(['config', 'user.name', 'Terminay Test'], project)
await writeFile(join(project, 'shared.txt'), 'base\n')
await git(['add', 'shared.txt'], project)
await git(['commit', '-m', 'initial commit'], project)
await git(['remote', 'add', 'origin', remote], project)
await git(['push', '-u', 'origin', 'main'], project)
await git(['symbolic-ref', 'HEAD', 'refs/heads/main'], remote)

await git(['clone', remote, contributor], root)
await git(['config', 'user.email', 'contributor@example.invalid'], contributor)
await git(['config', 'user.name', 'Contributor'], contributor)
await writeFile(join(contributor, 'shared.txt'), 'updated upstream\n')
await git(['add', 'shared.txt'], contributor)
await git(['commit', '-m', 'upstream change'], contributor)
await git(['push', 'origin', 'main'], contributor)

const service = new GitDiffService(fileBufferStub)
await service.pullWorktreeFromOrigin(project)

assert.equal(await readFile(join(project, 'shared.txt'), 'utf8'), 'updated upstream\n')
} finally {
await rm(root, { recursive: true, force: true })
}
})

const fileBufferStub = {
async getFileInfo(rawPath) {
const normalizedPath = resolve(rawPath)
Expand Down
15 changes: 15 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3767,6 +3767,20 @@ const ProjectWorkspace = forwardRef<
[loadDirectory, project.rootFolder, refreshGitStatuses],
);

const handlePullWorktreeFromOrigin = useCallback(
async (worktree: GitWorktreeStatus) => {
try {
await window.terminay.pullGitWorktreeFromOrigin(worktree.path);
setErrorText(null);
} catch (error) {
setErrorText(`Failed to pull from origin: ${String(error)}`);
} finally {
refreshFileExplorerTree();
}
},
[refreshFileExplorerTree],
);

const handleRevealWorktree = useCallback((worktree: GitWorktreeStatus) => {
void window.terminay.revealInOS(worktree.path);
}, []);
Expand Down Expand Up @@ -7148,6 +7162,7 @@ const ProjectWorkspace = forwardRef<
onOpenPushMenu={handleOpenWorktreePushMenu}
onOpenTerminal={handleOpenTerminalAtWorktree}
onOpenTerminalAtPath={handleOpenTerminalAt}
onPullFromOrigin={handlePullWorktreeFromOrigin}
onRenameWorktree={handleRenameWorktree}
onRevealWorktree={handleRevealWorktree}
onSwitchProjectRoot={handleSwitchProjectRootToWorktree}
Expand Down
16 changes: 16 additions & 0 deletions src/components/git-panel/WorktreesPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ChevronDown,
Copy,
Download,
FileEdit,
FolderInput,
FolderOpen,
Expand Down Expand Up @@ -33,6 +34,7 @@ export type WorktreesPanelProps = {
) => void;
onOpenTerminal: (worktree: GitWorktreeStatus) => void;
onOpenTerminalAtPath: (path: string) => void;
onPullFromOrigin: (worktree: GitWorktreeStatus) => void;
onRenameWorktree: (worktree: GitWorktreeStatus) => void;
onRevealWorktree: (worktree: GitWorktreeStatus) => void;
onSwitchProjectRoot: (worktree: GitWorktreeStatus) => void;
Expand Down Expand Up @@ -97,6 +99,7 @@ export function WorktreesPanel(props: WorktreesPanelProps): JSX.Element {
onOpenPushMenu,
onOpenTerminal,
onOpenTerminalAtPath,
onPullFromOrigin,
onRenameWorktree,
onRevealWorktree,
onSwitchProjectRoot,
Expand Down Expand Up @@ -343,6 +346,7 @@ export function WorktreesPanel(props: WorktreesPanelProps): JSX.Element {
items={buildWorktreeContextMenuItems({
onDeleteWorktree,
onOpenTerminal,
onPullFromOrigin,
onRenameWorktree,
onRevealWorktree,
onSwitchProjectRoot,
Expand All @@ -358,6 +362,7 @@ export function WorktreesPanel(props: WorktreesPanelProps): JSX.Element {
function buildWorktreeContextMenuItems(options: {
onDeleteWorktree: (worktree: GitWorktreeStatus) => void;
onOpenTerminal: (worktree: GitWorktreeStatus) => void;
onPullFromOrigin: (worktree: GitWorktreeStatus) => void;
onRenameWorktree: (worktree: GitWorktreeStatus) => void;
onRevealWorktree: (worktree: GitWorktreeStatus) => void;
onSwitchProjectRoot: (worktree: GitWorktreeStatus) => void;
Expand All @@ -367,6 +372,7 @@ function buildWorktreeContextMenuItems(options: {
const {
onDeleteWorktree,
onOpenTerminal,
onPullFromOrigin,
onRenameWorktree,
onRevealWorktree,
onSwitchProjectRoot,
Expand All @@ -378,6 +384,16 @@ function buildWorktreeContextMenuItems(options: {
worktree.isCurrent || worktree.isMain || worktree.isBare;

return [
{
label: 'Pull from origin',
icon: <Download size={14} />,
disabled:
unavailable ||
worktree.isDetached ||
!worktree.branch ||
!!worktree.errorMessage,
onClick: () => onPullFromOrigin(worktree),
},
{
label: 'Switch project root',
icon: <FolderInput size={14} />,
Expand Down
1 change: 1 addition & 0 deletions src/types/terminay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ export interface TerminayApi {
getWorktreePanelStatus: (dirPath: string) => Promise<WorktreePanelStatus>
moveGitWorktree: (payload: { repoPath: string; worktreePath: string; newPath: string }) => Promise<void>
removeGitWorktree: (payload: { force?: boolean; repoPath: string; worktreePath: string }) => Promise<void>
pullGitWorktreeFromOrigin: (worktreePath: string) => Promise<void>
getFileInfo: (filePath: string) => Promise<FileViewerFileInfo>
readFileBytes: (options: { path: string; start: number; length: number }) => Promise<FileViewerByteRange>
readFileText: (options: {
Expand Down
Loading