From 69e73fd7560ec7846451ad6fd859f9286812c64f Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Fri, 24 Nov 2023 16:02:58 +0100 Subject: pama: implement the revision control logic Implement the RevisionController interface to interact with a respository control system. Add the implementation for git. Other revision systems such as mercurial, pijul or fossil can be extended. Signed-off-by: Koni Marti Acked-by: Robin Jarry --- lib/pama/revctrl/revctrl.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/pama/revctrl/revctrl.go (limited to 'lib/pama/revctrl/revctrl.go') diff --git a/lib/pama/revctrl/revctrl.go b/lib/pama/revctrl/revctrl.go new file mode 100644 index 00000000..42532216 --- /dev/null +++ b/lib/pama/revctrl/revctrl.go @@ -0,0 +1,48 @@ +package revctrl + +import ( + "errors" + "fmt" + + "git.sr.ht/~rjarry/aerc/lib/pama/models" + "git.sr.ht/~rjarry/aerc/log" +) + +var ErrUnsupported = errors.New("unsupported") + +type factoryFunc func(string) models.RevisionController + +var controllers = map[string]factoryFunc{} + +func register(controllerID string, fn factoryFunc) { + controllers[controllerID] = fn +} + +func New(controllerID string, path string) (models.RevisionController, error) { + factoryFunc, ok := controllers[controllerID] + if !ok { + return nil, errors.New("cannot create revision control instance") + } + return factoryFunc(path), nil +} + +type detector interface { + Support() bool + Root() (string, error) +} + +func Detect(path string) (string, string, error) { + for controllerID, factoryFunc := range controllers { + rc, ok := factoryFunc(path).(detector) + if ok && rc.Support() { + log.Tracef("support found for %v", controllerID) + root, err := rc.Root() + if err != nil { + continue + } + log.Tracef("root found in %s", root) + return controllerID, root, nil + } + } + return "", "", fmt.Errorf("no supported repository found in %s", path) +} -- cgit