Line data Source code
1 : // Copyright (C) 2016 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.pgm.init.api; 16 : 17 : import com.google.gerrit.entities.Project; 18 : import com.google.gerrit.server.GerritPersonIdentProvider; 19 : import com.google.gerrit.server.config.SitePaths; 20 : import com.google.gerrit.server.git.meta.VersionedMetaData; 21 : import java.io.File; 22 : import java.io.IOException; 23 : import java.nio.file.Path; 24 : import org.eclipse.jgit.errors.ConfigInvalidException; 25 : import org.eclipse.jgit.internal.storage.file.FileRepository; 26 : import org.eclipse.jgit.lib.CommitBuilder; 27 : import org.eclipse.jgit.lib.ObjectId; 28 : import org.eclipse.jgit.lib.ObjectInserter; 29 : import org.eclipse.jgit.lib.ObjectReader; 30 : import org.eclipse.jgit.lib.PersonIdent; 31 : import org.eclipse.jgit.lib.RefUpdate; 32 : import org.eclipse.jgit.lib.Repository; 33 : import org.eclipse.jgit.lib.RepositoryCache.FileKey; 34 : import org.eclipse.jgit.revwalk.RevTree; 35 : import org.eclipse.jgit.revwalk.RevWalk; 36 : import org.eclipse.jgit.util.FS; 37 : 38 : public abstract class VersionedMetaDataOnInit extends VersionedMetaData { 39 : 40 : protected final String project; 41 : private final InitFlags flags; 42 : private final SitePaths site; 43 : private final String ref; 44 : 45 16 : protected VersionedMetaDataOnInit(InitFlags flags, SitePaths site, String project, String ref) { 46 16 : this.flags = flags; 47 16 : this.site = site; 48 16 : this.project = project; 49 16 : this.ref = ref; 50 16 : } 51 : 52 : @Override 53 : protected String getRefName() { 54 16 : return ref; 55 : } 56 : 57 : public VersionedMetaDataOnInit load() throws IOException, ConfigInvalidException { 58 16 : File path = getPath(); 59 16 : if (path != null) { 60 16 : try (Repository repo = new FileRepository(path)) { 61 16 : load(Project.nameKey(project), repo); 62 : } 63 : } 64 16 : return this; 65 : } 66 : 67 : public void save(String message) throws IOException, ConfigInvalidException { 68 0 : save(new GerritPersonIdentProvider(flags.cfg).get(), message); 69 0 : } 70 : 71 : protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException { 72 0 : File path = getPath(); 73 0 : if (path == null) { 74 0 : throw new IOException(project + " does not exist."); 75 : } 76 : 77 0 : try (Repository repo = new FileRepository(path); 78 0 : ObjectInserter i = repo.newObjectInserter(); 79 0 : ObjectReader r = repo.newObjectReader(); 80 0 : RevWalk rw = new RevWalk(r)) { 81 0 : inserter = i; 82 0 : reader = r; 83 : 84 0 : RevTree srcTree = revision != null ? rw.parseTree(revision) : null; 85 0 : newTree = readTree(srcTree); 86 : 87 0 : CommitBuilder commit = new CommitBuilder(); 88 0 : commit.setAuthor(ident); 89 0 : commit.setCommitter(ident); 90 0 : commit.setMessage(msg); 91 : 92 0 : onSave(commit); 93 : 94 0 : ObjectId res = newTree.writeTree(inserter); 95 0 : if (res.equals(srcTree)) { 96 0 : return; 97 : } 98 0 : commit.setTreeId(res); 99 : 100 0 : if (revision != null) { 101 0 : commit.addParentId(revision); 102 : } 103 0 : ObjectId newRevision = inserter.insert(commit); 104 0 : updateRef(repo, ident, newRevision, "commit: " + msg); 105 0 : revision = rw.parseCommit(newRevision); 106 0 : } finally { 107 0 : inserter = null; 108 0 : reader = null; 109 : } 110 0 : } 111 : 112 : private void updateRef(Repository repo, PersonIdent ident, ObjectId newRevision, String refLogMsg) 113 : throws IOException { 114 0 : RefUpdate ru = repo.updateRef(getRefName()); 115 0 : ru.setRefLogIdent(ident); 116 0 : ru.setNewObjectId(newRevision); 117 0 : ru.setExpectedOldObjectId(revision); 118 0 : ru.setRefLogMessage(refLogMsg, false); 119 0 : RefUpdate.Result r = ru.update(); 120 0 : switch (r) { 121 : case FAST_FORWARD: 122 : case NEW: 123 : case NO_CHANGE: 124 0 : break; 125 : case FORCED: 126 : case IO_FAILURE: 127 : case LOCK_FAILURE: 128 : case NOT_ATTEMPTED: 129 : case REJECTED: 130 : case REJECTED_CURRENT_BRANCH: 131 : case RENAMED: 132 : case REJECTED_MISSING_OBJECT: 133 : case REJECTED_OTHER_REASON: 134 : default: 135 0 : throw new IOException( 136 0 : "Failed to update " + getRefName() + " of " + project + ": " + r.name()); 137 : } 138 0 : } 139 : 140 : private File getPath() { 141 16 : Path basePath = site.resolve(flags.cfg.getString("gerrit", null, "basePath")); 142 16 : if (basePath == null) { 143 0 : throw new IllegalStateException("gerrit.basePath must be configured"); 144 : } 145 16 : return FileKey.resolve(basePath.resolve(project).toFile(), FS.DETECTED); 146 : } 147 : }