diff --git a/src/main/java/net/geedge/asw/module/app/service/IGitService.java b/src/main/java/net/geedge/asw/module/app/service/IGitService.java index 3c469df..a391418 100644 --- a/src/main/java/net/geedge/asw/module/app/service/IGitService.java +++ b/src/main/java/net/geedge/asw/module/app/service/IGitService.java @@ -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> listBranch(String workspaceId, String search); Map infoBranch(String workspaceId, String branchName); diff --git a/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java b/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java index b77d756..bd9b88c 100644 --- a/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java +++ b/src/main/java/net/geedge/asw/module/app/service/impl/GitServiceImpl.java @@ -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> listBranch(String workspaceId, String search) { List> 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 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> listApplication(String workspaceId, String branch, String q) { List> 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 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> 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> listApplicationCommit(String workspaceId, String branch, String applicationName, String file) { List> 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); diff --git a/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java b/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java index 46239aa..bd643dd 100644 --- a/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java +++ b/src/main/java/net/geedge/asw/module/workspace/service/impl/WorkspaceServiceImpl.java @@ -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