Bash Shell Script Process Each Directory in Home - bash

I am using Git Bash, and I would like to write a script that processes the same set of commands for each directory (local repo) in my home directory. This would be easy enough in DOS, which most consider as handicapped at best, so I'm sure there's a way to do it in bash.
For example, some pseudo-code:
ls --directories-in-this-folder -> $repo_list
for each $folder in $repo_list do {
...my commmand set for each repo...
}
Does anyone know how to do this in Bash?

You can do that in bash (even on Windows, if you name your script git-xxx anywhere in your %PATH%)
#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
cd /your/git/repos/dir/$folder
# your git command
done
As mentioned in "Git Status Across Multiple Repositories on a Mac", you don't even have to cd into the git repo folder in order to execute a git command:
#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
worktree=/your/git/repos/dir/$folder
gitdir=$worktree/.git # for non-bare repos
# your git command
git --git-dir=$gitdir --work-tree=$worktree ...
done

Related

--git-dir changes directory in .bashrc in WSL

I created a script that prints out all my git repo libraries with the current branch names. But for some reason, it changes the current directory at the end.
I have a ".bash_aliases" script that I call from .bashrc, so:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
The .bash_aliases file contains some aliases and variables, such as these:
repo1Dir=~/repo1/
repo2Dir=~/repo2/
repo3Dir=~/repo3/
alias repo1="cd $repo1Dir"
alias repo2="cd $repo2Dir"
alias repo3="cd $repo3Dir"
And I added this script at the end of the file:
echo -n "repo1 -> "
git --git-dir $repo1Dir".git" --work-tree=$repo1Dir rev-parse --abbrev-ref HEAD
echo -n "repo2 -> "
git --git-dir $repo2Dir".git" --work-tree=$repo2Dir rev-parse --abbrev-ref HEAD
echo -n "repo3 -> "
git --git-dir $repo3Dir".git" --work-tree=$repo3Dir rev-parse --abbrev-ref HEAD
When I start a WSL terminal, it will print this at initialization:
repo1 -> branch_abc
repo2 -> branch_cba
repo3 -> main
I expect that the current folder will end up being HOME because I didn't change that before and as far as I know the command --git-dir doesn't change the current dir. But in the end, the current folder will be this one:
myComputer#DESKTOP-PC:~/repo3$
So the folder of the last command. If I type one of these git commands in the terminal, the current folder will not change.
This becomes a problem if I want to work with repo2 for example. I type this:
repo2
and the current folder will be this of course: myComputer#DESKTOP-PC:~/repo2$. Then I start vscode with the code . command. The vscode opens repo2. The vscode's terminal will of course print the branches (which are in .bash_aliases) and then this will be the current directory: myComputer#DESKTOP-PC:~/repo3$
Why does it change the directory and how can I prevent it?
Thanks in advance for any answers!

Git hook on Ubuntu broken

I recently got a git hook from someone that aims to add the issue number, which is in a specific location of the branch name, to the beginning of all commits. The goal is to take the #number from feature/#number-issue. Here is some info:
➜ .githooks pwd
/home/luctia/.githooks
➜ .githooks git config --global --list
user.name=luctia
user.email=myemail
core.hookspath=/home/luctia/.githooks
➜ .githooks cat commit-msg
#!/bin/sh
WI=$(git status --branch | grep -iPo "(feature|bug)\/#\d+" | head -1)
WI=$(echo "($WI)" | grep -Po "\d+")
if [[ ! -z "$WI" ]]; then
WI="#$WI"
CM=$(cat "$1")
if [[ ! $CM == *"$WI "* ]]; then
echo "$WI $CM" > "$1"
fi
fi
This doesn't seem to work, though. The script is executable for every user, so that's not the issue. I have tried switching from sh to bash, and with that edit I've executed the script on a file in a repo, which added the number to the beginning of the file, so I know it works. I'm not sure if git hooks can execute bash files, but it doesn't make a difference whether I use sh or bash, though I would like to know if it can run bash scripts.
I'm using Webstorm for my IDE right now, and it doesn't work in there, and it also doesn't work on CLI git. I have no idea how to proceed.
Edit: I am pretty sure the script is not executed. When I add data > /tmp/hook to the script, no file appears. I do have to change from sh to bash though.
The problem was that I was trying to make this work on a pre-existing project, with an existing .git directory. I thought changing the config with the --global flag would just work, but apparently the config inside the .git directory of the project did not change, and the old hookspath was still there. When I changed it, the script started working.

Where are the svn hook files located in OS X?

I'm running OS X Mavericks. Pretty sure I got svn from the Command Line Tools from the Apple Developer site.
I've searched around for where hook script are supposed to be located. All the articles I've found (e.g. this one: https://stackoverflow.com/a/7577251/726378) say that there is a hooks directory in the repository directory. I have found no such directory.
Where is this directory?
Is this directory on the svn server or the client?
In Subversion, hooks are run on the server side, and are located inside the repository directory on the server.
Try this:
$ cd $HOME
$ svnadmin create foo_repo # Creating Subversion repository called "foo_repo"
$ cd foo_repo
$ hooks
post-commit.tmpl post-revprop-change.tmpl pre-commit.tmpl
pre-revprop-change.tmpl start-commit.tmpl post-lock.tmpl
post-unlock.tmpl pre-lock.tmpl pre-unlock.tmpl
There they are!
You can try using this repo if you'd like:
$ cd $HOME/foo_repo/conf
$ vi svnserve.conf # Remove the "#" from "password-db = passwd" (Line 27)
$ vi passwd # You want to define a password for your user
$ cd $HOME
$ svnserve -r foo_repo -d # Starts up the Subversion server
$ mkdir $HOME/workdir
$ cd $HOME/workdir
$ svn co svn://localhost localhost
$ cd localhost # Your Subversion working directory!
Now, you can play around with your various hook and see how it affects using your repository.

Shell script doesn't properly execute from ruby CGI script

I've got a ruby cgi script which calls a shell script.
The shell script does a git pull.
When I run the shell script from the command prompt it works.
But when I run it from the ruby cgi script it executes the script but the git pull doesn't happen.
I'm guessing it's possibly permissions related but I can't quite work out how to fix it.
The ruby script is:
#!/usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby
require "cgi"
git_pull = `sh /github/do_git_pull.sh`
move_apanels = `sh /github/move_apanels.sh`
puts "Content-type: text/html\n\n"
puts "<html><body>We've done the following:<ul>"
puts "<li>#{git_pull.to_s}</li>"
puts "<li>#{move_apanels.to_s}</li>"
puts "</ul></body></html>"
And the shell script is:
#!/bin/bash
sudo sh -c cd /github
sudo sh -c git pull origin master
echo "Git Pull Completed"
Both files have chmod 777
Any ideas?
Doing this:
sudo sh -c cd /github
only changes the PWD for the duration of that sh command. It does not affect the current shell. You need to cd and git pull in the same subshell:
sudo sh -c 'cd /github && git pull origin master'
Setting 777 on your scripts won't cut it. Try and find out the user under which your ruby script executes the shell script. Since git uses SSH keys for authentication and normally your SSH keys can only be used by you, then git pull would fail if another user tries to do the git pull.
Check out this question on how to run a shell script as a different user.
Also make sure that the PATH in the target environment is set properly and accessible (if you run the web server chrooted).

Stay in directory changed after ending of bash script

My bash script:
#!/bin/bash
cd /tmp
Before running my script:
pwd: /
After running my script:
pwd: /
After runnig my script trough sourcing it:
pwd: /tmp
How I can stay at the path from the script without sourcing it ?
You can't. Changes to the current directory only affect the current process.
Let me elaborate a little bit on this:
When you run the script, bash creates a new process for it, and changes to the current directory only affect that process.
When you source the script, the script is executed directly by the shell you are running, without creating extra processes, so changes to the current directory are visible to your main shell process.
So, as Ignacio pointed out, this can not be done
Ignacio is correct. However, as a heinous hack (totally ill advised and this really should get me at least 10 down votes) you can exec a new shell when you're done
#!/bin/bash
...
cd /
exec bash
Here's a silly idea. Use PROMPT_COMMAND. For example:
$ export PROMPT_COMMAND='test -f $CDFILE && cd $(cat $CDFILE) && rm $CDFILE'
$ export CDFILE=/tmp/cd.$$
Then, make the last line of your script be 'pwd > $CDFILE'
If you really need this behavior, you can make your script return the directory, then use it somehow. Something like:
#!/bin/bash
cd /tmp
echo $(pwd)
and then you can
cd $(./script.sh)
Ugly, but does the trick in this simple case.
You can define a function to run in the current shell to support this. E.g.
md() { mkdir -p "$1" && cd "$1"; }
I have the above in my ~/.bashrc

Resources