PowerShell script for extracting SVN logs in a CSV file

Yes. Some posts will be in English. Especially the coding ones, because I find it weird to translate words like “script”, “logs”, “commit” and etc.

So here’s a small script that can help you keeping track on your SVN logs.

Stuff you need to set before running the script:

Required:

  • A path to the corresponding SVN folder

Optional:

  • Authors –  You can specify which authors commits you need. If not – all authors will be listed.
  • Date From – You can limit the period of the commits you want to check. This sets the start date. If not specified – the defult date is 2000-01-01.
  • Date To – You can limit the end date too, but if not – the script gets the current date (today’s date).
  • Output Directory – You can set where do you want your file to appear. If not – the results will be printed in the console.
  • Output CSV file name – You can name your result file (if used).
$SvnDirectoriesToCheck = 'C:\smapp\_UPDATES\UPDATE v70\', '
C: \smapp\ _UPDATES\ UPDATE v68\ ' ;
$SvnAuthors = 'gkyuchukov', 'mminchev', 'ptodorova', 'dminkov', 'avasilev', 'igeorgiev', 'ntomov', 'mzheleva', 'ppetrova', 'znyagolova';#
if this array is empty all authors will be listed
$SvnGetLogsDateFrom = '2016-03-09 10:00';#
Use the following syntax.Also If empty gets 2000 - 01 - 01 as a date
$SvnGetLogsDateTo = '';#
If empty gets current date(today 's date)
$OutputDirectory = '';# If empty prints results in the console $CurrentDate = Get - Date - UFormat '%Y-%m-%d'; $OutputCsvFilePath = $OutputDirectory + $CurrentDate + '_svn_log.csv'; Clear - host;

Function GetSvnLogData() {
$dateFrom =
if ($SvnGetLogsDateFrom) {
$SvnGetLogsDateFrom
} else {
'2000-01-01 00:00'
};
$dateTo =

if ($SvnGetLogsDateTo) {
$SvnGetLogsDateTo
} else {
(get - date)
.ToString('yyyy-MM-dd HH:MM')
};
$RevisionDates = '{$dateFrom}:{$dateTo}';

([xml](svn log - v - r $RevisionDates--xml))
.log.logentry | % {
$nestedEntry = $_
$_.paths.path | % {
$nextpath = $_
$nestedEntry | Select - Object - Property `
@{n='Author'; e={$_.author }}, `
@ {
n = 'Revision';
e = {
([int] $_.revision)
}
}
, `
@{n='Date'; e={Get-Date $_.date }}, `
@ {
n = 'Action';
e = {
$nextpath.Action
}
}
, `
@{n='Path'; e={$nextpath.InnerText }}, `
@ {
n = 'Message';
e = {
$_.msg.replace('`n', ' ')
}
} | Foreach - Object {
$_ | where - object {
($SvnAuthors - And($SvnAuthors - contains $nestedEntry.author))
} | Select - Object Author, Revision, Date, Action, Path, Message
$_ | where - object {
!($SvnAuthors)
} | Select - Object Author, Revision, Date, Action, Path, Message
}
}
}
}

Function ExportToCSV() {
$counter = 0;
write - host 'SVN logs are loading. Please wait...`n';
foreach($dir in $SvnDirectoriesToCheck) {
$counter++;
if (Test - Path $dir) {
Push - Location $dir
$result = 'File [' + $counter + '] out of [' + $SvnDirectoriesToCheck.Length + '] - added!';
GetSvnLogData | Export - Csv - Path $OutputCsvFilePath - Append - NoTypeInformation
echo $result
Pop - Location
} else {
$errorResult = 'File [' + $counter + '] out of [' + $SvnDirectoriesToCheck.Length + '] - Invalid SVN directory: ' + $dir;
write - host $errorResult - foregroundcolor Red
}
}
write - host '`nLogs completed!' - BackgroundColor DarkGreen - foregroundcolor White
}

Function PrintToConsole() {
foreach($dir in $SvnDirectoriesToCheck) {
Push - Location $dir
GetSvnLogData
Pop - Location
}
write - host '`nLogs completed!' - BackgroundColor DarkGreen - foregroundcolor White
}

Function Main() {

if (!($OutputDirectory - like '')) {
if (Test - Path $OutputDirectory) {
ExportToCSV
} else {
write - host 'Invalid output folder' - foregroundcolor Red
}
} else {
PrintToConsole
}
}

Main

Leave a Reply