Skip to content

Commit bfe63df

Browse files
Merge pull request #69 from wcampbell0x2a/set-head-to-fetched-master
Set local HEAD of git repo when pull from previous zerus
2 parents eb85180 + bfc2bf6 commit bfe63df

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [0.8.0] - 05-29-2024
10+
- Properly set local HEAD to fetched git repo `crates.io-index` when updating from previous zerus invocation
11+
812
## [0.7.0] - 04-11-2024
913
- Add automatic crates.io git clone and fetch for mirror
1014

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "zerus"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
edition = "2021"
55
authors = ["wcampbell"]
66
description = "Lightweight binary to download only project required crates for offline crates.io mirror"

src/git.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn clone(repo_path: &Path) -> Result<(), git2::Error> {
9898
Ok(())
9999
}
100100

101-
pub fn fetch(repo: &Path) -> Result<(), git2::Error> {
101+
pub fn pull(repo: &Path) -> Result<(), git2::Error> {
102102
let repo = Repository::open(repo)?;
103103
let remote = "origin";
104104

@@ -188,5 +188,50 @@ pub fn fetch(repo: &Path) -> Result<(), git2::Error> {
188188
// needed objects are available locally.
189189
remote.update_tips(None, true, AutotagOption::Unspecified, None)?;
190190

191+
let fetch_head = repo.find_reference("FETCH_HEAD")?;
192+
let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?;
193+
194+
let refname = "refs/heads/master";
195+
match repo.find_reference(refname) {
196+
Ok(mut r) => {
197+
fast_forward(&repo, &mut r, &fetch_commit)?;
198+
}
199+
Err(_) => {
200+
// The branch doesn't exist so just set the reference to the
201+
// commit directly. Usually this is because you are pulling
202+
// into an empty repository.
203+
repo.reference(
204+
refname,
205+
fetch_commit.id(),
206+
true,
207+
&format!("Setting master to {}", fetch_commit.id()),
208+
)?;
209+
repo.set_head(refname)?;
210+
repo.checkout_head(Some(
211+
git2::build::CheckoutBuilder::default()
212+
.allow_conflicts(true)
213+
.conflict_style_merge(true)
214+
.force(),
215+
))?;
216+
}
217+
}
218+
219+
Ok(())
220+
}
221+
222+
fn fast_forward(
223+
repo: &Repository,
224+
lb: &mut git2::Reference,
225+
rc: &git2::AnnotatedCommit,
226+
) -> Result<(), git2::Error> {
227+
let name = match lb.name() {
228+
Some(s) => s.to_string(),
229+
None => String::from_utf8_lossy(lb.name_bytes()).to_string(),
230+
};
231+
let msg = format!("Fast-Forward: Setting {} to id: {}", name, rc.id());
232+
println!("{}", msg);
233+
lb.set_target(rc.id(), &msg)?;
234+
repo.set_head(&name)?;
235+
repo.checkout_head(None)?;
191236
Ok(())
192237
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use guppy::MetadataCommand;
1010
use rayon::prelude::*;
1111
use reqwest::blocking::Client;
1212

13-
use crate::git::{clone, fetch};
13+
use crate::git::{clone, pull};
1414

1515
mod git;
1616

@@ -54,7 +54,7 @@ fn main() {
5454
println!("[-] Syncing git index crates.io");
5555
let repo = args.mirror_path.join("crates.io-index");
5656
if repo.exists() {
57-
fetch(Path::new(&repo)).unwrap();
57+
pull(Path::new(&repo)).unwrap();
5858
} else {
5959
clone(Path::new(&repo)).unwrap();
6060
}

0 commit comments

Comments
 (0)