Eclipse插件开发之jgit下载与更新代码
需要依赖的插件:
org.eclipse.jgit,
org.eclipse.egit
- 添加git图标
需要的扩展点:org.eclipse.ui.commands
org.eclipse.ui.handlers
org.eclipse.ui.menus
rg.eclipse.ui.bindings
1.1添加命令
Id – 命令的id,通过这个id可以绑定很多内容
<extension
point="org.eclipse.ui.commands">
<category
name="Sample Category"
id="Test.commands.category">
</category>
<command
name="Sample Command"
categoryId="Test.commands.category"
id="com.djyos.dide.commands.gitCommand">
</command>
</extension>
1.2添加响应处理
commandId – 与以上的id绑定
class – 点击后相应的类
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="com.djyos.dide.commands.gitCommand"
class="com.djyos.dide.ui.git.GitUpdateHandler">
</handler>
</extension>
1.3添加git图标菜单
commandId – 与以上的id绑定
icon – git图标
tooltip – 鼠标移动到图标时的提示
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
<toolbar
id="Test.toolbars.sampleToolbar">
<command
commandId="com.djyos.dide.commands.gitCommand"
icon="icons/ovr16/git_update.png"
tooltip="更新Git上的Djyos源码"
id="Test.toolbars.sampleCommand">
</command>
</toolbar>
</menuContribution>
</extension>
1.4绑定快捷键
commandId – 与以上的id绑定
sequence – 快捷键序列
<extension
point="org.eclipse.ui.bindings">
<key
commandId="com.djyos.dide.commands.gitCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+6"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration>
</key>
</extension>
- 远程下载git源码
remotePath – 远程git网址
srcFile – 本地存储路径
branch – 下载的分支
CloneCommand cc = Git.cloneRepository().setURI(remotePath);
try {
cc.setBranch(branch).setDirectory(srcFile).call();
} 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();
}
- 更新本地代码
djysrcPath – 本地git路径
git.pull().call() — 更新远程代码到本地
Git git = Git.open(new File(djysrcPath + "/.git"));
String branchName = git.getRepository().getBranch();
if (!branchName.equals("release")) { git.checkout().setCreateBranch(true).setName("release").call();
git.pull().call();
} else {
git.pull().call();
}
- 获取远程信息
Branch – 需要获取的分支
FetchResult fetchResult = gitLocal.fetch().call();
TrackingRefUpdate refUpdate= fetchResult
.getTrackingRefUpdate("refs/remotes/origin/branch");
- 获取本地git源码版本
gitLocalFile – 本地git路径
remoteVersion – 本地git版本
File file = new File(gitLocalFile + "/FETCH_HEAD");
BufferedReader buf = null;
String line = null;
String releaseMsg = null;
if (file.exists()) {
try {
buf = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
while ((line = buf.readLine()) != null) {
if (line.contains("release")) {
releaseMsg = line;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (buf != null) {
try {
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (releaseMsg != null) {
String remoteVersion = releaseMsg.split("\\s+")[0];
return remoteVersion;
}
}
本文详细介绍了如何在Eclipse中使用jGit插件进行代码管理,包括添加自定义命令、响应处理、菜单图标及快捷键绑定,以及如何实现远程下载、本地更新和版本查询等功能。
1091

被折叠的 条评论
为什么被折叠?



