go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/invocations/graph/source_hash.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package graph contains methods to explore reachable invocations. 16 package graph 17 18 import ( 19 "crypto/sha256" 20 "encoding/hex" 21 22 "go.chromium.org/luci/resultdb/pbutil" 23 pb "go.chromium.org/luci/resultdb/proto/v1" 24 ) 25 26 // SourceHash stores a 12 byte hash of sources, as raw bytes. 27 // To display, use the String() method. 28 type SourceHash string 29 30 const ( 31 EmptySourceHash SourceHash = "" 32 ) 33 34 func (h SourceHash) String() string { 35 return hex.EncodeToString([]byte(h)) 36 } 37 38 // HashSources returns a hash of the given code sources proto, 39 // which can be used as a key in maps. 40 func HashSources(s *pb.Sources) SourceHash { 41 h := sha256.New() 42 b := pbutil.MustMarshal(s) 43 _, err := h.Write(b) 44 if err != nil { 45 panic(err) 46 } 47 return SourceHash(string(h.Sum(nil)[:12])) 48 }