I have a suggestion though. For cleaning untracked files, can you prevent having to run git clean -f -d -n twice by storing the output once and then running it through wc -l for checking in the if condition?
Similarly for cleaning branches, what about storing the output of git branch -r | awk "{print \\$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \\$1}"?
I should add I'm suggesting this as someone who doesn't do that much bash scripting, so maybe there's an issue you ran into that I'm not aware of when just reading those one-liners.
You're perfectly right! Back then I had some issues with the "clean" solution and no idea how to do it better. It just worked, so I kept it like this :)
You made me feel a bit challenged, though, so I've decided I'll try to fix the issue.
Basically, the ideal solution would look like this:
For some reason, it doesn't work in the case when there is nothing to clean - line count is always equal or greater to one. I cannot remember it, but I suppose that was the exact issue five years ago when I was writing the script.
But now I'm smarter (or just know Bash a bit more :) ) and I know how to do it right! echo adds a new line to the end of the printed string, even when it's empty. Unless -n switch is specified! So the code below works perfectly in all cases.
I have a suggestion though. For cleaning untracked files, can you prevent having to run
git clean -f -d -n
twice by storing the output once and then running it throughwc -l
for checking in theif
condition?Similarly for cleaning branches, what about storing the output of
git branch -r | awk "{print \\$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \\$1}"
?I should add I'm suggesting this as someone who doesn't do that much bash scripting, so maybe there's an issue you ran into that I'm not aware of when just reading those one-liners.
You're perfectly right! Back then I had some issues with the "clean" solution and no idea how to do it better. It just worked, so I kept it like this :)
You made me feel a bit challenged, though, so I've decided I'll try to fix the issue.
Basically, the ideal solution would look like this:
For some reason, it doesn't work in the case when there is nothing to clean - line count is always equal or greater to one. I cannot remember it, but I suppose that was the exact issue five years ago when I was writing the script.
But now I'm smarter (or just know Bash a bit more :) ) and I know how to do it right!
echo
adds a new line to the end of the printed string, even when it's empty. Unless-n
switch is specified! So the code below works perfectly in all cases.I'll update the scripts in the article. Thank you!
It turned out that
echo -n
trick isn't correct neither. I've decided to add the second condition for empty string, so the final version is:What about
wc -l "$TO_REMOVE"
?I don't think it gonna work. It tries to read a file under the path saved in
$TO_REMOVE
variable. That's not what we want to do.Oh interesting. Never mind then. Thanks for helping me understand all of this.