]> git.proxmox.com Git - cargo.git/commitdiff
Create hg repo with --hg flag on cargo new
authorMark J. Berger <mark.berger.j@gmail.com>
Fri, 5 Sep 2014 15:23:57 +0000 (08:23 -0700)
committerMark J. Berger <mark.berger.j@gmail.com>
Mon, 15 Sep 2014 03:06:32 +0000 (20:06 -0700)
src/bin/new.rs
src/cargo/ops/cargo_new.rs
src/cargo/util/mod.rs
src/cargo/util/vcs.rs [new file with mode: 0644]

index 7511746c04c3a8e5176941be90c901ed671a0956..86ce33fdcda03a5df1af7f696148c4d912135e52 100644 (file)
@@ -17,6 +17,7 @@ Options:
     --no-git            Don't initialize a new git repository
     --git               Initialize a new git repository, overriding a
                         global `git = false` configuration
+    --hg                Initialize a new hg repository
     --travis            Create a .travis.yml file
     --bin               Use a binary instead of a library template
     -v, --verbose       Use verbose output
@@ -26,11 +27,13 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
     debug!("executing; cmd=cargo-new; args={}", os::args());
     shell.set_verbose(options.flag_verbose);
 
-    let Options { flag_no_git, flag_travis, flag_bin, arg_path, flag_git, .. } = options;
+    let Options { flag_no_git, flag_travis,
+                  flag_bin,arg_path, flag_git, flag_hg, .. } = options;
 
     let opts = ops::NewOptions {
         no_git: flag_no_git,
         git: flag_git,
+        hg: flag_hg,
         travis: flag_travis,
         path: arg_path.as_slice(),
         bin: flag_bin,
index d74e381e1d89bd0291248488393e4759fe1c5888..edbb4c3f34f7cd71dec63d1935b97f5d51f4e44c 100644 (file)
@@ -1,14 +1,15 @@
 use std::os;
 use std::io::{mod, fs, File};
 
-use git2::{Repository, Config};
+use git2::Config;
 
-use util::{CargoResult, human, ChainError, config, internal};
+use util::{GitRepo, HgRepo, CargoResult, human, ChainError, config, internal};
 use core::shell::MultiShell;
 
 pub struct NewOptions<'a> {
     pub no_git: bool,
     pub git: bool,
+    pub hg: bool,
     pub travis: bool,
     pub bin: bool,
     pub path: &'a str,
@@ -35,15 +36,19 @@ pub fn new(opts: NewOptions, _shell: &mut MultiShell) -> CargoResult<()> {
 
 fn mk(path: &Path, name: &str, opts: &NewOptions) -> CargoResult<()> {
     let cfg = try!(global_config());
-    if !opts.git && (opts.no_git || cfg.git == Some(false)) {
+    let mut ignore = "/target\n".to_string();
+    if !opts.bin {
+        ignore.push_str("/Cargo.lock\n");
+    }
+
+    if opts.hg {
+        try!(HgRepo::init(path));
+        try!(File::create(&path.join(".hgignore")).write(ignore.as_bytes()));
+    } else if !opts.git && (opts.no_git || cfg.git == Some(false)) {
         try!(fs::mkdir(path, io::UserRWX));
     } else {
-        try!(Repository::init(path));
-        let mut gitignore = "/target\n".to_string();
-        if !opts.bin {
-            gitignore.push_str("/Cargo.lock\n");
-        }
-        try!(File::create(&path.join(".gitignore")).write(gitignore.as_bytes()));
+        try!(GitRepo::init(path));
+        try!(File::create(&path.join(".gitignore")).write(ignore.as_bytes()));
     }
 
     let (author_name, email) = try!(discover_author());
index 6b1aa72b20078a34bf0630459eddeee6aebd13ee..33bc04269331d1040beed407cb94ee3c856a74f0 100644 (file)
@@ -11,6 +11,7 @@ pub use self::dependency_queue::{DependencyQueue, Fresh, Dirty, Freshness};
 pub use self::dependency_queue::Dependency;
 pub use self::graph::Graph;
 pub use self::to_url::ToUrl;
+pub use self::vcs::{GitRepo, HgRepo};
 
 pub mod graph;
 pub mod process_builder;
@@ -25,3 +26,4 @@ pub mod profile;
 mod pool;
 mod dependency_queue;
 mod to_url;
+pub mod vcs;
diff --git a/src/cargo/util/vcs.rs b/src/cargo/util/vcs.rs
new file mode 100644 (file)
index 0000000..04b2fb3
--- /dev/null
@@ -0,0 +1,21 @@
+use git2;
+
+use util::{CargoResult, process};
+
+pub struct HgRepo;
+pub struct GitRepo;
+
+impl GitRepo {
+    pub fn init(path: &Path) -> CargoResult<GitRepo> {
+        try!(git2::Repository::init(path));
+        return Ok(GitRepo)
+    }
+}
+
+impl HgRepo {
+    pub fn init(path: &Path) -> CargoResult<HgRepo> {
+        let path_str = path.as_str().unwrap();
+        try!(process("hg").arg("init").arg(path_str).exec());
+        return Ok(HgRepo)
+    }
+}