mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-21 08:31:27 -05:00
improved ssh supports
This commit is contained in:
parent
d4728fafaf
commit
b1b6def5bc
6 changed files with 66 additions and 30 deletions
|
@ -1,4 +1,8 @@
|
||||||
APP_NAME = Gogs - Go Git Service
|
APP_NAME = Gogs - Go Git Service
|
||||||
|
RUN_USER = git
|
||||||
|
|
||||||
|
[repository]
|
||||||
|
ROOT = /home/git/gogs-repositories
|
||||||
|
|
||||||
[server]
|
[server]
|
||||||
HTTP_ADDR =
|
HTTP_ADDR =
|
||||||
|
|
3
gogs.go
3
gogs.go
|
@ -17,7 +17,7 @@ import (
|
||||||
// Test that go1.1 tag above is included in builds. main.go refers to this definition.
|
// Test that go1.1 tag above is included in builds. main.go refers to this definition.
|
||||||
const go11tag = true
|
const go11tag = true
|
||||||
|
|
||||||
const APP_VER = "0.0.0.0219"
|
const APP_VER = "0.0.0.0220"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
@ -36,5 +36,4 @@ func main() {
|
||||||
cli.BoolFlag{"noterm", "disable color output"},
|
cli.BoolFlag{"noterm", "disable color output"},
|
||||||
}...)
|
}...)
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
println("wo cao???")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,6 +77,8 @@ func setEngine() {
|
||||||
//x.ShowSQL = true
|
//x.ShowSQL = true
|
||||||
|
|
||||||
log.Trace("Initialized database -> %s", dbName)
|
log.Trace("Initialized database -> %s", dbName)
|
||||||
|
|
||||||
|
RepoRootPath = utils.Cfg.MustValue("repository", "ROOT")
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
git "github.com/libgit2/git2go"
|
git "github.com/libgit2/git2go"
|
||||||
|
"github.com/qiniu/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repo struct {
|
type Repo struct {
|
||||||
|
@ -35,7 +36,7 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
|
||||||
}
|
}
|
||||||
s, err := os.Stat(filepath.Join(RepoRootPath, user.Name, reposName))
|
s, err := os.Stat(filepath.Join(RepoRootPath, user.Name, reposName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, nil
|
||||||
}
|
}
|
||||||
return s.IsDir(), nil
|
return s.IsDir(), nil
|
||||||
}
|
}
|
||||||
|
@ -44,9 +45,7 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
|
||||||
// create a repository for a user or orgnaziation
|
// create a repository for a user or orgnaziation
|
||||||
//
|
//
|
||||||
func CreateRepository(user *User, reposName string) (*Repo, error) {
|
func CreateRepository(user *User, reposName string) (*Repo, error) {
|
||||||
p := filepath.Join(RepoRootPath, user.Name)
|
f := RepoPath(user.Name, reposName)
|
||||||
os.MkdirAll(p, os.ModePerm)
|
|
||||||
f := filepath.Join(p, reposName+".git")
|
|
||||||
_, err := git.InitRepository(f, false)
|
_, err := git.InitRepository(f, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -58,19 +57,28 @@ func CreateRepository(user *User, reposName string) (*Repo, error) {
|
||||||
session.Begin()
|
session.Begin()
|
||||||
_, err = session.Insert(&repo)
|
_, err = session.Insert(&repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.RemoveAll(f)
|
err2 := os.RemoveAll(f)
|
||||||
|
if err2 != nil {
|
||||||
|
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
||||||
|
}
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
|
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.RemoveAll(f)
|
err2 := os.RemoveAll(f)
|
||||||
|
if err2 != nil {
|
||||||
|
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
||||||
|
}
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = session.Commit()
|
err = session.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.RemoveAll(f)
|
err2 := os.RemoveAll(f)
|
||||||
|
if err2 != nil {
|
||||||
|
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
||||||
|
}
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -100,6 +108,10 @@ func UnWatchRepository() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RepoPath(userName, repoName string) string {
|
||||||
|
return filepath.Join(UserPath(userName), repoName+".git")
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteRepository deletes a repository for a user or orgnaztion.
|
// DeleteRepository deletes a repository for a user or orgnaztion.
|
||||||
func DeleteRepository(user *User, reposName string) (err error) {
|
func DeleteRepository(user *User, reposName string) (err error) {
|
||||||
session := orm.NewSession()
|
session := orm.NewSession()
|
||||||
|
@ -115,8 +127,9 @@ func DeleteRepository(user *User, reposName string) (err error) {
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = os.RemoveAll(filepath.Join(RepoRootPath, user.Name, reposName+".git")); err != nil {
|
if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil {
|
||||||
// TODO: log and delete manully
|
// TODO: log and delete manully
|
||||||
|
log.Error("delete repo %s/%s failed", user.Name, reposName)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -7,12 +7,15 @@ package models
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dchest/scrypt"
|
"github.com/dchest/scrypt"
|
||||||
|
|
||||||
"github.com/gogits/gogs/utils"
|
"github.com/gogits/gogs/utils"
|
||||||
|
"github.com/gogits/gogs/utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// User types.
|
// User types.
|
||||||
|
@ -96,10 +99,22 @@ func RegisterUser(user *User) (err error) {
|
||||||
|
|
||||||
user.LowerName = strings.ToLower(user.Name)
|
user.LowerName = strings.ToLower(user.Name)
|
||||||
user.Avatar = utils.EncodeMd5(user.Email)
|
user.Avatar = utils.EncodeMd5(user.Email)
|
||||||
user.Updated = time.Now()
|
|
||||||
user.EncodePasswd()
|
user.EncodePasswd()
|
||||||
_, err = orm.Insert(user)
|
_, err = orm.Insert(user)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.MkdirAll(UserPath(user.Name), os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
_, err2 := orm.Id(user.Id).Delete(&User{})
|
||||||
|
if err2 != nil {
|
||||||
|
log.Error("create userpath %s failed and delete table record faild",
|
||||||
|
user.Name)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUser updates user's information.
|
// UpdateUser updates user's information.
|
||||||
|
@ -129,6 +144,10 @@ func (user *User) EncodePasswd() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UserPath(userName string) string {
|
||||||
|
return filepath.Join(RepoRootPath, userName)
|
||||||
|
}
|
||||||
|
|
||||||
func GetUserByKeyId(keyId int64) (*User, error) {
|
func GetUserByKeyId(keyId int64) (*User, error) {
|
||||||
user := new(User)
|
user := new(User)
|
||||||
has, err := orm.Sql("select a.* from user as a, public_key as b where a.id = b.owner_id and b.id=?", keyId).Get(user)
|
has, err := orm.Sql("select a.* from user as a, public_key as b where a.id = b.owner_id and b.id=?", keyId).Get(user)
|
||||||
|
|
35
serve.go
35
serve.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -66,16 +65,12 @@ func runServ(*cli.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f, _ := os.Create("test2.log")
|
println(cmd)
|
||||||
f.WriteString(cmd)
|
|
||||||
f.Close()
|
|
||||||
|
|
||||||
log.Info("cmd is %s", cmd)
|
|
||||||
|
|
||||||
verb, args := parseCmd(cmd)
|
verb, args := parseCmd(cmd)
|
||||||
rr := strings.SplitN(strings.Trim(args, "'"), "/", 1)
|
rr := strings.SplitN(strings.Trim(args, "'"), "/", 2)
|
||||||
if len(rr) != 2 {
|
if len(rr) != 2 {
|
||||||
fmt.Printf("Unavilable repository")
|
println("Unavilable repository", args)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
repoName := rr[1]
|
repoName := rr[1]
|
||||||
|
@ -84,6 +79,9 @@ func runServ(*cli.Context) {
|
||||||
}
|
}
|
||||||
isWrite := In(verb, COMMANDS_WRITE)
|
isWrite := In(verb, COMMANDS_WRITE)
|
||||||
isRead := In(verb, COMMANDS_READONLY)
|
isRead := In(verb, COMMANDS_READONLY)
|
||||||
|
|
||||||
|
println("repoPath:", models.RepoPath(user.Name, repoName))
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case isWrite:
|
case isWrite:
|
||||||
has, err := models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
|
has, err := models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
|
||||||
|
@ -92,7 +90,7 @@ func runServ(*cli.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !has {
|
if !has {
|
||||||
fmt.Println("You have no right to access this repository")
|
println("You have no right to access this repository")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case isRead:
|
case isRead:
|
||||||
|
@ -109,36 +107,36 @@ func runServ(*cli.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !has {
|
if !has {
|
||||||
fmt.Println("You have no right to access this repository")
|
println("You have no right to access this repository")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unknown command")
|
println("Unknown command")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
isExist, err := models.IsRepositoryExist(user, repoName)
|
isExist, err := models.IsRepositoryExist(user, repoName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Inernel error")
|
println("Inernel error:", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isExist {
|
if !isExist {
|
||||||
if isRead {
|
if isRead {
|
||||||
fmt.Println("Repository is not exist")
|
println("Repository", user.Name+"/"+repoName, "is not exist")
|
||||||
return
|
return
|
||||||
} else if isWrite {
|
} else if isWrite {
|
||||||
_, err := models.CreateRepository(user, repoName)
|
_, err := models.CreateRepository(user, repoName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Create repository failed")
|
println("Create repository failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fullPath := filepath.Join(models.RepoRootPath, user.Name, repoName+".git")
|
fullPath := models.RepoPath(user.Name, repoName)
|
||||||
newcmd := fmt.Sprintf("%s '%s'", verb, fullPath)
|
newcmd := fmt.Sprintf("%s '%s'", verb, fullPath)
|
||||||
fmt.Println(newcmd)
|
println(newcmd)
|
||||||
gitcmd := exec.Command("git", "shell", "-c", newcmd)
|
gitcmd := exec.Command("git", "shell", "-c", newcmd)
|
||||||
gitcmd.Stdout = os.Stdout
|
gitcmd.Stdout = os.Stdout
|
||||||
gitcmd.Stderr = os.Stderr
|
gitcmd.Stderr = os.Stderr
|
||||||
|
@ -150,13 +148,14 @@ func runServ(*cli.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseCmd(cmd string) (string, string) {
|
func parseCmd(cmd string) (string, string) {
|
||||||
ss := strings.SplitN(cmd, " ", 1)
|
ss := strings.SplitN(cmd, " ", 2)
|
||||||
if len(ss) != 2 {
|
if len(ss) != 2 {
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
|
|
||||||
verb, args := ss[0], ss[1]
|
verb, args := ss[0], ss[1]
|
||||||
if verb == "git" {
|
if verb == "git" {
|
||||||
ss = strings.SplitN(args, " ", 1)
|
ss = strings.SplitN(args, " ", 2)
|
||||||
args = ss[1]
|
args = ss[1]
|
||||||
verb = fmt.Sprintf("%s %s", verb, ss[0])
|
verb = fmt.Sprintf("%s %s", verb, ss[0])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue