Skip to content

Instantly share code, notes, and snippets.

@dtrugman
Last active November 2, 2021 08:59
Show Gist options
  • Save dtrugman/ec4d40e7b05f01251e4c688ae62219fd to your computer and use it in GitHub Desktop.
Save dtrugman/ec4d40e7b05f01251e4c688ae62219fd to your computer and use it in GitHub Desktop.
Describe git projects
#!/bin/bash
git-describe() {
for dir in *; do
[[ -d "$dir" ]] || continue
[[ -d "$dir/.git" ]] || continue
local branch="$(cd "$dir"; git rev-parse --abbrev-ref HEAD)"
echo "$dir -> $branch"
done
}
[[ "$0" == "$BASH_SOURCE" ]] && git-describe "$@"
@aharonscada
Copy link

Hi. Nice start. Some comments:

  1. Use for dir in *; do -- no need for $(ls)
  2. You can use || to join the conditions of the two if statements together and have just one if.

What's the point of the symbolic link?

@dtrugman
Copy link
Author

dtrugman commented Nov 1, 2021

Thanks @aharonscada!
I wrote that some time ago and didn't consider changing anything before publishing ๐Ÿ˜Š

Now, regarding the symbolic link, I took a moment to think about your question.
At first, the point of the symbolic link was to be able to run ls -l and see all the directories and the branches they point to.
But, come to think of it, the command could've simply printed the output for you, thus resulting in a single bash command rather than two.

So thanks for all the suggestions! It's definitely much better now!

@aharonscada
Copy link

Glad to help. FYI, for the if what I had in mind was

if [[ ! -d "$dir" ]] || [[ ! d "$dir/.git" ]]
then
    continue
fi

@dtrugman
Copy link
Author

dtrugman commented Nov 2, 2021

Yeah I realized :) I prefer the shorter version where is very readable, so went with that...

@aharonscada
Copy link

We have different opinions about readability. But you're still young. :-) Take it easy --Aharon

@dtrugman
Copy link
Author

dtrugman commented Nov 2, 2021

As always, you are right ๐Ÿ˜‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment