How to copy files in svn into a new directory that depends on a variable value? - powershell

I'm currently writing a script in PowerShell which calculates a new tag in the tag directory. I want to copy files from one SVN directory to another directory that depends on the new tag number I calculated.
Here are the lines from the script:
$tag = Write-Host "$($svnMavenTagPrefix)$($nextMavenTagVersion)"
svn copy http://tlvsvn1/svn/repos-bls/MassAnalytics/trunk/ http://tlvsvn1/svn/repos-bls/MassAnalytics/tags/${tag}
For some reason it doesn't work and I receive the following error:
svn: E205007: Could not use external editor to fetch log message; consider setting the $SVN_EDITOR environment variable or using the --message (-m) or --file (-F) options
svn: E205007: None of the environment variables SVN_EDITOR, VISUAL or EDITOR are set, and no 'editor-cmd' run-time configuration option was found
How can I copy the files to a new tag?

The Write-Host cmdlet prints the string you are passing as an argument but doesn't write anything to the output thus $tag is empty. I would recommend you to use a format string:
$tag = '{0}{1}' -f $svnMavenTagPrefix, $nextMavenTagVersion
$url = 'http://tlvsvn1/svn/repos-bls/MassAnalytics/tags/{0}' -f $tag
svn copy http://tlvsvn1/svn/repos-bls/MassAnalytics/trunk/ $url

Related

Execute "SVN Copy Versioned Items Here" within Powershell

Within a PowerShell script I need to do the following:
Execute the "SVN Copy Versions Items Here" RMB command for copying an earlier version of File A to a later version of File A in order to compare the before / after prior to a commit.
I know the full path of both files, and the paths / call look something like this:
$fromFile = C:\trunk\v100\fileA.txt
$toFile = C:\trunk\v200\fileA.txt
If I execute the following command:
svn copy $fromFile $toFile
I get the following error: svn: E155010: Path 'C:\trunk\v200\fileA.txt' is not a directory
I believe $fromFile and $toFile are both stored as text.
Any suggestions?
I was able to figure this out. Posting solution in the event anyone comes across the same issue. The error message displayed was a red herring - it has nothing to do with a directory.
Note: In my example, the $toFile had been added via a Subversion Add.
Resolved in the following manner:
svn revert $toFile
Remove-Item $toFile
svn copy $fromFile $toFile
It seems like if the file is present an error is thrown. The revert and remove calls fixed this.

Azure devops - update json file - powershell script

I have created powershell script to update json file with variables. Json file is located in Azure devops repo, json file name var.json.
I am going to use this solution in azure devops, so I built pipeline and set test variable in variables tab in azure devops:
In my script I have param and variables blocks, presented below:
param(
[Parameter (Mandatory=$true)]
[String] $FileRes
)
#env variable
$Path = $Env:BUILD_SOURCESDIRECTORY
# Download variables from Json file
$JsonBase = #()
$JsonPath = "$Path\Var.json"
$JsonBase = Get-Content $JsonPath | out-string | ConvertFrom-Json
$JsonBase.FileNames[0].value = $FileRes
in my script I use commands: $JsonBase | ConvertTo-Json | Set-Content -Path $JsonPath to direct output to json file.
Json file structure:
{
"FileNames": [
{
"value": "AAAbbbccc123",
"value1": "www",
"value3": "swd",
"value4": "xvb"
}
]
}
Pipeline's status at the end is ok, all steps are green, but var.json file is not updated as I wanted. There is still old value --> "value": "AAAbbbccc123"
In fact, it has been replaced, but you need to see this change in the output repos.
For more clearly, you could use private agent to run this build. Then go the corresponding local repos and check the Var.json file after the build finished:
In your script, you are Set-Content into the file which exists under the $(Build.SourcesDirectory)\Var.json, not the one which stored in VSTS repos. So, to check whether it is replaced successfully, please go your output repos, the one in agent.
Sometimes, if what you used is hosted agent, you may could not view the detailed output repos since the host image will be recycled by the server after the pipeline finished.
At this time, you can add another script in it to print the JSON file content out, then you could check whether it is replaced successfully:
$content= Get-Content -Path $JsonPath
Write-Host $content
In addition, please make a little change into your script:
$JsonBase.FileNames[0].value = "$(FileRes)"
Here please use $(FileRes) instead of $FileRes, since you specified the value in the Variables tab. And do not forget the double quote "".
Update:
To sync the output repos change back into VSTS repos, try follow:
(1) The first command line task:
git config --global user.email "xxx#xx.com"
git config --global user.name "Merlin"
cd $(Build.SourcesDirectory)
git init
(2) In powershell task, execute set-content script.
(3) In second command line task, do git push to push the changes:
git add Var.json
git commit -m "aaaa"
git remote rm origin
git remote add origin https://xxx#dev.azure.com/xxx/xxx/_git/xxxx
git push -u origin HEAD:master
In addition, to run git script successfully in pipeline. Beside enable “Allow script to access........” you also should follow this permission setting.

Compare files stored on svn with files on local drive

I am trying to write a script on PowerShell that will compare svn files with local files and then to display the files that have changes. I would like also to display files name that exist on local drive but not on svn location.
My code is:
$list1 = (svn list C:\Users\name\Desktop\Workingfiles\)
foreach ($thisFile in $list1){
Write-Host $thisFile
$var = (svn diff C:\Users\name\Desktop\Workingfiles\$thisFile svn://server/files/Users/name/$thisFile )
# Write-Host $var
}
You can use the svn diff command with --old and --new options. However, it seems that you can not diff repository item with a local unversioned one. You should be able to diff a file from working copy with unversioned file, though.
But what about writing repo file's content into a file? Here is an example:
svn cat URL-TO-FILE > file-from-repo.txt
svn diff --old file-from-repo.txt --new local-file.txt

Load file from GitHub in PowerShell

I have written a PowerShell script which loads a json file and performs certain function on it. I am using:
$json = Get-Content 'C:\Users\Documents\test.json' | Out-String | ConvertFrom-Json
to load the file which works. But I want to store both these files in a git hub repository. How can I access the json file path once I store both the files in the same directory in a GitHub repository?
I am new to using GitHub so any help would be appreciated.
If you want to work with a remote path, you can use the webclient to download the file as a string and convert it using the ConvertFrom-Json cmdlet:
$jsonPath = 'https://raw.githubusercontent.com/jaypat/documents/master/test.json'
$json = (New-Object System.Net.WebClient).DownloadString($jsonPath) | ConvertFrom-Json
You might need to either use the Github API or commit and push using git. For the latter you should do something like the following from the local git repository:
git add test.json
git commit -m "Message for what changes you've made"
git pull
git push origin master
It'd be really helpful if you first read and understood what git is and how it works. Then you'd be ablt to use github seamlessly. Here is a great interactive tutorial for it: https://try.github.io/levels/1/challenges/1
I ended up using path binding to reference the file
$path = join-path $psscriptroot "env.json"
This sources to the directory where all the files are loaded.

How can I store the output of a command as a variable in PowerShell?

Say I execute the command:
git pshu
which will give me:
git: 'pshu' is not a git command. See 'git --help'.
Did you mean this?
push
If this was the last command I executed, I want retrieve the command and store its outputs in a variable for further use.
So far I have tried things like:
$var = echo (iex (h)[-1].CommandLine)
$var = iex (h)[-1].CommandLine | Out-String
etc.
How can I save such output to a variable?
Lets say you are running multiple commands, and you want to store store a run command based on its input.
Lets say you want to store non existing git commands, as git pshu. My proposal will be to store the command and search for the desired pattern on the output as "Did you mean...". In this case, you can follow as:
$output = git pshu *>&1
if ( "$output".indexOf("Did you mean") -ge 0 ) {
write-host "Command '$($(get-history)[$(get-history).count-1].CommandLine)' didnt exist"
where $(get-history)[$(get-history).count-1].CommandLine will contain the latest run command, in this example, `git pshu'