Fossil

Check-in [c5a98aa0]
Login

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

Overview
Comment:Incomplete implementation of the "fossil grep" command.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:c5a98aa0ba0692d1df4e9565cfee8ec584543370ba5e53e82bad57b35646a75f
User & Date: drh 2018-06-13 02:02:31
Context
2018-06-13
02:18
The "fossil grep" command now agrees with the documentation. But there is still a lot of opportunity to make enhancements. check-in: 6499c93d user: drh tags: trunk
02:02
Incomplete implementation of the "fossil grep" command. check-in: c5a98aa0 user: drh tags: trunk
2018-06-12
18:52
Reduce automatic scroll offset when multiple ranges are selected check-in: d46491d6 user: andygoth tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/regexp.c.

734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
































755
756
757
758
759
760
761
...
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793

























































*/
int re_add_sql_func(sqlite3 *db){
  return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, 0,
                                 re_sql_func, 0, 0);
}

/*
** Run a "grep" over a single file
*/
static void grep(ReCompiled *pRe, const char *zFile, FILE *in){
  int ln = 0;
  int n;
  char zLine[2000];
  while( fgets(zLine, sizeof(zLine), in) ){
    ln++;
    n = (int)strlen(zLine);
    while( n && (zLine[n-1]=='\n' || zLine[n-1]=='\r') ) n--;
    if( re_match(pRe, (const unsigned char*)zLine, n) ){
      printf("%s:%d:%.*s\n", zFile, ln, n, zLine);
    }
  }
































}

/*
** COMMAND: test-grep
**
** Usage: %fossil test-grep REGEXP [FILE...]
**
................................................................................
  int ignoreCase = find_option("ignore-case","i",0)!=0;
  if( g.argc<3 ){
    usage("REGEXP [FILE...]");
  }
  zErr = re_compile(&pRe, g.argv[2], ignoreCase);
  if( zErr ) fossil_fatal("%s", zErr);
  if( g.argc==3 ){
    grep(pRe, "-", stdin);
  }else{
    int i;
    for(i=3; i<g.argc; i++){
      FILE *in = fossil_fopen(g.argv[i], "rb");
      if( in==0 ){
        fossil_warning("cannot open \"%s\"", g.argv[i]);
      }else{
        grep(pRe, g.argv[i], in);
        fclose(in);
      }
    }
  }
  re_free(pRe);
}
































































|

|








|


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







 







|







|






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
...
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
*/
int re_add_sql_func(sqlite3 *db){
  return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, 0,
                                 re_sql_func, 0, 0);
}

/*
** Run a "grep" over a single file read from disk.
*/
static void grep_file(ReCompiled *pRe, const char *zFile, FILE *in){
  int ln = 0;
  int n;
  char zLine[2000];
  while( fgets(zLine, sizeof(zLine), in) ){
    ln++;
    n = (int)strlen(zLine);
    while( n && (zLine[n-1]=='\n' || zLine[n-1]=='\r') ) n--;
    if( re_match(pRe, (const unsigned char*)zLine, n) ){
      fossil_print("%s:%d:%.*s\n", zFile, ln, n, zLine);
    }
  }
}

/*
** 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,
  const char *z,
  u32 flags
){
  int i, j, n, ln, cnt;
  for(i=j=ln=cnt=0; z[i]; i=j+1){
    for(j=i; z[j] && z[j]!='\n'; j++){}
    n = j - i;
    if( z[j]=='\n' ) j++;
    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
**
** Usage: %fossil test-grep REGEXP [FILE...]
**
................................................................................
  int ignoreCase = find_option("ignore-case","i",0)!=0;
  if( g.argc<3 ){
    usage("REGEXP [FILE...]");
  }
  zErr = re_compile(&pRe, g.argv[2], ignoreCase);
  if( zErr ) fossil_fatal("%s", zErr);
  if( g.argc==3 ){
    grep_file(pRe, "-", stdin);
  }else{
    int i;
    for(i=3; i<g.argc; i++){
      FILE *in = fossil_fopen(g.argv[i], "rb");
      if( in==0 ){
        fossil_warning("cannot open \"%s\"", g.argv[i]);
      }else{
        grep_file(pRe, g.argv[i], in);
        fclose(in);
      }
    }
  }
  re_free(pRe);
}

/*
** COMMAND: grep
**
** Usage: %fossil grep [OPTIONS] PATTERN FILENAME|CHECKIN
**
** Run grep over all historic version of FILENAME or over all files
** in CHECKIN.
**
** Options:
**
**     -i|--ignore-case         Ignore case
**     -l|--files-with-matches  Print only filenames 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<3 ){
    usage("REGEXP FILENAME|CHECKIN");
  }
  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));
    Stmt q;
    if( fnid==0 ){
      fossil_fatal("no such file: \"%s\"", blob_str(&fullName));
    }
    add_content_sql_commands(g.db);
    db_prepare(&q,
      "SELECT content(uuid), substr(uuid,1,10)"
      "  FROM mlink, blob, event"
      " WHERE mlink.mid=event.objid"
      "   AND mlink.fid=blob.rid"
      "   AND mlink.fnid=%d"
      " ORDER BY event.mtime DESC",
      fnid
    );
    while( db_step(&q)==SQLITE_ROW ){
      grep_buffer(pRe, db_column_text(&q,1), db_column_text(&q,0), flags);
    }
    db_finalize(&q);
  }
}