View Javadoc

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.IOException;
29  import java.util.Collections;
30  import java.util.List;
31  
32  import com.aragost.javahg.Bookmark;
33  import com.aragost.javahg.HgVersion;
34  import com.aragost.javahg.Repository;
35  import com.aragost.javahg.commands.flags.BookmarksCommandFlags;
36  import com.aragost.javahg.internals.HgInputStream;
37  import com.aragost.javahg.internals.RuntimeIOException;
38  import com.aragost.javahg.log.Logger;
39  import com.aragost.javahg.log.LoggerFactory;
40  import com.google.common.collect.Lists;
41  
42  /**
43   * Command class for executing <tt>hg bookmarks</tt>.
44   */
45  public class BookmarksCommand extends BookmarksCommandFlags {
46  
47      private static final Logger LOG = LoggerFactory.getLogger(BookmarksCommand.class);
48  
49      private static final byte[] NO_BOOKMARKS = "no bookmarks set\n".getBytes();
50  
51      private static final HgVersion ISSUE3263_FIXED = HgVersion.fromString("2.1.1");
52  
53      public BookmarksCommand(Repository repository) {
54          super(repository);
55          withDebugFlag();
56      }
57  
58      /**
59       * 
60       * @return all bookmarks
61       */
62      public List<Bookmark> list() {
63          Repository repo = getRepository();
64          HgInputStream stream = launchStream();
65          try {
66              if (stream.match(NO_BOOKMARKS)) {
67                  stream.consumeAll();
68                  return Collections.emptyList();
69              }
70              List<Bookmark> result = Lists.newArrayList();
71              while (stream.peek() != -1) {
72                  stream.mustMatch(' ');
73                  boolean active = stream.read() == '*';
74                  stream.mustMatch(' ');
75                  String name = stream.textUpTo(' ');
76                  stream.upTo(':');
77                  String node = stream.textUpTo('\n');
78                  Bookmark bookmark = new Bookmark(repo.changeset(node), name, active);
79                  result.add(bookmark);
80              }
81              return result;
82          } catch (IOException e) {
83              throw new RuntimeIOException(e);
84          }
85      }
86  
87      /**
88       * Create a new bookmark
89       * 
90       * @param name
91       */
92      public void create(String name) {
93          launchString(name);
94          if (getRepository().getHgVersion().isBefore(ISSUE3263_FIXED)) {
95              // It seems that performing a 'bookmarks --list' is a
96              // workaround for
97              // http://mercurial.selenic.com/bts/issue3263
98              LOG.info("Issue3263 workaround: Executing 'bookmarks --list' as part of creating a bookmark");
99              list();
100         }
101     }
102 
103     /**
104      * Delete a bookmark
105      * 
106      * @param name
107      */
108     public void delete(String name) {
109         launchString("--delete", name);
110     }
111 
112     /**
113      * Rename a bookmark
114      * 
115      * @param oldName
116      * @param newName
117      */
118     public void rename(String oldName, String newName) {
119         launchString("--rename", oldName, newName);
120     }
121 }