Skip to content

Commit

Permalink
Merge pull request #179 from Eh2406/set_commit
Browse files Browse the repository at this point in the history
Set commit
  • Loading branch information
Byron authored Dec 11, 2024
2 parents adbc764 + b4e6379 commit 42e2d22
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ name = "update_and_get_most_recent_version"
required-features = ["git-https"]

[dependencies]
gix = { version = "0.67.0", default-features = false, features = [
gix = { version = "0.68.0", default-features = false, features = [
"max-performance-safe",
"blocking-network-client",
"revision",
], optional = true }
hex = { version = "0.4.3", features = ["serde"] }
home = "0.5.4"
Expand Down
6 changes: 1 addition & 5 deletions src/git/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ impl<'repo> Iterator for Changes<'repo> {

impl<'repo> Changes<'repo> {
pub(crate) fn new(index: &'repo GitIndex) -> Result<Self, GixError> {
let current = index
.repo
.find_object(index.head_commit)?
.peel_to_kind(gix::object::Kind::Commit)?
.into_commit();
let current = index.repo.find_object(index.head_commit)?.peel_to_commit()?;
let current_tree = current.tree()?;

Ok(Self {
Expand Down
69 changes: 65 additions & 4 deletions src/git/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use gix::bstr::ByteSlice;
use gix::config::tree::Key;
use std::io;
use std::path::{Path, PathBuf};
use std::time::Duration;
use std::time::SystemTime;

/// An individual change to a crate in the crates.io index, returned by [the changes iterator](GitIndex::changes).
Expand Down Expand Up @@ -139,6 +140,68 @@ impl GitIndex {
&self.url
}

/// Timestamp of the commit of repository being read, which may be the publication or modification date.
///
/// Note that currently only times at or past the Unix epoch are supported.
#[inline]
#[must_use]
pub fn time(&self) -> Result<SystemTime, GixError> {
Ok(SystemTime::UNIX_EPOCH
+ Duration::from_secs(
self.repo
.find_object(self.head_commit)?
.peel_to_commit()?
.time()?
.seconds
.max(0) as _,
))
}

/// git hash of the commit of repository being read
#[must_use]
pub fn commit(&self) -> &[u8; 20] {
self.head_commit.as_bytes().try_into().unwrap()
}

/// git hash of the commit of repository being read
#[must_use]
pub fn commit_hex(&self) -> String {
self.head_commit.to_string()
}

fn lookup_commit(&self, rev: &str) -> Option<gix::ObjectId> {
self.repo
.rev_parse_single(rev)
.ok()?
.object()
.ok()?
.try_into_commit()
.ok()?
.id
.into()
}

/// Change the commit of repository being read to the commit pointed to by a refspec.
/// Note that this is *in-memory* only, the repository will not be changed!
pub fn set_commit_from_refspec(&mut self, rev: &str) -> Result<(), Error> {
self.head_commit = self.lookup_commit(rev).ok_or_else(|| Error::MissingHead {
repo_path: self.path.to_owned(),
refs_tried: &[],
refs_available: self
.repo
.references()
.ok()
.and_then(|p| {
p.all()
.ok()?
.map(|r| r.ok().map(|r| r.name().as_bstr().to_string()))
.collect()
})
.unwrap_or_default(),
})?;
Ok(())
}

/// List crates that have changed (published or yanked), in reverse chronological order.
///
/// This iterator is aware of periodic index squashing crates.io performs,
Expand Down Expand Up @@ -198,7 +261,6 @@ impl GitIndex {
path,
url,
repo,
head_commit_hex: head_commit.to_hex().to_string(),
head_commit,
}))
}
Expand Down Expand Up @@ -244,7 +306,6 @@ impl GitIndex {

let head_commit = Self::find_repo_head(&self.repo, &self.path)?;
self.head_commit = head_commit;
self.head_commit_hex = head_commit.to_hex().to_string();

Ok(())
}
Expand All @@ -254,7 +315,7 @@ impl GitIndex {
/// directly from the git blob containing the crate information.
///
/// Use this only if you need to get very few crates. If you're going
/// to read majority of crates, prefer the [`GitIndex::crates()`] iterator.
/// to read the majority of crates, prefer the [`GitIndex::crates()`] iterator.
#[must_use]
pub fn crate_(&self, name: &str) -> Option<Crate> {
let rel_path = crate_name_to_relative_path(name, None)?;
Expand All @@ -268,7 +329,7 @@ impl GitIndex {
cache_path.push(".cache");
cache_path.push(&rel_path);
if let Ok(cache_bytes) = std::fs::read(&cache_path) {
if let Ok(krate) = Crate::from_cache_slice(&cache_bytes, Some(&self.head_commit_hex)) {
if let Ok(krate) = Crate::from_cache_slice(&cache_bytes, None) {
return Some(krate);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ pub struct GitIndex {

pub(crate) repo: gix::Repository,
pub(crate) head_commit: gix::ObjectId,
head_commit_hex: String,
}

///
Expand Down
12 changes: 8 additions & 4 deletions tests/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ pub(crate) mod with_https {
}

test_sval(&repo);

repo.update().expect("Failed to fetch crates.io index");

test_sval(&repo);
}

Expand Down Expand Up @@ -128,10 +126,16 @@ pub(crate) mod with_https {
}

test_sval(&repo);

repo.update().expect("Failed to fetch crates.io index");

test_sval(&repo);

let time_before_setting_commit_to_past = repo.time().unwrap();
repo.set_commit_from_refspec("@~100").unwrap();
assert_ne!(
repo.time().unwrap(),
time_before_setting_commit_to_past,
"different commits have different times"
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod mem {
peak = ByteSize(ALLOCATOR.max_allocated() as u64),
);
assert!(
per_crate < 7150,
per_crate < 10_000,
"per crate limit {per_crate}B should remain below memory limit"
);
}
Expand Down

0 comments on commit 42e2d22

Please sign in to comment.