This commit is contained in:
Dobromir Popov
2024-07-16 13:10:33 +03:00
parent 37cdca3719
commit 7568039e16

View File

@ -22,13 +22,23 @@ echo " branch $main_branch"
echo " checkout $main_branch"
# Keep track of declared branches
declare -A declared_branches
declared_branches[$main_branch]=1
declared_branches=("$main_branch")
# Function to check if a branch is declared
is_branch_declared() {
local branch=$1
for declared_branch in "${declared_branches[@]}"; do
if [[ "$declared_branch" == "$branch" ]]; then
return 0
fi
done
return 1
}
# Function to find the base branch
find_base_branch() {
local branch=$1
local base=$(git show-branch -a 2>/dev/null | grep '\*' | grep -v "$branch" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//')
local base=$(git merge-base --fork-point "$branch" "$main_branch" || echo "$main_branch")
if [[ -z "$base" ]]; then
echo "$main_branch"
else
@ -39,9 +49,9 @@ find_base_branch() {
# Function to declare a branch if not already declared
declare_branch() {
local branch=$1
if [[ -z "${declared_branches[$branch]}" ]]; then
if ! is_branch_declared "$branch"; then
echo " branch $(escape_for_mermaid "$branch")"
declared_branches[$branch]=1
declared_branches+=("$branch")
fi
}
@ -50,15 +60,31 @@ for branch in $(git for-each-ref --sort=committerdate --format='%(refname:short)
base_branch=$(find_base_branch "$branch")
declare_branch "$branch"
echo " checkout $(escape_for_mermaid "$branch")"
echo " commit id: \"$(git log -1 --pretty=format:%s "$branch")\""
if [ "$branch" != "$main_branch" ]; then
echo " checkout $(escape_for_mermaid "$base_branch")"
echo " merge $(escape_for_mermaid "$branch")"
# Check which branches are merged into this branch
merged_branches=$(git branch --merged "$branch" | grep -v "\* $branch" | grep -v "$main_branch")
if [ -n "$merged_branches" ]; then
for merged_branch in $merged_branches; do
if [ "$merged_branch" != "$branch" ]; then
declare_branch "$merged_branch"
echo " checkout $(escape_for_mermaid "$branch")"
echo " merge $(escape_for_mermaid "$merged_branch")"
fi
done
fi
done
# Check for branches not yet merged into master
not_merged=$(git branch --no-merged "$main_branch")
if [ -n "$not_merged" ]; then
echo " commit id: \"Branches not yet merged into master:\""
for branch in $not_merged; do
echo " commit id: \"$branch\""
done
fi
} > $output_file
echo "Mermaid diagram saved to $output_file"