Commits

Nathan Durnan committed 1b526cd

Purge Orphanded Backups is working!
Closes Issue #4 at BitBucket.

Comments (0)

Files changed (2)

 .hgignore,1326153052.39
 .hgsub,1325791137.19
 .hgsubstate,1325791876.03
-_HgPreBackup.au3,1326753399.14
+_HgPreBackup.au3,1326759193.05
 HgPreBackup.ico,1325031982.39
 HgPreBackup.svg,1325032006.66
 HgPreBackup_x64.exe,1326752358.3
 Global $Gi_CloneCount = 0			;Number of Clone commands issued.
 Global $Ga_BackupRepoCloneErr[1]	;Array to keep track of any repositories with errors durring Clone
 Global $Ga_BackupRepoList[1]		;Array containing paths of repositories in the backup directory.
-Global $Ga_BackupRepoValidErr[1]	;;Array containing list of backup repositories that failed validation.
+Global $Ga_BackupRepoValidErr[1]	;Array containing list of backup repositories that failed validation.
+Global $Ga_PurgedBackupList[1]		;Array containing list of orphaned backups that have been purged.
 
 ;email settings
 Global $Gb_ReportErrors		;True = email log if errors encountered.  False = no email.
 ;------------------------------------------------------------
 #Region Local Variables and Constants
 ;***** Declare Variables *****
-Local $iResult
+Local $iResult, $lError
+Local $ii, $jj
+Local $sBackupPath
+Local $sRelativePath
+Local $bFoundMatch
 
 #EndRegion Local Variables and Constants
 
 		$G_objFSO.CreateFolder($Gs_BackupDir)
 	EndIf ;Check for backup directory.
 	;Check source repositories.
-	Local $ii, $jj, $sBackupPath
-	Local $sRelativePath
 	For $ii = 1 To $Ga_RepoList[0]
 		$sRelativePath = _PathGetRelative($Gs_RepoHomeDir, $Ga_RepoList[$ii])
 		If @error Then ;NOTE: check @error IMMEDIATELY after function call!!!
 	ConsoleWrite("========== Post-Backup ==========" & @CRLF)
 	_LogFileAddMessage($Gh_LogFile, "Searching for Backup Repositories at " & $Gs_BackupDir)
 	_SearchFolderForRepositories($Gs_BackupDir, $Ga_BackupRepoList, $Ga_BackupRepoValidErr)
+	;Purge Orphaned Backups (backups that no longer have a source).
+	ConsoleWrite("Purge Orphaned Backups..." & @CRLF)
+	$ii = 1 ;Initialize Loop Variable
+	Do ;Loop through backup repo list.
+		$sBackupPath = _PathGetRelative($Gs_BackupDir, $Ga_BackupRepoList[$ii])
+		ConsoleWrite(@TAB & "$Ga_BackupRepoList[" & $ii & "] = " & $sBackupPath & @CRLF)
+		$bFoundMatch = False ;Initialize flag.
+		For $jj = 1 to $Ga_RepoList[0]
+			$sRelativePath = _PathGetRelative($Gs_RepoHomeDir, $Ga_RepoList[$jj])
+			$iResult = StringCompare($sBackupPath, $sRelativePath)
+			If ($iResult = 0) Then
+				ConsoleWrite(@TAB & "$Ga_RepoList[" & $jj & "] = " & $sRelativePath & @CRLF)
+				$bFoundMatch = True
+				ExitLoop ;($jj) ;Found a matching source!
+			EndIf ;check for match found.
+		Next ;loop through source repositories.
+		If Not $bFoundMatch Then
+			;Backup not found in Source.
+			ConsoleWrite(@TAB & "Backup no longer in Source!" & @CRLF)
+			_LogFileAddMessage($Gh_LogFile, "Purging Orphaned Backup: " & $Ga_BackupRepoList[$ii])
+			$iResult = _ArrayAdd($Ga_PurgedBackupList, $Ga_BackupRepoList[$ii])
+			If ($iResult > 0) Then
+				$Ga_PurgedBackupList[0] = $iResult
+			EndIf ;Check results of adding item to array.
+			$iResult = FileRecycle($Ga_BackupRepoList[$ii])
+			If ($iResult > 0) Then
+				$jj = _ArrayDelete($Ga_BackupRepoList, $ii)
+				If ($jj >= 0) Then
+					$Ga_BackupRepoList[0] = ($jj - 1) ;don't count the first item - is is placeholder for count only!
+					$ii -= 1 ;Jump back one index to make up for removed item on next loop.
+				EndIf ;Check for successful array item deletion.
+			EndIf ;Check for successful repository deletion.
+		EndIf ;Check for backup not found in source.
+		$ii += 1 ;Increment Loop Variable.
+	Until ($ii > $Ga_BackupRepoList[0]) ;Loop through backup repositories.
 	#EndRegion - Evaluate Backup Repositories
 EndIf ;Check for valid Mercurial installation.
 _LogFileAddMessage($Gh_LogFile, "End of Operations")
 				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       > " & $Ga_BackupRepoPullErr[$ii])
 			Next ;loop through list of repos
 		EndIf ;Check for Pull Errors
-		If ($Ga_RepoList[0] <= 0) Then ;check for any repositories found.
-			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.........: none (huh?)")
+		If ($Ga_BackupRepoList[0] <= 0) Then ;check for any repositories found.
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.............: none (huh?)")
 		Else ;At least one repository was found.
-			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.........: " & $Ga_BackupRepoList[0])
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Total Found.............: " & $Ga_BackupRepoList[0])
 			If ($Ga_BackupRepoValidErr[0] > 0) Then
-				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Validation Errors...: " & $Ga_BackupRepoValidErr[0])
+				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Validation Errors.......: " & $Ga_BackupRepoValidErr[0])
 				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       Review the following repositories:")
 				For $ii = 1 To $Ga_BackupRepoValidErr[0]
 					$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       > " & $Ga_BackupRepoValidErr[$ii])
 				$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Validation Errors.......: none (yaay!)")
 			EndIf ;Check for Validation Errors
 		EndIf ;check for any repositories found.
+		If ($Ga_PurgedBackupList[0] > 0) Then
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|     Orphaned Backups Purged.: " & $Ga_PurgedBackupList[0])
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       The following no longer have matching source repositories")
+			$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       and have been moved to the Recycle Bin: ")
+			For $ii = 1 to $Ga_PurgedBackupList[0]
+					$myResult = $myResult And FileWriteLine($IN_hLogFile, "|       > " & $Ga_PurgedBackupList[$ii])
+			Next ;Loop through list of repos
+		EndIf ;Check for any purged orphans.
 	Endif ;Check for mercurial installation/path errors.
 	$myResult = $myResult And FileWriteLine($IN_hLogFile, "+---------------------------------------------------------------------")
 	Return $myResult