Fossil

Diff
Login

Differences From Artifact [c028c6b677]:

To Artifact [c707ca1de0]:


29
30
31
32
33
34
35
36
37



38
39
40
41
42

43
44
45
46
47


48
49
50
51
52
53
54
55

56
57
58

59
60
61


62
63
64
65
66
67
68
69
70
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
95
96
97
98
99

100
101
102
103
104
105
106
107
108
109
110

111
112

113
114
115
116
117

118
119
120

121
122
123
124
125
126
127
29
30
31
32
33
34
35


36
37
38
39
40
41
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

58
59
60

61
62


63
64
65
66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

90
91
92
93
94
95
96
97
98
99
100
101

102
103
104
105
106
107
108
109
110
111
112

113
114

115
116
117
118
119

120
121
122

123
124
125
126
127
128
129
130







-
-
+
+
+




-
+





+
+







-
+


-
+

-
-
+
+









-
+















-
+











-
+










-
+

-
+




-
+


-
+








/*
** Propagate the tag given by tagid to the children of pid.
**
** This routine assumes that tagid is a tag that should be
** propagated and that the tag is already present in pid.
**
** If addFlag is true then the tag is added.  If false, then the
** tag is removed.
** If tagtype is 2 then the tag is being propagated from an
** ancestor node.  If tagtype is 0 it means a branch tag is
** being cancelled.
*/
void tag_propagate(
  int pid,             /* Propagate the tag to children of this node */
  int tagid,           /* Tag to propagate */
  int addFlag,         /* True to add the tag. False to delete it. */
  int tagType,         /* 2 for a propagating tag.  0 for an antitag */
  const char *zValue,  /* Value of the tag.  Might be NULL */
  double mtime         /* Timestamp on the tag */
){
  PQueue queue;
  Stmt s, ins, eventupdate;

  assert( tagType==0 || tagType==2 );
  pqueue_init(&queue);
  pqueue_insert(&queue, pid, 0.0);
  db_prepare(&s, 
     "SELECT cid, plink.mtime,"
     "       coalesce(srcid=0 AND tagxref.mtime<:mtime, %d) AS doit"
     "  FROM plink LEFT JOIN tagxref ON cid=rid AND tagid=%d"
     " WHERE pid=:pid AND isprim",
     addFlag, tagid
     tagType!=0, tagid
  );
  db_bind_double(&s, ":mtime", mtime);
  if( addFlag ){
  if( tagType==2 ){
    db_prepare(&ins,
       "REPLACE INTO tagxref(tagid, addFlag, srcid, value, mtime, rid)"
       "VALUES(%d,1,0,%Q,:mtime,:rid)",
       "REPLACE INTO tagxref(tagid, tagtype, srcid, value, mtime, rid)"
       "VALUES(%d,2,0,%Q,:mtime,:rid)",
       tagid, zValue
    );
    db_bind_double(&ins, ":mtime", mtime);
  }else{
    zValue = 0;
    db_prepare(&ins,
       "DELETE FROM tagxref WHERE tagid=%d AND rid=:rid", tagid
    );
  }
  if( tagid==TAG_BR_BGCOLOR ){
  if( tagid==TAG_BGCOLOR ){
    db_prepare(&eventupdate,
      "UPDATE event SET brbgcolor=%Q WHERE objid=:rid", zValue
    );
  }
  while( (pid = pqueue_extract(&queue))!=0 ){
    db_bind_int(&s, ":pid", pid);
    while( db_step(&s)==SQLITE_ROW ){
      int doit = db_column_int(&s, 2);
      if( doit ){
        int cid = db_column_int(&s, 0);
        double mtime = db_column_double(&s, 1);
        pqueue_insert(&queue, cid, mtime);
        db_bind_int(&ins, ":rid", cid);
        db_step(&ins);
        db_reset(&ins);
        if( tagid==TAG_BR_BGCOLOR ){
        if( tagid==TAG_BGCOLOR ){
          db_bind_int(&eventupdate, ":rid", cid);
          db_step(&eventupdate);
          db_reset(&eventupdate);
        }
      }
    }
    db_reset(&s);
  }
  pqueue_clear(&queue);
  db_finalize(&ins);
  db_finalize(&s);
  if( tagid==TAG_BR_BGCOLOR ){
  if( tagid==TAG_BGCOLOR ){
    db_finalize(&eventupdate);
  }
}

/*
** Propagate all propagatable tags in pid to its children.
*/
void tag_propagate_all(int pid){
  Stmt q;
  db_prepare(&q,
     "SELECT tagid, addflag, mtime, value FROM tagxref"
     "SELECT tagid, tagtype, mtime, value FROM tagxref"
     " WHERE rid=%d"
     "   AND (SELECT tagname FROM tag WHERE tagid=tagxref.tagid) LIKE 'br%'",
     "   AND (tagtype=0 OR tagtype=2)",
     pid
  );
  while( db_step(&q)==SQLITE_ROW ){
    int tagid = db_column_int(&q, 0);
    int addflag = db_column_int(&q, 1);
    int tagtype = db_column_int(&q, 1);
    double mtime = db_column_double(&q, 2);
    const char *zValue = db_column_text(&q, 3);
    tag_propagate(pid, tagid, addflag, zValue, mtime);
    tag_propagate(pid, tagid, tagtype, zValue, mtime);
  }
  db_finalize(&q);
}

/*
** Get a tagid for the given TAG.  Create a new tag if necessary
** if createFlag is 1.
137
138
139
140
141
142
143
144

145
146
147
148
149
150

151
152
153
154
155
156

157
158

159
160
161
162
163

164
165

166
167

168
169
170




171
172
173
174
175
176

177
178
179
180

181
182
183
184
185





186
187
188
189
190
191
192


193
194

195
196
197
198
199

200
201
202

203
204
205
206
207






208
209
210
211
212
213
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

252
253
254
255
256
257
258

259

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274


275
276
277
278
279
280
281
140
141
142
143
144
145
146

147
148
149
150
151
152
153
154
155
156
157
158
159

160
161

162
163
164
165
166

167
168
169
170
171
172
173



174
175
176
177


178
179
180

181
182
183
184

185
186
187
188


189
190
191
192
193
194
195
196
197
198


199
200
201

202
203
204
205
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

229

























230
231
232
233
234
235
236
237
238
239
240

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264


265
266
267
268
269
270
271
272
273







-
+






+





-
+

-
+




-
+


+


+
-
-
-
+
+
+
+
-
-



-
+



-
+



-
-
+
+
+
+
+





-
-
+
+

-
+




-
+



+





+
+
+
+
+
+






-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-











-
+







+

+













-
-
+
+







}

/*
** Insert a tag into the database.
*/
void tag_insert(
  const char *zTag,        /* Name of the tag (w/o the "+" or "-" prefix */
  int addFlag,             /* True to add.  False to remove */
  int tagtype,             /* 0:cancel  1:singleton  2:propagated */
  const char *zValue,      /* Value if the tag is really a property */
  int srcId,               /* Artifact that contains this tag */
  double mtime,            /* Timestamp.  Use default if <=0.0 */
  int rid                  /* Artifact to which the tag is to attached */
){
  Stmt s;
  const char *zCol;
  int tagid = tag_findid(zTag, 1);
  if( mtime<=0.0 ){
    mtime = db_double(0.0, "SELECT julianday('now')");
  }
  db_prepare(&s, 
    "REPLACE INTO tagxref(tagid,addFlag,srcId,value,mtime,rid)"
    "REPLACE INTO tagxref(tagid,tagtype,srcId,value,mtime,rid)"
    " VALUES(%d,%d,%d,%Q,:mtime,%d)",
    tagid, addFlag, srcId, zValue, rid
    tagid, tagtype, srcId, zValue, rid
  );
  db_bind_double(&s, ":mtime", mtime);
  db_step(&s);
  db_finalize(&s);
  if( addFlag==0 ){
  if( tagtype==0 ){
    zValue = 0;
  }
  zCol = 0;
  switch( tagid ){
    case TAG_BGCOLOR: {
      if( tagtype==1 ){
      db_multi_exec("UPDATE event SET bgcolor=%Q WHERE objid=%d", zValue, rid);
      break;
    }
        zCol = "bgcolor";
      }else{
        zCol = "brbgcolor";
      }
    case TAG_BR_BGCOLOR: {
      db_multi_exec("UPDATE event SET brbgcolor=%Q WHERE objid=%d", zValue,rid);
      break;
    }
    case TAG_COMMENT: {
      db_multi_exec("UPDATE event SET ecomment=%Q WHERE objid=%d", zValue, rid);
      zCol = "ecomment";
      break;
    }
    case TAG_USER: {
      db_multi_exec("UPDATE event SET euser=%Q WHERE objid=%d", zValue, rid);
      zCol = "euser";
      break;
    }
  }
  if( strncmp(zTag, "br", 2)==0 ){
    tag_propagate(rid, tagid, addFlag, zValue, mtime);
  if( zCol ){
    db_multi_exec("UPDATE event SET %s=%Q WHERE objid=%d", zCol, zValue, rid);
  }
  if( tagtype==0 || tagtype==2 ){
    tag_propagate(rid, tagid, tagtype, zValue, mtime);
  }
}


/*
** COMMAND: test-addtag
** %fossil test-addtag TAGNAME UUID ?VALUE?
** COMMAND: test-tag
** %fossil test-tag (+|*|-)TAGNAME UUID ?VALUE?
**
** Add a tag to the rebuildable tables of the local repository.
** Add a tag or anti-tag to the rebuildable tables of the local repository.
** No tag artifact is created so the new tag is erased the next
** time the repository is rebuilt.  This routine is for testing
** use only.
*/
void addtag_cmd(void){
void testtag_cmd(void){
  const char *zTag;
  const char *zValue;
  int rid;
  int tagtype;
  db_must_be_within_tree();
  if( g.argc!=4 && g.argc!=5 ){
    usage("TAGNAME UUID ?VALUE?");
  }
  zTag = g.argv[2];
  switch( zTag[0] ){
    case '+':  tagtype = 1;  break;
    case '*':  tagtype = 2;  break;
    case '-':  tagtype = 0;  break;
    default:   fossil_fatal("tag should begin with '+', '*', or '-'");
  }
  rid = name_to_rid(g.argv[3]);
  if( rid==0 ){
    fossil_fatal("no such object: %s", g.argv[3]);
  }
  zValue = g.argc==5 ? g.argv[4] : 0;
  db_begin_transaction();
  tag_insert(zTag, 1, zValue, -1, 0.0, rid);
  tag_insert(zTag, tagtype, zValue, -1, 0.0, rid);
  db_end_transaction(0); 
}
/*
** COMMAND: test-deltag
** %fossil test-deltag TAGNAME UUID
**
** Cancel a tag to the rebuildable tables of the local repository.
** No tag artifact is created so the cancellation is undone the next
** time the repository is rebuilt.  This routine is for testing
** use only.
*/
void deltag_cmd(void){
  const char *zTag;
  int rid;
  db_must_be_within_tree();
  if( g.argc!=4 ){
    usage("TAGNAME UUID");
  }
  zTag = g.argv[2];
  rid = name_to_rid(g.argv[3]);
  if( rid==0 ){
    fossil_fatal("no such object: %s", g.argv[3]);
  }
  db_begin_transaction();
  tag_insert(zTag, 0, 0, -1, 0.0, rid);
  db_end_transaction(0); 
}

/*
** Add a control record to the repository that either creates
** or cancels a tag.
*/
static void tag_add_artifact(
  const char *zTagname,       /* The tag to add or cancel */
  const char *zObjName,       /* Name of object attached to */
  const char *zValue,         /* Value for the tag.  Might be NULL */
  int addFlag                 /* True to add.  false to cancel */
  int tagtype                 /* 0:cancel 1:singleton 2:propagated */
){
  int rid;
  int nrid;
  char *zDate;
  Blob uuid;
  Blob ctrl;
  Blob cksum;
  static const char zTagtype[] = { '-', '+', '*' };

  assert( tagtype>=0 && tagtype<=2 );
  user_select();
  rid = name_to_rid(zObjName);
  blob_zero(&uuid);
  db_blob(&uuid, "SELECT uuid FROM blob WHERE rid=%d", rid);
  blob_zero(&ctrl);
  
  if( validate16(zTagname, strlen(zTagname)) ){
    fossil_fatal("invalid tag name \"%s\" - might be confused with a UUID",
                 zTagname);
  }
  zDate = db_text(0, "SELECT datetime('now')");
  zDate[10] = 'T';
  blob_appendf(&ctrl, "D %s\n", zDate);
  blob_appendf(&ctrl, "T %c%F %b", addFlag ? '+' : '-', zTagname, &uuid);
  if( addFlag && zValue && zValue[0] ){
  blob_appendf(&ctrl, "T %c%F %b", zTagtype[tagtype], zTagname, &uuid);
  if( tagtype && zValue && zValue[0] ){
    blob_appendf(&ctrl, " %F\n", zValue);
  }else{
    blob_appendf(&ctrl, "\n");
  }
  blob_appendf(&ctrl, "U %F\n", g.zLogin);
  md5sum_blob(&ctrl, &cksum);
  blob_appendf(&ctrl, "Z %b\n", &cksum);
291
292
293
294
295
296
297





298
299
300
301
302
303
304
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301







+
+
+
+
+







**
** Run various subcommands to control tags and properties
**
**     %fossil tag add TAGNAME UUID ?VALUE?
**
**         Add a new tag or property to UUID.
**
**     %fossil tag branch TAGNAME UUID ?VALUE?
**
**         Add a new tag or property to UUID and make that
**         tag propagate to all direct children.
**
**     %fossil tag delete TAGNAME UUID
**
**         Delete the tag TAGNAME from UUID
**
**     %fossil tag find TAGNAME
**
**         List all baselines that use TAGNAME
323
324
325
326
327
328
329









330
331
332
333
334
335
336
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342







+
+
+
+
+
+
+
+
+







    char *zValue;
    if( g.argc!=5 && g.argc!=6 ){
      usage("tag add TAGNAME UUID ?VALUE?");
    }
    zValue = g.argc==6 ? g.argv[5] : 0;
    tag_add_artifact(g.argv[3], g.argv[4], zValue, 1);
  }else

  if( strncmp(g.argv[2],"branch",n)==0 ){
    char *zValue;
    if( g.argc!=5 && g.argc!=6 ){
      usage("tag branch TAGNAME UUID ?VALUE?");
    }
    zValue = g.argc==6 ? g.argv[5] : 0;
    tag_add_artifact(g.argv[3], g.argv[4], zValue, 2);
  }else

  if( strncmp(g.argv[2],"delete",n)==0 ){
    if( g.argc!=5 ){
      usage("tag delete TAGNAME UUID");
    }
    tag_add_artifact(g.argv[3], g.argv[4], 0, 0);
  }else
355
356
357
358
359
360
361
362

363
364
365
366
367
368
369
370
371
372
373
374
375

376
377
378
379
380
381
382
361
362
363
364
365
366
367

368
369
370
371
372
373
374
375
376
377
378
379
380

381
382
383
384
385
386
387
388







-
+












-
+







    Stmt q;
    if( g.argc==3 ){
      db_prepare(&q, 
        "SELECT tagname"
        "  FROM tag"
        " WHERE EXISTS(SELECT 1 FROM tagxref"
        "               WHERE tagid=tag.tagid"
        "                 AND addflag)"
        "                 AND tagtype>0)"
        " ORDER BY tagname"
      );
      while( db_step(&q)==SQLITE_ROW ){
        printf("%s\n", db_column_text(&q, 0));
      }
      db_finalize(&q);
    }else if( g.argc==4 ){
      int rid = name_to_rid(g.argv[3]);
      db_prepare(&q,
        "SELECT tagname, value"
        "  FROM tagxref, tag"
        " WHERE tagxref.rid=%d AND tagxref.tagid=tag.tagid"
        "   AND addflag"
        "   AND tagtype>0"
        " ORDER BY tagname",
        rid
      );
      while( db_step(&q)==SQLITE_ROW ){
        const char *zName = db_column_text(&q, 0);
        const char *zValue = db_column_text(&q, 1);
        if( zValue ){
392
393
394
395
396
397
398
399

400
398
399
400
401
402
403
404

405
406







-
+

  }else
  {
    goto tag_cmd_usage;
  }
  return;

tag_cmd_usage:
  usage("add|delete|find|list ...");
  usage("add|branch|delete|find|list ...");
}