Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -g for git status #57

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ overriding variables set in `~/.cdcrc`. There's also a debug mode.
|-C|Disable colored output.|
|-l|List all directories to which you can `cdc`. Same as tab-completion.|
|-L|List the directories in which `cdc` will search.|
|-g|Cycle through all repositories and print the `git status`.|
|-i|List the directories that are to be ignored.|
|-d|List directories in history stack. Similar to the `dirs` command.|
|-n|`cd` to the current directory in the history stack.|
Expand Down
3 changes: 2 additions & 1 deletion cdc.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ _cdc() {
-h"[Print this help.]" \
- no_other_args \
-n"[cd to the current directory in the stack.]" \
-p"[cd to previous directory and pop from the stack]" \
-p"[cd to previous directory and pop from the stack.]" \
-g"[Cycle through all repositories and print git status.]" \
-t"[Toggle between the last two directories in the stack.]" \
-i"[List all directories that are to be ignored.]" \
-l"[List all directories that are cdc-able.]" \
Expand Down
41 changes: 39 additions & 2 deletions cdc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ cdc() {
local dir
local list
local directory
local subdir
local wdir
local marker
local cdc_last_element
local cdc_next_to_last_element
local cdc_history
local rc=0
local dirty=()
local cdc_list_dirs=false
local cdc_list_searched_dirs=false
local cdc_toggle=false
Expand All @@ -33,6 +35,7 @@ cdc() {
local which=false
local cdc_current=false
local cdc_pop=false
local cdc_git_status=false
local cdc_show_history=false
local cdc_list_ignored=false
local print_help=false
Expand All @@ -55,7 +58,7 @@ cdc() {
# If argument contains a slash, it's assumed to contain subdirectories.
# This splits them into the directory root and its subdirectories.
if [[ "$1" == */* ]]; then
local subdir="${1#*/}"
subdir="${1#*/}"
fi

##
Expand All @@ -68,7 +71,7 @@ cdc() {

##
# Case options if present. Suppress errors because we'll supply our own.
while getopts 'acCdDhilLnprRstuUw' opt 2>/dev/null; do
while getopts 'acCdDghilLnprRstuUw' opt 2>/dev/null; do
case $opt in

##
Expand All @@ -83,6 +86,10 @@ cdc() {
# -C: Disable color.
C) use_color=false ;;

##
# -g: Display git status for each repo.
g) cdc_git_status=true ;;

##
# -n: cd to the root of the current repository in the stack.
n) cdc_current=true ;;
Expand Down Expand Up @@ -236,6 +243,8 @@ cdc() {
echo ' | List all directories that are to be ignored.'
printf " ${CDC_WARNING_COLOR}-d${CDC_RESET}"
echo ' | List the directories in stack.'
printf " ${CDC_WARNING_COLOR}-g${CDC_RESET}"
echo ' | Cycle through all repositories and print git status.'
printf " ${CDC_WARNING_COLOR}-n${CDC_RESET}"
echo ' | `cd` to the current directory in the stack.'
printf " ${CDC_WARNING_COLOR}-p${CDC_RESET}"
Expand All @@ -262,6 +271,34 @@ cdc() {
return 0
fi

if $cdc_git_status; then
if $debug; then
_cdc_print 'success' 'Showing git status' $debug
fi
dir="$PWD"
for directory in "${CDC_DIRS[@]}"; do
[[ -d $directory ]] || continue
pushd "$directory" >/dev/null
for subdir in */; do
if ! $allow_ignored && _cdc_is_excluded_dir "$subdir"; then
continue
fi
[[ -d $subdir/.git ]] || continue
pushd "$subdir" >/dev/null
if [[ $( git diff --shortstat 2>/dev/null | tail -n1 ) != "" ]]; then
dirty+=("$subdir")
fi
popd >/dev/null
done
popd >/dev/null
done
if (( ${#dirty} > 0 )); then
echo "The following repositories have changes."
printf "%s\n" "${dirty[@]}"
fi
should_return=true
fi

if $cdc_list_searched_dirs; then
if $debug; then
_cdc_print 'success' 'Listing searched directories.' $debug
Expand Down