Patchwork [08,of,55,RFC,c-hglib:level1] hg_annotate: creating a high level function for mercurial annotate command

login
register
mail settings
Submitter Iulian Stana
Date Sept. 14, 2013, 12:35 a.m.
Message ID <f3bcd2f00b3ee02fba48.1379118920@doppler>
Download mbox | patch
Permalink /patch/2452/
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 1379110427 -10800
#      Sat Sep 14 01:13:47 2013 +0300
# Node ID f3bcd2f00b3ee02fba48024dd95c757509791d66
# Parent  1af66df0c7bf87a3c9df7f8d2599320cc7939804
hg_annotate: creating a high level function for mercurial annotate command

Patch

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -9,7 +9,9 @@ 
 #include "client.h"
 #include "utils.h"
 
+#define BUFF_SIZE 4096
 #define HGPATH "hg"
+#define CHANGESET "\\0{rev}\\n{node}\\n{tags}\\n{branch}\\n{author}\\n{date|isodate}\\n{desc}"
 
 /* Release data from handle pointers. */
 void free_data(hg_handle *handle){
@@ -401,6 +403,26 @@ 
 	return exitcode;
 }
 
+/* The high level annotate command for hglib API. */
+hg_linestream_buffer *hg_annotate(hg_handle *handle, int (*callback)
+			(const char *msg, size_t len), char *argument[])
+{
+	hg_linestream_buffer *lbuf = malloc(sizeof(hg_csetstream_buffer));
+	lbuf->handle = handle;
+
+	lbuf->command = cmdbuilder("annotate", argument, NULL);
+
+	if(hg_rawcommand(handle, lbuf->command) < 0){
+		return NULL;
+	}
+
+	lbuf->callback = callback;
+	lbuf->buffer = NULL;
+	lbuf->buf_size = 0;
+
+	return lbuf;
+}
+
 /* 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)
@@ -481,3 +503,22 @@ 
 	}
 	return exitcode;
 }
+
+/* The lbuf next step. Getting the next changeset. */
+int hg_fetch_annotate_entry(hg_linestream_buffer *lbuf, 
+						hg_annotate_entry *aentry)
+{
+	char *buff = NULL;
+	int return_value = hg_fetch_line_entry(lbuf, &buff);
+
+	if(return_value <= 0)
+		return return_value;
+
+	aentry->info = buff;
+	char *aux = buff;
+	aux = strstr(aux, ": ");
+	buff[aux - buff] = '\0';
+	aentry->content = aux + 2;
+
+	return return_value;
+}
diff --git a/client.h b/client.h
--- a/client.h
+++ b/client.h
@@ -141,6 +141,28 @@ 
 typedef struct hg_stream_buffer hg_csetstream_buffer;
 
 /**
+ * \struct hg_annotate_entry
+ * \brief This structure will contain information about an annotate line 
+ *
+ * \var hg_annotate_entry::info
+ * The info field will stock the wished information about a line(annotate).
+ * \var hg_annotate_entry::content
+ * The content field will stock the content of the line(annotate).
+ *
+ * \typedef hg_annotate_entry
+ * \brief This structure will contain information about an annotate line 
+ *
+ * \var info
+ * The info field will stock the wished information about a line(annotate).
+ * \var content
+ * The content field will stock the content of the line(annotate).
+ * */
+typedef struct hg_annotate_entry{
+	char *info;
+	char *content;
+}hg_annotate_entry;
+
+/**
  * \brief Reading the header from cmdsrv.
  *
  * The function will read the header from the command server and will save it to
@@ -348,6 +370,63 @@ 
 							char *argument[]);
 
 /**
+ * \brief hg_annotate command for hglib API.
+ *
+ * List changes in files, showing the revision id responsible for each line
+ *
+ * This command is useful for discovering when a change was made and by whom.
+ *
+ * Without the -a/--text option, annotate will avoid processing files it detects
+ * as binary. With -a, annotate will annotate the file anyway, although the
+ * results will probably be neither useful nor desirable.
+ *
+ * Options/Argument list option:
+ *
+ *	-r, --rev		annotate the specified revision
+ *	--follow		follow copies/renames and list the filename
+ *				(DEPRECATED)
+ *	--no-follow	don't follow copies and renames
+ *	-a, --text		treat all files as text
+ *	-u, --user		list the author (long with -v)
+ *	-f, --file		list the filename
+ *	-d, --date		list the date (short with -q)
+ *	-n, --number	list the revision number (default)
+ *	-c, --changeset	list the changeset
+ *	-l, --line-number	show line number at the first appearance
+ *	-w, --ignore-all-space
+ *				ignore white space when comparing lines
+ *	-b, --ignore-space-change
+ *				ignore changes in the amount of white space
+ *	-B, --ignore-blank-lines
+ *				ignore changes whose lines are all blank
+ *	-I, --include	include names matching the given patterns
+ *	-X, --exclude	exclude names matching the given patterns
+ *
+ *	aliases: blame
+ *
+ * To get annotate information use the returned hg_linestream_buffer structure 
+ * with hg_fetch_line_entry() or hg_annotate_line_entry().
+ *
+ * - Using the return value in hg_fetch_line_entry() function you will get 
+ * unparse data, one line at once.
+ * - Using the return value in hg_fetch_annotate_entry() function you will 
+ * get the data parse in hg_annotate_entry structure.
+ *
+ * \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_csetstream_buffer A pointer to hg_csetstream_buffer structure if 
+ *                              successful
+ * \retval NULL to indicate an error, with errno set appropriately.
+ *
+ * errno can be:
+ *      - hg_rawcommand errors
+ * */
+hg_linestream_buffer *hg_annotate(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()
@@ -420,4 +499,25 @@ 
  * */
 int hg_fetch_line_entry(hg_linestream_buffer *lbuf, char **lentry);
 
+/**
+ * \brief The iterator step. Getting the next line.
+ *
+ * Some commands could perform huge amount of data, to pass this data to users
+ * in a parse way mode I provide this function. This function will return a line
+ * in a single call.
+ *
+ * \param lbuf The buffer structure to store line data.
+ * \param aentry Address where a line will be placed in a parse way.
+ * \retval 1  Succesful operation, pass the first find line to lentry pointer.
+ * \retval 0  To indicate the end of command, everything works well.
+ * \retval -1 to indicate an error, with errno set appropriately.
+ *
+ * errno can be:
+ *      - EINVAL  - Invalid argument (handle it's set to a null pointer)
+ *      - read(2) command errors
+ *      - read_header error
+ * */
+int hg_fetch_annotate_entry(hg_linestream_buffer *lbuf, 
+						hg_annotate_entry *aentry);
+
 #endif