1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 09:13:10 +00:00

Fix dump and restore respository (#16698)

* Fix dump and restore

* return different error message for get commit

* Fix missing delete release attachment when deleting repository

* Fix ci and add some comments

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
Lunny Xiao 2021-08-31 09:18:15 +08:00 committed by GitHub
parent c0f5da3e1a
commit 8d7704b5a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 4 deletions

View file

@ -1497,6 +1497,11 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
releaseAttachments = append(releaseAttachments, attachments[i].RelativePath()) releaseAttachments = append(releaseAttachments, attachments[i].RelativePath())
} }
if _, err = sess.In("release_id", builder.Select("id").From("`release`").Where(builder.Eq{"`release`.repo_id": repoID})).
Delete(&Attachment{}); err != nil {
return err
}
if _, err := sess.Exec("UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)", repo.ID); err != nil { if _, err := sess.Exec("UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)", repo.ID); err != nil {
return err return err
} }

View file

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -481,7 +482,9 @@ func (g *RepositoryDumper) CreatePullRequests(prs ...*base.PullRequest) error {
if err != nil { if err != nil {
log.Error("Fetch branch from %s failed: %v", pr.Head.CloneURL, err) log.Error("Fetch branch from %s failed: %v", pr.Head.CloneURL, err)
} else { } else {
headBranch := filepath.Join(g.gitPath(), "refs", "heads", pr.Head.OwnerName, pr.Head.Ref) // a new branch name with <original_owner_name/original_branchname> will be created to as new head branch
ref := path.Join(pr.Head.OwnerName, pr.Head.Ref)
headBranch := filepath.Join(g.gitPath(), "refs", "heads", ref)
if err := os.MkdirAll(filepath.Dir(headBranch), os.ModePerm); err != nil { if err := os.MkdirAll(filepath.Dir(headBranch), os.ModePerm); err != nil {
return err return err
} }
@ -494,10 +497,14 @@ func (g *RepositoryDumper) CreatePullRequests(prs ...*base.PullRequest) error {
if err != nil { if err != nil {
return err return err
} }
pr.Head.Ref = ref
} }
} }
} }
} }
// whatever it's a forked repo PR, we have to change head info as the same as the base info
pr.Head.OwnerName = pr.Base.OwnerName
pr.Head.RepoName = pr.Base.RepoName
} }
var err error var err error

View file

@ -278,7 +278,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
if !release.Draft { if !release.Draft {
commit, err := g.gitRepo.GetTagCommit(rel.TagName) commit, err := g.gitRepo.GetTagCommit(rel.TagName)
if err != nil { if err != nil {
return fmt.Errorf("GetCommit: %v", err) return fmt.Errorf("GetTagCommit[%v]: %v", rel.TagName, err)
} }
rel.NumCommits, err = commit.CommitsCount() rel.NumCommits, err = commit.CommitsCount()
if err != nil { if err != nil {

View file

@ -54,6 +54,7 @@ func RestoreRepo(ctx context.Context, repoDir, ownerName, repoName string, units
if err := json.Unmarshal(body, &ret); err != nil { if err := json.Unmarshal(body, &ret); err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Response body Unmarshal error: %v", err.Error()) return http.StatusInternalServerError, fmt.Sprintf("Response body Unmarshal error: %v", err.Error())
} }
return http.StatusInternalServerError, ret.Err
} }
return http.StatusOK, fmt.Sprintf("Restore repo %s/%s successfully", ownerName, repoName) return http.StatusOK, fmt.Sprintf("Restore repo %s/%s successfully", ownerName, repoName)

View file

@ -126,7 +126,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) {
commit, err := gitRepo.GetCommit(mergeCommit[:40]) commit, err := gitRepo.GetCommit(mergeCommit[:40])
if err != nil { if err != nil {
return nil, fmt.Errorf("GetCommit: %v", err) return nil, fmt.Errorf("GetMergeCommit[%v]: %v", mergeCommit[:40], err)
} }
return commit, nil return commit, nil

View file

@ -44,7 +44,7 @@ func createTag(gitRepo *git.Repository, rel *models.Release, msg string) (bool,
commit, err := gitRepo.GetCommit(rel.Target) commit, err := gitRepo.GetCommit(rel.Target)
if err != nil { if err != nil {
return false, fmt.Errorf("GetCommit: %v", err) return false, fmt.Errorf("createTag::GetCommit[%v]: %v", rel.Target, err)
} }
// Trim '--' prefix to prevent command line argument vulnerability. // Trim '--' prefix to prevent command line argument vulnerability.