| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"io/ioutil" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"regexp" |
| 11 |
"runtime" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"gopkg.in/src-d/go-git.v4/config" |
| 16 |
"gopkg.in/src-d/go-git.v4/plumbing" |
| 17 |
"gopkg.in/src-d/go-git.v4/plumbing/filemode" |
| 18 |
"gopkg.in/src-d/go-git.v4/plumbing/format/gitignore" |
| 19 |
"gopkg.in/src-d/go-git.v4/plumbing/format/index" |
| 20 |
"gopkg.in/src-d/go-git.v4/plumbing/object" |
| 21 |
"gopkg.in/src-d/go-git.v4/storage/memory" |
| 22 |
|
| 23 |
"golang.org/x/text/unicode/norm" |
| 24 |
. "gopkg.in/check.v1" |
| 25 |
"gopkg.in/src-d/go-billy.v4/memfs" |
| 26 |
"gopkg.in/src-d/go-billy.v4/osfs" |
| 27 |
"gopkg.in/src-d/go-billy.v4/util" |
| 28 |
"gopkg.in/src-d/go-git-fixtures.v3" |
| 29 |
) |
| 30 |
|
| 31 |
type WorktreeSuite struct { |
| 32 |
BaseSuite |
| 33 |
} |
| 34 |
|
| 35 |
var _ = Suite(&WorktreeSuite{}) |
| 36 |
|
| 37 |
func (s *WorktreeSuite) SetUpTest(c *C) { |
| 38 |
f := fixtures.Basic().One() |
| 39 |
s.Repository = s.NewRepositoryWithEmptyWorktree(f) |
| 40 |
} |
| 41 |
|
| 42 |
func (s *WorktreeSuite) TestPullCheckout(c *C) { |
| 43 |
fs := memfs.New() |
| 44 |
r, _ := Init(memory.NewStorage(), fs) |
| 45 |
r.CreateRemote(&config.RemoteConfig{ |
| 46 |
Name: DefaultRemoteName, |
| 47 |
URLs: []string{s.GetBasicLocalRepositoryURL()}, |
| 48 |
}) |
| 49 |
|
| 50 |
w, err := r.Worktree() |
| 51 |
c.Assert(err, IsNil) |
| 52 |
|
| 53 |
err = w.Pull(&PullOptions{}) |
| 54 |
c.Assert(err, IsNil) |
| 55 |
|
| 56 |
fi, err := fs.ReadDir("") |
| 57 |
c.Assert(err, IsNil) |
| 58 |
c.Assert(fi, HasLen, 8) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *WorktreeSuite) TestPullFastForward(c *C) { |
| 62 |
url := c.MkDir() |
| 63 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 64 |
|
| 65 |
server, err := PlainClone(url, false, &CloneOptions{ |
| 66 |
URL: path, |
| 67 |
}) |
| 68 |
c.Assert(err, IsNil) |
| 69 |
|
| 70 |
r, err := PlainClone(c.MkDir(), false, &CloneOptions{ |
| 71 |
URL: url, |
| 72 |
}) |
| 73 |
c.Assert(err, IsNil) |
| 74 |
|
| 75 |
w, err := server.Worktree() |
| 76 |
c.Assert(err, IsNil) |
| 77 |
err = ioutil.WriteFile(filepath.Join(path, "foo"), []byte("foo"), 0755) |
| 78 |
c.Assert(err, IsNil) |
| 79 |
hash, err := w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 80 |
c.Assert(err, IsNil) |
| 81 |
|
| 82 |
w, err = r.Worktree() |
| 83 |
c.Assert(err, IsNil) |
| 84 |
|
| 85 |
err = w.Pull(&PullOptions{}) |
| 86 |
c.Assert(err, IsNil) |
| 87 |
|
| 88 |
head, err := r.Head() |
| 89 |
c.Assert(err, IsNil) |
| 90 |
c.Assert(head.Hash(), Equals, hash) |
| 91 |
} |
| 92 |
|
| 93 |
func (s *WorktreeSuite) TestPullNonFastForward(c *C) { |
| 94 |
url := c.MkDir() |
| 95 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 96 |
|
| 97 |
server, err := PlainClone(url, false, &CloneOptions{ |
| 98 |
URL: path, |
| 99 |
}) |
| 100 |
c.Assert(err, IsNil) |
| 101 |
|
| 102 |
r, err := PlainClone(c.MkDir(), false, &CloneOptions{ |
| 103 |
URL: url, |
| 104 |
}) |
| 105 |
c.Assert(err, IsNil) |
| 106 |
|
| 107 |
w, err := server.Worktree() |
| 108 |
c.Assert(err, IsNil) |
| 109 |
err = ioutil.WriteFile(filepath.Join(path, "foo"), []byte("foo"), 0755) |
| 110 |
c.Assert(err, IsNil) |
| 111 |
_, err = w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 112 |
c.Assert(err, IsNil) |
| 113 |
|
| 114 |
w, err = r.Worktree() |
| 115 |
c.Assert(err, IsNil) |
| 116 |
err = ioutil.WriteFile(filepath.Join(path, "bar"), []byte("bar"), 0755) |
| 117 |
c.Assert(err, IsNil) |
| 118 |
_, err = w.Commit("bar", &CommitOptions{Author: defaultSignature()}) |
| 119 |
c.Assert(err, IsNil) |
| 120 |
|
| 121 |
err = w.Pull(&PullOptions{}) |
| 122 |
c.Assert(err, Equals, ErrNonFastForwardUpdate) |
| 123 |
} |
| 124 |
|
| 125 |
func (s *WorktreeSuite) TestPullUpdateReferencesIfNeeded(c *C) { |
| 126 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 127 |
r.CreateRemote(&config.RemoteConfig{ |
| 128 |
Name: DefaultRemoteName, |
| 129 |
URLs: []string{s.GetBasicLocalRepositoryURL()}, |
| 130 |
}) |
| 131 |
|
| 132 |
err := r.Fetch(&FetchOptions{}) |
| 133 |
c.Assert(err, IsNil) |
| 134 |
|
| 135 |
_, err = r.Reference("refs/heads/master", false) |
| 136 |
c.Assert(err, NotNil) |
| 137 |
|
| 138 |
w, err := r.Worktree() |
| 139 |
c.Assert(err, IsNil) |
| 140 |
|
| 141 |
err = w.Pull(&PullOptions{}) |
| 142 |
c.Assert(err, IsNil) |
| 143 |
|
| 144 |
head, err := r.Reference(plumbing.HEAD, true) |
| 145 |
c.Assert(err, IsNil) |
| 146 |
c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 147 |
|
| 148 |
branch, err := r.Reference("refs/heads/master", false) |
| 149 |
c.Assert(err, IsNil) |
| 150 |
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 151 |
|
| 152 |
err = w.Pull(&PullOptions{}) |
| 153 |
c.Assert(err, Equals, NoErrAlreadyUpToDate) |
| 154 |
} |
| 155 |
|
| 156 |
func (s *WorktreeSuite) TestPullInSingleBranch(c *C) { |
| 157 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 158 |
err := r.clone(context.Background(), &CloneOptions{ |
| 159 |
URL: s.GetBasicLocalRepositoryURL(), |
| 160 |
SingleBranch: true, |
| 161 |
}) |
| 162 |
|
| 163 |
c.Assert(err, IsNil) |
| 164 |
|
| 165 |
w, err := r.Worktree() |
| 166 |
c.Assert(err, IsNil) |
| 167 |
|
| 168 |
err = w.Pull(&PullOptions{}) |
| 169 |
c.Assert(err, Equals, NoErrAlreadyUpToDate) |
| 170 |
|
| 171 |
branch, err := r.Reference("refs/heads/master", false) |
| 172 |
c.Assert(err, IsNil) |
| 173 |
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 174 |
|
| 175 |
branch, err = r.Reference("refs/remotes/foo/branch", false) |
| 176 |
c.Assert(err, NotNil) |
| 177 |
|
| 178 |
storage := r.Storer.(*memory.Storage) |
| 179 |
c.Assert(storage.Objects, HasLen, 28) |
| 180 |
} |
| 181 |
|
| 182 |
func (s *WorktreeSuite) TestPullProgress(c *C) { |
| 183 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 184 |
|
| 185 |
r.CreateRemote(&config.RemoteConfig{ |
| 186 |
Name: DefaultRemoteName, |
| 187 |
URLs: []string{s.GetBasicLocalRepositoryURL()}, |
| 188 |
}) |
| 189 |
|
| 190 |
w, err := r.Worktree() |
| 191 |
c.Assert(err, IsNil) |
| 192 |
|
| 193 |
buf := bytes.NewBuffer(nil) |
| 194 |
err = w.Pull(&PullOptions{ |
| 195 |
Progress: buf, |
| 196 |
}) |
| 197 |
|
| 198 |
c.Assert(err, IsNil) |
| 199 |
c.Assert(buf.Len(), Not(Equals), 0) |
| 200 |
} |
| 201 |
|
| 202 |
/* |
| 203 |
func (s *WorktreeSuite) TestPullProgressWithRecursion(c *C) { |
| 204 |
if testing.Short() { |
| 205 |
c.Skip("skipping test in short mode.") |
| 206 |
} |
| 207 |
|
| 208 |
path := fixtures.ByTag("submodule").One().Worktree().Root() |
| 209 |
|
| 210 |
dir, err := ioutil.TempDir("", "plain-clone-submodule") |
| 211 |
c.Assert(err, IsNil) |
| 212 |
defer os.RemoveAll(dir) |
| 213 |
|
| 214 |
r, _ := PlainInit(dir, false) |
| 215 |
r.CreateRemote(&config.RemoteConfig{ |
| 216 |
Name: DefaultRemoteName, |
| 217 |
URLs: []string{path}, |
| 218 |
}) |
| 219 |
|
| 220 |
w, err := r.Worktree() |
| 221 |
c.Assert(err, IsNil) |
| 222 |
|
| 223 |
err = w.Pull(&PullOptions{ |
| 224 |
RecurseSubmodules: DefaultSubmoduleRecursionDepth, |
| 225 |
}) |
| 226 |
c.Assert(err, IsNil) |
| 227 |
|
| 228 |
cfg, err := r.Config() |
| 229 |
c.Assert(err, IsNil) |
| 230 |
c.Assert(cfg.Submodules, HasLen, 2) |
| 231 |
} |
| 232 |
*/ |
| 233 |
|
| 234 |
func (s *RepositorySuite) TestPullAdd(c *C) { |
| 235 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 236 |
|
| 237 |
r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ |
| 238 |
URL: filepath.Join(path, ".git"), |
| 239 |
}) |
| 240 |
|
| 241 |
c.Assert(err, IsNil) |
| 242 |
|
| 243 |
storage := r.Storer.(*memory.Storage) |
| 244 |
c.Assert(storage.Objects, HasLen, 28) |
| 245 |
|
| 246 |
branch, err := r.Reference("refs/heads/master", false) |
| 247 |
c.Assert(err, IsNil) |
| 248 |
c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 249 |
|
| 250 |
ExecuteOnPath(c, path, |
| 251 |
"touch foo", |
| 252 |
"git add foo", |
| 253 |
"git config user.email [email protected]", |
| 254 |
"git config user.name foo", |
| 255 |
"git commit -m foo foo", |
| 256 |
) |
| 257 |
|
| 258 |
w, err := r.Worktree() |
| 259 |
c.Assert(err, IsNil) |
| 260 |
|
| 261 |
err = w.Pull(&PullOptions{RemoteName: "origin"}) |
| 262 |
c.Assert(err, IsNil) |
| 263 |
|
| 264 |
// the commit command has introduced a new commit, tree and blob |
| 265 |
c.Assert(storage.Objects, HasLen, 31) |
| 266 |
|
| 267 |
branch, err = r.Reference("refs/heads/master", false) |
| 268 |
c.Assert(err, IsNil) |
| 269 |
c.Assert(branch.Hash().String(), Not(Equals), "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") |
| 270 |
} |
| 271 |
|
| 272 |
func (s *WorktreeSuite) TestCheckout(c *C) { |
| 273 |
fs := memfs.New() |
| 274 |
w := &Worktree{ |
| 275 |
r: s.Repository, |
| 276 |
Filesystem: fs, |
| 277 |
} |
| 278 |
|
| 279 |
err := w.Checkout(&CheckoutOptions{ |
| 280 |
Force: true, |
| 281 |
}) |
| 282 |
c.Assert(err, IsNil) |
| 283 |
|
| 284 |
entries, err := fs.ReadDir("/") |
| 285 |
c.Assert(err, IsNil) |
| 286 |
|
| 287 |
c.Assert(entries, HasLen, 8) |
| 288 |
ch, err := fs.Open("CHANGELOG") |
| 289 |
c.Assert(err, IsNil) |
| 290 |
|
| 291 |
content, err := ioutil.ReadAll(ch) |
| 292 |
c.Assert(err, IsNil) |
| 293 |
c.Assert(string(content), Equals, "Initial changelog\n") |
| 294 |
|
| 295 |
idx, err := s.Repository.Storer.Index() |
| 296 |
c.Assert(err, IsNil) |
| 297 |
c.Assert(idx.Entries, HasLen, 9) |
| 298 |
} |
| 299 |
|
| 300 |
func (s *WorktreeSuite) TestCheckoutForce(c *C) { |
| 301 |
w := &Worktree{ |
| 302 |
r: s.Repository, |
| 303 |
Filesystem: memfs.New(), |
| 304 |
} |
| 305 |
|
| 306 |
err := w.Checkout(&CheckoutOptions{}) |
| 307 |
c.Assert(err, IsNil) |
| 308 |
|
| 309 |
w.Filesystem = memfs.New() |
| 310 |
|
| 311 |
err = w.Checkout(&CheckoutOptions{ |
| 312 |
Force: true, |
| 313 |
}) |
| 314 |
c.Assert(err, IsNil) |
| 315 |
|
| 316 |
entries, err := w.Filesystem.ReadDir("/") |
| 317 |
c.Assert(err, IsNil) |
| 318 |
c.Assert(entries, HasLen, 8) |
| 319 |
} |
| 320 |
|
| 321 |
func (s *WorktreeSuite) TestCheckoutSymlink(c *C) { |
| 322 |
if runtime.GOOS == "windows" { |
| 323 |
c.Skip("git doesn't support symlinks by default in windows") |
| 324 |
} |
| 325 |
|
| 326 |
dir, err := ioutil.TempDir("", "checkout") |
| 327 |
c.Assert(err, IsNil) |
| 328 |
defer os.RemoveAll(dir) |
| 329 |
|
| 330 |
r, err := PlainInit(dir, false) |
| 331 |
c.Assert(err, IsNil) |
| 332 |
|
| 333 |
w, err := r.Worktree() |
| 334 |
c.Assert(err, IsNil) |
| 335 |
|
| 336 |
w.Filesystem.Symlink("not-exists", "bar") |
| 337 |
w.Add("bar") |
| 338 |
w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 339 |
|
| 340 |
r.Storer.SetIndex(&index.Index{Version: 2}) |
| 341 |
w.Filesystem = osfs.New(filepath.Join(dir, "worktree-empty")) |
| 342 |
|
| 343 |
err = w.Checkout(&CheckoutOptions{}) |
| 344 |
c.Assert(err, IsNil) |
| 345 |
|
| 346 |
status, err := w.Status() |
| 347 |
c.Assert(err, IsNil) |
| 348 |
c.Assert(status.IsClean(), Equals, true) |
| 349 |
|
| 350 |
target, err := w.Filesystem.Readlink("bar") |
| 351 |
c.Assert(target, Equals, "not-exists") |
| 352 |
c.Assert(err, IsNil) |
| 353 |
} |
| 354 |
|
| 355 |
func (s *WorktreeSuite) TestFilenameNormalization(c *C) { |
| 356 |
if runtime.GOOS == "windows" { |
| 357 |
c.Skip("windows paths may contain non utf-8 sequences") |
| 358 |
} |
| 359 |
|
| 360 |
url := c.MkDir() |
| 361 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 362 |
|
| 363 |
server, err := PlainClone(url, false, &CloneOptions{ |
| 364 |
URL: path, |
| 365 |
}) |
| 366 |
c.Assert(err, IsNil) |
| 367 |
|
| 368 |
filename := "페" |
| 369 |
|
| 370 |
w, err := server.Worktree() |
| 371 |
c.Assert(err, IsNil) |
| 372 |
|
| 373 |
writeFile := func(path string) { |
| 374 |
err := util.WriteFile(w.Filesystem, path, []byte("foo"), 0755) |
| 375 |
c.Assert(err, IsNil) |
| 376 |
} |
| 377 |
|
| 378 |
writeFile(filename) |
| 379 |
origHash, err := w.Add(filename) |
| 380 |
c.Assert(err, IsNil) |
| 381 |
_, err = w.Commit("foo", &CommitOptions{Author: defaultSignature()}) |
| 382 |
c.Assert(err, IsNil) |
| 383 |
|
| 384 |
r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ |
| 385 |
URL: url, |
| 386 |
}) |
| 387 |
c.Assert(err, IsNil) |
| 388 |
|
| 389 |
w, err = r.Worktree() |
| 390 |
c.Assert(err, IsNil) |
| 391 |
|
| 392 |
status, err := w.Status() |
| 393 |
c.Assert(err, IsNil) |
| 394 |
c.Assert(status.IsClean(), Equals, true) |
| 395 |
|
| 396 |
err = w.Filesystem.Remove(filename) |
| 397 |
c.Assert(err, IsNil) |
| 398 |
|
| 399 |
modFilename := norm.Form(norm.NFKD).String(filename) |
| 400 |
writeFile(modFilename) |
| 401 |
|
| 402 |
_, err = w.Add(filename) |
| 403 |
c.Assert(err, IsNil) |
| 404 |
modHash, err := w.Add(modFilename) |
| 405 |
c.Assert(err, IsNil) |
| 406 |
// At this point we've got two files with the same content. |
| 407 |
// Hence their hashes must be the same. |
| 408 |
c.Assert(origHash == modHash, Equals, true) |
| 409 |
|
| 410 |
status, err = w.Status() |
| 411 |
c.Assert(err, IsNil) |
| 412 |
// However, their names are different and the work tree is still dirty. |
| 413 |
c.Assert(status.IsClean(), Equals, false) |
| 414 |
|
| 415 |
// Revert back the deletion of the first file. |
| 416 |
writeFile(filename) |
| 417 |
_, err = w.Add(filename) |
| 418 |
c.Assert(err, IsNil) |
| 419 |
|
| 420 |
status, err = w.Status() |
| 421 |
c.Assert(err, IsNil) |
| 422 |
// Still dirty - the second file is added. |
| 423 |
c.Assert(status.IsClean(), Equals, false) |
| 424 |
|
| 425 |
_, err = w.Remove(modFilename) |
| 426 |
c.Assert(err, IsNil) |
| 427 |
|
| 428 |
status, err = w.Status() |
| 429 |
c.Assert(err, IsNil) |
| 430 |
c.Assert(status.IsClean(), Equals, true) |
| 431 |
} |
| 432 |
|
| 433 |
func (s *WorktreeSuite) TestCheckoutSubmodule(c *C) { |
| 434 |
url := "https://github.com/git-fixtures/submodule.git" |
| 435 |
r := s.NewRepositoryWithEmptyWorktree(fixtures.ByURL(url).One()) |
| 436 |
|
| 437 |
w, err := r.Worktree() |
| 438 |
c.Assert(err, IsNil) |
| 439 |
|
| 440 |
err = w.Checkout(&CheckoutOptions{}) |
| 441 |
c.Assert(err, IsNil) |
| 442 |
|
| 443 |
status, err := w.Status() |
| 444 |
c.Assert(err, IsNil) |
| 445 |
c.Assert(status.IsClean(), Equals, true) |
| 446 |
} |
| 447 |
|
| 448 |
/* |
| 449 |
func (s *WorktreeSuite) TestCheckoutSubmoduleInitialized(c *C) { |
| 450 |
url := "https://github.com/git-fixtures/submodule.git" |
| 451 |
r := s.NewRepository(fixtures.ByURL(url).One()) |
| 452 |
|
| 453 |
w, err := r.Worktree() |
| 454 |
c.Assert(err, IsNil) |
| 455 |
|
| 456 |
sub, err := w.Submodules() |
| 457 |
c.Assert(err, IsNil) |
| 458 |
|
| 459 |
err = sub.Update(&SubmoduleUpdateOptions{Init: true}) |
| 460 |
c.Assert(err, IsNil) |
| 461 |
|
| 462 |
status, err := w.Status() |
| 463 |
c.Assert(err, IsNil) |
| 464 |
c.Assert(status.IsClean(), Equals, true) |
| 465 |
} |
| 466 |
*/ |
| 467 |
|
| 468 |
func (s *WorktreeSuite) TestCheckoutIndexMem(c *C) { |
| 469 |
fs := memfs.New() |
| 470 |
w := &Worktree{ |
| 471 |
r: s.Repository, |
| 472 |
Filesystem: fs, |
| 473 |
} |
| 474 |
|
| 475 |
err := w.Checkout(&CheckoutOptions{}) |
| 476 |
c.Assert(err, IsNil) |
| 477 |
|
| 478 |
idx, err := s.Repository.Storer.Index() |
| 479 |
c.Assert(err, IsNil) |
| 480 |
c.Assert(idx.Entries, HasLen, 9) |
| 481 |
c.Assert(idx.Entries[0].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88") |
| 482 |
c.Assert(idx.Entries[0].Name, Equals, ".gitignore") |
| 483 |
c.Assert(idx.Entries[0].Mode, Equals, filemode.Regular) |
| 484 |
c.Assert(idx.Entries[0].ModifiedAt.IsZero(), Equals, false) |
| 485 |
c.Assert(idx.Entries[0].Size, Equals, uint32(189)) |
| 486 |
|
| 487 |
// ctime, dev, inode, uid and gid are not supported on memfs fs |
| 488 |
c.Assert(idx.Entries[0].CreatedAt.IsZero(), Equals, true) |
| 489 |
c.Assert(idx.Entries[0].Dev, Equals, uint32(0)) |
| 490 |
c.Assert(idx.Entries[0].Inode, Equals, uint32(0)) |
| 491 |
c.Assert(idx.Entries[0].UID, Equals, uint32(0)) |
| 492 |
c.Assert(idx.Entries[0].GID, Equals, uint32(0)) |
| 493 |
} |
| 494 |
|
| 495 |
func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) { |
| 496 |
dir, err := ioutil.TempDir("", "checkout") |
| 497 |
c.Assert(err, IsNil) |
| 498 |
defer os.RemoveAll(dir) |
| 499 |
|
| 500 |
fs := osfs.New(filepath.Join(dir, "worktree")) |
| 501 |
w := &Worktree{ |
| 502 |
r: s.Repository, |
| 503 |
Filesystem: fs, |
| 504 |
} |
| 505 |
|
| 506 |
err = w.Checkout(&CheckoutOptions{}) |
| 507 |
c.Assert(err, IsNil) |
| 508 |
|
| 509 |
idx, err := s.Repository.Storer.Index() |
| 510 |
c.Assert(err, IsNil) |
| 511 |
c.Assert(idx.Entries, HasLen, 9) |
| 512 |
c.Assert(idx.Entries[0].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88") |
| 513 |
c.Assert(idx.Entries[0].Name, Equals, ".gitignore") |
| 514 |
c.Assert(idx.Entries[0].Mode, Equals, filemode.Regular) |
| 515 |
c.Assert(idx.Entries[0].ModifiedAt.IsZero(), Equals, false) |
| 516 |
c.Assert(idx.Entries[0].Size, Equals, uint32(189)) |
| 517 |
|
| 518 |
c.Assert(idx.Entries[0].CreatedAt.IsZero(), Equals, false) |
| 519 |
if runtime.GOOS != "windows" { |
| 520 |
c.Assert(idx.Entries[0].Dev, Not(Equals), uint32(0)) |
| 521 |
c.Assert(idx.Entries[0].Inode, Not(Equals), uint32(0)) |
| 522 |
c.Assert(idx.Entries[0].UID, Not(Equals), uint32(0)) |
| 523 |
c.Assert(idx.Entries[0].GID, Not(Equals), uint32(0)) |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
func (s *WorktreeSuite) TestCheckoutBranch(c *C) { |
| 528 |
w := &Worktree{ |
| 529 |
r: s.Repository, |
| 530 |
Filesystem: memfs.New(), |
| 531 |
} |
| 532 |
|
| 533 |
err := w.Checkout(&CheckoutOptions{ |
| 534 |
Branch: "refs/heads/branch", |
| 535 |
}) |
| 536 |
c.Assert(err, IsNil) |
| 537 |
|
| 538 |
head, err := w.r.Head() |
| 539 |
c.Assert(err, IsNil) |
| 540 |
c.Assert(head.Name().String(), Equals, "refs/heads/branch") |
| 541 |
|
| 542 |
status, err := w.Status() |
| 543 |
c.Assert(err, IsNil) |
| 544 |
c.Assert(status.IsClean(), Equals, true) |
| 545 |
} |
| 546 |
|
| 547 |
func (s *WorktreeSuite) TestCheckoutCreateWithHash(c *C) { |
| 548 |
w := &Worktree{ |
| 549 |
r: s.Repository, |
| 550 |
Filesystem: memfs.New(), |
| 551 |
} |
| 552 |
|
| 553 |
err := w.Checkout(&CheckoutOptions{ |
| 554 |
Create: true, |
| 555 |
Branch: "refs/heads/foo", |
| 556 |
Hash: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"), |
| 557 |
}) |
| 558 |
c.Assert(err, IsNil) |
| 559 |
|
| 560 |
head, err := w.r.Head() |
| 561 |
c.Assert(err, IsNil) |
| 562 |
c.Assert(head.Name().String(), Equals, "refs/heads/foo") |
| 563 |
c.Assert(head.Hash(), Equals, plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9")) |
| 564 |
|
| 565 |
status, err := w.Status() |
| 566 |
c.Assert(err, IsNil) |
| 567 |
c.Assert(status.IsClean(), Equals, true) |
| 568 |
} |
| 569 |
|
| 570 |
func (s *WorktreeSuite) TestCheckoutCreate(c *C) { |
| 571 |
w := &Worktree{ |
| 572 |
r: s.Repository, |
| 573 |
Filesystem: memfs.New(), |
| 574 |
} |
| 575 |
|
| 576 |
err := w.Checkout(&CheckoutOptions{ |
| 577 |
Create: true, |
| 578 |
Branch: "refs/heads/foo", |
| 579 |
}) |
| 580 |
c.Assert(err, IsNil) |
| 581 |
|
| 582 |
head, err := w.r.Head() |
| 583 |
c.Assert(err, IsNil) |
| 584 |
c.Assert(head.Name().String(), Equals, "refs/heads/foo") |
| 585 |
c.Assert(head.Hash(), Equals, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 586 |
|
| 587 |
status, err := w.Status() |
| 588 |
c.Assert(err, IsNil) |
| 589 |
c.Assert(status.IsClean(), Equals, true) |
| 590 |
} |
| 591 |
|
| 592 |
func (s *WorktreeSuite) TestCheckoutBranchAndHash(c *C) { |
| 593 |
w := &Worktree{ |
| 594 |
r: s.Repository, |
| 595 |
Filesystem: memfs.New(), |
| 596 |
} |
| 597 |
|
| 598 |
err := w.Checkout(&CheckoutOptions{ |
| 599 |
Branch: "refs/heads/foo", |
| 600 |
Hash: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"), |
| 601 |
}) |
| 602 |
|
| 603 |
c.Assert(err, Equals, ErrBranchHashExclusive) |
| 604 |
} |
| 605 |
|
| 606 |
func (s *WorktreeSuite) TestCheckoutCreateMissingBranch(c *C) { |
| 607 |
w := &Worktree{ |
| 608 |
r: s.Repository, |
| 609 |
Filesystem: memfs.New(), |
| 610 |
} |
| 611 |
|
| 612 |
err := w.Checkout(&CheckoutOptions{ |
| 613 |
Create: true, |
| 614 |
}) |
| 615 |
|
| 616 |
c.Assert(err, Equals, ErrCreateRequiresBranch) |
| 617 |
} |
| 618 |
|
| 619 |
func (s *WorktreeSuite) TestCheckoutTag(c *C) { |
| 620 |
f := fixtures.ByTag("tags").One() |
| 621 |
r := s.NewRepositoryWithEmptyWorktree(f) |
| 622 |
w, err := r.Worktree() |
| 623 |
c.Assert(err, IsNil) |
| 624 |
|
| 625 |
err = w.Checkout(&CheckoutOptions{}) |
| 626 |
c.Assert(err, IsNil) |
| 627 |
head, err := w.r.Head() |
| 628 |
c.Assert(err, IsNil) |
| 629 |
c.Assert(head.Name().String(), Equals, "refs/heads/master") |
| 630 |
|
| 631 |
status, err := w.Status() |
| 632 |
c.Assert(err, IsNil) |
| 633 |
c.Assert(status.IsClean(), Equals, true) |
| 634 |
|
| 635 |
err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/lightweight-tag"}) |
| 636 |
c.Assert(err, IsNil) |
| 637 |
head, err = w.r.Head() |
| 638 |
c.Assert(err, IsNil) |
| 639 |
c.Assert(head.Name().String(), Equals, "HEAD") |
| 640 |
c.Assert(head.Hash().String(), Equals, "f7b877701fbf855b44c0a9e86f3fdce2c298b07f") |
| 641 |
|
| 642 |
err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/commit-tag"}) |
| 643 |
c.Assert(err, IsNil) |
| 644 |
head, err = w.r.Head() |
| 645 |
c.Assert(err, IsNil) |
| 646 |
c.Assert(head.Name().String(), Equals, "HEAD") |
| 647 |
c.Assert(head.Hash().String(), Equals, "f7b877701fbf855b44c0a9e86f3fdce2c298b07f") |
| 648 |
|
| 649 |
err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/tree-tag"}) |
| 650 |
c.Assert(err, NotNil) |
| 651 |
head, err = w.r.Head() |
| 652 |
c.Assert(err, IsNil) |
| 653 |
c.Assert(head.Name().String(), Equals, "HEAD") |
| 654 |
} |
| 655 |
|
| 656 |
func (s *WorktreeSuite) TestCheckoutBisect(c *C) { |
| 657 |
if testing.Short() { |
| 658 |
c.Skip("skipping test in short mode.") |
| 659 |
} |
| 660 |
|
| 661 |
s.testCheckoutBisect(c, "https://github.com/src-d/go-git.git") |
| 662 |
} |
| 663 |
|
| 664 |
func (s *WorktreeSuite) TestCheckoutBisectSubmodules(c *C) { |
| 665 |
s.testCheckoutBisect(c, "https://github.com/git-fixtures/submodule.git") |
| 666 |
} |
| 667 |
|
| 668 |
// TestCheckoutBisect simulates a git bisect going through the git history and |
| 669 |
// checking every commit over the previous commit |
| 670 |
func (s *WorktreeSuite) testCheckoutBisect(c *C, url string) { |
| 671 |
f := fixtures.ByURL(url).One() |
| 672 |
r := s.NewRepositoryWithEmptyWorktree(f) |
| 673 |
|
| 674 |
w, err := r.Worktree() |
| 675 |
c.Assert(err, IsNil) |
| 676 |
|
| 677 |
iter, err := w.r.Log(&LogOptions{}) |
| 678 |
c.Assert(err, IsNil) |
| 679 |
|
| 680 |
iter.ForEach(func(commit *object.Commit) error { |
| 681 |
err := w.Checkout(&CheckoutOptions{Hash: commit.Hash}) |
| 682 |
c.Assert(err, IsNil) |
| 683 |
|
| 684 |
status, err := w.Status() |
| 685 |
c.Assert(err, IsNil) |
| 686 |
c.Assert(status.IsClean(), Equals, true) |
| 687 |
|
| 688 |
return nil |
| 689 |
}) |
| 690 |
} |
| 691 |
|
| 692 |
func (s *WorktreeSuite) TestStatus(c *C) { |
| 693 |
fs := memfs.New() |
| 694 |
w := &Worktree{ |
| 695 |
r: s.Repository, |
| 696 |
Filesystem: fs, |
| 697 |
} |
| 698 |
|
| 699 |
status, err := w.Status() |
| 700 |
c.Assert(err, IsNil) |
| 701 |
|
| 702 |
c.Assert(status.IsClean(), Equals, false) |
| 703 |
c.Assert(status, HasLen, 9) |
| 704 |
} |
| 705 |
|
| 706 |
func (s *WorktreeSuite) TestStatusEmpty(c *C) { |
| 707 |
fs := memfs.New() |
| 708 |
storage := memory.NewStorage() |
| 709 |
|
| 710 |
r, err := Init(storage, fs) |
| 711 |
c.Assert(err, IsNil) |
| 712 |
|
| 713 |
w, err := r.Worktree() |
| 714 |
c.Assert(err, IsNil) |
| 715 |
|
| 716 |
status, err := w.Status() |
| 717 |
c.Assert(err, IsNil) |
| 718 |
c.Assert(status.IsClean(), Equals, true) |
| 719 |
c.Assert(status, NotNil) |
| 720 |
} |
| 721 |
|
| 722 |
func (s *WorktreeSuite) TestStatusEmptyDirty(c *C) { |
| 723 |
fs := memfs.New() |
| 724 |
err := util.WriteFile(fs, "foo", []byte("foo"), 0755) |
| 725 |
c.Assert(err, IsNil) |
| 726 |
|
| 727 |
storage := memory.NewStorage() |
| 728 |
|
| 729 |
r, err := Init(storage, fs) |
| 730 |
c.Assert(err, IsNil) |
| 731 |
|
| 732 |
w, err := r.Worktree() |
| 733 |
c.Assert(err, IsNil) |
| 734 |
|
| 735 |
status, err := w.Status() |
| 736 |
c.Assert(err, IsNil) |
| 737 |
c.Assert(status.IsClean(), Equals, false) |
| 738 |
c.Assert(status, HasLen, 1) |
| 739 |
} |
| 740 |
|
| 741 |
func (s *WorktreeSuite) TestReset(c *C) { |
| 742 |
fs := memfs.New() |
| 743 |
w := &Worktree{ |
| 744 |
r: s.Repository, |
| 745 |
Filesystem: fs, |
| 746 |
} |
| 747 |
|
| 748 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 749 |
|
| 750 |
err := w.Checkout(&CheckoutOptions{}) |
| 751 |
c.Assert(err, IsNil) |
| 752 |
|
| 753 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 754 |
c.Assert(err, IsNil) |
| 755 |
c.Assert(branch.Hash(), Not(Equals), commit) |
| 756 |
|
| 757 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commit}) |
| 758 |
c.Assert(err, IsNil) |
| 759 |
|
| 760 |
branch, err = w.r.Reference(plumbing.Master, false) |
| 761 |
c.Assert(err, IsNil) |
| 762 |
c.Assert(branch.Hash(), Equals, commit) |
| 763 |
|
| 764 |
status, err := w.Status() |
| 765 |
c.Assert(err, IsNil) |
| 766 |
c.Assert(status.IsClean(), Equals, true) |
| 767 |
} |
| 768 |
|
| 769 |
func (s *WorktreeSuite) TestResetWithUntracked(c *C) { |
| 770 |
fs := memfs.New() |
| 771 |
w := &Worktree{ |
| 772 |
r: s.Repository, |
| 773 |
Filesystem: fs, |
| 774 |
} |
| 775 |
|
| 776 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 777 |
|
| 778 |
err := w.Checkout(&CheckoutOptions{}) |
| 779 |
c.Assert(err, IsNil) |
| 780 |
|
| 781 |
err = util.WriteFile(fs, "foo", nil, 0755) |
| 782 |
c.Assert(err, IsNil) |
| 783 |
|
| 784 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commit}) |
| 785 |
c.Assert(err, IsNil) |
| 786 |
|
| 787 |
status, err := w.Status() |
| 788 |
c.Assert(err, IsNil) |
| 789 |
c.Assert(status.IsClean(), Equals, true) |
| 790 |
} |
| 791 |
|
| 792 |
func (s *WorktreeSuite) TestResetSoft(c *C) { |
| 793 |
fs := memfs.New() |
| 794 |
w := &Worktree{ |
| 795 |
r: s.Repository, |
| 796 |
Filesystem: fs, |
| 797 |
} |
| 798 |
|
| 799 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 800 |
|
| 801 |
err := w.Checkout(&CheckoutOptions{}) |
| 802 |
c.Assert(err, IsNil) |
| 803 |
|
| 804 |
err = w.Reset(&ResetOptions{Mode: SoftReset, Commit: commit}) |
| 805 |
c.Assert(err, IsNil) |
| 806 |
|
| 807 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 808 |
c.Assert(err, IsNil) |
| 809 |
c.Assert(branch.Hash(), Equals, commit) |
| 810 |
|
| 811 |
status, err := w.Status() |
| 812 |
c.Assert(err, IsNil) |
| 813 |
c.Assert(status.IsClean(), Equals, false) |
| 814 |
c.Assert(status.File("CHANGELOG").Staging, Equals, Added) |
| 815 |
} |
| 816 |
|
| 817 |
func (s *WorktreeSuite) TestResetMixed(c *C) { |
| 818 |
fs := memfs.New() |
| 819 |
w := &Worktree{ |
| 820 |
r: s.Repository, |
| 821 |
Filesystem: fs, |
| 822 |
} |
| 823 |
|
| 824 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 825 |
|
| 826 |
err := w.Checkout(&CheckoutOptions{}) |
| 827 |
c.Assert(err, IsNil) |
| 828 |
|
| 829 |
err = w.Reset(&ResetOptions{Mode: MixedReset, Commit: commit}) |
| 830 |
c.Assert(err, IsNil) |
| 831 |
|
| 832 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 833 |
c.Assert(err, IsNil) |
| 834 |
c.Assert(branch.Hash(), Equals, commit) |
| 835 |
|
| 836 |
status, err := w.Status() |
| 837 |
c.Assert(err, IsNil) |
| 838 |
c.Assert(status.IsClean(), Equals, false) |
| 839 |
c.Assert(status.File("CHANGELOG").Staging, Equals, Untracked) |
| 840 |
} |
| 841 |
|
| 842 |
func (s *WorktreeSuite) TestResetMerge(c *C) { |
| 843 |
fs := memfs.New() |
| 844 |
w := &Worktree{ |
| 845 |
r: s.Repository, |
| 846 |
Filesystem: fs, |
| 847 |
} |
| 848 |
|
| 849 |
commitA := plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294") |
| 850 |
commitB := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 851 |
|
| 852 |
err := w.Checkout(&CheckoutOptions{}) |
| 853 |
c.Assert(err, IsNil) |
| 854 |
|
| 855 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commitA}) |
| 856 |
c.Assert(err, IsNil) |
| 857 |
|
| 858 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 859 |
c.Assert(err, IsNil) |
| 860 |
c.Assert(branch.Hash(), Equals, commitA) |
| 861 |
|
| 862 |
f, err := fs.Create(".gitignore") |
| 863 |
c.Assert(err, IsNil) |
| 864 |
_, err = f.Write([]byte("foo")) |
| 865 |
c.Assert(err, IsNil) |
| 866 |
err = f.Close() |
| 867 |
c.Assert(err, IsNil) |
| 868 |
|
| 869 |
err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commitB}) |
| 870 |
c.Assert(err, Equals, ErrUnstagedChanges) |
| 871 |
|
| 872 |
branch, err = w.r.Reference(plumbing.Master, false) |
| 873 |
c.Assert(err, IsNil) |
| 874 |
c.Assert(branch.Hash(), Equals, commitA) |
| 875 |
} |
| 876 |
|
| 877 |
func (s *WorktreeSuite) TestResetHard(c *C) { |
| 878 |
fs := memfs.New() |
| 879 |
w := &Worktree{ |
| 880 |
r: s.Repository, |
| 881 |
Filesystem: fs, |
| 882 |
} |
| 883 |
|
| 884 |
commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9") |
| 885 |
|
| 886 |
err := w.Checkout(&CheckoutOptions{}) |
| 887 |
c.Assert(err, IsNil) |
| 888 |
|
| 889 |
f, err := fs.Create(".gitignore") |
| 890 |
c.Assert(err, IsNil) |
| 891 |
_, err = f.Write([]byte("foo")) |
| 892 |
c.Assert(err, IsNil) |
| 893 |
err = f.Close() |
| 894 |
c.Assert(err, IsNil) |
| 895 |
|
| 896 |
err = w.Reset(&ResetOptions{Mode: HardReset, Commit: commit}) |
| 897 |
c.Assert(err, IsNil) |
| 898 |
|
| 899 |
branch, err := w.r.Reference(plumbing.Master, false) |
| 900 |
c.Assert(err, IsNil) |
| 901 |
c.Assert(branch.Hash(), Equals, commit) |
| 902 |
} |
| 903 |
|
| 904 |
func (s *WorktreeSuite) TestStatusAfterCheckout(c *C) { |
| 905 |
fs := memfs.New() |
| 906 |
w := &Worktree{ |
| 907 |
r: s.Repository, |
| 908 |
Filesystem: fs, |
| 909 |
} |
| 910 |
|
| 911 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 912 |
c.Assert(err, IsNil) |
| 913 |
|
| 914 |
status, err := w.Status() |
| 915 |
c.Assert(err, IsNil) |
| 916 |
c.Assert(status.IsClean(), Equals, true) |
| 917 |
|
| 918 |
} |
| 919 |
|
| 920 |
func (s *WorktreeSuite) TestStatusModified(c *C) { |
| 921 |
dir, err := ioutil.TempDir("", "status") |
| 922 |
c.Assert(err, IsNil) |
| 923 |
defer os.RemoveAll(dir) |
| 924 |
|
| 925 |
fs := osfs.New(filepath.Join(dir, "worktree")) |
| 926 |
w := &Worktree{ |
| 927 |
r: s.Repository, |
| 928 |
Filesystem: fs, |
| 929 |
} |
| 930 |
|
| 931 |
err = w.Checkout(&CheckoutOptions{}) |
| 932 |
c.Assert(err, IsNil) |
| 933 |
|
| 934 |
f, err := fs.Create(".gitignore") |
| 935 |
c.Assert(err, IsNil) |
| 936 |
_, err = f.Write([]byte("foo")) |
| 937 |
c.Assert(err, IsNil) |
| 938 |
err = f.Close() |
| 939 |
c.Assert(err, IsNil) |
| 940 |
|
| 941 |
status, err := w.Status() |
| 942 |
c.Assert(err, IsNil) |
| 943 |
c.Assert(status.IsClean(), Equals, false) |
| 944 |
c.Assert(status.File(".gitignore").Worktree, Equals, Modified) |
| 945 |
} |
| 946 |
|
| 947 |
func (s *WorktreeSuite) TestStatusIgnored(c *C) { |
| 948 |
fs := memfs.New() |
| 949 |
w := &Worktree{ |
| 950 |
r: s.Repository, |
| 951 |
Filesystem: fs, |
| 952 |
} |
| 953 |
|
| 954 |
w.Checkout(&CheckoutOptions{}) |
| 955 |
|
| 956 |
fs.MkdirAll("another", os.ModePerm) |
| 957 |
f, _ := fs.Create("another/file") |
| 958 |
f.Close() |
| 959 |
fs.MkdirAll("vendor/github.com", os.ModePerm) |
| 960 |
f, _ = fs.Create("vendor/github.com/file") |
| 961 |
f.Close() |
| 962 |
fs.MkdirAll("vendor/gopkg.in", os.ModePerm) |
| 963 |
f, _ = fs.Create("vendor/gopkg.in/file") |
| 964 |
f.Close() |
| 965 |
|
| 966 |
status, _ := w.Status() |
| 967 |
c.Assert(len(status), Equals, 3) |
| 968 |
_, ok := status["another/file"] |
| 969 |
c.Assert(ok, Equals, true) |
| 970 |
_, ok = status["vendor/github.com/file"] |
| 971 |
c.Assert(ok, Equals, true) |
| 972 |
_, ok = status["vendor/gopkg.in/file"] |
| 973 |
c.Assert(ok, Equals, true) |
| 974 |
|
| 975 |
f, _ = fs.Create(".gitignore") |
| 976 |
f.Write([]byte("vendor/g*/")) |
| 977 |
f.Close() |
| 978 |
f, _ = fs.Create("vendor/.gitignore") |
| 979 |
f.Write([]byte("!github.com/\n")) |
| 980 |
f.Close() |
| 981 |
|
| 982 |
status, _ = w.Status() |
| 983 |
c.Assert(len(status), Equals, 4) |
| 984 |
_, ok = status[".gitignore"] |
| 985 |
c.Assert(ok, Equals, true) |
| 986 |
_, ok = status["another/file"] |
| 987 |
c.Assert(ok, Equals, true) |
| 988 |
_, ok = status["vendor/.gitignore"] |
| 989 |
c.Assert(ok, Equals, true) |
| 990 |
_, ok = status["vendor/github.com/file"] |
| 991 |
c.Assert(ok, Equals, true) |
| 992 |
} |
| 993 |
|
| 994 |
func (s *WorktreeSuite) TestStatusUntracked(c *C) { |
| 995 |
fs := memfs.New() |
| 996 |
w := &Worktree{ |
| 997 |
r: s.Repository, |
| 998 |
Filesystem: fs, |
| 999 |
} |
| 1000 |
|
| 1001 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1002 |
c.Assert(err, IsNil) |
| 1003 |
|
| 1004 |
f, err := w.Filesystem.Create("foo") |
| 1005 |
c.Assert(err, IsNil) |
| 1006 |
c.Assert(f.Close(), IsNil) |
| 1007 |
|
| 1008 |
status, err := w.Status() |
| 1009 |
c.Assert(err, IsNil) |
| 1010 |
c.Assert(status.File("foo").Staging, Equals, Untracked) |
| 1011 |
c.Assert(status.File("foo").Worktree, Equals, Untracked) |
| 1012 |
} |
| 1013 |
|
| 1014 |
func (s *WorktreeSuite) TestStatusDeleted(c *C) { |
| 1015 |
dir, err := ioutil.TempDir("", "status") |
| 1016 |
c.Assert(err, IsNil) |
| 1017 |
defer os.RemoveAll(dir) |
| 1018 |
|
| 1019 |
fs := osfs.New(filepath.Join(dir, "worktree")) |
| 1020 |
w := &Worktree{ |
| 1021 |
r: s.Repository, |
| 1022 |
Filesystem: fs, |
| 1023 |
} |
| 1024 |
|
| 1025 |
err = w.Checkout(&CheckoutOptions{}) |
| 1026 |
c.Assert(err, IsNil) |
| 1027 |
|
| 1028 |
err = fs.Remove(".gitignore") |
| 1029 |
c.Assert(err, IsNil) |
| 1030 |
|
| 1031 |
status, err := w.Status() |
| 1032 |
c.Assert(err, IsNil) |
| 1033 |
c.Assert(status.IsClean(), Equals, false) |
| 1034 |
c.Assert(status.File(".gitignore").Worktree, Equals, Deleted) |
| 1035 |
} |
| 1036 |
|
| 1037 |
func (s *WorktreeSuite) TestSubmodule(c *C) { |
| 1038 |
path := fixtures.ByTag("submodule").One().Worktree().Root() |
| 1039 |
r, err := PlainOpen(path) |
| 1040 |
c.Assert(err, IsNil) |
| 1041 |
|
| 1042 |
w, err := r.Worktree() |
| 1043 |
c.Assert(err, IsNil) |
| 1044 |
|
| 1045 |
m, err := w.Submodule("basic") |
| 1046 |
c.Assert(err, IsNil) |
| 1047 |
|
| 1048 |
c.Assert(m.Config().Name, Equals, "basic") |
| 1049 |
} |
| 1050 |
|
| 1051 |
func (s *WorktreeSuite) TestSubmodules(c *C) { |
| 1052 |
path := fixtures.ByTag("submodule").One().Worktree().Root() |
| 1053 |
r, err := PlainOpen(path) |
| 1054 |
c.Assert(err, IsNil) |
| 1055 |
|
| 1056 |
w, err := r.Worktree() |
| 1057 |
c.Assert(err, IsNil) |
| 1058 |
|
| 1059 |
l, err := w.Submodules() |
| 1060 |
c.Assert(err, IsNil) |
| 1061 |
|
| 1062 |
c.Assert(l, HasLen, 2) |
| 1063 |
} |
| 1064 |
|
| 1065 |
func (s *WorktreeSuite) TestAddUntracked(c *C) { |
| 1066 |
fs := memfs.New() |
| 1067 |
w := &Worktree{ |
| 1068 |
r: s.Repository, |
| 1069 |
Filesystem: fs, |
| 1070 |
} |
| 1071 |
|
| 1072 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1073 |
c.Assert(err, IsNil) |
| 1074 |
|
| 1075 |
idx, err := w.r.Storer.Index() |
| 1076 |
c.Assert(err, IsNil) |
| 1077 |
c.Assert(idx.Entries, HasLen, 9) |
| 1078 |
|
| 1079 |
err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) |
| 1080 |
c.Assert(err, IsNil) |
| 1081 |
|
| 1082 |
hash, err := w.Add("foo") |
| 1083 |
c.Assert(hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5") |
| 1084 |
c.Assert(err, IsNil) |
| 1085 |
|
| 1086 |
idx, err = w.r.Storer.Index() |
| 1087 |
c.Assert(err, IsNil) |
| 1088 |
c.Assert(idx.Entries, HasLen, 10) |
| 1089 |
|
| 1090 |
e, err := idx.Entry("foo") |
| 1091 |
c.Assert(err, IsNil) |
| 1092 |
c.Assert(e.Hash, Equals, hash) |
| 1093 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 1094 |
|
| 1095 |
status, err := w.Status() |
| 1096 |
c.Assert(err, IsNil) |
| 1097 |
c.Assert(status, HasLen, 1) |
| 1098 |
|
| 1099 |
file := status.File("foo") |
| 1100 |
c.Assert(file.Staging, Equals, Added) |
| 1101 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1102 |
|
| 1103 |
obj, err := w.r.Storer.EncodedObject(plumbing.BlobObject, hash) |
| 1104 |
c.Assert(err, IsNil) |
| 1105 |
c.Assert(obj, NotNil) |
| 1106 |
c.Assert(obj.Size(), Equals, int64(3)) |
| 1107 |
} |
| 1108 |
|
| 1109 |
func (s *WorktreeSuite) TestIgnored(c *C) { |
| 1110 |
fs := memfs.New() |
| 1111 |
w := &Worktree{ |
| 1112 |
r: s.Repository, |
| 1113 |
Filesystem: fs, |
| 1114 |
} |
| 1115 |
|
| 1116 |
w.Excludes = make([]gitignore.Pattern, 0) |
| 1117 |
w.Excludes = append(w.Excludes, gitignore.ParsePattern("foo", nil)) |
| 1118 |
|
| 1119 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1120 |
c.Assert(err, IsNil) |
| 1121 |
|
| 1122 |
idx, err := w.r.Storer.Index() |
| 1123 |
c.Assert(err, IsNil) |
| 1124 |
c.Assert(idx.Entries, HasLen, 9) |
| 1125 |
|
| 1126 |
err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) |
| 1127 |
c.Assert(err, IsNil) |
| 1128 |
|
| 1129 |
status, err := w.Status() |
| 1130 |
c.Assert(err, IsNil) |
| 1131 |
c.Assert(status, HasLen, 0) |
| 1132 |
|
| 1133 |
file := status.File("foo") |
| 1134 |
c.Assert(file.Staging, Equals, Untracked) |
| 1135 |
c.Assert(file.Worktree, Equals, Untracked) |
| 1136 |
} |
| 1137 |
|
| 1138 |
func (s *WorktreeSuite) TestExcludedNoGitignore(c *C) { |
| 1139 |
f := fixtures.ByTag("empty").One() |
| 1140 |
r := s.NewRepository(f) |
| 1141 |
|
| 1142 |
fs := memfs.New() |
| 1143 |
w := &Worktree{ |
| 1144 |
r: r, |
| 1145 |
Filesystem: fs, |
| 1146 |
} |
| 1147 |
|
| 1148 |
_, err := fs.Open(".gitignore") |
| 1149 |
c.Assert(err, Equals, os.ErrNotExist) |
| 1150 |
|
| 1151 |
w.Excludes = make([]gitignore.Pattern, 0) |
| 1152 |
w.Excludes = append(w.Excludes, gitignore.ParsePattern("foo", nil)) |
| 1153 |
|
| 1154 |
err = util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) |
| 1155 |
c.Assert(err, IsNil) |
| 1156 |
|
| 1157 |
status, err := w.Status() |
| 1158 |
c.Assert(err, IsNil) |
| 1159 |
c.Assert(status, HasLen, 0) |
| 1160 |
|
| 1161 |
file := status.File("foo") |
| 1162 |
c.Assert(file.Staging, Equals, Untracked) |
| 1163 |
c.Assert(file.Worktree, Equals, Untracked) |
| 1164 |
} |
| 1165 |
|
| 1166 |
func (s *WorktreeSuite) TestAddModified(c *C) { |
| 1167 |
fs := memfs.New() |
| 1168 |
w := &Worktree{ |
| 1169 |
r: s.Repository, |
| 1170 |
Filesystem: fs, |
| 1171 |
} |
| 1172 |
|
| 1173 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1174 |
c.Assert(err, IsNil) |
| 1175 |
|
| 1176 |
idx, err := w.r.Storer.Index() |
| 1177 |
c.Assert(err, IsNil) |
| 1178 |
c.Assert(idx.Entries, HasLen, 9) |
| 1179 |
|
| 1180 |
err = util.WriteFile(w.Filesystem, "LICENSE", []byte("FOO"), 0644) |
| 1181 |
c.Assert(err, IsNil) |
| 1182 |
|
| 1183 |
hash, err := w.Add("LICENSE") |
| 1184 |
c.Assert(err, IsNil) |
| 1185 |
c.Assert(hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5") |
| 1186 |
|
| 1187 |
idx, err = w.r.Storer.Index() |
| 1188 |
c.Assert(err, IsNil) |
| 1189 |
c.Assert(idx.Entries, HasLen, 9) |
| 1190 |
|
| 1191 |
e, err := idx.Entry("LICENSE") |
| 1192 |
c.Assert(err, IsNil) |
| 1193 |
c.Assert(e.Hash, Equals, hash) |
| 1194 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1195 |
|
| 1196 |
status, err := w.Status() |
| 1197 |
c.Assert(err, IsNil) |
| 1198 |
c.Assert(status, HasLen, 1) |
| 1199 |
|
| 1200 |
file := status.File("LICENSE") |
| 1201 |
c.Assert(file.Staging, Equals, Modified) |
| 1202 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1203 |
} |
| 1204 |
|
| 1205 |
func (s *WorktreeSuite) TestAddUnmodified(c *C) { |
| 1206 |
fs := memfs.New() |
| 1207 |
w := &Worktree{ |
| 1208 |
r: s.Repository, |
| 1209 |
Filesystem: fs, |
| 1210 |
} |
| 1211 |
|
| 1212 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1213 |
c.Assert(err, IsNil) |
| 1214 |
|
| 1215 |
hash, err := w.Add("LICENSE") |
| 1216 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 1217 |
c.Assert(err, IsNil) |
| 1218 |
} |
| 1219 |
|
| 1220 |
func (s *WorktreeSuite) TestAddRemoved(c *C) { |
| 1221 |
fs := memfs.New() |
| 1222 |
w := &Worktree{ |
| 1223 |
r: s.Repository, |
| 1224 |
Filesystem: fs, |
| 1225 |
} |
| 1226 |
|
| 1227 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1228 |
c.Assert(err, IsNil) |
| 1229 |
|
| 1230 |
idx, err := w.r.Storer.Index() |
| 1231 |
c.Assert(err, IsNil) |
| 1232 |
c.Assert(idx.Entries, HasLen, 9) |
| 1233 |
|
| 1234 |
err = w.Filesystem.Remove("LICENSE") |
| 1235 |
c.Assert(err, IsNil) |
| 1236 |
|
| 1237 |
hash, err := w.Add("LICENSE") |
| 1238 |
c.Assert(err, IsNil) |
| 1239 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 1240 |
|
| 1241 |
e, err := idx.Entry("LICENSE") |
| 1242 |
c.Assert(err, IsNil) |
| 1243 |
c.Assert(e.Hash, Equals, hash) |
| 1244 |
c.Assert(e.Mode, Equals, filemode.Regular) |
| 1245 |
|
| 1246 |
status, err := w.Status() |
| 1247 |
c.Assert(err, IsNil) |
| 1248 |
c.Assert(status, HasLen, 1) |
| 1249 |
|
| 1250 |
file := status.File("LICENSE") |
| 1251 |
c.Assert(file.Staging, Equals, Deleted) |
| 1252 |
} |
| 1253 |
|
| 1254 |
func (s *WorktreeSuite) TestAddSymlink(c *C) { |
| 1255 |
dir, err := ioutil.TempDir("", "checkout") |
| 1256 |
c.Assert(err, IsNil) |
| 1257 |
defer os.RemoveAll(dir) |
| 1258 |
|
| 1259 |
r, err := PlainInit(dir, false) |
| 1260 |
c.Assert(err, IsNil) |
| 1261 |
err = util.WriteFile(r.wt, "foo", []byte("qux"), 0644) |
| 1262 |
c.Assert(err, IsNil) |
| 1263 |
err = r.wt.Symlink("foo", "bar") |
| 1264 |
c.Assert(err, IsNil) |
| 1265 |
|
| 1266 |
w, err := r.Worktree() |
| 1267 |
c.Assert(err, IsNil) |
| 1268 |
h, err := w.Add("foo") |
| 1269 |
c.Assert(err, IsNil) |
| 1270 |
c.Assert(h, Not(Equals), plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c")) |
| 1271 |
|
| 1272 |
h, err = w.Add("bar") |
| 1273 |
c.Assert(err, IsNil) |
| 1274 |
c.Assert(h, Equals, plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c")) |
| 1275 |
|
| 1276 |
obj, err := w.r.Storer.EncodedObject(plumbing.BlobObject, h) |
| 1277 |
c.Assert(err, IsNil) |
| 1278 |
c.Assert(obj, NotNil) |
| 1279 |
c.Assert(obj.Size(), Equals, int64(3)) |
| 1280 |
} |
| 1281 |
|
| 1282 |
func (s *WorktreeSuite) TestAddDirectory(c *C) { |
| 1283 |
fs := memfs.New() |
| 1284 |
w := &Worktree{ |
| 1285 |
r: s.Repository, |
| 1286 |
Filesystem: fs, |
| 1287 |
} |
| 1288 |
|
| 1289 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1290 |
c.Assert(err, IsNil) |
| 1291 |
|
| 1292 |
idx, err := w.r.Storer.Index() |
| 1293 |
c.Assert(err, IsNil) |
| 1294 |
c.Assert(idx.Entries, HasLen, 9) |
| 1295 |
|
| 1296 |
err = util.WriteFile(w.Filesystem, "qux/foo", []byte("FOO"), 0755) |
| 1297 |
c.Assert(err, IsNil) |
| 1298 |
err = util.WriteFile(w.Filesystem, "qux/baz/bar", []byte("BAR"), 0755) |
| 1299 |
c.Assert(err, IsNil) |
| 1300 |
|
| 1301 |
h, err := w.Add("qux") |
| 1302 |
c.Assert(err, IsNil) |
| 1303 |
c.Assert(h.IsZero(), Equals, true) |
| 1304 |
|
| 1305 |
idx, err = w.r.Storer.Index() |
| 1306 |
c.Assert(err, IsNil) |
| 1307 |
c.Assert(idx.Entries, HasLen, 11) |
| 1308 |
|
| 1309 |
e, err := idx.Entry("qux/foo") |
| 1310 |
c.Assert(err, IsNil) |
| 1311 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 1312 |
|
| 1313 |
e, err = idx.Entry("qux/baz/bar") |
| 1314 |
c.Assert(err, IsNil) |
| 1315 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 1316 |
|
| 1317 |
status, err := w.Status() |
| 1318 |
c.Assert(err, IsNil) |
| 1319 |
c.Assert(status, HasLen, 2) |
| 1320 |
|
| 1321 |
file := status.File("qux/foo") |
| 1322 |
c.Assert(file.Staging, Equals, Added) |
| 1323 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1324 |
|
| 1325 |
file = status.File("qux/baz/bar") |
| 1326 |
c.Assert(file.Staging, Equals, Added) |
| 1327 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1328 |
} |
| 1329 |
|
| 1330 |
func (s *WorktreeSuite) TestAddDirectoryErrorNotFound(c *C) { |
| 1331 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 1332 |
w, _ := r.Worktree() |
| 1333 |
|
| 1334 |
h, err := w.Add("foo") |
| 1335 |
c.Assert(err, NotNil) |
| 1336 |
c.Assert(h.IsZero(), Equals, true) |
| 1337 |
} |
| 1338 |
|
| 1339 |
func (s *WorktreeSuite) TestAddGlob(c *C) { |
| 1340 |
fs := memfs.New() |
| 1341 |
w := &Worktree{ |
| 1342 |
r: s.Repository, |
| 1343 |
Filesystem: fs, |
| 1344 |
} |
| 1345 |
|
| 1346 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1347 |
c.Assert(err, IsNil) |
| 1348 |
|
| 1349 |
idx, err := w.r.Storer.Index() |
| 1350 |
c.Assert(err, IsNil) |
| 1351 |
c.Assert(idx.Entries, HasLen, 9) |
| 1352 |
|
| 1353 |
err = util.WriteFile(w.Filesystem, "qux/qux", []byte("QUX"), 0755) |
| 1354 |
c.Assert(err, IsNil) |
| 1355 |
err = util.WriteFile(w.Filesystem, "qux/baz", []byte("BAZ"), 0755) |
| 1356 |
c.Assert(err, IsNil) |
| 1357 |
err = util.WriteFile(w.Filesystem, "qux/bar/baz", []byte("BAZ"), 0755) |
| 1358 |
c.Assert(err, IsNil) |
| 1359 |
|
| 1360 |
err = w.AddGlob(w.Filesystem.Join("qux", "b*")) |
| 1361 |
c.Assert(err, IsNil) |
| 1362 |
|
| 1363 |
idx, err = w.r.Storer.Index() |
| 1364 |
c.Assert(err, IsNil) |
| 1365 |
c.Assert(idx.Entries, HasLen, 11) |
| 1366 |
|
| 1367 |
e, err := idx.Entry("qux/baz") |
| 1368 |
c.Assert(err, IsNil) |
| 1369 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 1370 |
|
| 1371 |
e, err = idx.Entry("qux/bar/baz") |
| 1372 |
c.Assert(err, IsNil) |
| 1373 |
c.Assert(e.Mode, Equals, filemode.Executable) |
| 1374 |
|
| 1375 |
status, err := w.Status() |
| 1376 |
c.Assert(err, IsNil) |
| 1377 |
c.Assert(status, HasLen, 3) |
| 1378 |
|
| 1379 |
file := status.File("qux/qux") |
| 1380 |
c.Assert(file.Staging, Equals, Untracked) |
| 1381 |
c.Assert(file.Worktree, Equals, Untracked) |
| 1382 |
|
| 1383 |
file = status.File("qux/baz") |
| 1384 |
c.Assert(file.Staging, Equals, Added) |
| 1385 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1386 |
|
| 1387 |
file = status.File("qux/bar/baz") |
| 1388 |
c.Assert(file.Staging, Equals, Added) |
| 1389 |
c.Assert(file.Worktree, Equals, Unmodified) |
| 1390 |
} |
| 1391 |
|
| 1392 |
func (s *WorktreeSuite) TestAddGlobErrorNoMatches(c *C) { |
| 1393 |
r, _ := Init(memory.NewStorage(), memfs.New()) |
| 1394 |
w, _ := r.Worktree() |
| 1395 |
|
| 1396 |
err := w.AddGlob("foo") |
| 1397 |
c.Assert(err, Equals, ErrGlobNoMatches) |
| 1398 |
} |
| 1399 |
|
| 1400 |
func (s *WorktreeSuite) TestRemove(c *C) { |
| 1401 |
fs := memfs.New() |
| 1402 |
w := &Worktree{ |
| 1403 |
r: s.Repository, |
| 1404 |
Filesystem: fs, |
| 1405 |
} |
| 1406 |
|
| 1407 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1408 |
c.Assert(err, IsNil) |
| 1409 |
|
| 1410 |
hash, err := w.Remove("LICENSE") |
| 1411 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 1412 |
c.Assert(err, IsNil) |
| 1413 |
|
| 1414 |
status, err := w.Status() |
| 1415 |
c.Assert(err, IsNil) |
| 1416 |
c.Assert(status, HasLen, 1) |
| 1417 |
c.Assert(status.File("LICENSE").Staging, Equals, Deleted) |
| 1418 |
} |
| 1419 |
|
| 1420 |
func (s *WorktreeSuite) TestRemoveNotExistentEntry(c *C) { |
| 1421 |
fs := memfs.New() |
| 1422 |
w := &Worktree{ |
| 1423 |
r: s.Repository, |
| 1424 |
Filesystem: fs, |
| 1425 |
} |
| 1426 |
|
| 1427 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1428 |
c.Assert(err, IsNil) |
| 1429 |
|
| 1430 |
hash, err := w.Remove("not-exists") |
| 1431 |
c.Assert(hash.IsZero(), Equals, true) |
| 1432 |
c.Assert(err, NotNil) |
| 1433 |
} |
| 1434 |
|
| 1435 |
func (s *WorktreeSuite) TestRemoveDirectory(c *C) { |
| 1436 |
fs := memfs.New() |
| 1437 |
w := &Worktree{ |
| 1438 |
r: s.Repository, |
| 1439 |
Filesystem: fs, |
| 1440 |
} |
| 1441 |
|
| 1442 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1443 |
c.Assert(err, IsNil) |
| 1444 |
|
| 1445 |
hash, err := w.Remove("json") |
| 1446 |
c.Assert(hash.IsZero(), Equals, true) |
| 1447 |
c.Assert(err, IsNil) |
| 1448 |
|
| 1449 |
status, err := w.Status() |
| 1450 |
c.Assert(err, IsNil) |
| 1451 |
c.Assert(status, HasLen, 2) |
| 1452 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 1453 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 1454 |
|
| 1455 |
_, err = w.Filesystem.Stat("json") |
| 1456 |
c.Assert(os.IsNotExist(err), Equals, true) |
| 1457 |
} |
| 1458 |
|
| 1459 |
func (s *WorktreeSuite) TestRemoveDirectoryUntracked(c *C) { |
| 1460 |
fs := memfs.New() |
| 1461 |
w := &Worktree{ |
| 1462 |
r: s.Repository, |
| 1463 |
Filesystem: fs, |
| 1464 |
} |
| 1465 |
|
| 1466 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1467 |
c.Assert(err, IsNil) |
| 1468 |
|
| 1469 |
err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755) |
| 1470 |
c.Assert(err, IsNil) |
| 1471 |
|
| 1472 |
hash, err := w.Remove("json") |
| 1473 |
c.Assert(hash.IsZero(), Equals, true) |
| 1474 |
c.Assert(err, IsNil) |
| 1475 |
|
| 1476 |
status, err := w.Status() |
| 1477 |
c.Assert(err, IsNil) |
| 1478 |
c.Assert(status, HasLen, 3) |
| 1479 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 1480 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 1481 |
c.Assert(status.File("json/foo").Staging, Equals, Untracked) |
| 1482 |
|
| 1483 |
_, err = w.Filesystem.Stat("json") |
| 1484 |
c.Assert(err, IsNil) |
| 1485 |
} |
| 1486 |
|
| 1487 |
func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) { |
| 1488 |
fs := memfs.New() |
| 1489 |
w := &Worktree{ |
| 1490 |
r: s.Repository, |
| 1491 |
Filesystem: fs, |
| 1492 |
} |
| 1493 |
|
| 1494 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1495 |
c.Assert(err, IsNil) |
| 1496 |
|
| 1497 |
err = fs.Remove("LICENSE") |
| 1498 |
c.Assert(err, IsNil) |
| 1499 |
|
| 1500 |
hash, err := w.Remove("LICENSE") |
| 1501 |
c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 1502 |
c.Assert(err, IsNil) |
| 1503 |
|
| 1504 |
status, err := w.Status() |
| 1505 |
c.Assert(err, IsNil) |
| 1506 |
c.Assert(status, HasLen, 1) |
| 1507 |
c.Assert(status.File("LICENSE").Staging, Equals, Deleted) |
| 1508 |
} |
| 1509 |
|
| 1510 |
func (s *WorktreeSuite) TestRemoveGlob(c *C) { |
| 1511 |
fs := memfs.New() |
| 1512 |
w := &Worktree{ |
| 1513 |
r: s.Repository, |
| 1514 |
Filesystem: fs, |
| 1515 |
} |
| 1516 |
|
| 1517 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1518 |
c.Assert(err, IsNil) |
| 1519 |
|
| 1520 |
err = w.RemoveGlob(w.Filesystem.Join("json", "l*")) |
| 1521 |
c.Assert(err, IsNil) |
| 1522 |
|
| 1523 |
status, err := w.Status() |
| 1524 |
c.Assert(err, IsNil) |
| 1525 |
c.Assert(status, HasLen, 1) |
| 1526 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 1527 |
} |
| 1528 |
|
| 1529 |
func (s *WorktreeSuite) TestRemoveGlobDirectory(c *C) { |
| 1530 |
fs := memfs.New() |
| 1531 |
w := &Worktree{ |
| 1532 |
r: s.Repository, |
| 1533 |
Filesystem: fs, |
| 1534 |
} |
| 1535 |
|
| 1536 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1537 |
c.Assert(err, IsNil) |
| 1538 |
|
| 1539 |
err = w.RemoveGlob("js*") |
| 1540 |
c.Assert(err, IsNil) |
| 1541 |
|
| 1542 |
status, err := w.Status() |
| 1543 |
c.Assert(err, IsNil) |
| 1544 |
c.Assert(status, HasLen, 2) |
| 1545 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 1546 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 1547 |
|
| 1548 |
_, err = w.Filesystem.Stat("json") |
| 1549 |
c.Assert(os.IsNotExist(err), Equals, true) |
| 1550 |
} |
| 1551 |
|
| 1552 |
func (s *WorktreeSuite) TestRemoveGlobDirectoryDeleted(c *C) { |
| 1553 |
fs := memfs.New() |
| 1554 |
w := &Worktree{ |
| 1555 |
r: s.Repository, |
| 1556 |
Filesystem: fs, |
| 1557 |
} |
| 1558 |
|
| 1559 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1560 |
c.Assert(err, IsNil) |
| 1561 |
|
| 1562 |
err = fs.Remove("json/short.json") |
| 1563 |
c.Assert(err, IsNil) |
| 1564 |
|
| 1565 |
err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755) |
| 1566 |
c.Assert(err, IsNil) |
| 1567 |
|
| 1568 |
err = w.RemoveGlob("js*") |
| 1569 |
c.Assert(err, IsNil) |
| 1570 |
|
| 1571 |
status, err := w.Status() |
| 1572 |
c.Assert(err, IsNil) |
| 1573 |
c.Assert(status, HasLen, 3) |
| 1574 |
c.Assert(status.File("json/short.json").Staging, Equals, Deleted) |
| 1575 |
c.Assert(status.File("json/long.json").Staging, Equals, Deleted) |
| 1576 |
} |
| 1577 |
|
| 1578 |
func (s *WorktreeSuite) TestMove(c *C) { |
| 1579 |
fs := memfs.New() |
| 1580 |
w := &Worktree{ |
| 1581 |
r: s.Repository, |
| 1582 |
Filesystem: fs, |
| 1583 |
} |
| 1584 |
|
| 1585 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1586 |
c.Assert(err, IsNil) |
| 1587 |
|
| 1588 |
hash, err := w.Move("LICENSE", "foo") |
| 1589 |
c.Check(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") |
| 1590 |
c.Assert(err, IsNil) |
| 1591 |
|
| 1592 |
status, err := w.Status() |
| 1593 |
c.Assert(err, IsNil) |
| 1594 |
c.Assert(status, HasLen, 2) |
| 1595 |
c.Assert(status.File("LICENSE").Staging, Equals, Deleted) |
| 1596 |
c.Assert(status.File("foo").Staging, Equals, Added) |
| 1597 |
|
| 1598 |
} |
| 1599 |
|
| 1600 |
func (s *WorktreeSuite) TestMoveNotExistentEntry(c *C) { |
| 1601 |
fs := memfs.New() |
| 1602 |
w := &Worktree{ |
| 1603 |
r: s.Repository, |
| 1604 |
Filesystem: fs, |
| 1605 |
} |
| 1606 |
|
| 1607 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1608 |
c.Assert(err, IsNil) |
| 1609 |
|
| 1610 |
hash, err := w.Move("not-exists", "foo") |
| 1611 |
c.Assert(hash.IsZero(), Equals, true) |
| 1612 |
c.Assert(err, NotNil) |
| 1613 |
} |
| 1614 |
|
| 1615 |
func (s *WorktreeSuite) TestMoveToExistent(c *C) { |
| 1616 |
fs := memfs.New() |
| 1617 |
w := &Worktree{ |
| 1618 |
r: s.Repository, |
| 1619 |
Filesystem: fs, |
| 1620 |
} |
| 1621 |
|
| 1622 |
err := w.Checkout(&CheckoutOptions{Force: true}) |
| 1623 |
c.Assert(err, IsNil) |
| 1624 |
|
| 1625 |
hash, err := w.Move(".gitignore", "LICENSE") |
| 1626 |
c.Assert(hash.IsZero(), Equals, true) |
| 1627 |
c.Assert(err, Equals, ErrDestinationExists) |
| 1628 |
} |
| 1629 |
|
| 1630 |
func (s *WorktreeSuite) TestClean(c *C) { |
| 1631 |
fs := fixtures.ByTag("dirty").One().Worktree() |
| 1632 |
|
| 1633 |
// Open the repo. |
| 1634 |
fs, err := fs.Chroot("repo") |
| 1635 |
c.Assert(err, IsNil) |
| 1636 |
r, err := PlainOpen(fs.Root()) |
| 1637 |
c.Assert(err, IsNil) |
| 1638 |
|
| 1639 |
wt, err := r.Worktree() |
| 1640 |
c.Assert(err, IsNil) |
| 1641 |
|
| 1642 |
// Status before cleaning. |
| 1643 |
status, err := wt.Status() |
| 1644 |
c.Assert(len(status), Equals, 2) |
| 1645 |
|
| 1646 |
err = wt.Clean(&CleanOptions{}) |
| 1647 |
c.Assert(err, IsNil) |
| 1648 |
|
| 1649 |
// Status after cleaning. |
| 1650 |
status, err = wt.Status() |
| 1651 |
c.Assert(err, IsNil) |
| 1652 |
|
| 1653 |
c.Assert(len(status), Equals, 1) |
| 1654 |
|
| 1655 |
fi, err := fs.Lstat("pkgA") |
| 1656 |
c.Assert(err, IsNil) |
| 1657 |
c.Assert(fi.IsDir(), Equals, true) |
| 1658 |
|
| 1659 |
// Clean with Dir: true. |
| 1660 |
err = wt.Clean(&CleanOptions{Dir: true}) |
| 1661 |
c.Assert(err, IsNil) |
| 1662 |
|
| 1663 |
status, err = wt.Status() |
| 1664 |
c.Assert(err, IsNil) |
| 1665 |
|
| 1666 |
c.Assert(len(status), Equals, 0) |
| 1667 |
|
| 1668 |
// An empty dir should be deleted, as well. |
| 1669 |
_, err = fs.Lstat("pkgA") |
| 1670 |
c.Assert(err, ErrorMatches, ".*(no such file or directory.*|.*file does not exist)*.") |
| 1671 |
|
| 1672 |
} |
| 1673 |
|
| 1674 |
func (s *WorktreeSuite) TestAlternatesRepo(c *C) { |
| 1675 |
fs := fixtures.ByTag("alternates").One().Worktree() |
| 1676 |
|
| 1677 |
// Open 1st repo. |
| 1678 |
rep1fs, err := fs.Chroot("rep1") |
| 1679 |
c.Assert(err, IsNil) |
| 1680 |
rep1, err := PlainOpen(rep1fs.Root()) |
| 1681 |
c.Assert(err, IsNil) |
| 1682 |
|
| 1683 |
// Open 2nd repo. |
| 1684 |
rep2fs, err := fs.Chroot("rep2") |
| 1685 |
c.Assert(err, IsNil) |
| 1686 |
rep2, err := PlainOpen(rep2fs.Root()) |
| 1687 |
c.Assert(err, IsNil) |
| 1688 |
|
| 1689 |
// Get the HEAD commit from the main repo. |
| 1690 |
h, err := rep1.Head() |
| 1691 |
c.Assert(err, IsNil) |
| 1692 |
commit1, err := rep1.CommitObject(h.Hash()) |
| 1693 |
c.Assert(err, IsNil) |
| 1694 |
|
| 1695 |
// Get the HEAD commit from the shared repo. |
| 1696 |
h, err = rep2.Head() |
| 1697 |
c.Assert(err, IsNil) |
| 1698 |
commit2, err := rep2.CommitObject(h.Hash()) |
| 1699 |
c.Assert(err, IsNil) |
| 1700 |
|
| 1701 |
c.Assert(commit1.String(), Equals, commit2.String()) |
| 1702 |
} |
| 1703 |
|
| 1704 |
func (s *WorktreeSuite) TestGrep(c *C) { |
| 1705 |
cases := []struct { |
| 1706 |
name string |
| 1707 |
options GrepOptions |
| 1708 |
wantResult []GrepResult |
| 1709 |
dontWantResult []GrepResult |
| 1710 |
wantError error |
| 1711 |
}{ |
| 1712 |
{ |
| 1713 |
name: "basic word match", |
| 1714 |
options: GrepOptions{ |
| 1715 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 1716 |
}, |
| 1717 |
wantResult: []GrepResult{ |
| 1718 |
{ |
| 1719 |
FileName: "go/example.go", |
| 1720 |
LineNumber: 3, |
| 1721 |
Content: "import (", |
| 1722 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1723 |
}, |
| 1724 |
{ |
| 1725 |
FileName: "vendor/foo.go", |
| 1726 |
LineNumber: 3, |
| 1727 |
Content: "import \"fmt\"", |
| 1728 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1729 |
}, |
| 1730 |
}, |
| 1731 |
}, { |
| 1732 |
name: "case insensitive match", |
| 1733 |
options: GrepOptions{ |
| 1734 |
Patterns: []*regexp.Regexp{regexp.MustCompile(`(?i)IMport`)}, |
| 1735 |
}, |
| 1736 |
wantResult: []GrepResult{ |
| 1737 |
{ |
| 1738 |
FileName: "go/example.go", |
| 1739 |
LineNumber: 3, |
| 1740 |
Content: "import (", |
| 1741 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1742 |
}, |
| 1743 |
{ |
| 1744 |
FileName: "vendor/foo.go", |
| 1745 |
LineNumber: 3, |
| 1746 |
Content: "import \"fmt\"", |
| 1747 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1748 |
}, |
| 1749 |
}, |
| 1750 |
}, { |
| 1751 |
name: "invert match", |
| 1752 |
options: GrepOptions{ |
| 1753 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 1754 |
InvertMatch: true, |
| 1755 |
}, |
| 1756 |
dontWantResult: []GrepResult{ |
| 1757 |
{ |
| 1758 |
FileName: "go/example.go", |
| 1759 |
LineNumber: 3, |
| 1760 |
Content: "import (", |
| 1761 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1762 |
}, |
| 1763 |
{ |
| 1764 |
FileName: "vendor/foo.go", |
| 1765 |
LineNumber: 3, |
| 1766 |
Content: "import \"fmt\"", |
| 1767 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1768 |
}, |
| 1769 |
}, |
| 1770 |
}, { |
| 1771 |
name: "match at a given commit hash", |
| 1772 |
options: GrepOptions{ |
| 1773 |
Patterns: []*regexp.Regexp{regexp.MustCompile("The MIT License")}, |
| 1774 |
CommitHash: plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"), |
| 1775 |
}, |
| 1776 |
wantResult: []GrepResult{ |
| 1777 |
{ |
| 1778 |
FileName: "LICENSE", |
| 1779 |
LineNumber: 1, |
| 1780 |
Content: "The MIT License (MIT)", |
| 1781 |
TreeName: "b029517f6300c2da0f4b651b8642506cd6aaf45d", |
| 1782 |
}, |
| 1783 |
}, |
| 1784 |
dontWantResult: []GrepResult{ |
| 1785 |
{ |
| 1786 |
FileName: "go/example.go", |
| 1787 |
LineNumber: 3, |
| 1788 |
Content: "import (", |
| 1789 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1790 |
}, |
| 1791 |
}, |
| 1792 |
}, { |
| 1793 |
name: "match for a given pathspec", |
| 1794 |
options: GrepOptions{ |
| 1795 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 1796 |
PathSpecs: []*regexp.Regexp{regexp.MustCompile("go/")}, |
| 1797 |
}, |
| 1798 |
wantResult: []GrepResult{ |
| 1799 |
{ |
| 1800 |
FileName: "go/example.go", |
| 1801 |
LineNumber: 3, |
| 1802 |
Content: "import (", |
| 1803 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1804 |
}, |
| 1805 |
}, |
| 1806 |
dontWantResult: []GrepResult{ |
| 1807 |
{ |
| 1808 |
FileName: "vendor/foo.go", |
| 1809 |
LineNumber: 3, |
| 1810 |
Content: "import \"fmt\"", |
| 1811 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1812 |
}, |
| 1813 |
}, |
| 1814 |
}, { |
| 1815 |
name: "match at a given reference name", |
| 1816 |
options: GrepOptions{ |
| 1817 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 1818 |
ReferenceName: "refs/heads/master", |
| 1819 |
}, |
| 1820 |
wantResult: []GrepResult{ |
| 1821 |
{ |
| 1822 |
FileName: "go/example.go", |
| 1823 |
LineNumber: 3, |
| 1824 |
Content: "import (", |
| 1825 |
TreeName: "refs/heads/master", |
| 1826 |
}, |
| 1827 |
}, |
| 1828 |
}, { |
| 1829 |
name: "ambiguous options", |
| 1830 |
options: GrepOptions{ |
| 1831 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 1832 |
CommitHash: plumbing.NewHash("2d55a722f3c3ecc36da919dfd8b6de38352f3507"), |
| 1833 |
ReferenceName: "somereferencename", |
| 1834 |
}, |
| 1835 |
wantError: ErrHashOrReference, |
| 1836 |
}, { |
| 1837 |
name: "multiple patterns", |
| 1838 |
options: GrepOptions{ |
| 1839 |
Patterns: []*regexp.Regexp{ |
| 1840 |
regexp.MustCompile("import"), |
| 1841 |
regexp.MustCompile("License"), |
| 1842 |
}, |
| 1843 |
}, |
| 1844 |
wantResult: []GrepResult{ |
| 1845 |
{ |
| 1846 |
FileName: "go/example.go", |
| 1847 |
LineNumber: 3, |
| 1848 |
Content: "import (", |
| 1849 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1850 |
}, |
| 1851 |
{ |
| 1852 |
FileName: "vendor/foo.go", |
| 1853 |
LineNumber: 3, |
| 1854 |
Content: "import \"fmt\"", |
| 1855 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1856 |
}, |
| 1857 |
{ |
| 1858 |
FileName: "LICENSE", |
| 1859 |
LineNumber: 1, |
| 1860 |
Content: "The MIT License (MIT)", |
| 1861 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1862 |
}, |
| 1863 |
}, |
| 1864 |
}, { |
| 1865 |
name: "multiple pathspecs", |
| 1866 |
options: GrepOptions{ |
| 1867 |
Patterns: []*regexp.Regexp{regexp.MustCompile("import")}, |
| 1868 |
PathSpecs: []*regexp.Regexp{ |
| 1869 |
regexp.MustCompile("go/"), |
| 1870 |
regexp.MustCompile("vendor/"), |
| 1871 |
}, |
| 1872 |
}, |
| 1873 |
wantResult: []GrepResult{ |
| 1874 |
{ |
| 1875 |
FileName: "go/example.go", |
| 1876 |
LineNumber: 3, |
| 1877 |
Content: "import (", |
| 1878 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1879 |
}, |
| 1880 |
{ |
| 1881 |
FileName: "vendor/foo.go", |
| 1882 |
LineNumber: 3, |
| 1883 |
Content: "import \"fmt\"", |
| 1884 |
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 1885 |
}, |
| 1886 |
}, |
| 1887 |
}, |
| 1888 |
} |
| 1889 |
|
| 1890 |
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root() |
| 1891 |
server, err := PlainClone(c.MkDir(), false, &CloneOptions{ |
| 1892 |
URL: path, |
| 1893 |
}) |
| 1894 |
c.Assert(err, IsNil) |
| 1895 |
|
| 1896 |
w, err := server.Worktree() |
| 1897 |
c.Assert(err, IsNil) |
| 1898 |
|
| 1899 |
for _, tc := range cases { |
| 1900 |
gr, err := w.Grep(&tc.options) |
| 1901 |
if tc.wantError != nil { |
| 1902 |
c.Assert(err, Equals, tc.wantError) |
| 1903 |
} else { |
| 1904 |
c.Assert(err, IsNil) |
| 1905 |
} |
| 1906 |
|
| 1907 |
// Iterate through the results and check if the wanted result is present |
| 1908 |
// in the got result. |
| 1909 |
for _, wantResult := range tc.wantResult { |
| 1910 |
found := false |
| 1911 |
for _, gotResult := range gr { |
| 1912 |
if wantResult == gotResult { |
| 1913 |
found = true |
| 1914 |
break |
| 1915 |
} |
| 1916 |
} |
| 1917 |
if !found { |
| 1918 |
c.Errorf("unexpected grep results for %q, expected result to contain: %v", tc.name, wantResult) |
| 1919 |
} |
| 1920 |
} |
| 1921 |
|
| 1922 |
// Iterate through the results and check if the not wanted result is |
| 1923 |
// present in the got result. |
| 1924 |
for _, dontWantResult := range tc.dontWantResult { |
| 1925 |
found := false |
| 1926 |
for _, gotResult := range gr { |
| 1927 |
if dontWantResult == gotResult { |
| 1928 |
found = true |
| 1929 |
break |
| 1930 |
} |
| 1931 |
} |
| 1932 |
if found { |
| 1933 |
c.Errorf("unexpected grep results for %q, expected result to NOT contain: %v", tc.name, dontWantResult) |
| 1934 |
} |
| 1935 |
} |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
func (s *WorktreeSuite) TestAddAndCommit(c *C) { |
| 1940 |
dir, err := ioutil.TempDir("", "plain-repo") |
| 1941 |
c.Assert(err, IsNil) |
| 1942 |
defer os.RemoveAll(dir) |
| 1943 |
|
| 1944 |
repo, err := PlainInit(dir, false) |
| 1945 |
c.Assert(err, IsNil) |
| 1946 |
|
| 1947 |
w, err := repo.Worktree() |
| 1948 |
c.Assert(err, IsNil) |
| 1949 |
|
| 1950 |
_, err = w.Add(".") |
| 1951 |
c.Assert(err, IsNil) |
| 1952 |
|
| 1953 |
w.Commit("Test Add And Commit", &CommitOptions{Author: &object.Signature{ |
| 1954 |
Name: "foo", |
| 1955 |
Email: "[email protected]", |
| 1956 |
When: time.Now(), |
| 1957 |
}}) |
| 1958 |
|
| 1959 |
iter, err := w.r.Log(&LogOptions{}) |
| 1960 |
c.Assert(err, IsNil) |
| 1961 |
err = iter.ForEach(func(c *object.Commit) error { |
| 1962 |
files, err := c.Files() |
| 1963 |
if err != nil { |
| 1964 |
return err |
| 1965 |
} |
| 1966 |
|
| 1967 |
err = files.ForEach(func(f *object.File) error { |
| 1968 |
return errors.New("Expected no files, got at least 1") |
| 1969 |
}) |
| 1970 |
return err |
| 1971 |
}) |
| 1972 |
c.Assert(err, IsNil) |
| 1973 |
} |