dotfiles/.local/bin/scripts/git-fork

32 lines
1.2 KiB
Bash
Executable File

#!/bin/bash -e
# Clones a fork and sets a corresponding upstream
# ARGS
# - repo name (omit if setting upstream in existing repo)
# - upstream user
# - own user
# - host
# - target directory name (and further arguments to clone)
# In an existing repo, first arg is omitted
# TODO: transform https://codeberg.org/forgejo-contrib/forgejo-helm -> forgejo-helm forgejo-contrib "" codeberg.org
if test -d ".git" # TODO search upwards
then repo="$(basename "$PWD")"
git remote set-url origin "$(git-repo "${3:-github.com}" "$repo" "${2:-$(git config --get user.name)}" "${@:4}")"
git remote remove upstream 2>/dev/null || true
else test $# -eq 0 && echo "Usage: $0 <repo (omit if in repo)> <upstream owner> [own user] [host]" && exit 2
repo="$1" && shift
if test "$#" -eq 0
then git-get "$repo"
else
test -d "$d1" && cd "$d1/0-forks"
if ! git-get "${3:-github.com}" "$repo" "${2:-$(git config --get user.name)}" "${@:4}"
then repo="$1-$repo"
git-get "${3:-github.com}" "$repo" "${2:-$(git config --get user.name)}" "${@:4}"
fi
fi
cd "$(basename "$(expr "$4" \| "$repo")")"
fi
user="${1:-$repo}"
git-upstream "$user"
#git remote add upstream "$(git-repo "${3:-github.com}" "$repo" "$user")"
exec $SHELL