View Javadoc

1   package com.aragost.javahg.merge;
2   
3   import com.aragost.javahg.Changeset;
4   import com.aragost.javahg.Repository;
5   import com.aragost.javahg.commands.CommitCommand;
6   import com.aragost.javahg.commands.GraftCommand;
7   import com.aragost.javahg.commands.LogCommand;
8   
9   public class GraftContext extends ConflictResolvingContext {
10  
11      private final int sourceRev;
12  
13      private Changeset source;
14  
15      /**
16       * The graft command might decide to rollback a changeset. In
17       * which case the field is set.
18       * <p>
19       * If the changeset is rollback, a commit has to be done when
20       * conflicts are resolved, otherwise a graft --continue
21       */
22      private Changeset rollbackChangeset;
23  
24      public GraftContext(GraftCommand command, int sourceRev) {
25          super(command);
26          this.sourceRev = sourceRev;
27      }
28  
29      public synchronized Changeset getSource() {
30          if (this.source == null) {
31              this.source = LogCommand.on(getRepository()).rev("" + this.sourceRev).single();
32          }
33          return this.source;
34      }
35  
36      public Changeset commit() {
37          Repository repo = getRepository();
38          if (this.rollbackChangeset == null) {
39              repo.lock();
40              try {
41                  boolean createdNewChangeset = ((GraftCommand) getCommand()).executeContinue();
42                  return createdNewChangeset ? repo.tip() : null;
43              } finally {
44                  repo.unlock();
45              }
46          } else {
47              CommitCommand cmd = CommitCommand.on(repo);
48              cmd.user(this.rollbackChangeset.getUser());
49              cmd.date(this.rollbackChangeset.getTimestamp());
50              cmd.message(this.rollbackChangeset.getMessage());
51              cmd.extra("source", getSource().getNode());
52              return cmd.execute();
53          }
54      }
55  
56      public void setRollbackChangeset(Changeset rollbackChangeset) {
57          this.rollbackChangeset = rollbackChangeset;
58      }
59  
60  }