-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitCheckout.ps1
More file actions
93 lines (74 loc) · 2.64 KB
/
GitCheckout.ps1
File metadata and controls
93 lines (74 loc) · 2.64 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
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
92
93
<#
Git commandline helpers for PowerShell
Author: Damian Suess
Revision: 1 - 2020-01-08
Checkout (switch-to) branch.
If it doesn't exist locally, check "remotes/origin/" by default. You can optionally
specify a different remote other than the default.
Usage:
GitCheckout "BranchName" ; Checkout branch, origin as default
GitCheckout "BranchName" "origin" ; Pulls current branch from specified remote
TODO:
- Do the work
- Fetch if it doesn't find branch on first try
- Check alternative remote
Change Log:
2020-01-08 0.1 - Created
2020-05-05 0.2 - Updated error messages
#>
# git checkout -b feature-MyBranch remotes/origin/feature-MyBranch --
# Commandline Params ---
param(
[parameter(Mandatory=$true)][string] $branch = ""
# , [parameter(Mandatory=$false)][string] $remote = "origin"
);
# Include Files --------
. "$PSScriptRoot/lib/GitHelpers.ps1";
$remote = "origin";
# Our code -------------
[string] $branchName = GitCurrentBranch;
if ($branchName.Trim() -eq $branch.Trim())
{
Write-Host "DevOps: Already on that branch." -ForegroundColor Yellow;
exit;
}
# Attempt to get it locally
GitCheckout($branch);
$branchName = GitCurrentBranch;
if ($branchName.Trim() -eq $branch.Trim())
{
Write-Host "DevOps: Switched to $branch locally." -ForegroundColor Green;
exit;
}
# TODO - If optional remote provided, use it first
# .. here ..
## TODO:
## 1. Check if branch exists remotely first
## 2. If does not exist, fetch
## 3. If does not exist, create a new one
# Attempt to get it from remote repository
Write-Host "DevOps: Branch does not exist locally." -ForegroundColor Yellow;
Write-Host "DevOps: Checking for branch on remote, ""$remote""..." -ForegroundColor Yellow;
# -b or not to -b ??
Invoke-Expression "git checkout ""$branch"" ""remotes/$remote/$branch""";
$branchName = GitCurrentBranch;
if ($branchName.Trim() -eq $branch.Trim())
{
Write-Host "DevOps: Switched to '$branch' from remote." -ForegroundColor Green;
exit;
}
# Fetch and try again from remote repository
Write-Host "DevOps: Branch not found on remote." -ForegroundColor Yellow;
Write-Host "DevOps: Fetching latest..." -ForegroundColor Yellow;
GitFetch;
Invoke-Expression "git checkout -b ""$branch"" ""remotes/$remote/$branch""";
$branchName = GitCurrentBranch;
if ($branchName.Trim() -eq $branch.Trim())
{
Write-Host "DevOps: Switched to '$branch' from remote after 'fetch'." -ForegroundColor Green;
}
else
{
Write-Host "DevOps: The branch, ""$branch"" could not be found on ""$remote""." -ForegroundColor Red;
Write-Host "DevOps: Check that the spelling and remote are correct, or create a new branch." -ForegroundColor Yellow;
}