File: status.go

package info (click to toggle)
gitbatch 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: makefile: 5; sh: 1
file content (98 lines) | stat: -rw-r--r-- 2,306 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package command

import (
	"fmt"
	"os"
	"regexp"
	"sort"
	"strings"

	"github.com/isacikgoz/gitbatch/internal/git"
)

func shortStatus(r *git.Repository, option string) string {
	args := make([]string, 0)
	args = append(args, "status")
	args = append(args, option)
	args = append(args, "--short")
	out, err := Run(r.AbsPath, "git", args)
	if err != nil {
		return "?"
	}
	return out
}

// Status returns the dirty files
func Status(r *git.Repository) ([]*git.File, error) {
	// in case we want configure Status command externally
	mode := ModeLegacy

	switch mode {
	case ModeLegacy:
		return statusWithGit(r)
	case ModeNative:
		return statusWithGoGit(r)
	}
	return nil, fmt.Errorf("unhandled status operation")
}

// PlainStatus returns the plain status
func PlainStatus(r *git.Repository) (string, error) {
	args := make([]string, 0)
	args = append(args, "status")
	output, err := Run(r.AbsPath, "git", args)
	if err != nil {
		return "", err
	}
	re := regexp.MustCompile(`\n?\r`)
	output = re.ReplaceAllString(output, "\n")
	return output, err
}

// LoadFiles function simply commands a git status and collects output in a
// structured way
func statusWithGit(r *git.Repository) ([]*git.File, error) {
	files := make([]*git.File, 0)
	output := shortStatus(r, "--untracked-files=all")
	if len(output) == 0 {
		return files, nil
	}
	fileslist := strings.Split(output, "\n")
	for _, file := range fileslist {
		x := byte(file[0])
		y := byte(file[1])
		relativePathRegex := regexp.MustCompile(`[(\w|/|.|\-)]+`)
		path := relativePathRegex.FindString(file[2:])

		files = append(files, &git.File{
			Name:    path,
			AbsPath: r.AbsPath + string(os.PathSeparator) + path,
			X:       git.FileStatus(x),
			Y:       git.FileStatus(y),
		})
	}
	sort.Sort(git.FilesAlphabetical(files))
	return files, nil
}

func statusWithGoGit(r *git.Repository) ([]*git.File, error) {
	files := make([]*git.File, 0)
	w, err := r.Repo.Worktree()
	if err != nil {
		return files, err
	}
	s, err := w.Status()
	if err != nil {
		return files, err
	}
	for k, v := range s {
		files = append(files, &git.File{
			Name:    k,
			AbsPath: r.AbsPath + string(os.PathSeparator) + k,
			X:       git.FileStatus(v.Staging),
			Y:       git.FileStatus(v.Worktree),
		})
	}
	sort.Sort(git.FilesAlphabetical(files))
	return files, nil
}