forked from mmsaeed509/DevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-push.ps1
164 lines (129 loc) · 5.15 KB
/
git-push.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#####################################
# #
# @author : 00xWolf #
# GitHub : @mmsaeed509 #
# Developer : Mahmoud Mohamed #
# Copyright : Exodia OS #
# #
#####################################
Write-Host "#############################" -ForegroundColor Cyan
Write-Host "# Git Push Script #" -ForegroundColor Cyan
Write-Host "#############################" -ForegroundColor Cyan
Write-Host "" # add a new line #
# get branch name (e.g master, main, etc... ) #
$DEFAULT_BRANCH = git branch --show-current
$TARGET_BRANCH = $DEFAULT_BRANCH
Write-Host -NoNewline "[*] Your Current Branch : " -ForegroundColor Red
Write-Host $DEFAULT_BRANCH -ForegroundColor Yellow
Write-Host "" # add a new line #
# get new updates if it founded #
Write-Host "[+] Updating Repo..." -ForegroundColor Red
Write-Host "" # add a new line #
git pull
Write-Host "" # add a new line #
# get default commit message based on changes #
$DEFAULT_COMMIT_MSG = @("")
# git status #
$STATUSES = @(git status -s)
for ($i = 0; $i -lt $STATUSES.Count; $i++) {
$currentStatus = $STATUSES[$i].Trim()
if ($currentStatus -match '^D') {
Write-Host " ==> deleted : $($currentStatus -replace '^D\s*')" -ForegroundColor Red
$DEFAULT_COMMIT_MSG += " ==> deleted : $($currentStatus -replace '^D\s*')" + "`n"
}
elseif ($currentStatus -match '^M') {
Write-Host " ==> modified : $($currentStatus -replace '^M\s*')" -ForegroundColor Cyan
$DEFAULT_COMMIT_MSG += " ==> modified : $($currentStatus -replace '^M\s*')" + "`n"
}
elseif ($currentStatus -match '^\?\?') {
Write-Host " ==> added : $($currentStatus -replace '^\?\?\s*')" -ForegroundColor Green
$DEFAULT_COMMIT_MSG += " ==> added : $($currentStatus -replace '^\?\?\s*')" + "`n"
}
}
# Check if $DEFAULT_COMMIT_MSG is empty #
if ($DEFAULT_COMMIT_MSG -eq "") {
Write-Host " ==> NO changes" -ForegroundColor Yellow
$DEFAULT_COMMIT_MSG = " ==> NO changes"
}
# add a new line #
Write-Host ""
# Initialize variables #
$CREATE_PR = $false
$TARGET_PR_BRANCH = $null
# Loop through all arguments #
while ($args) {
switch ($args[0]) {
"--target-branch" {
$args = $args[1..$args.length]
$TARGET_BRANCH = $args[0]
$args = $args[1..$args.length]
}
"-t" {
$args = $args[1..$args.length]
$TARGET_BRANCH = $args[0]
$args = $args[1..$args.length]
}
"--commit-msg" {
$args = $args[1..$args.length]
$DEFAULT_COMMIT_MSG = $args[0]
$args = $args[1..$args.length]
}
"-m" {
$args = $args[1..$args.length]
$DEFAULT_COMMIT_MSG = $args[0]
$args = $args[1..$args.length]
}
"--create-pr" {
$CREATE_PR = $true
$args = $args[1..$args.length]
$TARGET_PR_BRANCH = $args[0]
$args = $args[1..$args.length]
}
default {
# Ignore unrecognized options #
$args = $args[1..$args.length]
}
}
}
# Display Target Branch #
Write-Host -NoNewline "[+] Target Branch : " -ForegroundColor Red
Write-Host $TARGET_BRANCH -ForegroundColor Yellow
# Check if Target Branch is different from Default Branch #
if ($TARGET_BRANCH -ne $DEFAULT_BRANCH) {
# Check if Target Branch exists #
$branchExists = git show-ref --verify --quiet "refs/heads/$TARGET_BRANCH"
if ($branchExists) {
# Target Branch exists, switch to it #
Write-Host -NoNewline " └──> Changing to the Target Branch: " -ForegroundColor Blue
Write-Host "$TARGET_BRANCH" -ForegroundColor Yellow
Write-Host "" # add a new line #
git checkout $TARGET_BRANCH
Write-Host "" # add a new line #
}
else {
# Target Branch does not exist, create and switch to it #
Write-Host -NoNewline " └──> Creating and changing to the Target Branch: " -ForegroundColor Blue
Write-Host "$TARGET_BRANCH" -ForegroundColor Yellow
Write-Host "" # add a new line #
git checkout -b $TARGET_BRANCH
Write-Host "" # add a new line #
}
}
Write-Host "[+] Adding new changes to the repo... " -ForegroundColor Cyan
Write-Host "" # add a new line #
git add --all .
git commit -m "$DEFAULT_COMMIT_MSG"
git push -u origin $TARGET_BRANCH
# Check and create a pull request #
if ($CREATE_PR -eq $true -and $TARGET_PR_BRANCH -ne $null) {
Write-Host "`n" # add a new line #
Write-Host -NoNewline "[+] Creating a pull request from " -ForegroundColor Blue
Write-Host -NoNewline "$TARGET_BRANCH " -ForegroundColor Red
Write-Host -NoNewline "to $TARGET_PR_BRANCH... " -ForegroundColor Yellow
Write-Host "`n" # add a new line #
# Assuming you have GitHub CLI (gh) installed for PowerShell #
gh pr create --base $TARGET_PR_BRANCH --head $TARGET_BRANCH --title "Pull Request: $TARGET_BRANCH to $TARGET_PR_BRANCH" --body "Please review and merge."
}
Write-Host "" # add a new line #
Write-Host "D O N E! " -ForegroundColor Green
Write-Host "" # add a new line #