@aaronshaf/ger 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Aaron Shafovaloff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # ger
2
+ gerrit cli
@@ -0,0 +1,124 @@
1
+ // Extract local branch names
2
+ const stdout = execSync(
3
+ `git for-each-ref --sort=committerdate refs/heads/ --format='%(refname:lstrip=2)' | grep -v '^master$'`
4
+ )
5
+ const localBranches = stdout.toString().trim().split('\n')
6
+
7
+ const localBranchToChangeId = {}
8
+
9
+ // Initialize an empty query string
10
+ let query = ''
11
+
12
+ localBranches.forEach(branch => {
13
+ // Get the latest commit message
14
+ const commit_message = execSync(`git log -1 --pretty=%B ${branch}`).toString().trim()
15
+
16
+ // Extract the Change-Id
17
+ const regex = /Change-Id: .{41}/
18
+ const change_id_match = commit_message.match(regex)
19
+
20
+ if (change_id_match) {
21
+ const change_id = change_id_match[0].substring(11)
22
+ localBranchToChangeId[branch] = change_id
23
+
24
+ // Append the Change-Id to the query string
25
+ query += `change:${change_id} OR `
26
+ } else {
27
+ localBranchToChangeId[branch] = null
28
+ }
29
+ })
30
+
31
+ // Remove the trailing " OR "
32
+ query = query.replace(/ OR $/, '')
33
+
34
+ // Run the gerrit query command
35
+ const gerrit_output = execSync(
36
+ `ssh gerrit "gerrit query --current-patch-set --format=JSON '${query}'"`
37
+ )
38
+ .toString()
39
+ .trim()
40
+ .split('\n')
41
+ .map(line => JSON.parse(line))
42
+
43
+ const gerritOutputByChangeId = {}
44
+ for (const change of gerrit_output) {
45
+ gerritOutputByChangeId[change.id] = change
46
+ }
47
+
48
+ function bold(text) {
49
+ return `\x1b[1m${text}\x1b[0m`
50
+ }
51
+ // grey background
52
+ function grey(text) {
53
+ return `\x1b[47m\x1b[30m${text}\x1b[0m`
54
+ }
55
+ function darkBrown(text) {
56
+ return `\x1b[43m\x1b[30m${text}\x1b[0m`
57
+ }
58
+ function green(text) {
59
+ return `\x1b[42m\x1b[30m${text}\x1b[0m`
60
+ }
61
+ // function red(text) {
62
+ // return `\x1b[41m\x1b[30m${text}\x1b[0m`
63
+ // }
64
+
65
+ const mergedBranches = localBranches.filter(localBranch => {
66
+ const changeId = localBranchToChangeId[localBranch]
67
+ const gerritData = gerritOutputByChangeId[changeId]
68
+ return gerritData?.status === 'MERGED'
69
+ })
70
+
71
+ for (const localBranch of localBranches) {
72
+ const changeId = localBranchToChangeId[localBranch]
73
+ if (changeId && gerritOutputByChangeId[changeId]) {
74
+ const gerritData = gerritOutputByChangeId[changeId]
75
+ const isWip = gerritData.wip || gerritData.subject.toLowerCase().startsWith('WIP')
76
+ const isMerged = gerritData.status === 'MERGED'
77
+ const isAbandoned = gerritData.status === 'ABANDONED'
78
+ const labels = [
79
+ isWip && darkBrown('WIP'),
80
+ isAbandoned && grey('Abandoned'),
81
+ isMerged && green('Merged'),
82
+ ].filter(Boolean)
83
+ console.log(`${bold(localBranch)}: ${gerritData.subject} ${labels.join(' ')}`)
84
+ console.log(` ${gerritData.url.replace('https://', '')}`)
85
+ for (const approval of gerritData.currentPatchSet.approvals) {
86
+ let description = approval.description
87
+ if (approval.type === 'SUBM' && !description) {
88
+ description = 'Submitted'
89
+ }
90
+ console.log(
91
+ ` ${description || approval.type}: ${approval.value} (${approval.by.name.replace(
92
+ ' (Bot)',
93
+ ''
94
+ )})`
95
+ )
96
+ }
97
+ } else {
98
+ console.log(`${localBranch}`)
99
+ }
100
+ // blank line
101
+ console.log('')
102
+ }
103
+
104
+ // if -d flag is passed
105
+ if (process.argv[3] === '-d') {
106
+ async function promptForMergedBranchDeletion() {
107
+ const readline = require('readline').createInterface({
108
+ input: process.stdin,
109
+ output: process.stdout,
110
+ })
111
+
112
+ for (const mergedLocalBranch of mergedBranches) {
113
+ const answer = await new Promise(resolve => {
114
+ readline.question(`Delete merged local branch ${bold(mergedLocalBranch)}? (y/n) `, resolve)
115
+ })
116
+ if (answer.toLowerCase() === 'y') {
117
+ execSync(`git branch -D ${mergedLocalBranch}`)
118
+ console.log(` Deleted ${bold(mergedLocalBranch)}`)
119
+ }
120
+ }
121
+ process.exit(0)
122
+ }
123
+ promptForMergedBranchDeletion()
124
+ }
package/ger.js ADDED
@@ -0,0 +1,6 @@
1
+ import {execSync} from 'child_process'
2
+ import branch from './branch.js'
3
+
4
+ if (process.argv[2] === 'branch') {
5
+ branch()
6
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@aaronshaf/ger",
3
+ "private": false,
4
+ "version": "1.0.0",
5
+ "description": "Gerrit CLI",
6
+ "main": "ger.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/aaronshaf/ger.git"
10
+ },
11
+ "keywords": [
12
+ "gerrit"
13
+ ],
14
+ "author": "Aaron Shafovaloff <aaronshaf@gmail.com>",
15
+ "license": "MIT",
16
+ "bugs": {
17
+ "url": "https://github.com/aaronshaf/ger/issues"
18
+ },
19
+ "homepage": "https://github.com/aaronshaf/ger#readme"
20
+ }