-
Notifications
You must be signed in to change notification settings - Fork 1
/
dlgitdocs.sh
118 lines (93 loc) · 4.37 KB
/
dlgitdocs.sh
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# export GIT_DISCOVERY_ACROSS_FILESYSTEM=0
# Array of GitHub repository URLs (replace with your actual repo URLs)
REPOS=(
)
SPARSED_REPOS=(
## Insert links per line here, enclosed in ""
# "http://github.com/example/project1.git"
# "http://github.com/example/project2.git"
)
# Array of GitHub safe directories paths for multi-user directories(replace with your actual directories)
# SAFE_DIRS=(
## Insert links per line here, enclosed in ""
# # "/mnt/example/data/docs/gitdocs"
# )
timestamp() {
date +"%T" # current time
}
GITDOCS_DIR=$PWD/gitdocs
LOGFILE=$GITDOCS_DIR/.log
echo -e "$(timestamp) ----------DLGitDocs---------\n" > $LOGFILE
printf "\nCreating and changing directory to $GITDOCS_DIR\n"
echo -e "$(timestamp) Creating and changing directory to $GITDOCS_DIR\n" >> $LOGFILE
mkdir -p $GITDOCS_DIR
cd $GITDOCS_DIR
git init
# Loop through each repository URL
printf "\nLooping through each repository URL\n"
echo -e "$(timestamp) Looping through each repository URL\n" >> $LOGFILE
for repo in "${REPOS[@]}"; do
# Extract the repository name from the URL
repo_name=$(basename "$repo" .git)
printf "\nAdding git safe directories\n"
echo -e "$(timestamp) Adding git safe directories\n" >> $LOGFILE
for safe_dir in "${SAFE_DIRS[@]}"; do
git config --global --add safe.directory $safe_dir/$repo_name
printf "\nGit safe directory added at $safe_dir/$repo_name\n"
echo -e "$(timestamp) Git safe directory added at $safe_dir/$repo_name\n" >> $LOGFILE
done
# Clone the repository as a submodule within the directory
printf "\nClone the repository as a submodule within the directory and changing directory to $repo_name\n"
echo -e "$(timestamp) Clone the repository as a submodule within the directory and changing directory to $repo_name\n" >> $LOGFILE
git clone --depth 1 "$repo" $GITDOCS_DIR/$repo_name
# Update project's .gitmodules to register the added submodules
# (This assumes you ran this script within your main Git project)
printf "\nUpdate project's .gitmodules to register the added submodules\n"
echo -e "$(timestamp) Update project's .gitmodules to register the added submodules\n" >> $LOGFILE
echo -e "[submodule \"$repo_name\"]
path = ./$repo_name
url = $repo\n" > .gitmodules
printf "\nEcho'd Submodule\n"
echo -e "$(timestamp) Echo'd Submodule\n" >> $LOGFILE
done
cd $GITDOCS_DIR
# Loop through each repository URL
printf "\nLooping through each SPARSED repository URL\n"
echo -e "$(timestamp) Looping through each repository URL\n" >> $LOGFILE
for repo in "${SPARSED_REPOS[@]}"; do
# Extract the repository name from the URL
repo_name=$(basename "$repo" .git)
printf "\nAdding git safe directories\n"
echo -e "$(timestamp) Adding git safe directories\n" >> $LOGFILE
for safe_dir in "${SAFE_DIRS[@]}"; do
git config --global --add safe.directory $safe_dir/$repo_name
printf "\nGit safe directory added at $safe_dir/$repo_name\n"
echo -e "$(timestamp) Git safe directory added at $safe_dir/$repo_name\n" >> $LOGFILE
done
# Clone the repository as a submodule within the directory
printf "\nClone the repository as a submodule within the directory and changing directory to $repo_name\n"
echo -e "$(timestamp) Clone the repository as a submodule within the directory and changing directory to $repo_name\n" >> $LOGFILE
git clone --no-checkout --depth 1 "$repo" $GITDOCS_DIR/$repo_name
# Change to Repo directory and sparse checkout
printf "\nSparsing Checkout and setting HEAD\n"
echo -e "$(timestamp) Sparsing Checkout and setting HEAD\n" >> $LOGFILE
cd $repo_name
git sparse-checkout set --no-cone '!/*' '/docs' '/Docs' '/doc' '/DOCS' '/Doc' '/DOC' '/documents' '/Documents' '/*.md' '/tutorials' '/examples' '/content' '/guides' '/*.txt' '/*.sh'
git read-tree -mu HEAD
# Return to the parent directory
cd ..
# Update project's .gitmodules to register the added submodules
# (This assumes you ran this script within your main Git project)
printf "\nUpdate project's .gitmodules to register the added submodules\n"
echo -e "$(timestamp) Update project's .gitmodules to register the added submodules\n" >> $LOGFILE
echo -e "[submodule \"$repo_name\"]
path = ./$repo_name
url = $repo\n" >> .gitmodules
printf "\nEcho'd Submodule\n"
echo -e "$(timestamp) Echo'd Submodule\n" >> $LOGFILE
done
# Initialize and update all submodules
git submodule init
git submodule update
git sparse-checkout disable