From 2f8435892ee2d1dbbb2cebca2edbe60ce39ce4c5 Mon Sep 17 00:00:00 2001 From: Jacob Hands Date: Wed, 27 Dec 2023 10:30:42 -0600 Subject: [PATCH] Add config for default branch name (#171) Co-authored-by: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> --- README.md | 2 +- config.yml | 4 +++ docs/configuration/cheat-sheet.md | 55 ++++++++++++++++--------------- docs/index.md | 2 +- docs/installation.md | 2 +- internal/config/config.go | 6 ++-- internal/git/commands.go | 21 +++++++----- internal/git/commands_test.go | 21 ++++++++++++ opengist.go | 2 +- 9 files changed, 73 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 63e5201..a001d39 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Opengist is now running on port 6157, you can browse http://localhost:6157 ### From source -Requirements : [Git](https://git-scm.com/downloads) (2.20+), [Go](https://go.dev/doc/install) (1.20+), [Node.js](https://nodejs.org/en/download/) (16+) +Requirements : [Git](https://git-scm.com/downloads) (2.28+), [Go](https://go.dev/doc/install) (1.20+), [Node.js](https://nodejs.org/en/download/) (16+) ```shell git clone https://github.com/thomiceli/opengist diff --git a/config.yml b/config.yml index 5b332a9..f32293a 100644 --- a/config.yml +++ b/config.yml @@ -18,6 +18,10 @@ opengist-home: # Name of the SQLite database file. Default: opengist.db db-filename: opengist.db +# Default branch name used by Opengist when initializing Git repositories. +# If not set, uses the Git default branch name. See https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup#_new_default_branch +git.default-branch: + # Set the journal mode for SQLite. Default: WAL # See https://www.sqlite.org/pragma.html#pragma_journal_mode sqlite.journal-mode: WAL diff --git a/docs/configuration/cheat-sheet.md b/docs/configuration/cheat-sheet.md index 7a9731d..67320a4 100644 --- a/docs/configuration/cheat-sheet.md +++ b/docs/configuration/cheat-sheet.md @@ -1,29 +1,30 @@ # Configuration Cheat Sheet -| YAML Config Key | Environment Variable | Default value | Description | -|-----------------------|--------------------------|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------| -| log-level | OG_LOG_LEVEL | `warn` | Set the log level to one of the following: `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `panic`. | -| log-output | OG_LOG_OUTPUT | `stdout,file` | Set the log output to one or more of the following: `stdout`, `file`. | -| external-url | OG_EXTERNAL_URL | none | Public URL for the Git HTTP/SSH connection. If not set, uses the URL from the request. | -| opengist-home | OG_OPENGIST_HOME | home directory | Path to the directory where Opengist stores its data. | -| db-filename | OG_DB_FILENAME | `opengist.db` | Name of the SQLite database file. | -| sqlite.journal-mode | OG_SQLITE_JOURNAL_MODE | `WAL` | Set the journal mode for SQLite. More info [here](https://www.sqlite.org/pragma.html#pragma_journal_mode) | -| http.host | OG_HTTP_HOST | `0.0.0.0` | The host on which the HTTP server should bind. | -| http.port | OG_HTTP_PORT | `6157` | The port on which the HTTP server should listen. | -| http.git-enabled | OG_HTTP_GIT_ENABLED | `true` | Enable or disable git operations (clone, pull, push) via HTTP. (`true` or `false`) | -| ssh.git-enabled | OG_SSH_GIT_ENABLED | `true` | Enable or disable git operations (clone, pull, push) via SSH. (`true` or `false`) | -| ssh.host | OG_SSH_HOST | `0.0.0.0` | The host on which the SSH server should bind. | -| ssh.port | OG_SSH_PORT | `2222` | The port on which the SSH server should listen. | -| ssh.external-domain | OG_SSH_EXTERNAL_DOMAIN | none | Public domain for the Git SSH connection, if it has to be different from the HTTP one. If not set, uses the URL from the request. | -| ssh.keygen-executable | OG_SSH_KEYGEN_EXECUTABLE | `ssh-keygen` | Path to the SSH key generation executable. | -| github.client-key | OG_GITHUB_CLIENT_KEY | none | The client key for the GitHub OAuth application. | -| github.secret | OG_GITHUB_SECRET | none | The secret for the GitHub OAuth application. | -| gitlab.client-key | OG_GITLAB_CLIENT_KEY | none | The client key for the GitLab OAuth application. | -| gitlab.secret | OG_GITLAB_SECRET | none | The secret for the GitLab OAuth application. | -| gitlab.url | OG_GITLAB_URL | `https://gitlab.com/` | The URL of the GitLab instance. | -| gitea.client-key | OG_GITEA_CLIENT_KEY | none | The client key for the Gitea OAuth application. | -| gitea.secret | OG_GITEA_SECRET | none | The secret for the Gitea OAuth application. | -| gitea.url | OG_GITEA_URL | `https://gitea.com/` | The URL of the Gitea instance. | -| oidc.client-key | OG_OIDC_CLIENT_KEY | none | The client key for the OpenID application. | -| oidc.secret | OG_OIDC_SECRET | none | The secret for the OpenID application. | -| oidc.discovery-url | OG_OIDC_DISCOVERY_URL | none | Discovery endpoint of the OpenID provider. | +| YAML Config Key | Environment Variable | Default value | Description | +|-----------------------|--------------------------|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| log-level | OG_LOG_LEVEL | `warn` | Set the log level to one of the following: `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `panic`. | +| log-output | OG_LOG_OUTPUT | `stdout,file` | Set the log output to one or more of the following: `stdout`, `file`. | +| external-url | OG_EXTERNAL_URL | none | Public URL for the Git HTTP/SSH connection. If not set, uses the URL from the request. | +| opengist-home | OG_OPENGIST_HOME | home directory | Path to the directory where Opengist stores its data. | +| db-filename | OG_DB_FILENAME | `opengist.db` | Name of the SQLite database file. | +| git.default-branch | OG_GIT_DEFAULT_BRANCH | none | Default branch name used by Opengist when initializing Git repositories. If not set, uses the Git default branch name. More info [here](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup#_new_default_branch) | +| sqlite.journal-mode | OG_SQLITE_JOURNAL_MODE | `WAL` | Set the journal mode for SQLite. More info [here](https://www.sqlite.org/pragma.html#pragma_journal_mode) | +| http.host | OG_HTTP_HOST | `0.0.0.0` | The host on which the HTTP server should bind. | +| http.port | OG_HTTP_PORT | `6157` | The port on which the HTTP server should listen. | +| http.git-enabled | OG_HTTP_GIT_ENABLED | `true` | Enable or disable git operations (clone, pull, push) via HTTP. (`true` or `false`) | +| ssh.git-enabled | OG_SSH_GIT_ENABLED | `true` | Enable or disable git operations (clone, pull, push) via SSH. (`true` or `false`) | +| ssh.host | OG_SSH_HOST | `0.0.0.0` | The host on which the SSH server should bind. | +| ssh.port | OG_SSH_PORT | `2222` | The port on which the SSH server should listen. | +| ssh.external-domain | OG_SSH_EXTERNAL_DOMAIN | none | Public domain for the Git SSH connection, if it has to be different from the HTTP one. If not set, uses the URL from the request. | +| ssh.keygen-executable | OG_SSH_KEYGEN_EXECUTABLE | `ssh-keygen` | Path to the SSH key generation executable. | +| github.client-key | OG_GITHUB_CLIENT_KEY | none | The client key for the GitHub OAuth application. | +| github.secret | OG_GITHUB_SECRET | none | The secret for the GitHub OAuth application. | +| gitlab.client-key | OG_GITLAB_CLIENT_KEY | none | The client key for the GitLab OAuth application. | +| gitlab.secret | OG_GITLAB_SECRET | none | The secret for the GitLab OAuth application. | +| gitlab.url | OG_GITLAB_URL | `https://gitlab.com/` | The URL of the GitLab instance. | +| gitea.client-key | OG_GITEA_CLIENT_KEY | none | The client key for the Gitea OAuth application. | +| gitea.secret | OG_GITEA_SECRET | none | The secret for the Gitea OAuth application. | +| gitea.url | OG_GITEA_URL | `https://gitea.com/` | The URL of the Gitea instance. | +| oidc.client-key | OG_OIDC_CLIENT_KEY | none | The client key for the OpenID application. | +| oidc.secret | OG_OIDC_SECRET | none | The secret for the OpenID application. | +| oidc.discovery-url | OG_OIDC_DISCOVERY_URL | none | Discovery endpoint of the OpenID provider. | diff --git a/docs/index.md b/docs/index.md index 27cc627..fd69363 100644 --- a/docs/index.md +++ b/docs/index.md @@ -35,7 +35,7 @@ Written in [Go](https://go.dev), Opengist aims to be fast and easy to deploy. ## System requirements [Git](https://git-scm.com/download) is obviously required to run Opengist, as it's the main feature of the app. -Version **2.20** or later is recommended as the app has not been tested with older Git versions. +Version **2.28** or later is recommended as the app has not been tested with older Git versions and some features would not work. [OpenSSH](https://www.openssh.com/) suite if you wish to use Git over SSH. diff --git a/docs/installation.md b/docs/installation.md index 585fa05..3bb42de 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -59,7 +59,7 @@ chmod +x opengist ## From source Requirements : -* [Git](https://git-scm.com/downloads) (2.20+) +* [Git](https://git-scm.com/downloads) (2.28+) * [Go](https://go.dev/doc/install) (1.20+) * [Node.js](https://nodejs.org/en/download/) (16+) diff --git a/internal/config/config.go b/internal/config/config.go index 20908bd..d575bc9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -29,6 +29,8 @@ type config struct { OpengistHome string `yaml:"opengist-home" env:"OG_OPENGIST_HOME"` DBFilename string `yaml:"db-filename" env:"OG_DB_FILENAME"` + GitDefaultBranch string `yaml:"git.default-branch" env:"OG_GIT_DEFAULT_BRANCH"` + SqliteJournalMode string `yaml:"sqlite.journal-mode" env:"OG_SQLITE_JOURNAL_MODE"` HttpHost string `yaml:"http.host" env:"OG_HTTP_HOST"` @@ -176,8 +178,8 @@ func CheckGitVersion(version string) (bool, error) { return false, fmt.Errorf("invalid minor version number") } - // Check if version is prior to 2.20 - if major < 2 || (major == 2 && minor < 20) { + // Check if version is prior to 2.28 + if major < 2 || (major == 2 && minor < 28) { return false, nil } return true, nil diff --git a/internal/git/commands.go b/internal/git/commands.go index 0a937bd..02f96ab 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -4,9 +4,6 @@ import ( "bytes" "context" "fmt" - "github.com/labstack/echo/v4" - "github.com/rs/zerolog/log" - "github.com/thomiceli/opengist/internal/config" "os" "os/exec" "path" @@ -14,6 +11,10 @@ import ( "strconv" "strings" "time" + + "github.com/labstack/echo/v4" + "github.com/rs/zerolog/log" + "github.com/thomiceli/opengist/internal/config" ) var ( @@ -61,12 +62,14 @@ func TmpRepositoriesPath() string { func InitRepository(user string, gist string) error { repositoryPath := RepositoryPath(user, gist) - cmd := exec.Command( - "git", - "init", - "--bare", - repositoryPath, - ) + var args []string + args = append(args, "init") + if config.C.GitDefaultBranch != "" { + args = append(args, "--initial-branch", config.C.GitDefaultBranch) + } + args = append(args, "--bare", repositoryPath) + + cmd := exec.Command("git", args...) if err := cmd.Run(); err != nil { return err diff --git a/internal/git/commands_test.go b/internal/git/commands_test.go index e55f8c3..23c0885 100644 --- a/internal/git/commands_test.go +++ b/internal/git/commands_test.go @@ -271,6 +271,27 @@ func TestInitViaGitInit(t *testing.T) { require.NoError(t, err) } +func TestGitInitBranchNames(t *testing.T) { + setup(t) + defer teardown(t) + + cmd := exec.Command("git", "symbolic-ref", "HEAD") + cmd.Dir = RepositoryPath("thomas", "gist1") + out, err := cmd.Output() + require.NoError(t, err, "Could not run git command") + require.Equal(t, "refs/heads/master", strings.TrimSpace(string(out)), "Repository should have master branch as default") + + config.C.GitDefaultBranch = "main" + + err = InitRepository("thomas", "gist2") + require.NoError(t, err) + cmd = exec.Command("git", "symbolic-ref", "HEAD") + cmd.Dir = RepositoryPath("thomas", "gist2") + out, err = cmd.Output() + require.NoError(t, err, "Could not run git command") + require.Equal(t, "refs/heads/main", strings.TrimSpace(string(out)), "Repository should have main branch as default") +} + func commitToBare(t *testing.T, user string, gist string, files map[string]string) { err := CloneTmp(user, gist, gist, "thomas@mail.com", true) require.NoError(t, err, "Could not commit to repository") diff --git a/opengist.go b/opengist.go index eb2a5a1..3b665b6 100644 --- a/opengist.go +++ b/opengist.go @@ -37,7 +37,7 @@ func initialize() { if ok, err := config.CheckGitVersion(gitVersion); err != nil { log.Fatal().Err(err).Send() } else if !ok { - log.Warn().Msg("Git version may be too old, as Opengist has not been tested prior git version 2.20. " + + log.Warn().Msg("Git version may be too old, as Opengist has not been tested prior git version 2.28 and some features would not work. " + "Current git version: " + gitVersion) }