-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert-FolderToGitRepo.ps1
More file actions
executable file
·65 lines (56 loc) · 2.15 KB
/
Convert-FolderToGitRepo.ps1
File metadata and controls
executable file
·65 lines (56 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Edit script settings
$CONFIG = @{
# 1 to do a dry run (i.e. simulate a run)
# 0 to actually run
dryrun = 1
# Specify folder(s) to convert to repos, one per line. Be sure to double-quote each path
# It is suggested to convert one repo at a time.
repos = @(
# Windows
# 'C:\path\to\folder1'
# 'C:\path\to\folder2'
# 'C:\path\to\folder3'
# Unix
# '/path/to/folder1'
# '/path/to/folder2'
# '/path/to/folder3'
)
# Optional: Override git author
# author = 'John Doe <johndoe@example.com>'
}
function Execute-Command ($Command, $DryRun) {
if ($Command) {
"Will execute: $Command" | Write-Host -ForegroundColor Yellow
if (!$DryRun) {
Invoke-Expression $Command
}
}
}
function Convert-FolderToGitRepo {
foreach ($repo in $CONFIG['repos']) {
"[Repo]" | Write-Host -ForegroundColor Cyan
"Repo: $repo" | Write-Host
Push-Location "$repo" -ErrorAction Stop
$groups = Get-ChildItem . -Recurse -File -Force | Sort-Object -Property LastWriteTime | Group-Object LastWriteTime #-Descending | Select-Object -first 1 | Select-Object -Expandproperty LastWriteTime
$groups | Out-String | Write-Host
"[Commands]" | Write-Host -ForegroundColor Cyan
Execute-Command -Command "git init" -DryRun $CONFIG['dryrun']
foreach ($group in $groups) {
foreach ($file in $group.Group) {
Execute-Command -Command "git add '$( $file.FullName )'" -DryRun $CONFIG['dryrun']
}
if ($date = $file.LastWriteTime.ToUniversalTime()) {
$isoDate = $date.tostring('yyyy-MM-dd HH:mm:ss zz00')
$gitDate = $date.tostring('ddd MMM d HH:mm:ss yyyy zz00')
$cmd = "git commit -m '[$isoDate] Add files' --date '$gitdate'"
if ($CONFIG['author']) {
$cmd += " --author '$( $CONFIG['author'] )'"
}
Execute-Command -Command $cmd -DryRun $CONFIG['dryrun']
}
}
"Completed repo: $repo" | Write-Host
Pop-Location
}
}
Convert-FolderToGitRepo