Skip to content

Commit 24bed8b

Browse files
author
Bhavi Dhingra
committed
feat(new-command): gswitch switches between local branches
1 parent 13bbf9a commit 24bed8b

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

.gitcommitscopes

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ gbranchcreate
66
gcommit
77
gdiff
88
gshow
9+
gswitch
910
header
1011
init
1112
new-command

cmd/gswitch

+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_local_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_local_branch)"
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_local_branch

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values.
4+
5+
function __select_local_branch() {
6+
7+
local branchName=$(git branch --sort=-committerdate | fzf | sed 's/^. //g')
8+
local rawBranchNameFile="$(getrawstr "${branchName}")"
9+
echo "${rawBranchNameFile}" | xargs -I "{}" echo {} # xargs used to handle "invalid reference" error in git switch command
10+
}

0 commit comments

Comments
 (0)