MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / sync_git_head_to_view

Function sync_git_head_to_view

atomic-cli/src/commands/git/shadow.rs:38–91  ·  view source on GitHub ↗

After an `atomic view switch`, point the git shadow's HEAD at the branch that mirrors the new view (Direction A of §5.4 — "git shadows Atomic"). Atomic is upstream, so it drives the downstream git shadow's branch selection. This is a **ref move**, never a `git checkout`: it updates HEAD (and realigns the index to the new branch's tree) but never re-renders the working copy, which Atomic just mate

(repo_root: &Path, view: &str)

Source from the content-addressed store, hash-verified

36/// warning, never a failure of the view switch — and a **no-op** outside a
37/// shadow-sync repo or when HEAD is already on the branch.
38pub(crate) fn sync_git_head_to_view(repo_root: &Path, view: &str) {
39 let git_repo = match GitRepository::discover(repo_root) {
40 Ok(r) => r,
41 Err(_) => return, // not a git repo — nothing to shadow
42 };
43 // Only touch git in repos where shadow sync is actually established.
44 if !shadow_sync_active(&git_repo) {
45 return;
46 }
47
48 let branch_ref = format!("refs/heads/{}", view);
49
50 // Idempotent: already on the mirror branch (no loop, no churn).
51 if git_repo
52 .head()
53 .ok()
54 .and_then(|h| h.name().map(str::to_owned))
55 .as_deref()
56 == Some(branch_ref.as_str())
57 {
58 return;
59 }
60
61 // Create the mirror branch at the current commit if it doesn't exist yet
62 // (a new draft view branches from wherever HEAD currently points).
63 if git_repo.find_reference(&branch_ref).is_err() {
64 match git_repo.head().and_then(|h| h.peel_to_commit()) {
65 Ok(commit) => {
66 if let Err(e) = git_repo.branch(view, &commit, false) {
67 print_warning(&format!(
68 "Could not create git branch '{}' to mirror the view: {}",
69 view, e
70 ));
71 return;
72 }
73 }
74 // Unborn HEAD / no commits yet — nothing to anchor a branch to.
75 Err(_) => return,
76 }
77 }
78
79 // Move HEAD to the mirror branch WITHOUT touching the working copy, then
80 // realign the index to the new branch tip so `git status` cleanly shows the
81 // view's content as the delta the next shadow push will commit.
82 if let Err(e) = git_repo.set_head(&branch_ref) {
83 print_warning(&format!(
84 "Could not point git HEAD at branch '{}': {}",
85 view, e
86 ));
87 return;
88 }
89 restore_index_from_head(&git_repo);
90 print_info(&format!("git shadow now tracks branch '{}'.", view));
91}
92
93/// Whether git shadow sync is established for this repo (the `.git/info/exclude`
94/// carries Atomic's shadow patterns, written by import/push). Used to gate the

Callers 1

runMethod · 0.85

Calls 7

shadow_sync_activeFunction · 0.85
print_warningFunction · 0.85
restore_index_from_headFunction · 0.85
print_infoFunction · 0.85
nameMethod · 0.45
as_strMethod · 0.45
branchMethod · 0.45

Tested by

no test coverage detected