-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
60 lines (51 loc) · 1.08 KB
/
utils.go
File metadata and controls
60 lines (51 loc) · 1.08 KB
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
package main
import (
"bufio"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io/fs"
"os"
)
func fileSha1(file string) (string, error) {
contents, err := os.ReadFile(file)
if err != nil {
return "", fmt.Errorf("Unable to read %s %d", file, err)
}
hash := getSha1(contents)
return hash, nil
}
func getSha1(data []byte) string {
hasher := sha1.New()
hasher.Write(data)
// Get the final hash and convert it to a hexadecimal string
hashInBytes := hasher.Sum(nil)
hashString := hex.EncodeToString(hashInBytes)
return hashString
}
func repoExists() bool {
_, err := os.Stat(".tgit")
if errors.Is(err, fs.ErrNotExist) {
if err != nil {
return false
}
}
return true
}
func currBranch(headInf fs.FileInfo) (string, error) {
headFile, err := os.OpenFile(".tgit/refs/HEAD", os.O_RDWR, fs.ModePerm)
if err != nil {
return "", fmt.Errorf("Unable to read HEAD file")
}
defer headFile.Close()
// Get the curr branch name
var currBranch string
if headInf.Size() > 0 {
sc := bufio.NewScanner(headFile)
for sc.Scan() {
currBranch = sc.Text()
}
}
return currBranch, nil
}