summaryrefslogtreecommitdiffstats
path: root/pkg/integration/components/git.go
blob: 1e2975be25a3d0ccecebb8082c96d3f97c97272f (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
57
package components

import (
	"fmt"
	"log"
	"strings"

	"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
)

type Git struct {
	*assertionHelper
	shell *Shell
}

func (self *Git) CurrentBranchName(expectedName string) *Git {
	return self.assert([]string{"git", "rev-parse", "--abbrev-ref", "HEAD"}, expectedName)
}

func (self *Git) TagNamesAt(ref string, expectedNames []string) *Git {
	return self.assert([]string{"git", "tag", "--sort=v:refname", "--points-at", ref}, strings.Join(expectedNames, "\n"))
}

func (self *Git) RemoteTagDeleted(ref string, tagName string) *Git {
	return self.expect([]string{"git", "ls-remote", ref, fmt.Sprintf("refs/tags/%s", tagName)}, func(s string) (bool, string) {
		return len(s) == 0, fmt.Sprintf("Expected tag %s to have been removed from %s", tagName, ref)
	})
}

func (self *Git) assert(cmdArgs []string, expected string) *Git {
	self.expect(cmdArgs, func(output string) (bool, string) {
		return output == expected, fmt.Sprintf("Expected current branch name to be '%s', but got '%s'", expected, output)
	})

	return self
}

func (self *Git) expect(cmdArgs []string, condition func(string) (bool, string)) *Git {
	self.assertWithRetries(func() (bool, string) {
		output, err := self.shell.runCommandWithOutput(cmdArgs)
		if err != nil {
			return false, fmt.Sprintf("Unexpected error running command: `%v`. Error: %s", cmdArgs, err.Error())
		}
		actual := strings.TrimSpace(output)
		return condition(actual)
	})

	return self
}

func (self *Git) Version() *git_commands.GitVersion {
	version, err := getGitVersion()
	if err != nil {
		log.Fatalf("Could not get git version: %v", err)
	}
	return version
}