-
Notifications
You must be signed in to change notification settings - Fork 6
/
worktree-fix.sh
56 lines (42 loc) · 1.94 KB
/
worktree-fix.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
#!/bin/bash
set -euo pipefail
# Colors for echo commands
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Source and destination paths
SOURCE_PATH="/storage/emulated/0/repos/Obsidian"
DEST_PATH="$HOME/repos/Obsidian"
# Create the destination directory if it doesn't exist
echo -e "${YELLOW}Creating destination directory at $DEST_PATH if it doesn't exist...${NC}"
mkdir -p "$DEST_PATH"
# Move all repositories from source to destination
for repo in "$SOURCE_PATH"/*; do
if [ -d "$repo/.git" ]; then
if [ -f "$repo/.git" ]; then
echo "$repo is a worktree. Skipping."
continue
fi
# Get the name of the repository
repo_name=$(basename "$repo")
# Move the repository
echo -e "${YELLOW}Moving repository $repo_name to $DEST_PATH...${NC}"
mv "$repo" "$DEST_PATH/"
# Change to the repository directory
echo -e "${YELLOW}Changing to the repository directory $DEST_PATH/$repo_name...${NC}"
cd "$DEST_PATH/$repo_name" || exit
# Get the current branch name
current_branch=$(git rev-parse --abbrev-ref HEAD)
git switch -c empty
git add -A
git commit -m "delete" # specifically so code-server doesn't show thousands of changes in the Git tab when opening vscode at /
# Remove all files and directories except the .git directory
echo -e "${YELLOW}Removing all files from the working directory of $repo_name except the .git directory...${NC}"
find . -mindepth 1 \( -not -path "./.git" -a -not -path "./.git/*" \) -exec rm -rf {} +
# Create a worktree for the repo back to the original location, checked out to the default branch
echo -e "${YELLOW}Creating a worktree for $repo_name back to the original location at $SOURCE_PATH/$repo_name, checked out to $current_branch...${NC}"
mkdir -p "$SOURCE_PATH/$repo_name"
git worktree add "$SOURCE_PATH/$repo_name" "$current_branch"
fi
done
echo -e "${GREEN}All repositories have been moved and worktrees created.${NC}"