Git - How to make custom alias function always available? - bash

I have a script that I've made that creates a repository locally, creates first commit and creates the remote repo. Here it is:
#!/bin/bash
function create-repo {
inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
# check if we already inside a repository
if [ "$inside_git_repo" = true ]; then
echo "Error: It is not possible to create a repository inside another one"
else
#check if the name of repository is not null
if [ "$1" = "" ]; then
echo "Error: Missing one argument (Repository name)"
else
#creating repository folder
echo "Repo will be created with the following name: $1 "
mkdir "$1"
cd "$1" || exit 1
echo
#Create a README.md file to push to remote
echo "# $1" >> README.md
echo
#initializing repo
git init
echo
#staging and commiting README.md file
git add README.md
git commit -m "first commit"
echo
#username in github
username="alanwilliam"
#secret token of github API
token="PUT HERE YOU SECRET TOKEN"
#json that will be sent to github API
json="{ \"name\":\"$1\" }"
#Curl command that does the magic to create the remote on github
curl --user "$username":"$token" --request POST -H "Content-Type: application/json" -d "$json" https://api.github.com/user/repos
echo
echo
echo
#adding remote and pushing
git remote add origin https://github.com/alanwilliam/"$1".git
git push -u origin master
fi
fi
}
This code is working just fine - it is perfect for me. I've put it inside a file called .custom_aliases and it worked. However, in order to git detect this file and make the command available, every time that I open git bash, I need to type
source ~/.custom_aliases to be able to use the command in script, which is annoying.
Is there a way to make it always available, so I don't have to type the source command every time I open git bash?

Related

Git push to various repositories via bash script

I'm trying to create a bash script that iterates over 4 different folders (git repositories) and updates/pushes the changes for each one of them. I have the script mostly complete, except the authentication part.
Here's my current script:
#!/bin/bash
# Fetch username and password
echo "Please insert your git credentials!"
read -p 'Username: ' username
read -sp 'Password: ' password
# Check if you svn and git installed in your machine
if which svn &> /dev/null && which git &> /dev/null; then
# Store the current dir
CUR_DIR=$(pwd)
# Let the person running the script know what's going on.
echo "Pulling in latest changes for all repositories..."
for D in $CUR_DIR/*; do
if [ -d "${D}" ]; then
echo "${D}"
cd $(basename $D);
# Make sure SVN is on trunk branch and git branch is on master
SVN_BRANCH=$(svn info | grep '^URL:' | egrep -o '(tags|branches)/[^/]+|trunk' | egrep -o '[^/]+$')
GIT_BRANCH=$(git symbolic-ref --short HEAD)
if [ "$SVN_BRANCH"!="trunk" ] || [ "$GIT_BRANCH"!="master" ]; then
echo $CUR_DIR
echo "Make sure you're on SVN trunk branch and git master branch."
exit -1
fi
echo "Update SVN repository";
svn up
# Update git
git add .;
git commit -m "Update with the changes from svn trunk branch."
echo "Start pushing changes to main repository."
git push origin master
echo "Update git repository";
git pull origin master
# Update SVN
svn add .
svn commit -m "Update with changes from git master branch."
cd ..
fi
echo "Complete!"
done
else
echo "You need both svn and git CLI's installed to run this script!";
fi
As you can see, I'm already fetching the credentials in the beginning of the script, my problem is how can I use them to do the actions that need authentication, namely
git push origin master
git pull origin master
How can I build the git commands to make the authenticated request?

bash, script to git add one file and commit

I'm trying to create a script to do this:
git add "file"
git commit -m "Comment"
My idea is to run:
gac "file" "Comment"
I know I can do something similar but for all files, with:
echo 'alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile
And the .sh would be:
!/bin/bash
git add .
echo “Enter commit message: “
git commit -am “$commitMessage”
Well you need two things :
A bin folder where you can put every sh script you want to use everywhere.
More knowledge about shell scripting and how you can get argv (in your ex: 'file' 'Comment')
So first go to your /home/<username> then mkdir bin && cd bin && pwd
then copy the pwd and add it into your PATH env variable inside your .bashrc
path example: PATH='/bin/:/sbin/:/home//bin
Then source ~/.bashrc you can now use every sh script inside you bin folder everywhere.
Cool so first problem done !
you don't have to do echo alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile anymore.
Now second problem here a post that can help you post
And let me show you for your example :
cd ~/bin && vi gac.sh
Now the script :
#!/bin/sh
if [ "$#" -ne 2 ]; then
echo "Usage: ./gac FILENAME COMMIT_MESSAGE" >&2
exit 1
fi
git add "$1"
git commit -am "$2"
First we check the number or arg then git add and commit.
Simple and fast maybe checking if arg one is a file might be a good idea too.
PS: i'm going to re write my post ahah
Here's what I have in my .bashrc:
ga ()
{
if test "$1" != "-f" && git rev-parse HEAD > /dev/null 2>&1 && ! git diff-index --quiet HEAD; then
echo 'Repo is dirty. -f to force' 1>&2;
return 1;
fi;
git add "$#";
list=$(git diff --name-only --cached | tr \\n \ );
git commit -m "Add $list"
}
The commit message is autogenerated, but you could easily modify it to prompt the user or take it from somewhere else.

Push nuget package only if the package version matches the Tag on Git's master branch

In our development environment, we have set up a NuGet local server (BaGet). We have adopted the Gitflow idea. When a library is ready to be released on Baget, the developer should first increase the Tag on the master branch (which needs to be approved first via a pull-request), then push the library to the Baget. We do this to keep the version of Git and Nuget in sync.
The process of keeping versions in sync (Git tag & NuGet version) is controlled manually by the developer and sometimes some team members forget to define the Git version tag and just push the library to Baget.
It would be a great help if the script could check the Current Git Tag before pushing the library to the Baget server, and only push it if the Tag and Version are the same. This can prevent pushing a version without matching Tag on git.
We use this script for pushing to Baget:
#!/bin/bash
clear
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
ostype=`uname`
KEY=$NUGET_KEY
SOURCE=$NUGET_URL
while :
do
clear
echo "Input your package version: "
read version
Common="Dayan.Common/bin/Debug/Dayan.Common."$version".nupkg"
dotnet nuget push $Common -s $SOURCE -k $KEY
echo "press enter to continue ..."
read
done
Can I somehow check use git commands in the bash to get the Tag of the last commit on the master branch of the project, and check it with the user input for version?
One way to make that check would be to use the git command rev-list.
This command will output the commit SHA of the most recent commit:
$ git rev-list -n 1 HEAD
dfe4a9989b33e97f25645d79fd62900cc3209ec7
While this command will output the commit SHA of the tag 3.1.5:
$ git rev-list -n 1 "3.1.5"
a35117a201290b63b53ba6372dbf8bbfc68f28b9
The following example script should get you started:
#!/bin/bash
echo "Input your package version: "
read version
last_commit=$(git rev-list -n 1 HEAD 2>/dev/null)
last_commit_result=$?
if [ "$last_commit_result" != "0" ]; then
echo "Failed to get the SHA of the most recent commit"
exit 1
fi
version_commit=$(git rev-list -n 1 "$version" 2>/dev/null)
version_commit_result=$?
if [ "$version_commit_result" != "0" ]; then
echo "There is no commit with the tag: $version"
exit 1
fi
if [ "$last_commit" = "$version_commit" ]; then
echo "The most recent commit has the tag: $version"
else
echo "The most recent commit does NOT have the tag: $version"
fi
If you also want to make sure the script is only run from master then add this near the script's start:
active_branch=$(git branch --show-current 2>/dev/null)
active_branch_result=$?
if [ "$active_branch_result" != "0" ]; then
echo "Failed to get the active branch"
exit 1
elif [ "$active_branch" != "master" ]; then
echo "The active branch is not master"
exit 1
fi

Automating git push and commit in a shell script on a Centos server

I am implementing a shell script that will be backing up the database and then push the sql file to Github, I am using centos server the project is located at /opt/server-scripts/backup.sh. How do I automate this?
Here is my implementation so far:
#!/bin/bash/
var=$CURRENT_DATE=date +"%D %T"
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
# Git Push
GIT=$(which git)
REPO_DIR=/opt/server-scripts/
cd ${REPO_DIR} || exit
${GIT} add --all .
${GIT} commit -m "backup:" + "'$CURRENT_DATE'"
${GIT} https://pmutua:xxxxx#github.com/pmutua/sqlbackup.git master
You can check if a command/executable is installed or it is with in your PATH, one way using type
if type -P git >/dev/null; then
echo 'git is installed.'
fi
If you want to negate the result add the !
if ! type -P git >/dev/null; then
echo 'git is not installed.'
fi
To add that to your script.
#!/usr/bin/env bash
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
if ! type -P git >/dev/null; then ##: Check if git is not installed
echo 'git is not installed.' >&2 ##: print an error message to stderr
exit 1 ##: Exit with an error
fi
# Git Push
CURRENT_DATE=$(date +"%D %T") ##: Assign the output of date in a variable
REPO_DIR=/opt/server-scripts/
cd "${REPO_DIR}" || exit
git add --all .
git commit -m "backup: '$CURRENT_DATE'"
git push https://pmutua:xxxxx#github.com/pmutua/sqlbackup.git master
You can add the date directly git commit -m "backup: '$(date +"%D %T")'"
that way the date will be same with the output of git log
Other ways to check if a command exists is via command and hash see Howto check if a program exists in my PATH

how to run a shell script as an alias?

I wanna alias a script to my zsh. Aliasing a script in zshrc does not work, the output of the script in nothing
There are no syntax errors in my script. i have tried running
"sh ./script.sh" in the script containing folder which does fetches the desired result but alias something="sh ~/script.sh" does not work
even alias something="source ~/script.sh" does not work
the script creates a local project and a github repo
contents of the script:
#!/bin/bash
function create () {
read -p 'Repository Name: ' uservar
projects_directory = ~/Downloads/Projects/ #change this path to the directory where you want to store you files
mkdir $projects_directory/$uservar
cd $projects_directory/$uservar
git init
touch README.md
echo -e "#$uservar" >> $projects_directory/$uservar/README.md
# this is where we make a github repo from cli
repo_name=$uservar
test -z $repo_name && echo "Repo name required." 1>&2 && exit 1
curl -u 'thisisshub' https://api.github.com/user/repos -d "{\"name\":\"$repo_name\"}" #change thisisshub to your <username>
#making a git repo from cli ends
git add .
git commit -m "Initial Commit"
git push -u origin master
code .
}
expected result: successful aliasing of a script
actual result: no output

Resources