@ -2,12 +2,11 @@ import type { TextEditor } from 'vscode';
import { window , workspace } from 'vscode' ;
import { Commands } from '../constants' ;
import type { Container } from '../container' ;
import type { PatchRevisionRange } from '../git/models/patch' ;
import { shortenRevision } from '../git/models/reference' ;
import { isSha , shortenRevision } from '../git/models/reference' ;
import type { Repository } from '../git/models/repository' ;
import type { Draft , LocalDraft } from '../gk/models/drafts' ;
import { showPatchesView } from '../plus/drafts/actions' ;
import type { Change } from '../plus/webviews/patchDetails/protocol' ;
import type { Change , CreateDraft } from '../plus/webviews/patchDetails/protocol' ;
import { getRepositoryOrShowPicker } from '../quickpicks/repositoryPicker' ;
import { command } from '../system/command' ;
import type { CommandContext } from './base' ;
@ -19,8 +18,8 @@ import {
} from './base' ;
export interface CreatePatchCommandArgs {
ref1 ? : string ;
ref2? : string ;
to ? : string ;
from ? : string ;
repoPath? : string ;
}
@ -36,13 +35,13 @@ export class CreatePatchCommand extends Command {
if ( isCommandContextViewNodeHasCommit ( context ) ) {
args = {
repoPath : context.node.commit.repoPath ,
ref1 : context.node.commit.ref ,
to : context.node.commit.ref ,
} ;
} else if ( isCommandContextViewNodeHasComparison ( context ) ) {
args = {
repoPath : context.node.uri.fsPath ,
ref1 : context.node.compareWith Ref.ref,
ref2 : context.node.compareRef. ref,
to : context.node.compare Ref.ref,
from : context . node . compareWithRef . ref,
} ;
}
}
@ -59,22 +58,11 @@ export class CreatePatchCommand extends Command {
repo = this . container . git . getRepository ( args . repoPath ) ;
}
if ( repo == null ) return undefined ;
if ( args ? . ref1 == null ) return ;
if ( args ? . to == null ) return ;
const diff = await getDiffContents ( this . container , repo , args ) ;
const diff = await this . container . git . getDiff ( repo . uri , args . to ? ? 'HEAD' , args . from ) ;
if ( diff == null ) return ;
// let repo;
// if (args?.repoPath == null) {
// repo = await getRepositoryOrShowPicker('Create Patch');
// } else {
// repo = this.container.git.getRepository(args.repoPath);
// }
// if (repo == null) return;
// const diff = await this.container.git.getDiff(repo.uri, args?.ref1 ?? 'HEAD', args?.ref2);
// if (diff == null) return;
const d = await workspace . openTextDocument ( { content : diff.contents , language : 'diff' } ) ;
await window . showTextDocument ( d ) ;
@ -88,101 +76,6 @@ export class CreatePatchCommand extends Command {
}
}
async function getDiffContents (
container : Container ,
repository : Repository ,
args : CreatePatchCommandArgs ,
) : Promise < { contents : string ; revision : PatchRevisionRange } | undefined > {
const sha = args . ref1 ? ? 'HEAD' ;
const diff = await container . git . getDiff ( repository . uri , sha , args . ref2 ) ;
if ( diff == null ) return undefined ;
return {
contents : diff.contents ,
revision : {
baseSha : args.ref2 ? ? ` ${ sha } ^ ` ,
sha : sha ,
} ,
} ;
}
interface CreateLocalChange {
title? : string ;
description? : string ;
changes : Change [ ] ;
}
async function createLocalChange (
container : Container ,
repository : Repository ,
args : CreatePatchCommandArgs ,
) : Promise < CreateLocalChange | undefined > {
if ( args . ref1 == null ) return undefined ;
const sha = args . ref1 ? ? 'HEAD' ;
// const [branchName] = await container.git.getCommitBranches(repository.uri, sha);
const change : Change = {
type : 'revision' ,
repository : {
name : repository.name ,
path : repository.path ,
uri : repository.uri.toString ( ) ,
} ,
files : undefined ! ,
revision : {
sha : sha ,
baseSha : args.ref2 ? ? ` ${ sha } ^ ` ,
// branchName: branchName ?? 'HEAD',
} ,
} ;
const create : CreateLocalChange = { changes : [ change ] } ;
const commit = await container . git . getCommit ( repository . uri , sha ) ;
if ( commit == null ) return undefined ;
const message = commit . message ! . trim ( ) ;
const index = message . indexOf ( '\n' ) ;
if ( index < 0 ) {
create . title = message ;
} else {
create . title = message . substring ( 0 , index ) ;
create . description = message . substring ( index + 1 ) ;
}
if ( args . ref2 == null ) {
change . files = commit . files != null ? [ . . . commit . files ] : [ ] ;
} else {
const diff = await getDiffContents ( container , repository , args ) ;
if ( diff == null ) return undefined ;
const result = await container . git . getDiffFiles ( repository . uri , diff . contents ) ;
if ( result ? . files == null ) return ;
create . title = ` Comparing ${ shortenRevision ( args . ref2 ) } with ${ shortenRevision ( args . ref1 ) } ` ;
change . files = result . files ;
}
// const change: Change = {
// type: 'commit',
// repository: {
// name: repository.name,
// path: repository.path,
// uri: repository.uri.toString(),
// },
// files: result.files,
// range: {
// ...range,
// branchName: branchName ?? 'HEAD',
// },
// };
return create ;
}
@command ( )
export class CreateCloudPatchCommand extends Command {
constructor ( private readonly container : Container ) {
@ -195,13 +88,13 @@ export class CreateCloudPatchCommand extends Command {
if ( isCommandContextViewNodeHasCommit ( context ) ) {
args = {
repoPath : context.node.commit.repoPath ,
ref1 : context.node.commit.ref ,
to : context.node.commit.ref ,
} ;
} else if ( isCommandContextViewNodeHasComparison ( context ) ) {
args = {
repoPath : context.node.uri.fsPath ,
ref1 : context.node.compareWith Ref.ref,
ref2 : context.node.compareRef. ref,
to : context.node.compare Ref.ref,
from : context . node . compareWithRef . ref,
} ;
}
}
@ -220,7 +113,7 @@ export class CreateCloudPatchCommand extends Command {
return showPatchesView ( { mode : 'create' } ) ;
}
const create = await createLocalChange ( this . container , repo , args ) ;
const create = await createDraft ( this . container , repo , args ) ;
if ( create == null ) {
return showPatchesView ( { mode : 'create' , create : { repositories : [ repo ] } } ) ;
}
@ -366,60 +259,83 @@ export class OpenCloudPatchCommand extends Command {
async execute ( args? : OpenCloudPatchCommandArgs ) {
if ( args ? . id == null && args ? . draft == null ) {
void window . showErrorMessage ( 'Cannot open cloud patch: no patch or patch id provided' ) ;
void window . showErrorMessage ( 'Cannot open Cloud Patch; no patch or patch id provided' ) ;
return ;
}
const draft = args ? . draft ? ? ( await this . container . drafts . getDraft ( args ? . id ) ) ;
if ( draft == null ) {
void window . showErrorMessage ( ` Cannot open cloud patch: patch ${ args . id } not found ` ) ;
void window . showErrorMessage ( ` Cannot open Cloud Patch; patch ' ${ args . id } ' not found` ) ;
return ;
}
// let patch: DraftPatch | undefined;
// if (args?.patchId) {
// patch = await this.container.drafts.getPatch(args.patchId);
// } else {
// const patches = draft.changesets?.[0]?.patches;
// if (patches == null || patches.length === 0) {
// void window.showErrorMessage(`Cannot open cloud patch: no patch found under id ${args.patchId}`);
// return;
// }
// patch = patches[0];
// if (patch.repo == null && patch.repoData != null) {
// const repo = await this.container.git.findMatchingRepository({
// firstSha: patch.repoData.initialCommitSha,
// remoteUrl: patch.repoData.remote?.url,
// });
// if (repo != null) {
// patch.repo = repo;
// }
// }
void showPatchesView ( { mode : 'view' , draft : draft } ) ;
}
}
// if (patch.repo == null) {
// void window.showErrorMessage(`Cannot open cloud patch: no repository found for patch ${args.patchId}`);
// return;
// }
async function createDraft (
container : Container ,
repository : Repository ,
args : CreatePatchCommandArgs ,
) : Promise < CreateDraft | undefined > {
if ( args . to == null ) return undefined ;
// // Opens the patch repository if it's not already open
// void this.container.git.getOrOpenRepository(patch.repo.uri);
const to = args . to ? ? 'HEAD' ;
// const patchContents = await this.container.drafts.getPatchContents(patch.id);
// if (patchContents == null) {
// void window.showErrorMessage(`Cannot open cloud patch: patch not found of contents empty`);
// return;
// }
// patch.contents = patchContents;
// }
const change : Change = {
type : 'revision' ,
repository : {
name : repository.name ,
path : repository.path ,
uri : repository.uri.toString ( ) ,
} ,
files : undefined ! ,
revision : { to : to , from : args . from ? ? ` ${ to } ^ ` } ,
} ;
// if (patch == null) {
// void window.showErrorMessage(`Cannot open cloud patch: patch not found`);
// return;
// }
const create : CreateDraft = { changes : [ change ] } ;
void showPatchesView ( { mode : 'view' , draft : draft } ) ;
const commit = await container . git . getCommit ( repository . uri , to ) ;
if ( commit == null ) return undefined ;
const message = commit . message ! . trim ( ) ;
const index = message . indexOf ( '\n' ) ;
if ( index < 0 ) {
create . title = message ;
} else {
create . title = message . substring ( 0 , index ) ;
create . description = message . substring ( index + 1 ) ;
}
if ( args . from == null ) {
if ( commit . files == null ) return ;
change . files = [ . . . commit . files ] ;
} else {
const diff = await container . git . getDiff ( repository . uri , to , args . from ) ;
if ( diff == null ) return ;
const result = await container . git . getDiffFiles ( repository . uri , diff . contents ) ;
if ( result ? . files == null ) return ;
change . files = result . files ;
create . title = ` Comparing ${ shortenRevision ( args . to ) } with ${ shortenRevision ( args . from ) } ` ;
if ( ! isSha ( args . to ) ) {
const commit = await container . git . getCommit ( repository . uri , args . to ) ;
if ( commit != null ) {
change . revision . to = commit . sha ;
}
}
if ( ! isSha ( args . from ) ) {
const commit = await container . git . getCommit ( repository . uri , args . from ) ;
if ( commit != null ) {
change . revision . from = commit . sha ;
}
}
}
return create ;
}