private void markReachable(final Set<ObjectId> have, final int maxTime) throws IOException {
    for (final Ref r : local.getAllRefs().values()) {
      try {
        final RevCommit o = walk.parseCommit(r.getObjectId());
        o.add(REACHABLE);
        reachableCommits.add(o);
      } catch (IOException readError) {
        // If we cannot read the value of the ref skip it.
      }
    }

    for (final ObjectId id : have) {
      try {
        final RevCommit o = walk.parseCommit(id);
        o.add(REACHABLE);
        reachableCommits.add(o);
      } catch (IOException readError) {
        // If we cannot read the value of the ref skip it.
      }
    }

    if (maxTime > 0) {
      // Mark reachable commits until we reach maxTime. These may
      // wind up later matching up against things we want and we
      // can avoid asking for something we already happen to have.
      //
      final Date maxWhen = new Date(maxTime * 1000L);
      walk.sort(RevSort.COMMIT_TIME_DESC);
      walk.markStart(reachableCommits);
      walk.setRevFilter(CommitTimeRevFilter.after(maxWhen));
      for (; ; ) {
        final RevCommit c = walk.next();
        if (c == null) break;
        if (c.has(ADVERTISED) && !c.has(COMMON)) {
          // This is actually going to be a common commit, but
          // our peer doesn't know that fact yet.
          //
          c.add(COMMON);
          c.carry(COMMON);
          reachableCommits.add(c);
        }
      }
    }
  }
示例#2
0
  // printout all commit messages in a given range -> use and reset an existing RevWalk
  private void printCommits(String outFile, RevWalk walk)
      throws IOException, NoHeadException, GitAPIException {

    PrintWriter pout = new PrintWriter(new FileWriter(outFile), true);
    RevCommit c = walk.next();
    while (c != null && !c.has(RevFlag.UNINTERESTING)) {
      pout.println(printCommit(c));
      c = walk.next();
    }
    walk.reset();
    pout.close();
  }
示例#3
0
  private void checkConnectivity() throws IOException {
    ObjectIdSubclassMap<ObjectId> baseObjects = null;
    ObjectIdSubclassMap<ObjectId> providedObjects = null;

    if (checkReferencedIsReachable) {
      baseObjects = parser.getBaseObjectIds();
      providedObjects = parser.getNewObjectIds();
    }
    parser = null;

    final ObjectWalk ow = new ObjectWalk(db);
    ow.setRetainBody(false);
    if (checkReferencedIsReachable) {
      ow.sort(RevSort.TOPO);
      if (!baseObjects.isEmpty()) ow.sort(RevSort.BOUNDARY, true);
    }

    for (final ReceiveCommand cmd : commands) {
      if (cmd.getResult() != Result.NOT_ATTEMPTED) continue;
      if (cmd.getType() == ReceiveCommand.Type.DELETE) continue;
      ow.markStart(ow.parseAny(cmd.getNewId()));
    }
    for (final ObjectId have : advertisedHaves) {
      RevObject o = ow.parseAny(have);
      ow.markUninteresting(o);

      if (checkReferencedIsReachable && !baseObjects.isEmpty()) {
        o = ow.peel(o);
        if (o instanceof RevCommit) o = ((RevCommit) o).getTree();
        if (o instanceof RevTree) ow.markUninteresting(o);
      }
    }

    RevCommit c;
    while ((c = ow.next()) != null) {
      if (checkReferencedIsReachable //
          && !c.has(RevFlag.UNINTERESTING) //
          && !providedObjects.contains(c))
        throw new MissingObjectException(c, Constants.TYPE_COMMIT);
    }

    RevObject o;
    while ((o = ow.nextObject()) != null) {
      if (o.has(RevFlag.UNINTERESTING)) continue;

      if (checkReferencedIsReachable) {
        if (providedObjects.contains(o)) continue;
        else throw new MissingObjectException(o, o.getType());
      }

      if (o instanceof RevBlob && !db.hasObject(o))
        throw new MissingObjectException(o, Constants.TYPE_BLOB);
    }

    if (checkReferencedIsReachable) {
      for (ObjectId id : baseObjects) {
        o = ow.parseAny(id);
        if (!o.has(RevFlag.UNINTERESTING)) throw new MissingObjectException(o, o.getType());
      }
    }
  }
示例#4
0
文件: Blame.java 项目: dgreensp/jgit
  @Override
  protected void run() throws Exception {
    if (file == null) {
      if (revision == null) throw die(CLIText.get().fileIsRequired);
      file = revision;
      revision = null;
    }

    boolean autoAbbrev = abbrev == 0;
    if (abbrev == 0)
      abbrev = db.getConfig().getInt("core", "abbrev", 7); // $NON-NLS-1$ //$NON-NLS-2$
    if (!showBlankBoundary)
      root =
          db.getConfig().getBoolean("blame", "blankboundary", false); // $NON-NLS-1$ //$NON-NLS-2$
    if (!root)
      root = db.getConfig().getBoolean("blame", "showroot", false); // $NON-NLS-1$ //$NON-NLS-2$

    if (showRawTimestamp) dateFmt = new SimpleDateFormat("ZZZZ"); // $NON-NLS-1$
    else dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ZZZZ"); // $NON-NLS-1$

    reader = db.newObjectReader();
    try (BlameGenerator generator = new BlameGenerator(db, file)) {
      RevFlag scanned = generator.newFlag("SCANNED"); // $NON-NLS-1$
      generator.setTextComparator(comparator);

      if (!reverseRange.isEmpty()) {
        RevCommit rangeStart = null;
        List<RevCommit> rangeEnd = new ArrayList<RevCommit>(2);
        for (RevCommit c : reverseRange) {
          if (c.has(RevFlag.UNINTERESTING)) rangeStart = c;
          else rangeEnd.add(c);
        }
        generator.reverse(rangeStart, rangeEnd);
      } else if (revision != null) {
        generator.push(null, db.resolve(revision + "^{commit}")); // $NON-NLS-1$
      } else {
        generator.push(null, db.resolve(Constants.HEAD));
        if (!db.isBare()) {
          DirCache dc = db.readDirCache();
          int entry = dc.findEntry(file);
          if (0 <= entry) generator.push(null, dc.getEntry(entry).getObjectId());

          File inTree = new File(db.getWorkTree(), file);
          if (db.getFS().isFile(inTree)) generator.push(null, new RawText(inTree));
        }
      }

      blame = BlameResult.create(generator);
      begin = 0;
      end = blame.getResultContents().size();
      if (rangeString != null) parseLineRangeOption();
      blame.computeRange(begin, end);

      int authorWidth = 8;
      int dateWidth = 8;
      int pathWidth = 1;
      int maxSourceLine = 1;
      for (int line = begin; line < end; line++) {
        RevCommit c = blame.getSourceCommit(line);
        if (c != null && !c.has(scanned)) {
          c.add(scanned);
          if (autoAbbrev) abbrev = Math.max(abbrev, uniqueAbbrevLen(c));
          authorWidth = Math.max(authorWidth, author(line).length());
          dateWidth = Math.max(dateWidth, date(line).length());
          pathWidth = Math.max(pathWidth, path(line).length());
        }
        while (line + 1 < end && blame.getSourceCommit(line + 1) == c) line++;
        maxSourceLine = Math.max(maxSourceLine, blame.getSourceLine(line));
      }

      String pathFmt = MessageFormat.format(" %{0}s", valueOf(pathWidth)); // $NON-NLS-1$
      String numFmt =
          MessageFormat.format(
              " %{0}d", //$NON-NLS-1$
              valueOf(1 + (int) Math.log10(maxSourceLine + 1)));
      String lineFmt =
          MessageFormat.format(
              " %{0}d) ", //$NON-NLS-1$
              valueOf(1 + (int) Math.log10(end + 1)));
      String authorFmt =
          MessageFormat.format(
              " (%-{0}s %{1}s", //$NON-NLS-1$
              valueOf(authorWidth), valueOf(dateWidth));

      for (int line = begin; line < end; ) {
        RevCommit c = blame.getSourceCommit(line);
        String commit = abbreviate(c);
        String author = null;
        String date = null;
        if (!noAuthor) {
          author = author(line);
          date = date(line);
        }
        do {
          outw.print(commit);
          if (showSourcePath) outw.format(pathFmt, path(line));
          if (showSourceLine) outw.format(numFmt, valueOf(blame.getSourceLine(line) + 1));
          if (!noAuthor) outw.format(authorFmt, author, date);
          outw.format(lineFmt, valueOf(line + 1));
          outw.flush();
          blame.getResultContents().writeLine(outs, line);
          outs.flush();
          outw.print('\n');
        } while (++line < end && blame.getSourceCommit(line) == c);
      }
    } finally {
      reader.close();
    }
  }