Skip to content

Accept ToComponents in gix_fs::Stack #1909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2025

Conversation

cruessler
Copy link
Contributor

This is a draft PR, intended to make sure I got the task right before making any larger changes that I would need to make now that I’ve changed gix_fs::Stack’s API. It is expected that it does not even compile at this stage.

A few observations:

  • There’s places that currently call Stack::new with a PathBuf. Would it make sense to impl ToComponents for PathBuf?
  • Having to add bstr as a dependency made me wonder: is gix-fs the right crate for ToComponents to live in or should it be moved somewhere else, e. g. into gix-path? gix-path also has os_string_into_bstring which should probably replace the chain .as_os_str().as_bytes().into() that I had to resort to on one line because gix-fs does not depend on gix-path currently.

Let me know what you think!

Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting started on this, it's much appreciated!

  • There’s places that currently call Stack::new with a PathBuf. Would it make sense to impl ToComponents for PathBuf?

No - I have made some more comments in code with details.

  • Having to add bstr as a dependency made me wonder: is gix-fs the right crate for ToComponents to live in or should it be moved somewhere else, e. g. into gix-path? gix-path also has os_string_into_bstring which should probably replace the chain .as_os_str().as_bytes().into() that I had to resort to on one line because gix-fs does not depend on gix-path currently.

Indeed, I think gix-path is the better spot for it. The reason is also that I think it will harbour the RepositoryPathBuf/RepositoryPath one day, which will be the typed version of a BString as we use it today.

The place where ToComponents needs to go is make_relative_path_current() and its callers. Then the callers have to be adjusted to avoid calls to gix::path::<conversion> as these shouldn't be needed anymore.

I hope that helps.

Comment on lines 68 to 70
root: PathBuf,
/// the most recent known cached that we know is valid.
current: PathBuf,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have to stay PathBuf as the type is a link between relative paths and the filesystem location they represent.
It's also in the interest of performance to keep it close to what it ought to represent.

impl Stack {
/// Create a new instance with `root` being the base for all future paths we handle, assuming it to be valid which includes
/// symbolic links to be included in it as well.
pub fn new(root: PathBuf) -> Self {
pub fn new(root: impl ToComponents) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the root and it is a filesystem location, it must remain a PathBuf.

ToComponents is really only for the relative paths that are passed in.
It's interesting that ToComponents currently doesn't convey information about whether the component is coming from a relative path, which kind of is a problem, but maybe one that can be tackled later.

@cruessler
Copy link
Contributor Author

cruessler commented Mar 24, 2025

I’m running into an issue when trying to turn relative: &Path into relative: impl gix_path::ToComponents. The !matches!(comp, Component::Normal(_)) in the code below does not work anymore when omponents is Vec<&BStr> instead of Components<'_>. It seems we would already need something like RepositoryPath for that matches! to work properly. Or am I missing anything? Removing the check does not seem to be an option as it would alter the function’s behaviour.

if !matches!(comp, Component::Normal(_)) {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Input path \"{}\" contains relative or absolute components",
relative.display()
),
));
}

@Byron
Copy link
Member

Byron commented Mar 24, 2025

I think the problem here is twofold:

  • the Stack wants to (and has to) build an actual Path, so each component of the iterator needs to be an OsStr`, which is fallible at least on Windows.
  • The conversion (and component) check could be put into the iterator itself (especially if it is fallible)

To me this means the trait would have to be owned by gix-fs for now as it needs to assure there are no non-normal components. Maybe that would be a start?

Another conclusion here probably is that there will be many ToComponents implementations, with encoding different requirements, and that's probably fine as well. We might consider naming them accordingly, this one here could be a ToPathComponent for instance.

@cruessler cruessler force-pushed the take-to-components-in-fs-stack branch from 177cd5b to 40e8e51 Compare March 25, 2025 20:00
@cruessler
Copy link
Contributor Author

I hope what I just pushed is closer to your suggestions, even though the new trait ToPathComponents lives in gix-path instead of gix-fs. So far, I’ve tried to change as little as possible of the existing code. That’s the reason I’ve added .collect() because that way .is_empty() can still be used.

@Byron Byron force-pushed the take-to-components-in-fs-stack branch from 5b7c771 to 788a7c7 Compare March 26, 2025 00:50
@Byron
Copy link
Member

Byron commented Mar 26, 2025

Thanks a lot - this looks like it should work!
Something I don't understand is how the MSRV check can fail while ToComponents in gix does the very same - it is using a feature that ought to be available from Rust 1.75, just one version above the current MSRV which has to be held until helix upgrades their MSRV. Let's check :).
As a next step, all the callers of make_relative_path_current() would have to be adjusted to use the trait too - in theory this won't be a breaking change as the trait will then be extended to be more compatible.
Please feel free to merge my commit down into yours and adjust the subject lines accordingly, as I changed names once again 😅. Oh, and it felt much better to keep ToNormalPathComponents where it is used and tested, so I moved it. This means the previous commits now would need to be reworked for sure.

@pascalkuthe Would helix be able to update their MSRV to something newer?

@Byron Byron force-pushed the take-to-components-in-fs-stack branch 5 times, most recently from 34deb0b to 3d8ad4d Compare March 26, 2025 02:41
@pascalkuthe
Copy link
Contributor

I didn't quite get which MSRV version you are looking for. Is it 1.75? That would be fine for us.

@Byron
Copy link
Member

Byron commented Mar 26, 2025 via email

@cruessler cruessler force-pushed the take-to-components-in-fs-stack branch from 3d8ad4d to 0280e80 Compare March 26, 2025 11:38
@cruessler
Copy link
Contributor Author

I’ve rebased and merged the commits, so now there’s only 2 left which definitely is a simplification. Before I randomly start changing callers, up to which level do we want to accept relative_path: ToNormalPathComponents?

@Byron
Copy link
Member

Byron commented Mar 26, 2025

A good point! There shouldn't be too many to begin with, and it looks like there are 3 in production code.

Screenshot 2025-03-27 at 06 44 21

I could probably take it from here if you wanted me to.

@cruessler cruessler marked this pull request as ready for review March 27, 2025 16:41
@cruessler
Copy link
Contributor Author

cruessler commented Mar 27, 2025

Feel free to take over! I’ve just marked this PR as ready and will have a look at the next item on your list of suggestions!

@Byron Byron self-assigned this Mar 27, 2025
@Byron Byron force-pushed the take-to-components-in-fs-stack branch 2 times, most recently from d29a179 to 8de6128 Compare March 30, 2025 04:28
@Byron Byron enabled auto-merge March 30, 2025 04:30
@Byron Byron force-pushed the take-to-components-in-fs-stack branch 2 times, most recently from 7591f37 to 84e1049 Compare March 30, 2025 04:44
@Byron Byron force-pushed the take-to-components-in-fs-stack branch from 84e1049 to 1f98edb Compare March 30, 2025 05:25
@Byron Byron merged commit 5cb5337 into GitoxideLabs:main Mar 30, 2025
21 checks passed
EliahKagan added a commit to EliahKagan/gitoxide that referenced this pull request May 9, 2025
This changes `cargo check` commands to `cargo build` in the
`ci-check-msrv` recipe in the `justfile`, which the `check-msrv` CI
jobs in `msrv.yml` use. (It updates a CI step name accordingly.)

The idea is to make sure at least `gix`, with the two combinations
of features tested, actually *builds* under the MSRV toolchain.

As in f10f18d, this also updates the `Makefile` rule corresponding
to that `justfile` recipe.

The idea of actually building was suggested in:
GitoxideLabs#1808 (comment)

However, this does not uncover any new breakages. And there has
been further improvement on GitoxideLabs#1808, including in the commits leading
up to this, as well as earlier, in 569c186 (GitoxideLabs#1909). Nonetheless, it
seems likely that some problems remain with some combinations of
crates and features that are not currently exercised in the MSRV
check. GitoxideLabs#1808 is most likely not yet fully fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants