Skip to content

StashSaveOptions::pathspec(): return a Result, handle invalid values#1295

Open
DanielEScherzer wants to merge 2 commits into
rust-lang:mainfrom
DanielEScherzer:stash-save-opts-pathspec
Open

StashSaveOptions::pathspec(): return a Result, handle invalid values#1295
DanielEScherzer wants to merge 2 commits into
rust-lang:mainfrom
DanielEScherzer:stash-save-opts-pathspec

Conversation

@DanielEScherzer

@DanielEScherzer DanielEScherzer commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

When the provided path spec cannot be converted to a CString, return an Err variant rather than panicking.

See also

… specs

The test is currently marked as `#[should_panic]` because the implementation
panics when the path given is invalid, a follow-up commit will switch to
returning a `Result`.
When the provided path spec cannot be converted to a `CString`, return an `Err`
variant rather than panicking.
@rustbot rustbot added the S-waiting-on-review Status: Waiting on review label Jul 16, 2026
@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

@rustbot label +semver-breaking-change

@rustbot rustbot added the semver-breaking-change A SemVer breaking change that will require a new SemVer-incompatible version number bump. label Jul 16, 2026
Comment thread src/stash.rs
/// Add to the array of paths patterns to build the stash.
pub fn pathspec<T: IntoCString>(&mut self, pathspec: T) -> &mut Self {
let s = util::cstring_to_repo_path(pathspec).unwrap();
pub fn pathspec<T: IntoCString>(&mut self, pathspec: T) -> Result<&mut Self, Error> {

@weihanglo weihanglo Jul 16, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know we kinda work towards this though it is still a bit annoying to users. Most of the people won't hit this issue and they need to take care of Result. I am a bit unsure.

Should we just make it an expect and document it would panic?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could, but I think it would be nicer to return a result - if the library is able to avoid a panic without being left in a problematic state, why not do so?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, and panic should be use mostly as a program invariant. This is kinda like a bad input. However, we can also see this as an invariant of these functions.

I am not sure though. I think technically it is more correct, though sacrifice ergonomic. And it may also be confusing as API caller now don't know how they should handle the error or not unless looking at the function body and realize it only checks bad cstring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could also switch it to requiring that the provided value be a CString rather than just something that can be converted to one?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That also loses ergonomic in another way 😞.

@DanielEScherzer DanielEScherzer Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i figured a result was more ergonomic than requiring a CString, because I think panicking is worse than either

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you point to a realistic workflow where an invalid pathspec reaches this API at runtime and the application needs to recover from it? Like in particular, is this intended for arbitrary user-provided data, or is it primarily addressing the fact that CString conversion is technically fallible?

Note that most OS and filesystem disallowed \0 in the file system

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So I just started working on writing an extension for PHP in Rust that integrates with the git2 rust library for the logic - and that is what I was thinking of when I wanted to reduce the use of panics

Basically, returning a result (from Rust) means that we can throw an exception (in PHP) without needing to do the validation manually

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Basically, returning a result (from Rust) means that we can throw an exception (in PHP) without needing to do the validation manually

I knew the mechanism. Ideally we want to recover from more errors, but in reality what case on application side would hit this? If PHP is going to throw an exception what the difference between that and Rust panic?

BTW, PHP seems to have https://github.com/libgit2/php-git.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PHP exceptions can be caught by the userland code

https://github.com/libgit2/php-git is very much outdated (and I'm doing this to be able to present a workshop on writing extensions in Rust, not just for fun)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Waiting on review semver-breaking-change A SemVer breaking change that will require a new SemVer-incompatible version number bump.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants