Skip to content

Commit

Permalink
chore: Improve the git status speed. (#359)
Browse files Browse the repository at this point in the history
* chore: Improve the git status speed.

By using the native git client its possible to improve the speed
significantly.

Signed-off-by: Matthias Glastra <matglas.git@gmail.com>

* chore: Add additional git attestor output.

When the git binary is used to for status we also include
the path to the binary and the hash of the binary files.

Also by default the git tool used to generate the attestation
data is added as contextual information.

Signed-off-by: Matthias Glastra <matglas.git@gmail.com>

* chore: Update schema of git attestor.

Signed-off-by: Matthias Glastra <matglas.git@gmail.com>

* Update attestation/git/git_bin.go

Co-authored-by: Kairo Araujo <kairo@kairo.eti.br>
Signed-off-by: Matthias Glastra <matglas.git@gmail.com>

* chore: Make the gitbinhash ouput a DigestSet.

Signed-off-by: Matthias Glastra <matglas.git@gmail.com>

---------

Signed-off-by: Matthias Glastra <matglas.git@gmail.com>
Co-authored-by: Kairo Araujo <kairo@kairo.eti.br>
  • Loading branch information
matglas and kairoaraujo authored Sep 26, 2024
1 parent b93a379 commit 80f9b23
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 4 deletions.
43 changes: 39 additions & 4 deletions attestation/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type Tag struct {
}

type Attestor struct {
GitTool string `json:"gittool"`
GitBinPath string `json:"gitbinpath,omitempty"`
GitBinHash cryptoutil.DigestSet `json:"gitbinhash,omitempty"`
CommitHash string `json:"commithash"`
Author string `json:"author"`
AuthorEmail string `json:"authoremail"`
Expand Down Expand Up @@ -221,14 +224,46 @@ func (a *Attestor) Attest(ctx *attestation.AttestationContext) error {

a.TreeHash = commit.TreeHash.String()

if GitExists() {
a.GitTool = "go-git+git-bin"

a.GitBinPath, err = GitGetBinPath()
if err != nil {
return err
}

a.GitBinHash, err = GitGetBinHash(ctx)
if err != nil {
return err
}

a.Status, err = GitGetStatus(ctx.WorkingDir())
if err != nil {
return err
}
} else {
a.GitTool = "go-git"

a.Status, err = GoGitGetStatus(repo)
if err != nil {
return err
}
}

return nil
}

func GoGitGetStatus(repo *git.Repository) (map[string]Status, error) {
var gitStatuses map[string]Status = make(map[string]Status)

worktree, err := repo.Worktree()
if err != nil {
return err
return map[string]Status{}, err
}

status, err := worktree.Status()
if err != nil {
return err
return map[string]Status{}, err
}

for file, status := range status {
Expand All @@ -241,10 +276,10 @@ func (a *Attestor) Attest(ctx *attestation.AttestationContext) error {
Staging: statusCodeString(status.Staging),
}

a.Status[file] = attestStatus
gitStatuses[file] = attestStatus
}

return nil
return gitStatuses, nil
}

func (a *Attestor) Data() *Attestor {
Expand Down
104 changes: 104 additions & 0 deletions attestation/git/git_bin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2024 The Witness Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package git

import (
"os/exec"
"strings"

"github.com/go-git/go-git/v5"
"github.com/in-toto/go-witness/attestation"
"github.com/in-toto/go-witness/cryptoutil"
)

// GitExists checks if the git binary is available.
// This can be used to fall back to go-git implementation.
func GitExists() bool {

_, err := exec.LookPath("git")
if err != nil {
return false
} else {
return true
}
}

// GitGetBinPath retrieves the path to the git binary that is used by the attestor.
func GitGetBinPath() (string, error) {
path, err := exec.LookPath("git")
if err != nil {
return "", err
} else {
return path, nil
}
}

// GitGetBinHash retrieves a sha256 hash of the git binary that is located on the system.
// The path is determined based on exec.LookPath().
func GitGetBinHash(ctx *attestation.AttestationContext) (cryptoutil.DigestSet, error) {
path, err := exec.LookPath("git")
if err != nil {
return cryptoutil.DigestSet{}, err
}

gitBinDigest, err := cryptoutil.CalculateDigestSetFromFile(path, ctx.Hashes())
if err != nil {
return cryptoutil.DigestSet{}, err
}

if err != nil {
return cryptoutil.DigestSet{}, err
}

return gitBinDigest, nil
}

// GitGetStatus retrieves the status of staging and worktree
// from the git status --porcelain output
func GitGetStatus(workDir string) (map[string]Status, error) {

// Execute the git status --porcelain command
cmd := exec.Command("git", "-C", workDir, "status", "--porcelain")
outputBytes, err := cmd.Output()
if err != nil {
return map[string]Status{}, err
}

// Convert the output to a string and split into lines
output := string(outputBytes)
lines := strings.Split(output, "\n")

// Iterate over the lines and parse the status
var gitStatuses map[string]Status = make(map[string]Status)
for _, line := range lines {
// Skip empty lines
if len(line) == 0 {
continue
}

// The first two characters are the status codes
repoStatus := statusCodeString(git.StatusCode(line[0]))
worktreeStatus := statusCodeString(git.StatusCode(line[1]))
filePath := strings.TrimSpace(line[2:])

// Append the parsed status to the list
gitStatuses[filePath] = Status{
Staging: repoStatus,
Worktree: worktreeStatus,
}
}

return gitStatuses, nil
}
10 changes: 10 additions & 0 deletions schemagen/git.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
"$defs": {
"Attestor": {
"properties": {
"gittool": {
"type": "string"
},
"gitbinpath": {
"type": "string"
},
"gitbinhash": {
"$ref": "#/$defs/DigestSet"
},
"commithash": {
"type": "string"
},
Expand Down Expand Up @@ -71,6 +80,7 @@
"additionalProperties": false,
"type": "object",
"required": [
"gittool",
"commithash",
"author",
"authoremail",
Expand Down

0 comments on commit 80f9b23

Please sign in to comment.