File Annotation
Not logged in
9346f2290c 2008-10-17       drh: /*
9346f2290c 2008-10-17       drh: ** Copyright (c) 2008 D. Richard Hipp
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** This program is free software; you can redistribute it and/or
9346f2290c 2008-10-17       drh: ** modify it under the terms of the GNU General Public
9346f2290c 2008-10-17       drh: ** License version 2 as published by the Free Software Foundation.
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** This program is distributed in the hope that it will be useful,
9346f2290c 2008-10-17       drh: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
9346f2290c 2008-10-17       drh: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9346f2290c 2008-10-17       drh: ** General Public License for more details.
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** You should have received a copy of the GNU General Public
9346f2290c 2008-10-17       drh: ** License along with this library; if not, write to the
9346f2290c 2008-10-17       drh: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
9346f2290c 2008-10-17       drh: ** Boston, MA  02111-1307, USA.
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** Author contact information:
9346f2290c 2008-10-17       drh: **   drh@hwaci.com
9346f2290c 2008-10-17       drh: **   http://www.hwaci.com/drh/
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: *******************************************************************************
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** This file contains code to implement the "all" command-line method.
9346f2290c 2008-10-17       drh: */
9346f2290c 2008-10-17       drh: #include "config.h"
9346f2290c 2008-10-17       drh: #include "allrepo.h"
9346f2290c 2008-10-17       drh: #include <assert.h>
9346f2290c 2008-10-17       drh: 
9346f2290c 2008-10-17       drh: /*
9346f2290c 2008-10-17       drh: ** The input string is a filename.  Return a new copy of this
9346f2290c 2008-10-17       drh: ** filename if the filename requires quoting due to special characters
9346f2290c 2008-10-17       drh: ** such as spaces in the name.
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** If the filename cannot be safely quoted, return a NULL pointer.
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** Space to hold the returned string is obtained from malloc.  A new
9346f2290c 2008-10-17       drh: ** string is returned even if no quoting is needed.
9346f2290c 2008-10-17       drh: */
9346f2290c 2008-10-17       drh: static char *quoteFilename(const char *zFilename){
9346f2290c 2008-10-17       drh:   int i, c;
9346f2290c 2008-10-17       drh:   int needQuote = 0;
9346f2290c 2008-10-17       drh:   for(i=0; (c = zFilename[i])!=0; i++){
9346f2290c 2008-10-17       drh:     if( c=='"' ) return 0;
9346f2290c 2008-10-17       drh:     if( isspace(c) ) needQuote = 1;
9346f2290c 2008-10-17       drh:     if( c=='\\' && zFilename[i+1]==0 ) return 0;
9346f2290c 2008-10-17       drh:     if( c=='$' ) return 0;
9346f2290c 2008-10-17       drh:   }
9346f2290c 2008-10-17       drh:   if( needQuote ){
9346f2290c 2008-10-17       drh:     return mprintf("\"%s\"", zFilename);
9346f2290c 2008-10-17       drh:   }else{
9346f2290c 2008-10-17       drh:     return mprintf("%s", zFilename);
9346f2290c 2008-10-17       drh:   }
9346f2290c 2008-10-17       drh: }
9346f2290c 2008-10-17       drh: 
9346f2290c 2008-10-17       drh: 
9346f2290c 2008-10-17       drh: /*
9346f2290c 2008-10-17       drh: ** COMMAND: all
9346f2290c 2008-10-17       drh: **
e85b538fa8 2009-08-09       bch: ** Usage: %fossil all (list|ls|pull|push|rebuild|sync)
9346f2290c 2008-10-17       drh: **
9346f2290c 2008-10-17       drh: ** The ~/.fossil file records the location of all repositories for a
9346f2290c 2008-10-17       drh: ** user.  This command performs certain operations on all repositories
9346f2290c 2008-10-17       drh: ** that can be useful before or after a period of disconnection operation.
9346f2290c 2008-10-17       drh: ** Available operations are:
9346f2290c 2008-10-17       drh: **
e85b538fa8 2009-08-09       bch: **    list|ls       Display the location of all repositories
2bd0690fe8 2008-10-17       drh: **
2bd0690fe8 2008-10-17       drh: **    pull       Run a "pull" operation on all repositories
2bd0690fe8 2008-10-17       drh: **
2bd0690fe8 2008-10-17       drh: **    push       Run a "push" on all repositories
9346f2290c 2008-10-17       drh: **
2bd0690fe8 2008-10-17       drh: **    rebuild    Rebuild on all repositories
9346f2290c 2008-10-17       drh: **
2bd0690fe8 2008-10-17       drh: **    sync       Run a "sync" on all repositories
9346f2290c 2008-10-17       drh: **
2bd0690fe8 2008-10-17       drh: ** Respositories are automatically added to the set of known repositories
2bd0690fe8 2008-10-17       drh: ** when one of the following commands against the repository: clone, info,
2bd0690fe8 2008-10-17       drh: ** pull, push, or sync
9346f2290c 2008-10-17       drh: */
9346f2290c 2008-10-17       drh: void all_cmd(void){
9346f2290c 2008-10-17       drh:   int n;
9346f2290c 2008-10-17       drh:   Stmt q;
9346f2290c 2008-10-17       drh:   const char *zCmd;
9346f2290c 2008-10-17       drh:   char *zSyscmd;
9346f2290c 2008-10-17       drh:   char *zFossil;
9346f2290c 2008-10-17       drh:   char *zQFilename;
9346f2290c 2008-10-17       drh:   int nMissing;
9346f2290c 2008-10-17       drh: 
9346f2290c 2008-10-17       drh:   if( g.argc<3 ){
e85b538fa8 2009-08-09       bch:     usage("list|ls|pull|push|rebuild|sync");
9346f2290c 2008-10-17       drh:   }
9346f2290c 2008-10-17       drh:   n = strlen(g.argv[2]);
00ac7945a9 2009-08-13       drh:   db_open_config(1);
9346f2290c 2008-10-17       drh:   zCmd = g.argv[2];
9346f2290c 2008-10-17       drh:   if( strncmp(zCmd, "list", n)==0 ){
9346f2290c 2008-10-17       drh:     zCmd = "list";
e85b538fa8 2009-08-09       bch:   }else if( strncmp(zCmd, "ls", n)==0 ){ /* alias for "list" above */
2bd0690fe8 2008-10-17       drh:     zCmd = "list";
9346f2290c 2008-10-17       drh:   }else if( strncmp(zCmd, "push", n)==0 ){
2bd0690fe8 2008-10-17       drh:     zCmd = "push -autourl -R";
9346f2290c 2008-10-17       drh:   }else if( strncmp(zCmd, "pull", n)==0 ){
2bd0690fe8 2008-10-17       drh:     zCmd = "pull -autourl -R";
2bd0690fe8 2008-10-17       drh:   }else if( strncmp(zCmd, "rebuild", n)==0 ){
2bd0690fe8 2008-10-17       drh:     zCmd = "rebuild";
9346f2290c 2008-10-17       drh:   }else if( strncmp(zCmd, "sync", n)==0 ){
2bd0690fe8 2008-10-17       drh:     zCmd = "sync -autourl -R";
9346f2290c 2008-10-17       drh:   }else{
9346f2290c 2008-10-17       drh:     fossil_fatal("\"all\" subcommand should be one of: "
f438092f88 2009-08-09       bch:                  "list ls push pull rebuild sync");
9346f2290c 2008-10-17       drh:   }
9346f2290c 2008-10-17       drh:   zFossil = quoteFilename(g.argv[0]);
9346f2290c 2008-10-17       drh:   nMissing = 0;
2bd0690fe8 2008-10-17       drh:   db_prepare(&q, "SELECT substr(name, 6) FROM global_config"
2bd0690fe8 2008-10-17       drh:                  " WHERE substr(name, 1, 5)=='repo:' ORDER BY 1");
9346f2290c 2008-10-17       drh:   while( db_step(&q)==SQLITE_ROW ){
9346f2290c 2008-10-17       drh:     const char *zFilename = db_column_text(&q, 0);
9346f2290c 2008-10-17       drh:     if( access(zFilename, 0) ){
9346f2290c 2008-10-17       drh:       nMissing++;
9346f2290c 2008-10-17       drh:       continue;
9346f2290c 2008-10-17       drh:     }
9346f2290c 2008-10-17       drh:     if( zCmd[0]=='l' ){
9346f2290c 2008-10-17       drh:       printf("%s\n", zFilename);
9346f2290c 2008-10-17       drh:       continue;
9346f2290c 2008-10-17       drh:     }
9346f2290c 2008-10-17       drh:     zQFilename = quoteFilename(zFilename);
2bd0690fe8 2008-10-17       drh:     zSyscmd = mprintf("%s %s %s", zFossil, zCmd, zQFilename);
9346f2290c 2008-10-17       drh:     printf("%s\n", zSyscmd);
9346f2290c 2008-10-17       drh:     fflush(stdout);
9346f2290c 2008-10-17       drh:     system(zSyscmd);
9346f2290c 2008-10-17       drh:     free(zSyscmd);
9346f2290c 2008-10-17       drh:     free(zQFilename);
9346f2290c 2008-10-17       drh:   }
9346f2290c 2008-10-17       drh: 
9346f2290c 2008-10-17       drh:   /* If any repositories hows names appear in the ~/.fossil file could not
9346f2290c 2008-10-17       drh:   ** be found, remove those names from the ~/.fossil file.
9346f2290c 2008-10-17       drh:   */
9346f2290c 2008-10-17       drh:   if( nMissing ){
9346f2290c 2008-10-17       drh:     db_begin_transaction();
9346f2290c 2008-10-17       drh:     db_reset(&q);
9346f2290c 2008-10-17       drh:     while( db_step(&q)==SQLITE_ROW ){
9346f2290c 2008-10-17       drh:       const char *zFilename = db_column_text(&q, 0);
9346f2290c 2008-10-17       drh:       if( access(zFilename, 0) ){
02eabf94e5 2008-10-17       drh:         char *zRepo = mprintf("repo:%s", zFilename);
9346f2290c 2008-10-17       drh:         db_unset(zRepo, 1);
9346f2290c 2008-10-17       drh:         free(zRepo);
9346f2290c 2008-10-17       drh:       }
9346f2290c 2008-10-17       drh:     }
2bd0690fe8 2008-10-17       drh:     db_reset(&q);
9346f2290c 2008-10-17       drh:     db_end_transaction(0);
9346f2290c 2008-10-17       drh:   }
2bd0690fe8 2008-10-17       drh:   db_finalize(&q);
9346f2290c 2008-10-17       drh: }