Last active
July 24, 2024 16:59
-
-
Save wallentx/072e522a1d1bc5e09d609723f7b789a5 to your computer and use it in GitHub Desktop.
don't use this bc you won't be able to commit these changes cleanly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
if [[ $OSTYPE == 'darwin'* ]]; then | |
XARGS='gxargs' | |
else | |
XARGS='xargs' | |
fi | |
# Default values | |
REPOS_FILE="" | |
SINGLE_REPO="" | |
FILE_TO_CLONE="" | |
ORG="" | |
CREATE_MISSING_FILE="" | |
DEBUG=false | |
BRANCH_NAME="" | |
# Colors | |
RED=$(tput setaf 1) | |
GREEN=$(tput setaf 2) | |
YELLOW=$(tput setaf 3) | |
BLUE=$(tput setaf 4) | |
RESET=$(tput sgr0) | |
# Function to display usage with colors | |
usage() { | |
echo "${GREEN}onlyclone${RESET} - Clone ONLY a specific file from a single, or multiple GitHub repositories" | |
echo "${GREEN}Usage:${RESET} ${BLUE}onlyclone${RESET} ${YELLOW}[-o org] [-r repo] [-l repos_file] [-f filename] [-c file_to_create] [-b branch_name] [-v]${RESET}" | |
echo " ${BLUE}-o org${RESET} : GitHub organization name (optional)" | |
echo " ${BLUE}-r repo${RESET} : Single repository name or org/repo if -o is not provided" | |
echo " ${BLUE}-l repos_file${RESET} : File containing a list of repository names or org/repo if -o is not provided" | |
echo " ${BLUE}-f filename${RESET} : Filename or path to clone (e.g., .github/CODEOWNERS or path/to/CODEOWNERS)" | |
echo " ${BLUE}-c file_to_create${RESET}: Create the specified file if the filename does not exist" | |
echo " ${BLUE}-b branch_name${RESET} : New branch name to switch to after cloning" | |
echo " ${BLUE}-v${RESET} : Enable verbose/debug output" | |
echo "" | |
exit 1 | |
} | |
# Parse command-line options | |
while getopts "o:r:l:f:c:b:v" OPTION; do | |
case $OPTION in | |
o) | |
ORG=${OPTARG} | |
;; | |
r) | |
SINGLE_REPO=${OPTARG} | |
;; | |
l) | |
REPOS_FILE=${OPTARG} | |
;; | |
f) | |
FILE_TO_CLONE=${OPTARG} | |
;; | |
c) | |
CREATE_MISSING_FILE=${OPTARG} | |
;; | |
b) | |
BRANCH_NAME=${OPTARG} | |
;; | |
v) | |
DEBUG=true | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
# Enable debugging if -v is set | |
if $DEBUG; then | |
set -x | |
fi | |
# Ensure filename is provided and either single repo or repos file is provided | |
if [[ -z "$FILE_TO_CLONE" ]] || ([[ -z "$SINGLE_REPO" ]] && [[ -z "$REPOS_FILE" ]] && [[ -z "$ORG" ]]); then | |
usage | |
fi | |
# Fetch the list of repositories if organization is provided and no single repo or repos file is specified | |
if [[ -n "$ORG" ]] && [[ -z "$SINGLE_REPO" ]] && [[ -z "$REPOS_FILE" ]]; then | |
REPOS=$(GH_PAGER=cat gh repo list -L 100 "$ORG" --topic SRE --json name,isArchived --jq '[.[] | select(.isArchived == false) | .name] | sort[]') | |
else | |
REPOS="" | |
fi | |
# Function to clone a specific file from a single repo | |
clone_specific_file() { | |
local REPO=$1 | |
if [[ -n "$ORG" ]]; then | |
local ORG_REPO="$ORG/$REPO" | |
else | |
local ORG_REPO="$REPO" | |
fi | |
local ORG=$(dirname "$ORG_REPO") | |
local REPO=$(basename "$ORG_REPO") | |
local BRANCH | |
BRANCH=$(gh api /repos/$ORG/$REPO --jq '.default_branch') | |
# Clone the repo with no files checked out | |
git clone -q \ | |
--no-checkout \ | |
--depth=1 \ | |
--filter=blob:none \ | |
--sparse \ | |
--single-branch \ | |
--branch="$BRANCH" \ | |
git@github.com:"$ORG"/"$REPO" onlyClone_"$REPO" &>/dev/null | |
cd onlyClone_"$REPO" | |
# Set sparse checkout and checkout the specified file | |
git sparse-checkout init --cone &>/dev/null | |
git sparse-checkout set "$FILE_TO_CLONE" &>/dev/null | |
if [[ -n "$BRANCH_NAME" ]]; then | |
git switch -q -c "$BRANCH_NAME" | |
fi | |
# Check if the specific file exists and get its full path | |
FILE_CLONE_PATH=$(git ls-tree -r origin/"$BRANCH" --name-only | grep "\.github/$FILE_TO_CLONE" || true) | |
if [[ -n "$FILE_CLONE_PATH" ]]; then | |
git checkout -q origin/"$BRANCH" -- "$FILE_CLONE_PATH" &>/dev/null | |
echo "Cloned ${GREEN}$FILE_TO_CLONE${RESET} from ${YELLOW}$ORG/$REPO${RESET}" | |
else | |
if [[ -n "$CREATE_MISSING_FILE" ]]; then | |
echo "${BLUE}$FILE_TO_CLONE${RESET} does not exist in ${YELLOW}$ORG/$REPO${RESET}" | |
mkdir -p "$(dirname "$CREATE_MISSING_FILE")" | |
touch "$CREATE_MISSING_FILE" | |
echo "${BLUE}└──${RESET}Created file: ${GREEN}$CREATE_MISSING_FILE${RESET}" | |
else | |
echo "${RED}$FILE_TO_CLONE${RESET} does not exist in ${YELLOW}$ORG/$REPO${RESET}" | |
fi | |
fi | |
cd .. | |
} | |
export RED GREEN YELLOW BLUE RESET ORG FILE_TO_CLONE CREATE_MISSING_FILE BRANCH_NAME | |
export -f clone_specific_file | |
# Process single repo | |
if [[ -n "$SINGLE_REPO" ]]; then | |
clone_specific_file "$SINGLE_REPO" | |
fi | |
if [[ -n "$REPOS_FILE" ]]; then | |
cat "$REPOS_FILE" | "$XARGS" -P 20 -I {} bash -c 'clone_specific_file "$@"' _ {} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment