Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check av's state file and REBASE_HEAD to prevent conflict #410

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/git/rebase.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package git

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/sirupsen/logrus"
)

// ref: https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt-codeREBASEHEADcode
const REBASE_STATE_FILE = "REBASE_HEAD"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other places, REBASE_HEAD is defined as string directly.
Should I align with that here as well? It means that I shouldn't define this string as const?


type RebaseOpts struct {
// Required (unless Continue is true)
// The upstream branch to rebase onto.
Expand Down Expand Up @@ -57,6 +63,10 @@ func (r *Repo) Rebase(opts RebaseOpts) (*Output, error) {
args = append(args, opts.Branch)
}

if _, err := os.Stat(filepath.Join(r.GitDir(), REBASE_STATE_FILE)); err == nil {
return nil, fmt.Errorf("failed to start rebase: rebase in progress")
}

return r.Run(&RunOpts{Args: args})
}

Expand Down
9 changes: 8 additions & 1 deletion internal/git/state_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -32,9 +33,15 @@ func (r *Repo) WriteStateFile(kind StateFileKind, msg any) error {
return nil
}

file := filepath.Join(r.AvDir(), string(kind))
if _, err := os.Stat(file); err == nil {
// When the state file already exists, it means that during fixing conflicts
return fmt.Errorf("Please execute the command after fixing the conflict")
}

bs, err := json.MarshalIndent(msg, "", " ")
if err != nil {
return err
}
return os.WriteFile(filepath.Join(r.AvDir(), string(kind)), bs, 0644)
return os.WriteFile(file, bs, 0644)
}
34 changes: 34 additions & 0 deletions internal/git/state_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package git_test

import (
"testing"

"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/git/gittest"
"github.com/stretchr/testify/require"
)

func TestWriteStateFile(t *testing.T) {
type Test struct {
Message string
}

tmpRepo := gittest.NewTempRepo(t)
repo, err := git.OpenRepo(tmpRepo.RepoDir, tmpRepo.GitDir)
require.NoError(t, err)

err = repo.WriteStateFile(git.StateFileKindSync, &Test{Message: "test write kind sync"})
require.NoError(t, err)

// Already state file exists, thus it should return an error
err = repo.WriteStateFile(git.StateFileKindSync, &Test{Message: "test write kind sync 2"})
require.Error(t, err)

// clean up
err = repo.WriteStateFile(git.StateFileKindSync, nil)
require.NoError(t, err)

// Write again
err = repo.WriteStateFile(git.StateFileKindSync, &Test{Message: "test write kind sync 2"})
require.NoError(t, err)
}
Loading