Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Copy-ChangedFilesFromGit
  4. .DESCRIPTION
  5. This function uses git (git status --porcelain) to get list of files
  6. modified in local git repository and copies those files into a separate
  7. directory to serve as a backup of your changes. It also saves full paths
  8. of all coppied files in index.txt (as default), toegether with the
  9. backup.
  10. .EXAMPLE
  11. copygit c:\git\myproj
  12. .NOTES
  13. author: Albert
  14. #>
  15. function Copy-ChangedFilesFromGit {
  16. [cmdletbinding()]
  17. param(
  18. [string] $repo = "C:\git\mfxi",
  19. [string] $output = "$home\Desktop\changes-backup\",
  20. [string] $index = "$output\index.txt"
  21. )
  22.  
  23. if(Test-Path $output){
  24. Write-Verbose "Renaming previous backup"
  25. Rename-Item $output ($output + "_" + (Get-Date -format "ddhhmmss"))
  26. }
  27.  
  28. Write-Verbose "Creating output directory"
  29. New-Item -Path $output -ItemType Directory
  30.  
  31. $gitOutput = git --git-dir="$repo\.git" status --porcelain
  32. $files = $gitOutput -replace "/","\" -split "`n"
  33. $files | ForEach-Object {
  34. if($_[1] -ne 'D'){
  35. $file = Get-ChildItem "$repo\*$($_.Substring(3))" | Select-Object -ExpandProperty FullName
  36. Write-Verbose "Copying: $file to: $output"
  37. Copy-Item $file -Destination $output
  38. Out-File -File $index -InputObject $file -Append
  39. }
  40. }
  41. }
  42.  
  43. New-Alias -Name copygit -Value Copy-ChangedFilesFromGit
  44. New-Alias -Name copychanges -Value Copy-ChangedFilesFromGit
  45. New-Alias -Name cpgit -Value Copy-ChangedFilesFromGit
  46. New-Alias -Name cpchanges -Value Copy-ChangedFilesFromGit
  47. Export-ModuleMember -Alias * -Function *
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement