Chromium Code Reviews| Index: go/src/infra/libs/git/intern.go |
| diff --git a/go/src/infra/libs/git/intern.go b/go/src/infra/libs/git/intern.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..66790c392479044686bedc2c66688609a081baf2 |
| --- /dev/null |
| +++ b/go/src/infra/libs/git/intern.go |
| @@ -0,0 +1,43 @@ |
| +package git |
| + |
| +import "fmt" |
| +import "strings" |
| + |
| +// InternResult contians either the ID of the intern'd object, or an error. |
|
M-A Ruel
2014/10/18 00:47:05
contains
iannucci
2014/10/20 21:11:57
Done (removed this struct entirely)
|
| +type InternResult struct { |
| + Err error |
| + ID ObjectID |
| +} |
| + |
| +// Intern takes an InternableObject (Blob, Tree, Commit), and writes it into |
| +// the on-disk Repo. |
| +func (r *Repo) Intern(obj InternableObject) InternResult { |
| + gotData := false |
| + var data string |
| + if !obj.Complete() { |
| + gotData = true |
| + data = obj.RawString() |
| + } |
| + |
| + if obj.ID() != NoID && r.HasObjectID(obj.ID()) { |
| + return InternResult{ID: obj.ID()} |
| + } |
| + |
| + switch obj.Type() { |
| + case "commit", "tree", "blob": |
| + default: |
| + return InternResult{Err: fmt.Errorf( |
| + "git.Intern: Unrecognized type %s", obj.Type())} |
| + } |
| + if !gotData { |
| + data = obj.RawString() |
| + } |
| + cmd := []string{"hash-object", "-t", obj.Type(), "-w", "--stdin"} |
| + out, ok := r.RunInput(data, cmd...) |
| + if !ok { |
| + return InternResult{ |
| + Err: fmt.Errorf("error running %s <- %s: not ok", cmd, data), |
| + } |
| + } |
| + return InternResult{ID: MakeObjectID(strings.TrimSpace(string(out)))} |
| +} |