Skip to content
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

Git LFS support #120

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
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
60 changes: 57 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
gouuid "github.com/satori/go.uuid"
"github.com/urfave/cli"

"encoding/json"
"github.com/dgrijalva/jwt-go"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/httplib"
Expand All @@ -27,6 +29,7 @@ import (

const (
_ACCESS_DENIED_MESSAGE = "Repository does not exist or you do not have access"
LFSAuthenticateVerb = "git-lfs-authenticate"
)

var CmdServ = cli.Command{
Expand Down Expand Up @@ -63,9 +66,10 @@ func parseCmd(cmd string) (string, string) {

var (
allowedCommands = map[string]models.AccessMode{
"git-upload-pack": models.ACCESS_MODE_READ,
"git-upload-archive": models.ACCESS_MODE_READ,
"git-receive-pack": models.ACCESS_MODE_WRITE,
"git-upload-pack": models.ACCESS_MODE_READ,
"git-upload-archive": models.ACCESS_MODE_READ,
"git-receive-pack": models.ACCESS_MODE_WRITE,
"git-lfs-authenticate": models.ACCESS_MODE_NONE,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use LFSAuthenticateVerb here?

}
)

Expand Down Expand Up @@ -154,6 +158,14 @@ func runServ(c *cli.Context) error {
}

verb, args := parseCmd(cmd)

var lfsVerb string
if verb == LFSAuthenticateVerb && strings.Contains(args, " ") {
argsSplit := strings.SplitN(args, " ", 2)
args = strings.TrimSpace(argsSplit[0])
lfsVerb = strings.TrimSpace(argsSplit[1])
}

repoPath := strings.ToLower(strings.Trim(args, "'"))
rr := strings.SplitN(repoPath, "/", 2)
if len(rr) != 2 {
Expand Down Expand Up @@ -189,6 +201,14 @@ func runServ(c *cli.Context) error {
fail("Unknown git command", "Unknown git command %s", verb)
}

if verb == LFSAuthenticateVerb {
if lfsVerb == "upload" {
requestedMode = models.ACCESS_MODE_WRITE
} else {
requestedMode = models.ACCESS_MODE_READ
}
}

// Prohibit push to mirror repositories.
if requestedMode > models.ACCESS_MODE_READ && repo.IsMirror {
fail("mirror repository is read-only", "")
Expand Down Expand Up @@ -252,6 +272,40 @@ func runServ(c *cli.Context) error {
}
}

//LFS token authentication

if verb == LFSAuthenticateVerb {

url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppUrl, repoUser.Name, repo.Name)

now := time.Now().UTC()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"repo": repo.ID,
"op": lfsVerb,
"exp": now.Add(5 * time.Minute).Unix(),
})

// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(setting.LFS.JWTSecretBytes)
if err != nil {
fail("Internal error", "Failed to sign JWT token: %v", err)
}

tokenAuthentication := &models.LFSTokenResponse{
Header: make(map[string]string),
Href: url,
}
tokenAuthentication.Header["Authorization"] = fmt.Sprintf("Bearer %s", tokenString)

enc := json.NewEncoder(os.Stdout)
err = enc.Encode(tokenAuthentication)
if err != nil {
fail("Internal error", "Failed to encode LFS json response: %v", err)
}

return nil
}

uuid := gouuid.NewV4().String()
os.Setenv("uuid", uuid)

Expand Down
13 changes: 12 additions & 1 deletion cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/bindata"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/lfs"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/modules/template"
Expand Down Expand Up @@ -99,7 +100,7 @@ func checkVersion() {
for _, c := range checkers {
if !version.Compare(c.Version(), c.Expected, ">=") {
log.Fatal(4, `Dependency outdated!
Package '%s' current version (%s) is below requirement (%s),
Package '%s' current version (%s) is below requirement (%s),
please use following command to update this package and recompile Gogs:
go get -u %[1]s`, c.ImportPath, c.Version(), c.Expected)
}
Expand Down Expand Up @@ -604,6 +605,16 @@ 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)
m.Any("/objects/:oid", lfsHandler.ObjectOidHandler)
m.Post("/objects", lfsHandler.PostHandler)
}, ignSignInAndCsrf)
}

m.Any("/*", ignSignInAndCsrf, repo.HTTP)
m.Head("/tasks/trigger", repo.TriggerTask)
})
Expand Down
3 changes: 3 additions & 0 deletions conf/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ app_name = Application Name
app_name_helper = Put your organization name here huge and loud!
repo_path = Repository Root Path
repo_path_helper = All Git remote repositories will be saved to this directory.
lfs_path = LFS Root Path
lfs_path_helper = Files stored with Git LFS will be stored in this directory. Leave empty to disable LFS.
run_user = Run User
run_user_helper = The user must have access to Repository Root Path and run Gogs.
domain = Domain
Expand Down Expand Up @@ -1041,6 +1043,7 @@ config.disable_router_log = Disable Router Log
config.run_user = Run User
config.run_mode = Run Mode
config.repo_root_path = Repository Root Path
config.lfs_root_path = LFS Root Path
config.static_file_root_path = Static File Root Path
config.log_file_root_path = Log File Root Path
config.script_type = Script Type
Expand Down
Loading