diff --git a/create_repo.sh b/create_repo.sh deleted file mode 100755 index c67f504..0000000 --- a/create_repo.sh +++ /dev/null @@ -1,101 +0,0 @@ -#!/bin/bash - -source env.sh - -if [ "$mainRepoURL" = "" ] || [ "$mainRepoPath" = "" ] || [ "$recipePath" = "" ]; then - echo "Invalid arguments. Exiting." - exit 1 -fi - -if ! command -v jq &> /dev/null -then - echo "jq could not be found" - exit 1 -fi - -if ! command -v yq &> /dev/null -then - echo "yq could not be found" - exit 1 -fi - -if ! command -v git &> /dev/null -then - echo "git could not be found" - exit 1 -fi - -[ ! -d "$mainRepoPath/resources" ] && git clone $mainRepoURL $mainRepoPath - -pushd $mainRepoPath - -mkdir -p resources - -for row in $(cat $recipePath | yq -r '.tasks[] | @base64'); do - json=$(echo $row | base64 -d) - action=$(echo $json | jq -r '.action') - skip=$(echo $json | jq -r '.skip') - if [ "$skip" = "true" ]; then - continue - fi - if [ "$action" = "waste_time" ]; then - echo "Wasting Time" - sleep $(echo $json | jq '.seconds') - elif [ "$action" = "download_github" ]; then - subpath=$(echo $json | jq -r '.subpath') - src=$(echo $json | jq -r '.src') - dest=$(echo $json | jq -r '.dest') - if [ "$subpath" != "null" ]; then - temp_path="./tmp/test/" - git clone $src $temp_path - mv "${temp_path}${subpath}" $dest - rm -rf $temp_path - continue - fi - ref=$(echo $json | jq -r '.ref') - - re="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+?)(\.git)?$" - if [[ $src =~ $re ]]; then - repo=${BASH_REMATCH[5]} - - dest=$(echo $dest | cut -c 3-) - - [ -d "$dest" ] && continue - - if [[ ${dest} != *"resources"* ]]; then - # A normal clone since its temporary - git clone -b $ref $src $dest - else - git remote add -f $repo $src - git subtree add --prefix $dest --squash $repo $ref -m "Add $repo as subtree" - git merge --squash --allow-unrelated-histories main - git push origin main - fi - fi - elif [ "$action" = "move_path" ]; then - src=$(echo $json | jq -r '.src') - dest=$(echo $json | jq -r '.dest') - mv $src $dest - elif [ "$action" = "download_file" ]; then - path=$(echo $json | jq -r '.path') - url=$(echo $json | jq -r '.url') - echo $path - echo $url - mkdir -p ${path%/*} - wget -O $path $url - elif [ "$action" = "unzip" ]; then - src=$(echo $json | jq -r '.src') - dest=$(echo $json | jq -r '.dest') - mkdir -p $dest - unzip $src -d $dest - elif [ "$action" = "remove_path" ]; then - path=$(echo $json | jq -r '.path') - echo "Removing path $path" - rm -rf $path - git add . - git commit -m "Non git stuff" - git push origin main - fi -done - -popd diff --git a/env.sh b/env.sh index 48302fb..61c100b 100644 --- a/env.sh +++ b/env.sh @@ -1,3 +1,4 @@ -export mainRepoURL="https://github.com/iLLeniumRoleplay/TestRepo" -export mainRepoPath="$HOME/go/github.com/iLLeniumRoleplay/TestRepo" -export recipePath="$(pwd)/recipes/qbcore.yaml" +export ORG_NAME="iLLeniumTest" +export REPO_NAME="Resources" +export REPO_PATH="$HOME/go/github.com" +export RECIPE_PATH="$(pwd)/recipes/qbcore.yaml" diff --git a/fivem-subtree.sh b/fivem-subtree.sh new file mode 100755 index 0000000..dcf1274 --- /dev/null +++ b/fivem-subtree.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +source env.sh +source functions.sh + +ensure_env +ensure_tools + +create_main_repo + +if [ "$1" = "create" ] || [ "$1" = "pull" ]; then + all_repos_in_recipe $1 +else + echo "Invalid action: $1" +fi + +popd diff --git a/functions.sh b/functions.sh new file mode 100644 index 0000000..907b28e --- /dev/null +++ b/functions.sh @@ -0,0 +1,188 @@ +function ensure_env() { + if [ "$ORG_NAME" = "" ] || [ "$REPO_NAME" = "" ] || [ "$REPO_PATH" = "" ] || [ "$RECIPE_PATH" = "" ]; then + echo "Invalid arguments. Exiting." + exit 1 + fi +} + +function ensure_tools() { + declare -a tools=("jq" "yq" "git" "gh") + + for i in "${tools[@]}" + do + if ! command -v $i &> /dev/null + then + echo "$i could not be found" + exit 1 + fi + done +} + +function create_main_repo() { + repoFull=$ORG_NAME/$REPO_NAME + gh repo view $repoFull &>/dev/null + if [ $? -eq 1 ]; then + # Repo doesn't exist. Create a new one + echo "Creating a new repo $repoFull" + gh repo create $repoFull --private + + mkdir -p $REPO_PATH/$repoFull + pushd $REPO_PATH/$repoFull &>/dev/null + echo "# Resources" >> README.md + git init &>/dev/null + git add README.md + git commit -m "first commit" &>/dev/null + git branch -M main + git remote add origin https://github.com/$repoFull.git + git push -u origin main &>/dev/null + else + echo "Repo already exists. Skipping" + pushd $REPO_PATH/$repoFull + fi + mkdir -p resources +} + +function all_repos_in_recipe() { + script_action=$1 + script_remote=$2 + for row in $(cat $RECIPE_PATH | yq -r '.tasks[] | @base64'); do + json=$(echo $row | base64 -d) + action=$(echo $json | jq -r '.action') + skip=$(echo $json | jq -r '.skip') + if [ "$skip" = "true" ]; then + continue + fi + if [ "$action" = "waste_time" ]; then + echo "Wasting Time" + sleep $(echo $json | jq '.seconds') + elif [ "$action" = "download_github" ]; then + subpath=$(echo $json | jq -r '.subpath') + src=$(echo $json | jq -r '.src') + dest=$(echo $json | jq -r '.dest') + ref=$(echo $json | jq -r '.ref') + if [ "$subpath" != "null" ]; then + if [ "$script_action" = "create" ]; then + temp_path="./tmp/test/" + git clone $src $temp_path + mv "${temp_path}${subpath}" $dest + rm -rf $temp_path + fi + continue + fi + + re="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+?)(\.git)?$" + if [[ $src =~ $re ]]; then + repo=${BASH_REMATCH[5]} + dest=$(echo $dest | cut -c 3-) + folder=$(basename $dest | sed 's/[][]//g') + + if [ "$script_action" = "create" ]; then + [ -d "$dest" ] && continue + + if [[ ${dest} != *"resources"* ]]; then + # A normal clone since its temporary + git clone -b $ref $src $dest + else + gh repo create $ORG_NAME/$folder --private + git clone --bare $src + cd "${repo}.git" + git push --mirror https://github.com/$ORG_NAME/$folder + cd .. + rm -rf "${repo}.git" + cd .. + gh repo edit $ORG_NAME/$folder --default-branch $ref + git clone https://github.com/$ORG_NAME/$folder + cd $folder + git remote add upstream $src + git remote set-url --push upstream DISABLE + cd ../$REPO_NAME + git remote add -f $repo https://github.com/$ORG_NAME/$folder + git subrepo clone -b $ref $repo $dest + #git subtree add --prefix $dest --squash $repo $ref -m "Add $repo as subtree" + #git merge --squash --allow-unrelated-histories main + git push origin main + fi + elif [ "$script_action" = "pull" ]; then + cd .. + cd $folder + git fetch upstream + git pull origin $ref --no-edit + git checkout -b origin/$ref origin/$ref + GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash upstream/$ref + + while true + do + if git diff-index --quiet HEAD --; then + git rebase --skip + retVal=$? + else + git rebase --continue + retVal=$? + fi + if [ $retVal -ne 1 ]; then + break + fi + done + + git pull origin $ref --no-edit + git push --force-with-lease origin origin/$ref:$ref + git checkout $ref + git pull --no-edit + git branch -d origin/$ref + + cd ../$REPO_NAME + git subrepo pull $dest + git push origin main + #git checkout -b update-subtree main + #git fetch $repo $ref + #git branch "$repo-$ref" "$repo/$ref" + #git merge --squash -s recursive -Xsubtree=$dest -Xtheirs --allow-unrelated-histories --no-commit "$repo-$ref" + #git commit -m "Update subtree $repo" + #git checkout main + #git merge update-subtree + #git branch -d update-subtree + #git branch -d "$repo-$ref" + #git push origin main + elif [ "$script_action" = "push" ] && [ "$script_remote" = "$dest" ]; then + git subrepo push -b $ref $dest + cd .. + cd $folder + git fetch upstream + git pull origin $ref --no-edit + break + fi + fi + elif [ "$action" = "move_path" ]; then + if [ "$script_action" = "create" ]; then + src=$(echo $json | jq -r '.src') + dest=$(echo $json | jq -r '.dest') + mv $src $dest + fi + elif [ "$action" = "download_file" ]; then + if [ "$script_action" = "create" ]; then + path=$(echo $json | jq -r '.path') + url=$(echo $json | jq -r '.url') + echo $path + echo $url + mkdir -p ${path%/*} + wget -O $path $url + fi + elif [ "$action" = "unzip" ]; then + if [ "$script_action" = "create" ]; then + src=$(echo $json | jq -r '.src') + dest=$(echo $json | jq -r '.dest') + mkdir -p $dest + unzip $src -d $dest + fi + elif [ "$action" = "remove_path" ]; then + if [ "$script_action" = "create" ]; then + path=$(echo $json | jq -r '.path') + echo "Removing path $path" + rm -rf $path + git add . + git commit -m "Non git stuff" + git push origin main + fi + fi + done +} diff --git a/pull_repo.sh b/pull_repo.sh deleted file mode 100755 index e0c5aea..0000000 --- a/pull_repo.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/bash - -source env.sh - -if [ "$mainRepoURL" = "" ] || [ "$mainRepoPath" = "" ] || [ "$recipePath" = "" ]; then - echo "Invalid arguments. Exiting." - exit 1 -fi - -if ! command -v jq &> /dev/null -then - echo "jq could not be found" - exit 1 -fi - -if ! command -v yq &> /dev/null -then - echo "yq could not be found" - exit 1 -fi - -if ! command -v git &> /dev/null -then - echo "git could not be found" - exit 1 -fi - -pushd $mainRepoPath - -for row in $(cat $recipePath | yq -r '.tasks[] | @base64'); do - json=$(echo $row | base64 -d) - action=$(echo $json | jq -r '.action') - skip=$(echo $json | jq -r '.skip') - if [ "$skip" = "true" ]; then - continue - fi - if [ "$action" = "waste_time" ]; then - echo "Wasting Time" - sleep $(echo $json | jq '.seconds') - elif [ "$action" = "download_github" ]; then - subpath=$(echo $json | jq -r '.subpath') - src=$(echo $json | jq -r '.src') - dest=$(echo $json | jq -r '.dest') - if [ "$subpath" != "null" ]; then - continue - fi - ref=$(echo $json | jq -r '.ref') - - re="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+?)(\.git)?$" - if [[ $src =~ $re ]]; then - repo=${BASH_REMATCH[5]} - - dest=$(echo $dest | cut -c 3-) - - git checkout -b update-subtree main - git fetch $repo $ref - git branch "$repo-$ref" "$repo/$ref" - git merge --squash -s recursive -Xsubtree=$dest -Xtheirs --allow-unrelated-histories --no-commit "$repo-$ref" - git commit -m "Update subtree $repo" - git checkout main - git rebase update-subtree - git branch -d update-subtree - git branch -d "$repo-$ref" - git push origin main - fi - fi -done - -popd