From 5833c53cab1df0c51939369dab11e00c66cfb336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arild=20H=C3=A5kon=20Olsen?= Date: Thu, 13 Jun 2024 15:10:42 +0200 Subject: [PATCH] ## Version 1.0.15 - Improved upgrade-cli - Refactored include.sh into smud-main.sh and include.sh --- CHANGELOG.md | 4 + smud-cli/download-and-install-cli.sh | 2 +- smud-cli/functions-init.sh | 125 ++++---- smud-cli/functions-list.sh | 34 ++- smud-cli/functions-upgrade.sh | 18 +- smud-cli/include.sh | 410 +++++++++------------------ smud-cli/install-cli.sh | 17 ++ smud-cli/smud-main.sh | 228 +++++++++++++++ smud-cli/smud.sh | 7 +- 9 files changed, 485 insertions(+), 360 deletions(-) create mode 100644 smud-cli/smud-main.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 36b1c04..baf38aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Version 1.0.15 +- Improved upgrade-cli +- Refactored include.sh into smud-main.sh and include.sh + ## Version 1.0.14 - Improved argument handling - Improved array iteration diff --git a/smud-cli/download-and-install-cli.sh b/smud-cli/download-and-install-cli.sh index dbdf2d6..16aa782 100644 --- a/smud-cli/download-and-install-cli.sh +++ b/smud-cli/download-and-install-cli.sh @@ -109,7 +109,7 @@ if [ -d $download_folder ]; then fi rm -rf $download_folder - . $destination_folder/install-cli.sh + . $destination_folder/install-cli.sh "$@" fi IFS=$old_SEP \ No newline at end of file diff --git a/smud-cli/functions-init.sh b/smud-cli/functions-init.sh index fd4ed44..c9b72ef 100644 --- a/smud-cli/functions-init.sh +++ b/smud-cli/functions-init.sh @@ -36,20 +36,17 @@ set_upstream() i=0 if [ "$new_upstream" ]; then - remove_upstream_command="git remote rm upstream" - run_command remove-upstream --command-from-var=remove_upstream_command --debug-title='Removing upstream config URL' + run_command --command="git remote rm upstream" --force-debug-title='Removing upstream config URL' if [ "$new_upstream" = "-" ]; then println_not_silent "Upstream is removed" $gray exit else - add_upstream_command="git remote add upstream $new_upstream" - run_command add_upstream_command --command-from-var=add_upstream_command --debug-title='Adding upstream with user specified URL' + run_command --command="git remote add upstream $new_upstream" --force-debug-title='Adding upstream with user specified URL' println_not_silent "Upstream configured with '$new_upstream' " $gray fi elif [ ! "$new_upstream" ]; then new_upstream="$default_upstream" - add_upstream_command="git remote add upstream $new_upstream" - run_command add_upstream_command --command-from-var=add_upstream_command --debug-title='Adding upstream with default URL' + run_command --command="git remote add upstream $new_upstream" --force-debug-title='Adding upstream with default URL' println_not_silent "Upstream configured with '$new_upstream' " $gray elif [ ! "$caller" ]; then println_not_silent "Upstream alredy configured with '$new_upstream' " $gray @@ -64,33 +61,40 @@ set_origin() fi exit_if_is_not_a_git_repository "Setting remote.origin.url require a git repository!" - + # Check if origin exists - check_origin_command="git config --get remote.origin.url" - run_command --check-origin --command-from-var=check_origin_command --return-var=remote_origin --debug-title='Checking if remote.origin.url exist in git config' + git_config_command="git config --get remote.origin.url" + run_command --command-in-var=git_config_command --return-var=remote_origin --debug-title='Checking if remote.origin.url exist in git config' # If string is empty, set the remote origin url if [ ! "$remote_origin" ]; then - ask remote_origin $yellow "Remote repository origin is not set, please enter URL for the remote origin.\nOrigin URL:" "true" + ask remote_origin $yellow "Remote repository origin is not set, please enter URL for the remote origin.\nOrigin URL:" "true" if [ "$remote_origin" ]; then - add_origin_command="git remote add origin $remote_origin" - run_command --set-origin --command-from-var=add_origin_command --return-var=dummy --debug-title='Adding remote origin' || return + println_not_silent "Setting remote origin '$remote_origin'" $gray + run_command --command="git remote add origin $remote_origin" --force-debug-title='Setting remote origin' fi + else + println_not_silent "Remote origin '$remote_origin' already set." $gray fi } merge_upstream() { - if [ "$skip_init_feature" ];then + if [ "$skip_init_feature" ] && [ ! "$merge" ];then return fi - if [ "$skip_auto_update" ]; then + if [ "$skip_auto_update" ] && [ ! "$merge" ]; then return fi - println_not_silent "Merging upstream into local branch..." $gray - merge_upstream_command="git merge upstream/main" - run_command merge-upstream --command-from-var=merge_upstream_command --return-var=dummy --debug-title='Merging upstream repository into local branch' || return + if [ "$merge" ] && [ "$merge" != "true" ]; then + git__setup_source_config "$merge" + fi + if [ "$source_branch" ]; then + msg="Merging upstream '$source_branch' into local branch '$current_branch' ..." + println_not_silent "Merging upstream '$source_branch' into local branch '$current_branch' ..." $gray + run_command --command="git merge $source_branch" --return-in-var=dev_null --debug-title="$msg" + fi } fetch_origin() @@ -107,26 +111,36 @@ fetch_origin() return fi - println_not_silent "Fetching origin..." $gray - fetch_origin_command="git fetch origin" - run_command fetch-origin --command-from-var=fetch_origin_command --return-var=dummy --debug-title='Fetching origin' || return + println_not_silent "Fetching origin '$remote_origin' into branch '$current_branch'..." $gray + run_command --command="git fetch origin" --return-in-var=dev_null --debug-title='Fetching origin' } +git_push() +{ + if [ ! "$remote_origin" ]; then + println_not_silent "Missing remote-origin. Pushing skipped..." $gray + return + fi + + println_not_silent "Push current branch '$current_branch' to origin-url '$remote_origin'..." $gray + run_command --command="git push origin $current_branch" --return-in-var=dev_null --debug-title='Push to remote' +} + init_repo() { - init_command="git init" if [ "$skip_init_feature" ];then return fi + if [ ! "$is_repo" ];then - run_command init-repo --command-from-var=init_command --return-var=dummy --debug-title='Initializing repository' || return + run_command --command="git init" --return-in-var=dev_null --debug-title='Initializing repository' || return is_repo="true" branches="$(git branch)" if [ ! -n "$branches" ]; then # "main" possibly not default branch name so create it - create_main_branch="git checkout -b main" - run_command checkout-main --command-from-var=create_main_branch --return-var=dummy --debug-title='Creating main branch' || return + git_checkout_command="git checkout -b main" + run_command --command-in-var git_checkout_command --return-in-var=dev_null --debug-title='Creating main branch' || return fi fi git__setup 'true' @@ -138,8 +152,7 @@ fetch_upstream() return fi - fetch_upstream_command="git fetch upstream" - run_command fetch-upstream --command-from-var=fetch_upstream_command --return-var=dummy --debug-title='Fetching upstream' || return + run_command --command="git fetch upstream" --return-in-var=dev_null --force-debug-title='Fetching upstream' } init_upstream_url() @@ -160,7 +173,6 @@ init_upstream_url() set_upstream "$upstream_url" print_debug "upstream_url: $upstream_url" fi - } # Initalizes repo, upstream and origin if not configured. Will always fetch upstream when called. @@ -184,6 +196,11 @@ init() printf "${yellow}default-branch:${normal} \n" printf " With Only ${green}$func${normal}, ${yellow}default-branch${normal} will be configured to '${bold}main${normal}' . \n" printf " With ${green}$func ${yellow}${bold}--default-branch ${normal}${normal}, ${bold}default-branch ''${normal} will be configured. \n" + printf "${yellow}merge upstream:${normal} \n" + printf " With ${green}$func ${yellow}${bold}--merge${normal} the '${bold}git merge upstream/main${normal}' command will be runned. \n" + printf " With ${green}$func ${yellow}${bold}--merge --push${normal} the ${gray}'git merge upstream/main; ${normal}${bold}git push${normal}' command will be runned. \n" + printf " With ${green}$func ${yellow}${bold}--merge ${normal}${normal} the '${bold}git merge upstream/${yellow}${bold}${normal}' command will be runned. \n" + printf " With ${green}$func ${yellow}${bold}--merge --push${normal}${normal} the '${gray}git merge upstream/${normal}; ${bold}git push${normal}' command will be runned. \n" printf "Show ${yellow}configs:${normal} \n" printf " ${green}$func${normal} ${yellow}--configs${normal} will list all repository config key/values. ${yellow}--show${normal} or ${yellow}--settings${normal} can be used as well. \n" printf " ${green}$func${normal} ${yellow}--show${normal} or ${yellow}--settings${normal} can be used as well. \n" @@ -204,11 +221,10 @@ init() set_upstream "-" return fi - if [ ! "$is_repo" ]; then local yes_no="yes" if [ ! "$silent" ]; then - ask yes_no $yellow "The current directory does not seem to be a git repository\nWould you like to initialize the repository and merge the remote upstream? (yes/no)" + ask yes_no $yellow "The current directory does not seem to be a git repository\nWould you like to initialize the repository and merge the remote upstream (Yes/No)?" "-" "yes" fi if [ ! "$yes_no" = "yes" ]; then println_not_silent "Aborting" $yellow @@ -220,53 +236,34 @@ init() fetch_upstream merge_upstream init_repo + if [ "$merge" ]; then + run_push=$force_push + fi else init_upstream_url fetch_upstream - fi - - if [ "$is_repo" ]; then - - if [ ! "$source_branch" ]; then - - if [ "$current_branch" ]; then - source_branch="$(git config --get source.$current_branch)" - fi - fi - - - if [ ! "$source_branch" ]; then - source_branch="upstream/$default_branch" - fi - - - old="" - if [ "$current_branch" ]; then - old="$(git config --get source.$current_branch)" - fi - - if [ ! "$old" = "$source_branch" ] || [ ! "$old" ] ; then - - if [ "$old" ] && [ "$current_branch" ]; then - dummy="$(git config --unset source.$current_branch)" - fi - - if [ "$current_branch" ] && [ "$source_branch" ]; then - dummy="$(git config --add source.$current_branch $source_branch)" - fi + if [ "$merge" ]; then + merge_upstream + run_push=$force_push fi fi + git__setup_source_config if [ ! "$remote_origin" ]; then - println_not_silent "Setting and fetching origin" $gray set_origin - fetch_origin + if [ ! "$run_push" ]; then + fetch_origin + fi println_not_silent "Initalization complete." $green fi + if [ "$run_push" ]; then + git_push + fi + if [ "$configs" ]; then - config_command="git config -l" - run_command config-list --command-var config_command --skip-error + git_config_command="git config -l" + run_command --command-in-var git_config_command --skip-error fi } diff --git a/smud-cli/functions-list.sh b/smud-cli/functions-list.sh index 3c104c9..628cb73 100644 --- a/smud-cli/functions-list.sh +++ b/smud-cli/functions-list.sh @@ -661,9 +661,9 @@ product_infos__print() # echo "{ stage: '$product_stage', product_name: '$product_name', latest_version: '$latest_version', commit: '$commit', product_info: '${product_infos[${stage_product_name}]}' }" if [ "$show_tags_in_list" ]; then if [ "$show_tags_in_list" = "reverse" ]; then - tags="$(get_tags "'$latest_version'" "'$current_version'")" + get_tags tags "'$latest_version'" "'$current_version'" else - tags="$(get_tags "'$current_version'" "'$latest_version'")" + get_tags tags "'$current_version'" "'$latest_version'" fi if [ "$major" ] && [ ! "$tags" = "MAJOR" ];then @@ -865,33 +865,43 @@ get_next_stage_version() get_tags() { + local -n get_tags_tags="$1" + + get_tags_tags="" replace_reg_exp="s/'//g" - cur_ver="$(echo "$1" | sed -e $replace_reg_exp)" - latest_ver="$(echo "$2" | sed -e $replace_reg_exp)" + cur_ver="$(echo "$2" | sed -e $replace_reg_exp)" + latest_ver="$(echo "$3" | sed -e $replace_reg_exp)" if [ ! "$cur_ver" ] && [ "$latest_ver" ]; then - echo "NEW" + get_tags_tags="NEW" return elif [ "$cur_ver" ] && [ "$latest_ver" ]; then # cur_ver_array=$(echo "$cur_ver" | tr "." "\n") # latest_ver_array=$(echo "$latest_ver" | tr "." "\n") cur_ver_major="$(echo "$cur_ver" | cut -d "." -f 1)" + # cur_ver_major="${cur_ver_major%%[[:cntrl:]]}" latest_ver_major="$(echo "$latest_ver" | cut -d "." -f 1)" - if [ ! "$cur_ver_major" = "$latest_ver_major" ];then - echo "MAJOR" + # latest_ver_major="${latest_ver_major%%[[:cntrl:]]}" + if [ "$cur_ver_major" != "$latest_ver_major" ];then + get_tags_tags="MAJOR" return fi + cur_ver_minor="$(echo "$cur_ver" | cut -d "." -f 2)" + # cur_ver_minor="${cur_ver_minor%%[[:cntrl:]]}" latest_ver_minor="$(echo "$latest_ver" | cut -d "." -f 2)" - if [ ! "$cur_ver_minor" = "$latest_ver_minor" ];then - echo "MINOR" + # latest_ver_minor="${latest_ver_minor%%[[:cntrl:]]}" + if [ "$cur_ver_minor" != "$latest_ver_minor" ];then + get_tags_tags="MINOR" return fi cur_ver_patch="$(echo "$cur_ver" | cut -d "." -f 3)" latest_ver_patch="$(echo "$latest_ver" | cut -d "." -f 3)" - if [ ! "$cur_ver_patch" = "$latest_ver_patch" ];then - echo "patch" - return + if [ "$cur_ver_patch" != "$latest_ver_patch" ];then + if [ ! "${cur_ver_patch%%[[:cntrl:]]}" = "${latest_ver_patch%%[[:cntrl:]]}" ];then + get_tags_tags="patch" + return + fi fi fi } diff --git a/smud-cli/functions-upgrade.sh b/smud-cli/functions-upgrade.sh index 4064947..e250a64 100644 --- a/smud-cli/functions-upgrade.sh +++ b/smud-cli/functions-upgrade.sh @@ -93,7 +93,7 @@ upgrade() if [ ! "$product" ]; then if [ ! "$silent" ]; then echo "No Products specified by [--products=, --product=, -P=] or [--all, -A]." - ask yes_no $yellow "Do you want to upgrade the GitOps-model (Yes/No)?" + ask yes_no $yellow "Do you want to upgrade the GitOps-model (Yes/No)?" "-" "yes" fi if [ "$yes_no" = "yes" ]; then local upgrade_filter="$devops_model_filter" @@ -145,7 +145,7 @@ upgrade() error_code=0 yes_no="yes" if [ ! "$silent" ]; then - ask yes_no "$yellow" "Do you want to continue upgrading the selected $context (Yes/No)?" + ask yes_no "$yellow" "Do you want to continue upgrading the selected $context (Yes/No)?" "-" "y" fi local cherrypick__continue_options="--keep-redundant-commits --allow-empty" local cherrypick_options="$cherrypick__continue_options -x" @@ -270,7 +270,7 @@ upgrade() if [ "$silent" ]; then echo "Aborting the cherry-pick process." cherrypick_abort_command="git cherry-pick --abort" - run_command cherry-pick-abort --command-var=cherrypick_abort_command --return-var=dummy --skip-error --debug-title='Abort cherry-pick' + run_command cherry-pick-abort --command-var=cherrypick_abort_command --return-in-var=dev_null --skip-error --debug-title='Abort cherry-pick' if [ ! "$error_code" ] || [ "$error_code" = "0" ]; then error_code=1 fi @@ -285,7 +285,7 @@ upgrade() if [ "$continue_or_abort" = "a" ]; then error_code=0 cherrypick_abort_command="git cherry-pick --abort" - run_command cherry-pick-abort --command-var=cherrypick_abort_command --return-var=dummy --skip-error --debug-title='Abort cherry-pick' + run_command cherry-pick-abort --command-var=cherrypick_abort_command --return-in-var=dev_null --skip-error --debug-title='Abort cherry-pick' exit fi @@ -423,7 +423,7 @@ reset_to_commit() fi yes_no="yes" if [ ! "$silent" ]; then - ask yes_no $yellow "Do you really want to reset to [$has_undo_commit]?\nThis is a destructive command. All changes newer than that commit will be lost!" + ask yes_no $yellow "Do you really want to reset to [$has_undo_commit]?\nThis is a destructive command. All changes newer than that commit will be lost!\nReset (Yes/No)?" "-" "n" fi if [ "$yes_no" = "yes" ]; then local flag="--hard" @@ -517,10 +517,10 @@ ensure_git_cred_is_configured() fi fi - local dummy="$(git config --unset user.name)" - local dummy="$(git config --unset user.email)" - local dummy="$(git config --add user.name "$user_name_ask" )" - local dummy="$(git config --add user.email "$user_email_ask" )" + local dev_null="$(git config --unset user.name)" + local dev_null="$(git config --unset user.email)" + local dev_null="$(git config --add user.name "$user_name_ask" )" + local dev_null="$(git config --add user.email "$user_email_ask" )" fi } diff --git a/smud-cli/include.sh b/smud-cli/include.sh index 1e765f8..c9119ce 100644 --- a/smud-cli/include.sh +++ b/smud-cli/include.sh @@ -1,30 +1,33 @@ #!/usr/bin/env bash -white='\033[1;37m' -magenta='\x1b[1;m' -thin_gray='\x1b[22;30m' -red='\x1b[22;31m' -green='\x1b[22;32m' -yellow='\x1b[22;33m' -blue='\x1b[22;34m' -magenta_bold='\x1b[22;35m' -cyan='\x1b[22;36m' -gray='\x1b[1;90m' -magenta='\033[38;5;53m' -bold="$(tput bold)" - -IFS=$'\n' - -# bold="$white" -normal="$(tput sgr0)" -reset="$normal" - -p_0="$(echo _0| sed -e 's/_/$/g')" -p_1="$(echo _1| sed -e 's/_/$/g')" -p_2="$(echo _2| sed -e 's/_/$/g')" - - -declare -A ARGS +include_main() +{ + print_verbose "**** START: include.sh" + include_loaded="true" + white='\033[1;37m' + magenta='\x1b[1;m' + thin_gray='\x1b[22;30m' + red='\x1b[22;31m' + green='\x1b[22;32m' + yellow='\x1b[22;33m' + blue='\x1b[22;34m' + magenta_bold='\x1b[22;35m' + cyan='\x1b[22;36m' + gray='\x1b[1;90m' + magenta='\033[38;5;53m' + bold="$(tput bold)" + + IFS=$'\n' + + # bold="$white" + normal="$(tput sgr0)" + reset="$normal" + + p_0="$(echo _0| sed -e 's/_/$/g')" + p_1="$(echo _1| sed -e 's/_/$/g')" + p_2="$(echo _2| sed -e 's/_/$/g')" + print_verbose "**** END: include.sh" +} exit_if_is_not_a_git_repository() { @@ -149,7 +152,7 @@ parse_arguments() old_value=${parse_arguments_args["$key"]} if [ "$old_value" != "$value" ]; then if [ "$old_value" ] && [ "$value" != "true" ]; then - if [ "$value" ]; then + if [ ! "$argument_single_mode" ] && [ "$value" ]; then value="$old_value,$value" else value="$old_value" @@ -209,7 +212,7 @@ get_arg() key_value="${get_arg_args[$key]}" fi if [ "$key_value" ]; then - if [ "$value" ] && [ "$value" != "true" ];then + if [ ! "$argument_single_mode" ] && [ "$value" ] && [ "$value" != "true" ];then value="$value,$key_value" else value="$key_value" @@ -317,12 +320,26 @@ ask() local color="$2" local question="$3" local skip_newline="$4" - if [ ! "$skip_newline" ]; then + local default="$5" + if [ ! "$silent" ] && [ "$skip_newline" != "true" ]; then echo "" fi + if [ ! "$silent" ]; then + if [ "$default" ]; then + answer=$default + if [ "$1" = "yes_no" ]; then + if [ "$default" = "yes" ] || [ "$default" = "y" ] || [ "$default" = "ja" ] || [ "$default" = "j" ]; then + default="Yes" + elif [ "$answer" = "no" ] || [ "$answer" = "n" ] || [ "$answer" = "nei" ]; then + default="No" + fi + question=$(sed -e "s/No/No -- Push ENTER to use '$default'/g" <<< "$question") - printf "$color$question $normal" - read answer + fi + fi + printf "$color$question $normal" + read answer + fi lower answer if [ "$1" = "yes_no" ]; then if [ "$answer" = "yes" ] || [ "$answer" = "y" ] || [ "$answer" = "ja" ] || [ "$answer" = "j" ]; then @@ -331,7 +348,14 @@ ask() answer="no" fi fi - print_debug "You selected: $answer" + if [ ! "$answer" ] && [ "$default" ]; then + lower default + answer=$default + fi + + if [ ! "$silent" ]; then + print_debug "You selected: $answer" + fi } @@ -414,25 +438,39 @@ progressbar__end() run_command() { declare -A run_command_args + local command="" + local command_from_var="" + local return_in_var="" + local debug_title="" + local force_debug_title="" + local error_code="" + local skip_error="" + local skip_shell="" + local return_array="" + argument_single_mode="true" parse_arguments run_command_args "$@" get_arg command_from_var '--command-var,--command-from-var,--command-in-var' '' run_command_args false - get_arg debug_title '--debug-title,-dt' '' run_command_args false get_arg return_in_var '--return-in-var,--return-var,--return' '' run_command_args false + get_arg debug_title '--debug-title,-dt' '' run_command_args false get_arg force_debug_title '--force-debug-title,-dt' '' run_command_args false get_arg error_code '--error-code' '' run_command_args false - get_arg skip_error '--skip-error' '' run_command_args false + get_arg skip_error '--skip-error,--ignore-error' '' run_command_args false get_arg skip_shell '--skip-shell' '' run_command_args false get_arg return_array '--return-array,--array' '' run_command_args false + argument_single_mode="" if [ "$command_from_var" ];then local -n command="$command_from_var" else - get_arg command '--command,--command,-c' '' run_command_args false + command="" + get_arg command '--command,-c' '' run_command_args false fi if [ "$return_in_var" ];then local -n run_command_result="$return_in_var" + else + local run_command_result="" fi if [ "$error_code" ];then local -n run_command_error_code="$error_code" @@ -574,6 +612,50 @@ setup__product_filters() fi } +git__setup_source_config() +{ + local local_source_branch=$1 + + if [ "$is_repo" ]; then + if [ ! "$local_source_branch" ]; then + local_source_branch=$source_branch + fi + current_branch_escaped=$(sed -e 's/\//.f./g' -e 's/\\/.b./g' <<< "source.$current_branch" ) + + if [ ! "$local_source_branch" ]; then + if [ "$current_branch" ]; then + local_source_branch="$(git config --get $current_branch_escaped 2>/dev/null)" + fi + fi + + if [ ! "$local_source_branch" ]; then + local_source_branch="upstream/$default_branch" + fi + + old="" + if [ "$current_branch" ]; then + old="$(git config --get $current_branch_escaped 2>/dev/null)" + fi + + if [ ! "$old" = "$local_source_branch" ] || [ ! "$old" ] ; then + + if [ "$old" ] && [ "$current_branch" ]; then + local dev_null="$(git config --unset-all $current_branch_escaped 2>/dev/null)" + fi + + if [ "$current_branch" ] && [ "$local_source_branch" ]; then + if [ `grep '/' -c <<< "$local_source_branch"` -eq 0 ]; then + local_source_branch="upstream/$local_source_branch" + fi + config_source_command="git config --add $current_branch_escaped \"$local_source_branch\" 2>/dev/null" + run_command --command-from-var=config_source_command --force-debug-title="Set config source.$current_branch" --ignore-error || echo "error" + fi + fi + source_branch=$local_source_branch + fi + +} + git__setup() { from_init_repo_function="$1" @@ -592,16 +674,16 @@ git__setup() can_do_git="$(git branch --list $default_branch)" if [ "$default_branch" ]; then - dummy="$(git config --add default.branch $default_branch)" + local dev_null="$(git config --add default.branch $default_branch)" fi fi # print_debug "git__setup(): [ default_branch='$default_branch' ]" else old="$(git config --get default.branch)" if [ "$old" ]; then - dummy="$(git config --unset default.branch)" + local dev_null="$(git config --unset default.branch)" fi - dummy="$(git config --add default.branch $default_branch)" + local dev_null="$(git config --add default.branch $default_branch)" fi current_branch="$(git branch --show-current)" if [ ! "$current_branch" ]; then @@ -614,6 +696,7 @@ git__setup() # print_debug "git__setup(): [ default_branch='$default_branch', current_branch='$current_branch', upstream_url='$upstream_url' ]" + git__setup_source_config if [ ! "$from_commit" ] && [ ! "$from_date" ] && [ "$current_branch" ] ; then from_commit_command="git rev-list $current_branch -1 2>/dev/null" @@ -621,19 +704,17 @@ git__setup() run_command from-commit --command-var from_commit_command --return-var from_commit --skip-error --debug-title "from-commit-command" } || { if [ "$from_init_repo_function" != "true" ]; then - printf "${red}No commits found, run ${gray}smud init ${red} to fetch the upstream repository. -- $from_init_repo_function\n${normal}" + if [ "$command" = "init" ]; then + return + fi + + printf "${red}No commits found in branch '$current_branch', run ${gray}smud init ${red} to fetch the upstream repository. -- $command\n${normal}" exit fi } fi - if [ ! "$to_commit" ] && [ ! "$is_smud_dev_repo" ];then - if [ ! "$source_branch" ]; then - source_branch="$(git config --get source.$current_branch)" - fi - if [ ! "$source_branch" ]; then - source_branch="upstream/$default_branch" - fi + if [ ! "$to_commit" ] && [ ! "$is_smud_dev_repo" ] && [ "$source_branch" ];then if [ "$upstream_url" ] || [ ! "$source_branch" = "upstream/$default_branch" ]; then to_commit_command="git rev-list $source_branch -1 2>/dev/null" run_command to-commit --command-var to_commit_command --return-var to_commit --skip-error --debug-title "to-commit-command" @@ -656,232 +737,19 @@ git__setup() fi } -first_param="$3" -shift -parse_arguments ARGS $@ - -curr_dir="$(pwd)" -namespace_filter="-A" - -get_arg silent '--silent' -get_arg verbose '--verbose' -get_arg debug '--debug' "$verbose" -print_verbose "**** START: include.sh" -print_debug "Loading arguments...\n" -get_arg upstream_url '--upstream-url,--upstream,--up-url,-up-url' -get_arg source_branch '--source-branch,--source' -get_arg default_branch '--default-branch' -get_arg configs '--configs,--config,--settings,--setting,--show' -get_arg skip_auto_update '--skip-auto-update,--skip-auto' -get_arg examples '--examples,--ex,-ex' -get_arg help '--help,-?,-h' "$examples" -get_arg separator '--separator,-sep' -get_arg col_separator '--col-separtor,-colsep', ' ' -get_arg new '--new' -get_arg major '--major' -get_arg minor '--minor' -get_arg patch '--patch' -get_arg same '--same' -get_arg changed '--changed,--changes,--release,--released' -get_arg installed '--installed,-I' -get_arg hide_title '--hide-title' - -get_arg product '--products,--product,-P,--P' -get_arg all '--all,-A' -get_arg version '--version,-V' -get_arg from_commit '--from-commit,-FC' -get_arg to_commit '--to-commit,-TC' -get_arg from_date '--from-date,-FD' -get_arg to_date '--to-date,-TD' -get_arg grep '--grep' -get_arg undo '--undo,--reset' -get_arg soft '--soft' -get_arg undo_date '--date' -get_arg no_progress '--no-progress,--skip-progress' "$silent" -get_arg skip_push '--skip-push,--no-push' -get_arg skip_files '--skip-files,--no-files' -get_arg show_files '--show-files,--files' -get_arg responsible '--responsible,--team' -get_arg conflicts_files '--conflict-files,--files' -get_arg merge_ours '--merge-ours,--our,--ours' -get_arg merge_theirs '--merge-theirs,--their,--theirs' -get_arg merge_union '--merge-union,--union' -get_arg namespace '--namespace,-N,-n' -get_arg development '--development,-D,-DEV,--DEV' -get_arg external_test '--external-test,-ET,--ET' -get_arg internal_test '--internal-test,-IT,--IT' -get_arg production '--production,-PROD,--PROD' -get_arg stage '--stage,-S' '**' - -grep="$(echo "$grep"| sed -e 's/true//g')" - -if [ "$namespace" ]; then - namespace_filter="-n $namespace" -fi - -if [ "$to_commit" = "true" ]; then - to_commit="" -fi -if [ "$from_commit" = "true" ]; then - from_commit="" -fi - -if [ "$conflicts_files" ]; then - conflicts_files=$(echo "$conflicts_files"| awk --field-separator=, '{ print $1}'|uniq) -fi - -if [ "$responsible" ]; then - responsible=$(echo "$responsible" | sed -e "s/\./\\./g" -e 's/*/.*/g') -fi - -if [ "$skip_files" ]; then - show_files="" -fi - -remote_origin="" -if [ -d ".git" ]; then - is_repo="true" - is_smud_cli_repo="" - is_smud_gitops_repo="" - - cGitOps=$(expr match "$(pwd)" '.*/SMUD-GitOps$') - cSmudCli=$(expr match "$(pwd)" '.*/smud-cli$') - if [ $cGitOps -gt 0 ]; then - if [ "$(git config --get remote.origin.url|grep 'dev.azure.com/dips/DIPS/_git')" ]; then - is_smud_gitops_repo="SMUD-GitOps" - fi - elif [ $cSmudCli -gt 0 ]; then - is_smud_cli_repo="smud-cli" - fi - # echo "is_smud_gitops_repo: '$is_smud_gitops_repo'" - # echo "is_smud_cli_repo: '$is_smud_cli_repo'" -fi - -skip_init_feature="" -if [ "$is_smud_gitops_repo" ]; then - installed="true" -fi - -if [ "$is_smud_gitops_repo" ] || [ "$is_smud_cli_repo" ] || [ "$(pwd)" == "$HOME" ]; then - skip_init_feature="true" -fi - -is_smud_dev_repo="$is_smud_gitops_repo$is_smud_cli_repo" - -if [ "$is_smud_gitops_repo" ] && [ "$changed" ]; then - stage="" - development="" - internal_test='true' - external_test='true' - production='true' - show_changes_only='true' - show_files="" - skip_dependecies="true" -fi - -if [ "$development" ]; then - if [ "$stage" = "**" ]; then stage="";fi - stage="$stage development" -fi -if [ "$internal_test" ]; then - if [ "$stage" = "**" ]; then stage="";fi - stage="$stage internal-test" -fi - -if [ "$external_test" ]; then - if [ "$stage" = "**" ]; then stage="";fi - stage="$stage external-test" -fi - -if [ "$production" ]; then - if [ "$stage" = "**" ]; then stage="";fi - stage="$stage production" -fi - -stage="$(echo "$stage"|xargs|sed -e 's/ /,/g'|xargs)" -selected_stage="$stage" -if [ "$selected_stage" = "**" ]; then - selected_stage="" -fi -if [ "$product" = "true" ]; then - product="" - all="true" -fi - -selected_product="$product" -if [ "$selected_product" = "**" ]; then - selected_product="" -fi - -filter_product_name="[$product] " -if [ "$filter_product_name" = "[**] " ] || [ ! "$is_smud_gitops_repo" ]; then - filter_product_name="" -fi - -can_list_direct="" -if ([ ! "$is_smud_gitops_repo" ] || [ "$filter_product_name" ]) && [ ! "$new" ]; then - can_list_direct="1" -fi - -print_verbose "can_list_direct=$can_list_direct, is_smud_gitops_repo=$is_smud_gitops_repo, filter_product_name=$filter_product_name, new=$new" - -if [ "$grep" ]; then - git_grep="$(echo "$grep"| sed -e 's/ /./g'| sed -e 's/"//g'| sed -e "s/'//g" )" - git_grep="--grep $git_grep" -fi - -git_pretty_commit='--pretty=format:%H' -git_pretty_commit_date='--pretty=format:%H|%ad' -current_branch="$default_branch" -if [ "$has_args" ] && [ ! "$help" ] && [ "$is_repo" ]; then - git__setup -fi - -if [ "$all" ] && [ ! "$product" ]; then - product="**" -fi - -if [ "$installed" ] && [ ! "$product" ]; then - product="**" -fi - -setup__product_filters - -devops_model_filter="GETTING_STARTED.md CHANGELOG.md applicationsets-staged/* environments/* gitops-engine/* repositories/*" -diff_filter='' - -if [ "$debug" ];then - print_debug "filter: $filter" - if [ "$installed" ]; then - print_debug "app_files_filter: $app_files_filter" - fi - if [ "$can_do_git" ]; then - print_debug "Can do commit:" - if [ "$commit_range" ]; then - if [ "$from_commit" ]; then print_debug " from-commit: $from_commit"; fi - if [ "$to_commit" ]; then print_debug " to-commit: $to_commit"; fi - print_debug " commit range: $commit_range" - fi - if [ "$date_range" ]; then - if [ "$from_date" ]; then print_debug " from-date: $from_date"; fi - if [ "$to_date" ]; then print_debug " from-date: $to_date"; fi - print_debug "date range: $date_range" - fi - fi -fi -git_range="$(echo "$commit_range $date_range"|xargs)" -if [ "$git_range" ] && [ "$git_grep" ]; then - git_range="$git_range $git_grep" -fi +if [ ! "$include_loaded" ]; then + declare -A ARGS -if [ ! "$all" ]; then - if [ ! "$new$major$minor$patch$same$changed$product$version$responsible$stage" ]; then - all="true" - fi -fi + first_param="$3" + shift + parse_arguments ARGS $@ + curr_dir="$(pwd)" + namespace_filter="-A" -# has_any_commits="$(git log ..5e21036a024abd6eb8d1aaa9ffe9f6c14687821c --max-count=1 --no-merges $git_pretty_commit -- $filter)" -# echo "hit: $has_any_commits" -# exit -print_verbose "**** END: include.sh" + get_arg silent '--silent' + get_arg verbose '--verbose' + get_arg debug '--debug' "$verbose" + + include_main +fi \ No newline at end of file diff --git a/smud-cli/install-cli.sh b/smud-cli/install-cli.sh index bb4f083..89256aa 100644 --- a/smud-cli/install-cli.sh +++ b/smud-cli/install-cli.sh @@ -1,4 +1,21 @@ #!/usr/bin/env bash +if [ ! "$include_loaded" ]; then + dir_name=$destination_folder + if [ ! "$dir_name" ]; then + dir_name="$(dirname "$(readlink -f "$0")")" + if [ ! "$dir_name" ]; then + dir_name="." + fi + fi + if [ ! "$dir_name/include.sh" ]; then +print_verbose() +{ + echo $0 +} + else + . $dir_name/include.sh "$@" + fi +fi print_verbose "**** START: install-cli.sh" diff --git a/smud-cli/smud-main.sh b/smud-cli/smud-main.sh new file mode 100644 index 0000000..4e13640 --- /dev/null +++ b/smud-cli/smud-main.sh @@ -0,0 +1,228 @@ +#!/usr/bin/env bash +if [ ! "$smud_main_loaded" ]; then + + smud_main_loaded="true" + . $(dirname "$0")/include.sh "$@" + + print_verbose "**** START: smud-main.sh" + print_debug "Loading arguments...\n" + get_arg upstream_url '--upstream-url,--upstream,--up-url,-up-url' + get_arg source_branch '--source-branch,--source' + get_arg default_branch '--default-branch' + get_arg configs '--configs,--config,--settings,--setting,--show' + get_arg skip_auto_update '--skip-auto-update,--skip-auto' + get_arg examples '--examples,--ex,-ex' + get_arg help '--help,-?,-h' "$examples" + get_arg separator '--separator,-sep' + get_arg col_separator '--col-separtor,-colsep', ' ' + get_arg new '--new' + get_arg major '--major' + get_arg minor '--minor' + get_arg patch '--patch' + get_arg same '--same' + get_arg changed '--changed,--changes,--release,--released' + get_arg installed '--installed,-I' + get_arg hide_title '--hide-title' + + get_arg product '--products,--product,-P,--P' + get_arg all '--all,-A' + get_arg version '--version,-V' + get_arg from_commit '--from-commit,-FC' + get_arg to_commit '--to-commit,-TC' + get_arg from_date '--from-date,-FD' + get_arg to_date '--to-date,-TD' + get_arg grep '--grep' + get_arg undo '--undo,--reset' + get_arg soft '--soft' + get_arg undo_date '--date' + get_arg no_progress '--no-progress,--skip-progress' "$silent" + get_arg skip_push '--skip-push,--no-push' + get_arg force_push '--push,--force-push' + get_arg skip_files '--skip-files,--no-files' + get_arg show_files '--show-files,--files' + get_arg responsible '--responsible,--team' + get_arg conflicts_files '--conflict-files,--files' + get_arg merge_ours '--merge-ours,--our,--ours' + get_arg merge_theirs '--merge-theirs,--their,--theirs' + get_arg merge_union '--merge-union,--union' + get_arg merge '--merge' + get_arg namespace '--namespace,-N,-n' + get_arg development '--development,-D,-DEV,--DEV' + get_arg external_test '--external-test,-ET,--ET' + get_arg internal_test '--internal-test,-IT,--IT' + get_arg production '--production,-PROD,--PROD' + get_arg stage '--stage,-S' '**' + + grep="$(echo "$grep"| sed -e 's/true//g')" + + if [ "$namespace" ]; then + namespace_filter="-n $namespace" + fi + + if [ "$to_commit" = "true" ]; then + to_commit="" + fi + if [ "$from_commit" = "true" ]; then + from_commit="" + fi + + if [ "$conflicts_files" ]; then + conflicts_files=$(echo "$conflicts_files"| awk --field-separator=, '{ print $1}'|uniq) + fi + + if [ "$responsible" ]; then + responsible=$(echo "$responsible" | sed -e "s/\./\\./g" -e 's/*/.*/g') + fi + + if [ "$skip_files" ]; then + show_files="" + fi + + remote_origin="" + if [ -d ".git" ]; then + is_repo="true" + is_smud_cli_repo="" + is_smud_gitops_repo="" + + cGitOps=$(expr match "$(pwd)" '.*/SMUD-GitOps$') + cSmudCli=$(expr match "$(pwd)" '.*/smud-cli$') + if [ $cGitOps -gt 0 ]; then + if [ "$(git config --get remote.origin.url|grep 'dev.azure.com/dips/DIPS/_git')" ]; then + is_smud_gitops_repo="SMUD-GitOps" + fi + elif [ $cSmudCli -gt 0 ]; then + is_smud_cli_repo="smud-cli" + fi + # echo "is_smud_gitops_repo: '$is_smud_gitops_repo'" + # echo "is_smud_cli_repo: '$is_smud_cli_repo'" + fi + + skip_init_feature="" + if [ "$is_smud_gitops_repo" ]; then + installed="true" + fi + + if [ "$is_smud_gitops_repo" ] || [ "$is_smud_cli_repo" ] || [ "$(pwd)" == "$HOME" ]; then + skip_init_feature="true" + fi + + is_smud_dev_repo="$is_smud_gitops_repo$is_smud_cli_repo" + + if [ "$is_smud_gitops_repo" ] && [ "$changed" ]; then + stage="" + development="" + internal_test='true' + external_test='true' + production='true' + show_changes_only='true' + show_files="" + skip_dependecies="true" + fi + + if [ "$development" ]; then + if [ "$stage" = "**" ]; then stage="";fi + stage="$stage development" + fi + if [ "$internal_test" ]; then + if [ "$stage" = "**" ]; then stage="";fi + stage="$stage internal-test" + fi + + if [ "$external_test" ]; then + if [ "$stage" = "**" ]; then stage="";fi + stage="$stage external-test" + fi + + if [ "$production" ]; then + if [ "$stage" = "**" ]; then stage="";fi + stage="$stage production" + fi + + stage="$(echo "$stage"|xargs|sed -e 's/ /,/g'|xargs)" + selected_stage="$stage" + if [ "$selected_stage" = "**" ]; then + selected_stage="" + fi + if [ "$product" = "true" ]; then + product="" + all="true" + fi + + selected_product="$product" + if [ "$selected_product" = "**" ]; then + selected_product="" + fi + + filter_product_name="[$product] " + if [ "$filter_product_name" = "[**] " ] || [ ! "$is_smud_gitops_repo" ]; then + filter_product_name="" + fi + + can_list_direct="" + if ([ ! "$is_smud_gitops_repo" ] || [ "$filter_product_name" ]) && [ ! "$new" ]; then + can_list_direct="1" + fi + + print_verbose "can_list_direct=$can_list_direct, is_smud_gitops_repo=$is_smud_gitops_repo, filter_product_name=$filter_product_name, new=$new" + + if [ "$grep" ]; then + git_grep="$(echo "$grep"| sed -e 's/ /./g'| sed -e 's/"//g'| sed -e "s/'//g" )" + git_grep="--grep $git_grep" + fi + + git_pretty_commit='--pretty=format:%H' + git_pretty_commit_date='--pretty=format:%H|%ad' + current_branch="$default_branch" + if [ "$has_args" ] && [ ! "$help" ] && [ "$is_repo" ]; then + git__setup + fi + + if [ "$all" ] && [ ! "$product" ]; then + product="**" + fi + + if [ "$installed" ] && [ ! "$product" ]; then + product="**" + fi + + setup__product_filters + + devops_model_filter="GETTING_STARTED.md CHANGELOG.md applicationsets-staged/* environments/* gitops-engine/* repositories/*" + diff_filter='' + + if [ "$debug" ];then + print_debug "filter: $filter" + if [ "$installed" ]; then + print_debug "app_files_filter: $app_files_filter" + fi + if [ "$can_do_git" ]; then + print_debug "Can do commit:" + if [ "$commit_range" ]; then + if [ "$from_commit" ]; then print_debug " from-commit: $from_commit"; fi + if [ "$to_commit" ]; then print_debug " to-commit: $to_commit"; fi + print_debug " commit range: $commit_range" + fi + if [ "$date_range" ]; then + if [ "$from_date" ]; then print_debug " from-date: $from_date"; fi + if [ "$to_date" ]; then print_debug " from-date: $to_date"; fi + print_debug "date range: $date_range" + fi + fi + fi + git_range="$(echo "$commit_range $date_range"|xargs)" + if [ "$git_range" ] && [ "$git_grep" ]; then + git_range="$git_range $git_grep" + fi + + if [ ! "$all" ]; then + if [ ! "$new$major$minor$patch$same$changed$product$version$responsible$stage" ]; then + all="true" + fi + fi + + + # has_any_commits="$(git log ..5e21036a024abd6eb8d1aaa9ffe9f6c14687821c --max-count=1 --no-merges $git_pretty_commit -- $filter)" + # echo "hit: $has_any_commits" + # exit + print_verbose "**** END: smud-main.sh" +fi \ No newline at end of file diff --git a/smud-cli/smud.sh b/smud-cli/smud.sh index e66501c..fe4079b 100644 --- a/smud-cli/smud.sh +++ b/smud-cli/smud.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash -. $(dirname "$0")/include.sh "$@" +command="$1" + +. $(dirname "$0")/smud-main.sh "$@" print_verbose "**** START: smud.sh" -. $(dirname "$0")/install-cli.sh +. $(dirname "$0")/install-cli.sh "$@" . $(dirname "$0")/functions-init.sh . $(dirname "$0")/functions.sh . $(dirname "$0")/functions-conflicts.sh @@ -10,7 +12,6 @@ print_verbose "**** START: smud.sh" . $(dirname "$0")/functions-gitops.sh . $(dirname "$0")/functions-resources.sh -command="$1" print_verbose "\n${bold}command: $command\n" if [ ! "$command" ] ; then