From 6bdae4835442f90170e124b1b1f7c6dfd6948335 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 12 Jan 2024 09:46:11 +0100 Subject: [PATCH] feat: pushing existing commands i made this some time ago, pushing as one commit --- descr | 27 +++++++++++++++++++++++++++ fetch-mirrors | 41 +++++++++++++++++++++++++++++++++++++++++ group | 30 ++++++++++++++++++++++++++++++ head | 30 ++++++++++++++++++++++++++++++ help | 17 +++++++++++++++++ hide | 25 +++++++++++++++++++++++++ log | 21 +++++++++++++++++++++ ls | 10 ++++++++++ mirror | 30 ++++++++++++++++++++++++++++++ new | 20 ++++++++++++++++++++ owner | 30 ++++++++++++++++++++++++++++++ page | 30 ++++++++++++++++++++++++++++++ public | 14 ++++++++++++++ show | 25 +++++++++++++++++++++++++ tree | 19 +++++++++++++++++++ upstream | 26 ++++++++++++++++++++++++++ 16 files changed, 395 insertions(+) create mode 100755 descr create mode 100755 fetch-mirrors create mode 100755 group create mode 100755 head create mode 100755 help create mode 100755 hide create mode 100755 log create mode 100755 ls create mode 100755 mirror create mode 100755 new create mode 100755 owner create mode 100755 page create mode 100755 public create mode 100755 show create mode 100755 tree create mode 100755 upstream diff --git a/descr b/descr new file mode 100755 index 0000000..b9678d6 --- /dev/null +++ b/descr @@ -0,0 +1,27 @@ +#!/bin/bash + +REPOSITORY="$1" + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository does not exist" + exit +fi + +if [ $# -eq 1 ]; then + echo "[!] no branch name specified" + exit +fi + + +echo "$2" > "/srv/git/$REPOSITORY/description" +echo "Changed description of $REPOSITORY to '$2'" + diff --git a/fetch-mirrors b/fetch-mirrors new file mode 100755 index 0000000..56bb80c --- /dev/null +++ b/fetch-mirrors @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import os +import os.path +import subprocess + +GIT_ROOT = os.environ.get("GIT_ROOT_DIR") or "/srv/git/" + +def is_mirror(repo): + cmd = ["git", "config", "-f", f"{GIT_ROOT}{repo}/config", "remote.origin.mirror"] + proc = subprocess.run(cmd, stdout=subprocess.PIPE) + if proc.stdout.decode().strip() == "true": + return True + return False + +def get_direction(repo): + cmd = ["git", "config", "-f", f"{GIT_ROOT}{repo}/config", "remote.origin.direction"] + proc = subprocess.run(cmd, stdout=subprocess.PIPE) + return proc.stdout.decode().strip() + +def fetch_all(repo): + print(f"[<] Fetching {repo}") + subprocess.run(["git", "fetch", "--all"], cwd=GIT_ROOT+repo) + if os.path.isfile(f"{GIT_ROOT}{repo}/hooks/post-receive"): + subprocess.run([f"{GIT_ROOT}{repo}/hooks/post-receive"]) + +def push_all(repo): + print(f"[>] Pushing {repo}") + subprocess.run(["git", "push", "origin"], cwd=GIT_ROOT+repo) + #subprocess.run([f"{GIT_ROOT}.hooks/post-update"], cwd=GIT_ROOT+repo) + +if __name__ == "__main__": + for element in os.listdir(GIT_ROOT): + if element.endswith(".git"): + if is_mirror(element): + direction = get_direction(element) + if direction == "up": + push_all(element) + if direction == "down": + fetch_all(element) + diff --git a/group b/group new file mode 100755 index 0000000..eccfde2 --- /dev/null +++ b/group @@ -0,0 +1,30 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" # with .git +REPONAME="$1" # without .git + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [[ "$REPONAME" == *.git ]]; then + REPONAME=$(echo "$REPONAME" | sed 's/\.git$//') +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository doesn't exist" + exit +fi + +if [ $# -eq 1 ]; then + git config -f "/srv/git/$REPOSITORY/config" --unset gitweb.category + echo "[ ] unset category from $REPONAME" +else + git config -f "/srv/git/$REPOSITORY/config" gitweb.category "$2" + echo "[,] set '$2' as $REPONAME category" +fi + diff --git a/head b/head new file mode 100755 index 0000000..645d9b1 --- /dev/null +++ b/head @@ -0,0 +1,30 @@ +#!/bin/bash + +REPOSITORY="$1" + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository does not exist" + exit +fi + +if [ $# -eq 1 ]; then + echo "[!] no branch name specified" + exit +fi + + +cd "/srv/git/$REPOSITORY" +git symbolic-ref HEAD "refs/heads/$2" +git config -f "/srv/git/$REPOSITORY/config" init.defaultBranch "$2" + +echo "Changed HEAD of $REPOSITORY to refs/heads/$2" + diff --git a/help b/help new file mode 100755 index 0000000..e84af2a --- /dev/null +++ b/help @@ -0,0 +1,17 @@ +#!/bin/bash +echo "Command list:" +echo " $ help print this" +echo " $ ls list all repositories" +echo " $ public list public repositories" +echo " $ new create new repo" +echo " $ mirror [name] mirror remote repository" +echo " $ upstream set repository to push to a given remote" +echo " $ tree show file tree of repo at HEAD" +echo " $ log show commit log of repo" +echo " $ head change default branch of repository" +echo " $ descr change description of repository" +echo " $ owner [owner] set or unset owner for repository" +echo " $ group [group] set or unset group for repository" +echo " $ page [page] set or unset homepage for repository" +echo " $ show make repository public" +echo " $ hide hide public repository" diff --git a/hide b/hide new file mode 100755 index 0000000..206044f --- /dev/null +++ b/hide @@ -0,0 +1,25 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" # with .git +REPONAME="$1" # without .git + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [[ "$REPONAME" == *.git ]]; then + REPONAME=$(echo "$REPONAME" | sed 's/\.git$//') +fi + +if [ ! -d "/srv/git/.public/$REPONAME" ]; then + echo "[!] repository isn't public" + exit +fi + +rm "/srv/git/.public/$REPONAME" + +echo "[#] made $REPONAME hidden" diff --git a/log b/log new file mode 100755 index 0000000..c61e98b --- /dev/null +++ b/log @@ -0,0 +1,21 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository doesn't exists" + exit +fi + +cd "/srv/git/$REPOSITORY" +echo "$ git log --graph --abbrev-commit --decorate $REPOSITORY" +TERM=xterm git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all +echo "" diff --git a/ls b/ls new file mode 100755 index 0000000..c113092 --- /dev/null +++ b/ls @@ -0,0 +1,10 @@ +#!/bin/bash + +# ls /srv/git -I 'git-shell-commands' + +# one above misses repos like '.dotfiles', let's do it by hand +for f in $(ls /srv/git -a); do + if [[ $f == *.git ]]; then + echo $f + fi +done diff --git a/mirror b/mirror new file mode 100755 index 0000000..3cbcc04 --- /dev/null +++ b/mirror @@ -0,0 +1,30 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository specified" + exit +fi + +REMOTE="origin" # TODO can we get this as argument but optionally? + +if [ $# -eq 1 ]; then + IFS='/' read -ra FRAGS <<< "$1" + REPOSITORY=${FRAGS[-1]} + REPONAME=${FRAGS[-1]} +else + REPOSITORY="$2" # with .git + REPONAME="$2" # without .git +fi + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [[ "$REPONAME" == *.git ]]; then + REPONAME=$(echo "$REPONAME" | sed 's/\.git$//') +fi + +git clone --mirror "$1" "/srv/git/$REPOSITORY" --origin "$REMOTE" +git config -f "/srv/git/$REPOSITORY/config" "remote.$REMOTE.direction" "down" + +cd "$REPOSITORY" +/srv/git/.hooks/post-receive diff --git a/new b/new new file mode 100755 index 0000000..4df4061 --- /dev/null +++ b/new @@ -0,0 +1,20 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [ -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository already exists" + exit +fi + +mkdir -p "/srv/git/$REPOSITORY" +cd "/srv/git/$REPOSITORY" +git init --bare diff --git a/owner b/owner new file mode 100755 index 0000000..6e3e85c --- /dev/null +++ b/owner @@ -0,0 +1,30 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" # with .git +REPONAME="$1" # without .git + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [[ "$REPONAME" == *.git ]]; then + REPONAME=$(echo "$REPONAME" | sed 's/\.git$//') +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository doesn't exist" + exit +fi + +if [ $# -eq 1 ]; then + git config -f "/srv/git/$REPOSITORY/config" --unset gitweb.owner + echo "[ ] unset owner from $REPONAME" +else + git config -f "/srv/git/$REPOSITORY/config" gitweb.owner "$2" + echo "['] set '$2' as $REPONAME owner" +fi + diff --git a/page b/page new file mode 100755 index 0000000..ce7e8b0 --- /dev/null +++ b/page @@ -0,0 +1,30 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" # with .git +REPONAME="$1" # without .git + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [[ "$REPONAME" == *.git ]]; then + REPONAME=$(echo "$REPONAME" | sed 's/\.git$//') +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository doesn't exist" + exit +fi + +if [ $# -eq 1 ]; then + git config -f "/srv/git/$REPOSITORY/config" --unset gitweb.homepage + echo "[ ] unset homepage from $REPONAME" +else + git config -f "/srv/git/$REPOSITORY/config" gitweb.homepage "$2" + echo "[@] set '$2' as $REPONAME homepage" +fi + diff --git a/public b/public new file mode 100755 index 0000000..3476e10 --- /dev/null +++ b/public @@ -0,0 +1,14 @@ +#!/bin/bash + +# ls /srv/git -I 'git-shell-commands' + +# one above misses repos like '.dotfiles', let's do it by hand +for f in $(ls /srv/git/.public -a); do + if [[ $f == '.' ]]; then + continue + elif [[ $f == '..' ]]; then + continue + fi + + echo "$f" +done diff --git a/show b/show new file mode 100755 index 0000000..ccda755 --- /dev/null +++ b/show @@ -0,0 +1,25 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" # with .git +REPONAME="$1" # without .git + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [[ "$REPONAME" == *.git ]]; then + REPONAME=$(echo "$REPONAME" | sed 's/\.git$//') +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository doesn't exist" + exit +fi + +ln -s "/srv/git/$REPOSITORY" "/srv/git/.public/$REPONAME" + +echo "[#] made $REPONAME publicly visible" diff --git a/tree b/tree new file mode 100755 index 0000000..02078c2 --- /dev/null +++ b/tree @@ -0,0 +1,19 @@ +#!/bin/bash +if [ $# -eq 0 ]; then + echo "[!] no repository name specified" + exit +fi + +REPOSITORY="$1" + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository doesn't exists" + exit +fi + +cd "/srv/git/$REPOSITORY" +git ls-tree -r HEAD --name-only --full-tree | TERM=xterm less diff --git a/upstream b/upstream new file mode 100755 index 0000000..43ced29 --- /dev/null +++ b/upstream @@ -0,0 +1,26 @@ +#!/bin/bash +if [ $# -lt 2 ]; then + echo "[!] must specify both repository and remote" + exit +fi + +REPOSITORY="$1" +URL="$2" + +if [[ ! "$REPOSITORY" == *.git ]]; then + REPOSITORY="$REPOSITORY.git" +fi + +if [ ! -d "/srv/git/$REPOSITORY" ]; then + echo "[!] repository does not exist" + exit +fi + +REMOTE="origin" # TODO can we get this as argument but optionally? + +cd "/srv/git/$REPOSITORY" + +git remote remove "$REMOTE" # TODO this complains about branches abandoned, but we will recreate the remote right after this, so maybe do it smartly and check? +git remote add "$REMOTE" "$URL" +git config remote.$REMOTE.mirror "true" +git config remote.$REMOTE.direction "up"