Skip to content

Commit 331780a

Browse files
committed
chore: automatically update local branches.
1 parent e8e9db9 commit 331780a

1 file changed

Lines changed: 53 additions & 10 deletions

File tree

cite/git/src/repository/repository.rs

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ impl Repository {
2323
Git2Repository::open(&self.path).map_err(|e| GitSourceError::Git(e))
2424
}
2525

26-
/// Update an existing repository (best-effort operation)
27-
pub fn update_from_remote(&mut self, remote_url: &str) -> Result<(), GitSourceError> {
28-
// Try to fetch latest changes - if it fails, that's okay too
29-
let _ = self.fetch_latest_changes(remote_url);
30-
Ok(())
31-
}
32-
3326
/// Clone the repository from the remote URL
3427
fn clone_repository(&self) -> Result<(), GitSourceError> {
3528
// Ensure parent directory exists
@@ -77,6 +70,56 @@ impl Repository {
7770
)
7871
.map_err(|e| GitSourceError::Git(e))?;
7972

73+
// Update local branches to point to the latest remote branches
74+
self.update_local_branches(&git_repo)?;
75+
76+
Ok(())
77+
}
78+
79+
/// Update local branches to point to the latest remote tracking branches
80+
fn update_local_branches(&self, git_repo: &Git2Repository) -> Result<(), GitSourceError> {
81+
// Get all remote tracking branches
82+
let remote_refs = git_repo.references_glob("refs/remotes/origin/*")?;
83+
84+
for remote_ref in remote_refs {
85+
let remote_ref = remote_ref?;
86+
let remote_ref_name = remote_ref.name().ok_or_else(|| {
87+
GitSourceError::InvalidRemote("Invalid remote reference name".to_string())
88+
})?;
89+
90+
// Extract branch name from refs/remotes/origin/branch_name
91+
if let Some(branch_name) = remote_ref_name.strip_prefix("refs/remotes/origin/") {
92+
// Skip HEAD reference
93+
if branch_name == "HEAD" {
94+
continue;
95+
}
96+
97+
let local_ref_name = format!("refs/heads/{}", branch_name);
98+
99+
// Check if local branch exists
100+
if let Ok(mut local_ref) = git_repo.find_reference(&local_ref_name) {
101+
// Update local branch to point to the same commit as remote branch
102+
let target_oid = remote_ref.target().ok_or_else(|| {
103+
GitSourceError::InvalidRemote("Invalid remote reference target".to_string())
104+
})?;
105+
106+
local_ref.set_target(target_oid, "Update from remote")?;
107+
} else {
108+
// Create local branch pointing to remote branch
109+
let target_oid = remote_ref.target().ok_or_else(|| {
110+
GitSourceError::InvalidRemote("Invalid remote reference target".to_string())
111+
})?;
112+
113+
git_repo.reference(
114+
&local_ref_name,
115+
target_oid,
116+
true,
117+
"Create local branch from remote",
118+
)?;
119+
}
120+
}
121+
}
122+
80123
Ok(())
81124
}
82125

@@ -189,11 +232,11 @@ impl Repository {
189232
// If repository doesn't exist, clone it first
190233
if !self.path.exists() {
191234
self.clone_repository()?;
192-
} else {
193-
// Repository exists, fetch latest changes to update existing branches/tags
194-
self.fetch_latest_changes(&self.remote)?;
195235
}
196236

237+
// Repository exists, fetch latest changes to update existing branches/tags
238+
self.fetch_latest_changes(&self.remote)?;
239+
197240
let git_repo = self.open_git_repo()?;
198241
let mut remote = git_repo.find_remote("origin").map_err(|e| GitSourceError::Git(e))?;
199242

0 commit comments

Comments
 (0)