Repository returns the Repository represented by this submodule
()
| 103 | |
| 104 | // Repository returns the Repository represented by this submodule |
| 105 | func (s *Submodule) Repository() (*Repository, error) { |
| 106 | if !s.initialized { |
| 107 | return nil, ErrSubmoduleNotInitialized |
| 108 | } |
| 109 | |
| 110 | storer, err := s.w.r.Storer.Module(s.c.Name) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | |
| 115 | _, err = storer.Reference(plumbing.HEAD) |
| 116 | if err != nil && err != plumbing.ErrReferenceNotFound { |
| 117 | return nil, err |
| 118 | } |
| 119 | |
| 120 | var exists bool |
| 121 | if err == nil { |
| 122 | exists = true |
| 123 | } |
| 124 | |
| 125 | // s.c.Path is sourced from the worktree's .gitmodules and is |
| 126 | // therefore tree-controlled. Apply the strict tree-path validator |
| 127 | // before chroot — the wrapper's tolerant validPath would let a |
| 128 | // final-position .git component through (e.g. "submodule/.git"), |
| 129 | // which a malicious .gitmodules could use to chroot the submodule |
| 130 | // worktree into the repository's actual .git directory. |
| 131 | if err := pathutil.ValidTreePath(s.c.Path); err != nil { |
| 132 | return nil, err |
| 133 | } |
| 134 | |
| 135 | var worktree billy.Filesystem |
| 136 | if worktree, err = s.w.Filesystem.Chroot(s.c.Path); err != nil { |
| 137 | return nil, err |
| 138 | } |
| 139 | |
| 140 | if exists { |
| 141 | return Open(storer, worktree) |
| 142 | } |
| 143 | |
| 144 | r, err := Init(storer, worktree) |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | |
| 149 | moduleEndpoint, err := transport.NewEndpoint(s.c.URL) |
| 150 | if err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | |
| 154 | // A relative submodule URL such as "../X.git" must resolve against |
| 155 | // the parent repository's remote URL, not against the process CWD. |
| 156 | // Detect relativity from the raw configured URL because |
| 157 | // transport.NewEndpoint normalizes local paths to absolute form via |
| 158 | // filepath.Abs, which would otherwise mask the relative form here. |
| 159 | if giturl.IsLocalEndpoint(s.c.URL) && |
| 160 | !path.IsAbs(s.c.URL) && !filepath.IsAbs(s.c.URL) { |
| 161 | |
| 162 | base, err := defaultRemote(s.w.r) |