Skip to content

Commit 296814e

Browse files
zeripathlafriks
authored andcommitted
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 <[email protected]> * 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 <[email protected]> * Redirect on bad paths Signed-off-by: Andrew Thornton <[email protected]> * Refactor to move the uploading functions out to a module Signed-off-by: Andrew Thornton <[email protected]> * Add LFS support Signed-off-by: Andrew Thornton <[email protected]> * 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 <[email protected]> * Add in test cases per @lafriks comment * Add space between gitea and github imports Signed-off-by: Andrew Thornton <[email protected]> * more examples in TestCleanUploadName Signed-off-by: Andrew Thornton <[email protected]> * fix formatting Signed-off-by: Andrew Thornton <[email protected]> * Set the SSH_ORIGINAL_COMMAND to ensure hooks are run Signed-off-by: Andrew Thornton <[email protected]> * Switch off SSH_ORIGINAL_COMMAND Signed-off-by: Andrew Thornton <[email protected]>
1 parent fc038ca commit 296814e

File tree

11 files changed

+1135
-598
lines changed

11 files changed

+1135
-598
lines changed

models/lfs.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package models
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
46
"errors"
7+
"fmt"
8+
"io"
59

610
"code.gitea.io/gitea/modules/util"
711
)
@@ -16,6 +20,11 @@ type LFSMetaObject struct {
1620
CreatedUnix util.TimeStamp `xorm:"created"`
1721
}
1822

23+
// Pointer returns the string representation of an LFS pointer file
24+
func (m *LFSMetaObject) Pointer() string {
25+
return fmt.Sprintf("%s\n%s%s\nsize %d\n", LFSMetaFileIdentifier, LFSMetaFileOidPrefix, m.Oid, m.Size)
26+
}
27+
1928
// LFSTokenResponse defines the JSON structure in which the JWT token is stored.
2029
// This structure is fetched via SSH and passed by the Git LFS client to the server
2130
// endpoint for authorization.
@@ -67,6 +76,16 @@ func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) {
6776
return m, sess.Commit()
6877
}
6978

79+
// GenerateLFSOid generates a Sha256Sum to represent an oid for arbitrary content
80+
func GenerateLFSOid(content io.Reader) (string, error) {
81+
h := sha256.New()
82+
if _, err := io.Copy(h, content); err != nil {
83+
return "", err
84+
}
85+
sum := h.Sum(nil)
86+
return hex.EncodeToString(sum), nil
87+
}
88+
7089
// GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID.
7190
// It may return ErrLFSObjectNotExist or a database error. If the error is nil,
7291
// the returned pointer is a valid LFSMetaObject.

models/repo_branch.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,46 @@ import (
1414
"github.com/Unknwon/com"
1515
)
1616

17+
// discardLocalRepoBranchChanges discards local commits/changes of
18+
// given branch to make sure it is even to remote branch.
19+
func discardLocalRepoBranchChanges(localPath, branch string) error {
20+
if !com.IsExist(localPath) {
21+
return nil
22+
}
23+
// No need to check if nothing in the repository.
24+
if !git.IsBranchExist(localPath, branch) {
25+
return nil
26+
}
27+
28+
refName := "origin/" + branch
29+
if err := git.ResetHEAD(localPath, true, refName); err != nil {
30+
return fmt.Errorf("git reset --hard %s: %v", refName, err)
31+
}
32+
return nil
33+
}
34+
35+
// DiscardLocalRepoBranchChanges discards the local repository branch changes
36+
func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
37+
return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
38+
}
39+
40+
// checkoutNewBranch checks out to a new branch from the a branch name.
41+
func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
42+
if err := git.Checkout(localPath, git.CheckoutOptions{
43+
Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
44+
Branch: newBranch,
45+
OldBranch: oldBranch,
46+
}); err != nil {
47+
return fmt.Errorf("git checkout -b %s %s: %v", newBranch, oldBranch, err)
48+
}
49+
return nil
50+
}
51+
52+
// CheckoutNewBranch checks out a new branch
53+
func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
54+
return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
55+
}
56+
1757
// Branch holds the branch information
1858
type Branch struct {
1959
Path string

0 commit comments

Comments
 (0)