Skip to content

Commit

Permalink
Merge 3a3c607 into e4635b7
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Mar 26, 2019
2 parents e4635b7 + 3a3c607 commit aab2ec0
Show file tree
Hide file tree
Showing 12 changed files with 611 additions and 74 deletions.
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
9 changes: 3 additions & 6 deletions clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,9 @@ 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
Expand Down
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
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

0 comments on commit aab2ec0

Please sign in to comment.