cargo/util/
vcs.rs
1use crate::util::CargoResult;
2use cargo_util::paths;
3use cargo_util::ProcessBuilder;
4use std::path::Path;
5
6pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
12 fn in_git_repo(path: &Path, cwd: &Path) -> bool {
13 if let Ok(repo) = GitRepo::discover(path, cwd) {
14 if repo.workdir().map_or(false, |workdir| workdir == path) {
16 true
17 } else {
18 !repo.is_path_ignored(path).unwrap_or(false)
19 }
20 } else {
21 false
22 }
23 }
24
25 in_git_repo(path, cwd) || HgRepo::discover(path, cwd).is_ok()
26}
27
28pub struct HgRepo;
29pub struct GitRepo;
30pub struct PijulRepo;
31pub struct FossilRepo;
32
33impl GitRepo {
34 pub fn init(path: &Path, _: &Path) -> CargoResult<GitRepo> {
35 git2::Repository::init(path)?;
36 Ok(GitRepo)
37 }
38 pub fn discover(path: &Path, _: &Path) -> Result<git2::Repository, git2::Error> {
39 git2::Repository::discover(path)
40 }
41}
42
43impl HgRepo {
44 pub fn init(path: &Path, cwd: &Path) -> CargoResult<HgRepo> {
45 ProcessBuilder::new("hg")
46 .cwd(cwd)
47 .arg("init")
48 .arg("--")
49 .arg(path)
50 .exec()?;
51 Ok(HgRepo)
52 }
53 pub fn discover(path: &Path, cwd: &Path) -> CargoResult<HgRepo> {
54 ProcessBuilder::new("hg")
55 .cwd(cwd)
56 .arg("--cwd")
57 .arg(path)
58 .arg("root")
59 .exec_with_output()?;
60 Ok(HgRepo)
61 }
62}
63
64impl PijulRepo {
65 pub fn init(path: &Path, cwd: &Path) -> CargoResult<PijulRepo> {
66 ProcessBuilder::new("pijul")
67 .cwd(cwd)
68 .arg("init")
69 .arg("--")
70 .arg(path)
71 .exec()?;
72 Ok(PijulRepo)
73 }
74}
75
76impl FossilRepo {
77 pub fn init(path: &Path, cwd: &Path) -> CargoResult<FossilRepo> {
78 paths::create_dir_all(path)?;
80
81 let db_fname = ".fossil";
83 let mut db_path = path.to_owned();
84 db_path.push(db_fname);
85
86 ProcessBuilder::new("fossil")
88 .cwd(cwd)
89 .arg("init")
90 .arg("--")
91 .arg(&db_path)
92 .exec()?;
93
94 ProcessBuilder::new("fossil")
96 .cwd(&path)
97 .arg("open")
98 .arg("--")
99 .arg(db_fname)
100 .exec()?;
101
102 Ok(FossilRepo)
103 }
104}