Skip to content

Commit

Permalink
fix builtin arg for author; add two new args (#4016)
Browse files Browse the repository at this point in the history
1. Add EARTHLY_GIT_AUTHOR_EMAIL and `EARTHLY_GIT_AUTHOR_NAME` built-in
args.
2. Address #3822
  • Loading branch information
idodod authored Apr 12, 2024
1 parent 5b2758b commit cf766de
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 43 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ All notable changes to [Earthly](https://github.com/earthly/earthly) will be doc
### Added

- New experimental wildcard-based copy, e.g. `COPY ./services/*+artifact/* .` which would invoke `COPY` for `./services/foo+artifact`, and `./services/bar+artifact` (assuming two services foo and bar, both having a `artifact` target in their respective Earthfile). Enable with the `VERSION --wildcard-copy` feature flag. [#3966](https://github.com/earthly/earthly/issues/3966).
- New built-in `ARG`s - `EARTHLY_GIT_AUTHOR_EMAIL` and `EARTHLY_GIT_AUTHOR_NAME` will contain the author email and author name respectively. Enable with the `VERSION --git-author-email-name-args` feature flag.

### Changed

- `EARTHLY_GIT_AUTHOR` built-in `ARG` will now contain both name and email, when enabled with the `VERSION --git-author-email-name-args` feature flag. Previously it only contained the email. [#3822](https://github.com/earthly/earthly/issues/3822)

### Fixed

Expand Down
31 changes: 21 additions & 10 deletions buildcontext/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ type resolvedGitProject struct {
// committerTs is the git committer timestamp.
committerTs string
// authorTs is the git author timestamp.
authorTs string
author string
coAuthors []string
authorTs string
authorEmail string
authorName string
coAuthors []string
// refs is the git refs
refs []string
// state is the state holding the git files.
Expand Down Expand Up @@ -202,7 +203,8 @@ func (gr *gitResolver) resolveEarthProject(ctx context.Context, gwClient gwclien
Tags: rgp.tags,
CommitterTimestamp: rgp.committerTs,
AuthorTimestamp: rgp.authorTs,
Author: rgp.author,
AuthorEmail: rgp.authorEmail,
AuthorName: rgp.authorName,
CoAuthors: rgp.coAuthors,
Refs: rgp.refs,
},
Expand Down Expand Up @@ -269,7 +271,8 @@ func (gr *gitResolver) resolveGitProject(ctx context.Context, gwClient gwclient.
"git describe --exact-match --tags >/dest/git-tags || touch /dest/git-tags ; " +
"git log -1 --format=%ct >/dest/git-committer-ts || touch /dest/git-committer-ts ; " +
"git log -1 --format=%at >/dest/git-author-ts || touch /dest/git-author-ts ; " +
"git log -1 --format=%ae >/dest/git-author || touch /dest/git-author ; " +
"git log -1 --format=%ae >/dest/git-author-email || touch /dest/git-author-email ; " +
"git log -1 --format=%an >/dest/git-author-name || touch /dest/git-author-name ; " +
"git log -1 --format=%b >/dest/git-body || touch /dest/git-body ; " +
"git for-each-ref --points-at HEAD --format '%(refname:lstrip=-1)' >/dest/git-refs || touch /dest/git-refs ; " +
"find -type f -name Earthfile > /dest/Earthfile-paths || touch /dest/Earthfile-paths ; " +
Expand Down Expand Up @@ -347,11 +350,17 @@ func (gr *gitResolver) resolveGitProject(ctx context.Context, gwClient gwclient.
if err != nil {
return nil, errors.Wrap(err, "read git-author-ts")
}
gitAuthorBytes, err := gitMetaRef.ReadFile(ctx, gwclient.ReadRequest{
Filename: "git-author",
gitAuthorEmailBytes, err := gitMetaRef.ReadFile(ctx, gwclient.ReadRequest{
Filename: "git-author-email",
})
if err != nil {
return nil, errors.Wrap(err, "read git-author")
return nil, errors.Wrap(err, "read git-author-email")
}
gitAuthorNameBytes, err := gitMetaRef.ReadFile(ctx, gwclient.ReadRequest{
Filename: "git-author-name",
})
if err != nil {
return nil, errors.Wrap(err, "read git-author-name")
}
gitBodyBytes, err := gitMetaRef.ReadFile(ctx, gwclient.ReadRequest{
Filename: "git-body",
Expand All @@ -375,7 +384,8 @@ func (gr *gitResolver) resolveGitProject(ctx context.Context, gwClient gwclient.
gitHash := strings.SplitN(string(gitHashBytes), "\n", 2)[0]
gitShortHash := strings.SplitN(string(gitShortHashBytes), "\n", 2)[0]
gitBranches := strings.SplitN(gitBranch, "\n", 2)
gitAuthor := strings.SplitN(string(gitAuthorBytes), "\n", 2)[0]
gitAuthorEmail := strings.SplitN(string(gitAuthorEmailBytes), "\n", 2)[0]
gitAuthorName := strings.SplitN(string(gitAuthorNameBytes), "\n", 2)[0]
gitCoAuthors := gitutil.ParseCoAuthorsFromBody(string(gitBodyBytes))
var gitBranches2 []string
for _, gitBranch := range gitBranches {
Expand Down Expand Up @@ -431,7 +441,8 @@ func (gr *gitResolver) resolveGitProject(ctx context.Context, gwClient gwclient.
tags: gitTags2,
committerTs: gitCommiterTs,
authorTs: gitAuthorTs,
author: gitAuthor,
authorEmail: gitAuthorEmail,
authorName: gitAuthorName,
coAuthors: gitCoAuthors,
refs: gitRefs2,
earthfilePaths: strings.Split(strings.TrimSpace(string(earthfilePathsRaw)), "\n"),
Expand Down
10 changes: 5 additions & 5 deletions cmd/earthly/subcmd/build_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ func (a *Build) ActionBuildImp(cliCtx *cli.Context, flagArgs, nonFlagArgs []stri
a.cli.SetAnaMetaTarget(target)

var (
gitCommitAuthor string
gitConfigEmail string
gitCommitAuthorEmail string
gitConfigEmail string
)
if !target.IsRemote() {
meta, _ := gitutil.Metadata(cliCtx.Context, target.GetLocalPath(), a.cli.Flags().GitBranchOverride)
if meta != nil {
// Git commit detection here is best effort
gitCommitAuthor = meta.Author
gitCommitAuthorEmail = meta.AuthorEmail
}
if email, err := gitutil.ConfigEmail(cliCtx.Context); err == nil {
gitConfigEmail = email
Expand Down Expand Up @@ -354,7 +354,7 @@ func (a *Build) ActionBuildImp(cliCtx *cli.Context, flagArgs, nonFlagArgs []stri
return errors.Wrapf(err, "could not init frontend")
}

cleanupTLS, err := a.cli.ConfigureSatellite(cliCtx, cloudClient, gitCommitAuthor, gitConfigEmail)
cleanupTLS, err := a.cli.ConfigureSatellite(cliCtx, cloudClient, gitCommitAuthorEmail, gitConfigEmail)
if err != nil {
return errors.Wrapf(err, "could not configure satellite")
}
Expand Down Expand Up @@ -647,7 +647,7 @@ func (a *Build) ActionBuildImp(cliCtx *cli.Context, flagArgs, nonFlagArgs []stri
}
setup := a.cli.LogbusSetup()
setup.SetOrgAndProject(orgName, projectName)
setup.SetGitAuthor(gitCommitAuthor, gitConfigEmail)
setup.SetGitAuthor(gitCommitAuthorEmail, gitConfigEmail)
_, isCI := analytics.DetectCI(a.cli.Flags().EarthlyCIRunner)
setup.SetCI(isCI)
if doLogstreamUpload {
Expand Down
28 changes: 15 additions & 13 deletions docs/earthfile/builtin-args.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ RUN echo "The current target is $EARTHLY_TARGET"

### Git-related args

| Name | Description | Example value |
| --- | --- | --- |
| `EARTHLY_GIT_AUTHOR` | The git author detected within the build context directory. If no git directory is detected, then the value is an empty string. | `John Doe <[email protected]>` |
| `EARTHLY_GIT_CO_AUTHORS` | The git co-authors detected within the build context directory, separated by space. If no git directory is detected, then the value is an empty string. | `Jane Doe <[email protected] Jack Smith <[email protected]>` |
| `EARTHLY_GIT_COMMIT_AUTHOR_TIMESTAMP` | The author timestamp, as unix seconds, of the git commit detected within the build context directory. If no git directory is detected, then the value is an empty string. | `1626881847` |
| `EARTHLY_GIT_BRANCH` | The git branch of the git commit detected within the build context directory. If no git directory is detected, then the value is an empty string. | `main` |
| `EARTHLY_GIT_COMMIT_TIMESTAMP` | The committer timestamp, as unix seconds, of the git commit detected within the build context directory. If no git directory is detected, then the value is an empty string. | `1626881847` |
| `EARTHLY_GIT_HASH` | The git hash detected within the build context directory. If no git directory is detected, then the value is an empty string. Take care when using this arg, as the frequently changing git hash may be cause for not using the cache. | `41cb5666ade67b29e42bef121144456d3977a67a` |
| `EARTHLY_GIT_ORIGIN_URL` | The git URL detected within the build context directory. If no git directory is detected, then the value is an empty string. Please note that this may be inconsistent, depending on whether an HTTPS or SSH URL was used. | `[email protected]:bar/buz.git` or `https://github.com/bar/buz.git` |
| `EARTHLY_GIT_PROJECT_NAME` | The git project name from within the git URL detected within the build context directory. If no git directory is detected, then the value is an empty string. | `bar/buz` |
| `EARTHLY_GIT_REFS` | The git references of the git commit detected within the build context directory, separated by space. If no git directory is detected, then the value is an empty string. | `issue-2735-git-ref main` |
| `EARTHLY_GIT_SHORT_HASH` | The first 8 characters of the git hash detected within the build context directory. If no git directory is detected, then the value is an empty string. Take care when using this arg, as the frequently changing git hash may be cause for not using the cache. | `41cb5666` |
| `EARTHLY_SOURCE_DATE_EPOCH` | The timestamp, as unix seconds, of the git commit detected within the build context directory. If no git directory is detected, then the value is `0` (the unix epoch) | `1626881847`, `0` |
| Name | Description | Example value | Feature Flag |
| --- | --- | --- |----------------------------------------|
| `EARTHLY_GIT_AUTHOR` | The git author detected within the build context directory. If no git directory is detected, then the value is an empty string. This is currently the author's email address but the feature flag adds the name as well | `[email protected]` (or `John Doe <[email protected]>` with flag turned on) | `--earthly-git-author-individual-args` |
| `EARTHLY_GIT_AUTHOR_EMAIL` | The git author email detected within the build context directory. If no git directory is detected, then the value is an empty string. | `[email protected]` | `--earthly-git-author-individual-args` |
| `EARTHLY_GIT_AUTHOR_NAME` | The git author name detected within the build context directory. If no git directory is detected, then the value is an empty string. | `John Doe` | `--earthly-git-author-individual-args` |
| `EARTHLY_GIT_CO_AUTHORS` | The git co-authors detected within the build context directory, separated by space. If no git directory is detected, then the value is an empty string. | `Jane Doe <[email protected] Jack Smith <[email protected]>` | |
| `EARTHLY_GIT_COMMIT_AUTHOR_TIMESTAMP` | The author timestamp, as unix seconds, of the git commit detected within the build context directory. If no git directory is detected, then the value is an empty string. | `1626881847` | |
| `EARTHLY_GIT_BRANCH` | The git branch of the git commit detected within the build context directory. If no git directory is detected, then the value is an empty string. | `main` | |
| `EARTHLY_GIT_COMMIT_TIMESTAMP` | The committer timestamp, as unix seconds, of the git commit detected within the build context directory. If no git directory is detected, then the value is an empty string. | `1626881847` | |
| `EARTHLY_GIT_HASH` | The git hash detected within the build context directory. If no git directory is detected, then the value is an empty string. Take care when using this arg, as the frequently changing git hash may be cause for not using the cache. | `41cb5666ade67b29e42bef121144456d3977a67a` | |
| `EARTHLY_GIT_ORIGIN_URL` | The git URL detected within the build context directory. If no git directory is detected, then the value is an empty string. Please note that this may be inconsistent, depending on whether an HTTPS or SSH URL was used. | `[email protected]:bar/buz.git` or `https://github.com/bar/buz.git` | |
| `EARTHLY_GIT_PROJECT_NAME` | The git project name from within the git URL detected within the build context directory. If no git directory is detected, then the value is an empty string. | `bar/buz` | |
| `EARTHLY_GIT_REFS` | The git references of the git commit detected within the build context directory, separated by space. If no git directory is detected, then the value is an empty string. | `issue-2735-git-ref main` | |
| `EARTHLY_GIT_SHORT_HASH` | The first 8 characters of the git hash detected within the build context directory. If no git directory is detected, then the value is an empty string. Take care when using this arg, as the frequently changing git hash may be cause for not using the cache. | `41cb5666` | |
| `EARTHLY_SOURCE_DATE_EPOCH` | The timestamp, as unix seconds, of the git commit detected within the build context directory. If no git directory is detected, then the value is `0` (the unix epoch) | `1626881847`, `0` | |

### Platform-related args

Expand Down
1 change: 1 addition & 0 deletions features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Features struct {
AllowPrivilegedFromDockerfile bool `long:"allow-privileged-from-dockerfile" description:"Allow the use of the --allow-privileged flag in the FROM DOCKERFILE command"`
RunWithAWS bool `long:"run-with-aws" description:"make AWS credentials in the environment or ~/.aws available to RUN commands"`
WildcardCopy bool `long:"wildcard-copy" description:"allow for the expansion of wildcard (glob) paths for COPY commands"`
GitAuthorEmailNameArgs bool `long:"git-author-email-name-args" description:"includes EARTHLY_GIT_AUTHOR_EMAIL and EARTHLY_GIT_AUTHOR_NAME builtin ARGs"`

Major int
Minor int
Expand Down
56 changes: 50 additions & 6 deletions tests/git-metadata/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ test -n \"\$(git config --global user.email)\"
# clone the repo and add a test Earthfile to it
git clone [email protected]:testuser/repo.git
mv Earthfile repo/
git -C repo add Earthfile
mkdir -p repo/flag_on repo/flag_off
cp Earthfile repo/flag_on
mv Earthfile repo/flag_off
sed -i '1s/VERSION --git-author-email-name-args \(.*\)/VERSION \1/' ./repo/flag_off/Earthfile
git -C repo add flag_on flag_off
git -C repo commit -m \"test
Co-authored-by: Testy McTest <[email protected]>
Expand All @@ -58,40 +62,80 @@ export expected_refs=\"\$(git -C repo for-each-ref --points-at HEAD --format '%(
test -n \"\$expected_refs\"
# finally perform earthly tests
# flag --git-author-email-name-args on
earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch \
--build-arg expected_refs \
./repo/flag_on+test-git-metadata
EARTHLY_GIT_BRANCH_OVERRIDE=branch-override earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch=branch-override \
--build-arg expected_refs \
./repo/flag_on+test-git-metadata
# flag --git-author-email-name-args off
earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch \
--build-arg expected_refs \
./repo+test-git-metadata
--build-arg flag_on=false \
./repo/flag_off+test-git-metadata
EARTHLY_GIT_BRANCH_OVERRIDE=branch-override earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch=branch-override \
--build-arg expected_refs \
./repo+test-git-metadata
--build-arg flag_on=false \
./repo/flag_off+test-git-metadata
rm -rf repo
export expected_refs=\"main\"
# flag on
earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch \
--build-arg expected_refs \
git.example.com/testuser/repo/flag_on:main+test-git-metadata
EARTHLY_GIT_BRANCH_OVERRIDE=branch-override earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch=branch-override \
--build-arg expected_refs \
git.example.com/testuser/repo/flag_on:main+test-git-metadata
# flag off
earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch \
--build-arg expected_refs \
git.example.com/testuser/repo:main+test-git-metadata
--build-arg flag_on=false \
git.example.com/testuser/repo/flag_off:main+test-git-metadata
EARTHLY_GIT_BRANCH_OVERRIDE=branch-override earthly --config \$earthly_config --verbose -D \
--build-arg expected_sha \
--build-arg expected_committer_timestamp \
--build-arg expected_author_timestamp \
--build-arg expected_branch=branch-override \
--build-arg expected_refs \
git.example.com/testuser/repo:main+test-git-metadata
--build-arg flag_on=false \
git.example.com/testuser/repo/flag_off:main+test-git-metadata
" >/tmp/test-earthly-script && chmod +x /tmp/test-earthly-script
DO --pass-args +RUN_EARTHLY_ARGS --pre_command=start-sshd --exec_cmd=/tmp/test-earthly-script

Expand Down
18 changes: 16 additions & 2 deletions tests/git-metadata/test.earth
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION 0.8
VERSION --git-author-email-name-args 0.8

# Note: this is tests both locally and remotely
# the population of git metadata occurs in two **different** functions respectively:
Expand All @@ -12,9 +12,21 @@ test-git-metadata:
ARG --required expected_author_timestamp
ARG --required expected_branch
ARG --required expected_refs
ARG flag_on=true
LET expected_author="[email protected]"
LET expected_author_email=""
LET expected_author_name=""
IF [ "$flag_on" = "true" ]
SET expected_author="test name <[email protected]>"
SET expected_author_email="[email protected]"
SET expected_author_name="test name"
END

ARG EARTHLY_GIT_SHORT_HASH
ARG EARTHLY_GIT_HASH
ARG EARTHLY_GIT_AUTHOR
ARG EARTHLY_GIT_AUTHOR_EMAIL
ARG EARTHLY_GIT_AUTHOR_NAME
ARG EARTHLY_GIT_CO_AUTHORS
ARG EARTHLY_GIT_COMMIT_TIMESTAMP
ARG EARTHLY_GIT_COMMIT_AUTHOR_TIMESTAMP
Expand All @@ -27,5 +39,7 @@ test-git-metadata:
RUN test "$EARTHLY_GIT_REFS" = "$expected_refs"
RUN test -n "$EARTHLY_GIT_SHORT_HASH"
RUN echo "$EARTHLY_GIT_HASH" | grep "$EARTHLY_GIT_SHORT_HASH"
RUN test "$EARTHLY_GIT_AUTHOR" = "[email protected]"
RUN test "$EARTHLY_GIT_AUTHOR" = "$expected_author"
RUN test "$EARTHLY_GIT_AUTHOR_EMAIL" = "$expected_author_email"
RUN test "$EARTHLY_GIT_AUTHOR_NAME" = "$expected_author_name"
RUN test "$EARTHLY_GIT_CO_AUTHORS" = "[email protected] [email protected]"
Loading

0 comments on commit cf766de

Please sign in to comment.