Skip to content

Commit

Permalink
Merge pull request cargo-generate#241 from 9999years/use-head-branch
Browse files Browse the repository at this point in the history
 Use default remote branch instead of `master`
  • Loading branch information
k0pernicus committed Sep 12, 2020
2 parents 6bd4e33 + b9320b6 commit c72a162
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
25 changes: 12 additions & 13 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct GitConfig {
}

impl GitConfig {
pub fn new(git: String, branch: String) -> Result<Self, failure::Error> {
pub fn new(git: String, branch: GitReference) -> Result<Self, failure::Error> {
let remote = match Url::parse(&git) {
Ok(u) => u,
Err(ParseError::RelativeUrlWithoutBase) => {
Expand All @@ -34,10 +34,7 @@ impl GitConfig {
Err(_) => return Err(format_err!("Failed parsing git remote: {}", &git)),
};

Ok(GitConfig {
remote,
branch: GitReference::Branch(branch),
})
Ok(GitConfig { remote, branch })
}
}

Expand All @@ -59,12 +56,14 @@ pub fn remove_history(project_dir: &PathBuf) -> Result<(), failure::Error> {
Ok(())
}

pub fn init(project_dir: &PathBuf, branch: &str) -> Result<GitRepository, failure::Error> {
Ok(GitRepository::init_opts(
project_dir,
RepositoryInitOptions::new()
.bare(false)
.initial_head(branch),
)
.context("Couldn't init new repository")?)
pub fn init(
project_dir: &PathBuf,
branch: Option<String>,
) -> Result<GitRepository, failure::Error> {
let mut opts = RepositoryInitOptions::new();
opts.bare(false);
if let Some(branch) = branch {
opts.initial_head(&branch);
}
Ok(GitRepository::init_opts(project_dir, &opts).context("Couldn't init new repository")?)
}
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod template;
use crate::git::GitConfig;
use crate::projectname::ProjectName;
use cargo;
use cargo::core::GitReference;
use config::{Config, CONFIG_FILE_NAME};
use console::style;
use std::env;
Expand Down Expand Up @@ -77,13 +78,17 @@ pub fn generate(args: Args) -> Result<(), failure::Error> {

fn create_git(args: Args, name: &ProjectName) -> Result<(), failure::Error> {
let force = args.force;
let branch = args.branch.unwrap_or_else(|| "master".to_string());
let config = GitConfig::new(args.git, branch.clone())?;
let branch_str = args.branch.clone();
let branch = args
.branch
.map(GitReference::Branch)
.unwrap_or_else(|| GitReference::Rev("FETCH_HEAD".to_string()));
let config = GitConfig::new(args.git.clone(), branch)?;
let verbose = args.verbose;
if let Some(dir) = &create_project_dir(&name, force) {
match git::create(dir, config) {
Ok(_) => {
git::remove_history(dir).unwrap_or(progress(name, dir, force, &branch, verbose)?)
git::remove_history(dir).unwrap_or(progress(name, dir, force, branch_str, verbose)?)
}
Err(e) => failure::bail!(
"{} {} {}",
Expand Down Expand Up @@ -134,7 +139,7 @@ fn progress(
name: &ProjectName,
dir: &PathBuf,
force: bool,
branch: &str,
branch: Option<String>,
verbose: bool,
) -> Result<(), failure::Error> {
let template = template::substitute(name, force)?;
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ version = "0.1.0"

#[test]
fn it_allows_a_git_branch_to_be_specified() {
// Build and commit on master
// Build and commit on branch named 'main'
let template = dir("template")
.file(
"Cargo.toml",
Expand Down Expand Up @@ -570,7 +570,7 @@ fn it_respects_template_branch_name() {
Command::new("git")
.arg("branch")
.arg("-m")
.arg("master")
.arg("main")
.arg("gh-pages")
.current_dir(template.path())
.assert()
Expand Down
27 changes: 23 additions & 4 deletions tests/integration/helpers/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ impl ProjectBuilder {
self
}

/// On Git >=2.28.0 `init.defaultBranch` can be set to change the default initial branch name
/// to something other than `master`. Calling this function after the first commit makes sure
/// the initial branch is named `main` in all our integration tests so that they're not
/// effected by `init.defaultBranch`.
fn rename_branch_to_main(&self) {
use assert_cmd::prelude::*;
std::process::Command::new("git")
.arg("branch")
.arg("--move")
.arg("main")
.current_dir(&self.root)
.assert()
.success();
}

pub fn build(self) -> Project {
drop(remove_dir_all(&self.root));
fs::create_dir_all(&self.root)
Expand Down Expand Up @@ -93,11 +108,11 @@ impl ProjectBuilder {
.success();

if let Some(ref branch) = self.branch {
// Create dummy content in master to aid testing
// Create dummy content in "main" branch to aid testing

fs::File::create(self.root.join("dummy.txt"))
.expect("Failed to create dummy")
.write_all(b"master dummy")
.write_all(b"main dummy")
.expect("Couldn't write out dummy text");

Command::new("git")
Expand All @@ -110,11 +125,13 @@ impl ProjectBuilder {
Command::new("git")
.arg("commit")
.arg("--message")
.arg("initial master commit")
.arg("initial main commit")
.current_dir(&self.root)
.assert()
.success();

self.rename_branch_to_main();

Command::new("git")
.arg("checkout")
.arg("-b")
Expand Down Expand Up @@ -153,10 +170,12 @@ impl ProjectBuilder {
if self.branch.is_some() {
Command::new("git")
.arg("checkout")
.arg("master")
.arg("main")
.current_dir(&self.root)
.assert()
.success();
} else {
self.rename_branch_to_main();
}
}

Expand Down

0 comments on commit c72a162

Please sign in to comment.