Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gci `
| %{
("`n", $_.Name, @($_ | gci -File).Length) -join "`t"
$_ `
| gci -File `
| %{
(
$_.CreationTime.ToString('yyyy-MM-dd'),
$_.Length,
$_.Name
) -join "`t"
} `
| sort
} `
| less
gci -Recurse -File `
| Group-Object -Property { Get-Date -Date $_.CreationTime -UFormat '%V' } `
| Sort-Object -Property Name `
| %{
$_.Name
(
$_.Group `
| %{ $_.CreationTime.toString('MMdd') }
| Sort-Object -Unique
) -join "`t"
}
Function pseudoVCS {
gci -Recurse `
| ?{
$_.Name -match '(^|[^a-z])((old|copy|bak|back|backup|test)([^a-z]|$)|20[0-9_ -]{8,10}[.])'
} `
| Group-Object -Property Name `
| Sort-Object -Property Name `
| %{
$_.Group `
| %{
$_.FullName -replace "^$([regex]::escape($pwd))/",''
} `
| Sort-Object
}
}
gci -Recurse -File `
| Group-Object -Property Extension `
| Sort-Object -Property Name `
| %{
$size = (
$_.Group `
| Measure-Object -Sum -Property Length `
).Sum / 1MB
if ($size -gt 20) {
"`n$($_.Name)`t$([math]::Round($size, 2))mB"
# $_.Group `
# | %{
# $_.FullName -replace "^$([regex]::escape($pwd))/",''
# } `
# | Sort-Object
}
}
'' `
| gci -Directory `
| %{
$to = "data/$($_.Parent.FullName -replace "^$([regex]::escape($pwd))/",'')/"
If (Test-Path "$to$($_.Name)") {
gci "$to$($_.Name)/*" | Move-Item -Destination "$($_.FullName)/"
rmdir "$to$($_.Name)/"
}
Else {
mkdir -p $to
}
mv $_.FullName $to
}
Filter Latest { Param($count = 1)
$_ | Get-ChildItem -Recurse -File `
| Sort-Object -Property CreationTime `
| Select-Object -Last $count
}
Filter Earliest { Param($count = 1)
$_ | Get-ChildItem -Recurse -File `
| Sort-Object -Property CreationTime `
| Select-Object -First $count
}
Function DiffFolders { Param ($listUnchanged=$NULL)
Begin {
$last = $NULL
$lastParent = $NULL
}
Process {
$current = [ordered]@{}
$parent = $_
$parentPath = (Get-Item -Path $parent).FullName
$parentRegex = "^$([regex]::escape($parentPath))"
Get-ChildItem -Path $parentPath -Recurse -File `
| %{
$name = $_.FullName -replace $parentRegex,''
$current.Add($name, (Get-FileHash -LiteralPath $_.FullName).Hash)
if (!$last) {
}
elseif ($last.Contains($name)) {
$last[$name] = $last[$name] -eq $current[$name]
}
else {
[PSCustomObject]@{
parent = $parent
event = 'Added'
value = $name
}
}
}
if ($last) {
if ($listUnchanged -isnot [Boolean]) {
$listUnchanged = 0
}
$last.GetEnumerator() `
| %{
if ($_.Value -isnot [Boolean]) {
$_.Value = 'Deleted'
}
elseif (!$_.Value) {
$_.Value = 'Changed'
}
elseif ($listUnchanged -isnot [Boolean]) {
++$listUnchanged
return
}
elseif (!$listUnchanged) {
return
}
else {
$_.Value = 'Unchanged'
}
[PSCustomObject]@{
parent = $parent
event = $_.Value
value = $_.Key
from = $lastParent
}
}
if ($listUnchanged -isnot [Boolean]) {
[PSCustomObject]@{
parent = $parent
event = 'Unchanged'
value = $listUnchanged
from = $lastParent
}
}
}
$last = $current
$lastParent = $parent
}
}
Function Gitify {
Param (
[Parameter(Mandatory=$true)]$git,
[switch]$dry,
[switch]$keep,
[switch]$hour,
$email,
$name,
[Parameter(ValueFromPipeline=$true)]$x
)
Begin {
$time = '[\d-]{10}'
if ($hour) {
$time += '[^\d]+\d{2}:'
}
$time = "(?<=^$time).*"
$latest = $env:GIT_AUTHOR_DATE
if ($name -and $email) {
$env:GIT_AUTHOR_NAME = $env:GIT_COMMITTER_NAME = $name
$env:GIT_AUTHOR_EMAIL = $env:GIT_COMMITTER_EMAIL = $email
}
elseif ($name -or $email) {
throw 'Must specify either none or both of name & email'
}
Function Commit {
Param ($parent)
if (!$latest) {
$latest = git `
-C $git `
'log' '-1' `
'--date=format:%F %T %z' `
'--format=%ad' `
2>$NULL
}
$env:GIT_AUTHOR_DATE = $env:GIT_COMMITTER_DATE = $latest
git `
-C $git `
'commit' `
"--date=$latest" `
"--author=$($env:GIT_AUTHOR_NAME) <$($env:GIT_AUTHOR_EMAIL)>" `
-m "$parent"
}
}
Process {
$y = $x
($git,$y) `
| DiffFolders `
| %{
$event = $_
Switch -regex ($event.event) {
'^Deleted$' {
[PSCustomObject]@{
date = [char]0x255
target = $event.value
command = 'rm'
parent = $event.parent
}
}
'^(Changed|Added)$' {
$from = "$($event.parent)$($event.value)"
$date = Get-Date -Date (Get-Item -Path $from).CreationTime -UFormat '%F %T %Z'
[PSCustomObject]@{
date = $date
target = $event.value
command = 'add'
parent = $event.parent
from = $from
}
}
}
} `
| Sort-Object -Property date
| %{
if ($dry) {
return $_
}
if ($_.date -eq [char]0x255) {
}
elseif ($_.date -lt $latest) {
Write-Error "'$($_.target)' of $($_.parent) has older date!"
Write-Error "$($_.date) vs $latest"
}
else {
if (
($latest -replace $time,'') -ne
($_.date -replace $time,'')
) {
Commit -parent $y
}
$latest = $_.date
}
if ($_.from) {
$to = Split-Path -Path ($git + $_.target)
if (!(Test-Path $to)) {
New-Item `
-Path $to `
-Type Directory `
-Force `
| Out-Null
}
Move-Item -Force -Path $_.from -Destination $to
}
git `
-C $git `
$_.command `
($_.target -replace '^[/\\]','')
}
if (!$dry) {
Commit -parent $y
}
}
End {
if (!$dry) {
if ($name -or $email) {
$env:GIT_AUTHOR_NAME = $env:GIT_COMMITTER_NAME = `
$env:GIT_AUTHOR_EMAIL = $env:GIT_COMMITTER_EMAIL = `
$NULL
}
if (!$keep) {
$env:GIT_AUTHOR_DATE = $env:GIT_COMMITTER_DATE = $NULL
}
}
}
}
.{
New-Item -Path git -Type Directory
git -C git init
$env:GIT_AUTHOR_NAME = $env:GIT_COMMITTER_NAME = '<redacted>'
$env:GIT_AUTHOR_EMAIL = $env:GIT_COMMITTER_EMAIL = 'redac@t.ed'
$env:GIT_AUTHOR_DATE = $env:GIT_COMMITTER_DATE = '2019-09-23 09:28:12 +10'
git -C git commit '--allow-empty' '-m' 'init' "--date=$($env:GIT_AUTHOR_DATE)" "--author=$($env:GIT_AUTHOR_NAME) <$($env:GIT_AUTHOR_EMAIL)>"
git -C git checkout -b develop
'2019-10-29','2019-10-31','2020-02-05','privacy','2020-02-06','footer','2020-11-02' `
| %{
git -C git checkout -b $_
$_ | Gitify -git git -keep -hour
git -C git checkout develop
git -C git merge '--no-ff' $_ '-m' $_
git -C git checkout master
git -C git merge '--no-ff' develop '-m' $_
git -C git checkout develop
}
git -C git remote add origin https://r@ed.act/ed.git
git -C git push '--all' '--force'
}
.{
Move-Item 2017* git
git -C git init
git -C git add -A
git -C git checkout -b develop
$env:GIT_AUTHOR_NAME = $env:GIT_COMMITTER_NAME = '<redacted>'
$env:GIT_AUTHOR_EMAIL = $env:GIT_COMMITTER_EMAIL = 'redac@t.ed'
$env:GIT_AUTHOR_DATE = $env:GIT_COMMITTER_DATE = '2017-11-22 10:58:00 +11'
git -C git commit '-m' '2017-11-22' "--date=$($env:GIT_AUTHOR_DATE)" "--author=$($env:GIT_AUTHOR_NAME) <$($env:GIT_AUTHOR_EMAIL)>"
'2018-02-20','2018-11-21','2018-12-03','2018-12-07','2019-01-10','2019-01-16','2019-01-30','2019-03-18' `
| Gitify -git git -keep
$env:GIT_AUTHOR_NAME = $env:GIT_COMMITTER_NAME = '<redacted2>'
$env:GIT_AUTHOR_EMAIL = $env:GIT_COMMITTER_EMAIL = 'redac2@t.ed'
'2019-07-10','2019-10-29' `
| Gitify -git git -keep
git -C git checkout -b master
git -C git checkout develop
'2019-11-05' `
| Gitify -git git -keep
git -C git checkout master
git -C git merge '--no-ff' develop '-m' '2019-11-05'
git -C git checkout develop
'2020-10-09' `
| Gitify -git git -keep
git -C git checkout master
git -C git merge '--no-ff' develop '-m' '2020-10-09'
git -C git checkout develop
git -C git remote add origin https://r@ed.act/ed.git
git -C git push '--all' '--force'
}
.{
New-Item -Type Directory -Name git
git -C git init
$env:GIT_AUTHOR_NAME = $env:GIT_COMMITTER_NAME = '<redacted>'
$env:GIT_AUTHOR_EMAIL = $env:GIT_COMMITTER_EMAIL = 'redac@t.ed'
$env:GIT_AUTHOR_DATE = $env:GIT_COMMITTER_DATE = '2012-05-01 09:30:51 +11'
git -C git commit '--allow-empty' '-m' 'init' "--date=$($env:GIT_AUTHOR_DATE)" "--author=$($env:GIT_AUTHOR_NAME) <$($env:GIT_AUTHOR_EMAIL)>"
git -C git checkout -b develop
'2015-07-28_0','2015-07-28' `
| Gitify -git git -keep
git -C git checkout master
git -C git merge '--no-ff' develop '-m' '2015-07-28'
git -C git checkout develop
'2015-11-09' `
| Gitify -git git -keep
git -C git checkout master
git -C git merge '--no-ff' develop '-m' '2015-11-09'
git -C git checkout develop
'2017-11-22','2019-10-31_01','2019-10-31_02','2019-10-31_03','2019-10-31_04','2019-10-31_05','2019-10-31_06','2019-10-31_07','2019-10-31_08','2019-10-31_09','2019-10-31_10','2019-10-31_11' `
| Gitify -git git -keep
$env:GIT_AUTHOR_NAME = $env:GIT_COMMITTER_NAME = '<redacted2>'
$env:GIT_AUTHOR_EMAIL = $env:GIT_COMMITTER_EMAIL = 'redac2@t.ed'
'2019-10-31' `
| Gitify -git git -keep
git -C git checkout master
git -C git merge '--no-ff' develop '-m' '2019-10-31'
git -C git checkout develop
git -C git checkout -b playground
'2019-10-31_playground_2019-10-31','2019-10-31_playground' `
| Gitify -git git -keep -hour
git -C git checkout develop
'2020-08-17' `
| Gitify -git git -keep -hour
git -C git checkout master
git -C git merge '--no-ff' develop '-m' '2020-08-17'
git -C git checkout develop
git -C git remote add origin https://r@ed.act/ed.git
git -C git push '--all' '--force'
}
Function CleanBackup {
Get-ChildItem `
| DiffFolders `
| %{
if ($_.event -notmatch '^(Changed|Deleted)$') {
return
}
$_.event = Split-Path -Path ([regex]'([/\\]?)$').replace($_.parent, "_$($_.event)`$1$($_.value)", 1)
$_.value = "$($_.from)$($_.value)"
if (!(Test-Path $_.event)) {
New-Item `
-Path $_.event `
-Type Directory `
-Force `
| Out-Null
}
Move-Item -LiteralPath $_.value -Destination $_.event
}
}
gci | Earliest -count 10 | %{ $_.CreationTime.ToString('yyyy-MM-dd') + "`t" + ($_.FullName -replace $pwd,'') }
Function seeAll {
Param ( $namePattern )
#gci */access*to*information*
gci "*/$namePattern" `
| %{
(
$_.CreationTime.ToString('yyyy-MM-dd'),
((Get-FileHash -LiteralPath $_.FullName).Hash -replace '(?<=^.{6}).*',''),
"$($_.Directory.FullName -replace "^$([regex]::escape($pwd))/",'')/",
$($_.Name)
) -join "`t"
} `
| Sort-Object
}
gci | Latest | %{ Get-Date -Date (Get-Item -LiteralPath $_).CreationTime -UFormat '%F %T %Z' }
gci | Latest | %{ $_.CreationTime.ToString('yyyy-MM-dd') + "`t" + ($_.FullName -replace $pwd,'') }
gci -Recurse -File | ?{ $_.Extension -eq '.htm' } | Sort-Object -Property LastWriteTime | %{ $_.CreationTime.ToString('yyyy-MM-dd') + "`t" + ($_.FullName -replace $pwd,'') }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment