From 296814e887f9bcf0b1d44552deaf40e89e08ab50 Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 12 Feb 2019 13:07:31 +0000 Subject: Refactor editor upload, update and delete to use git plumbing and add LFS support (#5702) * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFile * Use git plumbing for upload: #5621 repo_editor.go: GetDiffPreview * Use git plumbing for upload: #5621 repo_editor.go: DeleteRepoFile * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFiles * Move branch checkout functions out of repo_editor.go as they are no longer used there * BUGFIX: The default permissions should be 100644 This is a change from the previous code but is more in keeping with the default behaviour of git. Signed-off-by: Andrew Thornton * Standardise cleanUploadFilename to more closely match git See verify_path in: https://github.com/git/git/blob/7f4e64169352e03476b0ea64e7e2973669e491a2/read-cache.c#L951 Signed-off-by: Andrew Thornton * Redirect on bad paths Signed-off-by: Andrew Thornton * Refactor to move the uploading functions out to a module Signed-off-by: Andrew Thornton * Add LFS support Signed-off-by: Andrew Thornton * Update upload.go attribution header Upload.go is essentially the remnants of repo_editor.go. The remaining code is essentially unchanged from the Gogs code, hence the Gogs attribution. * Delete upload files after session committed * Ensure that GIT_AUTHOR_NAME etc. are valid for git see #5774 Signed-off-by: Andrew Thornton * Add in test cases per @lafriks comment * Add space between gitea and github imports Signed-off-by: Andrew Thornton * more examples in TestCleanUploadName Signed-off-by: Andrew Thornton * fix formatting Signed-off-by: Andrew Thornton * Set the SSH_ORIGINAL_COMMAND to ensure hooks are run Signed-off-by: Andrew Thornton * Switch off SSH_ORIGINAL_COMMAND Signed-off-by: Andrew Thornton --- models/lfs.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'models/lfs.go') diff --git a/models/lfs.go b/models/lfs.go index 39b0b2dd69..94d3f57905 100644 --- a/models/lfs.go +++ b/models/lfs.go @@ -1,7 +1,11 @@ package models import ( + "crypto/sha256" + "encoding/hex" "errors" + "fmt" + "io" "code.gitea.io/gitea/modules/util" ) @@ -16,6 +20,11 @@ type LFSMetaObject struct { CreatedUnix util.TimeStamp `xorm:"created"` } +// Pointer returns the string representation of an LFS pointer file +func (m *LFSMetaObject) Pointer() string { + return fmt.Sprintf("%s\n%s%s\nsize %d\n", LFSMetaFileIdentifier, LFSMetaFileOidPrefix, m.Oid, m.Size) +} + // LFSTokenResponse defines the JSON structure in which the JWT token is stored. // This structure is fetched via SSH and passed by the Git LFS client to the server // endpoint for authorization. @@ -67,6 +76,16 @@ func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { return m, sess.Commit() } +// GenerateLFSOid generates a Sha256Sum to represent an oid for arbitrary content +func GenerateLFSOid(content io.Reader) (string, error) { + h := sha256.New() + if _, err := io.Copy(h, content); err != nil { + return "", err + } + sum := h.Sum(nil) + return hex.EncodeToString(sum), nil +} + // GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID. // It may return ErrLFSObjectNotExist or a database error. If the error is nil, // the returned pointer is a valid LFSMetaObject. -- cgit v1.2.3