Skip to content

Commit

Permalink
Merge pull request #18 from pfnet-research/set-dummy-user-for-pull
Browse files Browse the repository at this point in the history
Set dummy user if not available
  • Loading branch information
dtaniwaki authored Apr 12, 2019
2 parents 95667ff + f7cf4cb commit 2513e80
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ type globalFlags struct {
}

func (gf globalFlags) WorkingEnvSpec() types.WorkingEnvSpec {
return types.WorkingEnvSpec{
workingEnvSpec := types.WorkingEnvSpec{
SrcDir: gf.srcDir,
GhostWorkingDir: gf.ghostWorkDir,
GhostRepo: gf.ghostRepo,
}
userName, userEmail, err := git.GetUserConfig(globalOpts.srcDir)
if err == nil {
workingEnvSpec.GhostUserName = userName
workingEnvSpec.GhostUserEmail = userEmail
} else {
log.Debug("failed to get user name and email of the source directory")
}
return workingEnvSpec
}

var (
Expand Down
40 changes: 30 additions & 10 deletions pkg/ghost/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

"github.com/pfnet-research/git-ghost/pkg/util"
"github.com/pfnet-research/git-ghost/pkg/util/errors"

gherrors "github.com/pkg/errors"
)

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

// CopyUserConfig copies user config from source directory to destination directory.
func CopyUserConfig(srcDir, dstDir string) errors.GitGhostError {
name, email, err := GetUserConfig(srcDir)
if err != nil {
return err
}
return SetUserConfig(dstDir, name, email)
}

// GetUserConfig returns a user config (name and email) from destination directory.
func GetUserConfig(dir string) (string, string, errors.GitGhostError) {
// Get user config from src
userNameBytes, err := util.JustOutputCmd(exec.Command("git", "-C", srcDir, "config", "user.name"))
nameBytes, err := util.JustOutputCmd(exec.Command("git", "-C", dir, "config", "user.name"))
if err != nil {
return errors.WithStack(err)
return "", "", errors.WithStack(gherrors.WithMessage(err, "failed to get git user name"))
}
userName := strings.TrimSuffix(string(userNameBytes), "\n")
userEmailBytes, err := util.JustOutputCmd(exec.Command("git", "-C", srcDir, "config", "user.email"))
name := strings.TrimSuffix(string(nameBytes), "\n")
emailBytes, err := util.JustOutputCmd(exec.Command("git", "-C", dir, "config", "user.email"))
if err != nil {
return errors.WithStack(err)
return "", "", errors.WithStack(gherrors.WithMessage(err, "failed to get git user email"))
}
userEmail := strings.TrimSuffix(string(userEmailBytes), "\n")
// Copy the user config to dst
err = util.JustRunCmd(exec.Command("git", "-C", dstDir, "config", "user.name", fmt.Sprintf("\"%s\"", userName)))
email := strings.TrimSuffix(string(emailBytes), "\n")
return name, email, nil
}

// SetUserConfig sets a user config (name and email) to destination directory.
func SetUserConfig(dir, name, email string) errors.GitGhostError {
// Set the user config to dst
err := util.JustRunCmd(exec.Command("git", "-C", dir, "config", "user.name", fmt.Sprintf("\"%s\"", name)))
if err != nil {
return errors.WithStack(err)
return errors.WithStack(gherrors.WithMessage(err, "failed to set git user name"))
}
return errors.WithStack(util.JustRunCmd(exec.Command("git", "-C", dstDir, "config", "user.email", fmt.Sprintf("\"%s\"", userEmail))))
err = util.JustRunCmd(exec.Command("git", "-C", dir, "config", "user.email", fmt.Sprintf("\"%s\"", email)))
if err != nil {
return errors.WithStack(gherrors.WithMessage(err, "failed to set git user email"))
}
return nil
}

// CommitAndPush commits and push to its origin
Expand Down
19 changes: 18 additions & 1 deletion pkg/ghost/types/workingenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
log "github.com/Sirupsen/logrus"
)

const (
defaultGhostUserName = "Git Ghost"
defaultGhostUserEmail = "[email protected]"
)

// WorkingEnvSpec abstract an environment git-ghost works with
type WorkingEnvSpec struct {
// SrcDir is local git directory
Expand All @@ -32,6 +37,10 @@ type WorkingEnvSpec struct {
GhostWorkingDir string
// GhostRepo is a repository url git-ghost works with
GhostRepo string
// GhostUserName is a user name which is used in ghost working directories.
GhostUserName string
// GhostUserEmail is a user email which is used in ghost working directories.
GhostUserEmail string
}

// WorkingEnv is initialized environment containing temporary local ghost repository
Expand All @@ -49,7 +58,15 @@ func (weSpec WorkingEnvSpec) Initialize() (*WorkingEnv, errors.GitGhostError) {
if ggerr != nil {
return nil, ggerr
}
ggerr = git.CopyUserConfig(weSpec.SrcDir, ghostDir)
ghostUserName := defaultGhostUserName
if weSpec.GhostUserName != "" {
ghostUserName = weSpec.GhostUserName
}
ghostUserEmail := defaultGhostUserEmail
if weSpec.GhostUserEmail != "" {
ghostUserEmail = weSpec.GhostUserEmail
}
ggerr = git.SetUserConfig(ghostDir, ghostUserName, ghostUserEmail)
if ggerr != nil {
return nil, ggerr
}
Expand Down

0 comments on commit 2513e80

Please sign in to comment.