Skip to content

Instantly share code, notes, and snippets.

@koraysels
Last active June 18, 2025 19:11
Show Gist options
  • Save koraysels/33c8d1706deaab7e54aa1342d8f51678 to your computer and use it in GitHub Desktop.
Save koraysels/33c8d1706deaab7e54aa1342d8f51678 to your computer and use it in GitHub Desktop.
IP1-repo-checkout-or-update
#!/bin/bash
# Target date and time in Brussels time
TARGET_DATE="2025-05-30T23:59:00+02:00"
# Get the absolute path of the starting directory
SCRIPT_DIR=$(pwd)
echo "Script starting in directory: $SCRIPT_DIR"
# Read the file line by line
while IFS= read -r line; do
echo "---"
echo "Processing line: '$line'"
# Skip completely empty lines
if [ -z "$line" ]; then
echo "Skipping empty line"
continue
fi
# Extract folder name and repo URL, trim whitespace
folder_name=$(echo "$line" | awk '{print $1}' | xargs)
repo_url=$(echo "$line" | awk '{print $2}' | xargs)
echo "Folder name: '$folder_name'"
echo "Repo URL: '$repo_url'"
# Skip if folder name is empty
if [ -z "$folder_name" ]; then
echo "Skipping line with no folder name"
continue
fi
# Check if repo URL is empty or doesn't start with git@
if [ -z "$repo_url" ] || [[ ! "$repo_url" =~ ^git@ ]]; then
empty_folder_name="${folder_name}_EMPTY"
echo "Creating empty folder: $empty_folder_name in $SCRIPT_DIR"
mkdir -p "$SCRIPT_DIR/$empty_folder_name"
continue
fi
# Use subshells to avoid directory pollution
if [ -d "$SCRIPT_DIR/$folder_name/.git" ]; then
echo "Updating existing repo in $folder_name"
(
cd "$SCRIPT_DIR/$folder_name" || exit 1
echo "Working in: $(pwd)"
git fetch --all
# Find the last commit before the target date
commit_hash=$(GIT_COMMITTER_DATE="$TARGET_DATE" git rev-list -1 --before="$TARGET_DATE" origin/main)
if [ -z "$commit_hash" ]; then
echo "No commit found before $TARGET_DATE"
else
echo "Resetting to commit $commit_hash"
git reset --hard "$commit_hash"
fi
)
else
echo "Cloning $repo_url into $SCRIPT_DIR/$folder_name"
(
cd "$SCRIPT_DIR" || exit 1
echo "Cloning from: $(pwd)"
git clone "$repo_url" "$folder_name"
if [ -d "$folder_name" ]; then
cd "$folder_name" || exit 1
echo "Working in cloned repo: $(pwd)"
# Find the last commit before the target date
commit_hash=$(GIT_COMMITTER_DATE="$TARGET_DATE" git rev-list -1 --before="$TARGET_DATE" main)
if [ -z "$commit_hash" ]; then
echo "No commit found before $TARGET_DATE"
else
echo "Checking out commit $commit_hash"
git checkout "$commit_hash"
fi
else
echo "Failed to clone $repo_url"
fi
)
fi
echo "Main script still in: $(pwd)"
done < repos.txt
echo "Script finished in directory: $(pwd)"
2_Pentacode [email protected]:kdg-ti/integratieproject-1/202425/2_pentacode/development.git
3_Codestorm [email protected]:kdg-ti/integratieproject-1/202425/3_codestorm/development.git
10_MergeConflict [email protected]:kdg-ti/integratieproject-1/202425/10_merge-conflict/development.git
14_Team14 [email protected]:kdg-ti/integratieproject-1/202425/14_team-14/development.git
16_MEWTS [email protected]:kdg-ti/integratieproject-1/202425/16_mewts/development.git
17_Team17 [email protected]:kdg-ti/integratieproject-1/202425/17_team-17/development.git
19_YoJoPiLa [email protected]:kdg-ti/integratieproject-1/202425/19_yojopila/development.git
20_CtrlAltElite [email protected]:kdg-ti/integratieproject-1/202425/20_ctrl-alt-elite/development.git
21_CRMH-Solutions [email protected]:kdg-ti/integratieproject-1/202425/21_crmh-solutions/development.git
23_PanelPioniers [email protected]:kdg-ti/integratieproject-1/202425/23_panelpioniers/burgerberaad.git
24_Whimp24 [email protected]:kdg-ti/integratieproject-1/202425/24_whimp24/development.git
25_PanelPioneers [email protected]:kdg-ti/integratieproject-1/202425/25_panel-pioneers/panelproject.git
26_API-Avengers [email protected]:kdg-ti/integratieproject-1/202425/26_apivengers/development.git
Ali_Alpay [email protected]:jaar2/android.git
Atteya_Alessio [email protected]:alessio.atteya/android_project_2025_festivals_performances.git
Azoukane_Wail
Bogaerts_Emma [email protected]:emmabogaerts/ui2-2025-android.git
Bogaerts_Jonas [email protected]:jonas.bogaerts/groeiproject_android.git
Croymans_Yorbe [email protected]:yorbe.croymans/groeiproject-android.git
Cuyt_Mathias
De_Prekel_Sander [email protected]:sanderdeprekel/ui_app.git
@koraysels
Copy link
Author

now it skips blank lines and also when no repo name it crates a folder with EMPTY in the foldername

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment