git-chopstick-core 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/README.md +42 -10
  2. package/dist/git/apply.d.ts +1 -1
  3. package/dist/git/apply.js +1 -1
  4. package/dist/git/apply.js.map +1 -1
  5. package/dist/git/environment.d.ts +1 -1
  6. package/dist/git/environment.js +1 -1
  7. package/dist/git/environment.js.map +1 -1
  8. package/dist/git/exec.js.map +1 -1
  9. package/dist/git/gitignore.js +1 -1
  10. package/dist/git/gitignore.js.map +1 -1
  11. package/dist/git/index.d.ts +1 -0
  12. package/dist/git/index.js +3 -0
  13. package/dist/git/index.js.map +1 -1
  14. package/dist/lib/fatal-error.d.ts +1 -1
  15. package/dist/lib/fatal-error.js +1 -1
  16. package/dist/lib/fatal-error.js.map +1 -1
  17. package/dist/lib/progress/from-process.js.map +1 -1
  18. package/dist/lib/progress/index.d.ts +5 -5
  19. package/dist/lib/progress/index.js +5 -5
  20. package/dist/lib/progress/index.js.map +1 -1
  21. package/dist/lib/progress/revert.d.ts +1 -1
  22. package/dist/lib/progress/revert.js +1 -1
  23. package/dist/lib/progress/revert.js.map +1 -1
  24. package/dist/lib/status-parser.js +0 -12
  25. package/dist/lib/status-parser.js.map +1 -1
  26. package/dist/models/repository.d.ts +1 -1
  27. package/dist/models/repository.js +1 -1
  28. package/dist/models/repository.js.map +1 -1
  29. package/package.json +39 -9
  30. package/src/git/add.ts +0 -16
  31. package/src/git/apply.ts +0 -154
  32. package/src/git/authentication.ts +0 -20
  33. package/src/git/branch.ts +0 -206
  34. package/src/git/checkout-index.ts +0 -40
  35. package/src/git/checkout.ts +0 -235
  36. package/src/git/cherry-pick.ts +0 -504
  37. package/src/git/clean.ts +0 -9
  38. package/src/git/clone.ts +0 -86
  39. package/src/git/coerce-to-buffer.ts +0 -4
  40. package/src/git/coerce-to-string.ts +0 -4
  41. package/src/git/commit.ts +0 -136
  42. package/src/git/config.ts +0 -392
  43. package/src/git/core.ts +0 -625
  44. package/src/git/create-tail-stream.ts +0 -36
  45. package/src/git/credential.ts +0 -83
  46. package/src/git/description.ts +0 -33
  47. package/src/git/diff-check.ts +0 -27
  48. package/src/git/diff-index.ts +0 -116
  49. package/src/git/diff.ts +0 -880
  50. package/src/git/environment.ts +0 -116
  51. package/src/git/exec.ts +0 -285
  52. package/src/git/fetch.ts +0 -141
  53. package/src/git/for-each-ref.ts +0 -160
  54. package/src/git/format-patch.ts +0 -17
  55. package/src/git/git-delimiter-parser.ts +0 -95
  56. package/src/git/gitignore.ts +0 -157
  57. package/src/git/index.ts +0 -46
  58. package/src/git/init.ts +0 -11
  59. package/src/git/interpret-trailers.ts +0 -176
  60. package/src/git/lfs.ts +0 -100
  61. package/src/git/log.ts +0 -376
  62. package/src/git/merge-tree.ts +0 -42
  63. package/src/git/merge.ts +0 -154
  64. package/src/git/multi-operation-terminal-output.ts +0 -68
  65. package/src/git/pull.ts +0 -130
  66. package/src/git/push-terminal-chunk.ts +0 -41
  67. package/src/git/push.ts +0 -119
  68. package/src/git/rebase.ts +0 -627
  69. package/src/git/reflog.ts +0 -127
  70. package/src/git/refs.ts +0 -63
  71. package/src/git/remote.ts +0 -143
  72. package/src/git/reorder.ts +0 -153
  73. package/src/git/reset.ts +0 -101
  74. package/src/git/rev-list.ts +0 -201
  75. package/src/git/rev-parse.ts +0 -92
  76. package/src/git/revert.ts +0 -55
  77. package/src/git/rm.ts +0 -31
  78. package/src/git/show.ts +0 -88
  79. package/src/git/spawn.ts +0 -38
  80. package/src/git/squash.ts +0 -173
  81. package/src/git/stage.ts +0 -97
  82. package/src/git/stash.ts +0 -302
  83. package/src/git/status.ts +0 -502
  84. package/src/git/submodule.ts +0 -212
  85. package/src/git/tag.ts +0 -134
  86. package/src/git/update-index.ts +0 -169
  87. package/src/git/update-ref.ts +0 -50
  88. package/src/git/var.ts +0 -42
  89. package/src/git/worktree-include.ts +0 -146
  90. package/src/git/worktree.ts +0 -219
  91. package/src/index.ts +0 -11
  92. package/src/lib/api.ts +0 -7
  93. package/src/lib/diff-parser.ts +0 -249
  94. package/src/lib/directory-exists.ts +0 -10
  95. package/src/lib/errno-exception.ts +0 -12
  96. package/src/lib/fatal-error.ts +0 -23
  97. package/src/lib/feature-flag.ts +0 -29
  98. package/src/lib/file-system.ts +0 -7
  99. package/src/lib/get-old-path.ts +0 -11
  100. package/src/lib/git/environment.ts +0 -14
  101. package/src/lib/git-perf.ts +0 -3
  102. package/src/lib/helpers/default-branch.ts +0 -3
  103. package/src/lib/helpers/path.ts +0 -5
  104. package/src/lib/hooks/with-hooks-env.ts +0 -7
  105. package/src/lib/merge.ts +0 -3
  106. package/src/lib/noop.ts +0 -1
  107. package/src/lib/patch-formatter.ts +0 -18
  108. package/src/lib/path-exists.ts +0 -7
  109. package/src/lib/progress/from-process.ts +0 -10
  110. package/src/lib/progress/index.ts +0 -43
  111. package/src/lib/progress/revert.ts +0 -17
  112. package/src/lib/rebase.ts +0 -3
  113. package/src/lib/remove-remote-prefix.ts +0 -4
  114. package/src/lib/resolve-git-proxy.ts +0 -3
  115. package/src/lib/round.ts +0 -4
  116. package/src/lib/split-buffer.ts +0 -14
  117. package/src/lib/status-parser.ts +0 -188
  118. package/src/lib/stores/helpers/find-default-remote.ts +0 -3
  119. package/src/lib/trampoline/trampoline-environment.ts +0 -8
  120. package/src/models/branch.ts +0 -78
  121. package/src/models/cherry-pick.ts +0 -12
  122. package/src/models/clone-options.ts +0 -6
  123. package/src/models/commit-identity.ts +0 -35
  124. package/src/models/commit.ts +0 -44
  125. package/src/models/computed-action.ts +0 -6
  126. package/src/models/diff/diff-data.ts +0 -78
  127. package/src/models/diff/diff-line.ts +0 -36
  128. package/src/models/diff/diff-selection.ts +0 -165
  129. package/src/models/diff/image-diff.ts +0 -6
  130. package/src/models/diff/image.ts +0 -8
  131. package/src/models/diff/index.ts +0 -6
  132. package/src/models/diff/raw-diff.ts +0 -41
  133. package/src/models/git-author.ts +0 -16
  134. package/src/models/index.ts +0 -36
  135. package/src/models/manual-conflict-resolution.ts +0 -4
  136. package/src/models/merge.ts +0 -6
  137. package/src/models/multi-commit-operation.ts +0 -6
  138. package/src/models/progress.ts +0 -67
  139. package/src/models/rebase.ts +0 -20
  140. package/src/models/remote.ts +0 -10
  141. package/src/models/repository.ts +0 -16
  142. package/src/models/stash-entry.ts +0 -25
  143. package/src/models/status.ts +0 -275
  144. package/src/models/submodule.ts +0 -13
  145. package/src/models/worktree.ts +0 -11
package/src/git/apply.ts DELETED
@@ -1,154 +0,0 @@
1
- import { GitError as DugiteError } from './exec.js'
2
- import { git } from './core.js'
3
- import {
4
- WorkingDirectoryFileChange,
5
- AppFileStatusKind,
6
- } from '../models/status.js'
7
- import { DiffType, ITextDiff, DiffSelection } from '../models/diff/index.js'
8
- import { Repository } from '../models/repository.js'
9
- import { getWorkingDirectoryDiff } from './diff.js'
10
- import { formatPatch, formatPatchToDiscardChanges } from '../lib/patch-formatter.js'
11
- import { assertNever } from '../lib/fatal-error.js'
12
-
13
- export async function applyPatchToIndex(
14
- repository: Repository,
15
- file: WorkingDirectoryFileChange
16
- ): Promise<void> {
17
- // If the file was a rename we have to recreate that rename since we've
18
- // just blown away the index. Think of this block of weird looking commands
19
- // as running `git mv`.
20
- if (file.status.kind === AppFileStatusKind.Renamed) {
21
- // Make sure the index knows of the removed file. We could use
22
- // update-index --force-remove here but we're not since it's
23
- // possible that someone staged a rename and then recreated the
24
- // original file and we don't have any guarantees for in which order
25
- // partial stages vs full-file stages happen. By using git add the
26
- // worst that could happen is that we re-stage a file already staged
27
- // by updateIndex.
28
- await git(
29
- ['add', '--update', '--', file.status.oldPath],
30
- repository.path,
31
- 'applyPatchToIndex'
32
- )
33
-
34
- // Figure out the blob oid of the removed file
35
- // <mode> SP <type> SP <object> TAB <file>
36
- const oldFile = await git(
37
- ['ls-tree', 'HEAD', '--', file.status.oldPath],
38
- repository.path,
39
- 'applyPatchToIndex'
40
- )
41
-
42
- const [info] = oldFile.stdout.split('\t', 1)
43
- const [mode, , oid] = info.split(' ', 3)
44
-
45
- // Add the old file blob to the index under the new name
46
- await git(
47
- ['update-index', '--add', '--cacheinfo', mode, oid, file.path],
48
- repository.path,
49
- 'applyPatchToIndex'
50
- )
51
- }
52
-
53
- const applyArgs: string[] = [
54
- 'apply',
55
- '--cached',
56
- '--unidiff-zero',
57
- '--whitespace=nowarn',
58
- '-',
59
- ]
60
-
61
- const diff = await getWorkingDirectoryDiff(repository, file)
62
-
63
- if (diff.kind !== DiffType.Text && diff.kind !== DiffType.LargeText) {
64
- const { kind } = diff
65
- switch (diff.kind) {
66
- case DiffType.Binary:
67
- case DiffType.Submodule:
68
- case DiffType.Image:
69
- throw new Error(
70
- `Can't create partial commit in binary file: ${file.path}`
71
- )
72
- case DiffType.Unrenderable:
73
- throw new Error(
74
- `File diff is too large to generate a partial commit: ${file.path}`
75
- )
76
- default:
77
- assertNever(diff, `Unknown diff kind: ${kind}`)
78
- }
79
- }
80
-
81
- const patch = await formatPatch(repository, file, file.selection)
82
- await git(applyArgs, repository.path, 'applyPatchToIndex', { stdin: patch })
83
-
84
- return Promise.resolve()
85
- }
86
-
87
- /**
88
- * Test a patch to see if it will apply cleanly.
89
- *
90
- * @param workTree work tree (which should be checked out to a specific commit)
91
- * @param patch a Git patch (or patch series) to try applying
92
- * @returns whether the patch applies cleanly
93
- *
94
- * See `formatPatch` to generate a patch series from existing Git commits
95
- */
96
- export async function checkPatch(
97
- workTree: { path: string },
98
- patch: string
99
- ): Promise<boolean> {
100
- const result = await git(
101
- ['apply', '--check', '-'],
102
- workTree.path,
103
- 'checkPatch',
104
- {
105
- stdin: patch,
106
- encoding: 'utf8' as any,
107
- expectedErrors: new Set<DugiteError>([DugiteError.PatchDoesNotApply]),
108
- }
109
- )
110
-
111
- if (result.gitError === DugiteError.PatchDoesNotApply) {
112
- // other errors will be thrown if encountered, so this is fine for now
113
- return false
114
- }
115
-
116
- return true
117
- }
118
-
119
- /**
120
- * Discards the local changes for the specified file based on the passed diff
121
- * and a selection of lines from it.
122
- *
123
- * When passed an empty selection, this method won't do anything. When passed a
124
- * full selection, all changes from the file will be discarded.
125
- *
126
- * @param repository The repository in which to update the working directory
127
- * with information from the index
128
- *
129
- * @param filePath The relative path in the working directory of the file to use
130
- *
131
- * @param diff The diff containing the file local changes
132
- *
133
- * @param selection The selection of changes from the diff to discard
134
- */
135
- export async function discardChangesFromSelection(
136
- repository: Repository,
137
- filePath: string,
138
- diff: ITextDiff,
139
- selection: DiffSelection
140
- ) {
141
- const file = new WorkingDirectoryFileChange(filePath, { kind: AppFileStatusKind.Modified }, selection)
142
- const patch = await formatPatchToDiscardChanges(repository, file)
143
-
144
- if (patch === null || patch === '') {
145
- // When the patch is null we don't need to apply it since it will be a noop.
146
- return
147
- }
148
-
149
- const args = ['apply', '--unidiff-zero', '--whitespace=nowarn', '-']
150
-
151
- await git(args, repository.path, 'discardChangesFromSelection', {
152
- stdin: patch,
153
- })
154
- }
@@ -1,20 +0,0 @@
1
- import { GitError as DugiteError } from './exec.js'
2
-
3
- /** Get the environment for authenticating remote operations. */
4
- export function envForAuthentication(): Record<string, string | undefined> {
5
- return {
6
- // supported since Git 2.3, this is used to ensure we never interactively prompt
7
- // for credentials - even as a fallback
8
- GIT_TERMINAL_PROMPT: '0',
9
- // localStorage is not available in Node.js; fall back to process.env
10
- GIT_TRACE: process.env.GIT_TRACE ?? '0',
11
- }
12
- }
13
-
14
- /** The set of errors which fit under the "authentication failed" umbrella. */
15
- export const AuthenticationErrors: ReadonlySet<DugiteError> = new Set([
16
- DugiteError.HTTPSAuthenticationFailed,
17
- DugiteError.SSHAuthenticationFailed,
18
- DugiteError.HTTPSRepositoryNotFound,
19
- DugiteError.SSHRepositoryNotFound,
20
- ])
package/src/git/branch.ts DELETED
@@ -1,206 +0,0 @@
1
- import { git, isGitError } from './core.js'
2
- import { Repository } from '../models/repository.js'
3
- import { Branch } from '../models/branch.js'
4
- import { formatAsLocalRef } from './refs.js'
5
- import { deleteRef } from './update-ref.js'
6
- import { GitError as DugiteError } from './exec.js'
7
- import { envForRemoteOperation } from './environment.js'
8
- import { createForEachRefParser } from './git-delimiter-parser.js'
9
- import { IRemote } from '../models/remote.js'
10
- import { coerceToString } from './coerce-to-string.js'
11
-
12
- /**
13
- * Create a new branch from the given start point.
14
- *
15
- * @param repository - The repository in which to create the new branch
16
- * @param name - The name of the new branch
17
- * @param startPoint - A committish string that the new branch should be based
18
- * on, or undefined if the branch should be created based
19
- * off of the current state of HEAD
20
- */
21
- export async function createBranch(
22
- repository: Repository,
23
- name: string,
24
- startPoint: string | null,
25
- noTrack?: boolean
26
- ): Promise<void> {
27
- const args =
28
- startPoint !== null ? ['branch', name, startPoint] : ['branch', name]
29
-
30
- // if we're branching directly from a remote branch, we don't want to track it
31
- // tracking it will make the rest of desktop think we want to push to that
32
- // remote branch's upstream (which would likely be the upstream of the fork)
33
- if (noTrack) {
34
- args.push('--no-track')
35
- }
36
-
37
- await git(args, repository.path, 'createBranch')
38
- }
39
-
40
- export const getBranchNames = ({ path }: Repository): Promise<string[]> => {
41
- const parser = createForEachRefParser({ name: '%(refname:short)' })
42
- return git(['branch', ...parser.formatArgs], path, 'getBranchNames').then(x =>
43
- parser.parse(x.stdout).map(b => b.name)
44
- )
45
- }
46
-
47
- /** Rename the given branch to a new name. */
48
- export async function renameBranch(
49
- repository: Repository,
50
- branch: Branch,
51
- newName: string,
52
- force?: boolean
53
- ): Promise<void> {
54
- try {
55
- await git(
56
- ['branch', force ? '-M' : '-m', branch.nameWithoutRemote, newName],
57
- repository.path,
58
- 'renameBranch'
59
- )
60
- } catch (error) {
61
- // If we failed to rename and the branch name only differs by case, we
62
- // we'll try again with the -M flag to force the rename. See
63
- // https://github.com/desktop/desktop/issues/21320
64
- if (
65
- // Only retry if the caller hasn't explicitly asked us to force the rename
66
- force === undefined &&
67
- isGitError(error) &&
68
- error.result.gitError === DugiteError.BranchAlreadyExists
69
- ) {
70
- const stderr = coerceToString(error.result.stderr)
71
- const m = /fatal: a branch named '(.+?)' already exists/.exec(stderr)
72
-
73
- if (m && m[1].toLowerCase() === newName.toLowerCase()) {
74
- // At this point we're almost certain that we are dealing with a
75
- // case-only rename on a case insensitive filesystem, but we can't
76
- // be 100% sure, NTFS can be configured to be case sensitive and macOS
77
- // might have case sensitive file systems mounted so we have to list
78
- // all branches and check the names.
79
- return (
80
- getBranchNames(repository)
81
- // Throw the original error if we fail to get the branch names
82
- .catch(() => Promise.reject(error))
83
- .then(names =>
84
- // If we find the new name in the list of branches we can't
85
- // safely assume it's a case-only rename and have to
86
- // propagate the original error, otherwise try again with -M
87
- names.includes(newName)
88
- ? Promise.reject(error)
89
- : renameBranch(repository, branch, newName, true)
90
- )
91
- )
92
- }
93
- }
94
- throw error
95
- }
96
- }
97
-
98
- /**
99
- * Delete the branch locally.
100
- */
101
- export async function deleteLocalBranch(
102
- repository: Repository,
103
- branchName: string
104
- ): Promise<true> {
105
- await git(['branch', '-D', branchName], repository.path, 'deleteLocalBranch')
106
- return true
107
- }
108
-
109
- /**
110
- * Deletes a remote branch
111
- *
112
- * @param remoteName - the name of the remote to delete the branch from
113
- * @param remoteBranchName - the name of the branch on the remote
114
- */
115
- export async function deleteRemoteBranch(
116
- repository: Repository,
117
- remote: IRemote,
118
- remoteBranchName: string
119
- ): Promise<true> {
120
- const args = ['push', remote.name, `:${remoteBranchName}`]
121
-
122
- // If the user is not authenticated, the push is going to fail
123
- // Let this propagate and leave it to the caller to handle
124
- const result = await git(args, repository.path, 'deleteRemoteBranch', {
125
- env: await envForRemoteOperation(remote.url),
126
- expectedErrors: new Set<DugiteError>([DugiteError.BranchDeletionFailed]),
127
- })
128
-
129
- // It's possible that the delete failed because the ref has already
130
- // been deleted on the remote. If we identify that specific
131
- // error we can safely remove our remote ref which is what would
132
- // happen if the push didn't fail.
133
- if (result.gitError === DugiteError.BranchDeletionFailed) {
134
- const ref = `refs/remotes/${remote.name}/${remoteBranchName}`
135
- await deleteRef(repository, ref)
136
- }
137
-
138
- return true
139
- }
140
-
141
- /**
142
- * Finds branches that have a tip equal to the given committish
143
- *
144
- * @param repository within which to execute the command
145
- * @param commitish a sha, HEAD, etc that the branch(es) tip should be
146
- * @returns list branch names. null if an error is encountered
147
- */
148
- export async function getBranchesPointedAt(
149
- repository: Repository,
150
- commitish: string
151
- ): Promise<Array<string> | null> {
152
- const args = [
153
- 'branch',
154
- `--points-at=${commitish}`,
155
- '--format=%(refname:short)',
156
- ]
157
- // this command has an implicit \n delimiter
158
- const { stdout, exitCode } = await git(
159
- args,
160
- repository.path,
161
- 'branchPointedAt',
162
- {
163
- // - 1 is returned if a common ancestor cannot be resolved
164
- // - 129 is returned if ref is malformed
165
- // "warning: ignoring broken ref refs/remotes/origin/main."
166
- successExitCodes: new Set([0, 1, 129]),
167
- }
168
- )
169
- if (exitCode === 1 || exitCode === 129) {
170
- return null
171
- }
172
- // split (and remove trailing element cause its always an empty string)
173
- return stdout.split('\n').slice(0, -1)
174
- }
175
-
176
- /**
177
- * Gets all branches that have been merged into the given branch
178
- *
179
- * @param repository The repository in which to search
180
- * @param branchName The to be used as the base branch
181
- * @returns map of branch canonical refs paired to its sha
182
- */
183
- export async function getMergedBranches(
184
- repository: Repository,
185
- branchName: string
186
- ): Promise<Map<string, string>> {
187
- const canonicalBranchRef = formatAsLocalRef(branchName)
188
- const { formatArgs, parse } = createForEachRefParser({
189
- sha: '%(objectname)',
190
- canonicalRef: '%(refname)',
191
- })
192
-
193
- const args = ['branch', ...formatArgs, '--merged', branchName]
194
- const mergedBranches = new Map<string, string>()
195
- const { stdout } = await git(args, repository.path, 'mergedBranches')
196
-
197
- for (const branch of parse(stdout)) {
198
- // Don't include the branch we're using to compare against
199
- // in the list of branches merged into that branch.
200
- if (branch.canonicalRef !== canonicalBranchRef) {
201
- mergedBranches.set(branch.canonicalRef, branch.sha)
202
- }
203
- }
204
-
205
- return mergedBranches
206
- }
@@ -1,40 +0,0 @@
1
- import { git } from './core.js'
2
- import { Repository } from '../models/repository.js'
3
-
4
- /**
5
- * Forcefully updates the working directory with information from the index
6
- * for a given set of files.
7
- *
8
- * This method is essentially the same as running `git checkout -- files`
9
- * except by using `checkout-index` we can pass the files we want updated
10
- * on stdin, avoiding all issues with too long arguments.
11
- *
12
- * Note that this function will not yield errors for paths that don't
13
- * exist in the index (-q).
14
- *
15
- * @param repository The repository in which to update the working directory
16
- * with information from the index
17
- *
18
- * @param paths The relative paths in the working directory to update
19
- * with information from the index.
20
- */
21
- export async function checkoutIndex(
22
- repository: Repository,
23
- paths: ReadonlyArray<string>
24
- ) {
25
- if (!paths.length) {
26
- return
27
- }
28
-
29
- const options = {
30
- successExitCodes: new Set([0, 1]),
31
- stdin: paths.join('\0'),
32
- }
33
-
34
- await git(
35
- ['checkout-index', '-f', '-u', '-q', '--stdin', '-z'],
36
- repository.path,
37
- 'checkoutIndex',
38
- options
39
- )
40
- }
@@ -1,235 +0,0 @@
1
- import { git, IGitStringExecutionOptions } from './core.js'
2
- import { Repository } from '../models/repository.js'
3
- import { Branch, BranchType } from '../models/branch.js'
4
- import { clampProgress, ICheckoutProgress } from '../models/progress.js'
5
- import {
6
- CheckoutProgressParser,
7
- executionOptionsWithProgress,
8
- } from '../lib/progress/index.js'
9
- import { AuthenticationErrors } from './authentication.js'
10
- import {
11
- envForRemoteOperation,
12
- getFallbackUrlForProxyResolve,
13
- } from './environment.js'
14
- import { WorkingDirectoryFileChange } from '../models/status.js'
15
- import { ManualConflictResolution } from '../models/manual-conflict-resolution.js'
16
- import { CommitOneLine, shortenSHA } from '../models/commit.js'
17
- import { IRemote } from '../models/remote.js'
18
- import { updateSubmodulesAfterOperation } from './submodule.js'
19
-
20
- export type ProgressCallback = (progress: ICheckoutProgress) => void
21
-
22
- const CheckoutStepWeight = 0.9
23
-
24
- function getCheckoutArgs(progressCallback?: ProgressCallback) {
25
- return ['checkout', ...(progressCallback ? ['--progress'] : [])]
26
- }
27
-
28
- async function getBranchCheckoutArgs(branch: Branch) {
29
- return [
30
- branch.name,
31
- ...(branch.type === BranchType.Remote
32
- ? ['-b', branch.nameWithoutRemote]
33
- : []),
34
- '--',
35
- ]
36
- }
37
-
38
- async function getCheckoutOpts(
39
- repository: Repository,
40
- title: string,
41
- target: string,
42
- currentRemote: IRemote | null,
43
- progressCallback?: ProgressCallback,
44
- initialDescription?: string
45
- ): Promise<IGitStringExecutionOptions> {
46
- const opts: IGitStringExecutionOptions = {
47
- env: await envForRemoteOperation(
48
- getFallbackUrlForProxyResolve(repository, currentRemote)
49
- ),
50
- expectedErrors: AuthenticationErrors,
51
- }
52
-
53
- if (!progressCallback) {
54
- return opts
55
- }
56
-
57
- const kind = 'checkout'
58
-
59
- // Initial progress
60
- progressCallback({
61
- kind,
62
- title,
63
- description: initialDescription ?? title,
64
- value: 0,
65
- target,
66
- })
67
-
68
- return await executionOptionsWithProgress(
69
- { ...opts, trackLFSProgress: true },
70
- new CheckoutProgressParser(),
71
- progress => {
72
- if (progress.kind === 'progress') {
73
- const description = progress.details.text
74
- const value = progress.percent
75
-
76
- progressCallback({
77
- kind,
78
- title,
79
- description,
80
- value,
81
- target,
82
- })
83
- }
84
- }
85
- )
86
- }
87
-
88
- /**
89
- * Check out the given branch.
90
- *
91
- * @param repository - The repository in which the branch checkout should
92
- * take place
93
- *
94
- * @param branch - The branch name that should be checked out
95
- *
96
- * @param progressCallback - An optional function which will be invoked
97
- * with information about the current progress
98
- * of the checkout operation. When provided this
99
- * enables the '--progress' command line flag for
100
- * 'git checkout'.
101
- */
102
- export async function checkoutBranch(
103
- repository: Repository,
104
- branch: Branch,
105
- currentRemote: IRemote | null,
106
- progressCallback?: ProgressCallback,
107
- allowFileProtocol: boolean = false
108
- ): Promise<true> {
109
- const title = `Checking out branch ${branch.name}`
110
- const opts = await getCheckoutOpts(
111
- repository,
112
- title,
113
- branch.name,
114
- currentRemote,
115
- progressCallback
116
- ? clampProgress(0, CheckoutStepWeight, progressCallback)
117
- : undefined,
118
- `Switching to branch`
119
- )
120
-
121
- const baseArgs = getCheckoutArgs(progressCallback)
122
- const args = [...baseArgs, ...(await getBranchCheckoutArgs(branch))]
123
-
124
- await git(args, repository.path, 'checkoutBranch', opts)
125
-
126
- // Update submodules after checkout
127
- await updateSubmodulesAfterOperation(
128
- repository,
129
- currentRemote,
130
- progressCallback
131
- ? clampProgress<ICheckoutProgress>(
132
- CheckoutStepWeight,
133
- 1,
134
- progressCallback
135
- )
136
- : undefined,
137
- 'checkout',
138
- title,
139
- branch.name,
140
- allowFileProtocol
141
- )
142
-
143
- // we return `true` here so `GitStore.performFailableGitOperation`
144
- // will return _something_ differentiable from `undefined` if this succeeds
145
- return true
146
- }
147
-
148
- /**
149
- * Check out the given commit.
150
- * Literally invokes `git checkout <commit SHA>`.
151
- *
152
- * @param repository - The repository in which the branch checkout should
153
- * take place
154
- *
155
- * @param commit - The commit that should be checked out
156
- *
157
- * @param progressCallback - An optional function which will be invoked
158
- * with information about the current progress
159
- * of the checkout operation. When provided this
160
- * enables the '--progress' command line flag for
161
- * 'git checkout'.
162
- */
163
- export async function checkoutCommit(
164
- repository: Repository,
165
- commit: CommitOneLine,
166
- currentRemote: IRemote | null,
167
- progressCallback?: ProgressCallback,
168
- allowFileProtocol: boolean = false
169
- ): Promise<true> {
170
- const title = `Checking out commit`
171
- const target = shortenSHA(commit.sha)
172
- const opts = await getCheckoutOpts(
173
- repository,
174
- title,
175
- target,
176
- currentRemote,
177
- progressCallback
178
- ? clampProgress(0, CheckoutStepWeight, progressCallback)
179
- : undefined
180
- )
181
-
182
- const baseArgs = getCheckoutArgs(progressCallback)
183
- const args = [...baseArgs, commit.sha]
184
-
185
- await git(args, repository.path, 'checkoutCommit', opts)
186
-
187
- // Update submodules after checkout
188
- await updateSubmodulesAfterOperation(
189
- repository,
190
- currentRemote,
191
- progressCallback
192
- ? clampProgress<ICheckoutProgress>(
193
- CheckoutStepWeight,
194
- 1,
195
- progressCallback
196
- )
197
- : undefined,
198
- 'checkout',
199
- title,
200
- target,
201
- allowFileProtocol
202
- )
203
-
204
- // we return `true` here so `GitStore.performFailableGitOperation`
205
- // will return _something_ differentiable from `undefined` if this succeeds
206
- return true
207
- }
208
-
209
- /** Check out the paths at HEAD. */
210
- export async function checkoutPaths(
211
- repository: Repository,
212
- paths: ReadonlyArray<string>
213
- ): Promise<void> {
214
- await git(
215
- ['checkout', 'HEAD', '--', ...paths],
216
- repository.path,
217
- 'checkoutPaths'
218
- )
219
- }
220
-
221
- /**
222
- * Check out either stage #2 (ours) or #3 (theirs) for a conflicted
223
- * file.
224
- */
225
- export async function checkoutConflictedFile(
226
- repository: Repository,
227
- file: WorkingDirectoryFileChange,
228
- resolution: ManualConflictResolution
229
- ) {
230
- await git(
231
- ['checkout', `--${resolution}`, '--', file.path],
232
- repository.path,
233
- 'checkoutConflictedFile'
234
- )
235
- }