Skip to content
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

Refactor git module, make Gitea use internal git config #19732

Merged
merged 32 commits into from
Jun 10, 2022

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented May 17, 2022

Related issues

This PR implements #19709

Close #19709

Before, Gitea uses current user's $HOME/.gitconfig. The config may conflict, some config options may affect security, and it's difficult for Gitea to maintain its own config options.

Details about this PR

  1. Make Gitea use its internal customized git config, by setting a separate HOME for git command. The system git config (aka /etc/gitconfig) is still read and used at the moment.
  2. Refactor the git config related functions, add test code for them.
  3. Remove the unnecessary calls to git.LoadGitVersion, in fact the CheckGitVersionAtLeast already handles versions correctly.
  4. The old code cmd/serv.go didn't call git.Init, did't respect the git env. This PR makes the git module always be initialized and used.
  5. TODO: A lot of duplicate code for PrepareTestEnv-like functions, they should be refactored later .....

About gogit

gogit doesn't support customized gitconfig, luckily, at the moment, gogit doesn't rely the gitconfig too much

So it's fine to have this PR, no affecting on gogit side in my mind. And in the future, gogit should be maintained separately, instead of mixing with native git.

Backport or not

The git module code have changed a lot since 1.16, the backport would be a little quite dirty (update: I have a feeling it's too complex, it would change a lot of modules, not worth to be backported).

⚠️ BREAKING ⚠️

Before: Gitea's git uses system gitconfig (/etc/gitconfig) and current user's global gitconfig ($HOME/.gitconfig)

After: Gitea's git uses system gitconfig (/etc/gitconfig) and internal customized global gitconfig. And, when running git, the HOME is set to its internal HOME.

If you have your own customized config for git in Gitea, you should set these configs in /etc/gitconfig or the .gitconfig in its HOME. All related home files for git command (like .gnupg) should also be put in Gitea's git home directory.

NOTICE, after #20114 , the [git].HOME_PATH will be used for Git HOME directory.

@wxiaoguang wxiaoguang linked an issue May 17, 2022 that may be closed by this pull request
@wxiaoguang wxiaoguang force-pushed the refactor-git-fix-safedirectory branch from 810b24a to ea727f2 Compare May 17, 2022 07:20
@wxiaoguang wxiaoguang added this to the 1.17.0 milestone May 17, 2022
@wxiaoguang wxiaoguang force-pushed the refactor-git-fix-safedirectory branch 3 times, most recently from 8894540 to 0e0bcda Compare May 17, 2022 07:56
@wxiaoguang wxiaoguang force-pushed the refactor-git-fix-safedirectory branch from 0e0bcda to dac2594 Compare May 17, 2022 08:15
@wxiaoguang

This comment was marked as outdated.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 17, 2022
modules/git/command.go Outdated Show resolved Hide resolved
@lunny

This comment was marked as outdated.

@wxiaoguang

This comment was marked as outdated.

@wxiaoguang wxiaoguang force-pushed the refactor-git-fix-safedirectory branch from 44dd482 to 22d62bc Compare May 17, 2022 09:59
@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented May 17, 2022

OK, almost done (ready for review). CI passes. The PR description has been updated with more details.

cmd/serv.go Outdated Show resolved Hide resolved
@codecov-commenter

This comment was marked as off-topic.

modules/git/command.go Outdated Show resolved Hide resolved
@lunny
Copy link
Member

lunny commented May 17, 2022

There is a break if he has changed

[user]
        name = Gitea
        email = [email protected]

modules/git/git.go Outdated Show resolved Hide resolved
@zeripath
Copy link
Contributor

zeripath commented Jun 10, 2022

Yup.

It's not enough to lock around Init* you need lock every time you read or write to a share state. That means everything that Init* touches needs to have a lock on every read of it too.

@wxiaoguang
Copy link
Contributor Author

I am working on it now. 2 choices:

  1. use Mutex to protect everything
  2. use atomic.Value to load/store a struct (I am trying this, since I think it's the direction for the refactoring of the whole setting module)

@zeripath
Copy link
Contributor

Please be aware that massive refactors to the settings need to wait until after 1.17 is released.

@zeripath
Copy link
Contributor

And if we complete context passing then we don't actually need concurrency - because the settings could be obtained from the context hierarchy.

For example, the use of setting.AppURL etc. should just be obtainable from the context hierarchy

@wxiaoguang
Copy link
Contributor Author

Please be aware that massive refactors to the settings need to wait until after 1.17 is released.

Sure, it will be a small change for git module only, won't affect others.

And if we complete context passing then we don't actually need concurrency - because the settings could be obtained from the context hierarchy.
For example, the use of setting.AppURL etc. should just be obtainable from the context hierarchy

I agree to use/pass the context as much as possible. However I do not think setting.AppURL / HTMLURL / ROOT_URL is the perfect use-case for the context. Using a relative URL to replace the HTMLURL will resolve the ROOT_URL problem clearly, then no need to feed the ctx to every HTMLURL call. ps: HTMLURL is not a good name in my mind 😂

@zeripath
Copy link
Contributor

And if we complete context passing then we don't actually need concurrency - because the settings could be obtained from the context hierarchy.
For example, the use of setting.AppURL etc. should just be obtainable from the context hierarchy

I agree to use/pass the context as much as possible. However I do not think setting.AppURL / HTMLURL / ROOT_URL is the perfect use-case for the context. Using a relative URL to replace the HTMLURL will resolve the ROOT_URL problem clearly, then no need to feed the ctx to every HTMLURL call. ps: HTMLURL is not a good name in my mind joy

This is going off-topic but I'll put a few points here:

  • IMO models.* shouldn't be doing anything with URLs - that belongs at presentation layer.
    • Similarly URL building really shouldn't be happening in the templates.
  • We cannot always emit relative URLs, most of the time we can but there are plenty of places where we need to actually emit absolute URLs.
    • At the very least we will always need some way providing a Full URL either to the API, to the Web or even to the SSH.
  • Unless we abandon support for submounting gitea without URL rewriting we're always going to need to be aware of Subdirs
    • I absolutely do not think we should abandon support for submounting gitea.
  • If we make the AppURL etc. gettable from the context or at least dependent on the request we can make hydra heading possible.

Why do we need to propagate the context?

  • We have to feed the context into every call for rendering to get the Locale.
  • We have to feed the context into every call for transparent db transactions
  • We have to feed the context into every call for cancellation and process support
  • We have to feed the context into every call for contextual logging

The first argument to almost every function really needs to become ctx context.Context (with the second line of those functions probably almost always logger := log.GetLogger(ctx))

I know that grates and feels like it's going back to Java like behaviour - and I expect that Go2 or Go3 is going to have to come up with some way to make this less omnipresent but the benefit of just accepting this will be immense.

@wxiaoguang
Copy link
Contributor Author

Agree with your points of views. The question in my mind is: what will the {{.HTMLURL}} become then -- to resolve the absolute URL bug (it's only a question, maybe it's too early to be answered now, it might be answered when more code is ready).

And I proposed a patch #19934 to resolve the data-race problem, it seems that it works, the first round test passes, now it's on the second round.

zjjhot added a commit to zjjhot/gitea that referenced this pull request Jun 11, 2022
* giteaoffical/main:
  Fix data-race problems in git module (quick patch) (go-gitea#19934)
  [skip ci] Updated translations via Crowdin
  Fix copy/paste of empty lines (go-gitea#19798)
  Normalize line endings in fomantic build files (go-gitea#19932)
  Make user profile image show full image on mobile (go-gitea#19840)
  Custom regexp external issues (go-gitea#17624)
  Use Golang 1.18 for Gitea 1.17 release (go-gitea#19918)
  Refactor git module, make Gitea use internal git config (go-gitea#19732)
  [skip ci] Updated translations via Crowdin
@wxiaoguang wxiaoguang added the type/changelog Adds the changelog for a new Gitea version label Jun 14, 2022
justusbunsi added a commit to justusbunsi/gitea that referenced this pull request Jun 25, 2022
With go-gitea#19732, the default location for the `.gnupg` folder has changed. To mitigate this breaking change, users can specify the home directory for gnupg via `$GNUPGHOME` environment variable to keep using their current location.
wxiaoguang added a commit to wxiaoguang/gitea that referenced this pull request Jul 8, 2022
Before, in go-gitea#19732, the old home directory is not correct.
This PR introduces a new config option for git home: git.HOME_PATH,
which is default to %(APP_DATA_PATH)/home

And pass env GNUPGHOME to git command, force Gitea to use a stable GNUPGHOME directory
lunny pushed a commit that referenced this pull request Jul 8, 2022
Before, in #19732, the old home directory is not correct.
This PR introduces a new config option for git home: git.HOME_PATH,
which is default to %(APP_DATA_PATH)/home

And pass env GNUPGHOME to git command, force Gitea to use a stable GNUPGHOME directory
lunny added a commit that referenced this pull request Jul 10, 2022
* Add hint for GNUPGHOME environment variable

With #19732, the default location for the `.gnupg` folder has changed. To mitigate this breaking change, users can specify the home directory for gnupg via `$GNUPGHOME` environment variable to keep using their current location.

* Update docs/content/doc/advanced/signing.en-us.md

Co-authored-by: wxiaoguang <[email protected]>

Co-authored-by: wxiaoguang <[email protected]>
Co-authored-by: Lunny Xiao <[email protected]>
Co-authored-by: John Olheiser <[email protected]>
Co-authored-by: zeripath <[email protected]>
vsysoev pushed a commit to IntegraSDL/gitea that referenced this pull request Aug 10, 2022
* Add hint for GNUPGHOME environment variable

With go-gitea#19732, the default location for the `.gnupg` folder has changed. To mitigate this breaking change, users can specify the home directory for gnupg via `$GNUPGHOME` environment variable to keep using their current location.

* Update docs/content/doc/advanced/signing.en-us.md

Co-authored-by: wxiaoguang <[email protected]>

Co-authored-by: wxiaoguang <[email protected]>
Co-authored-by: Lunny Xiao <[email protected]>
Co-authored-by: John Olheiser <[email protected]>
Co-authored-by: zeripath <[email protected]>
AbdulrhmnGhanem pushed a commit to kitspace/gitea that referenced this pull request Aug 24, 2022
* Refactor git module, make Gitea use internal git config, add safe.directory config

* introduce git.InitSimple and git.InitWithConfigSync, make serv cmd use gitconfig

* use HOME instead of GIT_CONFIG_GLOBAL, because git always needs a correct HOME

* fix cmd env in cmd/serv.go

* fine tune error message

* Fix a incorrect test case

* fix configAddNonExist

* fix configAddNonExist logic, add `--fixed-value` flag, add tests

* add configSetNonExist function in case it's needed.

* use configSetNonExist for `user.name` and `user.email`

* add some comments

* Update cmd/serv.go

Co-authored-by: zeripath <[email protected]>

* Update cmd/serv.go

Co-authored-by: zeripath <[email protected]>

* Update modules/git/git.go

Co-authored-by: zeripath <[email protected]>

* Update modules/setting/setting.go

Co-authored-by: zeripath <[email protected]>

* Update modules/git/repo_attribute.go

Co-authored-by: zeripath <[email protected]>

* fix spaces in messages

* use `configSet("core.protectNTFS", ...)` instead of `globalCommandArgs`

* remove GIT_CONFIG_NOSYSTEM, continue to use system's git config

* Update cmd/serv.go

Co-authored-by: zeripath <[email protected]>

* fix merge

* remove code for safe.directory

* separate git.CommonEnvs to CommonGitCmdEnvs and CommonCmdServEnvs

* avoid Golang's data race error

Co-authored-by: zeripath <[email protected]>
Co-authored-by: Lunny Xiao <[email protected]>
s-hamann added a commit to s-hamann/ansible-gitea that referenced this pull request Nov 8, 2022
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
@delvh delvh removed the type/changelog Adds the changelog for a new Gitea version label Oct 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. pr/breaking Merging this PR means builds will break. Needs a description what exactly breaks, and how to fix it! type/enhancement An improvement of existing functionality type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: make Gitea use its own customized global config
7 participants