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

Commit 3e6cb75

Browse files
authored
Merge pull request #2270 from zetter/copy-commit-sha-and-subject
Make it easier to copy the commit SHA and subject
2 parents 85681e2 + c50e4dc commit 3e6cb75

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

lib/controllers/recent-commits-controller.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default class RecentCommitsController extends React.Component {
6363
selectPreviousCommit={this.selectPreviousCommit}
6464
selectedCommitSha={this.state.selectedCommitSha}
6565
commands={this.props.commands}
66+
clipboard={atom.clipboard}
6667
/>
6768
);
6869
}

lib/views/recent-commits-view.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Timeago from './timeago';
1212

1313
class RecentCommitView extends React.Component {
1414
static propTypes = {
15+
commands: PropTypes.object.isRequired,
16+
clipboard: PropTypes.object.isRequired,
1517
commit: PropTypes.object.isRequired,
1618
undoLastCommit: PropTypes.func.isRequired,
1719
isMostRecent: PropTypes.bool.isRequired,
@@ -49,6 +51,10 @@ class RecentCommitView extends React.Component {
4951
'is-selected': this.props.isSelected,
5052
})}
5153
onClick={this.props.openCommit}>
54+
<Commands registry={this.props.commands} target={this.refRoot}>
55+
<Command command="github:copy-commit-sha" callback={this.copyCommitSha} />
56+
<Command command="github:copy-commit-subject" callback={this.copyCommitSubject} />
57+
</Commands>
5258
{this.renderAuthors()}
5359
<span
5460
className="github-RecentCommit-message"
@@ -104,6 +110,18 @@ class RecentCommitView extends React.Component {
104110
);
105111
}
106112

113+
copyCommitSha = event => {
114+
event.stopPropagation();
115+
const {commit, clipboard} = this.props;
116+
clipboard.write(commit.sha);
117+
}
118+
119+
copyCommitSubject = event => {
120+
event.stopPropagation();
121+
const {commit, clipboard} = this.props;
122+
clipboard.write(commit.messageSubject);
123+
}
124+
107125
undoLastCommit = event => {
108126
event.stopPropagation();
109127
this.props.undoLastCommit();
@@ -118,6 +136,7 @@ export default class RecentCommitsView extends React.Component {
118136
selectedCommitSha: PropTypes.string.isRequired,
119137

120138
// Atom environment
139+
clipboard: PropTypes.object.isRequired,
121140
commands: PropTypes.object.isRequired,
122141

123142
// Action methods
@@ -192,6 +211,8 @@ export default class RecentCommitsView extends React.Component {
192211
return (
193212
<RecentCommitView
194213
key={commit.getSha()}
214+
commands={this.props.commands}
215+
clipboard={this.props.clipboard}
195216
isMostRecent={i === 0}
196217
commit={commit}
197218
undoLastCommit={this.props.undoLastCommit}

menus/git.cson

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,13 @@
140140
'command': 'github:amend-last-commit'
141141
}
142142
]
143+
'.github-RecentCommit': [
144+
{
145+
'label': 'Copy Commit SHA'
146+
'command': 'github:copy-commit-sha'
147+
}
148+
{
149+
'label': 'Copy Commit Subject'
150+
'command': 'github:copy-commit-subject'
151+
}
152+
]

test/controllers/recent-commits-controller.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ describe('RecentCommitsController', function() {
4545
assert.isTrue(wrapper.find('RecentCommitsView').prop('isLoading'));
4646
});
4747

48+
it('passes the clipboard to the RecentCommitsView', function() {
49+
app = React.cloneElement(app);
50+
const wrapper = shallow(app);
51+
assert.deepEqual(wrapper.find('RecentCommitsView').prop('clipboard'), atom.clipboard);
52+
});
53+
4854
describe('openCommit({sha, preserveFocus})', function() {
4955
it('opens a commit detail item', async function() {
5056
sinon.stub(atomEnv.workspace, 'open').resolves();

test/views/recent-commits-view.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('RecentCommitsView', function() {
1414
app = (
1515
<RecentCommitsView
1616
commits={[]}
17+
clipboard={{}}
1718
isLoading={false}
1819
selectedCommitSha=""
1920
commands={atomEnv.commands}
@@ -220,4 +221,34 @@ describe('RecentCommitsView', function() {
220221
assert.isNull(await instance.retreatFocusFrom(CommitView.firstFocus));
221222
});
222223
});
224+
225+
describe('copying details of a commit', function() {
226+
let wrapper;
227+
let clipboard;
228+
229+
beforeEach(function() {
230+
const commits = [
231+
commitBuilder().sha('0000').messageSubject('subject 0').build(),
232+
commitBuilder().sha('1111').messageSubject('subject 1').build(),
233+
commitBuilder().sha('2222').messageSubject('subject 2').build(),
234+
];
235+
clipboard = {write: sinon.spy()};
236+
app = React.cloneElement(app, {commits, clipboard});
237+
wrapper = mount(app);
238+
});
239+
240+
it('copies the commit sha on github:copy-commit-sha', function() {
241+
const commitNode = wrapper.find('.github-RecentCommit').at(1).getDOMNode();
242+
atomEnv.commands.dispatch(commitNode, 'github:copy-commit-sha');
243+
assert.isTrue(clipboard.write.called);
244+
assert.isTrue(clipboard.write.calledWith('1111'));
245+
});
246+
247+
it('copies the commit subject on github:copy-commit-subject', function() {
248+
const commitNode = wrapper.find('.github-RecentCommit').at(1).getDOMNode();
249+
atomEnv.commands.dispatch(commitNode, 'github:copy-commit-subject');
250+
assert.isTrue(clipboard.write.called);
251+
assert.isTrue(clipboard.write.calledWith('subject 1'));
252+
});
253+
});
223254
});

0 commit comments

Comments
 (0)