Skip to content

Commit fb1ef73

Browse files
navn-rfcollonval
andcommitted
Apply suggestions from code review
Co-authored-by: Frédéric Collonval <[email protected]>
1 parent fa4c0a0 commit fb1ef73

File tree

7 files changed

+43
-38
lines changed

7 files changed

+43
-38
lines changed

jupyterlab_git/git.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,24 @@ async def status(self, path):
432432

433433
return data
434434

435-
async def log(self, path, history_count=10, follow_path="."):
435+
async def log(self, path, history_count=10, follow_path=None):
436436
"""
437437
Execute git log command & return the result.
438438
"""
439-
is_single_file = follow_path != "."
439+
is_single_file = follow_path != None
440440
cmd = [
441441
"git",
442442
"log",
443443
"--pretty=format:%H%n%an%n%ar%n%s",
444444
("-%d" % history_count),
445445
]
446446
if is_single_file:
447-
cmd = cmd + ["--numstat", "-z", "--follow", "--", follow_path]
447+
cmd = cmd + [
448+
"--numstat",
449+
"--follow",
450+
"--",
451+
follow_path,
452+
]
448453
code, my_output, my_error = await execute(
449454
cmd,
450455
cwd=path,
@@ -453,7 +458,10 @@ async def log(self, path, history_count=10, follow_path="."):
453458
return {"code": code, "command": " ".join(cmd), "message": my_error}
454459

455460
result = []
456-
line_array = my_output.replace("\0\0", "\n").splitlines()
461+
if is_single_file:
462+
# an extra newline get outputted when --numstat is used
463+
my_output = my_output.replace("\n\n", "\n")
464+
line_array = my_output.splitlines()
457465
i = 0
458466
PREVIOUS_COMMIT_OFFSET = 5 if is_single_file else 4
459467
while i < len(line_array):

jupyterlab_git/handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ async def post(self, path: str = ""):
196196

197197
class GitLogHandler(GitHandler):
198198
"""
199-
Handler for 'git log --pretty=format:%H-%an-%ar-%s --follow --'.
199+
Handler for 'git log'.
200200
Fetches Commit SHA, Author Name, Commit Date & Commit Message.
201201
"""
202202

@@ -208,7 +208,7 @@ async def post(self, path: str = ""):
208208
"""
209209
body = self.get_json_body()
210210
history_count = body.get("history_count", 25)
211-
follow_path = body.get("follow_path", ".")
211+
follow_path = body.get("follow_path")
212212
result = await self.git.log(
213213
self.url2localpath(path), history_count, follow_path
214214
)

src/commandsAndMenu.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -898,14 +898,19 @@ export function addCommands(
898898
label: trans.__('History'),
899899
caption: trans.__('View the history of this file'),
900900
execute: args => {
901-
const {
902-
files: [file]
903-
} = args as any as CommandArguments.IGitContextAction;
904-
if (file && file.status === 'unmodified') {
901+
const { files } = args as any as CommandArguments.IGitContextAction;
902+
const file = files[0];
903+
if (!file) {
904+
return;
905+
} else if (file.status === 'unmodified') {
905906
gitModel.selectedHistoryFile = file;
906907
shell.activateById('jp-git-sessions');
907908
}
908909
},
910+
isEnabled: args => {
911+
const { files } = args as any as CommandArguments.IGitContextAction;
912+
return files.length === 1;
913+
},
909914
icon: historyIcon.bindprops({ stylesheet: 'menuItem' })
910915
});
911916

@@ -1085,10 +1090,9 @@ export function addFileBrowserContextMenu(
10851090
)
10861091
);
10871092

1088-
// if looking at a tracked file with no changes,
1089-
// it has no status, nor any actions available
1093+
// if looking at a tracked file without any actions available
10901094
// (although `git rm` would be a valid action)
1091-
if (allCommands.size === 0 && statuses.size === 0) {
1095+
if (allCommands.size === 0) {
10921096
allCommands.add(ContextCommandIDs.gitNoAction);
10931097
}
10941098

src/components/HistorySideBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { TranslationBundle } from '@jupyterlab/translation';
2+
import { closeIcon } from '@jupyterlab/ui-components';
23
import { CommandRegistry } from '@lumino/commands';
34
import * as React from 'react';
45
import { CommandArguments } from '../commandsAndMenu';
56
import { GitExtension } from '../model';
67
import { hiddenButtonStyle } from '../style/ActionButtonStyle';
78
import { historySideBarStyle } from '../style/HistorySideBarStyle';
8-
import { removeIcon } from '../style/icons';
99
import { ContextCommandIDs, Git } from '../tokens';
1010
import { ActionButton } from './ActionButton';
1111
import { FileItem } from './FileItem';
@@ -112,7 +112,7 @@ export const HistorySideBar: React.FunctionComponent<IHistorySideBarProps> = (
112112
actions={
113113
<ActionButton
114114
className={hiddenButtonStyle}
115-
icon={removeIcon}
115+
icon={closeIcon}
116116
title={props.trans.__('Discard file history')}
117117
onClick={removeSelectedFile}
118118
/>

src/components/PastCommitNode.tsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { CommandRegistry } from '@lumino/commands';
44
import * as React from 'react';
55
import { classes } from 'typestyle';
66
import { GitExtension } from '../model';
7-
import { diffIcon } from '../style/icons';
87
import {
98
branchClass,
109
branchWrapperClass,
@@ -20,7 +19,6 @@ import {
2019
workingBranchClass
2120
} from '../style/PastCommitNode';
2221
import { Git } from '../tokens';
23-
import { ActionButton } from './ActionButton';
2422

2523
/**
2624
* Interface describing component properties.
@@ -107,6 +105,11 @@ export class PastCommitNode extends React.Component<
107105
? commitExpandedClass
108106
: null
109107
)}
108+
title={
109+
this.props.children
110+
? this.props.trans.__('View commit details')
111+
: this.props.trans.__('View file changes')
112+
}
110113
onClick={this._onCommitClick}
111114
>
112115
<div className={commitHeaderClass}>
@@ -119,19 +122,10 @@ export class PastCommitNode extends React.Component<
119122
<span className={commitHeaderItemClass}>
120123
{this.props.commit.date}
121124
</span>
122-
{this.props.children ? (
123-
this.state.expanded ? (
124-
<caretUpIcon.react className={iconButtonClass} tag="span" />
125-
) : (
126-
<caretDownIcon.react className={iconButtonClass} tag="span" />
127-
)
125+
{this.props.children && this.state.expanded ? (
126+
<caretUpIcon.react className={iconButtonClass} tag="span" />
128127
) : (
129-
!!this.props.onOpenDiff && (
130-
<ActionButton
131-
icon={diffIcon}
132-
title={this.props.trans.__('View file changes')}
133-
/>
134-
)
128+
<caretDownIcon.react className={iconButtonClass} tag="span" />
135129
)}
136130
</div>
137131
<div className={branchWrapperClass}>{this._renderBranches()}</div>
@@ -198,8 +192,8 @@ export class PastCommitNode extends React.Component<
198192
this.setState({
199193
expanded: !this.state.expanded
200194
});
201-
} else if (!!this.props.onOpenDiff) {
202-
this.props.onOpenDiff(event);
195+
} else {
196+
this.props.onOpenDiff?.call(this, event);
203197
}
204198
};
205199
}

src/model.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ export class GitExtension implements IGitExtension {
193193
return this._selectedHistoryFile;
194194
}
195195
set selectedHistoryFile(file: Git.IStatusFile | null) {
196-
this._selectedHistoryFile = file;
197-
this._selectedHistoryFileChanged.emit(file);
196+
if (this._selectedHistoryFile !== file) {
197+
this._selectedHistoryFile = file;
198+
this._selectedHistoryFileChanged.emit(file);
199+
}
198200
}
199201

200202
/**
@@ -722,9 +724,7 @@ export class GitExtension implements IGitExtension {
722724
'POST',
723725
{
724726
history_count: count,
725-
follow_path: !!this.selectedHistoryFile
726-
? this.selectedHistoryFile.to
727-
: '.'
727+
follow_path: this.selectedHistoryFile?.to
728728
}
729729
);
730730
}

style/icons/clock.svg

Lines changed: 1 addition & 2 deletions
Loading

0 commit comments

Comments
 (0)