Skip to content

Commit f2543fe

Browse files
committed
fix: pullAll
1 parent 7e7cf48 commit f2543fe

File tree

2 files changed

+117
-11
lines changed

2 files changed

+117
-11
lines changed

client/cmd/gnit/pull.go

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,31 @@ func (p *Pull) ExecuteAll() error {
5555
return err
5656
}
5757

58-
fmt.Println("Fetching list of files...")
58+
fmt.Println("Pulling all files from repository...")
5959

60-
query := fmt.Sprintf("%s.Repository.ListFiles()", p.config.RealmPath)
61-
result, err := p.client.QueryRaw(query)
60+
packageAlias := config.PackageAlias(p.config.RealmPath)
61+
query := fmt.Sprintf("%s.Repository.SerializePullAll()", packageAlias)
62+
63+
serializedData, err := p.client.RunQuery(p.config.RealmPath, query)
6264
if err != nil {
6365
if p.sourceMode {
6466
fmt.Println("Repository not found or empty, trying to pull realm source files...")
6567
return p.pullRealmSource()
6668
}
67-
return fmt.Errorf("failed to list files: %w", err)
69+
return fmt.Errorf("failed to pull files: %w", err)
6870
}
6971

70-
if len(result) == 0 {
72+
if len(serializedData) == 0 {
7173
fmt.Println("No files found in repository")
7274
if p.sourceMode {
7375
return p.pullRealmSource()
7476
}
7577
return nil
7678
}
7779

78-
files, err := parseFileList(result)
80+
files, err := parseSerializedFiles(string(serializedData))
7981
if err != nil {
80-
return fmt.Errorf("failed to parse file list: %w", err)
82+
return fmt.Errorf("failed to parse files: %w", err)
8183
}
8284

8385
if len(files) == 0 {
@@ -88,12 +90,13 @@ func (p *Pull) ExecuteAll() error {
8890
return nil
8991
}
9092

91-
fmt.Printf("Found %d file(s), pulling all...\n", len(files))
93+
fmt.Printf("Found %d file(s), writing to disk...\n", len(files))
9294

93-
for _, filename := range files {
94-
if err := p.Execute(filename); err != nil {
95-
return fmt.Errorf("failed to pull '%s': %w", filename, err)
95+
for filename, content := range files {
96+
if err := filesystem.WriteFile(filename, content); err != nil {
97+
return fmt.Errorf("failed to write '%s': %w", filename, err)
9698
}
99+
fmt.Printf(" pulled: %s (%d bytes)\n", filename, len(content))
97100
}
98101

99102
fmt.Printf("\nSuccessfully pulled %d file(s)\n", len(files))
@@ -169,6 +172,55 @@ func parseRealmFileList(output string) []string {
169172
return files
170173
}
171174

175+
func parseSerializedFiles(data string) (map[string][]byte, error) {
176+
lines := strings.Split(data, "\n")
177+
files := make(map[string][]byte)
178+
179+
for _, line := range lines {
180+
if line == "" {
181+
continue
182+
}
183+
184+
parts := strings.SplitN(line, "|", 2)
185+
if len(parts) != 2 {
186+
continue
187+
}
188+
189+
filename := unescapeString(parts[0])
190+
content := unescapeString(parts[1])
191+
files[filename] = []byte(content)
192+
}
193+
194+
return files, nil
195+
}
196+
197+
func unescapeString(s string) string {
198+
result := ""
199+
i := 0
200+
for i < len(s) {
201+
if s[i] == '\\' && i+1 < len(s) {
202+
next := s[i+1]
203+
if next == '\\' {
204+
result += "\\"
205+
i += 2
206+
} else if next == '|' {
207+
result += "|"
208+
i += 2
209+
} else if next == 'n' {
210+
result += "\n"
211+
i += 2
212+
} else {
213+
result += string(s[i])
214+
i++
215+
}
216+
} else {
217+
result += string(s[i])
218+
i++
219+
}
220+
}
221+
return result
222+
}
223+
172224
func parseFileList(data string) ([]string, error) {
173225
str := strings.TrimSpace(data)
174226

realms/src/api.gno

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,57 @@ func (r *Repository) ListFiles() []string {
154154

155155
return files
156156
}
157+
158+
func (r *Repository) PullAll() map[string][]byte {
159+
headCommit := r.GetHeadCommit()
160+
if headCommit == nil {
161+
return map[string][]byte{}
162+
}
163+
164+
treeValue, exists := r.objects.Get(headCommit.Tree)
165+
if !exists {
166+
return map[string][]byte{}
167+
}
168+
169+
tree := treeValue.(map[string]string)
170+
result := make(map[string][]byte)
171+
172+
for path, objectHash := range tree {
173+
fileValue, exists := r.objects.Get(objectHash)
174+
if exists {
175+
result[path] = fileValue.([]byte)
176+
}
177+
}
178+
179+
return result
180+
}
181+
182+
func (r *Repository) SerializePullAll() []byte {
183+
files := r.PullAll()
184+
185+
var result string
186+
for path, content := range files {
187+
escapedPath := escapeString(path)
188+
escapedContent := escapeString(string(content))
189+
result += escapedPath + "|" + escapedContent + "\n"
190+
}
191+
192+
return []byte(result)
193+
}
194+
195+
func escapeString(s string) string {
196+
result := ""
197+
for i := 0; i < len(s); i++ {
198+
c := s[i]
199+
if c == '\\' {
200+
result += "\\\\"
201+
} else if c == '|' {
202+
result += "\\|"
203+
} else if c == '\n' {
204+
result += "\\n"
205+
} else {
206+
result += string(c)
207+
}
208+
}
209+
return result
210+
}

0 commit comments

Comments
 (0)