Skip to content

Commit

Permalink
[skip ci] fix applying the patch
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Feb 7, 2023
1 parent d3891ea commit b688631
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
32 changes: 32 additions & 0 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ func (r *Repository) SaveUnstaged(files []string) error {
return err
}

func (r *Repository) HideUnstaged(files []string) error {
_, err := execGitCmd(
append([]string{
"git",
"checkout",
"--force",
"--",
}, files...)...,
)

return err
}

func (r *Repository) RestoreUnstaged() error {
if ok, _ := afero.Exists(r.Fs, r.unstagedPatchPath); !ok {
return nil
Expand All @@ -160,6 +173,10 @@ func (r *Repository) RestoreUnstaged() error {
r.unstagedPatchPath,
)

if err == nil {
err = r.Fs.Remove(r.unstagedPatchPath)
}

return err
}

Expand All @@ -185,6 +202,18 @@ func (r *Repository) StashUnstaged() (string, error) {
return stashHash, nil
}

func (r *Repository) DropUnstagedStash(hash string) error {
_, err := execGitCmd(
"git",
"stash",
"drop",
"--quiet",
hash,
)

return err
}

// FilesByCommand accepts git command and returns its result as a list of filepaths.
func (r *Repository) FilesByCommand(command string) ([]string, error) {

Expand Down Expand Up @@ -252,8 +281,11 @@ func execGit(command string) (string, error) {
return execGitCmd(args...)
}

// execGitCmd executes git command with LEFTHOOK=0 in order
// to prevent calling subsequent lefthook hooks.
func execGitCmd(args ...string) (string, error) {
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = append(os.Environ(), "LEFTHOOK=0")

out, err := cmd.CombinedOutput()
if err != nil {
Expand Down
21 changes: 17 additions & 4 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *Runner) runLFSHook() error {

if err != nil && (requiredExists || configExists) {
log.Warn(output)
return fmt.Errorf("git-lfs command failed: %w", err)
return fmt.Errorf("git-lfs command failed: %s", err)
}

return nil
Expand All @@ -171,7 +171,7 @@ func (r *Runner) preHook() {

partiallyStagedFiles, err := r.Repo.PartiallyStagedFiles()
if err != nil {
log.Warnf("Couldn't find partially staged files: %w\n", err)
log.Warnf("Couldn't find partially staged files: %s\n", err)
return
}

Expand All @@ -184,16 +184,29 @@ func (r *Runner) preHook() {

stashHash, err := r.Repo.StashUnstaged()
if err != nil {
log.Warnf("Couldn't stash partially staged files: %w\n", err)
log.Warnf("Couldn't stash partially staged files: %s\n", err)
return
}

err = r.Repo.HideUnstaged(r.partiallyStagedFiles)
if err != nil {
log.Warnf("Couldn't hide unstaged files: %s\n", err)
return
}

log.Debugf("[lefthook] hide partially staged files: %v\n", r.partiallyStagedFiles)

r.partiallyStagedStashHash = stashHash
}

func (r *Runner) postHook() {
if err := r.Repo.RestoreUnstaged(); err != nil {
log.Warnf("Couldn't restore hiden unstaged files: %w\n", err)
log.Warnf("Couldn't restore hidden unstaged files: %s\n", err)
return
}

if err := r.Repo.DropUnstagedStash(r.partiallyStagedStashHash); err != nil {
log.Warnf("Couldn't remove unstaged files backup: %s\n", err)
}
}

Expand Down

0 comments on commit b688631

Please sign in to comment.