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 a function to clean and update local repositories #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions home/dot_bash_aliases.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ cdw() {
cd "${win_home}"
}
# {{ end }}

git-reset-all-repos() {
local exclude_dir=("$@")
./home/dot_local/bin/executable_clean-local-repos.sh "${exclude_dir[@]}"
}
119 changes: 119 additions & 0 deletions home/dot_local/bin/executable_clean-local-repos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash
# ARG_OPTIONAL_SINGLE([repos_dir],[r],[Directory containing repositories],[.])
# ARG_OPTIONAL_REPEATED([exclude],[e],[Directories to exclude])
# ARG_HELP([Script to clean local git repositories])
# ARG_DEFAULTS_POS([])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info


die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}


begins_with_short_option()
{
local first_option all_short_options='reh'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}

# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_repos_dir="."
_arg_exclude=()


print_help()
{
printf '%s\n' "Script to clean local git repositories"
printf 'Usage: %s [-r|--repos_dir <arg>] [-e|--exclude <arg>] [-h|--help]\n' "$0"
printf '\t%s\n' "-r, --repos_dir: Directory containing repositories (default: '.')"
printf '\t%s\n' "-e, --exclude: Directories to exclude (empty by default)"
printf '\t%s\n' "-h, --help: Prints help"
}


parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
-r|--repos_dir)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_repos_dir="$2"
shift
;;
--repos_dir=*)
_arg_repos_dir="${_key##--repos_dir=}"
;;
-r*)
_arg_repos_dir="${_key##-r}"
;;
-e|--exclude)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_exclude+=("$2")
shift
;;
--exclude=*)
_arg_exclude+=("${_key##--exclude=}")
;;
-e*)
_arg_exclude+=("${_key##-e}")
;;
-h|--help)
print_help
exit 0
;;
-h*)
print_help
exit 0
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}

parse_commandline "$@"

# OTHER STUFF GENERATED BY Argbash

### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash


set -eu

repos_dir="${_arg_repos_dir}"
exclude=("${_arg_exclude[@]}")

folders=$(find "${repos_dir}" -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)
if [[ ${#exclude[@]} -ne 0 ]]; then
folders=$(echo "${folders}" | grep -v -E "$(IFS="|"; echo "${exclude[*]}")")
fi

for folder in ${folders}; do
echo "Updating folder: ${folder}"

cd "${repos_dir}/${folder}" || { echo "Fail to access ${folder}"; continue; }

branch=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
git reset --hard origin/"${branch}"
git checkout "${branch}"
git pull

echo "Update finished for: ${folder}"
done

# ] <-- needed because of Argbash
22 changes: 22 additions & 0 deletions scripts/clean-local-repos.sh
iaghoCristian marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
iaghoCristian marked this conversation as resolved.
Show resolved Hide resolved

repos_dir="${HOME}/repos"
iaghoCristian marked this conversation as resolved.
Show resolved Hide resolved
exclude_dir=("$@")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean-local-repos my-repo

Will in fact ignore my-repo. I'm not sure, but it doesn't seem very intuitive.

Maybe you can use argbash to make your script accept flags like:

clean-local-repos -e my-repo

-e being the same as the git clean command: --exclude (or except).


folders=$(find "${repos_dir}" -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This find command can return folders which are not git repos. You can protect against it, all git repos will have a .git folder at their root.

if [[ ${#exclude_dir[@]} -ne 0 ]]; then
folders=$(echo "${folders}" | grep -v -E "$(IFS="|"; echo "${exclude_dir[*]}")")
fi
Comment on lines +6 to +9
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simplify this, just add the exclusions in find command itself. Raw example:

cd $repos_dir
repos=$(find . -type d ! -name "felipe" ! -name "cassio")


for folder in ${folders}; do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use bash arrays:

readarray -t repos <<<"${repos}"
for repo in "${repos[@]}"; do

echo "Updating folder: ${folder}"

cd "${repos_dir}/${folder}" || { echo "Fail to access ${folder}"; continue; }

branch=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
git reset --hard origin/"${branch}"
git checkout "${branch}"
git pull
Comment on lines +17 to +19
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a crazy idea, but I think it's really nice.

Why don't you make this script even more generic? Like:

git-run-in-all-repos -e my-repo -- git pull --rebase

Then, you can create a git alias that does reset + checkout + pull, although it's very likely there is one already.


echo "Update finished for: ${folder}"
done