PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ssfossil/fossil/src/allrepo.c

https://github.com/paulfitz/coopy
C | 162 lines | 132 code | 1 blank | 29 comment | 2 complexity | bac00c517efbdcd313bfd2d58920b324 MD5 | raw file
  1. /*
  2. ** Copyright (c) 2008 D. Richard Hipp
  3. **
  4. ** This program is free software; you can redistribute it and/or
  5. ** modify it under the terms of the Simplified BSD License (also
  6. ** known as the "2-Clause License" or "FreeBSD License".)
  7. ** This program is distributed in the hope that it will be useful,
  8. ** but without any warranty; without even the implied warranty of
  9. ** merchantability or fitness for a particular purpose.
  10. **
  11. ** Author contact information:
  12. ** drh@hwaci.com
  13. ** http://www.hwaci.com/drh/
  14. **
  15. *******************************************************************************
  16. **
  17. ** This file contains code to implement the "all" command-line method.
  18. */
  19. #include "config.h"
  20. #include "allrepo.h"
  21. #include <assert.h>
  22. /*
  23. ** The input string is a filename. Return a new copy of this
  24. ** filename if the filename requires quoting due to special characters
  25. ** such as spaces in the name.
  26. **
  27. ** If the filename cannot be safely quoted, return a NULL pointer.
  28. **
  29. ** Space to hold the returned string is obtained from malloc. A new
  30. ** string is returned even if no quoting is needed.
  31. */
  32. static char *quoteFilename(const char *zFilename){
  33. int i, c;
  34. int needQuote = 0;
  35. for(i=0; (c = zFilename[i])!=0; i++){
  36. if( c=='"' ) return 0;
  37. if( isspace(c) ) needQuote = 1;
  38. if( c=='\\' && zFilename[i+1]==0 ) return 0;
  39. if( c=='$' ) return 0;
  40. }
  41. if( needQuote ){
  42. return mprintf("\"%s\"", zFilename);
  43. }else{
  44. return mprintf("%s", zFilename);
  45. }
  46. }
  47. /*
  48. ** COMMAND: all
  49. **
  50. ** Usage: %fossil all (list|ls|pull|push|rebuild|sync)
  51. **
  52. ** The ~/.fossil file records the location of all repositories for a
  53. ** user. This command performs certain operations on all repositories
  54. ** that can be useful before or after a period of disconnected operation.
  55. **
  56. ** On Win32 systems, the file is named "_fossil" and is located in
  57. ** %LOCALAPPDATA%, %APPDATA% or %HOMEPATH%.
  58. **
  59. ** Available operations are:
  60. **
  61. ** list | ls Display the location of all repositories
  62. **
  63. ** pull Run a "pull" operation on all repositories
  64. **
  65. ** push Run a "push" on all repositories
  66. **
  67. ** rebuild Rebuild on all repositories
  68. **
  69. ** sync Run a "sync" on all repositories
  70. **
  71. ** Respositories are automatically added to the set of known repositories
  72. ** when one of the following commands against the repository: clone, info,
  73. ** pull, push, or sync
  74. */
  75. void all_cmd(void){
  76. int n;
  77. Stmt q;
  78. const char *zCmd;
  79. char *zSyscmd;
  80. char *zFossil;
  81. char *zQFilename;
  82. int nMissing;
  83. if( g.argc<3 ){
  84. usage("list|ls|pull|push|rebuild|sync");
  85. }
  86. n = strlen(g.argv[2]);
  87. db_open_config(1);
  88. zCmd = g.argv[2];
  89. if( strncmp(zCmd, "list", n)==0 || strncmp(zCmd,"ls",n)==0 ){
  90. zCmd = "list";
  91. }else if( strncmp(zCmd, "push", n)==0 ){
  92. zCmd = "push -autourl -R";
  93. }else if( strncmp(zCmd, "pull", n)==0 ){
  94. zCmd = "pull -autourl -R";
  95. }else if( strncmp(zCmd, "rebuild", n)==0 ){
  96. zCmd = "rebuild";
  97. }else if( strncmp(zCmd, "sync", n)==0 ){
  98. zCmd = "sync -autourl -R";
  99. }else{
  100. fossil_fatal("\"all\" subcommand should be one of: "
  101. "list ls push pull rebuild sync");
  102. }
  103. zFossil = quoteFilename(g.argv[0]);
  104. nMissing = 0;
  105. db_prepare(&q,
  106. "SELECT DISTINCT substr(name, 6) COLLATE nocase"
  107. " FROM global_config"
  108. " WHERE substr(name, 1, 5)=='repo:' ORDER BY 1"
  109. );
  110. while( db_step(&q)==SQLITE_ROW ){
  111. const char *zFilename = db_column_text(&q, 0);
  112. if( access(zFilename, 0) ){
  113. nMissing++;
  114. continue;
  115. }
  116. if( !file_is_canonical(zFilename) ) nMissing++;
  117. if( zCmd[0]=='l' ){
  118. printf("%s\n", zFilename);
  119. continue;
  120. }
  121. zQFilename = quoteFilename(zFilename);
  122. zSyscmd = mprintf("%s %s %s", zFossil, zCmd, zQFilename);
  123. printf("%s\n", zSyscmd);
  124. fflush(stdout);
  125. portable_system(zSyscmd);
  126. free(zSyscmd);
  127. free(zQFilename);
  128. }
  129. /* If any repositories whose names appear in the ~/.fossil file could not
  130. ** be found, remove those names from the ~/.fossil file.
  131. */
  132. if( nMissing ){
  133. db_begin_transaction();
  134. db_reset(&q);
  135. while( db_step(&q)==SQLITE_ROW ){
  136. const char *zFilename = db_column_text(&q, 0);
  137. if( access(zFilename, 0) ){
  138. char *zRepo = mprintf("repo:%s", zFilename);
  139. db_unset(zRepo, 1);
  140. free(zRepo);
  141. }else if( !file_is_canonical(zFilename) ){
  142. Blob cname;
  143. char *zRepo = mprintf("repo:%s", zFilename);
  144. db_unset(zRepo, 1);
  145. free(zRepo);
  146. file_canonical_name(zFilename, &cname);
  147. zRepo = mprintf("repo:%s", blob_str(&cname));
  148. db_set(zRepo, "1", 1);
  149. free(zRepo);
  150. }
  151. }
  152. db_reset(&q);
  153. db_end_transaction(0);
  154. }
  155. db_finalize(&q);
  156. }