workspace-tools 0.38.6 → 0.40.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.
@@ -5,57 +5,59 @@
5
5
  //
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.listAllTrackedFiles = exports.getDefaultBranch = exports.parseRemoteBranch = exports.getRemoteBranch = exports.getParentBranch = exports.revertLocalChanges = exports.stageAndCommit = exports.commit = exports.stage = exports.init = exports.getFileAddedHash = exports.getCurrentHash = exports.getShortBranchName = exports.getFullBranchRef = exports.getBranchName = exports.getUserEmail = exports.getRecentCommitMessages = exports.getStagedChanges = exports.getChangesBetweenRefs = exports.getBranchChanges = exports.getChanges = exports.getUnstagedChanges = exports.fetchRemoteBranch = exports.fetchRemote = exports.getUntrackedChanges = void 0;
8
+ const config_1 = require("./config");
8
9
  const git_1 = require("./git");
9
- /**
10
- * Get a list of files with untracked changes.
11
- * Throws an error on failure.
12
- *
13
- * @returns An array of file paths with untracked changes
14
- */
15
- function getUntrackedChanges(cwd) {
16
- try {
17
- return processGitOutput((0, git_1.git)(["ls-files", "--others", "--exclude-standard"], { cwd }));
18
- }
19
- catch (e) {
20
- throw new git_1.GitError(`Cannot gather information about untracked changes`, e);
21
- }
10
+ const diffArgs = ["--no-pager", "diff", "--name-only", "--relative"];
11
+ function getUntrackedChanges(cwdOrOptions) {
12
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
13
+ const results = (0, git_1.git)(["ls-files", "--others", "--exclude-standard"], {
14
+ description: "Gathering information about untracked changes",
15
+ throwOnError: true,
16
+ ...options,
17
+ });
18
+ return (0, git_1.processGitOutput)(results, { excludeNodeModules: true });
22
19
  }
23
20
  exports.getUntrackedChanges = getUntrackedChanges;
24
- /**
25
- * Fetch from the given remote.
26
- * Throws an error on failure.
27
- */
28
- function fetchRemote(remote, cwd) {
29
- const results = (0, git_1.git)(["fetch", "--", remote], { cwd });
30
- if (!results.success) {
31
- throw new git_1.GitError(`Cannot fetch remote "${remote}"`);
32
- }
21
+ function fetchRemote(remoteOrOptions, cwd) {
22
+ const { remote, remoteBranch, options, ...gitOptions } = typeof remoteOrOptions === "string"
23
+ ? ({ remote: remoteOrOptions, cwd: cwd })
24
+ : remoteOrOptions;
25
+ if (remoteBranch && !remote) {
26
+ throw new Error('Must provide "remote" when using "remoteBranch" option');
27
+ }
28
+ const fetchArgs = [
29
+ "fetch",
30
+ "--",
31
+ ...(remote ? [remote] : []),
32
+ ...(remoteBranch ? [remoteBranch] : []),
33
+ ...(options || []),
34
+ ];
35
+ (0, git_1.git)(fetchArgs, {
36
+ description: remote
37
+ ? `Fetching ${remoteBranch ? `branch "${remoteBranch}" from ` : ""}remote "${remote}"`
38
+ : "Fetching all remotes",
39
+ throwOnError: true,
40
+ ...gitOptions,
41
+ });
33
42
  }
34
43
  exports.fetchRemote = fetchRemote;
35
44
  /**
36
- * Fetch from the given remote and branch.
37
- * Throws an error on failure.
45
+ * Fetch from the given remote and branch. Throws an error on failure.
46
+ * @deprecated Use `fetchRemote({ remote, remoteBranch, cwd })`
38
47
  */
48
+ // TODO: move to fetch.ts
39
49
  function fetchRemoteBranch(remote, remoteBranch, cwd) {
40
- const results = (0, git_1.git)(["fetch", "--", remote, remoteBranch], { cwd });
41
- if (!results.success) {
42
- throw new git_1.GitError(`Cannot fetch branch "${remoteBranch}" from remote "${remote}"`);
43
- }
50
+ fetchRemote({ remote, remoteBranch, cwd, throwOnError: true });
44
51
  }
45
52
  exports.fetchRemoteBranch = fetchRemoteBranch;
46
- /**
47
- * Gets file paths with changes that have not been staged yet.
48
- * Throws an error on failure.
49
- *
50
- * @returns An array of relative file paths with unstaged changes
51
- */
52
- function getUnstagedChanges(cwd) {
53
- try {
54
- return processGitOutput((0, git_1.git)(["--no-pager", "diff", "--name-only", "--relative"], { cwd }));
55
- }
56
- catch (e) {
57
- throw new git_1.GitError(`Cannot gather information about unstaged changes`, e);
58
- }
53
+ function getUnstagedChanges(cwdOrOptions) {
54
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
55
+ const results = (0, git_1.git)(diffArgs, {
56
+ description: "Gathering information about unstaged changes",
57
+ throwOnError: true,
58
+ ...options,
59
+ });
60
+ return (0, git_1.processGitOutput)(results, { excludeNodeModules: true });
59
61
  }
60
62
  exports.getUnstagedChanges = getUnstagedChanges;
61
63
  /**
@@ -63,259 +65,157 @@ exports.getUnstagedChanges = getUnstagedChanges;
63
65
  * Throws an error on failure.
64
66
  *
65
67
  * @returns An array of relative file paths that have changed
68
+ * @deprecated Use `getBranchChanges({ branch, cwd })`
66
69
  */
70
+ // TODO: move to getChanges.ts
67
71
  function getChanges(branch, cwd) {
68
- try {
69
- return processGitOutput((0, git_1.git)(["--no-pager", "diff", "--relative", "--name-only", branch + "..."], { cwd }));
70
- }
71
- catch (e) {
72
- throw new git_1.GitError(`Cannot gather information about changes`, e);
73
- }
72
+ return getChangesBetweenRefs({ fromRef: branch, cwd, throwOnError: true });
74
73
  }
75
74
  exports.getChanges = getChanges;
76
- /**
77
- * Gets file paths with changes between the branch and the merge-base.
78
- *
79
- * @returns An array of relative file paths that have changed
80
- */
81
- function getBranchChanges(branch, cwd) {
82
- return getChangesBetweenRefs(/*from*/ branch, /*to*/ "", /*options*/ [], /*file pattern*/ "", cwd);
75
+ function getBranchChanges(branchOrOptions, cwd) {
76
+ const { branch, ...options } = typeof branchOrOptions === "string" ? { branch: branchOrOptions, cwd: cwd } : branchOrOptions;
77
+ return getChangesBetweenRefs({ fromRef: branch, throwOnError: true, ...options });
83
78
  }
84
79
  exports.getBranchChanges = getBranchChanges;
85
- /**
86
- * Gets file paths with changes between two git references (commits, branches, tags).
87
- * Throws an error on failure.
88
- *
89
- * @param fromRef - The starting reference
90
- * @param toRef - The ending reference
91
- * @param options - Additional git diff options
92
- * @param pattern - Optional file pattern to filter results
93
- * @param cwd - The working directory
94
- * @returns An array of file paths that have changed
95
- */
96
80
  function getChangesBetweenRefs(fromRef, toRef, options, pattern, cwd) {
97
- try {
98
- return processGitOutput((0, git_1.git)([
99
- "--no-pager",
100
- "diff",
101
- "--name-only",
102
- "--relative",
103
- ...options,
104
- `${fromRef}...${toRef}`,
105
- ...(pattern ? ["--", pattern] : []),
106
- ], { cwd }));
107
- }
108
- catch (e) {
109
- throw new git_1.GitError(`Cannot gather information about change between refs changes (${fromRef} to ${toRef})`, e);
110
- }
81
+ let gitOptions;
82
+ if (typeof fromRef === "string") {
83
+ gitOptions = { cwd: cwd };
84
+ }
85
+ else {
86
+ ({ fromRef, toRef, options, pattern, ...gitOptions } = fromRef);
87
+ }
88
+ const range = `${fromRef}...${toRef || ""}`;
89
+ const results = (0, git_1.git)([...diffArgs, ...(options || []), range, ...(pattern ? ["--", pattern] : [])], {
90
+ description: `Gathering information about changes between refs (${range})`,
91
+ throwOnError: true,
92
+ ...gitOptions,
93
+ });
94
+ return (0, git_1.processGitOutput)(results, { excludeNodeModules: true });
111
95
  }
112
96
  exports.getChangesBetweenRefs = getChangesBetweenRefs;
113
- /**
114
- * Gets all files with staged changes (files added to the index).
115
- * Throws an error on failure.
116
- *
117
- * @returns An array of relative file paths that have been staged
118
- */
119
- function getStagedChanges(cwd) {
120
- try {
121
- return processGitOutput((0, git_1.git)(["--no-pager", "diff", "--relative", "--staged", "--name-only"], { cwd }));
122
- }
123
- catch (e) {
124
- throw new git_1.GitError(`Cannot gather information about staged changes`, e);
125
- }
97
+ function getStagedChanges(cwdOrOptions) {
98
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
99
+ const results = (0, git_1.git)([...diffArgs, "--staged"], {
100
+ description: "Gathering information about staged changes",
101
+ throwOnError: true,
102
+ ...options,
103
+ });
104
+ return (0, git_1.processGitOutput)(results, { excludeNodeModules: true });
126
105
  }
127
106
  exports.getStagedChanges = getStagedChanges;
128
- /**
129
- * Gets recent commit messages between the specified branch and HEAD.
130
- * Returns an empty array if the operation fails.
131
- *
132
- * @returns An array of commit message strings
133
- */
134
- function getRecentCommitMessages(branch, cwd) {
135
- try {
136
- const results = (0, git_1.git)(["log", "--decorate", "--pretty=format:%s", `${branch}..HEAD`], { cwd });
137
- if (!results.success) {
138
- return [];
139
- }
140
- return results.stdout
141
- .split(/\n/)
142
- .map((line) => line.trim())
143
- .filter((line) => !!line);
144
- }
145
- catch (e) {
146
- throw new git_1.GitError(`Cannot gather information about recent commits`, e);
147
- }
107
+ function getRecentCommitMessages(branchOrOptions, cwd) {
108
+ const { branch, ...options } = typeof branchOrOptions === "string" ? { branch: branchOrOptions, cwd: cwd } : branchOrOptions;
109
+ const results = (0, git_1.git)(["log", "--decorate", "--pretty=format:%s", `${branch}..HEAD`], {
110
+ description: `Getting recent commit messages for branch "${branch}"`,
111
+ ...options,
112
+ });
113
+ return (0, git_1.processGitOutput)(results);
148
114
  }
149
115
  exports.getRecentCommitMessages = getRecentCommitMessages;
150
- /**
151
- * Gets the user email from the git config.
152
- * @returns The email string if found, null otherwise
153
- */
154
- function getUserEmail(cwd) {
155
- try {
156
- const results = (0, git_1.git)(["config", "user.email"], { cwd });
157
- return results.success ? results.stdout : null;
158
- }
159
- catch (e) {
160
- throw new git_1.GitError(`Cannot gather information about user.email`, e);
161
- }
116
+ function getUserEmail(cwdOrOptions) {
117
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
118
+ return (0, config_1.getConfigValue)({ key: "user.email", ...options });
162
119
  }
163
120
  exports.getUserEmail = getUserEmail;
164
- /**
165
- * Gets the current branch name.
166
- * @returns The branch name if successful, null otherwise
167
- */
168
- function getBranchName(cwd) {
169
- try {
170
- const results = (0, git_1.git)(["rev-parse", "--abbrev-ref", "HEAD"], { cwd });
171
- return results.success ? results.stdout : null;
172
- }
173
- catch (e) {
174
- throw new git_1.GitError(`Cannot get branch name`, e);
175
- }
121
+ function getBranchName(cwdOrOptions) {
122
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
123
+ const results = (0, git_1.git)(["rev-parse", "--abbrev-ref", "HEAD"], {
124
+ description: "Getting current branch name",
125
+ ...options,
126
+ });
127
+ return results.success ? results.stdout : null;
176
128
  }
177
129
  exports.getBranchName = getBranchName;
178
- /**
179
- * Gets the full reference path for a given branch.
180
- * @param branch - The short branch name (e.g., `branch-name`)
181
- * @returns The full branch reference (e.g., `refs/heads/branch-name`) if found, null otherwise
182
- */
183
- function getFullBranchRef(branch, cwd) {
184
- const showRefResults = (0, git_1.git)(["show-ref", "--heads", branch], { cwd });
130
+ function getFullBranchRef(branchOrOptions, cwd) {
131
+ const { branch, ...options } = typeof branchOrOptions === "string" ? { branch: branchOrOptions, cwd: cwd } : branchOrOptions;
132
+ const showRefResults = (0, git_1.git)(["show-ref", "--heads", branch], options);
185
133
  return showRefResults.success ? showRefResults.stdout.split(" ")[1] : null;
186
134
  }
187
135
  exports.getFullBranchRef = getFullBranchRef;
188
- /**
189
- * Gets the short branch name from a full branch reference.
190
- * Note this may not work properly for the current branch.
191
- * @param fullBranchRef - The full branch reference (e.g., `refs/heads/branch-name`)
192
- * @returns The short branch name if successful, null otherwise
193
- */
194
- function getShortBranchName(fullBranchRef, cwd) {
195
- const showRefResults = (0, git_1.git)(["name-rev", "--name-only", fullBranchRef], {
196
- cwd,
197
- });
198
- return showRefResults.success ? showRefResults.stdout : null;
136
+ function getShortBranchName(refOrOptions, cwd) {
137
+ const { fullBranchRef, ...options } = typeof refOrOptions === "string" ? { fullBranchRef: refOrOptions, cwd: cwd } : refOrOptions;
138
+ // The original command `git name-rev --name-only` returned unreliable results if multiple
139
+ // named refs point to the same commit as the branch.
140
+ const showRefResults = (0, git_1.git)(["rev-parse", "--abbrev-ref", fullBranchRef], options);
141
+ return showRefResults.success ? showRefResults.stdout || null : null;
199
142
  }
200
143
  exports.getShortBranchName = getShortBranchName;
201
- /**
202
- * Gets the current commit hash (SHA).
203
- * @returns The hash if successful, null otherwise
204
- */
205
- function getCurrentHash(cwd) {
206
- try {
207
- const results = (0, git_1.git)(["rev-parse", "HEAD"], { cwd });
208
- return results.success ? results.stdout : null;
209
- }
210
- catch (e) {
211
- throw new git_1.GitError(`Cannot get current git hash`, e);
212
- }
144
+ function getCurrentHash(cwdOrOptions) {
145
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
146
+ const results = (0, git_1.git)(["rev-parse", "HEAD"], {
147
+ description: "Getting current git hash",
148
+ ...options,
149
+ });
150
+ return results.success ? results.stdout : null;
213
151
  }
214
152
  exports.getCurrentHash = getCurrentHash;
215
- /**
216
- * Get the commit hash in which the file was first added.
217
- * @returns The commit hash if found, undefined otherwise
218
- */
219
- function getFileAddedHash(filename, cwd) {
220
- const results = (0, git_1.git)(["rev-list", "--max-count=1", "HEAD", filename], { cwd });
221
- if (results.success) {
222
- return results.stdout.trim();
223
- }
224
- return undefined;
153
+ function getFileAddedHash(filenameOrOptions, cwd) {
154
+ const { filename, ...options } = typeof filenameOrOptions === "string" ? { filename: filenameOrOptions, cwd: cwd } : filenameOrOptions;
155
+ const results = (0, git_1.git)(["rev-list", "--max-count=1", "HEAD", filename], options);
156
+ return results.success ? results.stdout.trim() : undefined;
225
157
  }
226
158
  exports.getFileAddedHash = getFileAddedHash;
227
- /**
228
- * Initializes a git repository in the specified directory.
229
- * Optionally sets user email and username if not already configured.
230
- * Throws an error if required email or username is not provided and not already configured.
231
- *
232
- * @param cwd - The directory to initialize the git repository in
233
- * @param email - Optional email to set in git config
234
- * @param username - Optional username to set in git config
235
- */
236
- function init(cwd, email, username) {
237
- (0, git_1.git)(["init"], { cwd });
238
- const configLines = (0, git_1.git)(["config", "--list"], { cwd }).stdout.split("\n");
239
- if (!configLines.find((line) => line.includes("user.name"))) {
159
+ function init(cwdOrOptions, _email, _username) {
160
+ const { email, username, ...options } = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions, email: _email, username: _username } : cwdOrOptions;
161
+ (0, git_1.git)(["init"], { ...options, throwOnError: true });
162
+ if (!(0, config_1.getConfigValue)({ key: "user.name", ...options })) {
240
163
  if (!username) {
241
- throw new git_1.GitError("must include a username when initializing git repo");
164
+ throw new Error("must include a username when initializing git repo");
242
165
  }
243
- (0, git_1.git)(["config", "user.name", username], { cwd });
166
+ (0, git_1.git)(["config", "user.name", username], options);
244
167
  }
245
- if (!configLines.find((line) => line.includes("user.email"))) {
168
+ if (!getUserEmail(options)) {
246
169
  if (!email) {
247
170
  throw new Error("must include a email when initializing git repo");
248
171
  }
249
- (0, git_1.git)(["config", "user.email", email], { cwd });
172
+ (0, git_1.git)(["config", "user.email", email], options);
250
173
  }
251
174
  }
252
175
  exports.init = init;
253
- /**
254
- * Stages files matching the given patterns.
255
- */
256
- function stage(patterns, cwd) {
257
- try {
258
- patterns.forEach((pattern) => {
259
- (0, git_1.git)(["add", pattern], { cwd });
260
- });
261
- }
262
- catch (e) {
263
- throw new git_1.GitError(`Cannot stage changes`, e);
176
+ function stage(patternsOrOptions, cwd) {
177
+ const { patterns, ...options } = Array.isArray(patternsOrOptions)
178
+ ? { patterns: patternsOrOptions, cwd: cwd }
179
+ : patternsOrOptions;
180
+ for (const pattern of patterns) {
181
+ (0, git_1.git)(["add", pattern], { ...options, description: `Staging changes (git add ${pattern})` });
264
182
  }
265
183
  }
266
184
  exports.stage = stage;
267
- /**
268
- * Creates a commit with the given message and optional git commit options.
269
- * Throws an error on failure.
270
- *
271
- * @param message - The commit message
272
- * @param cwd - The working directory
273
- * @param options - Additional git commit options
274
- */
275
- function commit(message, cwd, options = []) {
276
- try {
277
- const commitResults = (0, git_1.git)(["commit", "-m", message, ...options], { cwd });
278
- if (!commitResults.success) {
279
- throw new Error(`Cannot commit changes: ${commitResults.stdout} ${commitResults.stderr}`);
280
- }
281
- }
282
- catch (e) {
283
- throw new git_1.GitError(`Cannot commit changes`, e);
284
- }
185
+ function commit(messageOrOptions, _cwd, _options) {
186
+ const { message, options, ...gitOptions } = typeof messageOrOptions === "string"
187
+ ? { message: messageOrOptions, cwd: _cwd, options: _options }
188
+ : messageOrOptions;
189
+ (0, git_1.git)(["commit", "-m", message, ...(options || [])], {
190
+ throwOnError: true,
191
+ description: "Committing changes",
192
+ ...gitOptions,
193
+ });
285
194
  }
286
195
  exports.commit = commit;
287
- /**
288
- * Stages files matching the given patterns and creates a commit with the specified message.
289
- * Convenience function that combines `stage()` and `commit()`.
290
- * Throws an error on commit failure.
291
- *
292
- * @param patterns - File patterns to stage
293
- * @param message - The commit message
294
- * @param cwd - The working directory
295
- * @param commitOptions - Additional git commit options
296
- */
297
- function stageAndCommit(patterns, message, cwd, commitOptions = []) {
298
- stage(patterns, cwd);
299
- commit(message, cwd, commitOptions);
196
+ function stageAndCommit(patternsOrOptions, message, cwd, commitOptions) {
197
+ const options = Array.isArray(patternsOrOptions)
198
+ ? { patterns: patternsOrOptions, message: message, cwd: cwd, options: commitOptions }
199
+ : patternsOrOptions;
200
+ stage(options);
201
+ commit(options);
300
202
  }
301
203
  exports.stageAndCommit = stageAndCommit;
302
- /**
303
- * Reverts all local changes (both staged and unstaged) by stashing them and then dropping the stash.
304
- * @returns True if the revert was successful, false otherwise
305
- */
306
- function revertLocalChanges(cwd) {
204
+ function revertLocalChanges(cwdOrOptions) {
205
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
307
206
  const stash = `workspace-tools_${new Date().getTime()}`;
308
- (0, git_1.git)(["stash", "push", "-u", "-m", stash], { cwd });
309
- const results = (0, git_1.git)(["stash", "list"]);
207
+ if (!(0, git_1.git)(["stash", "push", "-u", "-m", stash], options).success) {
208
+ return false;
209
+ }
210
+ const results = (0, git_1.git)(["stash", "list"], options);
310
211
  if (results.success) {
311
- const lines = results.stdout.split(/\n/);
312
- const foundLine = lines.find((line) => line.includes(stash));
313
- if (foundLine) {
314
- const matched = foundLine.match(/^[^:]+/);
315
- if (matched) {
316
- (0, git_1.git)(["stash", "drop", matched[0]]);
317
- return true;
318
- }
212
+ const matched = results.stdout
213
+ .split(/\n/)
214
+ .find((line) => line.includes(stash))
215
+ ?.match(/^[^:]+/);
216
+ if (matched) {
217
+ (0, git_1.git)(["stash", "drop", matched[0]], options);
218
+ return true;
319
219
  }
320
220
  }
321
221
  return false;
@@ -323,11 +223,11 @@ function revertLocalChanges(cwd) {
323
223
  exports.revertLocalChanges = revertLocalChanges;
324
224
  /**
325
225
  * Attempts to determine the parent branch of the current branch using `git show-branch`.
326
- *
327
226
  * @returns The parent branch name if found, null otherwise
227
+ * @deprecated Does not appear to be used
328
228
  */
329
229
  function getParentBranch(cwd) {
330
- const branchName = getBranchName(cwd);
230
+ const branchName = getBranchName({ cwd });
331
231
  if (!branchName || branchName === "HEAD") {
332
232
  return null;
333
233
  }
@@ -341,75 +241,48 @@ function getParentBranch(cwd) {
341
241
  return null;
342
242
  }
343
243
  exports.getParentBranch = getParentBranch;
344
- /**
345
- * Gets the remote tracking branch for the specified branch.
346
- *
347
- * @returns The remote branch name (e.g., `origin/main`) if found, null otherwise
348
- */
349
- function getRemoteBranch(branch, cwd) {
350
- const results = (0, git_1.git)(["rev-parse", "--abbrev-ref", "--symbolic-full-name", `${branch}@\{u\}`], { cwd });
351
- if (results.success) {
352
- return results.stdout.trim();
353
- }
354
- return null;
244
+ function getRemoteBranch(branchOrOptions, cwd) {
245
+ const options = typeof branchOrOptions === "string" ? { branch: branchOrOptions, cwd: cwd } : branchOrOptions;
246
+ const results = (0, git_1.git)(["rev-parse", "--abbrev-ref", "--symbolic-full-name", `${options.branch}@\{u\}`], options);
247
+ return results.success ? results.stdout.trim() : null;
355
248
  }
356
249
  exports.getRemoteBranch = getRemoteBranch;
357
- /**
358
- * Parses a remote branch string (e.g., `origin/main`) into its components.
359
- *
360
- * @param branch - The remote branch string to parse (e.g., `origin/main`)
361
- */
362
- function parseRemoteBranch(branch) {
363
- const firstSlashPos = branch.indexOf("/", 0);
364
- const remote = branch.substring(0, firstSlashPos);
365
- const remoteBranch = branch.substring(firstSlashPos + 1);
366
- return {
367
- remote,
368
- remoteBranch,
369
- };
250
+ function parseRemoteBranch(branchOrOptions) {
251
+ if (typeof branchOrOptions === "string") {
252
+ const branch = branchOrOptions;
253
+ const firstSlashPos = branch.indexOf("/", 0);
254
+ return {
255
+ remote: branch.substring(0, firstSlashPos),
256
+ remoteBranch: branch.substring(firstSlashPos + 1),
257
+ };
258
+ }
259
+ const { branch, knownRemotes = ["origin", "upstream"], ...options } = branchOrOptions;
260
+ if (!branch.includes("/")) {
261
+ return { remote: "", remoteBranch: branch };
262
+ }
263
+ let remote = knownRemotes.find((remote) => branch.startsWith(`${remote}/`));
264
+ if (!remote) {
265
+ const remotes = (0, git_1.git)(["remote"], options).stdout.trim().split(/\n/);
266
+ remote = remotes.find((remote) => branch.startsWith(`${remote}/`));
267
+ }
268
+ if (remote) {
269
+ return { remote, remoteBranch: branch.slice(remote.length + 1) };
270
+ }
271
+ return { remote: "", remoteBranch: branch };
370
272
  }
371
273
  exports.parseRemoteBranch = parseRemoteBranch;
372
- /**
373
- * Gets the default branch based on `git config init.defaultBranch`, falling back to `master`.
374
- */
375
- function getDefaultBranch(cwd) {
376
- const result = (0, git_1.git)(["config", "init.defaultBranch"], { cwd });
274
+ function getDefaultBranch(cwdOrOptions) {
275
+ const options = typeof cwdOrOptions === "string" ? { cwd: cwdOrOptions } : cwdOrOptions;
377
276
  // Default to the legacy 'master' for backwards compat and old git clients
378
- return result.success ? result.stdout.trim() : "master";
277
+ return (0, config_1.getConfigValue)({ key: "init.defaultBranch", ...options }) || "master";
379
278
  }
380
279
  exports.getDefaultBranch = getDefaultBranch;
381
- /**
382
- * Lists all tracked files matching the given patterns.
383
- *
384
- * @param patterns - File patterns to match (passed to git ls-files)
385
- * @param cwd - The working directory
386
- * @returns An array of file paths, or an empty array if no files are found
387
- */
388
- function listAllTrackedFiles(patterns, cwd) {
389
- const results = (0, git_1.git)(["ls-files", ...patterns], { cwd });
390
- return results.success && results.stdout.trim() ? results.stdout.trim().split(/\n/) : [];
280
+ function listAllTrackedFiles(patternsOrOptions, cwd) {
281
+ const { patterns, ...options } = Array.isArray(patternsOrOptions)
282
+ ? { patterns: patternsOrOptions, cwd: cwd }
283
+ : patternsOrOptions;
284
+ const results = (0, git_1.git)(["ls-files", ...patterns], { throwOnError: true, ...options });
285
+ return (0, git_1.processGitOutput)(results);
391
286
  }
392
287
  exports.listAllTrackedFiles = listAllTrackedFiles;
393
- /**
394
- * Processes git command output by splitting it into lines and filtering out empty lines and `node_modules`.
395
- *
396
- * If the command failed with stderr output, an error is thrown.
397
- *
398
- * @param output - The git command output to process
399
- * @returns An array of lines (presumably file paths), or an empty array if the command failed
400
- * without stderr output.
401
- */
402
- function processGitOutput(output) {
403
- if (!output.success) {
404
- if (output.stderr) {
405
- throw new Error(output.stderr);
406
- }
407
- // TODO: this inconsistency seems maybe not desirable?
408
- return [];
409
- }
410
- return output.stdout
411
- .split(/\n/)
412
- .map((line) => line.trim())
413
- .filter((line) => !!line && !line.includes("node_modules"));
414
- }
415
288
  //# sourceMappingURL=gitUtilities.js.map