Snippets

Radistao A Nested git repositories scripts: BASH

Created by Radistao A last modified
#!/bin/bash

# display current branch of every git repo in curent directory

GIT_REPOS=()
MAX_LEN=0
for dir in ./*/
do
    dir=${dir%*/}
    dir=${dir##*/}
    if [ -d $dir/.git ]; then
        GIT_REPOS+=($dir)
        if [ ${#dir} -gt $MAX_LEN ]; then MAX_LEN=${#dir}; fi
    fi
done

PADDING=$(($MAX_LEN + 5))

for dir in ${GIT_REPOS[@]}
do
    printf "%-${PADDING}s %s\n" "$dir" "$(git -C $dir rev-parse --abbrev-ref HEAD)"
done

#!/bin/bash

# usage:
# by default prints messages since $SINCE of author matching $AUTHOR pattern.
# you can override these values from command line:
# first argument would be interpret like "$1 days ago"
# second one (if specified) - as an author's name pattern

AUTHOR="kovalenko"
SINCE="13 days ago"

if [ "$1" -gt 0 ]; then
    SINCE="$1 days ago"
fi

if [ -n "$2" ]; then
    AUTHOR="$2"
fi

SHOW_MESSAGE="Showing changes since \"$SINCE\" of author \"$AUTHOR\""

printf "\n$SHOW_MESSAGE\n\n"

for dir in ./*/
do
    dir=${dir%*/}
    dir=${dir##*/}
    if [ -d $dir/.git ]; then
        echo $dir;  git --no-pager -C $dir hist \
                                      --pretty=format:'%C(auto)%h%C(reset) %C(bold green)%ad%C(reset) | %C(white)%s%C(reset)%C(auto)%d%C(reset) [%C(bold blue)%an%C(reset)]' \
				      --date=format:'%Y-%m-%d %H:%M' \
				      --color=always \
                                      --graph \
				      --decorate \
				      --since="$SINCE" \
				      --regexp-ignore-case \
				      --author="$AUTHOR" \
				      --grep="$COMMIT_MSG" 
        printf "\n\n\n"
    fi
done

printf "$SHOW_MESSAGE\n\n"
#!/bin/bash

# Perform git pull in all git repositories in current directory.
# If some chanes are not commited in a repo - they will be stashed, including untracked and indexed (cached) files.
# After pull is done, the script attempts to pop the stash. In case of error it will be printed.
# At the end of the script list of failded repos (if any) will be printed

# list of repos which were updated with errors
FAILED_REPOS=()

ROOT_DIR=$(pwd -P)

# console colors, see http://stackoverflow.com/questions/4332478/read-the-current-text-color-in-a-xterm/4332530#4332530
NORMAL=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)


for dir in ./*/
do
    # get pure repo directory name
    dir=${dir%*/}
    dir=${dir##*/}
    if [ -d $dir/.git ]; then
        pushd $dir > /dev/null
        printf "\nUpdating repo ${YELLOW}${dir}${NORMAL}\n"
        # I use "status --porcelain" instead of "diff --exit-code" because the first one tracks everything:
        # changed, untracked, indexed files
        if [ -n "$(git status --porcelain)" ]; then 
            echo "Changes detected - stash them"
            DATE=`date +"%Y-%m-%d %H:%M:%S"`
            BRANCH=`git rev-parse --abbrev-ref HEAD`
            STASHED=true
            git stash save --include-untracked --no-keep-index "Before pull $DATE in branch $BRANCH"
        else
            STASHED=false
        fi

        git pull

        if [ $? -ne 0 ]; then
            printf "\n{$RED}Pull error in repo ${YELLOW}$dir${NORMAL}\n"
            FAILED_REPOS+=($dir);
        fi

        if [ "$STASHED" = true  ]; then
            echo "Try to pop stashed changes back"
            git stash pop --index
            if [ $? -ne 0 ]; then
                printf "\n${RED}!!! Stash popping failded: apply the stash manually and then drop it !!!${NORMAL}\n"
                FAILED_REPOS+=($dir)
            fi
        fi

        printf "\n"
        popd > /dev/null
    fi
done

cd $ROOT_DIR > /dev/null


if [ ${#FAILED_REPOS[@]} -eq 0 ]; then
printf "\n${GREEN}All updates done successfully${NORMAL}\n"
else
    printf "\n${RED}!!!Some repos updating error:\n"
    printf '%s\n' "${FAILED_REPOS[@]}"
    printf "${NORMAL}"
fi
#!/bin/bash

# display stashes for every git repo in curent directory

for dir in ./*/
do
    dir=${dir%*/}
    dir=${dir##*/}
    if [ -d $dir/.git ]; then
        echo $dir; git --no-pager -C $dir stash list
	printf "\n"
    fi
done
#!/bin/bash

# display status of every git repo in curent directory

for dir in ./*/
do
    dir=${dir%*/}
    dir=${dir##*/}
    if [ -d $dir/.git ]; then
        echo $dir; git -C $dir status
	printf "\n"
    fi
done

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.