Mercurial > jhg
comparison src/org/tmatesoft/hg/core/Cset.java @ 64:19e9e220bf68
Convenient commands constitute hi-level API. org.tmatesoft namespace, GPL2 statement
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Fri, 21 Jan 2011 05:56:43 +0100 |
| parents | |
| children | e21df6259f83 |
comparison
equal
deleted
inserted
replaced
| 63:a47530a2ea12 | 64:19e9e220bf68 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@svnkit.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.core; | |
| 18 | |
| 19 import java.util.ArrayList; | |
| 20 import java.util.Collections; | |
| 21 import java.util.List; | |
| 22 | |
| 23 import org.tmatesoft.hg.core.LogCommand.FileRevision; | |
| 24 import org.tmatesoft.hg.util.PathPool; | |
| 25 | |
| 26 import com.tmate.hgkit.ll.Changeset; | |
| 27 import com.tmate.hgkit.ll.HgRepository; | |
| 28 import com.tmate.hgkit.ll.Nodeid; | |
| 29 import com.tmate.hgkit.ll.StatusCollector; | |
| 30 | |
| 31 /** | |
| 32 * TODO rename to Changeset along with original Changeset moved to .repo and renamed to HgChangeset? | |
| 33 * Not thread-safe, don't try to read from different threads | |
| 34 * | |
| 35 * @author Artem Tikhomirov | |
| 36 * @author TMate Software Ltd. | |
| 37 */ | |
| 38 public class Cset implements Cloneable { | |
| 39 private final StatusCollector statusHelper; | |
| 40 private final PathPool pathHelper; | |
| 41 | |
| 42 // | |
| 43 private Changeset changeset; | |
| 44 private Nodeid nodeid; | |
| 45 | |
| 46 // | |
| 47 private List<FileRevision> modifiedFiles, addedFiles; | |
| 48 private List<Path> deletedFiles; | |
| 49 private int revNumber; | |
| 50 | |
| 51 // XXX consider CommandContext with StatusCollector, PathPool etc. Commands optionally get CC through a cons or create new | |
| 52 // and pass it around | |
| 53 /*package-local*/Cset(StatusCollector statusCollector, PathPool pathPool) { | |
| 54 statusHelper = statusCollector; | |
| 55 pathHelper = pathPool; | |
| 56 } | |
| 57 | |
| 58 /*package-local*/ | |
| 59 void init(int localRevNumber, Nodeid nid, Changeset rawChangeset) { | |
| 60 revNumber = localRevNumber; | |
| 61 nodeid = nid; | |
| 62 changeset = rawChangeset; | |
| 63 } | |
| 64 | |
| 65 public String getUser() { | |
| 66 return changeset.user(); | |
| 67 } | |
| 68 public String getComment() { | |
| 69 return changeset.comment(); | |
| 70 } | |
| 71 public String getBranch() { | |
| 72 return changeset.branch(); | |
| 73 } | |
| 74 public String getDate() { | |
| 75 return changeset.dateString(); | |
| 76 } | |
| 77 | |
| 78 public List<Path> getAffectedFiles() { | |
| 79 ArrayList<Path> rv = new ArrayList<Path>(changeset.files().size()); | |
| 80 for (String name : changeset.files()) { | |
| 81 rv.add(pathHelper.path(name)); | |
| 82 } | |
| 83 return rv; | |
| 84 } | |
| 85 | |
| 86 public List<FileRevision> getModifiedFiles() { | |
| 87 if (modifiedFiles == null) { | |
| 88 initFileChanges(); | |
| 89 } | |
| 90 return modifiedFiles; | |
| 91 } | |
| 92 | |
| 93 public List<FileRevision> getAddedFiles() { | |
| 94 if (addedFiles == null) { | |
| 95 initFileChanges(); | |
| 96 } | |
| 97 return addedFiles; | |
| 98 } | |
| 99 | |
| 100 public List<Path> getRemovedFiles() { | |
| 101 if (deletedFiles == null) { | |
| 102 initFileChanges(); | |
| 103 } | |
| 104 return deletedFiles; | |
| 105 } | |
| 106 | |
| 107 @Override | |
| 108 public Cset clone() { | |
| 109 try { | |
| 110 Cset copy = (Cset) super.clone(); | |
| 111 copy.changeset = changeset.clone(); | |
| 112 return copy; | |
| 113 } catch (CloneNotSupportedException ex) { | |
| 114 throw new InternalError(ex.toString()); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 private /*synchronized*/ void initFileChanges() { | |
| 119 ArrayList<Path> deleted = new ArrayList<Path>(); | |
| 120 ArrayList<FileRevision> modified = new ArrayList<FileRevision>(); | |
| 121 ArrayList<FileRevision> added = new ArrayList<FileRevision>(); | |
| 122 StatusCollector.Record r = new StatusCollector.Record(); | |
| 123 statusHelper.change(revNumber, r); | |
| 124 final HgRepository repo = statusHelper.getRepo(); | |
| 125 for (String s : r.getModified()) { | |
| 126 Path p = pathHelper.path(s); | |
| 127 Nodeid nid = r.nodeidAfterChange(s); | |
| 128 if (nid == null) { | |
| 129 throw new IllegalArgumentException(); | |
| 130 } | |
| 131 modified.add(new FileRevision(repo, nid, p)); | |
| 132 } | |
| 133 for (String s : r.getAdded()) { | |
| 134 Path p = pathHelper.path(s); | |
| 135 Nodeid nid = r.nodeidAfterChange(s); | |
| 136 if (nid == null) { | |
| 137 throw new IllegalArgumentException(); | |
| 138 } | |
| 139 added.add(new FileRevision(repo, nid, p)); | |
| 140 } | |
| 141 for (String s : r.getRemoved()) { | |
| 142 deleted.add(pathHelper.path(s)); | |
| 143 } | |
| 144 modified.trimToSize(); | |
| 145 added.trimToSize(); | |
| 146 deleted.trimToSize(); | |
| 147 modifiedFiles = Collections.unmodifiableList(modified); | |
| 148 addedFiles = Collections.unmodifiableList(added); | |
| 149 deletedFiles = Collections.unmodifiableList(deleted); | |
| 150 } | |
| 151 } |
