Skip to content

Commit de2027c

Browse files
Fixes further use of recompose on limited commits and adds count to button
1 parent e0cb891 commit de2027c

File tree

3 files changed

+70
-33
lines changed

3 files changed

+70
-33
lines changed

src/webviews/apps/plus/composer/components/app.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,14 +1381,26 @@ export class ComposerApp extends LitElement {
13811381
// Reset feedback state and create new session ID for new composition
13821382
this.compositionFeedback = null;
13831383
this.compositionSessionId = `composer-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
1384-
let selectedCommitIdsForGenerate = !this.state.hasUsedAutoCompose ? [...this.selectedCommitIds] : [];
1384+
const hasLockedCommits = this.state.commits.some(c => c.locked);
1385+
let selectedCommitIdsForGenerate: string[] = [];
1386+
if (hasLockedCommits) {
1387+
selectedCommitIdsForGenerate = this.state.commits.filter(c => !c.locked).map(c => c.id);
1388+
}
1389+
1390+
if (!selectedCommitIdsForGenerate.length) {
1391+
selectedCommitIdsForGenerate =
1392+
this.state.recompose?.enabled && !this.state.hasUsedAutoCompose && !hasLockedCommits
1393+
? [...this.selectedCommitIds]
1394+
: [];
1395+
}
1396+
13851397
if (selectedCommitIdsForGenerate.length === 1) {
13861398
selectedCommitIdsForGenerate = [];
13871399
}
13881400

13891401
if (!selectedCommitIdsForGenerate.length && this.state.recompose?.commitShas?.length) {
13901402
const recomposeCommits = this.state.commits.filter(
1391-
c => c.sha && this.state.recompose!.commitShas!.includes(c.sha),
1403+
c => c.sha && !c.locked && this.state.recompose!.commitShas!.includes(c.sha),
13921404
);
13931405
selectedCommitIdsForGenerate = recomposeCommits.map(c => c.id);
13941406
}
@@ -1627,6 +1639,8 @@ export class ComposerApp extends LitElement {
16271639
// Get hunks with updated assignments
16281640
const hunks = this.hunksWithAssignments;
16291641

1642+
const hasLockedCommits = this.state.commits.some(c => c.locked);
1643+
16301644
return html`
16311645
<header class="header">
16321646
<div class="header__group">
@@ -1671,6 +1685,7 @@ export class ComposerApp extends LitElement {
16711685
.canMoveHunks=${this.canMoveHunks}
16721686
.canGenerateCommitsWithAI=${this.canGenerateCommitsWithAI}
16731687
.canReorderCommits=${this.canReorderCommits}
1688+
.hasLockedCommits=${hasLockedCommits}
16741689
.isPreviewMode=${this.isPreviewMode}
16751690
.baseCommit=${this.state.baseCommit}
16761691
.repoName=${this.state.baseCommit?.repoName ?? this.state.repositoryState?.current.name ?? null}

src/webviews/apps/plus/composer/components/commits-panel.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ export class CommitsPanel extends LitElement {
389389
@property({ type: Boolean })
390390
hasChanges: boolean = true;
391391

392+
@property({ type: Boolean })
393+
hasLockedCommits: boolean = false;
394+
392395
@property({ type: Object })
393396
aiModel?: AIModel = undefined;
394397

@@ -1094,6 +1097,11 @@ export class CommitsPanel extends LitElement {
10941097
}
10951098

10961099
private renderAutoComposeContainer(disabled = false) {
1100+
const recomposeCount = this.hasLockedCommits
1101+
? this.commits.filter(c => !c.locked).length
1102+
: this.recompose?.enabled && this.selectedCommitIds.size > 1
1103+
? this.selectedCommitIds.size
1104+
: null;
10971105
return html`
10981106
<div class="auto-compose${this.hasUsedAutoCompose ? ' is-used' : ''}">
10991107
${when(
@@ -1184,9 +1192,10 @@ export class CommitsPanel extends LitElement {
11841192
></code-icon>
11851193
${this.generating
11861194
? 'Generating Commits...'
1187-
: this.hasUsedAutoCompose || this.isRecomposeLocked
1188-
? this.selectedCommitIds.size > 1
1189-
? 'Recompose Selected Commits'
1195+
: this.hasUsedAutoCompose || this.recompose?.enabled
1196+
? recomposeCount
1197+
? html`Recompose ${recomposeCount}
1198+
${recomposeCount === 1 ? 'Commit' : 'Commits'}`
11901199
: 'Recompose Commits'
11911200
: 'Auto-Compose Commits'}
11921201
</gl-button>

src/webviews/plus/composer/composerWebview.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,36 +1023,49 @@ export class ComposerWebviewProvider implements WebviewProvider<State, State, Co
10231023
params.commitsToReplace.commits[params.commitsToReplace.commits.length - 1].sha ??
10241024
this._safetyState.headSha!;
10251025
}
1026-
const combinedDiff = await calculateCombinedDiffBetweenCommits(
1027-
this._currentRepository!,
1028-
baseSha,
1029-
headSha,
1030-
);
1031-
1032-
const combinedHunks = createHunksFromDiffs(combinedDiff!.contents);
1033-
for (const hunk of combinedHunks) {
1034-
const { author, coAuthors } = getAuthorAndCoAuthorsForCombinedDiffHunk(this._hunks, hunk);
1035-
hunk.author = author;
1036-
hunk.coAuthors = coAuthors.length ? coAuthors : undefined;
1037-
hunks.push({ ...hunk, assigned: true });
1038-
}
10391026

1040-
// Update the hunks. Note that if params.commitsToReplace is defined, then we need to remove all the hunks with indices that match the hunkIndices of the commits to replace, then add in the new hunks and
1041-
// reinder all of the hunks. Otherwise, we just replace the existing hunks with the new ones
1042-
if (params.commitsToReplace) {
1043-
const hunkIndicesToRemove = new Set(params.commitsToReplace.commits.flatMap(c => c.hunkIndices));
1044-
this._hunks = this._hunks.filter(h => !hunkIndicesToRemove.has(h.index));
1045-
// Reindex the hunks
1046-
let newIndexCounter = 1;
1047-
this._hunks.forEach(hunk => {
1048-
hunk.index = newIndexCounter++;
1049-
});
1050-
hunks.forEach(hunk => {
1051-
hunk.index = newIndexCounter++;
1052-
});
1053-
this._hunks.push(...hunks);
1027+
const shouldSkipDiffCalculation =
1028+
params.commitsToReplace?.commits?.length && params.commitsToReplace.commits.every(c => !c.sha);
1029+
if (shouldSkipDiffCalculation) {
1030+
// just set hunks to the existing hunks and move on
1031+
const hunkIndices = new Set(params.commitsToReplace!.commits.flatMap(c => c.hunkIndices));
1032+
for (const index of hunkIndices) {
1033+
hunks.push({ ...this._hunks.find(m => m.index === index)!, assigned: true });
1034+
}
10541035
} else {
1055-
this._hunks = hunks;
1036+
const combinedDiff = await calculateCombinedDiffBetweenCommits(
1037+
this._currentRepository!,
1038+
baseSha,
1039+
headSha,
1040+
);
1041+
1042+
const combinedHunks = createHunksFromDiffs(combinedDiff!.contents);
1043+
for (const hunk of combinedHunks) {
1044+
const { author, coAuthors } = getAuthorAndCoAuthorsForCombinedDiffHunk(this._hunks, hunk);
1045+
hunk.author = author;
1046+
hunk.coAuthors = coAuthors.length ? coAuthors : undefined;
1047+
hunks.push({ ...hunk, assigned: true });
1048+
}
1049+
1050+
// Update the hunks. Note that if params.commitsToReplace is defined, then we need to remove all the hunks with indices that match the hunkIndices of the commits to replace, then add in the new hunks and
1051+
// reinder all of the hunks. Otherwise, we just replace the existing hunks with the new ones
1052+
if (params.commitsToReplace) {
1053+
const hunkIndicesToRemove = new Set(
1054+
params.commitsToReplace.commits.flatMap(c => c.hunkIndices),
1055+
);
1056+
this._hunks = this._hunks.filter(h => !hunkIndicesToRemove.has(h.index));
1057+
// Reindex the hunks
1058+
let newIndexCounter = 1;
1059+
this._hunks.forEach(hunk => {
1060+
hunk.index = newIndexCounter++;
1061+
});
1062+
hunks.forEach(hunk => {
1063+
hunk.index = newIndexCounter++;
1064+
});
1065+
this._hunks.push(...hunks);
1066+
} else {
1067+
this._hunks = hunks;
1068+
}
10561069
}
10571070
} else {
10581071
// Working directory mode: use existing hunks

0 commit comments

Comments
 (0)