diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 4 | ||||
-rw-r--r-- | models/repo.go | 58 | ||||
-rw-r--r-- | models/user.go | 42 |
3 files changed, 51 insertions, 53 deletions
diff --git a/models/action.go b/models/action.go index 7917929d..d388bca9 100644 --- a/models/action.go +++ b/models/action.go @@ -22,8 +22,8 @@ const ( // Action represents user operation type and information to the repository. type Action struct { Id int64 - UserId int64 // Receiver user id. - OpType int + UserId int64 // Receiver user id. + OpType int // Operations: CREATE DELETE STAR ... ActUserId int64 // Action user id. ActUserName string // Action user name. RepoId int64 diff --git a/models/repo.go b/models/repo.go index 610c3597..38ab3d4a 100644 --- a/models/repo.go +++ b/models/repo.go @@ -5,6 +5,7 @@ package models import ( + "container/list" "errors" "fmt" "io/ioutil" @@ -144,10 +145,7 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv return nil, err } - rawSql := "UPDATE user SET num_repos = num_repos + 1 WHERE id = ?" - if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" { - rawSql = "UPDATE \"user\" SET num_repos = num_repos + 1 WHERE id = ?" - } + rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" if _, err = session.Exec(rawSql, user.Id); err != nil { session.Rollback() if err2 := os.RemoveAll(repoPath); err2 != nil { @@ -448,7 +446,7 @@ func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]* return nil, err } - commit, err := GetCommit(userName, reposName, branchName, commitId) + commit, err := repo.GetCommit(branchName, commitId) if err != nil { return nil, err } @@ -464,8 +462,10 @@ func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]* } var cm = commit - + var i int for { + i = i + 1 + //fmt.Println(".....", i, cm.Id(), cm.ParentCount()) if cm.ParentCount() == 0 { break } else if cm.ParentCount() == 1 { @@ -482,7 +482,10 @@ func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]* } else { var emptyCnt = 0 var sameIdcnt = 0 + var lastSameCm *git.Commit + //fmt.Println(".....", cm.ParentCount()) for i := 0; i < cm.ParentCount(); i++ { + //fmt.Println("parent", i, cm.Parent(i).Id()) p := cm.Parent(i) pt, _ := repo.SubTree(p.Tree, dirname) var pEntry *git.TreeEntry @@ -490,23 +493,31 @@ func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]* pEntry = pt.EntryByName(entry.Name) } + //fmt.Println("pEntry", pEntry) + if pEntry == nil { - if emptyCnt == cm.ParentCount()-1 { - goto loop - } else { - emptyCnt = emptyCnt + 1 - continue + emptyCnt = emptyCnt + 1 + if emptyCnt+sameIdcnt == cm.ParentCount() { + if lastSameCm == nil { + goto loop + } else { + cm = lastSameCm + break + } } } else { + //fmt.Println(i, "pEntry", pEntry.Id, "entry", entry.Id) if !pEntry.Id.Equal(entry.Id) { goto loop } else { - if sameIdcnt == cm.ParentCount()-1 { + lastSameCm = cm.Parent(i) + sameIdcnt = sameIdcnt + 1 + if emptyCnt+sameIdcnt == cm.ParentCount() { // TODO: now follow the first parent commit? - cm = cm.Parent(0) + cm = lastSameCm + //fmt.Println("sameId...") break } - sameIdcnt = sameIdcnt + 1 } } } @@ -541,26 +552,11 @@ func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, er return nil, err } - if commitid != "" { - oid, err := git.NewOidFromString(commitid) - if err != nil { - return nil, err - } - return repo.LookupCommit(oid) - } - if branchname == "" { - return nil, errors.New("no branch name and no commit id") - } - - r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname)) - if err != nil { - return nil, err - } - return r.LastCommit() + return repo.GetCommit(branchname, commitid) } // GetCommits returns all commits of given branch of repository. -func GetCommits(userName, reposName, branchname string) ([]*git.Commit, error) { +func GetCommits(userName, reposName, branchname string) (*list.List, error) { repo, err := git.OpenRepository(RepoPath(userName, reposName)) if err != nil { return nil, err diff --git a/models/user.go b/models/user.go index 87c644b2..5f08f9e9 100644 --- a/models/user.go +++ b/models/user.go @@ -19,14 +19,6 @@ import ( "github.com/gogits/gogs/modules/base" ) -var ( - UserPasswdSalt string -) - -func init() { - UserPasswdSalt = base.Cfg.MustValue("security", "USER_PASSWD_SALT") -} - // User types. const ( UT_INDIVIDUAL = iota + 1 @@ -56,6 +48,9 @@ type User struct { AvatarEmail string `xorm:"not null"` Location string Website string + IsActive bool + Rands string `xorm:"VARCHAR(10)"` + Expired time.Time Created time.Time `xorm:"created"` Updated time.Time `xorm:"updated"` } @@ -104,37 +99,44 @@ func (user *User) NewGitSig() *git.Signature { } } +// return a user salt token +func GetUserSalt() string { + return base.GetRandomString(10) +} + // RegisterUser creates record of a new user. -func RegisterUser(user *User) (err error) { +func RegisterUser(user *User) (*User, error) { isExist, err := IsUserExist(user.Name) if err != nil { - return err + return nil, err } else if isExist { - return ErrUserAlreadyExist + return nil, ErrUserAlreadyExist } isExist, err = IsEmailUsed(user.Email) if err != nil { - return err + return nil, err } else if isExist { - return ErrEmailAlreadyUsed + return nil, ErrEmailAlreadyUsed } user.LowerName = strings.ToLower(user.Name) user.Avatar = base.EncodeMd5(user.Email) user.AvatarEmail = user.Email + user.Expired = time.Now().Add(3 * 24 * time.Hour) + user.Rands = GetUserSalt() if err = user.EncodePasswd(); err != nil { - return err + return nil, err } else if _, err = orm.Insert(user); err != nil { - return err + return nil, err } else if err = os.MkdirAll(UserPath(user.Name), os.ModePerm); err != nil { if _, err := orm.Id(user.Id).Delete(&User{}); err != nil { - return errors.New(fmt.Sprintf( + return nil, errors.New(fmt.Sprintf( "both create userpath %s and delete table record faild: %v", user.Name, err)) } - return err + return nil, err } - return nil + return user, nil } // UpdateUser updates user's information. @@ -183,7 +185,7 @@ func DeleteUser(user *User) error { // EncodePasswd encodes password to safe format. func (user *User) EncodePasswd() error { - newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte(UserPasswdSalt), 16384, 8, 1, 64) + newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte(base.SecretKey), 16384, 8, 1, 64) user.Passwd = fmt.Sprintf("%x", newPasswd) return err } @@ -252,7 +254,7 @@ func LoginUserPlain(name, passwd string) (*User, error) { } else if !has { err = ErrUserNotExist } - return &user, nil + return &user, err } // FollowUser marks someone be another's follower. |