You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Clean.java 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (C) 2016, Ned Twigg <ned.twigg@diffplug.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import java.io.IOException;
  12. import java.text.MessageFormat;
  13. import java.util.Set;
  14. import org.eclipse.jgit.api.Git;
  15. import org.eclipse.jgit.api.errors.GitAPIException;
  16. import org.eclipse.jgit.errors.NoWorkTreeException;
  17. import org.eclipse.jgit.pgm.internal.CLIText;
  18. import org.kohsuke.args4j.Option;
  19. @Command(common = true, usage = "usage_Clean")
  20. class Clean extends TextBuiltin {
  21. @Option(name = "-d", usage = "usage_removeUntrackedDirectories")
  22. private boolean dirs = false;
  23. @Option(name = "--force", aliases = {
  24. "-f" }, usage = "usage_forceClean")
  25. private boolean force = false;
  26. @Option(name = "--dryRun", aliases = { "-n" })
  27. private boolean dryRun = false;
  28. /** {@inheritDoc} */
  29. @Override
  30. protected void run() {
  31. try (Git git = new Git(db)) {
  32. boolean requireForce = git.getRepository().getConfig()
  33. .getBoolean("clean", "requireForce", true); //$NON-NLS-1$ //$NON-NLS-2$
  34. if (requireForce && !(force || dryRun)) {
  35. throw die(CLIText.fatalError(CLIText.get().cleanRequireForce));
  36. }
  37. // Note that CleanCommand's setForce(true) will delete
  38. // .git folders. In the cgit cli, this behavior
  39. // requires setting "-f" twice, not sure how to do
  40. // this with args4j, so this feature is unimplemented
  41. // for now.
  42. Set<String> removedFiles = git.clean().setCleanDirectories(dirs)
  43. .setDryRun(dryRun).call();
  44. for (String removedFile : removedFiles) {
  45. outw.println(MessageFormat.format(CLIText.get().removing,
  46. removedFile));
  47. }
  48. } catch (NoWorkTreeException | GitAPIException | IOException e) {
  49. throw die(e.getMessage(), e);
  50. }
  51. }
  52. }