isomorphic-git 1.41.7 → 1.41.8

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.
@@ -2208,6 +2208,38 @@ class GitRefManager {
2208
2208
  throw new NotFoundError(ref)
2209
2209
  }
2210
2210
 
2211
+ /**
2212
+ * Checks whether a ref names the branch HEAD points at in a repository where
2213
+ * that branch has no commits yet.
2214
+ *
2215
+ * A repository created by `git init` has a HEAD pointing at a branch that
2216
+ * does not exist on disk. Resolving that branch fails the same way resolving
2217
+ * a misspelled ref does, so callers that want to treat the two differently
2218
+ * need this to tell them apart.
2219
+ *
2220
+ * @param {Object} args
2221
+ * @param {FSClient} args.fs - A file system implementation.
2222
+ * @param {string} [args.gitdir=join(dir, '.git')] - [required] The [git directory](dir-vs-gitdir.md) path
2223
+ * @param {string} args.ref - The ref to check.
2224
+ * @returns {Promise<boolean>} - True if the ref names an unborn branch.
2225
+ */
2226
+ static async isUnbornBranch({ fs, gitdir, ref }) {
2227
+ let target;
2228
+ try {
2229
+ // depth 2 stops at the symbolic target instead of following it to an oid.
2230
+ target = await GitRefManager.resolve({
2231
+ fs,
2232
+ gitdir,
2233
+ ref: 'HEAD',
2234
+ depth: 2,
2235
+ });
2236
+ } catch (_) {
2237
+ return false
2238
+ }
2239
+ if (!target.startsWith('refs/heads/')) return false
2240
+ return ref === 'HEAD' || refpaths(ref).includes(target)
2241
+ }
2242
+
2211
2243
  /**
2212
2244
  * Checks if a ref exists.
2213
2245
  *
@@ -5476,9 +5508,16 @@ class GitWalkerRepo {
5476
5508
  try {
5477
5509
  oid = await GitRefManager.resolve({ fs, gitdir, ref });
5478
5510
  } catch (e) {
5479
- if (e instanceof NotFoundError) {
5480
- // Handle fresh branches with no commits
5511
+ // Only a branch that has no commits yet gets the empty tree. Anything
5512
+ // else that fails to resolve is a ref the caller got wrong, and
5513
+ // swallowing it here would walk an empty tree instead of saying so.
5514
+ if (
5515
+ e instanceof NotFoundError &&
5516
+ (await GitRefManager.isUnbornBranch({ fs, gitdir, ref }))
5517
+ ) {
5481
5518
  oid = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
5519
+ } else {
5520
+ throw e
5482
5521
  }
5483
5522
  }
5484
5523
  const tree = await resolveTree({ fs, cache: this.cache, gitdir, oid });
@@ -582,6 +582,26 @@ export class GitRefManager {
582
582
  ref: string;
583
583
  depth?: number | undefined;
584
584
  }): Promise<string>;
585
+ /**
586
+ * Checks whether a ref names the branch HEAD points at in a repository where
587
+ * that branch has no commits yet.
588
+ *
589
+ * A repository created by `git init` has a HEAD pointing at a branch that
590
+ * does not exist on disk. Resolving that branch fails the same way resolving
591
+ * a misspelled ref does, so callers that want to treat the two differently
592
+ * need this to tell them apart.
593
+ *
594
+ * @param {Object} args
595
+ * @param {FSClient} args.fs - A file system implementation.
596
+ * @param {string} [args.gitdir=join(dir, '.git')] - [required] The [git directory](dir-vs-gitdir.md) path
597
+ * @param {string} args.ref - The ref to check.
598
+ * @returns {Promise<boolean>} - True if the ref names an unborn branch.
599
+ */
600
+ static isUnbornBranch({ fs, gitdir, ref }: {
601
+ fs: FSClient;
602
+ gitdir?: string | undefined;
603
+ ref: string;
604
+ }): Promise<boolean>;
585
605
  /**
586
606
  * Checks if a ref exists.
587
607
  *
@@ -581,6 +581,26 @@ export class GitRefManager {
581
581
  ref: string;
582
582
  depth?: number | undefined;
583
583
  }): Promise<string>;
584
+ /**
585
+ * Checks whether a ref names the branch HEAD points at in a repository where
586
+ * that branch has no commits yet.
587
+ *
588
+ * A repository created by `git init` has a HEAD pointing at a branch that
589
+ * does not exist on disk. Resolving that branch fails the same way resolving
590
+ * a misspelled ref does, so callers that want to treat the two differently
591
+ * need this to tell them apart.
592
+ *
593
+ * @param {Object} args
594
+ * @param {FSClient} args.fs - A file system implementation.
595
+ * @param {string} [args.gitdir=join(dir, '.git')] - [required] The [git directory](dir-vs-gitdir.md) path
596
+ * @param {string} args.ref - The ref to check.
597
+ * @returns {Promise<boolean>} - True if the ref names an unborn branch.
598
+ */
599
+ static isUnbornBranch({ fs, gitdir, ref }: {
600
+ fs: FSClient;
601
+ gitdir?: string | undefined;
602
+ ref: string;
603
+ }): Promise<boolean>;
584
604
  /**
585
605
  * Checks if a ref exists.
586
606
  *
package/managers/index.js CHANGED
@@ -2201,6 +2201,38 @@ class GitRefManager {
2201
2201
  throw new NotFoundError(ref)
2202
2202
  }
2203
2203
 
2204
+ /**
2205
+ * Checks whether a ref names the branch HEAD points at in a repository where
2206
+ * that branch has no commits yet.
2207
+ *
2208
+ * A repository created by `git init` has a HEAD pointing at a branch that
2209
+ * does not exist on disk. Resolving that branch fails the same way resolving
2210
+ * a misspelled ref does, so callers that want to treat the two differently
2211
+ * need this to tell them apart.
2212
+ *
2213
+ * @param {Object} args
2214
+ * @param {FSClient} args.fs - A file system implementation.
2215
+ * @param {string} [args.gitdir=join(dir, '.git')] - [required] The [git directory](dir-vs-gitdir.md) path
2216
+ * @param {string} args.ref - The ref to check.
2217
+ * @returns {Promise<boolean>} - True if the ref names an unborn branch.
2218
+ */
2219
+ static async isUnbornBranch({ fs, gitdir, ref }) {
2220
+ let target;
2221
+ try {
2222
+ // depth 2 stops at the symbolic target instead of following it to an oid.
2223
+ target = await GitRefManager.resolve({
2224
+ fs,
2225
+ gitdir,
2226
+ ref: 'HEAD',
2227
+ depth: 2,
2228
+ });
2229
+ } catch (_) {
2230
+ return false
2231
+ }
2232
+ if (!target.startsWith('refs/heads/')) return false
2233
+ return ref === 'HEAD' || refpaths(ref).includes(target)
2234
+ }
2235
+
2204
2236
  /**
2205
2237
  * Checks if a ref exists.
2206
2238
  *
@@ -5463,9 +5495,16 @@ class GitWalkerRepo {
5463
5495
  try {
5464
5496
  oid = await GitRefManager.resolve({ fs, gitdir, ref });
5465
5497
  } catch (e) {
5466
- if (e instanceof NotFoundError) {
5467
- // Handle fresh branches with no commits
5498
+ // Only a branch that has no commits yet gets the empty tree. Anything
5499
+ // else that fails to resolve is a ref the caller got wrong, and
5500
+ // swallowing it here would walk an empty tree instead of saying so.
5501
+ if (
5502
+ e instanceof NotFoundError &&
5503
+ (await GitRefManager.isUnbornBranch({ fs, gitdir, ref }))
5504
+ ) {
5468
5505
  oid = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
5506
+ } else {
5507
+ throw e
5469
5508
  }
5470
5509
  }
5471
5510
  const tree = await resolveTree({ fs, cache: this.cache, gitdir, oid });