File Annotation
Not logged in
dbda8d6ce9 2007-07-21       drh: /*
dbda8d6ce9 2007-07-21       drh: ** Copyright (c) 2007 D. Richard Hipp
dbda8d6ce9 2007-07-21       drh: **
dbda8d6ce9 2007-07-21       drh: ** This program is free software; you can redistribute it and/or
dbda8d6ce9 2007-07-21       drh: ** modify it under the terms of the GNU General Public
dbda8d6ce9 2007-07-21       drh: ** License version 2 as published by the Free Software Foundation.
dbda8d6ce9 2007-07-21       drh: **
dbda8d6ce9 2007-07-21       drh: ** This program is distributed in the hope that it will be useful,
dbda8d6ce9 2007-07-21       drh: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
dbda8d6ce9 2007-07-21       drh: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dbda8d6ce9 2007-07-21       drh: ** General Public License for more details.
dbda8d6ce9 2007-07-21       drh: **
dbda8d6ce9 2007-07-21       drh: ** You should have received a copy of the GNU General Public
dbda8d6ce9 2007-07-21       drh: ** License along with this library; if not, write to the
dbda8d6ce9 2007-07-21       drh: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
dbda8d6ce9 2007-07-21       drh: ** Boston, MA  02111-1307, USA.
dbda8d6ce9 2007-07-21       drh: **
dbda8d6ce9 2007-07-21       drh: ** Author contact information:
dbda8d6ce9 2007-07-21       drh: **   drh@hwaci.com
dbda8d6ce9 2007-07-21       drh: **   http://www.hwaci.com/drh/
dbda8d6ce9 2007-07-21       drh: **
dbda8d6ce9 2007-07-21       drh: *******************************************************************************
dbda8d6ce9 2007-07-21       drh: **
dbda8d6ce9 2007-07-21       drh: ** This file contains code used to check-out versions of the project
dbda8d6ce9 2007-07-21       drh: ** from the local repository.
dbda8d6ce9 2007-07-21       drh: */
dbda8d6ce9 2007-07-21       drh: #include "config.h"
dbda8d6ce9 2007-07-21       drh: #include "add.h"
dbda8d6ce9 2007-07-21       drh: #include <assert.h>
8c4e72e223 2008-12-07       drh: #include <dirent.h>
8c4e72e223 2008-12-07       drh: 
5bc5e88c86 2009-08-14       drh: /*
5bc5e88c86 2009-08-14       drh: ** Set to true if files whose names begin with "." should be
5bc5e88c86 2009-08-14       drh: ** included when processing a recursive "add" command.
5bc5e88c86 2009-08-14       drh: */
5bc5e88c86 2009-08-14       drh: static int includeDotFiles = 0;
dbda8d6ce9 2007-07-21       drh: 
dbda8d6ce9 2007-07-21       drh: /*
8c4e72e223 2008-12-07       drh: ** Add a single file
dbda8d6ce9 2007-07-21       drh: */
8c4e72e223 2008-12-07       drh: static void add_one_file(const char *zName, int vid, Blob *pOmit){
8c4e72e223 2008-12-07       drh:   Blob pathname;
8c4e72e223 2008-12-07       drh:   const char *zPath;
8c4e72e223 2008-12-07       drh: 
8c4e72e223 2008-12-07       drh:   file_tree_name(zName, &pathname, 1);
8c4e72e223 2008-12-07       drh:   zPath = blob_str(&pathname);
8c4e72e223 2008-12-07       drh:   if( strcmp(zPath, "manifest")==0
8c4e72e223 2008-12-07       drh:    || strcmp(zPath, "_FOSSIL_")==0
8c4e72e223 2008-12-07       drh:    || strcmp(zPath, "manifest.uuid")==0
8c4e72e223 2008-12-07       drh:    || blob_compare(&pathname, pOmit)==0
8c4e72e223 2008-12-07       drh:   ){
8c4e72e223 2008-12-07       drh:     fossil_warning("cannot add %s", zPath);
8c4e72e223 2008-12-07       drh:   }else{
da9d38e2c3 2007-10-15       drh:     if( !file_is_simple_pathname(zPath) ){
da9d38e2c3 2007-10-15       drh:       fossil_fatal("filename contains illegal characters: %s", zPath);
20bcbcf2f2 2007-08-08       drh:     }
dbda8d6ce9 2007-07-21       drh:     if( db_exists("SELECT 1 FROM vfile WHERE pathname=%Q", zPath) ){
dbda8d6ce9 2007-07-21       drh:       db_multi_exec("UPDATE vfile SET deleted=0 WHERE pathname=%Q", zPath);
dbda8d6ce9 2007-07-21       drh:     }else{
dbda8d6ce9 2007-07-21       drh:       db_multi_exec(
dbda8d6ce9 2007-07-21       drh:         "INSERT INTO vfile(vid,deleted,rid,mrid,pathname)"
dbda8d6ce9 2007-07-21       drh:         "VALUES(%d,0,0,0,%Q)", vid, zPath);
dbda8d6ce9 2007-07-21       drh:     }
8c4e72e223 2008-12-07       drh:     printf("ADDED  %s\n", zPath);
8c4e72e223 2008-12-07       drh:   }
8c4e72e223 2008-12-07       drh:   blob_reset(&pathname);
8c4e72e223 2008-12-07       drh: }
8c4e72e223 2008-12-07       drh: 
8c4e72e223 2008-12-07       drh: /*
8c4e72e223 2008-12-07       drh: ** All content of the zDir directory to the SFILE table.
8c4e72e223 2008-12-07       drh: */
8c4e72e223 2008-12-07       drh: void add_directory_content(const char *zDir){
8c4e72e223 2008-12-07       drh:   DIR *d;
8c4e72e223 2008-12-07       drh:   int origSize;
8c4e72e223 2008-12-07       drh:   struct dirent *pEntry;
8c4e72e223 2008-12-07       drh:   Blob path;
8c4e72e223 2008-12-07       drh: 
8c4e72e223 2008-12-07       drh:   blob_zero(&path);
8c4e72e223 2008-12-07       drh:   blob_append(&path, zDir, -1);
8c4e72e223 2008-12-07       drh:   origSize = blob_size(&path);
8c4e72e223 2008-12-07       drh:   d = opendir(zDir);
8c4e72e223 2008-12-07       drh:   if( d ){
8c4e72e223 2008-12-07       drh:     while( (pEntry=readdir(d))!=0 ){
8c4e72e223 2008-12-07       drh:       char *zPath;
5bc5e88c86 2009-08-14       drh:       if( pEntry->d_name[0]=='.' ){
5bc5e88c86 2009-08-14       drh:         if( !includeDotFiles ) continue;
5bc5e88c86 2009-08-14       drh:         if( pEntry->d_name[1]==0 ) continue;
5bc5e88c86 2009-08-14       drh:         if( pEntry->d_name[1]=='.' && pEntry->d_name[2]==0 ) continue;
5bc5e88c86 2009-08-14       drh:       }
8c4e72e223 2008-12-07       drh:       blob_appendf(&path, "/%s", pEntry->d_name);
8c4e72e223 2008-12-07       drh:       zPath = blob_str(&path);
8c4e72e223 2008-12-07       drh:       if( file_isdir(zPath)==1 ){
8c4e72e223 2008-12-07       drh:         add_directory_content(zPath);
8c4e72e223 2008-12-07       drh:       }else if( file_isfile(zPath) ){
8c4e72e223 2008-12-07       drh:         db_multi_exec("INSERT INTO sfile VALUES(%Q)", zPath);
8c4e72e223 2008-12-07       drh:       }
8c4e72e223 2008-12-07       drh:       blob_resize(&path, origSize);
8c4e72e223 2008-12-07       drh:     }
8c4e72e223 2008-12-07       drh:   }
8c4e72e223 2008-12-07       drh:   closedir(d);
8c4e72e223 2008-12-07       drh:   blob_reset(&path);
8c4e72e223 2008-12-07       drh: }
844718abbf 2008-05-13       drh: 
8c4e72e223 2008-12-07       drh: /*
8c4e72e223 2008-12-07       drh: ** Add all content of a directory.
8c4e72e223 2008-12-07       drh: */
8c4e72e223 2008-12-07       drh: void add_directory(const char *zDir, int vid, Blob *pOmit){
8c4e72e223 2008-12-07       drh:   Stmt q;
8c4e72e223 2008-12-07       drh:   add_directory_content(zDir);
8c4e72e223 2008-12-07       drh:   db_prepare(&q, "SELECT x FROM sfile ORDER BY x");
8c4e72e223 2008-12-07       drh:   while( db_step(&q)==SQLITE_ROW ){
8c4e72e223 2008-12-07       drh:     const char *zName = db_column_text(&q, 0);
8c4e72e223 2008-12-07       drh:     add_one_file(zName, vid, pOmit);
8c4e72e223 2008-12-07       drh:   }
8c4e72e223 2008-12-07       drh:   db_finalize(&q);
8c4e72e223 2008-12-07       drh:   db_multi_exec("DELETE FROM sfile");
8c4e72e223 2008-12-07       drh: }
844718abbf 2008-05-13       drh: 
844718abbf 2008-05-13       drh: /*
844718abbf 2008-05-13       drh: ** COMMAND: add
844718abbf 2008-05-13       drh: **
844718abbf 2008-05-13       drh: ** Usage: %fossil add FILE...
844718abbf 2008-05-13       drh: **
844718abbf 2008-05-13       drh: ** Make arrangements to add one or more files to the current checkout
844718abbf 2008-05-13       drh: ** at the next commit.
5bc5e88c86 2009-08-14       drh: **
5bc5e88c86 2009-08-14       drh: ** When adding files recursively, filenames that begin with "." are
5bc5e88c86 2009-08-14       drh: ** excluded by default.  To include such files, add the "--dotfiles"
5bc5e88c86 2009-08-14       drh: ** option to the command-line.
844718abbf 2008-05-13       drh: */
844718abbf 2008-05-13       drh: void add_cmd(void){
844718abbf 2008-05-13       drh:   int i;
844718abbf 2008-05-13       drh:   int vid;
141c31792b 2008-07-23       drh:   Blob repo;
844718abbf 2008-05-13       drh: 
5bc5e88c86 2009-08-14       drh:   includeDotFiles = find_option("dotfiles",0,0)!=0;
844718abbf 2008-05-13       drh:   db_must_be_within_tree();
844718abbf 2008-05-13       drh:   vid = db_lget_int("checkout",0);
844718abbf 2008-05-13       drh:   if( vid==0 ){
844718abbf 2008-05-13       drh:     fossil_panic("no checkout to add to");
844718abbf 2008-05-13       drh:   }
844718abbf 2008-05-13       drh:   db_begin_transaction();
141c31792b 2008-07-23       drh:   if( !file_tree_name(g.zRepositoryName, &repo, 0) ){
141c31792b 2008-07-23       drh:     blob_zero(&repo);
141c31792b 2008-07-23       drh:   }
8c4e72e223 2008-12-07       drh:   db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
844718abbf 2008-05-13       drh:   for(i=2; i<g.argc; i++){
844718abbf 2008-05-13       drh:     char *zName;
844718abbf 2008-05-13       drh:     int isDir;
844718abbf 2008-05-13       drh: 
844718abbf 2008-05-13       drh:     zName = mprintf("%/", g.argv[i]);
844718abbf 2008-05-13       drh:     isDir = file_isdir(zName);
8c4e72e223 2008-12-07       drh:     if( isDir==1 ){
8c4e72e223 2008-12-07       drh:       add_directory(zName, vid, &repo);
8c4e72e223 2008-12-07       drh:     }else if( isDir==0 ){
844718abbf 2008-05-13       drh:       fossil_fatal("not found: %s", zName);
8c4e72e223 2008-12-07       drh:     }else if( access(zName, R_OK) ){
844718abbf 2008-05-13       drh:       fossil_fatal("cannot open %s", zName);
844718abbf 2008-05-13       drh:     }else{
8c4e72e223 2008-12-07       drh:       add_one_file(zName, vid, &repo);
8c4e72e223 2008-12-07       drh:     }
dbda8d6ce9 2007-07-21       drh:     free(zName);
dbda8d6ce9 2007-07-21       drh:   }
dbda8d6ce9 2007-07-21       drh:   db_end_transaction(0);
dbda8d6ce9 2007-07-21       drh: }
dbda8d6ce9 2007-07-21       drh: 
dbda8d6ce9 2007-07-21       drh: /*
dbda8d6ce9 2007-07-21       drh: ** COMMAND: rm
dbda8d6ce9 2007-07-21       drh: ** COMMAND: del
dbda8d6ce9 2007-07-21       drh: **
c9fdb846fb 2007-08-18       drh: ** Usage: %fossil rm FILE...
c9fdb846fb 2007-08-18       drh: **    or: %fossil del FILE...
e146d800ac 2008-11-09       drh: **
dbda8d6ce9 2007-07-21       drh: ** Remove one or more files from the tree.
e146d800ac 2008-11-09       drh: **
e146d800ac 2008-11-09       drh: ** This command does not remove the files from disk.  It just marks the
e146d800ac 2008-11-09       drh: ** files as no longer being part of the project.  In other words, future
e146d800ac 2008-11-09       drh: ** changes to the named files will not be versioned.
dbda8d6ce9 2007-07-21       drh: */
dbda8d6ce9 2007-07-21       drh: void del_cmd(void){
dbda8d6ce9 2007-07-21       drh:   int i;
dbda8d6ce9 2007-07-21       drh:   int vid;
dbda8d6ce9 2007-07-21       drh: 
dbda8d6ce9 2007-07-21       drh:   db_must_be_within_tree();
dbda8d6ce9 2007-07-21       drh:   vid = db_lget_int("checkout", 0);
dbda8d6ce9 2007-07-21       drh:   if( vid==0 ){
dbda8d6ce9 2007-07-21       drh:     fossil_panic("no checkout to remove from");
dbda8d6ce9 2007-07-21       drh:   }
dbda8d6ce9 2007-07-21       drh:   db_begin_transaction();
dbda8d6ce9 2007-07-21       drh:   for(i=2; i<g.argc; i++){
dbda8d6ce9 2007-07-21       drh:     char *zName;
dbda8d6ce9 2007-07-21       drh:     char *zPath;
dbda8d6ce9 2007-07-21       drh:     Blob pathname;
dbda8d6ce9 2007-07-21       drh: 
c7278fd013 2007-09-22       jnc:     zName = mprintf("%/", g.argv[i]);
2ecc407d9b 2008-07-23       drh:     file_tree_name(zName, &pathname, 1);
dbda8d6ce9 2007-07-21       drh:     zPath = blob_str(&pathname);
dbda8d6ce9 2007-07-21       drh:     if( !db_exists(
dbda8d6ce9 2007-07-21       drh:              "SELECT 1 FROM vfile WHERE pathname=%Q AND NOT deleted", zPath) ){
20bcbcf2f2 2007-08-08       drh:       fossil_fatal("not in the repository: %s", zName);
dbda8d6ce9 2007-07-21       drh:     }else{
dbda8d6ce9 2007-07-21       drh:       db_multi_exec("UPDATE vfile SET deleted=1 WHERE pathname=%Q", zPath);
dbda8d6ce9 2007-07-21       drh:     }
dbda8d6ce9 2007-07-21       drh:     blob_reset(&pathname);
dbda8d6ce9 2007-07-21       drh:     free(zName);
dbda8d6ce9 2007-07-21       drh:   }
dbda8d6ce9 2007-07-21       drh:   db_multi_exec("DELETE FROM vfile WHERE deleted AND rid=0");
e146d800ac 2008-11-09       drh:   db_end_transaction(0);
e146d800ac 2008-11-09       drh: }
e146d800ac 2008-11-09       drh: 
e146d800ac 2008-11-09       drh: /*
e146d800ac 2008-11-09       drh: ** Rename a single file.
e146d800ac 2008-11-09       drh: **
e146d800ac 2008-11-09       drh: ** The original name of the file is zOrig.  The new filename is zNew.
e146d800ac 2008-11-09       drh: */
e146d800ac 2008-11-09       drh: static void mv_one_file(int vid, const char *zOrig, const char *zNew){
e146d800ac 2008-11-09       drh:   printf("RENAME %s %s\n", zOrig, zNew);
e146d800ac 2008-11-09       drh:   db_multi_exec(
e146d800ac 2008-11-09       drh:     "UPDATE vfile SET pathname='%s' WHERE pathname='%s' AND vid=%d",
e146d800ac 2008-11-09       drh:     zNew, zOrig, vid
e146d800ac 2008-11-09       drh:   );
e146d800ac 2008-11-09       drh: }
e146d800ac 2008-11-09       drh: 
e146d800ac 2008-11-09       drh: /*
e146d800ac 2008-11-09       drh: ** COMMAND: mv
e146d800ac 2008-11-09       drh: ** COMMAND: rename
e146d800ac 2008-11-09       drh: **
e146d800ac 2008-11-09       drh: ** Usage: %fossil mv|rename OLDNAME NEWNAME
e146d800ac 2008-11-09       drh: **    or: %fossil mv|rename OLDNAME... DIR
e146d800ac 2008-11-09       drh: **
e146d800ac 2008-11-09       drh: ** Move or rename one or more files within the tree
e146d800ac 2008-11-09       drh: **
67f44bb301 2008-11-12    kejoki: ** This command does not rename the files on disk.  All this command does is
e146d800ac 2008-11-09       drh: ** record the fact that filenames have changed so that appropriate notations
e146d800ac 2008-11-09       drh: ** can be made at the next commit/checkin.
e146d800ac 2008-11-09       drh: */
e146d800ac 2008-11-09       drh: void mv_cmd(void){
e146d800ac 2008-11-09       drh:   int i;
e146d800ac 2008-11-09       drh:   int vid;
e146d800ac 2008-11-09       drh:   char *zDest;
e146d800ac 2008-11-09       drh:   Blob dest;
e146d800ac 2008-11-09       drh:   Stmt q;
e146d800ac 2008-11-09       drh: 
e146d800ac 2008-11-09       drh:   db_must_be_within_tree();
e146d800ac 2008-11-09       drh:   vid = db_lget_int("checkout", 0);
e146d800ac 2008-11-09       drh:   if( vid==0 ){
e146d800ac 2008-11-09       drh:     fossil_panic("no checkout rename files in");
e146d800ac 2008-11-09       drh:   }
e146d800ac 2008-11-09       drh:   if( g.argc<4 ){
e146d800ac 2008-11-09       drh:     usage("OLDNAME NEWNAME");
e146d800ac 2008-11-09       drh:   }
e146d800ac 2008-11-09       drh:   zDest = g.argv[g.argc-1];
e146d800ac 2008-11-09       drh:   db_begin_transaction();
e146d800ac 2008-11-09       drh:   file_tree_name(zDest, &dest, 1);
e146d800ac 2008-11-09       drh:   db_multi_exec(
e146d800ac 2008-11-09       drh:     "UPDATE vfile SET origname=pathname WHERE origname IS NULL;"
e146d800ac 2008-11-09       drh:   );
e146d800ac 2008-11-09       drh:   db_multi_exec(
e146d800ac 2008-11-09       drh:     "CREATE TEMP TABLE mv(f TEXT UNIQUE ON CONFLICT IGNORE, t TEXT);"
e146d800ac 2008-11-09       drh:   );
e146d800ac 2008-11-09       drh:   if( file_isdir(zDest)!=1 ){
e146d800ac 2008-11-09       drh:     Blob orig;
e146d800ac 2008-11-09       drh:     if( g.argc!=4 ){
e146d800ac 2008-11-09       drh:       usage("OLDNAME NEWNAME");
e146d800ac 2008-11-09       drh:     }
e146d800ac 2008-11-09       drh:     file_tree_name(g.argv[2], &orig, 1);
e146d800ac 2008-11-09       drh:     db_multi_exec(
e146d800ac 2008-11-09       drh:       "INSERT INTO mv VALUES(%B,%B)", &orig, &dest
e146d800ac 2008-11-09       drh:     );
e146d800ac 2008-11-09       drh:   }else{
9fd8009007 2008-11-09       drh:     if( blob_eq(&dest, ".") ){
9fd8009007 2008-11-09       drh:       blob_reset(&dest);
9fd8009007 2008-11-09       drh:     }else{
9fd8009007 2008-11-09       drh:       blob_append(&dest, "/", 1);
9fd8009007 2008-11-09       drh:     }
e146d800ac 2008-11-09       drh:     for(i=2; i<g.argc-1; i++){
e146d800ac 2008-11-09       drh:       Blob orig;
e146d800ac 2008-11-09       drh:       char *zOrig;
e146d800ac 2008-11-09       drh:       int nOrig;
e146d800ac 2008-11-09       drh:       file_tree_name(g.argv[i], &orig, 1);
e146d800ac 2008-11-09       drh:       zOrig = blob_str(&orig);
e146d800ac 2008-11-09       drh:       nOrig = blob_size(&orig);
e146d800ac 2008-11-09       drh:       db_prepare(&q,
e146d800ac 2008-11-09       drh:          "SELECT pathname FROM vfile"
e146d800ac 2008-11-09       drh:          " WHERE vid=%d"
e146d800ac 2008-11-09       drh:          "   AND (pathname='%s' OR pathname GLOB '%s/*')"
e146d800ac 2008-11-09       drh:          " ORDER BY 1",
e146d800ac 2008-11-09       drh:          vid, zOrig, zOrig
e146d800ac 2008-11-09       drh:       );
e146d800ac 2008-11-09       drh:       while( db_step(&q)==SQLITE_ROW ){
e146d800ac 2008-11-09       drh:         const char *zPath = db_column_text(&q, 0);
e146d800ac 2008-11-09       drh:         int nPath = db_column_bytes(&q, 0);
e146d800ac 2008-11-09       drh:         const char *zTail;
e146d800ac 2008-11-09       drh:         if( nPath==nOrig ){
e146d800ac 2008-11-09       drh:           zTail = file_tail(zPath);
e146d800ac 2008-11-09       drh:         }else{
e146d800ac 2008-11-09       drh:           zTail = &zPath[nOrig+1];
e146d800ac 2008-11-09       drh:         }
e146d800ac 2008-11-09       drh:         db_multi_exec(
9fd8009007 2008-11-09       drh:           "INSERT INTO mv VALUES('%s','%s%s')",
e146d800ac 2008-11-09       drh:           zPath, blob_str(&dest), zTail
e146d800ac 2008-11-09       drh:         );
e146d800ac 2008-11-09       drh:       }
e146d800ac 2008-11-09       drh:       db_finalize(&q);
e146d800ac 2008-11-09       drh:     }
e146d800ac 2008-11-09       drh:   }
e146d800ac 2008-11-09       drh:   db_prepare(&q, "SELECT f, t FROM mv ORDER BY f");
e146d800ac 2008-11-09       drh:   while( db_step(&q)==SQLITE_ROW ){
e146d800ac 2008-11-09       drh:     const char *zFrom = db_column_text(&q, 0);
e146d800ac 2008-11-09       drh:     const char *zTo = db_column_text(&q, 1);
e146d800ac 2008-11-09       drh:     mv_one_file(vid, zFrom, zTo);
e146d800ac 2008-11-09       drh:   }
e146d800ac 2008-11-09       drh:   db_finalize(&q);
dbda8d6ce9 2007-07-21       drh:   db_end_transaction(0);
dbda8d6ce9 2007-07-21       drh: }