Skip to content

Commit

Permalink
move git-receive-pack to a hidden command
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Feb 20, 2025
1 parent 55095db commit cd1a7ff
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 62 deletions.
1 change: 1 addition & 0 deletions cmd/embed/workspace/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data
node_modules
.smallweb/repos
.env
.DS_Store
47 changes: 17 additions & 30 deletions cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,19 @@ import (
"github.com/spf13/cobra"
)

func NewCmdGit(baseDir string, reposDir string) *cobra.Command {
func NewCmdGitReceivePack() *cobra.Command {
cmd := &cobra.Command{
Use: "git",
Short: "Git server",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if _, err := exec.LookPath("git"); err != nil {
return err
}

return nil
},
}

cmd.AddCommand(NewCmdGitReceivePack(baseDir, reposDir))
cmd.AddCommand(NewCmdGitUploadPack(baseDir, reposDir))

return cmd
}

func NewCmdGitReceivePack(baseDir string, reposDir string) *cobra.Command {
cmd := &cobra.Command{
Use: "git-receive-pack <git-dir>",
Short: "Git receive-pack",
Args: cobra.ExactArgs(1),
Use: "git-receive-pack <git-dir>",
Short: "Git receive-pack",
Hidden: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
appDir := filepath.Join(baseDir, args[0])
appDir := filepath.Join(k.String("dir"), args[0])
if baseDir := filepath.Dir(appDir); baseDir != k.String("dir") {
return fmt.Errorf("not in an app directory")
}

reposDir := filepath.Join(k.String("dir"), ".smallweb", "repos")
if err := os.MkdirAll(reposDir, 0755); err != nil {
return err
}
Expand Down Expand Up @@ -85,15 +68,19 @@ func NewCmdGitReceivePack(baseDir string, reposDir string) *cobra.Command {
return cmd
}

func NewCmdGitUploadPack(baseDir string, reposDir string) *cobra.Command {

func NewCmdGitUploadPack() *cobra.Command {
cmd := &cobra.Command{
Use: "git-upload-pack <git-dir>",
Short: "Git upload-pack",
Args: cobra.ExactArgs(1),
Use: "git-upload-pack <git-dir>",
Short: "Git upload-pack",
Hidden: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
appDir := filepath.Join(baseDir, args[0])
appDir := filepath.Join(k.String("dir"), args[0])
if baseDir := filepath.Dir(appDir); baseDir != k.String("dir") {
return fmt.Errorf("not in an app directory")
}

reposDir := filepath.Join(k.String("dir"), ".smallweb", "repos")
repoDir := filepath.Join(reposDir, filepath.Base(appDir))
uploadCmd := exec.Command("git-upload-pack", repoDir)

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ func NewCmdRoot() *cobra.Command {
rootCmd.AddCommand(NewCmdLink())
rootCmd.AddCommand(NewCmdConfig())
rootCmd.AddCommand(NewCmdSecrets())
rootCmd.AddCommand(NewCmdGitReceivePack())
rootCmd.AddCommand(NewCmdGitUploadPack())

if env, ok := os.LookupEnv("SMALLWEB_DISABLED_COMMANDS"); ok {
disabledCommands := strings.Split(env, ",")
Expand Down
33 changes: 1 addition & 32 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,38 +288,7 @@ func NewCmdUp() *cobra.Command {
return
}
}
},
func(next ssh.Handler) ssh.Handler {
return func(sess ssh.Session) {
args := sess.Command()
if len(args) == 0 || args[0] != "git-receive-pack" && args[0] != "git-upload-pack" {
next(sess)
return
}

var baseDir string
if sess.User() == "_" {
baseDir = k.String("dir")
} else {
baseDir = filepath.Join(k.String("dir"), sess.User())
}

gitCmd := NewCmdGit(baseDir, filepath.Join(k.String("dir"), ".smallweb", "repos"))
gitCmd.SetArgs(args)

gitCmd.SetOut(sess)
gitCmd.SetErr(sess.Stderr())
gitCmd.SetIn(sess)

if err := gitCmd.Execute(); err != nil {
fmt.Fprintf(sess, "failed to execute git command: %v\n", err)
sess.Exit(1)
}

sess.Exit(0)
}
},
),
}),
)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.node_modules/
data/
.env
.smallweb/repos

0 comments on commit cd1a7ff

Please sign in to comment.