| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"regexp" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 17 |
"github.com/go-git/go-git/v5/config" |
| 18 |
"github.com/go-git/go-git/v5/plumbing" |
| 19 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 20 |
"github.com/go-git/go-git/v5/plumbing/filemode" |
| 21 |
"github.com/go-git/go-git/v5/plumbing/format/gitignore" |
| 22 |
"github.com/go-git/go-git/v5/plumbing/format/index" |
| 23 |
"github.com/go-git/go-git/v5/plumbing/object" |
| 24 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 25 |
"github.com/go-git/go-git/v5/storage/memory" |
| 26 |
"github.com/stretchr/testify/assert" |
| 27 |
"github.com/stretchr/testify/require" |
| 28 |
|
| 29 |
"github.com/go-git/go-billy/v5" |
| 30 |
"github.com/go-git/go-billy/v5/memfs" |
| 31 |
"github.com/go-git/go-billy/v5/osfs" |
| 32 |
"github.com/go-git/go-billy/v5/util" |
| 33 |
"golang.org/x/text/unicode/norm" |
| 34 |
. "gopkg.in/check.v1" |
| 35 |
) |
| 36 |
|
| 37 |
func defaultTestCommitOptions() *CommitOptions { |
| 38 |
return &CommitOptions{ |
| 39 |
Author: &object.Signature{Name: "testuser", Email: "testemail"}, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
type WorktreeSuite struct { |
| 44 |
BaseSuite |
| 45 |
} |
| 46 |
|
| 47 |
var _ = Suite(&WorktreeSuite{}) |
| 48 |
|
| 49 |
func (s *WorktreeSuite) SetUpTest(c *C) { |
| 50 |
f := fixtures.Basic().One() |
| 51 |
s.Repository = s.NewRepositoryWithEmptyWorktree(f) |
| 52 |
} |
| 53 |
|
| 54 |
func (s *WorktreeSuite) TestPullCheckout(c *C) { |
| 55 |
fs := memfs.New() |
| 56 |
r, _ := Init(memory.NewStorage(), fs) |
| 57 |
r.CreateRemote(&config.RemoteConfig{ |
| 58 |
Name: DefaultRemoteName, |
| 59 |
URLs: []string{s.GetBasicLocalRepositoryURL()}, |
| 60 |
}) |
| 61 |
|
| 62 |
w, err := r.Worktree() |
| 63 |
c.Assert(err, IsNil) |
| 64 |
|
| 65 |
err = w.Pull(&PullOptions{}) |
| 66 |
c.Assert(err, IsNil) |
| 67 |
|
| 68 |
fi, err := fs.ReadDir("") |
| 69 |
c.Assert(err, IsNil) |
| 70 |
c.Assert(fi, HasLen, 8) |
| 71 |
} |
| 72 |
|
| 73 |
func (s *WorktreeSuite) TestPullFastForward(c *C) { |
| 74 |
url := c.MkDir() |
| 75 |
|
| 76 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 77 |
|
| 78 |
server, err := PlainClone(url, false, &CloneOptions{ |
| 79 |
URL: path, |
| 80 |
}) |
| 81 |
c.Assert(err, IsNil) |
| 82 |
|
| 83 |
dir := c.MkDir() |
| 84 |
|
| 85 |
r, err := PlainClone(dir, false, &CloneOptions{ |
| 86 |
URL: url, |
| 87 |
}) |
| 88 |
c.Assert(err, IsNil) |
| 89 |
|
| 90 |
w, err := server.Worktree() |
| 91 |
c.Assert(err, IsNil) |
| 92 |
err = os.WriteFile(filepath.Join(url, "foo"), []byte("foo"), 0755) |
| 93 |
c.Assert(err, IsNil) |
| 94 |
w.Add("foo") |
| 95 |
hash, err := w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 96 |
c.Assert(err, IsNil) |
| 97 |
|
| 98 |
w, err = r.Worktree() |
| 99 |
c.Assert(err, IsNil) |
| 100 |
|
| 101 |
err = w.Pull(&PullOptions{}) |
| 102 |
c.Assert(err, IsNil) |
| 103 |
|
| 104 |
head, err := r.Head() |
| 105 |
c.Assert(err, IsNil) |
| 106 |
c.Assert(head.Hash(), Equals, hash) |
| 107 |
} |
| 108 |
|
| 109 |
func (s *WorktreeSuite) TestPullNonFastForward(c *C) { |
| 110 |
url := c.MkDir() |
| 111 |
|
| 112 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 113 |
|
| 114 |
server, err := PlainClone(url, false, &CloneOptions{ |
| 115 |
URL: path, |
| 116 |
}) |
| 117 |
c.Assert(err, IsNil) |
| 118 |
|
| 119 |
dir := c.MkDir() |
| 120 |
|
| 121 |
r, err := PlainClone(dir, false, &CloneOptions{ |
| 122 |
URL: url, |
| 123 |
}) |
| 124 |
c.Assert(err, IsNil) |
| 125 |
|
| 126 |
w, err := server.Worktree() |
| 127 |
c.Assert(err, IsNil) |
| 128 |
err = os.WriteFile(filepath.Join(url, "foo"), []byte("foo"), 0755) |
| 129 |
c.Assert(err, IsNil) |
| 130 |
w.Add("foo") |
| 131 |
_, err = w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 132 |
c.Assert(err, IsNil) |
| 133 |
|
| 134 |
w, err = r.Worktree() |
| 135 |
c.Assert(err, IsNil) |
| 136 |
err = os.WriteFile(filepath.Join(dir, "bar"), []byte("bar"), 0755) |
| 137 |
c.Assert(err, IsNil) |
| 138 |
w.Add("bar") |
| 139 |
_, err = w.Commit("bar", &CommitOptions{Author: defaultSignature()}) |
| 140 |
c.Assert(err, IsNil) |
| 141 |
|
| 142 |
err = w.Pull(&PullOptions{}) |
| 143 |
c.Assert(err, Equals, ErrNonFastForwardUpdate) |
| 144 |
} |
| 145 |
|
| 146 |
func (s *WorktreeSuite) TestPullUpdateReferencesIfNeeded(c *C) { |
| 147 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 148 |
r.CreateRemote(&config.RemoteConfig{ |
| 149 |
Name: DefaultRemoteName, |
| 150 |
URLs: []string{s.GetBasicLocalRepositoryURL()}, |
| 151 |
}) |
| 152 |
|
| 153 |
err := r.Fetch(&FetchOptions{}) |
| 154 |
c.Assert(err, IsNil) |
| 155 |
|
| 156 |
_, err = r.Reference("refs/heads/master", false) |
| 157 |
c.Assert(err, NotNil) |
| 158 |
|
| 159 |
w, err := r.Worktree() |
| 160 |
c.Assert(err, IsNil) |
| 161 |
|
| 162 |
err = w.Pull(&PullOptions{}) |
| 163 |
c.Assert(err, IsNil) |
| 164 |
|
| 165 |
head, err := r.Reference(plumbing.HEAD, true) |
| 166 |
c.Assert(err, IsNil) |
| 167 |
c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 168 |
|
| 169 |
branch, err := r.Reference("refs/heads/master", false) |
| 170 |
c.Assert(err, IsNil) |
| 171 |
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 172 |
|
| 173 |
err = w.Pull(&PullOptions{}) |
| 174 |
c.Assert(err, Equals, NoErrAlreadyUpToDate) |
| 175 |
} |
| 176 |
|
| 177 |
func (s *WorktreeSuite) TestPullInSingleBranch(c *C) { |
| 178 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 179 |
err := r.clone(context.Background(), &CloneOptions{ |
| 180 |
URL: s.GetBasicLocalRepositoryURL(), |
| 181 |
SingleBranch: true, |
| 182 |
}) |
| 183 |
|
| 184 |
c.Assert(err, IsNil) |
| 185 |
|
| 186 |
w, err := r.Worktree() |
| 187 |
c.Assert(err, IsNil) |
| 188 |
|
| 189 |
err = w.Pull(&PullOptions{}) |
| 190 |
c.Assert(err, Equals, NoErrAlreadyUpToDate) |
| 191 |
|
| 192 |
branch, err := r.Reference("refs/heads/master", false) |
| 193 |
c.Assert(err, IsNil) |
| 194 |
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 195 |
|
| 196 |
_, err = r.Reference("refs/remotes/foo/branch", false) |
| 197 |
c.Assert(err, NotNil) |
| 198 |
|
| 199 |
storage := r.Storer.(*memory.Storage) |
| 200 |
c.Assert(storage.Objects, HasLen, 28) |
| 201 |
} |
| 202 |
|
| 203 |
func (s *WorktreeSuite) TestPullProgress(c *C) { |
| 204 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 205 |
|
| 206 |
r.CreateRemote(&config.RemoteConfig{ |
| 207 |
Name: DefaultRemoteName, |
| 208 |
URLs: []string{s.GetBasicLocalRepositoryURL()}, |
| 209 |
}) |
| 210 |
|
| 211 |
w, err := r.Worktree() |
| 212 |
c.Assert(err, IsNil) |
| 213 |
|
| 214 |
buf := bytes.NewBuffer(nil) |
| 215 |
err = w.Pull(&PullOptions{ |
| 216 |
Progress: buf, |
| 217 |
}) |
| 218 |
|
| 219 |
c.Assert(err, IsNil) |
| 220 |
c.Assert(buf.Len(), Not(Equals), 0) |
| 221 |
} |
| 222 |
|
| 223 |
func (s *WorktreeSuite) TestPullProgressWithRecursion(c *C) { |
| 224 |
if testing.Short() { |
| 225 |
c.Skip("skipping test in short mode.") |
| 226 |
} |
| 227 |
|
| 228 |
path := fixtures.ByTag("submodule").One().Worktree().Root() |
| 229 |
|
| 230 |
dir := c.MkDir() |
| 231 |
|
| 232 |
r, _ := PlainInit(dir, false) |
| 233 |
r.CreateRemote(&config.RemoteConfig{ |
| 234 |
Name: DefaultRemoteName, |
| 235 |
URLs: []string{path}, |
| 236 |
}) |
| 237 |
|
| 238 |
w, err := r.Worktree() |
| 239 |
c.Assert(err, IsNil) |
| 240 |
|
| 241 |
err = w.Pull(&PullOptions{ |
| 242 |
RecurseSubmodules: DefaultSubmoduleRecursionDepth, |
| 243 |
}) |
| 244 |
c.Assert(err, IsNil) |
| 245 |
|
| 246 |
cfg, err := r.Config() |
| 247 |
c.Assert(err, IsNil) |
| 248 |
c.Assert(cfg.Submodules, HasLen, 2) |
| 249 |
} |
| 250 |
|
| 251 |
func (s *RepositorySuite) TestPullAdd(c *C) { |
| 252 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 253 |
|
| 254 |
r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ |
| 255 |
URL: filepath.Join(path, ".git"), |
| 256 |
}) |
| 257 |
|
| 258 |
c.Assert(err, IsNil) |
| 259 |
|
| 260 |
storage := r.Storer.(*memory.Storage) |
| 261 |
c.Assert(storage.Objects, HasLen, 28) |
| 262 |
|
| 263 |
branch, err := r.Reference("refs/heads/master", false) |
| 264 |
c.Assert(err, IsNil) |
| 265 |
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 266 |
|
| 267 |
ExecuteOnPath(c, path, |
| 268 |
"touch foo", |
| 269 |
"git add foo", |
| 270 |
"git commit --no-gpg-sign -m foo foo", |
| 271 |
) |
| 272 |
|
| 273 |
w, err := r.Worktree() |
| 274 |
c.Assert(err, IsNil) |
| 275 |
|
| 276 |
err = w.Pull(&PullOptions{RemoteName: "origin"}) |
| 277 |
c.Assert(err, IsNil) |
| 278 |
|
| 279 |
// the commit command has introduced a new commit, tree and blob |
| 280 |
c.Assert(storage.Objects, HasLen, 31) |
| 281 |
|
| 282 |
branch, err = r.Reference("refs/heads/master", false) |
| 283 |
c.Assert(err, IsNil) |
| 284 |
c.Assert(branch.Hash().String(), Not(Equals), "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 285 |
} |
| 286 |
|
| 287 |
func (s *WorktreeSuite) TestPullAlreadyUptodate(c *C) { |
| 288 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 289 |
|
| 290 |
fs := memfs.New() |
| 291 |
r, err := Clone(memory.NewStorage(), fs, &CloneOptions{ |
| 292 |
URL: filepath.Join(path, ".git"), |
| 293 |
}) |
| 294 |
|
| 295 |
c.Assert(err, IsNil) |
| 296 |
|
| 297 |
w, err := r.Worktree() |
| 298 |
c.Assert(err, IsNil) |
| 299 |
err = util.WriteFile(fs, "bar", []byte("bar"), 0755) |
| 300 |
c.Assert(err, IsNil) |
| 301 |
w.Add("bar") |
| 302 |
_, err = w.Commit("bar", &CommitOptions{Author: defaultSignature()}) |
| 303 |
c.Assert(err, IsNil) |
| 304 |
|
| 305 |
err = w.Pull(&PullOptions{}) |
| 306 |
c.Assert(err, Equals, NoErrAlreadyUpToDate) |
| 307 |
} |
| 308 |
|
| 309 |
func (s *WorktreeSuite) TestPullDepth(c *C) { |
| 310 |
r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ |
| 311 |
URL: fixtures.Basic().One().URL, |
| 312 |
Depth: 1, |
| 313 |
}) |
| 314 |
|
| 315 |
c.Assert(err, IsNil) |
| 316 |
|
| 317 |
w, err := r.Worktree() |
| 318 |
c.Assert(err, IsNil) |
| 319 |
err = w.Pull(&PullOptions{}) |
| 320 |
c.Assert(err, Equals, nil) |
| 321 |
} |
| 322 |
|
| 323 |
func (s *WorktreeSuite) TestPullAfterShallowClone(c *C) { |
| 324 |
tempDir := c.MkDir() |
| 325 |
remoteURL := filepath.Join(tempDir, "remote") |
| 326 |
repoDir := filepath.Join(tempDir, "repo") |
| 327 |
|
| 328 |
remote, err := PlainInit(remoteURL, false) |
| 329 |
c.Assert(err, IsNil) |
| 330 |
c.Assert(remote, NotNil) |
| 331 |
|
| 332 |
_ = CommitNewFile(c, remote, "File1") |
| 333 |
_ = CommitNewFile(c, remote, "File2") |
| 334 |
|
| 335 |
repo, err := PlainClone(repoDir, false, &CloneOptions{ |
| 336 |
URL: remoteURL, |
| 337 |
Depth: 1, |
| 338 |
Tags: NoTags, |
| 339 |
SingleBranch: true, |
| 340 |
ReferenceName: "master", |
| 341 |
}) |
| 342 |
c.Assert(err, IsNil) |
| 343 |
|
| 344 |
_ = CommitNewFile(c, remote, "File3") |
| 345 |
_ = CommitNewFile(c, remote, "File4") |
| 346 |
|
| 347 |
w, err := repo.Worktree() |
| 348 |
c.Assert(err, IsNil) |
| 349 |
|
| 350 |
err = w.Pull(&PullOptions{ |
| 351 |
RemoteName: DefaultRemoteName, |
| 352 |
SingleBranch: true, |
| 353 |
ReferenceName: plumbing.NewBranchReferenceName("master"), |
| 354 |
}) |
| 355 |
c.Assert(err, IsNil) |
| 356 |
} |
| 357 |
|
| 358 |
func (s *WorktreeSuite) TestCheckout(c *C) { |
| 359 |
fs := memfs.New() |
| 360 |
w := &Worktree{ |
| 361 |
r: s.Repository, |
| 362 |
Filesystem: fs, |
| 363 |
} |
| 364 |
|
| 365 |
err := w.Checkout(&CheckoutOptions{ |
| 366 |
Force: true, |
| 367 |
}) |
| 368 |
c.Assert(err, IsNil) |
| 369 |
|
| 370 |
entries, err := fs.ReadDir("/") |
| 371 |
c.Assert(err, IsNil) |
| 372 |
|
| 373 |
c.Assert(entries, HasLen, 8) |
| 374 |
ch, err := fs.Open("CHANGELOG") |
| 375 |
c.Assert(err, IsNil) |
| 376 |
|
| 377 |
content, err := io.ReadAll(ch) |
| 378 |
c.Assert(err, IsNil) |
| 379 |
c.Assert(string(content), Equals, "Initial changelog\n") |
| 380 |
|
| 381 |
idx, err := s.Repository.Storer.Index() |
| 382 |
c.Assert(err, IsNil) |
| 383 |
c.Assert(idx.Entries, HasLen, 9) |
| 384 |
} |
| 385 |
|
| 386 |
func (s *WorktreeSuite) TestCheckoutForce(c *C) { |
| 387 |
w := &Worktree{ |
| 388 |
r: s.Repository, |
| 389 |
Filesystem: memfs.New(), |
| 390 |
} |
| 391 |
|
| 392 |
err := w.Checkout(&CheckoutOptions{}) |
| 393 |
c.Assert(err, IsNil) |
| 394 |
|
| 395 |
w.Filesystem = memfs.New() |
| 396 |
|
| 397 |
err = w.Checkout(&CheckoutOptions{ |
| 398 |
Force: true, |
| 399 |
}) |
| 400 |
c.Assert(err, IsNil) |
| 401 |
|
| 402 |
entries, err := w.Filesystem.ReadDir("/") |
| 403 |
c.Assert(err, IsNil) |
| 404 |
c.Assert(entries, HasLen, 8) |
| 405 |
} |
| 406 |
|
| 407 |
func (s *WorktreeSuite) TestCheckoutKeep(c *C) { |
| 408 |
w := &Worktree{ |
| 409 |
r: s.Repository, |
| 410 |
Filesystem: memfs.New(), |
| 411 |
} |
| 412 |
|
| 413 |
err := w.Checkout(&CheckoutOptions{ |
| 414 |
Force: true, |
| 415 |
}) |
| 416 |
c.Assert(err, IsNil) |
| 417 |
|
| 418 |
// Create a new branch and create a new file. |
| 419 |
err = w.Checkout(&CheckoutOptions{ |
| 420 |
Branch: plumbing.NewBranchReferenceName("new-branch"), |
| 421 |
Create: true, |
| 422 |
}) |
| 423 |
c.Assert(err, IsNil) |
| 424 |
|
| 425 |
w.Filesystem = memfs.New() |
| 426 |
f, err := w.Filesystem.Create("new-file.txt") |
| 427 |
c.Assert(err, IsNil) |
| 428 |
_, err = f.Write([]byte("DUMMY")) |
| 429 |
c.Assert(err, IsNil) |
| 430 |
c.Assert(f.Close(), IsNil) |
| 431 |
|
| 432 |
// Add the file to staging. |
| 433 |
_, err = w.Add("new-file.txt") |
| 434 |
c.Assert(err, IsNil) |
| 435 |
|
| 436 |
// Switch branch to master, and verify that the new file was kept in staging. |
| 437 |
err = w.Checkout(&CheckoutOptions{ |
| 438 |
Keep: true, |
| 439 |
}) |
| 440 |
c.Assert(err, IsNil) |
| 441 |
|
| 442 |
fi, err := w.Filesystem.Stat("new-file.txt") |
| 443 |
c.Assert(err, IsNil) |
| 444 |
c.Assert(fi.Size(), Equals, int64(5)) |
| 445 |
} |
| 446 |
|
| 447 |
func (s *WorktreeSuite) TestCheckoutSymlink(c *C) { |
| 448 |
if runtime.GOOS == "windows" { |
| 449 |
c.Skip("git doesn't support symlinks by default in windows") |
| 450 |
} |
| 451 |
|
| 452 |
dir := c.MkDir() |
| 453 |
|
| 454 |
r, err := PlainInit(dir, false) |
| 455 |
c.Assert(err, IsNil) |
| 456 |
|
| 457 |
w, err := r.Worktree() |
| 458 |
c.Assert(err, IsNil) |
| 459 |
|
| 460 |
w.Filesystem.Symlink("not-exists", "bar") |
| 461 |
w.Add("bar") |
| 462 |
w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 463 |
|
| 464 |
r.Storer.SetIndex(&index.Index{Version: 2}) |
| 465 |
w.Filesystem = osfs.New(filepath.Join(dir, "worktree-empty")) |
| 466 |
|
| 467 |
err = w.Checkout(&CheckoutOptions{}) |
| 468 |
c.Assert(err, IsNil) |
| 469 |
|
| 470 |
status, err := w.Status() |
| 471 |
c.Assert(err, IsNil) |
| 472 |
c.Assert(status.IsClean(), Equals, true) |
| 473 |
|
| 474 |
target, err := w.Filesystem.Readlink("bar") |
| 475 |
c.Assert(target, Equals, "not-exists") |
| 476 |
c.Assert(err, IsNil) |
| 477 |
} |
| 478 |
|
| 479 |
func TestCheckoutSymlinkArbitraryTarget(t *testing.T) { |
| 480 |
if runtime.GOOS == "windows" { |
| 481 |
t.Skip("git doesn't support symlinks by default in windows") |
| 482 |
} |
| 483 |
t.Parallel() |
| 484 |
|
| 485 |
tests := []struct { |
| 486 |
name string |
| 487 |
target string |
| 488 |
}{ |
| 489 |
{name: "rel", target: "target"}, |
| 490 |
{name: "absolute", target: "/etc/passwd"}, |
| 491 |
{name: "dot-dot relative", target: "../../outside"}, |
| 492 |
} |
| 493 |
|
| 494 |
for _, tc := range tests { |
| 495 |
t.Run(tc.name, func(t *testing.T) { |
| 496 |
t.Parallel() |
| 497 |
|
| 498 |
dir := t.TempDir() |
| 499 |
wtFS := osfs.New(dir, osfs.WithBoundOS()) |
| 500 |
|
| 501 |
dotFS, err := wtFS.Chroot(".git") |
| 502 |
require.NoError(t, err) |
| 503 |
storage := filesystem.NewStorage(dotFS, cache.NewObjectLRUDefault()) |
| 504 |
|
| 505 |
r, err := Init(storage, wtFS) |
| 506 |
require.NoError(t, err) |
| 507 |
|
| 508 |
w, err := r.Worktree() |
| 509 |
require.NoError(t, err) |
| 510 |
|
| 511 |
require.NoError(t, w.Filesystem.Symlink(tc.target, "link")) |
| 512 |
_, err = w.Add("link") |
| 513 |
require.NoError(t, err) |
| 514 |
_, err = w.Commit("add symlink", &CommitOptions{Author: defaultSignature()}) |
| 515 |
require.NoError(t, err) |
| 516 |
|
| 517 |
require.NoError(t, r.Storer.SetIndex(&index.Index{Version: 2})) |
| 518 |
w.Filesystem = newWorktreeFilesystem( |
| 519 |
osfs.New(filepath.Join(dir, "worktree-empty")), true, true) |
| 520 |
|
| 521 |
require.NoError(t, w.Checkout(&CheckoutOptions{})) |
| 522 |
|
| 523 |
_, err = w.Status() |
| 524 |
require.NoError(t, err) |
| 525 |
|
| 526 |
got, err := w.Filesystem.Readlink("link") |
| 527 |
require.NoError(t, err) |
| 528 |
assert.Equal(t, tc.target, got) |
| 529 |
}) |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
func (s *WorktreeSuite) TestCheckoutSparse(c *C) { |
| 534 |
fs := memfs.New() |
| 535 |
r, err := Clone(memory.NewStorage(), fs, &CloneOptions{ |
| 536 |
URL: s.GetBasicLocalRepositoryURL(), |
| 537 |
NoCheckout: true, |
| 538 |
}) |
| 539 |
c.Assert(err, IsNil) |
| 540 |
|
| 541 |
w, err := r.Worktree() |
| 542 |
c.Assert(err, IsNil) |
| 543 |
|
| 544 |
sparseCheckoutDirectories := []string{"go", "json", "php"} |
| 545 |
c.Assert(w.Checkout(&CheckoutOptions{ |
| 546 |
SparseCheckoutDirectories: sparseCheckoutDirectories, |
| 547 |
}), IsNil) |
| 548 |
|
| 549 |
fis, err := fs.ReadDir("/") |
| 550 |
c.Assert(err, IsNil) |
| 551 |
|
| 552 |
for _, fi := range fis { |
| 553 |
c.Assert(fi.IsDir(), Equals, true) |
| 554 |
var oneOfSparseCheckoutDirs bool |
| 555 |
|
| 556 |
for _, sparseCheckoutDirectory := range sparseCheckoutDirectories { |
| 557 |
if strings.HasPrefix(fi.Name(), sparseCheckoutDirectory) { |
| 558 |
oneOfSparseCheckoutDirs = true |
| 559 |
} |
| 560 |
} |
| 561 |
c.Assert(oneOfSparseCheckoutDirs, Equals, true) |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
func (s *WorktreeSuite) TestFilenameNormalization(c *C) { |
| 566 |
if runtime.GOOS == "windows" { |
| 567 |
c.Skip("windows paths may contain non utf-8 sequences") |
| 568 |
} |
| 569 |
|
| 570 |
url := c.MkDir() |
| 571 |
|
| 572 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 573 |
|
| 574 |
server, err := PlainClone(url, false, &CloneOptions{ |
| 575 |
URL: path, |
| 576 |
}) |
| 577 |
c.Assert(err, IsNil) |
| 578 |
|
| 579 |
filename := "페" |
| 580 |
|
| 581 |
w, err := server.Worktree() |
| 582 |
c.Assert(err, IsNil) |
| 583 |
|
| 584 |
writeFile := func(path string) { |
| 585 |
err := util.WriteFile(w.Filesystem, path, []byte("foo"), 0755) |
| 586 |
c.Assert(err, IsNil) |
| 587 |
} |
| 588 |
|
| 589 |
writeFile(filename) |
| 590 |
origHash, err := w.Add(filename) |
| 591 |
c.Assert(err, IsNil) |
| 592 |
_, err = w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 593 |
c.Assert(err, IsNil) |
| 594 |
|
| 595 |
r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ |
| 596 |
URL: url, |
| 597 |
}) |
| 598 |
c.Assert(err, IsNil) |
| 599 |
|
| 600 |
w, err = r.Worktree() |
| 601 |
c.Assert(err, IsNil) |
| 602 |
|
| 603 |
status, err := w.Status() |
| 604 |
c.Assert(err, IsNil) |
| 605 |
c.Assert(status.IsClean(), Equals, true) |
| 606 |
|
| 607 |
err = w.Filesystem.Remove(filename) |
| 608 |
c.Assert(err, IsNil) |
| 609 |
|
| 610 |
modFilename := norm.NFKD.String(filename) |
| 611 |
writeFile(modFilename) |
| 612 |
|
| 613 |
_, err = w.Add(filename) |
| 614 |
c.Assert(err, IsNil) |
| 615 |
modHash, err := w.Add(modFilename) |
| 616 |
c.Assert(err, IsNil) |
| 617 |
// At this point we've got two files with the same content. |
| 618 |
// Hence their hashes must be the same. |
| 619 |
c.Assert(origHash == modHash, Equals, true) |
| 620 |
|
| 621 |
status, err = w.Status() |
| 622 |
c.Assert(err, IsNil) |
| 623 |
// However, their names are different and the work tree is still dirty. |
| 624 |
c.Assert(status.IsClean(), Equals, false) |
| 625 |
|
| 626 |
// Revert back the deletion of the first file. |
| 627 |
writeFile(filename) |
| 628 |
_, err = w.Add(filename) |
| 629 |
c.Assert(err, IsNil) |
| 630 |
|
| 631 |
status, err = w.Status() |
| 632 |
c.Assert(err, IsNil) |
| 633 |
// Still dirty - the second file is added. |
| 634 |
c.Assert(status.IsClean(), Equals, false) |
| 635 |
|
| 636 |
_, err = w.Remove(modFilename) |
| 637 |
c.Assert(err, IsNil) |
| 638 |
|
| 639 |
status, err = w.Status() |
| 640 |
c.Assert(err, IsNil) |
| 641 |
c.Assert(status.IsClean(), Equals, true) |
| 642 |
} |
| 643 |
|
| 644 |
func (s *WorktreeSuite) TestCheckoutSubmodule(c *C) { |
| 645 |
url := "https://github.com/git-fixtures/submodule.git" |
| 646 |
r := s.NewRepositoryWithEmptyWorktree(fixtures.ByURL(url).One()) |
| 647 |
|
| 648 |
w, err := r.Worktree() |
| 649 |
c.Assert(err, IsNil) |
| 650 |
|
| 651 |
err = w.Checkout(&CheckoutOptions{}) |
| 652 |
c.Assert(err, IsNil) |
| 653 |
|
| 654 |
status, err := w.Status() |
| 655 |
c.Assert(err, IsNil) |
| 656 |
c.Assert(status.IsClean(), Equals, true) |
| 657 |
} |
| 658 |
|
| 659 |
func (s *WorktreeSuite) TestCheckoutSubmoduleInitialized(c *C) { |
| 660 |
url := "https://github.com/git-fixtures/submodule.git" |
| 661 |
r := s.NewRepository(fixtures.ByURL(url).One()) |
| 662 |
|
| 663 |
w, err := r.Worktree() |
| 664 |
c.Assert(err, IsNil) |
| 665 |
|
| 666 |
sub, err := w.Submodules() |
| 667 |
c.Assert(err, IsNil) |
| 668 |
|
| 669 |
err = sub.Update(&SubmoduleUpdateOptions{Init: true}) |
| 670 |
c.Assert(err, IsNil) |
| 671 |
|
| 672 |
status, err := w.Status() |
| 673 |
c.Assert(err, IsNil) |
| 674 |
c.Assert(status.IsClean(), Equals, true) |
| 675 |
} |
| 676 |
|
| 677 |
func (s *WorktreeSuite) TestCheckoutRelativePathSubmoduleInitialized(c *C) { |
| 678 |
url := "https://github.com/git-fixtures/submodule.git" |
| 679 |
r := s.NewRepository(fixtures.ByURL(url).One()) |
| 680 |
|
| 681 |
// modify the .gitmodules from original one |
| 682 |
file, err := r.wt.OpenFile(".gitmodules", os.O_WRONLY|os.O_TRUNC, 0666) |
| 683 |
c.Assert(err, IsNil) |
| 684 |
|
| 685 |
n, err := io.WriteString(file, `[submodule "basic"] |
| 686 |
path = basic |
| 687 |
url = ../basic.git |
| 688 |
[submodule "itself"] |
| 689 |
path = itself |
| 690 |
url = ../submodule.git`) |
| 691 |
c.Assert(err, IsNil) |
| 692 |
c.Assert(n, Not(Equals), 0) |
| 693 |
|
| 694 |
w, err := r.Worktree() |
| 695 |
c.Assert(err, IsNil) |
| 696 |
|
| 697 |
w.Add(".gitmodules") |
| 698 |
w.Commit("test", &CommitOptions{}) |
| 699 |
|
| 700 |
// test submodule path |
| 701 |
modules, err := w.readGitmodulesFile() |
| 702 |
c.Assert(err, IsNil) |
| 703 |
|
| 704 |
c.Assert(modules.Submodules["basic"].URL, Equals, "../basic.git") |
| 705 |
c.Assert(modules.Submodules["itself"].URL, Equals, "../submodule.git") |
| 706 |
|
| 707 |
basicSubmodule, err := w.Submodule("basic") |
| 708 |
c.Assert(err, IsNil) |
| 709 |
basicRepo, err := basicSubmodule.Repository() |
| 710 |
c.Assert(err, IsNil) |
| 711 |
basicRemotes, err := basicRepo.Remotes() |
| 712 |
c.Assert(err, IsNil) |
| 713 |
c.Assert(basicRemotes[0].Config().URLs[0], Equals, "https://github.com/git-fixtures/basic.git") |
| 714 |
|
| 715 |
itselfSubmodule, err := w.Submodule("itself") |
| 716 |
c.Assert(err, IsNil) |
| 717 |
itselfRepo, err := itselfSubmodule.Repository() |
| 718 |
c.Assert(err, IsNil) |
| 719 |
itselfRemotes, err := itselfRepo.Remotes() |
| 720 |
c.Assert(err, IsNil) |
| 721 |
c.Assert(itselfRemotes[0].Config().URLs[0], Equals, "https://github.com/git-fixtures/submodule.git") |
| 722 |
|
| 723 |
sub, err := w.Submodules() |
| 724 |
c.Assert(err, IsNil) |
| 725 |
|
| 726 |
err = sub.Update(&SubmoduleUpdateOptions{Init: true, RecurseSubmodules: DefaultSubmoduleRecursionDepth}) |
| 727 |
c.Assert(err, IsNil) |
| 728 |
|
| 729 |
status, err := w.Status() |
| 730 |
c.Assert(err, IsNil) |
| 731 |
c.Assert(status.IsClean(), Equals, true) |
| 732 |
} |
| 733 |
|
| 734 |
func (s *WorktreeSuite) TestCheckoutIndexMem(c *C) { |
| 735 |
fs := memfs.New() |
| 736 |
w := &Worktree{ |
| 737 |
r: s.Repository, |
| 738 |
Filesystem: fs, |
| 739 |
} |
| 740 |
|
| 741 |
err := w.Checkout(&CheckoutOptions{}) |
| 742 |
c.Assert(err, IsNil) |
| 743 |
|
| 744 |
idx, err := s.Repository.Storer.Index() |
| 745 |
c.Assert(err, IsNil) |
| 746 |
c.Assert(idx.Entries, HasLen, 9) |
| 747 |
c.Assert(idx.Entries[0].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88") |
| 748 |
c.Assert(idx.Entries[0].Name, Equals, ".gitignore") |
| 749 |
c.Assert(idx.Entries[0].Mode, Equals, filemode.Regular) |
| 750 |
c.Assert(idx.Entries[0].ModifiedAt.IsZero(), Equals, false) |
| 751 |
c.Assert(idx.Entries[0].Size, Equals, uint32(189)) |
| 752 |
|
| 753 |
// ctime, dev, inode, uid and gid are not supported on memfs fs |
| 754 |
c.Assert(idx.Entries[0].CreatedAt.IsZero(), Equals, true) |
| 755 |
c.Assert(idx.Entries[0].Dev, Equals, uint32(0)) |
| 756 |
c.Assert(idx.Entries[0].Inode, Equals, uint32(0)) |
| 757 |
c.Assert(idx.Entries[0].UID, Equals, uint32(0)) |
| 758 |
c.Assert(idx.Entries[0].GID, Equals, uint32(0)) |
| 759 |
} |
| 760 |
|
| 761 |
func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) { |
| 762 |
fs := s.TemporalFilesystem(c) |
| 763 |
|
| 764 |
w := &Worktree{ |
| 765 |
r: s.Repository, |
| 766 |
Filesystem: fs, |
| 767 |
} |
| 768 |
|
| 769 |
err := w.Checkout(&CheckoutOptions{}) |
| 770 |
c.Assert(err, IsNil) |
| 771 |
|
| 772 |
idx, err := s.Repository.Storer.Index() |
| 773 |
c.Assert(err, IsNil) |
| 774 |
c.Assert(idx.Entries, HasLen, 9) |
| 775 |
c.Assert(idx.Entries[0].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88") |
| 776 |
c.Assert(idx.Entries[0].Name, Equals, ".gitignore") |
| 777 |
c.Assert(idx.Entries[0].Mode, Equals, filemode.Regular) |
| 778 |
c.Assert(idx.Entries[0].ModifiedAt.IsZero(), Equals, false) |
| 779 |
c.Assert(idx.Entries[0].Size, Equals, uint32(189)) |
| 780 |
|
| 781 |
c.Assert(idx.Entries[0].CreatedAt.IsZero(), Equals, false) |
| 782 |
if runtime.GOOS != "windows" { |
| 783 |
c.Assert(idx.Entries[0].Dev, Not(Equals), uint32(0)) |
| 784 |
c.Assert(idx.Entries[0].Inode, Not(Equals), uint32(0)) |
| 785 |
c.Assert(idx.Entries[0].UID, Not(Equals), uint32(0)) |
| 786 |
c.Assert(idx.Entries[0].GID, Not(Equals), uint32(0)) |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
func (s *WorktreeSuite) TestCheckoutBranch(c *C) { |
| 791 |
w := &Worktree{ |
| 792 |
r: s.Repository, |
| 793 |
Filesystem: memfs.New(), |
| 794 |
} |
| 795 |
|
| 796 |
err := w.Checkout(&CheckoutOptions{ |
| 797 |
Branch: "refs/heads/branch", |
| 798 |
}) |
| 799 |
c.Assert(err, IsNil) |
| 800 |
|
| 801 |
head, err := w.r.Head() |
| 802 |
c.Assert(err, IsNil) |
| 803 |
c.Assert(head.Name().String(), Equals, "refs/heads/branch") |
| 804 |
|
| 805 |
status, err := w.Status() |
| 806 |
c.Assert(err, IsNil) |
| 807 |
c.Assert(status.IsClean(), Equals, true) |
| 808 |
} |
| 809 |
|
| 810 |
func (s *WorktreeSuite) TestCheckoutBranchUntracked(c *C) { |
| 811 |
w := &Worktree{ |
| 812 |
r: s.Repository, |
| 813 |
Filesystem: memfs.New(), |
| 814 |
} |
| 815 |
|
| 816 |
uf, err := w.Filesystem.Create("untracked_file") |
| 817 |
c.Assert(err, IsNil) |
| 818 |
_, err = uf.Write([]byte("don't delete me")) |
| 819 |
c.Assert(err, IsNil) |
| 820 |
|
| 821 |
err = w.Checkout(&CheckoutOptions{ |
| 822 |
Branch: "refs/heads/branch", |
| 823 |
}) |
| 824 |
c.Assert(err, IsNil) |
| 825 |
|
| 826 |
head, err := w.r.Head() |
| 827 |
c.Assert(err, IsNil) |
| 828 |
c.Assert(head.Name().String(), Equals, "refs/heads/branch") |
| 829 |
|
| 830 |
status, err := w.Status() |
| 831 |
c.Assert(err, IsNil) |
| 832 |
// The untracked file should still be there, so it's not clean |
| 833 |
c.Assert(status.IsClean(), Equals, false) |
| 834 |
c.Assert(status.IsUntracked("untracked_file"), Equals, true) |
| 835 |
err = w.Filesystem.Remove("untracked_file") |
| 836 |
c.Assert(err, IsNil) |
| 837 |
status, err = w.Status() |
| 838 |
c.Assert(err, IsNil) |
| 839 |
// After deleting the untracked file it should now be clean |
| 840 |
c.Assert(status.IsClean(), Equals, true) |
| 841 |
} |
| 842 |
|
| 843 |
func (s *WorktreeSuite) TestCheckoutCreateWithHash(c *C) { |
| 844 |
w := &Worktree{ |
| 845 |
r: s.Repository, |
| 846 |
Filesystem: memfs.New(), |
| 847 |
} |
| 848 |
|
| 849 |
err := w.Checkout(&CheckoutOptions{ |
| 850 |
Create: true, |
| 851 |
Branch: "refs/heads/foo", |
| 852 |
Hash: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"), |
| 853 |
}) |
| 854 |
c.Assert(err, IsNil) |
| 855 |
|
| 856 |
head, err := w.r.Head() |
| 857 |
c.Assert(err, IsNil) |
| 858 |
c.Assert(head.Name().String(), Equals, "refs/heads/foo") |
| 859 |
c.Assert(head.Hash(), Equals, plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9")) |
| 860 |
|
| 861 |
status, err := w.Status() |
| 862 |
c.Assert(err, IsNil) |
| 863 |
c.Assert(status.IsClean(), Equals, true) |
| 864 |
} |
| 865 |
|
| 866 |
func (s *WorktreeSuite) TestCheckoutCreate(c *C) { |
| 867 |
w := &Worktree{ |
| 868 |
r: s.Repository, |
| 869 |
Filesystem: memfs.New(), |
| 870 |
} |
| 871 |
|
| 872 |
err := w.Checkout(&CheckoutOptions{ |
| 873 |
Create: true, |
| 874 |
Branch: "refs/heads/foo", |
| 875 |
}) |
| 876 |
c.Assert(err, IsNil) |
| 877 |
|
| 878 |
head, err := w.r.Head() |
| 879 |
c.Assert(err, IsNil) |
| 880 |
c.Assert(head.Name().String(), Equals, "refs/heads/foo") |
| 881 |
c.Assert(head.Hash(), Equals, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 882 |
|
| 883 |
status, err := w.Status() |
| 884 |
c.Assert(err, IsNil) |
| 885 |
c.Assert(status.IsClean(), Equals, true) |
| 886 |
} |
| 887 |
|
| 888 |
func (s *WorktreeSuite) TestCheckoutBranchAndHash(c *C) { |
| 889 |
w := &Worktree{ |
| 890 |
r: s.Repository, |
| 891 |
Filesystem: memfs.New(), |
| 892 |
} |
| 893 |
|
| 894 |
err := w.Checkout(&CheckoutOptions{ |
| 895 |
Branch: "refs/heads/foo", |
| 896 |
Hash: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"), |
| 897 |
}) |
| 898 |
|
| 899 |
c.Assert(err, Equals, ErrBranchHashExclusive) |
| 900 |
} |
| 901 |
|
| 902 |
func (s *WorktreeSuite) TestCheckoutCreateMissingBranch(c *C) { |
| 903 |
w := &Worktree{ |
| 904 |
r: s.Repository, |
| 905 |
Filesystem: memfs.New(), |
| 906 |
} |
| 907 |
|
| 908 |
err := w.Checkout(&CheckoutOptions{ |
| 909 |
Create: true, |
| 910 |
}) |
| 911 |
|
| 912 |
c.Assert(err, Equals, ErrCreateRequiresBranch) |
| 913 |
} |
| 914 |
|
| 915 |
func (s *WorktreeSuite) TestCheckoutCreateInvalidBranch(c *C) { |
| 916 |
w := &Worktree{ |
| 917 |
r: s.Repository, |
| 918 |
Filesystem: memfs.New(), |
| 919 |
} |
| 920 |
|
| 921 |
for _, name := range []plumbing.ReferenceName{ |
| 922 |
"foo", |
| 923 |
"-", |
| 924 |
"-foo", |
| 925 |
"refs/heads//", |
| 926 |
"refs/heads/..", |
| 927 |
"refs/heads/a..b", |
| 928 |
"refs/heads/.", |
| 929 |
} { |
| 930 |
err := w.Checkout(&CheckoutOptions{ |
| 931 |
Create: true, |
| 932 |
Branch: name, |
| 933 |
}) |
| 934 |
|
| 935 |
c.Assert(err, Equals, plumbing.ErrInvalidReferenceName) |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
func (s *WorktreeSuite) TestCheckoutTag(c *C) { |
| 940 |
f := fixtures.ByTag("tags").One() |
| 941 |
r := s.NewRepositoryWithEmptyWorktree(f) |
| 942 |
w, err := r.Worktree() |
| 943 |
c.Assert(err, IsNil) |
| 944 |
|
| 945 |
err = w.Checkout(&CheckoutOptions{}) |
| 946 |
c.Assert(err, IsNil) |
| 947 |
head, err := w.r.Head() |
| 948 |
c.Assert(err, IsNil) |
| 949 |
c.Assert(head.Name().String(), Equals, "refs/heads/master") |
| 950 |
|
| 951 |
status, err := w.Status() |
| 952 |
c.Assert(err, IsNil) |
| 953 |
c.Assert(status.IsClean(), Equals, true) |
| 954 |
|
| 955 |
err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/lightweight-tag"}) |
| 956 |
c.Assert(err, IsNil) |
| 957 |
head, err = w.r.Head() |
| 958 |
c.Assert(err, IsNil) |
| 959 |
c.Assert(head.Name().String(), Equals, "HEAD") |
| 960 |
c.Assert(head.Hash().String(), Equals, "f7b877701fbf855b44c0a9e86f3fdce2c298b07f") |
| 961 |
|
| 962 |
err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/commit-tag"}) |
| 963 |
c.Assert(err, IsNil) |
| 964 |
head, err = w.r.Head() |
| 965 |
c.Assert(err, IsNil) |
| 966 |
c.Assert(head.Name().String(), Equals, "HEAD") |
| 967 |
c.Assert(head.Hash().String(), Equals, "f7b877701fbf855b44c0a9e86f3fdce2c298b07f") |
| 968 |
|
| 969 |
err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/tree-tag"}) |
| 970 |
c.Assert(err, NotNil) |
| 971 |
head, err = w.r.Head() |
| 972 |
c.Assert(err, IsNil) |
| 973 |
c.Assert(head.Name().String(), Equals, "HEAD") |
| 974 |
} |
| 975 |
|
| 976 |
func (s *WorktreeSuite) TestCheckoutTagHash(c *C) { |
| 977 |
f := fixtures.ByTag("tags").One() |
| 978 |
r := s.NewRepositoryWithEmptyWorktree(f) |
| 979 |
w, err := r.Worktree() |
| 980 |
c.Assert(err, IsNil) |
| 981 |
|
| 982 |
for _, hash := range []string{ |
| 983 |
"b742a2a9fa0afcfa9a6fad080980fbc26b007c69", // annotated tag |
| 984 |
"ad7897c0fb8e7d9a9ba41fa66072cf06095a6cfc", // commit tag |
| 985 |
"f7b877701fbf855b44c0a9e86f3fdce2c298b07f", // lightweight tag |
| 986 |
} { |
| 987 |
err = w.Checkout(&CheckoutOptions{ |
| 988 |
Hash: plumbing.NewHash(hash), |
| 989 |
}) |
| 990 |
c.Assert(err, IsNil) |
| 991 |
head, err := w.r.Head() |
| 992 |
c.Assert(err, IsNil) |
| 993 |
c.Assert(head.Name().String(), Equals, "HEAD") |
| 994 |
|
| 995 |
status, err := w.Status() |
| 996 |
c.Assert(err, IsNil) |
| 997 |
c.Assert(status.IsClean(), Equals, true) |
| 998 |
} |
| 999 |
|
| 1000 |
for _, hash := range []string{ |
| 1001 |
"fe6cb94756faa81e5ed9240f9191b833db5f40ae", // blob tag |
| 1002 |
"152175bf7e5580299fa1f0ba41ef6474cc043b70", // tree tag |
| 1003 |
} { |
| 1004 |
err = w.Checkout(&CheckoutOptions{ |
| 1005 |
Hash: plumbing.NewHash(hash), |
| 1006 |
}) |
| 1007 |
c.Assert(err, NotNil) |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
func (s *WorktreeSuite) TestCheckoutBisect(c *C) { |
| 1012 |
if testing.Short() { |
| 1013 |
c.Skip("skipping test in short mode.") |
| 1014 |
} |
| 1015 |
|
| 1016 |
s.testCheckoutBisect(c, "https://github.com/src-d/go-git.git") |
| 1017 |
} |
| 1018 |
|
| 1019 |
func (s *WorktreeSuite) TestCheckoutBisectSubmodules(c *C) { |
| 1020 |
s.testCheckoutBisect(c, "https://github.com/git-fixtures/submodule.git") |
| 1021 |
} |
| 1022 |
|
| 1023 |
// TestCheckoutBisect simulates a git bisect going through the git history and |
| 1024 |
// checking every commit over the previous commit |
| 1025 |
func (s *WorktreeSuite) testCheckoutBisect(c *C, url string) { |
| 1026 |
f := fixtures.ByURL(url).One() |
| 1027 |
r := s.NewRepositoryWithEmptyWorktree(f) |
| 1028 |
|
| 1029 |
w, err := r.Worktree() |
| 1030 |
c.Assert(err, IsNil) |
| 1031 |
|
| 1032 |
iter, err := w.r.Log(&LogOptions{}) |
| 1033 |
c.Assert(err, IsNil) |
| 1034 |
|
| 1035 |
iter.ForEach(func(commit *object.Commit) error { |
| 1036 |
err := w.Checkout(&CheckoutOptions{Hash: commit.Hash}) |
| 1037 |
c.Assert(err, IsNil) |
| 1038 |
|
| 1039 |
status, err := w.Status() |
| 1040 |
c.Assert(err, IsNil) |
| 1041 |
c.Assert(status.IsClean(), Equals, true) |
| 1042 |
|
| 1043 |
return nil |
| 1044 |
}) |
| 1045 |
} |
| 1046 |
|
| 1047 |
func (s *WorktreeSuite) TestStatus(c *C) { |
| 1048 |
fs := memfs.New() |
| 1049 |
w := &Worktree{ |
| 1050 |
r: s.Repository, |
| 1051 |
Filesystem: fs, |
| 1052 |
} |
| 1053 |
|
| 1054 |
status, err := w.Status() |
| 1055 |
c.Assert(err, IsNil) |
| 1056 |
|
| 1057 |
c.Assert(status.IsClean(), Equals, false) |
| 1058 |
c.Assert(status, HasLen, 9) |
| 1059 |
} |
| 1060 |
|
| 1061 |
func (s *WorktreeSuite) TestStatusEmpty(c *C) { |
| 1062 |
fs := memfs.New() |
| 1063 |
storage := memory.NewStorage() |
| 1064 |
|
| 1065 |
r, err := Init(storage, fs) |
| 1066 |
c.Assert(err, IsNil) |
| 1067 |
|
| 1068 |
w, err := r.Worktree() |
| 1069 |
c.Assert(err, IsNil) |
| 1070 |
|
| 1071 |
status, err := w.Status() |
| 1072 |
c.Assert(err, IsNil) |
| 1073 |
c.Assert(status.IsClean(), Equals, true) |
| 1074 |
c.Assert(status, NotNil) |
| 1075 |
} |
| 1076 |
|
| 1077 |
func (s *WorktreeSuite) TestStatusCheckedInBeforeIgnored(c *C) { |
| 1078 |
fs := memfs.New() |
| 1079 |
storage := memory.NewStorage() |
| 1080 |
|
| 1081 |
r, err := Init(storage, fs) |
| 1082 |
c.Assert(err, IsNil) |
| 1083 |
|
| 1084 |
w, err := r.Worktree() |
| 1085 |
c.Assert(err, IsNil) |
| 1086 |
|
| 1087 |
err = util.WriteFile(fs, "fileToIgnore", []byte("Initial data"), 0755) |
| 1088 |
c.Assert(err, IsNil) |
| 1089 |
_, err = w.Add("fileToIgnore") |
| 1090 |
c.Assert(err, IsNil) |
| 1091 |
|
| 1092 |
_, err = w.Commit("Added file that will be ignored later", defaultTestCommitOptions()) |
| 1093 |
c.Assert(err, IsNil) |
| 1094 |
|
| 1095 |
err = util.WriteFile(fs, ".gitignore", []byte("fileToIgnore\nsecondIgnoredFile"), 0755) |
| 1096 |
c.Assert(err, IsNil) |
| 1097 |
_, err = w.Add(".gitignore") |
| 1098 |
c.Assert(err, IsNil) |
| 1099 |
_, err = w.Commit("Added .gitignore", defaultTestCommitOptions()) |
| 1100 |
c.Assert(err, IsNil) |
| 1101 |
status, err := w.Status() |
| 1102 |
c.Assert(err, IsNil) |
| 1103 |
c.Assert(status.IsClean(), Equals, true) |
| 1104 |
c.Assert(status, NotNil) |
| 1105 |
|
| 1106 |
err = util.WriteFile(fs, "secondIgnoredFile", []byte("Should be completely ignored"), 0755) |
| 1107 |
c.Assert(err, IsNil) |
| 1108 |
status = nil |
| 1109 |
status, err = w.Status() |
| 1110 |
c.Assert(err, IsNil) |
| 1111 |
c.Assert(status.IsClean(), Equals, true) |
| 1112 |
c.Assert(status, NotNil) |
| 1113 |
|
| 1114 |
err = util.WriteFile(fs, "fileToIgnore", []byte("Updated data"), 0755) |
| 1115 |
c.Assert(err, IsNil) |
| 1116 |
status = nil |
| 1117 |
status, err = w.Status() |
| 1118 |
c.Assert(err, IsNil) |
| 1119 |
c.Assert(status.IsClean(), Equals, false) |
| 1120 |
c.Assert(status, NotNil) |
| 1121 |
} |
| 1122 |
|
| 1123 |
func (s *WorktreeSuite) TestStatusEmptyDirty(c *C) { |
| 1124 |
fs := memfs.New() |
| 1125 |
err := util.WriteFile(fs, "foo", []byte("foo"), 0755) |
| 1126 |
c.Assert(err, IsNil) |
| 1127 |
|
| 1128 |
storage := memory.NewStorage() |
| 1129 |
|
| 1130 |
r, err := Init(storage, fs) |
| 1131 |
c.Assert(err, IsNil) |
| 1132 |
|
| 1133 |
w, err := r.Worktree() |
| 1134 |
c.Assert(err, IsNil) |
| 1135 |
|
| 1136 |
status, err := w.Status() |
| 1137 |
c.Assert(err, IsNil) |
| 1138 |
c.Assert(status.IsClean(), Equals, false) |
| 1139 |
c.Assert(status, HasLen, 1) |
| 1140 |
} |
| 1141 |
|
| 1142 |
func (s *WorktreeSuite) TestStatusUnmodified(c *C) { |
| 1143 |
fs := memfs.New() |
| 1144 |
w := &Worktree{ |
| 1145 |
r: s.Repository, |
| 1146 |
Filesystem: fs, |
| 1147 |
} |
| 1148 |
|
| 1149 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1150 |
c.Assert(err, IsNil) |
| 1151 |
|
| 1152 |
status, err := w.StatusWithOptions(StatusOptions{Strategy: Preload}) |
| 1153 |
c.Assert(err, IsNil) |
| 1154 |
c.Assert(status.IsClean(), Equals, true) |
| 1155 |
c.Assert(status.IsUntracked("LICENSE"), Equals, false) |
| 1156 |
|
| 1157 |
c.Assert(status.File("LICENSE").Staging, Equals, Unmodified) |
| 1158 |
c.Assert(status.File("LICENSE").Worktree, Equals, Unmodified) |
| 1159 |
|
| 1160 |
status, err = w.StatusWithOptions(StatusOptions{Strategy: Empty}) |
| 1161 |
c.Assert(err, IsNil) |
| 1162 |
c.Assert(status.IsClean(), Equals, true) |
| 1163 |
c.Assert(status.IsUntracked("LICENSE"), Equals, false) |
| 1164 |
|
| 1165 |
c.Assert(status.File("LICENSE").Staging, Equals, Untracked) |
| 1166 |
c.Assert(status.File("LICENSE").Worktree, Equals, Untracked) |
| 1167 |
} |
| 1168 |
|
| 1169 |
func (s *WorktreeSuite) TestReset(c *C) { |
| 1170 |
fs := memfs.New() |
| 1171 |
w := &Worktree{ |
| 1172 |
r: s.Repository, |
| 1173 |
Filesystem: fs, |
| 1174 |
} |
| 1175 |
|
| 1176 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 1177 |
|
| 1178 |
err := w.Checkout(&CheckoutOptions{}) |
| 1179 |
c.Assert(err, IsNil) |
| 1180 |
|
| 1181 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 1182 |
c.Assert(err, IsNil) |
| 1183 |
c.Assert(branch.Hash(), Not(Equals), commit) |
| 1184 |
|
| 1185 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commit}) |
| 1186 |
c.Assert(err, IsNil) |
| 1187 |
|
| 1188 |
branch, err = w.r.Reference(plumbing.Master, false) |
| 1189 |
c.Assert(err, IsNil) |
| 1190 |
c.Assert(branch.Hash(), Equals, commit) |
| 1191 |
|
| 1192 |
status, err := w.Status() |
| 1193 |
c.Assert(err, IsNil) |
| 1194 |
c.Assert(status.IsClean(), Equals, true) |
| 1195 |
} |
| 1196 |
|
| 1197 |
func (s *WorktreeSuite) TestResetWithUntracked(c *C) { |
| 1198 |
fs := memfs.New() |
| 1199 |
w := &Worktree{ |
| 1200 |
r: s.Repository, |
| 1201 |
Filesystem: fs, |
| 1202 |
} |
| 1203 |
|
| 1204 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 1205 |
|
| 1206 |
err := w.Checkout(&CheckoutOptions{}) |
| 1207 |
c.Assert(err, IsNil) |
| 1208 |
|
| 1209 |
err = util.WriteFile(fs, "foo", nil, 0755) |
| 1210 |
c.Assert(err, IsNil) |
| 1211 |
|
| 1212 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commit}) |
| 1213 |
c.Assert(err, IsNil) |
| 1214 |
|
| 1215 |
status, err := w.Status() |
| 1216 |
c.Assert(err, IsNil) |
| 1217 |
for file, st := range status { |
| 1218 |
if file == "foo" { |
| 1219 |
c.Assert(st.Worktree, Equals, Untracked) |
| 1220 |
c.Assert(st.Staging, Equals, Untracked) |
| 1221 |
continue |
| 1222 |
} |
| 1223 |
if st.Worktree != Unmodified || st.Staging != Unmodified { |
| 1224 |
c.Errorf("file %s not unmodified", file) |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
func (s *WorktreeSuite) TestResetSoft(c *C) { |
| 1230 |
fs := memfs.New() |
| 1231 |
w := &Worktree{ |
| 1232 |
r: s.Repository, |
| 1233 |
Filesystem: fs, |
| 1234 |
} |
| 1235 |
|
| 1236 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 1237 |
|
| 1238 |
err := w.Checkout(&CheckoutOptions{}) |
| 1239 |
c.Assert(err, IsNil) |
| 1240 |
|
| 1241 |
err = w.Reset(&ResetOptions{Mode: SoftReset, Commit: commit}) |
| 1242 |
c.Assert(err, IsNil) |
| 1243 |
|
| 1244 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 1245 |
c.Assert(err, IsNil) |
| 1246 |
c.Assert(branch.Hash(), Equals, commit) |
| 1247 |
|
| 1248 |
status, err := w.Status() |
| 1249 |
c.Assert(err, IsNil) |
| 1250 |
c.Assert(status.IsClean(), Equals, false) |
| 1251 |
c.Assert(status.File("CHANGELOG").Staging, Equals, Added) |
| 1252 |
} |
| 1253 |
|
| 1254 |
func (s *WorktreeSuite) TestResetMixed(c *C) { |
| 1255 |
fs := memfs.New() |
| 1256 |
w := &Worktree{ |
| 1257 |
r: s.Repository, |
| 1258 |
Filesystem: fs, |
| 1259 |
} |
| 1260 |
|
| 1261 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 1262 |
|
| 1263 |
err := w.Checkout(&CheckoutOptions{}) |
| 1264 |
c.Assert(err, IsNil) |
| 1265 |
|
| 1266 |
err = w.Reset(&ResetOptions{Mode: MixedReset, Commit: commit}) |
| 1267 |
c.Assert(err, IsNil) |
| 1268 |
|
| 1269 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 1270 |
c.Assert(err, IsNil) |
| 1271 |
c.Assert(branch.Hash(), Equals, commit) |
| 1272 |
|
| 1273 |
status, err := w.Status() |
| 1274 |
c.Assert(err, IsNil) |
| 1275 |
c.Assert(status.IsClean(), Equals, false) |
| 1276 |
c.Assert(status.File("CHANGELOG").Staging, Equals, Untracked) |
| 1277 |
} |
| 1278 |
|
| 1279 |
func (s *WorktreeSuite) TestResetMerge(c *C) { |
| 1280 |
fs := memfs.New() |
| 1281 |
w := &Worktree{ |
| 1282 |
r: s.Repository, |
| 1283 |
Filesystem: fs, |
| 1284 |
} |
| 1285 |
|
| 1286 |
commitA := plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294") |
| 1287 |
commitB := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 1288 |
|
| 1289 |
err := w.Checkout(&CheckoutOptions{}) |
| 1290 |
c.Assert(err, IsNil) |
| 1291 |
|
| 1292 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commitA}) |
| 1293 |
c.Assert(err, IsNil) |
| 1294 |
|
| 1295 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 1296 |
c.Assert(err, IsNil) |
| 1297 |
c.Assert(branch.Hash(), Equals, commitA) |
| 1298 |
|
| 1299 |
f, err := fs.Create(".gitignore") |
| 1300 |
c.Assert(err, IsNil) |
| 1301 |
_, err = f.Write([]byte("foo")) |
| 1302 |
c.Assert(err, IsNil) |
| 1303 |
err = f.Close() |
| 1304 |
c.Assert(err, IsNil) |
| 1305 |
|
| 1306 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commitB}) |
| 1307 |
c.Assert(err, Equals, ErrUnstagedChanges) |
| 1308 |
|
| 1309 |
branch, err = w.r.Reference(plumbing.Master, false) |
| 1310 |
c.Assert(err, IsNil) |
| 1311 |
c.Assert(branch.Hash(), Equals, commitA) |
| 1312 |
} |
| 1313 |
|
| 1314 |
func (s *WorktreeSuite) TestResetHard(c *C) { |
| 1315 |
fs := memfs.New() |
| 1316 |
w := &Worktree{ |
| 1317 |
r: s.Repository, |
| 1318 |
Filesystem: fs, |
| 1319 |
} |
| 1320 |
|
| 1321 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 1322 |
|
| 1323 |
err := w.Checkout(&CheckoutOptions{}) |
| 1324 |
c.Assert(err, IsNil) |
| 1325 |
|
| 1326 |
f, err := fs.Create(".gitignore") |
| 1327 |
c.Assert(err, IsNil) |
| 1328 |
_, err = f.Write([]byte("foo")) |
| 1329 |
c.Assert(err, IsNil) |
| 1330 |
err = f.Close() |
| 1331 |
c.Assert(err, IsNil) |
| 1332 |
|
| 1333 |
err = w.Reset(&ResetOptions{Mode: HardReset, Commit: commit}) |
| 1334 |
c.Assert(err, IsNil) |
| 1335 |
|
| 1336 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 1337 |
c.Assert(err, IsNil) |
| 1338 |
c.Assert(branch.Hash(), Equals, commit) |
| 1339 |
} |
| 1340 |
|
| 1341 |
func (s *WorktreeSuite) TestResetHardSubFolders(c *C) { |
| 1342 |
fs := memfs.New() |
| 1343 |
w := &Worktree{ |
| 1344 |
r: s.Repository, |
| 1345 |
Filesystem: fs, |
| 1346 |
} |
| 1347 |
|
| 1348 |
err := w.Checkout(&CheckoutOptions{}) |
| 1349 |
c.Assert(err, IsNil) |
| 1350 |
|
| 1351 |
err = fs.MkdirAll("dir", os.ModePerm) |
| 1352 |
c.Assert(err, IsNil) |
| 1353 |
tf, err := fs.Create("./dir/testfile.txt") |
| 1354 |
c.Assert(err, IsNil) |
| 1355 |
_, err = tf.Write([]byte("testfile content")) |
| 1356 |
c.Assert(err, IsNil) |
| 1357 |
err = tf.Close() |
| 1358 |
c.Assert(err, IsNil) |
| 1359 |
_, err = w.Add("dir/testfile.txt") |
| 1360 |
c.Assert(err, IsNil) |
| 1361 |
_, err = w.Commit("testcommit", &CommitOptions{Author: &object.Signature{Name: "name", Email: "email"}}) |
| 1362 |
c.Assert(err, IsNil) |
| 1363 |
|
| 1364 |
err = fs.Remove("dir/testfile.txt") |
| 1365 |
c.Assert(err, IsNil) |
| 1366 |
|
| 1367 |
status, err := w.Status() |
| 1368 |
c.Assert(err, IsNil) |
| 1369 |
c.Assert(status.IsClean(), Equals, false) |
| 1370 |
|
| 1371 |
err = w.Reset(&ResetOptions{Files: []string{"dir/testfile.txt"}, Mode: HardReset}) |
| 1372 |
c.Assert(err, IsNil) |
| 1373 |
|
| 1374 |
status, err = w.Status() |
| 1375 |
c.Assert(err, IsNil) |
| 1376 |
c.Assert(status.IsClean(), Equals, true) |
| 1377 |
} |
| 1378 |
|
| 1379 |
func (s *WorktreeSuite) TestResetHardWithGitIgnore(c *C) { |
| 1380 |
fs := memfs.New() |
| 1381 |
w := &Worktree{ |
| 1382 |
r: s.Repository, |
| 1383 |
Filesystem: fs, |
| 1384 |
} |
| 1385 |
|
| 1386 |
err := w.Checkout(&CheckoutOptions{}) |
| 1387 |
c.Assert(err, IsNil) |
| 1388 |
|
| 1389 |
tf, err := fs.Create("newTestFile.txt") |
| 1390 |
c.Assert(err, IsNil) |
| 1391 |
_, err = tf.Write([]byte("testfile content")) |
| 1392 |
c.Assert(err, IsNil) |
| 1393 |
err = tf.Close() |
| 1394 |
c.Assert(err, IsNil) |
| 1395 |
_, err = w.Add("newTestFile.txt") |
| 1396 |
c.Assert(err, IsNil) |
| 1397 |
_, err = w.Commit("testcommit", &CommitOptions{Author: &object.Signature{Name: "name", Email: "email"}}) |
| 1398 |
c.Assert(err, IsNil) |
| 1399 |
|
| 1400 |
err = fs.Remove("newTestFile.txt") |
| 1401 |
c.Assert(err, IsNil) |
| 1402 |
f, err := fs.Create(".gitignore") |
| 1403 |
c.Assert(err, IsNil) |
| 1404 |
_, err = f.Write([]byte("foo\n")) |
| 1405 |
c.Assert(err, IsNil) |
| 1406 |
_, err = f.Write([]byte("newTestFile.txt\n")) |
| 1407 |
c.Assert(err, IsNil) |
| 1408 |
err = f.Close() |
| 1409 |
c.Assert(err, IsNil) |
| 1410 |
|
| 1411 |
status, err := w.Status() |
| 1412 |
c.Assert(err, IsNil) |
| 1413 |
c.Assert(status.IsClean(), Equals, false) |
| 1414 |
|
| 1415 |
err = w.Reset(&ResetOptions{Mode: HardReset}) |
| 1416 |
c.Assert(err, IsNil) |
| 1417 |
|
| 1418 |
status, err = w.Status() |
| 1419 |
c.Assert(err, IsNil) |
| 1420 |
c.Assert(status.IsClean(), Equals, true) |
| 1421 |
} |
| 1422 |
|
| 1423 |
func (s *WorktreeSuite) TestResetSparsely(c *C) { |
| 1424 |
fs := memfs.New() |
| 1425 |
w := &Worktree{ |
| 1426 |
r: s.Repository, |
| 1427 |
Filesystem: fs, |
| 1428 |
} |
| 1429 |
|
| 1430 |
sparseResetDirs := []string{"php"} |
| 1431 |
|
| 1432 |
err := w.ResetSparsely(&ResetOptions{Mode: HardReset}, sparseResetDirs) |
| 1433 |
c.Assert(err, IsNil) |
| 1434 |
|
| 1435 |
files, err := fs.ReadDir("/") |
| 1436 |
c.Assert(err, IsNil) |
| 1437 |
c.Assert(files, HasLen, 1) |
| 1438 |
c.Assert(files[0].Name(), Equals, "php") |
| 1439 |
|
| 1440 |
files, err = fs.ReadDir("/php") |
| 1441 |
c.Assert(err, IsNil) |
| 1442 |
c.Assert(files, HasLen, 1) |
| 1443 |
c.Assert(files[0].Name(), Equals, "crappy.php") |
| 1444 |
} |
| 1445 |
|
| 1446 |
func (s *WorktreeSuite) TestStatusAfterCheckout(c *C) { |
| 1447 |
fs := memfs.New() |
| 1448 |
w := &Worktree{ |
| 1449 |
r: s.Repository, |
| 1450 |
Filesystem: fs, |
| 1451 |
} |
| 1452 |
|
| 1453 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1454 |
c.Assert(err, IsNil) |
| 1455 |
|
| 1456 |
status, err := w.Status() |
| 1457 |
c.Assert(err, IsNil) |
| 1458 |
c.Assert(status.IsClean(), Equals, true) |
| 1459 |
} |
| 1460 |
|
| 1461 |
func (s *WorktreeSuite) TestStatusAfterSparseCheckout(c *C) { |
| 1462 |
fs := memfs.New() |
| 1463 |
w := &Worktree{ |
| 1464 |
r: s.Repository, |
| 1465 |
Filesystem: fs, |
| 1466 |
} |
| 1467 |
|
| 1468 |
err := w.Checkout(&CheckoutOptions{ |
| 1469 |
SparseCheckoutDirectories: []string{"php"}, |
| 1470 |
Force: true, |
| 1471 |
}) |
| 1472 |
c.Assert(err, IsNil) |
| 1473 |
|
| 1474 |
status, err := w.Status() |
| 1475 |
c.Assert(err, IsNil) |
| 1476 |
c.Assert(status.IsClean(), Equals, true) |
| 1477 |
} |
| 1478 |
|
| 1479 |
func (s *WorktreeSuite) TestStatusModified(c *C) { |
| 1480 |
fs := s.TemporalFilesystem(c) |
| 1481 |
|
| 1482 |
w := &Worktree{ |
| 1483 |
r: s.Repository, |
| 1484 |
Filesystem: fs, |
| 1485 |
} |
| 1486 |
|
| 1487 |
err := w.Checkout(&CheckoutOptions{}) |
| 1488 |
c.Assert(err, IsNil) |
| 1489 |
|
| 1490 |
f, err := fs.Create(".gitignore") |
| 1491 |
c.Assert(err, IsNil) |
| 1492 |
_, err = f.Write([]byte("foo")) |
| 1493 |
c.Assert(err, IsNil) |
| 1494 |
err = f.Close() |
| 1495 |
c.Assert(err, IsNil) |
| 1496 |
|
| 1497 |
status, err := w.Status() |
| 1498 |
c.Assert(err, IsNil) |
| 1499 |
c.Assert(status.IsClean(), Equals, false) |
| 1500 |
c.Assert(status.File(".gitignore").Worktree, Equals, Modified) |
| 1501 |
} |
| 1502 |
|
| 1503 |
func (s *WorktreeSuite) TestStatusIgnored(c *C) { |
| 1504 |
fs := memfs.New() |
| 1505 |
w := &Worktree{ |
| 1506 |
r: s.Repository, |
| 1507 |
Filesystem: fs, |
| 1508 |
} |
| 1509 |
|
| 1510 |
w.Checkout(&CheckoutOptions{}) |
| 1511 |
|
| 1512 |
fs.MkdirAll("another", os.ModePerm) |
| 1513 |
f, _ := fs.Create("another/file") |
| 1514 |
f.Close() |
| 1515 |
fs.MkdirAll("vendor/github.com", os.ModePerm) |
| 1516 |
f, _ = fs.Create("vendor/github.com/file") |
| 1517 |
f.Close() |
| 1518 |
fs.MkdirAll("vendor/gopkg.in", os.ModePerm) |
| 1519 |
f, _ = fs.Create("vendor/gopkg.in/file") |
| 1520 |
f.Close() |
| 1521 |
|
| 1522 |
status, _ := w.Status() |
| 1523 |
c.Assert(len(status), Equals, 3) |
| 1524 |
_, ok := status["another/file"] |
| 1525 |
c.Assert(ok, Equals, true) |
| 1526 |
_, ok = status["vendor/github.com/file"] |
| 1527 |
c.Assert(ok, Equals, true) |
| 1528 |
_, ok = status["vendor/gopkg.in/file"] |
| 1529 |
c.Assert(ok, Equals, true) |
| 1530 |
|
| 1531 |
f, _ = fs.Create(".gitignore") |
| 1532 |
f.Write([]byte("vendor/g*/")) |
| 1533 |
f.Close() |
| 1534 |
f, _ = fs.Create("vendor/.gitignore") |
| 1535 |
f.Write([]byte("!github.com/\n")) |
| 1536 |
f.Close() |
| 1537 |
|
| 1538 |
status, _ = w.Status() |
| 1539 |
c.Assert(len(status), Equals, 4) |
| 1540 |
_, ok = status[".gitignore"] |
| 1541 |
c.Assert(ok, Equals, true) |
| 1542 |
_, ok = status["another/file"] |
| 1543 |
c.Assert(ok, Equals, true) |
| 1544 |
_, ok = status["vendor/.gitignore"] |
| 1545 |
c.Assert(ok, Equals, true) |
| 1546 |
_, ok = status["vendor/github.com/file"] |
| 1547 |
c.Assert(ok, Equals, true) |
| 1548 |
} |
| 1549 |
|
| 1550 |
func (s *WorktreeSuite) TestStatusUntracked(c *C) { |
| 1551 |
fs := memfs.New() |
| 1552 |
w := &Worktree{ |
| 1553 |
r: s.Repository, |
| 1554 |
Filesystem: fs, |
| 1555 |
} |
| 1556 |
|
| 1557 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1558 |
c.Assert(err, IsNil) |
| 1559 |
|
| 1560 |
f, err := w.Filesystem.Create("foo") |
| 1561 |
c.Assert(err, IsNil) |
| 1562 |
c.Assert(f.Close(), IsNil) |
| 1563 |
|
| 1564 |
status, err := w.Status() |
| 1565 |
c.Assert(err, IsNil) |
| 1566 |
c.Assert(status.File("foo").Staging, Equals, Untracked) |
| 1567 |
c.Assert(status.File("foo").Worktree, Equals, Untracked) |
| 1568 |
} |
| 1569 |
|
| 1570 |
func (s *WorktreeSuite) TestStatusDeleted(c *C) { |
| 1571 |
fs := s.TemporalFilesystem(c) |
| 1572 |
|
| 1573 |
w := &Worktree{ |
| 1574 |
r: s.Repository, |
| 1575 |
Filesystem: fs, |
| 1576 |
} |
| 1577 |
|
| 1578 |
err := w.Checkout(&CheckoutOptions{}) |
| 1579 |
c.Assert(err, IsNil) |
| 1580 |
|
| 1581 |
err = fs.Remove(".gitignore") |
| 1582 |
c.Assert(err, IsNil) |
| 1583 |
|
| 1584 |
status, err := w.Status() |
| 1585 |
c.Assert(err, IsNil) |
| 1586 |
c.Assert(status.IsClean(), Equals, false) |
| 1587 |
c.Assert(status.File(".gitignore").Worktree, Equals, Deleted) |
| 1588 |
} |
| 1589 |
|
| 1590 |
func (s *WorktreeSuite) TestSubmodule(c *C) { |
| 1591 |
path := fixtures.ByTag("submodule").One().Worktree().Root() |
| 1592 |
r, err := PlainOpen(path) |
| 1593 |
c.Assert(err, IsNil) |
| 1594 |
|
| 1595 |
w, err := r.Worktree() |
| 1596 |
c.Assert(err, IsNil) |
| 1597 |
|
| 1598 |
m, err := w.Submodule("basic") |
| 1599 |
c.Assert(err, IsNil) |
| 1600 |
|
| 1601 |
c.Assert(m.Config().Name, Equals, "basic") |
| 1602 |
} |
| 1603 |
|
| 1604 |
func (s *WorktreeSuite) TestSubmodules(c *C) { |
| 1605 |
path := fixtures.ByTag("submodule").One().Worktree().Root() |
| 1606 |
r, err := PlainOpen(path) |
| 1607 |
c.Assert(err, IsNil) |
| 1608 |
|
| 1609 |
w, err := r.Worktree() |
| 1610 |
c.Assert(err, IsNil) |
| 1611 |
|
| 1612 |
l, err := w.Submodules() |
| 1613 |
c.Assert(err, IsNil) |
| 1614 |
|
| 1615 |
c.Assert(l, HasLen, 2) |
| 1616 |
} |
| 1617 |
|
| 1618 |
func (s *WorktreeSuite) TestAddUntracked(c *C) { |
| 1619 |
fs := memfs.New() |
| 1620 |
w := &Worktree{ |
| 1621 |
r: s.Repository, |
| 1622 |
Filesystem: fs, |
| 1623 |
} |
| 1624 |
|
| 1625 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1626 |
c.Assert(err, IsNil) |
| 1627 |
|
| 1628 |
idx, err := w.r.Storer.Index() |
| 1629 |
c.Assert(err, IsNil) |
| 1630 |
c.Assert(idx.Entries, HasLen, 9) |
| 1631 |
|
| 1632 |
err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) |
| 1633 |
c.Assert(err, IsNil) |
| 1634 |
|
| 1635 |
hash, err := w.Add("foo") |
| 1636 |
c.Assert(hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5") |
| 1637 |
c.Assert(err, IsNil) |
| 1638 |
|
| 1639 |
idx, err = w.r.Storer.Index() |
| 1640 |
c.Assert(err, IsNil) |
| 1641 |
c.Assert(idx.Entries, HasLen, 10) |
| 1642 |
|
| 1643 |
e, err := idx.Entry("foo") |
| 1644 |
c.Assert(err, IsNil) |
| 1645 |
c.Assert(e.Hash, Equals, hash) |
| 1646 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 1647 |
|
| 1648 |
status, err := w.Status() |
| 1649 |
c.Assert(err, IsNil) |
| 1650 |
c.Assert(status, HasLen, 1) |
| 1651 |
|
| 1652 |
file := status.File("foo") |
| 1653 |
c.Assert(file.Staging, Equals, Added) |
| 1654 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1655 |
|
| 1656 |
obj, err := w.r.Storer.EncodedObject(plumbing.BlobObject, hash) |
| 1657 |
c.Assert(err, IsNil) |
| 1658 |
c.Assert(obj, NotNil) |
| 1659 |
c.Assert(obj.Size(), Equals, int64(3)) |
| 1660 |
} |
| 1661 |
|
| 1662 |
func (s *WorktreeSuite) TestIgnored(c *C) { |
| 1663 |
fs := memfs.New() |
| 1664 |
w := &Worktree{ |
| 1665 |
r: s.Repository, |
| 1666 |
Filesystem: fs, |
| 1667 |
} |
| 1668 |
|
| 1669 |
w.Excludes = make([]gitignore.Pattern, 0) |
| 1670 |
w.Excludes = append(w.Excludes, gitignore.ParsePattern("foo", nil)) |
| 1671 |
|
| 1672 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1673 |
c.Assert(err, IsNil) |
| 1674 |
|
| 1675 |
idx, err := w.r.Storer.Index() |
| 1676 |
c.Assert(err, IsNil) |
| 1677 |
c.Assert(idx.Entries, HasLen, 9) |
| 1678 |
|
| 1679 |
err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) |
| 1680 |
c.Assert(err, IsNil) |
| 1681 |
|
| 1682 |
status, err := w.Status() |
| 1683 |
c.Assert(err, IsNil) |
| 1684 |
c.Assert(status, HasLen, 0) |
| 1685 |
|
| 1686 |
file := status.File("foo") |
| 1687 |
c.Assert(file.Staging, Equals, Untracked) |
| 1688 |
c.Assert(file.Worktree, Equals, Untracked) |
| 1689 |
} |
| 1690 |
|
| 1691 |
func (s *WorktreeSuite) TestExcludedNoGitignore(c *C) { |
| 1692 |
f := fixtures.ByTag("empty").One() |
| 1693 |
r := s.NewRepository(f) |
| 1694 |
|
| 1695 |
fs := memfs.New() |
| 1696 |
w := &Worktree{ |
| 1697 |
r: r, |
| 1698 |
Filesystem: fs, |
| 1699 |
} |
| 1700 |
|
| 1701 |
_, err := fs.Open(".gitignore") |
| 1702 |
c.Assert(err, Equals, os.ErrNotExist) |
| 1703 |
|
| 1704 |
w.Excludes = make([]gitignore.Pattern, 0) |
| 1705 |
w.Excludes = append(w.Excludes, gitignore.ParsePattern("foo", nil)) |
| 1706 |
|
| 1707 |
err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) |
| 1708 |
c.Assert(err, IsNil) |
| 1709 |
|
| 1710 |
status, err := w.Status() |
| 1711 |
c.Assert(err, IsNil) |
| 1712 |
c.Assert(status, HasLen, 0) |
| 1713 |
|
| 1714 |
file := status.File("foo") |
| 1715 |
c.Assert(file.Staging, Equals, Untracked) |
| 1716 |
c.Assert(file.Worktree, Equals, Untracked) |
| 1717 |
} |
| 1718 |
|
| 1719 |
func (s *WorktreeSuite) TestAddModified(c *C) { |
| 1720 |
fs := memfs.New() |
| 1721 |
w := &Worktree{ |
| 1722 |
r: s.Repository, |
| 1723 |
Filesystem: fs, |
| 1724 |
} |
| 1725 |
|
| 1726 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1727 |
c.Assert(err, IsNil) |
| 1728 |
|
| 1729 |
idx, err := w.r.Storer.Index() |
| 1730 |
c.Assert(err, IsNil) |
| 1731 |
c.Assert(idx.Entries, HasLen, 9) |
| 1732 |
|
| 1733 |
err = util.WriteFile(w.Filesystem, "LICENSE", []byte("FOO"), 0644) |
| 1734 |
c.Assert(err, IsNil) |
| 1735 |
|
| 1736 |
hash, err := w.Add("LICENSE") |
| 1737 |
c.Assert(err, IsNil) |
| 1738 |
c.Assert(hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5") |
| 1739 |
|
| 1740 |
idx, err = w.r.Storer.Index() |
| 1741 |
c.Assert(err, IsNil) |
| 1742 |
c.Assert(idx.Entries, HasLen, 9) |
| 1743 |
|
| 1744 |
e, err := idx.Entry("LICENSE") |
| 1745 |
c.Assert(err, IsNil) |
| 1746 |
c.Assert(e.Hash, Equals, hash) |
| 1747 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1748 |
|
| 1749 |
status, err := w.Status() |
| 1750 |
c.Assert(err, IsNil) |
| 1751 |
c.Assert(status, HasLen, 1) |
| 1752 |
|
| 1753 |
file := status.File("LICENSE") |
| 1754 |
c.Assert(file.Staging, Equals, Modified) |
| 1755 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1756 |
} |
| 1757 |
|
| 1758 |
func (s *WorktreeSuite) TestAddUnmodified(c *C) { |
| 1759 |
fs := memfs.New() |
| 1760 |
w := &Worktree{ |
| 1761 |
r: s.Repository, |
| 1762 |
Filesystem: fs, |
| 1763 |
} |
| 1764 |
|
| 1765 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1766 |
c.Assert(err, IsNil) |
| 1767 |
|
| 1768 |
hash, err := w.Add("LICENSE") |
| 1769 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 1770 |
c.Assert(err, IsNil) |
| 1771 |
} |
| 1772 |
|
| 1773 |
func (s *WorktreeSuite) TestAddRemoved(c *C) { |
| 1774 |
fs := memfs.New() |
| 1775 |
w := &Worktree{ |
| 1776 |
r: s.Repository, |
| 1777 |
Filesystem: fs, |
| 1778 |
} |
| 1779 |
|
| 1780 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1781 |
c.Assert(err, IsNil) |
| 1782 |
|
| 1783 |
idx, err := w.r.Storer.Index() |
| 1784 |
c.Assert(err, IsNil) |
| 1785 |
c.Assert(idx.Entries, HasLen, 9) |
| 1786 |
|
| 1787 |
err = w.Filesystem.Remove("LICENSE") |
| 1788 |
c.Assert(err, IsNil) |
| 1789 |
|
| 1790 |
hash, err := w.Add("LICENSE") |
| 1791 |
c.Assert(err, IsNil) |
| 1792 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 1793 |
|
| 1794 |
e, err := idx.Entry("LICENSE") |
| 1795 |
c.Assert(err, IsNil) |
| 1796 |
c.Assert(e.Hash, Equals, hash) |
| 1797 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1798 |
|
| 1799 |
status, err := w.Status() |
| 1800 |
c.Assert(err, IsNil) |
| 1801 |
c.Assert(status, HasLen, 1) |
| 1802 |
|
| 1803 |
file := status.File("LICENSE") |
| 1804 |
c.Assert(file.Staging, Equals, Deleted) |
| 1805 |
} |
| 1806 |
|
| 1807 |
func (s *WorktreeSuite) TestAddRemovedInDirectory(c *C) { |
| 1808 |
fs := memfs.New() |
| 1809 |
w := &Worktree{ |
| 1810 |
r: s.Repository, |
| 1811 |
Filesystem: fs, |
| 1812 |
} |
| 1813 |
|
| 1814 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1815 |
c.Assert(err, IsNil) |
| 1816 |
|
| 1817 |
idx, err := w.r.Storer.Index() |
| 1818 |
c.Assert(err, IsNil) |
| 1819 |
c.Assert(idx.Entries, HasLen, 9) |
| 1820 |
|
| 1821 |
err = w.Filesystem.Remove("go/example.go") |
| 1822 |
c.Assert(err, IsNil) |
| 1823 |
|
| 1824 |
err = w.Filesystem.Remove("json/short.json") |
| 1825 |
c.Assert(err, IsNil) |
| 1826 |
|
| 1827 |
hash, err := w.Add("go") |
| 1828 |
c.Assert(err, IsNil) |
| 1829 |
c.Assert(hash.IsZero(), Equals, true) |
| 1830 |
|
| 1831 |
e, err := idx.Entry("go/example.go") |
| 1832 |
c.Assert(err, IsNil) |
| 1833 |
c.Assert(e.Hash, Equals, plumbing.NewHash("880cd14280f4b9b6ed3986d6671f907d7cc2a198")) |
| 1834 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1835 |
|
| 1836 |
e, err = idx.Entry("json/short.json") |
| 1837 |
c.Assert(err, IsNil) |
| 1838 |
c.Assert(e.Hash, Equals, plumbing.NewHash("c8f1d8c61f9da76f4cb49fd86322b6e685dba956")) |
| 1839 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1840 |
|
| 1841 |
status, err := w.Status() |
| 1842 |
c.Assert(err, IsNil) |
| 1843 |
c.Assert(status, HasLen, 2) |
| 1844 |
|
| 1845 |
file := status.File("go/example.go") |
| 1846 |
c.Assert(file.Staging, Equals, Deleted) |
| 1847 |
|
| 1848 |
file = status.File("json/short.json") |
| 1849 |
c.Assert(file.Staging, Equals, Unmodified) |
| 1850 |
} |
| 1851 |
|
| 1852 |
func (s *WorktreeSuite) TestAddRemovedInDirectoryWithTrailingSlash(c *C) { |
| 1853 |
fs := memfs.New() |
| 1854 |
w := &Worktree{ |
| 1855 |
r: s.Repository, |
| 1856 |
Filesystem: fs, |
| 1857 |
} |
| 1858 |
|
| 1859 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1860 |
c.Assert(err, IsNil) |
| 1861 |
|
| 1862 |
idx, err := w.r.Storer.Index() |
| 1863 |
c.Assert(err, IsNil) |
| 1864 |
c.Assert(idx.Entries, HasLen, 9) |
| 1865 |
|
| 1866 |
err = w.Filesystem.Remove("go/example.go") |
| 1867 |
c.Assert(err, IsNil) |
| 1868 |
|
| 1869 |
err = w.Filesystem.Remove("json/short.json") |
| 1870 |
c.Assert(err, IsNil) |
| 1871 |
|
| 1872 |
hash, err := w.Add("go/") |
| 1873 |
c.Assert(err, IsNil) |
| 1874 |
c.Assert(hash.IsZero(), Equals, true) |
| 1875 |
|
| 1876 |
e, err := idx.Entry("go/example.go") |
| 1877 |
c.Assert(err, IsNil) |
| 1878 |
c.Assert(e.Hash, Equals, plumbing.NewHash("880cd14280f4b9b6ed3986d6671f907d7cc2a198")) |
| 1879 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1880 |
|
| 1881 |
e, err = idx.Entry("json/short.json") |
| 1882 |
c.Assert(err, IsNil) |
| 1883 |
c.Assert(e.Hash, Equals, plumbing.NewHash("c8f1d8c61f9da76f4cb49fd86322b6e685dba956")) |
| 1884 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1885 |
|
| 1886 |
status, err := w.Status() |
| 1887 |
c.Assert(err, IsNil) |
| 1888 |
c.Assert(status, HasLen, 2) |
| 1889 |
|
| 1890 |
file := status.File("go/example.go") |
| 1891 |
c.Assert(file.Staging, Equals, Deleted) |
| 1892 |
|
| 1893 |
file = status.File("json/short.json") |
| 1894 |
c.Assert(file.Staging, Equals, Unmodified) |
| 1895 |
} |
| 1896 |
|
| 1897 |
func (s *WorktreeSuite) TestAddRemovedInDirectoryDot(c *C) { |
| 1898 |
fs := memfs.New() |
| 1899 |
w := &Worktree{ |
| 1900 |
r: s.Repository, |
| 1901 |
Filesystem: fs, |
| 1902 |
} |
| 1903 |
|
| 1904 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1905 |
c.Assert(err, IsNil) |
| 1906 |
|
| 1907 |
idx, err := w.r.Storer.Index() |
| 1908 |
c.Assert(err, IsNil) |
| 1909 |
c.Assert(idx.Entries, HasLen, 9) |
| 1910 |
|
| 1911 |
err = w.Filesystem.Remove("go/example.go") |
| 1912 |
c.Assert(err, IsNil) |
| 1913 |
|
| 1914 |
err = w.Filesystem.Remove("json/short.json") |
| 1915 |
c.Assert(err, IsNil) |
| 1916 |
|
| 1917 |
hash, err := w.Add(".") |
| 1918 |
c.Assert(err, IsNil) |
| 1919 |
c.Assert(hash.IsZero(), Equals, true) |
| 1920 |
|
| 1921 |
e, err := idx.Entry("go/example.go") |
| 1922 |
c.Assert(err, IsNil) |
| 1923 |
c.Assert(e.Hash, Equals, plumbing.NewHash("880cd14280f4b9b6ed3986d6671f907d7cc2a198")) |
| 1924 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1925 |
|
| 1926 |
e, err = idx.Entry("json/short.json") |
| 1927 |
c.Assert(err, IsNil) |
| 1928 |
c.Assert(e.Hash, Equals, plumbing.NewHash("c8f1d8c61f9da76f4cb49fd86322b6e685dba956")) |
| 1929 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1930 |
|
| 1931 |
status, err := w.Status() |
| 1932 |
c.Assert(err, IsNil) |
| 1933 |
c.Assert(status, HasLen, 2) |
| 1934 |
|
| 1935 |
file := status.File("go/example.go") |
| 1936 |
c.Assert(file.Staging, Equals, Deleted) |
| 1937 |
|
| 1938 |
file = status.File("json/short.json") |
| 1939 |
c.Assert(file.Staging, Equals, Deleted) |
| 1940 |
} |
| 1941 |
|
| 1942 |
func (s *WorktreeSuite) TestAddSymlink(c *C) { |
| 1943 |
dir := c.MkDir() |
| 1944 |
|
| 1945 |
r, err := PlainInit(dir, false) |
| 1946 |
c.Assert(err, IsNil) |
| 1947 |
err = util.WriteFile(r.wt, "foo", []byte("qux"), 0644) |
| 1948 |
c.Assert(err, IsNil) |
| 1949 |
err = r.wt.Symlink("foo", "bar") |
| 1950 |
c.Assert(err, IsNil) |
| 1951 |
|
| 1952 |
w, err := r.Worktree() |
| 1953 |
c.Assert(err, IsNil) |
| 1954 |
h, err := w.Add("foo") |
| 1955 |
c.Assert(err, IsNil) |
| 1956 |
c.Assert(h, Not(Equals), plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c")) |
| 1957 |
|
| 1958 |
h, err = w.Add("bar") |
| 1959 |
c.Assert(err, IsNil) |
| 1960 |
c.Assert(h, Equals, plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c")) |
| 1961 |
|
| 1962 |
obj, err := w.r.Storer.EncodedObject(plumbing.BlobObject, h) |
| 1963 |
c.Assert(err, IsNil) |
| 1964 |
c.Assert(obj, NotNil) |
| 1965 |
c.Assert(obj.Size(), Equals, int64(3)) |
| 1966 |
} |
| 1967 |
|
| 1968 |
func (s *WorktreeSuite) TestAddDirectory(c *C) { |
| 1969 |
fs := memfs.New() |
| 1970 |
w := &Worktree{ |
| 1971 |
r: s.Repository, |
| 1972 |
Filesystem: fs, |
| 1973 |
} |
| 1974 |
|
| 1975 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1976 |
c.Assert(err, IsNil) |
| 1977 |
|
| 1978 |
idx, err := w.r.Storer.Index() |
| 1979 |
c.Assert(err, IsNil) |
| 1980 |
c.Assert(idx.Entries, HasLen, 9) |
| 1981 |
|
| 1982 |
err = util.WriteFile(w.Filesystem, "qux/foo", []byte("FOO"), 0755) |
| 1983 |
c.Assert(err, IsNil) |
| 1984 |
err = util.WriteFile(w.Filesystem, "qux/baz/bar", []byte("BAR"), 0755) |
| 1985 |
c.Assert(err, IsNil) |
| 1986 |
|
| 1987 |
h, err := w.Add("qux") |
| 1988 |
c.Assert(err, IsNil) |
| 1989 |
c.Assert(h.IsZero(), Equals, true) |
| 1990 |
|
| 1991 |
idx, err = w.r.Storer.Index() |
| 1992 |
c.Assert(err, IsNil) |
| 1993 |
c.Assert(idx.Entries, HasLen, 11) |
| 1994 |
|
| 1995 |
e, err := idx.Entry("qux/foo") |
| 1996 |
c.Assert(err, IsNil) |
| 1997 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 1998 |
|
| 1999 |
e, err = idx.Entry("qux/baz/bar") |
| 2000 |
c.Assert(err, IsNil) |
| 2001 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 2002 |
|
| 2003 |
status, err := w.Status() |
| 2004 |
c.Assert(err, IsNil) |
| 2005 |
c.Assert(status, HasLen, 2) |
| 2006 |
|
| 2007 |
file := status.File("qux/foo") |
| 2008 |
c.Assert(file.Staging, Equals, Added) |
| 2009 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2010 |
|
| 2011 |
file = status.File("qux/baz/bar") |
| 2012 |
c.Assert(file.Staging, Equals, Added) |
| 2013 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2014 |
} |
| 2015 |
|
| 2016 |
func (s *WorktreeSuite) TestAddDirectoryErrorNotFound(c *C) { |
| 2017 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 2018 |
w, _ := r.Worktree() |
| 2019 |
|
| 2020 |
h, err := w.Add("foo") |
| 2021 |
c.Assert(err, NotNil) |
| 2022 |
c.Assert(h.IsZero(), Equals, true) |
| 2023 |
} |
| 2024 |
|
| 2025 |
func (s *WorktreeSuite) TestAddAll(c *C) { |
| 2026 |
fs := memfs.New() |
| 2027 |
w := &Worktree{ |
| 2028 |
r: s.Repository, |
| 2029 |
Filesystem: fs, |
| 2030 |
} |
| 2031 |
|
| 2032 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2033 |
c.Assert(err, IsNil) |
| 2034 |
|
| 2035 |
idx, err := w.r.Storer.Index() |
| 2036 |
c.Assert(err, IsNil) |
| 2037 |
c.Assert(idx.Entries, HasLen, 9) |
| 2038 |
|
| 2039 |
err = util.WriteFile(w.Filesystem, "file1", []byte("file1"), 0644) |
| 2040 |
c.Assert(err, IsNil) |
| 2041 |
|
| 2042 |
err = util.WriteFile(w.Filesystem, "file2", []byte("file2"), 0644) |
| 2043 |
c.Assert(err, IsNil) |
| 2044 |
|
| 2045 |
err = util.WriteFile(w.Filesystem, "file3", []byte("ignore me"), 0644) |
| 2046 |
c.Assert(err, IsNil) |
| 2047 |
|
| 2048 |
w.Excludes = make([]gitignore.Pattern, 0) |
| 2049 |
w.Excludes = append(w.Excludes, gitignore.ParsePattern("file3", nil)) |
| 2050 |
|
| 2051 |
err = w.AddWithOptions(&AddOptions{All: true}) |
| 2052 |
c.Assert(err, IsNil) |
| 2053 |
|
| 2054 |
idx, err = w.r.Storer.Index() |
| 2055 |
c.Assert(err, IsNil) |
| 2056 |
c.Assert(idx.Entries, HasLen, 11) |
| 2057 |
|
| 2058 |
status, err := w.Status() |
| 2059 |
c.Assert(err, IsNil) |
| 2060 |
c.Assert(status, HasLen, 2) |
| 2061 |
|
| 2062 |
file1 := status.File("file1") |
| 2063 |
c.Assert(file1.Staging, Equals, Added) |
| 2064 |
file2 := status.File("file2") |
| 2065 |
c.Assert(file2.Staging, Equals, Added) |
| 2066 |
file3 := status.File("file3") |
| 2067 |
c.Assert(file3.Staging, Equals, Untracked) |
| 2068 |
c.Assert(file3.Worktree, Equals, Untracked) |
| 2069 |
} |
| 2070 |
|
| 2071 |
func (s *WorktreeSuite) TestAddGlob(c *C) { |
| 2072 |
fs := memfs.New() |
| 2073 |
w := &Worktree{ |
| 2074 |
r: s.Repository, |
| 2075 |
Filesystem: fs, |
| 2076 |
} |
| 2077 |
|
| 2078 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2079 |
c.Assert(err, IsNil) |
| 2080 |
|
| 2081 |
idx, err := w.r.Storer.Index() |
| 2082 |
c.Assert(err, IsNil) |
| 2083 |
c.Assert(idx.Entries, HasLen, 9) |
| 2084 |
|
| 2085 |
err = util.WriteFile(w.Filesystem, "qux/qux", []byte("QUX"), 0755) |
| 2086 |
c.Assert(err, IsNil) |
| 2087 |
err = util.WriteFile(w.Filesystem, "qux/baz", []byte("BAZ"), 0755) |
| 2088 |
c.Assert(err, IsNil) |
| 2089 |
err = util.WriteFile(w.Filesystem, "qux/bar/baz", []byte("BAZ"), 0755) |
| 2090 |
c.Assert(err, IsNil) |
| 2091 |
|
| 2092 |
err = w.AddWithOptions(&AddOptions{Glob: w.Filesystem.Join("qux", "b*")}) |
| 2093 |
c.Assert(err, IsNil) |
| 2094 |
|
| 2095 |
idx, err = w.r.Storer.Index() |
| 2096 |
c.Assert(err, IsNil) |
| 2097 |
c.Assert(idx.Entries, HasLen, 11) |
| 2098 |
|
| 2099 |
e, err := idx.Entry("qux/baz") |
| 2100 |
c.Assert(err, IsNil) |
| 2101 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 2102 |
|
| 2103 |
e, err = idx.Entry("qux/bar/baz") |
| 2104 |
c.Assert(err, IsNil) |
| 2105 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 2106 |
|
| 2107 |
status, err := w.Status() |
| 2108 |
c.Assert(err, IsNil) |
| 2109 |
c.Assert(status, HasLen, 3) |
| 2110 |
|
| 2111 |
file := status.File("qux/qux") |
| 2112 |
c.Assert(file.Staging, Equals, Untracked) |
| 2113 |
c.Assert(file.Worktree, Equals, Untracked) |
| 2114 |
|
| 2115 |
file = status.File("qux/baz") |
| 2116 |
c.Assert(file.Staging, Equals, Added) |
| 2117 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2118 |
|
| 2119 |
file = status.File("qux/bar/baz") |
| 2120 |
c.Assert(file.Staging, Equals, Added) |
| 2121 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2122 |
} |
| 2123 |
|
| 2124 |
func (s *WorktreeSuite) TestAddFilenameStartingWithDot(c *C) { |
| 2125 |
fs := memfs.New() |
| 2126 |
w := &Worktree{ |
| 2127 |
r: s.Repository, |
| 2128 |
Filesystem: fs, |
| 2129 |
} |
| 2130 |
|
| 2131 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2132 |
c.Assert(err, IsNil) |
| 2133 |
|
| 2134 |
idx, err := w.r.Storer.Index() |
| 2135 |
c.Assert(err, IsNil) |
| 2136 |
c.Assert(idx.Entries, HasLen, 9) |
| 2137 |
|
| 2138 |
err = util.WriteFile(w.Filesystem, "qux", []byte("QUX"), 0o755) |
| 2139 |
c.Assert(err, IsNil) |
| 2140 |
err = util.WriteFile(w.Filesystem, "baz", []byte("BAZ"), 0o755) |
| 2141 |
c.Assert(err, IsNil) |
| 2142 |
err = util.WriteFile(w.Filesystem, "foo/bar/baz", []byte("BAZ"), 0o755) |
| 2143 |
c.Assert(err, IsNil) |
| 2144 |
|
| 2145 |
_, err = w.Add("./qux") |
| 2146 |
c.Assert(err, IsNil) |
| 2147 |
|
| 2148 |
_, err = w.Add("./baz") |
| 2149 |
c.Assert(err, IsNil) |
| 2150 |
|
| 2151 |
_, err = w.Add("foo/bar/../bar/./baz") |
| 2152 |
c.Assert(err, IsNil) |
| 2153 |
|
| 2154 |
idx, err = w.r.Storer.Index() |
| 2155 |
c.Assert(err, IsNil) |
| 2156 |
c.Assert(idx.Entries, HasLen, 12) |
| 2157 |
|
| 2158 |
e, err := idx.Entry("qux") |
| 2159 |
c.Assert(err, IsNil) |
| 2160 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 2161 |
|
| 2162 |
e, err = idx.Entry("baz") |
| 2163 |
c.Assert(err, IsNil) |
| 2164 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 2165 |
|
| 2166 |
status, err := w.Status() |
| 2167 |
c.Assert(err, IsNil) |
| 2168 |
c.Assert(status, HasLen, 3) |
| 2169 |
|
| 2170 |
file := status.File("qux") |
| 2171 |
c.Assert(file.Staging, Equals, Added) |
| 2172 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2173 |
|
| 2174 |
file = status.File("baz") |
| 2175 |
c.Assert(file.Staging, Equals, Added) |
| 2176 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2177 |
|
| 2178 |
file = status.File("foo/bar/baz") |
| 2179 |
c.Assert(file.Staging, Equals, Added) |
| 2180 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2181 |
} |
| 2182 |
|
| 2183 |
func (s *WorktreeSuite) TestAddGlobErrorNoMatches(c *C) { |
| 2184 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 2185 |
w, _ := r.Worktree() |
| 2186 |
|
| 2187 |
err := w.AddGlob("foo") |
| 2188 |
c.Assert(err, Equals, ErrGlobNoMatches) |
| 2189 |
} |
| 2190 |
|
| 2191 |
func (s *WorktreeSuite) TestAddSkipStatusAddedPath(c *C) { |
| 2192 |
fs := memfs.New() |
| 2193 |
w := &Worktree{ |
| 2194 |
r: s.Repository, |
| 2195 |
Filesystem: fs, |
| 2196 |
} |
| 2197 |
|
| 2198 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2199 |
c.Assert(err, IsNil) |
| 2200 |
|
| 2201 |
idx, err := w.r.Storer.Index() |
| 2202 |
c.Assert(err, IsNil) |
| 2203 |
c.Assert(idx.Entries, HasLen, 9) |
| 2204 |
|
| 2205 |
err = util.WriteFile(w.Filesystem, "file1", []byte("file1"), 0644) |
| 2206 |
c.Assert(err, IsNil) |
| 2207 |
|
| 2208 |
err = w.AddWithOptions(&AddOptions{Path: "file1", SkipStatus: true}) |
| 2209 |
c.Assert(err, IsNil) |
| 2210 |
|
| 2211 |
idx, err = w.r.Storer.Index() |
| 2212 |
c.Assert(err, IsNil) |
| 2213 |
c.Assert(idx.Entries, HasLen, 10) |
| 2214 |
|
| 2215 |
e, err := idx.Entry("file1") |
| 2216 |
c.Assert(err, IsNil) |
| 2217 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 2218 |
|
| 2219 |
status, err := w.Status() |
| 2220 |
c.Assert(err, IsNil) |
| 2221 |
c.Assert(status, HasLen, 1) |
| 2222 |
|
| 2223 |
file := status.File("file1") |
| 2224 |
c.Assert(file.Staging, Equals, Added) |
| 2225 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2226 |
} |
| 2227 |
|
| 2228 |
func (s *WorktreeSuite) TestAddSkipStatusModifiedPath(c *C) { |
| 2229 |
fs := memfs.New() |
| 2230 |
w := &Worktree{ |
| 2231 |
r: s.Repository, |
| 2232 |
Filesystem: fs, |
| 2233 |
} |
| 2234 |
|
| 2235 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2236 |
c.Assert(err, IsNil) |
| 2237 |
|
| 2238 |
idx, err := w.r.Storer.Index() |
| 2239 |
c.Assert(err, IsNil) |
| 2240 |
c.Assert(idx.Entries, HasLen, 9) |
| 2241 |
|
| 2242 |
err = util.WriteFile(w.Filesystem, "LICENSE", []byte("file1"), 0644) |
| 2243 |
c.Assert(err, IsNil) |
| 2244 |
|
| 2245 |
err = w.AddWithOptions(&AddOptions{Path: "LICENSE", SkipStatus: true}) |
| 2246 |
c.Assert(err, IsNil) |
| 2247 |
|
| 2248 |
idx, err = w.r.Storer.Index() |
| 2249 |
c.Assert(err, IsNil) |
| 2250 |
c.Assert(idx.Entries, HasLen, 9) |
| 2251 |
|
| 2252 |
e, err := idx.Entry("LICENSE") |
| 2253 |
c.Assert(err, IsNil) |
| 2254 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 2255 |
|
| 2256 |
status, err := w.Status() |
| 2257 |
c.Assert(err, IsNil) |
| 2258 |
c.Assert(status, HasLen, 1) |
| 2259 |
|
| 2260 |
file := status.File("LICENSE") |
| 2261 |
c.Assert(file.Staging, Equals, Modified) |
| 2262 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2263 |
} |
| 2264 |
|
| 2265 |
func (s *WorktreeSuite) TestAddSkipStatusNonModifiedPath(c *C) { |
| 2266 |
fs := memfs.New() |
| 2267 |
w := &Worktree{ |
| 2268 |
r: s.Repository, |
| 2269 |
Filesystem: fs, |
| 2270 |
} |
| 2271 |
|
| 2272 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2273 |
c.Assert(err, IsNil) |
| 2274 |
|
| 2275 |
idx, err := w.r.Storer.Index() |
| 2276 |
c.Assert(err, IsNil) |
| 2277 |
c.Assert(idx.Entries, HasLen, 9) |
| 2278 |
|
| 2279 |
err = w.AddWithOptions(&AddOptions{Path: "LICENSE", SkipStatus: true}) |
| 2280 |
c.Assert(err, IsNil) |
| 2281 |
|
| 2282 |
idx, err = w.r.Storer.Index() |
| 2283 |
c.Assert(err, IsNil) |
| 2284 |
c.Assert(idx.Entries, HasLen, 9) |
| 2285 |
|
| 2286 |
e, err := idx.Entry("LICENSE") |
| 2287 |
c.Assert(err, IsNil) |
| 2288 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 2289 |
|
| 2290 |
status, err := w.Status() |
| 2291 |
c.Assert(err, IsNil) |
| 2292 |
c.Assert(status, HasLen, 0) |
| 2293 |
|
| 2294 |
file := status.File("LICENSE") |
| 2295 |
c.Assert(file.Staging, Equals, Untracked) |
| 2296 |
c.Assert(file.Worktree, Equals, Untracked) |
| 2297 |
} |
| 2298 |
|
| 2299 |
func (s *WorktreeSuite) TestAddSkipStatusWithIgnoredPath(c *C) { |
| 2300 |
fs := memfs.New() |
| 2301 |
w := &Worktree{ |
| 2302 |
r: s.Repository, |
| 2303 |
Filesystem: fs, |
| 2304 |
} |
| 2305 |
|
| 2306 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2307 |
c.Assert(err, IsNil) |
| 2308 |
|
| 2309 |
idx, err := w.r.Storer.Index() |
| 2310 |
c.Assert(err, IsNil) |
| 2311 |
c.Assert(idx.Entries, HasLen, 9) |
| 2312 |
|
| 2313 |
err = util.WriteFile(fs, ".gitignore", []byte("fileToIgnore\n"), 0755) |
| 2314 |
c.Assert(err, IsNil) |
| 2315 |
_, err = w.Add(".gitignore") |
| 2316 |
c.Assert(err, IsNil) |
| 2317 |
_, err = w.Commit("Added .gitignore", defaultTestCommitOptions()) |
| 2318 |
c.Assert(err, IsNil) |
| 2319 |
|
| 2320 |
err = util.WriteFile(fs, "fileToIgnore", []byte("file to ignore"), 0644) |
| 2321 |
c.Assert(err, IsNil) |
| 2322 |
|
| 2323 |
status, err := w.Status() |
| 2324 |
c.Assert(err, IsNil) |
| 2325 |
c.Assert(status, HasLen, 0) |
| 2326 |
|
| 2327 |
file := status.File("fileToIgnore") |
| 2328 |
c.Assert(file.Staging, Equals, Untracked) |
| 2329 |
c.Assert(file.Worktree, Equals, Untracked) |
| 2330 |
|
| 2331 |
err = w.AddWithOptions(&AddOptions{Path: "fileToIgnore", SkipStatus: true}) |
| 2332 |
c.Assert(err, IsNil) |
| 2333 |
|
| 2334 |
idx, err = w.r.Storer.Index() |
| 2335 |
c.Assert(err, IsNil) |
| 2336 |
c.Assert(idx.Entries, HasLen, 10) |
| 2337 |
|
| 2338 |
e, err := idx.Entry("fileToIgnore") |
| 2339 |
c.Assert(err, IsNil) |
| 2340 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 2341 |
|
| 2342 |
status, err = w.Status() |
| 2343 |
c.Assert(err, IsNil) |
| 2344 |
c.Assert(status, HasLen, 1) |
| 2345 |
|
| 2346 |
file = status.File("fileToIgnore") |
| 2347 |
c.Assert(file.Staging, Equals, Added) |
| 2348 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 2349 |
} |
| 2350 |
|
| 2351 |
func (s *WorktreeSuite) TestRemove(c *C) { |
| 2352 |
fs := memfs.New() |
| 2353 |
w := &Worktree{ |
| 2354 |
r: s.Repository, |
| 2355 |
Filesystem: fs, |
| 2356 |
} |
| 2357 |
|
| 2358 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2359 |
c.Assert(err, IsNil) |
| 2360 |
|
| 2361 |
hash, err := w.Remove("LICENSE") |
| 2362 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 2363 |
c.Assert(err, IsNil) |
| 2364 |
|
| 2365 |
status, err := w.Status() |
| 2366 |
c.Assert(err, IsNil) |
| 2367 |
c.Assert(status, HasLen, 1) |
| 2368 |
c.Assert(status.File("LICENSE").Staging, Equals, Deleted) |
| 2369 |
} |
| 2370 |
|
| 2371 |
func (s *WorktreeSuite) TestRemoveNotExistentEntry(c *C) { |
| 2372 |
fs := memfs.New() |
| 2373 |
w := &Worktree{ |
| 2374 |
r: s.Repository, |
| 2375 |
Filesystem: fs, |
| 2376 |
} |
| 2377 |
|
| 2378 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2379 |
c.Assert(err, IsNil) |
| 2380 |
|
| 2381 |
hash, err := w.Remove("not-exists") |
| 2382 |
c.Assert(hash.IsZero(), Equals, true) |
| 2383 |
c.Assert(err, NotNil) |
| 2384 |
} |
| 2385 |
|
| 2386 |
func (s *WorktreeSuite) TestRemoveDirectory(c *C) { |
| 2387 |
fs := memfs.New() |
| 2388 |
w := &Worktree{ |
| 2389 |
r: s.Repository, |
| 2390 |
Filesystem: fs, |
| 2391 |
} |
| 2392 |
|
| 2393 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2394 |
c.Assert(err, IsNil) |
| 2395 |
|
| 2396 |
hash, err := w.Remove("json") |
| 2397 |
c.Assert(hash.IsZero(), Equals, true) |
| 2398 |
c.Assert(err, IsNil) |
| 2399 |
|
| 2400 |
status, err := w.Status() |
| 2401 |
c.Assert(err, IsNil) |
| 2402 |
c.Assert(status, HasLen, 2) |
| 2403 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 2404 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 2405 |
|
| 2406 |
_, err = w.Filesystem.Stat("json") |
| 2407 |
c.Assert(os.IsNotExist(err), Equals, true) |
| 2408 |
} |
| 2409 |
|
| 2410 |
func (s *WorktreeSuite) TestRemoveDirectoryUntracked(c *C) { |
| 2411 |
fs := memfs.New() |
| 2412 |
w := &Worktree{ |
| 2413 |
r: s.Repository, |
| 2414 |
Filesystem: fs, |
| 2415 |
} |
| 2416 |
|
| 2417 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2418 |
c.Assert(err, IsNil) |
| 2419 |
|
| 2420 |
err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755) |
| 2421 |
c.Assert(err, IsNil) |
| 2422 |
|
| 2423 |
hash, err := w.Remove("json") |
| 2424 |
c.Assert(hash.IsZero(), Equals, true) |
| 2425 |
c.Assert(err, IsNil) |
| 2426 |
|
| 2427 |
status, err := w.Status() |
| 2428 |
c.Assert(err, IsNil) |
| 2429 |
c.Assert(status, HasLen, 3) |
| 2430 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 2431 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 2432 |
c.Assert(status.File("json/foo").Staging, Equals, Untracked) |
| 2433 |
|
| 2434 |
_, err = w.Filesystem.Stat("json") |
| 2435 |
c.Assert(err, IsNil) |
| 2436 |
} |
| 2437 |
|
| 2438 |
func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) { |
| 2439 |
fs := memfs.New() |
| 2440 |
w := &Worktree{ |
| 2441 |
r: s.Repository, |
| 2442 |
Filesystem: fs, |
| 2443 |
} |
| 2444 |
|
| 2445 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2446 |
c.Assert(err, IsNil) |
| 2447 |
|
| 2448 |
err = fs.Remove("LICENSE") |
| 2449 |
c.Assert(err, IsNil) |
| 2450 |
|
| 2451 |
hash, err := w.Remove("LICENSE") |
| 2452 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 2453 |
c.Assert(err, IsNil) |
| 2454 |
|
| 2455 |
status, err := w.Status() |
| 2456 |
c.Assert(err, IsNil) |
| 2457 |
c.Assert(status, HasLen, 1) |
| 2458 |
c.Assert(status.File("LICENSE").Staging, Equals, Deleted) |
| 2459 |
} |
| 2460 |
|
| 2461 |
func (s *WorktreeSuite) TestRemoveGlob(c *C) { |
| 2462 |
fs := memfs.New() |
| 2463 |
w := &Worktree{ |
| 2464 |
r: s.Repository, |
| 2465 |
Filesystem: fs, |
| 2466 |
} |
| 2467 |
|
| 2468 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2469 |
c.Assert(err, IsNil) |
| 2470 |
|
| 2471 |
err = w.RemoveGlob(w.Filesystem.Join("json", "l*")) |
| 2472 |
c.Assert(err, IsNil) |
| 2473 |
|
| 2474 |
status, err := w.Status() |
| 2475 |
c.Assert(err, IsNil) |
| 2476 |
c.Assert(status, HasLen, 1) |
| 2477 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 2478 |
} |
| 2479 |
|
| 2480 |
func (s *WorktreeSuite) TestRemoveGlobDirectory(c *C) { |
| 2481 |
fs := memfs.New() |
| 2482 |
w := &Worktree{ |
| 2483 |
r: s.Repository, |
| 2484 |
Filesystem: fs, |
| 2485 |
} |
| 2486 |
|
| 2487 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2488 |
c.Assert(err, IsNil) |
| 2489 |
|
| 2490 |
err = w.RemoveGlob("js*") |
| 2491 |
c.Assert(err, IsNil) |
| 2492 |
|
| 2493 |
status, err := w.Status() |
| 2494 |
c.Assert(err, IsNil) |
| 2495 |
c.Assert(status, HasLen, 2) |
| 2496 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 2497 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 2498 |
|
| 2499 |
_, err = w.Filesystem.Stat("json") |
| 2500 |
c.Assert(os.IsNotExist(err), Equals, true) |
| 2501 |
} |
| 2502 |
|
| 2503 |
func (s *WorktreeSuite) TestRemoveGlobDirectoryDeleted(c *C) { |
| 2504 |
fs := memfs.New() |
| 2505 |
w := &Worktree{ |
| 2506 |
r: s.Repository, |
| 2507 |
Filesystem: fs, |
| 2508 |
} |
| 2509 |
|
| 2510 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2511 |
c.Assert(err, IsNil) |
| 2512 |
|
| 2513 |
err = fs.Remove("json/short.json") |
| 2514 |
c.Assert(err, IsNil) |
| 2515 |
|
| 2516 |
err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755) |
| 2517 |
c.Assert(err, IsNil) |
| 2518 |
|
| 2519 |
err = w.RemoveGlob("js*") |
| 2520 |
c.Assert(err, IsNil) |
| 2521 |
|
| 2522 |
status, err := w.Status() |
| 2523 |
c.Assert(err, IsNil) |
| 2524 |
c.Assert(status, HasLen, 3) |
| 2525 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 2526 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 2527 |
} |
| 2528 |
|
| 2529 |
func (s *WorktreeSuite) TestMove(c *C) { |
| 2530 |
fs := memfs.New() |
| 2531 |
w := &Worktree{ |
| 2532 |
r: s.Repository, |
| 2533 |
Filesystem: fs, |
| 2534 |
} |
| 2535 |
|
| 2536 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2537 |
c.Assert(err, IsNil) |
| 2538 |
|
| 2539 |
hash, err := w.Move("LICENSE", "foo") |
| 2540 |
c.Check(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 2541 |
c.Assert(err, IsNil) |
| 2542 |
|
| 2543 |
status, err := w.Status() |
| 2544 |
c.Assert(err, IsNil) |
| 2545 |
c.Assert(status, HasLen, 2) |
| 2546 |
c.Assert(status.File("LICENSE").Staging, Equals, Deleted) |
| 2547 |
c.Assert(status.File("foo").Staging, Equals, Added) |
| 2548 |
} |
| 2549 |
|
| 2550 |
func (s *WorktreeSuite) TestMoveNotExistentEntry(c *C) { |
| 2551 |
fs := memfs.New() |
| 2552 |
w := &Worktree{ |
| 2553 |
r: s.Repository, |
| 2554 |
Filesystem: fs, |
| 2555 |
} |
| 2556 |
|
| 2557 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2558 |
c.Assert(err, IsNil) |
| 2559 |
|
| 2560 |
hash, err := w.Move("not-exists", "foo") |
| 2561 |
c.Assert(hash.IsZero(), Equals, true) |
| 2562 |
c.Assert(err, NotNil) |
| 2563 |
} |
| 2564 |
|
| 2565 |
func (s *WorktreeSuite) TestMoveToExistent(c *C) { |
| 2566 |
fs := memfs.New() |
| 2567 |
w := &Worktree{ |
| 2568 |
r: s.Repository, |
| 2569 |
Filesystem: fs, |
| 2570 |
} |
| 2571 |
|
| 2572 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 2573 |
c.Assert(err, IsNil) |
| 2574 |
|
| 2575 |
hash, err := w.Move(".gitignore", "LICENSE") |
| 2576 |
c.Assert(hash.IsZero(), Equals, true) |
| 2577 |
c.Assert(err, Equals, ErrDestinationExists) |
| 2578 |
} |
| 2579 |
|
| 2580 |
func (s *WorktreeSuite) TestClean(c *C) { |
| 2581 |
fs := fixtures.ByTag("dirty").One().Worktree() |
| 2582 |
|
| 2583 |
// Open the repo. |
| 2584 |
fs, err := fs.Chroot("repo") |
| 2585 |
c.Assert(err, IsNil) |
| 2586 |
r, err := PlainOpen(fs.Root()) |
| 2587 |
c.Assert(err, IsNil) |
| 2588 |
|
| 2589 |
wt, err := r.Worktree() |
| 2590 |
c.Assert(err, IsNil) |
| 2591 |
|
| 2592 |
// Status before cleaning. |
| 2593 |
status, err := wt.Status() |
| 2594 |
c.Assert(err, IsNil) |
| 2595 |
c.Assert(len(status), Equals, 2) |
| 2596 |
|
| 2597 |
err = wt.Clean(&CleanOptions{}) |
| 2598 |
c.Assert(err, IsNil) |
| 2599 |
|
| 2600 |
// Status after cleaning. |
| 2601 |
status, err = wt.Status() |
| 2602 |
c.Assert(err, IsNil) |
| 2603 |
|
| 2604 |
c.Assert(len(status), Equals, 1) |
| 2605 |
|
| 2606 |
fi, err := fs.Lstat("pkgA") |
| 2607 |
c.Assert(err, IsNil) |
| 2608 |
c.Assert(fi.IsDir(), Equals, true) |
| 2609 |
|
| 2610 |
// Clean with Dir: true. |
| 2611 |
err = wt.Clean(&CleanOptions{Dir: true}) |
| 2612 |
c.Assert(err, IsNil) |
| 2613 |
|
| 2614 |
status, err = wt.Status() |
| 2615 |
c.Assert(err, IsNil) |
| 2616 |
|
| 2617 |
c.Assert(len(status), Equals, 0) |
| 2618 |
|
| 2619 |
// An empty dir should be deleted, as well. |
| 2620 |
_, err = fs.Lstat("pkgA") |
| 2621 |
c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.") |
| 2622 |
} |
| 2623 |
|
| 2624 |
func (s *WorktreeSuite) TestCleanBare(c *C) { |
| 2625 |
storer := memory.NewStorage() |
| 2626 |
|
| 2627 |
r, err := Init(storer, nil) |
| 2628 |
c.Assert(err, IsNil) |
| 2629 |
c.Assert(r, NotNil) |
| 2630 |
|
| 2631 |
wtfs := memfs.New() |
| 2632 |
|
| 2633 |
err = wtfs.MkdirAll("worktree", os.ModePerm) |
| 2634 |
c.Assert(err, IsNil) |
| 2635 |
|
| 2636 |
wtfs, err = wtfs.Chroot("worktree") |
| 2637 |
c.Assert(err, IsNil) |
| 2638 |
|
| 2639 |
r, err = Open(storer, wtfs) |
| 2640 |
c.Assert(err, IsNil) |
| 2641 |
|
| 2642 |
wt, err := r.Worktree() |
| 2643 |
c.Assert(err, IsNil) |
| 2644 |
|
| 2645 |
_, err = wt.Filesystem.Lstat(".") |
| 2646 |
c.Assert(err, IsNil) |
| 2647 |
|
| 2648 |
// Clean with Dir: true. |
| 2649 |
err = wt.Clean(&CleanOptions{Dir: true}) |
| 2650 |
c.Assert(err, IsNil) |
| 2651 |
|
| 2652 |
// Root worktree directory must remain after cleaning |
| 2653 |
_, err = wt.Filesystem.Lstat(".") |
| 2654 |
c.Assert(err, IsNil) |
| 2655 |
} |
| 2656 |
|
| 2657 |
func TestAlternatesRepo(t *testing.T) { |
| 2658 |
fs := fixtures.ByTag("alternates").One().Worktree() |
| 2659 |
|
| 2660 |
// Open 1st repo. |
| 2661 |
rep1fs, err := fs.Chroot("rep1") |
| 2662 |
assert.NoError(t, err) |
| 2663 |
rep1, err := PlainOpen(rep1fs.Root()) |
| 2664 |
assert.NoError(t, err) |
| 2665 |
|
| 2666 |
// Open 2nd repo. |
| 2667 |
rep2fs, err := fs.Chroot("rep2") |
| 2668 |
assert.NoError(t, err) |
| 2669 |
d, _ := rep2fs.Chroot(GitDirName) |
| 2670 |
storer := filesystem.NewStorageWithOptions(d, |
| 2671 |
cache.NewObjectLRUDefault(), filesystem.Options{ |
| 2672 |
AlternatesFS: fs, |
| 2673 |
}) |
| 2674 |
rep2, err := Open(storer, rep2fs) |
| 2675 |
|
| 2676 |
assert.NoError(t, err) |
| 2677 |
|
| 2678 |
// Get the HEAD commit from the main repo. |
| 2679 |
h, err := rep1.Head() |
| 2680 |
assert.NoError(t, err) |
| 2681 |
commit1, err := rep1.CommitObject(h.Hash()) |
| 2682 |
assert.NoError(t, err) |
| 2683 |
|
| 2684 |
// Get the HEAD commit from the shared repo. |
| 2685 |
h, err = rep2.Head() |
| 2686 |
assert.NoError(t, err) |
| 2687 |
commit2, err := rep2.CommitObject(h.Hash()) |
| 2688 |
assert.NoError(t, err) |
| 2689 |
|
| 2690 |
assert.Equal(t, commit1.String(), commit2.String()) |
| 2691 |
} |
| 2692 |
|
| 2693 |
func (s *WorktreeSuite) TestGrep(c *C) { |
| 2694 |
cases := []struct { |
| 2695 |
name string |
| 2696 |
options GrepOptions |
| 2697 |
wantResult []GrepResult |
| 2698 |
dontWantResult []GrepResult |
| 2699 |
wantError error |
| 2700 |
}{ |
| 2701 |
{ |
| 2702 |
name: "basic word match", |
| 2703 |
options: GrepOptions{ |
| 2704 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 2705 |
}, |
| 2706 |
wantResult: []GrepResult{ |
| 2707 |
{ |
| 2708 |
FileName: "go/example.go", |
| 2709 |
LineNumber: 3, |
| 2710 |
Content: "import (", |
| 2711 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2712 |
}, |
| 2713 |
{ |
| 2714 |
FileName: "vendor/foo.go", |
| 2715 |
LineNumber: 3, |
| 2716 |
Content: "import \"fmt\"", |
| 2717 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2718 |
}, |
| 2719 |
}, |
| 2720 |
}, { |
| 2721 |
name: "case insensitive match", |
| 2722 |
options: GrepOptions{ |
| 2723 |
Patterns: []*regexp.Regexp{regexp.MustCompile(`(?i)IMport`)}, |
| 2724 |
}, |
| 2725 |
wantResult: []GrepResult{ |
| 2726 |
{ |
| 2727 |
FileName: "go/example.go", |
| 2728 |
LineNumber: 3, |
| 2729 |
Content: "import (", |
| 2730 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2731 |
}, |
| 2732 |
{ |
| 2733 |
FileName: "vendor/foo.go", |
| 2734 |
LineNumber: 3, |
| 2735 |
Content: "import \"fmt\"", |
| 2736 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2737 |
}, |
| 2738 |
}, |
| 2739 |
}, { |
| 2740 |
name: "invert match", |
| 2741 |
options: GrepOptions{ |
| 2742 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 2743 |
InvertMatch: true, |
| 2744 |
}, |
| 2745 |
dontWantResult: []GrepResult{ |
| 2746 |
{ |
| 2747 |
FileName: "go/example.go", |
| 2748 |
LineNumber: 3, |
| 2749 |
Content: "import (", |
| 2750 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2751 |
}, |
| 2752 |
{ |
| 2753 |
FileName: "vendor/foo.go", |
| 2754 |
LineNumber: 3, |
| 2755 |
Content: "import \"fmt\"", |
| 2756 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2757 |
}, |
| 2758 |
}, |
| 2759 |
}, { |
| 2760 |
name: "match at a given commit hash", |
| 2761 |
options: GrepOptions{ |
| 2762 |
Patterns: []*regexp.Regexp{regexp.MustCompile("The MIT License")}, |
| 2763 |
CommitHash: plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"), |
| 2764 |
}, |
| 2765 |
wantResult: []GrepResult{ |
| 2766 |
{ |
| 2767 |
FileName: "LICENSE", |
| 2768 |
LineNumber: 1, |
| 2769 |
Content: "The MIT License (MIT)", |
| 2770 |
TreeName: "b029517f6300c2da0f4b651b8642506cd6aaf45d", |
| 2771 |
}, |
| 2772 |
}, |
| 2773 |
dontWantResult: []GrepResult{ |
| 2774 |
{ |
| 2775 |
FileName: "go/example.go", |
| 2776 |
LineNumber: 3, |
| 2777 |
Content: "import (", |
| 2778 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2779 |
}, |
| 2780 |
}, |
| 2781 |
}, { |
| 2782 |
name: "match for a given pathspec", |
| 2783 |
options: GrepOptions{ |
| 2784 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 2785 |
PathSpecs: []*regexp.Regexp{regexp.MustCompile("go/")}, |
| 2786 |
}, |
| 2787 |
wantResult: []GrepResult{ |
| 2788 |
{ |
| 2789 |
FileName: "go/example.go", |
| 2790 |
LineNumber: 3, |
| 2791 |
Content: "import (", |
| 2792 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2793 |
}, |
| 2794 |
}, |
| 2795 |
dontWantResult: []GrepResult{ |
| 2796 |
{ |
| 2797 |
FileName: "vendor/foo.go", |
| 2798 |
LineNumber: 3, |
| 2799 |
Content: "import \"fmt\"", |
| 2800 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2801 |
}, |
| 2802 |
}, |
| 2803 |
}, { |
| 2804 |
name: "match at a given reference name", |
| 2805 |
options: GrepOptions{ |
| 2806 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 2807 |
ReferenceName: "refs/heads/master", |
| 2808 |
}, |
| 2809 |
wantResult: []GrepResult{ |
| 2810 |
{ |
| 2811 |
FileName: "go/example.go", |
| 2812 |
LineNumber: 3, |
| 2813 |
Content: "import (", |
| 2814 |
TreeName: "refs/heads/master", |
| 2815 |
}, |
| 2816 |
}, |
| 2817 |
}, { |
| 2818 |
name: "ambiguous options", |
| 2819 |
options: GrepOptions{ |
| 2820 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 2821 |
CommitHash: plumbing.NewHash("2d55a722f3c3ecc36da919dfd8b6de38352f3507"), |
| 2822 |
ReferenceName: "somereferencename", |
| 2823 |
}, |
| 2824 |
wantError: ErrHashOrReference, |
| 2825 |
}, { |
| 2826 |
name: "multiple patterns", |
| 2827 |
options: GrepOptions{ |
| 2828 |
Patterns: []*regexp.Regexp{ |
| 2829 |
regexp.MustCompile("import"), |
| 2830 |
regexp.MustCompile("License"), |
| 2831 |
}, |
| 2832 |
}, |
| 2833 |
wantResult: []GrepResult{ |
| 2834 |
{ |
| 2835 |
FileName: "go/example.go", |
| 2836 |
LineNumber: 3, |
| 2837 |
Content: "import (", |
| 2838 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2839 |
}, |
| 2840 |
{ |
| 2841 |
FileName: "vendor/foo.go", |
| 2842 |
LineNumber: 3, |
| 2843 |
Content: "import \"fmt\"", |
| 2844 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2845 |
}, |
| 2846 |
{ |
| 2847 |
FileName: "LICENSE", |
| 2848 |
LineNumber: 1, |
| 2849 |
Content: "The MIT License (MIT)", |
| 2850 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2851 |
}, |
| 2852 |
}, |
| 2853 |
}, { |
| 2854 |
name: "multiple pathspecs", |
| 2855 |
options: GrepOptions{ |
| 2856 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 2857 |
PathSpecs: []*regexp.Regexp{ |
| 2858 |
regexp.MustCompile("go/"), |
| 2859 |
regexp.MustCompile("vendor/"), |
| 2860 |
}, |
| 2861 |
}, |
| 2862 |
wantResult: []GrepResult{ |
| 2863 |
{ |
| 2864 |
FileName: "go/example.go", |
| 2865 |
LineNumber: 3, |
| 2866 |
Content: "import (", |
| 2867 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2868 |
}, |
| 2869 |
{ |
| 2870 |
FileName: "vendor/foo.go", |
| 2871 |
LineNumber: 3, |
| 2872 |
Content: "import \"fmt\"", |
| 2873 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2874 |
}, |
| 2875 |
}, |
| 2876 |
}, |
| 2877 |
} |
| 2878 |
|
| 2879 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 2880 |
|
| 2881 |
dir := c.MkDir() |
| 2882 |
|
| 2883 |
server, err := PlainClone(dir, false, &CloneOptions{ |
| 2884 |
URL: path, |
| 2885 |
}) |
| 2886 |
c.Assert(err, IsNil) |
| 2887 |
|
| 2888 |
w, err := server.Worktree() |
| 2889 |
c.Assert(err, IsNil) |
| 2890 |
|
| 2891 |
for _, tc := range cases { |
| 2892 |
gr, err := w.Grep(&tc.options) |
| 2893 |
if tc.wantError != nil { |
| 2894 |
c.Assert(err, Equals, tc.wantError) |
| 2895 |
} else { |
| 2896 |
c.Assert(err, IsNil) |
| 2897 |
} |
| 2898 |
|
| 2899 |
// Iterate through the results and check if the wanted result is present |
| 2900 |
// in the got result. |
| 2901 |
for _, wantResult := range tc.wantResult { |
| 2902 |
found := false |
| 2903 |
for _, gotResult := range gr { |
| 2904 |
if wantResult == gotResult { |
| 2905 |
found = true |
| 2906 |
break |
| 2907 |
} |
| 2908 |
} |
| 2909 |
if !found { |
| 2910 |
c.Errorf("unexpected grep results for %q, expected result to contain: %v", tc.name, wantResult) |
| 2911 |
} |
| 2912 |
} |
| 2913 |
|
| 2914 |
// Iterate through the results and check if the not wanted result is |
| 2915 |
// present in the got result. |
| 2916 |
for _, dontWantResult := range tc.dontWantResult { |
| 2917 |
found := false |
| 2918 |
for _, gotResult := range gr { |
| 2919 |
if dontWantResult == gotResult { |
| 2920 |
found = true |
| 2921 |
break |
| 2922 |
} |
| 2923 |
} |
| 2924 |
if found { |
| 2925 |
c.Errorf("unexpected grep results for %q, expected result to NOT contain: %v", tc.name, dontWantResult) |
| 2926 |
} |
| 2927 |
} |
| 2928 |
} |
| 2929 |
} |
| 2930 |
|
| 2931 |
func (s *WorktreeSuite) TestGrepBare(c *C) { |
| 2932 |
cases := []struct { |
| 2933 |
name string |
| 2934 |
options GrepOptions |
| 2935 |
wantResult []GrepResult |
| 2936 |
dontWantResult []GrepResult |
| 2937 |
wantError error |
| 2938 |
}{ |
| 2939 |
{ |
| 2940 |
name: "basic word match", |
| 2941 |
options: GrepOptions{ |
| 2942 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 2943 |
CommitHash: plumbing.ZeroHash, |
| 2944 |
}, |
| 2945 |
wantResult: []GrepResult{ |
| 2946 |
{ |
| 2947 |
FileName: "go/example.go", |
| 2948 |
LineNumber: 3, |
| 2949 |
Content: "import (", |
| 2950 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2951 |
}, |
| 2952 |
{ |
| 2953 |
FileName: "vendor/foo.go", |
| 2954 |
LineNumber: 3, |
| 2955 |
Content: "import \"fmt\"", |
| 2956 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 2957 |
}, |
| 2958 |
}, |
| 2959 |
}, |
| 2960 |
} |
| 2961 |
|
| 2962 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 2963 |
|
| 2964 |
dir := c.MkDir() |
| 2965 |
|
| 2966 |
r, err := PlainClone(dir, true, &CloneOptions{ |
| 2967 |
URL: path, |
| 2968 |
}) |
| 2969 |
c.Assert(err, IsNil) |
| 2970 |
|
| 2971 |
for _, tc := range cases { |
| 2972 |
gr, err := r.Grep(&tc.options) |
| 2973 |
if tc.wantError != nil { |
| 2974 |
c.Assert(err, Equals, tc.wantError) |
| 2975 |
} else { |
| 2976 |
c.Assert(err, IsNil) |
| 2977 |
} |
| 2978 |
|
| 2979 |
// Iterate through the results and check if the wanted result is present |
| 2980 |
// in the got result. |
| 2981 |
for _, wantResult := range tc.wantResult { |
| 2982 |
found := false |
| 2983 |
for _, gotResult := range gr { |
| 2984 |
if wantResult == gotResult { |
| 2985 |
found = true |
| 2986 |
break |
| 2987 |
} |
| 2988 |
} |
| 2989 |
if !found { |
| 2990 |
c.Errorf("unexpected grep results for %q, expected result to contain: %v", tc.name, wantResult) |
| 2991 |
} |
| 2992 |
} |
| 2993 |
|
| 2994 |
// Iterate through the results and check if the not wanted result is |
| 2995 |
// present in the got result. |
| 2996 |
for _, dontWantResult := range tc.dontWantResult { |
| 2997 |
found := false |
| 2998 |
for _, gotResult := range gr { |
| 2999 |
if dontWantResult == gotResult { |
| 3000 |
found = true |
| 3001 |
break |
| 3002 |
} |
| 3003 |
} |
| 3004 |
if found { |
| 3005 |
c.Errorf("unexpected grep results for %q, expected result to NOT contain: %v", tc.name, dontWantResult) |
| 3006 |
} |
| 3007 |
} |
| 3008 |
} |
| 3009 |
} |
| 3010 |
|
| 3011 |
func (s *WorktreeSuite) TestResetLingeringDirectories(c *C) { |
| 3012 |
dir := c.MkDir() |
| 3013 |
|
| 3014 |
commitOpts := &CommitOptions{Author: &object.Signature{ |
| 3015 |
Name: "foo", |
| 3016 |
Email: "[email protected]", |
| 3017 |
When: time.Now(), |
| 3018 |
}} |
| 3019 |
|
| 3020 |
repo, err := PlainInit(dir, false) |
| 3021 |
c.Assert(err, IsNil) |
| 3022 |
|
| 3023 |
w, err := repo.Worktree() |
| 3024 |
c.Assert(err, IsNil) |
| 3025 |
|
| 3026 |
os.WriteFile(filepath.Join(dir, "README"), []byte("placeholder"), 0o644) |
| 3027 |
|
| 3028 |
_, err = w.Add(".") |
| 3029 |
c.Assert(err, IsNil) |
| 3030 |
|
| 3031 |
initialHash, err := w.Commit("Initial commit", commitOpts) |
| 3032 |
c.Assert(err, IsNil) |
| 3033 |
|
| 3034 |
os.MkdirAll(filepath.Join(dir, "a", "b"), 0o755) |
| 3035 |
os.WriteFile(filepath.Join(dir, "a", "b", "1"), []byte("1"), 0o644) |
| 3036 |
|
| 3037 |
_, err = w.Add(".") |
| 3038 |
c.Assert(err, IsNil) |
| 3039 |
|
| 3040 |
_, err = w.Commit("Add file in nested sub-directories", commitOpts) |
| 3041 |
c.Assert(err, IsNil) |
| 3042 |
|
| 3043 |
// reset to initial commit, which should remove a/b/1, a/b, and a |
| 3044 |
err = w.Reset(&ResetOptions{ |
| 3045 |
Commit: initialHash, |
| 3046 |
Mode: HardReset, |
| 3047 |
}) |
| 3048 |
c.Assert(err, IsNil) |
| 3049 |
|
| 3050 |
_, err = os.Stat(filepath.Join(dir, "a", "b", "1")) |
| 3051 |
c.Assert(errors.Is(err, os.ErrNotExist), Equals, true) |
| 3052 |
|
| 3053 |
_, err = os.Stat(filepath.Join(dir, "a", "b")) |
| 3054 |
c.Assert(errors.Is(err, os.ErrNotExist), Equals, true) |
| 3055 |
|
| 3056 |
_, err = os.Stat(filepath.Join(dir, "a")) |
| 3057 |
c.Assert(errors.Is(err, os.ErrNotExist), Equals, true) |
| 3058 |
} |
| 3059 |
|
| 3060 |
func (s *WorktreeSuite) TestAddAndCommit(c *C) { |
| 3061 |
expectedFiles := 2 |
| 3062 |
|
| 3063 |
dir := c.MkDir() |
| 3064 |
|
| 3065 |
repo, err := PlainInit(dir, false) |
| 3066 |
c.Assert(err, IsNil) |
| 3067 |
|
| 3068 |
w, err := repo.Worktree() |
| 3069 |
c.Assert(err, IsNil) |
| 3070 |
|
| 3071 |
os.WriteFile(filepath.Join(dir, "foo"), []byte("bar"), 0o644) |
| 3072 |
os.WriteFile(filepath.Join(dir, "bar"), []byte("foo"), 0o644) |
| 3073 |
|
| 3074 |
_, err = w.Add(".") |
| 3075 |
c.Assert(err, IsNil) |
| 3076 |
|
| 3077 |
_, err = w.Commit("Test Add And Commit", &CommitOptions{Author: &object.Signature{ |
| 3078 |
Name: "foo", |
| 3079 |
Email: "[email protected]", |
| 3080 |
When: time.Now(), |
| 3081 |
}}) |
| 3082 |
c.Assert(err, IsNil) |
| 3083 |
|
| 3084 |
iter, err := w.r.Log(&LogOptions{}) |
| 3085 |
c.Assert(err, IsNil) |
| 3086 |
|
| 3087 |
filesFound := 0 |
| 3088 |
err = iter.ForEach(func(c *object.Commit) error { |
| 3089 |
files, err := c.Files() |
| 3090 |
if err != nil { |
| 3091 |
return err |
| 3092 |
} |
| 3093 |
|
| 3094 |
err = files.ForEach(func(f *object.File) error { |
| 3095 |
filesFound++ |
| 3096 |
return nil |
| 3097 |
}) |
| 3098 |
return err |
| 3099 |
}) |
| 3100 |
c.Assert(err, IsNil) |
| 3101 |
c.Assert(filesFound, Equals, expectedFiles) |
| 3102 |
} |
| 3103 |
|
| 3104 |
func (s *WorktreeSuite) TestAddAndCommitEmpty(c *C) { |
| 3105 |
dir := c.MkDir() |
| 3106 |
|
| 3107 |
repo, err := PlainInit(dir, false) |
| 3108 |
c.Assert(err, IsNil) |
| 3109 |
|
| 3110 |
w, err := repo.Worktree() |
| 3111 |
c.Assert(err, IsNil) |
| 3112 |
|
| 3113 |
_, err = w.Add(".") |
| 3114 |
c.Assert(err, IsNil) |
| 3115 |
|
| 3116 |
_, err = w.Commit("Test Add And Commit", &CommitOptions{Author: &object.Signature{ |
| 3117 |
Name: "foo", |
| 3118 |
Email: "[email protected]", |
| 3119 |
When: time.Now(), |
| 3120 |
}}) |
| 3121 |
c.Assert(err, Equals, ErrEmptyCommit) |
| 3122 |
} |
| 3123 |
|
| 3124 |
func (s *WorktreeSuite) TestLinkedWorktree(c *C) { |
| 3125 |
fs := fixtures.ByTag("linked-worktree").One().Worktree() |
| 3126 |
|
| 3127 |
// Open main repo. |
| 3128 |
{ |
| 3129 |
fs, err := fs.Chroot("main") |
| 3130 |
c.Assert(err, IsNil) |
| 3131 |
repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) |
| 3132 |
c.Assert(err, IsNil) |
| 3133 |
|
| 3134 |
wt, err := repo.Worktree() |
| 3135 |
c.Assert(err, IsNil) |
| 3136 |
|
| 3137 |
status, err := wt.Status() |
| 3138 |
c.Assert(err, IsNil) |
| 3139 |
c.Assert(len(status), Equals, 2) // 2 files |
| 3140 |
|
| 3141 |
head, err := repo.Head() |
| 3142 |
c.Assert(err, IsNil) |
| 3143 |
c.Assert(string(head.Name()), Equals, "refs/heads/master") |
| 3144 |
} |
| 3145 |
|
| 3146 |
// Open linked-worktree #1. |
| 3147 |
{ |
| 3148 |
fs, err := fs.Chroot("linked-worktree-1") |
| 3149 |
c.Assert(err, IsNil) |
| 3150 |
repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) |
| 3151 |
c.Assert(err, IsNil) |
| 3152 |
|
| 3153 |
wt, err := repo.Worktree() |
| 3154 |
c.Assert(err, IsNil) |
| 3155 |
|
| 3156 |
status, err := wt.Status() |
| 3157 |
c.Assert(err, IsNil) |
| 3158 |
c.Assert(len(status), Equals, 3) // 3 files |
| 3159 |
|
| 3160 |
_, ok := status["linked-worktree-1-unique-file.txt"] |
| 3161 |
c.Assert(ok, Equals, true) |
| 3162 |
|
| 3163 |
head, err := repo.Head() |
| 3164 |
c.Assert(err, IsNil) |
| 3165 |
c.Assert(string(head.Name()), Equals, "refs/heads/linked-worktree-1") |
| 3166 |
} |
| 3167 |
|
| 3168 |
// Open linked-worktree #2. |
| 3169 |
{ |
| 3170 |
fs, err := fs.Chroot("linked-worktree-2") |
| 3171 |
c.Assert(err, IsNil) |
| 3172 |
repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) |
| 3173 |
c.Assert(err, IsNil) |
| 3174 |
|
| 3175 |
wt, err := repo.Worktree() |
| 3176 |
c.Assert(err, IsNil) |
| 3177 |
|
| 3178 |
status, err := wt.Status() |
| 3179 |
c.Assert(err, IsNil) |
| 3180 |
c.Assert(len(status), Equals, 3) // 3 files |
| 3181 |
|
| 3182 |
_, ok := status["linked-worktree-2-unique-file.txt"] |
| 3183 |
c.Assert(ok, Equals, true) |
| 3184 |
|
| 3185 |
head, err := repo.Head() |
| 3186 |
c.Assert(err, IsNil) |
| 3187 |
c.Assert(string(head.Name()), Equals, "refs/heads/branch-with-different-name") |
| 3188 |
} |
| 3189 |
|
| 3190 |
// Open linked-worktree #2. |
| 3191 |
{ |
| 3192 |
fs, err := fs.Chroot("linked-worktree-invalid-commondir") |
| 3193 |
c.Assert(err, IsNil) |
| 3194 |
_, err = PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true}) |
| 3195 |
c.Assert(err, Equals, ErrRepositoryIncomplete) |
| 3196 |
} |
| 3197 |
} |
| 3198 |
|
| 3199 |
func TestValidPath(t *testing.T) { |
| 3200 |
type testcase struct { |
| 3201 |
path string |
| 3202 |
wantErr bool |
| 3203 |
} |
| 3204 |
|
| 3205 |
tests := []testcase{ |
| 3206 |
{".git", true}, |
| 3207 |
{".git/b", true}, |
| 3208 |
{".git\\b", true}, |
| 3209 |
{"git~1", true}, |
| 3210 |
{"a/../b", true}, |
| 3211 |
{"a\\..\\b", true}, |
| 3212 |
{"/", true}, |
| 3213 |
{"", true}, |
| 3214 |
{".gitmodules", false}, |
| 3215 |
{".gitignore", false}, |
| 3216 |
{"a..b", false}, |
| 3217 |
{".", true}, |
| 3218 |
{"a/.git/b", true}, |
| 3219 |
{"a\\.git\\b", true}, |
| 3220 |
{"a/.git", false}, |
| 3221 |
{"a\\.git", false}, |
| 3222 |
} |
| 3223 |
|
| 3224 |
if runtime.GOOS == "windows" { |
| 3225 |
tests = append(tests, []testcase{ |
| 3226 |
{"\\\\a\\b", true}, |
| 3227 |
{"C:\\a\\b", true}, |
| 3228 |
{".git . . .", true}, |
| 3229 |
{".git . . ", true}, |
| 3230 |
{".git ", true}, |
| 3231 |
{".git.", true}, |
| 3232 |
{".git::$INDEX_ALLOCATION", true}, |
| 3233 |
}...) |
| 3234 |
} |
| 3235 |
|
| 3236 |
fs := newWorktreeFilesystem(nil, defaultProtectNTFS(), defaultProtectHFS()) |
| 3237 |
for _, tc := range tests { |
| 3238 |
t.Run(tc.path, func(t *testing.T) { |
| 3239 |
err := fs.validPath(tc.path) |
| 3240 |
if tc.wantErr { |
| 3241 |
assert.Error(t, err) |
| 3242 |
} else { |
| 3243 |
assert.NoError(t, err) |
| 3244 |
} |
| 3245 |
}) |
| 3246 |
} |
| 3247 |
} |
| 3248 |
|
| 3249 |
// TestWorktreeFilesystemMkdirAllRootIsNoop locks in the contract that |
| 3250 |
// MkdirAll on a root-equivalent path is a silent no-op against the |
| 3251 |
// wrapper. validPath itself still rejects "", ".", and "/" (see |
| 3252 |
// TestValidPath), but MkdirAll specifically tolerates them because |
| 3253 |
// "ensure the root exists" is always trivially satisfied. |
| 3254 |
func TestWorktreeFilesystemMkdirAllRootIsNoop(t *testing.T) { |
| 3255 |
t.Parallel() |
| 3256 |
|
| 3257 |
rootPaths := []string{"", ".", "/"} |
| 3258 |
for _, p := range rootPaths { |
| 3259 |
t.Run(p, func(t *testing.T) { |
| 3260 |
t.Parallel() |
| 3261 |
|
| 3262 |
mfs := memfs.New() |
| 3263 |
fs := newWorktreeFilesystem(mfs, true, true) |
| 3264 |
|
| 3265 |
require.NoError(t, fs.MkdirAll(p, 0o755)) |
| 3266 |
|
| 3267 |
entries, err := mfs.ReadDir("/") |
| 3268 |
require.NoError(t, err) |
| 3269 |
assert.Empty(t, entries, "MkdirAll(%q) must not materialise a directory entry", p) |
| 3270 |
}) |
| 3271 |
} |
| 3272 |
} |
| 3273 |
|
| 3274 |
var statusCodeNames = map[StatusCode]string{ |
| 3275 |
Unmodified: "Unmodified", |
| 3276 |
Untracked: "Untracked", |
| 3277 |
Modified: "Modified", |
| 3278 |
Added: "Added", |
| 3279 |
Deleted: "Deleted", |
| 3280 |
Renamed: "Renamed", |
| 3281 |
Copied: "Copied", |
| 3282 |
UpdatedButUnmerged: "UpdatedButUnmerged", |
| 3283 |
} |
| 3284 |
|
| 3285 |
func setupForRestore(c *C, s *WorktreeSuite) (fs billy.Filesystem, w *Worktree, names []string) { |
| 3286 |
fs = memfs.New() |
| 3287 |
w = &Worktree{ |
| 3288 |
r: s.Repository, |
| 3289 |
Filesystem: fs, |
| 3290 |
} |
| 3291 |
|
| 3292 |
err := w.Checkout(&CheckoutOptions{}) |
| 3293 |
c.Assert(err, IsNil) |
| 3294 |
|
| 3295 |
names = []string{"foo", "CHANGELOG", "LICENSE", "binary.jpg"} |
| 3296 |
verifyStatus(c, "Checkout", w, names, []FileStatus{ |
| 3297 |
{Worktree: Untracked, Staging: Untracked}, |
| 3298 |
{Worktree: Untracked, Staging: Untracked}, |
| 3299 |
{Worktree: Untracked, Staging: Untracked}, |
| 3300 |
{Worktree: Untracked, Staging: Untracked}, |
| 3301 |
}) |
| 3302 |
|
| 3303 |
// Touch of bunch of files including create a new file and delete an exsiting file |
| 3304 |
for _, name := range names { |
| 3305 |
err = util.WriteFile(fs, name, []byte("Foo Bar"), 0755) |
| 3306 |
c.Assert(err, IsNil) |
| 3307 |
} |
| 3308 |
err = util.RemoveAll(fs, names[3]) |
| 3309 |
c.Assert(err, IsNil) |
| 3310 |
|
| 3311 |
// Confirm the status after doing the edits without staging anything |
| 3312 |
verifyStatus(c, "Edits", w, names, []FileStatus{ |
| 3313 |
{Worktree: Untracked, Staging: Untracked}, |
| 3314 |
{Worktree: Modified, Staging: Unmodified}, |
| 3315 |
{Worktree: Modified, Staging: Unmodified}, |
| 3316 |
{Worktree: Deleted, Staging: Unmodified}, |
| 3317 |
}) |
| 3318 |
|
| 3319 |
// Stage all files and verify the updated status |
| 3320 |
for _, name := range names { |
| 3321 |
_, err = w.Add(name) |
| 3322 |
c.Assert(err, IsNil) |
| 3323 |
} |
| 3324 |
verifyStatus(c, "Staged", w, names, []FileStatus{ |
| 3325 |
{Worktree: Unmodified, Staging: Added}, |
| 3326 |
{Worktree: Unmodified, Staging: Modified}, |
| 3327 |
{Worktree: Unmodified, Staging: Modified}, |
| 3328 |
{Worktree: Unmodified, Staging: Deleted}, |
| 3329 |
}) |
| 3330 |
|
| 3331 |
// Add secondary changes to a file to make sure we only restore the staged file |
| 3332 |
err = util.WriteFile(fs, names[1], []byte("Foo Bar:11"), 0755) |
| 3333 |
c.Assert(err, IsNil) |
| 3334 |
err = util.WriteFile(fs, names[2], []byte("Foo Bar:22"), 0755) |
| 3335 |
c.Assert(err, IsNil) |
| 3336 |
|
| 3337 |
verifyStatus(c, "Secondary Edits", w, names, []FileStatus{ |
| 3338 |
{Worktree: Unmodified, Staging: Added}, |
| 3339 |
{Worktree: Modified, Staging: Modified}, |
| 3340 |
{Worktree: Modified, Staging: Modified}, |
| 3341 |
{Worktree: Unmodified, Staging: Deleted}, |
| 3342 |
}) |
| 3343 |
|
| 3344 |
return |
| 3345 |
} |
| 3346 |
|
| 3347 |
func verifyStatus(c *C, marker string, w *Worktree, files []string, statuses []FileStatus) { |
| 3348 |
c.Assert(len(files), Equals, len(statuses)) |
| 3349 |
|
| 3350 |
status, err := w.Status() |
| 3351 |
c.Assert(err, IsNil) |
| 3352 |
|
| 3353 |
for i, file := range files { |
| 3354 |
current := status.File(file) |
| 3355 |
expected := statuses[i] |
| 3356 |
c.Assert(current.Worktree, Equals, expected.Worktree, Commentf("%s - [%d] : %s Worktree %s != %s", marker, i, file, statusCodeNames[current.Worktree], statusCodeNames[expected.Worktree])) |
| 3357 |
c.Assert(current.Staging, Equals, expected.Staging, Commentf("%s - [%d] : %s Staging %s != %s", marker, i, file, statusCodeNames[current.Staging], statusCodeNames[expected.Staging])) |
| 3358 |
} |
| 3359 |
} |
| 3360 |
|
| 3361 |
func (s *WorktreeSuite) TestRestoreStaged(c *C) { |
| 3362 |
fs, w, names := setupForRestore(c, s) |
| 3363 |
|
| 3364 |
// Attempt without files should throw an error like the git restore --staged |
| 3365 |
opts := RestoreOptions{Staged: true} |
| 3366 |
err := w.Restore(&opts) |
| 3367 |
c.Assert(err, Equals, ErrNoRestorePaths) |
| 3368 |
|
| 3369 |
// Restore Staged files in 2 groups and confirm status |
| 3370 |
opts.Files = []string{names[0], "./" + names[1]} |
| 3371 |
err = w.Restore(&opts) |
| 3372 |
c.Assert(err, IsNil) |
| 3373 |
verifyStatus(c, "Restored First", w, names, []FileStatus{ |
| 3374 |
{Worktree: Untracked, Staging: Untracked}, |
| 3375 |
{Worktree: Modified, Staging: Unmodified}, |
| 3376 |
{Worktree: Modified, Staging: Modified}, |
| 3377 |
{Worktree: Unmodified, Staging: Deleted}, |
| 3378 |
}) |
| 3379 |
|
| 3380 |
// Make sure the restore didn't overwrite our secondary changes |
| 3381 |
contents, err := util.ReadFile(fs, names[1]) |
| 3382 |
c.Assert(err, IsNil) |
| 3383 |
c.Assert(string(contents), Equals, "Foo Bar:11") |
| 3384 |
|
| 3385 |
opts.Files = []string{"./" + names[2], names[3]} |
| 3386 |
err = w.Restore(&opts) |
| 3387 |
c.Assert(err, IsNil) |
| 3388 |
verifyStatus(c, "Restored Second", w, names, []FileStatus{ |
| 3389 |
{Worktree: Untracked, Staging: Untracked}, |
| 3390 |
{Worktree: Modified, Staging: Unmodified}, |
| 3391 |
{Worktree: Modified, Staging: Unmodified}, |
| 3392 |
{Worktree: Deleted, Staging: Unmodified}, |
| 3393 |
}) |
| 3394 |
|
| 3395 |
// Make sure the restore didn't overwrite our secondary changes |
| 3396 |
contents, err = util.ReadFile(fs, names[2]) |
| 3397 |
c.Assert(err, IsNil) |
| 3398 |
c.Assert(string(contents), Equals, "Foo Bar:22") |
| 3399 |
} |
| 3400 |
|
| 3401 |
func (s *WorktreeSuite) TestRestoreWorktree(c *C) { |
| 3402 |
_, w, names := setupForRestore(c, s) |
| 3403 |
|
| 3404 |
// Attempt without files should throw an error like the git restore |
| 3405 |
opts := RestoreOptions{} |
| 3406 |
err := w.Restore(&opts) |
| 3407 |
c.Assert(err, Equals, ErrNoRestorePaths) |
| 3408 |
|
| 3409 |
opts.Files = []string{names[0], names[1]} |
| 3410 |
err = w.Restore(&opts) |
| 3411 |
c.Assert(err, Equals, ErrRestoreWorktreeOnlyNotSupported) |
| 3412 |
} |
| 3413 |
|
| 3414 |
func (s *WorktreeSuite) TestRestoreBoth(c *C) { |
| 3415 |
_, w, names := setupForRestore(c, s) |
| 3416 |
|
| 3417 |
// Attempt without files should throw an error like the git restore --staged --worktree |
| 3418 |
opts := RestoreOptions{Staged: true, Worktree: true} |
| 3419 |
err := w.Restore(&opts) |
| 3420 |
c.Assert(err, Equals, ErrNoRestorePaths) |
| 3421 |
|
| 3422 |
// Restore Staged files in 2 groups and confirm status |
| 3423 |
opts.Files = []string{names[0], names[1]} |
| 3424 |
err = w.Restore(&opts) |
| 3425 |
c.Assert(err, IsNil) |
| 3426 |
verifyStatus(c, "Restored First", w, names, []FileStatus{ |
| 3427 |
{Worktree: Untracked, Staging: Untracked}, |
| 3428 |
{Worktree: Untracked, Staging: Untracked}, |
| 3429 |
{Worktree: Modified, Staging: Modified}, |
| 3430 |
{Worktree: Unmodified, Staging: Deleted}, |
| 3431 |
}) |
| 3432 |
|
| 3433 |
opts.Files = []string{names[2], names[3]} |
| 3434 |
err = w.Restore(&opts) |
| 3435 |
c.Assert(err, IsNil) |
| 3436 |
verifyStatus(c, "Restored Second", w, names, []FileStatus{ |
| 3437 |
{Worktree: Untracked, Staging: Untracked}, |
| 3438 |
{Worktree: Untracked, Staging: Untracked}, |
| 3439 |
{Worktree: Untracked, Staging: Untracked}, |
| 3440 |
{Worktree: Untracked, Staging: Untracked}, |
| 3441 |
}) |
| 3442 |
} |
| 3443 |
|
| 3444 |
func TestFilePermissions(t *testing.T) { |
| 3445 |
// Initialize an in memory repository |
| 3446 |
remoteUrl := t.TempDir() |
| 3447 |
|
| 3448 |
inMemoryFs := memfs.New() |
| 3449 |
remoteFs := osfs.New(remoteUrl) |
| 3450 |
remoteStorage := filesystem.NewStorage(remoteFs, cache.NewObjectLRUDefault()) |
| 3451 |
|
| 3452 |
remoteRepository, err := Init(remoteStorage, inMemoryFs) |
| 3453 |
assert.NoError(t, err) |
| 3454 |
|
| 3455 |
err = util.WriteFile(inMemoryFs, "fileWithExecuteBit", []byte("Initial data"), 0755) |
| 3456 |
assert.NoError(t, err) |
| 3457 |
|
| 3458 |
err = util.WriteFile(inMemoryFs, "regularFile", []byte("Initial data"), 0644) |
| 3459 |
assert.NoError(t, err) |
| 3460 |
|
| 3461 |
remoteWorktree, err := remoteRepository.Worktree() |
| 3462 |
assert.NoError(t, err) |
| 3463 |
|
| 3464 |
_, err = remoteWorktree.Add("fileWithExecuteBit") |
| 3465 |
assert.NoError(t, err) |
| 3466 |
|
| 3467 |
_, err = remoteWorktree.Add("regularFile") |
| 3468 |
assert.NoError(t, err) |
| 3469 |
|
| 3470 |
_, err = remoteWorktree.Commit("my commit", &CommitOptions{}) |
| 3471 |
assert.NoError(t, err) |
| 3472 |
|
| 3473 |
worktreePath := t.TempDir() |
| 3474 |
|
| 3475 |
localRepo, err := PlainClone(worktreePath, false, &CloneOptions{URL: remoteUrl}) |
| 3476 |
assert.NoError(t, err) |
| 3477 |
|
| 3478 |
localWorktree, err := localRepo.Worktree() |
| 3479 |
assert.NoError(t, err) |
| 3480 |
|
| 3481 |
idx, err := localWorktree.r.Storer.Index() |
| 3482 |
assert.NoError(t, err) |
| 3483 |
|
| 3484 |
expectedEntries := []index.Entry{ |
| 3485 |
{ |
| 3486 |
Name: "fileWithExecuteBit", |
| 3487 |
Mode: filemode.Executable, |
| 3488 |
}, |
| 3489 |
{ |
| 3490 |
Name: "regularFile", |
| 3491 |
Mode: filemode.Regular, |
| 3492 |
}, |
| 3493 |
} |
| 3494 |
|
| 3495 |
assert.Len(t, idx.Entries, len(expectedEntries)) |
| 3496 |
|
| 3497 |
for i, expectedEntry := range expectedEntries { |
| 3498 |
assert.Equal(t, expectedEntry.Name, idx.Entries[i].Name) |
| 3499 |
assert.Equal(t, expectedEntry.Mode, idx.Entries[i].Mode) |
| 3500 |
} |
| 3501 |
} |