Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 5396e43

Browse files
authored
Merge pull request #2 from RobBoeckermann/branching
Branching
2 parents 457caa3 + cea8b03 commit 5396e43

File tree

6 files changed

+422
-8
lines changed

6 files changed

+422
-8
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ This extension depends on [Code for IBM i](https://github.com/halcyon-tech/code-
1010

1111
This adds 'Status', 'Commits' and 'File History' to the source control view. It will assume that your home directory, set in Code for IBM i is also a git repository. When you change your home directory, the panels will refresh automatically.
1212

13-
#### Status
13+
TODO: Stashing
14+
15+
#### Branches
1416

15-
This view will allow you to stage, unstage, restore and view a diff of your working tree.
17+
This view will display remote and local branches and allow you to create, delete, checkout, and merge branches.
1618

17-
To do:
19+
#### Status
1820

19-
* Commit, pull & push
21+
This view will allow you to commit, pull, push, stage, unstage, restore and view a diff of your working tree.
2022

2123
#### Commits
2224

extension.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const vscode = require(`vscode`);
44

55
const {instance, Field, CustomUI} = vscode.extensions.getExtension(`halcyontechltd.code-for-ibmi`).exports;
66

7+
const branchesView = require(`./src/views/branches`);
78
const statusView = require(`./src/views/status`);
89
const commitView = require(`./src/views/commits`);
910
const fileHistory = require(`./src/views/fileHistory`);
@@ -19,6 +20,11 @@ function activate(context) {
1920
console.log(`Congratulations, your extension "git-client-ibmi" is now active!`);
2021

2122
context.subscriptions.push(
23+
vscode.window.registerTreeDataProvider(
24+
`git-client-ibmi.branches`,
25+
new branchesView(context)
26+
),
27+
2228
vscode.window.registerTreeDataProvider(
2329
`git-client-ibmi.status`,
2430
new statusView(context)

package.json

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@
4040
"onCommand:git-client-ibmi.commits.refresh",
4141
"onView:git-client-ibmi.fileHistory",
4242
"onCommand:git-client-ibmi.viewCommitFile",
43-
"onCommand:git-client-ibmi.viewCommitFileDiff"
43+
"onCommand:git-client-ibmi.viewCommitFileDiff",
44+
"onCommand:git-client-ibmi.branches.refresh",
45+
"onCommand:git-client-ibmi.branches.branch",
46+
"onCommand:git-client-ibmi.branches.deleteBranch",
47+
"onCommand:git-client-ibmi.branches.checkout",
48+
"onCommand:git-client-ibmi.branches.merge"
4449
],
4550
"main": "./extension",
4651
"contributes": {
@@ -104,6 +109,33 @@
104109
"command": "git-client-ibmi.viewCommitFileDiff",
105110
"title": "View diff",
106111
"category": "Git on IBM i"
112+
},
113+
{
114+
"command": "git-client-ibmi.branches.branch",
115+
"title": "Create Branch",
116+
"category": "Git on IBM i",
117+
"icon": "$(git-branch-create)"
118+
},
119+
{
120+
"command": "git-client-ibmi.branches.deleteBranch",
121+
"title": "Delete Branch",
122+
"category": "Git on IBM i"
123+
},
124+
{
125+
"command": "git-client-ibmi.branches.checkout",
126+
"title": "Checkout Branch",
127+
"category": "Git on IBM i"
128+
},
129+
{
130+
"command": "git-client-ibmi.branches.merge",
131+
"title": "Merge Branch",
132+
"category": "Git on IBM i"
133+
},
134+
{
135+
"command": "git-client-ibmi.branches.refresh",
136+
"title": "Refresh branches view",
137+
"category": "Git on IBM i",
138+
"icon": "$(refresh)"
107139
}
108140
],
109141
"viewsWelcome": [{
@@ -113,6 +145,11 @@
113145
}],
114146
"views": {
115147
"scm": [{
148+
"id": "git-client-ibmi.branches",
149+
"name": "Branches",
150+
"contextualTitle": "IBM i"
151+
},
152+
{
116153
"id": "git-client-ibmi.status",
117154
"name": "Status",
118155
"contextualTitle": "IBM i"
@@ -156,6 +193,16 @@
156193
"command": "git-client-ibmi.commits.refresh",
157194
"group": "navigation",
158195
"when": "view == git-client-ibmi.commits"
196+
},
197+
{
198+
"command": "git-client-ibmi.branches.branch",
199+
"group": "navigation",
200+
"when": "view == git-client-ibmi.branches"
201+
},
202+
{
203+
"command": "git-client-ibmi.branches.refresh",
204+
"group": "navigation",
205+
"when": "view == git-client-ibmi.branches"
159206
}
160207
],
161208
"view/item/context": [
@@ -174,6 +221,18 @@
174221
{
175222
"command": "git-client-ibmi.viewCommitFile",
176223
"when": "view == git-client-ibmi.commits && viewItem == commitFile"
224+
},
225+
{
226+
"command": "git-client-ibmi.branches.checkout",
227+
"when": "view == git-client-ibmi.branches && viewItem == remote || viewItem == local"
228+
},
229+
{
230+
"command": "git-client-ibmi.branches.deleteBranch",
231+
"when": "view == git-client-ibmi.branches && viewItem == remote || viewItem == local"
232+
},
233+
{
234+
"command": "git-client-ibmi.branches.merge",
235+
"when": "view == git-client-ibmi.branches && viewItem == local"
177236
}
178237
]
179238
}
@@ -195,4 +254,4 @@
195254
"mocha": "^8.2.1",
196255
"vscode-test": "^1.5.0"
197256
}
198-
}
257+
}

src/api/git.js

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,108 @@ module.exports = class Git {
225225
this.path,
226226
);
227227
}
228-
}
228+
229+
/**
230+
* @returns {remote: branch_name[], local: {branch_name, state}[]}}
231+
*/
232+
async list_branches() {
233+
const connection = instance.getConnection();
234+
let remote = [], local = [];
235+
236+
let item = {branch_name: '', state: ''};
237+
let content = await connection.paseCommand(
238+
`echo '"' && ${this.gitPath} branch --all --list`,
239+
this.path,
240+
);
241+
242+
content = content.substring(1);
243+
244+
for (let line of content.split(`\n`)) {
245+
if (line.trim() === ``) continue;
246+
247+
item.state = (line[0] == '*') ? 'checked out' : '';
248+
item.branch_name = line.substr(2);
249+
const remote_or_local = (item.branch_name.split('/')[0] == 'remotes') ? 'remote' : 'local';
250+
251+
switch (remote_or_local) {
252+
case `remote`:
253+
remote.push(item.branch_name);
254+
break;
255+
case `local`:
256+
local.push({branch_name: item.branch_name, state: item.state});
257+
break;
258+
}
259+
}
260+
261+
return {remote, local};
262+
}
263+
264+
/**
265+
* Create a branch
266+
* @param {string} new_branch_name
267+
*/
268+
async create_branch(new_branch_name) {
269+
const connection = instance.getConnection();
270+
await connection.paseCommand(
271+
`${this.gitPath} branch "${new_branch_name}"`,
272+
this.path,
273+
);
274+
}
275+
276+
/**
277+
* Delete a remote branch
278+
* @param {string} branch_to_delete
279+
* @param {string} remote_or_local
280+
*/
281+
async deleteBranch(branch_to_delete, remote_or_local) {
282+
let result = await vscode.window.showWarningMessage(`Are you sure you want to delete branch ${branch_to_delete}?`, `Yes`, `Cancel`);
283+
284+
if (result === `Yes`) {
285+
const connection = instance.getConnection();
286+
if(remote_or_local == "remote"){
287+
const split_branch_to_delete = branch_to_delete.split('/');
288+
var command = `${this.gitPath} push "${split_branch_to_delete[1]}" --delete "${split_branch_to_delete[2]}"`;
289+
}
290+
else{
291+
var command = `${this.gitPath} branch -D "${branch_to_delete}"`;
292+
}
293+
294+
await connection.paseCommand(
295+
command,
296+
this.path,
297+
);
298+
}
299+
}
300+
301+
/**
302+
* Checkout a branch
303+
* @param {string} branch_to_checkout
304+
* @param {string} remote_or_local
305+
*/
306+
async checkout(branch_to_checkout, remote_or_local) {
307+
const connection = instance.getConnection();
308+
if(remote_or_local == "remote"){
309+
const split_branch_name = branch_to_checkout.split('/');
310+
var command = `${this.gitPath} checkout -b "${split_branch_name[2]}" "${split_branch_name[1]}"/"${split_branch_name[2]}"`;
311+
}
312+
else{
313+
var command = `${this.gitPath} checkout "${branch_to_checkout}"`;
314+
}
315+
await connection.paseCommand(
316+
command,
317+
this.path,
318+
);
319+
}
320+
321+
/**
322+
* Merge a branch into the current branch
323+
* @param {string} branch_to_merge_into_current_branch
324+
*/
325+
async merge(branch_to_merge_into_current_branch) {
326+
const connection = instance.getConnection();
327+
await connection.paseCommand(
328+
`${this.gitPath} merge "${branch_to_merge_into_current_branch}"`,
329+
this.path,
330+
);
331+
}
332+
}

0 commit comments

Comments
 (0)