Skip to content

Commit a525b28

Browse files
author
Bhavi Dhingra
committed
grestore and grestorestaged commands
1 parent 170c5f9 commit a525b28

File tree

6 files changed

+250
-2
lines changed

6 files changed

+250
-2
lines changed

cmd/grestore

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC1091
4+
#shellcheck disable=SC2155
5+
6+
source "$__CUSTOM_GIT_UTIL"/__intend_to_add_untracked_files
7+
source "$__CUSTOM_GIT_UTIL"/__reset_intend_to_add_files
8+
source "$__CUSTOM_GIT_UTIL"/fzf_headers/__grestore_header
9+
source "$__CUSTOM_CONSOLE_UTIL"/__print_info
10+
source "$__CUSTOM_CONSOLE_UTIL"/__print_err
11+
source "$__CUSTOM_CONSOLE_UTIL"/__split_str
12+
13+
function main() {
14+
15+
numUnstagedFiles=$(git status --short | grep "^.[A-Z?] " -c)
16+
if [[ $numUnstagedFiles -eq 0 ]]; then
17+
gstatus
18+
echo
19+
__print_info "No unstaged files."
20+
return
21+
fi
22+
23+
__intend_to_add_untracked_files
24+
25+
if [[ $# -gt 0 ]]; then
26+
git status --short | grep "^.[A-Z?] " | read_and_restore_files
27+
else
28+
local GRESTORE_HEADER="$(__grestore_header)"
29+
git status --short | grep "^.[A-Z?] " |\
30+
fzf -m --color 'fg:160'\
31+
--bind 'q:abort'\
32+
--bind '?:toggle-preview'\
33+
--header "${GRESTORE_HEADER}"\
34+
--preview-window hidden\
35+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/__gdiff_preview; __gdiff_preview {}" |\
36+
read_and_restore_files
37+
fi
38+
39+
__reset_intend_to_add_files
40+
gstatus
41+
}
42+
43+
function read_and_restore_files() {
44+
45+
while read -r str; do
46+
47+
if [[ ${str:0:1} != "R" ]]; then
48+
if [[ ${str:1:1} == " " ]]; then
49+
local file="${str#"${str:0:2}"}"
50+
else
51+
local file="${str#"${str:0:3}"}"
52+
fi
53+
54+
if [[ "${str:0:1}" == "A" ]]; then
55+
prompt_and_remove_file "${file}"
56+
elif [[ "${str:0:1}" == "D" ]]; then
57+
restore_file "${file}"
58+
else
59+
prompt_and_restore_file "${file}"
60+
fi
61+
continue
62+
fi
63+
64+
__split_str "${str}" " -> "
65+
local oldFile="${RESULT_ARR[0]}"
66+
local newFile="${RESULT_ARR[1]}"
67+
68+
if [[ "${oldFile:1:1}" == " " ]]; then
69+
oldFile="${oldFile#"R "}"
70+
restore_file "${oldFile}"
71+
prompt_and_remove_file "${newFile}"
72+
else
73+
prompt_and_restore_file "${newFile}"
74+
fi
75+
done
76+
}
77+
78+
function prompt_and_remove_file() {
79+
gstatus --short
80+
81+
echo
82+
__println_as red "[changes will be lost.. this step can't be undone]"
83+
84+
local rawFile="$(getrawstr "${1}")"
85+
git reset -- "${rawFile}" &> /dev/null
86+
echo "${rawFile}" | xargs -p rm
87+
}
88+
89+
function restore_file() {
90+
gstatus --short
91+
local rawFile="$(getrawstr "${1}")"
92+
git restore "${rawFile}"
93+
}
94+
95+
function prompt_and_restore_file() {
96+
gstatus --short
97+
98+
echo
99+
__println_as red "[changes will be lost.. this step can't be undone]"
100+
101+
local rawFile="$(getrawstr "${1}")"
102+
echo "${rawFile}" | xargs -p git restore
103+
}
104+
105+
if [[ $# -eq 0 ]]; then
106+
main
107+
exit 0
108+
fi
109+
110+
if [[ $# -eq 1 && "$*" == "." ]]; then
111+
main "."
112+
exit 0
113+
fi
114+
115+
__print_err "valid input to grestore: \".\""

cmd/grestorestaged

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC1091
4+
#shellcheck disable=SC2155
5+
6+
source "$__CUSTOM_GIT_UTIL"/__intend_to_add_untracked_files
7+
source "$__CUSTOM_GIT_UTIL"/__reset_intend_to_add_files
8+
source "$__CUSTOM_GIT_UTIL"/fzf_headers/__grestorestaged_header
9+
source "$__CUSTOM_CONSOLE_UTIL"/__print_info
10+
source "$__CUSTOM_CONSOLE_UTIL"/__print_err
11+
source "$__CUSTOM_CONSOLE_UTIL"/__split_str
12+
13+
function main() {
14+
15+
numStagedFiles=$(git status --short | grep "^[A-Z] " -c)
16+
if [[ $numStagedFiles -eq 0 ]]; then
17+
gstatus
18+
echo
19+
__print_info "No staged files."
20+
return
21+
fi
22+
23+
__intend_to_add_untracked_files
24+
25+
if [[ $# -gt 0 ]]; then
26+
git status --short | grep "^[A-Z] " | read_and_restore_staged_files
27+
else
28+
local GRESTORESTAGED_HEADER="$(__grestorestaged_header)"
29+
git status --short | grep "^[A-Z] " |\
30+
fzf -m --color 'fg:64'\
31+
--bind 'q:abort'\
32+
--bind '?:toggle-preview'\
33+
--header "${GRESTORESTAGED_HEADER}"\
34+
--preview-window hidden\
35+
--preview "source $__CUSTOM_GIT_UTIL/fzf_previews/__gdiff_preview; __gdiff_preview {}" |\
36+
read_and_restore_staged_files
37+
fi
38+
39+
__reset_intend_to_add_files
40+
gstatus
41+
}
42+
43+
function read_and_restore_staged_files() {
44+
45+
while read -r str; do
46+
47+
if [[ ${str:0:1} != "R" ]]; then
48+
local file="${str#"${str:0:3}"}"
49+
restore_staged_file "${file}"
50+
continue
51+
fi
52+
53+
__split_str "${str}" " -> "
54+
local oldFile="${RESULT_ARR[0]}"
55+
local newFile="${RESULT_ARR[1]}"
56+
57+
oldFile="${oldFile#"R "}"
58+
restore_staged_file "${oldFile}"
59+
restore_staged_file "${newFile}"
60+
done
61+
}
62+
63+
function restore_staged_file() {
64+
local rawFile="$(getrawstr "${1}")"
65+
git restore --staged "${rawFile}"
66+
}
67+
68+
if [[ $# -eq 0 ]]; then
69+
main
70+
exit 0
71+
fi
72+
73+
if [[ $# -eq 1 && "$*" == "." ]]; then
74+
main "."
75+
exit 0
76+
fi
77+
78+
__print_err "valid input to grestorestaged: \".\""

cmd/gstatus

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#!/usr/bin/env bash
22

33
clear
4-
git status
4+
5+
if [[ $# -eq 0 ]]; then
6+
git status
7+
else
8+
git status "$*"
9+
fi

custom-console-bash

util/fzf_headers/__grestore_header

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC1091
4+
#shellcheck disable=SC2001
5+
#shellcheck disable=SC2155
6+
7+
source "$__CUSTOM_CONSOLE_UTIL"/__common
8+
9+
function __grestore_header() {
10+
11+
local msg="grestore | <tab>: select multiple files, <enter>: restore modified file(s) / remove new files, <?>: toggle preview, <q/esc>: quit"
12+
local colormsg="${UNDERLINE}${MAGENTA}grestore${RESET} | \
13+
${BLUE}<tab>: ${RESET}select multiple files, \
14+
${BLUE}<enter>: ${RESET}restore modified file(s) / remove new files, \
15+
${BLUE}<?>: ${RESET}toggle preview, \
16+
${BLUE}<q/esc>: ${RESET}quit"
17+
18+
local edge="$(echo "${msg}" | sed -e 's/./-/g')"
19+
local GRESTORE_HEADER="${edge}"'
20+
'"${colormsg}"'
21+
'"${edge}"'
22+
'
23+
24+
echo "${GRESTORE_HEADER}"
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC1091
4+
#shellcheck disable=SC2001
5+
#shellcheck disable=SC2155
6+
7+
source "$__CUSTOM_CONSOLE_UTIL"/__common
8+
9+
function __grestorestaged_header() {
10+
11+
local msg="grestorestaged | <tab>: select multiple files, <enter>: restore staged file(s), <?>: toggle preview, <q/esc>: quit"
12+
local colormsg="${UNDERLINE}${MAGENTA}grestorestaged${RESET} | \
13+
${BLUE}<tab>: ${RESET}select multiple files, \
14+
${BLUE}<enter>: ${RESET}restore staged file(s), \
15+
${BLUE}<?>: ${RESET}toggle preview, \
16+
${BLUE}<q/esc>: ${RESET}quit"
17+
18+
local edge="$(echo "${msg}" | sed -e 's/./-/g')"
19+
local GRESTORESTAGED_HEADER="${edge}"'
20+
'"${colormsg}"'
21+
'"${edge}"'
22+
'
23+
24+
echo "${GRESTORESTAGED_HEADER}"
25+
}

0 commit comments

Comments
 (0)