/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/*
Copyright (c) 2013-2014 D. Richard Hipp
This program is free software; you can redistribute it and/or
modify it under the terms of the Simplified BSD License (also
known as the "2-Clause License" or "FreeBSD License".)
This program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.
Author contact information:
drh@hwaci.com
http://www.hwaci.com/drh/
*****************************************************************************
This file implements a basic timeline [test] app using the
libfossil API.
*/
#include "fossil-scm/fossil-cli.h"
static struct TLApp_ {
int limit;
char showFiles;
char utc;
char const * ckoutUuid;
char * filterTag;
char * filterBranch;
char * filterComment;
} TLApp = {
-1/*limit*/,
0/*showFiles*/,
0/*utc*/,
NULL/*ckoutUuid*/,
NULL/*filterTag*/,
NULL/*filterBranch*/,
NULL/*filterComment*/
};
/**
fsl_stmt_each_f() implementation for a basic timeline view. The
state parameter is ignored.
*/
static int stmt_each_f_timeline1( fsl_stmt * q, void * state ){
fsl_cx * f = fcli.f;
char const * x;
char const * uuid = fsl_stmt_g_text(q,0/*uuid*/,NULL);
char const * type = fsl_stmt_g_text(q,3/*type*/,NULL);
char const isCheckin = 'c'==*type;;
switch(*type){
case 'c': type = "checkin"; break;
case 'w': type = "wiki"; break;
case 'g': type = "tag"; break;
case 'e': type = "event"; break;
case 't': type = "ticket"; break;
};
/* Tip to copy/pasters: q->rowCount can be used to determine what
row number we're on. It starts counting at 1, not 0.
*/
fsl_outputf(f, "%-8s[%.*s] @ %s by [%s]",
type, 12, uuid,
fsl_stmt_g_text(q,1/*time*/,NULL),
fsl_stmt_g_text(q,2/*user*/,NULL)
);
if( (x = fsl_stmt_g_text(q,4/*branch*/,NULL)) ){
fsl_outputf(f, " branch [%s]", x);
}
if(isCheckin && TLApp.ckoutUuid && 0==fsl_uuidcmp(uuid,TLApp.ckoutUuid)){
fsl_outputf(f," *CURRENT*");
}
fsl_outputf(f, "\n\n\t%s\n",
fsl_stmt_g_text(q,5/*comment*/,NULL));
if(isCheckin && TLApp.showFiles){
fsl_stmt * st = NULL;
fsl_db * db = fsl_cx_db_repo(f);
int rc;
char doneHead = 0;
assert(db);
rc = fsl_db_prepare_cached(db, &st,
"SELECT "
/*0*/"bf.uuid, "
/*1*/"filename.name fname, "
/*2*/"bf.size, "
/*3*/"mlink.pid, "
/*4*/"mlink.fid "
"FROM mlink, filename "
"LEFT JOIN blob bf -- FILE blob\n"
" ON bf.rid=mlink.fid "
"LEFT JOIN blob bm -- MANIFEST/checkin blob\n"
" ON bm.rid=mlink.mid "
"WHERE "
"bm.uuid = ? "
"AND filename.fnid=mlink.fnid "
/* "AND bf.rid=mlink.fid " */
/*"AND bm.rid=mlink.mid "*/
"ORDER BY filename.name %s",
fsl_cx_filename_collation(f));
if(rc){
return fsl_cx_uplift_db_error(f, db);
}
rc = fsl_stmt_bind_text(st, 1, uuid, FSL_UUID_STRLEN, 0);
assert(0==rc);
while(FSL_RC_STEP_ROW==(rc=fsl_stmt_step(st))){
char const * changeType;
if(!doneHead){
doneHead = 1;
fsl_outputf(f,"\n\t%-11s%s %13s Name\n", "Change", "File UUID", "Size");
}
if(0==fsl_stmt_g_id(st, 4/*==>fid*/)){
fsl_outputf(f, "\t%-35s%s\n", "REMOVED",
fsl_stmt_g_text(st, 1, NULL));
continue;
}else if(0==fsl_stmt_g_id(st, 3/*==>pid*/)){
changeType = "ADDED";
}else{
changeType = "MODIFIED";
}
fsl_outputf(f,"\t%-11s%.*s %10"FSL_INT64_T_PFMT" %s\n",
changeType,
12, fsl_stmt_g_text(st, 0, NULL),
(fsl_int64_t)fsl_stmt_g_int64(st, 2),
fsl_stmt_g_text(st, 1, NULL));
}
fsl_stmt_cached_yield(st);
}
fsl_output(f, "\n", 1);
return 0;
}
static int my_timeline(){
fcli_t * a = &fcli;
fsl_buffer sql = fsl_buffer_empty;
int rc;
fsl_db * db = fsl_cx_db_repo(a->f);
fsl_id_t tagId = 0;
fsl_buffer_appendf(&sql, "SELECT "
/*0*/"uuid AS uuid, "
/*1*/"datetime(event.mtime%s) AS timestampString, "
/*2*/"coalesce(euser, user) AS user, "
/*3*/"event.type AS eventType, "
/*4*/"(SELECT group_concat(substr(tagname,5), ',') FROM tag, tagxref "
"WHERE tagname GLOB 'sym-*' AND tag.tagid=tagxref.tagid "
"AND tagxref.rid=blob.rid AND tagxref.tagtype>0) as tags, "
/*5*/"coalesce(ecomment, comment) AS comment "
"FROM event JOIN blob "
"WHERE "
"blob.rid=event.objid ",
TLApp.utc ? "" : ", 'localtime'");
/*
TODO: filters:
- branch
- timeframe
- user
- event type
- tag
- comment
*/
if(TLApp.filterBranch){
tagId = fsl_db_g_id(db, 0,
"SELECT tagid FROM tag WHERE tagname='sym-%q'",
TLApp.filterBranch);
if(tagId>0){
fsl_buffer_appendf(&sql,
" AND EXISTS(SELECT 1 FROM tagxref"
" WHERE tagid=%"FSL_ID_T_PFMT
" AND tagtype>0 AND rid=blob.rid)",
(fsl_id_t)tagId);
}else{
rc = fcli_err_set(FSL_RC_NOT_FOUND,"No such branch: %s",
TLApp.filterBranch);
goto end;
}
}
if(TLApp.filterTag){
tagId = fsl_db_g_id(db, 0,
"SELECT tagid FROM tag WHERE tagname='%q'",
TLApp.filterTag);
if(tagId>0){
fsl_buffer_appendf(&sql,
" AND EXISTS(SELECT 1 FROM tagxref"
" WHERE tagid=%"FSL_ID_T_PFMT
" AND tagtype>0 AND rid=blob.rid)",
(fsl_id_t)tagId);
}else{
rc = fcli_err_set(FSL_RC_NOT_FOUND,"No such tag: %s",
TLApp.filterTag);
goto end;
}
}
if(TLApp.filterComment){
rc = fsl_buffer_appendf(&sql,
" AND lower(comment) GLOB lower('*%q*')",
TLApp.filterComment);
if(rc) goto end;
}
fsl_buffer_appendf(&sql,
" ORDER BY event.mtime DESC");
if(TLApp.limit > 0){
fsl_buffer_appendf(&sql, " LIMIT %d", TLApp.limit);
}
rc = fsl_db_each(db, stmt_each_f_timeline1, NULL,
"%b", &sql);
fsl_flush(a->f);
end:
fsl_buffer_clear(&sql);
if(rc && db->error.code){
rc = fsl_cx_uplift_db_error(a->f, db);
}
return rc;
}
static void fcli_local_help(){
printf("Usage:\n\t%s [options]\n\n", fcli.appName);
puts("Displays timeline information for a Fossil repository db.");
puts("Options:\n");
puts("\t-n|-limit=NUMBER limits the results to that many. "
"n=0 means unlimited. Negative values are ignored "
"and use the default limit.\n");
puts("\t--show-files|-f enables the listing of the files changed for "
"each commit.\n");
puts("\t--comment|-c=TEXT Filters on the comment string "
"as a case-insensitive GLOB matching lower('*TEXT*').\n");
puts("\t--branch|-b=BRANCH Filters on the given branch name.\n");
puts("\t--tag|-t=TAG Filters on the given tag name. "
"--tag=sym-XYZ is equivalent to --branch=XYZ.\n");
puts("\t--utc Changes time values from local time to UTC.\n");
}
int main(int argc, char * const * argv ){
int rc = 0;
char * zLimit = NULL;
fsl_cx * f = NULL;
fcli.appHelp = fcli_local_help;
rc = fcli_setup(argc, argv);
if(FSL_RC_BREAK==rc) return 0;
else if(rc) goto end;
f = fcli.f;
fcli_flag2("t", "tag", &TLApp.filterTag);
fcli_flag2("b", "branch", &TLApp.filterBranch);
fcli_flag2("c", "comment", &TLApp.filterComment);
TLApp.utc = fcli_flag("utc", NULL);
TLApp.showFiles = fcli_flag2("f", "show-files", NULL);
fcli_flag2("n", "limit", &zLimit);
if(zLimit){
TLApp.limit = atoi(zLimit);
fsl_free(zLimit);
zLimit = NULL;
}
if(TLApp.limit<0) TLApp.limit = 5;
if(fcli_has_unused_flags(0)) goto end;
fsl_checkout_version_info(f, NULL, &TLApp.ckoutUuid);
if(!fsl_cx_db_repo(f)){
rc = fsl_cx_err_set(f, FSL_RC_MISUSE, "Repo db required.");
goto end;
}
rc = my_timeline();
end:
fsl_free(TLApp.filterTag);
fsl_free(TLApp.filterBranch);
fsl_free(TLApp.filterComment);
return (fcli_err_report(0) || rc)
? EXIT_FAILURE : EXIT_SUCCESS;
}