Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/utils/git.rs
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;
Expand Down Expand Up @@ -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...
Copy link
Contributor

Choose a reason for hiding this comment

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

leftover todo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let tag_ref = repo.find_reference(&format!("refs/tags/{}", name)).expect("reference not found");
Copy link
Contributor

@mullr mullr Jul 10, 2018

Choose a reason for hiding this comment

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

panicing when the reference isn't found definitely isn't what we should do, especially considering this can be used as a library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"));
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

full_names.push(full_name)
}
Copy link
Contributor

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| ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}

Expand Down
9 changes: 8 additions & 1 deletion src/utils/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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> {
Copy link
Contributor

Choose a reason for hiding this comment

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

what does lightweight mean?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I accept this

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
Expand Down
44 changes: 31 additions & 13 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");

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

Copy link
Contributor Author

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

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"]);
Copy link
Contributor

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

Copy link
Contributor Author

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

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");
Expand All @@ -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");

Expand All @@ -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, ());
}
}
Expand All @@ -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");

Choose a reason for hiding this comment

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

Wrap long lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

assert_eq!(res3.description(), "invalid remote RSL");
}
}
Expand Down