summaryrefslogtreecommitdiffstats
path: root/pkg/commands/models/worktree.go
blob: f02dc184cea35f776b1c2dec78ad412c1849b85b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package models

import (
	"fmt"
	"github.com/go-errors/errors"
	"io/fs"
	"log"
	"os"
	"path/filepath"
)

// Worktree : A git worktree
type Worktree struct {
	Id     int
	Path   string
	Branch string
}

func (w *Worktree) RefName() string {
	return w.Name()
}

func (w *Worktree) ID() string {
	return w.RefName()
}

func (w *Worktree) Description() string {
	return w.RefName()
}

func (w *Worktree) Name() string {
	return filepath.Base(w.Path)
}

func (w *Worktree) Main() bool {
	return w.Id == 0
}

func (w *Worktree) Current() bool {
	pwd, err := os.Getwd()
	if err != nil {
		log.Fatalln(err.Error())
	}

	return pwd == w.Path
}

func (w *Worktree) Missing() bool {
	if _, err := os.Stat(w.Path); err != nil {
		if errors.Is(err, fs.ErrNotExist) {
			return true
		}
		log.Fatalln(fmt.Errorf("failed to check if worktree path `%s` exists\n%w", w.Path, err).Error())
	}
	return false
}