Skip to content

Commit c3ed480

Browse files
author
Bhavi Dhingra
committed
feat(new-command): gswitchremote helps to switch to a remote branch
1 parent 9e6398c commit c3ed480

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

cmd/gswitchremote

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
#shellcheck disable=SC1091 # Not following: ./__assertgitrepo was not specified as input
4+
#shellcheck disable=SC2086 # Double quote to prevent globbing and word splitting
5+
#shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values
6+
7+
# ensure the command is run in a git repo
8+
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
9+
source "${__CUSTOM_GIT_UTIL}"/__select_remote_branch
10+
source "${__CUSTOM_CONSOLE_UTIL}"/__exit_if_empty_str
11+
source "${__CUSTOM_CONSOLE_UTIL}"/__common
12+
13+
main () {
14+
15+
__assertgitrepo
16+
17+
local branchName="$(__select_remote_branch "gswitchremote" "select remote branch to switch to" "exit")"
18+
__exit_if_empty_str "${branchName}" ${SUCCESS}
19+
20+
clear
21+
git switch "${branchName}"
22+
}
23+
24+
if [[ $# -gt 0 ]]; then
25+
# branch name is provided in command line arguments
26+
git switch "$*"
27+
exit ${SUCCESS}
28+
fi
29+
30+
main

util/__select_remote_branch

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values.
4+
#shellcheck disable=SC1091 # Not following: ./fzf_headers/__select_branch_header was not specified as input (see shellcheck -x)
5+
6+
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/__select_branch_header
7+
8+
# Usage: __select_remote_branch "gswitchremote" "select remote branch to switch to" "exit"
9+
function __select_remote_branch() {
10+
11+
local cmd="${1}"
12+
local cmdMsg="${2}"
13+
local escAction="${3}"
14+
local HEADER="$(__select_branch_header "${cmd}" "${cmdMsg}" "${escAction}")"
15+
local branchName="$(git branch -r --sort=-committerdate |\
16+
grep -v "\->" |\
17+
sed 's/^ origin\///g' |\
18+
fzf --height=65%\
19+
--header "${HEADER}")"
20+
21+
local rawBranchNameFile="$(getrawstr "${branchName}")"
22+
echo "${rawBranchNameFile}" | xargs -I "{}" echo {} # xargs used to handle "invalid reference" for special characters
23+
# like ', '#' in branch names
24+
}

0 commit comments

Comments
 (0)