-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add switch command
- Loading branch information
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
createOption string | ||
) | ||
|
||
// switchCmd represents the switch command | ||
var switchCmd = &cobra.Command{ | ||
Use: "switch", | ||
Short: "switch branches", | ||
Long: "switch branches", | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
if client.RootGoitPath == "" { | ||
return ErrGoitNotInitialized | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// validation | ||
if len(args) >= 2 { | ||
return errors.New("fatal: only one reference expected") | ||
} | ||
if createOption == "" && len(args) == 0 { | ||
return errors.New("fatal: missing branch") | ||
} else if createOption != "" && len(args) >= 1 { | ||
return errors.New("invalid create option format") | ||
} | ||
|
||
// switch branch == update HEAD | ||
if len(args) == 1 { | ||
if err := client.Head.Update(client.Refs, client.RootGoitPath, args[0]); err != nil { | ||
return fmt.Errorf("fail to update HEAD: %w", err) | ||
} | ||
} | ||
|
||
if createOption != "" { | ||
if err := client.Refs.AddBranch(client.RootGoitPath, createOption, client.Head.Commit.Hash); err != nil { | ||
return fmt.Errorf("fail to create new branch %s: %w", createOption, err) | ||
} | ||
if err := client.Head.Update(client.Refs, client.RootGoitPath, createOption); err != nil { | ||
return fmt.Errorf("fail to update HEAD: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(switchCmd) | ||
|
||
switchCmd.Flags().StringVarP(&createOption, "create", "c", "", "create new branch") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters