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

Conversation

iaghoCristian
Copy link

No description provided.

#!/bin/bash

repos_dir="${HOME}/repos"
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).

scripts/clean-local-repos.sh Show resolved Hide resolved
scripts/clean-local-repos.sh Show resolved Hide resolved
scripts/clean-local-repos.sh Show resolved Hide resolved
Comment on lines +6 to +9
folders=$(find "${repos_dir}" -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)
if [[ ${#exclude_dir[@]} -ne 0 ]]; then
folders=$(echo "${folders}" | grep -v -E "$(IFS="|"; echo "${exclude_dir[*]}")")
fi
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")

folders=$(echo "${folders}" | grep -v -E "$(IFS="|"; echo "${exclude_dir[*]}")")
fi

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

repos_dir="${HOME}/repos"
exclude_dir=("$@")

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.

Comment on lines +17 to +19
git reset --hard origin/"${branch}"
git checkout "${branch}"
git pull
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants