| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CommitCommand |
|
| 1.875;1.875 |
| 1 | /* | |
| 2 | * #%L | |
| 3 | * JavaHg | |
| 4 | * %% | |
| 5 | * Copyright (C) 2011 aragost Trifork ag | |
| 6 | * %% | |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 8 | * of this software and associated documentation files (the "Software"), to deal | |
| 9 | * in the Software without restriction, including without limitation the rights | |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 11 | * copies of the Software, and to permit persons to whom the Software is | |
| 12 | * furnished to do so, subject to the following conditions: | |
| 13 | * | |
| 14 | * The above copyright notice and this permission notice shall be included in | |
| 15 | * all copies or substantial portions of the Software. | |
| 16 | * | |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 23 | * THE SOFTWARE. | |
| 24 | * #L% | |
| 25 | */ | |
| 26 | package com.aragost.javahg.commands; | |
| 27 | ||
| 28 | import java.io.File; | |
| 29 | ||
| 30 | import com.aragost.javahg.Changeset; | |
| 31 | import com.aragost.javahg.Repository; | |
| 32 | import com.aragost.javahg.commands.flags.CommitCommandFlags; | |
| 33 | import com.aragost.javahg.internals.Utils; | |
| 34 | ||
| 35 | /** | |
| 36 | * Command class for executing <tt>hg commit</tt>. Set flags from | |
| 37 | * {@link CommitCommandFlags} and call the {@link #execute} method. | |
| 38 | */ | |
| 39 | public class CommitCommand extends CommitCommandFlags { | |
| 40 | ||
| 41 | private String message; | |
| 42 | ||
| 43 | /** | |
| 44 | * @param repository | |
| 45 | * the repository associated with this command. | |
| 46 | */ | |
| 47 | public CommitCommand(Repository repository) { | |
| 48 | 949 | super(repository); |
| 49 | 949 | withDebugFlag(); |
| 50 | 949 | } |
| 51 | ||
| 52 | /** | |
| 53 | * Check if the commit was successful. In contrast with Mercurial, | |
| 54 | * this returns true if <tt>hg commit</tt> exited with a return | |
| 55 | * code of 1. This happens if there were no changes and is | |
| 56 | * signaled to the caller by returning null instead of a | |
| 57 | * changeset. | |
| 58 | */ | |
| 59 | @Override | |
| 60 | public boolean isSuccessful() { | |
| 61 | 1714 | return super.isSuccessful() || getReturnCode() == 1; |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * Commit changes in the passed in files only | |
| 66 | * | |
| 67 | * @param files | |
| 68 | * the files to commit. | |
| 69 | * @return the changeset created by the commit, or null if nothing | |
| 70 | * was committed. | |
| 71 | */ | |
| 72 | public Changeset execute(String... files) { | |
| 73 | 1 | return doExecute(files); |
| 74 | } | |
| 75 | ||
| 76 | private Changeset doExecute(String... files) { | |
| 77 | 1715 | if (this.message == null) { |
| 78 | 1 | throw new IllegalStateException("message not set for command"); |
| 79 | } | |
| 80 | ||
| 81 | 1714 | if (files == null) { |
| 82 | 1713 | files = new String[0]; |
| 83 | 1 | } else if (files.length == 0) { |
| 84 | 0 | files = new String[2]; |
| 85 | 0 | files[0] = "--exclude"; |
| 86 | 0 | files[1] = "*"; |
| 87 | } | |
| 88 | ||
| 89 | 1714 | String output = launchString(files); |
| 90 | 1712 | int rc = getReturnCode(); |
| 91 | 1712 | if (rc == 1) { |
| 92 | 779 | return null; |
| 93 | } else { | |
| 94 | 933 | int length = output.length(); |
| 95 | 933 | String node = new String(output.substring(length - 41, length - 1)); |
| 96 | 933 | return getRepository().changeset(node); |
| 97 | } | |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * | |
| 102 | * @param files | |
| 103 | * the files to commit. | |
| 104 | * @return the changeset created by the commit, or null if nothing | |
| 105 | * was committed. | |
| 106 | */ | |
| 107 | public Changeset execute(File... files) { | |
| 108 | 0 | return execute(Utils.fileArray2StringArray(files)); |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Commit all changes | |
| 113 | * | |
| 114 | * @return the changeset created by the commit, or null if nothing was | |
| 115 | * committed. | |
| 116 | */ | |
| 117 | public Changeset execute() { | |
| 118 | 1714 | return doExecute((String[])null); |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Set the commit message. A message is mandatory for commit. | |
| 123 | */ | |
| 124 | @Override | |
| 125 | public CommitCommand message(String text) { | |
| 126 | 947 | this.message = text; |
| 127 | 947 | return super.message(text); |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Add a field to the extra dictionary for the changeset. | |
| 132 | * <p> | |
| 133 | * Note this is implemented via the javahg extension for | |
| 134 | * Mercurial. | |
| 135 | * | |
| 136 | * @param key | |
| 137 | * @param value | |
| 138 | */ | |
| 139 | public void extra(String key, String value) { | |
| 140 | 0 | cmdAppend("--javahg-extra", key + "=" + value); |
| 141 | 0 | } |
| 142 | } |