Git Alias to Create Aliases - bash

so I have a git alias called makealias that is set to the following:
!f() { git config --global alias.$1 '!f(){ $2;};f';}; f
When I run the command, I usually run it with something like this:
git makealias "testalias" "echo 'hello world'"
make alias successfully creates a new alias, but it doesnt bring the echo statement along for the ride. When I run my command to see the text of my aliases, i find that testalias looks like this:
!f(){ $2;};f
What do I need to do differently so that when I run git makealias "testalias" "echo 'hello world'" the newly created command of testalias will be !f(){ echo 'hello world';};f

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'

"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

Embedded terminal startup script

I usually use bash scripts to setup my environments (mostly aliases that interact with Docker), ie:
# ops-setup.sh
#!/bin/bash
PROJECT_NAME="my_awesome_project"
PROJECT_PATH=`pwd`/${BASH_SOURCE[0]}
WEB_CONTAINER=${PROJECT_NAME}"_web_1"
DB_CONTAINER=${PROJECT_NAME}"_db_1"
alias chroot_project="cd $PROJECT_PATH"
alias compose="chroot_project;COMPOSE_PROJECT_NAME=$PROJECT_NAME docker-compose"
alias up="compose up -d"
alias down="compose stop;compose rm -f --all nginx web python"
alias web_exec="docker exec -ti $WEB_CONTAINER"
alias db="docker exec -ti $DB_CONTAINER su - postgres -c 'psql $PROJECT_NAME'"
# ...
I'd like them to be run when I open the embedded terminal.
I tried Startup Tasks but they are not run in my terminal contexts.
Since I have a dedicated script for each of my projects, I can't run them from .bashrc or other.
How can I get my aliases automatically set at terminal opening ?
Today I'm running . ./ops-setup.sh manually each time I open a new embedded terminal.
You can create an alias in your .bashrc file like so:
alias ops-setup='bash --init-file <(echo '. /home/test/ops-setup.sh'; echo '. /home/test/.bashrc')'
If you call ops-setup, it will open up a new bash inside your terminal, and source .bashrc like it normally would, as well as your own script.
The only way I see to completely automate this is to modify the source code of your shell, e.g. bash, and recompile it. The files that are sourced are hardcoded into the source code.

Git: Creating an alias for running ruby commands from terminal

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 .

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