-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add tag support #62
base: master
Are you sure you want to change the base?
Add tag support #62
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... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover todo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
panic
ing when the reference isn't found definitely isn't what we should do, especially considering this can be used as a library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/utils/git.rs
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is more idiomatically done with ref_names.map(|n| ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/utils/test_helper.rs
Outdated
@@ -87,6 +87,13 @@ pub fn create_file_with_text<P: AsRef<Path>>(path: P, text: &str) -> () { | |||
file.write_all(text.as_bytes()).unwrap(); | |||
} | |||
|
|||
|
|||
pub fn tag_lightweight<'repo>(repo: &'repo Repository, tag_name: &str) -> Reference<'repo> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does lightweight mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we already have signing set up for the integration tests we may as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I accept this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should delete the commented lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're out as of 2fc4cdc
src/rsl.rs
Outdated
@@ -232,7 +237,8 @@ impl<'remote, 'repo> RSL<'remote, 'repo> { | |||
let mut revwalk: Revwalk = self.repo.revwalk()?; | |||
revwalk.push(self.remote_head)?; | |||
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space after let
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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"), | ||
Some(v) => v.to_owned(), | ||
}; | ||
// TODO - reduce code duplication across the top level of the binaries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move todo into an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9acbc9f and issue incoming
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use error handling here instead of unwrapping with expect (cause it's basically just a panic)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/rsl.rs
Outdated
) -> Result<Option<PushEntry>> { | ||
let mut revwalk: Revwalk = self.repo.revwalk()?; | ||
revwalk.push(self.remote_head)?; | ||
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/integration_test.rs
Outdated
assert_eq!(res3, ()); | ||
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines need to be wrapped somehow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2fc4cdc dig the fancy macro
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")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/integration_test.rs
Outdated
assert_eq!(res2, ()); | ||
|
||
attack::rollback(&context.remote, "master"); | ||
|
||
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap long lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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