summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/google/gerrit/server/patch/PatchDetailAction.java
blob: 9c4155cece31b08084469dd00c9836b399b56a19 (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
// 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.gerrit.server.patch;

import com.google.gerrit.client.data.AccountInfoCacheFactory;
import com.google.gerrit.client.data.LineWithComments;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.Change;
import com.google.gerrit.client.reviewdb.Patch;
import com.google.gerrit.client.reviewdb.PatchContent;
import com.google.gerrit.client.reviewdb.PatchLineComment;
import com.google.gerrit.client.reviewdb.PatchSet;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.BaseServiceImplementation;
import com.google.gerrit.client.rpc.Common;
import com.google.gerrit.client.rpc.CorruptEntityException;
import com.google.gerrit.client.rpc.NoDifferencesException;
import com.google.gerrit.client.rpc.NoSuchEntityException;
import com.google.gerrit.client.rpc.BaseServiceImplementation.Action;
import com.google.gerrit.client.rpc.BaseServiceImplementation.Failure;
import com.google.gerrit.git.InvalidRepositoryException;
import com.google.gerrit.git.RepositoryCache;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.ResultSet;

import org.spearce.jgit.errors.IncorrectObjectTypeException;
import org.spearce.jgit.lib.AnyObjectId;
import org.spearce.jgit.lib.Constants;
import org.spearce.jgit.lib.ObjectId;
import org.spearce.jgit.lib.ObjectLoader;
import org.spearce.jgit.lib.Repository;
import org.spearce.jgit.patch.CombinedFileHeader;
import org.spearce.jgit.patch.FileHeader;
import org.spearce.jgit.patch.FormatError;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


abstract class PatchDetailAction<T> implements Action<T> {
  protected static final byte[] EMPTY_FILE = {};

  protected final Patch.Key patchKey;
  protected final List<PatchSet.Id> requestedVersions;
  protected final boolean direct;
  private final RepositoryCache repoCache;
  private Repository repo;
  protected Change change;
  protected Patch patch;
  protected FileHeader file;
  protected int fileCnt;
  protected AccountInfoCacheFactory accountInfo;
  protected Account.Id me;

  protected HashMap<Integer, List<PatchLineComment>> published[];
  protected HashMap<Integer, List<PatchLineComment>> drafted[];

  PatchDetailAction(final RepositoryCache rc, final Patch.Key key,
      final List<PatchSet.Id> fileVersions) {
    this.repoCache = rc;
    this.patchKey = key;
    this.requestedVersions = fileVersions;
    this.direct = isBaseAndPatchOnly(key, fileVersions);
  }

  private static boolean isBaseAndPatchOnly(final Patch.Key key,
      final List<PatchSet.Id> v) {
    if (v == null || v.size() < 2) {
      return true;
    }
    for (int i = 0; i < v.size() - 1; i++) {
      if (!PatchSet.BASE.equals(v.get(i))) {
        return false;
      }
    }
    return v.equals(key.getParentKey());
  }

  protected void init(final ReviewDb db) throws OrmException, Failure {
    patch = db.patches().get(patchKey);
    if (patch == null) {
      throw new Failure(new NoSuchEntityException());
    }
    change = db.changes().get(patch.getKey().getParentKey().getParentKey());
    BaseServiceImplementation.assertCanRead(change);

    file = readFileHeader(db);
    if (file instanceof CombinedFileHeader) {
      fileCnt = ((CombinedFileHeader) file).getParentCount() + 1;
    } else {
      fileCnt = 2;
    }

    accountInfo = new AccountInfoCacheFactory(db);
    me = Common.getAccountId();

    published = new HashMap[fileCnt];
    for (int n = 0; n < fileCnt; n++) {
      published[n] = new HashMap<Integer, List<PatchLineComment>>();
    }
    indexComments(published, direct ? db.patchComments().published(patchKey)
        : db.patchComments().published(change.getId(), patchKey.get()));

    if (me != null) {
      drafted = new HashMap[fileCnt];
      for (int n = 0; n < fileCnt; n++) {
        drafted[n] = new HashMap<Integer, List<PatchLineComment>>();
      }
      indexComments(drafted, direct ? db.patchComments().draft(patchKey, me)
          : db.patchComments().draft(change.getId(), patchKey.get(), me));
    }
  }

  protected FileHeader readFileHeader(final ReviewDb db) throws Failure,
      OrmException {
    if (direct) {
      return readCachedFileHeader(db);
    }

    // TODO Fix this gross hack so we aren't running git diff-tree for
    // an interdiff file side by side view.
    //
    final Map<PatchSet.Id, PatchSet> psMap =
        db.patchSets().toMap(db.patchSets().byChange(change.getId()));
    final Repository repo = openRepository();
    final List<String> args = new ArrayList<String>();
    args.add("git");
    args.add("--git-dir=.");
    args.add("diff-tree");
    args.add("--full-index");
    if (requestedVersions.size() > 2) {
      args.add("--cc");
    } else {
      args.add("--unified=5");
    }
    for (int i = 0; i < requestedVersions.size(); i++) {
      final PatchSet.Id psi = requestedVersions.get(i);
      if (psi == null) {
        throw new Failure(new NoSuchEntityException());

      } else if (psi.equals(PatchSet.BASE)) {
        final PatchSet p = psMap.get(patchKey.getParentKey());
        if (p == null || p.getRevision() == null
            || p.getRevision().get() == null) {
          throw new Failure(new NoSuchEntityException());
        }
        args.add(p.getRevision().get() + "^" + (i + 1));

      } else {
        final PatchSet p = psMap.get(psi);
        if (p == null || p.getRevision() == null
            || p.getRevision().get() == null) {
          throw new Failure(new NoSuchEntityException());
        }
        args.add(p.getRevision().get());
      }
    }
    args.add("--");
    args.add(patch.getFileName());

    try {
      final Process proc =
          Runtime.getRuntime().exec(args.toArray(new String[args.size()]),
              null, repo.getDirectory());
      try {
        final org.spearce.jgit.patch.Patch p =
            new org.spearce.jgit.patch.Patch();
        proc.getOutputStream().close();
        proc.getErrorStream().close();
        p.parse(proc.getInputStream());
        proc.getInputStream().close();
        if (p.getFiles().isEmpty())
          throw new Failure(new NoDifferencesException());
        if (p.getFiles().size() != 1)
          throw new IOException("unexpected file count back");
        return p.getFiles().get(0);
      } finally {
        try {
          if (proc.waitFor() != 0) {
            throw new IOException("git diff-tree exited abnormally");
          }
        } catch (InterruptedException ie) {
        }
      }
    } catch (IOException e) {
      throw new Failure(e);
    }
  }

  protected FileHeader readCachedFileHeader(final ReviewDb db) throws Failure,
      OrmException {
    return parse(patch, read(db, patch));
  }

  protected List<Patch> history(final ReviewDb db) throws OrmException {
    return db.patches().history(change.getId(), patch.getFileName()).toList();
  }

  protected void indexComments(
      final HashMap<Integer, List<PatchLineComment>>[] out,
      final ResultSet<PatchLineComment> comments) {
    for (final PatchLineComment c : comments) {
      short fileId;
      if (direct) {
        fileId = c.getSide();
      } else {
        for (fileId = 0; fileId < requestedVersions.size(); fileId++) {
          final PatchSet.Id i = requestedVersions.get(fileId);
          if (PatchSet.BASE.equals(i) && c.getSide() == fileId
              && patchKey.equals(c.getKey().getParentKey())) {
            break;
          }
          if (c.getSide() == requestedVersions.size() - 1
              && i.equals(c.getKey().getParentKey().getParentKey())) {
            break;
          }
        }
      }

      if (0 <= fileId && fileId < out.length) {
        final HashMap<Integer, List<PatchLineComment>> m = out[fileId];
        List<PatchLineComment> l = m.get(c.getLine());
        if (l == null) {
          l = new ArrayList<PatchLineComment>(4);
          m.put(c.getLine(), l);
        }
        l.add(c);
      }
    }
  }

  protected void addComments(final LineWithComments pLine,
      final HashMap<Integer, List<PatchLineComment>>[] cache, final int side,
      final int line) {
    List<PatchLineComment> l = cache[side].remove(line);
    if (l != null) {
      for (final PatchLineComment c : l) {
        pLine.addComment(c);
        accountInfo.want(c.getAuthor());
      }
    }
  }

  protected Repository openRepository() throws Failure {
    if (repo == null) {
      if (change.getDest() == null) {
        throw new Failure(new CorruptEntityException(change.getId()));
      }
      try {
        repo = repoCache.get(change.getDest().getParentKey().get());
      } catch (InvalidRepositoryException err) {
        throw new Failure(err);
      }
    }
    return repo;
  }

  protected byte[] read(final Repository repo, final AnyObjectId id)
      throws Failure {
    if (id == null || ObjectId.zeroId().equals(id)) {
      return EMPTY_FILE;
    }
    try {
      final ObjectLoader ldr = repo.openObject(id);
      if (ldr == null) {
        throw new Failure(new CorruptEntityException(patch.getKey()));
      }
      final byte[] content = ldr.getCachedBytes();
      if (ldr.getType() != Constants.OBJ_BLOB) {
        throw new Failure(new IncorrectObjectTypeException(id.toObjectId(),
            Constants.TYPE_BLOB));
      }
      return content;
    } catch (IOException err) {
      throw new Failure(err);
    }
  }

  protected static String read(final ReviewDb db, final Patch patch)
      throws Failure, OrmException {
    final PatchContent.Key key = patch.getContent();
    if (key == null) {
      throw new Failure(new CorruptEntityException(patch.getKey()));
    }

    final PatchContent pc = db.patchContents().get(key);
    if (pc == null || pc.getContent() == null) {
      throw new Failure(new CorruptEntityException(patch.getKey()));
    }

    return pc.getContent();
  }

  protected static FileHeader parse(final Patch patch, final String content)
      throws Failure {
    final byte[] buf = Constants.encode(content);
    final org.spearce.jgit.patch.Patch p = new org.spearce.jgit.patch.Patch();
    p.parse(buf, 0, buf.length);
    for (final FormatError err : p.getErrors()) {
      if (err.getSeverity() == FormatError.Severity.ERROR) {
        throw new Failure(new CorruptEntityException(patch.getKey()));
      }
    }
    if (p.getFiles().size() != 1) {
      throw new Failure(new CorruptEntityException(patch.getKey()));
    }
    return p.getFiles().get(0);
  }
}