Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Unified Diff: go/src/infra/libs/git/objectID.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: more tests and refactors Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/libs/git/object.go ('k') | go/src/infra/libs/git/objectID_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/libs/git/objectID.go
diff --git a/go/src/infra/libs/git/objectID.go b/go/src/infra/libs/git/objectID.go
new file mode 100644
index 0000000000000000000000000000000000000000..b43a342ee712acfc005caadb89413eae019aee20
--- /dev/null
+++ b/go/src/infra/libs/git/objectID.go
@@ -0,0 +1,90 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package git
+
+import (
+ "crypto/sha1"
+ "encoding/hex"
+ "fmt"
+)
+
+// Types ///////////////////////////////////////////////////////////////////////
+
+// ObjectID is an immutable 20-byte git-sha for any Object type.
+// This is a struct instead of a straight simple type so that external users
+// may not manipulate the byte content directly.
+type ObjectID struct {
+ raw [20]byte
+}
+
+// Constants ///////////////////////////////////////////////////////////////////
+
+// The empty ObjectID (all zeros)
+var NoID = ObjectID{}
+
+// Constructors ///////////////////////////////////////////////////////////////
+
+// MakeObjectIDForData hashes |data| as the git type |typ| (blob, commit, tree),
+// and returns an ObjectID for it.
+func MakeObjectIDForData(typ ObjectType, data []byte) *ObjectID {
+ h := sha1.New()
+ h.Write([]byte(fmt.Sprintf("%s %d\x00", typ, len(data))))
+ h.Write(data)
+ buf := [20]byte{}
+ h.Sum(buf[:0]) // Sum APPENDS to the slice.
+ return MakeObjectIDRaw(buf)
+}
+
+// MakeObjectIDRawErr treats |raw| as a 20-byte Id. panic()'s if len(raw) is not 20
+func MakeObjectIDRawErr(raw []byte) (*ObjectID, error) {
+ if len(raw) != 20 {
+ return nil, fmt.Errorf(
+ "MakeObjectIdRawErr() must be invoked with a 20 byte string, got %#v",
+ raw)
+ }
+ ret := &ObjectID{}
+ copy(ret.raw[:], raw)
+ return ret, nil
+}
+
+func MakeObjectIDRaw(raw [20]byte) *ObjectID {
+ ret := &ObjectID{}
+ copy(ret.raw[:], raw[:])
+ return ret
+}
+
+// MakeObjectID decodes the 40-byte |hexval| into an ObjectID. error's if
+// if it is not valid hexadecimal or the decoded value's length is not 20
+func MakeObjectIDErr(hexval string) (*ObjectID, error) {
+ raw, err := hex.DecodeString(hexval)
+ if err != nil {
+ return nil, err
+ }
+ return MakeObjectIDRawErr(raw)
+}
+
+func MakeObjectID(hexval string) *ObjectID {
+ r, err := MakeObjectIDErr(hexval)
+ if err != nil {
+ panic(err)
+ }
+ return r
+}
+
+// Member functions ////////////////////////////////////////////////////////////
+
+func (o *ObjectID) ID() *ObjectID { return o }
+func (o *ObjectID) String() string {
+ if *o != NoID {
+ return hex.EncodeToString(o.raw[:])
+ }
+ return "<NoID>"
+}
+
+// RawString returns a git hash-object compatible (binary) representation
+// of the ObjectID (e.g. suitable for inclusion in a tree object output)
+func (o *ObjectID) RawString() string {
+ return string(o.raw[:])
+}
« no previous file with comments | « go/src/infra/libs/git/object.go ('k') | go/src/infra/libs/git/objectID_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698