Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JGIT validate if repository is valid

Is there a method to kill a clone operation mid-stream? I will just use the cloning to validate the repository? Is there any other way to test if the remote url/repository is valid?

like image 625
code-red Avatar asked Jun 14 '12 10:06

code-red


People also ask

How check if Git exists?

To check whether or not you have git installed, simply open a terminal window and type "git --version". If you've already followed the video Installing Git for Windows on a Windows Machine you'll see a message like "git version 1.9. 5. msysgit.

What is JGit used for?

JGit is a lightweight, pure Java library implementation of the Git version control system – including repository access routines, network protocols, and core version control algorithms. JGit is a relatively full-featured implementation of Git written in Java and is widely used in the Java community.

Can you use Git in Java?

If you want to use Git from within a Java program, there is a fully featured Git library called JGit. JGit is a relatively full-featured implementation of Git written natively in Java, and is widely used in the Java community.


2 Answers

you can invoke 'git ls-remote ' using JGIT. see at here

Sample code as below:

    final LsRemoteCommand lsCmd = new LsRemoteCommand(null);
    final List<String> repos = Arrays.asList(
            "https://github.com/MuchContact/java.git",
            "[email protected]:MuchContact/java.git");
    for (String gitRepo: repos){
        lsCmd.setRemote(gitRepo);
        System.out.println(lsCmd.call().toString());
    }
like image 144
Muco Avatar answered Sep 24 '22 16:09

Muco


I'm using the following heuristic (to be improved further):

private final static String INFO_REFS_PATH = "info/refs";

public static boolean isValidRepository(URIish repoUri) {
  if (repoUri.isRemote()) {
    return isValidRemoteRepository(repoUri);
  } else {
    return isValidLocalRepository(repoUri);
  }
}

private static boolean isValidLocalRepository(URIish repoUri) {
  boolean result;
  try {
    result = new FileRepository(repoUri.getPath()).getObjectDatabase().exists();
  } catch (IOException e) {
    result = false;
  }
  return result;
}

private static boolean isValidRemoteRepository(URIish repoUri) {
  boolean result;

  if (repoUri.getScheme().toLowerCase().startsWith("http") ) {
    String path = repoUri.getPath();
    String newPath = path.endsWith("/")? path + INFO_REFS_PATH : path + "/" + INFO_REFS_PATH;
    URIish checkUri = repoUri.setPath(newPath);

    InputStream ins = null;
    try {
      URLConnection conn = new URL(checkUri.toString()).openConnection();
      conn.setReadTimeout(NETWORK_TIMEOUT_MSEC);
      ins = conn.getInputStream();
      result = true;
    } catch (Exception e) {
      result = false;
    } finally {
      try { ins.close(); } catch (Exception e) { /* ignore */ }
    }

  } else if (repoUri.getScheme().toLowerCase().startsWith("ssh") ) {

    RemoteSession ssh = null;
    Process exec = null;

    try {
      ssh = SshSessionFactory.getInstance().getSession(repoUri, null, FS.detect(), 5000);
      exec = ssh.exec("cd " + repoUri.getPath() +"; git rev-parse --git-dir", 5000);

      Integer exitValue = null;
      do {
        try {
          exitValue = exec.exitValue();
        } catch (Exception e) { 
          try{Thread.sleep(1000);}catch(Exception ee){}
        }
      } while (exitValue == null);

      result = exitValue == 0;

    } catch (Exception e) {
      result = false;

    } finally {
      try { exec.destroy(); } catch (Exception e) { /* ignore */ }
      try { ssh.disconnect(); } catch (Exception e) { /* ignore */ }
    }

  } else {
    // TODO need to implement tests for other schemas
    result = true;
  }
  return result;
}

This works well with bare and non-bare repositories.

Please note that there seems to be a problem with the URIish.isRemote() method. When you create an URIish from a file-URL, the host is not null but an empty string! URIish.isRemote() however returns true, if the host field is not null...

EDIT: Added ssh support to the isValidRemoteRepository() method.

like image 20
Udo Avatar answered Sep 25 '22 16:09

Udo