Skip to content

Commit

Permalink
Always convert to absolute path before calculating diff
Browse files Browse the repository at this point in the history
  • Loading branch information
escritorio-gustavo committed Jan 26, 2024
1 parent a3ac452 commit df8dc5c
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,39 +144,43 @@ where
let path = path.as_ref();
let base = base.as_ref();

if path.is_absolute() != base.is_absolute() {
if path.is_absolute() {
Some(PathBuf::from(path))
} else {
None
}
let path = if path.is_absolute() {
path.to_path_buf()
} else {
let mut ita = path.components();
let mut itb = base.components();
let mut comps: Vec<Component> = vec![];
loop {
match (ita.next(), itb.next()) {
(None, None) => break,
(Some(a), None) => {
comps.push(a);
comps.extend(ita.by_ref());
break;
}
(None, _) => comps.push(Component::ParentDir),
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
(Some(a), Some(Component::CurDir)) => comps.push(a),
(Some(_), Some(Component::ParentDir)) => return None,
(Some(a), Some(_)) => {
std::env::current_dir().unwrap().join(path)
};

let base = if base.is_absolute() {
base.to_path_buf()
} else {
std::env::current_dir().unwrap().join(base)
};

let mut ita = path.components();
let mut itb = base.components();
let mut comps: Vec<Component> = vec![];
loop {
match (ita.next(), itb.next()) {
(None, None) => break,
(Some(a), None) => {
comps.push(a);
comps.extend(ita.by_ref());
break;
}
(None, _) => comps.push(Component::ParentDir),
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
(Some(a), Some(Component::CurDir)) => comps.push(a),
(Some(_), Some(Component::ParentDir)) => return None,
(Some(a), Some(_)) => {
comps.push(Component::ParentDir);
for _ in itb {
comps.push(Component::ParentDir);
for _ in itb {
comps.push(Component::ParentDir);
}
comps.push(a);
comps.extend(ita.by_ref());
break;
}
comps.push(a);
comps.extend(ita.by_ref());
break;
}
}
Some(comps.iter().map(|c| c.as_os_str()).collect())
}
Some(comps.iter().map(|c| c.as_os_str()).collect())
}

0 comments on commit df8dc5c

Please sign in to comment.