@Signature public boolean isBare() { return getWrappedObject().getRepository().isBare(); }
/** * Checks whether the working tree contains a .gitmodules file. That's a * hint that the repo contains submodules. * * @param repository * the repository to check * @return <code>true</code> if the working tree contains a .gitmodules file, * <code>false</code> otherwise. Always returns <code>false</code> * for bare repositories. * @throws java.io.IOException * @throws CorruptObjectException if any. * @since 3.6 */ public static boolean containsGitModulesFile(Repository repository) throws IOException { if (repository.isBare()) { return false; } File modulesFile = new File(repository.getWorkTree(), Constants.DOT_GIT_MODULES); return (modulesFile.exists()); }
/** * Get the index file location or {@code null} if repository isn't local. * * @return the index file location or {@code null} if repository isn't * local. * @throws org.eclipse.jgit.errors.NoWorkTreeException * if this is bare, which implies it has no working directory. * See {@link #isBare()}. */ @NonNull public File getIndexFile() throws NoWorkTreeException { if (isBare()) throw new NoWorkTreeException(); return indexFile; }
/** * Get the root directory of the working tree, where files are checked out * for viewing and editing. * * @return the root directory of the working tree, where files are checked * out for viewing and editing. * @throws org.eclipse.jgit.errors.NoWorkTreeException * if this is bare, which implies it has no working directory. * See {@link #isBare()}. */ @NonNull public File getWorkTree() throws NoWorkTreeException { if (isBare()) throw new NoWorkTreeException(); return workTree; }
private static Repository openMainRepo(File mainRepoDir) throws IOException { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository mainRepo = builder.setGitDir(new File(mainRepoDir.getAbsolutePath(), ".git")) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); if(mainRepo.isBare()) { throw new IllegalStateException("Repository at " + mainRepoDir + " should not be bare"); } return mainRepo; }
private static Repository openMainRepo(File mainRepoDir) throws IOException { FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository mainRepo = builder.setGitDir(new File(mainRepoDir.getAbsolutePath(), ".git")) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); if(mainRepo.isBare()) { throw new IllegalStateException("Repository at " + mainRepoDir + " should not be bare"); } return mainRepo; }
private static void addSubmodule(Repository mainRepo) throws GitAPIException { System.out.println("Adding submodule"); try (Git git = new Git(mainRepo)) { try (Repository subRepoInit = git.submoduleAdd(). setURI("https://github.com/github/testrepo.git"). setPath("testrepo"). call()) { if(subRepoInit.isBare()) { throw new IllegalStateException("Repository at " + subRepoInit.getDirectory() + " should not be bare"); } } } }
private static void addSubmodule(Repository mainRepo) throws GitAPIException { System.out.println("Adding submodule"); try (Git git = new Git(mainRepo)) { try (Repository subRepoInit = git.submoduleAdd(). setURI("https://github.com/github/testrepo.git"). setPath("testrepo"). call()) { if(subRepoInit.isBare()) { throw new IllegalStateException("Repository at " + subRepoInit.getDirectory() + " should not be bare"); } } } }
@Nullable private String readCommitMsgFile(String msgFilename) throws IOException { if (isBare() || getDirectory() == null) throw new NoWorkTreeException(); File mergeMsgFile = new File(getDirectory(), msgFilename); try { return RawParseUtils.decode(IO.readFully(mergeMsgFile)); } catch (FileNotFoundException e) { if (mergeMsgFile.exists()) { throw e; } // the file has disappeared in the meantime ignore it return null; } }
public static void main(String[] args) throws IOException, GitAPIException { File mainRepoDir = createRepository(); try (Repository mainRepo = openMainRepo(mainRepoDir)) { addSubmodule(mainRepo); FileRepositoryBuilder builder = new FileRepositoryBuilder(); try (Repository subRepo = builder.setGitDir(new File("testrepo/.git")) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build()) { if (subRepo.isBare()) { throw new IllegalStateException("Repository at " + subRepo.getDirectory() + " should not be bare"); } } } System.out.println("All done!"); // clean up here to not keep using more and more disk-space for these samples FileUtils.deleteDirectory(mainRepoDir); }
public static void main(String[] args) throws IOException, GitAPIException { File mainRepoDir = createRepository(); try (Repository mainRepo = openMainRepo(mainRepoDir)) { addSubmodule(mainRepo); FileRepositoryBuilder builder = new FileRepositoryBuilder(); try (Repository subRepo = builder.setGitDir(new File("testrepo/.git")) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build()) { if (subRepo.isBare()) { throw new IllegalStateException("Repository at " + subRepo.getDirectory() + " should not be bare"); } } } System.out.println("All done!"); // clean up here to not keep using more and more disk-space for these samples FileUtils.deleteDirectory(mainRepoDir); }
/** * Return the information stored in the file $GIT_DIR/MERGE_HEAD. In this * file operations triggering a merge will store the IDs of all heads which * should be merged together with HEAD. * * @return a list of commits which IDs are listed in the MERGE_HEAD file or * {@code null} if this file doesn't exist. Also if the file exists * but is empty {@code null} will be returned * @throws java.io.IOException * @throws org.eclipse.jgit.errors.NoWorkTreeException * if this is bare, which implies it has no working directory. * See {@link #isBare()}. */ @Nullable public List<ObjectId> readMergeHeads() throws IOException, NoWorkTreeException { if (isBare() || getDirectory() == null) throw new NoWorkTreeException(); byte[] raw = readGitDirectoryFile(Constants.MERGE_HEAD); if (raw == null) return null; LinkedList<ObjectId> heads = new LinkedList<>(); for (int p = 0; p < raw.length;) { heads.add(ObjectId.fromString(raw, p)); p = RawParseUtils .nextLF(raw, p + Constants.OBJECT_ID_STRING_LENGTH); } return heads; }
/** * Return the information stored in the file $GIT_DIR/ORIG_HEAD. * * @return object id from ORIG_HEAD file or {@code null} if this file * doesn't exist. Also if the file exists but is empty {@code null} * will be returned * @throws java.io.IOException * @throws org.eclipse.jgit.errors.NoWorkTreeException * if this is bare, which implies it has no working directory. * See {@link #isBare()}. */ @Nullable public ObjectId readOrigHead() throws IOException, NoWorkTreeException { if (isBare() || getDirectory() == null) throw new NoWorkTreeException(); byte[] raw = readGitDirectoryFile(Constants.ORIG_HEAD); return raw != null ? ObjectId.fromString(raw, 0) : null; }
/** * Return the information stored in the file $GIT_DIR/REVERT_HEAD. * * @return object id from REVERT_HEAD file or {@code null} if this file * doesn't exist. Also if the file exists but is empty {@code null} * will be returned * @throws java.io.IOException * @throws org.eclipse.jgit.errors.NoWorkTreeException * if this is bare, which implies it has no working directory. * See {@link #isBare()}. */ @Nullable public ObjectId readRevertHead() throws IOException, NoWorkTreeException { if (isBare() || getDirectory() == null) throw new NoWorkTreeException(); byte[] raw = readGitDirectoryFile(Constants.REVERT_HEAD); if (raw == null) return null; return ObjectId.fromString(raw, 0); }
/** * Return the information stored in the file $GIT_DIR/CHERRY_PICK_HEAD. * * @return object id from CHERRY_PICK_HEAD file or {@code null} if this file * doesn't exist. Also if the file exists but is empty {@code null} * will be returned * @throws java.io.IOException * @throws org.eclipse.jgit.errors.NoWorkTreeException * if this is bare, which implies it has no working directory. * See {@link #isBare()}. */ @Nullable public ObjectId readCherryPickHead() throws IOException, NoWorkTreeException { if (isBare() || getDirectory() == null) throw new NoWorkTreeException(); byte[] raw = readGitDirectoryFile(Constants.CHERRY_PICK_HEAD); if (raw == null) return null; return ObjectId.fromString(raw, 0); }
/** {@inheritDoc} */ @Override public RefUpdate newUpdate(String name, boolean detach) throws IOException { if (!repo.isBare() && name.indexOf('/') < 0 && !HEAD.equals(name)) { return bootstrap.newUpdate(name, detach); } if (conflictsWithBootstrap(name)) { return new AlwaysFailUpdate(this, name); } Ref r = exactRef(name); if (r == null) { r = new ObjectIdRef.Unpeeled(Storage.NEW, name, null); } boolean detaching = detach && r.isSymbolic(); if (detaching) { r = new ObjectIdRef.Unpeeled(LOOSE, name, r.getObjectId()); } RefTreeUpdate u = new RefTreeUpdate(this, r); if (detaching) { u.setDetachingSymbolicRef(); } return u; }
/** {@inheritDoc} */ @Override public Ref exactRef(String name) throws IOException { if (!repo.isBare() && name.indexOf('/') < 0 && !HEAD.equals(name)) { // Pass through names like MERGE_HEAD, ORIG_HEAD, FETCH_HEAD. return bootstrap.exactRef(name); } else if (conflictsWithBootstrap(name)) { return null; } boolean partial = false; Ref src = bootstrap.exactRef(txnCommitted); Scanner.Result c = refs; if (c == null || !c.refTreeId.equals(idOf(src))) { c = Scanner.scanRefTree(repo, src, prefixOf(name), false); partial = true; } Ref r = c.all.get(name); if (r != null && r.isSymbolic()) { r = c.sym.get(name); if (partial && r.getObjectId() == null) { // Attempting exactRef("HEAD") with partial scan will leave // an unresolved symref as its target e.g. refs/heads/master // was not read by the partial scan. Scan everything instead. return getRefs(ALL).get(name); } } return r; }
@Override public Transport open(URIish uri, Repository local, String remoteName) throws NoRemoteRepositoryException { File localPath = local.isBare() ? local.getDirectory() : local.getWorkTree(); File path = local.getFS().resolve(localPath, uri.getPath()); // If the reference is to a local file, C Git behavior says // assume this is a bundle, since repositories are directories. if (path.isFile()) return new TransportBundleFile(local, uri, path); File gitDir = RepositoryCache.FileKey.resolve(path, local.getFS()); if (gitDir == null) throw new NoRemoteRepositoryException(uri, JGitText.get().notFound); return new TransportLocal(local, uri, gitDir); }
? getRef().getName() : getRef().getLeaf().getName(); if (myName.startsWith(Constants.R_HEADS) && !getRepository().isBare()) {
private void addSubmodule(String name, String url, String path, String revision, List<CopyFile> copyfiles, List<LinkFile> linkfiles, Git git) throws GitAPIException, IOException { assert (!repo.isBare()); assert (git != null); if (!linkfiles.isEmpty()) { throw new UnsupportedOperationException( JGitText.get().nonBareLinkFilesNotSupported); } SubmoduleAddCommand add = git.submoduleAdd().setName(name).setPath(path) .setURI(url); if (monitor != null) add.setProgressMonitor(monitor); Repository subRepo = add.call(); if (revision != null) { try (Git sub = new Git(subRepo)) { sub.checkout().setName(findRef(revision, subRepo)).call(); } subRepo.close(); git.add().addFilepattern(path).call(); } for (CopyFile copyfile : copyfiles) { copyfile.copy(); git.add().addFilepattern(copyfile.dest).call(); } }