@@ -21,6 +21,8 @@ import (
2121
2222 "github.com/pfnet-research/git-ghost/pkg/util"
2323 "github.com/pfnet-research/git-ghost/pkg/util/errors"
24+
25+ gherrors "github.com/pkg/errors"
2426)
2527
2628var (
@@ -41,23 +43,41 @@ func InitializeGitDir(dir, repo, branch string) errors.GitGhostError {
4143
4244// CopyUserConfig copies user config from source directory to destination directory.
4345func CopyUserConfig (srcDir , dstDir string ) errors.GitGhostError {
46+ name , email , err := GetUserConfig (srcDir )
47+ if err != nil {
48+ return err
49+ }
50+ return SetUserConfig (dstDir , name , email )
51+ }
52+
53+ // GetUserConfig returns a user config (name and email) from destination directory.
54+ func GetUserConfig (dir string ) (string , string , errors.GitGhostError ) {
4455 // Get user config from src
45- userNameBytes , err := util .JustOutputCmd (exec .Command ("git" , "-C" , srcDir , "config" , "user.name" ))
56+ nameBytes , err := util .JustOutputCmd (exec .Command ("git" , "-C" , dir , "config" , "user.name" ))
4657 if err != nil {
47- return errors .WithStack (err )
58+ return "" , "" , errors .WithStack (gherrors . WithMessage ( err , "failed to get git user name" ) )
4859 }
49- userName := strings .TrimSuffix (string (userNameBytes ), "\n " )
50- userEmailBytes , err := util .JustOutputCmd (exec .Command ("git" , "-C" , srcDir , "config" , "user.email" ))
60+ name := strings .TrimSuffix (string (nameBytes ), "\n " )
61+ emailBytes , err := util .JustOutputCmd (exec .Command ("git" , "-C" , dir , "config" , "user.email" ))
5162 if err != nil {
52- return errors .WithStack (err )
63+ return "" , "" , errors .WithStack (gherrors . WithMessage ( err , "failed to get git user email" ) )
5364 }
54- userEmail := strings .TrimSuffix (string (userEmailBytes ), "\n " )
55- // Copy the user config to dst
56- err = util .JustRunCmd (exec .Command ("git" , "-C" , dstDir , "config" , "user.name" , fmt .Sprintf ("\" %s\" " , userName )))
65+ email := strings .TrimSuffix (string (emailBytes ), "\n " )
66+ return name , email , nil
67+ }
68+
69+ // SetUserConfig sets a user config (name and email) to destination directory.
70+ func SetUserConfig (dir , name , email string ) errors.GitGhostError {
71+ // Set the user config to dst
72+ err := util .JustRunCmd (exec .Command ("git" , "-C" , dir , "config" , "user.name" , fmt .Sprintf ("\" %s\" " , name )))
5773 if err != nil {
58- return errors .WithStack (err )
74+ return errors .WithStack (gherrors . WithMessage ( err , "failed to set git user name" ) )
5975 }
60- return errors .WithStack (util .JustRunCmd (exec .Command ("git" , "-C" , dstDir , "config" , "user.email" , fmt .Sprintf ("\" %s\" " , userEmail ))))
76+ err = util .JustRunCmd (exec .Command ("git" , "-C" , dir , "config" , "user.email" , fmt .Sprintf ("\" %s\" " , email )))
77+ if err != nil {
78+ return errors .WithStack (gherrors .WithMessage (err , "failed to set git user email" ))
79+ }
80+ return nil
6181}
6282
6383// CommitAndPush commits and push to its origin
0 commit comments