Skip to content

Commit

Permalink
feat(backend,ui): add repo project name
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 227f178 commit 02b44f8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
23 changes: 23 additions & 0 deletions server/backend/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
description = "description"
exportOk = "git-daemon-export-ok"
private = "private"
projectName = "project-name"
settings = "settings"
)

Expand Down Expand Up @@ -573,6 +574,23 @@ func (fb *FileBackend) SetPrivate(repo string, priv bool) error {
return nil
}

// ProjectName returns the project name.
//
// It implements backend.Backend.
func (fb *FileBackend) ProjectName(repo string) string {
repo = utils.SanitizeRepo(repo) + ".git"
r := &Repo{path: filepath.Join(fb.reposPath(), repo), root: fb.reposPath()}
return r.ProjectName()
}

// SetProjectName sets the project name of the given repo.
//
// It implements backend.Backend.
func (fb *FileBackend) SetProjectName(repo string, name string) error {
repo = utils.SanitizeRepo(repo) + ".git"
return os.WriteFile(filepath.Join(fb.reposPath(), repo, projectName), []byte(name), 0600)
}

// CreateRepository creates a new repository.
//
// Created repositories are always bare.
Expand Down Expand Up @@ -607,6 +625,11 @@ func (fb *FileBackend) CreateRepository(repo string, private bool) (backend.Repo
return nil, err
}

if err := fb.SetProjectName(repo, name); err != nil {
logger.Debug("failed to set project name", "err", err)
return nil, err
}

r := &Repo{path: rp, root: fb.reposPath()}
// Add to cache.
fb.repos[name] = r
Expand Down
12 changes: 10 additions & 2 deletions server/backend/file/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ func (r *Repo) Name() string {
return strings.TrimPrefix(name, "/")
}

// ProjectName returns the repository's project name.
func (r *Repo) ProjectName() string {
pn, err := readOneLine(filepath.Join(r.path, projectName))
if err != nil {
return ""
}

return strings.TrimSpace(pn)
}

// Description returns the repository's description.
//
// It implements backend.Repository.
func (r *Repo) Description() string {
desc, err := readAll(filepath.Join(r.path, description))
if err != nil {
logger.Debug("failed to read description file", "err", err,
"path", filepath.Join(r.path, description))
return ""
}

Expand Down
6 changes: 6 additions & 0 deletions server/backend/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type RepositoryStore interface {

// RepositoryMetadata is an interface for managing repository metadata.
type RepositoryMetadata interface {
// ProjectName returns the repository's project name.
ProjectName(repo string) string
// SetProjectName sets the repository's project name.
SetProjectName(repo, name string) error
// Description returns the repository's description.
Description(repo string) string
// SetDescription sets the repository's description.
Expand Down Expand Up @@ -57,6 +61,8 @@ type RepositoryAccess interface {
type Repository interface {
// Name returns the repository's name.
Name() string
// ProjectName returns the repository's project name.
ProjectName() string
// Description returns the repository's description.
Description() string
// IsPrivate returns whether the repository is private.
Expand Down
1 change: 1 addition & 0 deletions server/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func rootCommand() *cobra.Command {
hookCommand(),
listCommand(),
privateCommand(),
projectName(),
renameCommand(),
settingCommand(),
tagCommand(),
Expand Down
40 changes: 40 additions & 0 deletions server/cmd/project_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"strings"

"github.com/spf13/cobra"
)

func projectName() *cobra.Command {
cmd := &cobra.Command{
Use: "project-name REPOSITORY [NAME]",
Aliases: []string{"project"},
Short: "Set or get the project name for a repository",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, _ := fromContext(cmd)
rn := strings.TrimSuffix(args[0], ".git")
switch len(args) {
case 1:
if err := checkIfReadable(cmd, args); err != nil {
return err
}

pn := cfg.Backend.ProjectName(rn)
cmd.Println(pn)
default:
if err := checkIfCollab(cmd, args); err != nil {
return err
}
if err := cfg.Backend.SetProjectName(rn, strings.Join(args[1:], " ")); err != nil {
return err
}
}

return nil
},
}

return cmd
}
6 changes: 5 additions & 1 deletion ui/pages/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ func (r *Repo) headerView() string {
return ""
}
truncate := lipgloss.NewStyle().MaxWidth(r.common.Width)
name := r.common.Styles.Repo.HeaderName.Render(r.selectedRepo.Name())
name := r.selectedRepo.ProjectName()
if name == "" {
name = r.selectedRepo.Name()
}
name = r.common.Styles.Repo.HeaderName.Render(name)
desc := r.selectedRepo.Description()
if desc == "" {
desc = name
Expand Down
7 changes: 6 additions & 1 deletion ui/pages/selection/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ func (i Item) ID() string {

// Title returns the item title. Implements list.DefaultItem.
func (i Item) Title() string {
return i.repo.Name()
name := i.repo.ProjectName()
if name == "" {
name = i.repo.Name()
}

return name
}

// Description returns the item description. Implements list.DefaultItem.
Expand Down

0 comments on commit 02b44f8

Please sign in to comment.