From f8182ac5216153f9ac6dbfe31437b03ddadbf286 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 5 Feb 2016 14:11:53 -0500 Subject: [PATCH] #2558 delete local wiki copy when rename repo and user --- README.md | 2 +- gogs.go | 2 +- models/admin.go | 15 +++++++++++++++ models/repo.go | 32 ++++++++++++-------------------- models/user.go | 12 ++++++++++-- templates/.VERSION | 2 +- 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index dd8c088cb5..c8226d8d9f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) -##### Current version: 0.8.30 +##### Current version: 0.8.31 | Web | UI | Preview | |:-------------:|:-------:|:-------:| diff --git a/gogs.go b/gogs.go index 7695d0478d..0da7485e04 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.8.30.0204" +const APP_VER = "0.8.31.0205" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/admin.go b/models/admin.go index 1d6bf62961..811edde244 100644 --- a/models/admin.go +++ b/models/admin.go @@ -5,12 +5,15 @@ package models import ( + "fmt" + "os" "strings" "time" "github.com/Unknwon/com" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" ) type NoticeType int @@ -47,6 +50,18 @@ func CreateRepositoryNotice(desc string) error { return CreateNotice(NOTICE_REPOSITORY, desc) } +// RemoveAllWithNotice removes all directories in given path and +// creates a system notice when error occurs. +func RemoveAllWithNotice(title, path string) { + if err := os.RemoveAll(path); err != nil { + desc := fmt.Sprintf("%s [%s]: %v", title, path, err) + log.Warn(desc) + if err = CreateRepositoryNotice(desc); err != nil { + log.Error(4, "CreateRepositoryNotice: %v", err) + } + } +} + // CountNotices returns number of notices. func CountNotices() int64 { count, _ := x.Count(new(Notice)) diff --git a/models/repo.go b/models/repo.go index 868498bc8d..f838de7eaa 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1123,16 +1123,22 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) return ErrRepoAlreadyExist{u.Name, newRepoName} } + repo, err := GetRepositoryByName(u.Id, oldRepoName) + if err != nil { + return fmt.Errorf("GetRepositoryByName: %v", err) + } + // Change repository directory name. - if err = os.Rename(RepoPath(u.Name, oldRepoName), RepoPath(u.Name, newRepoName)); err != nil { + if err = os.Rename(repo.RepoPath(), RepoPath(u.Name, newRepoName)); err != nil { return fmt.Errorf("rename repository directory: %v", err) } - wikiPath := WikiPath(u.Name, oldRepoName) + wikiPath := repo.WikiPath() if com.IsExist(wikiPath) { if err = os.Rename(wikiPath, WikiPath(u.Name, newRepoName)); err != nil { return fmt.Errorf("rename repository wiki: %v", err) } + RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath()) } return nil @@ -1295,30 +1301,16 @@ func DeleteRepository(uid, repoID int64) error { // Remove repository files. repoPath := repo.repoPath(sess) - if err = os.RemoveAll(repoPath); err != nil { - desc := fmt.Sprintf("delete repository files [%s]: %v", repoPath, err) - log.Warn(desc) - if err = CreateRepositoryNotice(desc); err != nil { - log.Error(4, "CreateRepositoryNotice: %v", err) - } - } + RemoveAllWithNotice("Delete repository files", repoPath) wikiPaths := []string{repo.WikiPath(), repo.LocalWikiPath()} for _, wikiPath := range wikiPaths { - if err = os.RemoveAll(wikiPath); err != nil { - desc := fmt.Sprintf("delete repository wiki [%s]: %v", wikiPath, err) - log.Warn(desc) - if err = CreateRepositoryNotice(desc); err != nil { - log.Error(4, "CreateRepositoryNotice: %v", err) - } - } + RemoveAllWithNotice("Delete repository wiki", wikiPath) } // Remove attachment files. for i := range attachmentPaths { - if err = os.Remove(attachmentPaths[i]); err != nil { - log.Warn("delete attachment: %v", err) - } + RemoveAllWithNotice("Delete attachment", attachmentPaths[i]) } if err = sess.Commit(); err != nil { @@ -1333,7 +1325,7 @@ func DeleteRepository(uid, repoID int64) error { } for i := range forkRepos { if err = DeleteRepository(forkRepos[i].OwnerID, forkRepos[i].ID); err != nil { - log.Error(4, "updateRepository[%d]: %v", forkRepos[i].ID, err) + log.Error(4, "DeleteRepository [%d]: %v", forkRepos[i].ID, err) } } } else { diff --git a/models/user.go b/models/user.go index 23ed5ae8d2..09ad74d76d 100644 --- a/models/user.go +++ b/models/user.go @@ -598,11 +598,19 @@ func ChangeUserName(u *User, newUserName string) (err error) { return ErrUserAlreadyExist{newUserName} } - err = ChangeUsernameInPullRequests(u.Name, newUserName) - if err != nil { + if err = ChangeUsernameInPullRequests(u.Name, newUserName); err != nil { return fmt.Errorf("ChangeUsernameInPullRequests: %v", err) } + // Delete all local copies of repository wiki that user owns. + if err = x.Where("owner_id=?", u.Id).Iterate(new(Repository), func(idx int, bean interface{}) error { + repo := bean.(*Repository) + RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath()) + return nil + }); err != nil { + return fmt.Errorf("Delete repository wiki local copy: %v", err) + } + return os.Rename(UserPath(u.Name), UserPath(newUserName)) } diff --git a/templates/.VERSION b/templates/.VERSION index 44eb8eeeaf..9ac7ae77d1 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.8.30.0204 \ No newline at end of file +0.8.31.0205 \ No newline at end of file