From 09fb647f03bea51d933a243f7486cc1e1b0be79f Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Tue, 21 Nov 2023 02:03:59 +0900 Subject: [PATCH] Fix: bare first branch name, truncated output hanging (#157) --- internal/git/commands.go | 26 +++++++++++++++++--------- internal/i18n/locales/en-US.yml | 2 +- internal/i18n/locales/fr-FR.yml | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/git/commands.go b/internal/git/commands.go index 1b45781..707c81e 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -2,6 +2,7 @@ package git import ( "bytes" + "context" "fmt" "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" @@ -12,6 +13,7 @@ import ( "path/filepath" "strconv" "strings" + "time" ) var ( @@ -121,7 +123,12 @@ func GetFileContent(user string, gist string, revision string, filename string, maxBytes = truncateLimit } - cmd := exec.Command( + // Set up a context with a timeout + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + + cmd := exec.CommandContext( + ctx, "git", "--no-pager", "show", @@ -129,22 +136,17 @@ func GetFileContent(user string, gist string, revision string, filename string, ) cmd.Dir = repositoryPath - stdout, _ := cmd.StdoutPipe() - err := cmd.Start() + output, err := cmd.Output() if err != nil { return "", false, err } - output, truncated, err := truncateCommandOutput(stdout, maxBytes) + content, truncated, err := truncateCommandOutput(bytes.NewReader(output), maxBytes) if err != nil { return "", false, err } - if err := cmd.Wait(); err != nil { - return "", false, err - } - - return output, truncated, nil + return content, truncated, nil } func GetLog(user string, gist string, skip int) ([]*Commit, error) { @@ -459,6 +461,12 @@ fi const postReceive = `#!/bin/sh +while read oldrev newrev refname; do + if ! git rev-parse --verify --quiet HEAD &>/dev/null; then + git symbolic-ref HEAD "$refname" + fi +done + echo "" echo "Your new repository has been created here: %s" echo "" diff --git a/internal/i18n/locales/en-US.yml b/internal/i18n/locales/en-US.yml index 5df332a..6551238 100644 --- a/internal/i18n/locales/en-US.yml +++ b/internal/i18n/locales/en-US.yml @@ -80,7 +80,7 @@ gist.revision.go-to-revision: Go to revision gist.revision.file-created: file created gist.revision.file-deleted: file deleted gist.revision.file-renamed: renamed to -gist.revision.diff-truncated: Diff truncated because it's too large to be shown +gist.revision.diff-truncated: Diff is too large to be shown gist.revision.file-renamed-no-changes: File renamed without changes gist.revision.empty-file: Empty file gist.revision.no-changes: No changes diff --git a/internal/i18n/locales/fr-FR.yml b/internal/i18n/locales/fr-FR.yml index ced1ca9..59bc4a1 100644 --- a/internal/i18n/locales/fr-FR.yml +++ b/internal/i18n/locales/fr-FR.yml @@ -80,7 +80,7 @@ gist.revision.go-to-revision: Aller à la révision gist.revision.file-created: fichier créé gist.revision.file-deleted: fichier supprimé gist.revision.file-renamed: renommé en -gist.revision.diff-truncated: Révision tronquée car trop volumineuse pour être affichée +gist.revision.diff-truncated: Révision trop volumineuse pour être affichée gist.revision.file-renamed-no-changes: Fichier renommé sans modifications gist.revision.empty-file: Fichier vide gist.revision.no-changes: Aucun changement