Patchwork [24,of,55,RFC,c-hglib:level1] hg_grep: creating a high level function for mercurial grep command

login
register
mail settings
Submitter Iulian Stana
Date Sept. 14, 2013, 12:35 a.m.
Message ID <72ea63f42ec0e0f871d8.1379118936@doppler>
Download mbox | patch
Permalink /patch/2468/
State Deferred, archived
Headers show

Comments

Iulian Stana - Sept. 14, 2013, 12:35 a.m.
# HG changeset patch
# User Iulian Stana <julian.stana@gmail.com>
# Date 1379112051 -10800
#      Sat Sep 14 01:40:51 2013 +0300
# Node ID 72ea63f42ec0e0f871d8c6cd9edb3c093e2df439
# Parent  80cead1fa1ce3870bffc797bf1b8cefafa1a1fc8
hg_grep: creating a high level function for mercurial grep command

Patch

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -794,6 +794,26 @@ 
 	return exitcode;
 }
 
+/* The high level grep command for hglib API. */
+hg_linestream_buffer *hg_grep(hg_handle *handle, int (*callback)
+			(const char *msg, size_t len), char *argument[])
+{
+	hg_linestream_buffer *gbuf = malloc(sizeof(hg_csetstream_buffer));
+	gbuf->handle = handle;
+
+	gbuf->command = cmdbuilder("grep", argument, NULL);
+
+	if(hg_rawcommand(handle, gbuf->command) < 0){
+		return NULL;
+	}
+
+	gbuf->callback = callback;
+	gbuf->buffer = NULL;
+	gbuf->buf_size = 0;
+
+	return gbuf;
+}
+
 /* The yield next step. Getting the next entry. */
 int hg_fetch_entry(hg_stream_buffer *stream, int (*detect_byte)(char *buff, 
 			int buf_size, int data_on_pipe), int func_type)
diff --git a/client.h b/client.h
--- a/client.h
+++ b/client.h
@@ -1017,6 +1017,56 @@ 
 							char *argument[]);
 
 /**
+ * \brief hg_grep command for hglib API.
+ *
+ * Search revisions of files for a regular expression.
+ *
+ * This command behaves differently than Unix grep. It only accepts Python/Perl
+ * regexps. It searches repository history, not the working directory. It always
+ * prints the revision number in which a match appears.
+ *
+ * By default, grep only prints output for the first revision of a file in which
+ * it finds a match. To get it to print every revision that contains a change in
+ * match status ("-" for a match that becomes a non-match, or "+" for a
+ * non-match that becomes a match), use the --all flag.
+ *
+ * Options/Argument list option:
+ *
+ *	-0, --print0	end fields with NUL
+ *	--all		print all revisions that match
+ *	-a, --text		treat all files as text
+ *	-f, --follow	follow changeset history, or file history across copies
+ *				and renames
+ *	-i, --ignore-case	ignore case when matching
+ *	-l, --files-with-matches
+ *				print only filenames and revisions that match
+ *	-n, --line-number	print matching line numbers
+ *	-r, --rev		only search files changed within revision range
+ *	-u, --user		list the author (long with -v)
+ *	-d, --date		list the date (short with -q)
+ *	-I, --include	include names matching the given patterns
+ *	-X, --exclude	exclude names matching the given patterns
+ *
+ * To get grep information use the returned hg_linestream_buffer structure with
+ * hg_fetch_line_entry() function.
+ * 
+ * If you use -o option, you still need to call hg_fetch_line_entry() function.
+ *
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \param callback A function that will handle error data. 
+ *                 A NULL pointer will ignore error data.
+ * \param argument The option list. Will contain all option that you wish.
+ * \retval hg_linestream_buffer A pointer to hg_linestream_buffer structure if 
+ *                              successful
+ * \retval NULL to indicate an error, with errno set appropriately.
+ *
+ * errno can be:
+ *      - hg_rawcommand errors
+ * */
+hg_linestream_buffer *hg_grep(hg_handle *handle, int (*callback)
+			(const char *msg, size_t len), char *argument[]);
+
+/**
  * \brief The yield mechanism that will get the next entry.
  *
  * This function is used inside of hg_fetch_cset_entry() and hg_fetch_line_entry()