Skip to content

Refactor some file edit related code #34744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/optional/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ package optional

import "strconv"

// Option is a generic type that can hold a value of type T or be empty (None).
//
// It must use the slice type to work with "chi" form values binding:
// * non-existing value are represented as an empty slice (None)
// * existing value is represented as a slice with one element (Some)
// * multiple values are represented as a slice with multiple elements (Some), the Value is the first element (not well-defined in this case)
type Option[T any] []T

func None[T any]() Option[T] {
Expand Down
15 changes: 11 additions & 4 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,20 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
return
}

operation := "update"
var operation string
if isNewFile {
operation = "create"
} else if !form.Content.Has() && ctx.Repo.TreePath != form.TreePath {
// The form content only has data if file is representable as text, is not too large and not in lfs. If it doesn't
// have data, the only possible operation is a rename
} else if form.Content.Has() {
// The form content only has data if the file is representable as text, is not too large and not in lfs.
operation = "update"
} else if ctx.Repo.TreePath != form.TreePath {
// If it doesn't have data, the only possible operation is a "rename"
operation = "rename"
} else {
// It should never happen, just in case
ctx.Flash.Error(ctx.Tr("error.occurred"))
ctx.HTML(http.StatusOK, tplEditFile)
return
}

if _, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
Expand Down
2 changes: 1 addition & 1 deletion services/packages/cargo/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func alterRepositoryContent(ctx context.Context, doer *user_model.User, repo *re
}

func writeObjectToIndex(ctx context.Context, t *files_service.TemporaryUploadRepository, path string, r io.Reader) error {
hash, err := t.HashObject(ctx, r)
hash, err := t.HashObjectAndWrite(ctx, r)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func GetDiffPreview(ctx context.Context, repo *repo_model.Repository, branch, tr
}

// Add the object to the database
objectHash, err := t.HashObject(ctx, strings.NewReader(content))
objectHash, err := t.HashObjectAndWrite(ctx, strings.NewReader(content))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/repository/files/temp_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ func (t *TemporaryUploadRepository) RemoveFilesFromIndex(ctx context.Context, fi
return nil
}

// HashObject writes the provided content to the object db and returns its hash
func (t *TemporaryUploadRepository) HashObject(ctx context.Context, content io.Reader) (string, error) {
// HashObjectAndWrite writes the provided content to the object db and returns its hash
func (t *TemporaryUploadRepository) HashObjectAndWrite(ctx context.Context, content io.Reader) (string, error) {
stdOut := new(bytes.Buffer)
stdErr := new(bytes.Buffer)

Expand Down
Loading