如何使用java来操作git gitlab?_java 操作git

/**
* 移动(mv)
*/
public static void mv(String localPath, String sourcePath, String targetPath, File file) {
System.out.println(sourcePath);
try {
File newDir = new File(targetPath);
if (!newDir.exists()) {
newDir.mkdirs();
}
File newFile = new File(newDir, file.getName());
boolean success = file.renameTo(newFile);
if (success) {
add(localPath, “.”);
rm(localPath, sourcePath);
commit(localPath, “File moved to new directory”);
push(localPath);
} else {
// handle error
log.error(“文件移动异常!”);
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 状态(status) git status
*
* @throws Exception
*/
public static Map<String, String> status(String localPath) throws Exception {
Map<String, String> map = new HashMap<>();
Status status = openRpo(localPath).status().call();
map.put(“Added”, status.getAdded().toString());
map.put(“Changed”, status.getChanged().toString());
map.put(“Conflicting”, status.getConflicting().toString());
map.put(“ConflictingStageState”, status.getConflictingStageState().toString());
map.put(“IgnoredNotInIndex”, status.getIgnoredNotInIndex().toString());
map.put(“Missing”, status.getMissing().toString());
map.put(“Modified”, status.getModified().toString());
map.put(“Removed”, status.getRemoved().toString());
map.put(“UntrackedFiles”, status.getUntracked().toString());
map.put(“UntrackedFolders”, status.getUntrackedFolders().toString());
return map;
}

/**
* ===分支操作=
*/
/**
* 创建分支(Create Branch) git branch dev
*
* @throws Exception
*/
public static void branch(String localPath, String branchName) throws Exception {
openRpo(localPath).branchCreate()
.setName(branchName)
.call();
}

/**
* 删除分支(Delete Branch) git branch -d dev
*
* @throws Exception
*/
public static void delBranch(String localPath, String branchName) throws Exception {
openRpo(localPath).branchDelete()
.setBranchNames(branchName)
.call();
}

/**
* 切换分支(Checkout Branch) git checkout dev
*
* @throws Exception
*/
public static void checkoutBranch(String localPath, String branchName) throws Exception {
openRpo(localPath).checkout()
.setName(branchName)
.call();
}

/**
* 所有分支(BranchList) git branch
*
* @throws Exception
*/
public static List listBranch(String localPath) throws Exception {
return openRpo(localPath).branchList().call();
}

/**
* 合并分支(Merge Branch) git merge dev
*
* @throws Exception
*/
public static void mergeBranch(String localPath, String branchName, String commitMsg) throws Exception {
//切换分支获取分支信息存入Ref对象里
Ref refdev = openRpo(localPath).checkout().setName(branchName).call();
//切换回main分支
openRpo(localPath).checkout().setName(“main”).call();
// 合并目标分支
MergeResult mergeResult = openRpo(localPath).merge().include(refdev)
//同时提交
.setCommit(true)
// 分支合并策略NO_FF代表普通合并, FF代表快速合并
.setFastForward(MergeCommand.FastForwardMode.NO_FF)
.setMessage(Optional.ofNullable(commitMsg).orElse(“master Merge”))
.call();
}

/**
* 远端仓库操作(Repository)======
*/
/**
* 推送(Push) git push origin master
*
* @throws Exception
*/
public static void push(String localPath) throws Exception {
openRpo(localPath).push()
//设置推送的URL名称如"origin"
.setRemote(“origin”)
//设置需要推送的分支,如果远端没有则创建
.setRefSpecs(new RefSpec(“main”))
//身份验证
.setCredentialsProvider(provider)
.call();
}

public static void push(String localPath, String branch) throws Exception {
openRpo(localPath).push()
//设置推送的URL名称如"origin"
.setRemote(“origin”)
//设置需要推送的分支,如果远端没有则创建
.setRefSpecs(new RefSpec(branch))
//身份验证
.setCredentialsProvider(provider)
.call();
}

/**
* /拉取(Pull) git pull origin
*
* @throws Exception
*/
public static void pull(String localPath, String remotePath) throws Exception {
//判断localPath是否存在,不存在调用clone方法
File directory = new File(localPath);
if (!directory.exists()) {
gitClone(localPath, remotePath, “main”);
}
openRpo(localPath).pull()
.setRemoteBranchName(“main”)
.setCredentialsProvider(provider)
.call();
}

public static void pull(String localPath, String remotePath, String branch) throws Exception {
//判断localPath是否存在,不存在调用clone方法
File directory = new File(localPath);
if (!directory.exists()) {
gitClone(localPath, remotePath, branch);
}
openRpo(localPath).pull()
.setRemoteBranchName(branch)
.setCredentialsProvider(provider)
.call();
}

/**
* 克隆(Clone) git clone https://xxx.git
*
* @throws Exception
*/
public static void gitClone(String localPath, String remotePath, String branch) throws Exception {
//克隆
Git git = Git.cloneRepository()
.setURI(remotePath)
.setDirectory(new File(localPath))
.setCredentialsProvider(provider)
//设置是否克隆子仓库
.setCloneSubmodules(true)
//设置克隆分支
.setBranch(branch)
.call();
//关闭源,以释放本地仓库锁
git.getRepository().close();
git.close();

}
}

(5)直接在对应的业务代码中调用上述工具类中的方法即可。
如果有些朋友还需要集成其他命令,可以了解下这个开源项目:https://github.com/centic9/jgit-cookbook,git的几乎所有操作都在这里面集成了。
2.另外一种方式大家想必也猜到了。既然可以在命令行中执行git命令来操作git,那么能不能通过使用java控制终端来执行git命令呢?答案是肯定的,但前提是需要在你电脑或服务器上先安装好git客户端。
下面是示例代码:

@Slf4j
public class GitUtils {
public static String executeCommand(String command) {
StringBuilder output = new StringBuilder();
Process process;
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append(“\n”);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return output.toString();
}

public static void cloneRepository(String remotePath, String localPath) {
String command = "git clone " + remotePath + " " + localPath;
String output = executeCommand(command);
System.out.println(output);
}

public static void pull(String localPath,String branch) {
String command = "git -C " + localPath + " pull origin "+branch;
String output = executeCommand(command);
System.out.println(output);
}

public static void checkoutBranch(String localPath, String branchName) {
String command = "git -C " + localPath + " checkout " + branchName;
String output = executeCommand(command);
System.out.println(output);
}

public static void commit(String localPath, String message) {
String command = “git -C " + localPath + " commit -a -m “” + message + “””;
String output = executeCommand(command);
System.out.println(output);
}

public static void add(String localPath) {
String command = “git -C " + localPath + " add .”;
String output = executeCommand(command);
System.out.println(output);
}

public static void push(String localPath) {
String command = “git -C " + localPath + " push”;
String output = executeCommand(command);
System.out.println(output);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
img

图片转存中…(img-RVGK6CQG-1712575473177)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
[外链图片转存中…(img-LUh6KW30-1712575473178)]

  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值