From 5e6afc7e6bf97e217b391eb80b6b333830824a24 Mon Sep 17 00:00:00 2001 From: Jacob Hands Date: Fri, 15 Dec 2023 07:37:22 -0600 Subject: [PATCH 1/3] Use branch 'main' when initializing gist repository --- internal/git/commands.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/git/commands.go b/internal/git/commands.go index 707c81ec..7f111880 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 ( @@ -58,6 +59,8 @@ func InitRepository(user string, gist string) error { cmd := exec.Command( "git", "init", + "--initial-branch", + "main", "--bare", repositoryPath, ) From 5c187403a4c77e496ebc36c0bac0da24e3782767 Mon Sep 17 00:00:00 2001 From: Jacob Hands Date: Fri, 15 Dec 2023 15:53:53 -0600 Subject: [PATCH 2/3] Add config for git default branch --- docs/configuration/cheat-sheet.md | 1 + internal/config/config.go | 16 ++++++++++++---- internal/git/commands.go | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/configuration/cheat-sheet.md b/docs/configuration/cheat-sheet.md index 54005700..c6498da9 100644 --- a/docs/configuration/cheat-sheet.md +++ b/docs/configuration/cheat-sheet.md @@ -6,6 +6,7 @@ | 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 | `main` | Default branch name used when initializing Git repositories. Can contain: a-z, 0-9, -, _ | | 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. | diff --git a/internal/config/config.go b/internal/config/config.go index 1eaefb8b..0b9130a6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "strconv" "strings" @@ -22,10 +23,11 @@ var C *config // Not using nested structs because the library // doesn't support dot notation in this case sadly type config struct { - LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"` - ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"` - OpengistHome string `yaml:"opengist-home" env:"OG_OPENGIST_HOME"` - DBFilename string `yaml:"db-filename" env:"OG_DB_FILENAME"` + LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"` + ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"` + 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"` @@ -57,6 +59,7 @@ func configWithDefaults() (*config, error) { c.LogLevel = "warn" c.OpengistHome = "" c.DBFilename = "opengist.db" + c.GitDefaultBranch = "main" c.SqliteJournalMode = "WAL" @@ -235,5 +238,10 @@ func checks(c *config) error { return err } + branchRe := regexp.MustCompile("^[a-z0-9-_]+$") + if !branchRe.MatchString(c.GitDefaultBranch) { + return fmt.Errorf("invalid git default branch name: %s", c.GitDefaultBranch) + } + return nil } diff --git a/internal/git/commands.go b/internal/git/commands.go index 7f111880..c60aecbc 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -60,7 +60,7 @@ func InitRepository(user string, gist string) error { "git", "init", "--initial-branch", - "main", + config.C.GitDefaultBranch, "--bare", repositoryPath, ) From cd2c36478cafe818e9c3c52ec2d042fbe5c7fe58 Mon Sep 17 00:00:00 2001 From: Thomas Miceli Date: Wed, 27 Dec 2023 15:55:43 +0100 Subject: [PATCH 3/3] Remove Regex, use git default name, update docs --- README.md | 2 +- config.yml | 4 +++ docs/configuration/cheat-sheet.md | 48 +++++++++++++++---------------- docs/index.md | 2 +- docs/installation.md | 2 +- internal/config/config.go | 22 ++++++-------- internal/git/commands.go | 16 +++++------ internal/git/commands_test.go | 21 ++++++++++++++ opengist.go | 2 +- 9 files changed, 69 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 63e52011..a001d391 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 f48d79ed..5d3b85a8 100644 --- a/config.yml +++ b/config.yml @@ -15,6 +15,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 c6498da9..88bd4788 100644 --- a/docs/configuration/cheat-sheet.md +++ b/docs/configuration/cheat-sheet.md @@ -1,26 +1,26 @@ # 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`. | -| 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 | `main` | Default branch name used when initializing Git repositories. Can contain: a-z, 0-9, -, _ | -| 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. | -| 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`. | +| 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. | +| 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 27cc6271..fd69363a 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 585fa054..3bb42de3 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 0b9130a6..79a5625b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,7 +6,6 @@ import ( "os" "path/filepath" "reflect" - "regexp" "strconv" "strings" @@ -23,11 +22,12 @@ var C *config // Not using nested structs because the library // doesn't support dot notation in this case sadly type config struct { - LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"` - ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"` - 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"` + LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"` + ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"` + 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"` @@ -59,7 +59,6 @@ func configWithDefaults() (*config, error) { c.LogLevel = "warn" c.OpengistHome = "" c.DBFilename = "opengist.db" - c.GitDefaultBranch = "main" c.SqliteJournalMode = "WAL" @@ -147,8 +146,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 @@ -238,10 +237,5 @@ func checks(c *config) error { return err } - branchRe := regexp.MustCompile("^[a-z0-9-_]+$") - if !branchRe.MatchString(c.GitDefaultBranch) { - return fmt.Errorf("invalid git default branch name: %s", c.GitDefaultBranch) - } - return nil } diff --git a/internal/git/commands.go b/internal/git/commands.go index c60aecbc..ae0a46aa 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -56,14 +56,14 @@ func TmpRepositoriesPath() string { func InitRepository(user string, gist string) error { repositoryPath := RepositoryPath(user, gist) - cmd := exec.Command( - "git", - "init", - "--initial-branch", - config.C.GitDefaultBranch, - "--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 c879430d..93528878 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") require.NoError(t, err, "Could not commit to repository") diff --git a/opengist.go b/opengist.go index eb2a5a1a..3b665b69 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) }