Browse Source

check-shallow-before-unshallow-repos (#13)

* check-shallow-before-unshallow

* 更改版本号
LoGin 1 year ago
parent
commit
2ebfffe739
3 changed files with 30 additions and 2 deletions
  1. 1 1
      Cargo.toml
  2. 1 1
      rust-toolchain.toml
  3. 28 0
      src/executor/source.rs

+ 1 - 1
Cargo.toml

@@ -1,7 +1,7 @@
 [package]
 name = "dadk"
 authors = ["longjin <longjin@DragonOS.org>", "chikejian <chikejian@DragonOS.org>"]
-version = "0.1.2"
+version = "0.1.3"
 edition = "2021"
 description = "DragonOS Application Development Kit\nDragonOS应用开发工具"
 license = "GPL-2.0-only"

+ 1 - 1
rust-toolchain.toml

@@ -1,3 +1,3 @@
 [toolchain]
-channel = "nightly-2023-01-21"
+channel = "nightly-2023-08-15"
 components = ["rust-src"]

+ 28 - 0
src/executor/source.rs

@@ -222,6 +222,10 @@ impl GitSource {
     }
     /// # 把浅克隆的仓库变成深克隆
     fn unshallow(&self, target_dir: &CacheDir) -> Result<(), String> {
+        if self.is_shallow(target_dir)? == false {
+            return Ok(());
+        }
+
         let mut cmd = Command::new("git");
         cmd.current_dir(&target_dir.path);
         cmd.arg("fetch").arg("--unshallow");
@@ -245,6 +249,30 @@ impl GitSource {
         return Ok(());
     }
 
+    /// 判断当前仓库是否是浅克隆
+    fn is_shallow(&self, target_dir: &CacheDir) -> Result<bool, String> {
+        let mut cmd = Command::new("git");
+        cmd.current_dir(&target_dir.path);
+        cmd.arg("rev-parse").arg("--is-shallow-repository");
+
+        let proc: std::process::Child = cmd
+            .stderr(Stdio::piped())
+            .spawn()
+            .map_err(|e| e.to_string())?;
+        let output = proc.wait_with_output().map_err(|e| e.to_string())?;
+
+        if !output.status.success() {
+            return Err(format!(
+                "Failed to check if shallow {}, message: {}",
+                target_dir.path.display(),
+                StdioUtils::tail_n_str(StdioUtils::stderr_to_lines(&output.stderr), 5)
+            ));
+        }
+
+        let is_shallow = String::from_utf8_lossy(&output.stdout).trim() == "true";
+        return Ok(is_shallow);
+    }
+
     fn fetch_all(&self, target_dir: &CacheDir) -> Result<(), String> {
         self.set_fetch_config(target_dir)?;
         let mut cmd = Command::new("git");