62 lines
1.7 KiB
Bash
Executable File
62 lines
1.7 KiB
Bash
Executable File
#!/bin/bash
|
||
# ---------------------------------------------------------------------------
|
||
# gpull – Pull changes from a remote Git repository
|
||
#
|
||
# Usage:
|
||
# ./gpull [repo] [remote] [branch]
|
||
#
|
||
# repo – (optional) Directory of repository to pull, default is current
|
||
directory.
|
||
# remote – (optional) Remote name, default is origin.
|
||
# branch – (optional) Branch name, default is current branch.
|
||
#
|
||
# The script will pull the specific remote branch into the local repository.
|
||
# ---------------------------------------------------------------------------
|
||
|
||
# Function to handle errors gracefully without closing terminal
|
||
handle_error() {
|
||
echo "Error: $1"
|
||
return 1
|
||
}
|
||
|
||
# Get command line arguments
|
||
repo_dir=${1:-$(pwd)}
|
||
remote=${2:-origin}
|
||
branch=${3:-}
|
||
|
||
# Change to the specified directory
|
||
if [ ! -d "$repo_dir" ]; then
|
||
handle_error "Directory '$repo_dir' does not exist."
|
||
exit 1
|
||
fi
|
||
|
||
cd "$repo_dir" || { handle_error "Failed to change to directory '$repo_dir'."; exit 1; }
|
||
|
||
# Check if the current directory is a git repository
|
||
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
|
||
handle_error "'$repo_dir' is not a git repository."
|
||
exit 1
|
||
fi
|
||
|
||
# If no branch specified, get the current branch
|
||
if [ -z "$branch" ]; then
|
||
branch=$(git rev-parse --abbrev-ref HEAD)
|
||
if [ "$branch" = "HEAD" ]; then
|
||
handle_error "Not currently on a branch. Please specify a branch to pull."
|
||
exit 1
|
||
fi
|
||
fi
|
||
|
||
echo "Pulling changes from $remote/$branch into $repo_dir"
|
||
|
||
# Perform the git pull
|
||
git pull "$remote" "$branch"
|
||
|
||
# Check if the pull was successful
|
||
if [ $? -eq 0 ]; then
|
||
echo "Successfully pulled changes from $remote/$branch"
|
||
else
|
||
handle_error "Failed to pull changes from $remote/$branch"
|
||
exit 1
|
||
fi
|