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

ref #5 implement mv command. #17

Merged
merged 7 commits into from
Mar 26, 2019
Merged
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ RRH is now growing. Please hack RRH itself.
* [`rrh fetch-all`](#rrh-fetch-all)
* [`rrh group`](#rrh-group)
* [`rrh list`](#rrh-list)
* [`rrh mv`](#rrh-mv)
* [`rrh prune`](#rrh-prune)
* [`rrh rm`](#rrh-rm)
* [`rrh status`](#rrh-status)
Expand Down Expand Up @@ -204,6 +205,20 @@ ARGUMENTS
if no groups are specified, all groups are printed.
```

### `rrh mv`

Move repositories to another group.

```sh
rrh mv [OPTIONS] <FROMS...> <TO>
OPTIONS
-v, --verbose verbose mode

ARGUMENTS
FROMS... specifies move from, formatted in <GROUP_NAME/REPO_ID>, or <GROUP_NAME>
TO specifies move to, formatted in <GROUP_NAME>
```

### `rrh prune`

Deletes unnecessary groups and repositories.
Expand Down
16 changes: 1 addition & 15 deletions add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ func (add *AddCommand) isExistAndGitRepository(absPath string, path string) erro
return nil
}

func (add *AddCommand) createGroupIfNeeded(db *common.Database, groupName string) error {
if !db.HasGroup(groupName) {
if db.Config.GetValue(common.RrhAutoCreateGroup) == "true" {
var _, err = db.CreateGroup(groupName, "")
return err
}
}
if db.HasGroup(groupName) {
return nil
}
return fmt.Errorf("%s: group not found", groupName)
}

func checkDuplication(db *common.Database, repoID string, path string) error {
var repo = db.FindRepository(repoID)
if repo != nil && repo.Path != path {
Expand Down Expand Up @@ -73,11 +60,10 @@ func (add *AddCommand) addRepositoryToGroup(db *common.Database, groupName strin
AddRepositoriesToGroup registers the given repositories to the specified group.
*/
func (add *AddCommand) AddRepositoriesToGroup(db *common.Database, args []string, groupName string) []error {
var err = add.createGroupIfNeeded(db, groupName)
var _, err = db.AutoCreateGroup(groupName, "")
if err != nil {
return []error{err}
}

var errorlist = []error{}
for _, item := range args {
errorlist = add.addRepositoryToGroup(db, groupName, item, errorlist)
Expand Down
23 changes: 12 additions & 11 deletions clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ DoClone performs `git clone` command and register the cloned repositories to RRH
*/
func (clone *CloneCommand) DoClone(db *common.Database, arguments []string) (int, []error) {
if len(arguments) == 1 {
var err = clone.DoCloneARepository(db, arguments[0])
var err = clone.doCloneARepository(db, arguments[0])
if err != nil {
return 0, []error{err}
}
return 1, []error{}
}
return clone.doCloneRepositories(db, arguments)
}

func (clone *CloneCommand) doCloneRepositories(db *common.Database, arguments []string) (int, []error) {
var errorlist = []error{}
var count = 0
for _, url := range arguments {
var increment, err = clone.DoCloneEachRepository(db, url)
var increment, err = clone.doCloneEachRepository(db, url)
if err != nil {
errorlist = append(errorlist, err)
if db.Config.GetValue(common.RrhOnError) == common.FailImmediately {
Expand All @@ -72,22 +76,19 @@ func (clone *CloneCommand) DoClone(db *common.Database, arguments []string) (int
}

func (clone *CloneCommand) relateTo(db *common.Database, groupID string, repoID string) error {
if !db.HasGroup(groupID) {
if db.Config.GetValue(common.RrhAutoCreateGroup) == "true" {
db.CreateGroup(groupID, "")
} else {
return fmt.Errorf("%s: group not found", groupID)
}
var _, err = db.AutoCreateGroup(groupID, "")
if err != nil {
return fmt.Errorf("%s: group not found", groupID)
}
db.Relate(groupID, repoID)
return nil
}

/*
DoCloneEachRepository performes `git clone` for each repository.
doCloneEachRepository performes `git clone` for each repository.
This function is called repeatedly.
*/
func (clone *CloneCommand) DoCloneEachRepository(db *common.Database, URL string) (int, error) {
func (clone *CloneCommand) doCloneEachRepository(db *common.Database, URL string) (int, error) {
var count int
var id = findID(URL)
var path = filepath.Join(clone.Options.dest, id)
Expand All @@ -104,7 +105,7 @@ func (clone *CloneCommand) DoCloneEachRepository(db *common.Database, URL string
/*
DoCloneARepository clones a repository from given URL.
*/
func (clone *CloneCommand) DoCloneARepository(db *common.Database, URL string) error {
func (clone *CloneCommand) doCloneARepository(db *common.Database, URL string) error {
var id, path string

if clone.isExistDir(clone.Options.dest) {
Expand Down
8 changes: 6 additions & 2 deletions clone/clone_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func printResult(count int, dest string, group string) {
}
}

func (clone *CloneCommand) parse(args []string, config *common.Config) ([]string, error) {
func (clone *CloneCommand) buildFlagSets(config *common.Config) (*flag.FlagSet, *cloneOptions) {
var defaultGroup = config.GetDefaultValue(common.RrhDefaultGroupName)
var options = cloneOptions{defaultGroup, ".", false}
flags := flag.NewFlagSet("clone", flag.ContinueOnError)
Expand All @@ -114,11 +114,15 @@ func (clone *CloneCommand) parse(args []string, config *common.Config) ([]string
flags.StringVar(&options.dest, "dest", ".", "destination")
flags.BoolVar(&options.verbose, "v", false, "verbose mode")
flags.BoolVar(&options.verbose, "verbose", false, "verbose mode")
return flags, &options
}

func (clone *CloneCommand) parse(args []string, config *common.Config) ([]string, error) {
var flags, options = clone.buildFlagSets(config)
if err := flags.Parse(args); err != nil {
return nil, err
}
clone.Options = &options
clone.Options = options

return flags.Args(), nil
}
8 changes: 8 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func (config *Config) Update(label string, value string) error {
return fmt.Errorf("%s: Unknown variable name", label)
}

func (config *Config) IsSet(label string) bool {
var value = config.GetValue(label)
if label != RrhAutoCreateGroup && label != RrhAutoDeleteGroup && label != RrhSortOnUpdating {
return false
}
return strings.ToLower(value) == "true"
}

func (config *Config) GetValue(label string) string {
var value, _ = config.GetString(label)
return value
Expand Down
12 changes: 11 additions & 1 deletion common/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (db *Database) FindGroup(groupID string) *Group {
}

func sortIfNeeded(db *Database) {
if db.Config.GetValue(RrhSortOnUpdating) != "true" {
if !db.Config.IsSet(RrhSortOnUpdating) {
return
}
sort.Slice(db.Repositories, func(i, j int) bool {
Expand Down Expand Up @@ -170,6 +170,16 @@ func (db *Database) CreateRepository(repoID string, path string, remotes []Remot
return &repo, nil
}

func (db *Database) AutoCreateGroup(groupID string, description string) (*Group, error) {
if db.HasGroup(groupID) {
return db.FindGroup(groupID), nil
}
if db.Config.IsSet(RrhAutoCreateGroup) {
return db.CreateGroup(groupID, description)
}
return nil, fmt.Errorf("%s: could not create group", groupID)
}

/*
CreateGroup returns the group by creating the given parameters and store it to database.
*/
Expand Down
9 changes: 5 additions & 4 deletions group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ func (grc *groupRemoveCommand) removeGroupsImpl(db *common.Database, groupName s

func (grc *groupRemoveCommand) removeGroups(db *common.Database) error {
for _, groupName := range grc.Options.args {
if db.HasGroup(groupName) && grc.Inquiry(groupName) {
if err := grc.removeGroupsImpl(db, groupName); err != nil {
return err
}
if !db.HasGroup(groupName) || !grc.Inquiry(groupName) {
return nil
}
if err := grc.removeGroupsImpl(db, groupName); err != nil {
return err
}
}
return nil
Expand Down
19 changes: 14 additions & 5 deletions group/group_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (grc *groupRemoveCommand) Inquiry(groupName string) bool {
return common.IsInputYes(fmt.Sprintf("%s: remove group? [yN]", groupName))
}

func (grc *groupRemoveCommand) parse(args []string) (*removeOptions, error) {
func (grc *groupRemoveCommand) buildFlagSet() (*flag.FlagSet, *removeOptions) {
var opt = removeOptions{}
flags := flag.NewFlagSet("rm", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(grc.Help()) }
Expand All @@ -244,15 +244,20 @@ func (grc *groupRemoveCommand) parse(args []string) (*removeOptions, error) {
flags.BoolVar(&opt.inquiry, "inquiry", false, "inquiry mode")
flags.BoolVar(&opt.verbose, "verbose", false, "verbose mode")
flags.BoolVar(&opt.force, "force", false, "force remove")
return flags, &opt
}

func (grc *groupRemoveCommand) parse(args []string) (*removeOptions, error) {
var flags, opt = grc.buildFlagSet()
if err := flags.Parse(args); err != nil {
return nil, err
}
opt.args = flags.Args()
if len(opt.args) == 0 {
return nil, fmt.Errorf("no arguments are specified")
}
grc.Options = &opt
return &opt, nil
grc.Options = opt
return opt, nil
}

/*
Expand Down Expand Up @@ -311,7 +316,7 @@ func (guc *groupUpdateCommand) Run(args []string) int {
return 0
}

func (guc *groupUpdateCommand) parse(args []string) (*updateOptions, error) {
func (guc *groupUpdateCommand) buildFlagSet() (*flag.FlagSet, *updateOptions) {
var opt = updateOptions{}
flags := flag.NewFlagSet("update", flag.ContinueOnError)
flags.Usage = func() { fmt.Println(guc.Help()) }
Expand All @@ -321,7 +326,11 @@ func (guc *groupUpdateCommand) parse(args []string) (*updateOptions, error) {
flags.StringVar(&opt.desc, "desc", "", "specify the description")
flags.StringVar(&opt.omitList, "omit-list", "false", "set the omit list flag. ")
flags.StringVar(&opt.omitList, "o", "false", "set the omit list flag. ")
return flags, &opt
}

func (guc *groupUpdateCommand) parse(args []string) (*updateOptions, error) {
var flags, opt = guc.buildFlagSet()
if err := flags.Parse(args); err != nil {
return nil, err
}
Expand All @@ -333,7 +342,7 @@ func (guc *groupUpdateCommand) parse(args []string) (*updateOptions, error) {
return nil, fmt.Errorf("could not accept multiple arguments")
}
opt.target = arguments[0]
return &opt, nil
return opt, nil
}

/*
Expand Down
82 changes: 82 additions & 0 deletions move/move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package move

import (
"fmt"

"github.com/tamada/rrh/common"
)

func (mv *MoveCommand) moveRepositoryToRepository(db *common.Database, from target, to target) error {
if from.repositoryName != to.repositoryName {
return fmt.Errorf("repository name did not match: %s, %s", from.original, to.original)
}
if _, err := db.AutoCreateGroup(to.groupName, ""); err != nil {
return err
}
if from.targetType == GroupAndRepoType {
db.Unrelate(from.groupName, from.repositoryName)
mv.Options.printIfNeeded(fmt.Sprintf("unrelate group %s and repository %s", from.groupName, from.repositoryName))
}
db.Relate(to.groupName, to.repositoryName)
mv.Options.printIfNeeded(fmt.Sprintf("relate group %s and repository %s", to.groupName, to.repositoryName))
return nil
}

func (mv *MoveCommand) moveRepositoryToGroup(db *common.Database, from target, to target) error {
if to.targetType == GroupType || to.targetType == GroupOrRepoType {
if _, err := db.AutoCreateGroup(to.original, ""); err != nil {
return err
}
}
if from.targetType == GroupAndRepoType {
db.Unrelate(from.groupName, from.repositoryName)
}
db.Relate(to.original, from.repositoryName)
return nil
}

func isFailImmediately(config *common.Config) bool {
return config.GetValue(common.RrhOnError) == common.FailImmediately
}

func (mv *MoveCommand) moveRepositoriesToGroup(db *common.Database, froms []target, to target) []error {
var list = []error{}
for _, from := range froms {
var err = mv.moveRepositoryToGroup(db, from, to)
if err != nil {
if isFailImmediately(db.Config) {
return []error{err}
}
list = append(list, err)
}
}
return list
}

func (mv *MoveCommand) moveGroupsToGroup(db *common.Database, froms []target, to target) []error {
var list = []error{}
for _, from := range froms {
var errs = mv.moveGroupToGroup(db, from, to)
if len(errs) != 0 {
if isFailImmediately(db.Config) {
return errs
}
list = append(list, errs...)
}
}
return list
}

func (mv *MoveCommand) moveGroupToGroup(db *common.Database, from target, to target) []error {
if _, err := db.AutoCreateGroup(to.groupName, ""); err != nil {
return []error{err}
}
var repos = db.FindRelationsOfGroup(from.groupName)
for _, repo := range repos {
db.Unrelate(from.groupName, repo)
mv.Options.printIfNeeded(fmt.Sprintf("unrelate group %s and repository %s", from.groupName, repo))
db.Relate(to.groupName, repo)
mv.Options.printIfNeeded(fmt.Sprintf("relate group %s and repository %s", to.groupName, repo))
}
return []error{}
}
Loading