-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitbucket_compare_pr_commits.js
More file actions
220 lines (192 loc) · 7.59 KB
/
bitbucket_compare_pr_commits.js
File metadata and controls
220 lines (192 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
javascript:(function(){
/* @title: Compare Bitbucket PR Commits */
/* @description: Opens a comparison view between commits in a Bitbucket pull request */
console.log('Bitbucket Compare Helper: Starting...');
/* Check if we're on a Bitbucket commits page */
if (!document.querySelector('.commits-table')) {
alert('This bookmarklet only works on Bitbucket commit pages');
return;
}
let selectedCommits = [];
const maxSelections = 2;
/* Extract project and repo from URL */
const urlParts = window.location.pathname.split('/');
const projectIndex = urlParts.indexOf('projects');
const repoIndex = urlParts.indexOf('repos');
if (projectIndex === -1 || repoIndex === -1) {
alert('Could not determine project and repository from URL');
return;
}
const project = urlParts[projectIndex + 1];
const repo = urlParts[repoIndex + 1];
console.log(`Project: ${project}, Repo: ${repo}`);
/* Add selection UI styles */
const style = document.createElement('style');
style.textContent = `
.commit-selector {
cursor: pointer;
padding: 4px 8px;
margin: 2px;
border: 2px solid #ccc;
border-radius: 4px;
background: #f5f5f5;
font-size: 11px;
display: inline-block;
}
.commit-selector:hover {
background: #e0e0e0;
border-color: #999;
}
.commit-selector.selected {
background: #007acc;
color: white;
border-color: #005a99;
}
.compare-toolbar {
position: fixed;
top: 10px;
right: 10px;
background: white;
border: 2px solid #007acc;
border-radius: 8px;
padding: 15px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
z-index: 9999;
font-family: Arial, sans-serif;
}
.compare-button {
background: #007acc;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin: 5px;
}
.compare-button:hover {
background: #005a99;
}
.compare-button:disabled {
background: #ccc;
cursor: not-allowed;
}
.selection-info {
font-size: 12px;
margin-bottom: 10px;
}
`;
document.head.appendChild(style);
/* Create the compare toolbar */
const toolbar = document.createElement('div');
toolbar.className = 'compare-toolbar';
toolbar.innerHTML = `
<div class="selection-info">
<strong>Bitbucket Compare Helper</strong><br>
Select 2 commits to compare<br>
<span id="selection-count">Selected: 0/2</span>
</div>
<button class="compare-button" id="compare-btn" disabled>Compare Commits</button>
<button class="compare-button" id="clear-btn">Clear Selection</button>
<button class="compare-button" id="close-btn">Close</button>
`;
document.body.appendChild(toolbar);
/* Function to extract commit hash from row */
function getCommitHash(row) {
const commitLink = row.querySelector('.commit-hash a');
if (!commitLink) return null;
const href = commitLink.getAttribute('href');
const hashMatch = href.match(/commits\/([a-f0-9]+)/);
return hashMatch ? hashMatch[1] : null;
}
/* Function to update selection display */
function updateSelectionDisplay() {
document.getElementById('selection-count').textContent = `Selected: ${selectedCommits.length}/2`;
document.getElementById('compare-btn').disabled = selectedCommits.length !== 2;
console.log('Current selection:', selectedCommits);
}
/* Function to toggle commit selection */
function toggleCommitSelection(row, hash) {
const selector = row.querySelector('.commit-selector');
const existingIndex = selectedCommits.findIndex(c => c.hash === hash);
if (existingIndex !== -1) {
/* Deselect */
selectedCommits.splice(existingIndex, 1);
selector.classList.remove('selected');
selector.textContent = 'Select';
console.log(`Deselected commit: ${hash}`);
} else if (selectedCommits.length < maxSelections) {
/* Select */
selectedCommits.push({
hash: hash,
row: row
});
selector.classList.add('selected');
selector.textContent = `#${selectedCommits.length}`;
console.log(`Selected commit: ${hash}`);
} else {
alert('Maximum 2 commits can be selected. Clear selection first.');
return;
}
updateSelectionDisplay();
}
/* Add selectors to each commit row */
const commitRows = document.querySelectorAll('.commits-table tbody tr');
console.log(`Found ${commitRows.length} commit rows`);
commitRows.forEach((row, index) => {
const hash = getCommitHash(row);
if (!hash) {
console.log(`Could not extract hash for row ${index}`);
return;
}
console.log(`Adding selector for commit: ${hash}`);
const commitCell = row.querySelector('.commit');
if (commitCell) {
const selector = document.createElement('div');
selector.className = 'commit-selector';
selector.textContent = 'Select';
selector.title = `Select commit ${hash} for comparison`;
selector.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
toggleCommitSelection(row, hash);
});
commitCell.appendChild(selector);
}
});
/* Compare button handler */
document.getElementById('compare-btn').addEventListener('click', function() {
if (selectedCommits.length !== 2) {
alert('Please select exactly 2 commits');
return;
}
const baseUrl = window.location.origin;
const sourceBranch = selectedCommits[1].hash; /* Second selected (newer) */
const targetBranch = selectedCommits[0].hash; /* First selected (older) */
const compareUrl = `${baseUrl}/projects/${project}/repos/${repo}/compare/diff?sourceBranch=${sourceBranch}&targetBranch=${targetBranch}`;
console.log('Opening compare URL:', compareUrl);
window.open(compareUrl, '_blank');
});
/* Clear button handler */
document.getElementById('clear-btn').addEventListener('click', function() {
selectedCommits.forEach(commit => {
const selector = commit.row.querySelector('.commit-selector');
selector.classList.remove('selected');
selector.textContent = 'Select';
});
selectedCommits = [];
updateSelectionDisplay();
console.log('Selection cleared');
});
/* Close button handler */
document.getElementById('close-btn').addEventListener('click', function() {
toolbar.remove();
style.remove();
/* Remove all selectors */
document.querySelectorAll('.commit-selector').forEach(selector => {
selector.remove();
});
console.log('Bitbucket Compare Helper closed');
});
console.log('Bitbucket Compare Helper: Ready!');
})();