|
| 1 | +import type { Container } from '../../../container'; |
| 2 | +import type { GitCommit } from '../../models/commit'; |
| 3 | +import { GitFileChange } from '../../models/fileChange'; |
| 4 | +import { uncommitted, uncommittedStaged } from '../../models/revision'; |
| 5 | +import type { GitStatusFile } from '../../models/statusFile'; |
| 6 | +import type { GitUser } from '../../models/user'; |
| 7 | +import { createUncommittedChangesCommit } from './commit.utils'; |
| 8 | + |
| 9 | +export function getPseudoCommits( |
| 10 | + container: Container, |
| 11 | + files: GitStatusFile[] | undefined, |
| 12 | + user: GitUser | undefined, |
| 13 | +): GitCommit[] { |
| 14 | + if (!files?.length) return []; |
| 15 | + |
| 16 | + let now = new Date(); |
| 17 | + const repoPath = files[0].repoPath; |
| 18 | + |
| 19 | + let conflicted: GitFileChange[] | undefined; |
| 20 | + let staged: GitFileChange[] | undefined; |
| 21 | + let wip: GitFileChange[] | undefined; |
| 22 | + |
| 23 | + for (const file of files) { |
| 24 | + if (file.conflicted) { |
| 25 | + conflicted ??= []; |
| 26 | + conflicted.push( |
| 27 | + new GitFileChange( |
| 28 | + container, |
| 29 | + repoPath, |
| 30 | + file.path, |
| 31 | + file.status, |
| 32 | + file.originalPath, |
| 33 | + 'HEAD', |
| 34 | + undefined, |
| 35 | + false, |
| 36 | + ), |
| 37 | + ); |
| 38 | + } else { |
| 39 | + if (file.wip) { |
| 40 | + wip ??= []; |
| 41 | + wip.push( |
| 42 | + new GitFileChange( |
| 43 | + container, |
| 44 | + repoPath, |
| 45 | + file.path, |
| 46 | + file.workingTreeStatus ?? file.status, |
| 47 | + file.originalPath, |
| 48 | + file.staged ? uncommittedStaged : 'HEAD', |
| 49 | + undefined, |
| 50 | + false, |
| 51 | + ), |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + if (file.staged) { |
| 56 | + staged ??= []; |
| 57 | + staged.push( |
| 58 | + new GitFileChange( |
| 59 | + container, |
| 60 | + repoPath, |
| 61 | + file.path, |
| 62 | + file.indexStatus ?? file.status, |
| 63 | + file.originalPath, |
| 64 | + 'HEAD', |
| 65 | + undefined, |
| 66 | + true, |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + const commits: GitCommit[] = []; |
| 74 | + |
| 75 | + if (conflicted?.length || wip?.length) { |
| 76 | + const conflictedAndWipFiles = [...(conflicted ?? []), ...(wip ?? [])]; |
| 77 | + commits.push( |
| 78 | + createUncommittedChangesCommit(container, repoPath, uncommitted, now, user, { |
| 79 | + files: { |
| 80 | + file: conflictedAndWipFiles.length === 1 ? conflictedAndWipFiles[0] : undefined, |
| 81 | + files: conflictedAndWipFiles, |
| 82 | + }, |
| 83 | + parents: [staged?.length ? uncommittedStaged : 'HEAD'], |
| 84 | + }), |
| 85 | + ); |
| 86 | + |
| 87 | + // Decrements the date to guarantee the staged entry (if exists) will be sorted before the working entry (most recent first) |
| 88 | + now = new Date(now.getTime() - 60000); |
| 89 | + } |
| 90 | + |
| 91 | + if (staged?.length) { |
| 92 | + commits.push( |
| 93 | + createUncommittedChangesCommit(container, repoPath, uncommittedStaged, now, user, { |
| 94 | + files: { file: staged.length === 1 ? staged[0] : undefined, files: staged }, |
| 95 | + parents: ['HEAD'], |
| 96 | + }), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + return commits; |
| 101 | +} |
| 102 | + |
| 103 | +export async function getPseudoCommitsWithStats( |
| 104 | + container: Container, |
| 105 | + files: GitStatusFile[] | undefined, |
| 106 | + user: GitUser | undefined, |
| 107 | +): Promise<GitCommit[]> { |
| 108 | + const pseudoCommits = getPseudoCommits(container, files, user); |
| 109 | + if (!pseudoCommits.length) return pseudoCommits; |
| 110 | + |
| 111 | + const diffProvider = container.git.diff(pseudoCommits[0].repoPath); |
| 112 | + |
| 113 | + const commits: GitCommit[] = []; |
| 114 | + |
| 115 | + for (const commit of pseudoCommits) { |
| 116 | + commits.push( |
| 117 | + commit.with({ |
| 118 | + stats: await diffProvider.getChangedFilesCount(commit.sha, 'HEAD', { |
| 119 | + uris: commit.files?.map(f => f.uri), |
| 120 | + }), |
| 121 | + }), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + return commits; |
| 126 | +} |
0 commit comments