<commit_before>package true_git

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"
)

func SyncDevBranchWithStagedFiles(ctx context.Context, gitDir, workTreeCacheDir, commit string) (string, error) {
	var resCommit string

	if err := withWorkTreeCacheLock(ctx, workTreeCacheDir, func() error {
		var err error
		if gitDir, err = filepath.Abs(gitDir); err != nil {
			return fmt.Errorf("bad git dir %s: %s", gitDir, err)
		}

		if workTreeCacheDir, err = filepath.Abs(workTreeCacheDir); err != nil {
			return fmt.Errorf("bad work tree cache dir %s: %s", workTreeCacheDir, err)
		}

		if err := checkSubmoduleConstraint(); err != nil {
			return err
		}

		workTreeDir, err := prepareWorkTree(ctx, gitDir, workTreeCacheDir, commit, true)
		if err != nil {
			return fmt.Errorf("unable to prepare worktree for commit %v: %s", commit, err)
		}

		currentCommitPath := filepath.Join(workTreeCacheDir, "current_commit")
		if err := os.RemoveAll(currentCommitPath); err != nil {
			return fmt.Errorf("unable to remove %s: %s", currentCommitPath, err)
		}

		devBranchName := fmt.Sprintf("werf-dev-%s", commit)
		var isDevBranchExist bool
		if output, err := runGitCmd(ctx, []string{"branch", "--list", devBranchName}, workTreeDir, runGitCmdOptions{}); err != nil {
			return err
		} else {
			isDevBranchExist = output.Len() != 0
		}

		var devHeadCommit string
		if isDevBranchExist {
			if _, err := runGitCmd(ctx, []string{"checkout", devBranchName}, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			}

			if output, err := runGitCmd(ctx, []string{"rev-parse", devBranchName}, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			} else {
				devHeadCommit = strings.TrimSpace(output.String())
			}
		} else {
			if _, err := runGitCmd(ctx, []string{"checkout", "-b", devBranchName, commit}, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			}

			devHeadCommit = commit
		}

		if diffOutput, err := runGitCmd(ctx, []string{"diff", "--cached", devHeadCommit}, gitDir, runGitCmdOptions{}); err != nil {
			return err
		} else if len(diffOutput.Bytes()) == 0 {
			resCommit = devHeadCommit
		} else {
			if _, err := runGitCmd(ctx, []string{"apply", "--index"}, workTreeDir, runGitCmdOptions{stdin: diffOutput}); err != nil {
				return err
			}

			gitArgs := []string{"-c", "user.email=<EMAIL>", "-c", "user.name=werf", "commit", "-m", time.Now().String()}
			if _, err := runGitCmd(ctx, gitArgs, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			}
<commit_msg>[dev] Support staged binary files<commit_after>package true_git

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"
)

func SyncDevBranchWithStagedFiles(ctx context.Context, gitDir, workTreeCacheDir, commit string) (string, error) {
	var resCommit string

	if err := withWorkTreeCacheLock(ctx, workTreeCacheDir, func() error {
		var err error
		if gitDir, err = filepath.Abs(gitDir); err != nil {
			return fmt.Errorf("bad git dir %s: %s", gitDir, err)
		}

		if workTreeCacheDir, err = filepath.Abs(workTreeCacheDir); err != nil {
			return fmt.Errorf("bad work tree cache dir %s: %s", workTreeCacheDir, err)
		}

		if err := checkSubmoduleConstraint(); err != nil {
			return err
		}

		workTreeDir, err := prepareWorkTree(ctx, gitDir, workTreeCacheDir, commit, true)
		if err != nil {
			return fmt.Errorf("unable to prepare worktree for commit %v: %s", commit, err)
		}

		currentCommitPath := filepath.Join(workTreeCacheDir, "current_commit")
		if err := os.RemoveAll(currentCommitPath); err != nil {
			return fmt.Errorf("unable to remove %s: %s", currentCommitPath, err)
		}

		devBranchName := fmt.Sprintf("werf-dev-%s", commit)
		var isDevBranchExist bool
		if output, err := runGitCmd(ctx, []string{"branch", "--list", devBranchName}, workTreeDir, runGitCmdOptions{}); err != nil {
			return err
		} else {
			isDevBranchExist = output.Len() != 0
		}

		var devHeadCommit string
		if isDevBranchExist {
			if _, err := runGitCmd(ctx, []string{"checkout", devBranchName}, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			}

			if output, err := runGitCmd(ctx, []string{"rev-parse", devBranchName}, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			} else {
				devHeadCommit = strings.TrimSpace(output.String())
			}
		} else {
			if _, err := runGitCmd(ctx, []string{"checkout", "-b", devBranchName, commit}, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			}

			devHeadCommit = commit
		}

		gitDiffArgs := []string{
			"-c", "diff.renames=false",
			"-c", "core.quotePath=false",
			"diff",
			"--full-index",
			"--binary",
			"--cached",
			devHeadCommit,
		}
		if diffOutput, err := runGitCmd(ctx, gitDiffArgs, gitDir, runGitCmdOptions{}); err != nil {
			return err
		} else if len(diffOutput.Bytes()) == 0 {
			resCommit = devHeadCommit
		} else {
			if _, err := runGitCmd(ctx, []string{"apply", "--binary", "--index"}, workTreeDir, runGitCmdOptions{stdin: diffOutput}); err != nil {
				return err
			}

			gitArgs := []string{"-c", "user.email=<EMAIL>", "-c", "user.name=werf", "commit", "-m", time.Now().String()}
			if _, err := runGitCmd(ctx, gitArgs, workTreeDir, runGitCmdOptions{}); err != nil {
				return err
			}

{'fraction_non_alphanumeric': 0.15654952076677317, 'fraction_numerical': 0.0007517383950385266, 'mean_word_length': 4.016022620169651, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1750913', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1834, 'n_words': 470}