#!/bin/bash checkout-git-folder() { if [[ -z "$1" ]]; then echo '$1/branch is empty.' exit 1 fi if [[ -z "$2" ]]; then echo '$2/path is empty.' exit 1 fi local git_branch="$1" local base_path="$2" local git_folders=$(ls-git-folder "$base_path") for git_folder in $git_folders; do git_folder="${git_folder#./}" local commit_ref=$(git config -f .gitmodules --get submodule."$git_folder".commit) if [[ -z "$commit_ref" ]]; then echo "No commit found in .gitmodules for submodule: $git_folder" >&2 continue fi local shallow=$(git config --file .gitmodules --get submodule."$git_folder".shallow) if [[ "$shallow" = "true" ]]; then git -C "$git_folder" stash >/dev/null 2>&1 || true git -C "$git_folder" fetch origin "$git_branch:$git_branch" >/dev/null 2>&1 git -C "$git_folder" checkout "$commit_ref" >/dev/null 2>&1 else git -C "$git_folder" stash >/dev/null 2>&1 || true git -C "$git_folder" checkout "$commit_ref" >/dev/null 2>&1 fi done } cgf() { checkout-git-folder "$@" }