Skip to content

Commit e6b6567

Browse files
committed
feat(Script): add git_archiver.sh
0 parents  commit e6b6567

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

.gitignore

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
2+
# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,macos
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,macos
4+
5+
### macOS ###
6+
# General
7+
.DS_Store
8+
.AppleDouble
9+
.LSOverride
10+
11+
# Icon must end with two \r
12+
Icon
13+
14+
15+
# Thumbnails
16+
._*
17+
18+
# Files that might appear in the root of a volume
19+
.DocumentRevisions-V100
20+
.fseventsd
21+
.Spotlight-V100
22+
.TemporaryItems
23+
.Trashes
24+
.VolumeIcon.icns
25+
.com.apple.timemachine.donotpresent
26+
27+
# Directories potentially created on remote AFP share
28+
.AppleDB
29+
.AppleDesktop
30+
Network Trash Folder
31+
Temporary Items
32+
.apdisk
33+
34+
### macOS Patch ###
35+
# iCloud generated files
36+
*.icloud
37+
38+
### VisualStudioCode ###
39+
.vscode/*
40+
!.vscode/settings.json
41+
!.vscode/tasks.json
42+
!.vscode/launch.json
43+
!.vscode/extensions.json
44+
!.vscode/*.code-snippets
45+
46+
# Local History for Visual Studio Code
47+
.history/
48+
49+
# Built Visual Studio Code Extensions
50+
*.vsix
51+
52+
### VisualStudioCode Patch ###
53+
# Ignore all local history of files
54+
.history
55+
.ionide
56+
57+
### Windows ###
58+
# Windows thumbnail cache files
59+
Thumbs.db
60+
Thumbs.db:encryptable
61+
ehthumbs.db
62+
ehthumbs_vista.db
63+
64+
# Dump file
65+
*.stackdump
66+
67+
# Folder config file
68+
[Dd]esktop.ini
69+
70+
# Recycle Bin used on file shares
71+
$RECYCLE.BIN/
72+
73+
# Windows Installer files
74+
*.cab
75+
*.msi
76+
*.msix
77+
*.msm
78+
*.msp
79+
80+
# Windows shortcuts
81+
*.lnk
82+
83+
# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,macos
84+
85+
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
86+

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Git Archiver Script
2+
Shell script on MacOS to create a complete archive of a git repository with all submodules.
3+
4+
# Features
5+
- [x] Write log file for debug
6+
- [x] Initialize submodules before running
7+
- [x] Archive main repository and all submodules and nested submodules
8+
- [x] Export to zip file `release.zip`
9+
- [x] Not include all files and folders from your `.gitignore`
10+
- [x] Open root folder after running
11+
12+
# Usage
13+
14+
- Place the script in your root project directory
15+
- Run the command `sh git_archiver.sh` or `sh git_archiver.sh true` to initialize submodules
16+
- Get your archive as a `release.zip` file in the root directory
17+

git_archiver.sh

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# Color codes
7+
RED=$(tput setaf 1)
8+
GREEN=$(tput setaf 2)
9+
RESET=$(tput sgr0)
10+
11+
# Log file path
12+
LOG_FILE="archive.log"
13+
14+
# # Enable the line below to write output/error to the log file.
15+
exec > "$LOG_FILE" 2>&1
16+
17+
echo "Starting the archive process at $(date)"
18+
19+
# [UPDATE_SUBMODULE] is used to update submodule or not. Default is "false".
20+
UPDATE_SUBMODULE=$1
21+
22+
if [ -z "$UPDATE_SUBMODULE" ]; then
23+
UPDATE_SUBMODULE="false"
24+
fi
25+
26+
if [[ "${UPDATE_SUBMODULE}" == "true" ]]; then
27+
# Ensure all submodules are updated recursively
28+
echo "Updating submodules recursively..."
29+
git submodule update --init --recursive
30+
31+
echo "Updated submodules successfully."
32+
fi
33+
34+
# Create a temporary directory for the archive
35+
TEMP_DIR=$(mktemp -d)
36+
echo "Created temporary directory: $TEMP_DIR"
37+
38+
# Archive the main repository
39+
echo "Archiving main repository..."
40+
git archive HEAD | tar -x -C "$TEMP_DIR"
41+
echo "Main repository archived successfully."
42+
43+
# Archive submodules and their nested submodules
44+
echo "Archiving submodules and nested submodules..."
45+
git submodule foreach '
46+
echo "Archiving submodule: $sm_path"
47+
48+
# Create the submodule directory structure inside the temporary folder
49+
mkdir -p '"$TEMP_DIR"'/$sm_path
50+
51+
# Archive the submodule into the appropriate folder in the temporary directory
52+
(cd $toplevel/$sm_path && git archive HEAD) | tar -x -C '"$TEMP_DIR"'/$sm_path
53+
54+
# Check if this submodule has nested submodules and archive them as well
55+
if [ -f "$toplevel/$sm_path/.gitmodules" ]; then
56+
echo "Found nested submodules in $sm_path, archiving them..."
57+
58+
SUBMODULE='"$TEMP_DIR"'/$sm_path
59+
60+
# (cd "$toplevel/$sm_path" && git submodule update --init --recursive)
61+
(cd "$toplevel/$sm_path" && git submodule foreach "
62+
echo \"Archiving nested submodule: \$sm_path\";
63+
mkdir -p "$SUBMODULE"/\$sm_path;
64+
(cd \$toplevel/\$sm_path && git archive HEAD) | tar -x -C "$SUBMODULE"/\$sm_path;
65+
")
66+
fi
67+
'
68+
69+
# Zip the entire content of the temporary directory
70+
echo "Creating release.zip in the current directory..."
71+
cd "$TEMP_DIR" || exit 1
72+
zip -r "$OLDPWD/release.zip" . 2>/dev/null # Suppress permission warnings
73+
echo "Zip archive created: release.zip."
74+
open $OLDPWD
75+
76+
# Clean up
77+
echo "Cleaning up temporary files..."
78+
cd ..
79+
rm -rf "$TEMP_DIR"
80+
81+
echo "${GREEN}✅ Archive release.zip created successfully.${RESET}"
82+
echo "${GREEN}Archive process completed at $(date)${RESET}"

0 commit comments

Comments
 (0)