| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package git |
| 6 |
| 7 import ( |
| 8 "crypto/sha1" |
| 9 "encoding/hex" |
| 10 "fmt" |
| 11 ) |
| 12 |
| 13 // Types /////////////////////////////////////////////////////////////////////// |
| 14 |
| 15 // ObjectID is an immutable 20-byte git-sha for any Object type. |
| 16 // This is a struct instead of a straight simple type so that external users |
| 17 // may not manipulate the byte content directly. |
| 18 type ObjectID struct { |
| 19 raw [20]byte |
| 20 } |
| 21 |
| 22 // Constants /////////////////////////////////////////////////////////////////// |
| 23 |
| 24 // The empty ObjectID (all zeros) |
| 25 var NoID = ObjectID{} |
| 26 |
| 27 // Constructors /////////////////////////////////////////////////////////////// |
| 28 |
| 29 // MakeObjectIDForData hashes |data| as the git type |typ| (blob, commit, tree), |
| 30 // and returns an ObjectID for it. |
| 31 func MakeObjectIDForData(typ ObjectType, data []byte) *ObjectID { |
| 32 h := sha1.New() |
| 33 h.Write([]byte(fmt.Sprintf("%s %d\x00", typ, len(data)))) |
| 34 h.Write(data) |
| 35 buf := [20]byte{} |
| 36 h.Sum(buf[:0]) // Sum APPENDS to the slice. |
| 37 return MakeObjectIDRaw(buf) |
| 38 } |
| 39 |
| 40 // MakeObjectIDRawErr treats |raw| as a 20-byte Id. panic()'s if len(raw) is not
20 |
| 41 func MakeObjectIDRawErr(raw []byte) (*ObjectID, error) { |
| 42 if len(raw) != 20 { |
| 43 return nil, fmt.Errorf( |
| 44 "MakeObjectIdRawErr() must be invoked with a 20 byte str
ing, got %#v", |
| 45 raw) |
| 46 } |
| 47 ret := &ObjectID{} |
| 48 copy(ret.raw[:], raw) |
| 49 return ret, nil |
| 50 } |
| 51 |
| 52 func MakeObjectIDRaw(raw [20]byte) *ObjectID { |
| 53 ret := &ObjectID{} |
| 54 copy(ret.raw[:], raw[:]) |
| 55 return ret |
| 56 } |
| 57 |
| 58 // MakeObjectID decodes the 40-byte |hexval| into an ObjectID. error's if |
| 59 // if it is not valid hexadecimal or the decoded value's length is not 20 |
| 60 func MakeObjectIDErr(hexval string) (*ObjectID, error) { |
| 61 raw, err := hex.DecodeString(hexval) |
| 62 if err != nil { |
| 63 return nil, err |
| 64 } |
| 65 return MakeObjectIDRawErr(raw) |
| 66 } |
| 67 |
| 68 func MakeObjectID(hexval string) *ObjectID { |
| 69 r, err := MakeObjectIDErr(hexval) |
| 70 if err != nil { |
| 71 panic(err) |
| 72 } |
| 73 return r |
| 74 } |
| 75 |
| 76 // Member functions //////////////////////////////////////////////////////////// |
| 77 |
| 78 func (o *ObjectID) ID() *ObjectID { return o } |
| 79 func (o *ObjectID) String() string { |
| 80 if *o != NoID { |
| 81 return hex.EncodeToString(o.raw[:]) |
| 82 } |
| 83 return "<NoID>" |
| 84 } |
| 85 |
| 86 // RawString returns a git hash-object compatible (binary) representation |
| 87 // of the ObjectID (e.g. suitable for inclusion in a tree object output) |
| 88 func (o *ObjectID) RawString() string { |
| 89 return string(o.raw[:]) |
| 90 } |
| OLD | NEW |