diff options
Diffstat (limited to 'org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java')
-rw-r--r-- | org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java | 141 |
1 files changed, 78 insertions, 63 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java index 12d4208152..c96e475613 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2011, 2015 François Rey <eclipse.org_@_francois_._rey_._name> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011, 2015 François Rey <eclipse.org_@_francois_._rey_._name> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; @@ -54,14 +21,17 @@ import java.util.TreeSet; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.StatusCommand; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.IndexDiff.StageState; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.pgm.internal.CLIText; -import org.kohsuke.args4j.Option; - import org.eclipse.jgit.pgm.opt.UntrackedFilesHandler; +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.Option; +import org.kohsuke.args4j.spi.RestOfArgumentsHandler; /** * Status command @@ -69,8 +39,6 @@ import org.eclipse.jgit.pgm.opt.UntrackedFilesHandler; @Command(usage = "usage_Status", common = true) class Status extends TextBuiltin { - protected final String lineFormat = CLIText.get().lineFormat; - protected final String statusFileListFormat = CLIText.get().statusFileListFormat; protected final String statusFileListFormatWithPrefix = CLIText.get().statusFileListFormatWithPrefix; @@ -83,17 +51,24 @@ class Status extends TextBuiltin { @Option(name = "--untracked-files", aliases = { "-u", "-uno", "-uall" }, usage = "usage_untrackedFilesMode", handler = UntrackedFilesHandler.class) protected String untrackedFilesMode = "all"; // default value //$NON-NLS-1$ - @Option(name = "--", metaVar = "metaVar_path", multiValued = true) + @Argument(required = false, index = 0, metaVar = "metaVar_paths") + @Option(name = "--", metaVar = "metaVar_paths", handler = RestOfArgumentsHandler.class) protected List<String> filterPaths; @Override - protected void run() throws Exception { - StatusCommand statusCommand = new Git(db).status(); - if (filterPaths != null && filterPaths.size() > 0) - for (String path : filterPaths) - statusCommand.addPath(path); - org.eclipse.jgit.api.Status status = statusCommand.call(); - printStatus(status); + protected void run() { + try (Git git = new Git(db)) { + StatusCommand statusCommand = git.status(); + if (filterPaths != null) { + for (String path : filterPaths) { + statusCommand.addPath(path); + } + } + org.eclipse.jgit.api.Status status = statusCommand.call(); + printStatus(status); + } catch (GitAPIException | NoWorkTreeException | IOException e) { + throw die(e.getMessage(), e); + } } private void printStatus(org.eclipse.jgit.api.Status status) @@ -115,7 +90,7 @@ class Status extends TextBuiltin { Map<String, StageState> conflicting = status.getConflictingStageState(); // build a sorted list of all paths except untracked and ignored - TreeSet<String> sorted = new TreeSet<String>(); + TreeSet<String> sorted = new TreeSet<>(); sorted.addAll(added); sorted.addAll(changed); sorted.addAll(removed); @@ -183,7 +158,7 @@ class Status extends TextBuiltin { // untracked are always at the end of the list if ("all".equals(untrackedFilesMode)) { //$NON-NLS-1$ - TreeSet<String> untracked = new TreeSet<String>( + TreeSet<String> untracked = new TreeSet<>( status.getUntracked()); for (String path : untracked) printPorcelainLine('?', '?', path); @@ -200,7 +175,7 @@ class Status extends TextBuiltin { private void printLongStatus(org.eclipse.jgit.api.Status status) throws IOException { // Print current branch name - final Ref head = db.getRef(Constants.HEAD); + final Ref head = db.exactRef(Constants.HEAD); if (head != null && head.isSymbolic()) { String branch = Repository.shortenRefName(head.getLeaf().getName()); outw.println(CLIText.formatLine(MessageFormat.format( @@ -219,7 +194,7 @@ class Status extends TextBuiltin { Collection<String> untracked = status.getUntracked(); Map<String, StageState> unmergedStates = status .getConflictingStageState(); - Collection<String> toBeCommitted = new ArrayList<String>(added); + Collection<String> toBeCommitted = new ArrayList<>(added); toBeCommitted.addAll(changed); toBeCommitted.addAll(removed); int nbToBeCommitted = toBeCommitted.size(); @@ -230,7 +205,7 @@ class Status extends TextBuiltin { toBeCommitted, added, changed, removed); firstHeader = false; } - Collection<String> notStagedForCommit = new ArrayList<String>(modified); + Collection<String> notStagedForCommit = new ArrayList<>(modified); notStagedForCommit.addAll(missing); int nbNotStagedForCommit = notStagedForCommit.size(); if (nbNotStagedForCommit > 0) { @@ -251,7 +226,7 @@ class Status extends TextBuiltin { firstHeader = false; } int nbUntracked = untracked.size(); - if (nbUntracked > 0 && ("all".equals(untrackedFilesMode))) { //$NON-NLS-1$ + if (nbUntracked > 0 && "all".equals(untrackedFilesMode)) { //$NON-NLS-1$ if (!firstHeader) printSectionHeader(""); //$NON-NLS-1$ printSectionHeader(CLIText.get().untrackedFiles); @@ -259,20 +234,39 @@ class Status extends TextBuiltin { } } + /** + * Print section header + * + * @param pattern + * a {@link String} object. + * @param arguments + * a {@link Object} object. + * @throws IOException + * if an IO error occurred + */ protected void printSectionHeader(String pattern, Object... arguments) throws IOException { if (!porcelain) { outw.println(CLIText.formatLine(MessageFormat.format(pattern, arguments))); - if (!pattern.equals("")) //$NON-NLS-1$ + if (!pattern.isEmpty()) outw.println(CLIText.formatLine("")); //$NON-NLS-1$ outw.flush(); } } + /** + * Print String list + * + * @param list + * a {@link Collection} object. + * @return size of the list + * @throws IOException + * if an IO error occurred + */ protected int printList(Collection<String> list) throws IOException { if (!list.isEmpty()) { - List<String> sortedList = new ArrayList<String>(list); + List<String> sortedList = new ArrayList<>(list); java.util.Collections.sort(sortedList); for (String filename : sortedList) { outw.println(CLIText.formatLine(String.format( @@ -280,16 +274,37 @@ class Status extends TextBuiltin { } outw.flush(); return list.size(); - } else - return 0; + } + return 0; } + /** + * Print String list + * + * @param status1 + * a {@link String} object. + * @param status2 + * a {@link String} object. + * @param status3 + * a {@link String} object. + * @param list + * a {@link Collection} object. + * @param set1 + * a {@link Collection} object. + * @param set2 + * a {@link Collection} object. + * @param set3 + * a {@link Collection} object. + * @return a int. + * @throws IOException + * if an IO error occurred + */ protected int printList(String status1, String status2, String status3, Collection<String> list, Collection<String> set1, Collection<String> set2, - @SuppressWarnings("unused") Collection<String> set3) + Collection<String> set3) throws IOException { - List<String> sortedList = new ArrayList<String>(list); + List<String> sortedList = new ArrayList<>(list); java.util.Collections.sort(sortedList); for (String filename : sortedList) { String prefix; @@ -309,7 +324,7 @@ class Status extends TextBuiltin { private void printUnmerged(Map<String, StageState> unmergedStates) throws IOException { - List<String> paths = new ArrayList<String>(unmergedStates.keySet()); + List<String> paths = new ArrayList<>(unmergedStates.keySet()); Collections.sort(paths); for (String path : paths) { StageState state = unmergedStates.get(path); |