Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolveRef should be available through a mix-in #142

Open
binki opened this issue Jul 7, 2018 · 2 comments
Open

resolveRef should be available through a mix-in #142

binki opened this issue Jul 7, 2018 · 2 comments

Comments

@binki
Copy link

binki commented Jul 7, 2018

function resolveRef(repo, hashish, callback) {

To do something, I have to basically copy this entire function into my own code. It would be useful if this were available through a mix-in.

@binki
Copy link
Author

binki commented Jul 7, 2018

Hi, in fact I need more than resolveRef(). I need something to also expand intermediate refs so that I can expand, for example, HEAD which resolves to a ref. For example, this is what I am using now:

function resolveRef(repo, ref) {
  // Shouldn’t js-git also expose some helper for this?
  if (/^[0-9a-f]{40}$/.test(ref)) {
    return ref;
  }
  const refRefMatch = /^ref: *(.*)$/.exec(ref);
  if (refRefMatch) {
    return resolveRef(repo, refRefMatch[1]);
  }
  return new Promise((resolve, reject) => repo.readRef(ref, (ex, ref) => ex ? reject(ex) : resolve(ref))).then(ref => {
    if (!ref) {
      // Indicates that the ref could not be resolved to a hash.
      return null;
    }
    return resolveRef(repo, ref);
  });
}

function resolveRefOrFail(repo, ref) {
  return resolveRef(repo, resolvedRef => {
    if (!resolvedRef) {
      throw new Error(`Unable to resolve ref ${ref}`);
    }
    return resolvedRef;
  });
}

@TheKnarf
Copy link
Collaborator

Feel free to send a pull-request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants