Skip to content

Use case-insensitive matching for Git error "Not a valid object name"#36728

Merged
wxiaoguang merged 4 commits intogo-gitea:mainfrom
mdferdousalam:fix/case-insensitive-git-error-message
Feb 24, 2026
Merged

Use case-insensitive matching for Git error "Not a valid object name"#36728
wxiaoguang merged 4 commits intogo-gitea:mainfrom
mdferdousalam:fix/case-insensitive-git-error-message

Conversation

@mdferdousalam
Copy link
Copy Markdown
Contributor

Fixes #36727

Git is lowercasing the fatal: Not a valid object name error message
to follow its CodingGuidelines. This change makes the string matching
case-insensitive so it works with both the current and future Git versions.

Changes:

  • Use strings.ToLower() before checking for the error substring

See: https://lore.kernel.org/git/pull.2052.git.1771836302101.gitgitgadget@gmail.com

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 23, 2026
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Feb 23, 2026
@wxiaoguang
Copy link
Copy Markdown
Contributor

Thank you very much. I will try to extract the logic into a shared function to improve maintainability.

@wxiaoguang wxiaoguang added backport/v1.25 This PR should be backported to Gitea 1.25 type/enhancement An improvement of existing functionality labels Feb 23, 2026
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 24, 2026
@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 24, 2026

I'm not sure the refactor is warrented. Previous code was easy to read with two strings.Contains calls, now the reader has to jump to another file. I think if it's refactored, it should be a errorMessageContainsAny(err, []string).

@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 24, 2026

I investigated for more git errors that are matched case-sensitively in the code base and found these:

  • "fatal: Needed a single revision" in modules/git/repo_commit_gogit.go:61
  • "Not possible to fast-forward, aborting" in services/pull/merge.go:526
  • "Unable to create" in services/mirror/mirror_pull.go:101
  • "Authentication failed" in services/task/migrate.go:144, routers/api/v1/repo/migrate.go:248, routers/web/repo/migrate.go:112
  • "Bad credentials" in routers/api/v1/repo/migrate.go:249, routers/web/repo/migrate.go:113
  • "could not read Username" in services/task/migrate.go:145, routers/web/repo/migrate.go:114

Maybe we should fix them all here, or in another PR.

@wxiaoguang wxiaoguang removed the backport/v1.25 This PR should be backported to Gitea 1.25 label Feb 24, 2026
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 24, 2026
@wxiaoguang
Copy link
Copy Markdown
Contributor

I'm not sure the refactor is warrented. Previous code was easy to read with two strings.Contains calls, now the reader has to jump to another file. I think if it's refactored, it should be a errorMessageContainsAny(err, []string).

No

I investigated for more git errors that are matched case-sensitively in the code base and found these:

They are different cases.


Using magic string is fragile

@wxiaoguang wxiaoguang merged commit 429ba9c into go-gitea:main Feb 24, 2026
26 checks passed
@GiteaBot GiteaBot added this to the 1.26.0 milestone Feb 24, 2026
@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 24, 2026

They are different cases.

Do you agree they need fixing?

Using magic string is fragile

Could be made constants

@lunny
Copy link
Copy Markdown
Member

lunny commented Feb 24, 2026

@silverwind
Copy link
Copy Markdown
Member

lore.kernel.org/git/pull.2052.git.1771836302101.gitgitgadget@gmail.com

But upstream doesn't convert them to lower case lore.kernel.org/git/pull.2052.git.1771836302101.gitgitgadget@gmail.com

Not yet, but I assume git authors will want to eventually unify all their messages to lowercase and we could be pre-prepared for when it happens.

@wxiaoguang
Copy link
Copy Markdown
Contributor

wxiaoguang commented Feb 24, 2026

They are different cases.

Do you agree they need fixing?

No

Using magic string is fragile

Could be made constants

No

Not necessary at the moment, can be done later when there are enough good use cases.

I have designed the git error system and made it flexible enough. For example:

image

To introduce more "constants", need to investigate the related code and make a general design, not in this PR's scope and I don't want to do it at the moment.

@wxiaoguang
Copy link
Copy Markdown
Contributor

wxiaoguang commented Feb 24, 2026

To introduce more "constants", need to investigate the related code and make a general design, not in this PR's scope and I don't want to do it at the moment.

By the way, the design can be like this, just FYI:

gitcmd.StderrEqual(err, ...)
gitcmd.StderrEquaFold(err, ...)
gitcmd.StderrHasPrefix(err, ...)
gitcmd.StderrHasPrefixFold(err, ...)
gitcmd.StderrContainsAny(err, ...)
gitcmd.StderrContainsAnyFold(err, ...)

Or always do "fold" comparing, only keep StderrEqual, StderrHasPrefix and StderrContainsAny.

Another idea is to introduce enough functions for different error types, and sometimes we need to parse the git's stderr.


However, I don't think these ideas are mature enough, haven't spend time on it.

@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 24, 2026

Yeah anything like that is fine, I just don't want gitea to break when git changes casing in more error messages in the future. Matching error message by string is already a bad API, we should make it liberally accept any casing.

@wxiaoguang
Copy link
Copy Markdown
Contributor

Yeah anything like that is fine, I just don't want gitea to break when git changes casing in more error messages in the future. Matching error message by string is already a bad API, we should make it liberally accept any casing.

Yes, that's why I made many changes to the "git" packages. https://github.com/go-gitea/gitea/pulls?q=is%3Apr+git+is%3Aclosed+author%3Awxiaoguang There are also some comments describing the future direction.

Till now, the situation is much better than before, but there are still many legacy problems like this:

func (r *runStdError) Error() string {
	// FIXME: GIT-CMD-STDERR: it is a bad design, the stderr should not be put in the error message
	// But a lot of code only checks `strings.Contains(err.Error(), "git error")`
	....

@silverwind
Copy link
Copy Markdown
Member

I wonder if some of these cases could be changed to determine outcome by exit code instead of message.

@wxiaoguang
Copy link
Copy Markdown
Contributor

Answer is in code

zjjhot added a commit to zjjhot/gitea that referenced this pull request Feb 25, 2026
* giteaofficial/main:
  Fix path resolving (go-gitea#36734)
  [skip ci] Updated translations via Crowdin
  Fix track time list permission check (go-gitea#36662)
  Fix incorrect setting loading order (go-gitea#36735)
  Use case-insensitive matching for Git error "Not a valid object name" (go-gitea#36728)
  feat: Add workflow dependencies visualization (go-gitea#36248)
silverwind added a commit to silverwind/gitea that referenced this pull request Feb 26, 2026
* main: (24 commits)
  Instance-wide (global) info banner and maintenance mode (go-gitea#36571)
  Add created_by filter to SearchIssues (go-gitea#36670)
  Inline and lazy-load EasyMDE CSS, fix border colors (go-gitea#36714)
  Fix release draft access check logic (go-gitea#36720)
  Change image transparency grid to CSS (go-gitea#36711)
  Avoid opening new tab when downloading actions logs (go-gitea#36740)
  Add validation constraints for repository creation fields (go-gitea#36671)
  Fix SVG height calculation in diff viewer (go-gitea#36748)
  Fix path resolving (go-gitea#36734)
  [skip ci] Updated translations via Crowdin
  Fix track time list permission check (go-gitea#36662)
  Fix incorrect setting loading order (go-gitea#36735)
  Use case-insensitive matching for Git error "Not a valid object name" (go-gitea#36728)
  feat: Add workflow dependencies visualization (go-gitea#36248)
  Add keyboard shortcuts for repository file and code search (go-gitea#36416)
  Refactor text utility classes to Tailwind CSS (go-gitea#36703)
  Prevent redirect bypasses via backslash-encoded paths (go-gitea#36660)
  Fix force push time-line commit comments of pull request (go-gitea#36653)
  Fix get release draft permission check (go-gitea#36659)
  Move `X_FRAME_OPTIONS` setting from `cors` to `security` section (go-gitea#30256)
  ...

# Conflicts:
#	web_src/css/base.css
#	web_src/css/index.css
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/go Pull requests that update Go code type/enhancement An improvement of existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use case-insensitive matching for Git error message "Not a valid object name"

5 participants