-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgithub-3way-sync.flowl
99 lines (90 loc) · 2.8 KB
/
github-3way-sync.flowl
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
// 3way-sync: Automatically synchronize local, origin, upstream three repositories.
load "go:http_get"
load "go:git_push"
load "go:git_check_merge"
load "go:git_fetch"
load "go:git_rebase"
load "go:git_pull"
load "go:git_add_upstream"
load "go:git_basic"
// Get some base information from local git repo
var info
co git_basic -> info
if_none_exit $(info.github_org) "not found github_org in local git repo"
if_none_exit $(info.github_repo) "not found github_repo in local git repo"
// Figure out the parent repo that the github repo forks from.
var resp
co http_get -> resp {
"url": "https://api.github.com/repos/$(info.github_org)/$(info.github_repo)"
"query_json_path": "parent.clone_url"
}
if_none_exit $(resp.parent.clone_url) "not found parent.clone_url through github api"
switch {
case $(info.upstream) == "" {
// Add upstream
co git_add_upstream {
"upstream_url": "$(resp.parent.clone_url)"
}
}
case $(info.upstream) != "" && $(info.upstream) != $(resp.parent.clone_url) {
exit "upstream and clone_url are not the same git repo"
}
case $(info.current_branch) != "master" && $(info.current_branch) != "main" {
exit "current branch is not master or main"
}
}
co git_pull
co git_fetch
// Check if the current branch is up to date with upstream git repo, if not, will rebase
// the current branch.
fn git_check_local_upstream = git_check_merge {
var to = $(info.current_branch)
var from = "upstream/$(info.current_branch)"
args = {
"to_branch": "$(to)"
"from_branch": "$(from)"
}
}
fn git_rebase_upstream = git_rebase {
var branch = "upstream/$(info.current_branch)"
args = {
"branch": "$(branch)"
}
}
var local_upstream_conflict
co git_check_local_upstream -> local_upstream_conflict
switch {
case $(local_upstream_conflict.status) == "no-conflict" {
co git_rebase_upstream
}
case $(local_upstream_conflict.status) == "conflict" {
exit "local and upstream have conflicts"
}
}
// Check if the origin branch is up to date with local current branch, if not, will push
// the current branch to origin.
fn git_check_local_origin = git_check_merge {
var to = "origin/$(info.current_branch)"
var from = "$(info.current_branch)"
args = {
"to_branch": "$(to)"
"from_branch": "$(from)"
}
}
fn git_push_origin = git_push {
var branch = $(info.current_branch)
args = {
"local_branch": "$(branch)"
"remote_branch": "$(branch)"
}
}
var local_origin_conflict
co git_check_local_origin -> local_origin_conflict
switch {
case $(local_origin_conflict.status) == "no-conflict" {
co git_push_origin
}
case $(local_origin_conflict.status) == "conflict" {
exit "local and origin have conflicts"
}
}