-
Notifications
You must be signed in to change notification settings - Fork 2
/
writeTree.go
122 lines (110 loc) · 3.61 KB
/
writeTree.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"strings"
"github.com/JunNishimura/Goit/internal/object"
index "github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)
func writeTreeObject(rootGoitPath string, entries []*index.Entry) (*object.Object, error) {
var dirName string
var data []byte
var entryBuf []*index.Entry
i := 0
for {
if i >= len(entries) {
// if the last entry is in the directory
if dirName != "" {
treeObject, err := writeTreeObject(rootGoitPath, entryBuf)
if err != nil {
return nil, err
}
data = append(data, []byte(fmt.Sprintf("040000 %s", dirName))...)
data = append(data, 0x00)
data = append(data, treeObject.Hash...)
}
break
}
entry := entries[i]
slashSplit := strings.SplitN(string(entry.Path), "/", 2)
if len(slashSplit) == 1 { // if entry is not in sub-directory
if dirName != "" { // if previous entry is in sub-directory
// make tree object from entryBuf
treeObject, err := writeTreeObject(rootGoitPath, entryBuf)
if err != nil {
return nil, err
}
data = append(data, []byte(fmt.Sprintf("040000 %s", dirName))...)
data = append(data, 0x00)
data = append(data, treeObject.Hash...)
// clear dirName and entryBuf
dirName = ""
entryBuf = make([]*index.Entry, 0)
}
data = append(data, []byte(fmt.Sprintf("100644 %s", string(entry.Path)))...)
data = append(data, 0x00)
data = append(data, entry.Hash...)
} else { // if entry is in sub-directory
if dirName == "" { // previous entry is not in sub-directory
dirName = slashSplit[0] // root sub-directory name e.x) cmd/pkg/main.go -> cmd
newEntry := index.NewEntry(entry.Hash, []byte(slashSplit[1]))
entryBuf = append(entryBuf, newEntry)
} else if dirName != "" && dirName == slashSplit[0] { // previous entry is in sub-directory, and current entry is in the same sub-directory
newEntry := index.NewEntry(entry.Hash, []byte(slashSplit[1]))
entryBuf = append(entryBuf, newEntry)
} else if dirName != "" && dirName != slashSplit[0] { // previous entry is in sub-directory, and current entry is in the different sub-directory
// make tree object
treeObject, err := writeTreeObject(rootGoitPath, entryBuf)
if err != nil {
return nil, err
}
data = append(data, []byte(fmt.Sprintf("040000 %s", dirName))...)
data = append(data, 0x00)
data = append(data, treeObject.Hash...)
// start making tree object for different sub-directory
dirName = slashSplit[0]
newEntry := index.NewEntry(entry.Hash, []byte(slashSplit[1]))
entryBuf = []*index.Entry{newEntry}
}
}
i++
}
// make tree object
treeObject, err := object.NewObject(object.TreeObject, data)
if err != nil {
return nil, err
}
// write tree object
if err := treeObject.Write(rootGoitPath); err != nil {
return nil, err
}
return treeObject, nil
}
// writeTreeCmd represents the writeTree command
var writeTreeCmd = &cobra.Command{
Use: "write-tree",
Short: "write tree object from index",
Long: "this is a command to write tree object from index",
PreRunE: func(cmd *cobra.Command, args []string) error {
if client.RootGoitPath == "" {
return ErrGoitNotInitialized
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// make and write treeObject from index
rootTreeObject, err := writeTreeObject(client.RootGoitPath, client.Idx.Entries)
if err != nil {
return fmt.Errorf("fail to write tree object: %w", err)
}
// print out tree object hash
fmt.Printf("%s\n", rootTreeObject.Hash)
return nil
},
}
func init() {
rootCmd.AddCommand(writeTreeCmd)
}