-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgitPush1by1.fsx
executable file
·298 lines (249 loc) · 8.93 KB
/
gitPush1by1.fsx
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env -S dotnet fsi
open System
open System.IO
open System.Linq
open System.Threading
#r "System.Configuration"
open System.Configuration
#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62"
open Fsdk
open Fsdk.Process
// mimic https://stackoverflow.com/a/3230241/544947
let GitSpecificPush
(remoteName: string)
(commitSha: string)
(remoteBranchName: string)
(force: bool)
=
let forceFlag =
if force then
"--force"
else
"--force-with-lease"
let gitPush =
{
Command = "git"
Arguments =
sprintf
"push %s %s:refs/heads/%s %s"
remoteName
commitSha
remoteBranchName
forceFlag
}
let pushProc = Process.Execute(gitPush, Echo.OutputOnly)
match pushProc.Result with
| Error _ -> failwith "Push failed ^"
| WarningsOrAmbiguous _
| Success _ -> ()
let GitFetch(remoteOpt: Option<string>) =
let remoteArg =
match remoteOpt with
| None -> "--all"
| Some remote -> remote
let gitFetch =
{
Command = "git"
Arguments = sprintf "fetch %s" remoteArg
}
let fetchProc = Process.Execute(gitFetch, Echo.OutputOnly)
match fetchProc.Result with
| Error _ -> failwith "Fetch failed ^"
| WarningsOrAmbiguous _
| Success _ -> ()
let GetLastNthCommitFromRemoteBranch
(remoteName: string)
(remoteBranch: string)
(n: uint32)
=
let gitShow =
{
Command = "git"
Arguments =
sprintf "show %s/%s~%i --no-patch" remoteName remoteBranch n
}
let gitShowProcOutput = Process.Execute(gitShow, Echo.Off).UnwrapDefault()
let firstLine =
(Misc.CrossPlatformStringSplitInLines gitShowProcOutput)
.First()
// split this line: commit 938634a3e7d4dc7e6dd357927a16165120bbea68 (HEAD -> master, origin/master, origin/HEAD)
let commitHash = firstLine.Split([| " " |], StringSplitOptions.None).[1]
commitHash
let FindUnpushedCommits (remoteName: string) (remoteBranch: string) =
let rec findUnpushedCommits
localCommitsWalkedSoFar
currentSkipCount
remoteCommits
=
let rec findIntersection localCommits (remoteCommits: List<string>) =
match localCommits with
| [] -> None
| head :: tail ->
if remoteCommits.Contains head then
Some tail
else
findIntersection tail remoteCommits
Console.WriteLine "Walking tree..."
let currentHash =
Process
.Execute(
{
Command = "git"
Arguments =
sprintf
"log -1 --skip=%i --format=format:%%H"
currentSkipCount
},
Echo.Off
)
.UnwrapDefault()
.Trim()
let newRemoteCommits =
(GetLastNthCommitFromRemoteBranch
remoteName
remoteBranch
currentSkipCount)
:: remoteCommits
let newLocalCommitsWalkedSoFar = currentHash :: localCommitsWalkedSoFar
match findIntersection newLocalCommitsWalkedSoFar newRemoteCommits with
| Some theCommitsToPush -> theCommitsToPush
| None ->
findUnpushedCommits
newLocalCommitsWalkedSoFar
(currentSkipCount + 1u)
newRemoteCommits
GitFetch(Some remoteName)
findUnpushedCommits List.empty 0u List.empty
let GetLastCommits(count: UInt32) =
let rec getLastCommits commitsFoundSoFar currentSkipCount currentCount =
if currentCount = 0u then
commitsFoundSoFar
else
let currentHash =
Process
.Execute(
{
Command = "git"
Arguments =
sprintf
"log -1 --skip=%i --format=format:%%H"
currentSkipCount
},
Echo.Off
)
.UnwrapDefault()
.Trim()
getLastCommits
(currentHash :: commitsFoundSoFar)
(currentSkipCount + 1u)
(currentCount - 1u)
getLastCommits List.empty 0u count
let remotes = Git.GetRemotes()
if not(remotes.Any()) then
Console.Error.WriteLine "No remotes found, please add one first."
Environment.Exit 5
let args = Misc.FsxOnlyArguments()
if args.Length > 3 then
Console.Error.WriteLine
"Usage: gitpush.fsx [remoteName(optional)] [numberOfCommits(optional)]"
Environment.Exit 1
let maybeRemote, maybeNumberOfCommits, force =
if args.Length > 1 then
match UInt32.TryParse args.[1] with
| true, 0u ->
Console.Error.WriteLine
"Second argument should be an integer higher than zero"
Environment.Exit 2
failwith "Unreachable"
| true, num ->
let numberOfCommits = Some num
let remote = Some args.[0]
let force =
if args.Length = 3 then
if args.[2] = "-f" || args.[2] = "--force" then
true
else
false
else
false
remote, numberOfCommits, force
| _ ->
Console.Error.WriteLine "Second argument should be an integer"
Environment.Exit 3
failwith "Unreachable"
elif args.Length = 0 then
None, None, false
else // if args.Length = 1 then
match UInt32.TryParse args.[0] with
| true, 0u ->
Console.Error.WriteLine
"Argument for the number of commits should be an integer higher than zero"
Environment.Exit 2
failwith "Unreachable"
| true, num ->
let numberOfCommits = Some num
let remote = None
remote, numberOfCommits, false
| _ ->
let numberOfCommits = None
let remote = Some(args.[0])
remote, numberOfCommits, false
let remote, remoteUrl =
match maybeRemote with
| Some remoteProvided ->
match
Seq.tryFind
(fun (currentRemote, _) -> currentRemote = remoteProvided)
remotes
with
| None ->
Console.Error.WriteLine(
sprintf "Remote '%s' not found" remoteProvided
)
Environment.Exit 4
failwith "unreachable"
| Some remote -> remote
| None ->
if remotes.Count() > 1 then
Console.Error.WriteLine
"Usage: gitpush.fsx <remoteName> [numberOfCommits(optional)]"
Environment.Exit 6
remotes.ElementAt 0
let currentBranch = Git.GetCurrentBranch()
let commitsToBePushed =
match maybeNumberOfCommits with
| None ->
let commitsToPush = FindUnpushedCommits remote currentBranch
if commitsToPush.Length = 0 then
Console.Error.WriteLine(
sprintf
"Current branch '%s' in remote '%s' is already up to date. Force push by specifying number of commits as 2nd argument?"
currentBranch
remote
)
Environment.Exit 5
failwith "Unreachable"
elif commitsToPush.Length = 1 then
// no need to ask for confirmation since 1 commit doesn't need to be separated from other commits
// (one by one doesn't apply to a length of one)
commitsToPush
else //if commitsToPush.Length > 1 then
Console.WriteLine(
sprintf
"Detected a delta of %i commits between local branch '%s' and the one in remote '%s', to be pushed one by one. Press any key to continue or CTRL+C to abort."
commitsToPush.Length
currentBranch
remote
)
Console.ReadKey true |> ignore
Console.WriteLine "Pushing..."
commitsToPush
| Some numberOfCommits -> GetLastCommits numberOfCommits
let numberOfCommitsToPush = commitsToBePushed.Length
for commit in commitsToBePushed do
GitSpecificPush remote commit currentBranch force
if numberOfCommitsToPush > 1 && remoteUrl.Contains "gitlab" then
Console.WriteLine
"NOTE: if you have issues with pipelines being canceled, visit https://gitlab.com/yourUserNameOrOrgName/yourRepoName/-/settings/ci_cd"
Console.WriteLine
"then click 'Expand' on 'General Pipelines', and uncheck 'Auto-cancel redundant pipelines'"