1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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  
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  
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  
89  
90  
91  
92      public void create(String name) {
93          launchString(name);
94          if (getRepository().getHgVersion().isBefore(ISSUE3263_FIXED)) {
95              
96              
97              
98              LOG.info("Issue3263 workaround: Executing 'bookmarks --list' as part of creating a bookmark");
99              list();
100         }
101     }
102 
103     
104 
105 
106 
107 
108     public void delete(String name) {
109         launchString("--delete", name);
110     }
111 
112     
113 
114 
115 
116 
117 
118     public void rename(String oldName, String newName) {
119         launchString("--rename", oldName, newName);
120     }
121 }