package git

import (
	"context"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"net/url"
	"os"
	"path"
	"path/filepath"
	"strings"
	"time"

	"github.com/go-git/go-billy/v6"
	"github.com/go-git/go-billy/v6/osfs"
	"github.com/go-git/go-billy/v6/util"

	"github.com/go-git/go-git/v6/config"
	giturl "github.com/go-git/go-git/v6/internal/url"
	"github.com/go-git/go-git/v6/plumbing"
	"github.com/go-git/go-git/v6/plumbing/filemode"
	"github.com/go-git/go-git/v6/plumbing/format/gitignore"
	"github.com/go-git/go-git/v6/plumbing/format/index"
	"github.com/go-git/go-git/v6/plumbing/object"
	"github.com/go-git/go-git/v6/plumbing/storer"
	"github.com/go-git/go-git/v6/utils/convert"
	"github.com/go-git/go-git/v6/utils/ioutil"
	"github.com/go-git/go-git/v6/utils/merkletrie"
	"github.com/go-git/go-git/v6/utils/sync"
	"github.com/go-git/go-git/v6/utils/trace"
)

// Worktree errors.
var (
	// ErrWorktreeNotClean is returned when the worktree is not clean.
	ErrWorktreeNotClean = errors.New("worktree is not clean")
	// ErrSubmoduleNotFound is returned when the submodule is not found.
	ErrSubmoduleNotFound = errors.New("submodule not found")
	// ErrUnstagedChanges is returned when the worktree has unstaged changes.
	ErrUnstagedChanges = errors.New("worktree contains unstaged changes")
	// ErrLocalChanges is returned when a KeepReset is attempted but a file
	// that would be changed by the reset has local modifications.
	ErrLocalChanges = errors.New("worktree contains local changes that would be overwritten by reset")
	// ErrGitModulesSymlink is returned when .gitmodules is a symlink.
	ErrGitModulesSymlink = errors.New(gitmodulesFile + " is a symlink")
	// ErrNonFastForwardUpdate is returned when a non-fast-forward update is attempted.
	ErrNonFastForwardUpdate = errors.New("non-fast-forward update")
	// ErrRestoreWorktreeOnlyNotSupported is returned when worktree only restore is not supported.
	ErrRestoreWorktreeOnlyNotSupported = errors.New("worktree only is not supported")
	// ErrSparseResetDirectoryNotFound is returned when a sparse-reset directory is not found.
	ErrSparseResetDirectoryNotFound = errors.New("sparse-reset directory not found on commit")
)

// Worktree represents a git worktree.
type Worktree struct {
	// External excludes not found in the repository .gitignore
	Excludes []gitignore.Pattern

	r          *Repository
	filesystem *worktreeFilesystem
}

// Filesystem returns the underlying filesystem for the worktree.
func (w *Worktree) Filesystem() billy.Filesystem {
	return w.filesystem.Filesystem
}

// reusableRootFS returns a worktree filesystem to use for a single bulk
// checkout/reset. When the worktree lives on an OS-backed billy filesystem,
// it opens one *os.Root for the whole operation and wraps it in the same
// validating worktreeFilesystem, so each file is opened relative to a
// reused directory instead of opening and closing a fresh root per call.
// It falls back to the default filesystem when that is not possible.
func (w *Worktree) reusableRootFS() (*worktreeFilesystem, func()) {
	bos, ok := w.filesystem.Filesystem.(*osfs.BoundOS)
	if !ok {
		return w.filesystem, func() {}
	}
	root, err := os.OpenRoot(bos.Root())
	if err != nil {
		return w.filesystem, func() {}
	}
	rfs, err := osfs.FromRoot(root)
	if err != nil {
		_ = root.Close()
		return w.filesystem, func() {}
	}
	return newWorktreeFilesystem(rfs, w.filesystem.protectNTFS, w.filesystem.protectHFS),
		func() { _ = root.Close() }
}

// Pull incorporates changes from a remote repository into the current branch.
// Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are
// no changes to be fetched, or an error.
//
// Pull only supports merges where the can be resolved as a fast-forward.
func (w *Worktree) Pull(o *PullOptions) error {
	return w.PullContext(context.Background(), o)
}

// PullContext incorporates changes from a remote repository into the current
// branch. Returns nil if the operation is successful, NoErrAlreadyUpToDate if
// there are no changes to be fetched, or an 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 the
// transport operations.
func (w *Worktree) PullContext(ctx context.Context, o *PullOptions) error {
	if trace.Performance.Enabled() {
		start := time.Now()
		defer func() {
			trace.Performance.Printf("performance: %.9f s: git command: git pull", time.Since(start).Seconds())
		}()
	}

	if err := o.Validate(); err != nil {
		return err
	}

	remote, err := w.r.Remote(o.RemoteName)
	if err != nil {
		return err
	}

	fetchHead, err := remote.fetch(ctx, &FetchOptions{
		RemoteName:    o.RemoteName,
		RemoteURL:     o.RemoteURL,
		Depth:         o.Depth,
		ClientOptions: o.ClientOptions,
		Progress:      o.Progress,
		Force:         o.Force,
	})

	updated := true
	if errors.Is(err, NoErrAlreadyUpToDate) {
		updated = false
	} else if err != nil {
		return err
	}

	ref, err := storer.ResolveReference(fetchHead, o.ReferenceName)
	if err != nil {
		return err
	}

	head, err := w.r.Head()
	if err == nil {
		// if we don't have a shallows list, just ignore it
		shallowList, _ := w.r.Storer.Shallow()

		headAheadOfRef, err := isFastForward(w.r.Storer, ref.Hash(), head.Hash(), shallowList)
		if err != nil {
			return err
		}

		if !updated && headAheadOfRef {
			return NoErrAlreadyUpToDate
		}

		ff, err := isFastForward(w.r.Storer, head.Hash(), ref.Hash(), shallowList)
		if err != nil {
			return err
		}

		if !ff {
			return ErrNonFastForwardUpdate
		}
	}

	if err != nil && !errors.Is(err, plumbing.ErrReferenceNotFound) {
		return err
	}

	if err := w.updateHEAD(ref.Hash()); err != nil {
		return err
	}

	if err := w.Reset(&ResetOptions{
		Mode:   MergeReset,
		Commit: ref.Hash(),
	}); err != nil {
		return err
	}

	if o.RecurseSubmodules != NoRecurseSubmodules {
		return w.updateSubmodules(ctx, &SubmoduleUpdateOptions{
			RecurseSubmodules: o.RecurseSubmodules,
			ClientOptions:     o.ClientOptions,
		})
	}

	return nil
}

func (w *Worktree) updateSubmodules(ctx context.Context, o *SubmoduleUpdateOptions) error {
	s, err := w.Submodules()
	if err != nil {
		return err
	}
	o.Init = true
	return s.UpdateContext(ctx, o)
}

// Checkout switch branches or restore working tree files.
func (w *Worktree) Checkout(opts *CheckoutOptions) error {
	if trace.Performance.Enabled() {
		start := time.Now()
		defer func() {
			trace.Performance.Printf("performance: %.9f s: git command: git checkout", time.Since(start).Seconds())
		}()
	}

	if err := opts.Validate(); err != nil {
		return err
	}

	if opts.Create {
		if err := w.createBranch(opts); err != nil {
			return err
		}
	}

	c, err := w.getCommitFromCheckoutOptions(opts)
	if err != nil {
		return err
	}

	ro := &ResetOptions{
		Commit:     c,
		Mode:       MergeReset,
		SparseDirs: opts.SparseCheckoutDirectories,
	}
	if opts.Force {
		ro.Mode = HardReset
	} else if opts.Keep {
		ro.Mode = SoftReset
	}

	if !opts.Hash.IsZero() && !opts.Create {
		err = w.setHEADToCommit(opts.Hash)
	} else {
		err = w.setHEADToBranch(opts.Branch, c)
	}

	if err != nil {
		return err
	}

	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)
	}

	if !errors.Is(err, plumbing.ErrReferenceNotFound) {
		return err
	}

	if opts.Hash.IsZero() {
		ref, err := w.r.Head()
		if err != nil {
			return err
		}

		opts.Hash = ref.Hash()
	}

	return w.r.Storer.SetReference(
		plumbing.NewHashReference(opts.Branch, opts.Hash),
	)
}

func (w *Worktree) getCommitFromCheckoutOptions(opts *CheckoutOptions) (plumbing.Hash, error) {
	hash := opts.Hash
	if hash.IsZero() {
		b, err := w.r.Reference(opts.Branch, true)
		if err != nil {
			return plumbing.ZeroHash, err
		}

		hash = b.Hash()
	}

	o, err := w.r.Object(plumbing.AnyObject, hash)
	if err != nil {
		return plumbing.ZeroHash, err
	}

	switch o := o.(type) {
	case *object.Tag:
		if o.TargetType != plumbing.CommitObject {
			return plumbing.ZeroHash, fmt.Errorf("%w: tag target %q", object.ErrUnsupportedObject, o.TargetType)
		}

		return o.Target, nil
	case *object.Commit:
		return o.Hash, nil
	}

	return plumbing.ZeroHash, fmt.Errorf("%w: %q", object.ErrUnsupportedObject, o.Type())
}

func (w *Worktree) setHEADToCommit(commit plumbing.Hash) error {
	head := plumbing.NewHashReference(plumbing.HEAD, commit)
	return w.r.Storer.SetReference(head)
}

func (w *Worktree) setHEADToBranch(branch plumbing.ReferenceName, commit plumbing.Hash) error {
	target, err := w.r.Storer.Reference(branch)
	if err != nil {
		return err
	}

	var head *plumbing.Reference
	if target.Name().IsBranch() {
		head = plumbing.NewSymbolicReference(plumbing.HEAD, target.Name())
	} else {
		head = plumbing.NewHashReference(plumbing.HEAD, commit)
	}

	return w.r.Storer.SetReference(head)
}

// Reset the worktree to a specified state.
func (w *Worktree) Reset(opts *ResetOptions) error {
	if trace.Performance.Enabled() {
		start := time.Now()
		defer func() {
			trace.Performance.Printf("performance: %.9f s: reset_worktree", time.Since(start).Seconds())
		}()
	}

	if err := opts.Validate(w.r); err != nil {
		return err
	}

	cfg, err := w.r.Config()
	if err != nil {
		return err
	}

	if opts.Mode == MergeReset {
		unstaged, err := w.containsUnstagedChanges(cfg)
		if err != nil {
			return err
		}

		if unstaged {
			return ErrUnstagedChanges
		}
	}

	if opts.Mode == SoftReset {
		return w.setHEADCommit(opts.Commit)
	}

	t, err := w.r.getTreeFromCommitHash(opts.Commit)
	if err != nil {
		return err
	}

	if len(opts.SparseDirs) > 0 && !opts.SkipSparseDirValidation {
		if !treeContainsDirs(t, opts.SparseDirs) {
			return ErrSparseResetDirectoryNotFound
		}
	}

	// For HardReset and KeepReset, capture the current HEAD tree before
	// resetting HEAD. resetWorktreeToTree will diff prevTree→t and apply only
	// those changes to the worktree. Since the diff is tree-to-tree, untracked
	// files are invisible and are never deleted — matching real git reset --hard.
	var prevTree *object.Tree
	if opts.Mode == HardReset || opts.Mode == KeepReset {
		prevTree, err = w.headTree()
		if err != nil {
			return err
		}
	}

	if opts.Mode == KeepReset {
		if err := w.checkKeepResetConflicts(prevTree, t, opts.SparseDirs, opts.Files); err != nil {
			return err
		}
	}

	if err := w.setHEADCommit(opts.Commit); err != nil {
		return err
	}

	var removedFiles []string
	if opts.Mode == MixedReset || opts.Mode == MergeReset || opts.Mode == HardReset || opts.Mode == KeepReset {
		if removedFiles, err = w.resetIndex(t, opts.SparseDirs, opts.Files); err != nil {
			return err
		}
	}

	if opts.Mode == MergeReset && len(removedFiles) > 0 {
		if err := w.resetWorktree(cfg, t, removedFiles); err != nil {
			return err
		}
	}

	if opts.Mode == HardReset || opts.Mode == KeepReset {
		if err := w.resetWorktreeToTree(cfg, prevTree, t, opts.Files); err != nil {
			return err
		}
	}

	return nil
}

// treeContainsDirs checks if the given tree contains all the directories.
// if dirs is empty, it returns false.
func treeContainsDirs(tree *object.Tree, dirs []string) bool {
	if len(dirs) == 0 {
		return false
	}

	for _, dir := range dirs {
		entry, err := tree.FindEntry(dir)
		if err != nil {
			return false
		}
		if entry.Mode != filemode.Dir {
			return false
		}
	}

	return true
}

// 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
}

func (w *Worktree) resetIndex(t *object.Tree, dirs, files []string) ([]string, error) {
	idx, err := w.r.Storer.Index()
	if err != nil {
		return nil, err
	}

	b := newIndexBuilder(idx)

	changes, err := w.diffTreeWithStaging(t, true)
	if err != nil {
		return nil, err
	}

	removedFiles := make([]string, 0, len(changes))
	filesMap := buildFilePathMap(files)
	for _, ch := range changes {
		a, err := ch.Action()
		if err != nil {
			return nil, err
		}

		var name string
		var e *object.TreeEntry

		switch a {
		case merkletrie.Modify, merkletrie.Insert:
			name = ch.To.String()
			e, err = t.FindEntry(name)
			if err != nil {
				return nil, err
			}
		case merkletrie.Delete:
			name = ch.From.String()
		}

		if len(files) > 0 {
			contains := inFiles(filesMap, name)
			if !contains {
				continue
			}
		}

		b.Remove(name)
		removedFiles = append(removedFiles, name)
		if e == nil {
			continue
		}

		b.Add(&index.Entry{
			Name: name,
			Hash: e.Hash,
			Mode: e.Mode,
		})
	}

	b.Write(idx)

	if len(dirs) > 0 {
		idx.SkipUnless(dirs)
	}

	return removedFiles, w.r.Storer.SetIndex(idx)
}

// inFiles checks if the given file is in the list of files. The incoming filepaths in files should be cleaned before calling this function.
func inFiles(files map[string]struct{}, v string) bool {
	v = filepath.Clean(v)
	_, exists := files[v]
	return exists
}

// headTree returns the tree for the current HEAD commit.
// Returns nil, nil if there is no HEAD yet (e.g. an unborn branch).
func (w *Worktree) headTree() (*object.Tree, error) {
	head, err := w.r.Head()
	if err != nil {
		if errors.Is(err, plumbing.ErrReferenceNotFound) {
			return nil, nil
		}
		return nil, err
	}
	c, err := w.r.CommitObject(head.Hash())
	if err != nil {
		return nil, err
	}
	return c.Tree()
}

// checkKeepResetConflicts implements the safety check for KeepReset
// (git reset --keep): it aborts if any file that would be modified by the
// reset has local staged or unstaged modifications in the worktree.
//
// Two categories of "touched" paths are checked:
//
//  1. Files that differ between fromTree and toTree (filtered by files when
//     non-empty). Among these, paths that will be *written* to disk (Insert or
//     Modify) are additionally checked for untracked-overwrite collisions.
//
//  2. When sparseDirs is non-empty: currently-on-disk tracked files that will
//     become SkipWorktree because their path is no longer under any of the new
//     sparse directories. Step 3 of resetWorktreeToTree removes them from disk,
//     so KeepReset must refuse if they carry local modifications.
func (w *Worktree) checkKeepResetConflicts(fromTree, toTree *object.Tree, sparseDirs, files []string) error {
	changes, err := diffTrees(fromTree, toTree)
	if err != nil {
		return err
	}

	filesMap := buildFilePathMap(files)

	// touched: all paths that the reset will affect (checked for local mods).
	// writtenPaths: subset of touched that will be written to disk (Insert /
	// Modify). An untracked file at such a path would be silently overwritten,
	// which KeepReset must refuse — matching git reset --keep behaviour.
	touched := make(map[string]struct{})
	writtenPaths := make(map[string]struct{})

	for _, ch := range changes {
		// Canonical path for the files-filter: prefer To (Insert/Modify),
		// fall back to From (Delete).
		var name string
		if ch.To != nil {
			name = ch.To.String()
		} else {
			name = ch.From.String()
		}
		if len(files) > 0 && !inFiles(filesMap, name) {
			continue
		}
		if ch.From != nil {
			touched[ch.From.String()] = struct{}{}
		}
		if ch.To != nil {
			touched[ch.To.String()] = struct{}{}
			writtenPaths[ch.To.String()] = struct{}{}
		}
	}

	// When SparseDirs is changing, any currently-on-disk tracked file that
	// falls outside the new sparse set will be removed from disk by step 3 of
	// resetWorktreeToTree. KeepReset must refuse if such a file has local mods.
	if len(sparseDirs) > 0 {
		idx, err := w.r.Storer.Index()
		if err != nil {
			return err
		}
		for _, e := range idx.Entries {
			if e.SkipWorktree {
				continue // already excluded from the worktree
			}
			if len(files) > 0 && !inFiles(filesMap, e.Name) {
				continue
			}
			included := false
			for _, dir := range sparseDirs {
				if strings.HasPrefix(e.Name, dir+"/") || e.Name == dir {
					included = true
					break
				}
			}
			if !included {
				touched[e.Name] = struct{}{}
			}
		}
	}

	if len(touched) == 0 {
		return nil
	}

	// Check worktree status for local modifications on touched paths.
	status, err := w.Status()
	if err != nil {
		return err
	}
	for path, st := range status {
		if _, willChange := touched[path]; !willChange {
			continue
		}
		// Tracked file with staged or unstaged local changes → conflict.
		if (st.Staging != Unmodified && st.Staging != Untracked) ||
			(st.Worktree != Unmodified && st.Worktree != Untracked) {
			return fmt.Errorf("%w: %s", ErrLocalChanges, path)
		}
		// Untracked file at a path that will be written to disk → the reset
		// would silently overwrite it, which KeepReset must refuse.
		if st.Staging == Untracked && st.Worktree == Untracked {
			if _, willWrite := writtenPaths[path]; willWrite {
				return fmt.Errorf("%w: %s", ErrLocalChanges, path)
			}
		}
	}
	return nil
}

// resetWorktreeToTree updates the worktree to match toTree, mirroring
// real git reset --hard / checkout -f:
//
//  1. Tree-to-tree diff (fromTree→toTree): remove files that were tracked in
//     fromTree but deleted in toTree. Because the diff is purely object-graph,
//     untracked files never appear and are never deleted.
//
//  2. New-index-to-worktree diff: write files that are in the new index but
//     absent or different on disk. For Delete actions (file on disk, but absent
//     from the index): the file is truly untracked and is preserved.
//     (SkipWorktree entries are invisible to the merkletrie diff; they are
//     handled in step 3.)
//
//  3. Remove SkipWorktree files from disk: diffStagingWithWorktree never
//     surfaces SkipWorktree-flagged entries as Delete actions because the
//     merkletrie marks them skip=true. Mirror git's behaviour: any tracked
//     file with SkipWorktree=true must not exist in the worktree.
//
// files optionally restricts the operation to a specific subset of paths.
func (w *Worktree) resetWorktreeToTree(cfg *config.Config, fromTree, toTree *object.Tree, files []string) error {
	filesMap := buildFilePathMap(files)

	fs, closeFS := w.reusableRootFS()
	defer closeFS()

	// Step 1: delete files removed from the tracked tree.
	treeChanges, err := diffTrees(fromTree, toTree)
	if err != nil {
		return err
	}
	for _, ch := range treeChanges {
		a, err := ch.Action()
		if err != nil {
			return err
		}
		if a != merkletrie.Delete {
			continue
		}
		name := ch.From.String()
		if len(files) > 0 && !inFiles(filesMap, name) {
			continue
		}
		if err := rmFileAndDirsIfEmpty(fs, name); err != nil {
			return err
		}
	}

	// Step 2: write files that are in the new index but missing or stale
	// in the worktree. We use diffStagingWithWorktree(reverse=true) which
	// gives Insert for "in index, not on disk" and Modify for "differs".
	// Delete means "on disk, not in index" = untracked → always skip.
	// Note: SkipWorktree entries are invisible to the merkletrie diff;
	// they are cleaned up in step 3 below.
	//
	// excludeIgnoredChanges=true engages the filesystem walker's
	// IgnoreMatcher so untracked entries inside gitignored directories are
	// pruned at enumeration time rather than walked and then dropped as
	// Delete actions. The observable result is unchanged because Delete
	// actions are skipped by the loop below; the matcher only avoids the
	// pointless lstat of every file under directories like node_modules.
	worktreeChanges, err := w.diffStagingWithWorktree(cfg, true, true)
	if err != nil {
		return err
	}

	idx, err := w.r.Storer.Index()
	if err != nil {
		return err
	}
	b := newIndexBuilder(idx)

	for _, ch := range worktreeChanges {
		a, err := ch.Action()
		if err != nil {
			return err
		}
		if a == merkletrie.Delete {
			// In the reverse diff, Delete means the file exists on disk but is
			// absent from the index node tree. SkipWorktree entries are marked
			// skip=true in the merkletrie and never appear here; they are removed
			// in step 3 below. A Delete action here therefore always means the
			// file is truly untracked — preserve it.
			continue
		}

		if len(files) > 0 {
			file := ch.To.String()
			if !inFiles(filesMap, file) {
				continue
			}
		}

		if err := w.checkoutChange(cfg, fs, ch, toTree, b); err != nil {
			return err
		}
	}

	// Step 3: remove tracked files that are SkipWorktree=true from disk.
	// diffStagingWithWorktree builds the index node tree with skip=true for
	// SkipWorktree entries, so they never appear as Delete actions in step 2.
	// git removes these files when the sparse-checkout contract excludes them.
	for _, e := range idx.Entries {
		if !e.SkipWorktree {
			continue
		}
		if len(files) > 0 && !inFiles(filesMap, e.Name) {
			continue
		}
		if _, statErr := fs.Lstat(e.Name); os.IsNotExist(statErr) {
			continue
		}
		if err := rmFileAndDirsIfEmpty(fs, e.Name); err != nil {
			return err
		}
	}

	b.Write(idx)
	return w.r.Storer.SetIndex(idx)
}

// resetWorktree updates the worktree to match the staging area.
// files restricts the operation to the named paths; nil means all files.
//
// excludeIgnoredChanges is intentionally false here. The caller is
// MergeReset, and files contains paths that were just removed from the
// index by resetIndex. A tracked-but-gitignored path that was removed
// from the index in this same Reset is no longer in idxMap, so the
// noder's IgnoreMatcher would prune it from the walk and the Delete
// action needed to remove it from disk would never be emitted.
func (w *Worktree) resetWorktree(cfg *config.Config, t *object.Tree, files []string) error {
	changes, err := w.diffStagingWithWorktree(cfg, true, false)
	if err != nil {
		return err
	}

	idx, err := w.r.Storer.Index()
	if err != nil {
		return err
	}
	b := newIndexBuilder(idx)

	fs, closeFS := w.reusableRootFS()
	defer closeFS()

	filesMap := buildFilePathMap(files)
	for _, ch := range changes {
		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(filesMap, file)
			if !contains {
				continue
			}
		}

		if err := w.checkoutChange(cfg, fs, ch, t, b); err != nil {
			return err
		}
	}

	b.Write(idx)
	return w.r.Storer.SetIndex(idx)
}

func (w *Worktree) checkoutChange(cfg *config.Config, fs *worktreeFilesystem, ch merkletrie.Change, t *object.Tree, idx *indexBuilder) error {
	a, err := ch.Action()
	if err != nil {
		return err
	}

	var e *object.TreeEntry
	var name string
	var isSubmodule bool

	switch a {
	case merkletrie.Modify, merkletrie.Insert:
		name = ch.To.String()
		// FindEntry validates name via pathutil.ValidTreePath, so a
		// dangerous tree-derived path is refused at the lookup
		// boundary before we materialise anything.
		e, err = t.FindEntry(name)
		if err != nil {
			return err
		}

		isSubmodule = e.Mode == filemode.Submodule
	case merkletrie.Delete:
		// checkoutChange.Delete is only reached from resetWorktree's
		// filesystem-vs-index merkletrie diff (resetWorktreeToTree's
		// tree-derived deletes call rmFileAndDirsIfEmpty directly).
		// The path source is therefore the local worktree filesystem,
		// where the tolerant worktreeFilesystem wrapper is the right
		// fit: we want to be able to clean up legitimately-tracked
		// shapes like "submodule/.git" rather than abort the whole
		// reset on a single weird untracked file.
		return rmFileAndDirsIfEmpty(fs, ch.From.String())
	}

	if isSubmodule {
		return w.checkoutChangeSubmodule(fs, name, a, e, idx)
	}

	return w.checkoutChangeRegularFile(cfg, fs, name, a, t, e, idx)
}

func (w *Worktree) containsUnstagedChanges(cfg *config.Config) (bool, error) {
	ch, err := w.diffStagingWithWorktree(cfg, false, true)
	if err != nil {
		return false, err
	}

	for _, c := range ch {
		a, err := c.Action()
		if err != nil {
			return false, err
		}

		if a == merkletrie.Insert {
			continue
		}

		return true, nil
	}

	return false, nil
}

func (w *Worktree) setHEADCommit(commit plumbing.Hash) error {
	head, err := w.r.Reference(plumbing.HEAD, false)
	if err != nil {
		return err
	}

	if head.Type() == plumbing.HashReference {
		head = plumbing.NewHashReference(plumbing.HEAD, commit)
		return w.r.Storer.SetReference(head)
	}

	branch, err := w.r.Reference(head.Target(), false)
	if err != nil {
		return err
	}

	if !branch.Name().IsBranch() {
		return fmt.Errorf("invalid HEAD target should be a branch, found %s", branch.Type())
	}

	branch = plumbing.NewHashReference(branch.Name(), commit)
	return w.r.Storer.SetReference(branch)
}

func (w *Worktree) checkoutChangeSubmodule(fs *worktreeFilesystem,
	name string,
	a merkletrie.Action,
	e *object.TreeEntry,
	idx *indexBuilder,
) error {
	switch a {
	case merkletrie.Modify:
		sub, err := w.Submodule(name)
		if err != nil {
			return err
		}

		if !sub.initialized {
			return nil
		}

		return w.addIndexFromTreeEntry(name, e, idx)
	case merkletrie.Insert:
		mode, err := e.Mode.ToOSFileMode()
		if err != nil {
			return err
		}

		if err := w.clearBlockingSymlinks(fs, name); err != nil {
			return err
		}

		if err := fs.MkdirAll(name, mode); err != nil {
			return err
		}

		return w.addIndexFromTreeEntry(name, e, idx)
	}

	return nil
}

func (w *Worktree) checkoutChangeRegularFile(cfg *config.Config,
	fs *worktreeFilesystem,
	name string,
	a merkletrie.Action,
	t *object.Tree,
	e *object.TreeEntry,
	idx *indexBuilder,
) error {
	switch a {
	case merkletrie.Modify:
		idx.Remove(name)

		// to apply perm changes the file is deleted, billy doesn't implement
		// chmod
		if err := fs.Remove(name); err != nil {
			return err
		}

		fallthrough
	case merkletrie.Insert:
		f, err := t.File(name)
		if err != nil {
			return err
		}

		if err := w.checkoutFile(cfg, fs, f); err != nil {
			return err
		}

		return w.addIndexFromFile(fs, name, e.Hash, idx)
	}

	return nil
}

// clearBlockingSymlinks removes a symlink that is in the way of
// materialising name, so the checkout writes a real entry in its place
// instead of following the link out of the worktree. Two cases:
//
//   - a leading directory component that is a symlink (e.g. "s" while
//     writing "s/config", where "s" links to ".git"): OpenFile/MkdirAll
//     would traverse it, so the write would land under the link's target.
//   - the final component itself being a symlink (e.g. writing "s" while
//     "s" links to ".git/config"): OpenFile with O_TRUNC, or Symlink,
//     would follow/replace through it and clobber the target.
//
// A symlink can never be a legitimate parent of, or the destination for,
// a tracked entry, so removing it is always correct. This mirrors upstream
// Git's forced checkout, which unlinks a blocking symlink in the leading
// path (create_directories) and unlinks an existing entry before
// write_entry.
// https://github.com/git/git/blob/v2.54.0/entry.c#L50
func (w *Worktree) clearBlockingSymlinks(fs *worktreeFilesystem, name string) error {
	var dirs []string
	for dir := filepath.Dir(name); dir != "." && dir != "" && dir != string(filepath.Separator); dir = filepath.Dir(dir) {
		dirs = append(dirs, dir)
	}
	// Leading components, shallowest-first: removing the shallowest symlink
	// invalidates every component beneath it, so a single removal is enough.
	for i := len(dirs) - 1; i >= 0; i-- {
		fi, err := fs.Lstat(dirs[i])
		if err != nil {
			continue
		}
		if fi.Mode()&os.ModeSymlink != 0 {
			return fs.Remove(dirs[i])
		}
	}
	// Final component: an existing symlink here would be followed by the
	// subsequent OpenFile/Symlink/MkdirAll, so replace it.
	if fi, err := fs.Lstat(name); err == nil && fi.Mode()&os.ModeSymlink != 0 {
		return fs.Remove(name)
	}
	return nil
}

func (w *Worktree) checkoutFile(cfg *config.Config, fs *worktreeFilesystem, f *object.File) (err error) {
	// checkoutFile is the materialisation boundary for tracked entries.
	// Remove any blocking symlink first so the subsequent OpenFile or
	// Symlink call writes the entry itself instead of following a planted
	// final-component link in the underlying filesystem.
	if err := w.clearBlockingSymlinks(fs, f.Name); err != nil {
		return err
	}

	mode, err := f.Mode.ToOSFileMode()
	if err != nil {
		return err
	}

	if mode&os.ModeSymlink != 0 {
		return w.checkoutFileSymlink(fs, f)
	}

	dstFile, err := fs.OpenFile(f.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm())
	if err != nil {
		return err
	}
	defer ioutil.CheckClose(dstFile, &err)

	return w.copyObjectToWorktree(cfg, f, dstFile)
}

func (w *Worktree) copyObjectToWorktree(cfg *config.Config, object *object.File, file billy.File) (err error) {
	var src io.ReadCloser
	var dst io.Writer = file

	src, err = object.Reader()
	if err != nil {
		return err
	}
	defer ioutil.CheckClose(src, &err)

	if cfg.Core.AutoCRLF == "true" {
		br := sync.GetBufioReader(src)
		defer sync.PutBufioReader(br)

		stat, err := convert.GetStat(br)
		if err != nil {
			return err
		}

		src, err = object.Reader()
		if err != nil {
			return err
		}
		defer ioutil.CheckClose(src, &err)

		if !stat.IsBinary() {
			dst = convert.NewCRLFWriter(dst)
		}
	}

	_, err = ioutil.CopyBufferPool(dst, src)
	return err
}

func (w *Worktree) checkoutFileSymlink(fs *worktreeFilesystem, f *object.File) (err error) {
	// .gitmodules symlink rejection (and its NTFS / HFS variants) is
	// enforced by the worktreeFilesystem wrapper's Symlink method via
	// validSymlinkName. See https://github.com/git/git/commit/10ecfa7
	// for the upstream rationale.

	from, err := f.Reader()
	if err != nil {
		return err
	}

	defer ioutil.CheckClose(from, &err)

	bytes, err := io.ReadAll(from)
	if err != nil {
		return err
	}

	err = fs.Symlink(string(bytes), f.Name)

	// On windows, this might fail.
	// Follow Git on Windows behavior by writing the link as it is.
	if err != nil && isSymlinkWindowsNonAdmin(err) {
		mode, _ := f.Mode.ToOSFileMode()

		to, err := fs.OpenFile(f.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm())
		if err != nil {
			return err
		}

		defer ioutil.CheckClose(to, &err)

		_, err = to.Write(bytes)
		return err
	}
	return err
}

func (w *Worktree) addIndexFromTreeEntry(name string, f *object.TreeEntry, idx *indexBuilder) error {
	idx.Remove(name)
	idx.Add(&index.Entry{
		Hash: f.Hash,
		Name: name,
		Mode: filemode.Submodule,
	})
	return nil
}

func (w *Worktree) addIndexFromFile(fs *worktreeFilesystem, name string, h plumbing.Hash, idx *indexBuilder) error {
	idx.Remove(name)
	fi, err := fs.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,
		Mode:       mode,
		ModifiedAt: fi.ModTime(),
		Size:       uint32(fi.Size()),
	}

	// if the FileInfo.Sys() comes from os the ctime, dev, inode, uid and gid
	// can be retrieved, otherwise this doesn't apply
	if fillSystemInfo != nil {
		fillSystemInfo(e, fi.Sys())
	}
	idx.Add(e)
	return nil
}

func (r *Repository) getTreeFromCommitHash(commit plumbing.Hash) (*object.Tree, error) {
	c, err := r.CommitObject(commit)
	if err != nil {
		return nil, err
	}

	return c.Tree()
}

var fillSystemInfo func(e *index.Entry, sys any)

const gitmodulesFile = ".gitmodules"

// Submodule returns the submodule with the given name
func (w *Worktree) Submodule(name string) (*Submodule, error) {
	l, err := w.Submodules()
	if err != nil {
		return nil, err
	}

	for _, m := range l {
		if m.Config().Name == name {
			return m, nil
		}
	}

	return nil, ErrSubmoduleNotFound
}

// resolveModuleURL resolves relative URLs based on the originURL.
// The moduleURL is returned verbatim when originURL is empty or moduleURL is an absolute URL.
func resolveModuleURL(originURL, moduleURL string) (string, error) {
	if originURL == "" {
		return moduleURL, nil
	}
	if !strings.HasPrefix(moduleURL, "../") && !strings.HasPrefix(moduleURL, "./") {
		return moduleURL, nil
	}
	if !giturl.MatchesScheme(originURL) && giturl.MatchesScpLike(originURL) {
		user, host, portStr, p := giturl.FindScpLikeComponents(originURL)
		p = path.Join(p, moduleURL)
		if portStr != "" {
			portStr += ":"
		}
		return fmt.Sprintf("%s@%s:%s%s", user, host, portStr, p), nil
	}
	base, err := url.Parse(originURL)
	if err != nil {
		return "", err
	}
	base.Path = path.Join(base.Path, moduleURL)
	return base.String(), nil
}

// Submodules returns all the available submodules
func (w *Worktree) Submodules() (Submodules, error) {
	cfg, err := w.r.Config()
	if err != nil {
		return nil, err
	}

	return w.submodulesWithConfig(cfg)
}

// submodulesWithConfig returns all the available submodules using an already-loaded config.
func (w *Worktree) submodulesWithConfig(cfg *config.Config) (Submodules, error) {
	l := make(Submodules, 0)
	m, err := w.readGitmodulesFile()
	if err != nil || m == nil {
		return l, err
	}

	var originURL string
	if origin, err := w.r.Remote(DefaultRemoteName); err == nil {
		if origin.c != nil && len(origin.c.URLs) > 0 {
			originURL = origin.c.URLs[0]
		}
	}

	for _, s := range m.Submodules {
		sub := w.newSubmodule(s, cfg.Submodules[s.Name])
		subCfg := sub.Config()
		resolvedURL, err := resolveModuleURL(originURL, subCfg.URL)
		if err != nil {
			return nil, fmt.Errorf("failed to resolve submodule URL %q: %w", s.URL, err)
		}
		subCfg.URL = resolvedURL
		l = append(l, sub)
	}

	return l, nil
}

func (w *Worktree) newSubmodule(fromModules, fromConfig *config.Submodule) *Submodule {
	m := &Submodule{w: w}
	m.initialized = fromConfig != nil

	if !m.initialized {
		m.c = fromModules
		return m
	}

	m.c = fromConfig
	m.c.Path = fromModules.Path
	return m
}

func (w *Worktree) isSymlink(path string) bool {
	if s, err := w.filesystem.Lstat(path); err == nil {
		return s.Mode()&os.ModeSymlink != 0
	}
	return false
}

func (w *Worktree) readGitmodulesFile() (*config.Modules, error) {
	if w.isSymlink(gitmodulesFile) {
		return nil, ErrGitModulesSymlink
	}

	f, err := w.filesystem.Open(gitmodulesFile)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}

		return nil, err
	}

	defer func() { _ = f.Close() }()
	input, err := io.ReadAll(f)
	if err != nil {
		return nil, err
	}

	m := config.NewModules()
	if err := m.Unmarshal(input); err != nil {
		return nil, err
	}

	return m, nil
}

// Clean the worktree by removing untracked files.
// An empty dir could be removed - this is what  `git clean -f -d .` does.
func (w *Worktree) Clean(opts *CleanOptions) error {
	s, err := w.Status()
	if err != nil {
		return err
	}

	root := ""
	files, err := w.filesystem.ReadDir(root)
	if err != nil {
		return err
	}
	return w.doClean(s, opts, root, files)
}

func (w *Worktree) doClean(status Status, opts *CleanOptions, dir string, files []fs.DirEntry) error {
	for _, fi := range files {
		if fi.Name() == GitDirName {
			continue
		}

		// relative path under the root
		path := filepath.Join(dir, fi.Name())
		if fi.IsDir() {
			if !opts.Dir {
				continue
			}

			subfiles, err := w.filesystem.ReadDir(path)
			if err != nil {
				return err
			}
			err = w.doClean(status, opts, path, subfiles)
			if err != nil {
				return err
			}
		} else if status.IsUntracked(path) {
			if err := w.filesystem.Remove(path); err != nil {
				return err
			}
		}
	}

	if opts.Dir && dir != "" {
		_, err := removeDirIfEmpty(w.filesystem, dir)
		return err
	}

	return nil
}

// GrepResult is structure of a grep result.
type GrepResult struct {
	// FileName is the name of file which contains match.
	FileName string
	// LineNumber is the line number of a file at which a match was found.
	LineNumber int
	// Content is the content of the file at the matching line.
	Content string
	// TreeName is the name of the tree (reference name/commit hash) at
	// which the match was performed.
	TreeName string
}

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 repository.
func (r *Repository) Grep(opts *GrepOptions) ([]GrepResult, error) {
	if err := opts.validate(r); err != nil {
		return nil, err
	}

	// Obtain commit hash from options (CommitHash or ReferenceName).
	var commitHash plumbing.Hash
	// treeName contains the value of TreeName in GrepResult.
	var treeName string

	if opts.ReferenceName != "" {
		ref, err := r.Reference(opts.ReferenceName, true)
		if err != nil {
			return nil, err
		}
		commitHash = ref.Hash()
		treeName = opts.ReferenceName.String()
	} else if !opts.CommitHash.IsZero() {
		commitHash = opts.CommitHash
		treeName = opts.CommitHash.String()
	}

	// Obtain a tree from the commit hash and get a tracked files iterator from
	// the tree.
	tree, err := r.getTreeFromCommitHash(commitHash)
	if err != nil {
		return nil, err
	}
	fileiter := tree.Files()

	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.
func findMatchInFiles(fileiter *object.FileIter, treeName string, opts *GrepOptions) ([]GrepResult, error) {
	var results []GrepResult

	err := fileiter.ForEach(func(file *object.File) error {
		var fileInPathSpec bool

		// When no pathspecs are provided, search all the files.
		if len(opts.PathSpecs) == 0 {
			fileInPathSpec = true
		}

		// Check if the file name matches with the pathspec. Break out of the
		// loop once a match is found.
		for _, pathSpec := range opts.PathSpecs {
			if pathSpec != nil && pathSpec.MatchString(file.Name) {
				fileInPathSpec = true
				break
			}
		}

		// If the file does not match with any of the pathspec, skip it.
		if !fileInPathSpec {
			return nil
		}

		grepResults, err := findMatchInFile(file, treeName, opts)
		if err != nil {
			return err
		}
		results = append(results, grepResults...)

		return nil
	})

	return results, err
}

// findMatchInFile takes a single File, worktree name and GrepOptions,
// and returns a slice of GrepResult containing the result of regex pattern
// matching in the given file.
func findMatchInFile(file *object.File, treeName string, opts *GrepOptions) ([]GrepResult, error) {
	var grepResults []GrepResult

	content, err := file.Contents()
	if err != nil {
		return grepResults, err
	}

	// Split the file content and parse line-by-line.
	contentByLine := strings.Split(content, "\n")
	for lineNum, cnt := range contentByLine {
		addToResult := false

		// Match the patterns and content. Break out of the loop once a
		// match is found.
		for _, pattern := range opts.Patterns {
			if pattern != nil && pattern.MatchString(cnt) {
				// Add to result only if invert match is not enabled.
				if !opts.InvertMatch {
					addToResult = true
					break
				}
			} else if opts.InvertMatch {
				// If matching fails, and invert match is enabled, add to
				// results.
				addToResult = true
				break
			}
		}

		if addToResult {
			grepResults = append(grepResults, GrepResult{
				FileName:   file.Name,
				LineNumber: lineNum + 1,
				Content:    cnt,
				TreeName:   treeName,
			})
		}
	}

	return grepResults, nil
}

// 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)
	for dir != "." && dir != "" {
		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
}

// 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 false, err
	}

	if len(files) > 0 {
		return false, nil
	}

	err = fs.Remove(dir)
	if err != nil {
		return false, err
	}

	return true, nil
}

type indexBuilder struct {
	entries map[string]*index.Entry
}

func newIndexBuilder(idx *index.Index) *indexBuilder {
	entries := make(map[string]*index.Entry, len(idx.Entries))
	for _, e := range idx.Entries {
		entries[e.Name] = e
	}
	return &indexBuilder{
		entries: entries,
	}
}

func (b *indexBuilder) Write(idx *index.Index) {
	idx.Entries = idx.Entries[:0]
	for _, e := range b.entries {
		idx.Entries = append(idx.Entries, e)
	}
}

func (b *indexBuilder) Add(e *index.Entry) {
	b.entries[e.Name] = e
}

func (b *indexBuilder) Remove(name string) {
	delete(b.entries, filepath.ToSlash(name))
}

// buildFilePathMap creates a map of cleaned file paths for efficient lookup.
// Returns nil if the input slice is empty.
func buildFilePathMap(files []string) map[string]struct{} {
	if len(files) == 0 {
		return nil
	}
	filesMap := make(map[string]struct{}, len(files))
	for _, f := range files {
		filesMap[filepath.Clean(f)] = struct{}{}
	}
	return filesMap
}
