go-git / go-git

A highly extensible Git implementation in pure Go.
https://pkg.go.dev/github.com/go-git/go-git/v5
Apache License 2.0
5.87k stars 734 forks source link

Environment variables are not loaded for commit action #401

Closed endorama closed 1 month ago

endorama commented 2 years ago

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:

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.

github-actions[bot] commented 5 months ago

To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

Thanks for your understanding and cooperation!

github-actions[bot] commented 2 months ago

To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

Thanks for your understanding and cooperation!

svdo commented 1 month ago

I am considering making a PR to add support for GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL environment variables. Could someone give me feedback on (a) whether this would be appreciated, and (b) whether you agree that ConfigScoped (in repository.go) would be the right place to do this?

Just to clarify: my use case is that I want to use argocd-autopilot to bootstrap a Bitbucket repo. When using token-based authentication, Bitbucket requires committing using a special git author email address, and currently I have no way of passing that (other than setting XDG_CONFIG_HOME to a special folder that is only used for argocd-autopilot, but that's an ugly workaround IMHO :) The relevant call in argocd-bootstrap is:

https://github.com/argoproj-labs/argocd-autopilot/blob/55eb5621fbd4650604a151b3d0efc2592f7dcfdc/pkg/git/repository.go#L372

That's why I want to add it in ConfigScoped.