github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/parse_nogogit.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 //go:build !gogit 7 8 package git 9 10 import ( 11 "bufio" 12 "bytes" 13 "fmt" 14 "io" 15 "strconv" 16 "strings" 17 18 "github.com/gitbundle/modules/log" 19 ) 20 21 // ParseTreeEntries parses the output of a `git ls-tree -l` command. 22 func ParseTreeEntries(data []byte) ([]*TreeEntry, error) { 23 return parseTreeEntries(data, nil) 24 } 25 26 func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) { 27 entries := make([]*TreeEntry, 0, 10) 28 for pos := 0; pos < len(data); { 29 // expect line to be of the form "<mode> <type> <sha> <space-padded-size>\t<filename>" 30 entry := new(TreeEntry) 31 entry.ptree = ptree 32 if pos+6 > len(data) { 33 return nil, fmt.Errorf("Invalid ls-tree output: %s", string(data)) 34 } 35 switch string(data[pos : pos+6]) { 36 case "100644": 37 entry.entryMode = EntryModeBlob 38 pos += 12 // skip over "100644 blob " 39 case "100755": 40 entry.entryMode = EntryModeExec 41 pos += 12 // skip over "100755 blob " 42 case "120000": 43 entry.entryMode = EntryModeSymlink 44 pos += 12 // skip over "120000 blob " 45 case "160000": 46 entry.entryMode = EntryModeCommit 47 pos += 14 // skip over "160000 object " 48 case "040000": 49 entry.entryMode = EntryModeTree 50 pos += 12 // skip over "040000 tree " 51 default: 52 return nil, fmt.Errorf("unknown type: %v", string(data[pos:pos+6])) 53 } 54 55 if pos+40 > len(data) { 56 return nil, fmt.Errorf("Invalid ls-tree output: %s", string(data)) 57 } 58 id, err := NewIDFromString(string(data[pos : pos+40])) 59 if err != nil { 60 return nil, fmt.Errorf("Invalid ls-tree output: %v", err) 61 } 62 entry.ID = id 63 pos += 41 // skip over sha and trailing space 64 65 end := pos + bytes.IndexByte(data[pos:], '\t') 66 if end < pos { 67 return nil, fmt.Errorf("Invalid ls-tree -l output: %s", string(data)) 68 } 69 entry.size, _ = strconv.ParseInt(strings.TrimSpace(string(data[pos:end])), 10, 64) 70 entry.sized = true 71 72 pos = end + 1 73 74 end = pos + bytes.IndexByte(data[pos:], '\n') 75 if end < pos { 76 return nil, fmt.Errorf("Invalid ls-tree output: %s", string(data)) 77 } 78 79 // In case entry name is surrounded by double quotes(it happens only in git-shell). 80 if data[pos] == '"' { 81 entry.name, err = strconv.Unquote(string(data[pos:end])) 82 if err != nil { 83 return nil, fmt.Errorf("Invalid ls-tree output: %v", err) 84 } 85 } else { 86 entry.name = string(data[pos:end]) 87 } 88 89 pos = end + 1 90 entries = append(entries, entry) 91 } 92 return entries, nil 93 } 94 95 func catBatchParseTreeEntries(ptree *Tree, rd *bufio.Reader, sz int64) ([]*TreeEntry, error) { 96 fnameBuf := make([]byte, 4096) 97 modeBuf := make([]byte, 40) 98 shaBuf := make([]byte, 40) 99 entries := make([]*TreeEntry, 0, 10) 100 101 loop: 102 for sz > 0 { 103 mode, fname, sha, count, err := ParseTreeLine(rd, modeBuf, fnameBuf, shaBuf) 104 if err != nil { 105 if err == io.EOF { 106 break loop 107 } 108 return nil, err 109 } 110 sz -= int64(count) 111 entry := new(TreeEntry) 112 entry.ptree = ptree 113 114 switch string(mode) { 115 case "100644": 116 entry.entryMode = EntryModeBlob 117 case "100755": 118 entry.entryMode = EntryModeExec 119 case "120000": 120 entry.entryMode = EntryModeSymlink 121 case "160000": 122 entry.entryMode = EntryModeCommit 123 case "40000": 124 entry.entryMode = EntryModeTree 125 default: 126 log.Debug("Unknown mode: %v", string(mode)) 127 return nil, fmt.Errorf("unknown mode: %v", string(mode)) 128 } 129 130 entry.ID = MustID(sha) 131 entry.name = string(fname) 132 entries = append(entries, entry) 133 } 134 if _, err := rd.Discard(1); err != nil { 135 return entries, err 136 } 137 138 return entries, nil 139 }