Eclipse插件开发之jgit下载与更新代码

本文详细介绍了如何在Eclipse中使用jGit插件进行代码管理,包括添加自定义命令、响应处理、菜单图标及快捷键绑定,以及如何实现远程下载、本地更新和版本查询等功能。

Eclipse插件开发之jgit下载与更新代码

                                                                                                                                                                                                           

需要依赖的插件:

org.eclipse.jgit

org.eclipse.egit

  1. 添加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>

 

  1. 远程下载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();

           }

  1. 更新本地代码

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();

}

  1. 获取远程信息

Branch – 需要获取的分支

FetchResult fetchResult = gitLocal.fetch().call();

TrackingRefUpdate refUpdate= fetchResult

.getTrackingRefUpdate("refs/remotes/origin/branch");

  1. 获取本地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;

                }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值