comparison test/org/tmatesoft/hg/test/TestCommit.java @ 618:7c0d2ce340b8

Refactor approach how content finds it way down to a commit revision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 16 May 2013 19:46:13 +0200
parents 65c01508f002
children 99ad1e3a4e4d
comparison
equal deleted inserted replaced
617:65c01508f002 618:7c0d2ce340b8
19 import static org.junit.Assert.*; 19 import static org.junit.Assert.*;
20 import static org.tmatesoft.hg.repo.HgRepository.*; 20 import static org.tmatesoft.hg.repo.HgRepository.*;
21 21
22 import java.io.File; 22 import java.io.File;
23 import java.io.IOException; 23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.util.List; 24 import java.util.List;
26 25
27 import org.junit.Rule; 26 import org.junit.Rule;
28 import org.junit.Test; 27 import org.junit.Test;
29 import org.tmatesoft.hg.core.HgAddRemoveCommand; 28 import org.tmatesoft.hg.core.HgAddRemoveCommand;
36 import org.tmatesoft.hg.core.Nodeid; 35 import org.tmatesoft.hg.core.Nodeid;
37 import org.tmatesoft.hg.core.SessionContext; 36 import org.tmatesoft.hg.core.SessionContext;
38 import org.tmatesoft.hg.internal.ByteArrayChannel; 37 import org.tmatesoft.hg.internal.ByteArrayChannel;
39 import org.tmatesoft.hg.internal.COWTransaction; 38 import org.tmatesoft.hg.internal.COWTransaction;
40 import org.tmatesoft.hg.internal.CommitFacility; 39 import org.tmatesoft.hg.internal.CommitFacility;
40 import org.tmatesoft.hg.internal.DataSerializer.ByteArrayDataSource;
41 import org.tmatesoft.hg.internal.FileContentSupplier; 41 import org.tmatesoft.hg.internal.FileContentSupplier;
42 import org.tmatesoft.hg.internal.Internals; 42 import org.tmatesoft.hg.internal.Internals;
43 import org.tmatesoft.hg.internal.Transaction; 43 import org.tmatesoft.hg.internal.Transaction;
44 import org.tmatesoft.hg.repo.HgDataFile; 44 import org.tmatesoft.hg.repo.HgDataFile;
45 import org.tmatesoft.hg.repo.HgLookup; 45 import org.tmatesoft.hg.repo.HgLookup;
75 HgRepository hgRepo = new HgLookup().detect(repoLoc); 75 HgRepository hgRepo = new HgLookup().detect(repoLoc);
76 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), 0); 76 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), 0);
77 // FIXME test diff for processing changed newlines (ie \r\n -> \n or vice verse) - if a whole line or 77 // FIXME test diff for processing changed newlines (ie \r\n -> \n or vice verse) - if a whole line or
78 // just changed endings are in the patch! 78 // just changed endings are in the patch!
79 HgDataFile df = hgRepo.getFileNode("file1"); 79 HgDataFile df = hgRepo.getFileNode("file1");
80 cf.add(df, new ByteArraySupplier("hello\nworld".getBytes())); 80 cf.add(df, new ByteArrayDataSource("hello\nworld".getBytes()));
81 Transaction tr = newTransaction(hgRepo); 81 Transaction tr = newTransaction(hgRepo);
82 Nodeid secondRev = cf.commit("SECOND", tr); 82 Nodeid secondRev = cf.commit("SECOND", tr);
83 tr.commit(); 83 tr.commit();
84 // 84 //
85 List<HgChangeset> commits = new HgLogCommand(hgRepo).execute(); 85 List<HgChangeset> commits = new HgLogCommand(hgRepo).execute();
104 HgRepository hgRepo = new HgLookup().detect(repoLoc); 104 HgRepository hgRepo = new HgLookup().detect(repoLoc);
105 assertEquals("[sanity]", 0, new HgLogCommand(hgRepo).execute().size()); 105 assertEquals("[sanity]", 0, new HgLogCommand(hgRepo).execute().size());
106 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), NO_REVISION); 106 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), NO_REVISION);
107 HgDataFile df = hgRepo.getFileNode(fname); 107 HgDataFile df = hgRepo.getFileNode(fname);
108 final byte[] initialContent = "hello\nworld".getBytes(); 108 final byte[] initialContent = "hello\nworld".getBytes();
109 cf.add(df, new ByteArraySupplier(initialContent)); 109 cf.add(df, new ByteArrayDataSource(initialContent));
110 String comment = "commit 1"; 110 String comment = "commit 1";
111 Transaction tr = newTransaction(hgRepo); 111 Transaction tr = newTransaction(hgRepo);
112 Nodeid c1Rev = cf.commit(comment, tr); 112 Nodeid c1Rev = cf.commit(comment, tr);
113 tr.commit(); 113 tr.commit();
114 List<HgChangeset> commits = new HgLogCommand(hgRepo).execute(); 114 List<HgChangeset> commits = new HgLogCommand(hgRepo).execute();
139 HgChangeset parentCset = new HgLogCommand(hgRepo).range(parentCsetRevIndex, parentCsetRevIndex).execute().get(0); 139 HgChangeset parentCset = new HgLogCommand(hgRepo).range(parentCsetRevIndex, parentCsetRevIndex).execute().get(0);
140 assertEquals("[sanity]", DEFAULT_BRANCH_NAME, parentCset.getBranch()); 140 assertEquals("[sanity]", DEFAULT_BRANCH_NAME, parentCset.getBranch());
141 // 141 //
142 RepoUtils.modifyFileAppend(fileD, "A CHANGE\n"); 142 RepoUtils.modifyFileAppend(fileD, "A CHANGE\n");
143 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex); 143 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex);
144 FileContentSupplier contentProvider = new FileContentSupplier(fileD); 144 FileContentSupplier contentProvider = new FileContentSupplier(hgRepo, fileD);
145 cf.add(dfD, contentProvider); 145 cf.add(dfD, contentProvider);
146 cf.branch("branch1"); 146 cf.branch("branch1");
147 Transaction tr = newTransaction(hgRepo); 147 Transaction tr = newTransaction(hgRepo);
148 Nodeid commitRev1 = cf.commit("FIRST", tr); 148 Nodeid commitRev1 = cf.commit("FIRST", tr);
149 tr.commit(); 149 tr.commit();
150 contentProvider.done();
151 // 150 //
152 List<HgChangeset> commits = new HgLogCommand(hgRepo).range(parentCsetRevIndex+1, TIP).execute(); 151 List<HgChangeset> commits = new HgLogCommand(hgRepo).range(parentCsetRevIndex+1, TIP).execute();
153 assertEquals(1, commits.size()); 152 assertEquals(1, commits.size());
154 HgChangeset c1 = commits.get(0); 153 HgChangeset c1 = commits.get(0);
155 errorCollector.assertEquals(c1.getNodeid(), commitRev1); 154 errorCollector.assertEquals(c1.getNodeid(), commitRev1);
169 assertTrue("[sanity]", hgRepo.getFileNode("d").exists()); 168 assertTrue("[sanity]", hgRepo.getFileNode("d").exists());
170 assertTrue("[sanity]", new File(repoLoc, "d").canRead()); 169 assertTrue("[sanity]", new File(repoLoc, "d").canRead());
171 RepoUtils.createFile(new File(repoLoc, "xx"), "xyz"); 170 RepoUtils.createFile(new File(repoLoc, "xx"), "xyz");
172 new HgAddRemoveCommand(hgRepo).add(Path.create("xx")).remove(Path.create("d")).execute(); 171 new HgAddRemoveCommand(hgRepo).add(Path.create("xx")).remove(Path.create("d")).execute();
173 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), hgRepo.getChangelog().getLastRevision()); 172 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), hgRepo.getChangelog().getLastRevision());
174 FileContentSupplier contentProvider = new FileContentSupplier(new File(repoLoc, "xx")); 173 FileContentSupplier contentProvider = new FileContentSupplier(hgRepo, new File(repoLoc, "xx"));
175 cf.add(hgRepo.getFileNode("xx"), contentProvider); 174 cf.add(hgRepo.getFileNode("xx"), contentProvider);
176 cf.forget(hgRepo.getFileNode("d")); 175 cf.forget(hgRepo.getFileNode("d"));
177 Transaction tr = newTransaction(hgRepo); 176 Transaction tr = newTransaction(hgRepo);
178 Nodeid commitRev = cf.commit("Commit with add/remove cmd", tr); 177 Nodeid commitRev = cf.commit("Commit with add/remove cmd", tr);
179 tr.commit(); 178 tr.commit();
180 contentProvider.done();
181 // 179 //
182 List<HgChangeset> commits = new HgLogCommand(hgRepo).changeset(commitRev).execute(); 180 List<HgChangeset> commits = new HgLogCommand(hgRepo).changeset(commitRev).execute();
183 HgChangeset cmt = commits.get(0); 181 HgChangeset cmt = commits.get(0);
184 errorCollector.assertEquals(1, cmt.getAddedFiles().size()); 182 errorCollector.assertEquals(1, cmt.getAddedFiles().size());
185 errorCollector.assertEquals("xx", cmt.getAddedFiles().get(0).getPath().toString()); 183 errorCollector.assertEquals("xx", cmt.getAddedFiles().get(0).getPath().toString());
204 assertTrue("[sanity]", fileD.canRead()); 202 assertTrue("[sanity]", fileD.canRead());
205 // 203 //
206 RepoUtils.modifyFileAppend(fileD, " 1 \n"); 204 RepoUtils.modifyFileAppend(fileD, " 1 \n");
207 final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision(); 205 final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision();
208 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex); 206 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex);
209 FileContentSupplier contentProvider = new FileContentSupplier(fileD); 207 FileContentSupplier contentProvider = new FileContentSupplier(hgRepo, fileD);
210 cf.add(dfD, contentProvider); 208 cf.add(dfD, contentProvider);
211 cf.branch("branch1"); 209 cf.branch("branch1");
212 Transaction tr = newTransaction(hgRepo); 210 Transaction tr = newTransaction(hgRepo);
213 Nodeid commitRev1 = cf.commit("FIRST", tr); 211 Nodeid commitRev1 = cf.commit("FIRST", tr);
214 contentProvider.done();
215 // 212 //
216 RepoUtils.modifyFileAppend(fileD, " 2 \n"); 213 RepoUtils.modifyFileAppend(fileD, " 2 \n");
217 cf.add(dfD, contentProvider = new FileContentSupplier(fileD)); 214 cf.add(dfD, contentProvider = new FileContentSupplier(hgRepo, fileD));
218 cf.branch("branch2"); 215 cf.branch("branch2");
219 Nodeid commitRev2 = cf.commit("SECOND", tr); 216 Nodeid commitRev2 = cf.commit("SECOND", tr);
220 contentProvider.done();
221 // 217 //
222 RepoUtils.modifyFileAppend(fileD, " 2 \n"); 218 RepoUtils.modifyFileAppend(fileD, " 2 \n");
223 cf.add(dfD, contentProvider = new FileContentSupplier(fileD)); 219 cf.add(dfD, contentProvider = new FileContentSupplier(hgRepo, fileD));
224 cf.branch(DEFAULT_BRANCH_NAME); 220 cf.branch(DEFAULT_BRANCH_NAME);
225 Nodeid commitRev3 = cf.commit("THIRD", tr); 221 Nodeid commitRev3 = cf.commit("THIRD", tr);
226 contentProvider.done();
227 tr.commit(); 222 tr.commit();
228 // 223 //
229 List<HgChangeset> commits = new HgLogCommand(hgRepo).range(parentCsetRevIndex+1, TIP).execute(); 224 List<HgChangeset> commits = new HgLogCommand(hgRepo).range(parentCsetRevIndex+1, TIP).execute();
230 assertEquals(3, commits.size()); 225 assertEquals(3, commits.size());
231 HgChangeset c1 = commits.get(0); 226 HgChangeset c1 = commits.get(0);
329 RepoUtils.modifyFileAppend(new File(repoLoc, "a"), "whatever"); 324 RepoUtils.modifyFileAppend(new File(repoLoc, "a"), "whatever");
330 // 325 //
331 final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision(); 326 final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision();
332 // HgCommitCommand can't do branch yet 327 // HgCommitCommand can't do branch yet
333 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex); 328 CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex);
334 cf.add(hgRepo.getFileNode("a"), new FileContentSupplier(new File(repoLoc, "a"))); 329 cf.add(hgRepo.getFileNode("a"), new FileContentSupplier(hgRepo, new File(repoLoc, "a")));
335 cf.branch(branch); 330 cf.branch(branch);
336 Transaction tr = newTransaction(hgRepo); 331 Transaction tr = newTransaction(hgRepo);
337 Nodeid commit = cf.commit("FIRST", tr); 332 Nodeid commit = cf.commit("FIRST", tr);
338 tr.commit(); 333 tr.commit();
339 errorCollector.assertEquals("commit with branch shall update WC", branch, hgRepo.getWorkingCopyBranchName()); 334 errorCollector.assertEquals("commit with branch shall update WC", branch, hgRepo.getWorkingCopyBranchName());
358 return trFactory.create(ctxSource); 353 return trFactory.create(ctxSource);
359 } 354 }
360 355
361 public static void main(String[] args) throws Exception { 356 public static void main(String[] args) throws Exception {
362 new TestCommit().testCommitToEmpty(); 357 new TestCommit().testCommitToEmpty();
363 if (Boolean.TRUE.booleanValue()) {
364 return;
365 }
366 String input = "abcdefghijklmnopqrstuvwxyz";
367 ByteArraySupplier bas = new ByteArraySupplier(input.getBytes());
368 ByteBuffer bb = ByteBuffer.allocate(7);
369 byte[] result = new byte[26];
370 int rpos = 0;
371 while (bas.read(bb) != -1) {
372 bb.flip();
373 bb.get(result, rpos, bb.limit());
374 rpos += bb.limit();
375 bb.clear();
376 }
377 if (input.length() != rpos) {
378 throw new AssertionError();
379 }
380 String output = new String(result);
381 if (!input.equals(output)) {
382 throw new AssertionError();
383 }
384 System.out.println(output);
385 }
386
387 static class ByteArraySupplier implements CommitFacility.ByteDataSupplier {
388
389 private final byte[] data;
390 private int pos = 0;
391
392 public ByteArraySupplier(byte[] source) {
393 data = source;
394 }
395
396 public int read(ByteBuffer buf) {
397 if (pos >= data.length) {
398 return -1;
399 }
400 int count = Math.min(buf.remaining(), data.length - pos);
401 buf.put(data, pos, count);
402 pos += count;
403 return count;
404 }
405 } 358 }
406 } 359 }