Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(clone): shared pure helpers (shorthand, validation, progress parse)

enes5453cccbcca9
diff --git a/src/main/git-service.ts b/src/main/git-service.ts
index 4d51f1281..db989857c 100644
--- a/src/main/git-service.ts
+++ b/src/main/git-service.ts
@@ -10,6 +10,7 @@ import { loadGitHistoryFromExecutor } from '../shared/git-history'
import * as worktreeOps from '../shared/worktree-ops'
import type { GitHistoryOptions, GitHistoryResult } from '../shared/git-history'
import { resolveGitRemote, runRemoteGit } from './remote-ssh/remote-git'
+import { isValidCloneUrl } from '../shared/clone-url'
const run = promisify(execFile)
@@ -96,17 +97,6 @@ function isValidRef(name: string): boolean {
return !/[\s~^:?*[\\]|\.\.|^\/|\/$|@\{/.test(n)
}
-/**
- * Validate a clone URL: must use a known scheme (or scp-style git@host:path) and
- * must not begin with `-`, so it can't be parsed by git as an option flag
- * (e.g. `--upload-pack=…`, which is a remote-code-execution vector).
- */
-function isValidCloneUrl(url: string): boolean {
- const u = url.trim()
- if (!u || u.startsWith('-')) return false
- return /^(https?:\/\/|ssh:\/\/|git:\/\/|git@[^/]+:)/.test(u)
-}
-
/** path -> {added, deleted} from `git diff --numstat` output. */
function parseNumstat(out: string): Map<string, { added: number; deleted: number }> {
const map = new Map<string, { added: number; deleted: number }>()
diff --git a/src/shared/clone-url.test.ts b/src/shared/clone-url.test.ts
new file mode 100644
index 000000000..a76ba8887
--- /dev/null
+++ b/src/shared/clone-url.test.ts
@@ -0,0 +1,79 @@
+import { describe, it, expect } from 'vitest'
+import {
+ expandCloneUrl,
+ isValidCloneUrl,
+ deriveRepoDirName,
+ parseCloneProgress,
+ stripAnsiCodes
+} from './clone-url'
+
+describe('expandCloneUrl', () => {
+ it('expands owner/repo shorthand to a GitHub HTTPS URL', () => {
+ expect(expandCloneUrl('facebook/react')).toBe('https://github.com/facebook/react.git')
+ expect(expandCloneUrl(' torvalds/linux ')).toBe('https://github.com/torvalds/linux.git')
+ expect(expandCloneUrl('a-b.c/d_e.git')).toBe('https://github.com/a-b.c/d_e.git')
+ })
+ it('leaves full URLs and non-shorthand input untouched', () => {
+ expect(expandCloneUrl('https://github.com/a/b.git')).toBe('https://github.com/a/b.git')
+ expect(expandCloneUrl('[email protected]:a/b.git')).toBe('[email protected]:a/b.git')
+ expect(expandCloneUrl('a/b/c')).toBe('a/b/c')
+ expect(expandCloneUrl('-x/y')).toBe('-x/y') // never turn a flag-looking string into a URL
+ })
+})
+
+describe('isValidCloneUrl', () => {
+ it('accepts known schemes and scp-style', () => {
+ for (const u of [
+ 'https://github.com/a/b.git',
+ 'http://host/repo',
+ 'ssh://git@host/a/b',
+ 'git://host/a',
+ '[email protected]:a/b.git'
+ ])
+ expect(isValidCloneUrl(u)).toBe(true)
+ })
+ it('rejects empty, flag-leading and unknown schemes', () => {
+ for (const u of ['', ' ', '-https://x', '--upload-pack=/bin/sh', 'file:///etc', 'ftp://x/y'])
+ expect(isValidCloneUrl(u)).toBe(false)
+ })
+})
+
+describe('deriveRepoDirName', () => {
+ it('derives the repo folder from common URL shapes', () => {
+ expect(deriveRepoDirName('https://github.com/a/repo.git')).toBe('repo')
+ expect(deriveRepoDirName('https://github.com/a/repo.git/')).toBe('repo')
+ expect(deriveRepoDirName('[email protected]:a/repo.git')).toBe('repo')
+ expect(deriveRepoDirName('ssh://git@host/x/y/repo')).toBe('repo')
+ })
+ it('rejects names that could escape the parent dir', () => {
+ expect(deriveRepoDirName('https://host/..')).toBeNull()
+ expect(deriveRepoDirName('https://host/.git')).toBeNull()
+ expect(deriveRepoDirName('')).toBeNull()
+ })
+})
+
+describe('parseCloneProgress', () => {
+ it('returns the LAST progress line of a chunk', () => {
+ const chunk = 'Receiving objects: 10% (1/10)\rReceiving objects: 40% (4/10)\r'
+ expect(parseCloneProgress(chunk)).toEqual({ phase: 'Receiving objects', percent: 40 })
+ })
+ it('parses all git phases and clamps to 100', () => {
+ expect(parseCloneProgress('Resolving deltas: 100% (5/5), done.')).toEqual({
+ phase: 'Resolving deltas',
+ percent: 100
+ })
+ expect(parseCloneProgress('Counting objects: 120% weird')).toEqual({
+ phase: 'Counting objects',
+ percent: 100
+ })
+ })
+ it('returns null when a chunk has no percentage', () => {
+ expect(parseCloneProgress("Cloning into 'repo'...\n")).toBeNull()
+ })
+})
+
+describe('stripAnsiCodes', () => {
+ it('removes SGR color codes', () => {
+ expect(stripAnsiCodes('\x1b[31mfatal:\x1b[0m nope')).toBe('fatal: nope')
+ })
+})
diff --git a/src/shared/clone-url.ts b/src/shared/clone-url.ts
new file mode 100644
index 000000000..db20079dc
--- /dev/null
+++ b/src/shared/clone-url.ts
@@ -0,0 +1,56 @@
+// Pure helpers for the clone-repo flow. No electron/node imports — shared by the
+// renderer dialog (live validation/preview) and the main process (authoritative checks).
+
+export interface CloneProgress {
+ phase: string
+ percent: number
+}
+
+/** `owner/repo` GitHub shorthand → HTTPS clone URL. Anything else passes through. */
+export function expandCloneUrl(input: string): string {
+ const s = input.trim()
+ const m = s.match(/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/)
+ if (m && !s.startsWith('-')) {
+ return `https://github.com/${m[1]}/${m[2].replace(/\.git$/i, '')}.git`
+ }
+ return s
+}
+
+/**
+ * Validate a clone URL: must use a known scheme (or scp-style git@host:path) and
+ * must not begin with `-`, so it can't be parsed by git as an option flag
+ * (e.g. `--upload-pack=…`, which is a remote-code-execution vector).
+ */
+export function isValidCloneUrl(url: string): boolean {
+ const u = url.trim()
+ if (!u || u.startsWith('-')) return false
+ return /^(https?:\/\/|ssh:\/\/|git:\/\/|git@[^/]+:)/.test(u)
+}
+
+/**
+ * The folder name `git clone` would create for this URL. Returns null for names that
+ * are empty/`.`/`..` or contain path separators — a crafted URL must never be able to
+ * write outside (or delete) the parent directory.
+ */
+export function deriveRepoDirName(url: string): string | null {
+ const trimmed = url.trim().replace(/\/+$/, '').replace(/\.git$/i, '')
+ const base = trimmed.split(/[/:\\]/).pop() ?? ''
+ if (!base || base === '.' || base === '..') return null
+ return base
+}
+
+/** Last `<phase>: NN%` line in a stderr chunk (git overwrites lines with \r). */
+export function parseCloneProgress(chunk: string): CloneProgress | null {
+ let out: CloneProgress | null = null
+ for (const line of chunk.split(/[\r\n]+/)) {
+ const m = line.match(/([A-Za-z][A-Za-z\s-]*):\s+(\d+)%/)
+ if (m) out = { phase: m[1].trim(), percent: Math.min(100, parseInt(m[2], 10)) }
+ }
+ return out
+}
+
+/** Remove ANSI SGR sequences (git colorizes fatal lines on some setups). */
+export function stripAnsiCodes(s: string): string {
+ // eslint-disable-next-line no-control-regex
+ return s.replace(/\x1b\[[0-9;]*m/g, '')
+}