Eclipse外掛開發之jgit下載與更新程式碼
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;
}
}