Skip to content

Commit 2513e80

Browse files
authored
Merge pull request #18 from pfnet-research/set-dummy-user-for-pull
Set dummy user if not available
2 parents 95667ff + f7cf4cb commit 2513e80

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

cmd/root.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@ type globalFlags struct {
3535
}
3636

3737
func (gf globalFlags) WorkingEnvSpec() types.WorkingEnvSpec {
38-
return types.WorkingEnvSpec{
38+
workingEnvSpec := types.WorkingEnvSpec{
3939
SrcDir: gf.srcDir,
4040
GhostWorkingDir: gf.ghostWorkDir,
4141
GhostRepo: gf.ghostRepo,
4242
}
43+
userName, userEmail, err := git.GetUserConfig(globalOpts.srcDir)
44+
if err == nil {
45+
workingEnvSpec.GhostUserName = userName
46+
workingEnvSpec.GhostUserEmail = userEmail
47+
} else {
48+
log.Debug("failed to get user name and email of the source directory")
49+
}
50+
return workingEnvSpec
4351
}
4452

4553
var (

pkg/ghost/git/repo.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2628
var (
@@ -41,23 +43,41 @@ func InitializeGitDir(dir, repo, branch string) errors.GitGhostError {
4143

4244
// CopyUserConfig copies user config from source directory to destination directory.
4345
func 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

pkg/ghost/types/workingenv.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import (
2424
log "github.com/Sirupsen/logrus"
2525
)
2626

27+
const (
28+
defaultGhostUserName = "Git Ghost"
29+
defaultGhostUserEmail = "git-ghost@example.com"
30+
)
31+
2732
// WorkingEnvSpec abstract an environment git-ghost works with
2833
type WorkingEnvSpec struct {
2934
// SrcDir is local git directory
@@ -32,6 +37,10 @@ type WorkingEnvSpec struct {
3237
GhostWorkingDir string
3338
// GhostRepo is a repository url git-ghost works with
3439
GhostRepo string
40+
// GhostUserName is a user name which is used in ghost working directories.
41+
GhostUserName string
42+
// GhostUserEmail is a user email which is used in ghost working directories.
43+
GhostUserEmail string
3544
}
3645

3746
// WorkingEnv is initialized environment containing temporary local ghost repository
@@ -49,7 +58,15 @@ func (weSpec WorkingEnvSpec) Initialize() (*WorkingEnv, errors.GitGhostError) {
4958
if ggerr != nil {
5059
return nil, ggerr
5160
}
52-
ggerr = git.CopyUserConfig(weSpec.SrcDir, ghostDir)
61+
ghostUserName := defaultGhostUserName
62+
if weSpec.GhostUserName != "" {
63+
ghostUserName = weSpec.GhostUserName
64+
}
65+
ghostUserEmail := defaultGhostUserEmail
66+
if weSpec.GhostUserEmail != "" {
67+
ghostUserEmail = weSpec.GhostUserEmail
68+
}
69+
ggerr = git.SetUserConfig(ghostDir, ghostUserName, ghostUserEmail)
5370
if ggerr != nil {
5471
return nil, ggerr
5572
}

0 commit comments

Comments
 (0)