Skip to content

Commit 0cc46ea

Browse files
Improve handling of duplicate paths
Currently if we add a duplicate path to a pathlist, it won't get removed when the changes are unwound. By taking the number of occurrences into account we can at least remove the duplicate that we added correctly. We don't have enough info to know which occurence to remove so we always remove the ones at the front of the pathlist since that's the most common case.
1 parent 1cf1774 commit 0cc46ea

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/shadowenv.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -293,28 +293,27 @@ fn env_prepend_to_pathlist(env: &mut HashMap<String, String>, a: String, b: Stri
293293
}
294294

295295
fn diff_vecs(oldvec: Vec<&str>, newvec: Vec<&str>) -> (Vec<String>, Vec<String>) {
296-
let mut additions: Vec<String> = vec![];
297-
let mut deletions: Vec<String> = vec![];
296+
let mut delta: BTreeMap<&str, i32> = BTreeMap::new();
298297

299-
let mut oldset: HashSet<String> = HashSet::new();
300-
for oldval in &oldvec {
301-
oldset.insert(oldval.to_string());
298+
for &item in &oldvec {
299+
*delta.entry(item).or_insert(0) -= 1;
302300
}
303-
304-
let mut newset: HashSet<String> = HashSet::new();
305-
for newval in &newvec {
306-
newset.insert(newval.to_string());
301+
for &item in &newvec {
302+
*delta.entry(item).or_insert(0) += 1;
307303
}
308304

309-
for oldval in oldvec {
310-
if !newset.contains(oldval) {
311-
deletions.push(oldval.to_string());
312-
}
313-
}
305+
let mut additions = Vec::new();
306+
let mut deletions = Vec::new();
314307

315-
for newval in newvec {
316-
if !oldset.contains(newval) {
317-
additions.push(newval.to_string());
308+
for (&item, &count) in &delta {
309+
if count > 0 {
310+
for _ in 0..count {
311+
additions.push(item.to_string());
312+
}
313+
} else if count < 0 {
314+
for _ in 0..(-count) {
315+
deletions.push(item.to_string());
316+
}
318317
}
319318
}
320319

@@ -390,13 +389,13 @@ mod tests {
390389
],
391390
lists: vec![List {
392391
name: "PATH".to_string(),
393-
additions: vec!["/path4".to_string(), "/path3".to_string()],
392+
additions: vec!["/path3".to_string(), "/path4".to_string()],
394393
deletions: vec!["/path1".to_string()],
395394
}],
396395
prev_dirs: Default::default(),
397396
};
398397

399-
let expected_formatted_data = r#"00000000075bcd15:{"scalars":[{"name":"VAR_A","original":"v0","current":"v2","no_clobber":false},{"name":"VAR_B","original":"v0","current":null,"no_clobber":false},{"name":"VAR_C","original":null,"current":"v3","no_clobber":false}],"lists":[{"name":"PATH","additions":["/path4","/path3"],"deletions":["/path1"]}],"prev_dirs":[]}"#;
398+
let expected_formatted_data = r#"00000000075bcd15:{"scalars":[{"name":"VAR_A","original":"v0","current":"v2","no_clobber":false},{"name":"VAR_B","original":"v0","current":null,"no_clobber":false},{"name":"VAR_C","original":null,"current":"v3","no_clobber":false}],"lists":[{"name":"PATH","additions":["/path3","/path4"],"deletions":["/path1"]}],"prev_dirs":[]}"#;
400399

401400
assert_eq!(shadowenv.shadowenv_data(), expected);
402401

0 commit comments

Comments
 (0)