git keyword substitution under windows using gnutools (gnuwin32) - windows

I just converted my SVN repository to git. For the management I use keyword-substitution.
Excerpt from the git config
smudge = "set author=`git log --pretty=format:%ae -1`; SET last_date=`git log --pretty=format:\"%ai\" -1`; SET version=`echo $lastdate | cut -d \" \" -f 1-2 | sed -e \"s/[ -:]/./g\"`; sed -e \"s/[$]Revision[$]/\\$Revision: $last_date \$/\" -e \"s/[$]Date[$]/\\$Date: $last_date \$/\" -e \"s/[$]Author[$]/\\$Author: $author \\$/\" "
clean = sed -r -e 's/([$]Revision|Date|Author)(:[^$]+ [$])/\\1$/'
Source: https://github.com/np-trivial/git-keyword-substitution
This solution should in principle also run under Windows, since I also use gnuwin32. Tools are accessible in the system environment variable.
Unfortunately I always get an error message. As far as I could isolate it is because of the above code.
I just have no idea what the problem is.

You do not need gnuwin32.
Content filter driver would work from a regular CMD session, and would be executed with the git bash included with Git for Windows.
That means your smudge/clean scripts should be in bash.
set xx= is a BAT assignmemnt. xx=... is a bash assignment.
Try:
smudge = "author=$(git log --pretty=format:%ae -1); last_date=$(git log --pretty=format:\"%ai\" -1); version=$(echo $lastdate | cut -d \" \" -f 1-2 | sed -e \"s/[ -:]/./g\"); sed -e \"s/[$]Revision[$]/\\$Revision: $last_date \$/\" -e \"s/[$]Date[$]/\\$Date: $last_date \$/\" -e \"s/[$]Author[$]/\\$Author: $author \\$/\" "
clean = sed -r -e 's/([$]Revision|Date|Author)(:[^$]+ [$])/\\1$/'
In other words, remove the set.
However, the OP ozz confirms in the comments that SmartGit does not support content filter driver.
A simple git checkout or git switch does triggers it (successfully) in command-line though.

Related

Set environment variable on branch checkout/switch

I'm using the post-checkout hook to try and set environment variables when switching branches.
#!/bin/sh
echo "Updating environment variables..."
OLD_IFS=$IFS
IFS=$'\n'
for x in $(cat .env | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g")
do
var_name=$( cut -d '=' -f 1 <<< "$x" )
export $x
pwsh.exe -c "\$env:$x"
pwsh.exe -c "echo 1; echo \$env:$var_name"
export $x
done
IFS=$OLD_IFS
The problem is that git hook is executed with WSL so the variables I set are lost after the post-hook
I assume this is because of the shebang?
I've tried #!/usr/bin/env pwsh but I get the error Processing -File '.git/hooks/post-checkout' failed because the file does not have a '.ps1' extension. Specify a valid PowerShell script file name, and then try again.
Is this something that can be done? I want to automatically change the DB connection when I switch branches.
As anthony sottlie noted, you can't do it that way.
What you need instead is a command that you run instead of git switch or git checkout. In this command, you will:
run git switch or git checkout, then
set the environment variables you would have set in your script, the way you would have set them
and since this will be done by the command itself, rather than in a subprocess, it will affect further commands run by this same command-line interpreter.

sed: can't read ../../build.gradle: No such file or directory

I am new to git and github. I am working on a project where I need to commit my changes to github repository in a specific branch.
But I am getting the error
$ git commit
3.5.0.1
s/3.5.0.1/3.5.1.1/g
sed: can't read ../../build.gradle: No such file or directory
I have also attached the pre-commit file code here.
#!/bin/sh
## finding the exact line in the gradle file
#ORIGINAL_STRING=$(cat ../../build.gradle | grep -E '\d\.\d\.\d\.\d')
## extracting the exact parts but with " around
#TEMP_STRING=$(echo $ORIGINAL_STRING | grep -Eo '"(.*)"')
## the exact numbering scheme
#FINAL_VERSION=$(echo $TEMP_STRING | sed 's/"//g') # 3.5.0.1
#Extract APK version
v=$(cat build.gradle | grep rtVersionName | awk '{print $1}')
FINAL_VERSION=$(echo ${v} | cut -d"\"" -f2)
echo ${FINAL_VERSION}
major=0
minor=0
build=0
assets=0
regex="([0-9]+).([0-9]+).([0-9]+).([0-9]+)"
if [[ $FINAL_VERSION =~ $regex ]]; then
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
build="${BASH_REMATCH[3]}"
assets="${BASH_REMATCH[4]}"
fi
# increment the build number
build=$(echo $build + 1 | bc)
NEW_VERSION="${major}.${minor}.${build}.${assets}"
SED_ARGUMENT=$(echo "s/${FINAL_VERSION}/${NEW_VERSION}/g")
echo $SED_ARGUMENT
sed -i -e `printf $SED_ARGUMENT` ../../build.gradle
The error comes in the last line of this file basically. I am using windows.
Things I tried:
sed -i -e `printf $SED_ARGUMENT` ../../build.gradle
sed -i ' ' -e `printf $SED_ARGUMENT` ../../build.gradle
I am unable to understand where am I actually doing wrong. Kindly help me out.
sed: can't read ../../build.gradle: No such file or directory
This one is rather simple. Your build.gradle file is not at ../../build.gradle.
The solution is to determine actual path to the build.gradle file relative to the script, and change the path in the script.
To debug this, do echo Current Directory: $PWD in the script to see what the actual working directory is, then you should be able to determine the correct path to use.

.bash_profile command arguments

I was looking for a redhat Tree command workaround and keep finding this specifically
alias lst='ls -R | grep ":$" | sed -e '"'"'s/:$//'"'"' -e '"'"'s/[^-][^\/]*\//--/g'"'"' -e '"'"'s/^/ /'"'"' -e '"'"'s/-/|/'"'"
I'm trying to add files to the output tree and I have no idea what any of this is saying, could someone tell me what this command is saying and how to add to it?

Can't execute ls using shell script

I'm trying to parse vsftpd logs to do some extra processing on the successfully uploaded files.
username will be the user so I create the home dir
filename is the file name in the log: it gives a wonky result i.e. "/foo.txt" but that doesn't matter
#!/bin/sh
sudo tail -F /var/log/vsftpd.log | while read line; do
if sudo echo "$line" | grep -q 'OK UPLOAD:'; then
username=$(echo "$line" | cut -d" " -f8 | sed 's/\[\(.*\)\]/\1/')
filename=$(echo "$line" | cut -d, -f2 | sed 's/^[ \t]*//')
home="/home/vsftpd/$username"
if sudo ls "$home$filename" &> /dev/null; then
# do something with $filename
echo "some text"
fi
fi
done
When a file is uploaded I expect the text "some text". I never get that instead I can see it reports:
ls: cannot access /home/vsftpd/user1"/foo.txt": No such file or directory
Although I can run the command in the shell:
$ sudo ls /home/vsftpd/user1"/foo.txt"
/home/vsftpd/user1/foo.txt
I'm guessing permissions related but I've got it running as sudo and I've given the directories full access. Any ideas?
Your problem is that you have an extra set of quotes around the file name component that you need to strip. The file name in the vsftpd logs (just verified this for myself) is surrounded with quotes, and unlike with username you're not removing those quotes.
This means that $filename ends up being set to, literally, "/foo.txt" including the quotes. When you construct the file name for ls with "$home$filename", the variables are interpolated, but the shell isn't then going to strip off another level of quotes. The quotes stay in the final file name, and the directory /home/vsftpd/user1" with the trailing quote doesn't exist.
This works when you enter the command from the shell because you aren't quoting the file name, so the shell does another round of quote interpolation and removes the double quotes.
If sudo works from the shell, it's possible that sudo has the NOEXEC flag set, which prevents it from executing scripts. You can read more about NOEXEC here.

Using sed to swap Windows location for Mac

I am writing a bash script for an automator service that will take a Windows directory location and change it to Mac and open a finder window. It's working except for when it hits folders with spaces. I have put in to remove them but it won't work on anything with spaces still. I must have made some sort of syntax mistake.
sed -e 's:\\\\fmg_cifs1\\Dept_Shares:/Volumes/Dept_Shares:' -e 's: :\ :g' -e 's:\\:/:g' | pbcopy
TAG=$(pbpaste)
cd $TAG; open .
This is almost certainly all you have to change:
cd "$TAG"
Quoting fixes everything!
Do you need to use pbcopy and pbpaste and a variable?
cd "$(sed -e 's:\\\\fmg_cifs1\\Dept_Shares:/Volumes/Dept_Shares:' -e 's: :\ :g' -e 's:\\:/:g')"
As Jonathan pointed out, some of the sed command is unnecessary. Of course, something needs to be fed to sed. This may be all you need:
cd "$(echo "$dir" | sed -e 's:\\\\fmg_cifs1\\Dept_Shares:/Volumes/Dept_Shares:')"

Resources