-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit-co-remote-branch.sh
executable file
·70 lines (65 loc) · 2.3 KB
/
git-co-remote-branch.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/sh
########################################################################
# git-co-remote-branch.sh: Git Remote Branch Checkout Script
#
# Description:
# This script simplifies the process of checking out a remote branch in Git.
# It automatically creates a new local branch that tracks the specified remote branch.
# This is useful for quickly switching to and working on branches that exist
# on the remote repository but not yet locally. It checks for Git installation
# before proceeding.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.3 2025-03-17
# Encapsulated all logic in functions and introduced main function.
# v1.2 2024-01-07
# Updated command existence and execution permission checks
# using a common function for enhanced reliability and maintainability.
# v1.1 2023-12-07
# Added check for Git installation.
# v1.0 2016-01-26
# Initial release. Implements functionality to checkout a remote branch in Git,
# creating a corresponding local branch that tracks it.
#
# Usage:
# Run the script with the name of the remote branch:
# ./git-co-remote-branch.sh <branch>
#
# Example:
# ./git-co-remote-branch.sh feature-branch
# This will create and switch to a local branch called 'feature-branch'
# that tracks 'origin/feature-branch'.
#
########################################################################
# Function to check required commands
check_commands() {
for cmd in "$@"; do
cmd_path=$(command -v "$cmd" 2>/dev/null)
if [ -z "$cmd_path" ]; then
echo "Error: Command '$cmd' is not installed. Please install $cmd and try again." >&2
exit 127
elif [ ! -x "$cmd_path" ]; then
echo "Error: Command '$cmd' is not executable. Please check the permissions." >&2
exit 126
fi
done
}
# Main function to execute the script
main() {
# Check if Git is installed
check_commands git
if [ -n "$1" ]; then
check_git_installed
git checkout -b $1 origin/$1
else
echo "usage: git-co-remote-branch <branch>"
exit 0
fi
}
# Execute main function
main "$@"