aboutsummaryrefslogtreecommitdiffstats
path: root/modules/git/repo_object.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/git/repo_object.go')
-rw-r--r--modules/git/repo_object.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/modules/git/repo_object.go b/modules/git/repo_object.go
index 3be8400d22..67060e30b0 100644
--- a/modules/git/repo_object.go
+++ b/modules/git/repo_object.go
@@ -4,6 +4,12 @@
package git
+import (
+ "bytes"
+ "io"
+ "strings"
+)
+
// ObjectType git object type
type ObjectType string
@@ -17,3 +23,24 @@ const (
// ObjectTag tag object type
ObjectTag ObjectType = "tag"
)
+
+// HashObject takes a reader and returns SHA1 hash for that reader
+func (repo *Repository) HashObject(reader io.Reader) (SHA1, error) {
+ idStr, err := repo.hashObject(reader)
+ if err != nil {
+ return SHA1{}, err
+ }
+ return NewIDFromString(idStr)
+}
+
+func (repo *Repository) hashObject(reader io.Reader) (string, error) {
+ cmd := NewCommand("hash-object", "-w", "--stdin")
+ stdout := new(bytes.Buffer)
+ stderr := new(bytes.Buffer)
+ err := cmd.RunInDirFullPipeline(repo.Path, stdout, stderr, reader)
+
+ if err != nil {
+ return "", err
+ }
+ return strings.TrimSpace(stdout.String()), nil
+}