-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
484 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package build | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/docker/buildx/util/gitutil" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const DockerfileLabel = "com.docker.image.source.entrypoint" | ||
|
||
type provenanceDetails struct { | ||
Source string | ||
Revision string | ||
DockerfilePath string | ||
} | ||
|
||
func getProvenanceDetails(ctx context.Context, contextPath string, dockerfilePath string, full bool) (*provenanceDetails, error) { | ||
if contextPath == "" { | ||
return nil, nil | ||
} | ||
|
||
// figure out in which directory the git command needs to run in | ||
var wd string | ||
if filepath.IsAbs(contextPath) { | ||
wd = contextPath | ||
} else { | ||
cwd, _ := os.Getwd() | ||
wd, _ = filepath.Abs(filepath.Join(cwd, contextPath)) | ||
} | ||
|
||
gitc, _ := gitutil.New(gitutil.WithContext(ctx), gitutil.WithWorkingDir(wd)) | ||
if !gitc.IsInsideWorkTree() { | ||
logrus.Warnf("Unable to determine Git information") | ||
return nil, nil | ||
} | ||
|
||
sha, err := gitc.FullCommit() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error obtaining git head") | ||
} | ||
if gitc.IsDirty() { | ||
sha += "-dirty" | ||
} | ||
|
||
prvdetails := &provenanceDetails{ | ||
Revision: sha, | ||
} | ||
|
||
// add a remote url if full Git details are requested; if there aren't any remotes don't fail | ||
if full { | ||
if rurl, err := gitc.RemoteURL(); err == nil && rurl != "" { | ||
prvdetails.Source = rurl | ||
} | ||
} | ||
|
||
// add Dockerfile path; there is no org.opencontainers annotation for this | ||
if dockerfilePath == "" { | ||
dockerfilePath = filepath.Join(wd, "Dockerfile") | ||
} | ||
|
||
// obtain Git root directory | ||
root, err := gitc.RootDir() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get git root") | ||
} | ||
|
||
// record only Dockerfile paths that are within the Git root | ||
if !filepath.IsAbs(dockerfilePath) { | ||
cwd, _ := os.Getwd() | ||
dockerfilePath = filepath.Join(cwd, dockerfilePath) | ||
} | ||
dockerfilePath, _ = filepath.Rel(root, dockerfilePath) | ||
if !strings.HasPrefix(dockerfilePath, "..") { | ||
prvdetails.DockerfilePath = dockerfilePath | ||
} | ||
|
||
return prvdetails, nil | ||
} |
Oops, something went wrong.