From 52f6f00f919a8b3341f396c5f6af5057cf675527 Mon Sep 17 00:00:00 2001 From: Toon Schoenmakers Date: Wed, 20 Apr 2022 15:38:29 +0200 Subject: [PATCH] Added a backup.bare-clone option When creating purely backups bare repositories are fine and checking out a branch isn't needed. This would save space and would possibly speed up backing up. --- README.md | 2 ++ cmd/git-backup/main.go | 3 ++- repository.go | 11 ++++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0633cf9..0870214 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ Options: The path to your config file. (default "git-backup.yml") -backup.fail-at-end Fail at the end of backing up repositories, rather than right away. + -backup.bare-clone + Make bare clones without checking out the main branch. ``` ## Usage: Docker diff --git a/cmd/git-backup/main.go b/cmd/git-backup/main.go index 5298cf7..037147e 100644 --- a/cmd/git-backup/main.go +++ b/cmd/git-backup/main.go @@ -12,6 +12,7 @@ import ( var configFilePath = flag.String("config.file", "git-backup.yml", "The path to your config file.") var targetPath = flag.String("backup.path", "backup", "The target path to the backup folder.") var failAtEnd = flag.Bool("backup.fail-at-end", false, "Fail at the end of backing up repositories, rather than right away.") +var bareClone = flag.Bool("backup.bare-clone", false, "Make bare clones without checking out the main branch.") func main() { flag.Parse() @@ -45,7 +46,7 @@ func main() { log.Printf("Failed to create directory: %s", err) os.Exit(100) } - err = repo.CloneInto(targetPath) + err = repo.CloneInto(targetPath, *bareClone) if err != nil { errors++ log.Printf("Failed to clone: %s", err) diff --git a/repository.go b/repository.go index dd3ceec..8932c66 100644 --- a/repository.go +++ b/repository.go @@ -1,12 +1,13 @@ package git_backup import ( - "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing/transport" - "github.com/go-git/go-git/v5/plumbing/transport/http" "log" "net/url" "os" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/transport" + "github.com/go-git/go-git/v5/plumbing/transport/http" ) type RepositorySource interface { @@ -20,7 +21,7 @@ type Repository struct { FullName string } -func (r *Repository) CloneInto(path string) error { +func (r *Repository) CloneInto(path string, bare bool) error { var auth http.AuthMethod if r.GitURL.User != nil { password, _ := r.GitURL.User.Password() @@ -29,7 +30,7 @@ func (r *Repository) CloneInto(path string) error { Password: password, } } - gitRepo, err := git.PlainClone(path, false, &git.CloneOptions{ + gitRepo, err := git.PlainClone(path, bare, &git.CloneOptions{ URL: r.GitURL.String(), Auth: auth, Progress: os.Stdout,