Improve error messaging for git submodule push failures (#5226)

Detect when git push operations fail due to un-pushed or out-of-sync
submodules, and extract the specific submodule name to provide clear,
actionable guidance to the user.

Additionally, update the Source Control error UI to allow better text
wrapping and word-breaking for detailed error alerts.
This commit is contained in:
Jinjing
2026-06-11 17:32:49 -07:00
committed by GitHub
parent 08456103cb
commit 1f6bfe0503
6 changed files with 197 additions and 5 deletions
+33
View File
@@ -9,11 +9,39 @@
// scrubbing passwords on any scheme and HTTPS token-only forms.
const USERPASS_URL_PATTERN = /([a-z][a-z0-9+.-]*:\/\/)[^\s/@:]+:[^\s/@]+@/gi
const HTTPS_TOKEN_URL_PATTERN = /(https?:\/\/)[^\s/@:]+@/gi
const SUBMODULE_PUSH_FAILURE_PATTERN = /Unable to push submodule ['"](.+?)['"]/i
const SUBMODULE_PUSH_FAILURE_SENTINEL_PATTERN =
/failed to push all needed submodules|Unable to push submodule/i
const SUBMODULE_REMOTE_CHANGED_PATTERN =
/non-fast-forward|fetch first|updates were rejected|remote contains work that you do not have/i
const NORMALIZED_SUBMODULE_PUSH_FAILURE_PATTERN =
/(?:^|:\s)((?:Submodule '[^'\n]+'|A submodule) (?:has remote changes\. Pull inside the submodule, then try again\.|could not be pushed\. Resolve the submodule push error, then try again\.))(?:$|\s)/i
export function stripCredentialsFromMessage(message: string): string {
return message.replace(USERPASS_URL_PATTERN, '$1').replace(HTTPS_TOKEN_URL_PATTERN, '$1')
}
export function formatSubmodulePushFailureDetail(message: string): string | null {
const raw = stripCredentialsFromMessage(message)
const normalized = raw.replace(/\r\n/g, '\n').trim()
const normalizedMatch = normalized.match(NORMALIZED_SUBMODULE_PUSH_FAILURE_PATTERN)
if (normalizedMatch) {
return normalizedMatch[1]
}
if (!SUBMODULE_PUSH_FAILURE_SENTINEL_PATTERN.test(normalized)) {
return null
}
// Why: recursive push can hide the actionable nested rejection behind a
// top-level "failed to push all needed submodules" fatal line.
const submoduleName = normalized.match(SUBMODULE_PUSH_FAILURE_PATTERN)?.[1]?.trim()
const subject = submoduleName ? `Submodule '${submoduleName}'` : 'A submodule'
if (SUBMODULE_REMOTE_CHANGED_PATTERN.test(normalized)) {
return `${subject} has remote changes. Pull inside the submodule, then try again.`
}
return `${subject} could not be pushed. Resolve the submodule push error, then try again.`
}
function extractTailLine(message: string): string {
// Why: execFile rejections prefix the message with "Command failed: git ..."
// followed by the full stderr. The meaningful diagnostic is typically the
@@ -39,6 +67,11 @@ export function normalizeGitErrorMessage(error: unknown, operation?: GitRemoteOp
// literals today, but this hardens against accidental leakage later.
const raw = stripCredentialsFromMessage(error.message)
const submodulePushFailureDetail = formatSubmodulePushFailureDetail(raw)
if ((operation === 'push' || operation === undefined) && submodulePushFailureDetail) {
return submodulePushFailureDetail
}
// Why: `non-fast-forward` / `fetch first` can appear on fetch (after a
// remote force-push updating a tracking ref) and on pull (with
// `pull.ff=only`), so the "pull or sync first" guidance only makes sense