From 4586c386e7fb41e3f7b673a120d661399703af87 Mon Sep 17 00:00:00 2001 From: Martin Garton Date: Wed, 2 Apr 2014 20:35:29 +0100 Subject: [PATCH 1/5] fix unique --- models/publickey.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/publickey.go b/models/publickey.go index 3f2fcabd3b..ed47ff209d 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -77,8 +77,8 @@ func init() { // PublicKey represents a SSH key of user. type PublicKey struct { Id int64 - OwnerId int64 `xorm:"index"` - Name string `xorm:"unique not null"` + OwnerId int64 `xorm:"unique(s) index not null"` + Name string `xorm:"unique(s) not null"` Fingerprint string Content string `xorm:"TEXT not null"` Created time.Time `xorm:"created"` From 6b9f7fd75883ebf9b38db87b9c22985f24fe4a54 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 4 Apr 2014 17:26:46 +0800 Subject: [PATCH 2/5] basiclly http push --- routers/repo/repo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 1cd3dd86ba..f45d063b5e 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -245,10 +245,10 @@ func Http(ctx *middleware.Context, params martini.Params) { reponame = reponame[:len(reponame)-4] } + dir := models.RepoPath(username, reponame) prefix := path.Join("/", username, params["reponame"]) server := webdav.NewServer( - models.RepoPath(username, reponame), - prefix, true) + dir, prefix, true) server.ServeHTTP(ctx.ResponseWriter, ctx.Req) } From fa34656b5dd914938950da6ce95230caab733da7 Mon Sep 17 00:00:00 2001 From: Wind0r Date: Fri, 4 Apr 2014 12:25:14 +0200 Subject: [PATCH 3/5] Add missing deps --- .gopmfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gopmfile b/.gopmfile index 9857af3502..ae92d45e3f 100644 --- a/.gopmfile +++ b/.gopmfile @@ -19,6 +19,9 @@ github.com/gogits/gfm= github.com/gogits/cache= github.com/gogits/session= github.com/gogits/webdav= +github.com/martini-contrib/oauth2= +github.com/martini-contrib/sessions= +code.google.com/p/goauth2= [res] include=templates|public|conf From eb803ec5eb3a7e66a107873ce6ee584b8c77b131 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 4 Apr 2014 18:31:09 -0400 Subject: [PATCH 4/5] Add transfer repository --- README.md | 4 +- README_ZH.md | 4 +- models/action.go | 13 +++++ models/models.go | 12 ++-- models/publickey.go | 4 +- models/repo.go | 100 +++++++++++++++++++++++++-------- models/user.go | 6 ++ modules/base/tool.go | 10 +++- routers/install.go | 2 +- routers/repo/repo.go | 23 ++++++++ templates/admin/config.tmpl | 2 +- templates/admin/dashboard.tmpl | 2 +- templates/admin/nav.tmpl | 2 +- templates/admin/repos.tmpl | 2 +- templates/admin/users.tmpl | 2 +- templates/repo/setting.tmpl | 54 +++++++++++++++++- templates/user/setting.tmpl | 2 +- 17 files changed, 198 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 8e971a8866..6061f5a715 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ More importantly, Gogs only needs one binary to setup your own project hosting o - Activity timeline - SSH/HTTPS(Clone only) protocol support. -- Register/delete account. -- Create/delete/watch public repository. +- Register/delete/rename account. +- Create/delete/watch/rename public repository. - Repository viewer. - Issue tracker. - Gravatar and cache support. diff --git a/README_ZH.md b/README_ZH.md index f1823a631c..e66f607a14 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -24,8 +24,8 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 - 活动时间线 - SSH/HTTPS(仅限 Clone) 协议支持 -- 注册/删除用户 -- 创建/删除/关注公开仓库 +- 注册/删除/重命名用户 +- 创建/删除/关注/重命名公开仓库 - 仓库浏览器 - Bug 追踪系统 - Gravatar 以及缓存支持 diff --git a/models/action.go b/models/action.go index 1e55df85e9..a642a82c98 100644 --- a/models/action.go +++ b/models/action.go @@ -21,6 +21,7 @@ const ( OP_COMMIT_REPO OP_CREATE_ISSUE OP_PULL_REQUEST + OP_TRANSFER_REPO ) // Action represents user operation type and other information to repository., @@ -108,6 +109,18 @@ func NewRepoAction(user *User, repo *Repository) (err error) { return err } +// TransferRepoAction adds new action for transfering repository. +func TransferRepoAction(user, newUser *User, repo *Repository) (err error) { + if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email, + OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name}); err != nil { + log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name) + return err + } + + log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName) + return err +} + // GetFeeds returns action list of given user in given context. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) { actions := make([]Action, 0, 20) diff --git a/models/models.go b/models/models.go index 384f1fc425..0ad863371c 100644 --- a/models/models.go +++ b/models/models.go @@ -53,10 +53,10 @@ func NewTestEngine(x *xorm.Engine) (err error) { // os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm) // x, err = xorm.NewEngine("sqlite3", DbCfg.Path) default: - return fmt.Errorf("Unknown database type: %s\n", DbCfg.Type) + return fmt.Errorf("Unknown database type: %s", DbCfg.Type) } if err != nil { - return fmt.Errorf("models.init(fail to conntect database): %v\n", err) + return fmt.Errorf("models.init(fail to conntect database): %v", err) } return x.Sync(new(User), new(PublicKey), new(Repository), new(Watch), @@ -75,10 +75,10 @@ func SetEngine() (err error) { os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm) orm, err = xorm.NewEngine("sqlite3", DbCfg.Path) default: - return fmt.Errorf("Unknown database type: %s\n", DbCfg.Type) + return fmt.Errorf("Unknown database type: %s", DbCfg.Type) } if err != nil { - return fmt.Errorf("models.init(fail to conntect database): %v\n", err) + return fmt.Errorf("models.init(fail to conntect database): %v", err) } // WARNNING: for serv command, MUST remove the output to os.stdout, @@ -89,7 +89,7 @@ func SetEngine() (err error) { f, err := os.Create(logPath) if err != nil { - return fmt.Errorf("models.init(fail to create xorm.log): %v\n", err) + return fmt.Errorf("models.init(fail to create xorm.log): %v", err) } orm.Logger = f @@ -104,7 +104,7 @@ func NewEngine() (err error) { return err } else if err = orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch), new(Action), new(Access), new(Issue), new(Comment)); err != nil { - return fmt.Errorf("sync database struct error: %v\n", err) + return fmt.Errorf("sync database struct error: %v", err) } return nil } diff --git a/models/publickey.go b/models/publickey.go index ed47ff209d..42d2523b5f 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -77,8 +77,8 @@ func init() { // PublicKey represents a SSH key of user. type PublicKey struct { Id int64 - OwnerId int64 `xorm:"unique(s) index not null"` - Name string `xorm:"unique(s) not null"` + OwnerId int64 `xorm:" index not null"` + Name string `xorm:" not null"` //UNIQUE(s) Fingerprint string Content string `xorm:"TEXT not null"` Created time.Time `xorm:"created"` diff --git a/models/repo.go b/models/repo.go index 56f2890977..e8ebce9250 100644 --- a/models/repo.go +++ b/models/repo.go @@ -143,17 +143,17 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv if err = initRepository(repoPath, user, repo, initReadme, repoLang, license); err != nil { return nil, err } - session := orm.NewSession() - defer session.Close() - session.Begin() + sess := orm.NewSession() + defer sess.Close() + sess.Begin() - if _, err = session.Insert(repo); err != nil { + if _, err = sess.Insert(repo); err != nil { if err2 := os.RemoveAll(repoPath); err2 != nil { log.Error("repo.CreateRepository(repo): %v", err) return nil, errors.New(fmt.Sprintf( "delete repo directory %s/%s failed(1): %v", user.Name, repoName, err2)) } - session.Rollback() + sess.Rollback() return nil, err } @@ -162,8 +162,8 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv RepoName: strings.ToLower(path.Join(user.Name, repo.Name)), Mode: AU_WRITABLE, } - if _, err = session.Insert(&access); err != nil { - session.Rollback() + if _, err = sess.Insert(&access); err != nil { + sess.Rollback() if err2 := os.RemoveAll(repoPath); err2 != nil { log.Error("repo.CreateRepository(access): %v", err) return nil, errors.New(fmt.Sprintf( @@ -173,8 +173,8 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv } rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" - if _, err = session.Exec(rawSql, user.Id); err != nil { - session.Rollback() + if _, err = sess.Exec(rawSql, user.Id); err != nil { + sess.Rollback() if err2 := os.RemoveAll(repoPath); err2 != nil { log.Error("repo.CreateRepository(repo count): %v", err) return nil, errors.New(fmt.Sprintf( @@ -183,8 +183,8 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv return nil, err } - if err = session.Commit(); err != nil { - session.Rollback() + if err = sess.Commit(); err != nil { + sess.Rollback() if err2 := os.RemoveAll(repoPath); err2 != nil { log.Error("repo.CreateRepository(commit): %v", err) return nil, errors.New(fmt.Sprintf( @@ -369,6 +369,59 @@ func RepoPath(userName, repoName string) string { return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git") } +// TransferOwnership transfers all corresponding setting from old user to new one. +func TransferOwnership(user *User, newOwner string, repo *Repository) (err error) { + newUser, err := GetUserByName(newOwner) + if err != nil { + return err + } + + // Update accesses. + accesses := make([]Access, 0, 10) + if err = orm.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repo.LowerName}); err != nil { + return err + } + for i := range accesses { + accesses[i].RepoName = newUser.LowerName + "/" + repo.LowerName + if accesses[i].UserName == user.LowerName { + accesses[i].UserName = newUser.LowerName + } + if err = UpdateAccess(&accesses[i]); err != nil { + return err + } + } + + // Update repository. + repo.OwnerId = newUser.Id + if _, err := orm.Id(repo.Id).Update(repo); err != nil { + return err + } + + // Update user repository number. + rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" + if _, err = orm.Exec(rawSql, newUser.Id); err != nil { + return err + } + rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" + if _, err = orm.Exec(rawSql, user.Id); err != nil { + return err + } + + // Add watch of new owner to repository. + if !IsWatching(newUser.Id, repo.Id) { + if err = WatchRepo(newUser.Id, repo.Id, true); err != nil { + return err + } + } + + if err = TransferRepoAction(user, newUser, repo); err != nil { + return err + } + + // Change repository directory name. + return os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)) +} + // ChangeRepositoryName changes all corresponding setting from old repository name to new one. func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) { // Update accesses. @@ -410,29 +463,30 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) { return ErrRepoNotExist } - session := orm.NewSession() - if err = session.Begin(); err != nil { + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { return err } - if _, err = session.Delete(&Repository{Id: repoId}); err != nil { - session.Rollback() + if _, err = sess.Delete(&Repository{Id: repoId}); err != nil { + sess.Rollback() return err } - if _, err := session.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil { - session.Rollback() + if _, err := sess.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil { + sess.Rollback() return err } rawSql := "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" - if _, err = session.Exec(rawSql, userId); err != nil { - session.Rollback() + if _, err = sess.Exec(rawSql, userId); err != nil { + sess.Rollback() return err } - if _, err = session.Delete(&Watch{RepoId: repoId}); err != nil { - session.Rollback() + if _, err = sess.Delete(&Watch{RepoId: repoId}); err != nil { + sess.Rollback() return err } - if err = session.Commit(); err != nil { - session.Rollback() + if err = sess.Commit(); err != nil { + sess.Rollback() return err } if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil { diff --git a/models/user.go b/models/user.go index c5e46b4804..2641a15ff2 100644 --- a/models/user.go +++ b/models/user.go @@ -105,11 +105,17 @@ type Member struct { // IsUserExist checks if given user name exist, // the user name should be noncased unique. func IsUserExist(name string) (bool, error) { + if len(name) == 0 { + return false, nil + } return orm.Get(&User{LowerName: strings.ToLower(name)}) } // IsEmailUsed returns true if the e-mail has been used. func IsEmailUsed(email string) (bool, error) { + if len(email) == 0 { + return false, nil + } return orm.Get(&User{Email: email}) } diff --git a/modules/base/tool.go b/modules/base/tool.go index 6876da7625..3946c4b56b 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -494,6 +494,8 @@ func ActionIcon(opType int) string { return "arrow-circle-o-right" case 6: // Create issue. return "exclamation-circle" + case 8: // Transfer repository. + return "share" default: return "invalid type" } @@ -503,8 +505,9 @@ const ( TPL_CREATE_REPO = `%s created repository %s` TPL_COMMIT_REPO = `%s pushed to %s at %s%s` TPL_COMMIT_REPO_LI = `
user-avatar %s %s
` - TPL_CREATE_Issue = `%s opened issue %s#%s + TPL_CREATE_ISSUE = `%s opened issue %s#%s
user-avatar %s
` + TPL_TRANSFER_REPO = `%s transfered repository %s to %s` ) type PushCommit struct { @@ -547,8 +550,11 @@ func ActionDesc(act Actioner) string { buf.String()) case 6: // Create issue. infos := strings.SplitN(content, "|", 2) - return fmt.Sprintf(TPL_CREATE_Issue, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0], + return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0], AvatarLink(email), infos[1]) + case 8: // Transfer repository. + newRepoLink := content + "/" + repoName + return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink) default: return "invalid type" } diff --git a/routers/install.go b/routers/install.go index 6bdae2afd5..032af48020 100644 --- a/routers/install.go +++ b/routers/install.go @@ -43,7 +43,7 @@ func GlobalInit() { if base.InstallLock { if err := models.NewEngine(); err != nil { - fmt.Println("%v", err) + fmt.Println(err) os.Exit(2) } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index bc41345a89..6a5a7ae434 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -314,6 +314,29 @@ func SettingPost(ctx *middleware.Context) { ctx.HTML(200, "repo/setting") } log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + case "transfer": + if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { + ctx.RenderWithErr("Please make sure you entered repository name is correct.", "repo/setting", nil) + return + } + + newOwner := ctx.Query("owner") + // Check if new owner exists. + isExist, err := models.IsUserExist(newOwner) + if err != nil { + ctx.Handle(404, "repo.SettingPost(transfer: check existence)", err) + return + } else if !isExist { + ctx.RenderWithErr("Please make sure you entered owner name is correct.", "repo/setting", nil) + return + } else if err = models.TransferOwnership(ctx.User, newOwner, ctx.Repo.Repository); err != nil { + ctx.Handle(404, "repo.SettingPost(transfer repository)", err) + return + } + log.Trace("%s Repository transfered: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newOwner) + + ctx.Redirect("/") + return case "delete": if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { ctx.RenderWithErr("Please make sure you entered repository name is correct.", "repo/setting", nil) diff --git a/templates/admin/config.tmpl b/templates/admin/config.tmpl index 96c52fb5a0..31cfb77bad 100644 --- a/templates/admin/config.tmpl +++ b/templates/admin/config.tmpl @@ -2,7 +2,7 @@ {{template "base/navbar" .}}
{{template "admin/nav" .}} -
+
Server Configuration diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 31970f7ac4..2334c676d9 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -2,7 +2,7 @@ {{template "base/navbar" .}}
{{template "admin/nav" .}} -
+
Statistic diff --git a/templates/admin/nav.tmpl b/templates/admin/nav.tmpl index 2ac6462571..33ecfae5e3 100644 --- a/templates/admin/nav.tmpl +++ b/templates/admin/nav.tmpl @@ -1,4 +1,4 @@ -
+
  • Dashboard
  • Users
  • diff --git a/templates/admin/repos.tmpl b/templates/admin/repos.tmpl index 8cc424db68..3c0f5e09f7 100644 --- a/templates/admin/repos.tmpl +++ b/templates/admin/repos.tmpl @@ -2,7 +2,7 @@ {{template "base/navbar" .}}
    {{template "admin/nav" .}} -
    +
    Repository Management diff --git a/templates/admin/users.tmpl b/templates/admin/users.tmpl index 63da451743..14fddf33c4 100644 --- a/templates/admin/users.tmpl +++ b/templates/admin/users.tmpl @@ -2,7 +2,7 @@ {{template "base/navbar" .}}
    {{template "admin/nav" .}} -
    +
    User Management diff --git a/templates/repo/setting.tmpl b/templates/repo/setting.tmpl index a177aba6ad..6e2d3bec8b 100644 --- a/templates/repo/setting.tmpl +++ b/templates/repo/setting.tmpl @@ -25,7 +25,7 @@
    - +
    @@ -64,12 +64,62 @@ Danger Zone
    +
    + +
    +
    Transfer ownership
    +
    Transfer this repo to another user or to an organization where you have admin rights.
    + + + +
    +
    -
    Delete this repository.
    +
    Delete this repository
    Once you delete a repository, there is no going back. Please be certain.
    diff --git a/templates/user/setting.tmpl b/templates/user/setting.tmpl index f44218cdec..b32689fe12 100644 --- a/templates/user/setting.tmpl +++ b/templates/user/setting.tmpl @@ -12,7 +12,7 @@
    - +
    From e41ab839c7dbbdffc60a4e02775f24add9d126d9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 4 Apr 2014 18:55:17 -0400 Subject: [PATCH 5/5] Use session for rolling back --- gogs.go | 2 +- models/access.go | 11 +++++++++++ models/repo.go | 43 ++++++++++++++++++++++++++++++++++++------- models/user.go | 18 +++++++++++++++--- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/gogs.go b/gogs.go index 034e131bc0..8d9159d6a8 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.2.0.0403 Alpha" +const APP_VER = "0.2.0.0404 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/access.go b/models/access.go index 83261575e6..2c0900151f 100644 --- a/models/access.go +++ b/models/access.go @@ -7,6 +7,8 @@ package models import ( "strings" "time" + + "github.com/lunny/xorm" ) // Access types. @@ -40,6 +42,15 @@ func UpdateAccess(access *Access) error { return err } +// UpdateAccess updates access information with session for rolling back. +func UpdateAccessWithSession(sess *xorm.Session, access *Access) error { + if _, err := sess.Id(access.Id).Update(access); err != nil { + sess.Rollback() + return err + } + return nil +} + // HasAccess returns true if someone can read or write to given repository. func HasAccess(userName, repoName string, mode int) (bool, error) { return orm.Get(&Access{ diff --git a/models/repo.go b/models/repo.go index e8ebce9250..acee6f6af6 100644 --- a/models/repo.go +++ b/models/repo.go @@ -381,45 +381,62 @@ func TransferOwnership(user *User, newOwner string, repo *Repository) (err error if err = orm.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repo.LowerName}); err != nil { return err } + + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + for i := range accesses { accesses[i].RepoName = newUser.LowerName + "/" + repo.LowerName if accesses[i].UserName == user.LowerName { accesses[i].UserName = newUser.LowerName } - if err = UpdateAccess(&accesses[i]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { return err } } // Update repository. repo.OwnerId = newUser.Id - if _, err := orm.Id(repo.Id).Update(repo); err != nil { + if _, err := sess.Id(repo.Id).Update(repo); err != nil { + sess.Rollback() return err } // Update user repository number. rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" - if _, err = orm.Exec(rawSql, newUser.Id); err != nil { + if _, err = sess.Exec(rawSql, newUser.Id); err != nil { + sess.Rollback() return err } rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" - if _, err = orm.Exec(rawSql, user.Id); err != nil { + if _, err = sess.Exec(rawSql, user.Id); err != nil { + sess.Rollback() return err } // Add watch of new owner to repository. if !IsWatching(newUser.Id, repo.Id) { if err = WatchRepo(newUser.Id, repo.Id, true); err != nil { + sess.Rollback() return err } } if err = TransferRepoAction(user, newUser, repo); err != nil { + sess.Rollback() return err } // Change repository directory name. - return os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)) + if err = os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() } // ChangeRepositoryName changes all corresponding setting from old repository name to new one. @@ -429,15 +446,27 @@ func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) if err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil { return err } + + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + for i := range accesses { accesses[i].RepoName = userName + "/" + newRepoName - if err = UpdateAccess(&accesses[i]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { return err } } // Change repository directory name. - return os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)) + if err = os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() } func UpdateRepository(repo *Repository) error { diff --git a/models/user.go b/models/user.go index 2641a15ff2..1ec3b29520 100644 --- a/models/user.go +++ b/models/user.go @@ -218,11 +218,18 @@ func ChangeUserName(user *User, newUserName string) (err error) { if err = orm.Find(&accesses, &Access{UserName: user.LowerName}); err != nil { return err } + + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + for i := range accesses { accesses[i].UserName = newUserName if strings.HasPrefix(accesses[i].RepoName, user.LowerName+"/") { accesses[i].RepoName = strings.Replace(accesses[i].RepoName, user.LowerName, newUserName, 1) - if err = UpdateAccess(&accesses[i]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { return err } } @@ -241,14 +248,19 @@ func ChangeUserName(user *User, newUserName string) (err error) { for j := range accesses { accesses[j].RepoName = newUserName + "/" + repos[i].LowerName - if err = UpdateAccess(&accesses[j]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil { return err } } } // Change user directory name. - return os.Rename(UserPath(user.LowerName), UserPath(newUserName)) + if err = os.Rename(UserPath(user.LowerName), UserPath(newUserName)); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() } // UpdateUser updates user's information.