{"record":{"id":"228d74822a94b19b","repo":"vectordotdev/vector","slug":"propagated-git2-error-from-branch-lookup-no-literal-message","errorCode":null,"errorMessage":"propagated git2::Error from branch lookup (no literal message)","messagePattern":"propagated git2::Error from branch lookup \\(no literal message\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"vdev/src/utils/git.rs","lineNumber":208,"sourceCode":"    Command::new(\"git\").args([\"clone\", repo_url]).check_output()\n}\n\n/// Walks up from the current working directory until it finds a `.git`\n/// and opens that repo.  Panics (Err) if none is found.\nfn find_repo() -> Result<Repository, git2::Error> {\n    Repository::discover(\".\")\n}\n\npub fn branch_exists(branch: &str) -> Result<bool> {\n    let repo = find_repo()?;\n\n    // Do the lookup inside its own scope so the temporary Branch<'_> is dropped\n    // before we try to drop `repo` at function exit.\n    let exists = {\n        match repo.find_branch(branch, BranchType::Local) {\n            Ok(_) => true,\n            Err(e) if e.code() == ErrorCode::NotFound => false,\n            Err(e) => bail!(e),\n        }\n    };\n\n    Ok(exists)\n}\n\npub fn checkout_branch(branch_name: &str) -> Result<()> {\n    let _output = run_and_check_output(&[\"checkout\", branch_name])?;\n    Ok(())\n}\n\npub fn checkout_main_branch() -> Result<()> {\n    let _output = run_and_check_output(&[\"switch\", MASTER_BRANCH])?;\n    Ok(())\n}\n\npub fn create_branch(branch_name: &str) -> Result<()> {\n    let repo = find_repo()?;","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/vectordotdev/vector/blob/0d4ab78a4f0d9c9e5d03a50108a2ae4d2f4b2460/vdev/src/utils/git.rs#L190-L226","documentation":"branch_exists looks up a local branch with git2's Repository::find_branch. A NotFound error is translated to `Ok(false)`, but any other git2::Error (e.g. invalid branch name specification, corrupt refs, libgit2 backend failure) is propagated verbatim into an anyhow error via `bail!(e)`, so the surfaced message is libgit2's own — not a custom literal.","triggerScenarios":"Calling branch_exists (directly or via checkout_or_create_branch) when find_branch fails with a non-NotFound code: malformed branch name (invalid ref characters), a refs database problem, or another libgit2 internal error.","commonSituations":"Passing a branch name containing invalid ref characters (spaces, `..`, leading/trailing dots); running in a repo with corrupted or packed-refs issues; using names git treats as ambiguous or invalid like `HEAD` misuse.","solutions":["Fix the branch-name string to a valid git ref name (no spaces, `..`, `~`, `^`, `:`, `?`, `*`, `[`, `\\`, control chars, or leading/trailing dots)","Check repo health: `git fsck`, and verify `refs/heads/<branch>` isn't corrupted","Inspect the propagated git2 message for the underlying libgit2 cause and address it (e.g. rebuild refs, re-clone)"],"exampleFix":"// before\nbranch_exists(\"feature/fix bug\")?; // invalid ref name -> git2::Error\n// after\nbranch_exists(\"feature/fix-bug\")?; // Ok(true/false)","handlingStrategy":"try-catch","validationCode":"const INVALID_REF_CHARS: &[char] = &[' ', '~', '^', ':', '?', '*', '[', '\\\\'];\nfn valid_branch_name(b: &str) -> bool {\n    !b.is_empty() && !b.starts_with('.') && !b.ends_with('.') && !b.contains(\"..\")\n        && !b.chars().any(|c| INVALID_REF_CHARS.contains(&c) || c.is_control())\n}","typeGuard":null,"tryCatchPattern":"match branch_exists(name) {\n    Ok(true) => { /* branch exists */ }\n    Ok(false) => { /* create it */ }\n    Err(e) => {\n        // message is the raw git2/libgit2 error; log context alongside it\n        eprintln!(\"branch lookup failed for '{name}': {e:#}\");\n    }\n}","preventionTips":["Validate branch names against git-check-ref-format rules before lookup","Log the branch name together with the propagated git2 message to expose the root cause","Run `git fsck` when libgit2 reports non-NotFound ref errors"],"tags":["git","libgit2","branch","error-propagation"],"backgroundTag":"git-command-failed","analyzedSha":"0d4ab78a4f0d9c9e5d03a50108a2ae4d2f4b2460","analyzedAt":"2026-09-16T02:53:35.741Z","contentChangedAt":"2026-09-16T02:53:35.741Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}