Skip to content

Instantly share code, notes, and snippets.

@jaimergp
Created November 12, 2025 14:12
Show Gist options
  • Select an option

  • Save jaimergp/7249d30dbd309949ea17d55745d71d9c to your computer and use it in GitHub Desktop.

Select an option

Save jaimergp/7249d30dbd309949ea17d55745d71d9c to your computer and use it in GitHub Desktop.
A handy bash function to work on conda-forge feedstocks
#!/bin/bash
# Set your GitHub username here
GH_USERNAME="REPLACE_ME"
DEVEL_DIR="${HOME}/devel"
conda_fork_clone_branch() {
local feedstock_name="$1"
local new_branch_name="$2"
local feedstock_repo="${feedstock_name}-feedstock"
local feedstock_url="[email protected]:conda-forge/${feedstock_repo}.git"
local local_dir="${DEVEL_DIR}/${feedstock_repo}"
local main_branch="main"
local needs_forking=false
if [[ -z "$feedstock_name" ]]; then
echo "❌ ERROR: You must provide the feedstock name as the first argument."
echo "Usage: conda_fork_clone_branch <feedstock-name> [new-branch-name]"
return 1
fi
echo "--- πŸš€ Starting setup for ${feedstock_repo} ---"
# ------------------------------------------------------------------
# --- PRIMARY CHECK (STEP 1: Check Local Clone) ---
# ------------------------------------------------------------------
if [[ -d "$local_dir" ]]; then
echo "1. Checking if local directory **${local_dir}** exists and has a valid upstream..."
(
cd "$local_dir" || return 1
local current_upstream_url
current_upstream_url=$(git remote get-url upstream 2>/dev/null)
# Check if upstream matches the required SSH URL
if [[ "$current_upstream_url" == "$feedstock_url" ]]; then
echo " - βœ… Found existing clone with correct 'upstream' remote (**SSH format**)."
# Stashing logic
local current_branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" != "$main_branch" ]] || git status --porcelain | grep -q '^\s*'; then
echo " - Stashing local changes or switching from branch **${current_branch}** to ${main_branch}..."
if git status --porcelain | grep -q '^\s*'; then
git stash push --include-untracked -m "WIP before switching to main for sync" &> /dev/null
fi
fi
# Sync
echo " - Switching to **${main_branch}** and syncing from upstream..."
git checkout "$main_branch"
git fetch upstream
git pull upstream "$main_branch"
else
echo " - ⚠️ Directory exists, but 'upstream' remote is missing or incorrect."
echo " - Proceeding to fork/clone logic."
needs_forking=true
fi
)
else
echo "1. Local directory **${local_dir}** not found. Proceeding to fork/clone logic."
needs_forking=true
fi
# ------------------------------------------------------------------
# --- FORK AND CLONE LOGIC (Secondary Path) ---
# ------------------------------------------------------------------
if $needs_forking; then
echo "--- Executing Fork & Clone Logic ---"
# 2. Fork/Sync the Upstream Repository (gh defaults to SSH if configured)
echo "2. Ensuring fork exists and is up-to-date on GitHub..."
if gh repo view "${GH_USERNAME}/${feedstock_repo}" &> /dev/null; then
echo " - Fork already exists. Syncing main branch with upstream..."
gh repo sync --branch "$main_branch" "${GH_USERNAME}/${feedstock_repo}"
else
echo " - Forking ${feedstock_name} to ${GH_USERNAME}..."
gh repo fork "conda-forge/${feedstock_repo}" --clone=false --remote=false
if [ $? -ne 0 ]; then
echo "❌ ERROR: Failed to fork the repository. Check gh authentication/permissions."
return 1
fi
fi
# 3. Clone the new fork
echo "3. Cloning my fork (via SSH) to ${local_dir}..."
if ! gh repo clone "${GH_USERNAME}/${feedstock_repo}" "$local_dir"; then
echo "❌ ERROR: Failed to clone the repository."
return 1
fi
# Add upstream remote using the explicit SSH URL
(
cd "$local_dir" || return 1
echo " - Adding upstream remote (${feedstock_url})..."
# *** ADDING UPSTREAM WITH EXPLICIT SSH URL ***
git remote add upstream "$feedstock_url"
git fetch upstream
)
fi
# ------------------------------------------------------------------
# --- CREATE NEW BRANCH (Final Step) ---
# ------------------------------------------------------------------
if [[ -n "$new_branch_name" ]]; then
if [[ "$new_branch_name" == "new" ]]; then
new_branch_name="patch-$(date +%Y%m%d%H%M%S)"
fi
echo "--- Creating New Branch ---"
echo "Creating new branch: **${new_branch_name}**"
(
cd "$local_dir" || return 1
git checkout "$main_branch"
# Check if the branch already exists locally
if git show-ref --verify --quiet "refs/heads/$new_branch_name"; then
echo " - Branch **${new_branch_name}** already exists locally. Switching to it."
git checkout "$new_branch_name"
else
echo " - Branch **${new_branch_name}** does not exist. Creating new branch."
git checkout -b "$new_branch_name"
fi
)
fi
echo "--- βœ… Setup complete! ---"
echo "You are now in ${local_dir} on the **${new_branch_name}** branch."
echo "Opening in code..."
code "${local_dir}"
}
conda_fork_clone_branch $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment