grim/hgkeeper

Parents f24e3134ae3d
Children 4c9cc0c56dff
Add support for repository specific hgrc files. Fixes HGKEEPER-2
--- a/hg/hg.go Thu Nov 12 00:53:46 2020 -0600
+++ b/hg/hg.go Thu Nov 12 00:55:06 2020 -0600
@@ -19,7 +19,7 @@
writeable bool
}
-func (c *Command) Setup() error {
+func (c *Command) Setup(repoName string) error {
hgrc, err := createHgrc(c.writeable)
if err != nil {
return err
@@ -27,10 +27,14 @@
c.tmpHgrc = hgrc
- rcs := []string{
- filepath.Join(access.AdminRepoPath(), "site.hgrc"),
- c.tmpHgrc,
- }
+ // site.hgrc is deprecated, but we still support it for now
+ rcs := []string{filepath.Join(access.AdminRepoPath(), "site.hgrc")}
+
+ // add the repo specific hgrc's
+ rcs = append(rcs, findHgrcsForRepo(repoName)...)
+
+ // add the hgrc that controls writability
+ rcs = append(rcs, c.tmpHgrc)
c.cmd.Env = append(
os.Environ(),
@@ -49,8 +53,8 @@
}
}
-func (c *Command) Exec() (string, error) {
- c.Setup()
+func (c *Command) Exec(repoName string) (string, error) {
+ c.Setup(repoName)
defer c.Teardown()
raw, err := c.Cmd().CombinedOutput()
@@ -58,8 +62,8 @@
return string(raw), err
}
-func (c *Command) ExecPiped(stdin io.Reader, stdout, stderr io.Writer) error {
- if err := c.Setup(); err != nil {
+func (c *Command) ExecPiped(repoName string, stdin io.Reader, stdout, stderr io.Writer) error {
+ if err := c.Setup(repoName); err != nil {
return err
}
defer c.Teardown()
--- a/hg/hgrc.go Thu Nov 12 00:53:46 2020 -0600
+++ b/hg/hgrc.go Thu Nov 12 00:55:06 2020 -0600
@@ -2,8 +2,11 @@
import (
"io/ioutil"
+ "path/filepath"
"gopkg.in/ini.v1"
+
+ "keep.imfreedom.org/grim/hgkeeper/access"
)
func createHgrc(writeable bool) (string, error) {
@@ -29,3 +32,27 @@
return tmpfile.Name(), nil
}
+
+func findHgrcsForRepo(repoName string) []string {
+ ret := []string{}
+
+ adminPath := access.AdminRepoPath()
+
+ for {
+ ret = append(ret, filepath.Join(adminPath, "config", repoName, "hgrc"))
+ if repoName == "." {
+ break
+ }
+ repoName = filepath.Dir(repoName)
+ }
+
+ // we're assuming that the order of hgrc files is last wins, so we need to
+ // reverse the list so that the repository specific hgrc wins. If the list
+ // has an odd number of items, the middle is skipped as it's already in the
+ // correct location.
+ for i, j := 0, len(ret)-1; i < j; i, j = i+1, j-1 {
+ ret[i], ret[j] = ret[j], ret[i]
+ }
+
+ return ret
+}
--- a/hg/hgweb.go Thu Nov 12 00:53:46 2020 -0600
+++ b/hg/hgweb.go Thu Nov 12 00:55:06 2020 -0600
@@ -10,7 +10,7 @@
)
func TemplatesDir() (string, error) {
- templatesDir, err := Config("web.templates").Exec()
+ templatesDir, err := Config("web.templates").Exec("")
if err != nil || templatesDir == "" {
if err != nil {
--- a/once/command.go Thu Nov 12 00:53:46 2020 -0600
+++ b/once/command.go Thu Nov 12 00:55:06 2020 -0600
@@ -22,12 +22,8 @@
writeable := access.CanWrite(c.User, "/"+repoPath)
hgcmd := hg.Serve(filepath.Join(reposPath, repoPath), writeable)
- if err := hgcmd.Setup(); err != nil {
- return err
- }
- defer hgcmd.Teardown()
- return hgcmd.ExecPiped(os.Stdin, os.Stdout, os.Stderr)
+ return hgcmd.ExecPiped(repoPath, os.Stdin, os.Stdout, os.Stderr)
}
func (c *Command) init(reposPath, repoPath string) error {
@@ -36,12 +32,8 @@
}
hgcmd := hg.Init(filepath.Join(reposPath, repoPath))
- if err := hgcmd.Setup(); err != nil {
- return err
- }
- defer hgcmd.Teardown()
- return hgcmd.ExecPiped(os.Stdin, os.Stdout, os.Stderr)
+ return hgcmd.ExecPiped(repoPath, os.Stdin, os.Stdout, os.Stderr)
}
func (c *Command) Run(g *globals.Globals) error {
--- a/ssh/commands/init.go Thu Nov 12 00:53:46 2020 -0600
+++ b/ssh/commands/init.go Thu Nov 12 00:55:06 2020 -0600
@@ -28,7 +28,7 @@
return fmt.Errorf("access denied")
}
- if err := run(hg.Init(i.repoPath), session); err != nil {
+ if err := run(i.repoPath, hg.Init(i.repoPath), session); err != nil {
return err
}
--- a/ssh/commands/run.go Thu Nov 12 00:53:46 2020 -0600
+++ b/ssh/commands/run.go Thu Nov 12 00:55:06 2020 -0600
@@ -6,6 +6,6 @@
"keep.imfreedom.org/grim/hgkeeper/hg"
)
-func run(hgCmd *hg.Command, session ssh.Session) error {
- return hgCmd.ExecPiped(session, session, session.Stderr())
+func run(repoPath string, hgCmd *hg.Command, session ssh.Session) error {
+ return hgCmd.ExecPiped(repoPath, session, session, session.Stderr())
}
--- a/ssh/commands/serve.go Thu Nov 12 00:53:46 2020 -0600
+++ b/ssh/commands/serve.go Thu Nov 12 00:55:06 2020 -0600
@@ -30,7 +30,8 @@
writeable := access.CanWrite(username, "/"+s.repoName)
- if err := run(hg.Serve(s.repoPath, writeable), session); err != nil {
+ hgcmd := hg.Serve(s.repoPath, writeable)
+ if err := run(s.repoName, hgcmd, session); err != nil {
return err
}