Fossil

Check-in [1bf2f848]
Login

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

Overview
Comment:Enhance the "fossil grep" command with new options. Work in progress. Needs more testing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | grep-enhancements
Files: files | file ages | folders
SHA3-256: 1bf2f84843b063c61afc7f3ba95313dc5035d69d57dcad1266c4811fe1167b01
User & Date: drh 2019-11-29 14:44:04
Context
2019-11-30
13:38
Completely rework the "fossil grep" command. Omit the -H option. Instead, print a header line that includes both the file hash and the check-in hash and the timestamp for every file that contains any match. Scan all files together, in reverse chronological order. Leaf check-in: 9c2080a3 user: drh tags: grep-enhancements
2019-11-29
14:44
Enhance the "fossil grep" command with new options. Work in progress. Needs more testing. check-in: 1bf2f848 user: drh tags: grep-enhancements
2019-11-28
10:31
Changes to support CGI on IIS web servers. check-in: c06e0b2d user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/regexp.c.

717
718
719
720
721
722
723

724
725
726
727
728
729
730
...
735
736
737
738
739
740
741
742
743
744

745

746
747
748
749
750
751
752
...
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799




800
801





802
803
804
805
806
807
808
809
810








811
812
813
814












815
816
817
818
819
820
821
822







823
824
825
826






827
828
829
830
831
832
833
834

835
836
837

838
839
840


841
842








843






844








845
846
847
  }
}

/*
** Flags for grep_buffer()
*/
#define GREP_EXISTS    0x001    /* If any match, print only the name and stop */


/*
** Run a "grep" over a text file
*/
static int grep_buffer(
  ReCompiled *pRe,
  const char *zName,
................................................................................
  for(i=j=ln=cnt=0; z[i]; i=j+1){
    for(j=i; z[j] && z[j]!='\n'; j++){}
    n = j - i;
    ln++;
    if( re_match(pRe, (const unsigned char*)(z+i), j-i) ){
      cnt++;
      if( flags & GREP_EXISTS ){
        fossil_print("%S\n", zName);
        break;
      }

      fossil_print("%S:%d:%.*s\n", zName, ln, n, z+i);

    }
  }
  return cnt;
}

/*
** COMMAND: test-grep
................................................................................
  }
  re_free(pRe);
}

/*
** COMMAND: grep
**
** Usage: %fossil grep [OPTIONS] PATTERN FILENAME
**
** Attempt to match the given POSIX extended regular expression PATTERN
** over all historic versions of FILENAME.  For details of the supported
** RE dialect, see https://fossil-scm.org/fossil/doc/trunk/www/grep.md
**
** Options:
**




**     -i|--ignore-case         Ignore case
**     -l|--files-with-matches  List only checkin ID for versions that match





**     -v|--verbose             Show each file as it is analyzed
*/
void re_grep_cmd(void){
  u32 flags = 0;
  int bVerbose = 0;
  ReCompiled *pRe;
  const char *zErr;
  int ignoreCase = 0;
  Blob fullName;









  if( find_option("ignore-case","i",0)!=0 ) ignoreCase = 1;
  if( find_option("files-with-matches","l",0)!=0 ) flags |= GREP_EXISTS;
  if( find_option("verbose","v",0)!=0 ) bVerbose = 1;












  db_find_and_open_repository(0, 0);
  verify_all_options();
  if( g.argc<4 ){
    usage("REGEXP FILENAME");
  }
  zErr = re_compile(&pRe, g.argv[2], ignoreCase);
  if( zErr ) fossil_fatal("%s", zErr);








  if( file_tree_name(g.argv[3], &fullName, 0, 0) ){
    int fnid = db_int(0, "SELECT fnid FROM filename WHERE name=%Q",
                      blob_str(&fullName));
    if( fnid ){






      Stmt q;
      add_content_sql_commands(g.db);
      db_prepare(&q,
        "SELECT content(ux), ux FROM ("
        "  SELECT blob.uuid AS ux, min(event.mtime) AS mx"
        "    FROM mlink, blob, event"
        "   WHERE mlink.mid=event.objid"
        "     AND mlink.fid=blob.rid"

        "     AND mlink.fnid=%d"
        "   GROUP BY blob.uuid"
        ") ORDER BY mx DESC;",

        fnid
      );
      while( db_step(&q)==SQLITE_ROW ){


        if( bVerbose ) fossil_print("%S:\n", db_column_text(&q,1));
        grep_buffer(pRe, db_column_text(&q,1), db_column_text(&q,0), flags);








      }






      db_finalize(&q);








    }
  }
}







>







 







|


>
|
>







 







|







>
>
>
>
|
|
>
>
>
>
>
|








>
>
>
>
>
>
>
>



|
>
>
>
>
>
>
>
>
>
>
>
>



|




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



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
...
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
...
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889

890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
  }
}

/*
** Flags for grep_buffer()
*/
#define GREP_EXISTS    0x001    /* If any match, print only the name and stop */
#define GREP_QUIET     0x002    /* Return code only */

/*
** Run a "grep" over a text file
*/
static int grep_buffer(
  ReCompiled *pRe,
  const char *zName,
................................................................................
  for(i=j=ln=cnt=0; z[i]; i=j+1){
    for(j=i; z[j] && z[j]!='\n'; j++){}
    n = j - i;
    ln++;
    if( re_match(pRe, (const unsigned char*)(z+i), j-i) ){
      cnt++;
      if( flags & GREP_EXISTS ){
        if( (flags & GREP_QUIET)==0 ) fossil_print("%S\n", zName);
        break;
      }
      if( (flags & GREP_QUIET)==0 ){
        fossil_print("%S:%d:%.*s\n", zName, ln, n, z+i);
      }
    }
  }
  return cnt;
}

/*
** COMMAND: test-grep
................................................................................
  }
  re_free(pRe);
}

/*
** COMMAND: grep
**
** Usage: %fossil grep [OPTIONS] PATTERN FILENAME ...
**
** Attempt to match the given POSIX extended regular expression PATTERN
** over all historic versions of FILENAME.  For details of the supported
** RE dialect, see https://fossil-scm.org/fossil/doc/trunk/www/grep.md
**
** Options:
**
**     -c|--count                 Suppress normal output; instead print a count
**                                of the number of matching files
**     -H|--checkin-hash          Show the check-in hash rather than
**                                file artifact hash for each match
**     -i|--ignore-case           Ignore case
**     -l|--files-with-matches    List only hash for each match
**     --once                     Stop searching after the first match
**     -s|--no-messages           Suppress error messages about nonexistant
**                                or unreadable files
**     -v|--invert-match          Invert the sense of matching.  Show only
**                                files that have no matches. Implies -l
**     --verbose                  Show each file as it is analyzed
*/
void re_grep_cmd(void){
  u32 flags = 0;
  int bVerbose = 0;
  ReCompiled *pRe;
  const char *zErr;
  int ignoreCase = 0;
  Blob fullName;
  int ckinHash = 0;
  int ii;
  int nMatch = 0;
  int bNoMsg;
  int cntFlag;
  int bOnce;
  int bInvert;
  int nSearch = 0;

  if( find_option("ignore-case","i",0)!=0 ) ignoreCase = 1;
  if( find_option("files-with-matches","l",0)!=0 ) flags |= GREP_EXISTS;
  if( find_option("verbose",0,0)!=0 ) bVerbose = 1;
  ckinHash = find_option("checkin-hash","H",0)!=0;
  if( find_option("quiet","q",0) ) flags |= GREP_QUIET|GREP_EXISTS;
  bNoMsg = find_option("no-messages","s",0)!=0;
  bOnce = find_option("once",0,0)!=0;
  bInvert = find_option("invert-match","v",0)!=0;
  if( bInvert ){
    flags |= GREP_QUIET|GREP_EXISTS;
  }
  cntFlag = find_option("count","c",0)!=0;
  if( cntFlag ){
    flags |= GREP_QUIET|GREP_EXISTS;
  }
  db_find_and_open_repository(0, 0);
  verify_all_options();
  if( g.argc<4 ){
    usage("REGEXP FILENAME ...");
  }
  zErr = re_compile(&pRe, g.argv[2], ignoreCase);
  if( zErr ) fossil_fatal("%s", zErr);

  add_content_sql_commands(g.db);
  for(ii=3; ii<g.argc; ii++){
    const char *zTarget = g.argv[ii];
    if( nMatch ){
      if( (flags & GREP_QUIET)!=0 ) break;
      if( bOnce ) break;
    }
    if( file_tree_name(zTarget, &fullName, 0, 1) ){
      int fnid = db_int(0, "SELECT fnid FROM filename WHERE name=%Q",
                        blob_str(&fullName));
      if( !fnid ){
        if( bNoMsg ) continue;
        if( file_size(zTarget, ExtFILE)<0 ){
          fossil_fatal("no such file: %s", zTarget);
        }
        fossil_fatal("not a managed file: %s", zTarget);
      }else{
        Stmt q;

        db_prepare(&q,
          "SELECT content(ux), %w FROM ("
          "  SELECT A.uuid AS ux, B.uuid AS ckin, min(event.mtime) AS mx"
          "    FROM mlink, blob A, blob B, event"
          "   WHERE mlink.mid=event.objid"
          "     AND mlink.fid=A.rid"
          "     AND mlink.mid=B.rid"
          "     AND mlink.fnid=%d"
          "   GROUP BY A.uuid"
          ") ORDER BY mx DESC;",
          ckinHash ? "ckin" : "ux",
          fnid
        );
        while( db_step(&q)==SQLITE_ROW ){
          const char *zHash = db_column_text(&q,1);
          const char *zContent = db_column_text(&q,0);
          if( bVerbose ) fossil_print("%S:\n", zHash);

          nSearch++;
          nMatch += grep_buffer(pRe, zHash, zContent, flags);
          if( bInvert && cntFlag==0 ){
            if( nMatch==0 ){
              fossil_print("%S\n", zHash);
              if( bOnce ) nMatch = 1;
            }else{
              nMatch = 0;
            }
          }
          if( nMatch ){
            if( (flags & GREP_QUIET)!=0 ) break;
            if( bOnce ) break;
          }
        }
        db_finalize(&q);
      }
    }
  }
  if( cntFlag ){
    if( bInvert ){
      fossil_print("%d\n", nSearch-nMatch);
    }else{
      fossil_print("%d\n", nMatch);
    }
  }
}