2

I know from Retrieve specific commit from a remote Git repository that it's possible to fetch a specific commit from a remote git repository.

However, trying to do so from JGit, it's failing. For the same repository, when I run git fetch origin 57eab609d9efda3b8ee370582c3762c0e721033d:HEAD from terminal, the commit I want (with all its ancestors) is fetched from the remote repository. However, when I run the following using JGit, I get an exception:

RefSpec refSpec = new RefSpec()
    .setSourceDestination(commitId, HEAD)
    .setForceUpdate(true);
refs = Lists.newArrayList(refSpec);
git.fetch().setRemote(GIT_REMOTE_NAME)
    .setTimeout(REMOTE_FETCH_TIMEOUT)
    .setTransportConfigCallback(transportConfigurer)
    .setCredentialsProvider(gitCredentials)
    .setTagOpt(TagOpt.FETCH_TAGS)
    .setRefSpecs(refs)
    .call();

The exception is org.eclipse.jgit.errors.TransportException: Remote does not have 57eab609d9efda3b8ee370582c3762c0e721033d available for fetch.

Community
  • 1
  • 1
Alaa Nassef
  • 280
  • 2
  • 13

1 Answers1

3

With Git porcelain commands, you can only fetch refs, not specific commits. See also here: Fetch specific commit from remote git repo

The post you refer to only explains how to fetch a specific ref (refs/remotes/origin/branch in the given example) from a remote. I can't see where a specific commit (that is not referred to by a ref) is fetched.

The same applies to JGit: the FetchCommand need to be given a refspec (hence the name setRefSpecs()) to fetch from. With that information, JGit will fetch the commit to which the refspec points to and all its ancestors.

Community
  • 1
  • 1
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Well, I'm trying to set the `RefSpec`, but JGit can't find it, since it's the SHA of the `RevCommit`, not the `RefSpec` of a branch or a tag. However, with normal git command, I can fetch with specific commit SHA as per the original question. I looked at the code of `FetchProcess`, and it does call `conn.getRef(spec.getSource())`, which looks only at the `advertisedRefs`, so it seems that it's not implemented in JGit after all – Alaa Nassef Feb 12 '17 at 19:39
  • One more thing, the accepted answer from the question you referenced is from 2014. As per http://stackoverflow.com/a/30701724/552396, it seems that the functionality I described in my question was implemented in 2015, and it's described well in the Question referred to at the beginning of my question. I really wished it was implemented in JGit. Will try to send to Dev mailing list to see if there are plans for it. – Alaa Nassef Feb 12 '17 at 20:15
  • 1
    Thanks for pointing this out. I assume the low level APIs of JGit can be used to fetch certain commits, however the FetchCommand does not support this yet. – Rüdiger Herrmann Feb 13 '17 at 04:34
  • Just for completeness, I asked about that on the developers' mailing list, http://dev.eclipse.org/mhonarc/lists/jgit-dev/msg03273.html, and it seems that it's not yet supported in JGit – Alaa Nassef Feb 15 '17 at 20:21