Skip to content

Commit 4de7dd1

Browse files
committed
Fixes from code review
1 parent 1f76690 commit 4de7dd1

File tree

10 files changed

+95
-226
lines changed

10 files changed

+95
-226
lines changed

jupyterlab_git/git.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
ALLOWED_OPTIONS = ['user.name', 'user.email']
1414

1515

16-
class git_auth_input_wrapper:
16+
class GitAuthInputWrapper:
1717
"""
1818
Helper class which is meant to replace subprocess.Popen for communicating
1919
with git CLI when also sending username and password for auth
2020
"""
21-
def __init__(self, command, cwd, env, username, password, *args, **kwargs):
22-
super(git_auth_input_wrapper, self).__init__(*args, **kwargs)
21+
def __init__(self, command, cwd, env, username, password):
2322
self.command = command
2423
self.cwd = cwd
2524
self.env = env
@@ -162,7 +161,7 @@ def clone(self, current_path, repo_url, auth=None):
162161
env = os.environ.copy()
163162
if (auth):
164163
env["GIT_TERMINAL_PROMPT"] = "1"
165-
p = git_auth_input_wrapper(
164+
p = GitAuthInputWrapper(
166165
command='git clone {} -q'.format(unquote(repo_url)),
167166
cwd=os.path.join(self.root_dir, current_path),
168167
env = env,
@@ -589,36 +588,14 @@ def checkout_all(self, top_repo_path):
589588
)
590589
return my_output
591590

592-
def commit(self, commit_msg, top_repo_path, author_name=None, author_email=None):
591+
def commit(self, commit_msg, top_repo_path):
593592
"""
594593
Execute git commit <filename> command & return the result.
595594
"""
596-
command = ["git", "commit", "-m", commit_msg]
597-
598-
if author_name != None and author_email != None:
599-
command.insert(1, '-c')
600-
command.insert(2, f'user.name=\'{author_name}\'')
601-
command.insert(3, '-c')
602-
command.insert(4, f'user.email=\'{author_email}\'')
603-
604-
p = Popen(
605-
command,
606-
stdout=PIPE,
607-
stderr=PIPE,
608-
cwd=top_repo_path,
595+
my_output = subprocess.check_output(
596+
["git", "commit", "-m", commit_msg], cwd=top_repo_path
609597
)
610-
my_output, my_error = p.communicate()
611-
612-
if p.returncode == 0:
613-
return {
614-
"code": p.returncode,
615-
"message": my_output.decode("utf-8").strip("\n"),
616-
}
617-
else:
618-
return {
619-
"code": p.returncode,
620-
"message": my_error.decode("utf-8").strip("\n"),
621-
}
598+
return my_output
622599

623600
def pull(self, curr_fb_path, auth=None):
624601
"""
@@ -628,7 +605,7 @@ def pull(self, curr_fb_path, auth=None):
628605
env = os.environ.copy()
629606
if (auth):
630607
env["GIT_TERMINAL_PROMPT"] = "1"
631-
p = git_auth_input_wrapper(
608+
p = GitAuthInputWrapper(
632609
command = 'git pull --no-commit',
633610
cwd = os.path.join(self.root_dir, curr_fb_path),
634611
env = env,
@@ -661,7 +638,7 @@ def push(self, remote, branch, curr_fb_path, auth=None):
661638
env = os.environ.copy()
662639
if (auth):
663640
env["GIT_TERMINAL_PROMPT"] = "1"
664-
p = git_auth_input_wrapper(
641+
p = GitAuthInputWrapper(
665642
command = 'git push {} {}'.format(remote, branch),
666643
cwd = os.path.join(self.root_dir, curr_fb_path),
667644
env = env,

jupyterlab_git/handlers.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,6 @@ def post(self):
306306
class GitCommitHandler(GitHandler):
307307
"""
308308
Handler for 'git commit -m <message>'. Commits files.
309-
310-
When author information is sent, adds that information to commit
311-
312-
Input format:
313-
{
314-
'top_repo_path': 'top/repo/path',
315-
'commit_msg': 'commit_message_to_add',
316-
OPTIONAL: 'author_name' : 'Notebook User',
317-
OPTIONAL: 'author_email' : '[email protected]'
318-
}
319309
"""
320310

321311
def post(self):
@@ -325,12 +315,7 @@ def post(self):
325315
data = self.get_json_body()
326316
top_repo_path = data["top_repo_path"]
327317
commit_msg = data["commit_msg"]
328-
if "author_name" in data.keys() and "author_email" in data.keys():
329-
author_name = data["author_name"]
330-
author_email = data["author_email"]
331-
my_output = self.git.commit(commit_msg, top_repo_path, author_name, author_email)
332-
else:
333-
my_output = self.git.commit(commit_msg, top_repo_path)
318+
my_output = self.git.commit(commit_msg, top_repo_path)
334319
self.finish(my_output)
335320

336321

@@ -361,13 +346,7 @@ def post(self):
361346
POST request handler, pulls files from a remote branch to your current branch.
362347
"""
363348
data = self.get_json_body()
364-
365-
#Different request with and without auth
366-
if 'auth' in data.keys():
367-
auth = data['auth']
368-
response = self.git.pull(data['current_path'], data['auth'])
369-
else:
370-
response = self.git.pull(data['current_path'])
349+
response = self.git.pull(data['current_path'], data.get('auth', None))
371350

372351
self.finish(json.dumps(response))
373352

@@ -402,12 +381,7 @@ def post(self):
402381
remote = upstream[0]
403382
branch = ":".join(["HEAD", upstream[1]])
404383

405-
#Different request with and without auth
406-
if 'auth' in data.keys():
407-
auth = data['auth']
408-
response = self.git.push(remote, branch, current_path, auth)
409-
else:
410-
response = self.git.push(remote, branch, current_path)
384+
response = self.git.push(remote, branch, current_path, data.get('auth', None))
411385

412386
else:
413387
response = {

jupyterlab_git/tests/test_clone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from mock import patch, call, Mock
44

5-
from jupyterlab_git.git import Git, git_auth_input_wrapper
5+
from jupyterlab_git.git import Git, GitAuthInputWrapper
66

77

88
@patch('subprocess.Popen')

src/components/CommitAuthorBox.tsx

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/components/CredentialsBox.tsx

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/components/PathHeader.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import * as React from 'react';
1010

1111
import { classes } from 'typestyle';
1212

13-
import { Git, IGitAuth } from '../git';
13+
import { Git } from '../git';
1414

1515
import { Dialog } from '@jupyterlab/apputils';
1616

1717
import { GitPullPushDialog, Operation } from '../widgets/gitPushPull';
1818

19-
import { GitCredentialsForm } from './CredentialsBox';
19+
import { GitCredentialsForm } from '../widgets/CredentialsBox';
2020

2121
export interface IPathHeaderState {
2222
refresh: any;
@@ -97,7 +97,7 @@ export class PathHeader extends React.Component<
9797
let result = await dialog.launch();
9898
dialog.dispose();
9999
let retry = false;
100-
while (result.button.label === 'Cancel') {
100+
while (!result.button.accept) {
101101
let credentialsDialog = new Dialog({
102102
title: 'Git credentials required',
103103
body: new GitCredentialsForm(
@@ -111,19 +111,15 @@ export class PathHeader extends React.Component<
111111
let response = await credentialsDialog.launch();
112112
credentialsDialog.dispose();
113113

114-
if (response.button.label === 'OK') {
114+
if (response.button.accept) {
115115
// user accepted attempt to login
116-
117-
let authJson = JSON.parse(decodeURIComponent(response.value));
118-
// call gitApi.push again with credentials
119-
let auth: IGitAuth = {
120-
username: authJson.username,
121-
password: authJson.password
122-
};
123-
124116
dialog = new Dialog({
125117
title: `Git ${operation}`,
126-
body: new GitPullPushDialog(currentFileBrowserPath, operation, auth),
118+
body: new GitPullPushDialog(
119+
currentFileBrowserPath,
120+
operation,
121+
response.value
122+
),
127123
buttons: [Dialog.okButton({ label: 'DISMISS' })]
128124
});
129125
result = await dialog.launch();

src/git.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,12 @@ export class Git {
197197
constructor() {}
198198

199199
/** Make request for the Git Pull API. */
200-
async pull(
201-
path: string,
202-
auth: IGitAuth = undefined
203-
): Promise<IGitPushPullResult> {
200+
async pull(path: string, auth?: IGitAuth): Promise<IGitPushPullResult> {
204201
try {
205202
let obj: IGitPushPull = {
206-
current_path: path
203+
current_path: path,
204+
auth
207205
};
208-
if (auth) {
209-
obj.auth = auth;
210-
}
211206

212207
let response = await httpGitRequest('/git/pull', 'POST', obj);
213208
if (response.status !== 200) {
@@ -221,19 +216,13 @@ export class Git {
221216
}
222217

223218
/** Make request for the Git Push API. */
224-
async push(
225-
path: string,
226-
auth: IGitAuth = undefined
227-
): Promise<IGitPushPullResult> {
219+
async push(path: string, auth?: IGitAuth): Promise<IGitPushPullResult> {
228220
try {
229221
let obj: IGitPushPull = {
230-
current_path: path
222+
current_path: path,
223+
auth
231224
};
232225

233-
if (auth) {
234-
obj.auth = auth;
235-
}
236-
237226
let response = await httpGitRequest('/git/push', 'POST', obj);
238227
if (response.status !== 200) {
239228
const data = await response.json();
@@ -249,18 +238,15 @@ export class Git {
249238
async clone(
250239
path: string,
251240
url: string,
252-
auth: IGitAuth = undefined
241+
auth?: IGitAuth
253242
): Promise<IGitCloneResult> {
254243
try {
255244
let obj: IGitClone = {
256245
current_path: path,
257-
clone_url: url
246+
clone_url: url,
247+
auth
258248
};
259249

260-
if (auth) {
261-
obj.auth = auth;
262-
}
263-
264250
let response = await httpGitRequest('/git/clone', 'POST', obj);
265251
if (response.status !== 200) {
266252
const data = await response.json();

0 commit comments

Comments
 (0)