Skip to content

Commit

Permalink
Bug fixes (#184)
Browse files Browse the repository at this point in the history
* Fix gist content when going back to editing

* Fix not outputting non-truncated large files for editon/zip download

* Allow dashes in usernames

* Delete keys associated to deleted user

* Fix error message when there is no files in gist

* Show if there is not files in gist preview

* Fix log parsing for the 11th empty commit
  • Loading branch information
thomiceli authored Dec 27, 2023
1 parent 3b19265 commit 780cc42
Show file tree
Hide file tree
Showing 13 changed files with 692 additions and 237 deletions.
6 changes: 3 additions & 3 deletions internal/db/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,21 @@ func (gist *Gist) DeleteRepository() error {
return git.DeleteRepository(gist.User.Username, gist.Uuid)
}

func (gist *Gist) Files(revision string) ([]*git.File, error) {
func (gist *Gist) Files(revision string, truncate bool) ([]*git.File, error) {
var files []*git.File
filesStr, err := git.GetFilesOfRepository(gist.User.Username, gist.Uuid, revision)
if err != nil {
// if the revision or the file do not exist

if exiterr, ok := err.(*exec.ExitError); ok && exiterr.ExitCode() == 128 {
return nil, nil
return nil, &git.RevisionNotFoundError{}
}

return nil, err
}

for _, fileStr := range filesStr {
file, err := gist.File(revision, fileStr, true)
file, err := gist.File(revision, fileStr, truncate)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion internal/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func (user *User) BeforeDelete(tx *gorm.DB) error {
return err
}

err = tx.Where("user_id = ?", user.ID).Delete(&SSHKey{}).Error
if err != nil {
return err
}

// Delete all gists created by this user
return tx.Where("user_id = ?", user.ID).Delete(&Gist{}).Error
}
Expand Down Expand Up @@ -189,7 +194,7 @@ func (user *User) DeleteProviderID(provider string) error {
// -- DTO -- //

type UserDTO struct {
Username string `form:"username" validate:"required,max=24,alphanum,notreserved"`
Username string `form:"username" validate:"required,max=24,alphanumdash,notreserved"`
Password string `form:"password" validate:"required"`
}

Expand Down
6 changes: 6 additions & 0 deletions internal/git/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ var (

const truncateLimit = 2 << 18

type RevisionNotFoundError struct{}

func (m *RevisionNotFoundError) Error() string {
return "revision not found"
}

func RepositoryPath(user string, gist string) string {
return filepath.Join(config.GetHomeDir(), ReposDirectory, strings.ToLower(user), gist)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/git/output_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func parseLog(out io.Reader, maxBytes int) []*Commit {

scanner.Scan()

if len(scanner.Bytes()) == 0 {
commits = append(commits, currentCommit)
break
}

// if there is no shortstat, it means that the commit is empty, we add it and move onto the next one
if scanner.Bytes()[0] != ' ' {
commits = append(commits, currentCommit)
Expand Down
2 changes: 1 addition & 1 deletion internal/i18n/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ gist.raw: Raw
gist.file-truncated: This file has been truncated.
gist.watch-full-file: View the full file.
gist.file-not-valid: This file is not a valid CSV file.
gist.no-content: No content
gist.no-content: No files found

gist.new.new_gist: New gist
gist.new.title: Title
Expand Down
2 changes: 1 addition & 1 deletion internal/i18n/locales/fr-FR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ gist.raw: Brut
gist.file-truncated: Ce fichier a été tronqué.
gist.watch-full-file: Voir le fichier complet.
gist.file-not-valid: Ce fichier n'est pas un fichier CSV valide.
gist.no-content: Pas de contenu
gist.no-content: Aucun fichier

gist.new.new_gist: Nouveau gist
gist.new.title: Titre
Expand Down
21 changes: 10 additions & 11 deletions internal/web/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/git"
"github.com/thomiceli/opengist/internal/render"
"html/template"
"net/url"
Expand Down Expand Up @@ -286,13 +287,11 @@ func gistIndex(ctx echo.Context) error {
revision = "HEAD"
}

files, err := gist.Files(revision)
if err != nil {
return errorRes(500, "Error fetching files", err)
}

if len(files) == 0 {
files, err := gist.Files(revision, true)
if _, ok := err.(*git.RevisionNotFoundError); ok {
return notFound("Revision not found")
} else if err != nil {
return errorRes(500, "Error fetching files", err)
}

renderedFiles, err := render.HighlightFiles(files)
Expand All @@ -310,7 +309,7 @@ func gistIndex(ctx echo.Context) error {

func gistJson(ctx echo.Context) error {
gist := getData(ctx, "gist").(*db.Gist)
files, err := gist.Files("HEAD")
files, err := gist.Files("HEAD", true)
if err != nil {
return errorRes(500, "Error fetching files", err)
}
Expand Down Expand Up @@ -358,7 +357,7 @@ func gistJs(ctx echo.Context) error {
}

gist := getData(ctx, "gist").(*db.Gist)
files, err := gist.Files("HEAD")
files, err := gist.Files("HEAD", true)
if err != nil {
return errorRes(500, "Error fetching files", err)
}
Expand Down Expand Up @@ -481,7 +480,7 @@ func processCreate(ctx echo.Context) error {
if isCreate {
return html(ctx, "create.html")
} else {
files, err := gist.Files("HEAD")
files, err := gist.Files("HEAD", false)
if err != nil {
return errorRes(500, "Error fetching files", err)
}
Expand Down Expand Up @@ -690,7 +689,7 @@ func downloadFile(ctx echo.Context) error {
func edit(ctx echo.Context) error {
gist := getData(ctx, "gist").(*db.Gist)

files, err := gist.Files("HEAD")
files, err := gist.Files("HEAD", false)
if err != nil {
return errorRes(500, "Error fetching files from repository", err)
}
Expand All @@ -705,7 +704,7 @@ func downloadZip(ctx echo.Context) error {
gist := getData(ctx, "gist").(*db.Gist)
revision := ctx.Param("revision")

files, err := gist.Files(revision)
files, err := gist.Files(revision, true)
if err != nil {
return errorRes(500, "Error fetching files from repository", err)
}
Expand Down
Loading

0 comments on commit 780cc42

Please sign in to comment.