Jesse Wilson | 0ad6047 | 2009-09-14 11:23:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import java.util.List; |
| 18 | import java.util.UUID; |
| 19 | |
| 20 | /** |
| 21 | * Download two versions of Apache Harmony from their SVN version, and use it |
| 22 | * to perform a three-way merge with Dalvik. |
| 23 | */ |
| 24 | public class PullHarmonyCode { |
| 25 | |
| 26 | private final int currentVersion; |
| 27 | private final int targetVersion; |
| 28 | |
| 29 | public PullHarmonyCode(int currentVersion, int targetVersion) { |
| 30 | this.currentVersion = currentVersion; |
| 31 | this.targetVersion = targetVersion; |
| 32 | } |
| 33 | |
| 34 | public void pull(Module module) { |
| 35 | String path = module.path(); |
| 36 | String svnOldBranch = path + "_" + currentVersion; |
| 37 | String svnNewBranch = path + "_" + targetVersion; |
| 38 | String dalvikBranch = path + "_dalvik"; |
| 39 | |
| 40 | Git git = new Git(); |
| 41 | Filesystem filesystem = new Filesystem(); |
| 42 | Svn svn = new Svn(); |
| 43 | |
| 44 | // Assume we're starting with the current Dalvik code. Tuck this away |
| 45 | // somewhere while we rewrite history. |
| 46 | String temp = "/tmp/" + UUID.randomUUID(); |
| 47 | filesystem.mkdir(temp); |
| 48 | |
| 49 | // To prepare a three-way-merge, we need a common starting point: the |
| 50 | // time at which Dalvik and Harmony were most the same. We'll use the |
| 51 | // previous Harmony SVN code as this starting point. We grab the old |
| 52 | // code from their repository, and commit it as a git branch. |
| 53 | System.out.print("Creating branch " + svnOldBranch + "..."); |
| 54 | git.branch(svnOldBranch); |
| 55 | filesystem.move(path, temp + "/" + path); |
| 56 | svn.checkOut(currentVersion, module.getSvnBaseUrl() + "/" + path); |
| 57 | filesystem.rm(filesystem.find(path, ".svn")); |
| 58 | for (MappedDirectory mappedDirectory : module.getMappedDirectories()) { |
| 59 | filesystem.moveContents(mappedDirectory.svnPath(), mappedDirectory.gitPath()); |
| 60 | } |
| 61 | git.rm(git.listDeleted()); |
| 62 | git.add(path); |
| 63 | git.commit(svnOldBranch); |
| 64 | System.out.println("done"); |
| 65 | |
| 66 | // Create a branch that's derived from the starting point. It will |
| 67 | // contain all of the changes Harmony has made from then until now. |
| 68 | System.out.print("Creating branch " + svnNewBranch + "..."); |
| 69 | git.branch(svnNewBranch, svnOldBranch); |
| 70 | filesystem.rm(path); |
| 71 | svn.checkOut(targetVersion, module.getSvnBaseUrl() + "/" + path); |
| 72 | filesystem.rm(filesystem.find(path, ".svn")); |
| 73 | for (MappedDirectory mappedDirectory : module.getMappedDirectories()) { |
| 74 | filesystem.moveContents(mappedDirectory.svnPath(), mappedDirectory.gitPath()); |
| 75 | } |
| 76 | git.rm(git.listDeleted()); |
| 77 | git.add(path); |
| 78 | git.commit(svnNewBranch); |
| 79 | System.out.println("done"); |
| 80 | |
| 81 | // Create another branch that's derived from the starting point. It will |
| 82 | // contain all of the changes Dalvik has made from then until now. |
| 83 | System.out.print("Creating branch " + dalvikBranch + "..."); |
| 84 | git.branch(dalvikBranch, svnOldBranch); |
| 85 | filesystem.rm(path); |
| 86 | filesystem.move(temp + "/" + path, path); |
| 87 | git.rm(git.listDeleted()); |
| 88 | git.add(path); |
| 89 | git.commit(dalvikBranch); |
| 90 | System.out.println("done"); |
| 91 | |
| 92 | // Merge the two sets of changes together: Harmony's and Dalvik's. By |
| 93 | // initializing a common starting point, git can make better decisions |
| 94 | // when the two new versions differ. For example, if today's Dalvik has |
| 95 | // a method that today's Harmony does not, it may be because Dalvik |
| 96 | // added it, or because Harmony deleted it! |
| 97 | System.out.println("Merging " + svnNewBranch + " into " + dalvikBranch + ":"); |
| 98 | List<String> mergeResults = git.merge(svnNewBranch); |
| 99 | for (String mergeResult : mergeResults) { |
| 100 | System.out.print(" "); |
| 101 | System.out.println(mergeResult); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public static void main(String[] args) { |
| 106 | // new PullHarmonyCode(527399, 802921).pull(Modules.CRYPTO); |
| 107 | new PullHarmonyCode(772995, 802921).pull(Modules.ARCHIVE); |
| 108 | } |
| 109 | } |