MCPcopy Create free account
hub / github.com/gogs/gogs / RepoRef

Function RepoRef

internal/context/repo.go:299–433  ·  view source on GitHub ↗

RepoRef handles repository reference name including those contain `/`.

()

Source from the content-addressed store, hash-verified

297
298// RepoRef handles repository reference name including those contain `/`.
299func RepoRef() macaron.Handler {
300 return func(c *Context) {
301 // Empty repository does not have reference information.
302 if c.Repo.Repository.IsBare {
303 return
304 }
305
306 var (
307 refName string
308 err error
309 )
310
311 // For API calls.
312 if c.Repo.GitRepo == nil {
313 repoPath := database.RepoPath(c.Repo.Owner.Name, c.Repo.Repository.Name)
314 c.Repo.GitRepo, err = git.Open(repoPath)
315 if err != nil {
316 c.Error(err, "open repository")
317 return
318 }
319 }
320
321 // Get default branch.
322 if c.Params("*") == "" {
323 refName = c.Repo.Repository.DefaultBranch
324 if !c.Repo.GitRepo.HasBranch(refName) {
325 branches, err := c.Repo.GitRepo.Branches()
326 if err != nil {
327 c.Error(err, "get branches")
328 return
329 }
330 refName = branches[0]
331 }
332 c.Repo.Commit, err = c.Repo.GitRepo.BranchCommit(refName)
333 if err != nil {
334 c.Error(err, "get branch commit")
335 return
336 }
337 c.Repo.CommitID = c.Repo.Commit.ID.String()
338 c.Repo.IsViewBranch = true
339
340 } else {
341 hasMatched := false
342 parts := strings.Split(c.Params("*"), "/")
343 for i, part := range parts {
344 refName = strings.TrimPrefix(refName+"/"+part, "/")
345
346 if c.Repo.GitRepo.HasBranch(refName) ||
347 c.Repo.GitRepo.HasTag(refName) {
348 if i < len(parts)-1 {
349 c.Repo.TreePath = strings.Join(parts[i+1:], "/")
350 }
351 hasMatched = true
352 break
353 }
354 }
355 if !hasMatched && len(parts[0]) == 40 {
356 refName = parts[0]

Callers 2

runWebFunction · 0.92
RegisterRoutesFunction · 0.92

Calls 9

RepoPathFunction · 0.92
SplitMethod · 0.80
IsWriterMethod · 0.80
HasForkedByMethod · 0.80
RepositoriesMethod · 0.80
AllowsPullsMethod · 0.80
NotFoundMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected