55package git
66
77import (
8- "bufio"
98 "fmt"
10- "io"
119 "net"
1210 "net/url"
1311 "path"
@@ -17,90 +15,15 @@ import (
1715
1816var scpSyntax = regexp .MustCompile (`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$` )
1917
20- // SubModule submodule is a reference on git repository
21- type SubModule struct {
22- Path string
23- URL string
24- Branch string
25- }
26-
27- // parseSubmoduleContent this is not a complete parse for gitmodules file, it only
28- // parses the url and path of submodules
29- func parseSubmoduleContent (r io.Reader ) (* ObjectCache , error ) {
30- var path , url , branch string
31- var state int // 0: find section, 1: find path and url
32- subModules := newObjectCache ()
33- scanner := bufio .NewScanner (r )
34- for scanner .Scan () {
35- line := strings .TrimSpace (scanner .Text ())
36-
37- // Skip empty lines and comments
38- if line == "" || strings .HasPrefix (line , "#" ) || strings .HasPrefix (line , ";" ) {
39- continue
40- }
41-
42- // Section header [section]
43- if strings .HasPrefix (line , "[submodule" ) && strings .HasSuffix (line , "]" ) {
44- if path != "" && url != "" {
45- subModules .Set (path , & SubModule {
46- Path : path ,
47- URL : url ,
48- Branch : branch ,
49- })
50- }
51- state = 1
52- path = ""
53- url = ""
54- branch = ""
55- continue
56- }
57-
58- if state != 1 {
59- continue
60- }
61-
62- parts := strings .SplitN (line , "=" , 2 )
63- if len (parts ) != 2 {
64- continue
65- }
66- key := strings .TrimSpace (parts [0 ])
67- value := strings .TrimSpace (parts [1 ])
68- switch key {
69- case "path" :
70- path = value
71- case "url" :
72- url = value
73- case "branch" :
74- branch = value
75- }
76- }
77-
78- if err := scanner .Err (); err != nil {
79- return nil , fmt .Errorf ("error reading file: %w" , err )
80- }
81- if path != "" && url != "" {
82- subModules .Set (path , & SubModule {
83- Path : path ,
84- URL : url ,
85- Branch : branch ,
86- })
87- }
88-
89- return subModules , nil
90- }
91-
92- // SubModuleFile represents a file with submodule type.
93- type SubModuleFile struct {
94- * Commit
95-
18+ // CommitSubModuleFile represents a file with submodule type.
19+ type CommitSubModuleFile struct {
9620 refURL string
9721 refID string
9822}
9923
100- // NewSubModuleFile create a new submodule file
101- func NewSubModuleFile (c * Commit , refURL , refID string ) * SubModuleFile {
102- return & SubModuleFile {
103- Commit : c ,
24+ // NewCommitSubModuleFile create a new submodule file
25+ func NewCommitSubModuleFile (refURL , refID string ) * CommitSubModuleFile {
26+ return & CommitSubModuleFile {
10427 refURL : refURL ,
10528 refID : refID ,
10629 }
@@ -177,11 +100,12 @@ func getRefURL(refURL, urlPrefix, repoFullName, sshDomain string) string {
177100}
178101
179102// RefURL guesses and returns reference URL.
180- func (sf * SubModuleFile ) RefURL (urlPrefix , repoFullName , sshDomain string ) string {
103+ // FIXME: template passes AppURL as urlPrefix, it needs to figure out the correct approach (no hard-coded AppURL anymore)
104+ func (sf * CommitSubModuleFile ) RefURL (urlPrefix , repoFullName , sshDomain string ) string {
181105 return getRefURL (sf .refURL , urlPrefix , repoFullName , sshDomain )
182106}
183107
184108// RefID returns reference ID.
185- func (sf * SubModuleFile ) RefID () string {
109+ func (sf * CommitSubModuleFile ) RefID () string {
186110 return sf .refID
187111}
0 commit comments