Skip to content

Git LFS support #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move LFS routes to /user/repo/info/lfs/*
  • Loading branch information
Fabian committed Nov 7, 2016
commit 0f95196699a66199481465361f44c871a23ebf90
33 changes: 10 additions & 23 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,35 +605,22 @@ func runWeb(ctx *cli.Context) error {
}, ignSignIn, context.RepoAssignment(true), context.RepoRef())

m.Group("/:reponame", func() {

if setting.LFS.StartServer {
lfsHandler := lfs.NewLFSHandler()
m.Group("/info/lfs", func() {
m.Post("/objects/batch", lfsHandler.BatchHandler) // TODO MetaMatcher
m.Any("/objects/:oid", lfsHandler.ObjectOidHandler)
m.Post("/objects", lfsHandler.PostHandler) // TODO MetaMatcher
}, ignSignInAndCsrf)
}

m.Any("/*", ignSignInAndCsrf, repo.HTTP)
m.Head("/tasks/trigger", repo.TriggerTask)
})
})
// ***** END: Repository *****

// ***** START: LFS *****

if setting.LFS.StartServer {

lfsHandler := lfs.NewLFSHandler()

m.Group("/lfs", func() {

m.Post("/:user/:repo/objects/batch", lfsHandler.BatchHandler) // TODO MetaMatcher
m.Any("/:user/:repo/objects/:oid", lfsHandler.ObjectOidHandler)

m.Post("/:user/:repo/objects", lfsHandler.PostHandler) // TODO MetaMatcher
m.Post("/objects/batch", lfsHandler.BatchHandler) // TODO MetaMatcher

m.Any("/objects/:oid", lfsHandler.ObjectOidHandler)
m.Post("/objects", lfsHandler.PostHandler) // TODO MetaMatcher

}, ignSignInAndCsrf)

}

// ***** END: LFS *****

m.Group("/api", func() {
apiv1.RegisterRoutes(m)
}, ignSignIn)
Expand Down
12 changes: 6 additions & 6 deletions models/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
)

type LFSMetaObject struct {
ID int64 `xorm:"pk autoincr"`
Oid string `xorm:"UNIQUE NOT NULL"`
Size int64 `xorm:"NOT NULL"`
Existing bool `xorm:"-"`
Created time.Time `xorm:"-"`
CreatedUnix int64
Oid string `xorm:"pk"`
Size int64 `xorm:"NOT NULL"`
RepositoryID int64 `xorm:"NOT NULL"`
Existing bool `xorm:"-"`
Created time.Time `xorm:"-"`
CreatedUnix int64
}

var (
Expand Down
46 changes: 26 additions & 20 deletions modules/lfs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,7 @@ type ObjectError struct {

// ObjectLink builds a URL linking to the object.
func (v *RequestVars) ObjectLink() string {
path := ""

if len(v.User) > 0 {
path += fmt.Sprintf("/%s", v.User)
}

if len(v.Repo) > 0 {
path += fmt.Sprintf("/%s", v.Repo)
}

path += fmt.Sprintf("/objects/%s", v.Oid)

return fmt.Sprintf("%slfs%s", setting.AppUrl, path)
return fmt.Sprintf("%s%s/%s/info/lfs/objects/%s", setting.AppUrl, v.User, v.Repo, v.Oid)
}

// link provides a structure used to build a hypermedia representation of an HTTP link.
Expand Down Expand Up @@ -189,7 +177,16 @@ func (a *LFSHandler) PostHandler(ctx *context.Context) {
requireAuth(ctx)
}

meta, err := models.NewLFSMetaObject(&models.LFSMetaObject{Oid: rv.Oid, Size: rv.Size})
repositoryString := rv.User + "/" + rv.Repo
repository, err := models.GetRepositoryByRef(repositoryString)

if err != nil {
log.Debug("Could not find repository: %s - %s", repositoryString, err)
writeStatus(ctx, 404)
return
}

meta, err := models.NewLFSMetaObject(&models.LFSMetaObject{Oid: rv.Oid, Size: rv.Size, RepositoryID: repository.ID})

if err != nil {
writeStatus(ctx, 404)
Expand Down Expand Up @@ -230,8 +227,17 @@ func (a *LFSHandler) BatchHandler(ctx *context.Context) {
continue
}

repositoryString := object.User + "/" + object.Repo
repository, err := models.GetRepositoryByRef(repositoryString)

if err != nil {
log.Debug("Could not find repository: %s - %s", repositoryString, err)
writeStatus(ctx, 404)
return
}

// Object is not found
meta, err = models.NewLFSMetaObject(&models.LFSMetaObject{Oid: object.Oid, Size: object.Size})
meta, err = models.NewLFSMetaObject(&models.LFSMetaObject{Oid: object.Oid, Size: object.Size, RepositoryID: repository.ID})

if err == nil {
responseObjects = append(responseObjects, a.Represent(object, meta, meta.Existing, true))
Expand Down Expand Up @@ -316,8 +322,8 @@ func MetaMatcher(r macaron.Request) bool {
func unpack(ctx *context.Context) *RequestVars {
r := ctx.Req
rv := &RequestVars{
User: ctx.Params("user"),
Repo: ctx.Params("repo"),
User: ctx.Params("username"),
Repo: strings.TrimSuffix(ctx.Params("reponame"), ".git"),
Oid: ctx.Params("oid"),
Authorization: r.Header.Get("Authorization"),
}
Expand Down Expand Up @@ -350,8 +356,8 @@ func unpackbatch(ctx *context.Context) *BatchVars {
}

for i := 0; i < len(bv.Objects); i++ {
bv.Objects[i].User = ctx.Params("user")
bv.Objects[i].Repo = ctx.Params("repo")
bv.Objects[i].User = ctx.Params("username")
bv.Objects[i].Repo = strings.TrimSuffix(ctx.Params("reponame"), ".git")
bv.Objects[i].Authorization = r.Header.Get("Authorization")
}

Expand All @@ -373,7 +379,7 @@ func writeStatus(ctx *context.Context, status int) {
}

func logRequest(r macaron.Request, status int) {
log.Debug("LFS request - Method: %s, URL: %s, Status %s", r.Method, r.URL, status)
log.Debug("LFS request - Method: %s, URL: %s, Status %d", r.Method, r.URL, status)
}

// authenticate uses the authorization string to determine whether
Expand Down