[Java]JGit用法总结
来源:互联网 发布:html实体编码转换 js 编辑:程序博客网 时间:2024/06/05 23:48
clone
public static void gitClone(String remoteUrl, File repoDir) { try { Git git = Git.cloneRepository() .setURI(remoteUrl) .setDirectory(repoDir) .call(); logger.info("Cloning from " + remoteUrl + " to " + git.getRepository()); } catch (Exception e) { logger.info(e.getMessage()); }}
checkout
checkout
要在/.git
目录中进行
public static void gitCheckout(File repoDir, String version) { File RepoGitDir = new File(repoDir.getAbsolutePath() + "/.git"); if (!RepoGitDir.exists()) { logger.info("Error! Not Exists : " + RepoGitDir.getAbsolutePath()); } else { Repository repo = null; try { repo = new FileRepository(RepoGitDir.getAbsolutePath()); Git git = new Git(repo); CheckoutCommand checkout = git.checkout(); checkout.setName(version); checkout.call(); logger.info("Checkout to " + version); PullCommand pullCmd = git.pull(); pullCmd.call(); logger.info("Pulled from remote repository to local repository at " + repo.getDirectory()); } catch (Exception e) { logger.info(e.getMessage() + " : " + RepoGitDir.getAbsolutePath()); } finally { if (repo != null) { repo.close(); } } }}
pull
pull
要在/.git
目录中进行
public static void gitPull(File repoDir) { File RepoGitDir = new File(repoDir.getAbsolutePath() + "/.git"); if (!RepoGitDir.exists()) { logger.info("Error! Not Exists : " + RepoGitDir.getAbsolutePath()); } else { Repository repo = null; try { repo = new FileRepository(RepoGitDir.getAbsolutePath()); Git git = new Git(repo); PullCommand pullCmd = git.pull(); pullCmd.call(); logger.info("Pulled from remote repository to local repository at " + repo.getDirectory()); } catch (Exception e) { logger.info(e.getMessage() + " : " + RepoGitDir.getAbsolutePath()); } finally { if (repo != null) { repo.close(); } } }}
show status
show status
要在/.git
目录中进行
public static void gitShowStatus(File repoDir) { File RepoGitDir = new File(repoDir.getAbsolutePath() + "/.git"); if (!RepoGitDir.exists()) { logger.info("Error! Not Exists : " + RepoGitDir.getAbsolutePath()); } else { Repository repo = null; try { repo = new FileRepository(RepoGitDir.getAbsolutePath()); Git git = new Git(repo); Status status = git.status().call(); logger.info("Git Change: " + status.getChanged()); logger.info("Git Modified: " + status.getModified()); logger.info("Git UncommittedChanges: " + status.getUncommittedChanges()); logger.info("Git Untracked: " + status.getUntracked()); } catch (Exception e) { logger.info(e.getMessage() + " : " + repoDir.getAbsolutePath()); } finally { if (repo != null) { repo.close(); } } }}
参考资料
JGit API:http://download.eclipse.org/jgit/site/3.7.0.201502260915-r/index.html
JGit Cookbook:https://github.com/centic9/jgit-cookbook
1 0
- [Java]JGit用法总结
- java使用jgit提交代码
- Java 使用JGit获取Git代码
- JGit Authentication JGit验证机制
- java 枚举用法总结
- Java ByteBuffer用法总结
- Java Static 用法总结
- [Java] this 用法总结
- java unmodifiedList用法总结
- java一些用法总结
- Java Static 用法总结
- Java RMI 用法总结
- Java ByteBuffer用法总结
- Java习惯用法总结
- Java习惯用法总结
- Java习惯用法总结
- Java习惯用法总结
- Java习惯用法总结
- 【Java学习之代码学习】 Prog28_打印出杨辉三角形的问题
- VR一体机技术系列三之生态
- 第十六周项目—学生成绩处理2
- SQLServer跨库查询--分布式查询 用openrowset连接远程SQL或插入数据
- 周末待看的资料
- [Java]JGit用法总结
- [Web前端技术教学][基于Web标准的网页设计与制作]教材的精选100单选试题及答案
- iOS添加圆角好方法
- Blog开业大吉
- WeakReference 在android中的应用 弱引用防止内存泄漏
- Mybatis映射文件写法学习
- Socket编程 (连接,发送消息) (Tcp、Udp) - Part1
- 第十六周 项目2:学生成绩处理(2)
- Oracle中用Exp命令导出指定用户下的部分表或数据