Coverage Report - com.aragost.javahg.commands.BookmarksCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
BookmarksCommand
94%
32/34
87%
7/8
2.4
 
 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  1
     private static final Logger LOG = LoggerFactory.getLogger(BookmarksCommand.class);
 48  
 
 49  1
     private static final byte[] NO_BOOKMARKS = "no bookmarks set\n".getBytes();
 50  
 
 51  1
     private static final HgVersion ISSUE3263_FIXED = HgVersion.fromString("2.1.1");
 52  
 
 53  
     public BookmarksCommand(Repository repository) {
 54  22
         super(repository);
 55  22
         withDebugFlag();
 56  22
     }
 57  
 
 58  
     /**
 59  
      * 
 60  
      * @return all bookmarks
 61  
      */
 62  
     public List<Bookmark> list() {
 63  20
         Repository repo = getRepository();
 64  20
         HgInputStream stream = launchStream();
 65  
         try {
 66  20
             if (stream.match(NO_BOOKMARKS)) {
 67  3
                 stream.consumeAll();
 68  3
                 return Collections.emptyList();
 69  
             }
 70  17
             List<Bookmark> result = Lists.newArrayList();
 71  37
             while (stream.peek() != -1) {
 72  20
                 stream.mustMatch(' ');
 73  20
                 boolean active = stream.read() == '*';
 74  20
                 stream.mustMatch(' ');
 75  20
                 String name = stream.textUpTo(' ');
 76  20
                 stream.upTo(':');
 77  20
                 String node = stream.textUpTo('\n');
 78  20
                 Bookmark bookmark = new Bookmark(repo.changeset(node), name, active);
 79  20
                 result.add(bookmark);
 80  20
             }
 81  17
             return result;
 82  0
         } catch (IOException e) {
 83  0
             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  7
         launchString(name);
 94  7
         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  7
             LOG.info("Issue3263 workaround: Executing 'bookmarks --list' as part of creating a bookmark");
 99  7
             list();
 100  
         }
 101  7
     }
 102  
 
 103  
     /**
 104  
      * Delete a bookmark
 105  
      * 
 106  
      * @param name
 107  
      */
 108  
     public void delete(String name) {
 109  1
         launchString("--delete", name);
 110  1
     }
 111  
 
 112  
     /**
 113  
      * Rename a bookmark
 114  
      * 
 115  
      * @param oldName
 116  
      * @param newName
 117  
      */
 118  
     public void rename(String oldName, String newName) {
 119  1
         launchString("--rename", oldName, newName);
 120  1
     }
 121  
 }