From bbefbff9b8271ec58e5c40be89d7bcfba08019ce Mon Sep 17 00:00:00 2001 From: leleliu008 Date: Wed, 18 Sep 2024 18:36:42 +0800 Subject: [PATCH] optimized --- README.md | 12 ++-- ndk-pkg | 126 +++++++---------------------------------- ndk-pkg-zsh-completion | 16 +----- 3 files changed, 31 insertions(+), 123 deletions(-) diff --git a/README.md b/README.md index 3b8e322..60773d8 100644 --- a/README.md +++ b/README.md @@ -511,8 +511,12 @@ a typical hierarchical structure under `~/.ndk-pkg` directory looks like below: ```bash ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo - ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo master - ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo main + ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --branch=master + ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --branch=main + + ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --sync + ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --sync --branch=master + ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --sync --branch=main ``` - **delete a existing formula repository** @@ -956,9 +960,7 @@ If a ndk-pkg formula repository is `disabled`, which means ndk-pkg would not sea ## ndk-pkg formula repository management -run `ndk-pkg formula-repo-add ` command to create a new formula repository locally from an exsting remote git repository. - -run `ndk-pkg formula-repo-init` command to create a new formula repository locally without taking any further action. +run `ndk-pkg formula-repo-add ` command to create a new formula repository locally. ## ndk-pkg official formula repository diff --git a/ndk-pkg b/ndk-pkg index f69c68e..c6274a5 100755 --- a/ndk-pkg +++ b/ndk-pkg @@ -510,25 +510,20 @@ version_match() { ############################################################################## # {{{ ndk-pkg formula-repo-add -# __create_a_formula_repository_then_sync_it [--branch=VALUE --pin/--unpin --enable/--disable] - __create_a_formula_repository_then_sync_it() { +# __create_a_formula_repository [--branch=VALUE --pin/--unpin --enable/--disable --sync] + __create_a_formula_repository() { [ -z "$1" ] && abort 1 "please specify a repository name." [ -z "$2" ] && abort 1 "please specify a repository url." FORMULA_REPO_NAME="$1" FORMULA_REPO_URL="$2" - if [ -z "$NDKPKG_URL_TRANSFORM" ] ; then - GIT_FETCH_URL="$FORMULA_REPO_URL" - else - GIT_FETCH_URL="$("$NDKPKG_URL_TRANSFORM" "$FORMULA_REPO_URL")" - fi - shift 2 FORMULA_REPO_BRANCH= FORMULA_REPO_PINNED= FORMULA_REPO_ENABLED= + FORMULA_REPO_SYNC= while [ -n "$1" ] do @@ -548,6 +543,9 @@ version_match() { --disable) FORMULA_REPO_ENABLED=0 ;; + --sync) + FORMULA_REPO_SYNC=1 + ;; *) abort 1 "unrecognized argument: $1" esac shift @@ -571,90 +569,22 @@ version_match() { run install -d "$SESSION_DIR" run cd "$SESSION_DIR" - run git -c init.defaultBranch=master init - run git remote add origin "$GIT_FETCH_URL" - run git -c protocol.version=2 fetch --progress origin "+refs/heads/$FORMULA_REPO_BRANCH:refs/remotes/origin/$FORMULA_REPO_BRANCH" - run git checkout --progress --force -B "$FORMULA_REPO_BRANCH" "refs/remotes/origin/$FORMULA_REPO_BRANCH" - - cat > .ndk-pkg-formula-repo.yml < [--branch=VALUE --pin/--unpin --enable/--disable] - __create_a_formula_repository() { - [ -z "$1" ] && abort 1 "please specify a repository name." - [ -z "$2" ] && abort 1 "please specify a repository url." - - FORMULA_REPO_NAME="$1" + if [ "$FORMULA_REPO_SYNC" = 1 ] ; the + if [ -z "$NDKPKG_URL_TRANSFORM" ] ; then + GIT_FETCH_URL="$FORMULA_REPO_URL" + else + GIT_FETCH_URL="$("$NDKPKG_URL_TRANSFORM" "$FORMULA_REPO_URL")" + fi - if [ -z "$NDKPKG_URL_TRANSFORM" ] ; then - FORMULA_REPO_URL="$2" + run git -c init.defaultBranch=master init + run git remote add origin "$GIT_FETCH_URL" + run git -c protocol.version=2 fetch --progress origin "+refs/heads/$FORMULA_REPO_BRANCH:refs/remotes/origin/$FORMULA_REPO_BRANCH" + run git checkout --progress --force -B "$FORMULA_REPO_BRANCH" "refs/remotes/origin/$FORMULA_REPO_BRANCH" else - FORMULA_REPO_URL="$("$NDKPKG_URL_TRANSFORM" "$2")" - fi - - shift 2 - - FORMULA_REPO_BRANCH= - FORMULA_REPO_PINNED= - FORMULA_REPO_ENABLED= - - while [ -n "$1" ] - do - case $1 in - --branch=*) - FORMULA_REPO_BRANCH="${1#*=}" - ;; - --pin) - FORMULA_REPO_PINNED=1 - ;; - --unpin) - FORMULA_REPO_PINNED=0 - ;; - --enable) - FORMULA_REPO_ENABLED=1 - ;; - --disable) - FORMULA_REPO_ENABLED=0 - ;; - *) abort 1 "unrecognized argument: $1" - esac - shift - done - - FORMULA_REPO_BRANCH="${FORMULA_REPO_BRANCH:-master}" - FORMULA_REPO_PINNED="${FORMULA_REPO_PINNED:-1}" - FORMULA_REPO_ENABLED="${FORMULA_REPO_ENABLED:-1}" - - FORMULA_REPO_PATH="$NDKPKG_HOME/repos.d/$FORMULA_REPO_NAME" - - if [ -d "$FORMULA_REPO_PATH" ] ; then - abort 1 "formula repository '$FORMULA_REPO_NAME' already exists." + run git -c init.defaultBranch=master init + run git remote add origin "$FORMULA_REPO_URL" fi - printf '%b\n' "${COLOR_PURPLE}==> ndk-pkg formula repository${COLOR_OFF} ${COLOR_GREEN}$FORMULA_REPO_NAME${COLOR_OFF} ${COLOR_PURPLE} is being created${COLOR_OFF}" - - SESSION_DIR="$NDKPKG_HOME/run/$$/$FORMULA_REPO_NAME" - - run rm -rf "$SESSION_DIR" - run install -d "$SESSION_DIR" - run cd "$SESSION_DIR" - - run git -c init.defaultBranch=master init - run git remote add origin "$FORMULA_REPO_URL" - cat > .ndk-pkg-formula-repo.yml < [--branch=VALUE --pin/--unpin --enable/--disable]${COLOR_OFF} - create a new empty formula repository. - -${COLOR_GREEN}ndk-pkg formula-repo-add [--branch=VALUE --pin/--unpin --enable/--disable]${COLOR_OFF} - create a new empty formula repository then sync with server. +${COLOR_GREEN}ndk-pkg formula-repo-add [--branch=VALUE --pin/--unpin --enable/--disable --sync]${COLOR_OFF} + create a new empty formula repository. If --sync option is given, sync with the server after the formula repo is created. ${COLOR_GREEN}ndk-pkg formula-repo-del ${COLOR_OFF} delete the given formula repository. @@ -9506,15 +9433,6 @@ case $1 in formula-repo-info) shift; __info_the_given_formula_repository "$@" ;; formula-repo-conf) shift; __conf_the_given_formula_repository "$@" ;; formula-repo-sync) shift; __sync_the_given_formula_repository "$@" ;; - formula-repo-init) - shift - - case $1 in - official-*) abort 1 "ndk-pkg formula repository name that starts with 'official-' is reserved for ndk-pkg official formula repository, please use other name." - esac - - __create_a_formula_repository "$@" - ;; formula-repo-add) shift @@ -9522,7 +9440,7 @@ case $1 in official-*) abort 1 "ndk-pkg formula repository name that starts with 'official-' is reserved for ndk-pkg official formula repository, please use other name." esac - __create_a_formula_repository_then_sync_it "$@" + __create_a_formula_repository "$@" ;; formula-repo-del) shift diff --git a/ndk-pkg-zsh-completion b/ndk-pkg-zsh-completion index 408f015..727ea87 100644 --- a/ndk-pkg-zsh-completion +++ b/ndk-pkg-zsh-completion @@ -19,8 +19,7 @@ _ndk_pkg_actions=( 'search:search all available packages whose name matches the given regular expression pattern.' 'info-available:show information of the given available package.' 'info-installed:show information of the given installed package.' - 'formula-repo-init:create a new empty formula repository.' - 'formula-repo-add:create a new empty formula repository then sync with server.' + 'formula-repo-add:create a new empty formula repository.' 'formula-repo-del:delete the given formula repository from your local cache.' 'formula-repo-sync:sync the given formula repository.' 'formula-repo-conf:change the config of the given formula repository.' @@ -90,18 +89,7 @@ function _ndk_pkg() { '1:formula-repo-name:(name)' \ '2:formula-repo-url:(https://)' \ '--branch=-[specify the formula repo branch]' \ - + '(pin)' \ - '--pin[set pinned to 1]' \ - '--unpin[set pinned to 0]' \ - + '(enable)' \ - '--enable[set enabled to 1]' \ - '--disable[set enabled to 0]' - ;; - formula-repo-init) - _arguments \ - '1:formula-repo-name:(name)' \ - '2:formula-repo-url:(https://)' \ - '--branch=-[specify the formula repo branch]' \ + '--sync[sync with the server after the formula repo is created]' \ + '(pin)' \ '--pin[set pinned to 1]' \ '--unpin[set pinned to 0]' \