Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func checkGitAvailability() error {
func backUp(backupDir string, repo *Repository, bare bool, wg *sync.WaitGroup) ([]byte, error) {
defer wg.Done()

if repo.Private && ignorePrivate != nil && *ignorePrivate {
log.Printf("Skipping %s as it is a private repo.\n", repo.Name)
return nil, nil
}

repoDir := getRepoDir(backupDir, repo, bare)

_, err := appFS.Stat(repoDir)
Expand Down Expand Up @@ -74,11 +79,6 @@ func cloneNewRepo(repoDir string, repo *Repository, bare bool) ([]byte, error) {
log.Printf("Cloning %s\n", repo.Name)
log.Printf("%#v\n", repo)

if repo.Private && ignorePrivate != nil && *ignorePrivate {
log.Printf("Skipping %s as it is a private repo.\n", repo.Name)
return nil, nil
}

cloneURL := repo.CloneURL
if useHTTPSClone != nil && *useHTTPSClone {
// Add username and token to the clone URL
Expand Down
41 changes: 41 additions & 0 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func fakePullCommand(command string, args ...string) (cmd *exec.Cmd) {
return cmd
}

func fakeFailingPullCommand(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperFailingPullProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
return cmd
}

func fakeCloneCommand(command string, args ...string) (cmd *exec.Cmd) {
cs := []string{"-test.run=TestHelperCloneProcess", "--", command}
cs = append(cs, args...)
Expand Down Expand Up @@ -69,6 +77,39 @@ func TestBackup(t *testing.T) {
}
}

func TestBackupSkipsExistingPrivateRepoWhenIgnorePrivate(t *testing.T) {
oldFS, oldIgnorePrivate, oldExecCommand := appFS, ignorePrivate, execCommand
t.Cleanup(func() {
appFS, ignorePrivate, execCommand = oldFS, oldIgnorePrivate, oldExecCommand
})

appFS = afero.NewMemMapFs()
ignorePrivateValue := true
ignorePrivate = &ignorePrivateValue
execCommand = fakeFailingPullCommand

backupDir := "/synthetic-backups"
repo := Repository{Name: "private-repo", Namespace: "synthetic-owner", Private: true}
if err := appFS.MkdirAll(getRepoDir(backupDir, &repo, false), 0771); err != nil {
t.Fatal(err)
}

var wg sync.WaitGroup
wg.Add(1)
if output, err := backUp(backupDir, &repo, false, &wg); err != nil {
t.Fatalf("private repository should be skipped without updating it; output=%q err=%v", output, err)
}
wg.Wait()
}

func TestHelperFailingPullProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
fmt.Fprint(os.Stdout, "synthetic pull failure")
os.Exit(1)
}

func TestBareBackup(t *testing.T) {
var wg sync.WaitGroup
repo := Repository{Name: "testrepo", CloneURL: "git://foo.com/foo"}
Expand Down
Loading