github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_archive.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 // Copyright 2015 The Gogs Authors. All rights reserved. 7 8 package git 9 10 import ( 11 "context" 12 "fmt" 13 "io" 14 "path/filepath" 15 "strings" 16 ) 17 18 // ArchiveType archive types 19 type ArchiveType int 20 21 const ( 22 // ZIP zip archive type 23 ZIP ArchiveType = iota + 1 24 // TARGZ tar gz archive type 25 TARGZ 26 // BUNDLE bundle archive type 27 BUNDLE 28 ) 29 30 // String converts an ArchiveType to string 31 func (a ArchiveType) String() string { 32 switch a { 33 case ZIP: 34 return "zip" 35 case TARGZ: 36 return "tar.gz" 37 case BUNDLE: 38 return "bundle" 39 } 40 return "unknown" 41 } 42 43 // CreateArchive create archive content to the target path 44 func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, target io.Writer, usePrefix bool, commitID string) error { 45 if format.String() == "unknown" { 46 return fmt.Errorf("unknown format: %v", format) 47 } 48 49 args := []string{ 50 "archive", 51 } 52 if usePrefix { 53 args = append(args, "--prefix="+filepath.Base(strings.TrimSuffix(repo.Path, ".git"))+"/") 54 } 55 56 args = append(args, 57 "--format="+format.String(), 58 commitID, 59 ) 60 61 var stderr strings.Builder 62 err := NewCommand(ctx, args...).Run(&RunOpts{ 63 Dir: repo.Path, 64 Stdout: target, 65 Stderr: &stderr, 66 }) 67 if err != nil { 68 return ConcatenateError(err, stderr.String()) 69 } 70 return nil 71 }