mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
cloneTrackedRepo(), cloneRepoFull(), and sparseCloneSubdir() now accept a branch parameter. When non-empty, --branch <name> is passed to git clone. Fallback chain (partial → full) preserved. Ref: #90
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package install
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func gitSupportsSparseCheckout() bool {
|
|
out, err := exec.Command("git", "--version").Output()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return supportsSparseCheckoutVersion(strings.TrimSpace(string(out)))
|
|
}
|
|
|
|
func supportsSparseCheckoutVersion(versionOutput string) bool {
|
|
// Examples:
|
|
// git version 2.44.0
|
|
// git version 2.39.3 (Apple Git-146)
|
|
fields := strings.Fields(versionOutput)
|
|
if len(fields) < 3 {
|
|
return false
|
|
}
|
|
ver := fields[2]
|
|
parts := strings.Split(ver, ".")
|
|
if len(parts) < 2 {
|
|
return false
|
|
}
|
|
major, err := strconv.Atoi(parts[0])
|
|
if err != nil {
|
|
return false
|
|
}
|
|
minor, err := strconv.Atoi(parts[1])
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
if major > 2 {
|
|
return true
|
|
}
|
|
return major == 2 && minor >= 25
|
|
}
|
|
|
|
func sparseCloneSubdir(url, subdir, destPath, branch string, extraEnv []string, onProgress ProgressCallback) error {
|
|
subdir = strings.TrimSpace(subdir)
|
|
if subdir == "" {
|
|
return fmt.Errorf("sparse checkout requires non-empty subdir")
|
|
}
|
|
|
|
cloneArgs := []string{"clone", "--filter=blob:none", "--no-checkout", "--depth", "1"}
|
|
if branch != "" {
|
|
cloneArgs = append(cloneArgs, "--branch", branch)
|
|
}
|
|
if onProgress != nil {
|
|
cloneArgs = append(cloneArgs, "--progress")
|
|
} else {
|
|
cloneArgs = append(cloneArgs, "--quiet")
|
|
}
|
|
cloneArgs = append(cloneArgs, url, destPath)
|
|
if err := runGitCommandWithProgress(cloneArgs, "", extraEnv, onProgress); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := runGitCommandWithProgress([]string{"sparse-checkout", "set", subdir}, destPath, extraEnv, nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := runGitCommandWithProgress([]string{"checkout"}, destPath, extraEnv, nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|