mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
Bump go-git
This commit is contained in:
399
vendor/github.com/jesseduffield/go-git/v5/worktree.go
generated
vendored
399
vendor/github.com/jesseduffield/go-git/v5/worktree.go
generated
vendored
@ -5,12 +5,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
stdioutil "io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/util"
|
||||
"github.com/jesseduffield/go-git/v5/config"
|
||||
"github.com/jesseduffield/go-git/v5/plumbing"
|
||||
"github.com/jesseduffield/go-git/v5/plumbing/filemode"
|
||||
@ -20,17 +21,16 @@ import (
|
||||
"github.com/jesseduffield/go-git/v5/plumbing/storer"
|
||||
"github.com/jesseduffield/go-git/v5/utils/ioutil"
|
||||
"github.com/jesseduffield/go-git/v5/utils/merkletrie"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/util"
|
||||
"github.com/jesseduffield/go-git/v5/utils/sync"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrWorktreeNotClean = errors.New("worktree is not clean")
|
||||
ErrSubmoduleNotFound = errors.New("submodule not found")
|
||||
ErrUnstagedChanges = errors.New("worktree contains unstaged changes")
|
||||
ErrGitModulesSymlink = errors.New(gitmodulesFile + " is a symlink")
|
||||
ErrNonFastForwardUpdate = errors.New("non-fast-forward update")
|
||||
ErrWorktreeNotClean = errors.New("worktree is not clean")
|
||||
ErrSubmoduleNotFound = errors.New("submodule not found")
|
||||
ErrUnstagedChanges = errors.New("worktree contains unstaged changes")
|
||||
ErrGitModulesSymlink = errors.New(gitmodulesFile + " is a symlink")
|
||||
ErrNonFastForwardUpdate = errors.New("non-fast-forward update")
|
||||
ErrRestoreWorktreeOnlyNotSupported = errors.New("worktree only is not supported")
|
||||
)
|
||||
|
||||
// Worktree represents a git worktree.
|
||||
@ -59,7 +59,7 @@ func (w *Worktree) Pull(o *PullOptions) error {
|
||||
// Pull only supports merges where the can be resolved as a fast-forward.
|
||||
//
|
||||
// The provided Context must be non-nil. If the context expires before the
|
||||
// operation is complete, an error is returned. The context only affects to the
|
||||
// operation is complete, an error is returned. The context only affects the
|
||||
// transport operations.
|
||||
func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
|
||||
if err := o.Validate(); err != nil {
|
||||
@ -72,11 +72,15 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
|
||||
}
|
||||
|
||||
fetchHead, err := remote.fetch(ctx, &FetchOptions{
|
||||
RemoteName: o.RemoteName,
|
||||
Depth: o.Depth,
|
||||
Auth: o.Auth,
|
||||
Progress: o.Progress,
|
||||
Force: o.Force,
|
||||
RemoteName: o.RemoteName,
|
||||
RemoteURL: o.RemoteURL,
|
||||
Depth: o.Depth,
|
||||
Auth: o.Auth,
|
||||
Progress: o.Progress,
|
||||
Force: o.Force,
|
||||
InsecureSkipTLS: o.InsecureSkipTLS,
|
||||
CABundle: o.CABundle,
|
||||
ProxyOptions: o.ProxyOptions,
|
||||
})
|
||||
|
||||
updated := true
|
||||
@ -93,7 +97,15 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
|
||||
|
||||
head, err := w.r.Head()
|
||||
if err == nil {
|
||||
headAheadOfRef, err := isFastForward(w.r.Storer, ref.Hash(), head.Hash())
|
||||
// if we don't have a shallows list, just ignore it
|
||||
shallowList, _ := w.r.Storer.Shallow()
|
||||
|
||||
var earliestShallow *plumbing.Hash
|
||||
if len(shallowList) > 0 {
|
||||
earliestShallow = &shallowList[0]
|
||||
}
|
||||
|
||||
headAheadOfRef, err := isFastForward(w.r.Storer, ref.Hash(), head.Hash(), earliestShallow)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -102,7 +114,7 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
|
||||
return NoErrAlreadyUpToDate
|
||||
}
|
||||
|
||||
ff, err := isFastForward(w.r.Storer, head.Hash(), ref.Hash())
|
||||
ff, err := isFastForward(w.r.Storer, head.Hash(), ref.Hash(), earliestShallow)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -128,7 +140,7 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
|
||||
}
|
||||
|
||||
if o.RecurseSubmodules != NoRecurseSubmodules {
|
||||
return w.updateSubmodules(&SubmoduleUpdateOptions{
|
||||
return w.updateSubmodules(ctx, &SubmoduleUpdateOptions{
|
||||
RecurseSubmodules: o.RecurseSubmodules,
|
||||
Auth: o.Auth,
|
||||
})
|
||||
@ -137,13 +149,13 @@ func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Worktree) updateSubmodules(o *SubmoduleUpdateOptions) error {
|
||||
func (w *Worktree) updateSubmodules(ctx context.Context, o *SubmoduleUpdateOptions) error {
|
||||
s, err := w.Submodules()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.Init = true
|
||||
return s.Update(o)
|
||||
return s.UpdateContext(ctx, o)
|
||||
}
|
||||
|
||||
// Checkout switch branches or restore working tree files.
|
||||
@ -180,9 +192,18 @@ func (w *Worktree) Checkout(opts *CheckoutOptions) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(opts.SparseCheckoutDirectories) > 0 {
|
||||
return w.ResetSparsely(ro, opts.SparseCheckoutDirectories)
|
||||
}
|
||||
|
||||
return w.Reset(ro)
|
||||
}
|
||||
|
||||
func (w *Worktree) createBranch(opts *CheckoutOptions) error {
|
||||
if err := opts.Branch.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := w.r.Storer.Reference(opts.Branch)
|
||||
if err == nil {
|
||||
return fmt.Errorf("a branch named %q already exists", opts.Branch)
|
||||
@ -207,20 +228,17 @@ func (w *Worktree) createBranch(opts *CheckoutOptions) error {
|
||||
}
|
||||
|
||||
func (w *Worktree) getCommitFromCheckoutOptions(opts *CheckoutOptions) (plumbing.Hash, error) {
|
||||
if !opts.Hash.IsZero() {
|
||||
return opts.Hash, nil
|
||||
hash := opts.Hash
|
||||
if hash.IsZero() {
|
||||
b, err := w.r.Reference(opts.Branch, true)
|
||||
if err != nil {
|
||||
return plumbing.ZeroHash, err
|
||||
}
|
||||
|
||||
hash = b.Hash()
|
||||
}
|
||||
|
||||
b, err := w.r.Reference(opts.Branch, true)
|
||||
if err != nil {
|
||||
return plumbing.ZeroHash, err
|
||||
}
|
||||
|
||||
if !b.Name().IsTag() {
|
||||
return b.Hash(), nil
|
||||
}
|
||||
|
||||
o, err := w.r.Object(plumbing.AnyObject, b.Hash())
|
||||
o, err := w.r.Object(plumbing.AnyObject, hash)
|
||||
if err != nil {
|
||||
return plumbing.ZeroHash, err
|
||||
}
|
||||
@ -228,7 +246,7 @@ func (w *Worktree) getCommitFromCheckoutOptions(opts *CheckoutOptions) (plumbing
|
||||
switch o := o.(type) {
|
||||
case *object.Tag:
|
||||
if o.TargetType != plumbing.CommitObject {
|
||||
return plumbing.ZeroHash, fmt.Errorf("unsupported tag object target %q", o.TargetType)
|
||||
return plumbing.ZeroHash, fmt.Errorf("%w: tag target %q", object.ErrUnsupportedObject, o.TargetType)
|
||||
}
|
||||
|
||||
return o.Target, nil
|
||||
@ -236,7 +254,7 @@ func (w *Worktree) getCommitFromCheckoutOptions(opts *CheckoutOptions) (plumbing
|
||||
return o.Hash, nil
|
||||
}
|
||||
|
||||
return plumbing.ZeroHash, fmt.Errorf("unsupported tag target %q", o.Type())
|
||||
return plumbing.ZeroHash, fmt.Errorf("%w: %q", object.ErrUnsupportedObject, o.Type())
|
||||
}
|
||||
|
||||
func (w *Worktree) setHEADToCommit(commit plumbing.Hash) error {
|
||||
@ -260,8 +278,7 @@ func (w *Worktree) setHEADToBranch(branch plumbing.ReferenceName, commit plumbin
|
||||
return w.r.Storer.SetReference(head)
|
||||
}
|
||||
|
||||
// Reset the worktree to a specified state.
|
||||
func (w *Worktree) Reset(opts *ResetOptions) error {
|
||||
func (w *Worktree) ResetSparsely(opts *ResetOptions, dirs []string) error {
|
||||
if err := opts.Validate(w.r); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -285,19 +302,19 @@ func (w *Worktree) Reset(opts *ResetOptions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
t, err := w.getTreeFromCommitHash(opts.Commit)
|
||||
t, err := w.r.getTreeFromCommitHash(opts.Commit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.Mode == MixedReset || opts.Mode == MergeReset || opts.Mode == HardReset {
|
||||
if err := w.resetIndex(t); err != nil {
|
||||
if err := w.resetIndex(t, dirs, opts.Files); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if opts.Mode == MergeReset || opts.Mode == HardReset {
|
||||
if err := w.resetWorktree(t); err != nil {
|
||||
if err := w.resetWorktree(t, opts.Files); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -305,11 +322,52 @@ func (w *Worktree) Reset(opts *ResetOptions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Worktree) resetIndex(t *object.Tree) error {
|
||||
// Restore restores specified files in the working tree or stage with contents from
|
||||
// a restore source. If a path is tracked but does not exist in the restore,
|
||||
// source, it will be removed to match the source.
|
||||
//
|
||||
// If Staged and Worktree are true, then the restore source will be the index.
|
||||
// If only Staged is true, then the restore source will be HEAD.
|
||||
// If only Worktree is true or neither Staged nor Worktree are true, will
|
||||
// result in ErrRestoreWorktreeOnlyNotSupported because restoring the working
|
||||
// tree while leaving the stage untouched is not currently supported.
|
||||
//
|
||||
// Restore with no files specified will return ErrNoRestorePaths.
|
||||
func (w *Worktree) Restore(o *RestoreOptions) error {
|
||||
if err := o.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if o.Staged {
|
||||
opts := &ResetOptions{
|
||||
Files: o.Files,
|
||||
}
|
||||
|
||||
if o.Worktree {
|
||||
// If we are doing both Worktree and Staging then it is a hard reset
|
||||
opts.Mode = HardReset
|
||||
} else {
|
||||
// If we are doing just staging then it is a mixed reset
|
||||
opts.Mode = MixedReset
|
||||
}
|
||||
|
||||
return w.Reset(opts)
|
||||
}
|
||||
|
||||
return ErrRestoreWorktreeOnlyNotSupported
|
||||
}
|
||||
|
||||
// Reset the worktree to a specified state.
|
||||
func (w *Worktree) Reset(opts *ResetOptions) error {
|
||||
return w.ResetSparsely(opts, nil)
|
||||
}
|
||||
|
||||
func (w *Worktree) resetIndex(t *object.Tree, dirs []string, files []string) error {
|
||||
idx, err := w.r.Storer.Index()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b := newIndexBuilder(idx)
|
||||
|
||||
changes, err := w.diffTreeWithStaging(t, true)
|
||||
@ -337,6 +395,13 @@ func (w *Worktree) resetIndex(t *object.Tree) error {
|
||||
name = ch.From.String()
|
||||
}
|
||||
|
||||
if len(files) > 0 {
|
||||
contains := inFiles(files, name)
|
||||
if !contains {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
b.Remove(name)
|
||||
if e == nil {
|
||||
continue
|
||||
@ -351,11 +416,27 @@ func (w *Worktree) resetIndex(t *object.Tree) error {
|
||||
}
|
||||
|
||||
b.Write(idx)
|
||||
|
||||
if len(dirs) > 0 {
|
||||
idx.SkipUnless(dirs)
|
||||
}
|
||||
|
||||
return w.r.Storer.SetIndex(idx)
|
||||
}
|
||||
|
||||
func (w *Worktree) resetWorktree(t *object.Tree) error {
|
||||
changes, err := w.diffStagingWithWorktree(true)
|
||||
func inFiles(files []string, v string) bool {
|
||||
v = filepath.Clean(v)
|
||||
for _, s := range files {
|
||||
if filepath.Clean(s) == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (w *Worktree) resetWorktree(t *object.Tree, files []string) error {
|
||||
changes, err := w.diffStagingWithWorktree(true, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -367,6 +448,28 @@ func (w *Worktree) resetWorktree(t *object.Tree) error {
|
||||
b := newIndexBuilder(idx)
|
||||
|
||||
for _, ch := range changes {
|
||||
if err := w.validChange(ch); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(files) > 0 {
|
||||
file := ""
|
||||
if ch.From != nil {
|
||||
file = ch.From.String()
|
||||
} else if ch.To != nil {
|
||||
file = ch.To.String()
|
||||
}
|
||||
|
||||
if file == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
contains := inFiles(files, file)
|
||||
if !contains {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if err := w.checkoutChange(ch, t, b); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -376,6 +479,108 @@ func (w *Worktree) resetWorktree(t *object.Tree) error {
|
||||
return w.r.Storer.SetIndex(idx)
|
||||
}
|
||||
|
||||
// worktreeDeny is a list of paths that are not allowed
|
||||
// to be used when resetting the worktree.
|
||||
var worktreeDeny = map[string]struct{}{
|
||||
// .git
|
||||
GitDirName: {},
|
||||
|
||||
// For other historical reasons, file names that do not conform to the 8.3
|
||||
// format (up to eight characters for the basename, three for the file
|
||||
// extension, certain characters not allowed such as `+`, etc) are associated
|
||||
// with a so-called "short name", at least on the `C:` drive by default.
|
||||
// Which means that `git~1/` is a valid way to refer to `.git/`.
|
||||
"git~1": {},
|
||||
}
|
||||
|
||||
// validPath checks whether paths are valid.
|
||||
// The rules around invalid paths could differ from upstream based on how
|
||||
// filesystems are managed within go-git, but they are largely the same.
|
||||
//
|
||||
// For upstream rules:
|
||||
// https://github.com/git/git/blob/564d0252ca632e0264ed670534a51d18a689ef5d/read-cache.c#L946
|
||||
// https://github.com/git/git/blob/564d0252ca632e0264ed670534a51d18a689ef5d/path.c#L1383
|
||||
func validPath(paths ...string) error {
|
||||
for _, p := range paths {
|
||||
parts := strings.FieldsFunc(p, func(r rune) bool { return (r == '\\' || r == '/') })
|
||||
if len(parts) == 0 {
|
||||
return fmt.Errorf("invalid path: %q", p)
|
||||
}
|
||||
|
||||
if _, denied := worktreeDeny[strings.ToLower(parts[0])]; denied {
|
||||
return fmt.Errorf("invalid path prefix: %q", p)
|
||||
}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
// Volume names are not supported, in both formats: \\ and <DRIVE_LETTER>:.
|
||||
if vol := filepath.VolumeName(p); vol != "" {
|
||||
return fmt.Errorf("invalid path: %q", p)
|
||||
}
|
||||
|
||||
if !windowsValidPath(parts[0]) {
|
||||
return fmt.Errorf("invalid path: %q", p)
|
||||
}
|
||||
}
|
||||
|
||||
for _, part := range parts {
|
||||
if part == ".." {
|
||||
return fmt.Errorf("invalid path %q: cannot use '..'", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// windowsPathReplacer defines the chars that need to be replaced
|
||||
// as part of windowsValidPath.
|
||||
var windowsPathReplacer *strings.Replacer
|
||||
|
||||
func init() {
|
||||
windowsPathReplacer = strings.NewReplacer(" ", "", ".", "")
|
||||
}
|
||||
|
||||
func windowsValidPath(part string) bool {
|
||||
if len(part) > 3 && strings.EqualFold(part[:4], GitDirName) {
|
||||
// For historical reasons, file names that end in spaces or periods are
|
||||
// automatically trimmed. Therefore, `.git . . ./` is a valid way to refer
|
||||
// to `.git/`.
|
||||
if windowsPathReplacer.Replace(part[4:]) == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// For yet other historical reasons, NTFS supports so-called "Alternate Data
|
||||
// Streams", i.e. metadata associated with a given file, referred to via
|
||||
// `<filename>:<stream-name>:<stream-type>`. There exists a default stream
|
||||
// type for directories, allowing `.git/` to be accessed via
|
||||
// `.git::$INDEX_ALLOCATION/`.
|
||||
//
|
||||
// For performance reasons, _all_ Alternate Data Streams of `.git/` are
|
||||
// forbidden, not just `::$INDEX_ALLOCATION`.
|
||||
if len(part) > 4 && part[4:5] == ":" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *Worktree) validChange(ch merkletrie.Change) error {
|
||||
action, err := ch.Action()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch action {
|
||||
case merkletrie.Delete:
|
||||
return validPath(ch.From.String())
|
||||
case merkletrie.Insert:
|
||||
return validPath(ch.To.String())
|
||||
case merkletrie.Modify:
|
||||
return validPath(ch.From.String(), ch.To.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Worktree) checkoutChange(ch merkletrie.Change, t *object.Tree, idx *indexBuilder) error {
|
||||
a, err := ch.Action()
|
||||
if err != nil {
|
||||
@ -396,7 +601,7 @@ func (w *Worktree) checkoutChange(ch merkletrie.Change, t *object.Tree, idx *ind
|
||||
|
||||
isSubmodule = e.Mode == filemode.Submodule
|
||||
case merkletrie.Delete:
|
||||
return rmFileAndDirIfEmpty(w.Filesystem, ch.From.String())
|
||||
return rmFileAndDirsIfEmpty(w.Filesystem, ch.From.String())
|
||||
}
|
||||
|
||||
if isSubmodule {
|
||||
@ -407,7 +612,7 @@ func (w *Worktree) checkoutChange(ch merkletrie.Change, t *object.Tree, idx *ind
|
||||
}
|
||||
|
||||
func (w *Worktree) containsUnstagedChanges() (bool, error) {
|
||||
ch, err := w.diffStagingWithWorktree(false)
|
||||
ch, err := w.diffStagingWithWorktree(false, true)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@ -512,18 +717,12 @@ func (w *Worktree) checkoutChangeRegularFile(name string,
|
||||
return err
|
||||
}
|
||||
|
||||
return w.addIndexFromFile(name, e.Hash, idx)
|
||||
return w.addIndexFromFile(name, e.Hash, f.Mode, idx)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var copyBufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, 32*1024)
|
||||
},
|
||||
}
|
||||
|
||||
func (w *Worktree) checkoutFile(f *object.File) (err error) {
|
||||
mode, err := f.Mode.ToOSFileMode()
|
||||
if err != nil {
|
||||
@ -547,13 +746,18 @@ func (w *Worktree) checkoutFile(f *object.File) (err error) {
|
||||
}
|
||||
|
||||
defer ioutil.CheckClose(to, &err)
|
||||
buf := copyBufferPool.Get().([]byte)
|
||||
_, err = io.CopyBuffer(to, from, buf)
|
||||
copyBufferPool.Put(buf)
|
||||
buf := sync.GetByteSlice()
|
||||
_, err = io.CopyBuffer(to, from, *buf)
|
||||
sync.PutByteSlice(buf)
|
||||
return
|
||||
}
|
||||
|
||||
func (w *Worktree) checkoutFileSymlink(f *object.File) (err error) {
|
||||
// https://github.com/git/git/commit/10ecfa76491e4923988337b2e2243b05376b40de
|
||||
if strings.EqualFold(f.Name, gitmodulesFile) {
|
||||
return ErrGitModulesSymlink
|
||||
}
|
||||
|
||||
from, err := f.Reader()
|
||||
if err != nil {
|
||||
return
|
||||
@ -561,7 +765,7 @@ func (w *Worktree) checkoutFileSymlink(f *object.File) (err error) {
|
||||
|
||||
defer ioutil.CheckClose(from, &err)
|
||||
|
||||
bytes, err := stdioutil.ReadAll(from)
|
||||
bytes, err := io.ReadAll(from)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -596,18 +800,13 @@ func (w *Worktree) addIndexFromTreeEntry(name string, f *object.TreeEntry, idx *
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *indexBuilder) error {
|
||||
func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, mode filemode.FileMode, idx *indexBuilder) error {
|
||||
idx.Remove(name)
|
||||
fi, err := w.Filesystem.Lstat(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode, err := filemode.NewFromOSFileMode(fi.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e := &index.Entry{
|
||||
Hash: h,
|
||||
Name: name,
|
||||
@ -625,8 +824,8 @@ func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *indexBuil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Worktree) getTreeFromCommitHash(commit plumbing.Hash) (*object.Tree, error) {
|
||||
c, err := w.r.CommitObject(commit)
|
||||
func (r *Repository) getTreeFromCommitHash(commit plumbing.Hash) (*object.Tree, error) {
|
||||
c, err := r.CommitObject(commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -710,13 +909,17 @@ func (w *Worktree) readGitmodulesFile() (*config.Modules, error) {
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
input, err := stdioutil.ReadAll(f)
|
||||
input, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := config.NewModules()
|
||||
return m, m.Unmarshal(input)
|
||||
if err := m.Unmarshal(input); err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Clean the worktree by removing untracked files.
|
||||
@ -765,9 +968,11 @@ func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files
|
||||
}
|
||||
}
|
||||
|
||||
if opts.Dir {
|
||||
return doCleanDirectories(w.Filesystem, dir)
|
||||
if opts.Dir && dir != "" {
|
||||
_, err := removeDirIfEmpty(w.Filesystem, dir)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -788,9 +993,9 @@ func (gr GrepResult) String() string {
|
||||
return fmt.Sprintf("%s:%s:%d:%s", gr.TreeName, gr.FileName, gr.LineNumber, gr.Content)
|
||||
}
|
||||
|
||||
// Grep performs grep on a worktree.
|
||||
func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
|
||||
if err := opts.Validate(w); err != nil {
|
||||
// Grep performs grep on a repository.
|
||||
func (r *Repository) Grep(opts *GrepOptions) ([]GrepResult, error) {
|
||||
if err := opts.validate(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -800,7 +1005,7 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
|
||||
var treeName string
|
||||
|
||||
if opts.ReferenceName != "" {
|
||||
ref, err := w.r.Reference(opts.ReferenceName, true)
|
||||
ref, err := r.Reference(opts.ReferenceName, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -813,7 +1018,7 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
|
||||
|
||||
// Obtain a tree from the commit hash and get a tracked files iterator from
|
||||
// the tree.
|
||||
tree, err := w.getTreeFromCommitHash(commitHash)
|
||||
tree, err := r.getTreeFromCommitHash(commitHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -822,6 +1027,11 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
|
||||
return findMatchInFiles(fileiter, treeName, opts)
|
||||
}
|
||||
|
||||
// Grep performs grep on a worktree.
|
||||
func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
|
||||
return w.r.Grep(opts)
|
||||
}
|
||||
|
||||
// findMatchInFiles takes a FileIter, worktree name and GrepOptions, and
|
||||
// returns a slice of GrepResult containing the result of regex pattern matching
|
||||
// in content of all the files.
|
||||
@ -908,25 +1118,52 @@ func findMatchInFile(file *object.File, treeName string, opts *GrepOptions) ([]G
|
||||
return grepResults, nil
|
||||
}
|
||||
|
||||
func rmFileAndDirIfEmpty(fs billy.Filesystem, name string) error {
|
||||
// will walk up the directory tree removing all encountered empty
|
||||
// directories, not just the one containing this file
|
||||
func rmFileAndDirsIfEmpty(fs billy.Filesystem, name string) error {
|
||||
if err := util.RemoveAll(fs, name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dir := filepath.Dir(name)
|
||||
return doCleanDirectories(fs, dir)
|
||||
for {
|
||||
removed, err := removeDirIfEmpty(fs, dir)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if !removed {
|
||||
// directory was not empty and not removed,
|
||||
// stop checking parents
|
||||
break
|
||||
}
|
||||
|
||||
// move to parent directory
|
||||
dir = filepath.Dir(dir)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doCleanDirectories removes empty subdirs (without files)
|
||||
func doCleanDirectories(fs billy.Filesystem, dir string) error {
|
||||
// removeDirIfEmpty will remove the supplied directory `dir` if
|
||||
// `dir` is empty
|
||||
// returns true if the directory was removed
|
||||
func removeDirIfEmpty(fs billy.Filesystem, dir string) (bool, error) {
|
||||
files, err := fs.ReadDir(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
if len(files) == 0 {
|
||||
return fs.Remove(dir)
|
||||
|
||||
if len(files) > 0 {
|
||||
return false, nil
|
||||
}
|
||||
return nil
|
||||
|
||||
err = fs.Remove(dir)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
type indexBuilder struct {
|
||||
|
Reference in New Issue
Block a user