diff options
author | Koni Marti <koni.marti@gmail.com> | 2023-11-24 16:02:58 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-12-30 15:42:09 +0100 |
commit | 69e73fd7560ec7846451ad6fd859f9286812c64f (patch) | |
tree | 1ffbda4855c1a0afbf5f831fc6e3f2b187baee89 /lib/pama/revctrl/revctrl.go | |
parent | 724d3a22cdaae3299df23195788ccfad7e332db8 (diff) | |
download | aerc-69e73fd7560ec7846451ad6fd859f9286812c64f.tar.gz |
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 <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/pama/revctrl/revctrl.go')
-rw-r--r-- | lib/pama/revctrl/revctrl.go | 48 |
1 files changed, 48 insertions, 0 deletions
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) +} |