Files
forge/routers/web/repo/middlewares.go
zeekayandHanzo Dev 9a07b4d089 legal: Hanzo AI, Inc. copyright above the upstream notices
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git),
so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form
used by the handful of files that had ours.

Shape is now:

	// Copyright 2026 Hanzo AI, Inc. All rights reserved.
	// Copyright 2015 The Gogs Authors. All rights reserved.
	// Copyright 2019 The Gitea Authors. All rights reserved.
	// SPDX-License-Identifier: MIT

Ours on top, theirs retained verbatim. Retained because MIT requires it, and
separately because they are true — those authors did write that code.

Not renamed to "The Git Authors": Git is a different project under a different
license, and putting its name here would attribute this code to people who have
nothing to do with it. Adding our line is the fix; overwriting theirs would have
been both a licence violation and a false statement.

gofmt still reports import-order drift in some of these files. It predates this
change (verified against a stash) and is left alone rather than burying a
one-line-per-file legal change under 3000 files of unrelated reordering.

Build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 13:49:52 -07:00

116 lines
4.1 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"strconv"
user_model "github.com/hanzoai/git/models/user"
"github.com/hanzoai/git/modules/log"
"github.com/hanzoai/git/modules/optional"
"github.com/hanzoai/git/modules/setting"
"github.com/hanzoai/git/modules/util"
"github.com/hanzoai/git/services/context"
"github.com/hanzoai/git/services/gitdiff"
user_service "github.com/hanzoai/git/services/user"
)
// SetEditorconfigIfExists set editor config as render variable
func SetEditorconfigIfExists(ctx *context.Context) {
if ctx.Repo.Repository.IsEmpty {
return
}
ec, _, err := ctx.Repo.GetEditorconfig(ctx)
if err != nil {
// it used to check `!git.IsErrNotExist(err)` and create a system notice, but it is quite annoying and useless
// because network errors also happen frequently, so we just ignore it
return
}
ctx.Data["Editorconfig"] = ec
}
func GetDiffViewStyle(ctx *context.Context) string {
return util.Iif(ctx.Data["IsSplitStyle"] == true, gitdiff.DiffStyleSplit, gitdiff.DiffStyleUnified)
}
// SetDiffViewStyle set diff style as render variable
func SetDiffViewStyle(ctx *context.Context) {
style := ctx.FormString("style")
if ctx.IsSigned {
style = util.IfZero(style, ctx.Doer.DiffViewStyle)
style = util.Iif(style == gitdiff.DiffStyleSplit, gitdiff.DiffStyleSplit, gitdiff.DiffStyleUnified)
if style != ctx.Doer.DiffViewStyle {
err := user_service.UpdateUser(ctx, ctx.Doer, &user_service.UpdateOptions{DiffViewStyle: optional.Some(style)})
if err != nil {
log.Error("UpdateUser DiffViewStyle: %v", err)
}
}
}
ctx.Data["IsSplitStyle"] = style == "split"
}
// SetWhitespaceBehavior set whitespace behavior as render variable
func SetWhitespaceBehavior(ctx *context.Context) {
const defaultWhitespaceBehavior = "show-all"
whitespaceBehavior := ctx.FormString("whitespace")
switch whitespaceBehavior {
case "", "ignore-all", "ignore-eol", "ignore-change":
break
default:
whitespaceBehavior = defaultWhitespaceBehavior
}
if ctx.IsSigned {
userWhitespaceBehavior, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, defaultWhitespaceBehavior)
if err == nil {
if whitespaceBehavior == "" {
whitespaceBehavior = userWhitespaceBehavior
} else if whitespaceBehavior != userWhitespaceBehavior {
_ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, whitespaceBehavior)
}
} // else: we can ignore the error safely
}
// these behaviors are for gitdiff.GetWhitespaceFlag
if whitespaceBehavior == "" {
ctx.Data["WhitespaceBehavior"] = defaultWhitespaceBehavior
} else {
ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
}
}
func GetWhitespaceBehavior(ctx *context.Context) string {
behavior, ok := ctx.Data["WhitespaceBehavior"].(string)
if !ok {
setting.PanicInDevOrTesting("WhitespaceBehavior is not set in context data or is not a string")
}
return behavior
}
// SetShowOutdatedComments set the show outdated comments option as context variable
func SetShowOutdatedComments(ctx *context.Context) {
showOutdatedCommentsValue := ctx.FormString("show-outdated")
if showOutdatedCommentsValue != "true" && showOutdatedCommentsValue != "false" {
// invalid or no value for this form string -> use default or stored user setting
showOutdatedCommentsValue = "true"
if ctx.IsSigned {
showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
}
} else if ctx.IsSigned {
// valid value -> update user setting if user is logged in
_ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
}
ctx.Data["ShowOutdatedComments"], _ = strconv.ParseBool(showOutdatedCommentsValue)
}
func GetShowOutdatedComments(ctx *context.Context) bool {
show, ok := ctx.Data["ShowOutdatedComments"].(bool)
if !ok {
setting.PanicInDevOrTesting("ShowOutdatedComments is not set in context data or is not a bool")
}
return show
}