Patchwork [36,of,55,RFC,c-hglib:level1] hg_paths: creating a high level function for mercurial paths command

login
register
mail settings
Submitter Iulian Stana
Date Sept. 14, 2013, 12:35 a.m.
Message ID <0b729e5b81951203e125.1379118948@doppler>
Download mbox | patch
Permalink /patch/2481/
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 1379115078 -10800
#      Sat Sep 14 02:31:18 2013 +0300
# Node ID 0b729e5b81951203e1251c4eb65df8be84bb0198
# Parent  a6f8421b57f2d0c21942612b1b1a4d5bce1ea227
hg_paths: creating a high level function for mercurial paths command

Patch

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -1032,6 +1032,66 @@ 
 	return cbuf;
 }
 
+/* The high level paths command for hglib API. */
+hg_path_entry *hg_paths(hg_handle *handle, int(*callback)(const char *msg, 
+				size_t len), char *path_name, int *path_size)
+{
+	int exitcode, i;
+
+	char **command = cmdbuilder("paths", NULL, path_name, NULL);
+
+	if(hg_rawcommand(handle, command) < 0){
+		return NULL;
+	}
+	free(command);
+
+	exitcode = hg_runcommand(handle, callback, NULL);
+
+	if(exitcode){
+		*path_size = -1;
+		return NULL;
+	}
+
+	char *out = get_output_data(handle);
+
+	/* it out is NULL, it means not path is set. */
+	if(out == NULL){
+		*path_size = 0;
+		return NULL;
+	}
+
+	*path_size = number_of_lines(out);
+	char *origin = out;
+	/* if path_name is set, return the url for the path_name*/
+	if(path_name){
+		hg_path_entry *path = malloc(sizeof(hg_path_entry));
+		path[0].pathname = path_name;
+		path[0].url = out;
+
+		out = strstr(out, "\n") + 1;
+		origin[out - origin - 1] = '\0';
+		return path;
+	}
+
+	/* else return a list with all set pats. */
+	hg_path_entry *path = malloc(sizeof(hg_path_entry) * *path_size);
+
+	for(i = 0; i < *path_size; ++i){
+		/* set pathname */
+		path[i].pathname = out;
+		/* skip to '=' char. */
+		out = strstr(out, "=") + 2;
+		/* mark the position */
+		origin[out - origin - 3] = '\0';
+		path[i].url = out;
+
+		out = strstr(out, "\n") + 1;
+		origin[out - origin - 1] = '\0';
+	}
+
+	return path;
+}
+
 /* 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
@@ -242,6 +242,29 @@ 
 }hg_manifest_entry;
 
 /**
+ * \struct hg_path_entry
+ * \brief This structure will contain information about a path.(pathname, url) 
+ *
+ * \var hg_path_entry::pathname
+ * The pathname field will stock the pathname.
+ * \var hg_parth_entry::url
+ * The url field will stock the a url for the repository.
+ *
+ * \typedef hg_path_entry
+ * \brief This structure will contain information about a path.(pathname, url) 
+ *
+ * \param pathname
+ * The pathname field will stock the pathname.
+ * \param url
+ * The url field will stock the a url for the repository.
+ *
+ * */
+typedef struct hg_path_entry{
+	char *pathname;
+	char *url;
+}hg_path_entry;
+
+/**
  * \struct hg_rev_entry
  * \brief This structure will contain revision information about a file.
  * (rev:node name) 
@@ -1574,6 +1597,35 @@ 
 			(const char *msg, size_t len), char *argument[]);						
 
 /**
+ * \brief hg_paths command for hglib API.
+ *
+ * Show definition of symbolic path name NAME. If no name is given, show
+ * definition of all available names.
+ *
+ * Option -q/--quiet suppresses all output when searching for NAME and shows
+ * only the path names when listing all definitions.
+ *
+ * Path names are defined in the [paths] section of your configuration file and
+ * in /etc/mercurial/hgrc. If run inside a repository, .hg/hgrc is used, too.
+ *
+ * \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 path_name Name of symbolic path I want to see.
+ * \param phase_size Address where the array size will be set.
+ * \retval hg_path_entry A list this with all repository paths or the path for
+ * the specific path_name value.
+ * \retval NULL-path_size=0 Succesful, but no path is set.
+ * \retval NULL-path_size=-1 Error, with error set appropriate.
+ *
+ * errno can be:
+ *      - hg_rawcommand errors
+ *
+ * */
+hg_path_entry *hg_paths(hg_handle *handle, int(*callback)(const char *msg, 
+				size_t len), char *path_name, int *path_size);
+
+/**
  * \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()
diff --git a/utils.c b/utils.c
--- a/utils.c
+++ b/utils.c
@@ -149,3 +149,14 @@ 
 
 	return 0;
 }
+
+/*
+ * Count the number of line in the out pointer.
+ * */
+int number_of_lines(char *out)
+{
+	int number = 0;
+	while(out = strstr(out + 1, "\n"), out != NULL)
+		number ++;
+	return number;
+}
diff --git a/utils.h b/utils.h
--- a/utils.h
+++ b/utils.h
@@ -121,5 +121,12 @@ 
  * */
 int detect_endline_byte(char *buffer, int buf_size, int data_on_pipe);
 
+/**
+ * \brief Count the number of line in the out pointer.
+ *
+ * \param out The data pointer.
+ * \retval number The number of lines.
+ * */
+int number_of_lines(char *out);
 
 #endif