protected void run()

in org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java [109:240]


	protected void run() {
		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", //$NON-NLS-1$ //$NON-NLS-2$
					OBJECT_ID_ABBREV_STRING_LENGTH);
		}
		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$
		}

		try (ObjectReader reader = db.newObjectReader();
				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<>(2);
				for (RevCommit c : reverseRange) {
					if (c.has(RevFlag.UNINTERESTING)) {
						rangeStart = c;
					} else {
						rangeEnd.add(c);
					}
				}
				generator.reverse(rangeStart, rangeEnd);
			} else if (revision != null) {
				ObjectId rev = db.resolve(revision + "^{commit}"); //$NON-NLS-1$
				if (rev == null) {
					throw die(MessageFormat.format(CLIText.get().noSuchRef,
							revision));
				}
				generator.push(null, rev);
			} else {
				generator.prepareHead();
			}

			blame = BlameResult.create(generator);
			if (blame == null) {
				throw die(MessageFormat.format(CLIText.get().noSuchPathInRef,
						file, revision != null ? revision : Constants.HEAD));
			}
			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(reader, c));
					}
					authorWidth = Math.max(authorWidth, author(line).length());
					dateWidth = Math.max(dateWidth, date(line).length());
					pathWidth = Math.max(pathWidth, path(line).length());
				} else if (c == null) {
					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
						&& sameCommit(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(reader, 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
						&& sameCommit(blame.getSourceCommit(line), c));
			}
		} catch (NoWorkTreeException | NoHeadException | IOException e) {
			throw die(e.getMessage(), e);
		}
	}