Esempio n. 1
0
  @Override
  protected void run() throws Exception {
    if (squash && ff == FastForwardMode.NO_FF) throw die(CLIText.get().cannotCombineSquashWithNoff);
    // determine the merge strategy
    if (strategyName != null) {
      mergeStrategy = MergeStrategy.get(strategyName);
      if (mergeStrategy == null)
        throw die(MessageFormat.format(CLIText.get().unknownMergeStrategy, strategyName));
    }

    // determine the other revision we want to merge with HEAD
    final Ref srcRef = db.getRef(ref);
    final ObjectId src = db.resolve(ref + "^{commit}"); // $NON-NLS-1$
    if (src == null) throw die(MessageFormat.format(CLIText.get().refDoesNotExistOrNoCommit, ref));

    Ref oldHead = db.getRef(Constants.HEAD);
    Git git = new Git(db);
    MergeCommand mergeCmd =
        git.merge()
            .setStrategy(mergeStrategy)
            .setSquash(squash)
            .setFastForward(ff)
            .setCommit(!noCommit);
    if (srcRef != null) mergeCmd.include(srcRef);
    else mergeCmd.include(src);
    MergeResult result;
    try {
      result = mergeCmd.call();
    } catch (CheckoutConflictException e) {
      result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT
    }

    switch (result.getMergeStatus()) {
      case ALREADY_UP_TO_DATE:
        if (squash) outw.print(CLIText.get().nothingToSquash);
        outw.println(CLIText.get().alreadyUpToDate);
        break;
      case FAST_FORWARD:
        ObjectId oldHeadId = oldHead.getObjectId();
        outw.println(
            MessageFormat.format(
                CLIText.get().updating,
                oldHeadId.abbreviate(7).name(),
                result.getNewHead().abbreviate(7).name()));
        outw.println(result.getMergeStatus().toString());
        break;
      case CHECKOUT_CONFLICT:
        outw.println(CLIText.get().mergeCheckoutConflict);
        for (String collidingPath : result.getCheckoutConflicts())
          outw.println("\t" + collidingPath); // $NON-NLS-1$
        outw.println(CLIText.get().mergeCheckoutFailed);
        break;
      case CONFLICTING:
        for (String collidingPath : result.getConflicts().keySet())
          outw.println(MessageFormat.format(CLIText.get().mergeConflict, collidingPath));
        outw.println(CLIText.get().mergeFailed);
        break;
      case FAILED:
        for (Map.Entry<String, MergeFailureReason> entry : result.getFailingPaths().entrySet())
          switch (entry.getValue()) {
            case DIRTY_WORKTREE:
            case DIRTY_INDEX:
              outw.println(CLIText.get().dontOverwriteLocalChanges);
              outw.println("        " + entry.getKey()); // $NON-NLS-1$
              break;
            case COULD_NOT_DELETE:
              outw.println(CLIText.get().cannotDeleteFile);
              outw.println("        " + entry.getKey()); // $NON-NLS-1$
              break;
          }
        break;
      case MERGED:
        String name;
        if (!isMergedInto(oldHead, src)) name = mergeStrategy.getName();
        else name = "recursive"; // $NON-NLS-1$
        outw.println(MessageFormat.format(CLIText.get().mergeMadeBy, name));
        break;
      case MERGED_NOT_COMMITTED:
        outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
        break;
      case MERGED_SQUASHED:
      case FAST_FORWARD_SQUASHED:
      case MERGED_SQUASHED_NOT_COMMITTED:
        outw.println(CLIText.get().mergedSquashed);
        outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
        break;
      case ABORTED:
        throw die(CLIText.get().ffNotPossibleAborting);
      case NOT_SUPPORTED:
        outw.println(MessageFormat.format(CLIText.get().unsupportedOperation, result.toString()));
    }
  }
Esempio n. 2
0
  @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();
    }
  }