Repository returns the Repository represented by this submodule
()
| 100 | |
| 101 | // Repository returns the Repository represented by this submodule |
| 102 | func (s *Submodule) Repository() (*Repository, error) { |
| 103 | if !s.initialized { |
| 104 | return nil, ErrSubmoduleNotInitialized |
| 105 | } |
| 106 | |
| 107 | storer, err := s.w.r.Storer.Module(s.c.Name) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | _, err = storer.Reference(plumbing.HEAD) |
| 113 | if err != nil && err != plumbing.ErrReferenceNotFound { |
| 114 | return nil, err |
| 115 | } |
| 116 | |
| 117 | var exists bool |
| 118 | if err == nil { |
| 119 | exists = true |
| 120 | } |
| 121 | |
| 122 | var worktree billy.Filesystem |
| 123 | if worktree, err = s.w.Filesystem.Chroot(s.c.Path); err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | |
| 127 | if exists { |
| 128 | return Open(storer, worktree) |
| 129 | } |
| 130 | |
| 131 | r, err := Init(storer, worktree) |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
| 135 | |
| 136 | moduleEndpoint, err := transport.NewEndpoint(s.c.URL) |
| 137 | if err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | |
| 141 | if !path.IsAbs(moduleEndpoint.Path) && moduleEndpoint.Protocol == "file" { |
| 142 | remotes, err := s.w.r.Remotes() |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | |
| 147 | rootEndpoint, err := transport.NewEndpoint(remotes[0].c.URLs[0]) |
| 148 | if err != nil { |
| 149 | return nil, err |
| 150 | } |
| 151 | |
| 152 | rootEndpoint.Path = path.Join(rootEndpoint.Path, moduleEndpoint.Path) |
| 153 | *moduleEndpoint = *rootEndpoint |
| 154 | } |
| 155 | |
| 156 | _, err = r.CreateRemote(&config.RemoteConfig{ |
| 157 | Name: DefaultRemoteName, |
| 158 | URLs: []string{moduleEndpoint.String()}, |
| 159 | }) |