Skip to content

Commit

Permalink
feat: add '--reset' flag to reset the worktree before applying changes
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed May 27, 2021
1 parent 506830e commit 3329958
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ USAGE

FLAGS
-bump-deps false bump dependencies
-checkout-main-branch true switch to the main branch before applying the maintenance
-no-fetch false do not fetch origin
-checkout-main-branch true switch to the main branch before applying the changes
-fetch true fetch origin before applying the changes
-open-pr true open a new pull-request with the changes
-reset false reset dirty worktree before applying the changes
-show-diff true display git diff of the changes
-std true standard maintenance tasks
```
Expand All @@ -54,9 +56,14 @@ USAGE
template-post-clone

FLAGS
-checkout-main-branch true switch to the main branch before applying the templatePostClone script
-no-fetch false do not fetch origin
-show-diff true display git diff of the changes
-checkout-main-branch true switch to the main branch before applying the changes
-fetch true fetch origin before applying the changes
-open-pr true open a new pull-request with the changes
-reset false reset dirty worktree before applying the changes
-rm-go-binary false whether to delete everything related to go binary and only keep a library
-show-diff true display git diff of the changes
-template-name golang-repo-template template's name (to change with the new project's name)
-template-owner moul template owner's name (to change with the new owner)
```

## Install
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type projectOpts struct {
Fetch bool
ShowDiff bool
OpenPR bool
Reset bool
}

type Opts struct {
Expand All @@ -35,9 +36,10 @@ type Opts struct {
Standard bool
}
TemplatePostClone struct {
Project projectOpts
TemplateName string
TemplateOwner string
Project projectOpts
RemoveGoBinary bool
TemplateName string
TemplateOwner string
}
Doctor struct{}
Version struct{}
Expand Down Expand Up @@ -67,6 +69,7 @@ func run(args []string) error {
fs.BoolVar(&opts.Fetch, "fetch", true, "fetch origin before applying the changes")
fs.BoolVar(&opts.ShowDiff, "show-diff", true, "display git diff of the changes")
fs.BoolVar(&opts.OpenPR, "open-pr", true, "open a new pull-request with the changes")
fs.BoolVar(&opts.Reset, "reset", false, "reset dirty worktree before applying the changes")
}
// root
setupRootFlags(rootFs)
Expand All @@ -79,6 +82,7 @@ func run(args []string) error {
setupProjectFlags(templatePostCloneFs, &opts.TemplatePostClone.Project)
templatePostCloneFs.StringVar(&opts.TemplatePostClone.TemplateName, "template-name", "golang-repo-template", "template's name (to change with the new project's name)")
templatePostCloneFs.StringVar(&opts.TemplatePostClone.TemplateOwner, "template-owner", "moul", "template owner's name (to change with the new owner)")
templatePostCloneFs.BoolVar(&opts.TemplatePostClone.RemoveGoBinary, "rm-go-binary", false, "whether to delete everything related to go binary and only keep a library")
// moul.io/<name> -> github.com/something/<name>

// version
Expand Down
24 changes: 23 additions & 1 deletion project.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,29 @@ func (p *project) prepareWorkspace(opts projectOpts) error {
}

if p.Git.IsDirty {
return fmt.Errorf("worktree is dirty, please commit or discard changes before retrying") // nolint:goerr113
if opts.Reset {
// reset
{
err := p.Git.workTree.Reset(&git.ResetOptions{
Mode: git.HardReset,
})
if err != nil {
return fmt.Errorf("reset worktree: %w", err)
}
}
// update status
{
status, err := p.Git.workTree.Status()
if err != nil {
return fmt.Errorf("failed to get status: %w", err)
}
p.Git.status = status
p.Git.IsDirty = !status.IsClean()
}
}
if p.Git.IsDirty {
return fmt.Errorf("worktree is dirty, please commit or discard changes before retrying") // nolint:goerr113
}
}

if opts.Fetch {
Expand Down

0 comments on commit 3329958

Please sign in to comment.