Very basic git batch in Windows 7 - windows

Synopsis
Pass arguments to git bash using a .bat file
OR
Use git some other way by running a batch file.
Disclaimer
There are a lot of answers to similar questions out there, but having tried and failed most or all of them, I hope you don't smack me with a DUPLICATE stamp at first sight.
My system
Windows 7, 64 bit
git version 1.9.4.msysgit.2
The challenge
I want to rationalize my workflow from navigating to a number of git repos and running git status, git add --all etc, to simply run a batch.
Other posts certainly suggests it's possible. I think most of them use Unix, but this one at least got me up and running on Windows so I could test it out. However, I'm having the same problems as OP in that post when it comes to passing the commands to the git bash, and to a complete beginner like me it seems a bit complicated to use the suggestions from #Erik further down in the same post if you want to include more commands in the work flow.
Following the suggestions from #inf3rno in the same post, it seems I'm able to change folders but not able to use git commands like this:
set bash=C:\Program Files (x86)\Git\bin\bash.exe
"%bash%" --login -i -c "exec "%1""
cd c:\repos\research
git status
pause
Does anyone have suggestions as to how I can change the code above to get it working?
And by the way, what's the deal with #!bin\bash from other posts like this one? I assumed I had to use #!Program Files (x86)\Git\bin\bash, but I had no luck with that either...
Anyway, thank you for any suggestions!

First, uninstall git 1.9.4 (msygit, which is now obsolete): git-for-windows will offer a much recent bash (2013 vs. 2005).
Simply unzip PortableGit-2.6.1-64-bit.7z.exe anywhere you want, add C:\path\PortableGit-2.6.1-64-bit\bin to your %PATH% and you are good to go.
Second, inf3rno's answer is about executing any bash script ("%bash%" --login -i -c "exec "%1"": the %1 is the path/name of the bash script to be executed)
The right shebang to use in your bash scripts would be #!/bin/bash (see "What is the preferred Bash shebang?")
With the latest git 2.6, that would be:
c:\prgs\git\PortableGit-2.6.1-64-bit\bin\bash.exe --login -i -c "exec ./myscript"
Since that folder is supposed to be in your %PATH%:
bash --login -i -c "exec ./myscript"
With myscript being a file using Unix-style eol (LF), not Windows-style (CRLF)
Note also that any bash script (even on Windows) called git-myscript can be directly called with:
git myscript
I described in 2012 another approach in "Running a batch file in git shell" for executing git command.
But for a pure bash script, you will want to go with bash --login -i -c "exec ./myscript".
For writing bash scripts with unix eol style, you can choose various editor from Notepad++, SublimeText 3 or Atom.io.

Related

Preserving PS1 while in SSH with GitBash?

I have a complicated prompt. Very. I use git-bash for windows.
SSH-ing in on gitbash sends me to the CMD prompt. I know to type ssh -t user#host "bash -l" to get a bash prompt. It works, however, I use a repository called gitstatus to speed up the parsing of the git commands, and sshing in using bash -l calls the PS1 set in /c/cygwin/etc/bash.bashrc (for Mintty), NOT ~/bash.bashrc (for GitBash). This initially seems fine, as I can just copy paste the code from the GitBash *.bashrc to cygwin's. However, the gitstatus repository only works on bash terminals, aka not Cygwin/Mintty, so the prompt when I ssh in appears quite slower in git repos (Mintty doesn't allow gitstatus to be sourced and speed up parsing, once again) than if I was not SSH-ed in and using the GitBash-sourced prompt.
Are there any work arounds for this? I have seen many simnilar questions here but none have provided me a solution.
If you're in my situation, just source the scripts (duh). If you can't execute foo in file x when it should do the same thing as bar in file y, just source bar from file x. It seems obvious but took me a moment.
ALSO: Make sure to run dos2unix.exe on admin command prompt to remove carriage returns every time you make a change.

Run shell script in WSL with \r\n

I have a Python project which is a bunch of Docker containers and terraform scripts. My workstation is Windows 10 so I have installed Ubuntu in WSL to develop it. The problem I am facing is there are a lot of shell scripts (.sh) that I need to run as the build process. In Git it always checks out the files as Windows style and commit files as Unix Style. As a result, the Ubuntu bash shell does not like these shell scripts and I have to run dos2unix before running a file. Is there a one liner that allows me to run these scripts without modifying them?
If the scripts don't call another script you could
tr "\r\n" "\n" script.sh | bash
But in the other case I think you need to change your repository config to tell git to don't convert files with .sh extension. Look at this answer https://stackoverflow.com/a/39461324/5032541
If you want to change git configuration to remove auto conversion, take a look on official documentation. But it's a bad idea.
The main problem imo is checking in the wrong file endings into git.
An easy way to fix all shell scripts at once is:
find . -type f -name "*.sh" -exec dos2unix {} \;
It will recursively find all files with the .sh extension and perform dos2unix on it.
I would recommend setting your git repo to checkout linux style rather than windows line endings.
I do a lot of node/docker/linux work but have a windows 10 laptop, this is what I have done.
git config --global core.autocrlf input
See this link for some more details. This way you dont need to continuously run scripts to make sure you have the correct line endings. Also a good thing to do is set you editor to default to linux line endings.

`gitk` program found using the `where` command but cannot be executed?

I'm trying to run gitk on Windows but the console gives me:
'gitk' is not recognized as an internal or external command,
operable program or batch file.
But then, I can do
> where gitk
C:\Program Files (x86)\Git\bin\gitk
How comes?
UPDATE: Stealing the OP's comment:
Thanks for a (lengthy) explanation, the easiest fix for me was to move from the old msysGit (1.9.5) version to the newer "Git for Windows" builds which has a proper Windows wrapper for gitk.
gitk, unlike most git commands, is a shell script that invokes Tcl.
If you examine the gitk executable file itself, its first few lines are:
#!/bin/sh
# Tcl ignores the next line -*- tcl -*- \
exec wish "$0" -- "$#"
(At least that's what it looks like on my Linux system; the Windows installer for gitk might change that.)
On a Unix-like system, that first line, known as a "shebang", tells the system to invoke /bin/sh to execute the file rather than executing it directly.
The third line executes the wish command, which executes the script as a Tcl script.
If you have Tcl installed on your Windows system, you should be able to run gitk as a Tcl script using something like this:
C:\> tclsh "C:\Program Files (x86)\Git\bin\gitk"
(Disclaimer: I have not tried this.)
You might also be able to change the command's name from gitk to gitk.tcl and set up a file association in Windows so it will be invoked correctly. (I'm a little surprised that the Windows git installer didn't do this for you.)
More information on running Tcl scripts on Windows can be found here.
And this question discusses invoking gitk on Windows from the context menu rather than from the command line.

msysgit: Prevent the Git bash from locking files of running shell scripts

I have some scripts that are (always) running during development. Trying to write to these files in Windows 7 fails (git pull fails with Permission denied error). Everything works fine in Linux.
Can I prevent Git bash from locking the files without changing my global UAC settings?
What other alternatives do I have (besides fixing/hacking the Git bash or Windows)?
Here is an example script as basis for the discussion:
#!/bin/bash
echo "command + args: $0 $1 $2 $3"
while sleep 2; do date +%s; done
I found a somehow related topic. But unfortunately it did not provide a suitable solution, e.g., Run as Administrator does not help.
Another interesting observation is that I can still write the files when editing them in vim using :w! (forcing an overwrite).
You could simply run a copy of the file.
cp foo.sh /usr/local/bin
foo.sh
This would allow you to edit the file in the repo while the other is running. You could also try a hard link.
ln foo.sh /usr/local/bin
foo.sh

What is wrong with this .command file?

This is my script to automate the git push of my static blog.
When I run each command one by one in the Terminal, it works. Do I need to add delays, maybe it's going too fast. The pelican command (static website generator) takes quite a lot of time (2 seconds). Is the rest of the script crashing during that?
#!/bin/sh
dropbox
cd blog
pelican . -s /Users/Paul-Arthur/Desktop/Desktop/Dropbox/blog/pelican.conf.py -t subtle
cd output
git add .
git commit -m 'commit'
git push
Updated: Sorry, yeah dropbox is a custom command in my bash_profile (this is not the problem, it works I know ;) ).
Sadly, when I click my script, it executes (but does not work) extremely quickly so I cannot see the errors.
Here is the output from the calepin command. The errors are normal and I expect it to run with that. Do you think that this is the problem? If so what can I do?
familys-imac:blog Paul-Arthur$ pelican . -s /Users/Paul-Arthur/Desktop/Desktop/Dropbox/blog/pelican.conf.py -t subtle
ERROR: Skipping ./articles/aboutme.md: impossible to find informations about 'title'
ERROR: Skipping ./articles/static_sites.md: impossible to find informations about 'title'
familys-imac:blog Paul
It might be due to the « cd » command, since it's not a command, it is a builtin from the shell, and doesn't act like a command.
To debug it, try adding «pwd» command inside your script before and after the «cd» line, to be sure the working directory has change.
It could also be due to the shell you are using, in the shebang (first line of your script), you are using the /bin/sh script. Is it the good one ? When you do it in your shell, you are maybe using another like bash, dash, zsh etc.
To determine that, type that in your current shell :
which `echo $0`
You will get an answer like :
/bin/bash
or something like this. Use this in your shell script :
#!/bin/bash
And try again your script.
Good luck with your project.

Resources