Skip to content

[WIP] Refactor: Replace RepoPath(string,string) with (*Repository).RepoPath() #6623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ func runHookPreReceive(c *cli.Context) error {
username := os.Getenv(models.EnvRepoUsername)
reponame := os.Getenv(models.EnvRepoName)
userIDStr := os.Getenv(models.EnvPusherID)
repoPath := models.RepoPath(username, reponame)

repo, err := models.GetRepositoryByOwnerAndName(username, reponame)
if err != nil {
fail("repository %s/%s does not exist: %v", username, reponame, err)
}
repoPath := repo.RepoPath()

buf := bytes.NewBuffer(nil)
scanner := bufio.NewScanner(os.Stdin)
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v82.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
userCache[repo.OwnerID] = user
}

gitRepo, err = git.OpenRepository(models.RepoPath(user.Name, repo.Name))
gitRepo, err = git.OpenRepository(models.MakeRepoPath(user.Name, repo.Name))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false)
}()

headRepoPath := RepoPath(pr.HeadUserName, pr.HeadRepo.Name)
headRepoPath := MakeRepoPath(pr.HeadUserName, pr.HeadRepo.Name)

// Clone base repo.
tmpBasePath := path.Join(LocalCopyPath(), "merge-"+com.ToStr(time.Now().Nanosecond())+".git")
Expand Down Expand Up @@ -1172,7 +1172,7 @@ func (pr *PullRequest) UpdatePatch() (err error) {

// Add a temporary remote.
tmpRemote := com.ToStr(time.Now().UnixNano())
if err = headGitRepo.AddRemote(tmpRemote, RepoPath(pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name), true); err != nil {
if err = headGitRepo.AddRemote(tmpRemote, MakeRepoPath(pr.BaseRepo.MustOwner().Name, pr.BaseRepo.Name), true); err != nil {
return fmt.Errorf("AddRemote: %v", err)
}
defer func() {
Expand Down
4 changes: 2 additions & 2 deletions models/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestRelease_Create(t *testing.T) {

user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
repoPath := RepoPath(user.Name, repo.Name)
repoPath := MakeRepoPath(user.Name, repo.Name)

gitRepo, err := git.OpenRepository(repoPath)
assert.NoError(t, err)
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestRelease_MirrorDelete(t *testing.T) {

user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
repoPath := RepoPath(user.Name, repo.Name)
repoPath := MakeRepoPath(user.Name, repo.Name)
migrationOptions := MigrateRepoOptions{
Name: "test_mirror",
Description: "Test mirror",
Expand Down
22 changes: 11 additions & 11 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (repo *Repository) getBaseRepo(e Engine) (err error) {
}

func (repo *Repository) repoPath(e Engine) string {
return RepoPath(repo.mustOwnerName(e), repo.Name)
return MakeRepoPath(repo.mustOwnerName(e), repo.Name)
}

// RepoPath returns the repository path
Expand Down Expand Up @@ -790,7 +790,7 @@ func (repo *Repository) patchPath(e Engine, index int64) (string, error) {
return "", err
}

return filepath.Join(RepoPath(repo.Owner.Name, repo.Name), "pulls", com.ToStr(index)+".patch"), nil
return filepath.Join(MakeRepoPath(repo.Owner.Name, repo.Name), "pulls", com.ToStr(index)+".patch"), nil
}

// SavePatch saves patch data to corresponding location by given issue ID.
Expand Down Expand Up @@ -821,7 +821,7 @@ func isRepositoryExist(e Engine, u *User, repoName string) (bool, error) {
OwnerID: u.ID,
LowerName: strings.ToLower(repoName),
})
return has && com.IsDir(RepoPath(u.Name, repoName)), err
return has && com.IsDir(MakeRepoPath(u.Name, repoName)), err
}

// IsRepositoryExist returns true if the repository with given name under user has already existed.
Expand Down Expand Up @@ -910,7 +910,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
return nil, err
}

repoPath := RepoPath(u.Name, opts.Name)
repoPath := MakeRepoPath(u.Name, opts.Name)
wikiPath := WikiPath(u.Name, opts.Name)

if u.IsOrganization() {
Expand Down Expand Up @@ -1404,7 +1404,7 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err

// No need for init mirror.
if !opts.IsMirror {
repoPath := RepoPath(u.Name, repo.Name)
repoPath := MakeRepoPath(u.Name, repo.Name)
if err = initRepository(sess, repoPath, u, repo, opts); err != nil {
if err2 := os.RemoveAll(repoPath); err2 != nil {
log.Error("initRepository: %v", err)
Expand Down Expand Up @@ -1456,8 +1456,8 @@ func CountUserRepositories(userID int64, private bool) int64 {
return countRepositories(userID, private)
}

// RepoPath returns repository path by given user and repository name.
func RepoPath(userName, repoName string) string {
// MakeRepoPath returns repository path by given user and repository name.
func MakeRepoPath(userName, repoName string) string {
return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
}

Expand Down Expand Up @@ -1558,7 +1558,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
return fmt.Errorf("Failed to create dir %s: %v", dir, err)
}

if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
if err = os.Rename(MakeRepoPath(owner.Name, repo.Name), MakeRepoPath(newOwner.Name, repo.Name)); err != nil {
return fmt.Errorf("rename repository directory: %v", err)
}
removeAllWithNotice(sess, "Delete repository local copy", repo.LocalCopyPath())
Expand Down Expand Up @@ -1606,7 +1606,7 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))

newRepoPath := RepoPath(u.Name, newRepoName)
newRepoPath := MakeRepoPath(u.Name, newRepoName)
if err = os.Rename(repo.RepoPath(), newRepoPath); err != nil {
return fmt.Errorf("rename repository directory: %v", err)
}
Expand Down Expand Up @@ -2268,7 +2268,7 @@ func GitGcRepos() error {
}
_, stderr, err := process.GetManager().ExecDir(
time.Duration(setting.Git.Timeout.GC)*time.Second,
RepoPath(repo.Owner.Name, repo.Name), "Repository garbage collection",
MakeRepoPath(repo.Owner.Name, repo.Name), "Repository garbage collection",
"git", args...)
if err != nil {
return fmt.Errorf("%v: %v", err, stderr)
Expand Down Expand Up @@ -2454,7 +2454,7 @@ func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *R
return nil, err
}

repoPath := RepoPath(u.Name, repo.Name)
repoPath := MakeRepoPath(u.Name, repo.Name)
_, stderr, err := process.GetManager().ExecTimeout(10*time.Minute,
fmt.Sprintf("ForkRepository(git clone): %s/%s", u.Name, repo.Name),
"git", "clone", "--bare", oldRepo.repoPath(sess), repoPath)
Expand Down
4 changes: 2 additions & 2 deletions models/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func TestTransferOwnership(t *testing.T) {
transferredRepo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.EqualValues(t, 2, transferredRepo.OwnerID)

assert.False(t, com.IsExist(RepoPath("user3", "repo3")))
assert.True(t, com.IsExist(RepoPath("user2", "repo3")))
assert.False(t, com.IsExist(MakeRepoPath("user3", "repo3")))
assert.True(t, com.IsExist(MakeRepoPath("user2", "repo3")))
AssertExistsAndLoadBean(t, &Action{
OpType: ActionTransferRepo,
ActUserID: 2,
Expand Down
2 changes: 1 addition & 1 deletion models/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
}

repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
repoPath := MakeRepoPath(opts.RepoUserName, opts.RepoName)

gitUpdate := exec.Command("git", "update-server-info")
gitUpdate.Dir = repoPath
Expand Down
2 changes: 1 addition & 1 deletion modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func ReferencesGitRepo() macaron.Handler {

// For API calls.
if ctx.Repo.GitRepo == nil {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
repoPath := models.MakeRepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
Expand Down
6 changes: 3 additions & 3 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ func RepoAssignment() macaron.Handler {
return
}

gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
gitRepo, err := git.OpenRepository(models.MakeRepoPath(userName, repoName))
if err != nil {
ctx.ServerError("RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
ctx.ServerError("RepoAssignment Invalid repo "+models.MakeRepoPath(userName, repoName), err)
return
}
ctx.Repo.GitRepo = gitRepo
Expand Down Expand Up @@ -557,7 +557,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {

// For API calls.
if ctx.Repo.GitRepo == nil {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
repoPath := models.MakeRepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
if err != nil {
ctx.ServerError("RepoRef Invalid repo "+repoPath, err)
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func GetArchive(ctx *context.APIContext) {
// responses:
// 200:
// description: success
repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
repoPath := models.MakeRepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
ctx.Error(500, "OpenRepository", err)
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
headRepo = ctx.Repo.Repository
headGitRepo = ctx.Repo.GitRepo
} else {
headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
headGitRepo, err = git.OpenRepository(models.MakeRepoPath(headUser.Name, headRepo.Name))
if err != nil {
ctx.Error(500, "OpenRepository", err)
return nil, nil, nil, nil, "", ""
Expand All @@ -683,7 +683,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
return nil, nil, nil, nil, "", ""
}

prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
prInfo, err := headGitRepo.GetPullRequestInfo(models.MakeRepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
if err != nil {
ctx.Error(500, "GetPullRequestInfo", err)
return nil, nil, nil, nil, "", ""
Expand Down
6 changes: 3 additions & 3 deletions routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func Diff(ctx *context.Context) {

ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)

diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
diff, err := models.GetDiffCommit(models.MakeRepoPath(userName, repoName),
commitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil {
Expand Down Expand Up @@ -249,7 +249,7 @@ func Diff(ctx *context.Context) {
// RawDiff dumps diff results of repository in given commit ID to io.Writer
func RawDiff(ctx *context.Context) {
if err := models.GetRawDiff(
models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
models.MakeRepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
ctx.Params(":sha"),
models.RawDiffType(ctx.Params(":ext")),
ctx.Resp,
Expand All @@ -274,7 +274,7 @@ func CompareDiff(ctx *context.Context) {
return
}

diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
diff, err := models.GetDiffRange(models.MakeRepoPath(userName, repoName), beforeCommitID,
afterCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullReq
return nil
}

prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
prInfo, err := headGitRepo.GetPullRequestInfo(models.MakeRepoPath(repo.Owner.Name, repo.Name),
pull.BaseBranch, pull.HeadBranch)
if err != nil {
if strings.Contains(err.Error(), "fatal: Not a valid object name") {
Expand Down Expand Up @@ -457,7 +457,7 @@ func ViewPullFiles(ctx *context.Context) {
return
}

headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
headRepoPath := models.MakeRepoPath(pull.HeadUserName, pull.HeadRepo.Name)

headGitRepo, err := git.OpenRepository(headRepoPath)
if err != nil {
Expand Down Expand Up @@ -690,7 +690,7 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
headGitRepo = ctx.Repo.GitRepo
ctx.Data["BaseName"] = headUser.Name
} else {
headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
headGitRepo, err = git.OpenRepository(models.MakeRepoPath(headUser.Name, headRepo.Name))
ctx.Data["BaseName"] = baseRepo.OwnerName
if err != nil {
ctx.ServerError("OpenRepository", err)
Expand Down Expand Up @@ -722,7 +722,7 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
}
ctx.Data["HeadBranches"] = headBranches

prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
prInfo, err := headGitRepo.GetPullRequestInfo(models.MakeRepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
if err != nil {
ctx.ServerError("GetPullRequestInfo", err)
return nil, nil, nil, nil, "", ""
Expand Down Expand Up @@ -762,7 +762,7 @@ func PrepareCompareDiff(
return true
}

diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
diff, err := models.GetDiffRange(models.MakeRepoPath(headUser.Name, headRepo.Name),
prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil {
Expand Down