Merge pull request #3819 from giggsoff/dirty-fix

Append dirty tag with content hash
This commit is contained in:
Avi Deitcher 2022-09-07 12:49:48 +03:00 committed by GitHub
commit fc060cac15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -3,10 +3,13 @@ package pkglib
// Thin wrappers around git CLI invocations
import (
"bufio"
"crypto/sha256"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
@ -83,6 +86,32 @@ func (g git) isWorkTree(pkg string) (bool, error) {
return false, fmt.Errorf("unexpected output from git rev-parse --is-inside-work-tree: %s", tf)
}
func (g git) contentHash() (string, error) {
hash := sha256.New()
out, err := g.commandStdout(nil, "ls-files")
if err != nil {
return "", err
}
scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(out)))
for scanner.Scan() {
f, err := os.Open(filepath.Join(g.dir, scanner.Text()))
if err != nil {
return "", err
}
if _, err := io.Copy(hash, f); err != nil {
_ = f.Close()
return "", err
}
if err = f.Close(); err != nil {
return "", err
}
}
if err = scanner.Err(); err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func (g git) treeHash(pkg, commit string) (string, error) {
// we have to check if pkg is at the top level of the git tree,
// if that's the case we need to use tree hash from the commit itself

View File

@ -250,7 +250,15 @@ func NewFromCLI(fs *flag.FlagSet, args ...string) ([]Pkg, error) {
}
if dirty {
pkgHash += "-dirty"
contentHash, err := git.contentHash()
if err != nil {
return nil, err
}
if len(contentHash) < 7 {
return nil, fmt.Errorf("unexpected hash len: %d", len(contentHash))
}
// construct <ls-tree>-dirty-<content hash> tag
pkgHash += fmt.Sprintf("-dirty-%s", contentHash[0:7])
}
}
}