Skip to content

Commit

Permalink
Add GHORG_TARGET_REPOS_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrie30 committed Jan 24, 2024
1 parent e29b712 commit 440e7fe
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [1.9.11] - Unreleased
### Added
- GHORG_TARGET_REPOS_PATH
### Changed
### Deprecated
### Removed
### Fixed
### Security

## [1.9.10] - 1/21/24
### Added
- Automated docker images (#354); thanks @afonsoc12
Expand Down
60 changes: 60 additions & 0 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func cloneFunc(cmd *cobra.Command, argz []string) {
os.Setenv("GHORG_IGNORE_PATH", path)
}

if cmd.Flags().Changed("target-repos-path") {
path := cmd.Flag("target-repos-path").Value.String()
os.Setenv("GHORG_TARGET_REPOS_PATH", path)
}

if cmd.Flags().Changed("git-filter") {
filter := cmd.Flag("git-filter").Value.String()
os.Setenv("GHORG_GIT_FILTER", filter)
Expand Down Expand Up @@ -353,6 +358,23 @@ func printRemainingMessages() {
}
}

func readTargetReposFile() ([]string, error) {
file, err := os.Open(os.Getenv("GHORG_TARGET_REPOS_PATH"))
if err != nil {
return nil, err
}
defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() != "" {
lines = append(lines, scanner.Text())
}
}
return lines, scanner.Err()
}

func readGhorgIgnore() ([]string, error) {
file, err := os.Open(configs.GhorgIgnoreLocation())
if err != nil {
Expand Down Expand Up @@ -538,6 +560,44 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
cloneTargets = filterByExcludeMatchPrefix(cloneTargets)
}

// filter down repos based on target repos flag file path
if os.Getenv("GHORG_TARGET_REPOS_PATH") != "" {
_, err := os.Stat(os.Getenv("GHORG_TARGET_REPOS_PATH"))

if err != nil {
colorlog.PrintErrorAndExit(fmt.Sprintf("Error finding your GHORG_TARGET_REPOS_PATH file, error: %v", err))
}

if !os.IsNotExist(err) {
// Open the file parse each line and remove cloneTargets containing
toTarget, err := readTargetReposFile()
if err != nil {
colorlog.PrintErrorAndExit(fmt.Sprintf("Error parsing your GHORG_TARGET_REPOS_PATH file, error: %v", err))
}

colorlog.PrintInfo("Using GHORG_TARGET_REPOS_PATH, filtering repos down...")

filteredCloneTargets := []scm.Repo{}
var flag bool
for _, cloned := range cloneTargets {
flag = false
for _, targetRepo := range toTarget {
clonedRepoName := strings.TrimSuffix(filepath.Base(cloned.URL), ".git")
if strings.EqualFold(clonedRepoName, targetRepo) {
flag = true
}
}

if flag {
filteredCloneTargets = append(filteredCloneTargets, cloned)
}
}

cloneTargets = filteredCloneTargets

}
}

// filter repos down based on ghorgignore if one exists
_, err := os.Stat(configs.GhorgIgnoreLocation())
if !os.IsNotExist(err) {
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
config string
gitlabGroupExcludeMatchRegex string
ghorgIgnorePath string
targetReposPath string
ghorgReClonePath string
githubAppID string
githubAppPemPath string
Expand Down Expand Up @@ -233,6 +234,7 @@ func InitConfig() {
getOrSetDefaults("GHORG_EXIT_CODE_ON_CLONE_INFOS")
getOrSetDefaults("GHORG_EXIT_CODE_ON_CLONE_ISSUES")
// Optionally set
getOrSetDefaults("GHORG_TARGET_REPOS_PATH")
getOrSetDefaults("GHORG_CLONE_DEPTH")
getOrSetDefaults("GHORG_GITHUB_TOKEN")
getOrSetDefaults("GHORG_COLOR")
Expand Down Expand Up @@ -278,6 +280,7 @@ func init() {
_ = viper.BindPFlag("color", rootCmd.PersistentFlags().Lookup("color"))
_ = viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))

cloneCmd.Flags().StringVar(&targetReposPath, "target-repos-path", "", "GHORG_TARGET_REPOS_PATH - Path to file with list of repo names to clone, file should contain one repo name per line")
cloneCmd.Flags().StringVar(&protocol, "protocol", "", "GHORG_CLONE_PROTOCOL - Protocol to clone with, ssh or https, (default https)")
cloneCmd.Flags().StringVarP(&path, "path", "p", "", "GHORG_ABSOLUTE_PATH_TO_CLONE_TO - Absolute path to the home for ghorg clones. Must start with / (default $HOME/ghorg)")
cloneCmd.Flags().StringVarP(&branch, "branch", "b", "", "GHORG_BRANCH - Branch left checked out for each repo cloned (default master)")
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

const ghorgVersion = "v1.9.10"
const ghorgVersion = "v1.9.11"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
5 changes: 5 additions & 0 deletions sample-conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ GHORG_CONCURRENCY: 25
# flag (--clone-depth) eg: --clone-depth=1
GHORG_CLONE_DEPTH:

# Path to file with list of repo names to clone, file should contain one repo name per line
# This allows you to only clone a specific list of repos
# flag (--target-repos-path)
GHORG_TARGET_REPOS_PATH:

# A comma separated list of topics to filter by, currently github/gitlab/gitea only
# flag (--topics) eg: --topics=frontend
# If any topics exist here, ghorg will only clone repos that match at least one of these topics
Expand Down

0 comments on commit 440e7fe

Please sign in to comment.