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 1 commit
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
6 changes: 6 additions & 0 deletions home/dot_bash_aliases.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ cdw() {
cd "${win_home}"
}
# {{ end }}

clean-repos() {
local exclude_dir=("$@")
cd "${HOME}/repos/dotfiles" || { echo "Fail to access dotfiles"; return; }
./scripts/clean-local-repos.sh "${exclude_dir[@]}"
}
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