From f24d2041524d4910b169db4226441d884b3e9a6f Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Mon, 7 Jul 2025 11:48:50 +0200 Subject: [PATCH] feat: added git user and mail config --- cmd/gogitlabber/git.go | 30 ++++++++++++++++++++++++++++++ cmd/gogitlabber/input.go | 4 ++++ readme.md | 14 +++++++------- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/cmd/gogitlabber/git.go b/cmd/gogitlabber/git.go index 6546f32..6a5e0b9 100644 --- a/cmd/gogitlabber/git.go +++ b/cmd/gogitlabber/git.go @@ -66,6 +66,11 @@ func checkoutRepositories(repositories []Repository) { cloneRepository := func(repoDestination string, url string) (string, error) { cloneCmd := exec.Command("git", "clone", url, repoDestination) cloneOutput, err := cloneCmd.CombinedOutput() + + // set username and email + setGitUserName(repoName, repoDestination) + setGitUserMail(repoName, repoDestination) + logger.Print("Cloning repository: "+repoName+" to "+repoDestination, nil) return string(cloneOutput), err @@ -87,6 +92,11 @@ func checkoutRepositories(repositories []Repository) { case strings.Contains(string(repoStatus), url): logger.Print("Decided to pull repository: "+repoName, nil) pullRepository(repoName, repoDestination) + + // set username and email + setGitUserName(repoName, repoDestination) + setGitUserMail(repoName, repoDestination) + if !config.Debug { _ = bar.Add(1) } @@ -164,3 +174,23 @@ func pullRepository(repoName string, repoDestination string) { // log activity logger.Print("Pulled repository: "+repoName+" at "+repoDestination, nil) } + +// function to set the git user name +func setGitUserName(repoName string, repoDestination string) error { + + gitUserNameCmd := exec.Command("git", "-C", repoDestination, "config", "user.name", config.GitUserName) + _, err := gitUserNameCmd.CombinedOutput() + + logger.Print("Setting git username for: "+repoName, nil) + return err +} + +// function to set the git user mail +func setGitUserMail(repoName string, repoDestination string) error { + + gitUserMailCmd := exec.Command("git", "-C", repoDestination, "config", "user.mail", config.GitUserMail) + _, err := gitUserMailCmd.CombinedOutput() + + logger.Print("Setting git email for: "+repoName, nil) + return err +} diff --git a/cmd/gogitlabber/input.go b/cmd/gogitlabber/input.go index cd99b41..75804f6 100644 --- a/cmd/gogitlabber/input.go +++ b/cmd/gogitlabber/input.go @@ -19,6 +19,8 @@ type Config struct { GitBackend string `yaml:"git_backend"` GitHost string `yaml:"git_host"` GitToken string `yaml:"git_token"` + GitUserMail string `yaml:"git_user_mail"` + GitUserName string `yaml:"git_user_name"` IncludeArchived string `yaml:"include_archived"` } @@ -30,6 +32,8 @@ func (conf *Config) setDefaults() { conf.GitBackend = "" conf.GitHost = "gitlab.com" conf.GitToken = "" + conf.GitUserMail = "" + conf.GitUserName = "" conf.IncludeArchived = "excluded" } diff --git a/readme.md b/readme.md index 8218392..05b8bb3 100644 --- a/readme.md +++ b/readme.md @@ -29,23 +29,23 @@ GitLab: ```yaml # ~/.config/gogitlabber/gitlab.example.com.yaml -debug: false concurrency: 15 -git_host: "gitlab.example.net" -git_token: "glpat-" -git_backend: "gitlab" -include_archived: "excluded" +debug: false destination: "$HOME/Documents" +git_backend: "gitlab" +git_host: "gitlab.example.com" +git_token: "glpat-" +git_user_mail: "john.doe@example.com" +git_user_name: "John Doe" +include_archived: "excluded" ``` - ## Usage ```bash gogitlabber -config=~/.config/gogitlabber/gitlab.example.com.yaml ``` - ## Access Token Permissions ### Gitea