mirror of
				https://github.com/linuxkit/linuxkit.git
				synced 2025-10-31 03:40:16 +00:00 
			
		
		
		
	Merge pull request #3819 from giggsoff/dirty-fix
Append dirty tag with content hash
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -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]) | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user