Fossil SCM

fossil-scm / src / json_finfo.c
Source Blame History 152 lines
f825f8c… stephan 1 #ifdef FOSSIL_ENABLE_JSON
f825f8c… stephan 2 /*
c19f34c… drh 3 ** Copyright (c) 2011 D. Richard Hipp
f825f8c… stephan 4 **
f825f8c… stephan 5 ** This program is free software; you can redistribute it and/or
f825f8c… stephan 6 ** modify it under the terms of the Simplified BSD License (also
f825f8c… stephan 7 ** known as the "2-Clause License" or "FreeBSD License".)
f825f8c… stephan 8 **
f825f8c… stephan 9 ** This program is distributed in the hope that it will be useful,
f825f8c… stephan 10 ** but without any warranty; without even the implied warranty of
f825f8c… stephan 11 ** merchantability or fitness for a particular purpose.
f825f8c… stephan 12 **
f825f8c… stephan 13 ** Author contact information:
f825f8c… stephan 14 ** [email protected]
f825f8c… stephan 15 ** http://www.hwaci.com/drh/
f825f8c… stephan 16 **
f825f8c… stephan 17 */
f825f8c… stephan 18 #include "VERSION.h"
f825f8c… stephan 19 #include "config.h"
f825f8c… stephan 20 #include "json_finfo.h"
f825f8c… stephan 21
f825f8c… stephan 22 #if INTERFACE
f825f8c… stephan 23 #include "json_detail.h"
f825f8c… stephan 24 #endif
f825f8c… stephan 25
f825f8c… stephan 26 /*
f825f8c… stephan 27 ** Implements the /json/finfo page/command.
f825f8c… stephan 28 **
f825f8c… stephan 29 */
a80f274… stephan 30 cson_value * json_page_finfo(void){
f825f8c… stephan 31 cson_object * pay = NULL;
f825f8c… stephan 32 cson_array * checkins = NULL;
f825f8c… stephan 33 char const * zFilename = NULL;
f825f8c… stephan 34 Blob sql = empty_blob;
f825f8c… stephan 35 Stmt q = empty_Stmt;
f825f8c… stephan 36 char const * zAfter = NULL;
f825f8c… stephan 37 char const * zBefore = NULL;
f825f8c… stephan 38 int limit = -1;
f825f8c… stephan 39 int currentRow = 0;
6472199… stephan 40 char const * zCheckin = NULL;
b7c7c20… preben 41 signed char sort = -1;
f825f8c… stephan 42 if(!g.perm.Read){
f825f8c… stephan 43 json_set_err(FSL_JSON_E_DENIED,"Requires 'o' privileges.");
f825f8c… stephan 44 return NULL;
f825f8c… stephan 45 }
275da70… danield 46 json_warn( FSL_JSON_W_UNKNOWN,
275da70… danield 47 "Achtung: the output of the finfo command is up for change.");
f825f8c… stephan 48
275da70… danield 49 /* For the "name" argument we have to jump through some hoops to make sure
275da70… danield 50 that we don't get the fossil-internally-assigned "name" option.
6472199… stephan 51 */
7b89c7b… stephan 52 zFilename = json_find_option_cstr2("name",NULL,NULL, g.json.dispatchDepth+1);
7b89c7b… stephan 53 if(!zFilename || !*zFilename){
f825f8c… stephan 54 json_set_err(FSL_JSON_E_MISSING_ARGS, "Missing 'name' parameter.");
f825f8c… stephan 55 return NULL;
f825f8c… stephan 56 }
7b89c7b… stephan 57
7b89c7b… stephan 58 if(0==db_int(0,"SELECT 1 FROM filename WHERE name=%Q",zFilename)){
7b89c7b… stephan 59 json_set_err(FSL_JSON_E_RESOURCE_NOT_FOUND, "File entry not found.");
7b89c7b… stephan 60 return NULL;
7b89c7b… stephan 61 }
bf9669f… andygoth 62
f825f8c… stephan 63 zBefore = json_find_option_cstr("before",NULL,"b");
f825f8c… stephan 64 zAfter = json_find_option_cstr("after",NULL,"a");
f825f8c… stephan 65 limit = json_find_option_int("limit",NULL,"n", -1);
6472199… stephan 66 zCheckin = json_find_option_cstr("checkin",NULL,"ci");
6472199… stephan 67
bf9669f… andygoth 68 blob_append_sql(&sql,
7b89c7b… stephan 69 /*0*/ "SELECT b.uuid,"
7b89c7b… stephan 70 /*1*/ " ci.uuid,"
275da70… danield 71 /*2*/ " (SELECT uuid FROM blob WHERE rid=mlink.fid),"/* Current file uuid */
7b89c7b… stephan 72 /*3*/ " cast(strftime('%%s',event.mtime) AS INTEGER),"
7b89c7b… stephan 73 /*4*/ " coalesce(event.euser, event.user),"
7b89c7b… stephan 74 /*5*/ " coalesce(event.ecomment, event.comment),"
7b89c7b… stephan 75 /*6*/ " (SELECT uuid FROM blob WHERE rid=mlink.pid)," /* Parent file uuid */
7b89c7b… stephan 76 /*7*/ " event.bgcolor,"
8c6dc24… stephan 77 /*8*/ " b.size,"
8c6dc24… stephan 78 /*9*/ " (mlink.pid==0) AS isNew,"
8c6dc24… stephan 79 /*10*/ " (mlink.fid==0) AS isDel"
b3e32c8… jan.nijtmans 80 " FROM mlink, blob b, event, blob ci, filename"
4c5c96c… jan.nijtmans 81 " WHERE filename.name=%Q"
f825f8c… stephan 82 " AND mlink.fnid=filename.fnid"
f825f8c… stephan 83 " AND b.rid=mlink.fid"
f825f8c… stephan 84 " AND event.objid=mlink.mid"
f825f8c… stephan 85 " AND event.objid=ci.rid",
4c5c96c… jan.nijtmans 86 zFilename
f825f8c… stephan 87 );
6472199… stephan 88
6472199… stephan 89 if( zCheckin && *zCheckin ){
6472199… stephan 90 char * zU = NULL;
6472199… stephan 91 int rc = name_to_uuid2( zCheckin, "ci", &zU );
7b89c7b… stephan 92 /*printf("zCheckin=[%s], zU=[%s]", zCheckin, zU);*/
6472199… stephan 93 if(rc<=0){
275da70… danield 94 json_set_err((rc<0) ? FSL_JSON_E_AMBIGUOUS_UUID :
275da70… danield 95 FSL_JSON_E_RESOURCE_NOT_FOUND,
8ad5e46… wyoung 96 "Check-in hash %s.", (rc<0) ? "is ambiguous" : "not found");
6472199… stephan 97 blob_reset(&sql);
6472199… stephan 98 return NULL;
6472199… stephan 99 }
49b0ff1… drh 100 blob_append_sql(&sql, " AND ci.uuid='%q'", zU);
6472199… stephan 101 free(zU);
6472199… stephan 102 }else{
7b89c7b… stephan 103 if( zAfter && *zAfter ){
49b0ff1… drh 104 blob_append_sql(&sql, " AND event.mtime>=julianday('%q')", zAfter);
5dcaeca… stephan 105 sort = 1;
7b89c7b… stephan 106 }else if( zBefore && *zBefore ){
49b0ff1… drh 107 blob_append_sql(&sql, " AND event.mtime<=julianday('%q')", zBefore);
6472199… stephan 108 }
6472199… stephan 109 }
7b89c7b… stephan 110
275da70… danield 111 blob_append_sql(&sql," ORDER BY event.mtime %s /*sort*/",
275da70… danield 112 (sort>0 ? "ASC" : "DESC"));
6472199… stephan 113 /*printf("SQL=\n%s\n",blob_str(&sql));*/
49b0ff1… drh 114 db_prepare(&q, "%s", blob_sql_text(&sql));
f825f8c… stephan 115 blob_reset(&sql);
f825f8c… stephan 116
f825f8c… stephan 117 pay = cson_new_object();
f825f8c… stephan 118 cson_object_set(pay, "name", json_new_string(zFilename));
f825f8c… stephan 119 if( limit > 0 ){
f825f8c… stephan 120 cson_object_set(pay, "limit", json_new_int(limit));
f825f8c… stephan 121 }
f825f8c… stephan 122 checkins = cson_new_array();
f825f8c… stephan 123 cson_object_set(pay, "checkins", cson_array_value(checkins));
f825f8c… stephan 124 while( db_step(&q)==SQLITE_ROW ){
f825f8c… stephan 125 cson_object * row = cson_new_object();
8c6dc24… stephan 126 int const isNew = db_column_int(&q,9);
8c6dc24… stephan 127 int const isDel = db_column_int(&q,10);
f825f8c… stephan 128 cson_array_append( checkins, cson_object_value(row) );
f825f8c… stephan 129 cson_object_set(row, "checkin", json_new_string( db_column_text(&q,1) ));
6472199… stephan 130 cson_object_set(row, "uuid", json_new_string( db_column_text(&q,2) ));
275da70… danield 131 /*cson_object_set(row, "parentArtifact",
275da70… danield 132 json_new_string( db_column_text(&q,6) ));*/
f89a32d… jan.nijtmans 133 cson_object_set(row, "timestamp", json_new_int( db_column_int64(&q,3) ));
f825f8c… stephan 134 cson_object_set(row, "user", json_new_string( db_column_text(&q,4) ));
f825f8c… stephan 135 cson_object_set(row, "comment", json_new_string( db_column_text(&q,5) ));
275da70… danield 136 /*cson_object_set(row, "bgColor",
275da70… danield 137 json_new_string( db_column_text(&q,7) ));*/
f89a32d… jan.nijtmans 138 cson_object_set(row, "size", json_new_int( db_column_int64(&q,8) ));
275da70… danield 139 cson_object_set(row, "state", json_new_string(
275da70… danield 140 json_artifact_status_to_string(isNew, isDel)));
f825f8c… stephan 141 if( (0 < limit) && (++currentRow >= limit) ){
f825f8c… stephan 142 break;
f825f8c… stephan 143 }
f825f8c… stephan 144 }
f825f8c… stephan 145 db_finalize(&q);
f825f8c… stephan 146
6472199… stephan 147 return pay ? cson_object_value(pay) : NULL;
f825f8c… stephan 148 }
f825f8c… stephan 149
f825f8c… stephan 150
f825f8c… stephan 151
f825f8c… stephan 152 #endif /* FOSSIL_ENABLE_JSON */

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button