Skip to content

Commit

Permalink
Add optional username and email fields to repo for commits
Browse files Browse the repository at this point in the history
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi committed Jan 16, 2025
1 parent 58fda21 commit 38b5c92
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type Worktree interface {
}

type repo struct {
// username and email are used to commit changes.
// these fields are optional, and set in the `setUser` method.
username string
email string

dir string
gitPath string
remote string
Expand Down Expand Up @@ -176,6 +181,12 @@ func (r *repo) CopyToModify(dest string) (Repo, error) {
gitEnvs: r.gitEnvs,
}

if r.username != "" || r.email != "" {
if err := cloned.setUser(context.Background(), r.username, r.email); err != nil {
return nil, fmt.Errorf("failed to set user: %v", err)
}
}

// because we did a local cloning so set the remote url of origin
if err := cloned.setRemote(context.Background(), r.remote); err != nil {
return nil, err
Expand Down Expand Up @@ -402,6 +413,9 @@ func (r repo) addCommit(ctx context.Context, message string, trailers map[string

// setUser configures username and email for local user of this repo.
func (r *repo) setUser(ctx context.Context, username, email string) error {
r.username = username
r.email = email

if out, err := r.runGitCommand(ctx, "config", "user.name", username); err != nil {
return formatCommandError(err, out)
}
Expand Down

0 comments on commit 38b5c92

Please sign in to comment.