Add/ghorg prune untouched (#459)

This commit is contained in:
gabrie30
2024-09-22 15:49:21 -07:00
committed by GitHub
parent 1ca84c6c42
commit 230725234c
9 changed files with 298 additions and 99 deletions

View File

@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
### Added
- Reclone name and description to reclone output
- GHORG_PRESERVE_SCM_HOSTNAME, note that this feature changes the directory struture that gitlab all-users and all-groups clone into; thanks @rrrix
- GHORG_PRUNE_UNTOUCHED, to prune repos that users make no changes in; thanks @MaxG87
### Changed
### Deprecated
### Removed

View File

@@ -187,6 +187,14 @@ func cloneFunc(cmd *cobra.Command, argz []string) {
os.Setenv("GHORG_PRUNE_NO_CONFIRM", "true")
}
if cmd.Flags().Changed("prune-untouched") {
os.Setenv("GHORG_PRUNE_UNTOUCHED", "true")
}
if cmd.Flags().Changed("prune-untouched-no-confirm") {
os.Setenv("GHORG_PRUNE_UNTOUCHED_NO_CONFIRM", "true")
}
if cmd.Flags().Changed("fetch-all") {
os.Setenv("GHORG_FETCH_ALL", "true")
}
@@ -663,6 +671,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
// maps in go are not safe for concurrent use
var mutex = &sync.RWMutex{}
var untouchedReposToPrune []string
for i := range cloneTargets {
repo := cloneTargets[i]
@@ -738,8 +747,61 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
repo.HostPath = filepath.Join(outputDirAbsolutePath, repoSlug, repo.GitLabSnippetInfo.Title+"-"+repo.GitLabSnippetInfo.ID)
}
action := "cloning"
repoWillBePulled := repoExistsLocally(repo)
// Repos are considered untouched if
// 1. There are no new branches, ghorg only clones one branch so if there are more then the user has done something in the repo
// 2. If there are no branches locally, this means the repo is empty or all commits have been removed
// 3. If there are any commits on the default branch locally that are not on the remote
// 4. There are any modified changes locally
if os.Getenv("GHORG_PRUNE_UNTOUCHED") == "true" && repoWillBePulled {
git.FetchCloneBranch(repo)
branches, err := git.Branch(repo)
if err != nil {
colorlog.PrintError(fmt.Sprintf("Failed to list local branches for repository %s: %v", repo.Name, err))
return
}
// Delete if it has no branches
if branches == "" {
untouchedReposToPrune = append(untouchedReposToPrune, repo.HostPath)
return
}
if len(strings.Split(strings.TrimSpace(branches), "\n")) > 1 {
return
}
status, err := git.ShortStatus(repo)
if err != nil {
colorlog.PrintError(fmt.Sprintf("Failed to get short status for repository %s: %v", repo.Name, err))
return
}
if status != "" {
return
}
// Check for new commits on the branch that exist locally but not on the remote
commits, err := git.RevListCompare(repo, "HEAD", "@{u}")
if err != nil {
colorlog.PrintError(fmt.Sprintf("Failed to get commit differences for repository %s. The repository may be empty or does not have a .git directory. Error: %v", repo.Name, err))
return
}
if commits != "" {
return
}
untouchedReposToPrune = append(untouchedReposToPrune, repo.HostPath)
}
// Don't clone any new repos when prune untouched is active
if os.Getenv("GHORG_PRUNE_UNTOUCHED") == "true" {
return
}
action := "cloning"
if repoWillBePulled {
// prevents git from asking for user for credentials, needs to be unset so creds aren't stored
err := git.SetOriginWithCredentials(repo)
@@ -918,9 +980,30 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
}
limit.WaitAndClose()
var untouchedPrunes int
if os.Getenv("GHORG_PRUNE_UNTOUCHED") == "true" && len(untouchedReposToPrune) > 0 {
if os.Getenv("GHORG_PRUNE_UNTOUCHED_NO_CONFIRM") != "true" {
colorlog.PrintSuccess(fmt.Sprintf("PLEASE CONFIRM: The following %d untouched repositories will be deleted. Press enter to confirm: ", len(untouchedReposToPrune)))
for _, repoPath := range untouchedReposToPrune {
colorlog.PrintInfo(fmt.Sprintf("- %s", repoPath))
}
fmt.Scanln()
}
for _, repoPath := range untouchedReposToPrune {
err := os.RemoveAll(repoPath)
if err != nil {
colorlog.PrintError(fmt.Sprintf("Failed to prune repository at %s: %v", repoPath, err))
} else {
untouchedPrunes++
colorlog.PrintSuccess(fmt.Sprintf("Successfully deleted %s", repoPath))
}
}
}
printRemainingMessages()
printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits)
printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits, untouchedPrunes)
if hasCollisions {
fmt.Println("")
@@ -1230,7 +1313,7 @@ func pruneRepos(cloneTargets []scm.Repo) int {
return count
}
func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits int) {
func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits, untouchedPrunes int) {
if updateRemoteCount > 0 {
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v, remotes updated: %v", cloneCount, pulledCount, newCommits, updateRemoteCount))
return
@@ -1241,6 +1324,11 @@ func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommi
return
}
if untouchedPrunes > 0 {
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total prunes: %v", cloneCount, pulledCount, untouchedPrunes))
return
}
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v", cloneCount, pulledCount))
}

View File

@@ -105,6 +105,18 @@ func (g MockGitClient) RepoCommitCount(repo scm.Repo) (int, error) {
return 0, nil
}
func (g MockGitClient) Branch(repo scm.Repo) (string, error) {
return "", nil
}
func (g MockGitClient) RevListCompare(repo scm.Repo, ref1 string, ref2 string) (string, error) {
return "", nil
}
func (g MockGitClient) ShortStatus(repo scm.Repo) (string, error) {
return "", nil
}
func TestInitialClone(t *testing.T) {
defer UnsetEnv("GHORG_")()
dir, err := os.MkdirTemp("", "ghorg_test_initial")

View File

@@ -70,6 +70,8 @@ var (
noDirSize bool
ghorgStatsEnabled bool
ghorgPreserveScmHostname bool
ghorgPruneUntouched bool
ghorgPruneUntouchedNoConfirm bool
args []string
cloneErrors []string
cloneInfos []string
@@ -173,6 +175,10 @@ func getOrSetDefaults(envVar string) {
os.Setenv(envVar, "false")
case "GHORG_PRUNE_NO_CONFIRM":
os.Setenv(envVar, "false")
case "GHORG_PRUNE_UNTOUCHED":
os.Setenv(envVar, "false")
case "GHORG_PRUNE_UNTOUCHED_NO_CONFIRM":
os.Setenv(envVar, "false")
case "GHORG_INSECURE_GITLAB_CLIENT":
os.Setenv(envVar, "false")
case "GHORG_INSECURE_GITEA_CLIENT":
@@ -275,6 +281,8 @@ func InitConfig() {
getOrSetDefaults("GHORG_FETCH_ALL")
getOrSetDefaults("GHORG_PRUNE")
getOrSetDefaults("GHORG_PRUNE_NO_CONFIRM")
getOrSetDefaults("GHORG_PRUNE_UNTOUCHED")
getOrSetDefaults("GHORG_PRUNE_UNTOUCHED_NO_CONFIRM")
getOrSetDefaults("GHORG_DRY_RUN")
getOrSetDefaults("GHORG_GITHUB_USER_OPTION")
getOrSetDefaults("GHORG_CLONE_WIKI")
@@ -364,6 +372,8 @@ func init() {
cloneCmd.Flags().BoolVar(&includeSubmodules, "include-submodules", false, "GHORG_INCLUDE_SUBMODULES - Include submodules in all clone and pull operations.")
cloneCmd.Flags().BoolVar(&ghorgStatsEnabled, "stats-enabled", false, "GHORG_STATS_ENABLED - Creates a CSV in the GHORG_ABSOLUTE_PATH_TO_CLONE_TO called _ghorg_stats.csv with info about each clone. This allows you to track clone data over time such as number of commits and size in megabytes of the clone directory.")
cloneCmd.Flags().BoolVar(&ghorgPreserveScmHostname, "preserve-scm-hostname", false, "GHORG_PRESERVE_SCM_HOSTNAME - Appends the scm hostname to the GHORG_ABSOLUTE_PATH_TO_CLONE_TO which will organize your clones into specific folders by the scm provider. e.g. /github.com/kuberentes")
cloneCmd.Flags().BoolVar(&ghorgPruneUntouched, "prune-untouched", false, "GHORG_PRUNE_UNTOUCHED - Prune repositories that don't have any local changes, see sample-conf.yaml for more details")
cloneCmd.Flags().BoolVar(&ghorgPruneUntouchedNoConfirm, "prune-untouched-no-confirm", false, "GHORG_PRUNE_UNTOUCHED_NO_CONFIRM - Automatically delete repos without showing an interactive confirmation prompt.")
cloneCmd.Flags().StringVarP(&baseURL, "base-url", "", "", "GHORG_SCM_BASE_URL - Change SCM base url, for on self hosted instances (currently gitlab, gitea and github (use format of https://git.mydomain.com/api/v3))")
cloneCmd.Flags().StringVarP(&concurrency, "concurrency", "", "", "GHORG_CONCURRENCY - Max goroutines to spin up while cloning (default 25)")
cloneCmd.Flags().StringVarP(&cloneDepth, "clone-depth", "", "", "GHORG_CLONE_DEPTH - Create a shallow clone with a history truncated to the specified number of commits")

View File

@@ -19,6 +19,9 @@ type Gitter interface {
SetOriginWithCredentials(scm.Repo) error
Clean(scm.Repo) error
Checkout(scm.Repo) error
RevListCompare(scm.Repo, string, string) (string, error)
ShortStatus(scm.Repo) (string, error)
Branch(scm.Repo) (string, error)
UpdateRemote(scm.Repo) error
FetchAll(scm.Repo) error
FetchCloneBranch(scm.Repo) error
@@ -182,6 +185,35 @@ func (g GitClient) FetchAll(repo scm.Repo) error {
return cmd.Run()
}
func (g GitClient) Branch(repo scm.Repo) (string, error) {
args := []string{"branch"}
cmd := exec.Command("git", args...)
cmd.Dir = repo.HostPath
if os.Getenv("GHORG_DEBUG") != "" {
if err := printDebugCmd(cmd, repo); err != nil {
return "", err
}
}
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
// RevListCompare returns the list of commits in the local branch that are not in the remote branch.
func (g GitClient) RevListCompare(repo scm.Repo, localBranch string, remoteBranch string) (string, error) {
cmd := exec.Command("git", "-C", repo.HostPath, "rev-list", localBranch, "^"+remoteBranch)
output, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
func (g GitClient) FetchCloneBranch(repo scm.Repo) error {
args := []string{"fetch", "origin", repo.CloneBranch}
@@ -198,6 +230,25 @@ func (g GitClient) FetchCloneBranch(repo scm.Repo) error {
return cmd.Run()
}
func (g GitClient) ShortStatus(repo scm.Repo) (string, error) {
args := []string{"status", "--short"}
cmd := exec.Command("git", args...)
cmd.Dir = repo.HostPath
if os.Getenv("GHORG_DEBUG") != "" {
if err := printDebugCmd(cmd, repo); err != nil {
return "", err
}
}
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
func (g GitClient) RepoCommitCount(repo scm.Repo) (int, error) {
args := []string{"rev-list", "--count", repo.CloneBranch}
cmd := exec.Command("git", args...)

View File

@@ -62,6 +62,18 @@ GHORG_PRUNE: false
# flag (--prune-no-confirm)
GHORG_PRUNE_NO_CONFIRM: false
# Prune repositories that are considered untouched. A repository is considered untouched if:
# 1. There are no new branches, ghorg only clones one branch so if there are more then the user has done something in the repo
# 2. If there are no branches locally, this means the repo is empty or all commits have been removed
# 3. If there are any commits on the default branch locally that are not on the remote
# 4. There are any modified changes locally
# flag (--prune-untouched)
GHORG_PRUNE_UNTOUCHED: false
# Automatically delete repos without showing an interactive confirmation prompt.
# flag (--prune-untouched-no-confirm)
GHORG_PRUNE_UNTOUCHED_NO_CONFIRM: false
# Color output (enabled, disabled)
# flag( --color) eg: --color=enabled eg: --color=disabled
GHORG_COLOR: disabled

View File

@@ -36,6 +36,17 @@ else
exit 1
fi
# clone an org preserving scm hostname
ghorg clone $GITHUB_ORG --token=$GITHUB_TOKEN --preserve-scm-hostname --prune-untouched --prune-untouched-no-confirm
if [ -z "$(ls -A $HOME/ghorg/github.com/$GITHUB_ORG)" ]
then
echo "Pass: github org clone preserving scm hostname prune untouched"
else
echo "Fail: github org clone preserving scm hostnamey prune untouched"
exit 1
fi
# clone an org with no config file to a specific path
ghorg clone $GITHUB_ORG --token=$GITHUB_TOKEN --path=/tmp --output-dir=testing_output_dir

View File

@@ -368,83 +368,83 @@ echo "CLONE AND TEST ALL-GROUPS, OUTPUT DIR, WIKI"
exit 1
fi
# ########### CLONE AND TEST ALL-GROUPS, OUTPUT DIR, WIKI, SNIPPETS ############
# ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --clone-wiki --clone-snippets --output-dir=local-gitlab-v15-repos-flat-wiki-snippets
# ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --clone-wiki --clone-snippets --output-dir=local-gitlab-v15-repos-flat-wiki-snippets
########### CLONE AND TEST ALL-GROUPS, OUTPUT DIR, WIKI, SNIPPETS ############
ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --clone-wiki --clone-snippets --output-dir=local-gitlab-v15-repos-flat-wiki-snippets
ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --clone-wiki --clone-snippets --output-dir=local-gitlab-v15-repos-flat-wiki-snippets
# GOT=$( ghorg ls local-gitlab-v15-repos-flat-wiki-snippets | grep -o 'local-gitlab-v15-repos-flat-wiki-snippets.*')
# WANT=$(cat <<EOF
# local-gitlab-v15-repos-flat-wiki-snippets/_ghorg_root_level_snippets
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz0
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz0.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz1
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz1.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz2
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz2.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz3
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz3.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz0
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz0.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz0.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz1
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz1.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz1.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz2
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz2.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz2.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz3
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz3.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz3.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_0
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_0.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_0.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_1
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_1.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_1.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_2
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_2.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_2.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_3
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_3.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_3.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_0
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_0.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_0.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_1
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_1.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_1.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_2
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_2.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_2.wiki
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_3
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_3.snippets
# local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_3.wiki
# EOF
# )
GOT=$( ghorg ls local-gitlab-v15-repos-flat-wiki-snippets | grep -o 'local-gitlab-v15-repos-flat-wiki-snippets.*')
WANT=$(cat <<EOF
local-gitlab-v15-repos-flat-wiki-snippets/_ghorg_root_level_snippets
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz0
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz0.wiki
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz1
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz1.wiki
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz2
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz2.wiki
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz3
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group1_baz3.wiki
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz0
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz0.snippets
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz0.wiki
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz1
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz1.snippets
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz1.wiki
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz2
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz2.snippets
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz2.wiki
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz3
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz3.snippets
local-gitlab-v15-repos-flat-wiki-snippets/local-gitlab-group2_baz3.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_0
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_0.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_0.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_1
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_1.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_1.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_2
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_2.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_2.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_3
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_3.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_a_repo_3.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_0
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_0.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_0.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_1
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_1.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_1.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_2
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_2.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_2.wiki
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_3
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_3.snippets
local-gitlab-v15-repos-flat-wiki-snippets/subgroup_b_repo_3.wiki
EOF
)
# if [ "${WANT}" != "${GOT}" ]
# then
# echo "CLONE AND TEST ALL-GROUPS, OUTPUT DIR, WIKI, AND SNIPPETS"
# exit 1
# fi
if [ "${WANT}" != "${GOT}" ]
then
echo "CLONE AND TEST ALL-GROUPS, OUTPUT DIR, WIKI, AND SNIPPETS"
exit 1
fi
# ############ CLONE AND TEST ALL-GROUPS, OUTPUT DIR, SNIPPETS, ROOT LEVEL ############
# ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="$TOKEN" --preserve-dir --clone-snippets --output-dir=local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups
# ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="$TOKEN" --preserve-dir --clone-snippets --output-dir=local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups
############ CLONE AND TEST ALL-GROUPS, OUTPUT DIR, SNIPPETS, ROOT LEVEL ############
ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="$TOKEN" --preserve-dir --clone-snippets --output-dir=local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups
ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="$TOKEN" --preserve-dir --clone-snippets --output-dir=local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups
# # Test root level snippets
# GOT=$( ghorg ls local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups/_ghorg_root_level_snippets | grep -o 'local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups.*')
# WANT=$(cat <<EOF
# local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups/_ghorg_root_level_snippets/snippet1-1
# local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups/_ghorg_root_level_snippets/snippet2-2
# EOF
# )
# Test root level snippets
GOT=$( ghorg ls local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups/_ghorg_root_level_snippets | grep -o 'local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups.*')
WANT=$(cat <<EOF
local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups/_ghorg_root_level_snippets/snippet1-2
local-gitlab-v15-snippets-preserve-dir-output-dir-all-groups/_ghorg_root_level_snippets/snippet2-3
EOF
)
# if [ "${WANT}" != "${GOT}" ]
# then
# echo "CLONE AND TEST ALL-GROUPS, OUTPUT DIR, SNIPPETS, ROOT LEVEL FAILED"
# exit 1
# fi
if [ "${WANT}" != "${GOT}" ]
then
echo "CLONE AND TEST ALL-GROUPS, OUTPUT DIR, SNIPPETS, ROOT LEVEL FAILED"
exit 1
fi
############ CLONE ALL-GROUPS, BACKUP, CLONE WIKI, OUTPUT DIR ############
ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --backup --clone-wiki --output-dir=local-gitlab-v15-backup
@@ -458,31 +458,20 @@ ghorg clone all-groups --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}
# # # # ## # # # # # # # # # # #
##### ### # # ##### ####### ####### ##### ##### ####### # #
############ CLONE SINGLE USER, OUTPUT DIR, SNIPPETS ############
ghorg clone root --clone-type=user --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --clone-snippets --output-dir=local-gitlab-v15-root-user-repos-snippets --prune-untouched --prune-untouched-no-confirm
############ CLONE SINGLE USER, OUTPUT DIR, PRUNE, PRUNE NO CONFIRM ############
ghorg clone root --clone-type=user --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --output-dir=local-gitlab-v15-root-user-repos --prune --prune-no-confirm
ghorg clone root --clone-type=user --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --output-dir=local-gitlab-v15-root-user-repos --prune --prune-no-confirm
# Test root level snippets
GOT=$( ghorg ls local-gitlab-v15-root-user-repos-snippets | grep -o 'local-gitlab-v15-root-user-repos-snippets.*')
WANT=$(cat <<EOF
EOF
)
# ############ CLONE SINGLE USER, OUTPUT DIR, SNIPPETS ############
# ghorg clone root --clone-type=user --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --clone-snippets --output-dir=local-gitlab-v15-root-user-repos-snippets
# ghorg clone root --clone-type=user --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --clone-snippets --output-dir=local-gitlab-v15-root-user-repos-snippets
# # Test root level snippets
# GOT=$( ghorg ls local-gitlab-v15-root-user-repos-snippets | grep -o 'local-gitlab-v15-root-user-repos-snippets.*')
# WANT=$(cat <<EOF
# local-gitlab-v15-root-user-repos-snippets/rootrepos0
# local-gitlab-v15-root-user-repos-snippets/rootrepos1
# local-gitlab-v15-root-user-repos-snippets/rootrepos1.snippets
# local-gitlab-v15-root-user-repos-snippets/rootrepos2
# local-gitlab-v15-root-user-repos-snippets/rootrepos3
# EOF
# )
# if [ "${WANT}" != "${GOT}" ]
# then
# echo "CLONE AND TEST ALL-GROUPS, OUTPUT DIR, SNIPPETS, ROOT LEVEL FAILED"
# exit 1
# fi
if [ "${WANT}" != "${GOT}" ]
then
echo "CLONE SINGLE USER, OUTPUT DIR, SNIPPETS, PRUNE UNTOUCHED, ROOT LEVEL FAILED"
exit 1
fi
####### ####### ###### # ####### # # ####### # ##### ###### ####### # # ######
# # # # # # # # # # # # # # # # # # # # #
@@ -524,6 +513,20 @@ echo "CLONE AND TEST TOP LEVEL GROUP FAILED"
exit 1
fi
############ CLONE AND TEST TOP LEVEL GROUP PRUNE UNTOUCHED ############
ghorg clone local-gitlab-group3 --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --output-dir=local-gitlab-v15-top-level-group --prune-untouched --prune-untouched-no-confirm
GOT=$(ghorg ls local-gitlab-v15-top-level-group | grep -o 'local-gitlab-v15-top-level-group.*')
WANT=$(cat <<EOF
EOF
)
if [ "${WANT}" != "${GOT}" ]
then
echo "CLONE AND TEST TOP LEVEL GROUP PRUNE UNTOUCHED FAILED"
exit 1
fi
############ CLONE AND TEST TOP LEVEL GROUP, PRESERVE SCM HOSTNAME ############
ghorg clone local-gitlab-group3 --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --output-dir=local-gitlab-v15-top-level-group --preserve-scm-hostname
ghorg clone local-gitlab-group3 --scm=gitlab --base-url="${GITLAB_URL}" --token="${TOKEN}" --output-dir=local-gitlab-v15-top-level-group --preserve-scm-hostname
@@ -867,13 +870,13 @@ exit 1
fi
############ CLONE AND TEST ALL-USERS, OUTPUT DIR, SNIPPETS ############
ghorg clone all-users --scm=gitlab --clone-type=user --base-url="${GITLAB_URL}" --token="${TOKEN}" --output-dir=local-gitlab-v15-all-users-snippets --clone-snippets
TEST_ALL_USERS_SNIPPETS_GOT=$(ghorg ls local-gitlab-v15-all-users-snippets | grep -o 'local-gitlab-v15-all-users-snippets.*')
TEST_ALL_USERS_SNIPPETS_WANT=$(cat <<EOF
local-gitlab-v15-all-users-snippets/_ghorg_root_level_snippets
local-gitlab-v15-all-users-snippets/rootrepos0
local-gitlab-v15-all-users-snippets/rootrepos1
local-gitlab-v15-all-users-snippets/rootrepos1.snippets
local-gitlab-v15-all-users-snippets/rootrepos2
local-gitlab-v15-all-users-snippets/rootrepos3
local-gitlab-v15-all-users-snippets/testuser1-repo
@@ -895,6 +898,7 @@ TEST_ALL_USERS_SNIPPETS_WANT=$(cat <<EOF
gitlab.example.com/local-gitlab-v15-all-users-snippets/_ghorg_root_level_snippets
gitlab.example.com/local-gitlab-v15-all-users-snippets/rootrepos0
gitlab.example.com/local-gitlab-v15-all-users-snippets/rootrepos1
gitlab.example.com/local-gitlab-v15-all-users-snippets/rootrepos1.snippets
gitlab.example.com/local-gitlab-v15-all-users-snippets/rootrepos2
gitlab.example.com/local-gitlab-v15-all-users-snippets/rootrepos3
gitlab.example.com/local-gitlab-v15-all-users-snippets/testuser1-repo

View File

@@ -130,6 +130,16 @@ do
curl --header "PRIVATE-TOKEN: $TOKEN" -X POST "${GITLAB_URL}/api/v4/projects?name=rootrepos${a}&initialize_with_readme=true"
done
sleep 1
SNIPPET_DATA='{"title": "my-first-snippet", "file_name": "snippet.txt", "content": "This is my first snippet", "visibility": "public"}'
curl --request POST --header "PRIVATE-TOKEN: $TOKEN" \
--header "Content-Type: application/json" \
--data "${SNIPPET_DATA}" \
"${GITLAB_URL}/api/v4/projects/root%2Frootrepos1/snippets"
echo -e "\n\n\n"
echo ""
echo ""
echo ""