func ToCommit(c *git.Commit) *api.PayloadCommit { authorUsername := "" author, err := models.GetUserByEmail(c.Author.Email) if err == nil { authorUsername = author.Name } committerUsername := "" committer, err := models.GetUserByEmail(c.Committer.Email) if err == nil { committerUsername = committer.Name } return &api.PayloadCommit{ ID: c.ID.String(), Message: c.Message(), URL: "Not implemented", Author: &api.PayloadUser{ Name: c.Author.Name, Email: c.Author.Email, UserName: authorUsername, }, Committer: &api.PayloadUser{ Name: c.Committer.Name, Email: c.Committer.Email, UserName: committerUsername, }, Timestamp: c.Author.When, } }
// CommitToPushCommit transforms a git.Commit to PushCommit type. func CommitToPushCommit(commit *git.Commit) *PushCommit { return &PushCommit{ Sha1: commit.ID.String(), Message: commit.Message(), AuthorEmail: commit.Author.Email, AuthorName: commit.Author.Name, CommitterEmail: commit.Committer.Email, CommitterName: commit.Committer.Name, Timestamp: commit.Author.When, } }
func Download(ctx *context.Context) { var ( uri = ctx.Params("*") refName string ext string archivePath string archiveType git.ArchiveType ) switch { case strings.HasSuffix(uri, ".zip"): ext = ".zip" archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") archiveType = git.ZIP case strings.HasSuffix(uri, ".tar.gz"): ext = ".tar.gz" archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") archiveType = git.TARGZ default: log.Trace("Unknown format: %s", uri) ctx.Error(404) return } refName = strings.TrimSuffix(uri, ext) if !com.IsDir(archivePath) { if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err) return } } // Get corresponding commit. var ( commit *git.Commit err error ) gitRepo := ctx.Repo.GitRepo if gitRepo.IsBranchExist(refName) { commit, err = gitRepo.GetBranchCommit(refName) if err != nil { ctx.Handle(500, "GetBranchCommit", err) return } } else if gitRepo.IsTagExist(refName) { commit, err = gitRepo.GetTagCommit(refName) if err != nil { ctx.Handle(500, "GetTagCommit", err) return } } else if len(refName) == 40 { commit, err = gitRepo.GetCommit(refName) if err != nil { ctx.Handle(404, "GetCommit", nil) return } } else { ctx.Handle(404, "Download", nil) return } archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext) if !com.IsFile(archivePath) { if err := commit.CreateArchive(archivePath, archiveType); err != nil { ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) return } } ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+refName+ext) }