You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.3 KiB
62 lines
1.3 KiB
8 years ago
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
||
5 years ago
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||
8 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package git
|
||
|
|
||
|
import (
|
||
4 years ago
|
"context"
|
||
8 years ago
|
"fmt"
|
||
4 years ago
|
"io"
|
||
8 years ago
|
"path/filepath"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
8 years ago
|
// ArchiveType archive types
|
||
8 years ago
|
type ArchiveType int
|
||
|
|
||
|
const (
|
||
8 years ago
|
// ZIP zip archive type
|
||
8 years ago
|
ZIP ArchiveType = iota + 1
|
||
8 years ago
|
// TARGZ tar gz archive type
|
||
8 years ago
|
TARGZ
|
||
|
)
|
||
|
|
||
5 years ago
|
// String converts an ArchiveType to string
|
||
|
func (a ArchiveType) String() string {
|
||
|
switch a {
|
||
8 years ago
|
case ZIP:
|
||
5 years ago
|
return "zip"
|
||
8 years ago
|
case TARGZ:
|
||
5 years ago
|
return "tar.gz"
|
||
8 years ago
|
}
|
||
5 years ago
|
return "unknown"
|
||
|
}
|
||
|
|
||
|
// CreateArchive create archive content to the target path
|
||
4 years ago
|
func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, target io.Writer, usePrefix bool, commitID string) error {
|
||
|
if format.String() == "unknown" {
|
||
|
return fmt.Errorf("unknown format: %v", format)
|
||
5 years ago
|
}
|
||
|
|
||
|
args := []string{
|
||
|
"archive",
|
||
|
}
|
||
4 years ago
|
if usePrefix {
|
||
|
args = append(args, "--prefix="+filepath.Base(strings.TrimSuffix(repo.Path, ".git"))+"/")
|
||
5 years ago
|
}
|
||
|
|
||
|
args = append(args,
|
||
4 years ago
|
"--format="+format.String(),
|
||
|
commitID,
|
||
5 years ago
|
)
|
||
8 years ago
|
|
||
4 years ago
|
var stderr strings.Builder
|
||
|
err := NewCommandContext(ctx, args...).RunInDirPipeline(repo.Path, target, &stderr)
|
||
|
if err != nil {
|
||
|
return ConcatenateError(err, stderr.String())
|
||
|
}
|
||
|
return nil
|
||
8 years ago
|
}
|