-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-tree.js
More file actions
executable file
·186 lines (157 loc) · 6.77 KB
/
Copy pathgit-tree.js
File metadata and controls
executable file
·186 lines (157 loc) · 6.77 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
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
const version = "1.0.0"
const keychainService = 'dev.annino.gittree'
const { TreeBuilder, Branch, PullRequest, TreeNode, Tree } = require('./model')
const retriveConfiguration = require('./config').retriveConfiguration
const process = require('process')
let readline = require('readline')
const Git = require("nodegit")
const https = require("https")
const argv = require('yargs').argv
async function app(config) {
// Open the git directory. This will be useful for the next version when
// the script will be able to pull the main branch into the dependent branches automatically
const repo = await Git.Repository.open(config.path)
const currentBranch = await repo.getCurrentBranch()
// currentBranch.name() returns refs/head/BRANCH_NAME
const currentBranchName = currentBranch.name().split("/")[2]
console.log(`Your current branch is: ${currentBranchName} \n`)
const remote = await repo.getRemote("origin")
const repositoryName = remote.url().split(":")[1].split(".")[0]
const options = {
path: `/repos/${repositoryName}/pulls`,
headers: {
Authorization: `token ${config.token}`,
'User-Agent': `gittree/${version}`,
}
}
const pullRequestURL = "https://api.github.com"
// Make the request
https.get(pullRequestURL, options, res => {
const { statusCode } = res
const contentType = res.headers['content-type']
let error
if (statusCode !== 200) {
error = new Error('Request Failed.\n' + `Status Code: ${statusCode}`)
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' + `Expected application/json but received ${contentType}`)
}
if (error) {
console.error(error.message)
// Consume response data to free up memory
res.resume()
return
}
let rawData = ''
res.on('data', (chunk) => { rawData += chunk })
res.on('end', async () => {
try {
// Parse the gotten data as JSON
const parsedData = JSON.parse(rawData)
// Extract the info
const PRs = getPullRequestData(parsedData)
const treeBuilder = new TreeBuilder(PRs, "develop")
const tree = treeBuilder.generate()
if (config.all) {
console.log(tree.toString())
process.exit()
} else {
if (tree.rootNode.children == null) {
console.log("Your current branch's PR doesn't have any dependent PR")
process.exit()
}
var y = tree.depthFirstSearch(currentBranchName)
var x = new TreeNode(y.data)
while (y.parent != null) {
const n = new TreeNode(y.parent.data)
x.parent = n
n.children = [x]
y = y.parent
x = x.parent
}
const reducedTree = new Tree(x)
console.log(reducedTree.toString())
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
await new Promise((resolve, error) => {
var waitForUserInput = async function() {
rl.question("Do you want to update the all chain of branches?[y/n] ", async function (answer) {
if (answer == "y") {
rl.close()
resolve()
}
else if (answer == "n") {
process.exit()
}
else {
waitForUserInput()
}
});
}
return waitForUserInput();
})
repo.fetch("origin", {
callbacks: {
credentials: function(url, userName) {
return nodegit.Cred.sshKeyFromAgent(userName);
}
}
})
// Pull the last state
const pull = await repo.mergeBranches(reducedTree.rootNode.data, `origin/${reducedTree.rootNode.data}`)
console.log(`Pulling ${reducedTree.rootNode.data}`)
let next = reducedTree.rootNode.children[0]
const references = await repo.getReferences()
references.forEach((ref) => {
console.log(ref.toString())
})
while(next != null) {
console.log(`On ${next.data}`)
const findResult = references.find((ref, _, obj) => {
return ref.name().includes(`refs/heads/${next.data}`)
})
if (findResult == null) {
const remoteBranch = `origin/${next.data}`;
let commit = await repo.getBranchCommit(remoteBranch);
await repo.createBranch(next.data, commit, 1)
await repo.mergeBranches(next.data, `origin/${next.data}`)
console.log(`Pulling ${next.data}`)
} else {
await repo.mergeBranches(next.data, `origin/${next.data}`)
console.log(`Pulling ${next.data}`)
}
next = next.children[0]
}
console.log("Now Updating all the dependent branches")
let head = reducedTree.rootNode
let nextHead = head.children[0]
while(nextHead != null) {
console.log(`Merging ${head.data} into ${nextHead.data}`)
await repo.mergeBranches(nextHead.data, `origin/${head.data}`)
head = nextHead
nextHead = head.children[0]
}
}
} catch (e) {
console.error(e.message)
}
})
})
function getPullRequestData(data) {
return data.map( (_, jsonIndex) => {
// Get the base branch
const base = data[jsonIndex].base
const baseBranch = new Branch(base.ref)
// Get the head branch
const head = data[jsonIndex].head
const headBranch = new Branch(head.ref)
return new PullRequest(headBranch, baseBranch)
})
}
}
retriveConfiguration(argv, keychainService).then((config, errpr) => {
// Run
app(config)
})