-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-utils.go
More file actions
77 lines (62 loc) · 1.63 KB
/
git-utils.go
File metadata and controls
77 lines (62 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package utils
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func GitSparseClone(repoURL string, localDir string, subdirectories ...string) error {
// Create the local directory if it doesn't exist
if err := os.MkdirAll(localDir, os.ModePerm); err != nil {
return err
}
// Change to the local directory
if err := os.Chdir(localDir); err != nil {
return err
}
// Initialize a Git repository
cmd := exec.Command("git", "init")
if err := cmd.Run(); err != nil {
return err
}
// Add a remote and fetch
cmd = exec.Command("git", "remote", "add", "-f", "origin", repoURL)
if err := cmd.Run(); err != nil {
return err
}
// Configure sparse checkout
cmd = exec.Command("git", "config", "core.sparseCheckout", "true")
if err := cmd.Run(); err != nil {
return err
}
// Write subdirectories to .git/info/sparse-checkout
sparseFile := filepath.Join(".git", "info", "sparse-checkout")
file, err := os.Create(sparseFile)
if err != nil {
return err
}
defer file.Close()
for _, subdir := range subdirectories {
fmt.Fprintln(file, subdir)
}
// Pull from the remote repository
cmd = exec.Command("git", "pull", "origin", "master")
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func ExtractBranchNames(input string) []string {
fmt.Printf(input)
var branchNames []string
lines := strings.Split(input, "[new branch]")
for _, line := range lines {
parts := strings.Fields(line)
if len(parts) >= 4 && parts[3] == "->" {
branchName := strings.TrimSpace(parts[2])
branchNames = append(branchNames, branchName)
}
}
return branchNames
}