Skip to content

Commit

Permalink
repo: delete local copies on owner name change (#5843)
Browse files Browse the repository at this point in the history
* Protect local repo copy deletion with repoWorkingPool, and delete the local
copy on owner name change.

* Update internal/db/user.go

Co-Authored-By: Unknwon <u@gogs.io>

* Error format on local repo and wiki deletion
  • Loading branch information
guysmoilov authored and unknwon committed Nov 3, 2019
1 parent 97772f4 commit b40b85e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 9 additions & 2 deletions internal/db/repo.go
Expand Up @@ -1338,7 +1338,8 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil { if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
return fmt.Errorf("rename repository directory: %v", err) return fmt.Errorf("rename repository directory: %v", err)
} }
RemoveAllWithNotice("Delete repository local copy", repo.LocalCopyPath())
deleteRepoLocalCopy(repo)


// Rename remote wiki repository to new path and delete local copy. // Rename remote wiki repository to new path and delete local copy.
wikiPath := WikiPath(owner.Name, repo.Name) wikiPath := WikiPath(owner.Name, repo.Name)
Expand All @@ -1352,6 +1353,12 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
return sess.Commit() return sess.Commit()
} }


func deleteRepoLocalCopy(repo *Repository) {
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
RemoveAllWithNotice("Delete repository local copy", repo.LocalCopyPath())
}

// ChangeRepositoryName changes all corresponding setting from old repository name to new one. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) { func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
oldRepoName = strings.ToLower(oldRepoName) oldRepoName = strings.ToLower(oldRepoName)
Expand Down Expand Up @@ -1385,7 +1392,7 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath()) RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath())
} }


RemoveAllWithNotice("Delete repository local copy", repo.LocalCopyPath()) deleteRepoLocalCopy(repo)
return nil return nil
} }


Expand Down
6 changes: 4 additions & 2 deletions internal/db/user.go
Expand Up @@ -667,13 +667,15 @@ func ChangeUserName(u *User, newUserName string) (err error) {
return fmt.Errorf("ChangeUsernameInPullRequests: %v", err) return fmt.Errorf("ChangeUsernameInPullRequests: %v", err)
} }


// Delete all local copies of repository wiki that user owns. // Delete all local copies of repositories and wikis the user owns.
if err = x.Where("owner_id=?", u.ID).Iterate(new(Repository), func(idx int, bean interface{}) error { if err = x.Where("owner_id=?", u.ID).Iterate(new(Repository), func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
deleteRepoLocalCopy(repo)
// TODO: By the same reasoning, shouldn't we also sync access to the local wiki path?
RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath()) RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath())
return nil return nil
}); err != nil { }); err != nil {
return fmt.Errorf("Delete repository wiki local copy: %v", err) return fmt.Errorf("delete repository and wiki local copy: %v", err)
} }


// Rename or create user base directory // Rename or create user base directory
Expand Down

0 comments on commit b40b85e

Please sign in to comment.