Skip to content

Commit d08c0d1

Browse files
committed
Feat: Add checkout master branch in plugin repo cmd
1 parent f583d53 commit d08c0d1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ If your CWD is `~/sites/financepeople/wp-content/plugins/dc-post-grid/`, dc-plug
3737
- `$ dc-plugged uv patch` | `$ dc-plugged update-version patch`
3838
- Update version numbers in multiple files
3939
- `$ dc-plugged uv --files package-lock.json,dc-post-grid.php patch`
40+
- Checkout the master branch in plugin repo
41+
- `$ dc-plugged cm` | `$ dc-plugged checkout-master`

main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ func stageChanges() {
162162
getThePluginDir())
163163
}
164164

165+
// Checks out the master branch in the plugin repo as long as the repo is clean
166+
func checkoutMasterBranch() {
167+
// Open the repo so we can make changes to it
168+
repo, err := git.PlainOpen(getThePluginDir())
169+
check(err)
170+
171+
// Open the worktree
172+
workTree, err := repo.Worktree()
173+
check(err)
174+
175+
// Check if repo is dirty
176+
workTreeStatus, err := workTree.Status()
177+
if !workTreeStatus.IsClean() {
178+
fmt.Printf("%s is dirty.\n", getThePluginDir())
179+
fmt.Println("Please commit or stash your changes.")
180+
return
181+
}
182+
183+
// Checkout the new branch
184+
err = workTree.Checkout(&git.CheckoutOptions{Branch: "refs/heads/master"})
185+
check(err)
186+
187+
if err == nil {
188+
fmt.Println("Checked out master branch.")
189+
}
190+
}
191+
165192
// Updates the plugin version number based on input level
166193
// `level` can be: major, minor or patch
167194
// It only changes version number in package.json by default.
@@ -305,6 +332,15 @@ func main() {
305332
return
306333
},
307334
},
335+
{
336+
Name: "checkout-master",
337+
Aliases: []string{"cm"},
338+
Usage: "Checkout master branch in plugin repo",
339+
Action: func(c *cli.Context) (err error) {
340+
checkoutMasterBranch()
341+
return
342+
},
343+
},
308344
{
309345
Name: "setup",
310346
Usage: "Setup needed dirs and stuff for dc-plugged",

0 commit comments

Comments
 (0)