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; package net.geedge.asw.module.app.service;
import org.eclipse.jgit.lib.Repository;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public interface IGitService { public interface IGitService {
Repository initRepository(String workspaceId);
List<Map<Object, Object>> listBranch(String workspaceId, String search); List<Map<Object, Object>> listBranch(String workspaceId, String search);
Map<Object, Object> infoBranch(String workspaceId, String branchName); Map<Object, Object> infoBranch(String workspaceId, String branchName);

View File

@@ -86,16 +86,14 @@ public class GitServiceImpl implements IGitService {
/** /**
* get git instance * get git instance
*/ */
public Git getGitInstance(File repoDir) { public Git getGitInstance(String workspaceId) {
File repoDir = this.getRepoDirPath(workspaceId);
try { try {
// 目录不存在,初始化裸仓库 // 目录不存在,初始化裸仓库
if (!repoDir.exists()) { if (!repoDir.exists()) {
log.info("[getGitInstance] [dir not exist] [init new repository] [path: {}]", repoDir); log.info("[getGitInstance] [dir not exist] [init new repository] [path: {}]", repoDir);
return Git.init() Repository repo = this.initRepository(workspaceId);
.setBare(true) return new Git(repo);
.setDirectory(repoDir)
.setInitialBranch("main")
.call();
} }
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
@@ -109,14 +107,45 @@ public class GitServiceImpl implements IGitService {
return new Git(repository); return new Git(repository);
} else { } else {
log.info("[getGitInstance] [init new repository] [path: {}]", repoDir); 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) .setBare(true)
.setDirectory(repoDir) .setDirectory(repoDir)
.setInitialBranch("main") .setInitialBranch("main")
.call(); .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) { } catch (GitAPIException | IOException e) {
log.error(e, "[getGitInstance] [error] [path: {}]", repoDir); log.error(e, "[initRepository] [git init error]");
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@@ -125,8 +154,7 @@ public class GitServiceImpl implements IGitService {
public List<Map<Object, Object>> listBranch(String workspaceId, String search) { public List<Map<Object, Object>> listBranch(String workspaceId, String search) {
List<Map<Object, Object>> resultList = T.ListUtil.list(true); List<Map<Object, Object>> resultList = T.ListUtil.list(true);
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
Repository repository = git.getRepository(); Repository repository = git.getRepository();
String fullBranch = repository.getFullBranch(); String fullBranch = repository.getFullBranch();
@@ -179,8 +207,7 @@ public class GitServiceImpl implements IGitService {
@Override @Override
public Map<Object, Object> newBranch(String workspaceId, String branchName, String ref) { public Map<Object, Object> newBranch(String workspaceId, String branchName, String ref) {
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
git.branchCreate() git.branchCreate()
.setName(branchName) .setName(branchName)
.setStartPoint(ref) .setStartPoint(ref)
@@ -195,8 +222,7 @@ public class GitServiceImpl implements IGitService {
@Override @Override
public void deleteBranch(String workspaceId, String branchName) { public void deleteBranch(String workspaceId, String branchName) {
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
git.branchDelete() git.branchDelete()
.setBranchNames(branchName) .setBranchNames(branchName)
.setForce(true) .setForce(true)
@@ -211,8 +237,7 @@ public class GitServiceImpl implements IGitService {
public List<Map<Object, Object>> listApplication(String workspaceId, String branch, String q) { public List<Map<Object, Object>> listApplication(String workspaceId, String branch, String q) {
List<Map<Object, Object>> resultList = T.ListUtil.list(true); List<Map<Object, Object>> resultList = T.ListUtil.list(true);
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
Repository repository = git.getRepository(); Repository repository = git.getRepository();
try (TreeWalk treeWalk = new TreeWalk(repository); try (TreeWalk treeWalk = new TreeWalk(repository);
@@ -315,8 +340,7 @@ public class GitServiceImpl implements IGitService {
.put("branch", branch) .put("branch", branch)
.build(); .build();
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
Repository repository = git.getRepository(); Repository repository = git.getRepository();
// 获取指定版本的最新ID // 获取指定版本的最新ID
@@ -366,8 +390,7 @@ public class GitServiceImpl implements IGitService {
@Override @Override
public void newApplication(String workspaceId, String branch, String applicationName) { public void newApplication(String workspaceId, String branch, String applicationName) {
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
Repository repository = git.getRepository(); Repository repository = git.getRepository();
Map<String, ObjectId> filePathAndBlobIdMap = T.MapUtil.newHashMap(true); Map<String, ObjectId> filePathAndBlobIdMap = T.MapUtil.newHashMap(true);
@@ -438,8 +461,7 @@ public class GitServiceImpl implements IGitService {
@Override @Override
public void deleteApplication(String workspaceId, String branch, String applicationName) { public void deleteApplication(String workspaceId, String branch, String applicationName) {
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
Repository repository = git.getRepository(); Repository repository = git.getRepository();
try (ObjectInserter inserter = repository.getObjectDatabase().newInserter(); try (ObjectInserter inserter = repository.getObjectDatabase().newInserter();
@@ -478,8 +500,7 @@ public class GitServiceImpl implements IGitService {
@Override @Override
public void updateApplication(String workspaceId, String branch, String lastCommitId, String message, List<Map<String, String>> updateContent) { 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(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
Repository repository = git.getRepository(); Repository repository = git.getRepository();
// 分支最新 commitId // 分支最新 commitId
@@ -579,8 +600,7 @@ public class GitServiceImpl implements IGitService {
public List<Map<Object, Object>> listApplicationCommit(String workspaceId, String branch, String applicationName, String file) { public List<Map<Object, Object>> listApplicationCommit(String workspaceId, String branch, String applicationName, String file) {
List<Map<Object, Object>> resultList = T.ListUtil.list(true); List<Map<Object, Object>> resultList = T.ListUtil.list(true);
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = this.getGitInstance(workspaceId)) {
try (Git git = this.getGitInstance(repoDir)) {
String filterPath = T.StrUtil.concat(true, "applications/", applicationName); String filterPath = T.StrUtil.concat(true, "applications/", applicationName);
if (T.StrUtil.isNotEmpty(file)) { if (T.StrUtil.isNotEmpty(file)) {
filterPath = T.StrUtil.concat(true, filterPath, "/", file); filterPath = T.StrUtil.concat(true, filterPath, "/", file);
@@ -611,8 +631,7 @@ public class GitServiceImpl implements IGitService {
.put("path", path) .put("path", path)
.build(); .build();
File repoDir = this.getRepoDirPath(workspaceId); try (Git git = getGitInstance(workspaceId)) {
try (Git git = getGitInstance(repoDir)) {
Repository repository = git.getRepository(); Repository repository = git.getRepository();
try (TreeWalk treeWalk = new TreeWalk(repository); 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.Constants;
import net.geedge.asw.common.util.RCode; import net.geedge.asw.common.util.RCode;
import net.geedge.asw.common.util.T; 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.dao.WorkspaceDao;
import net.geedge.asw.module.workspace.entity.WorkspaceEntity; import net.geedge.asw.module.workspace.entity.WorkspaceEntity;
import net.geedge.asw.module.workspace.entity.WorkspaceMemberEntity; import net.geedge.asw.module.workspace.entity.WorkspaceMemberEntity;
import net.geedge.asw.module.workspace.service.IWorkspaceMemberService; import net.geedge.asw.module.workspace.service.IWorkspaceMemberService;
import net.geedge.asw.module.workspace.service.IWorkspaceService; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -29,6 +28,9 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
private final static Log log = Log.get(); private final static Log log = Log.get();
@Autowired
private IGitService gitService;
@Autowired @Autowired
private IWorkspaceService workspaceService; private IWorkspaceService workspaceService;
@@ -72,16 +74,9 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
workspaceMemberService.save(member); workspaceMemberService.save(member);
} }
try { // init repository
Git.init() gitService.initRepository(workspace.getId());
.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);
}
return workspace; return workspace;
} }