Skip to content

Commit df99cc2

Browse files
committed
Triggers an AI-powered rebase over target branch
(#4443, #4522)
1 parent b464bb0 commit df99cc2

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/env/node/git/sub-providers/graph.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ export class GraphGitSubProvider implements GitGraphSubProvider {
348348
branch = branchMap.get(tip);
349349
branchId = branch?.id ?? getBranchId(repoPath, false, tip);
350350

351-
// Check if branch has commits that can be recomposed
352-
const recomposable = await this.isBranchRecomposable(branch, repoPath);
351+
// Check if branch has commits that can be recomposed and get merge base
352+
const mergeBaseCommit = await this.getMergeBaseCommit(branch, repoPath);
353353

354354
context = {
355355
webviewItem: `gitlens:branch${head ? '+current' : ''}${
@@ -362,7 +362,7 @@ export class GraphGitSubProvider implements GitGraphSubProvider {
362362
: ''
363363
}${branch?.starred ? '+starred' : ''}${branch?.upstream?.state.ahead ? '+ahead' : ''}${
364364
branch?.upstream?.state.behind ? '+behind' : ''
365-
}${recomposable ? '+recomposable' : ''}`,
365+
}${mergeBaseCommit ? '+recomposable' : ''}`,
366366
webviewItemValue: {
367367
type: 'branch',
368368
ref: createReference(tip, repoPath, {
@@ -372,6 +372,7 @@ export class GraphGitSubProvider implements GitGraphSubProvider {
372372
remote: false,
373373
upstream: branch?.upstream,
374374
}),
375+
mergeBaseCommit: mergeBaseCommit,
375376
},
376377
};
377378

@@ -621,8 +622,8 @@ export class GraphGitSubProvider implements GitGraphSubProvider {
621622
return getCommitsForGraphCore.call(this, defaultLimit, selectSha, undefined, cancellation);
622623
}
623624

624-
private async isBranchRecomposable(branch: GitBranch | undefined, repoPath: string): Promise<boolean> {
625-
if (!branch || branch.remote) return false;
625+
private async getMergeBaseCommit(branch: GitBranch | undefined, repoPath: string): Promise<string | undefined> {
626+
if (!branch || branch.remote) return undefined;
626627

627628
try {
628629
const upstreamName = branch.upstream?.name;
@@ -643,10 +644,11 @@ export class GraphGitSubProvider implements GitGraphSubProvider {
643644
const validTargets = [validStoredTarget, validStoredMergeBase];
644645
const targetCommit = await this.selectMostRecentMergeBase(branch.name, validTargets, svc);
645646

646-
return Boolean(targetCommit && targetCommit !== branch.sha);
647+
const isRecomposable = Boolean(targetCommit && targetCommit !== branch.sha);
648+
return isRecomposable ? targetCommit : undefined;
647649
} catch {
648650
// If we can't determine, assume not recomposable
649-
return false;
651+
return undefined;
650652
}
651653
}
652654

src/webviews/plus/graph/graphWebview.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,6 +3247,18 @@ export class GraphWebviewProvider implements WebviewProvider<State, State, Graph
32473247
@log()
32483248
private aiRebaseBranch(item?: GraphItemContext) {
32493249
if (isGraphItemRefContext(item, 'branch')) {
3250+
const { ref, mergeBaseCommit } = item.webviewItemValue;
3251+
3252+
if (!mergeBaseCommit) {
3253+
return Promise.resolve();
3254+
}
3255+
3256+
return executeCommand<GenerateRebaseCommandArgs>('gitlens.ai.generateRebase', {
3257+
repoPath: ref.repoPath,
3258+
head: ref,
3259+
base: createReference(mergeBaseCommit, ref.repoPath, { refType: 'revision' }),
3260+
source: { source: 'graph' },
3261+
});
32503262
}
32513263

32523264
return Promise.resolve();

src/webviews/plus/graph/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ export interface GraphIssueContextValue {
567567
export interface GraphBranchContextValue {
568568
type: 'branch';
569569
ref: GitBranchReference;
570+
mergeBaseCommit?: string;
570571
}
571572

572573
export interface GraphCommitContextValue {

0 commit comments

Comments
 (0)