Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
feat(proxy): push all branches when creating a project (#1278)
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolfs committed Nov 18, 2020
1 parent b1a2a5f commit 930d3d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
32 changes: 30 additions & 2 deletions proxy/api/src/http/project.rs
Expand Up @@ -481,7 +481,23 @@ mod test {
};

// Create the repository for which we'll create a project for
coco::control::clone_platinum(repo_path)?;
coco::control::clone_platinum(repo_path.clone())?;

let repo = git2::Repository::open(repo_path)?;

let branches = repo
.branches(None)?
.filter_map(|branch_result| {
let (branch, _) = branch_result.ok()?;
let name = branch.name().ok()?;
name.map(String::from)
})
.collect::<Vec<String>>();

assert_eq!(
branches,
vec!["dev", "master", "origin/dev", "origin/master"]
);

let res = request()
.method("POST")
Expand All @@ -507,7 +523,7 @@ mod test {
},
"shareableEntityIdentifier": format!("%{}", meta.id.to_string()),
"stats": {
"branches": 1,
"branches": 2,
"commits": 15,
"contributors": 4,
},
Expand All @@ -516,6 +532,18 @@ mod test {
assert_eq!(res.status(), StatusCode::CREATED);
assert_eq!(have, want);

let api2 = http::source::filters(ctx.clone().into());
let res2 = request()
.method("GET")
.path(&format!("/revisions/{}", meta.id))
.reply(&api2)
.await;

let have2: Value = serde_json::from_slice(res2.body()).unwrap();

assert_eq!(res2.status(), StatusCode::OK);
assert_eq!(have2[0]["branches"], json!(["dev", "master"]));

Ok(())
}

Expand Down
30 changes: 17 additions & 13 deletions proxy/coco/src/project/create/validation.rs
@@ -1,14 +1,11 @@
//! Validation logic for safely checking that a [`super::Repo`] is valid before setting up the
//! working copy.

use std::{io, marker::PhantomData, path::PathBuf};
use std::{io, path::PathBuf};

use librad::{
git::{
local::url::LocalUrl,
types::{remote::Remote, FlatRef},
},
git_ext::{self, OneLevel, RefLike},
git::{local::url::LocalUrl, types::remote::Remote},
git_ext::{self, OneLevel},
std_ext::result::ResultExt as _,
};
use radicle_surf::vcs::git::git2;
Expand Down Expand Up @@ -266,16 +263,23 @@ impl Repository {
log::debug!("Creating rad remote");
let mut git_remote = Self::existing_remote(repo, &url)?
.map_or_else(|| Remote::rad_remote(url, None).create(repo), Ok)?;
Self::push_default(&mut git_remote, default_branch)?;
Self::push_branches(repo, &mut git_remote)?;
Ok(())
}

/// Push the default branch to the provided remote.
fn push_default(remote: &mut git2::Remote, default_branch: &OneLevel) -> Result<(), Error> {
let default: FlatRef<RefLike, _> =
FlatRef::head(PhantomData, None, default_branch.clone().into());
log::debug!("Pushing default branch '{}'", default);
remote.push(&[default.to_string()], None)?;
fn push_branches(repo: &git2::Repository, remote: &mut git2::Remote) -> Result<(), Error> {
let local_branches = repo
.branches(Some(git2::BranchType::Local))?
.filter_map(|branch_result| {
let (branch, _) = branch_result.ok()?;
let name = branch.name().ok()?;
name.map(|branch| format!("refs/heads/{}", branch))
})
.collect::<Vec<String>>();

log::debug!("Pushing branches {:?}", local_branches);

remote.push(&local_branches, None)?;
Ok(())
}

Expand Down

0 comments on commit 930d3d2

Please sign in to comment.