-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Improve the git status speed. (#359)
* 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
1 parent
b93a379
commit 80f9b23
Showing
3 changed files
with
153 additions
and
4 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 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,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 | ||
} |
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