Parse branch name, initiate commit with name in the commit message - regex

My team uses a common naming convention for branch names, which include the Jira task number in the branch name.
feature/ACD-1664_update-api-call
feature/VZ-1943_new-provider-template
hotfix/RV-977_fix-loading-issue
I want to create a git alias that will automatically stub out a commit message which includes the Jira task number. Ideally some bash script that will parse the branch name and echo out the commit -m command with the first part of the message pre-created.
I need to regex out the commmit message.
I need to pull ACD-1664 from feature/ACD-1664_update-api-call
Echo this string out into the terminal in a stubbed-out commit command like:
git commit -m "ACD-1664 | <cursor>"

Although this is not the solution you requested, I'd like to hint at another way to cover this, with a commit hook :
You can put in .git/hooks a commit-msg file with these contents :
#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
tmp=$(mktemp) || exit
echo "$current_branch $(cat "$1")" > "$tmp"
mv "$tmp" "$1"
(Thanks guys for the improvements in bash syntax made with your help here)
Then it would automatically prepend your commit messages with the branch name, which does the trick in JIRA.
For the rare occasions when you'd prefer NOT to trigger the hook, do this :
git commit -n -m"Your message"

Related

Symfony & AWS BS - Env vars for console outside .env files

I have a symfony API which runs on AWS beanstalk instances. My .env file registers the environment variables that I need in my app. I override all of them in the AWS console to adapt to my environments.
For exemple :
Env file : DEFAULT_CONNECTION_DSN=bolt://user:password#host:port
Test server : DEFAULT_CONNECTION_DSN=bolt://test:azerty#test.toto.com:7687
Prod server : DEFAULT_CONNECTION_DSN=bolt://prod:azerty#toto.com:7687
This works because AWS overrides the environment variables when the PHP server is started, so the values placed in the .env file are ignored.
The problem is that I try to create a CRON on the server. The CRON is executed from command line, and I saw that, in this case, the variables still have the value specified in the .env file at runtime.
If I list the environment variables on the server, I see that DEFAULT_CONNECTION_DSN has the value that I want, but if I dump the value in my code (or execute php bin/console debug:container --env-vars), DEFAULT_CONNECTION_DSN has the .env file value. I already tried to delete the entry from my .env file. In this case, I have an error saying my environment variable is not found.
I must precise that I work with a .env.local locally, file which is not versionned, and the deploys are based on git versionning, so it seems difficult to add a .env.env-name file for each environement.
What could I do ?
Symfony loads env vars only if they are not already present. Your problem looks like more how to add env vars with cron in AWS. As I don't know BS I can't help you with this.
In your cron I think you can still run DEFAULT_CONNECTION_DSN=bolt://prod:azerty#toto.com:7687 php bin/console ..., this will set your env var at runtime.
When you run something, from a bash, it inherits the exported variables, plus the ones given in the same command line.
Suppose this case:
xavi#bromo:~$ export -p
declare -x HOME="/home/xavi"
declare -x TERM="linux"
declare -x USER="xavi"
xavi#bromo:~$
Say you echo something not defined: You don't get anything, as expected:
xavi#bromo:~$ echo $ABC $XYZ
xavi#bromo:~$
You can place this echo inside a bash script, for example:
xavi#bromo:~$ cat /tmp/test.sh
#!/usr/bin/env bash
echo $ABC $XYZ
Then give it execution permissions:
xavi#bromo:~$ chmod a+x /tmp/test.sh
xavi#bromo:~$
Now, if you execute the script it also says nothing, but if you prefix them with variable assignment the value lives "exclussively" inside that call. See examples with hello-bye and orange-banana. If you later just show the values, they are not there:
xavi#bromo:~$ /tmp/test.sh
xavi#bromo:~$ ABC=hello XYZ=bye /tmp/test.sh
hello bye
xavi#bromo:~$ ABC=orange XYZ=banana /tmp/test.sh
orange banana
xavi#bromo:~$ echo $ABC $XYZ
xavi#bromo:~$
This would be a good approach for the solution of Fabien Papet: To prefix the cron call with the variable assignment.
But if you cannot do that, you can go furthter:
Env vars are not inherited when not exported but inherited when exported: See this:
xavi#bromo:~$ ABC=pita
xavi#bromo:~$ XYZ=pota
xavi#bromo:~$ /tmp/test.sh
xavi#bromo:~$ export ABC=pita
xavi#bromo:~$ export XYZ=pota
xavi#bromo:~$ /tmp/test.sh
pita pota
You could take advantage of bash dot-import command . to import variables.
Place in these files those contents:
xavi#bromo:~$ cat /tmp/fruits
export ABC=orange
export XYZ=tangerine
xavi#bromo:~$ cat /tmp/places
export ABC=Paris
export XYZ=Barcelona
xavi#bromo:~$
Note that the previous files do not have a she-bang as they are not meant to be executed, they do not have to create a bash instance. They are meant for inclussion from an existing bash instance.
Now edit the test.sh to make an inclusion of a file which we'll pass via the first command line parameter:
xavi#bromo:~$ cat /tmp/test.sh
#!/usr/bin/env bash
. $1
echo $ABC $XYZ
xavi#bromo:~$
You can now play with the invocation. I still have the pita-pota pair from the last test. See what happens:
xavi#bromo:~$ echo $ABC $XYZ
pita pota
xavi#bromo:~$ /tmp/test.sh /tmp/fruits
orange tangerine
xavi#bromo:~$ /tmp/test.sh /tmp/places
Paris Barcelona
xavi#bromo:~$ echo $ABC $XYZ
pita pota
xavi#bromo:~$
The first line echo $ABC $XYZ displays our current environment.
The second line invokes a new bash (via the she-bang of /tmp/test.sh) and as pita-pota were exported they are momentarily there. But as soon as . $1 is executed, it is expanded into . /tmp/fruits which overrides the environment by exporting new variables, thus the result.
The second scripts (the one with fruits) ends, so the bash is terminated and its environment is destroyed. We return to our main bash. In here we still have pota-pita. If we had printed now, we'd see the pita-pota. We go with the places, now.
The reasoning with the places is identical to the reasoning with the fruits.
As soon as we return to the main bash, the child env is destroyed, so the places have been blown away and we return to the first initial environment with pita-pota, which is then printed.
So...
With all this you can:
Setup a bash script that wraps:
loading the environment from some
place.
Call the php bin/console
In the cron, do not invoke the php but your wrapper script.
This allows you to
Change the script with different environments without depending on versioning.
Keep your credentials and configuration separated from the code.
In conclusion:
Make your source version control system to have the cron versioned, and the wrapper bash script versioned.
Make your deployer to place a different "includable" parameters file in each environment.
Make your cron to call the wrapper.
Make your wrapper to setup the env vars and call the php bin/console.
Does this solve your issue?

Conditions from sh syntax to Makefile syntax

I have a certain file UPDATE.sh. The task of this file is to update my program using git merge.
UPDATE.sh:
#!/usr/bin/env bash
git fetch https://xxxxx
newUpdatesAvailable=`git diff HEAD FETCH_HEAD`
if [[ "$newUpdatesAvailable" != "" ]]
then
git branch backup
git checkout backup
git add .
git add -u
git commit -m date "+%d.%m.%Y"
git checkout master
git merge FETCH_HEAD
else
echo "No updates..."
fi
I have to write the same thing but on Make syntax.
I tried to figure out the conditions in Make, but it didn't work out for me.
Please tell me how to write this correctly in Makefile without using:
update:
./UPDATE.sh
Another alternative, that does not require extensive quoting, semi-colons, etc, is to take advantage of the fact that make will stop executing a rule when the exit code is non zero.
This will usually result in easier to maintain Makefile, with simpler rules.
update:
git fetch https://xxxxx
git diff -q HEAD FETCH_HEAD || { echo "No Update" ; false ; }
git branch backup
git checkout backup
git add .
git add -u
git commit -m date "+%d.%m.%Y"
git checkout master
git merge FETCH_HEAD
You don't need to capture the output of git diff; you just need to know if there is a diff or not. Use the -q option.
if ! git diff -q HEAD FETCH_HEAD; then
git branch backup
...
else
echo "No updates"
fi
-q suppresses output and implies --exit-code, which causes git diff to exit with a non-zero exit status if there are differences.
In a Makefile, this would be
.PHONY: update
update:
git fetch https://xxxxx
if ! git diff -q HEAD FETCH_HEAD; then \
git branch backup && \
git checkout backup && \
git add . && \
git add -u && \
git commit -m date "+%d.%m.%Y" && \
git checkout master && \
git merge FETCH_HEAD; \
else \
echo "No updates"; \
fi
The semicolons and backslashes are necessary because the entire if statement has to appear on one logical line.

Installed go with hombrew, can find $GOROOT causing package failures

I installed Go with homebrew and it usually works. Following the tutorial here on creating serverless api in Go. When I try to run the unit tests, I get the following error:
# _/Users/pro/Documents/Code/Go/ServerLess
main_test.go:6:2: cannot find package "github.com/strechr/testify/assert" in any of:
/usr/local/Cellar/go/1.9.2/libexec/src/github.com/strechr/testify/assert (from $GOROOT)
/Users/pro/go/src/github.com/strechr/testify/assert (from $GOPATH)
FAIL _/Users/pro/Documents/Code/Go/ServerLess [setup failed]
Pros-MBP:ServerLess Santi$ echo $GOROOT
I have installed the test library with : go get github.com/stretchr/testify
I would appreciate it if anyone could point me in the right direction.
Also confusing is when I run echo $GOPATH it doesnt return anything. same goes for echo $GOROOT
Some things to try/verify:
As JimB notes, starting with Go 1.8 the GOPATH env var is now optional and has default values: https://rakyll.org/default-gopath/
While you don't need to set it, the directory does need to have the Go workspace structure: https://golang.org/doc/code.html#Workspaces
Once that is created, create your source file in something like: $GOPATH/src/github.com/DataKid/sample/main.go
cd into that directory, and re-run the go get commands:
go get -u -v github.com/stretchr/testify
go get -u -v github.com/aws/aws-lambda-go/lambda
Then try running the test command again: go test -v
The -v option is for verbose output, the -u option ensures you download the latest package versions (https://golang.org/cmd/go/#hdr-Download_and_install_packages_and_dependencies).

How do you get Amazon SES working on Debian Squeeze?

All the Perl dependencies for it are met but I'm getting this error:
Can't locate object method "ssl_opts" via package "LWP::UserAgent" at SES.pm line 250.
I just wanted to document what I had to do to get this running on my Debian system. The solution for Ubuntu is probably the same.
First, to let Perl find SES.pm make the directory /usr/local/lib/site_perl and copy SES.pm there. I prefer this solution over what the README recommends.
Your system probably has a lot of the dependencies met already so instead of installing duplicate packages just check first which ones it needs. To do that run these commands. If it gives an error it's not met:
perl -e 'use Crypt::SSLeay'
perl -e 'use Digest::SHA'
perl -e 'use Bundle::LWP'
perl -e 'use LWP::Protocol::https'
perl -e 'use MIME::Base64'
perl -e 'use Crypt::SSLeay'
perl -e 'use XML::LibXML'
I had to remove the package libcrypt-ssleay-perl because it's not compatible with this Amazon script. With it the script produces the error in the question.
I installed these packages from Debian:
libxml-libxml-perl
libssl-dev (needed to compile dependencies)
To find out which package contains the Perl module you need use this page on the Debian site to search the contents of packages:
http://www.debian.org/distrib/packages
Replace the :: in the package with / and put .pm at the end. For example if you need XML::LibXML search for XML/LibXML.pm
I installed these packages from CPAN. It takes a little while though. There are a lot of dependencies.
perl -MCPAN -e 'install LWP::Protocol::https'
perl -MCPAN -e 'install Crypt::SSLeay'
Comment out line 250 in SES.pm, as follows:
# $browser->ssl_opts(verify_hostname => 1);
Then it will run. Probably less secure. But it runs.

Mercurial templates: have "{branches}" return "default"?

What's the simplest way to have hg ... --template '{branches}' return default instead of an empty string when the changeset being printed is part of the default branch?
I've not found an in-template way to do that. If I'm in a shellscript I do something like:
BRANCH=$(hg log --revision 0 --template '{branches}')
echo ${BRANCH:=default}
Though if you have the excellent hg prompt extension in place you could do:
hg prompt '{branch}'