git log --pretty=%P -n 1 <child> does not work on GitPython - gitpython

Does anyone know how I can make this work on GitPython?
REPO.git.log('--pretty=%P -n 1 202bfa07e1fbaf7d210468a2ca6c7f4352fc2b25')
puts it into an infinite loop.

Related

Is it possible to extract a message from the commit and put it as a topic in the pre-push?

I have almost 3 days thinking about it and I can't find a way to make me add a topic without having to do a git push.
Is there any way to modify your push after doing a git push?
Let me explain:
I'm trying to make a pre-push extract a specific value from the message and put it as a topic so I can do this:
git commit -m "CELL:ANT-1234 Testing pre-push"
git push master
Be equal to:
git commit -m "CELL:ANT-1234 Testing pre-push"
git push master -o topic=ANT-1234
I have managed to prepare everything and have the topic and message prepared without problems but I can't find a way to put it in the push without having to do another git push inside the pre-push, this makes me do the push 2 times and... although it works, in the terminal I get an error because when it is executed 2 times it tells me that there are no changes.
I have managed to get this far:
#!/bin/sh
while read local_ref local_sha remote_ref remote_sha
do
FULL_COMMIT_MESSAGE=$(git log -1 --pretty=%B $local_sha)
MESSAGE=$(echo "$FULL_COMMIT_MESSAGE" | awk '{print $1}')
TOPIC=$(echo "$remote_ref" | awk -F 'topic=' '{print $2}')
# If the value of the topic is not specified in the flag, we obtain it from the CELL identifier
echo "Validating if topic are set"
if [ -z "$TOPIC" ]; then
if echo "$MESSAGE" | grep -iE '^CELL:(ANT|JDRL)' > /dev/null; then
TOPIC=$(echo "$MESSAGE" | awk -F ':' '{print $2}')
echo "The topic are set with value -> ${TOPIC}"
# PROBLEM HERE
git push --push-option=topic=$TOPIC origin "$local_sha:$remote_ref$TOPIC"
exit 0
fi
fi
echo "Validating that the commit set message is correct"
echo $MESSAGE
if ! echo "$MESSAGE" | grep -iE '^CELL:(ANT|JDRL)' > /dev/null; then
echo "ERROR: Commit message must start with 'CELL:ANT' or 'CELL:JDRL'"
exit 1
fi
echo "Validate if exist a topic in this commit"
if [ -z "$TOPIC" ]; then
echo "ERROR: Flag not found -o topic=XXX or commit message does not start with 'CELL:ANT' or 'CELL:JDRL'"
exit 1
fi
done
my problem is in:
git push --push-option=topic=$TOPIC origin "$local_sha:$remote_ref$TOPIC"
isn't there another way to add the topic just to the first push I did in the terminal instead of the pre-push?
Pre-push is a validator, if you want to control the push options you're going to have to script it, probably a git alias is best because it allows easy repo-local overrides.

Bash Script - Determine whether to add file to Git commit [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 months ago.
This is probably a simple one for a bash scripter, which I am not.
I'm running a cron job that downloads some data, and then depending on that data, may or may not modify a second file. After the job, I want to git commit one or both files. For the conditional commit, I tried this in a .sh script:
# attempt to capture whether MyNotes.txt was changed
# by counting lines in git status output
mywc=(git status -s MyNotes.txt | wc -l)
echo $mywc found!
if [ $mywc = 1 ]; then
echo Add file for commit
else
echo Nothing to add
fi
I'm pretty much getting nowhere; this thing seems to fail on the first line with syntax error near unexpected token '|'. If I run git status -s MyNotes.txt | wc -l on the command line, I get the numeric output I expect.
What am I doing wrong and how can I make this work?
If there's a more elegant way to determine whether a file changed, feel free to share.
Also, for my edification, how could I get this to work without the interim mywc variable? I.e., if I wanted to just do the command within the if, something like this:
if [[ $(git status -s MyNotes.txt | wc -l) = 1 ]]; then
...
Thanks!
What am I doing wrong and how can I make this work?
put a dollar before parenthesis.
foo=$(command)
The thing you are using looks like a bash array
declare -a letters=(a b c d)
If there's a more elegant way to determine whether a file changed, feel free to share.
Consider this:
$ git diff -s --exit-code README.md || echo has changed
has changed
$ git checkout README.md
Updated 1 path from the index
$ git diff -s --exit-code README.md || echo has changed
The OR (||) runs if the first command exits with a non-zero code.
Same thing essentially:
$ false || echo false exits with 1
false exits with 1
$ true || echo will not trigger
An aspect of bash that people overlook is that [[, ]], [ and ] are separate commands. They have return codes too. With this knowledge, you can leverage the return codes with if and any other command.
$ if true; then echo yes; else echo no; fi
yes
$ if false; then echo yes; else echo no; fi
no
So for detecting changes in a tracked file:
$ if git diff -s --exit-code README.md; then echo same as in index; else echo changed; fi
same as in index
$ echo 123 >> README.md
$ if git diff -s --exit-code README.md; then echo same as in index; else echo changed; fi
changed
With all of that said...
Just add the file. You don't need to check anything. If it hasn't changed, nothing will happen.
$ echo foo >> myfile
$ git add myfile
$ git commit -m 'maybe changed' myfile
[master b561cc1] maybe changed
1 file changed, 1 insertion(+), 1 deletion(-)
$ git add myfile
$ git commit -m 'maybe changed' myfile
no changes added to commit (use "git add" and/or "git commit -a")
if you need to avoid a non-zero exit code (such as with set -e), just put a || true after the command that you want to ignore the exit status of:
$ cat foo.sh
#!/bin/basho
set -e
echo foo >> myfile
git add myfile
git commit -m 'maybe changed' myfile
git add myfile
git commit -m 'maybe changed' myfile > /dev/null || true
echo no error here. it\'s fine..
false
echo fill never reach this.
Try running that script and see what happens
I search for a way for checking if file changed.
git diff --exit-code -s <path>
Now the bash scripter knows that every command returns a status code which can be checked with $?. In case everything went smoothly, 0 is returned. In that case we get 0 if file is not changed.
Every bash scripter knows too that you can use that with && and || operators (because of lazy evaluation) to write such construct:
git diff --exit-code -s <path> && echo "should add file"
About your edification, what you wrote is perfectly fine!
As CryptoFool pointed out in a comment, I failed to include a $ in my variable assignment. Simple fix in the first line of my script:
mywc=$(git status -s MyNotes.txt | wc -l)
As matt pointed out in a subsequent comment, doing a git add on a file that hasn't changed has no effect. It won't stage the file for commit. So instead of doing conditional logic to determine whether to git add myfile.txt, I'll just blindly execute git add myfile.txt, which will either stage the file if there are changes, or do nothing if there are no changes. Therefore, my entire script can be replaced with one line:
git add MyNotes.txt

Cronjob output avoiding eval echo

I want to run a cronjob to commit and push some changes to a txt file. I created this small bash script,
eval "$(ssh-agent -s)"
ssh-add -k ~/.ssh/git_key
number=$(eval "shuf -i 10-20 -n 1")
echo $number
for i in $(eval echo "{1..$number}")
do
echo "a" >> file.txt
git commit -am "another add"
done
git push
which runs perfectly fine if i run it as
./fakeGit.sh
But as soon as I put it into a cronjob (with crontab -e), like such:
48 17 * * * cd ~/GreenIsCool && ./fakeGit.sh > log.txt 2>&1
It only commit one time, and here is the output from log.txt
Agent pid 11687
Identity added: /home/----/.ssh/git_key (/home/----/.ssh/git_key)
18
[master 5dcc943] another add
1 file changed, 1 insertion(+)
To github.com:------.git
4362432..5dcc943 master -> master
It feels like the loop is not working properly, I feel like it is because the echo may be redirected to the wrong place (because the cronjob is outputing to log.txt), but I cannot find a way to avoid this. How would I correct the script so it works with > log.txt ?

SVN Pre Commit search for a string in file

I am trying to create a pre commit hook for a SVN repo that will prevent a commit if the version number contains 7.5
VERSIONNUM="<COMMENT>7.5"
grep -Fwq "$VERSIONNUM" APP.CFG
then
exit 1
else
exit 0
fi
The version number is located in the APP.CFG file and the start of the line is 7.5. I do not want to match the entire line which is why I am using -w in my grep which I think just returns a match if $VERSIONNUM is located anywhere in the file
here is the contents of the APP.CFG file
> <?xml version="1.0" encoding="utf-8"?> <APP AUTH="" PRODUCTS="2"
> VER="hmidesigner"> <VERSION AUTHOR="CODRA" DATE_NEUTRAL="01/11/2019
> 09:48:48" ID="CODRA.Panorama.Persist" SERIAL_VERSION="7">
> <COMMENT>7.5.17.0</COMMENT>
Got it to work with a slightly different approach than what I was originally going for
SVNLOOK diff -t "$TXN" "$REPOS" | grep -i "<COMMENT>7.5." > /dev/null && { echo "Trying to Commit version 7.5" 1>&2; exit 1; }
exit 0;

read a value from list and writing it in command line in shell script [duplicate]

This question already has answers here:
List files and show them in a menu with bash
(2 answers)
Closed 6 years ago.
Need some help in reading the value from list and writing it as an input to the next command.
I need to write a shell script where it needs to have
git tag -l --- which would list out all tags or version numbers like:
v1.0
v2.0
v3.0
v4.0
I need to read one of the value from the list output say, v3.0 and pass it to the next command
read v3.0
git checkout <version number> example: git checkout v3.0
How do i achieve it in shell scripting? Pls help
Reading your last comment, I think that #fedorqui is right. This question is a duplicate: you would like to build a menu and prompt the user. In your situation you should use options=( $(git tag -l) ).
The script at List options in menu in bash becomes:
#!/bin/bash
prompt="Please select a file:"
options=( $(git tag -l) )
PS3="$prompt "
select opt in "${options[#]}" "Quit" ; do
if (( REPLY == 1 + ${#options[#]} )) ; then
exit
elif (( REPLY > 0 && REPLY <= ${#options[#]} )) ; then
echo "You picked $opt which is file $REPLY"
break
else
echo "Invalid option. Try another one."
fi
done
git checkout $opt
----
First answer for History.
The command you are looking for is xargs:
git tag -l | fgrep v3.0 | xargs -I xxx git checkout xxx
I have to warn you, if more than one tag contain v3.0 then git checkout will be executed more than once.

Resources