diff --git a/pkg/git/git.go b/pkg/git/git.go
index 591fafb069b141c9e7089f6b6982349b396feef4..66338a126e7cd4af6e064978bdecbeca66283bfc 100644
--- a/pkg/git/git.go
+++ b/pkg/git/git.go
@@ -255,15 +255,22 @@ return err
}
if !info.IsDir() {
- c, err := g.FileContent(name)
+ file, err := tree.File(name)
+ if err != nil {
+ return err
+ }
+
+ reader, err := file.Blob.Reader()
if err != nil {
return err
}
- _, err = tw.Write([]byte(c))
+ _, err = io.Copy(tw, reader)
if err != nil {
+ reader.Close()
return err
}
+ reader.Close()
}
}
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index aed991747bb2e3d5054d2a0921321de6388f48d9..8bb40025338ef5193305370c0999e805dbf91af9 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -39,7 +39,7 @@ GetFileContent(name, ref, path string) (string, error)
GetAbout(name string) (string, error)
ListTags(name string) ([]*plumbing.Reference, error)
ListBranches(name string) ([]*plumbing.Reference, error)
- WriteTarGZip(w io.Writer, name, ref, filename string) error
+ WriteTarGZip(w io.Writer, name, ref, prefix string) error
}
configurationRepository interface {
@@ -91,30 +91,24 @@
func (g *GitHandler) Archive(w http.ResponseWriter, r *http.Request) error {
ext.SetGZip(w)
name := r.PathValue("name")
- refs := r.PathValue("refs")
- ref := strings.TrimSuffix(refs, ".tar.gz")
+ file := r.PathValue("file")
+ ref := strings.TrimSuffix(file, ".tar.gz")
// TODO: remove it once we can support more than gzip
- if !strings.HasSuffix(refs, ".tar.gz") {
+ if !strings.HasSuffix(file, ".tar.gz") {
ext.NotFound(w)
return nil
}
- filenameWithExt := fmt.Sprintf("%s-%s.tar.gz", name, ref)
- ext.SetFileName(w, filenameWithExt)
- filename := fmt.Sprintf("%s-%s", name, ref)
-
- // writing to a buffer so we can run all the process before writing error
- var buf bytes.Buffer
- err := g.gitService.WriteTarGZip(&buf, name, ref, filename)
- if err != nil {
- return err
- }
+ filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
+ ext.SetFileName(w, filename)
- // since that has write to w it cannot return a error.
- _, err = io.Copy(w, &buf)
+ prefix := fmt.Sprintf("%s-%s", name, ref)
+ err := g.gitService.WriteTarGZip(w, name, ref, prefix)
if err != nil {
- slog.Error("Error copying buffer", "error", err)
+ // once we start writing to the body we can't report error anymore
+ // so we are only left with printing the error.
+ slog.Error("Error generating tar gzip file", "error", err)
}
return nil
diff --git a/pkg/handler/router.go b/pkg/handler/router.go
index 2293ab662f2e92a23bc3d3c953413ad00489cd58..6ee7ba344943842ee40233b224fda539bcb4392b 100644
--- a/pkg/handler/router.go
+++ b/pkg/handler/router.go
@@ -41,7 +41,7 @@ mux.HandleFunc("/{name}/refs/{$}", gitHandler.Refs)
mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree)
mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob)
mux.HandleFunc("/{name}/log/{ref}/", gitHandler.Log)
- mux.HandleFunc("/{name}/archive/{refs...}", gitHandler.Archive)
+ mux.HandleFunc("/{name}/archive/{file}", gitHandler.Archive)
mux.HandleFunc("/config", configHandler)
mux.HandleFunc("/about", aboutHandler.About)
mux.HandleFunc("/", gitHandler.List)
diff --git a/pkg/service/git.go b/pkg/service/git.go
index cbee90a375721e07a0efea8b2653aa9a711d87ed..654d6ac5152f1fb1f225de87be42047c6fac3c8b 100644
--- a/pkg/service/git.go
+++ b/pkg/service/git.go
@@ -94,7 +94,7 @@ }
return repo.Commits(count)
}
-func (g *GitService) WriteTarGZip(w io.Writer, name, ref string, filename string) error {
+func (g *GitService) WriteTarGZip(w io.Writer, name, ref string, prefix string) error {
r := g.configRepo.GetByName(name)
if r == nil {
return RepositoryNotFoundErr
@@ -113,8 +113,12 @@
gw := gzip.NewWriter(w)
defer gw.Close()
- return repo.WriteTar(gw, filename)
+ err = repo.WriteTar(gw, prefix)
+ if err != nil {
+ return err
+ }
+ return nil
}
func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) {