2014-10-03 90 views
0

我正在嘗試使用jgit從git存儲庫中提取更改。jgit pull問題頭部分離

我面臨着頭部卸下了錯誤的小問題,當我做一拉

我已經在這裏計算器閱讀其他的答案。嘗試這些解決方案似乎沒有幫助。

我有2個遠程分支機構 1.分期 2.主

下面是示例代碼:

public static void main(String [] args){ 
    final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git"; 
    final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar; 

    try{ 
     //setting up local repository as just testNewLiferRepo 
     String repoName = localRepositoryPath + "testNewLiferRepo"; 
     File f = new File(repoName); 
     Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git"); 
     ObjectId oldObjid = null; 
     Git git = null; 
     if (f.exists()) { 
      oldObjid = localRepo.resolve(Constants.HEAD); 
      git = Git.open(f); 
      git.pull().call(); 
      ObjectId currentObjid = localRepo.resolve(Constants.HEAD); 
      git.getRepository().close(); 
     } else { 
      git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call(); 
      ObjectId currentObjid =localRepo.resolve(Constants.HEAD); 
      if(!localRepo.getBranch().equalsIgnoreCase("staging")){ 
       git.checkout().setName("refs/remotes/origin/staging").setForce(true).call(); 
      } 
      git.getRepository().close(); 
     } 
    } catch (InvalidRemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (TransportException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (GitAPIException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (RevisionSyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (AmbiguousObjectException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IncorrectObjectTypeException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

您可以在存儲庫來測試拉更新在testXML文件版本。

任何指針的高度讚賞

感謝

+0

請聯繫您已經閱讀其他的解決方案,讓其他人知道你已經嘗試過的解決方案。 – 2014-10-03 00:09:56

回答

0

看起來像我有我的發言交換。這裏是一個新的解決方案

public static void main(String [] args){ 
    final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git"; 
    final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar; 

    SSHSessionFactory sessionFactory = new SSHSessionFactory(); 
    sessionFactory.Initialize(); 
    SshSessionFactory.setInstance(sessionFactory); 
    try{ 
     //setting up local repository as just testNewLiferRepo 
     String repoName = localRepositoryPath + "testNewLiferRepo"; 
     File f = new File(repoName); 
     Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git"); 
     ObjectId oldObjid = null; 
     Git git = null; 
     if (f.exists()) { 
      oldObjid = localRepo.resolve(Constants.HEAD); 
      git = Git.open(f); 
      Map<String,Ref> m = git.getRepository().getAllRefs(); 
      System.out.println("Current Branch: "+git.getRepository().getBranch()); 
      if(git.getRepository().isValidRefName("refs/remotes/origin/staging")){ 
       System.out.println("Valid"); 
      } 
      git.pull().call(); 
      git.checkout().setName("staging").call(); 
      ObjectId currentObjid = localRepo.resolve(Constants.HEAD); 

      git.getRepository().close(); 
     } else { 
      git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call(); 
      ObjectId currentObjid =localRepo.resolve(Constants.HEAD); 
      if(!localRepo.getBranch().equalsIgnoreCase("staging")){ 
       git.branchCreate().setForce(true).setName("staging").setStartPoint("refs/remotes/origin/staging").call(); 
      } 
      git.checkout().setName("staging").call(); 
      git.getRepository().close(); 
     } 
    } catch (InvalidRemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (TransportException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (GitAPIException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (RevisionSyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (AmbiguousObjectException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IncorrectObjectTypeException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
}