Java tutorial
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.ocsoft.olivia.models.project; import java.io.IOException; import javax.annotation.Nullable; import org.eclipse.jgit.api.AddCommand; import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.NoHeadException; import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.frows.genericfile.v0_3_2.GenericFile; import org.ocsoft.olivia.logger.LogData; import org.ocsoft.olivia.logger.OliviaLogger; import org.ocsoft.olivia.logger.def.BasicLogDef; import org.ocsoft.olivia.models.file.OliviaFile; /** * ?Git?????.<br> * JGit?????. * @author tohhy */ public class GitManager { /** * ?????. */ private final OliviaProject project; /** * ???????. */ private Repository repo; /** * JGit?. */ @Nullable private Git git; /** * .gitignore. */ private final OliviaFile gitIgnoreFile; /** * .gitignore?. */ private String gitIgnoreString = ".olivia/"; /** * ????GitManager??. * @param proj ?? */ GitManager(OliviaProject proj) { if (proj == null) throw new IllegalArgumentException("project must not be null"); this.project = proj; this.gitIgnoreFile = OliviaFile.create(proj.getProjectDir(), ".gitignore"); try { setUpGitIgnore(); } catch (IOException e) { OliviaLogger.catchException(e); } } /** * * @throws IOException */ private void setUpGitIgnore() throws IOException { if (gitIgnoreFile.exists()) { gitIgnoreString = gitIgnoreFile.getOrigin().getReader().readAsString(); } else { gitIgnoreFile.getOrigin().getHandler().createEmptyFile(); storeGitIgnore(); } } /** * * @throws IOException */ private void storeGitIgnore() throws IOException { gitIgnoreFile.getOrigin().getWriter().appendString(gitIgnoreString, "UTF-8"); } /** * * @throws GitAPIException */ public void init() throws GitAPIException { Git.init().setDirectory(project.getProjectDir().getOrigin().getFile()).call(); } /** * * @throws GitAPIException */ public void addAll() throws GitAPIException { Git git = getGitInstance(); AddCommand add = git.add(); add.addFilepattern(".").call(); } /** * * @throws IOException * @throws GitAPIException */ public void commit() throws GitAPIException { commit("commited by olivia"); } public void commit(String message) throws GitAPIException { Git git = getGitInstance(); CommitCommand commit = git.commit(); commit.setMessage(message).call(); } /** * */ public void addAndCommitAll() { addAndCommitAll("commited by olivia"); } /** * * @param message */ public void addAndCommitAll(String message) { try { addAll(); commit(message); } catch (Exception e) { OliviaLogger.log(BasicLogDef.UNEXPECTED_EXCEPTION, new LogData() { { exception(e); } }); } } /** * */ public void getLog() { try { Iterable<RevCommit> log = getGitInstance().log().call(); log.forEach((e) -> { e.getFullMessage(); }); //TODO } catch (GitAPIException e) { e.printStackTrace(); } } /** * ?.<br> * ?????????-1?. * @return ?UNIX * @throws GitAPIException */ public long getLastCommitedAt() throws GitAPIException { try { Iterable<RevCommit> log = getGitInstance().log().call(); RevCommit recent = log.iterator().next(); return ((long) recent.getCommitTime()) * 1000; } catch (NoHeadException e) { return -1; } } /** * * @return * @throws IOException */ private Repository getRepo() throws IOException { if (repo == null) repo = new FileRepository(getGitDir().getFile()); return repo; } /** * * @return * @throws IOException * @throws GitAPIException */ @Nullable private Git getGitInstance() throws GitAPIException { if (git == null) { try { git = new Git(getRepo()); } catch (IOException e) { OliviaLogger.catchException(e); return null; } } if (repo.isBare()) init(); return git; } /** * * @return */ public OliviaProject getProject() { return project; } /** * * @return */ public GenericFile getGitDir() { return new GenericFile(project.getProjectDir().getOrigin().getFile(), "/.git"); } /** * * @return */ public String getGitIgnoreString() { return gitIgnoreString; } /** * * @param gitIgnoreString */ public void setGitIgnoreString(String gitIgnoreString) { this.gitIgnoreString = gitIgnoreString; try { storeGitIgnore(); } catch (IOException e) { OliviaLogger.catchException(e); } } }