| Index: go/src/infra/libs/git/object.go
|
| diff --git a/go/src/infra/libs/git/object.go b/go/src/infra/libs/git/object.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..06780e7bc4552116c2675d9f5c953d7f35612c18
|
| --- /dev/null
|
| +++ b/go/src/infra/libs/git/object.go
|
| @@ -0,0 +1,57 @@
|
| +// 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 (
|
| + "fmt"
|
| + "strings"
|
| +)
|
| +
|
| +func ObjectFromRaw(typ ObjectType, data []byte) (InternableObject, error) {
|
| + return ObjectFromRawWithID(MakeObjectIDForData(typ, data), typ, data)
|
| +}
|
| +
|
| +func ObjectFromRawWithID(id Identifiable, typ ObjectType, data []byte) (InternableObject, error) {
|
| + switch typ {
|
| + case BlobType:
|
| + return NewBlobFromRawWithID(id, data), nil
|
| + case TreeType:
|
| + return NewTreeFromRawWithID(id, data)
|
| + case CommitType:
|
| + return NewCommitFromRawWithID(id, data)
|
| + default:
|
| + return nil, fmt.Errorf("unsupported object type: %s", typ)
|
| + }
|
| +}
|
| +
|
| +func InternImpl(s GitService, obj InternableObject, intern func(string) (string, error)) (*ObjectID, error) {
|
| + gotData := false
|
| + var data string
|
| + if !obj.Complete() {
|
| + gotData = true
|
| + data = obj.RawString()
|
| + }
|
| +
|
| + if *obj.ID() == NoID {
|
| + return nil, fmt.Errorf("%v still has NoID after calling RawString()", obj)
|
| + }
|
| + if s.HasObjectID(obj) {
|
| + return obj.ID(), nil
|
| + }
|
| +
|
| + switch obj.Type() {
|
| + case CommitType, BlobType, TreeType:
|
| + default:
|
| + return nil, fmt.Errorf("git.Intern: Unrecognized type %s", obj.Type())
|
| + }
|
| + if !gotData {
|
| + data = obj.RawString()
|
| + }
|
| + id_raw, err := intern(data)
|
| + if err != nil {
|
| + return nil, err
|
| + }
|
| + return MakeObjectIDErr(strings.TrimSpace(id_raw))
|
| +}
|
|
|