mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
380 lines
13 KiB
380 lines
13 KiB
7 years ago
|
---
|
||
|
date: "2016-12-01T16:00:00+02:00"
|
||
|
title: "Hacking on Gitea"
|
||
|
slug: "hacking-on-gitea"
|
||
1 year ago
|
sidebar_position: 10
|
||
7 years ago
|
toc: false
|
||
|
draft: false
|
||
2 years ago
|
aliases:
|
||
|
- /en-us/hacking-on-gitea
|
||
7 years ago
|
menu:
|
||
|
sidebar:
|
||
2 years ago
|
parent: "development"
|
||
7 years ago
|
name: "Hacking on Gitea"
|
||
1 year ago
|
sidebar_position: 10
|
||
7 years ago
|
identifier: "hacking-on-gitea"
|
||
|
---
|
||
|
|
||
|
# Hacking on Gitea
|
||
|
|
||
2 years ago
|
## Quickstart
|
||
|
|
||
|
To get a quick working development environment you could use Gitpod.
|
||
|
|
||
2 years ago
|
[![Open in Gitpod](/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/go-gitea/gitea)
|
||
2 years ago
|
|
||
4 years ago
|
## Installing go
|
||
7 years ago
|
|
||
6 years ago
|
You should [install go](https://golang.org/doc/install) and set up your go
|
||
4 years ago
|
environment correctly.
|
||
7 years ago
|
|
||
5 years ago
|
Next, [install Node.js with npm](https://nodejs.org/en/download/) which is
|
||
|
required to build the JavaScript and CSS files. The minimum supported Node.js
|
||
1 year ago
|
version is @minNodeVersion@ and the latest LTS version is recommended.
|
||
5 years ago
|
|
||
6 years ago
|
**Note**: When executing make tasks that require external tools, like
|
||
3 years ago
|
`make watch-backend`, Gitea will automatically download and build these as
|
||
6 years ago
|
necessary. To be able to use these you must have the `"$GOPATH"/bin` directory
|
||
|
on the executable path. If you don't add the go bin directory to the
|
||
|
executable path you will have to manage this yourself.
|
||
|
|
||
1 year ago
|
**Note 2**: Go version @minGoVersion@ or higher is required.
|
||
2 years ago
|
Gitea uses `gofmt` to format source code. However, the results of
|
||
3 years ago
|
`gofmt` can differ by the version of `go`. Therefore it is
|
||
5 years ago
|
recommended to install the version of Go that our continuous integration is
|
||
1 year ago
|
running. As of last update, the Go version should be @goVersion@.
|
||
6 years ago
|
|
||
1 year ago
|
To lint the template files, ensure [Python](https://www.python.org/) and
|
||
|
[Poetry](https://python-poetry.org/) are installed.
|
||
|
|
||
4 years ago
|
## Installing Make
|
||
|
|
||
|
Gitea makes heavy use of Make to automate tasks and improve development. This
|
||
|
guide covers how to install Make.
|
||
|
|
||
4 years ago
|
### On Linux
|
||
4 years ago
|
|
||
|
Install with the package manager.
|
||
|
|
||
|
On Ubuntu/Debian:
|
||
|
|
||
|
```bash
|
||
|
sudo apt-get install make
|
||
|
```
|
||
|
|
||
|
On Fedora/RHEL/CentOS:
|
||
|
|
||
|
```bash
|
||
|
sudo yum install make
|
||
|
```
|
||
|
|
||
4 years ago
|
### On Windows
|
||
4 years ago
|
|
||
|
One of these three distributions of Make will run on Windows:
|
||
|
|
||
|
- [Single binary build](http://www.equation.com/servlet/equation.cmd?fa=make). Copy somewhere and add to `PATH`.
|
||
3 years ago
|
- [32-bits version](http://www.equation.com/ftpdir/make/32/make.exe)
|
||
|
- [64-bits version](http://www.equation.com/ftpdir/make/64/make.exe)
|
||
|
- [MinGW-w64](https://www.mingw-w64.org) / [MSYS2](https://www.msys2.org/).
|
||
2 years ago
|
- MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software, it includes MinGW-w64.
|
||
3 years ago
|
- In MingGW-w64, the binary is called `mingw32-make.exe` instead of `make.exe`. Add the `bin` folder to `PATH`.
|
||
|
- In MSYS2, you can use `make` directly. See [MSYS2 Porting](https://www.msys2.org/wiki/Porting/).
|
||
3 years ago
|
- To compile Gitea with CGO_ENABLED (eg: SQLite3), you might need to use [tdm-gcc](https://jmeubank.github.io/tdm-gcc/) instead of MSYS2 gcc, because MSYS2 gcc headers lack some Windows-only CRT functions like `_beginthread`.
|
||
4 years ago
|
- [Chocolatey package](https://chocolatey.org/packages/make). Run `choco install make`
|
||
|
|
||
3 years ago
|
**Note**: If you are attempting to build using make with Windows Command Prompt, you may run into issues. The above prompts (Git bash, or MinGW) are recommended, however if you only have command prompt (or potentially PowerShell) you can set environment variables using the [set](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1) command, e.g. `set TAGS=bindata`.
|
||
3 years ago
|
|
||
6 years ago
|
## Downloading and cloning the Gitea source code
|
||
|
|
||
5 years ago
|
The recommended method of obtaining the source code is by using `git clone`.
|
||
6 years ago
|
|
||
|
```bash
|
||
5 years ago
|
git clone https://github.com/go-gitea/gitea
|
||
7 years ago
|
```
|
||
|
|
||
5 years ago
|
(Since the advent of go modules, it is no longer necessary to build go projects
|
||
|
from within the `$GOPATH`, hence the `go get` approach is no longer recommended.)
|
||
7 years ago
|
|
||
6 years ago
|
## Forking Gitea
|
||
|
|
||
4 years ago
|
Download the main Gitea source code as above. Then, fork the
|
||
5 years ago
|
[Gitea repository](https://github.com/go-gitea/gitea) on GitHub,
|
||
6 years ago
|
and either switch the git remote origin for your fork or add your fork as another remote:
|
||
|
|
||
|
```bash
|
||
|
# Rename original Gitea origin to upstream
|
||
|
git remote rename origin upstream
|
||
|
git remote add origin "git@github.com:$GITHUB_USERNAME/gitea.git"
|
||
|
git fetch --all --prune
|
||
7 years ago
|
```
|
||
6 years ago
|
|
||
|
or:
|
||
|
|
||
|
```bash
|
||
|
# Add new remote for our fork
|
||
|
git remote add "$FORK_NAME" "git@github.com:$GITHUB_USERNAME/gitea.git"
|
||
|
git fetch --all --prune
|
||
7 years ago
|
```
|
||
|
|
||
7 years ago
|
To be able to create pull requests, the forked repository should be added as a remote
|
||
6 years ago
|
to the Gitea sources. Otherwise, changes can't be pushed.
|
||
7 years ago
|
|
||
6 years ago
|
## Building Gitea (Basic)
|
||
|
|
||
|
Take a look at our
|
||
1 year ago
|
[instructions](installation/from-source.md)
|
||
|
for [building from source](installation/from-source.md).
|
||
6 years ago
|
|
||
|
The simplest recommended way to build from source is:
|
||
|
|
||
|
```bash
|
||
5 years ago
|
TAGS="bindata sqlite sqlite_unlock_notify" make build
|
||
7 years ago
|
```
|
||
6 years ago
|
|
||
4 years ago
|
The `build` target will execute both `frontend` and `backend` sub-targets. If the `bindata` tag is present, the frontend files will be compiled into the binary. It is recommended to leave out the tag when doing frontend development so that changes will be reflected.
|
||
|
|
||
4 years ago
|
See `make help` for all available `make` targets. Also see [`.drone.yml`](https://github.com/go-gitea/gitea/blob/main/.drone.yml) to see how our continuous integration works.
|
||
4 years ago
|
|
||
|
## Building continuously
|
||
|
|
||
4 years ago
|
To run and continuously rebuild when source files change:
|
||
4 years ago
|
|
||
4 years ago
|
```bash
|
||
3 years ago
|
# for both frontend and backend
|
||
4 years ago
|
make watch
|
||
3 years ago
|
|
||
|
# or: watch frontend files (html/js/css) only
|
||
|
make watch-frontend
|
||
|
|
||
|
# or: watch backend files (go) only
|
||
|
make watch-backend
|
||
4 years ago
|
```
|
||
4 years ago
|
|
||
|
On macOS, watching all backend source files may hit the default open files limit which can be increased via `ulimit -n 12288` for the current shell or in your shell startup file for all future shells.
|
||
6 years ago
|
|
||
5 years ago
|
### Formatting, code analysis and spell check
|
||
6 years ago
|
|
||
3 years ago
|
Our continuous integration will reject PRs that fail the code linters (including format check, code analysis and spell check).
|
||
6 years ago
|
|
||
3 years ago
|
You should format your code:
|
||
6 years ago
|
|
||
|
```bash
|
||
|
make fmt
|
||
|
```
|
||
|
|
||
3 years ago
|
and lint the source code:
|
||
6 years ago
|
|
||
|
```bash
|
||
3 years ago
|
# lint both frontend and backend code
|
||
|
make lint
|
||
|
# lint only backend code
|
||
|
make lint-backend
|
||
6 years ago
|
```
|
||
|
|
||
3 years ago
|
**Note**: The results of `gofmt` are dependent on the version of `go` present.
|
||
6 years ago
|
You should run the same version of go that is on the continuous integration
|
||
3 years ago
|
server as mentioned above.
|
||
6 years ago
|
|
||
5 years ago
|
### Working on JS and CSS
|
||
6 years ago
|
|
||
1 year ago
|
Frontend development should follow [Guidelines for Frontend Development](contributing/guidelines-frontend.md)
|
||
3 years ago
|
|
||
|
To build with frontend resources, either use the `watch-frontend` target mentioned above or just build once:
|
||
6 years ago
|
|
||
|
```bash
|
||
5 years ago
|
make build && ./gitea
|
||
|
```
|
||
|
|
||
|
Before committing, make sure the linters pass:
|
||
|
|
||
|
```bash
|
||
|
make lint-frontend
|
||
6 years ago
|
```
|
||
|
|
||
3 years ago
|
### Configuring local ElasticSearch instance
|
||
|
|
||
|
Start local ElasticSearch instance using docker:
|
||
|
|
||
|
```sh
|
||
|
mkdir -p $(pwd)/data/elasticsearch
|
||
|
sudo chown -R 1000:1000 $(pwd)/data/elasticsearch
|
||
3 years ago
|
docker run --rm --memory="4g" -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" -v "$(pwd)/data/elasticsearch:/usr/share/elasticsearch/data" docker.elastic.co/elasticsearch/elasticsearch:7.16.3
|
||
3 years ago
|
```
|
||
|
|
||
|
Configure `app.ini`:
|
||
|
|
||
|
```ini
|
||
|
[indexer]
|
||
|
ISSUE_INDEXER_TYPE = elasticsearch
|
||
|
ISSUE_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200
|
||
|
REPO_INDEXER_ENABLED = true
|
||
|
REPO_INDEXER_TYPE = elasticsearch
|
||
|
REPO_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200
|
||
|
```
|
||
|
|
||
4 years ago
|
### Building and adding SVGs
|
||
|
|
||
1 year ago
|
SVG icons are built using the `make svg` target which compiles the icon sources defined in `build/generate-svg.js` into the output directory `public/assets/img/svg`. Custom icons can be added in the `web_src/svg` directory.
|
||
4 years ago
|
|
||
4 years ago
|
### Building the Logo
|
||
6 years ago
|
|
||
2 years ago
|
The PNG and SVG versions of the Gitea logo are built from a single SVG source file `assets/logo.svg` using the `TAGS="gitea" make generate-images` target. To run it, Node.js and npm must be available.
|
||
4 years ago
|
|
||
|
The same process can also be used to generate custom logo PNGs from a SVG source file by updating `assets/logo.svg` and running `make generate-images`. Omitting the `gitea` tag will update only the user-designated logo files.
|
||
5 years ago
|
|
||
6 years ago
|
### Updating the API
|
||
|
|
||
6 years ago
|
When creating new API routes or modifying existing API routes, you **MUST**
|
||
6 years ago
|
update and/or create [Swagger](https://swagger.io/docs/specification/2-0/what-is-swagger/)
|
||
|
documentation for these using [go-swagger](https://goswagger.io/) comments.
|
||
|
The structure of these comments is described in the [specification](https://goswagger.io/use/spec.html#annotation-syntax).
|
||
6 years ago
|
If you want more information about the Swagger structure, you can look at the
|
||
6 years ago
|
[Swagger 2.0 Documentation](https://swagger.io/docs/specification/2-0/basic-structure/)
|
||
6 years ago
|
or compare with a previous PR adding a new API endpoint, e.g. [PR #5483](https://github.com/go-gitea/gitea/pull/5843/files#diff-2e0a7b644cf31e1c8ef7d76b444fe3aaR20)
|
||
6 years ago
|
|
||
|
You should be careful not to break the API for downstream users which depend
|
||
6 years ago
|
on a stable API. In general, this means additions are acceptable, but deletions
|
||
|
or fundamental changes to the API will be rejected.
|
||
6 years ago
|
|
||
|
Once you have created or changed an API endpoint, please regenerate the Swagger
|
||
|
documentation using:
|
||
|
|
||
|
```bash
|
||
|
make generate-swagger
|
||
|
```
|
||
|
|
||
|
You should validate your generated Swagger file and spell-check it with:
|
||
|
|
||
|
```bash
|
||
5 years ago
|
make swagger-validate misspell-check
|
||
6 years ago
|
```
|
||
|
|
||
4 years ago
|
You should commit the changed swagger JSON file. The continuous integration
|
||
6 years ago
|
server will check that this has been done using:
|
||
|
|
||
|
```bash
|
||
|
make swagger-check
|
||
|
```
|
||
|
|
||
|
**Note**: Please note you should use the Swagger 2.0 documentation, not the
|
||
|
OpenAPI 3 documentation.
|
||
|
|
||
|
### Creating new configuration options
|
||
|
|
||
|
When creating new configuration options, it is not enough to add them to the
|
||
|
`modules/setting` files. You should add information to `custom/conf/app.ini`
|
||
|
and to the
|
||
1 year ago
|
[configuration cheat sheet](administration/config-cheat-sheet.md)
|
||
2 years ago
|
found in `docs/content/doc/administer/config-cheat-sheet.en-us.md`
|
||
6 years ago
|
|
||
|
### Changing the logo
|
||
|
|
||
6 years ago
|
When changing the Gitea logo SVG, you will need to run and commit the results
|
||
6 years ago
|
of:
|
||
|
|
||
|
```bash
|
||
|
make generate-images
|
||
|
```
|
||
|
|
||
|
This will create the necessary Gitea favicon and others.
|
||
|
|
||
|
### Database Migrations
|
||
|
|
||
|
If you make breaking changes to any of the database persisted structs in the
|
||
6 years ago
|
`models/` directory, you will need to make a new migration. These can be found
|
||
6 years ago
|
in `models/migrations/`. You can ensure that your migrations work for the main
|
||
|
database types using:
|
||
|
|
||
|
```bash
|
||
3 years ago
|
make test-sqlite-migration # with SQLite switched for the appropriate database
|
||
7 years ago
|
```
|
||
|
|
||
6 years ago
|
## Testing
|
||
|
|
||
|
There are two types of test run by Gitea: Unit tests and Integration Tests.
|
||
|
|
||
3 years ago
|
### Unit Tests
|
||
|
|
||
|
Unit tests are covered by `*_test.go` in `go test` system.
|
||
3 years ago
|
You can set the environment variable `GITEA_UNIT_TESTS_LOG_SQL=1` to display all SQL statements when running the tests in verbose mode (i.e. when `GOTESTFLAGS=-v` is set).
|
||
3 years ago
|
|
||
6 years ago
|
```bash
|
||
|
TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests
|
||
|
```
|
||
|
|
||
3 years ago
|
### Integration Tests
|
||
|
|
||
6 years ago
|
Unit tests will not and cannot completely test Gitea alone. Therefore, we
|
||
|
have written integration tests; however, these are database dependent.
|
||
6 years ago
|
|
||
|
```bash
|
||
5 years ago
|
TAGS="bindata sqlite sqlite_unlock_notify" make build test-sqlite
|
||
6 years ago
|
```
|
||
|
|
||
3 years ago
|
will run the integration tests in an SQLite environment. Integration tests
|
||
4 years ago
|
require `git lfs` to be installed. Other database tests are available but
|
||
5 years ago
|
may need adjustment to the local environment.
|
||
6 years ago
|
|
||
2 years ago
|
Take a look at [`tests/integration/README.md`](https://github.com/go-gitea/gitea/blob/main/tests/integration/README.md)
|
||
6 years ago
|
for more information and how to run a single test.
|
||
|
|
||
3 years ago
|
### Testing for a PR
|
||
|
|
||
6 years ago
|
Our continuous integration will test the code passes its unit tests and that
|
||
6 years ago
|
all supported databases will pass integration test in a Docker environment.
|
||
|
Migration from several recent versions of Gitea will also be tested.
|
||
6 years ago
|
|
||
|
Please submit your PR with additional tests and integration tests as
|
||
|
appropriate.
|
||
|
|
||
|
## Documentation for the website
|
||
|
|
||
|
Documentation for the website is found in `docs/`. If you change this you
|
||
|
can test your changes to ensure that they pass continuous integration using:
|
||
|
|
||
|
```bash
|
||
5 years ago
|
# from the docs directory within Gitea
|
||
6 years ago
|
make trans-copy clean build
|
||
|
```
|
||
|
|
||
|
You will require a copy of [Hugo](https://gohugo.io/) to run this task. Please
|
||
3 years ago
|
note: this may generate a number of untracked Git objects, which will need to
|
||
6 years ago
|
be cleaned up.
|
||
|
|
||
|
## Visual Studio Code
|
||
|
|
||
|
A `launch.json` and `tasks.json` are provided within `contrib/ide/vscode` for
|
||
|
Visual Studio Code. Look at
|
||
4 years ago
|
[`contrib/ide/README.md`](https://github.com/go-gitea/gitea/blob/main/contrib/ide/README.md)
|
||
6 years ago
|
for more information.
|
||
|
|
||
3 years ago
|
## GoLand
|
||
|
|
||
2 years ago
|
Clicking the `Run Application` arrow on the function `func main()` in `/main.go`
|
||
3 years ago
|
can quickly start a debuggable Gitea instance.
|
||
3 years ago
|
|
||
2 years ago
|
The `Output Directory` in `Run/Debug Configuration` MUST be set to the
|
||
|
gitea project directory (which contains `main.go` and `go.mod`),
|
||
|
otherwise, the started instance's working directory is a GoLand's temporary directory
|
||
|
and prevents Gitea from loading dynamic resources (eg: templates) in a development environment.
|
||
3 years ago
|
|
||
|
To run unit tests with SQLite in GoLand, set `-tags sqlite,sqlite_unlock_notify`
|
||
|
in `Go tool arguments` of `Run/Debug Configuration`.
|
||
|
|
||
6 years ago
|
## Submitting PRs
|
||
|
|
||
|
Once you're happy with your changes, push them up and open a pull request. It
|
||
|
is recommended that you allow Gitea Managers and Owners to modify your PR
|
||
4 years ago
|
branches as we will need to update it to main before merging and/or may be
|
||
6 years ago
|
able to help fix issues directly.
|
||
|
|
||
|
Any PR requires two approvals from the Gitea maintainers and needs to pass the
|
||
4 years ago
|
continuous integration. Take a look at our
|
||
4 years ago
|
[`CONTRIBUTING.md`](https://github.com/go-gitea/gitea/blob/main/CONTRIBUTING.md)
|
||
6 years ago
|
document.
|
||
|
|
||
|
If you need more help pop on to [Discord](https://discord.gg/gitea) #Develop
|
||
|
and chat there.
|
||
7 years ago
|
|
||
6 years ago
|
That's it! You are ready to hack on Gitea.
|