|
|
@ -1,5 +1,5 @@ |
|
|
|
import type { TextDocumentShowOptions } from 'vscode'; |
|
|
|
import { env, Range, Uri, window } from 'vscode'; |
|
|
|
import { env, Range, Uri, window, workspace } from 'vscode'; |
|
|
|
import type { DiffWithCommandArgs } from '../../commands/diffWith'; |
|
|
|
import type { DiffWithPreviousCommandArgs } from '../../commands/diffWithPrevious'; |
|
|
|
import type { DiffWithWorkingCommandArgs } from '../../commands/diffWithWorking'; |
|
|
@ -22,31 +22,39 @@ import type { GitFile } from '../models/file'; |
|
|
|
import type { GitRevisionReference } from '../models/reference'; |
|
|
|
import { getReferenceFromRevision, isUncommitted, isUncommittedStaged } from '../models/reference'; |
|
|
|
|
|
|
|
export async function applyChanges(file: string | GitFile, ref1: GitRevisionReference, ref2?: GitRevisionReference) { |
|
|
|
export async function applyChanges(file: string | GitFile, rev1: GitRevisionReference, rev2?: GitRevisionReference) { |
|
|
|
let create = false; |
|
|
|
let ref = ref1.ref; |
|
|
|
let ref1 = rev1.ref; |
|
|
|
let ref2 = rev2?.ref; |
|
|
|
if (typeof file !== 'string') { |
|
|
|
// If the file is `?` (untracked), then this must be a stash, so get the ^3 commit to access the untracked file
|
|
|
|
if (file.status === '?') { |
|
|
|
ref = `${ref}^3`; |
|
|
|
ref1 = `${ref1}^3`; |
|
|
|
create = true; |
|
|
|
} else if (file.status === 'A') { |
|
|
|
create = true; |
|
|
|
} else if (file.status === 'D') { |
|
|
|
// If the file is deleted, check to see if it exists, if so, apply the delete, otherwise restore it from the previous commit
|
|
|
|
const uri = GitUri.fromFile(file, rev1.repoPath); |
|
|
|
try { |
|
|
|
await workspace.fs.stat(uri); |
|
|
|
} catch { |
|
|
|
create = true; |
|
|
|
|
|
|
|
ref2 = ref1; |
|
|
|
ref1 = `${ref1}^`; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (create) { |
|
|
|
const uri = GitUri.fromFile(file, ref1.repoPath); |
|
|
|
await Container.instance.git.applyChangesToWorkingFile(uri, ref, ref2?.ref); |
|
|
|
const uri = GitUri.fromFile(file, rev1.repoPath); |
|
|
|
await Container.instance.git.applyChangesToWorkingFile(uri, ref1, ref2); |
|
|
|
await openFile(uri, { preserveFocus: true, preview: false }); |
|
|
|
} else { |
|
|
|
// Open the working file to ensure undo will work
|
|
|
|
await openFile(file, ref1, { preserveFocus: true, preview: false }); |
|
|
|
await Container.instance.git.applyChangesToWorkingFile( |
|
|
|
GitUri.fromFile(file, ref1.repoPath, ref), |
|
|
|
ref, |
|
|
|
ref2?.ref, |
|
|
|
); |
|
|
|
await openFile(file, rev1, { preserveFocus: true, preview: false }); |
|
|
|
await Container.instance.git.applyChangesToWorkingFile(GitUri.fromFile(file, rev1.repoPath, ref1), ref1, ref2); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -550,7 +558,20 @@ export async function restoreFile(file: string | GitFile, revision: GitRevisionR |
|
|
|
ref = revision.ref; |
|
|
|
} else { |
|
|
|
path = file.path; |
|
|
|
ref = file.status === `?` ? `${revision.ref}^3` : file.status === 'D' ? `${revision.ref}^` : revision.ref; |
|
|
|
if (file.status === 'D') { |
|
|
|
// If the file is deleted, check to see if it exists, if so, restore it from the previous commit, otherwise restore it from the current commit
|
|
|
|
const uri = GitUri.fromFile(file, revision.repoPath); |
|
|
|
try { |
|
|
|
await workspace.fs.stat(uri); |
|
|
|
ref = `${revision.ref}^`; |
|
|
|
} catch { |
|
|
|
ref = revision.ref; |
|
|
|
} |
|
|
|
} else if (file.status === '?') { |
|
|
|
ref = `${revision.ref}^3`; |
|
|
|
} else { |
|
|
|
ref = revision.ref; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
await Container.instance.git.checkout(revision.repoPath, ref, { path: path }); |
|
|
|