Conversation
Prior to this commit, we were wrapping strings representing branch names into BranchName types to reduce potential for misuse. This commit renames that to ReferenceName to reflect upcoming tag support.
Prior to this commit, the git methods for push were bound to using branches. This commit removes that and generalizes it to enable pushing a branch or tag ref. It adds a new test for tag pushing as well as a helper method to tag the latest commit in a repo. It also updates the integration tests to use the ReferenceName type.
Prior to this commit, the help prompts for the binaries still told the user that they need to supply BRANCH references. This commit generalizes that to include branches OR tags by using REFERENCE
Prior to this commit, push entries would try to locate a branch using the shorthand name provided from the user. This was problematic because if the user was attempting to push a tag, the operation would fail. This commit changes it to instead use the git helper method to find a git2::Reference using the shorthand name, so that we can stash the target git2::Oid of either a branch OR a tag in the RSL push entry.
Prior to this commit, the `find_remote_push_entry_for_branch` method tried to use the shorthand name to locate the full reference name. This commit changes it to instead find the reference, either a branch or a tag, and get that name instead. This allows this method to locate RSL push entries for tags as well as branches.
Prior to this commit, methods to locate refererences that had been previously changed from locating branches had not been renamed to reflect their new functionality. This commit does all that stuff.
src/utils/git.rs
Outdated
| fn get_ref_from_name<'repo>(repo: &'repo Repository, name: &str) -> Option<Reference<'repo>> { | ||
| match repo.find_branch(name, BranchType::Local) { | ||
| Ok(branch) => Some(branch.into_reference()), | ||
| Err(e) => { // toDo may be other errors here... |
src/utils/git.rs
Outdated
| match repo.find_branch(name, BranchType::Local) { | ||
| Ok(branch) => Some(branch.into_reference()), | ||
| Err(e) => { // toDo may be other errors here... | ||
| let tag_ref = repo.find_reference(&format!("refs/tags/{}", name)).expect("reference not found"); |
There was a problem hiding this comment.
panicing when the reference isn't found definitely isn't what we should do, especially considering this can be used as a library.
| let reference = get_ref_from_name(&repo, name).expect("failed to find reference from name"); | ||
| let full_name = String::from(reference.name().expect("failed to get full name of ref")); | ||
| full_names.push(full_name) | ||
| } |
There was a problem hiding this comment.
this is more idiomatically done with ref_names.map(|n| ...)
src/utils/git.rs
Outdated
| let mut full_names: Vec<String> = vec![]; | ||
| for name in ref_names { | ||
| let reference = get_ref_from_name(&repo, name).expect("failed to find reference from name"); | ||
| let full_name = String::from(reference.name().expect("failed to get full name of ref")); |
There was a problem hiding this comment.
We shouldn't panic here either; basically, down in the internal modules, nothing should ever panic. It's more forgivable at the top level of a CLI application.
src/utils/test_helper.rs
Outdated
| } | ||
|
|
||
|
|
||
| pub fn tag_lightweight<'repo>(repo: &'repo Repository, tag_name: &str) -> Reference<'repo> { |
There was a problem hiding this comment.
It's basically just a pointer vs an annotated tag with all kinds of other information. I feel like for the test I probably don't need to create a full on annotated tag, but am open to doing that instead if it makes more sense.
There was a problem hiding this comment.
If we already have signing set up for the integration tests we may as well?
There was a problem hiding this comment.
Ok, I accept this
tests/integration_test.rs
Outdated
| do_work_on_branch(&context.local, "refs/heads/master"); | ||
| // let local_tag = tag_lightweight(&mut context.local, "v6.66"); | ||
|
|
||
| // git_rsl::utils::git::push(&context.local, &mut context.local.find_remote("origin").expect("failed to find remote"), &["v6.66"]); |
There was a problem hiding this comment.
should delete the commented lines
src/rsl.rs
Outdated
| let mut current = Some(self.remote_head); | ||
| let ref_name = format!("refs/heads/{}", branch); | ||
| let reference = git::get_ref_from_name(self.repo, branch).expect("failed to get ref"); | ||
| let ref_name = reference.name().expect("failed to get ref name"); |
windwardrail
left a comment
There was a problem hiding this comment.
Most of my comments are on error handling. I didn't flag all the instances, but using expect is essentially a panic! which we should avoid in favor of error handling such as Result or error chain.
Also there are long lines that should be wrapped. Didn't flag all of those either.
src/bin/git-secure-fetch.rs
Outdated
| None => panic!("Must supply a REFERENCE argument"), | ||
| Some(v) => v.to_owned(), | ||
| }; | ||
| // TODO - reduce code duplication across the top level of the binaries |
| let branch = match matches.value_of("BRANCH") { | ||
| None => panic!("Must supply a BRANCH argument"), | ||
| let reference = match matches.value_of("REFERENCE") { | ||
| None => panic!("Must supply a REFERENCE argument"), |
There was a problem hiding this comment.
It would be nice to avoid panics. You could use bail! from error chain if you moved the logic in main to another function and then called handle_error from main?
There was a problem hiding this comment.
I think that this specific case should probably go into the issue tracker and be addressed as part of a different PR, since this one is really just supposed to be adding tag support.
src/push_entry.rs
Outdated
| .get() | ||
| .target() | ||
| .unwrap(); | ||
| let full_ref = utils::git::get_ref_from_name(repo, branch_str).expect("failed to get reference"); |
There was a problem hiding this comment.
Use error handling here instead of unwrapping with expect (cause it's basically just a panic)
src/push_entry.rs
Outdated
| .target() | ||
| .unwrap(); | ||
| let full_ref = utils::git::get_ref_from_name(repo, branch_str).expect("failed to get reference"); | ||
| let target_oid = full_ref.target().expect("failed to get target oid"); |
src/push_entry.rs
Outdated
| PushEntry { | ||
| ref_name: format!("refs/heads/{}", branch_str), | ||
| oid: branch_head, | ||
| ref_name: String::from(full_ref.name().expect("failed to get ref's full name")), |
src/rsl.rs
Outdated
| let mut current = Some(self.remote_head); | ||
| let ref_name = format!("refs/heads/{}", branch); | ||
| let reference = git::get_ref_from_name(self.repo, reference).expect("failed to get ref"); | ||
| let ref_name = reference.name().expect("failed to get ref name"); |
tests/integration_test.rs
Outdated
|
|
||
| do_work_on_branch(&context.local, "refs/heads/master"); | ||
| let res4 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("Could not run third push"); | ||
| let res4 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("Could not run third push"); |
There was a problem hiding this comment.
These lines need to be wrapped somehow
tests/integration_test.rs
Outdated
| let remote_tag = &context.remote.find_reference("refs/tags/v6.66").expect("reference not found"); | ||
| assert!(remote_tag.is_tag()); | ||
|
|
||
| assert_eq!((), git_rsl::secure_fetch_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("v6.66")).expect("could not fetch tag")); |
tests/integration_test.rs
Outdated
|
|
||
| do_work_on_branch(&context.local, "refs/heads/master"); | ||
| let res3 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect_err("Checking for invalid RSL detection"); | ||
| let res3 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect_err("Checking for invalid RSL detection"); |
|
Compiler warnings... |
This commit cleans up some formatting issues and implements a macro for checking that method calls are ok in the integration tests file.
|
@windwardrail compiler warnings fixed as of 2fc4cdc |
Prior to this commit, the tag test created a lightweight tag for tagging a specific release. This commit changes the helper method and test to instead create a more informative annotated tag.
Prior to this commit, I was doing this awkwardly. This commit uses map instead.
Prior to this commit, the src files for the binaries still had toDo's in them. Those toDos are still things that we need toDo, but would be better served being represented as issues in the issue tracker, so this commit removes them from the code.
Prior to this commit, there were lots of panics in the new lib functions. This commit adds in error handling for those cases.
|
Addressed all the comments. I'd appreciate another quick look through when you have the chance @windwardrail @mullr |
This PR adds support for secure-push/fetch of tags to git-rsl.
Closes #48