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

Add config for default branch name #171

Merged
merged 4 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuration/cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
16 changes: 12 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"

Expand All @@ -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"`

Expand Down Expand Up @@ -57,6 +59,7 @@ func configWithDefaults() (*config, error) {
c.LogLevel = "warn"
c.OpengistHome = ""
c.DBFilename = "opengist.db"
c.GitDefaultBranch = "main"

c.SqliteJournalMode = "WAL"

Expand Down Expand Up @@ -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)
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea but it kind of restrict allowed branch names with slashs or dots.
Of course almost everyone would use master or main so it's not a big deal at all, but it seems weird to me to restrict valid branches names from Git if there is a restriction in the first place.

Maybe we could remove this restriction or use a more (much) complicated regex ?

For reference : https://git-scm.com/docs/git-check-ref-format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer having input validation, but I suppose it would throw errors if an invalid branch name is specified, so removing this is probably fine.

return nil
}
9 changes: 6 additions & 3 deletions internal/git/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"bytes"
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/config"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/config"
)

var (
Expand Down Expand Up @@ -58,6 +59,8 @@ func InitRepository(user string, gist string) error {
cmd := exec.Command(
"git",
"init",
"--initial-branch",
config.C.GitDefaultBranch,
"--bare",
repositoryPath,
)
Expand Down