Add tar.gz download button and other mirror updates

This commit is contained in:
Unknown 2014-06-08 17:53:53 -04:00
parent 1f58d6f5d9
commit f160b4f33c
7 changed files with 79 additions and 89 deletions

View file

@ -1,4 +1,4 @@
// Copyright github.com/juju2013. All rights reserved. // Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -20,12 +20,13 @@ import (
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
) )
// Login types. type LoginType int
const ( const (
LT_NOTYPE = iota NOTYPE LoginType = iota
LT_PLAIN PLAIN
LT_LDAP LDAP
LT_SMTP SMTP
) )
var ( var (
@ -34,9 +35,9 @@ var (
ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users") ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users")
) )
var LoginTypes = map[int]string{ var LoginTypes = map[LoginType]string{
LT_LDAP: "LDAP", LDAP: "LDAP",
LT_SMTP: "SMTP", SMTP: "SMTP",
} }
// Ensure structs implmented interface. // Ensure structs implmented interface.
@ -49,7 +50,6 @@ type LDAPConfig struct {
ldap.Ldapsource ldap.Ldapsource
} }
// implement
func (cfg *LDAPConfig) FromDB(bs []byte) error { func (cfg *LDAPConfig) FromDB(bs []byte) error {
return json.Unmarshal(bs, &cfg.Ldapsource) return json.Unmarshal(bs, &cfg.Ldapsource)
} }
@ -65,7 +65,6 @@ type SMTPConfig struct {
TLS bool TLS bool
} }
// implement
func (cfg *SMTPConfig) FromDB(bs []byte) error { func (cfg *SMTPConfig) FromDB(bs []byte) error {
return json.Unmarshal(bs, cfg) return json.Unmarshal(bs, cfg)
} }
@ -76,13 +75,13 @@ func (cfg *SMTPConfig) ToDB() ([]byte, error) {
type LoginSource struct { type LoginSource struct {
Id int64 Id int64
Type int Type LoginType
Name string `xorm:"unique"` Name string `xorm:"UNIQUE"`
IsActived bool `xorm:"not null default false"` IsActived bool `xorm:"NOT NULL DEFAULT false"`
Cfg core.Conversion `xorm:"TEXT"` Cfg core.Conversion `xorm:"TEXT"`
Created time.Time `xorm:"created"` AllowAutoRegister bool `xorm:"NOT NULL DEFAULT false"`
Updated time.Time `xorm:"updated"` Created time.Time `xorm:"CREATED"`
AllowAutoRegister bool `xorm:"not null default false"` Updated time.Time `xorm:"UPDATED"`
} }
func (source *LoginSource) TypeString() string { func (source *LoginSource) TypeString() string {
@ -97,21 +96,25 @@ func (source *LoginSource) SMTP() *SMTPConfig {
return source.Cfg.(*SMTPConfig) return source.Cfg.(*SMTPConfig)
} }
// for xorm callback
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
if colName == "type" { if colName == "type" {
ty := (*val).(int64) ty := (*val).(int64)
switch ty { switch LoginType(ty) {
case LT_LDAP: case LDAP:
source.Cfg = new(LDAPConfig) source.Cfg = new(LDAPConfig)
case LT_SMTP: case SMTP:
source.Cfg = new(SMTPConfig) source.Cfg = new(SMTPConfig)
} }
} }
} }
func CreateSource(source *LoginSource) error {
_, err := orm.Insert(source)
return err
}
func GetAuths() ([]*LoginSource, error) { func GetAuths() ([]*LoginSource, error) {
var auths = make([]*LoginSource, 0) var auths = make([]*LoginSource, 0, 5)
err := orm.Find(&auths) err := orm.Find(&auths)
return auths, err return auths, err
} }
@ -121,18 +124,12 @@ func GetLoginSourceById(id int64) (*LoginSource, error) {
has, err := orm.Id(id).Get(source) has, err := orm.Id(id).Get(source)
if err != nil { if err != nil {
return nil, err return nil, err
} } else if !has {
if !has {
return nil, ErrAuthenticationNotExist return nil, ErrAuthenticationNotExist
} }
return source, nil return source, nil
} }
func AddSource(source *LoginSource) error {
_, err := orm.Insert(source)
return err
}
func UpdateSource(source *LoginSource) error { func UpdateSource(source *LoginSource) error {
_, err := orm.Id(source.Id).AllCols().Update(source) _, err := orm.Id(source.Id).AllCols().Update(source)
return err return err
@ -164,14 +161,14 @@ func UserSignIn(uname, passwd string) (*User, error) {
return nil, err return nil, err
} }
if u.LoginType == LT_NOTYPE { if u.LoginType == NOTYPE {
if has { if has {
u.LoginType = LT_PLAIN u.LoginType = PLAIN
} }
} }
// for plain login, user must have existed. // for plain login, user must have existed.
if u.LoginType == LT_PLAIN { if u.LoginType == PLAIN {
if !has { if !has {
return nil, ErrUserNotExist return nil, ErrUserNotExist
} }
@ -191,22 +188,20 @@ func UserSignIn(uname, passwd string) (*User, error) {
} }
for _, source := range sources { for _, source := range sources {
if source.Type == LT_LDAP { if source.Type == LDAP {
u, err := LoginUserLdapSource(nil, uname, passwd, u, err := LoginUserLdapSource(nil, uname, passwd,
source.Id, source.Cfg.(*LDAPConfig), true) source.Id, source.Cfg.(*LDAPConfig), true)
if err == nil { if err == nil {
return u, nil return u, nil
} else {
log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err)
} }
} else if source.Type == LT_SMTP { log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err)
} else if source.Type == SMTP {
u, err := LoginUserSMTPSource(nil, uname, passwd, u, err := LoginUserSMTPSource(nil, uname, passwd,
source.Id, source.Cfg.(*SMTPConfig), true) source.Id, source.Cfg.(*SMTPConfig), true)
if err == nil { if err == nil {
return u, nil return u, nil
} else {
log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err)
} }
log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err)
} }
} }
@ -224,10 +219,10 @@ func UserSignIn(uname, passwd string) (*User, error) {
} }
switch u.LoginType { switch u.LoginType {
case LT_LDAP: case LDAP:
return LoginUserLdapSource(u, u.LoginName, passwd, return LoginUserLdapSource(u, u.LoginName, passwd,
source.Id, source.Cfg.(*LDAPConfig), false) source.Id, source.Cfg.(*LDAPConfig), false)
case LT_SMTP: case SMTP:
return LoginUserSMTPSource(u, u.LoginName, passwd, return LoginUserSMTPSource(u, u.LoginName, passwd,
source.Id, source.Cfg.(*SMTPConfig), false) source.Id, source.Cfg.(*SMTPConfig), false)
} }
@ -252,7 +247,7 @@ func LoginUserLdapSource(user *User, name, passwd string, sourceId int64, cfg *L
user = &User{ user = &User{
LowerName: strings.ToLower(name), LowerName: strings.ToLower(name),
Name: strings.ToLower(name), Name: strings.ToLower(name),
LoginType: LT_LDAP, LoginType: LDAP,
LoginSource: sourceId, LoginSource: sourceId,
LoginName: name, LoginName: name,
IsActive: true, IsActive: true,
@ -320,9 +315,8 @@ func SmtpAuth(host string, port int, a smtp.Auth, useTls bool) error {
return err return err
} }
return nil return nil
} else {
return ErrUnsupportedLoginType
} }
return ErrUnsupportedLoginType
} }
// Query if name/passwd can login against the LDAP direcotry pool // Query if name/passwd can login against the LDAP direcotry pool
@ -358,13 +352,12 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S
user = &User{ user = &User{
LowerName: strings.ToLower(loginName), LowerName: strings.ToLower(loginName),
Name: strings.ToLower(loginName), Name: strings.ToLower(loginName),
LoginType: LT_SMTP, LoginType: SMTP,
LoginSource: sourceId, LoginSource: sourceId,
LoginName: name, LoginName: name,
IsActive: true, IsActive: true,
Passwd: passwd, Passwd: passwd,
Email: name, Email: name,
} }
return RegisterUser(user) return RegisterUser(user)
} }

View file

@ -109,11 +109,11 @@ func NewRepoContext() {
// Repository represents a git repository. // Repository represents a git repository.
type Repository struct { type Repository struct {
Id int64 Id int64
OwnerId int64 `xorm:"unique(s)"` OwnerId int64 `xorm:"UNIQUE(s)"`
Owner *User `xorm:"-"` Owner *User `xorm:"-"`
ForkId int64 ForkId int64
LowerName string `xorm:"unique(s) index not null"` LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
Name string `xorm:"index not null"` Name string `xorm:"INDEX NOT NULL"`
Description string Description string
Website string Website string
NumWatches int NumWatches int
@ -131,8 +131,8 @@ type Repository struct {
IsBare bool IsBare bool
IsGoget bool IsGoget bool
DefaultBranch string DefaultBranch string
Created time.Time `xorm:"created"` Created time.Time `xorm:"CREATED"`
Updated time.Time `xorm:"updated"` Updated time.Time `xorm:"UPDATED"`
} }
func (repo *Repository) GetOwner() (err error) { func (repo *Repository) GetOwner() (err error) {
@ -184,6 +184,25 @@ type Mirror struct {
NextUpdate time.Time NextUpdate time.Time
} }
// MirrorRepository creates a mirror repository from source.
func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error {
_, stderr, err := com.ExecCmd("git", "clone", "--mirror", url, repoPath)
if err != nil {
return errors.New("git clone --mirror: " + stderr)
}
if _, err = orm.InsertOne(&Mirror{
RepoId: repoId,
RepoName: strings.ToLower(userName + "/" + repoName),
Interval: 24,
NextUpdate: time.Now().Add(24 * time.Hour),
}); err != nil {
return err
}
return git.UnpackRefs(repoPath)
}
func GetMirror(repoId int64) (*Mirror, error) { func GetMirror(repoId int64) (*Mirror, error) {
m := &Mirror{RepoId: repoId} m := &Mirror{RepoId: repoId}
has, err := orm.Get(m) has, err := orm.Get(m)
@ -223,25 +242,6 @@ func MirrorUpdate() {
} }
} }
// MirrorRepository creates a mirror repository from source.
func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error {
_, stderr, err := com.ExecCmd("git", "clone", "--mirror", url, repoPath)
if err != nil {
return errors.New("git clone --mirror: " + stderr)
}
if _, err = orm.InsertOne(&Mirror{
RepoId: repoId,
RepoName: strings.ToLower(userName + "/" + repoName),
Interval: 24,
NextUpdate: time.Now().Add(24 * time.Hour),
}); err != nil {
return err
}
return git.UnpackRefs(repoPath)
}
// MigrateRepository migrates a existing repository from other project hosting. // MigrateRepository migrates a existing repository from other project hosting.
func MigrateRepository(user *User, name, desc string, private, mirror bool, url string) (*Repository, error) { func MigrateRepository(user *User, name, desc string, private, mirror bool, url string) (*Repository, error) {
repo, err := CreateRepository(user, name, desc, "", "", private, mirror, false) repo, err := CreateRepository(user, name, desc, "", "", private, mirror, false)
@ -746,16 +746,11 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) {
sess.Rollback() sess.Rollback()
return err return err
} }
if err = sess.Commit(); err != nil { if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} }
if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil { return sess.Commit()
// TODO: log and delete manully
log.Error("delete repo %s/%s failed: %v", userName, repo.Name, err)
return err
}
return nil
} }
// GetRepositoryByName returns the repository by given name under user if exists. // GetRepositoryByName returns the repository by given name under user if exists.

View file

@ -47,7 +47,7 @@ type User struct {
FullName string FullName string
Email string `xorm:"unique not null"` Email string `xorm:"unique not null"`
Passwd string `xorm:"not null"` Passwd string `xorm:"not null"`
LoginType int LoginType LoginType
LoginSource int64 `xorm:"not null default 0"` LoginSource int64 `xorm:"not null default 0"`
LoginName string LoginName string
Type int Type int

View file

@ -10,7 +10,7 @@ body {
html, body { html, body {
height: 100%; height: 100%;
font-family: Helvetica, Arial, sans-serif; font-family: Arial, Helvetica, sans-serif;
} }
/* override bs3 */ /* override bs3 */

View file

@ -38,8 +38,8 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
} }
var u core.Conversion var u core.Conversion
switch form.Type { switch models.LoginType(form.Type) {
case models.LT_LDAP: case models.LDAP:
u = &models.LDAPConfig{ u = &models.LDAPConfig{
Ldapsource: ldap.Ldapsource{ Ldapsource: ldap.Ldapsource{
Host: form.Host, Host: form.Host,
@ -53,7 +53,7 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Name: form.AuthName, Name: form.AuthName,
}, },
} }
case models.LT_SMTP: case models.SMTP:
u = &models.SMTPConfig{ u = &models.SMTPConfig{
Auth: form.SmtpAuth, Auth: form.SmtpAuth,
Host: form.SmtpHost, Host: form.SmtpHost,
@ -66,14 +66,14 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
} }
var source = &models.LoginSource{ var source = &models.LoginSource{
Type: form.Type, Type: models.LoginType(form.Type),
Name: form.AuthName, Name: form.AuthName,
IsActived: true, IsActived: true,
AllowAutoRegister: form.AllowAutoRegister, AllowAutoRegister: form.AllowAutoRegister,
Cfg: u, Cfg: u,
} }
if err := models.AddSource(source); err != nil { if err := models.CreateSource(source); err != nil {
ctx.Handle(500, "admin.auths.NewAuth", err) ctx.Handle(500, "admin.auths.NewAuth", err)
return return
} }
@ -116,8 +116,8 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
} }
var config core.Conversion var config core.Conversion
switch form.Type { switch models.LoginType(form.Type) {
case models.LT_LDAP: case models.LDAP:
config = &models.LDAPConfig{ config = &models.LDAPConfig{
Ldapsource: ldap.Ldapsource{ Ldapsource: ldap.Ldapsource{
Host: form.Host, Host: form.Host,
@ -131,7 +131,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Name: form.AuthName, Name: form.AuthName,
}, },
} }
case models.LT_SMTP: case models.SMTP:
config = &models.SMTPConfig{ config = &models.SMTPConfig{
Auth: form.SmtpAuth, Auth: form.SmtpAuth,
Host: form.SmtpHost, Host: form.SmtpHost,
@ -147,7 +147,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Id: form.Id, Id: form.Id,
Name: form.AuthName, Name: form.AuthName,
IsActived: form.IsActived, IsActived: form.IsActived,
Type: form.Type, Type: models.LoginType(form.Type),
AllowAutoRegister: form.AllowAutoRegister, AllowAutoRegister: form.AllowAutoRegister,
Cfg: config, Cfg: config,
} }

View file

@ -51,12 +51,13 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
Email: form.Email, Email: form.Email,
Passwd: form.Password, Passwd: form.Password,
IsActive: true, IsActive: true,
LoginType: models.LT_PLAIN, LoginType: models.PLAIN,
} }
if len(form.LoginType) > 0 { if len(form.LoginType) > 0 {
fields := strings.Split(form.LoginType, "-") fields := strings.Split(form.LoginType, "-")
u.LoginType, _ = strconv.Atoi(fields[0]) tp, _ := strconv.Atoi(fields[0])
u.LoginType = models.LoginType(tp)
u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64) u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64)
u.LoginName = form.LoginName u.LoginName = form.LoginName
fmt.Println(u.LoginType, u.LoginSource, u.LoginName) fmt.Println(u.LoginType, u.LoginSource, u.LoginName)

View file

@ -27,6 +27,7 @@
<hr/> <hr/>
<div class="clone-zip text-center"> <div class="clone-zip text-center">
<a class="btn btn-success btn-lg" href="{{.RepoLink}}/archive/{{.BranchName}}/{{.Repository.Name}}.zip" rel="nofollow"><i class="fa fa-suitcase"></i>Download ZIP</a> <a class="btn btn-success btn-lg" href="{{.RepoLink}}/archive/{{.BranchName}}/{{.Repository.Name}}.zip" rel="nofollow"><i class="fa fa-suitcase"></i>Download ZIP</a>
<a class="btn btn-success btn-lg" href="{{.RepoLink}}/archive/{{.BranchName}}/{{.Repository.Name}}.tar.gz" rel="nofollow"><i class="fa fa-suitcase"></i>Download TAR.GZ</a>
</div> </div>
</div> </div>
</div> </div>