fix: git init 默认提交 README.md 文件,解决新仓库中没有分支问题

This commit is contained in:
shizhendong
2024-10-22 17:04:55 +08:00
parent a7d9c06388
commit 862b8ccd98
3 changed files with 59 additions and 41 deletions

View File

@@ -1,10 +1,14 @@
package net.geedge.asw.module.app.service;
import org.eclipse.jgit.lib.Repository;
import java.util.List;
import java.util.Map;
public interface IGitService {
Repository initRepository(String workspaceId);
List<Map<Object, Object>> listBranch(String workspaceId, String search);
Map<Object, Object> infoBranch(String workspaceId, String branchName);

View File

@@ -86,16 +86,14 @@ public class GitServiceImpl implements IGitService {
/**
* get git instance
*/
public Git getGitInstance(File repoDir) {
public Git getGitInstance(String workspaceId) {
File repoDir = this.getRepoDirPath(workspaceId);
try {
// 目录不存在,初始化裸仓库
if (!repoDir.exists()) {
log.info("[getGitInstance] [dir not exist] [init new repository] [path: {}]", repoDir);
return Git.init()
.setBare(true)
.setDirectory(repoDir)
.setInitialBranch("main")
.call();
Repository repo = this.initRepository(workspaceId);
return new Git(repo);
}
FileRepositoryBuilder builder = new FileRepositoryBuilder();
@@ -109,14 +107,45 @@ public class GitServiceImpl implements IGitService {
return new Git(repository);
} else {
log.info("[getGitInstance] [init new repository] [path: {}]", repoDir);
return Git.init()
Repository repo = this.initRepository(workspaceId);
return new Git(repo);
}
} catch (Exception e) {
log.error(e, "[getGitInstance] [error] [path: {}]", repoDir);
throw new RuntimeException(e);
}
}
@Override
public Repository initRepository(String workspaceId) {
File repoDir = this.getRepoDirPath(workspaceId);
try (
Git git = Git.init()
.setBare(true)
.setDirectory(repoDir)
.setInitialBranch("main")
.call();
}
Repository repository = git.getRepository();
ObjectInserter inserter = repository.getObjectDatabase().newInserter();
) {
ObjectId objectId = this.insertBlobFileToDatabase(repository, "".getBytes());
DirCacheEntry entry = new DirCacheEntry("README.md");
entry.setFileMode(FileMode.REGULAR_FILE);
entry.setObjectId(objectId);
DirCache dirCache = DirCache.newInCore();
DirCacheBuilder builder = dirCache.builder();
builder.add(entry);
builder.finish();
// commit
this.createCommit(repository, "main", dirCache.writeTree(inserter), "Initial commit");
return git.getRepository();
} catch (GitAPIException | IOException e) {
log.error(e, "[getGitInstance] [error] [path: {}]", repoDir);
log.error(e, "[initRepository] [git init error]");
throw new RuntimeException(e);
}
}
@@ -125,8 +154,7 @@ public class GitServiceImpl implements IGitService {
public List<Map<Object, Object>> listBranch(String workspaceId, String search) {
List<Map<Object, Object>> resultList = T.ListUtil.list(true);
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
Repository repository = git.getRepository();
String fullBranch = repository.getFullBranch();
@@ -179,8 +207,7 @@ public class GitServiceImpl implements IGitService {
@Override
public Map<Object, Object> newBranch(String workspaceId, String branchName, String ref) {
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
git.branchCreate()
.setName(branchName)
.setStartPoint(ref)
@@ -195,8 +222,7 @@ public class GitServiceImpl implements IGitService {
@Override
public void deleteBranch(String workspaceId, String branchName) {
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
git.branchDelete()
.setBranchNames(branchName)
.setForce(true)
@@ -211,8 +237,7 @@ public class GitServiceImpl implements IGitService {
public List<Map<Object, Object>> listApplication(String workspaceId, String branch, String q) {
List<Map<Object, Object>> resultList = T.ListUtil.list(true);
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
Repository repository = git.getRepository();
try (TreeWalk treeWalk = new TreeWalk(repository);
@@ -315,8 +340,7 @@ public class GitServiceImpl implements IGitService {
.put("branch", branch)
.build();
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
Repository repository = git.getRepository();
// 获取指定版本的最新ID
@@ -366,8 +390,7 @@ public class GitServiceImpl implements IGitService {
@Override
public void newApplication(String workspaceId, String branch, String applicationName) {
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
Repository repository = git.getRepository();
Map<String, ObjectId> filePathAndBlobIdMap = T.MapUtil.newHashMap(true);
@@ -438,8 +461,7 @@ public class GitServiceImpl implements IGitService {
@Override
public void deleteApplication(String workspaceId, String branch, String applicationName) {
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
Repository repository = git.getRepository();
try (ObjectInserter inserter = repository.getObjectDatabase().newInserter();
@@ -478,8 +500,7 @@ public class GitServiceImpl implements IGitService {
@Override
public void updateApplication(String workspaceId, String branch, String lastCommitId, String message, List<Map<String, String>> updateContent) {
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
Repository repository = git.getRepository();
// 分支最新 commitId
@@ -579,8 +600,7 @@ public class GitServiceImpl implements IGitService {
public List<Map<Object, Object>> listApplicationCommit(String workspaceId, String branch, String applicationName, String file) {
List<Map<Object, Object>> resultList = T.ListUtil.list(true);
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = this.getGitInstance(repoDir)) {
try (Git git = this.getGitInstance(workspaceId)) {
String filterPath = T.StrUtil.concat(true, "applications/", applicationName);
if (T.StrUtil.isNotEmpty(file)) {
filterPath = T.StrUtil.concat(true, filterPath, "/", file);
@@ -611,8 +631,7 @@ public class GitServiceImpl implements IGitService {
.put("path", path)
.build();
File repoDir = this.getRepoDirPath(workspaceId);
try (Git git = getGitInstance(repoDir)) {
try (Git git = getGitInstance(workspaceId)) {
Repository repository = git.getRepository();
try (TreeWalk treeWalk = new TreeWalk(repository);

View File

@@ -9,13 +9,12 @@ import net.geedge.asw.common.util.ASWException;
import net.geedge.asw.common.util.Constants;
import net.geedge.asw.common.util.RCode;
import net.geedge.asw.common.util.T;
import net.geedge.asw.module.app.service.IGitService;
import net.geedge.asw.module.workspace.dao.WorkspaceDao;
import net.geedge.asw.module.workspace.entity.WorkspaceEntity;
import net.geedge.asw.module.workspace.entity.WorkspaceMemberEntity;
import net.geedge.asw.module.workspace.service.IWorkspaceMemberService;
import net.geedge.asw.module.workspace.service.IWorkspaceService;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -29,6 +28,9 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
private final static Log log = Log.get();
@Autowired
private IGitService gitService;
@Autowired
private IWorkspaceService workspaceService;
@@ -72,16 +74,9 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
workspaceMemberService.save(member);
}
try {
Git.init()
.setBare(true)
.setDirectory(T.FileUtil.file(T.WebPathUtil.getRootPath(), "workspace", workspace.getId()))
.setInitialBranch("main")
.call();
} catch (GitAPIException e) {
log.error(e, "[saveWorkspace] [git init error]");
throw new RuntimeException(e);
}
// init repository
gitService.initRepository(workspace.getId());
return workspace;
}