[PATCH 13 of 55 RFC c-hglib:level1] hg_bookmarks: creating a high level function for mercurial bookmarks command
Iulian Stana
julian.stana at gmail.com
Fri Sep 13 19:35:25 CDT 2013
# HG changeset patch
# User Iulian Stana <julian.stana at gmail.com>
# Date 1379111036 -10800
# Sat Sep 14 01:23:56 2013 +0300
# Node ID feee1c5f54cb765436019adcc09f678946d8214b
# Parent 6036c2b6cf10a8d09e681b7f6a2f0052ee85384e
hg_bookmarks: creating a high level function for mercurial bookmarks command
diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -551,6 +551,26 @@
return exitcode;
}
+/* The high level bookmarks command for hglib API. */
+hg_linestream_buffer *hg_bookmarks(hg_handle *handle, int (*callback)
+ (const char *msg, size_t len))
+{
+ hg_linestream_buffer *bbuf = malloc(sizeof(hg_csetstream_buffer));
+ bbuf->handle = handle;
+
+ bbuf->command = cmdbuilder("bookmarks", NULL, 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)
@@ -650,3 +670,38 @@
return return_value;
}
+
+/* The lbuf next step. Getting the next changeset. */
+int hg_fetch_bookmarks_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;
+
+ if(strcmp(buff, "no bookmarks set") == 0)
+ return 0;
+
+ char *aux = buff;
+ int back_count = 0;
+ rentry->name = buff + 3;
+
+ /* 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';
+
+ return return_value;
+}
diff --git a/client.h b/client.h
--- a/client.h
+++ b/client.h
@@ -210,6 +210,37 @@
}hg_cset_entry;
/**
+ * \struct hg_rev_entry
+ * \brief This structure will contain revision information about a file.
+ * (rev:node name)
+ *
+ * \var hg_rev_entry::rev
+ * The rev field will stock the revision number.
+ * \var hg_rev_entry::node
+ * The node field will stock the node hash.
+ * \var hg_rev_entry::name
+ * The name field will stock a content or a name(the name can be a bookmark
+ * name, a branch name or tag name).
+ *
+ * \typedef hg_rev_entry
+ * \brief This structure will contain revision information about a file.
+ * (rev:node name)
+ *
+ * \param rev
+ * The rev field will stock the revision number.
+ * \param node
+ * The node field will stock the node hash.
+ * \param name
+ * The name field will stock a content or a name(the name can be a bookmark
+ * name, a branch name or tag name).
+ * */
+typedef struct hg_rev_entry{
+ char *rev;
+ char *node;
+ char *name;
+}hg_rev_entry;
+
+/**
* \brief Reading the header from cmdsrv.
*
* The function will read the header from the command server and will save it to
@@ -594,6 +625,32 @@
char *argument[]);
/**
+ * \brief hg_bookmarks command for hglib API.
+ *
+ * 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.
+ * \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_bookmarks(hg_handle *handle, int (*callback)
+ (const char *msg, size_t len));
+
+/**
* \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()
@@ -687,4 +744,25 @@
int hg_fetch_annotate_entry(hg_linestream_buffer *lbuf,
hg_annotate_entry *aentry);
+/**
+ * \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_bookmarks_entry(hg_linestream_buffer *lbuf, hg_rev_entry *rentry);
+
+
#endif
More information about the Mercurial-devel
mailing list