From c17d57ede5e76c80982f90f79b6f8efc7a86603a Mon Sep 17 00:00:00 2001 From: Tim Bornemann <65897899+timbornemann@users.noreply.github.com> Date: Sun, 5 Apr 2026 01:17:03 +0200 Subject: [PATCH] Fix push fallback to auto-set upstream for new branches --- src/components/layout/state/appStateShared.ts | 2 +- src/components/layout/useAppState.ts | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/components/layout/state/appStateShared.ts b/src/components/layout/state/appStateShared.ts index 0312af0..22cc88e 100644 --- a/src/components/layout/state/appStateShared.ts +++ b/src/components/layout/state/appStateShared.ts @@ -25,6 +25,7 @@ export const DEFAULT_SETTINGS: AppSettingsDto = { export type RunGitCommandOptions = { skipDirtyGuard?: boolean; skipSecretScan?: boolean; + skipAutoSetUpstreamOnPushFailure?: boolean; }; export const GUARDED_COMMANDS = new Set(['checkout', 'merge', 'reset']); @@ -57,4 +58,3 @@ export const DEFAULT_SIDEBAR_COLLAPSE_STATE: SidebarCollapseState = { export const DEFAULT_SIDEBAR_GENERAL_COLLAPSE_STATE: SidebarGeneralCollapseState = { repoPanelCollapsed: false, }; - diff --git a/src/components/layout/useAppState.ts b/src/components/layout/useAppState.ts index 64d1b17..78efef1 100644 --- a/src/components/layout/useAppState.ts +++ b/src/components/layout/useAppState.ts @@ -185,6 +185,30 @@ export const useAppState = () => { if (!window.electronAPI || !workspace.activeRepo || args.length === 0) return false; const command = args[0]; + const normalizedError = (value: unknown) => String(value || '').toLowerCase(); + const isMissingUpstreamError = (value: unknown) => { + const message = normalizedError(value); + return ( + message.includes('has no upstream branch') + || message.includes('no upstream branch') + || message.includes('--set-upstream') + || message.includes('set-upstream') + ); + }; + const tryAutoSetUpstreamPush = async (failureMessage: unknown): Promise => { + if (command !== 'push' || options?.skipAutoSetUpstreamOnPushFailure || !isMissingUpstreamError(failureMessage)) { + return false; + } + + const fallbackArgs = ['push', ...args.slice(1), '-u', 'origin', 'HEAD']; + const fallbackSuccess = await runGitCommand( + fallbackArgs, + tr('Branch gepusht und Upstream gesetzt.', 'Pushed branch and set upstream.'), + tr('Push mit Upstream wird ausgefuehrt...', 'Running push with upstream...'), + { ...options, skipAutoSetUpstreamOnPushFailure: true }, + ); + return fallbackSuccess; + }; const shouldGuard = settings.confirmDangerousOps && !options?.skipDirtyGuard && GUARDED_COMMANDS.has(command); const shouldScanPushSecrets = command === 'push' @@ -305,6 +329,9 @@ export const useAppState = () => { triggerRefresh(); return true; } + if (await tryAutoSetUpstreamPush(r.error)) { + return true; + } const mergeInProgress = isMergeInProgressError(r.error); triggerRefresh(); try { @@ -345,6 +372,9 @@ export const useAppState = () => { setGitActionToast({ msg: r.error || tr('Fehler beim Ausführen von git.', 'Error while running git.'), isError: true }); return false; } catch (e: any) { + if (await tryAutoSetUpstreamPush(e?.message)) { + return true; + } const mergeInProgress = isMergeInProgressError(e?.message); triggerRefresh(); try { @@ -1068,4 +1098,3 @@ export const useAppState = () => { -