/* git-reports.c for creating git equivalent of cvs-reports. * * Copyright Galt Barber 2010. * * Anyone is free to use it. * Just include me in your credits. * Likewise cvs-reports was created by Mark Diekhans. */ #include "common.h" #include "options.h" #include "dystring.h" #include "errabort.h" #include "hash.h" #include "linefile.h" #include "htmshell.h" #include "portable.h" struct hash *userHash = NULL; struct slName *users = NULL; char *startTag = NULL; char *endTag = NULL; char *startDate = NULL; char *endDate = NULL; char *title = NULL; char *repoDir = NULL; char *outDir = NULL; char *outPrefix = NULL; int contextSize; char gitCmd[1024]; char *tempMakeDiffName = NULL; struct files { struct files *next; char type; char *path; int linesChanged; }; struct commit { struct commit *next; int commitNumber; // used for sorting fileviews char *commitId; char *merge; char *author; char *date; char *comment; struct files *files; }; struct comFile { struct comFile *next; struct files *f; struct commit *commit; }; void usage(char *msg) /* Explain usage and exit. */ { errAbort( "%s\n\n" "git-reports - produce source code reports useful for code-review on git repository \n" "\n" "Usage:\n" " git-reports startTag endTag startDate endDate title repoDir outDir outPrefix\n" "where " " startTag and endTag are repository tags marking the beginning and end of the git range\n" " startDate and endDate and title are just strings that get printed on the report\n" " title is usually the branch number, e.g. v225\n" " repoDir is where the git repository (use absolute path)\n" " outDir is the output directory (use absolute path).\n" " outPrefix is typically \"branch\" or \"review\" directory.\n" " --context=N - show N lines of context around a change\n" " --help - this help screen\n", msg); } static struct optionSpec options[] = { {"-context", OPTION_INT}, {"-help", OPTION_BOOLEAN}, {NULL, 0}, }; void runShell(char *cmd) /* Run a command and do simple error-checking */ { int exitCode = system(cmd); if (exitCode != 0) errAbort("system command [%s] failed with exitCode %d", cmd, exitCode); } void makeDiffAndSplit(struct commit *c, char *u, boolean full); // FOREWARD REFERENCE struct commit* getCommits() /* Get all commits from startTag to endTag */ { int numCommits = 0; safef(gitCmd,sizeof(gitCmd), "" "git log %s..%s --name-status > commits.tmp" , startTag, endTag); runShell(gitCmd); struct lineFile *lf = lineFileOpen("commits.tmp", TRUE); int lineSize; char *line; struct commit *commits = NULL, *commit = NULL; struct files *files = NULL, *f = NULL; char *sep = ""; while (lineFileNext(lf, &line, &lineSize)) { char *w = nextWord(&line); AllocVar(commit); if (!sameString("commit", w)) errAbort("expected keyword commit parsing commits.tmp\n"); commit->commitId = cloneString(nextWord(&line)); commit->commitNumber = ++numCommits; lineFileNext(lf, &line, &lineSize); w = nextWord(&line); if (sameString("Merge:", w)) { commit->merge = cloneString(line); lineFileNext(lf, &line, &lineSize); w = nextWord(&line); } if (!sameString("Author:", w)) errAbort("expected keyword Author: parsing commits.tmp\n"); /* by request, keep just the email account name */ char *lc = strchr(line, '<'); if (!lc) errAbort("expected '<' char in email address in Author: parsing commits.tmp\n"); ++lc; char *rc = strchr(lc, '>'); if (!rc) errAbort("expected '>' char in email address in Author: parsing commits.tmp\n"); char *ac = strchr(lc, '@'); if (ac) rc = ac; commit->author = cloneStringZ(lc, rc-lc); lineFileNext(lf, &line, &lineSize); w = nextWord(&line); if (!sameString("Date:", w)) errAbort("expected keyword Date: parsing commits.tmp\n"); commit->date = cloneString(line); lineFileNext(lf, &line, &lineSize); if (!sameString("", line)) errAbort("expected blank line parsing commits.tmp\n"); /* collect the comment-lines */ struct dyString *dy = NULL; dy = dyStringNew(0); sep = ""; files = NULL; while (lineFileNext(lf, &line, &lineSize)) { if (sameString("", line)) break; w = skipLeadingSpaces(line); dyStringPrintf(dy, "%s%s", w, sep); sep = "\n"; } commit->comment = cloneString(dy->string); freeDyString(&dy); if (commit->merge) { makeDiffAndSplit(commit, "getFileNamesForMergeCommit", FALSE); // special tricks to get this list (status field will not be applicable). } else { /* collect the files-list */ while (lineFileNext(lf, &line, &lineSize)) { if (sameString("", line)) break; AllocVar(f); w = nextWord(&line); f->type = w[0]; f->path = cloneString(line); slAddHead(&files, f); } slReverse(&files); commit->files = files; } if (!startsWith("Merge branch 'master' of", commit->comment) && !endsWith(commit->comment, "elease log update")) /* filter out automatic release log commits */ slAddHead(&commits, commit); verbose(2, "commitId: %s\n" "author: %s\n" "date: %s\n" "comment: [%s]\n" "file(s): \n" , commit->commitId , commit->author , commit->date , commit->comment); for (f=commit->files; f; f = f->next) { verbose(2, "%c %s\n", f->type, f->path); // anything other than M or A? if (f->type != 'M' && f->type != 'A' ) verbose(2, "special type: %c %s\n", f->type, f->path); } verbose(2, "------------\n"); } lineFileClose(&lf); /* We want to keep them chronological order, so do not need slReverse since the addHead reversed git log's rev chron order already */ unlink("commits.tmp"); return commits; } int makeHtml(char *diffPath, char *htmlPath, char *path, char *commitId) /* Make a color-coded html diff * Return the number of lines changed */ { int linesChanged = 0; FILE *h = mustOpen(htmlPath, "w"); struct lineFile *lf = lineFileOpen(diffPath, TRUE); int lineSize; char *line; char *xline = NULL; char fmtString[256]; boolean inBody = FALSE; boolean inBlock = TRUE; int blockP = 0, blockN = 0; fprintf(h, "\n
\n\n", path, commitId);
boolean hasMore = TRUE;
boolean combinedDiff = FALSE;
while (hasMore)
{
boolean checkEob = FALSE;
hasMore = lineFileNext(lf, &line, &lineSize);
if (hasMore)
{
char *color = NULL;
xline = htmlEncode(line);
if ((line[0] == '-') || (combinedDiff && (line[1] == '-')))
{
color = "#FF9999"; /* deleted text light red */
if (inBody)
{
inBlock = TRUE;
++blockN;
}
}
else if ((line[0] == '+') || (combinedDiff && (line[1] == '+')))
{
color = "#99FF99"; /* added text light green */
if (inBody)
{
inBlock = TRUE;
++blockP;
}
}
else
{
if (line[0] == '@')
{
color = "#FFFF99"; /* diff control text light yellow (red+green) */
if (!combinedDiff && startsWith("@@@", line))
combinedDiff = TRUE;
}
checkEob = TRUE;
}
if (color)
safef(fmtString, sizeof(fmtString), "%%s\n", color);
else
safef(fmtString, sizeof(fmtString), "%%s\n");
fprintf(h, fmtString, xline);
if (line[0] == '@')
inBody = TRUE;
freeMem(xline);
}
else
{
checkEob = TRUE;
}
if (checkEob && inBlock)
{
inBlock = FALSE;
if (blockP >= blockN)
linesChanged += blockP;
else
linesChanged += blockN;
blockP = 0;
blockN = 0;
}
}
lineFileClose(&lf);
fprintf(h, "
\n