-
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
Changes from 1 commit
33bc92a
52b791e
5315bc7
e368a1c
478d4d3
9febba0
2fc4cdc
f687fb8
b776d8f
9acbc9f
638721c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
use std::env; | ||
use std::{env, str}; | ||
use std::path::Path; | ||
|
||
use git2; | ||
use git2::BranchType; | ||
use git2::build::CheckoutBuilder; | ||
use git2::{Commit, DiffOptions, FetchOptions, Oid, PushOptions, Remote, RemoteCallbacks, | ||
Repository, RepositoryState, Signature, Tree}; | ||
Repository, RepositoryState, Signature, Tree, Reference}; | ||
|
||
use git2::CredentialType; | ||
use git2::MergeAnalysis; | ||
|
@@ -267,21 +267,31 @@ pub fn fetch( | |
}) | ||
} | ||
|
||
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... | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if tag_ref.is_tag() { | ||
Some(tag_ref) | ||
} else { // not a branch or a tag | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Push the branches or tags given in ref_names | ||
pub fn push(repo: &Repository, remote: &mut Remote, ref_names: &[&str]) -> Result<()> { | ||
let refs: Vec<String> = ref_names | ||
.iter() | ||
.map(|name: &&str| { | ||
format!( | ||
"refs/heads/{}:refs/heads/{}", | ||
name.to_string(), | ||
name.to_string() | ||
) | ||
}) | ||
.collect(); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
full_names.push(full_name) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is more idiomatically done with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
let mut refspecs: Vec<&str> = vec![]; | ||
for name in &refs { | ||
for name in &full_names { | ||
refspecs.push(name) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ use super::git; | |
use fs_extra::dir::*; | ||
use tempdir::TempDir; | ||
|
||
use git2::Repository; | ||
use git2::{Repository, Object, ObjectType, Reference}; | ||
|
||
pub struct Context { | ||
pub local: Repository, | ||
|
@@ -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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||
let tag_target = &repo.head().expect("failed to get head").peel(ObjectType::Commit).expect("failed to peel"); | ||
let tag_oid = repo.tag_lightweight(tag_name, tag_target, false); | ||
repo.find_reference(&format!("refs/tags/{}", tag_name)).expect("tag ref not found") | ||
} | ||
|
||
pub fn do_work_on_branch(repo: &Repository, branch_name: &str) -> () { | ||
git::checkout_branch(&repo, branch_name).unwrap(); | ||
git::add_and_commit(&repo, None, "a commit with some work", branch_name).unwrap(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ extern crate tempdir; | |
mod utils; | ||
|
||
use git_rsl::utils::test_helper::*; | ||
use git_rsl::{BranchName, RemoteName}; | ||
use git_rsl::{ReferenceName, RemoteName}; | ||
use std::process::Command; | ||
use std::sync::Mutex; | ||
use utils::attack; | ||
|
@@ -30,36 +30,54 @@ macro_rules! sequential_test { | |
} | ||
|
||
sequential_test! { | ||
fn push_and_fetch() { | ||
fn push_and_fetch_branch() { | ||
let mut context = setup_fresh(); | ||
{ | ||
assert_eq!((), git_rsl::rsl_init_with_cleanup(&mut context.local, &RemoteName::new("origin")) | ||
.expect("Could not rsl-init")); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("Could not run first push"); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("Could not run first push"); | ||
assert_eq!(res, ()); | ||
do_work_on_branch(&context.local, "refs/heads/master"); | ||
|
||
let res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("Could not run second push"); | ||
let res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("Could not run second push"); | ||
assert_eq!(res2, ()); | ||
|
||
let res3 = git_rsl::secure_fetch_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("Could not run fetch"); | ||
let res3 = git_rsl::secure_fetch_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("Could not run fetch"); | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 2fc4cdc dig the fancy macro |
||
assert_eq!(res4, ()); | ||
// TODO check that the git log of RSL looks how we want it to | ||
} | ||
} | ||
} | ||
|
||
sequential_test! { | ||
fn push_and_fetch_tag() { | ||
let mut context = setup_fresh(); | ||
{ | ||
assert_eq!((), git_rsl::rsl_init_with_cleanup(&mut context.local, &RemoteName::new("origin")) | ||
.expect("Could not rsl-init")); | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. They're out as of 2fc4cdc |
||
assert_eq!((), git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("v6.66")).expect("Could not run third push")); | ||
let remote_tag = &context.remote.find_reference("refs/tags/v6.66").expect("reference not found"); | ||
// assert_eq!(local_tag.target(), remote_tag.target()) | ||
assert!(remote_tag.is_tag()); | ||
} | ||
} | ||
} | ||
|
||
sequential_test! { | ||
fn error_handling() { | ||
let mut context = setup_fresh(); | ||
{ | ||
assert_eq!((), git_rsl::rsl_init_with_cleanup(&mut context.local, &RemoteName::new("origin")) | ||
.expect("Could not rsl-init")); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).unwrap(); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).unwrap(); | ||
assert_eq!(res, ()); | ||
|
||
let nonce_file = context.repo_dir.join(".git/NONCE"); | ||
|
@@ -70,7 +88,7 @@ sequential_test! { | |
.expect("failed to change permissions"); | ||
|
||
do_work_on_branch(&context.local, "refs/heads/master"); | ||
let _res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).unwrap_err(); | ||
let _res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).unwrap_err(); | ||
let head = context.local.head().unwrap().name().unwrap().to_owned(); | ||
assert_eq!(head, "refs/heads/master"); | ||
|
||
|
@@ -84,11 +102,11 @@ sequential_test! { | |
{ | ||
assert_eq!((), git_rsl::rsl_init_with_cleanup(&mut context.local, &RemoteName::new("origin")) | ||
.expect("Could not rsl-init")); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("First push failed"); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("First push failed"); | ||
assert_eq!(res, ()); | ||
do_work_on_branch(&context.local, "refs/heads/master"); | ||
|
||
let res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("Second push failed"); | ||
let res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("Second push failed"); | ||
assert_eq!(res2, ()); | ||
} | ||
} | ||
|
@@ -100,17 +118,17 @@ sequential_test! { | |
{ | ||
assert_eq!((), git_rsl::rsl_init_with_cleanup(&mut context.local, &RemoteName::new("origin")) | ||
.expect("Could not rsl-init")); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("First push failed"); | ||
let res = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("First push failed"); | ||
assert_eq!(res, ()); | ||
do_work_on_branch(&context.local, "refs/heads/master"); | ||
|
||
let res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &BranchName::new("master")).expect("Second push failed"); | ||
let res2 = git_rsl::secure_push_with_cleanup(&mut context.local, &RemoteName::new("origin"), &ReferenceName::new("master")).expect("Second push failed"); | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||
assert_eq!(res3.description(), "invalid remote RSL"); | ||
} | ||
} | ||
|
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.
2fc4cdc