diff options
author | Unknwon <u@gogs.io> | 2016-02-05 14:11:53 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-02-05 14:11:53 -0500 |
commit | f8182ac5216153f9ac6dbfe31437b03ddadbf286 (patch) | |
tree | a18bc6c2e250fc4b39f85c0427ab3b5ddb9b21c3 /models | |
parent | 4e96a4a62b1bfbe07bc97e75478c1289f6b4d5a9 (diff) | |
download | gitea-f8182ac5216153f9ac6dbfe31437b03ddadbf286.tar.gz gitea-f8182ac5216153f9ac6dbfe31437b03ddadbf286.zip |
#2558 delete local wiki copy when rename repo and user
Diffstat (limited to 'models')
-rw-r--r-- | models/admin.go | 15 | ||||
-rw-r--r-- | models/repo.go | 32 | ||||
-rw-r--r-- | models/user.go | 12 |
3 files changed, 37 insertions, 22 deletions
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)) } |