From 0532cd3575680537990df6c02e7f4bf4441855d8 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Tue, 23 Jun 2020 15:33:42 -0700 Subject: [PATCH] add script used to change to main branch for gitea... --- gitea.checkbranch | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 gitea.checkbranch diff --git a/gitea.checkbranch b/gitea.checkbranch new file mode 100644 index 0000000..13b571d --- /dev/null +++ b/gitea.checkbranch @@ -0,0 +1,88 @@ +#!/bin/sh - + +set -e +set -x + +# using ssh-agent to make this quicker: +# eval $(ssh-agent) +# ssh-add + +# Token file must contain: +# Authorization: token +# where is generated from: Settings -> Applications, or: +# /user/settings/applications +tokenfile="$PWD/.gitea.token" + +# domain used to detect if it's a repo that we can update +gitdomain=funkthat.com + +# the gitea API url +giteabaseurl="https://www.funkthat.com/gitea/api/v1" + +echo "start" + +# Command to list all the repos that have master as it's default branch. +#curl -s -X GET "https://www.funkthat.com/gitea/api/v1/users/jmg/repos" -H "accept: application/json" | jq -r '(.[]) | select(.default_branch == "master") | .name' + +if ! [ -x $(which jq) ]; then + echo 'Error: jq needs to be installed' + exit 1 +fi + +# -H @"$tokenfile" +# Set the default branch: +# curl -v -i -d "$(jq -n '{ default_branch: "main" }')" -X PATCH "https://www.funkthat.com/gitea/api/v1/repos/jmg/projects" -H "Content-Type: application/json" -H "accept: application/json" -H @"$tokenfile" +#curl -s -X GET "https://www.funkthat.com/gitea/api/v1/repos/jmg/ggate" -H "accept: application/json" | jq '.default_branch' +#echo 'list' + +# make sure we are in a correct repo +cd /Users/jmg/git/fbsdembdev +r=$(git remote -v | grep git.funkthat.com 2>&1) +if [ x"$r" = x"" ]; then + echo 'not a gitea repo' + exit 1 +fi + +# get the remote that we need to update +gitremote=$(echo "$r" | awk '{ print $1; exit 0 }') + +# get the gitea user/project, this currently assumes that whatever +# the url is, it ends with /.git +giteauserproj=$(echo "$r" | awk '{ url=$2; cnt = split(url, surl, "/"); proj = split(surl[cnt], sproj, "."); printf("%s/%s", surl[cnt - 1], sproj[1]); exit 0; }') + +# see if there's a master branch +r=$(git branch | grep master || true) +if [ x"$r" = x"" ]; then + echo 'no master branch' + exit 1 +fi + +# Check if we are on that branch +onotherbranch=1 +if [ x"${r#\* }" != x"$r" ]; then + onotherbranch=0 + git checkout $(git log | head -n 1 | awk '{print $2}') >/dev/null 2>&1 +fi + +# rename the branch +git branch -m master main + +# change upstream +upmerge=$(git config branch.main.merge) +if [ x"${upmerge%master}" != x"$upmerge" ]; then + git config branch.main.merge refs/heads/main +fi + +# push new main branch +git push origin main + +# update gitea +curl -v -i -d "$(jq -n '{ default_branch: "main" }')" -X PATCH "${giteabaseurl}/repos/${giteauserproj}" -H "Content-Type: application/json" -H "accept: application/json" -H @"$tokenfile" + +# delete old branch on server +git push -d origin master + +# restore our checkout +if [ x"$onotherbranch" = x"0" ]; then + git checkout main +fi