Skip to content

Environment variables are not loaded for commit action #401

@endorama

Description

@endorama

I'm using go-git through a project and faced this error: [...]: author field is required.

In my Git configuration I do not specify the user.email field; here is the excerpt:

   1   │ [user]
   2   │     name = Edoardo ...
   3   │     useConfigOnly = true

This configuration forces to set user.email setting for each repo.

I went on debugging this:

  • the error is triggered by

    go-git/options.go

    Lines 505 to 507 in 99457e5

    if o.Author == nil {
    return ErrMissingAuthor
    }

    in the
    func (o *CommitOptions) loadConfigAuthorAndCommitter(r *Repository) error {

    function
  • loadConfigAuthorAndCommitter is called by CommitOptions.Validate(), executed as first step in Worktree.Commit()
    func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error) {
    if err := opts.Validate(w.r); err != nil {
    return plumbing.ZeroHash, err
    }
  • loadConfigAuthorAndCommitter function first step is to load the system configurations:
    cfg, err := r.ConfigScoped(config.SystemScope)

This behaviour seems to contradicts the git documentation about Environment variables. Under Committing in Git Internals Environment variables is written:

The final creation of a Git commit object is usually done by git-commit-tree, which uses these environment variables as its primary source of information, falling back to configuration values only if these aren’t present.

(Incidentally these variables include GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL, which should override any file based configuration and I tried using to overcome the mentioned error).

Expected behaviour: go-git follows git behaviour and respects environment variables at commit time, not relying only on file based configurations

By looking at other issues I noticed aligning with what git does is important, so I would contribute a PR to change this behaviour by loading specified environment variables in loadConfigAuthorAndCommitter before system configuration and configuring author and committer accordingly to what git docs say.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions