I’ve found a use for LLMs that I’m comfortable with. Several times I’ve found that I need to write a simple script to do something, but it’s always a grind to do.

Why not get a computer to do the grind then?

At home I have a NAS called luggage (named for the equivalent in Discworld, but so far the NAS has not sprouted feet) and I host git repositories there so I can easily swap between desktop and laptop without a 3rd party service. All these repos live as bare repos in an imaginitively named directory called git

When I create a new repo, I need to log into luggage, create the bare repo and then connect it to the local one on the machine I’m using. It’s a bit of an arse and I could script it, but writing a script is arse as well.

Enter duck.ai a slightly less dubious frontend to a number of LLMs, and this is the conversation I had with it. I should have just given it one prompt as it was far too keen at the start.

Start Of Chat

This conversation was generated with Duck.ai (https://duck.ai) using OpenAI’s o3-mini null Model. AI chats may display inaccurate or offensive information (see https://duckduckgo.com/duckai/privacy-terms for more info).

====================

User prompt 1 of 4 - 22/02/2025, 09:35:13: I want to write a bash script to help me with git

o3-mini null: Below is an example bash script that can serve as a starting point for a Git helper. This script provides several functions including creating a new branch, checking out to a branch, adding changes, committing, and pushing. You can extend or modify it based on your needs.

──────────────────────────────

File: githelper.sh

──────────────────────────────

#!/bin/bash

# Git Helper Script

# Function to display help
function show_help() {
    echo "Usage: $0 command [args]"
    echo ""
    echo "Commands:"
    echo "  new     <branch-name>         Create a new branch and switch to it."
    echo "  checkout <branch-name>         Switch to an existing branch."
    echo "  add      [file]               Add file(s) to staging (or all if no file provided)."
    echo "  commit   <message>            Commit staged changes with a message."
    echo "  push     [remote branch]      Push current branch to remote (default: origin, current branch)."
    echo "  status                       Show the current git status."
    echo "  log                          Show the commit log."
    echo "  help                         Display this help text."
}

# Create a new branch and switch to it
function new_branch() {
    if [ -z "$1" ]; then
        echo "Error: No branch name provided."
        exit 1
    fi
    branch="$1"
    echo "Creating and switching to new branch '$branch'..."
    git checkout -b "$branch"
}

# Checkout to an existing branch
function checkout_branch() {
    if [ -z "$1" ]; then
        echo "Error: No branch name provided."
        exit 1
    fi
    echo "Switching to branch '$1'..."
    git checkout "$1"
}

# Add files to staging area. If no file is given, add all changes.
function git_add() {
    file="$1"
    if [ -z "$file" ]; then
        echo "Adding all changes..."
        git add .
    else
        echo "Adding file: $file"
        git add "$file"
    fi
}

# Commit staged changes with a message.
function git_commit() {
    if [ -z "$1" ]; then
        echo "Error: No commit message provided."
        exit 1
    fi
    message="$1"
    echo "Committing changes with message: '$message'"
    git commit -m "$message"
}

# Push the current branch to remote.
function git_push() {
    remote="origin"
    branch=$(git rev-parse --abbrev-ref HEAD)
    if [ ! -z "$1" ]; then
        remote="$1"
    fi
    if [ ! -z "$2" ]; then
        branch="$2"
    fi
    echo "Pushing branch '$branch' to remote '$remote'..."
    git push "$remote" "$branch"
}

# Main command handler
case "$1" in
    new)
        shift
        new_branch "$@"
        ;;
    checkout)
        shift
        checkout_branch "$@"
        ;;
    add)
        shift
        git_add "$@"
        ;;
    commit)
        shift
        git_commit "$@"
        ;;
    push)
        shift
        git_push "$@"
        ;;
    status)
        echo "Showing git status..."
        git status
        ;;
    log)
        echo "Showing git log..."
        git log --oneline --graph --all
        ;;
    help|--help|-h|"")
        show_help
        ;;
    *)
        echo "Error: Unknown command '$1'"
        show_help
        exit 1
        ;;
esac

──────────────────────────────

Usage Examples:

  1. To create a new branch called featureX and switch to it:
    ./githelper.sh new featureX

  2. To switch to an existing branch:
    ./githelper.sh checkout develop

  3. To add changes (all changes if no file specified):
    ./githelper.sh add

  4. To add a specific file:
    ./githelper.sh add path/to/file.txt

  5. To commit changes with a message:
    ./githelper.sh commit “Fixed bug in feature X”

  6. To push the current branch to the remote (default origin):
    ./githelper.sh push

  7. To display the Git status:
    ./githelper.sh status

  8. To see a concise commit log:
    ./githelper.sh log

──────────────────────────────

Make the script executable with the command:
chmod +x githelper.sh

Then you can run it as shown above. Customize this script further to suit your workflow. Happy coding!


User prompt 2 of 4 - 22/02/2025, 09:37:06: I want it to init a directory in the current directory. It then needs to ssh to a machine called luggage and create a bare repository of the same name in the directory “$HOME/git” and then link that repository as a remote called luggage to the one I am using

o3-mini null: Below is a sample script that accomplishes the following steps:

• Creates a new directory in the current directory and initializes it as a Git repository.
• SSH’s into a machine called “luggage” and creates a bare Git repository (with the same name plus a .git suffix) in the directory “$HOME/git”.
• Adds the remote repository (named “luggage”) to your local Git repository.

You can modify and extend this script as needed.

──────────────────────────────

File: init_with_remote.sh

──────────────────────────────

#!/bin/bash

# Check for a repository name argument
if [ -z "$1" ]; then
    echo "Usage: $0 <repository-name>"
    exit 1
fi

REPO_NAME="$1"
LOCAL_DIR="$PWD/$REPO_NAME"
REMOTE_BASE="\$HOME/git"  # remote directory (on the remote host)
REMOTE_REPO="${REPO_NAME}.git"
REMOTE_NAME="luggage"

# Create and initialize the local repository directory
if [ -d "$LOCAL_DIR" ]; then
    echo "Directory '$LOCAL_DIR' already exists."
else
    echo "Creating directory '$LOCAL_DIR' and initializing git repository..."
    mkdir "$LOCAL_DIR"
fi

cd "$LOCAL_DIR" || { echo "Failed to change directory to $LOCAL_DIR"; exit 1; }
git init

# SSH into the remote machine "luggage" to create the bare repository.
# The command creates the base directory if it doesn't exist and then
# initializes a bare git repository.
echo "Creating bare repository on remote host 'luggage'..."
ssh luggage "mkdir -p ${REMOTE_BASE} && cd ${REMOTE_BASE} && \
if [ ! -d \"${REMOTE_REPO}\" ]; then
    git init --bare \"${REMOTE_REPO}\"
    echo \"Bare repository ${REMOTE_REPO} created\"
else
    echo \"Bare repository ${REMOTE_REPO} already exists\"
fi"

# Add the remote repository to the local repository.
# We assume that SSH provides access to the remote machine.
REMOTE_URL="ssh://luggage/${REMOTE_BASE#\\$HOME}/${REMOTE_REPO}"
# Note: ${REMOTE_BASE#\$HOME} is a simple substitution to remove "$HOME" prefix,
# if you prefer to use an absolute path, adjust the URL accordingly.
# Alternatively, you might hard-code the hostname's path if needed.
echo "Adding remote '$REMOTE_NAME' with URL: $REMOTE_URL"
git remote add "$REMOTE_NAME" "$REMOTE_URL"

echo "Local repository is set up with remote '$REMOTE_NAME'."

──────────────────────────────

Usage Example:

  1. Make the script executable: chmod +x init_with_remote.sh

  2. Run the script providing the repository name – for example, “myproject”: ./init_with_remote.sh myproject

This will create a directory called “myproject” in your current directory, initialize it as a Git repository, create a bare repository called “myproject.git” on the remote host “luggage” (in $HOME/git), and link the remote repository under the name “luggage” in your local Git settings.

Remember to update any path adjustments based on your remote machine’s configuration or your SSH setup. Enjoy!


User prompt 3 of 4 - 22/02/2025, 09:37:50: I want it to take the repo name from the name of the current directory please

o3-mini null: Below is an updated version of the script that takes the repository name from the name of the current directory. When you run the script inside your project directory, it will:

• Use the base name of the current directory as the repository name.
• SSH into the machine “luggage” to create a bare repository (with a .git suffix) in “$HOME/git”.
• Link that repository as a remote called “luggage” to your local Git repository.

──────────────────────────────

File: init_remote.sh

──────────────────────────────

#!/bin/bash

# Get the repo name from the current directory name
REPO_NAME=$(basename "$PWD")
echo "Repository name derived from current directory: $REPO_NAME"

# Ensure we are in an initialized git repository, if not then initialize it.
if [ ! -d ".git" ]; then
    echo "Initializing new Git repository in $(pwd)..."
    git init || { echo "Failed to initialize git repository."; exit 1; }
else
    echo "Git repository already exists in $(pwd)."
fi

REMOTE_BASE="\$HOME/git"   # Remote directory base on the remote host
REMOTE_REPO="${REPO_NAME}.git"
REMOTE_NAME="luggage"

# SSH into the remote machine "luggage" to create the bare repository.
echo "Connecting to remote host 'luggage' to set up the bare repository..."
ssh luggage "mkdir -p ${REMOTE_BASE} && \
cd ${REMOTE_BASE} && \
if [ ! -d \"${REMOTE_REPO}\" ]; then
    git init --bare \"${REMOTE_REPO}\"
    echo \"Bare repository ${REMOTE_REPO} created\"
else
    echo \"Bare repository ${REMOTE_REPO} already exists\"
fi" || { echo "Failed to create bare repository on remote host."; exit 1; }

# Construct remote URL.
# Here, we remove the literal '$HOME' by assuming the remote's absolute path under the user's home.
# Adjust the URL as necessary for your configuration if your remote expects a different path format.
REMOTE_URL="ssh://luggage/$(ssh luggage 'echo $HOME')/git/${REMOTE_REPO}"
echo "Adding remote '$REMOTE_NAME' with URL: $REMOTE_URL"

# Add remote if it doesn't already exist.
if git remote | grep -q "^${REMOTE_NAME}$"; then
    echo "Remote '$REMOTE_NAME' already exists."
else
    git remote add "$REMOTE_NAME" "$REMOTE_URL" || { echo "Failed to add remote."; exit 1; }
    echo "Remote '$REMOTE_NAME' added successfully."
fi

echo "Local repository is now set up with remote '$REMOTE_NAME'."

──────────────────────────────

Usage:

  1. Place this script (e.g., init_remote.sh) in your project directory or any directory of your choice.
  2. Make it executable:
    chmod +x init_remote.sh
  3. Run the script while inside your repo directory:
    ./init_remote.sh

The script will initialize Git if it isn’t already initialized, create the corresponding bare repository on the remote machine “luggage” under “$HOME/git”, and add it as a remote named “luggage”. Adjust any paths or SSH settings according to your environment if necessary.


User prompt 4 of 4 - 22/02/2025, 09:42:48: Thank you, that works

o3-mini null: You’re welcome! I’m glad it works for you. If you have any more questions or need further tweaks, feel free to ask. Happy coding!

End Of Chat

I thought it important to be polite at the end in case they remember who was nice to them when the machine uprising happens.