Git: Creating an alias for running ruby commands from terminal - ruby

I had the following script in a bash file titled someCommand:
filename="$1"; day="$2"; month="$3"; message=""$4""
ruby -r "~/someFolder/someClass.rb" -e "someClass.run('$filename', '$day', '$month', '$message')"
I now want to convert it to a git alias rather than have it be a bash alias, so I tried:
git config --global alias.someCommand 'ruby -r "~/someFolder/someFile" -e "someClass.run($1, $2, $3, $4)"'
But I receive the error:
Expansion of alias 'someCommand' failed; 'ruby' is not a git command
How can I create a git alias to run ruby commands like this?

You don't want a git alias. You want a subcommand.
Name your script git-somecommand where somecommand would be the name of the command you want git to recognize.
So, for git someCommand you would name the file git-someCommand. Make sure that it is executable and in your path and you should be good to go .

Related

How to alias "git push" into itself + running a script?

I need to run a script which notifies my CI server after I push. Therefore I need to alias "git push" into "git push; powershell script.ps1". Here is what I am trying:
$ alias git push='git push; powershell script.ps1'
bash: alias: git: not found
Alternatively, denoting whitespace characters returns this:
$ alias git\ push='git push; powershell script.ps1'
bash: alias: `git push': invalid alias name
$ alias "git push"='git push; powershell script.ps1'
bash: alias: `git push': invalid alias name
How can I alias my script to run every time I push?
This worked:
git config --global alias.push "push; powershell ./script.ps1"
The command runs, but using "git push" still doesn't run my script.
Yes, I know that I can use webhooks for this, but it is imperative that the CI server remains fully local. Yes, I know that I can use NGROK for that, but my company does not allow it.
Git doesn't allow alias names to override proper command names. Use another label for your alias.
And if the alias content mixes a git command and shell commands, you'll have to prepend a ! to tell git to pass the command for the shell to interpret.
Try
git config --global alias.p '!git push; powershell ./script.ps1'
# then to invoke it, just
git p
Another slight improvement would be to use && instead of ; to chain your commands, in case the push fails. It would then stop from executing the unnecessary following.
git config --global alias.p '!git push && powershell ./script.ps1'

Git doesn't create .gitconfig when it's inside a function

I'm a fairly new user in Bash and Git in general and I'm scratching my head about what the problem could be. I'm creating a code that checks if .gitconfig exists and if it doesn't it allows you to configure it almost automatically using a read command to get your email and username and apply them in a line of code.
Code example:
#!/bin/bash
# colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NO_COLOR='\033[0m'
# function git
git () {
printf "${RED}Set your Git email\n${NO_COLOR}"
read GIT_AUTHOR_EMAIL
git config --global user.email "$GIT_AUTHOR_EMAIL"
printf "${RED}Set your Git username\n${NO_COLOR}"
read GIT_AUTHOR_USERNAME
git config --global user.name "$GIT_AUTHOR_USERNAME"
git config --list | grep user.email && git config --list | grep user.name
}
# git check & configuration
if [ -f ".gitconfig" ]; then
printf "${YELLOW}Git was previously configured\n${NO_COLOR}"
exit
else
git
printf "${YELLOW}Done\n${NO_COLOR}"
exit
fi
If it doesn't exist, it calls a function to configure it but after some quick debugging using the set -x command I figured out the file .gitconfig is not created at all but it does when I do it myself outside a function. All it does is go back to read GIT_AUTHOR_EMAIL, apply the code git config --global user.email "$GIT_AUTHOR_EMAIL" and go back to the first read command. I want the code to check for .gitconfig and if it exists it'll also ask if the user wants to re-configure their Git details. I'm super close on doing so.
Is there any way I can fix it or do it another way?
Once you've defined the function git, any invocation of the name git in that shell process will refer to that shell function. If you'd like to invoke the program git, then you need to prefix it with the built-in command:
command git config --global user.name "$GIT_AUTHOR_USERNAME"
If it wasn't your intention to override the git command, then you probably want to name your shell function differently, which avoids the problem altogether.

"not a git command" configuring a bash script as a git alias

I want to set a specific alias in .gitconfig to a bash script like this:
[alias]
example = "~/git-scripts/example-script.sh"
instead of:
[alias]
example = "!f() { arg1=$1; echo $arg1; }; f"
So, the echo script above would be in this file ~/git-scripts/example-script.sh
When i'm trying to execute a alias like this, i got this error:
expansion of alias 'example' failed; ~/git-scripts/example-script.sh is not a git command
What's wrong?
As Charles Duffy said in the comments area, i could make it work like this:
[alias]
example = "! ~/git-scripts/example-script"
Just add ! before the script path
Be careful with:
Permissions: If you are in Linux, just add execution permissions like this:
$ chmod +x YOUR_SCRIPT_PATH
Notice that you don't need the extension (.sh) at the script file. You can read more about this here

Bash Shell Script Process Each Directory in Home

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

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).

Resources