package gitvcs

import (
	
	
	
	
	
	
	

	
)

type preparedTag struct {
	name     string
	commitID string
	message  string
}

type session struct {
	repo    *Repo
	dir     string
	rev     vcs.Revision
	head    string
	tags    []preparedTag
	signing bool
	closed  bool
}

func ( *session) () fs.FS {
	return os.DirFS(.dir)
}

func ( *session) ( string,  []byte) error {
	return vcs.WriteDirFile(.dir, , )
}

func ( *session) ( context.Context) (string, error) {
	,  := runGit(, .repo.program, .dir, "rev-parse", "HEAD")
	if  != nil {
		return "", 
	}
	.head = strings.TrimSpace()
	return .head, nil
}

func ( *session) ( context.Context,  string) ([]vcs.FileEntry, error) {
	return listFiles(, .repo.program, .dir, "HEAD", )
}

func ( *session) ( context.Context,  []string,  vcs.CommitOptions) (string, error) {
	if len() == 0 {
		return .head, nil
	}
	 := append([]string{"add", "--"}, ...)
	if ,  := runGit(, .repo.program, .dir, ...);  != nil {
		return "", fmt.Errorf("git add: %w", )
	}

	 := []string{"commit", "-m", .Message}
	if .repo.cfg.Sign != nil {
		if *.repo.cfg.Sign {
			 = append(, "-S")
		} else {
			 = append(, "--no-gpg-sign")
		}
	}
	if ,  := runGit(, .repo.program, .dir, ...);  != nil {
		return "", fmt.Errorf("git commit: %w", )
	}
	return .Head()
}

func ( *session) ( context.Context,  string,  vcs.TagOptions) error {
	,  := .Head()
	if  != nil {
		return 
	}
	.tags = append(.tags, preparedTag{
		name:     ,
		commitID: ,
		message:  .Message,
	})
	return nil
}

func ( *session) ( context.Context) (vcs.PublishResult, error) {
	,  := .Head()
	if  != nil {
		return vcs.PublishResult{}, 
	}

	var  bytes.Buffer
	var  []preparedTag

	 := ""
	if ,  := runGit(, .repo.program, .repo.root, "symbolic-ref", "-q", "HEAD");  == nil {
		 = strings.TrimSpace()
	}

	 := .rev.Ref != "" &&  != .rev.ID
	 :=  && .rev.Ref != 
	 :=  && .rev.Ref == 

	if  {
		fmt.Fprintf(&, "update %s %s %s\n", .rev.Ref, , .rev.ID)
	}

	 := .repo.cfg.isAnnotated()
	 := .signing
	if .repo.cfg.Sign != nil {
		 = *.repo.cfg.Sign
	}

	for ,  := range .tags {
		switch {
		case !:
			fmt.Fprintf(&, "create refs/tags/%s %s\n", .name, .commitID)
		case :
			 = append(, )
		default:
			,  := .makeAnnotatedTagObject(, )
			if  != nil {
				return vcs.PublishResult{}, 
			}
			fmt.Fprintf(&, "create refs/tags/%s %s\n", .name, )
		}
	}

	if  {
		if ,  := runGit(, .repo.program, .repo.root, "merge", "--ff-only", );  != nil {
			return vcs.PublishResult{}, fmt.Errorf("fast-forward %s to %s: %w", .rev.Label, , )
		}
	}

	if .Len() > 0 {
		if ,  := runGitBytes(, .repo.program, .repo.root, .Bytes(), "update-ref", "--stdin");  != nil {
			return vcs.PublishResult{}, fmt.Errorf("atomic ref update: %w", )
		}
	}

	for ,  := range  {
		if ,  := runGit(, .repo.program, .repo.root, "tag", "-a", "-s", "-m", .message, .name, .commitID);  != nil {
			return vcs.PublishResult{}, fmt.Errorf("create signed tag %s: %w", .name, )
		}
	}

	 := make([]string, len(.tags))
	for ,  := range .tags {
		[] = .name
	}
	return vcs.PublishResult{
		StartID: .rev.ID,
		FinalID: ,
		Ref:     .rev.Ref,
		Label:   .rev.Label,
		Tags:    ,
	}, nil
}

func ( *session) ( context.Context,  preparedTag) (string, error) {
	,  := runGit(, .repo.program, .repo.root, "var", "GIT_COMMITTER_IDENT")
	if  != nil {
		return "", fmt.Errorf("resolve git committer identity: %w", )
	}
	var  bytes.Buffer
	fmt.Fprintf(&, "object %s\n", .commitID)
	.WriteString("type commit\n")
	fmt.Fprintf(&, "tag %s\n", .name)
	fmt.Fprintf(&, "tagger %s\n", strings.TrimSpace())
	.WriteByte('\n')
	.WriteString(.message)
	if !strings.HasSuffix(.message, "\n") {
		.WriteByte('\n')
	}

	,  := runGitBytes(, .repo.program, .repo.root, .Bytes(), "mktag")
	if  != nil {
		return "", fmt.Errorf("create tag object for %s: %w", .name, )
	}
	return strings.TrimSpace(string()), nil
}

func ( *session) () error {
	if .closed {
		return nil
	}
	.closed = true
	,  := runGit(context.Background(), .repo.program, .repo.root, "worktree", "remove", "--force", .dir)
	 := os.RemoveAll(.dir)
	return errors.Join(, )
}