Skip to content

Commit d19b67f

Browse files
authored
Update cache url if protocol is updated (#103)
1 parent 142d46a commit d19b67f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/git/cache.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl ProtofetchGitCache {
6969
pub fn repository(&self, entry: &Coordinate) -> Result<ProtoGitRepository, CacheError> {
7070
let repo = match self.get_entry(entry) {
7171
None => self.clone_repo(entry)?,
72-
Some(path) => self.open_entry(&path)?,
72+
Some(path) => self.open_entry(&path, entry)?,
7373
};
7474

7575
Ok(ProtoGitRepository::new(self, repo))
@@ -90,8 +90,28 @@ impl ProtofetchGitCache {
9090
}
9191
}
9292

93-
fn open_entry(&self, path: &Path) -> Result<Repository, CacheError> {
94-
Repository::open(path).map_err(|e| e.into())
93+
fn open_entry(&self, path: &Path, entry: &Coordinate) -> Result<Repository, CacheError> {
94+
let repo = Repository::open(path).map_err(CacheError::from)?;
95+
96+
{
97+
let remote = repo.find_remote("origin").map_err(CacheError::from)?;
98+
99+
if let (Some(url), Some(protocol)) = (remote.url(), entry.protocol) {
100+
let new_url = entry.to_git_url(protocol);
101+
102+
if url != new_url {
103+
// If true then the protocol was updated before updating the cache.
104+
trace!(
105+
"Updating remote existing url {} to new url {}",
106+
url,
107+
new_url
108+
);
109+
repo.remote_set_url("origin", &new_url)?;
110+
}
111+
}
112+
} // `remote` reference is dropped here so that we can return `repo`
113+
114+
Ok(repo)
95115
}
96116

97117
fn clone_repo(&self, entry: &Coordinate) -> Result<Repository, CacheError> {

0 commit comments

Comments
 (0)