/
whereami.go
46 lines (36 loc) · 1.12 KB
/
whereami.go
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
package controller
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/julien040/gut/src/executor"
)
func WhereAmI(cmd *cobra.Command, args []string) {
// Get the current directory
wd, err := os.Getwd()
if err != nil {
exitOnError("Sorry, I can't get the current working directory 😢", err)
}
// Check if the current directory is a git repository
checkIfGitRepoInitialized(wd)
hash, err := executor.GetHeadHash(wd)
if err != nil {
exitOnError("Sorry, I can't get the current the hash of the HEAD 😢", err)
}
// Get the current branch
branch, err := executor.GetCurrentBranch(wd)
if err != nil {
exitOnError("Sorry, I can't get the current branch 😢", err)
}
// Check if the head is detached
detached, err := executor.IsDetachedHead(wd)
if err != nil {
exitOnError("Sorry, I can't check if the HEAD is detached 😢", err)
}
if detached {
fmt.Fprintf(color.Output, "HEAD is detached (lookout mode) at %s\n", color.HiGreenString(hash))
return
}
fmt.Fprintf(color.Output, "HEAD is at %s on branch %s\n", color.HiGreenString(hash), color.HiGreenString(branch))
}