Browse Source

repo: delete local copies on owner name change (#5843)

* 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
Guy Smoilov 5 years ago
parent
commit
b40b85e006
2 changed files with 13 additions and 4 deletions
  1. 9 2
      internal/db/repo.go
  2. 4 2
      internal/db/user.go

+ 9 - 2
internal/db/repo.go

@@ -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 {
 		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.
 	wikiPath := WikiPath(owner.Name, repo.Name)
@@ -1352,6 +1353,12 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
 	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.
 func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
 	oldRepoName = strings.ToLower(oldRepoName)
@@ -1385,7 +1392,7 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
 		RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath())
 	}
 
-	RemoveAllWithNotice("Delete repository local copy", repo.LocalCopyPath())
+	deleteRepoLocalCopy(repo)
 	return nil
 }
 

+ 4 - 2
internal/db/user.go

@@ -667,13 +667,15 @@ func ChangeUserName(u *User, newUserName string) (err error) {
 		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 {
 		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())
 		return 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