-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitSync.ps1
91 lines (73 loc) · 2.58 KB
/
GitSync.ps1
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<#
Git commandline helpers for PowerShell
Author: Damian Suess
Revision: 3 - 2021-10-11
Usage:
GitSync ; Sync current branch with develop
GitSync -push ; Sync current branch with develop and push changes (syncing branch)
TODO:
- GitCheckIn $message (check-in/push)
- Option "-force" to merge unrelated histories
- Option "-track" to set tracking; don't track by default
Change Log:
2021-10-11 r3 - Merge unrelated histories
2018-10-03 r2 - Some enhancements
2018-07-30 r1 - Created
#>
# Commandline Params ---
param(
[parameter(Mandatory=$false)][string] $syncBranch = "develop",
[parameter(Mandatory=$false)][string] $remote = "origin",
[parameter(Mandatory=$false)][switch] $push = $false,
[parameter(Mandatory=$false)][switch] $force = $false,
[parameter(Mandatory=$false)][bool] $useCheckout = $false
);
# Clear out cache of previous PowerShell sessions
#Remove-Variable * -ErrorAction SilentlyContinue;
#Remove-Module *;
#$error.Clear();
## Clear-Host;
# Include Files --------
. "$PSScriptRoot/lib/GitHelpers.ps1";
# Our code -------------
$current = GitCurrentBranch;
$current = $current.Trim();
if ($current -eq "")
{
Write-Host "No repository found in currect directory." -ForegroundColor Red;
exit;
}
$stopWatch = New-Object System.Diagnostics.Stopwatch
$stopWatch.Start()
Write-Host "------------------------------" -ForegroundColor Yellow;
Write-Host "Syncing '${current}' with '${syncBranch}' branch..." -ForegroundColor Yellow;
if ($useCheckout -eq $false)
{
Write-Host "Fetching latest..." -ForegroundColor Yellow;
Invoke-Expression "git fetch";
Write-Host "Fetching latest branch, '${syncBranch}'..." -ForegroundColor Yellow;
Invoke-Expression "git fetch ${remote} ${syncBranch}:${syncBranch}";
Write-Host "Merging '${syncBranch}' with '${current}'..." -ForegroundColor Yellow;
GitMerge($syncBranch);
}
else
{
Write-Host "Switching to ${syncBranch}..." -ForegroundColor Yellow;
GitCheckout($syncBranch);
Write-Host "Pulling latest..." -ForegroundColor Yellow;
GitPull($remote)($syncBranch);
Write-Host "Switching back to ${current}..." -ForegroundColor Yellow;
GitCheckout($current);
Write-Host "Merging branches..." -ForegroundColor Yellow;
GitMerge($syncBranch);
}
# Automatically PUSH branch upstream via "-push" switch
if ($push)
{
Write-Host "Pushing updated branch..." -ForegroundColor Green;
GitPush($remote)($current);
}
$stopWatch.Stop();
Write-Host "Sync with '${syncBranch}' complete!" -ForegroundColor Green;
Write-Host "Elapsed Time: " $stopWatch.Elapsed -ForegroundColor Green;
Write-Host "";