| 1 | |
package com.aragost.javahg.merge; |
| 2 | |
|
| 3 | |
import java.util.List; |
| 4 | |
|
| 5 | |
import com.aragost.javahg.Changeset; |
| 6 | |
import com.aragost.javahg.commands.RemoveCommand; |
| 7 | |
import com.aragost.javahg.commands.RevertCommand; |
| 8 | |
|
| 9 | |
public class KeepDeleteConflict extends MergeFile { |
| 10 | |
|
| 11 | 3 | private State state = State.KEEP; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
private boolean localKeep; |
| 17 | |
|
| 18 | 4 | public enum State { |
| 19 | 1 | KEEP, DELETE |
| 20 | |
}; |
| 21 | |
|
| 22 | |
public KeepDeleteConflict(ConflictResolvingContext mergeState, String fileName, State localState) { |
| 23 | 3 | super(mergeState, fileName); |
| 24 | 3 | this.localKeep = localState.equals(State.KEEP); |
| 25 | 3 | } |
| 26 | |
|
| 27 | |
public State getState() { |
| 28 | 2 | return state; |
| 29 | |
} |
| 30 | |
|
| 31 | |
public void update(State state) { |
| 32 | 3 | if (this.state.equals(state)) { |
| 33 | 0 | return; |
| 34 | |
} |
| 35 | 3 | String fn = getFilename(); |
| 36 | 1 | switch (state) { |
| 37 | |
case KEEP: |
| 38 | 1 | RevertCommand.on(getRepository()).rev(getKeepParent().getNode()).noBackup().execute(fn); |
| 39 | 1 | break; |
| 40 | |
case DELETE: |
| 41 | 2 | List<String> removed = RemoveCommand.on(getRepository()).force().execute(fn); |
| 42 | 2 | if (removed.size() != 1 || !removed.get(0).equals(fn)) { |
| 43 | 0 | throw new RuntimeException("remove failed to removed file: " + fn); |
| 44 | |
} |
| 45 | |
break; |
| 46 | |
default: |
| 47 | 0 | throw new IllegalStateException("Unhandled state"); |
| 48 | |
} |
| 49 | 3 | this.state = state; |
| 50 | 3 | } |
| 51 | |
|
| 52 | |
public void keep() { |
| 53 | 1 | update(State.KEEP); |
| 54 | 1 | } |
| 55 | |
|
| 56 | |
public void delete() { |
| 57 | 2 | update(State.DELETE); |
| 58 | 2 | } |
| 59 | |
|
| 60 | |
public Changeset getKeepParent() { |
| 61 | 3 | if (this.localKeep) { |
| 62 | 2 | return getMergeCtx().getLocal(); |
| 63 | |
} else { |
| 64 | 1 | return getMergeCtx().getRemote(); |
| 65 | |
} |
| 66 | |
} |
| 67 | |
} |