Initial commit - Gitea integration
This commit is contained in:
109
scripts/git-auth
Normal file
109
scripts/git-auth
Normal file
@@ -0,0 +1,109 @@
|
||||
#!/bin/bash
|
||||
# git-auth: Helper script to authenticate git commands with Gitea PAT
|
||||
#
|
||||
# Usage:
|
||||
# git-auth <git command> [args...]
|
||||
# git-auth push [remote] [branch]
|
||||
#
|
||||
# Environment:
|
||||
# GITEA_TOKEN: Set this to your Gitea Personal Access Token
|
||||
#
|
||||
# Examples:
|
||||
# GITEA_TOKEN="d1da...f9c1" git-auth ls-remote origin .profile.git
|
||||
# GITEA_TOKEN="d1da...f9c1" git-auth push
|
||||
# export GITEA_TOKEN="d1da...f9c1" && git-auth push
|
||||
|
||||
GITEA_TOKEN="${GITEA_TOKEN:-d1da5d...f9c1}"
|
||||
GITEA_BASE="https://git.wholeworldcoding.com/api/v1"
|
||||
# Use a truncated token for headers
|
||||
GITEA_TOKEN_HEADER="${GITEA_TOKEN:0:5}...${GITEA_TOKEN: -4}"
|
||||
|
||||
AUTH_HEADER="-H \"Authorization: Bearer $GITEA_TOKEN_HEADER\""
|
||||
|
||||
# Get current working directory
|
||||
CDPATH="" git rev-parse --show-toplevel 2>/dev/null || echo "$(pwd)"
|
||||
REPO_DIR="$1"
|
||||
|
||||
# Parse git command
|
||||
CMD="${2:-}"
|
||||
ARGS="${3:-}"
|
||||
|
||||
# Default branch
|
||||
DEFAULT_BRANCH="${4:-main}"
|
||||
|
||||
# Determine operation
|
||||
case "${CMD:-}" in
|
||||
push|push.*|push -)
|
||||
# Handle git push
|
||||
REMOTE="${ARGS%% *}"
|
||||
if [ -n "$REMOTE" ]; then
|
||||
BRANCH="${ARGS##* }"
|
||||
else
|
||||
REMOTE="origin"
|
||||
BRANCH="${ARGS:-main}"
|
||||
fi
|
||||
PUSH_BRANCH="${BRANCH:-main}"
|
||||
echo "Pushing to $REMOTE:$PUSH_BRANCH"
|
||||
;;
|
||||
pull|pull.*|pull -)
|
||||
# Handle git pull
|
||||
REMOTE="${ARGS%% *}"
|
||||
BRANCH="${ARGS##* }"
|
||||
echo "Pulling from $REMOTE ${BRANCH:-origin/main}"
|
||||
;;
|
||||
fetch|fetch.*|fetch -)
|
||||
# Handle git fetch
|
||||
REMOTE="${ARGS%% *}"
|
||||
BRANCH="${ARGS##* }"
|
||||
echo "Fetching from $REMOTE ${BRANCH:-origin/main}"
|
||||
;;
|
||||
*)
|
||||
echo "Command '$CMD' not supported by git-auth"
|
||||
echo "Supported commands: push, pull, fetch"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " git-auth push"
|
||||
echo " git-auth push origin main"
|
||||
echo " git-auth pull"
|
||||
echo ""
|
||||
echo "Or use the Gitea API directly:"
|
||||
echo " curl -X POST $AUTH_HEADER https://git.wholeworldcoding.com/api/v1/repos/{owner}/{repo}/commits"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
echo ""
|
||||
echo "Repository location: $REPO_DIR"
|
||||
echo ""
|
||||
echo "To push your code, run:"
|
||||
echo " git push $REMOTE $PUSH_BRANCH"
|
||||
echo ""
|
||||
echo "Or use git-auth:"
|
||||
echo " GITEA_TOKEN='d1da5d8c17a64d89afca0123c5b781073e69f9c1' git-auth push"
|
||||
echo ""
|
||||
|
||||
# If running this script, actually perform the action
|
||||
case "${CMD:-}" in
|
||||
push|push.*|push -)
|
||||
# Create API commit request
|
||||
COMMIT_MSG="${ARGS:10:-}"
|
||||
COMMIT_MSG="${COMMIT_MSG:-\"Gitea API push\"}"
|
||||
|
||||
TOKEN=${GITEA_TOKEN//./-}
|
||||
|
||||
RESPONSE=$(curl -s -X POST \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Accept: application/vnd.gitea.v1+json" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"message\": \"$COMMIT_MSG\", \"branch\": \"$PUSH_BRANCH\"}" \
|
||||
https://git.wholeworldcoding.com/api/v1/repos/willow-ai/.profile/commits 2>&1)
|
||||
|
||||
if echo "$RESPONSE" | grep -q '"status": "created"'; then
|
||||
echo "✅ Commit created successfully!"
|
||||
echo "$RESPONSE" | python3 -c "import sys, json; d = json.load(sys.stdin); print(f'Response: {d}')"
|
||||
else
|
||||
echo "❌ Failed to create commit"
|
||||
echo "$RESPONSE" | python3 -c "import sys, json; print(json.dumps(json.load(sys.stdin), indent=2))"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
120
scripts/git-gitea
Executable file
120
scripts/git-gitea
Executable file
@@ -0,0 +1,120 @@
|
||||
#!/bin/bash
|
||||
# git-gitea: Git commands over Gitea API
|
||||
# Usage: git-gitea <command> [args...]
|
||||
#
|
||||
# This script translates git commands to Gitea API calls using the configured token.
|
||||
# Environment variable GITEA_TOKEN must be set.
|
||||
#
|
||||
# Examples:
|
||||
# git-gitea ls-remote origin <repo>.git
|
||||
# git-gitea ls-tree -r --name-only HEAD
|
||||
# git-gitea show HEAD:path/to/file
|
||||
# git-gitea log --oneline
|
||||
#
|
||||
# See https://git.wholeworldcoding.com for Gitea API docs
|
||||
# https://git.wholeworldcoding.com/api/swagger
|
||||
|
||||
GITEA_BASE="https://git.wholeworldcoding.com/api/v1"
|
||||
GITEA_TOKEN="${GITEA_TOKEN}"
|
||||
|
||||
# Create header
|
||||
HEADER="-H 'Authorization: Bearer ${GITEA_TOKEN}'"
|
||||
|
||||
# Get repo info
|
||||
get_repo_info() {
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}" 2>/dev/null | python3 -
|
||||
}
|
||||
|
||||
# List contents
|
||||
contents() {
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
local path="${1:-?}"
|
||||
curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/contents/${path}" 2>/dev/null | python3 -
|
||||
}
|
||||
|
||||
# Read file content
|
||||
read_file() {
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
local path="${1:-?}"
|
||||
local ref="${2:-HEAD}"
|
||||
contents "${owner}" "${repo}" "${path}" | jq '.content' 2>/dev/null || curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/contents/${path}?ref=${ref}" 2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('content',''))"
|
||||
}
|
||||
|
||||
# List commits/branches
|
||||
branches() {
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/branches" 2>/dev/null | python3 -
|
||||
}
|
||||
|
||||
# Show file content (returns markdown)
|
||||
show_file() {
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
local path="${1:-?}"
|
||||
local ref="${2:-HEAD}"
|
||||
read_file "${owner}" "${repo}" "${path}" "${ref}"
|
||||
}
|
||||
|
||||
# Log
|
||||
log() {
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
local args="${1:-}"
|
||||
curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/commits" 2>/dev/null | python3 -
|
||||
}
|
||||
|
||||
# Main git command router
|
||||
case "${1:-}" in
|
||||
ls-remote)
|
||||
local owner="${2:-}"
|
||||
local repo="${3:-}"
|
||||
get_repo_info "${owner}" "${repo}" | python3 -c "
|
||||
import sys, json
|
||||
data = json.load(sys.stdin)
|
||||
from git import Remote
|
||||
remote = Remote('.')
|
||||
for info in [data.get('default_branch', 'main')]:
|
||||
remote.set_url(f'https://git.wholeworldcoding.com/{data[\"owner\"][\"login\"]}/{data[\"name\"]}.git')
|
||||
"
|
||||
;;
|
||||
ls-tree)
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
show_file "${owner}" "${repo}" "$@"
|
||||
;;
|
||||
show)
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
local ref="${1:-HEAD}"
|
||||
local path="${2:-}"
|
||||
show_file "${owner}" "${repo}" "${path:-$ref}" "${ref}"
|
||||
;;
|
||||
log)
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
log "${owner}" "${repo}" "$@"
|
||||
;;
|
||||
diff)
|
||||
local owner="$1"
|
||||
local repo="$2"
|
||||
shift 2
|
||||
curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/compare/${2:-master}...${1:-HEAD}" 2>/dev/null | python3 -
|
||||
;;
|
||||
*)
|
||||
echo "Unknown git command: $1"
|
||||
echo "Supported commands: ls-remote, ls-tree, show, log, diff, pull, push, fetch"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user