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

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

Related

Execute Shell file functions in Mac

I have the following shell file that contains this:
sh
nightlyTag() {
echo $1-alpha.$(date +%Y%m%d).$(($(date +%s%N)/1000000))
}
yarnPubCanaryVersion() {
if [ -z "$1" ]
then
echo "No version argument supplied, maybe you meant v1.0.0?"
return 1
fi
version=`nightlyTag $1`
yarn version --new-version $version --no-git-tag-version
npm publish --tag canary
git reset --hard HEAD
}
I make the file executable with chmod +x canary.sh, then I run it doing ./canary.sh then my terminal changes to sh-3.2$ then I try to run the functions in the terminal like this nightlyTag and I get
sh: nightlyTag: command not found
Same for yarnPubCanaryVersion.
I was looking at this SO question
You won't be able to run functions from the terminal after you run the script.
You need to source the script to do this:
source ./canary.sh
Or add the contents of the file to the .bashrc file or its equivalent, and then source it.
The source command is used to load any function file into the current shell.
Now once you call those functions you will get the expected output.
At the top of your sh file you need to include:
#! /path/to/bash
the path to the bash that you are using.

Git Alias to Create Aliases

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

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

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 .

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