Skip to content

Commit 432a472

Browse files
committed
Fix tests and add a bunch of tracing
1 parent 2bd8a5d commit 432a472

File tree

7 files changed

+78
-11
lines changed

7 files changed

+78
-11
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ nom = "5.1.2"
1919
thiserror = "1.0.30"
2020
tracing = "0.1.30"
2121
tracing-subscriber = { version = "0.3.8", features = ["env-filter"] }
22+
derivative = "2.2.0"
2223

2324
[features]
2425

src/index.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ use std::path::{Path, PathBuf};
1212
use anyhow::{anyhow, bail, Result};
1313
use cookie_factory as cf;
1414
use crypto::sha1::Sha1;
15+
use derivative::Derivative;
1516

1617
use self::checksum::*;
1718
use crate::lockfile::*;
1819
use crate::workspace::*;
1920

21+
#[derive(Derivative)]
22+
#[derivative(Debug)]
2023
pub struct Index {
2124
entries: BTreeMap<PathBuf, Entry>,
2225
parents: HashMap<PathBuf, HashSet<PathBuf>>,
26+
#[derivative(Debug = "ignore")]
2327
file: Option<ChecksummedFile<Lockfile, Sha1>>,
2428
changed: bool,
2529
}
@@ -103,6 +107,7 @@ impl Index {
103107
Ok(index)
104108
}
105109

110+
#[tracing::instrument]
106111
pub fn add(&mut self, file: &WorkspacePath, oid: &str) -> Result<()> {
107112
let metadata = file.stat()?;
108113
let entry = Entry::new(file, oid, &metadata);
@@ -113,18 +118,53 @@ impl Index {
113118
Ok(())
114119
}
115120

121+
#[tracing::instrument]
116122
fn store_entry(&mut self, entry_path: &Path, entry: Entry) {
117123
for path in entry_path.ancestors() {
118124
let paths_for_parent = self.parents.entry(path.to_owned()).or_default();
119125
paths_for_parent.insert(entry_path.to_owned());
120126
}
121127
self.entries.insert(entry_path.to_owned(), entry);
128+
tracing::debug!(?entry_path, entries = ?self.entries, parents = ?self.parents, "entry stored");
122129
}
123130

131+
#[tracing::instrument]
132+
fn remove_entry(&mut self, entry_path: &Path) {
133+
if self.entries.remove(entry_path).is_some() {
134+
for parent in entry_path.ancestors() {
135+
if let Some(paths_for_parent) = self.parents.get_mut(parent) {
136+
paths_for_parent.remove(entry_path);
137+
if paths_for_parent.is_empty() {
138+
self.parents.remove(parent);
139+
}
140+
}
141+
}
142+
tracing::debug!(
143+
?entry_path,
144+
entries = ?self.entries,
145+
parents = ?self.parents,
146+
"entry removed and parents updated"
147+
);
148+
}
149+
}
150+
151+
#[tracing::instrument]
152+
fn remove_children(&mut self, entry_path: &Path) {
153+
if let Some(children) = self.parents.get(entry_path) {
154+
let children = children.clone();
155+
for child in children {
156+
self.remove_entry(&child);
157+
}
158+
tracing::debug!(?entry_path, entries = ?self.entries, "children removed");
159+
}
160+
}
161+
162+
#[tracing::instrument]
124163
fn discard_conflicts(&mut self, path: &Path) {
125164
for parent in path.ancestors() {
126-
self.entries.remove(parent);
165+
self.remove_entry(parent);
127166
}
167+
self.remove_children(path);
128168
}
129169

130170
fn serialize_entries<'a, W: Write + 'a>(
@@ -533,7 +573,7 @@ mod tests {
533573

534574
// Assert
535575
let index_paths = index.iter().map(|entry| &entry.path).collect::<Vec<_>>();
536-
assert_eq!(index_paths, ["alice.txt/nested.txt", "bob.txt"]);
576+
assert_eq!(index_paths, ["alice.txt", "nested"]);
537577
}
538578

539579
#[test]
@@ -582,6 +622,6 @@ mod tests {
582622

583623
// Assert
584624
let index_paths = index.iter().map(|entry| &entry.path).collect::<Vec<_>>();
585-
assert_eq!(index_paths, ["alice.txt/nested.txt", "bob.txt"]);
625+
assert_eq!(index_paths, ["alice.txt", "nested"]);
586626
}
587627
}

src/index/checksum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ use std::iter;
33

44
use anyhow::{Context, Error};
55
use crypto::digest::Digest;
6+
use derivative::Derivative;
67

8+
#[derive(Derivative)]
9+
#[derivative(Debug)]
710
pub struct ChecksummedFile<I, D: Digest> {
811
inner: I,
12+
#[derivative(Debug = "ignore")]
913
hasher: D,
1014
}
1115

src/lockfile.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::PathBuf;
55

66
use anyhow::{bail, Result};
77

8+
#[derive(Debug)]
89
pub struct Lockfile {
910
path: PathBuf,
1011
lock_path: PathBuf,
@@ -53,9 +54,7 @@ impl Write for Lockfile {
5354

5455
impl Drop for Lockfile {
5556
fn drop(&mut self) {
56-
fs::remove_file(&self.lock_path).unwrap_or_else(|_| panic!(
57-
"Failed to remove lock file: {}",
58-
self.lock_path.display()
59-
));
57+
fs::remove_file(&self.lock_path)
58+
.unwrap_or_else(|_| panic!("Failed to remove lock file: {}", self.lock_path.display()));
6059
}
6160
}

src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod database;
22
mod index;
33
mod lockfile;
44
mod refs;
5+
mod telemetry;
56
mod workspace;
67

78
use std::env;
@@ -120,10 +121,7 @@ fn add(paths: Vec<PathBuf>) -> Result<()> {
120121
}
121122

122123
fn main() -> Result<()> {
123-
if std::env::var("RUST_LOG").is_err() {
124-
std::env::set_var("RUST_LOG", "info");
125-
}
126-
tracing_subscriber::fmt::init();
124+
telemetry::init();
127125

128126
let opt = Opt::from_args();
129127
match opt {

src/telemetry.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use tracing_subscriber::fmt::format;
2+
use tracing_subscriber::EnvFilter;
3+
4+
pub fn init() {
5+
if std::env::var("RUST_LOG").is_err() {
6+
std::env::set_var("RUST_LOG", "info");
7+
}
8+
tracing_subscriber::fmt::fmt()
9+
.event_format(format().pretty())
10+
.with_env_filter(EnvFilter::from_default_env())
11+
.init();
12+
}

0 commit comments

Comments
 (0)