-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_repos.go
More file actions
99 lines (87 loc) · 2.71 KB
/
Copy pathinit_repos.go
File metadata and controls
99 lines (87 loc) · 2.71 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"os"
"os/exec"
)
var (
courseRepos = []string{"assignments", "website", "tests"}
assignmentsRepo = courseRepos[0]
)
// repoName returns the actual repository name for the given internal name.
// This allows mapping internal names (like "website") to actual repository
// names (like "dat520.github.io").
func repoName(internalName string) string {
if internalName == "website" {
return websiteRepo()
}
return internalName
}
// orgName returns the GitHub organization name for the given internal name.
// For the website repository, it returns just the course name (without year)
// since the website is shared across multiple years.
// For other repositories, it returns the course organization with the year.
func orgName(internalName string) string {
if internalName == "website" {
return course()
}
return courseOrg()
}
// initRepos initializes the course repositories so that they are
// ready to be populated with data from the main course repository.
// Once the repositories are initialized, they are ready to be pushed
// to the per-year course organization on GitHub.
func initRepos(_ []string) {
if err := loadEnv(); err != nil {
exitErr(err, "Error loading environment variables")
}
for _, repo := range courseRepos {
path := repoPath(repo)
if exists(path) {
fmt.Printf("Repository %q already exists, skipping.\n", repo)
continue
}
if err := os.MkdirAll(path, os.ModePerm); err != nil {
exitErr(err, "Error creating repository directory")
}
if err := initGitRepository(path, orgName(repo), repoName(repo)); err != nil {
exitErr(err, "Error initializing git repository")
}
}
}
func initGitRepository(workingDir, ghOrg, repo string) error {
remoteURL := gitURL(ghOrg, repo)
// Initialize repository and add remote
initCommands := [][]string{
{"git", "init"},
{"git", "branch", "-M", "main"},
{"git", "remote", "add", "origin", remoteURL},
}
for _, cmd := range initCommands {
if err := runCommand(workingDir, cmd...); err != nil {
return err
}
}
// Try to pull if remote has content
if remoteHasContent(remoteURL, "main") {
pullCommands := [][]string{
{"git", "pull", "origin", "main"},
{"git", "branch", "--set-upstream-to=origin/main", "main"},
}
for _, cmd := range pullCommands {
if err := runCommand(workingDir, cmd...); err != nil {
return err
}
}
}
return nil
}
func remoteHasContent(remoteURL, branch string) bool {
cmd := exec.Command("git", "ls-remote", "--heads", remoteURL, branch)
output, err := cmd.Output()
return err == nil && len(output) > 0
}
func gitURL(ghOrg, repo string) string {
// TODO(meling) consider supporting HTTPS remote origin
return fmt.Sprintf("git@github.com:%s/%s.git", ghOrg, repo)
}