Batch adds additional \ in cp path - windows

I wrote a batch script, which is branching my project for svn and want to create docs with sphinx which I then copy into the branched path.
The part where I do this is:
ECHO ##### Creating Docs Branch %VERSION%
cd %~dp0\trunk\Python\MyPackage\docs
call make html
cp -r _build\html %~dp0\branches\%VERSION%\Python\docs\MyPackage
svn add %~dp0\branches\%VERSION%\Python\docs\MyPackage
svn commit %~dp0\branches\%VERSION%\Python\docs\MyPackage -m "Added Docs Branch %VERSION%"
This fails with cp: cannot mkdir "D:/00_Workspace/MyProject/MyPackage//branches/1.1.0/Python/docs/MyPackage": Path could not be found
Apparently there is a second / added between MyPackage and branches
Why does this happen?
Here is the full version of the batch
#echo off
set "VERSION=%1"
set "COMMON_VERSION=%2"
if "%VERSION%"=="" set /p VERSION=Enter the version number you want to create the branch for?:
if "%COMMON_VERSION%"=="" set /p COMMON_VERSION=Enter the version number of the common python toolchain you want to use?:
set PACKAGE_NAME=%~dp0
ECHO ##### Updating SVN Repository
svn update
ECHO ##### Creating requirements.txt
cd trunk
call python create_requirements_txt.py %VERSION% %COMMON_VERSION%
svn add requirements.txt
svn commit requirements.txt -m "Updated requirements.txt to version %VERSION%"
cd %~dp0
ECHO ##### Creating Branch for version %VERSION%
for /f "delims=" %%i in ('svn info ^| FINDSTR /R /C:"Relative URL"') do set URL=%%i
set "URL=%URL:*: =%"
for /f "delims=" %%i in ('svn info ^| FINDSTR /R /C:"Repository Root"') do set ROOT=%%i
set "ROOT=%ROOT:*: =%"
set "DEST_URL=%ROOT%%URL:*\trunk=%/branches/%VERSION%"
set "SRC_URL=%ROOT%%URL%/trunk"
ECHO ##### Package: %PACKAGE_NAME%
ECHO ##### Source: %SRC_URL%
ECHO ##### Destination: %DEST_URL%
call svn copy %SRC_URL% %DEST_URL% -m "Branch %PACKAGE_NAME% Version %VERSION%"
ECHO ##### Succesfully created branch %VERSION%
call svn update
ECHO ##### Creating Docs Branch %VERSION%
cd %~dp0\trunk\Python\MyPackage\docs
call make html
cp -r _build\html %~dp0\branches\%VERSION%\Python\docs\MyPackage
svn add %~dp0\branches\%VERSION%\Python\docs\MyPackage
svn commit %~dp0\branches\%VERSION%\Python\docs\MyPackage -m "Added Docs Branch %VERSION%"
pause
exit /b 0
:SVN_ERROR
ECHO ##### SVN Error: Either Source Repo (%SRC_URL%) does not exist or the Tag (%DEST_URL%) already exists!
pause
exit /b 1

As #elzooilogico said
%~dp0 contains drive letter plus path from where script is run ended with backslash. change svn add %~dp0\branches\%VERSION%\Python\docs\MyPackage with svn add %~dp0branches\%VERSION%\Python\docs\MyPackage and all similar paths.
that should work.

Related

Git annoyingly persistent in repo

I'm creating a project that covers frontend, backend, and mobile. I've set up git in the project root so all phases could be committed. However, I bootstrapped the frontend folder with create-react-app, which creates a project structure of its own (including git).
As it happens, when there's a git inside a git, it is interpreted as a subproject. I just wanted to commit them all to the same repository. I, therefore, proceeded to eliminate all git references from the frontend folder to have it only in the root.
I tried every trick I could think of and it still hasn't worked, the files are still tracked separately. Here's a list of commands I tried both in Powershell and Git Bash:
rm -rf .git
rm -rf .git*
find . | grep "\.git/" | xargs rm -rf
del /F /S /Q /A .git
rmdir .git
rm -rf .gitkeep
rd .git /S/Q
FOR /F "tokens=*" %G IN ('DIR /B /AD /S *.git*') DO RMDIR /S /Q "%G"
Any hints of how getting it the hell out of my folder?
Thanks!
Removing the .git is a good first step, but what you are missing is the gitlink (special entry in the index of your main repo referencing the SHA1 of the root tree of the subrepo)
You need to add:
git rm subrepoRootFolder # no trailing /
git commit -m "remove subrepo gitlink"
Then you can git add ., and all files within subrepoRootFolder will be (finally) added to your main repository. Then commit and push.

bat script to unzip .gz file in a location and extract to another location

I need to know how to write bat script to unzip file in a location and save the extracted file to a different location, also I need to delete the original file after the extraction is done.
For example suppose my .gz file are in location C:\Users\jibin\Desktop\CDR\ and I need to extract all the files in this location to C:\Users\jibin\Desktop\CDR\CDR_Extract(destination).
This can easily be achieved using 7-zip, combined with forfiles, something like (not tested, don't kill me if it doesn't work immediately):
forfiles /M *.gz /C "cmd /c 7z x #path -oC:\Users\jibin\Desktop\CDR\CDR_Extract *.* -r"
Where:
x means "extract"
-o means "output directory"
*.* means "filename inside can be anything, extract every file"
-r means "recursive subdirectories"
Deleting the zipfile afterwards is obvious.
Without need to install third party software - you can use gzipjs.bat:
md "C:\Users\jibin\Desktop\CDR\CDR_Extract\" >nul 2>&1
for %%# in ("C:\Users\jibin\Desktop\CDR\*.gz") do (
call gzipjs.bat -u "%%~f#" "C:\Users\jibin\Desktop\CDR\CDR_Extract\%%~n#"
rem del "%%~f#" /q /f
)
Check if this is what you need and delete the rem word in the last file in order to activate deletion of the source files as you requested.
If you are using windows 10 with all the latest updates (from build 17063 and above) you can use TAR.
If you're using windows-10, version 1803+, (December 2017 to April 2018 - minimum 10.0.17063), then you may be better advised to use the built-in tar executable:
#If Exist "C:\Users\jibin\Desktop\CDR\*.gz" (
If Not Exist "%__AppDir__%tar.exe" Exit /B 1
PushD "C:\Users\jibin\Desktop\CDR"
If Not Exist "CDR_Extract\" MD "CDR_Extract" 2>NUL || Exit /B 1
For %%G In (*.gz) Do #("%__AppDir__%tar.exe" -xf "%%G" -C "CDR_Extract"
If Not ErrorLevel 1 Del "%%G")
PopD)

Batch file quits unexpectedly

I am trying to make a bat file that will grab a git repository and download it to a specific directory.
The error I get when running this script is:
' was unexpected at this time.
Here is my code:
#echo off
TITLE Starter Kit
cls
echo "Note: This script will download the 'CodeKit-Starter-Kit' repository from #NicholasAdamou's GitHub."
set /p response="Do you want to continue? <y/n>"
if /i "%response%"=="y" (
goto :downloadKit
:downloadKit
cls
echo "Downloading the CodeKit-Starter-Kit repository from #NicholasAdamou's GitHub."
set "filePath=%~dp0"
cd %filePath%
cd ../dist
git clone https://github.com/NicholasAdamou/CodeKit-Starter-Kit.git 'StarterKit (CodeKit)'
)
if /i "%response%"=="n" (
call exit
)
You need to escape the parenthesis of the git line, like this:
git clone https://github.com/NicholasAdamou/CodeKit-Starter-Kit.git 'StarterKit (CodeKit^)'
This is because batch views closing parenthesis as special characters
I don't understand
set "filePath=%~dp0"
Try to substitute single quotes in git command with doublequotes .
Anyway , we are in 2016 , it's time to learn powershell :-)
$filepath="$home\\dp0"
read-host -prompt "Do you want download it ? " -outvariable response
if ( $response -eq "y"){
set-location "$filepath\\dist"
git clone https://github.com/NicholasAdamou/CodeKit-Starter-Kit.git 'StarterKit (CodeKit)'
}

How to get the latest git tag on Windows?

I want to checkout the latest git tag on Windows.
I use the following command on Linux:
git checkout `git describe --tags --abbrev=0 origin/master`
Whats the equivalent for this command on Windows?
I tried using for & set to expand the backticked command into a variable for usage on the git checkout.
I started out with:
for /f %a in ('git describe') do (echo %a).
It returns v1.0.18-131-g792d5a2. This works, but contains a bit too much information. I just need v1.0.18. That's why i'm trying to use --abbrev=<n> to suppress the long format and only show the closest tag.
When i added the arguments:
for /f %a in ('git describe --tags --abbrev=0 origin/master') do (echo %a)
it fails with: fatal: Not a valid object name 0. Not sure why...
Here is what i got so far:
checkout-latest-tag.bat
#echo off
for /f "usebackq delims=" %%a in ('git describe --tags --abbrev=0 origin/master') do (
set LATEST_TAG=%%a
)
echo The latest tag is: %LATEST_TAG%
git checkout %LATEST_TAG%
It would be nice, if you could come up with a one-liner.
Use ^ to escape the offending =.
for /f %%a in ('git describe --tags --abbrev^=0 origin/master') do git checkout %%a

Create archive of modified files in GIT via batch file

I'm using this git command to create an archive of the files modified within a specific commit:
git archive -o update.zip HEAD $(git diff --name-only COMMITID^)
where COMMITID is the id of the commit I'm looking to archive. This works fine from the command line but I would like to run it from a batch file. Here are the contents of the batch file I'm using:
git archive -o update.zip HEAD $(git diff --name-only %1^^)
where %1 is the commit id being passed in via SourceTree. The problem I'm having is that this command when run from a batch file returns the following error:
error: unknown option `name-only'
I'm guessing there may be some character escaping issues going but I'm unable to find what is specifically breaking.
How would I write that git command so that it will work from a batch file?
UPDATE
I tried removing the --name-only option and received the following error when trying the batch script via SourceTree:
fatal: path not found: $(git
Hopefully that helps narrow down what may be going on.
FURTHER UPDATE
Turns out my syntax was wrong for grabbing only the modified files from a specific commit but using msandiford's answer I came up with this batch file script that works perfectly:
setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff-tree --no-commit-id --name-only -r %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal
Assuming you need a windows batch file, and not a bash script, here is a minimal batch file that may do what you want:
setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff --name-only %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal
It works by collecting all lines of the output of the git diff ... command into an environment variable, and then using this to perform the git archive ... operation.
Documentation for the Windows for command is here, including the /f switch. Documentation for the setlocal command is here.

Resources