Last active
August 8, 2023 13:39
-
-
Save pourmand1376/2f024628a84ec84c4e6f072f4ac6ef65 to your computer and use it in GitHub Desktop.
Sync Obsidian Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get and validate username | |
get_name() { | |
read -p "Enter your name: " name | |
echo "Username: $name" | |
if [ -z "$name" ]; then | |
echo "Name is empty!" | |
exit 1 | |
fi | |
return $name | |
} | |
# Get and validate email | |
get_email() { | |
read -p "Enter your email: " email | |
# Validate email | |
if [[ $email =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$ ]]; then | |
echo "Valid Email: $email" | |
return $email | |
else | |
echo "Invalid email" | |
exit 1 | |
fi | |
} | |
# Get and validate git URL | |
get_giturl() { | |
read -p "Enter Git URL: " git_url | |
# Validate git URL | |
if [[ $git_url == git@* ]] || [[ $git_url == https://* ]]; then | |
echo "Valid Git URL: $git_url" | |
return $git_url | |
else | |
echo "Invalid Git URL" | |
exit 1 | |
fi | |
} | |
install_required_deps() | |
{ | |
apt update | |
apt upgrade -y | |
pkg install openssh -y | |
pkg install git -y | |
} | |
access_storage() | |
{ | |
termux-setup-storage | |
} | |
configure_git() { | |
name="$1" | |
email="$2" | |
git config --global user.name "$name" | |
git config --global user.email "$email" | |
git config --global credential.helper store | |
git config --global pull.rebase false | |
git config --global --add safe.directory '*' | |
git config --global core.protectNTFS false | |
} | |
generate_ssh_key() { | |
email="$1" | |
# Check if key already exists | |
if [ ! -f ~/.ssh/id_ed25519 ]; then | |
# Generate key non-interactively | |
ssh-keygen -q -t ed25519 -N "" -f ~/.ssh/id_ed25519 -C "$email" | |
echo "Generated new SSH key with email $email" | |
else | |
echo "SSH key already exists" | |
fi | |
} | |
init() | |
{ | |
name="$1" | |
email="$2" | |
install_required_deps | |
access_storage | |
configure_git $name $email | |
generate_ssh_key $email | |
} | |
# check if this is a git repository | |
is_git_repo() { | |
if [ -d .git ]; then | |
echo "This is a git repository" | |
return 0 | |
else | |
echo "This is not a git repository" | |
return 1 | |
fi | |
} | |
# add gitignore file | |
add_gitignore_entries() { | |
GITIGNORE=".gitignore" | |
ENTRIES=".trash/ | |
.obsidian/workspace | |
.obsidian/workspace.json | |
.obsidian/workspace-mobile.json | |
.obsidian/app.json" | |
if [ ! -f "$GITIGNORE" ]; then | |
touch "$GITIGNORE" | |
fi | |
for entry in $ENTRIES; do | |
if ! grep -q -Fx "$entry" "$GITIGNORE"; then | |
echo "$entry" >> "$GITIGNORE" | |
fi | |
done | |
} | |
add_gitattributes_entry() { | |
GITATTRIBUTES=".gitattributes" | |
ENTRY="*.md merge=union" | |
if [ ! -f "$GITATTRIBUTES" ]; then | |
touch "$GITATTRIBUTES" | |
fi | |
if ! grep -q -F "$ENTRY" "$GITATTRIBUTES"; then | |
echo "$ENTRY" >> "$GITATTRIBUTES" | |
fi | |
} | |
remove_files_from_git() | |
{ | |
FILES=".obsidian/workspace | |
.obsidian/workspace.json | |
.obsidian/workspace-mobile.json | |
.obsidian/app.json" | |
for file in $FILES; do | |
if [ -f "$file" ]; then | |
git rm --cached "$file" | |
fi | |
done | |
if git status | grep "new file" ; then | |
git commit -am "Remove ignored files" | |
fi | |
} | |
clone_repo() { | |
# Default folder name | |
folder="obsidian" | |
# Check if folder name provided | |
if [ -n "$2" ]; then | |
folder="$2" | |
fi | |
cd ~/ | |
mkdir "$folder" | |
git --git-dir ~/"$folder" --work-dir ~/storage/downloads/"$folder" clone "$1" | |
} | |
sync_obsidian() | |
{ | |
cd "$1" | |
git add . | |
git commit -m "Android Commit" | |
git fetch | |
git merge --no-edit | |
git add . | |
git commit -m "automerge android" | |
git push | |
echo "Sync is finished" | |
sleep 2 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Like to use in wget