-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-ctx.sh
More file actions
executable file
·74 lines (60 loc) · 2.33 KB
/
git-ctx.sh
File metadata and controls
executable file
·74 lines (60 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/env zsh
HEAD=${1:-HEAD}
CURRENT_BRANCH=$(git current)
# declare associative array descendants
declare -A descendants
function printDescendants() {
for i v in "${(@kv)descendants}"; do
echo "branch: $i"
echo "immediate descendants: $v"
echo
done
return 0
}
function getDescendants() {
branch=$1
local _descendants=($(git-descendants.sh $branch))
# Create an indexed array to store the descendants
local -a descendants_array
for dbranch in $_descendants; do
# Append dbranch to descendants_array
descendants_array+=("$dbranch")
done
# Assign the indexed array to descendants[$branch]
descendants[$branch]=${descendants_array}
return 0
}
getDescendants $CURRENT_BRANCH
# for each descendant of CURRENT_BRANCH, get its descendants and update descendants[CURRENT_BRANCH] to only include immediate descendants
for dbranch in ${descendants[$CURRENT_BRANCH]}; do
# Split the space-separated dbranch into an array
IFS=" " read -rA dbranch_array <<< "$dbranch"
# Iterate over the individual branch names within dbranch
for branch_name in "${dbranch_array[@]}"; do
# echo "branch_name: $branch_name"
getDescendants $branch_name
done
done
# for each key in descendants, we want to remove branches in the value list which are not immediate descendants of key
for i v in "${(@kv)descendants}"; do
local key="$i"
IFS=" " read -rA value <<< "$v"
local -a immediate_descendants=(${value[@]})
# Iterate over the value list
for branch in "${value[@]}"; do
descendants_of_branch=${descendants[$branch]}
# if there are descendants of branch, then each of those descendants are not immediate descendants of key
# for each descendant of branch, remove it from immediate_descendants
if [ -n "$descendants_of_branch" ]; then
IFS=" " read -rA dob <<< "$descendants_of_branch"
for descendant in "${dob[@]}"; do
# echo "descendant: $descendant removed from $key"
immediate_descendants=(${immediate_descendants[@]/$descendant})
done
fi
done
# Update the value list with the immediate descendants
descendants[$key]=${immediate_descendants}
done
# print immediate descendants of CURRENT_BRANCH
echo ${descendants[$CURRENT_BRANCH]}