-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_git_repo
executable file
·60 lines (53 loc) · 1.65 KB
/
find_git_repo
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
#!/bin/bash
find_git_repo(){
# Parameters
ITER=0
# Functions
helpFunction(){
echo ""
echo "Usage: find_git_repo <path>"
echo ""
echo -e " -h/--help \tHelp"
echo -e " <path> \tGive the path as base dir to start searching,"
echo -e " \tabsolute or relative paths are both ok."
exit 1
}
findGitRepo(){
local SEARCH_BASE=$1
for sub in $(ls ${SEARCH_BASE}); do
if (test -d ${SEARCH_BASE}/${sub}) ; then
SUCCESSOR_DIR=$( cd ${SEARCH_BASE}/${sub} && pwd)
if (isGitRepo ${SUCCESSOR_DIR}); then
echo "${SUCCESSOR_DIR} is a git repository."
GIT_REPO_DIR[ITER]=${SUCCESSOR_DIR}
ITER=$[ ${ITER} + 1 ] # ITER=${ITER}+1 is also Okay.
else
# echo "${SUCCESSOR_DIR} is a normal folder."
findGitRepo ${SUCCESSOR_DIR}
fi
fi
done
}
isGitRepo(){
CANDIDATE_DIR=$1
if [ $(ls -al ${CANDIDATE_DIR} | grep "^d" | grep -w ".git" | wc -l) -eq 1 ]; then
return 0 # true
else
return 1 # false
fi
}
main(){
if [ $# -eq 0 ] || [ $# -gt 1 ] || [ $1 = "-h" ] || [ $1 = "--help" ] ; then
helpFunction
else
BASE_DIR=$(cd $1 && pwd)
echo -e "\033[36mcurrent dir is: ${BASE_DIR} \033[0m"
findGitRepo ${BASE_DIR}
export GIT_REPO_DIR
echo "Found Git repositories: ${#GIT_REPO_DIR[*]}"
fi
}
# Entry
main $@
}
find_git_repo $@