mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-10-16 19:50:09 +00:00
Compare commits
4 commits
77f8fe20c9
...
0b5e62264e
Author | SHA1 | Date | |
---|---|---|---|
|
0b5e62264e | ||
|
9bcafe1c27 | ||
|
3151c9c1a3 | ||
|
8b95897adb |
5 changed files with 120 additions and 0 deletions
|
@ -57,6 +57,16 @@ function takeurl() {
|
|||
cd "$thedir"
|
||||
}
|
||||
|
||||
function takezip() {
|
||||
local data thedir
|
||||
data="$(mktemp)"
|
||||
curl -L "$1" > "$data"
|
||||
unzip "$data" -d "./"
|
||||
thedir="$(unzip -l "$data" | awk 'NR==4 {print $4}' | sed 's/\/.*//')"
|
||||
rm "$data"
|
||||
cd "$thedir"
|
||||
}
|
||||
|
||||
function takegit() {
|
||||
git clone "$1"
|
||||
cd "$(basename ${1%%.git})"
|
||||
|
@ -65,6 +75,8 @@ function takegit() {
|
|||
function take() {
|
||||
if [[ $1 =~ ^(https?|ftp).*\.(tar\.(gz|bz2|xz)|tgz)$ ]]; then
|
||||
takeurl "$1"
|
||||
elif [[ $1 =~ ^(https?|ftp).*\.(zip)$ ]]; then
|
||||
takezip "$1"
|
||||
elif [[ $1 =~ ^([A-Za-z0-9]\+@|https?|git|ssh|ftps?|rsync).*\.git/?$ ]]; then
|
||||
takegit "$1"
|
||||
else
|
||||
|
|
52
plugins/backupzshrc/README.md
Normal file
52
plugins/backupzshrc/README.md
Normal file
|
@ -0,0 +1,52 @@
|
|||
# Oh My Zsh Backup Plugin
|
||||
|
||||
This plugin provides a convenient way to back up your `~/.zshrc` file locally and to a GitHub repository. It automatically detects changes in the `.zshrc` file, commits them with a timestamped message, and pushes changes to the specified GitHub repository. If you ever rm your zshrc, simply go to your Github repo and check it out.
|
||||
|
||||
## Dependencies
|
||||
1. Locally installed git with a git config for pulling the repo url and username
|
||||
2. A repo in Github https://github.com/yourusername/oh-my-zsh-backup
|
||||
Example: https://github.com/yourusername/oh-my-zsh-backup
|
||||
3. Push privileges configured to the main/master origin branch
|
||||
4. Create directory privileges for this script - create "$HOME/projects/backups" manually otherwise
|
||||
|
||||
## Installation
|
||||
|
||||
1. Clone this repository into your Oh My Zsh custom plugins directory:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/yourusername/oh-my-zsh-backup ~/.oh-my-zsh/custom/plugins/backup
|
||||
```
|
||||
|
||||
2. Add `backup` to the plugins array in your `~/.zshrc` file:
|
||||
|
||||
```bash
|
||||
plugins=(... backup)
|
||||
```
|
||||
|
||||
3. Restart your terminal.
|
||||
|
||||
## Usage
|
||||
|
||||
Simply open your terminal or source your `~/.zshrc` file, and the backup will be triggered automatically. Any changes detected in `~/.zshrc` will be backed up to the specified GitHub repository.
|
||||
|
||||
## Configuration
|
||||
|
||||
### GitHub Username
|
||||
|
||||
If your GitHub username is not already set globally, the plugin will prompt you to enter it the first time you run it. Alternatively, you can set it manually using:
|
||||
|
||||
```bash
|
||||
git config --global user.name "Your Username"
|
||||
```
|
||||
|
||||
### Branch
|
||||
|
||||
By default, the plugin pushes changes to the branch specified GitHub repository is checked out to locally in the "$HOME/projects/backups" backup directory. You can change the target branch by checking out to a new branch in the backup directory.
|
||||
|
||||
## Contributions
|
||||
|
||||
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on GitHub.
|
||||
|
||||
## License
|
||||
|
||||
This plugin is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
54
plugins/backupzshrc/backupzshrc.plugin.zsh
Normal file
54
plugins/backupzshrc/backupzshrc.plugin.zsh
Normal file
|
@ -0,0 +1,54 @@
|
|||
function backup_zshrc() {
|
||||
# Function to back up ~/.zshrc locally and to a GitHub repository
|
||||
# Runs every time zsh is opened or source ~/.zshrc is run
|
||||
# Checks for git changes, will commit and push changes if detected
|
||||
#
|
||||
|
||||
local current_dir=$(pwd)
|
||||
local backup_dir="$HOME/projects/backups"
|
||||
github_username=$(git config --global user.name | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
|
||||
local commit_message="Backup ~/.zshrc at $(date +'%Y_%m_%d-%H%M%S')"
|
||||
|
||||
# Prompt for GitHub username if not set
|
||||
if [[ -z "$github_username" ]]; then
|
||||
read -p "Enter your GitHub username: " github_username
|
||||
fi
|
||||
|
||||
# Create a backup directory if it doesn't exist
|
||||
mkdir -p "$backup_dir"
|
||||
|
||||
cd "$backup_dir" || return
|
||||
|
||||
# Backup ~/.zshrc with a timestamp
|
||||
local timestamp=$(date +'%Y_%m_%d-%H%M%S')
|
||||
local backup_file="$backup_dir/zshrc_backup_$timestamp"
|
||||
cp "$HOME/.zshrc" "$backup_dir/.zshrc"
|
||||
|
||||
changes=$(git diff --quiet --exit-code -- "$backup_dir" || echo "yes")
|
||||
|
||||
if [[ "$changes" == "yes" ]]; then
|
||||
echo "Changes detected in $backup_dir/.zshrc"
|
||||
cp "$HOME/.zshrc" "$backup_file"
|
||||
git add . > /dev/null 2>&1
|
||||
if git commit -m "$commit_message" > /dev/null 2>&1; then
|
||||
if git push > /dev/null 2>&1; then
|
||||
repo_url="https://github.com/$github_username/backups"
|
||||
echo "Backup successfully pushed to GitHub. View it at: $repo_url"
|
||||
else
|
||||
push_error=$(git push 2>&1)
|
||||
echo "Error: Failed to push backup to GitHub. Error details: $push_error"
|
||||
fi
|
||||
else
|
||||
commit_error=$(git commit -m "$commit_message" 2>&1)
|
||||
echo "Error: Failed to commit changes for backup. Error details: $commit_error"
|
||||
fi
|
||||
else
|
||||
echo "No changes detected in $backup_dir for .zshrc"
|
||||
fi
|
||||
|
||||
cd "$backup_dir" || return
|
||||
git add . > /dev/null 2>&1
|
||||
cd "$current_dir" || return
|
||||
}
|
||||
|
||||
backup_zshrc
|
|
@ -73,6 +73,7 @@ plugins=(... git)
|
|||
| `gcans!` | `git commit --verbose --all --signoff --no-edit --amend` |
|
||||
| `gcann!` | `git commit --verbose --all --date=now --no-edit --amend` |
|
||||
| `gc!` | `git commit --verbose --amend` |
|
||||
| `gcn` | `git commit --verbose --no-edit` |
|
||||
| `gcn!` | `git commit --verbose --no-edit --amend` |
|
||||
| `gcs` | `git commit -S` |
|
||||
| `gcss` | `git commit -S -s` |
|
||||
|
|
|
@ -197,6 +197,7 @@ alias gcan!='git commit --verbose --all --no-edit --amend'
|
|||
alias gcans!='git commit --verbose --all --signoff --no-edit --amend'
|
||||
alias gcann!='git commit --verbose --all --date=now --no-edit --amend'
|
||||
alias gc!='git commit --verbose --amend'
|
||||
alias gcn='git commit --verbose --no-edit'
|
||||
alias gcn!='git commit --verbose --no-edit --amend'
|
||||
alias gcf='git config --list'
|
||||
alias gdct='git describe --tags $(git rev-list --tags --max-count=1)'
|
||||
|
|
Loading…
Reference in a new issue