org.eclipse.jgit.util.FileUtils.delete()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(157)

本文整理了Java中org.eclipse.jgit.util.FileUtils.delete()方法的一些代码示例,展示了FileUtils.delete()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.delete()方法的具体详情如下:
包路径:org.eclipse.jgit.util.FileUtils
类名称:FileUtils
方法名:delete

FileUtils.delete介绍

[英]Delete file or empty folder
[中]删除文件或空文件夹

代码示例

代码示例来源:origin: spring-cloud/spring-cloud-config

public static String prepareLocalSvnRepo(String sourceDir, String checkoutDir) throws Exception {
  File sourceDirFile = new File(sourceDir);
  sourceDirFile.mkdirs();
  File local = new File(checkoutDir);
  if (local.exists()) {
    FileUtils.delete(local, FileUtils.RECURSIVE);
  }
  local.mkdirs();
  FileSystemUtils.copyRecursively(sourceDirFile, local);
  return StringUtils.cleanPath("file:///" + local.getAbsolutePath());
}

代码示例来源:origin: spring-cloud/spring-cloud-config

private void deleteBaseDirIfExists() {
  if (getBasedir().exists()) {
    for (File file : getBasedir().listFiles()) {
      try {
        FileUtils.delete(file, FileUtils.RECURSIVE);
      }
      catch (IOException e) {
        throw new IllegalStateException("Failed to initialize base directory",
            e);
      }
    }
  }
}

代码示例来源:origin: spring-cloud/spring-cloud-config

File dest = new File(buildDir + repoPath);
if (dest.exists()) {
  FileUtils.delete(dest, FileUtils.RECURSIVE | FileUtils.RETRY);
if (git.exists()) {
  if (dotGit.exists()) {
    FileUtils.delete(dotGit, FileUtils.RECURSIVE);
File local = new File(checkoutDir);
if (local.exists()) {
  FileUtils.delete(local, FileUtils.RECURSIVE);

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * Delete a file. Throws an exception if delete fails.
 *
 * @param f
 *            a {@link java.io.File} object.
 * @throws java.io.IOException
 *             this may be a Java7 subclass with detailed information
 * @since 3.3
 */
public void delete(File f) throws IOException {
  FileUtils.delete(f);
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
   * Remove the <code>.keep</code> file that holds this pack in place.
   *
   * @throws java.io.IOException
   *             if deletion of .keep file failed
   */
  public void unlock() throws IOException {
    FileUtils.delete(keepFile);
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * Delete file or empty folder
 *
 * @param f
 *            {@code File} to be deleted
 * @throws java.io.IOException
 *             if deletion of {@code f} fails. This may occur if {@code f}
 *             didn't exist when the method was called. This can therefore
 *             cause java.io.IOExceptions during race conditions when
 *             multiple concurrent threads all try to delete the same file.
 */
public static void delete(File f) throws IOException {
  delete(f, NONE);
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private void deleteChildren(File file) throws IOException {
    File[] files = file.listFiles();
    if (files == null) {
      return;
    }
    for (File child : files) {
      FileUtils.delete(child, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING
          | FileUtils.IGNORE_ERRORS);
    }
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private void writeCommitMsg(File msgFile, String msg) throws IOException {
  if (msg != null) {
    try (FileOutputStream fos = new FileOutputStream(msgFile)) {
      fos.write(msg.getBytes(UTF_8));
    }
  } else {
    FileUtils.delete(msgFile, FileUtils.SKIP_MISSING);
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private void detectSymlinkSupport() {
  File tempFile = null;
  try {
    tempFile = File.createTempFile("tempsymlinktarget", ""); //$NON-NLS-1$ //$NON-NLS-2$
    File linkName = new File(tempFile.getParentFile(), "tempsymlink"); //$NON-NLS-1$
    createSymLink(linkName, tempFile.getPath());
    supportSymlinks = Boolean.TRUE;
    linkName.delete();
  } catch (IOException | UnsupportedOperationException
      | InternalError e) {
    supportSymlinks = Boolean.FALSE;
  } finally {
    if (tempFile != null)
      try {
        FileUtils.delete(tempFile);
      } catch (IOException e) {
        throw new RuntimeException(e); // panic
      }
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * Unlock the given file.
 * <p>
 * This method can be used for recovering from a thrown
 * {@link org.eclipse.jgit.errors.LockFailedException} . This method does
 * not validate that the lock is or is not currently held before attempting
 * to unlock it.
 *
 * @param file
 *            a {@link java.io.File} object.
 * @return true if unlocked, false if unlocking failed
 */
public static boolean unlock(File file) {
  final File lockFile = getLockFile(file);
  final int flags = FileUtils.RETRY | FileUtils.SKIP_MISSING;
  try {
    FileUtils.delete(lockFile, flags);
  } catch (IOException ignored) {
    // Ignore and return whether lock file still exists
  }
  return !lockFile.exists();
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private void cleanup() {
  try {
    if (directory != null) {
      if (!directoryExistsInitially) {
        FileUtils.delete(directory, FileUtils.RECURSIVE
            | FileUtils.SKIP_MISSING | FileUtils.IGNORE_ERRORS);
      } else {
        deleteChildren(directory);
      }
    }
    if (gitDir != null) {
      if (!gitDirExistsInitially) {
        FileUtils.delete(gitDir, FileUtils.RECURSIVE
            | FileUtils.SKIP_MISSING | FileUtils.IGNORE_ERRORS);
      } else {
        deleteChildren(directory);
      }
    }
  } catch (IOException e) {
    // Ignore; this is a best-effort cleanup in error cases, and
    // IOException should not be raised anyway
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * Write the given heads to a file in the git directory.
 *
 * @param heads
 *            a list of object ids to write or null if the file should be
 *            deleted.
 * @param filename
 * @throws FileNotFoundException
 * @throws IOException
 */
private void writeHeadsFile(List<? extends ObjectId> heads, String filename)
    throws FileNotFoundException, IOException {
  File headsFile = new File(getDirectory(), filename);
  if (heads != null) {
    try (OutputStream bos = new BufferedOutputStream(
        new FileOutputStream(headsFile))) {
      for (ObjectId id : heads) {
        id.copyTo(bos);
        bos.write('\n');
      }
    }
  } else {
    FileUtils.delete(headsFile, FileUtils.SKIP_MISSING);
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * Recursively delete the *contents* of path, but leave path as an empty
 * directory
 *
 * @param path
 *            the path to clean
 * @throws IOException
 */
private void deinit(String path) throws IOException {
  File dir = new File(repo.getWorkTree(), path);
  if (!dir.isDirectory()) {
    throw new JGitInternalException(MessageFormat.format(
        JGitText.get().expectedDirectoryNotSubmodule, path));
  }
  final File[] ls = dir.listFiles();
  if (ls != null) {
    for (int i = 0; i < ls.length; i++) {
      FileUtils.delete(ls[i], RECURSIVE);
    }
  }
}

代码示例来源:origin: FlowCI/flow-platform

@Override
public File clone(String branch, Set<String> checkoutFiles, ProgressMonitor monitor) throws GitException {
  checkGitUrl();
  File gitDir = getGitPath().toFile();
  // delete existing git folder since may have conflict
  if (gitDir.exists()) {
    try {
      FileUtils.delete(gitDir.getParentFile(), FileUtils.RECURSIVE);
    } catch (IOException e) {
      // IO error on delete existing folder
    }
  }
  // git init
  initGit(checkoutFiles);
  pull(branch, monitor);
  return gitDir;
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * Delete the preserved directory including all pack files within
 */
private void prunePreserved() {
  if (pconfig != null && pconfig.isPrunePreserved()) {
    try {
      FileUtils.delete(repo.getObjectDatabase().getPreservedDirectory(),
          FileUtils.RECURSIVE | FileUtils.RETRY | FileUtils.SKIP_MISSING);
    } catch (IOException e) {
      // Deletion of the preserved pack files failed. Silently return.
    }
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private void writeRewrittenHashes() throws RevisionSyntaxException,
    IOException, RefNotFoundException {
  File currentCommitFile = rebaseState.getFile(CURRENT_COMMIT);
  if (!currentCommitFile.exists())
    return;
  ObjectId headId = getHead().getObjectId();
  // getHead() checks for null
  assert headId != null;
  String head = headId.getName();
  String currentCommits = rebaseState.readFile(CURRENT_COMMIT);
  for (String current : currentCommits.split("\n")) //$NON-NLS-1$
    RebaseState
        .createFile(rebaseState.getRewrittenDir(), current, head);
  FileUtils.delete(currentCommitFile);
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * Deletes old pack file, unless 'preserve-oldpacks' is set, in which case it
 * moves the pack file to the preserved directory
 *
 * @param packFile
 * @param packName
 * @param ext
 * @param deleteOptions
 * @throws IOException
 */
private void removeOldPack(File packFile, String packName, PackExt ext,
    int deleteOptions) throws IOException {
  if (pconfig != null && pconfig.isPreserveOldPacks()) {
    File oldPackDir = repo.getObjectDatabase().getPreservedDirectory();
    FileUtils.mkdir(oldPackDir, true);
    String oldPackName = "pack-" + packName + ".old-" + ext.getExtension();  //$NON-NLS-1$ //$NON-NLS-2$
    File oldPackFile = new File(oldPackDir, oldPackName);
    FileUtils.rename(packFile, oldPackFile);
  } else {
    FileUtils.delete(packFile, deleteOptions);
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private RebaseResult finishRebase(RevCommit finalHead,
    boolean lastStepIsForward) throws IOException, GitAPIException {
  String headName = rebaseState.readFile(HEAD_NAME);
  updateHead(headName, finalHead, upstreamCommit);
  boolean stashConflicts = autoStashApply();
  getRepository().autoGC(monitor);
  FileUtils.delete(rebaseState.getDir(), FileUtils.RECURSIVE);
  if (stashConflicts)
    return RebaseResult.STASH_APPLY_CONFLICTS_RESULT;
  if (lastStepIsForward || finalHead == null)
    return RebaseResult.FAST_FORWARD_RESULT;
  return RebaseResult.OK_RESULT;
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

private RevCommit checkoutCurrentHead() throws IOException, NoHeadException {
  ObjectId headTree = repo.resolve(Constants.HEAD + "^{tree}"); //$NON-NLS-1$
  if (headTree == null)
    throw new NoHeadException(
        JGitText.get().cannotRebaseWithoutCurrentHead);
  DirCache dc = repo.lockDirCache();
  try {
    DirCacheCheckout dco = new DirCacheCheckout(repo, dc, headTree);
    dco.setFailOnConflict(false);
    dco.setProgressMonitor(monitor);
    boolean needsDeleteFiles = dco.checkout();
    if (needsDeleteFiles) {
      List<String> fileList = dco.getToBeDeleted();
      for (String filePath : fileList) {
        File fileToDelete = new File(repo.getWorkTree(), filePath);
        if (repo.getFS().exists(fileToDelete))
          FileUtils.delete(fileToDelete, FileUtils.RECURSIVE
              | FileUtils.RETRY);
      }
    }
  } finally {
    dc.unlock();
  }
  try (RevWalk rw = new RevWalk(repo)) {
    RevCommit commit = rw.parseCommit(repo.resolve(Constants.HEAD));
    return commit;
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

@Override
  public RemoteFile readFileWithMode(String uri, String ref, String path)
      throws GitAPIException, IOException {
    File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$
    try (Git git = Git.cloneRepository().setBare(true).setDirectory(dir)
        .setURI(uri).call()) {
      Repository repo = git.getRepository();
      ObjectId refCommitId = sha1(uri, ref);
      if (refCommitId == null) {
        throw new InvalidRefNameException(MessageFormat
            .format(JGitText.get().refNotResolved, ref));
      }
      RevCommit commit = repo.parseCommit(refCommitId);
      TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree());
      // TODO(ifrade): Cope better with big files (e.g. using
      // InputStream instead of byte[])
      return new RemoteFile(
          tw.getObjectReader().open(tw.getObjectId(0))
              .getCachedBytes(Integer.MAX_VALUE),
          tw.getFileMode(0));
    } finally {
      FileUtils.delete(dir, FileUtils.RECURSIVE);
    }
  }
}

相关文章