code.gitea.io/gitea@v1.22.3/modules/git/repo_archive.go (about) 1 // Copyright 2015 The Gogs Authors. All rights reserved. 2 // Copyright 2020 The Gitea Authors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 5 package git 6 7 import ( 8 "context" 9 "fmt" 10 "io" 11 "os" 12 "path/filepath" 13 "strings" 14 ) 15 16 // ArchiveType archive types 17 type ArchiveType int 18 19 const ( 20 // ZIP zip archive type 21 ZIP ArchiveType = iota + 1 22 // TARGZ tar gz archive type 23 TARGZ 24 // BUNDLE bundle archive type 25 BUNDLE 26 ) 27 28 // String converts an ArchiveType to string 29 func (a ArchiveType) String() string { 30 switch a { 31 case ZIP: 32 return "zip" 33 case TARGZ: 34 return "tar.gz" 35 case BUNDLE: 36 return "bundle" 37 } 38 return "unknown" 39 } 40 41 func ToArchiveType(s string) ArchiveType { 42 switch s { 43 case "zip": 44 return ZIP 45 case "tar.gz": 46 return TARGZ 47 case "bundle": 48 return BUNDLE 49 } 50 return 0 51 } 52 53 // CreateArchive create archive content to the target path 54 func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, target io.Writer, usePrefix bool, commitID string) error { 55 if format.String() == "unknown" { 56 return fmt.Errorf("unknown format: %v", format) 57 } 58 59 cmd := NewCommand(ctx, "archive") 60 if usePrefix { 61 cmd.AddOptionFormat("--prefix=%s", filepath.Base(strings.TrimSuffix(repo.Path, ".git"))+"/") 62 } 63 cmd.AddOptionFormat("--format=%s", format.String()) 64 cmd.AddDynamicArguments(commitID) 65 66 // Avoid LFS hooks getting installed because of /etc/gitconfig, which can break pull requests. 67 env := append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1") 68 69 var stderr strings.Builder 70 err := cmd.Run(&RunOpts{ 71 Dir: repo.Path, 72 Stdout: target, 73 Stderr: &stderr, 74 Env: env, 75 }) 76 if err != nil { 77 return ConcatenateError(err, stderr.String()) 78 } 79 return nil 80 }