Hello,

for working easier with the agit work-flow at codeberg/forgejo I created a script. Maybe this is useful for others, too. So please find it attached.

Features:

 * Checks whether you are on a "topic" branch already, otherwise
   creates one
 * If the topic was already pushed (and no pull-request created):
     o Update the pull-request
 * If the topic was net yet pushed (no pull-request created)
     o tries to determine the base branch ("for"-branch in agit)
     o asks for a title for the pull-request
     o asks for a description for the pull-requests (not yet working
       due to limitations of forgejo/codeberg)

Any improvements are welcome :-)

--
Regards
Hartmut Goebel

| Hartmut Goebel          |[email protected]               |
|www.crazy-compilers.com | compilers which you thought are impossible |
#!/bin/bash
# Helper for the agit workflow, see
# https://forgejo.org/docs/latest/user/agit-support/
#
# Copyright 2025 by Hartmut Goebel <[email protected]>
# SPDX-License-Identifier: AGPL-3.0-or-later


set -eu -o pipefail

EDITOR=${EDITOR:-emacs}

TOPIC=$(git branch --show-current)
if [ "$TOPIC" = "master" -o "$TOPIC" = "main" ] ; then
    echo "On $TOPIC, please enter the name for the topic."
    echo "A branch with this name will be created, too."
    read -p "Topic name: " -e TOPIC
    git branch -c $TOPIC
    git checkout $TOPIC
else
    echo "OKAY: topic (auto-detected): $TOPIC"
fi

REMOTE_BRANCH=$(git config --local --get branch.$TOPIC.merge || true)
if [ "${REMOTE_BRANCH#refs/for/}" != "$REMOTE_BRANCH" ] ; then
    # already pushed
    PUSHED=yes
else
    PUSHED=
fi

if [ -z "$PUSHED" ] ; then
    # not yet pushed, get for-branch
    for fb in main master ; do
        if git branch --contains $fb 2>/dev/null; then
            FOR_BRANCH=$fb
            break
        fi
    done
    read -p "for branch: " -e -i "$FOR_BRANCH" FOR_BRANCH
    if [ -z "$FOR_BRANCH" ] ; then
        echo "no for-branch given, exiting"
        exit 1
    fi
    REMOTE_BRANCH=refs/for/$FOR_BRANCH/$TOPIC
fi

declare -a OPTIONS
#OPTIONS=
FORCE=

if [ -z "$PUSHED" ] ; then
    echo "Pull-request does not yet exist, please provide a title and a 
description"
    read -p "title: " -e TITLE
    OPTIONS+=("-o" "title=$TITLE")

    tmpfile=$(mktemp --suffix .md)
    $EDITOR $tmpfile
    # push options must not have new line characters, thus
    # base64-encode it
    DESCRIPTION=$(base64 --wrap=0 $tmpfile)
    if [ -n "$DESCRIPTION" ] ; then
        OPTIONS+=("-o" "description={base64}$DESCRIPTION")
    fi
    # rm $tmpfile
else
    FORCE=-f
    OPTIONS+=("-o" "force-push")
fi

git push upstream $FORCE $TOPIC:$REMOTE_BRANCH "${OPTIONS[@]}"
git config branch.$TOPIC.merge $REMOTE_BRANCH

Reply via email to