added git ssh support and ablity to download repo via zip, tar.gz, and bundle
This commit is contained in:
@@ -3,6 +3,7 @@ package git
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -467,3 +468,43 @@ func parseUnifiedDiff(raw string) []FileDiff {
|
||||
commit()
|
||||
return files
|
||||
}
|
||||
|
||||
// ArchiveStream writes a git archive of ref in the requested format to w.
|
||||
// format must be one of "zip", "tar.gz", or "bundle".
|
||||
// Output is streamed directly to w without buffering.
|
||||
func ArchiveStream(repoPath string, ref string, format string, w io.Writer) error {
|
||||
clean := filepath.Clean(repoPath)
|
||||
if repoRoot != "" {
|
||||
root := repoRoot + string(filepath.Separator)
|
||||
if !strings.HasPrefix(clean+string(filepath.Separator), root) && clean != repoRoot {
|
||||
return ErrPathTraversal
|
||||
}
|
||||
}
|
||||
|
||||
baseEnv := []string{"GIT_TERMINAL_PROMPT=0", "HOME=/tmp"}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
switch format {
|
||||
case "zip", "tar.gz":
|
||||
cmd = exec.Command("git", "archive", "--format="+format, ref)
|
||||
case "bundle":
|
||||
cmd = exec.Command("git", "bundle", "create", "-", "--all")
|
||||
default:
|
||||
return fmt.Errorf("git archive: unsupported format %q", format)
|
||||
}
|
||||
|
||||
cmd.Dir = clean
|
||||
cmd.Env = baseEnv
|
||||
cmd.Stdout = w
|
||||
|
||||
var errBuf strings.Builder
|
||||
cmd.Stderr = &errBuf
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
if errBuf.Len() > 0 {
|
||||
return fmt.Errorf("git archive: %w: %s", err, errBuf.String())
|
||||
}
|
||||
return fmt.Errorf("git archive: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user