| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"path" |
| 9 |
"path/filepath" |
| 10 |
|
| 11 |
"github.com/go-git/go-billy/v5" |
| 12 |
"github.com/go-git/go-git/v5/config" |
| 13 |
"github.com/go-git/go-git/v5/internal/pathutil" |
| 14 |
giturl "github.com/go-git/go-git/v5/internal/url" |
| 15 |
"github.com/go-git/go-git/v5/plumbing" |
| 16 |
"github.com/go-git/go-git/v5/plumbing/format/index" |
| 17 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
ErrSubmoduleAlreadyInitialized = errors.New("submodule already initialized") |
| 22 |
ErrSubmoduleNotInitialized = errors.New("submodule not initialized") |
| 23 |
) |
| 24 |
|
| 25 |
// Submodule a submodule allows you to keep another Git repository in a |
| 26 |
// subdirectory of your repository. |
| 27 |
type Submodule struct { |
| 28 |
// initialized defines if a submodule was already initialized. |
| 29 |
initialized bool |
| 30 |
|
| 31 |
c *config.Submodule |
| 32 |
w *Worktree |
| 33 |
} |
| 34 |
|
| 35 |
// Config returns the submodule config |
| 36 |
func (s *Submodule) Config() *config.Submodule { |
| 37 |
return s.c |
| 38 |
} |
| 39 |
|
| 40 |
// Init initialize the submodule reading the recorded Entry in the index for |
| 41 |
// the given submodule |
| 42 |
func (s *Submodule) Init() error { |
| 43 |
cfg, err := s.w.r.Config() |
| 44 |
if err != nil { |
| 45 |
return err |
| 46 |
} |
| 47 |
|
| 48 |
_, ok := cfg.Submodules[s.c.Name] |
| 49 |
if ok { |
| 50 |
return ErrSubmoduleAlreadyInitialized |
| 51 |
} |
| 52 |
|
| 53 |
s.initialized = true |
| 54 |
|
| 55 |
cfg.Submodules[s.c.Name] = s.c |
| 56 |
return s.w.r.Storer.SetConfig(cfg) |
| 57 |
} |
| 58 |
|
| 59 |
// Status returns the status of the submodule. |
| 60 |
func (s *Submodule) Status() (*SubmoduleStatus, error) { |
| 61 |
idx, err := s.w.r.Storer.Index() |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
|
| 66 |
return s.status(idx) |
| 67 |
} |
| 68 |
|
| 69 |
func (s *Submodule) status(idx *index.Index) (*SubmoduleStatus, error) { |
| 70 |
status := &SubmoduleStatus{ |
| 71 |
Path: s.c.Path, |
| 72 |
} |
| 73 |
|
| 74 |
e, err := idx.Entry(s.c.Path) |
| 75 |
if err != nil && err != index.ErrEntryNotFound { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
|
| 79 |
if e != nil { |
| 80 |
status.Expected = e.Hash |
| 81 |
} |
| 82 |
|
| 83 |
if !s.initialized { |
| 84 |
return status, nil |
| 85 |
} |
| 86 |
|
| 87 |
r, err := s.Repository() |
| 88 |
if err != nil { |
| 89 |
return nil, err |
| 90 |
} |
| 91 |
|
| 92 |
head, err := r.Head() |
| 93 |
if err == nil { |
| 94 |
status.Current = head.Hash() |
| 95 |
} |
| 96 |
|
| 97 |
if err != nil && err == plumbing.ErrReferenceNotFound { |
| 98 |
err = nil |
| 99 |
} |
| 100 |
|
| 101 |
return status, err |
| 102 |
} |
| 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) |
| 163 |
if err != nil { |
| 164 |
return nil, fmt.Errorf("resolving relative submodule URL: %w", err) |
| 165 |
} |
| 166 |
|
| 167 |
rootEndpoint, err := transport.NewEndpoint(base.URLs[0]) |
| 168 |
if err != nil { |
| 169 |
return nil, err |
| 170 |
} |
| 171 |
|
| 172 |
rootEndpoint.Path = path.Join(rootEndpoint.Path, s.c.URL) |
| 173 |
*moduleEndpoint = *rootEndpoint |
| 174 |
} |
| 175 |
|
| 176 |
_, err = r.CreateRemote(&config.RemoteConfig{ |
| 177 |
Name: DefaultRemoteName, |
| 178 |
URLs: []string{moduleEndpoint.String()}, |
| 179 |
}) |
| 180 |
|
| 181 |
return r, err |
| 182 |
} |
| 183 |
|
| 184 |
// defaultRemote returns the remote that relative submodule URLs are |
| 185 |
// resolved against, mirroring canonical Git's repo_default_remote |
| 186 |
// (remote.c) and resolve_relative_url (builtin/submodule--helper.c): |
| 187 |
// |
| 188 |
// 1. if HEAD is on a branch with branch.<name>.remote configured, |
| 189 |
// use that remote; |
| 190 |
// 2. else if exactly one remote is configured, use it; |
| 191 |
// 3. otherwise fall back to DefaultRemoteName ("origin"). |
| 192 |
// |
| 193 |
// Each rule falls through unconditionally: a branch lookup that |
| 194 |
// finds the branch but with an empty Remote does not short-circuit |
| 195 |
// rule (2). Returns an error when the chosen remote is not configured. |
| 196 |
func defaultRemote(r *Repository) (*config.RemoteConfig, error) { |
| 197 |
cfg, err := r.Config() |
| 198 |
if err != nil { |
| 199 |
return nil, err |
| 200 |
} |
| 201 |
|
| 202 |
if ref, err := r.Reference(plumbing.HEAD, false); err == nil && |
| 203 |
ref.Type() == plumbing.SymbolicReference && |
| 204 |
ref.Target().IsBranch() { |
| 205 |
if b, ok := cfg.Branches[ref.Target().Short()]; ok && b.Remote != "" { |
| 206 |
return lookupRemote(cfg, b.Remote) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if len(cfg.Remotes) == 1 { |
| 211 |
for name := range cfg.Remotes { |
| 212 |
return lookupRemote(cfg, name) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return lookupRemote(cfg, DefaultRemoteName) |
| 217 |
} |
| 218 |
|
| 219 |
func lookupRemote(cfg *config.Config, name string) (*config.RemoteConfig, error) { |
| 220 |
rc, ok := cfg.Remotes[name] |
| 221 |
if !ok { |
| 222 |
return nil, fmt.Errorf("remote %q not found", name) |
| 223 |
} |
| 224 |
if len(rc.URLs) == 0 { |
| 225 |
return nil, fmt.Errorf("remote %q has no configured URL", name) |
| 226 |
} |
| 227 |
return rc, nil |
| 228 |
} |
| 229 |
|
| 230 |
// Update the registered submodule to match what the superproject expects, the |
| 231 |
// submodule should be initialized first calling the Init method or setting in |
| 232 |
// the options SubmoduleUpdateOptions.Init equals true |
| 233 |
func (s *Submodule) Update(o *SubmoduleUpdateOptions) error { |
| 234 |
return s.UpdateContext(context.Background(), o) |
| 235 |
} |
| 236 |
|
| 237 |
// UpdateContext the registered submodule to match what the superproject |
| 238 |
// expects, the submodule should be initialized first calling the Init method or |
| 239 |
// setting in the options SubmoduleUpdateOptions.Init equals true. |
| 240 |
// |
| 241 |
// The provided Context must be non-nil. If the context expires before the |
| 242 |
// operation is complete, an error is returned. The context only affects the |
| 243 |
// transport operations. |
| 244 |
func (s *Submodule) UpdateContext(ctx context.Context, o *SubmoduleUpdateOptions) error { |
| 245 |
return s.update(ctx, o, plumbing.ZeroHash) |
| 246 |
} |
| 247 |
|
| 248 |
func (s *Submodule) update(ctx context.Context, o *SubmoduleUpdateOptions, forceHash plumbing.Hash) error { |
| 249 |
if !s.initialized && !o.Init { |
| 250 |
return ErrSubmoduleNotInitialized |
| 251 |
} |
| 252 |
|
| 253 |
if !s.initialized && o.Init { |
| 254 |
if err := s.Init(); err != nil { |
| 255 |
return err |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
idx, err := s.w.r.Storer.Index() |
| 260 |
if err != nil { |
| 261 |
return err |
| 262 |
} |
| 263 |
|
| 264 |
hash := forceHash |
| 265 |
if hash.IsZero() { |
| 266 |
e, err := idx.Entry(s.c.Path) |
| 267 |
if err != nil { |
| 268 |
return err |
| 269 |
} |
| 270 |
|
| 271 |
hash = e.Hash |
| 272 |
} |
| 273 |
|
| 274 |
r, err := s.Repository() |
| 275 |
if err != nil { |
| 276 |
return err |
| 277 |
} |
| 278 |
|
| 279 |
if err := s.fetchAndCheckout(ctx, r, o, hash); err != nil { |
| 280 |
return err |
| 281 |
} |
| 282 |
|
| 283 |
return s.doRecursiveUpdate(ctx, r, o) |
| 284 |
} |
| 285 |
|
| 286 |
func (s *Submodule) doRecursiveUpdate(ctx context.Context, r *Repository, o *SubmoduleUpdateOptions) error { |
| 287 |
if o.RecurseSubmodules == NoRecurseSubmodules { |
| 288 |
return nil |
| 289 |
} |
| 290 |
|
| 291 |
w, err := r.Worktree() |
| 292 |
if err != nil { |
| 293 |
return err |
| 294 |
} |
| 295 |
|
| 296 |
l, err := w.Submodules() |
| 297 |
if err != nil { |
| 298 |
return err |
| 299 |
} |
| 300 |
|
| 301 |
new := &SubmoduleUpdateOptions{} |
| 302 |
*new = *o |
| 303 |
|
| 304 |
new.RecurseSubmodules-- |
| 305 |
return l.UpdateContext(ctx, new) |
| 306 |
} |
| 307 |
|
| 308 |
func (s *Submodule) fetchAndCheckout( |
| 309 |
ctx context.Context, r *Repository, o *SubmoduleUpdateOptions, hash plumbing.Hash, |
| 310 |
) error { |
| 311 |
if !o.NoFetch { |
| 312 |
err := r.FetchContext(ctx, &FetchOptions{Auth: o.Auth, Depth: o.Depth}) |
| 313 |
if err != nil && err != NoErrAlreadyUpToDate { |
| 314 |
return err |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
w, err := r.Worktree() |
| 319 |
if err != nil { |
| 320 |
return err |
| 321 |
} |
| 322 |
|
| 323 |
// Handle a case when submodule refers to an orphaned commit that's still reachable |
| 324 |
// through Git server using a special protocol capability[1]. |
| 325 |
// |
| 326 |
// [1]: https://git-scm.com/docs/protocol-capabilities#_allow_reachable_sha1_in_want |
| 327 |
if !o.NoFetch { |
| 328 |
if _, err := w.r.Object(plumbing.AnyObject, hash); err != nil { |
| 329 |
refSpec := config.RefSpec("+" + hash.String() + ":" + hash.String()) |
| 330 |
|
| 331 |
err := r.FetchContext(ctx, &FetchOptions{ |
| 332 |
Auth: o.Auth, |
| 333 |
RefSpecs: []config.RefSpec{refSpec}, |
| 334 |
Depth: o.Depth, |
| 335 |
}) |
| 336 |
if err != nil && err != NoErrAlreadyUpToDate && err != ErrExactSHA1NotSupported { |
| 337 |
return err |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
if err := w.Checkout(&CheckoutOptions{Hash: hash}); err != nil { |
| 343 |
return err |
| 344 |
} |
| 345 |
|
| 346 |
head := plumbing.NewHashReference(plumbing.HEAD, hash) |
| 347 |
return r.Storer.SetReference(head) |
| 348 |
} |
| 349 |
|
| 350 |
// Submodules list of several submodules from the same repository. |
| 351 |
type Submodules []*Submodule |
| 352 |
|
| 353 |
// Init initializes the submodules in this list. |
| 354 |
func (s Submodules) Init() error { |
| 355 |
for _, sub := range s { |
| 356 |
if err := sub.Init(); err != nil { |
| 357 |
return err |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
return nil |
| 362 |
} |
| 363 |
|
| 364 |
// Update updates all the submodules in this list. |
| 365 |
func (s Submodules) Update(o *SubmoduleUpdateOptions) error { |
| 366 |
return s.UpdateContext(context.Background(), o) |
| 367 |
} |
| 368 |
|
| 369 |
// UpdateContext updates all the submodules in this list. |
| 370 |
// |
| 371 |
// The provided Context must be non-nil. If the context expires before the |
| 372 |
// operation is complete, an error is returned. The context only affects the |
| 373 |
// transport operations. |
| 374 |
func (s Submodules) UpdateContext(ctx context.Context, o *SubmoduleUpdateOptions) error { |
| 375 |
for _, sub := range s { |
| 376 |
if err := sub.UpdateContext(ctx, o); err != nil { |
| 377 |
return err |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
return nil |
| 382 |
} |
| 383 |
|
| 384 |
// Status returns the status of the submodules. |
| 385 |
func (s Submodules) Status() (SubmodulesStatus, error) { |
| 386 |
var list SubmodulesStatus |
| 387 |
|
| 388 |
var r *Repository |
| 389 |
for _, sub := range s { |
| 390 |
if r == nil { |
| 391 |
r = sub.w.r |
| 392 |
} |
| 393 |
|
| 394 |
idx, err := r.Storer.Index() |
| 395 |
if err != nil { |
| 396 |
return nil, err |
| 397 |
} |
| 398 |
|
| 399 |
status, err := sub.status(idx) |
| 400 |
if err != nil { |
| 401 |
return nil, err |
| 402 |
} |
| 403 |
|
| 404 |
list = append(list, status) |
| 405 |
} |
| 406 |
|
| 407 |
return list, nil |
| 408 |
} |
| 409 |
|
| 410 |
// SubmodulesStatus contains the status for all submodiles in the worktree |
| 411 |
type SubmodulesStatus []*SubmoduleStatus |
| 412 |
|
| 413 |
// String is equivalent to `git submodule status` |
| 414 |
func (s SubmodulesStatus) String() string { |
| 415 |
buf := bytes.NewBuffer(nil) |
| 416 |
for _, sub := range s { |
| 417 |
fmt.Fprintln(buf, sub) |
| 418 |
} |
| 419 |
|
| 420 |
return buf.String() |
| 421 |
} |
| 422 |
|
| 423 |
// SubmoduleStatus contains the status for a submodule in the worktree |
| 424 |
type SubmoduleStatus struct { |
| 425 |
Path string |
| 426 |
Current plumbing.Hash |
| 427 |
Expected plumbing.Hash |
| 428 |
Branch plumbing.ReferenceName |
| 429 |
} |
| 430 |
|
| 431 |
// IsClean is the HEAD of the submodule is equals to the expected commit |
| 432 |
func (s *SubmoduleStatus) IsClean() bool { |
| 433 |
return s.Current == s.Expected |
| 434 |
} |
| 435 |
|
| 436 |
// String is equivalent to `git submodule status <submodule>` |
| 437 |
// |
| 438 |
// This will print the SHA-1 of the currently checked out commit for a |
| 439 |
// submodule, along with the submodule path and the output of git describe fo |
| 440 |
// the SHA-1. Each SHA-1 will be prefixed with - if the submodule is not |
| 441 |
// initialized, + if the currently checked out submodule commit does not match |
| 442 |
// the SHA-1 found in the index of the containing repository. |
| 443 |
func (s *SubmoduleStatus) String() string { |
| 444 |
var extra string |
| 445 |
var status = ' ' |
| 446 |
|
| 447 |
if s.Current.IsZero() { |
| 448 |
status = '-' |
| 449 |
} else if !s.IsClean() { |
| 450 |
status = '+' |
| 451 |
} |
| 452 |
|
| 453 |
if len(s.Branch) != 0 { |
| 454 |
extra = string(s.Branch[5:]) |
| 455 |
} else if !s.Current.IsZero() { |
| 456 |
extra = s.Current.String()[:7] |
| 457 |
} |
| 458 |
|
| 459 |
if extra != "" { |
| 460 |
extra = fmt.Sprintf(" (%s)", extra) |
| 461 |
} |
| 462 |
|
| 463 |
return fmt.Sprintf("%c%s %s%s", status, s.Expected, s.Path, extra) |
| 464 |
} |