summaryrefslogtreecommitdiffstats
path: root/mgrapp/src/com/google/codereview/manager/unpack/UnpackBundleOp.java
blob: 0b5726975c8171ab3542a79a4f5235c23ce2f64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.codereview.manager.unpack;

import com.google.codereview.internal.NextReceivedBundle.BundleSegmentRequest;
import com.google.codereview.internal.NextReceivedBundle.BundleSegmentResponse;
import com.google.codereview.internal.NextReceivedBundle.NextReceivedBundleResponse;
import com.google.codereview.internal.SubmitChange.SubmitChangeRequest;
import com.google.codereview.internal.SubmitChange.SubmitChangeResponse;
import com.google.codereview.internal.UpdateReceivedBundle.UpdateReceivedBundleRequest;
import com.google.codereview.internal.UpdateReceivedBundle.UpdateReceivedBundleRequest.CodeType;
import com.google.codereview.manager.Backend;
import com.google.codereview.manager.InvalidRepositoryException;
import com.google.codereview.rpc.SimpleController;
import com.google.codereview.util.GitMetaUtil;
import com.google.codereview.util.MutableBoolean;
import com.google.protobuf.RpcCallback;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.spearce.jgit.errors.MissingBundlePrerequisiteException;
import org.spearce.jgit.lib.NullProgressMonitor;
import org.spearce.jgit.lib.ObjectId;
import org.spearce.jgit.lib.Ref;
import org.spearce.jgit.lib.RefUpdate;
import org.spearce.jgit.lib.Repository;
import org.spearce.jgit.revwalk.RevCommit;
import org.spearce.jgit.revwalk.RevSort;
import org.spearce.jgit.revwalk.RevWalk;
import org.spearce.jgit.transport.FetchConnection;
import org.spearce.jgit.transport.Transport;
import org.spearce.jgit.transport.TransportBundleStream;
import org.spearce.jgit.transport.URIish;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/** Unpacks a bundle and imports the commits into the code review system. */
class UnpackBundleOp {
  private static final Log LOG = LogFactory.getLog(UnpackBundleOp.class);

  private static String refOf(final int changeId, final int patchsetId) {
    final StringBuilder b = new StringBuilder();
    final int dh = changeId % 100;
    b.append("refs/changes/");
    if (dh < 10) {
      b.append('0');
    }
    b.append(dh);
    b.append('/');
    b.append(changeId);
    b.append('/');
    b.append(patchsetId);
    return b.toString();
  }

  private final Backend server;
  private final NextReceivedBundleResponse in;
  private Repository db;
  private ObjectId tip;
  private int changeId;
  private int patchsetId;
  private String patchsetKey;

  UnpackBundleOp(final Backend be, final NextReceivedBundleResponse bundleInfo) {
    server = be;
    in = bundleInfo;
  }

  UpdateReceivedBundleRequest unpack() {
    final UpdateReceivedBundleRequest.Builder update;
    update = UpdateReceivedBundleRequest.newBuilder();
    update.setBundleKey(in.getBundleKey());
    try {
      unpackImpl();
      update.setStatusCode(CodeType.UNPACKED_OK);
    } catch (UnpackException ue) {
      update.setStatusCode(ue.status);
      update.setErrorDetails(ue.details);
      LOG.error("Unpacking bundle " + in.getBundleKey() + " failed.", ue);
    }
    return update.build();
  }

  private void unpackImpl() throws UnpackException {
    LOG.debug("Unpacking bundle " + in.getBundleKey());
    openRepository();
    unpackTip();
    createChanges(newCommits());
  }

  private void openRepository() throws UnpackException {
    try {
      db = server.getRepositoryCache().get(in.getDestProject());
    } catch (InvalidRepositoryException notGit) {
      final String m = "Repository \"" + in.getDestProject() + "\" unknown.";
      throw new UnpackException(CodeType.UNKNOWN_PROJECT, m, notGit);
    }
  }

  private void unpackTip() throws UnpackException {
    final Transport bundleTransport = openBundle();
    try {
      final FetchConnection fc = bundleTransport.openFetch();
      if (fc.getRefs().size() > 1) {
        final String m = "Bundle contains more than one head";
        throw new UnpackException(CodeType.INVALID_BUNDLE, m);
      } else if (fc.getRefs().size() == 0) {
        final String m = "Bundle contains no heads";
        throw new UnpackException(CodeType.INVALID_BUNDLE, m);
      }

      fc.fetch(NullProgressMonitor.INSTANCE, fc.getRefs());
      tip = fc.getRefs().iterator().next().getObjectId();
      LOG.debug("Unpacked " + tip.name() + " from " + in.getBundleKey());
    } catch (MissingBundlePrerequisiteException e) {
      throw new UnpackException(CodeType.MISSING_BASE, e.getMessage(), e);
    } catch (IOException readError) {
      final String m = "Processing the bundle stream failed";
      throw new UnpackException(CodeType.INVALID_BUNDLE, m, readError);
    } finally {
      bundleTransport.close();
    }
  }

  private List<RevCommit> newCommits() throws UnpackException {
    final RevWalk rw = new RevWalk(db);
    rw.sort(RevSort.REVERSE, true);
    rw.sort(RevSort.TOPO, true);

    try {
      rw.markStart(rw.parseCommit(tip));
    } catch (IOException e) {
      final String m = "Chain " + tip.name() + " is corrupt";
      throw new UnpackException(CodeType.INVALID_BUNDLE, m, e);
    }

    for (final Ref r : db.getAllRefs().values()) {
      try {
        rw.markUninteresting(rw.parseCommit(r.getObjectId()));
      } catch (IOException err) {
        final String m = "Local ref is invalid";
        throw new UnpackException(CodeType.SUSPEND_BUNDLE, m, err);
      }
    }

    try {
      final List<RevCommit> newList = new ArrayList<RevCommit>();
      RevCommit c;
      while ((c = rw.next()) != null) {
        // Ensure the parents are parsed so we know the parent's tree.
        // We need that later to compute a difference.
        //
        for (final RevCommit p : c.getParents()) {
          rw.parse(p);
        }
        newList.add(c);
      }
      return newList;
    } catch (IOException e) {
      final String m = "Chain " + tip.name() + " is corrupt";
      throw new UnpackException(CodeType.INVALID_BUNDLE, m, e);
    }
  }

  private void createChanges(final List<RevCommit> newCommits)
      throws UnpackException {
    for (final RevCommit c : newCommits) {
      if (submitChange(c)) {
        createChangeRef(c);
        server.asyncExec(new PatchSetUploader(server, db, c, patchsetKey));
      }
    }
  }

  private boolean submitChange(final RevCommit c) throws UnpackException {
    final SubmitChangeRequest.Builder req = SubmitChangeRequest.newBuilder();

    req.setOwner(in.getOwner());
    req.setDestBranchKey(in.getDestBranchKey());
    req.setCommit(GitMetaUtil.toGitCommit(c));

    final MutableBoolean continueCreation = new MutableBoolean();
    final SimpleController ctrl = new SimpleController();
    server.getChangeService().submitChange(ctrl, req.build(),
        new RpcCallback<SubmitChangeResponse>() {
          public void run(final SubmitChangeResponse rsp) {
            final SubmitChangeResponse.CodeType sc = rsp.getStatusCode();

            if (sc == SubmitChangeResponse.CodeType.CREATED) {
              changeId = rsp.getChangeId();
              patchsetId = rsp.getPatchsetId();
              patchsetKey = rsp.getPatchsetKey();
              continueCreation.value = true;
              LOG.debug("Commit " + c.getId().name() + " is change " + changeId
                  + " patchset " + patchsetId);

            } else if (sc == SubmitChangeResponse.CodeType.PATCHSET_EXISTS) {
              LOG.debug("Commit " + c.getId().name() + " exists in data store");

            } else {
              ctrl.setFailed("Unknown status " + sc.name());
            }
          }
        });
    if (ctrl.failed()) {
      throw new UnpackException(CodeType.SUSPEND_BUNDLE, ctrl.errorText());
    }
    return continueCreation.value;
  }

  private void createChangeRef(final RevCommit c) throws UnpackException {
    final String name = refOf(changeId, patchsetId);
    final RefUpdate.Result r;
    try {
      final RefUpdate u = db.updateRef(name);
      u.setNewObjectId(c.getId());
      u.setForceUpdate(true);
      u.setRefLogMessage("Change submitted", false);
      r = u.update();
    } catch (IOException err) {
      final String m = "Failure creating " + name;
      throw new UnpackException(CodeType.SUSPEND_BUNDLE, m, err);
    }

    if (r == RefUpdate.Result.NEW) {
    } else if (r == RefUpdate.Result.FAST_FORWARD) {
    } else if (r == RefUpdate.Result.FORCED) {
    } else if (r == RefUpdate.Result.NO_CHANGE) {
    } else {
      final String m = "Failure creating " + name + ": " + r.name();
      throw new UnpackException(CodeType.SUSPEND_BUNDLE, m);
    }
  }

  private TransportBundleStream openBundle() {
    final URIish uri = makeURI(in);
    return new TransportBundleStream(db, uri, new BundleStream());
  }

  private static URIish makeURI(final NextReceivedBundleResponse in) {
    URIish u = new URIish();
    u = u.setScheme("codereview-bundle");
    u = u.setPath(in.getBundleKey());
    return u;
  }

  private class BundleStream extends InputStream {
    private int segmentId = 1;
    private final int totalSegments = in.getNSegments();
    private InputStream stream = in.getBundleData().newInput();

    @Override
    public int read() throws IOException {
      for (;;) {
        if (stream == null) {
          return -1;
        }

        final int r = stream.read();
        if (r < 0) {
          openNextStream();
        } else {
          return r;
        }
      }
    }

    @Override
    public int read(final byte[] b, final int off, final int len)
        throws IOException {
      for (;;) {
        if (stream == null) {
          return -1;
        }

        final int r = stream.read(b, off, len);
        if (r < 0) {
          openNextStream();
        } else {
          return r;
        }
      }
    }

    private void openNextStream() throws IOException {
      if (segmentId >= totalSegments) {
        stream = null;
        return;
      }

      segmentId++;
      final BundleSegmentRequest.Builder req;

      req = BundleSegmentRequest.newBuilder();
      req.setBundleKey(in.getBundleKey());
      req.setSegmentId(segmentId);

      final SimpleController ctrl = new SimpleController();
      server.getBundleStoreService().bundleSegment(ctrl, req.build(),
          new RpcCallback<BundleSegmentResponse>() {
            public void run(final BundleSegmentResponse rsp) {
              final BundleSegmentResponse.CodeType sc = rsp.getStatusCode();
              if (sc == BundleSegmentResponse.CodeType.DATA) {
                stream = rsp.getBundleData().newInput();
              } else {
                ctrl.setFailed(sc.name());
              }
            }
          });
      if (ctrl.failed()) {
        throw new IOException("Bundle" + in.getBundleKey() + " segment "
            + segmentId + " unavailable: " + ctrl.errorText());
      }
    }
  }
}