Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/andreijiroh-dev/cs50x-monorepo. The monorepo for all things CS50x in one place. Used locally on his cs50.harvard.edu/x
cs50x cs50x2025
Something went wrong. Try again.
3.6 kB · 101 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102#!/usr/bin/env bash# subtree50 - Shell script for managing subtrees on CS50-related repositories via an monorepo
set -o pipefail
if [[ $DEBUG != "" ]]; then set -xfi
error() { echo "Error: $1" >&2 exit 1}
warn() { echo "Warning: $1" >&2}
info() { echo "Info: $1"}
debug() { [[ $DEBUG != "" ]] && echo "Debug: $1"}
try_codespaces_loader() { if [[ $CODESPACES == "true" ]]; then CS50_UID="${RepositoryName}" CS50_USER="${GITHUB_USER}" fi}
try_dotenvx_loader() { if command -v dotenvx &> /dev/null; then if [[ $CODESPACES != "true" ]] && [[ -f "$PWD/.env.keys" ]]; then eval "$(dotenvx get --format eval)" else warn ".env.keys not found in workspace root or running in GitHub Codespaces" fi else warn "dotenvx is not installed, try again by running 'npm i' in this workspace or via https://cs50.dev/open" warn "ignore this warning if you are inside the codespace" fi}
help() { echo "Usage: $0 <git-subtree command> <codespace|me50|profile>" echo echo "Supports most git-subtree commands (push, pull, merge) for the following subtrees" echo echo "Additional commands:" echo " check-remotes - check if the remote repositories are set up correctly" echo " help - Show this help message" exit 0}
# load configs from .env firsttry_dotenvx_loadertry_codespaces_loader
CODE50_REPO="https://${CS50_USER}:${CS50_TOKEN}@github.com/code50/${CS50_UID}.git"ME50_REPO="https://${CS50_USER}:${CS50_TOKEN}@github.com/me50/${CS50_USER}.git"PROFILE_REPO="https://${CS50_USER}:${CS50_TOKEN}@github.com/${CS50_USER}/${CS50_USER}.git"
if [[ $1 == "" || $1 == "--help" || $1 == "-h" || "$1" == "help" ]]; then helpelif [[ $1 == "push" || $1 == "pull" || $1 == "merge" || $1 == "add" || $1 == "merge" || "$1" == "spilt" ]]; then if [ -z "${CS50_TOKEN}" ]; then error "missing dedicated GitHub token for CS50x-specific account, see warnings above or try again in https://cs50.dev/open" fi if [[ $2 == "codespace" ]]; then git subtree "$1" --prefix=codespace code50 main || error "Failed to $1 code50 subtree" elif [[ $2 == "me50" ]]; then git subtree "$1" --prefix=submissions me50 main || error "Failed to $1 me50 subtree" elif [[ $2 == "profile" ]]; then git subtree "$1" --prefix=profile profile main || error "Failed to $1 profile subtree" else error "Invalid argument: $2. Use 'codespace', 'me50', or 'profile'." fielif [[ $1 == "check-remotes" ]]; then for repo in code50 me50 profile; do repo_upper=$(echo "$repo" | tr '[:lower:]' '[:upper:]') repo_var="${repo_upper}_REPO" repo_url="${!repo_var}"
debug "current url for remote '${repo}': $(git remote get-url ${repo} 2>/dev/null || echo "not set")" if [[ -z "$(git remote get-url ${repo} 2>/dev/null)" ]]; then warn "Remote repository '${repo}' is not set up locally, attempting to set it up now..." git remote add "${repo}" "${repo_url}" || error "Failed to add remote repository '${repo}'" else if [[ "$(git remote get-url ${repo})" != "${repo_url}" ]]; then warn "Remote repository '${repo}' is set up but does not match the expected URL, updating it now..." git remote set-url "${repo}" "${repo_url}" || error "Failed to update remote repository '${repo}'" else info "Remote repository '${repo}' is set up correctly" fi fi done exit 0fi