fork of go-git with some jj specific features
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #86 from jk2K/master

feat: add file with using .gitignore, fixed src-d/go-git#1219

authored by

Máximo Cuadros and committed by
GitHub
390968ce 96a108e3

+117 -36
+6
options.go
··· 373 373 ErrMissingAuthor = errors.New("author field is required") 374 374 ) 375 375 376 + // AddOptions describes how a add operation should be performed 377 + type AddOptions struct { 378 + All bool 379 + Path string 380 + } 381 + 376 382 // CommitOptions describes how a commit operation should be performed. 377 383 type CommitOptions struct { 378 384 // All automatically stage files that have been modified and deleted, but
+65 -36
worktree_status.go
··· 7 7 "os" 8 8 "path" 9 9 "path/filepath" 10 + "strings" 10 11 11 12 "github.com/go-git/go-billy/v5/util" 12 13 "github.com/go-git/go-git/v5/plumbing" ··· 264 265 // the worktree to the index. If any of the files is already staged in the index 265 266 // no error is returned. When path is a file, the blob.Hash is returned. 266 267 func (w *Worktree) Add(path string) (plumbing.Hash, error) { 267 - // TODO(mcuadros): remove plumbing.Hash from signature at v5. 268 - s, err := w.Status() 269 - if err != nil { 270 - return plumbing.ZeroHash, err 271 - } 272 - 273 - idx, err := w.r.Storer.Index() 274 - if err != nil { 275 - return plumbing.ZeroHash, err 276 - } 277 - 278 - var h plumbing.Hash 279 - var added bool 280 - 281 - fi, err := w.Filesystem.Lstat(path) 282 - if err != nil || !fi.IsDir() { 283 - added, h, err = w.doAddFile(idx, s, path) 284 - } else { 285 - added, err = w.doAddDirectory(idx, s, path) 286 - } 287 - 288 - if err != nil { 289 - return h, err 290 - } 291 - 292 - if !added { 293 - return h, nil 294 - } 295 - 296 - return h, w.r.Storer.SetIndex(idx) 268 + return w.doAdd(path, make([]gitignore.Pattern, 0)) 297 269 } 298 270 299 - func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string) (added bool, err error) { 271 + func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string, ignorePattern []gitignore.Pattern) (added bool, err error) { 300 272 files, err := w.Filesystem.ReadDir(directory) 301 273 if err != nil { 302 274 return false, err 303 275 } 276 + if len(ignorePattern) > 0 { 277 + m := gitignore.NewMatcher(ignorePattern) 278 + matchPath := strings.Split(directory, string(os.PathSeparator)) 279 + if m.Match(matchPath, true) { 280 + // ignore 281 + return false, nil 282 + } 283 + } 304 284 305 285 for _, file := range files { 306 286 name := path.Join(directory, file.Name()) ··· 311 291 // ignore special git directory 312 292 continue 313 293 } 314 - a, err = w.doAddDirectory(idx, s, name) 294 + a, err = w.doAddDirectory(idx, s, name, ignorePattern) 315 295 } else { 316 - a, _, err = w.doAddFile(idx, s, name) 296 + a, _, err = w.doAddFile(idx, s, name, ignorePattern) 317 297 } 318 298 319 299 if err != nil { ··· 328 308 return 329 309 } 330 310 311 + // add changes from all tracked and untracked files 312 + func (w *Worktree) AddWithOptions(opts *AddOptions) (plumbing.Hash, error) { 313 + if opts.All { 314 + return w.doAdd(".", w.Excludes) 315 + } 316 + return w.Add(opts.Path) 317 + } 318 + 319 + func (w *Worktree) doAdd(path string, ignorePattern []gitignore.Pattern) (plumbing.Hash, error) { 320 + // TODO(mcuadros): remove plumbing.Hash from signature at v5. 321 + s, err := w.Status() 322 + if err != nil { 323 + return plumbing.ZeroHash, err 324 + } 325 + 326 + idx, err := w.r.Storer.Index() 327 + if err != nil { 328 + return plumbing.ZeroHash, err 329 + } 330 + 331 + var h plumbing.Hash 332 + var added bool 333 + 334 + fi, err := w.Filesystem.Lstat(path) 335 + if err != nil || !fi.IsDir() { 336 + added, h, err = w.doAddFile(idx, s, path, ignorePattern) 337 + } else { 338 + added, err = w.doAddDirectory(idx, s, path, ignorePattern) 339 + } 340 + 341 + if err != nil { 342 + return h, err 343 + } 344 + 345 + if !added { 346 + return h, nil 347 + } 348 + 349 + return h, w.r.Storer.SetIndex(idx) 350 + } 351 + 331 352 // AddGlob adds all paths, matching pattern, to the index. If pattern matches a 332 353 // directory path, all directory contents are added to the index recursively. No 333 354 // error is returned if all matching paths are already staged in index. ··· 360 381 361 382 var added bool 362 383 if fi.IsDir() { 363 - added, err = w.doAddDirectory(idx, s, file) 384 + added, err = w.doAddDirectory(idx, s, file, make([]gitignore.Pattern, 0)) 364 385 } else { 365 - added, _, err = w.doAddFile(idx, s, file) 386 + added, _, err = w.doAddFile(idx, s, file, make([]gitignore.Pattern, 0)) 366 387 } 367 388 368 389 if err != nil { ··· 383 404 384 405 // doAddFile create a new blob from path and update the index, added is true if 385 406 // the file added is different from the index. 386 - func (w *Worktree) doAddFile(idx *index.Index, s Status, path string) (added bool, h plumbing.Hash, err error) { 407 + func (w *Worktree) doAddFile(idx *index.Index, s Status, path string, ignorePattern []gitignore.Pattern) (added bool, h plumbing.Hash, err error) { 387 408 if s.File(path).Worktree == Unmodified { 388 409 return false, h, nil 410 + } 411 + if len(ignorePattern) > 0 { 412 + m := gitignore.NewMatcher(ignorePattern) 413 + matchPath := strings.Split(path, string(os.PathSeparator)) 414 + if m.Match(matchPath, true) { 415 + // ignore 416 + return false, h, nil 417 + } 389 418 } 390 419 391 420 h, err = w.copyFileToStorage(path)
+46
worktree_test.go
··· 1370 1370 c.Assert(h.IsZero(), Equals, true) 1371 1371 } 1372 1372 1373 + func (s *WorktreeSuite) TestAddAll(c *C) { 1374 + fs := memfs.New() 1375 + w := &Worktree{ 1376 + r: s.Repository, 1377 + Filesystem: fs, 1378 + } 1379 + 1380 + err := w.Checkout(&CheckoutOptions{Force: true}) 1381 + c.Assert(err, IsNil) 1382 + 1383 + idx, err := w.r.Storer.Index() 1384 + c.Assert(err, IsNil) 1385 + c.Assert(idx.Entries, HasLen, 9) 1386 + 1387 + err = util.WriteFile(w.Filesystem, "file1", []byte("file1"), 0644) 1388 + c.Assert(err, IsNil) 1389 + 1390 + err = util.WriteFile(w.Filesystem, "file2", []byte("file2"), 0644) 1391 + c.Assert(err, IsNil) 1392 + 1393 + err = util.WriteFile(w.Filesystem, "file3", []byte("ignore me"), 0644) 1394 + c.Assert(err, IsNil) 1395 + 1396 + w.Excludes = make([]gitignore.Pattern, 0) 1397 + w.Excludes = append(w.Excludes, gitignore.ParsePattern("file3", nil)) 1398 + 1399 + _, err = w.AddWithOptions(&AddOptions{All: true}) 1400 + c.Assert(err, IsNil) 1401 + 1402 + idx, err = w.r.Storer.Index() 1403 + c.Assert(err, IsNil) 1404 + c.Assert(idx.Entries, HasLen, 11) 1405 + 1406 + status, err := w.Status() 1407 + c.Assert(err, IsNil) 1408 + c.Assert(status, HasLen, 2) 1409 + 1410 + file1 := status.File("file1") 1411 + c.Assert(file1.Staging, Equals, Added) 1412 + file2 := status.File("file2") 1413 + c.Assert(file2.Staging, Equals, Added) 1414 + file3 := status.File("file3") 1415 + c.Assert(file3.Staging, Equals, Untracked) 1416 + c.Assert(file3.Worktree, Equals, Untracked) 1417 + } 1418 + 1373 1419 func (s *WorktreeSuite) TestAddGlob(c *C) { 1374 1420 fs := memfs.New() 1375 1421 w := &Worktree{