git-chopstick-core 0.1.0

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 (119) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +71 -0
  3. package/examples/get-status.ts +84 -0
  4. package/package.json +20 -0
  5. package/src/git/add.ts +16 -0
  6. package/src/git/apply.ts +154 -0
  7. package/src/git/authentication.ts +19 -0
  8. package/src/git/branch.ts +206 -0
  9. package/src/git/checkout-index.ts +40 -0
  10. package/src/git/checkout.ts +235 -0
  11. package/src/git/cherry-pick.ts +504 -0
  12. package/src/git/clean.ts +9 -0
  13. package/src/git/clone.ts +86 -0
  14. package/src/git/coerce-to-buffer.ts +4 -0
  15. package/src/git/coerce-to-string.ts +4 -0
  16. package/src/git/commit.ts +136 -0
  17. package/src/git/config.ts +392 -0
  18. package/src/git/core.ts +625 -0
  19. package/src/git/create-tail-stream.ts +36 -0
  20. package/src/git/credential.ts +83 -0
  21. package/src/git/description.ts +33 -0
  22. package/src/git/diff-check.ts +27 -0
  23. package/src/git/diff-index.ts +116 -0
  24. package/src/git/diff.ts +880 -0
  25. package/src/git/environment.ts +116 -0
  26. package/src/git/exec.ts +285 -0
  27. package/src/git/fetch.ts +141 -0
  28. package/src/git/for-each-ref.ts +160 -0
  29. package/src/git/format-patch.ts +17 -0
  30. package/src/git/git-delimiter-parser.ts +95 -0
  31. package/src/git/gitignore.ts +157 -0
  32. package/src/git/index.ts +36 -0
  33. package/src/git/init.ts +11 -0
  34. package/src/git/interpret-trailers.ts +176 -0
  35. package/src/git/lfs.ts +100 -0
  36. package/src/git/log.ts +376 -0
  37. package/src/git/merge-tree.ts +42 -0
  38. package/src/git/merge.ts +154 -0
  39. package/src/git/multi-operation-terminal-output.ts +68 -0
  40. package/src/git/pull.ts +130 -0
  41. package/src/git/push-terminal-chunk.ts +41 -0
  42. package/src/git/push.ts +119 -0
  43. package/src/git/rebase.ts +627 -0
  44. package/src/git/reflog.ts +127 -0
  45. package/src/git/refs.ts +63 -0
  46. package/src/git/remote.ts +143 -0
  47. package/src/git/reorder.ts +153 -0
  48. package/src/git/reset.ts +101 -0
  49. package/src/git/rev-list.ts +201 -0
  50. package/src/git/rev-parse.ts +92 -0
  51. package/src/git/revert.ts +55 -0
  52. package/src/git/rm.ts +31 -0
  53. package/src/git/show.ts +88 -0
  54. package/src/git/spawn.ts +38 -0
  55. package/src/git/squash.ts +173 -0
  56. package/src/git/stage.ts +97 -0
  57. package/src/git/stash.ts +302 -0
  58. package/src/git/status.ts +502 -0
  59. package/src/git/submodule.ts +212 -0
  60. package/src/git/tag.ts +134 -0
  61. package/src/git/update-index.ts +169 -0
  62. package/src/git/update-ref.ts +50 -0
  63. package/src/git/var.ts +42 -0
  64. package/src/git/worktree-include.ts +146 -0
  65. package/src/git/worktree.ts +219 -0
  66. package/src/lib/api.ts +7 -0
  67. package/src/lib/diff-parser.ts +249 -0
  68. package/src/lib/directory-exists.ts +10 -0
  69. package/src/lib/errno-exception.ts +12 -0
  70. package/src/lib/fatal-error.ts +23 -0
  71. package/src/lib/feature-flag.ts +29 -0
  72. package/src/lib/file-system.ts +7 -0
  73. package/src/lib/get-old-path.ts +11 -0
  74. package/src/lib/git/environment.ts +14 -0
  75. package/src/lib/git-perf.ts +3 -0
  76. package/src/lib/helpers/default-branch.ts +3 -0
  77. package/src/lib/helpers/path.ts +5 -0
  78. package/src/lib/hooks/with-hooks-env.ts +7 -0
  79. package/src/lib/merge.ts +3 -0
  80. package/src/lib/noop.ts +1 -0
  81. package/src/lib/patch-formatter.ts +18 -0
  82. package/src/lib/path-exists.ts +7 -0
  83. package/src/lib/progress/from-process.ts +10 -0
  84. package/src/lib/progress/index.ts +43 -0
  85. package/src/lib/progress/revert.ts +17 -0
  86. package/src/lib/rebase.ts +3 -0
  87. package/src/lib/remove-remote-prefix.ts +4 -0
  88. package/src/lib/resolve-git-proxy.ts +3 -0
  89. package/src/lib/round.ts +4 -0
  90. package/src/lib/split-buffer.ts +14 -0
  91. package/src/lib/status-parser.ts +188 -0
  92. package/src/lib/stores/helpers/find-default-remote.ts +3 -0
  93. package/src/lib/trampoline/trampoline-environment.ts +8 -0
  94. package/src/models/branch.ts +78 -0
  95. package/src/models/cherry-pick.ts +12 -0
  96. package/src/models/clone-options.ts +6 -0
  97. package/src/models/commit-identity.ts +35 -0
  98. package/src/models/commit.ts +44 -0
  99. package/src/models/computed-action.ts +6 -0
  100. package/src/models/diff/diff-data.ts +78 -0
  101. package/src/models/diff/diff-line.ts +36 -0
  102. package/src/models/diff/diff-selection.ts +165 -0
  103. package/src/models/diff/image-diff.ts +6 -0
  104. package/src/models/diff/image.ts +8 -0
  105. package/src/models/diff/index.ts +6 -0
  106. package/src/models/diff/raw-diff.ts +41 -0
  107. package/src/models/git-author.ts +16 -0
  108. package/src/models/manual-conflict-resolution.ts +4 -0
  109. package/src/models/merge.ts +6 -0
  110. package/src/models/multi-commit-operation.ts +6 -0
  111. package/src/models/progress.ts +67 -0
  112. package/src/models/rebase.ts +20 -0
  113. package/src/models/remote.ts +10 -0
  114. package/src/models/repository.ts +16 -0
  115. package/src/models/stash-entry.ts +25 -0
  116. package/src/models/status.ts +275 -0
  117. package/src/models/submodule.ts +13 -0
  118. package/src/models/worktree.ts +11 -0
  119. package/tsconfig.json +17 -0
@@ -0,0 +1,235 @@
1
+ import { git, IGitStringExecutionOptions } from './core'
2
+ import { Repository } from '../models/repository'
3
+ import { Branch, BranchType } from '../models/branch'
4
+ import { clampProgress, ICheckoutProgress } from '../models/progress'
5
+ import {
6
+ CheckoutProgressParser,
7
+ executionOptionsWithProgress,
8
+ } from '../lib/progress'
9
+ import { AuthenticationErrors } from './authentication'
10
+ import {
11
+ envForRemoteOperation,
12
+ getFallbackUrlForProxyResolve,
13
+ } from './environment'
14
+ import { WorkingDirectoryFileChange } from '../models/status'
15
+ import { ManualConflictResolution } from '../models/manual-conflict-resolution'
16
+ import { CommitOneLine, shortenSHA } from '../models/commit'
17
+ import { IRemote } from '../models/remote'
18
+ import { updateSubmodulesAfterOperation } from './submodule'
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
+ }