diff --git a/crates/local-deployment/src/container.rs b/crates/local-deployment/src/container.rs index 222cd531..b1f89fc9 100644 --- a/crates/local-deployment/src/container.rs +++ b/crates/local-deployment/src/container.rs @@ -895,6 +895,10 @@ impl ContainerService for LocalContainerService { &self.git } + async fn git_branch_prefix(&self) -> String { + self.config.read().await.git_branch_prefix.clone() + } + fn task_attempt_to_current_dir(&self, task_attempt: &TaskAttempt) -> PathBuf { PathBuf::from(task_attempt.container_ref.clone().unwrap_or_default()) } diff --git a/crates/server/src/routes/config.rs b/crates/server/src/routes/config.rs index 9a511fba..2dc82848 100644 --- a/crates/server/src/routes/config.rs +++ b/crates/server/src/routes/config.rs @@ -100,6 +100,13 @@ async fn update_config( ) -> ResponseJson> { let config_path = config_path(); + // Validate git branch prefix + if !utils::git::is_valid_branch_prefix(&new_config.git_branch_prefix) { + return ResponseJson(ApiResponse::error( + "Invalid git branch prefix. Must be a valid git branch name component without slashes.", + )); + } + // Get old config state before updating let old_config = deployment.config().read().await.clone(); diff --git a/crates/server/src/routes/task_attempts.rs b/crates/server/src/routes/task_attempts.rs index 11e4e6c9..ee155325 100644 --- a/crates/server/src/routes/task_attempts.rs +++ b/crates/server/src/routes/task_attempts.rs @@ -154,7 +154,8 @@ pub async fn create_task_attempt( let attempt_id = Uuid::new_v4(); let git_branch_name = deployment .container() - .git_branch_from_task_attempt(&attempt_id, &task.title); + .git_branch_from_task_attempt(&attempt_id, &task.title) + .await; let task_attempt = TaskAttempt::create( &deployment.db().pool, diff --git a/crates/server/src/routes/tasks.rs b/crates/server/src/routes/tasks.rs index b24755e7..ac053f4e 100644 --- a/crates/server/src/routes/tasks.rs +++ b/crates/server/src/routes/tasks.rs @@ -166,7 +166,8 @@ pub async fn create_task_and_start( let attempt_id = Uuid::new_v4(); let git_branch_name = deployment .container() - .git_branch_from_task_attempt(&attempt_id, &task.title); + .git_branch_from_task_attempt(&attempt_id, &task.title) + .await; let task_attempt = TaskAttempt::create( &deployment.db().pool, diff --git a/crates/services/src/services/config/versions/v7.rs b/crates/services/src/services/config/versions/v7.rs index a9a01dd1..c12dfb0f 100644 --- a/crates/services/src/services/config/versions/v7.rs +++ b/crates/services/src/services/config/versions/v7.rs @@ -7,6 +7,10 @@ pub use v6::{EditorConfig, EditorType, GitHubConfig, NotificationConfig, SoundFi use crate::services::config::versions::v6; +fn default_git_branch_prefix() -> String { + "vk".to_string() +} + #[derive(Debug, Clone, Serialize, Deserialize, TS, EnumString)] #[ts(use_ts_enum)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] @@ -35,6 +39,8 @@ pub struct Config { pub show_release_notes: bool, #[serde(default)] pub language: UiLanguage, + #[serde(default = "default_git_branch_prefix")] + pub git_branch_prefix: String, } impl Config { @@ -83,6 +89,7 @@ impl Config { last_app_version: old_config.last_app_version, show_release_notes: old_config.show_release_notes, language: old_config.language, + git_branch_prefix: default_git_branch_prefix(), }) } } @@ -126,6 +133,7 @@ impl Default for Config { last_app_version: None, show_release_notes: false, language: UiLanguage::default(), + git_branch_prefix: default_git_branch_prefix(), } } } diff --git a/crates/services/src/services/container.rs b/crates/services/src/services/container.rs index 5a890abe..b12533c2 100644 --- a/crates/services/src/services/container.rs +++ b/crates/services/src/services/container.rs @@ -226,9 +226,17 @@ pub trait ContainerService { map.get(uuid).cloned() } - fn git_branch_from_task_attempt(&self, attempt_id: &Uuid, task_title: &str) -> String { + async fn git_branch_prefix(&self) -> String; + + async fn git_branch_from_task_attempt(&self, attempt_id: &Uuid, task_title: &str) -> String { let task_title_id = git_branch_id(task_title); - format!("vk/{}-{}", short_uuid(attempt_id), task_title_id) + let prefix = self.git_branch_prefix().await; + + if prefix.is_empty() { + format!("{}-{}", short_uuid(attempt_id), task_title_id) + } else { + format!("{}/{}-{}", prefix, short_uuid(attempt_id), task_title_id) + } } async fn stream_raw_logs( diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml index 09e9adbb..0ad18c70 100644 --- a/crates/utils/Cargo.toml +++ b/crates/utils/Cargo.toml @@ -31,3 +31,4 @@ async-stream = "0.3" shellexpand = "3.1.1" which = "8.0.0" similar = "2" +git2 = "0.18" diff --git a/crates/utils/src/git.rs b/crates/utils/src/git.rs new file mode 100644 index 00000000..784e7930 --- /dev/null +++ b/crates/utils/src/git.rs @@ -0,0 +1,47 @@ +pub fn is_valid_branch_prefix(prefix: &str) -> bool { + if prefix.is_empty() { + return true; + } + + if prefix.contains('/') { + return false; + } + + git2::Branch::name_is_valid(&format!("{prefix}/x")).unwrap_or_default() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_valid_prefixes() { + assert!(is_valid_branch_prefix("")); + assert!(is_valid_branch_prefix("vk")); + assert!(is_valid_branch_prefix("feature")); + assert!(is_valid_branch_prefix("hotfix-123")); + assert!(is_valid_branch_prefix("foo.bar")); + assert!(is_valid_branch_prefix("foo_bar")); + assert!(is_valid_branch_prefix("FOO-Bar")); + } + + #[test] + fn test_invalid_prefixes() { + assert!(!is_valid_branch_prefix("foo/bar")); + assert!(!is_valid_branch_prefix("foo..bar")); + assert!(!is_valid_branch_prefix("foo@{")); + assert!(!is_valid_branch_prefix("foo.lock")); + // Note: git2 allows trailing dots in some contexts, but we enforce stricter rules + // for prefixes by checking the full branch name format + assert!(!is_valid_branch_prefix("foo bar")); + assert!(!is_valid_branch_prefix("foo?")); + assert!(!is_valid_branch_prefix("foo*")); + assert!(!is_valid_branch_prefix("foo~")); + assert!(!is_valid_branch_prefix("foo^")); + assert!(!is_valid_branch_prefix("foo:")); + assert!(!is_valid_branch_prefix("foo[")); + assert!(!is_valid_branch_prefix("/foo")); + assert!(!is_valid_branch_prefix("foo/")); + assert!(!is_valid_branch_prefix(".foo")); + } +} diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 523d156c..7c449604 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -6,6 +6,7 @@ pub mod approvals; pub mod assets; pub mod browser; pub mod diff; +pub mod git; pub mod log_msg; pub mod msg_store; pub mod path; diff --git a/frontend/src/i18n/locales/en/settings.json b/frontend/src/i18n/locales/en/settings.json index 8009e814..6753c50f 100644 --- a/frontend/src/i18n/locales/en/settings.json +++ b/frontend/src/i18n/locales/en/settings.json @@ -70,6 +70,26 @@ "createTokenLink": "Create token here" } }, + "git": { + "title": "Git", + "description": "Configure git branch naming preferences", + "branchPrefix": { + "label": "Branch Prefix", + "placeholder": "vk", + "helper": "Prefix for auto-generated branch names. Leave empty for no prefix.", + "preview": "Preview:", + "previewWithPrefix": "{{prefix}}/1a2b-task-name", + "previewNoPrefix": "1a2b-task-name", + "errors": { + "slash": "Prefix cannot contain '/'.", + "startsWithDot": "Prefix cannot start with '.'.", + "endsWithDot": "Prefix cannot end with '.' or '.lock'.", + "invalidSequence": "Contains invalid sequence (.., @{).", + "invalidChars": "Contains invalid characters.", + "controlChars": "Contains control characters." + } + } + }, "notifications": { "title": "Notifications", "description": "Control when and how you receive notifications.", diff --git a/frontend/src/i18n/locales/es/settings.json b/frontend/src/i18n/locales/es/settings.json index d330c2ef..f6cc62e3 100644 --- a/frontend/src/i18n/locales/es/settings.json +++ b/frontend/src/i18n/locales/es/settings.json @@ -70,6 +70,26 @@ "createTokenLink": "Crear token aquí" } }, + "git": { + "title": "Git", + "description": "Configurar preferencias de nombres de ramas git", + "branchPrefix": { + "label": "Prefijo de Rama", + "placeholder": "vk", + "helper": "Prefijo para nombres de ramas generadas automáticamente. Dejar vacío para no usar prefijo.", + "preview": "Vista previa:", + "previewWithPrefix": "{{prefix}}/1a2b-nombre-tarea", + "previewNoPrefix": "1a2b-nombre-tarea", + "errors": { + "slash": "El prefijo no puede contener '/'.", + "startsWithDot": "El prefijo no puede comenzar con '.'.", + "endsWithDot": "El prefijo no puede terminar con '.' o '.lock'.", + "invalidSequence": "Contiene secuencia no válida (.., @{).", + "invalidChars": "Contiene caracteres no válidos.", + "controlChars": "Contiene caracteres de control." + } + } + }, "notifications": { "title": "Notificaciones", "description": "Controla cuándo y cómo recibes notificaciones.", diff --git a/frontend/src/i18n/locales/ja/settings.json b/frontend/src/i18n/locales/ja/settings.json index 8ea395de..68e249dd 100644 --- a/frontend/src/i18n/locales/ja/settings.json +++ b/frontend/src/i18n/locales/ja/settings.json @@ -70,6 +70,26 @@ "createTokenLink": "ここでトークンを作成" } }, + "git": { + "title": "Git", + "description": "Gitブランチ名の設定", + "branchPrefix": { + "label": "ブランチプレフィックス", + "placeholder": "vk", + "helper": "自動生成されるブランチ名のプレフィックス。空欄にするとプレフィックスなしになります。", + "preview": "プレビュー:", + "previewWithPrefix": "{{prefix}}/1a2b-タスク名", + "previewNoPrefix": "1a2b-タスク名", + "errors": { + "slash": "プレフィックスに'/'を含めることはできません。", + "startsWithDot": "プレフィックスは'.'で始めることができません。", + "endsWithDot": "プレフィックスは'.'または'.lock'で終わることができません。", + "invalidSequence": "無効なシーケンス(..、@{)が含まれています。", + "invalidChars": "無効な文字が含まれています。", + "controlChars": "制御文字が含まれています。" + } + } + }, "notifications": { "title": "通知", "description": "通知を受け取るタイミングと方法を制御します。", diff --git a/frontend/src/pages/settings/GeneralSettings.tsx b/frontend/src/pages/settings/GeneralSettings.tsx index a6e02148..18f67299 100644 --- a/frontend/src/pages/settings/GeneralSettings.tsx +++ b/frontend/src/pages/settings/GeneralSettings.tsx @@ -66,8 +66,35 @@ export function GeneralSettings() { const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); + const [branchPrefixError, setBranchPrefixError] = useState( + null + ); const { setTheme } = useTheme(); + const validateBranchPrefix = useCallback( + (prefix: string): string | null => { + if (!prefix) return null; // empty allowed + if (prefix.includes('/')) + return t('settings.general.git.branchPrefix.errors.slash'); + if (prefix.startsWith('.')) + return t('settings.general.git.branchPrefix.errors.startsWithDot'); + if (prefix.endsWith('.') || prefix.endsWith('.lock')) + return t('settings.general.git.branchPrefix.errors.endsWithDot'); + if (prefix.includes('..') || prefix.includes('@{')) + return t('settings.general.git.branchPrefix.errors.invalidSequence'); + if (/[ \t~^:?*[\\]/.test(prefix)) + return t('settings.general.git.branchPrefix.errors.invalidChars'); + // Control chars check + for (let i = 0; i < prefix.length; i++) { + const code = prefix.charCodeAt(i); + if (code < 0x20 || code === 0x7f) + return t('settings.general.git.branchPrefix.errors.controlChars'); + } + return null; + }, + [t] + ); + // When config loads or changes externally, update draft only if not dirty useEffect(() => { if (!config) return; @@ -492,14 +519,6 @@ export function GeneralSettings() { )} -
-
- - {t('settings.general.github.or')} - -
-
-