Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional username and email fields to repo for commits #5477

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading