Patchwork [15,of,55,RFC,c-hglib:level1] hg_branches: creating a high level function for mercurial branches command

login
register
mail settings
Submitter Iulian Stana
Date Sept. 14, 2013, 12:35 a.m.
Message ID <2c9552a85a2610d09b92.1379118927@doppler>
Download mbox | patch
Permalink /patch/2459/
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 1379111322 -10800
#      Sat Sep 14 01:28:42 2013 +0300
# Node ID 2c9552a85a2610d09b92328405e471e73a018a12
# Parent  d324f35caf029c549fc952656f44d3fbe31d44e0
hg_branches: creating a high level function for mercurial branches command

Patch

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -613,6 +613,26 @@ 
 	return out;
 }
 
+/* The high level branches command for hglib API. */
+hg_linestream_buffer *hg_branches(hg_handle *handle, int (*callback)
+			(const char *msg, size_t len), char *argument[])
+{
+	hg_linestream_buffer *bbuf = malloc(sizeof(hg_csetstream_buffer));
+	bbuf->handle = handle;
+
+	bbuf->command = cmdbuilder("branches", argument, NULL);
+
+	if(hg_rawcommand(handle, bbuf->command) < 0){
+		return NULL;
+	}
+
+	bbuf->callback = callback;
+	bbuf->buffer = NULL;
+	bbuf->buf_size = 0;
+
+	return bbuf;
+}
+
 /* 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)
@@ -747,3 +767,45 @@ 
 
 	return return_value;
 }
+
+/* The lbuf next step. Getting the next changeset. */
+int hg_fetch_branches_entry(hg_linestream_buffer *lbuf, hg_rev_entry *rentry)
+{
+	char *buff = NULL;
+	int return_value = hg_fetch_line_entry(lbuf, &buff);
+
+	if(return_value <= 0)
+		return return_value;
+
+	char *aux = buff;
+	int back_count = 0;
+	rentry->name = buff;
+
+	/* skip to node position and mark it*/
+	aux = strstr(aux, ":") + 1;
+	buff[aux - buff - 1] = '\0';
+	
+	rentry->node = aux;
+
+	/* go back to get the revision */
+	while(buff[aux - buff - 1 - back_count++] != ' ');
+
+	rentry->rev = aux - back_count + 1;
+
+	/* go back to mark the end of name */
+	while(buff[aux - buff - 1 - back_count++] == ' ');
+
+	buff[aux - buff - back_count + 1] = '\0';
+
+	/* go forward to erase "(inactive)". */
+	back_count = 0;
+	while(buff[aux - buff + back_count] != '\0'){ 
+		if(buff[aux - buff + back_count] == ' ')
+			break;
+		back_count ++;
+	}
+
+	buff[aux - buff + back_count] = '\0';
+
+	return return_value;
+}
diff --git a/client.h b/client.h
--- a/client.h
+++ b/client.h
@@ -681,6 +681,45 @@ 
  * */
 char *hg_branch(hg_handle *handle, int (*callback)(const char *msg, size_t len),
 							char *argument[]);
+
+/**
+ * \brief hg_branches command for hglib API.
+ * 
+ * List the repository's named branches, indicating which ones are inactive. If
+ * -c/--closed is specified, also list branches which have been marked closed
+ * (see hg commit --close-branch).
+ *
+ * If -a/--active is specified, only show active branches. A branch is
+ * considered active if it contains repository heads.
+ *
+ * Options/Argument list option:
+ *
+ *	-a, --active	show only branches that have unmerged heads
+ *	-c, --closed	show normal and closed branches
+ *
+ * Return a yield-like mechanism to get bookmarks from cmd-server.
+ *
+ * To get bookmarks information use the returned hg_linestream_buffer structure 
+ * with hg_fetch_line_entry() or hg_fetch_bookmarks_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_bookmarks_entry() function you will get 
+ *  the data parse in hg_rev_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_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_branches(hg_handle *handle, int (*callback)
+			(const char *msg, size_t len), char *argument[]);
 		
 /**
  * \brief The yield mechanism that will get the next entry.
@@ -796,5 +835,24 @@ 
  * */
 int hg_fetch_bookmarks_entry(hg_linestream_buffer *lbuf, hg_rev_entry *rentry);
 
+/**
+ * \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 rentry 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_branches_entry(hg_linestream_buffer *lbuf, hg_rev_entry *rentry);
 
 #endif