| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"path/filepath" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/go-git/go-billy/v5/memfs" |
| 10 |
"github.com/go-git/go-git/v5/config" |
| 11 |
"github.com/go-git/go-git/v5/plumbing" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 13 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 14 |
"github.com/go-git/go-git/v5/storage/filesystem/dotgit" |
| 15 |
"github.com/go-git/go-git/v5/storage/memory" |
| 16 |
|
| 17 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 18 |
. "gopkg.in/check.v1" |
| 19 |
) |
| 20 |
|
| 21 |
type SubmoduleSuite struct { |
| 22 |
BaseSuite |
| 23 |
Worktree *Worktree |
| 24 |
} |
| 25 |
|
| 26 |
var _ = Suite(&SubmoduleSuite{}) |
| 27 |
|
| 28 |
func (s *SubmoduleSuite) SetUpTest(c *C) { |
| 29 |
path := fixtures.ByTag("submodule").One().Worktree().Root() |
| 30 |
|
| 31 |
dir := c.MkDir() |
| 32 |
|
| 33 |
r, err := PlainClone(filepath.Join(dir, "worktree"), false, &CloneOptions{ |
| 34 |
URL: path, |
| 35 |
}) |
| 36 |
|
| 37 |
c.Assert(err, IsNil) |
| 38 |
|
| 39 |
s.Repository = r |
| 40 |
s.Worktree, err = r.Worktree() |
| 41 |
c.Assert(err, IsNil) |
| 42 |
} |
| 43 |
|
| 44 |
func (s *SubmoduleSuite) TestInit(c *C) { |
| 45 |
sm, err := s.Worktree.Submodule("basic") |
| 46 |
c.Assert(err, IsNil) |
| 47 |
|
| 48 |
c.Assert(sm.initialized, Equals, false) |
| 49 |
err = sm.Init() |
| 50 |
c.Assert(err, IsNil) |
| 51 |
|
| 52 |
c.Assert(sm.initialized, Equals, true) |
| 53 |
|
| 54 |
cfg, err := s.Repository.Config() |
| 55 |
c.Assert(err, IsNil) |
| 56 |
|
| 57 |
c.Assert(cfg.Submodules, HasLen, 1) |
| 58 |
c.Assert(cfg.Submodules["basic"], NotNil) |
| 59 |
|
| 60 |
status, err := sm.Status() |
| 61 |
c.Assert(err, IsNil) |
| 62 |
c.Assert(status.IsClean(), Equals, false) |
| 63 |
} |
| 64 |
|
| 65 |
func (s *SubmoduleSuite) TestUpdate(c *C) { |
| 66 |
if testing.Short() { |
| 67 |
c.Skip("skipping test in short mode.") |
| 68 |
} |
| 69 |
|
| 70 |
sm, err := s.Worktree.Submodule("basic") |
| 71 |
c.Assert(err, IsNil) |
| 72 |
|
| 73 |
err = sm.Update(&SubmoduleUpdateOptions{ |
| 74 |
Init: true, |
| 75 |
}) |
| 76 |
|
| 77 |
c.Assert(err, IsNil) |
| 78 |
|
| 79 |
r, err := sm.Repository() |
| 80 |
c.Assert(err, IsNil) |
| 81 |
|
| 82 |
ref, err := r.Reference(plumbing.HEAD, true) |
| 83 |
c.Assert(err, IsNil) |
| 84 |
c.Assert(ref.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 85 |
|
| 86 |
status, err := sm.Status() |
| 87 |
c.Assert(err, IsNil) |
| 88 |
c.Assert(status.IsClean(), Equals, true) |
| 89 |
} |
| 90 |
|
| 91 |
func (s *SubmoduleSuite) TestRepositoryWithoutInit(c *C) { |
| 92 |
sm, err := s.Worktree.Submodule("basic") |
| 93 |
c.Assert(err, IsNil) |
| 94 |
|
| 95 |
r, err := sm.Repository() |
| 96 |
c.Assert(err, Equals, ErrSubmoduleNotInitialized) |
| 97 |
c.Assert(r, IsNil) |
| 98 |
} |
| 99 |
|
| 100 |
func (s *SubmoduleSuite) TestUpdateWithoutInit(c *C) { |
| 101 |
sm, err := s.Worktree.Submodule("basic") |
| 102 |
c.Assert(err, IsNil) |
| 103 |
|
| 104 |
err = sm.Update(&SubmoduleUpdateOptions{}) |
| 105 |
c.Assert(err, Equals, ErrSubmoduleNotInitialized) |
| 106 |
} |
| 107 |
|
| 108 |
func (s *SubmoduleSuite) TestUpdateWithNotFetch(c *C) { |
| 109 |
sm, err := s.Worktree.Submodule("basic") |
| 110 |
c.Assert(err, IsNil) |
| 111 |
|
| 112 |
err = sm.Update(&SubmoduleUpdateOptions{ |
| 113 |
Init: true, |
| 114 |
NoFetch: true, |
| 115 |
}) |
| 116 |
|
| 117 |
// Since we are not fetching, the object is not there |
| 118 |
c.Assert(err, Equals, plumbing.ErrObjectNotFound) |
| 119 |
} |
| 120 |
|
| 121 |
func (s *SubmoduleSuite) TestUpdateWithRecursion(c *C) { |
| 122 |
if testing.Short() { |
| 123 |
c.Skip("skipping test in short mode.") |
| 124 |
} |
| 125 |
|
| 126 |
sm, err := s.Worktree.Submodule("itself") |
| 127 |
c.Assert(err, IsNil) |
| 128 |
|
| 129 |
err = sm.Update(&SubmoduleUpdateOptions{ |
| 130 |
Init: true, |
| 131 |
RecurseSubmodules: 2, |
| 132 |
}) |
| 133 |
|
| 134 |
c.Assert(err, IsNil) |
| 135 |
|
| 136 |
fs := s.Worktree.Filesystem |
| 137 |
_, err = fs.Stat(fs.Join("itself", "basic", "LICENSE")) |
| 138 |
c.Assert(err, IsNil) |
| 139 |
} |
| 140 |
|
| 141 |
func (s *SubmoduleSuite) TestUpdateWithInitAndUpdate(c *C) { |
| 142 |
if testing.Short() { |
| 143 |
c.Skip("skipping test in short mode.") |
| 144 |
} |
| 145 |
|
| 146 |
sm, err := s.Worktree.Submodule("basic") |
| 147 |
c.Assert(err, IsNil) |
| 148 |
|
| 149 |
err = sm.Update(&SubmoduleUpdateOptions{ |
| 150 |
Init: true, |
| 151 |
}) |
| 152 |
c.Assert(err, IsNil) |
| 153 |
|
| 154 |
idx, err := s.Repository.Storer.Index() |
| 155 |
c.Assert(err, IsNil) |
| 156 |
|
| 157 |
for i, e := range idx.Entries { |
| 158 |
if e.Name == "basic" { |
| 159 |
e.Hash = plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d") |
| 160 |
} |
| 161 |
|
| 162 |
idx.Entries[i] = e |
| 163 |
} |
| 164 |
|
| 165 |
err = s.Repository.Storer.SetIndex(idx) |
| 166 |
c.Assert(err, IsNil) |
| 167 |
|
| 168 |
err = sm.Update(&SubmoduleUpdateOptions{}) |
| 169 |
c.Assert(err, IsNil) |
| 170 |
|
| 171 |
r, err := sm.Repository() |
| 172 |
c.Assert(err, IsNil) |
| 173 |
|
| 174 |
ref, err := r.Reference(plumbing.HEAD, true) |
| 175 |
c.Assert(err, IsNil) |
| 176 |
c.Assert(ref.Hash().String(), Equals, "b029517f6300c2da0f4b651b8642506cd6aaf45d") |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
func (s *SubmoduleSuite) TestSubmodulesInit(c *C) { |
| 181 |
sm, err := s.Worktree.Submodules() |
| 182 |
c.Assert(err, IsNil) |
| 183 |
|
| 184 |
err = sm.Init() |
| 185 |
c.Assert(err, IsNil) |
| 186 |
|
| 187 |
sm, err = s.Worktree.Submodules() |
| 188 |
c.Assert(err, IsNil) |
| 189 |
|
| 190 |
for _, m := range sm { |
| 191 |
c.Assert(m.initialized, Equals, true) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func (s *SubmoduleSuite) TestGitSubmodulesSymlink(c *C) { |
| 196 |
// Plant the malicious symlink directly on the inner filesystem. |
| 197 |
// The worktreeFilesystem wrapper's Symlink rejects .gitmodules |
| 198 |
// link names by design (see validSymlinkName); the read-side |
| 199 |
// detection in Submodules() is the layer being exercised here, |
| 200 |
// so the setup goes through the unwrapped billy.Filesystem. |
| 201 |
fs := s.Worktree.Filesystem |
| 202 |
if wfs, ok := fs.(*worktreeFilesystem); ok { |
| 203 |
fs = wfs.Filesystem |
| 204 |
} |
| 205 |
|
| 206 |
f, err := fs.Create("badfile") |
| 207 |
c.Assert(err, IsNil) |
| 208 |
defer func() { _ = f.Close() }() |
| 209 |
|
| 210 |
err = fs.Remove(gitmodulesFile) |
| 211 |
c.Assert(err, IsNil) |
| 212 |
|
| 213 |
err = fs.Symlink("badfile", gitmodulesFile) |
| 214 |
c.Assert(err, IsNil) |
| 215 |
|
| 216 |
_, err = s.Worktree.Submodules() |
| 217 |
c.Assert(err, Equals, ErrGitModulesSymlink) |
| 218 |
} |
| 219 |
|
| 220 |
func (s *SubmoduleSuite) TestSubmodulesStatus(c *C) { |
| 221 |
sm, err := s.Worktree.Submodules() |
| 222 |
c.Assert(err, IsNil) |
| 223 |
|
| 224 |
status, err := sm.Status() |
| 225 |
c.Assert(err, IsNil) |
| 226 |
c.Assert(status, HasLen, 2) |
| 227 |
} |
| 228 |
|
| 229 |
func (s *SubmoduleSuite) TestSubmodulesUpdateContext(c *C) { |
| 230 |
if testing.Short() { |
| 231 |
c.Skip("skipping test in short mode.") |
| 232 |
} |
| 233 |
|
| 234 |
sm, err := s.Worktree.Submodules() |
| 235 |
c.Assert(err, IsNil) |
| 236 |
|
| 237 |
ctx, cancel := context.WithCancel(context.Background()) |
| 238 |
cancel() |
| 239 |
|
| 240 |
err = sm.UpdateContext(ctx, &SubmoduleUpdateOptions{Init: true}) |
| 241 |
c.Assert(err, NotNil) |
| 242 |
} |
| 243 |
|
| 244 |
func (s *SubmoduleSuite) TestSubmodulesFetchDepth(c *C) { |
| 245 |
if testing.Short() { |
| 246 |
c.Skip("skipping test in short mode.") |
| 247 |
} |
| 248 |
|
| 249 |
sm, err := s.Worktree.Submodule("basic") |
| 250 |
c.Assert(err, IsNil) |
| 251 |
|
| 252 |
err = sm.Update(&SubmoduleUpdateOptions{ |
| 253 |
Init: true, |
| 254 |
Depth: 1, |
| 255 |
}) |
| 256 |
c.Assert(err, IsNil) |
| 257 |
|
| 258 |
r, err := sm.Repository() |
| 259 |
c.Assert(err, IsNil) |
| 260 |
|
| 261 |
lr, err := r.Log(&LogOptions{}) |
| 262 |
c.Assert(err, IsNil) |
| 263 |
|
| 264 |
commitCount := 0 |
| 265 |
for _, err := lr.Next(); err == nil; _, err = lr.Next() { |
| 266 |
commitCount++ |
| 267 |
} |
| 268 |
c.Assert(err, IsNil) |
| 269 |
|
| 270 |
c.Assert(commitCount, Equals, 1) |
| 271 |
} |
| 272 |
|
| 273 |
func (s *SubmoduleSuite) TestSubmoduleParseScp(c *C) { |
| 274 |
repo := &Repository{ |
| 275 |
Storer: memory.NewStorage(), |
| 276 |
wt: memfs.New(), |
| 277 |
} |
| 278 |
worktree := &Worktree{ |
| 279 |
Filesystem: memfs.New(), |
| 280 |
r: repo, |
| 281 |
} |
| 282 |
submodule := &Submodule{ |
| 283 |
initialized: true, |
| 284 |
c: nil, |
| 285 |
w: worktree, |
| 286 |
} |
| 287 |
|
| 288 |
submodule.c = &config.Submodule{ |
| 289 |
Path: "child", |
| 290 |
URL: "[email protected]:username/submodule_repo", |
| 291 |
} |
| 292 |
|
| 293 |
_, err := submodule.Repository() |
| 294 |
c.Assert(err, IsNil) |
| 295 |
} |
| 296 |
|
| 297 |
// newSubmoduleForRelativeURL constructs an in-memory Repository with |
| 298 |
// the given parent remote URL configured as origin, plus a Submodule |
| 299 |
// whose configured URL is the given submoduleURL. Returns the |
| 300 |
// submodule for direct Repository() invocation. Pass parentRemoteURL |
| 301 |
// = "" to omit the origin remote entirely. |
| 302 |
func newSubmoduleForRelativeURL(c *C, parentRemoteURL, submoduleName, submoduleURL string) *Submodule { |
| 303 |
repo := &Repository{ |
| 304 |
Storer: memory.NewStorage(), |
| 305 |
wt: memfs.New(), |
| 306 |
} |
| 307 |
if parentRemoteURL != "" { |
| 308 |
_, err := repo.CreateRemote(&config.RemoteConfig{ |
| 309 |
Name: DefaultRemoteName, |
| 310 |
URLs: []string{parentRemoteURL}, |
| 311 |
}) |
| 312 |
c.Assert(err, IsNil) |
| 313 |
} |
| 314 |
worktree := &Worktree{ |
| 315 |
Filesystem: memfs.New(), |
| 316 |
r: repo, |
| 317 |
} |
| 318 |
return &Submodule{ |
| 319 |
initialized: true, |
| 320 |
c: &config.Submodule{ |
| 321 |
Name: submoduleName, |
| 322 |
Path: submoduleName, |
| 323 |
URL: submoduleURL, |
| 324 |
}, |
| 325 |
w: worktree, |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func (s *SubmoduleSuite) TestRepositoryRelativeURLHTTPSParent(c *C) { |
| 330 |
sm := newSubmoduleForRelativeURL(c, |
| 331 |
"https://example.invalid/group/proj.git", "basic", "../X.git") |
| 332 |
|
| 333 |
r, err := sm.Repository() |
| 334 |
c.Assert(err, IsNil) |
| 335 |
|
| 336 |
remotes, err := r.Remotes() |
| 337 |
c.Assert(err, IsNil) |
| 338 |
c.Assert(remotes, HasLen, 1) |
| 339 |
c.Assert(remotes[0].Config().URLs[0], Equals, |
| 340 |
"https://example.invalid/group/X.git") |
| 341 |
} |
| 342 |
|
| 343 |
func (s *SubmoduleSuite) TestRepositoryRelativeURLSSHParent(c *C) { |
| 344 |
sm := newSubmoduleForRelativeURL(c, |
| 345 |
"ssh://[email protected]/group/proj.git", "basic", "../X.git") |
| 346 |
|
| 347 |
r, err := sm.Repository() |
| 348 |
c.Assert(err, IsNil) |
| 349 |
|
| 350 |
remotes, err := r.Remotes() |
| 351 |
c.Assert(err, IsNil) |
| 352 |
c.Assert(remotes, HasLen, 1) |
| 353 |
c.Assert(remotes[0].Config().URLs[0], Equals, |
| 354 |
"ssh://[email protected]/group/X.git") |
| 355 |
} |
| 356 |
|
| 357 |
func (s *SubmoduleSuite) TestRepositoryRelativeURLDeepTraversal(c *C) { |
| 358 |
sm := newSubmoduleForRelativeURL(c, |
| 359 |
"https://example.invalid/group/proj.git", "basic", "../../org/X.git") |
| 360 |
|
| 361 |
r, err := sm.Repository() |
| 362 |
c.Assert(err, IsNil) |
| 363 |
|
| 364 |
remotes, err := r.Remotes() |
| 365 |
c.Assert(err, IsNil) |
| 366 |
c.Assert(remotes, HasLen, 1) |
| 367 |
c.Assert(remotes[0].Config().URLs[0], Equals, |
| 368 |
"https://example.invalid/org/X.git") |
| 369 |
} |
| 370 |
|
| 371 |
func (s *SubmoduleSuite) TestRepositoryAbsoluteLocalURLPreserved(c *C) { |
| 372 |
raw := "/abs/path/X.git" |
| 373 |
sm := newSubmoduleForRelativeURL(c, "", "basic", raw) |
| 374 |
|
| 375 |
r, err := sm.Repository() |
| 376 |
c.Assert(err, IsNil) |
| 377 |
|
| 378 |
remotes, err := r.Remotes() |
| 379 |
c.Assert(err, IsNil) |
| 380 |
c.Assert(remotes, HasLen, 1) |
| 381 |
|
| 382 |
// transport.NewEndpoint -> parseFile normalizes via filepath.Abs; |
| 383 |
// on Windows this prepends a drive letter. Mirror that here so the |
| 384 |
// assertion is portable. The point of this test is that the |
| 385 |
// relative-resolution branch is skipped for absolute inputs, not |
| 386 |
// the exact form parseFile produces. |
| 387 |
expected, err := filepath.Abs(raw) |
| 388 |
c.Assert(err, IsNil) |
| 389 |
c.Assert(remotes[0].Config().URLs[0], Equals, "file://"+expected) |
| 390 |
} |
| 391 |
|
| 392 |
func (s *SubmoduleSuite) TestRepositoryRelativeURLNoParentRemote(c *C) { |
| 393 |
sm := newSubmoduleForRelativeURL(c, "", "basic", "../X.git") |
| 394 |
|
| 395 |
_, err := sm.Repository() |
| 396 |
c.Assert(err, NotNil) |
| 397 |
c.Assert(err, ErrorMatches, `resolving relative submodule URL: remote "origin" not found`) |
| 398 |
} |
| 399 |
|
| 400 |
func (s *SubmoduleSuite) TestDefaultRemote(c *C) { |
| 401 |
type testCase struct { |
| 402 |
name string |
| 403 |
remotes map[string]string // remote name → URL |
| 404 |
branches map[string]string // branch name → branch.<name>.remote value |
| 405 |
head *plumbing.Reference |
| 406 |
want string // expected remote name |
| 407 |
wantErrIn string // substring required in error message; "" means no error |
| 408 |
} |
| 409 |
|
| 410 |
hashRef := plumbing.NewHashReference(plumbing.HEAD, plumbing.NewHash("0000000000000000000000000000000000000001")) |
| 411 |
mainSym := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName("main")) |
| 412 |
tagSym := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/tags/v1")) |
| 413 |
|
| 414 |
cases := []testCase{ |
| 415 |
{ |
| 416 |
name: "branch-override-wins", |
| 417 |
remotes: map[string]string{"origin": "file:///o", "upstream": "file:///u"}, |
| 418 |
branches: map[string]string{"main": "upstream"}, |
| 419 |
head: mainSym, |
| 420 |
want: "upstream", |
| 421 |
}, |
| 422 |
{ |
| 423 |
name: "branch-override-with-bogus-remote", |
| 424 |
remotes: map[string]string{"origin": "file:///o", "upstream": "file:///u"}, |
| 425 |
branches: map[string]string{"main": "bogus"}, |
| 426 |
head: mainSym, |
| 427 |
wantErrIn: `remote "bogus" not found`, |
| 428 |
}, |
| 429 |
{ |
| 430 |
name: "single-remote-wins-over-origin-fallback", |
| 431 |
remotes: map[string]string{"upstream": "file:///u"}, |
| 432 |
head: hashRef, |
| 433 |
want: "upstream", |
| 434 |
}, |
| 435 |
{ |
| 436 |
name: "single-remote-with-empty-branch-remote", |
| 437 |
remotes: map[string]string{"upstream": "file:///u"}, |
| 438 |
branches: map[string]string{"main": ""}, |
| 439 |
head: mainSym, |
| 440 |
want: "upstream", |
| 441 |
}, |
| 442 |
{ |
| 443 |
name: "origin-fallback-among-multiple", |
| 444 |
remotes: map[string]string{"origin": "file:///o", "upstream": "file:///u"}, |
| 445 |
head: hashRef, |
| 446 |
want: "origin", |
| 447 |
}, |
| 448 |
{ |
| 449 |
name: "origin-fallback-not-present", |
| 450 |
remotes: map[string]string{"upstream": "file:///u", "fork": "file:///f"}, |
| 451 |
head: hashRef, |
| 452 |
wantErrIn: `remote "origin" not found`, |
| 453 |
}, |
| 454 |
{ |
| 455 |
name: "no-remotes", |
| 456 |
wantErrIn: `remote "origin" not found`, |
| 457 |
}, |
| 458 |
{ |
| 459 |
name: "unborn-branch", |
| 460 |
remotes: map[string]string{"origin": "file:///o", "upstream": "file:///u"}, |
| 461 |
branches: map[string]string{"main": "upstream"}, |
| 462 |
head: mainSym, |
| 463 |
want: "upstream", |
| 464 |
}, |
| 465 |
{ |
| 466 |
name: "head-on-tag-falls-through", |
| 467 |
remotes: map[string]string{"origin": "file:///o", "upstream": "file:///u"}, |
| 468 |
head: tagSym, |
| 469 |
want: "origin", |
| 470 |
}, |
| 471 |
} |
| 472 |
|
| 473 |
for _, tc := range cases { |
| 474 |
c.Logf("case: %s", tc.name) |
| 475 |
|
| 476 |
r := &Repository{Storer: memory.NewStorage()} |
| 477 |
cfg, err := r.Config() |
| 478 |
c.Assert(err, IsNil) |
| 479 |
for name, url := range tc.remotes { |
| 480 |
cfg.Remotes[name] = &config.RemoteConfig{ |
| 481 |
Name: name, |
| 482 |
URLs: []string{url}, |
| 483 |
} |
| 484 |
} |
| 485 |
for name, remote := range tc.branches { |
| 486 |
cfg.Branches[name] = &config.Branch{Name: name, Remote: remote} |
| 487 |
} |
| 488 |
c.Assert(r.Storer.SetConfig(cfg), IsNil) |
| 489 |
|
| 490 |
if tc.head != nil { |
| 491 |
c.Assert(r.Storer.SetReference(tc.head), IsNil) |
| 492 |
} |
| 493 |
|
| 494 |
got, err := defaultRemote(r) |
| 495 |
if tc.wantErrIn != "" { |
| 496 |
c.Assert(err, NotNil) |
| 497 |
c.Assert(err, ErrorMatches, ".*"+tc.wantErrIn+".*") |
| 498 |
continue |
| 499 |
} |
| 500 |
c.Assert(err, IsNil) |
| 501 |
c.Assert(got.Name, Equals, tc.want) |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
func (s *SubmoduleSuite) TestSubmoduleRelativeURLPicksOrigin(c *C) { |
| 506 |
// Two remotes plus a relative submodule URL. With the prior code, |
| 507 |
// remotes[0] from map iteration could be either origin or upstream; |
| 508 |
// the resolved submodule URL therefore differed across runs. Loop |
| 509 |
// 20× to exercise different map orderings within a single test run |
| 510 |
// — every iteration must resolve against origin. |
| 511 |
for i := range 20 { |
| 512 |
parent := &Repository{ |
| 513 |
Storer: memory.NewStorage(), |
| 514 |
wt: memfs.New(), |
| 515 |
} |
| 516 |
cfg, err := parent.Config() |
| 517 |
c.Assert(err, IsNil) |
| 518 |
cfg.Remotes["origin"] = &config.RemoteConfig{ |
| 519 |
Name: "origin", |
| 520 |
URLs: []string{"file:///parent/origin"}, |
| 521 |
} |
| 522 |
cfg.Remotes["upstream"] = &config.RemoteConfig{ |
| 523 |
Name: "upstream", |
| 524 |
URLs: []string{"file:///parent/upstream"}, |
| 525 |
} |
| 526 |
c.Assert(parent.Storer.SetConfig(cfg), IsNil) |
| 527 |
|
| 528 |
sub := &Submodule{ |
| 529 |
initialized: true, |
| 530 |
w: &Worktree{Filesystem: memfs.New(), r: parent}, |
| 531 |
c: &config.Submodule{ |
| 532 |
Name: "child", |
| 533 |
Path: "child", |
| 534 |
URL: "../child", |
| 535 |
}, |
| 536 |
} |
| 537 |
|
| 538 |
subRepo, err := sub.Repository() |
| 539 |
c.Assert(err, IsNil, Commentf("iteration %d", i)) |
| 540 |
|
| 541 |
remotes, err := subRepo.Remotes() |
| 542 |
c.Assert(err, IsNil) |
| 543 |
c.Assert(remotes, HasLen, 1, Commentf("iteration %d", i)) |
| 544 |
c.Assert(remotes[0].Config().URLs[0], Equals, |
| 545 |
"file:///parent/child", |
| 546 |
Commentf("iteration %d: expected URL resolved against origin", i)) |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
func (s *SubmoduleSuite) TestSubmoduleRelativeURLRemoteWithoutURLs(c *C) { |
| 551 |
// Defense in depth: a relative submodule URL must be joined onto |
| 552 |
// the chosen parent remote. If that remote has no configured URL, |
| 553 |
// earlier code panicked on `base.URLs[0]`. Mutating the in-memory |
| 554 |
// config directly bypasses SetConfig's validation, mirroring the |
| 555 |
// on-disk case where a `[remote "origin"]` section with no |
| 556 |
// `url =` entry could be loaded. |
| 557 |
parent := &Repository{ |
| 558 |
Storer: memory.NewStorage(), |
| 559 |
wt: memfs.New(), |
| 560 |
} |
| 561 |
cfg, err := parent.Config() |
| 562 |
c.Assert(err, IsNil) |
| 563 |
cfg.Remotes["origin"] = &config.RemoteConfig{Name: "origin", URLs: nil} |
| 564 |
|
| 565 |
sub := &Submodule{ |
| 566 |
initialized: true, |
| 567 |
w: &Worktree{Filesystem: memfs.New(), r: parent}, |
| 568 |
c: &config.Submodule{ |
| 569 |
Name: "child", |
| 570 |
Path: "child", |
| 571 |
URL: "../child", |
| 572 |
}, |
| 573 |
} |
| 574 |
|
| 575 |
subRepo, err := sub.Repository() |
| 576 |
c.Assert(err, NotNil) |
| 577 |
c.Assert(subRepo, IsNil) |
| 578 |
c.Assert(err, ErrorMatches, |
| 579 |
`resolving relative submodule URL: remote "origin" has no configured URL`) |
| 580 |
} |
| 581 |
|
| 582 |
// TestSubmoduleRepositoryRejectsEscapingName covers the storage-layer |
| 583 |
// defence against submodule name path traversal. Constructing a |
| 584 |
// Submodule with `Name = ".."` programmatically (bypassing the |
| 585 |
// .gitmodules parser) must not result in `Repository()` opening a |
| 586 |
// storer rooted in the parent's `.git/` directory, and must leave the |
| 587 |
// parent's HEAD reference untouched. |
| 588 |
func (s *SubmoduleSuite) TestSubmoduleRepositoryRejectsEscapingName(c *C) { |
| 589 |
dotfs := memfs.New() |
| 590 |
wtfs := memfs.New() |
| 591 |
// Use filesystem.NewStorage rather than memory.NewStorage for the |
| 592 |
// parent because only the filesystem-backed ModuleStorer exercises |
| 593 |
// the dotgit defence; the in-memory implementation maps names to |
| 594 |
// storage units directly without traversing a path. |
| 595 |
storer := filesystem.NewStorage(dotfs, cache.NewObjectLRUDefault()) |
| 596 |
r, err := Init(storer, wtfs) |
| 597 |
c.Assert(err, IsNil) |
| 598 |
|
| 599 |
wt, err := r.Worktree() |
| 600 |
c.Assert(err, IsNil) |
| 601 |
|
| 602 |
headBefore, err := storer.Reference(plumbing.HEAD) |
| 603 |
c.Assert(err, IsNil) |
| 604 |
|
| 605 |
sm := &Submodule{ |
| 606 |
initialized: true, |
| 607 |
c: &config.Submodule{ |
| 608 |
Name: "..", |
| 609 |
Path: "deps/x", |
| 610 |
URL: "https://example.com/", |
| 611 |
}, |
| 612 |
w: wt, |
| 613 |
} |
| 614 |
|
| 615 |
repo, err := sm.Repository() |
| 616 |
c.Assert(err, NotNil) |
| 617 |
c.Assert(errors.Is(err, dotgit.ErrModuleNameEscape), Equals, true) |
| 618 |
c.Assert(repo, IsNil) |
| 619 |
|
| 620 |
headAfter, err := storer.Reference(plumbing.HEAD) |
| 621 |
c.Assert(err, IsNil) |
| 622 |
c.Assert(headBefore.Target(), Equals, headAfter.Target(), |
| 623 |
Commentf("parent HEAD must not be overwritten")) |
| 624 |
} |