-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Function to check if a directory is a Git repository | ||
is_git_repo() { | ||
git -C "$1" rev-parse --is-inside-work-tree &>/dev/null | ||
} | ||
|
||
find_repo_dirs() { | ||
# 1. Recursively find .git folders. | ||
# 2. Remove the /.git from path. | ||
# 3. Make the path relative. | ||
# 4. Sort paths alphabetically. | ||
find "$(pwd)" -type d -name ".git" -print0 \ | ||
| xargs -0 -I {} dirname {} \ | ||
| xargs -I {} realpath --relative-to="$(pwd)" {} \ | ||
| sort | ||
} | ||
|
||
# Recursively runs git commands on all git repos in a directory | ||
git_recursive() { | ||
mapfile -t repo_dirs < <(find_repo_dirs) | ||
|
||
for repo_dir in "${repo_dirs[@]}"; do | ||
if is_git_repo "$repo_dir"; then | ||
echo "git -C \"$repo_dir\" $*" | ||
git -C "$repo_dir" "$@" | ||
fi | ||
done | ||
} | ||
|
||
# Deletes branches that used to have a matching ref on the remote, but it has | ||
# been deleted. This happens when PRs are merged. | ||
# (Not sure how to share the common code with the above) | ||
git_recursive_delete_merged_branches() { | ||
mapfile -t repo_dirs < <(find_repo_dirs) | ||
|
||
for repo_dir in "${repo_dirs[@]}"; do | ||
if is_git_repo "$repo_dir"; then | ||
echo "git -C \"$repo_dir\" for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == "[gone]" {print \$1}' | xargs -r git -C \"$repo_dir\" branch -D" | ||
git -C "$repo_dir" for-each-ref --format '%(refname:short) %(upstream:track)' | awk '$2 == "[gone]" {print $1}' | xargs -r git -C "$repo_dir" branch -D | ||
fi | ||
done | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
source "$(dirname "$0")/common_git_functions.sh" | ||
git_recursive "$@" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
source "$(dirname "$0")/common_git_functions.sh" | ||
git_recursive_delete_merged_branches |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
source "$(dirname "$0")/common_git_functions.sh" | ||
|
||
git_recursive fetch --all --prune | ||
git_recursive checkout develop | ||
git_recursive reset --hard | ||
git_recursive pull | ||
|
||
git_recursive_delete_merged_branches | ||
|
||
if [ "$1" != "develop" ]; then | ||
git_recursive checkout "$1" | ||
git_recursive reset --hard | ||
git_recursive pull | ||
git_recursive rebase develop | ||
fi |
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