Fossil

Check-in [fb3c81d8]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Complete the parts of the "fossil unversioned" command used to add and remove content.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | unversioned-files
Files: files | file ages | folders
SHA1:fb3c81d8ba0f632ad05fd59c7e8089ae745ed8b8
User & Date: drh 2016-08-06 21:01:18
Context
2016-08-08
14:31
Merge changes from trunk. check-in: 3506156a user: drh tags: unversioned-files
2016-08-06
21:01
Complete the parts of the "fossil unversioned" command used to add and remove content. check-in: fb3c81d8 user: drh tags: unversioned-files
17:40
Add a link to the web-cache status screen to the Admin main menu. check-in: 7c176f7a user: drh tags: unversioned-files
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/unversioned.c.

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
...
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









131
132
133
134
135
136
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
** Make sure the unversioned table exists in the repository.
*/
void unversioned_schema(void){
  if( !db_table_exists("repository", "unversioned") ){
    db_multi_exec(zUnversionedInit /*works-like:"%w"*/, db_name("repository"));
  }
}
































/*
** COMMAND: unversioned
**
** Usage: %fossil unversioned SUBCOMMAND ARGS...
**
** Unversioned files (UV-files) are artifacts that are synced and are available
** for download but which do not preserve history.  Only the most recent version
** of each UV-file is retained.  Changes to an UV-file are permanent and cannot
** be undone, so use appropriate caution with this command.
**
** Subcommands:
**
**    add FILE ?INPUT?     Add or update an unversioned file FILE in the local
**                         repository so that it matches the INPUT file on disk.
**                         Content is read from stdin if INPUT is "-" or omitted.

**                         Changes are not pushed to other repositories until
**                         the next sync.
**
**    cat FILE ?OUTFILE?   Write the content of unversioned file FILE to OUTFILE
**                         on disk, or to stdout if OUTFILE is "-" or is omitted.

**
**    list | ls            Show all unversioned files held in the local repository.
**
**    revert ?URL?         Restore the state of all unversioned files in the local
**                         repository to match the remote repository URL.
**
**    rm FILE ...          Remove an unversioned files from the local repository.
................................................................................
  if( zMtime==0 ){
    mtime = time(0);
  }else{
    mtime = db_int(0, "SELECT strftime('%%s',%Q)", zMtime);
    if( mtime<=0 ) fossil_fatal("bad timestamp: %Q", zMtime);
  }
  if( memcmp(zCmd, "add", nCmd)==0 ){
    const char *zFile;
    const char *zIn;
    Blob file;
    Blob hash;
    Blob compressed;
    Stmt ins;
    if( g.argc!=4 && g.argc!=5 ) usage("add FILE ?INPUT?");
    zFile = g.argv[3];
    if( !file_is_simple_pathname(zFile,1) ){
      fossil_fatal("'%Q' is not an acceptable filename", zFile);
    }
    zIn = g.argc==5 ? g.argv[4] : "-";
    blob_init(&file,0,0);
    blob_read_from_file(&file, zIn);
    sha1sum_blob(&file, &hash);
    blob_compress(&file, &compressed);
    db_begin_transaction();
    content_rcvid_init();
    db_prepare(&ins,
      "REPLACE INTO unversioned(name,rcvid,mtime,hash,sz,content)"
      " VALUES(:name,:rcvid,:mtime,:hash,:sz,:content)"
    );









    db_bind_text(&ins, ":name", zFile);
    db_bind_int(&ins, ":rcvid", g.rcvid);
    db_bind_int64(&ins, ":mtime", mtime);
    db_bind_text(&ins, ":hash", blob_str(&hash));
    db_bind_int(&ins, ":sz", blob_size(&file));
    db_bind_blob(&ins, ":content", &compressed);
    db_step(&ins);
    db_finalize(&ins);
    blob_reset(&compressed);
    blob_reset(&hash);
    blob_reset(&file);
    /* Clear the uvhash cache */



    db_end_transaction(0);
  }else if( memcmp(zCmd, "cat", nCmd)==0 ){































  }else if( memcmp(zCmd, "list", nCmd)==0 || memcmp(zCmd, "ls", nCmd)==0 ){
    Stmt q;














    db_prepare(&q,
      "SELECT hash, datetime(mtime,'unixepoch'), sz, name, content IS NULL"
      "   FROM unversioned"
      "  WHERE hash IS NOT NULL"
      "  ORDER BY name;"
    );
    while( db_step(&q)==SQLITE_ROW ){








      fossil_print("%12.12s %s %8d %s%s\n",
         db_column_text(&q,0),

         db_column_text(&q,1),
         db_column_int(&q,2),
         db_column_text(&q,3),
         db_column_int(&q,4) ? " ** no content ** ": ""

      );

    }
    db_finalize(&q);
  }else if( memcmp(zCmd, "revert", nCmd)==0 || memcmp(zCmd,"sync",nCmd)==0 ){
    fossil_fatal("not yet implemented...");
  }else if( memcmp(zCmd, "rm", nCmd)==0 ){












  }else{
    usage("add|cat|ls|revert|rm|sync");
  }
}

#if 0
***************************************************************************
DESIGN NOTES
Web interface:







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













|
|
|
>



|
|
>







 







|
|




|
|
|
|
|
<
<
<
<
<






>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
<
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
<
|
|
|
>
>
>
>
>
>
>
>
|
<
>
|
|
|
<
>
|
>





>
>
>
>
>
>
>
>
>
>
>
>

|







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
...
135
136
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
** Make sure the unversioned table exists in the repository.
*/
void unversioned_schema(void){
  if( !db_table_exists("repository", "unversioned") ){
    db_multi_exec(zUnversionedInit /*works-like:"%w"*/, db_name("repository"));
  }
}

/*
** Return a string which is the hash of the unversioned content.
** This is the hash used by repositories to compare content before
** exchanging a catalog.  So all repositories must compute this hash
** in exactly the same way.
**
** If debugFlag is set, force the value to be recomputed and write
** the text of the hashed string to stdout.
*/
const char *unversioned_content_hash(int debugFlag){
  const char *zHash = debugFlag ? 0 : db_get("uv-hash", 0);
  if( zHash==0 ){
    Stmt q;
    db_prepare(&q,
      "SELECT printf('%%s %%s %%s\n',name,datetime(mtime,'unixepoch'),hash)"
      "  FROM unversioned"
      " WHERE hash IS NOT NULL"
      " ORDER BY name"
    );
    while( db_step(&q)==SQLITE_ROW ){
      const char *z = db_column_text(&q, 0);
      if( debugFlag ) fossil_print("%s", z);
      sha1sum_step_text(z,-1);
    }
    db_finalize(&q);
    db_set("uv-hash", sha1sum_finish(0), 0);
    zHash = db_get("uv-hash",0);
  }
  return zHash;
}

/*
** COMMAND: unversioned
**
** Usage: %fossil unversioned SUBCOMMAND ARGS...
**
** Unversioned files (UV-files) are artifacts that are synced and are available
** for download but which do not preserve history.  Only the most recent version
** of each UV-file is retained.  Changes to an UV-file are permanent and cannot
** be undone, so use appropriate caution with this command.
**
** Subcommands:
**
**    add FILE ...         Add or update an unversioned files in the local
**                         repository so that it matches FILE on disk.
**                         Use "--as UVFILE" to give the file a different name
**                         in the repository than what it called on disk.
**                         Changes are not pushed to other repositories until
**                         the next sync.
**
**    cat FILE ...         Concatenate the content of FILEs to stdout.
**
**    export FILE OUTPUT   Write the content of FILE into OUTPUT on disk
**
**    list | ls            Show all unversioned files held in the local repository.
**
**    revert ?URL?         Restore the state of all unversioned files in the local
**                         repository to match the remote repository URL.
**
**    rm FILE ...          Remove an unversioned files from the local repository.
................................................................................
  if( zMtime==0 ){
    mtime = time(0);
  }else{
    mtime = db_int(0, "SELECT strftime('%%s',%Q)", zMtime);
    if( mtime<=0 ) fossil_fatal("bad timestamp: %Q", zMtime);
  }
  if( memcmp(zCmd, "add", nCmd)==0 ){
    const char *zIn;
    const char *zAs;
    Blob file;
    Blob hash;
    Blob compressed;
    Stmt ins;
    int i;

    zAs = find_option("as",0,1);
    if( zAs && g.argc!=4 ) usage("add DISKFILE --as UVFILE");
    verify_all_options();





    db_begin_transaction();
    content_rcvid_init();
    db_prepare(&ins,
      "REPLACE INTO unversioned(name,rcvid,mtime,hash,sz,content)"
      " VALUES(:name,:rcvid,:mtime,:hash,:sz,:content)"
    );
    for(i=3; i<g.argc; i++){
      zIn = zAs ? zAs : g.argv[i];
      if( zIn[0]==0 || zIn[0]=='/' || !file_is_simple_pathname(zIn,1) ){
        fossil_fatal("'%Q' is not an acceptable filename", zIn);
      }
      blob_init(&file,0,0);
      blob_read_from_file(&file, g.argv[i]);
      sha1sum_blob(&file, &hash);
      blob_compress(&file, &compressed);
      db_bind_text(&ins, ":name", zIn);
      db_bind_int(&ins, ":rcvid", g.rcvid);
      db_bind_int64(&ins, ":mtime", mtime);
      db_bind_text(&ins, ":hash", blob_str(&hash));
      db_bind_int(&ins, ":sz", blob_size(&file));
      db_bind_blob(&ins, ":content", &compressed);
      db_step(&ins);
      db_reset(&ins);
      blob_reset(&compressed);
      blob_reset(&hash);
      blob_reset(&file);

    }
    db_finalize(&ins);
    db_unset("uv-hash", 0);
    db_end_transaction(0);
  }else if( memcmp(zCmd, "cat", nCmd)==0 ){
    int i;
    verify_all_options();
    db_begin_transaction();
    for(i=3; i<g.argc; i++){
      Blob content;
      blob_init(&content, 0, 0);
      db_blob(&content, "SELECT content FROM unversioned WHERE name=%Q",g.argv[i]);
      if( blob_size(&content)==0 ){
        fossil_fatal("no such uv-file: %Q", g.argv[i]);
      }
      blob_uncompress(&content, &content);
      blob_write_to_file(&content, "-");
      blob_reset(&content);
    }
    db_end_transaction(0);
  }else if( memcmp(zCmd, "export", nCmd)==0 ){
    Blob content;
    verify_all_options();
    if( g.argc!=5 ) usage("export UVFILE OUTPUT");
    blob_init(&content, 0, 0);
    db_blob(&content, "SELECT content FROM unversioned WHERE name=%Q", g.argv[3]);
    if( blob_size(&content)==0 ){
      fossil_fatal("no such uv-file: %Q", g.argv[3]);
    }
    blob_uncompress(&content, &content);
    blob_write_to_file(&content, g.argv[4]);
    blob_reset(&content);
  }else if( memcmp(zCmd, "hash", nCmd)==0 ){  /* undocumented */
    /* Show the hash value used during uv sync */
    int debugFlag = find_option("debug",0,0)!=0;
    fossil_print("%s\n", unversioned_content_hash(debugFlag));
  }else if( memcmp(zCmd, "list", nCmd)==0 || memcmp(zCmd, "ls", nCmd)==0 ){
    Stmt q;
    int allFlag = find_option("all","a",0)!=0;
    int longFlag = find_option("l",0,0)!=0 || (nCmd>1 && zCmd[1]=='i');
    verify_all_options();
    if( !longFlag ){
      if( allFlag ){
        db_prepare(&q, "SELECT name FROM unversioned ORDER BY name");
      }else{
        db_prepare(&q, "SELECT name FROM unversioned WHERE hash IS NOT NULL"
                       " ORDER BY name");
      }
      while( db_step(&q)==SQLITE_ROW ){
        fossil_print("%s\n", db_column_text(&q,0));
      }
    }else{
      db_prepare(&q,
        "SELECT hash, datetime(mtime,'unixepoch'), sz, name, content IS NULL"
        "   FROM unversioned"

        "  ORDER BY name;"
      );
      while( db_step(&q)==SQLITE_ROW ){
        const char *zHash = db_column_text(&q, 0);
        const char *zNoContent = "";
        if( zHash==0 ){
          if( !allFlag ) continue;
          zHash = "(deleted)";
        }else if( db_column_int(&q,4) ){
          zNoContent = " (no content)";
        }
        fossil_print("%12.12s %s %8d %s%s\n",

           zHash,
           db_column_text(&q,1),
           db_column_int(&q,2),
           db_column_text(&q,3),

           zNoContent
        );
      }
    }
    db_finalize(&q);
  }else if( memcmp(zCmd, "revert", nCmd)==0 || memcmp(zCmd,"sync",nCmd)==0 ){
    fossil_fatal("not yet implemented...");
  }else if( memcmp(zCmd, "rm", nCmd)==0 ){
    int i;
    verify_all_options();
    db_begin_transaction();
    for(i=3; i<g.argc; i++){
      db_multi_exec(
        "UPDATE unversioned"
        "   SET hash=NULL, content=NULL, mtime=%lld, sz=0 WHERE name=%Q",
        mtime, g.argv[i]
      );
    }
    db_unset("uv-hash", 0);
    db_end_transaction(0);
  }else{
    usage("add|cat|export|ls|revert|rm|sync");
  }
}

#if 0
***************************************************************************
DESIGN NOTES
Web interface: