annotate cmdline/org/tmatesoft/hg/console/Merge.java @ 709:497e697636fc

Report merged lines as changed block if possible, not as a sequence of added/deleted blocks. To facilitate access to merge parent lines AddBlock got mergeLineAt() method that reports index of the line in the second parent (if any), while insertedAt() has been changed to report index in the first parent always
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 21 Aug 2013 16:23:27 +0200
parents cd5c87d96315
children
rev   line source
704
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.console;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.util.Arrays;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.util.HashSet;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import org.tmatesoft.hg.core.HgCallbackTargetException;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import org.tmatesoft.hg.core.HgFileRevision;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import org.tmatesoft.hg.core.HgMergeCommand;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import org.tmatesoft.hg.core.HgMergeCommand.Resolver;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.core.HgRepoFacade;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import org.tmatesoft.hg.core.Nodeid;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 /**
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 * Command-line frontend for merge command, 'hg merge' counterpart.
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 * @author Artem Tikhomirov
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * @author TMate Software Ltd.
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 */
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 public class Merge {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 public static void main(String[] args) throws Exception {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 Options cmdLineOpts = Options.parse(args, new HashSet<String>(Arrays.asList("--dry-run")));
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 HgRepoFacade hgRepo = new HgRepoFacade();
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 if (!hgRepo.init(cmdLineOpts.findRepository())) {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation());
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 return;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 HgMergeCommand.Mediator m = null;
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 if (cmdLineOpts.getBoolean("--dry-run") || Boolean.TRUE.booleanValue()) {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 m = new Dump();
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 final String revParam = cmdLineOpts.getSingle("-r", "--rev");
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 final HgMergeCommand cmd = hgRepo.createMergeCommand();
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 if (revParam.trim().length() == Nodeid.SIZE_ASCII) {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 cmd.changeset(Nodeid.fromAscii(revParam.trim()));
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 } else {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 cmd.changeset(Integer.parseInt(revParam));
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 cmd.execute(m);
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 static class Dump implements HgMergeCommand.Mediator {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58
705
b4242b7e7dfe Merge command: implement conflict resolution alternatives
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 704
diff changeset
59 public void same(HgFileRevision rev, Resolver resolver) throws HgCallbackTargetException {
706
cd5c87d96315 Merge: tests for mediator notifications
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 705
diff changeset
60 System.out.printf("Unchanged %s:%s\n", rev.getPath(), rev.getRevision().shortNotation());
704
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 public void onlyA(HgFileRevision base, HgFileRevision rev, Resolver resolver) throws HgCallbackTargetException {
706
cd5c87d96315 Merge: tests for mediator notifications
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 705
diff changeset
64 System.out.printf("Left in first trunk only %s:%s\n", rev.getPath(), rev.getRevision().shortNotation());
704
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 public void onlyB(HgFileRevision base, HgFileRevision rev, Resolver resolver) throws HgCallbackTargetException {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 System.out.printf("Left in second trunk only %s:%s\n", rev.getPath(), rev.getRevision().shortNotation());
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 public void newInA(HgFileRevision rev, Resolver resolver) throws HgCallbackTargetException {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 System.out.printf("Introduced in first trunk %s:%s\n", rev.getPath(), rev.getRevision().shortNotation());
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 public void newInB(HgFileRevision rev, Resolver resolver) throws HgCallbackTargetException {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 System.out.printf("Introduced in second trunk %s:%s\n", rev.getPath(), rev.getRevision().shortNotation());
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 public void fastForwardA(HgFileRevision base, HgFileRevision first, Resolver resolver) throws HgCallbackTargetException {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 System.out.printf("Changed in first trunk only %s: %s..%s\n", first.getPath(), base.getRevision().shortNotation(), first.getRevision().shortNotation());
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 public void fastForwardB(HgFileRevision base, HgFileRevision second, Resolver resolver) throws HgCallbackTargetException {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 System.out.printf("Changed in second trunk only %s: %s..%s\n", second.getPath(), base.getRevision().shortNotation(), second.getRevision().shortNotation());
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 public void resolve(HgFileRevision base, HgFileRevision first, HgFileRevision second, Resolver resolver) throws HgCallbackTargetException {
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 System.out.printf("Changed in boths trunks %s: %s and %s from %s\n", first.getPath(), first.getRevision().shortNotation(), second.getRevision().shortNotation(), base.getRevision().shortNotation());
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 }
7743a9c10bfa Merge command introduced
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 }