Patchwork [14,of,55,RFC,c-hglib:level1] hg_branch: creating a high level function for mercurial branch command

login
register
mail settings
Submitter Iulian Stana
Date Sept. 14, 2013, 12:35 a.m.
Message ID <d324f35caf029c549fc9.1379118926@doppler>
Download mbox | patch
Permalink /patch/2457/
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 1379111198 -10800
#      Sat Sep 14 01:26:38 2013 +0300
# Node ID d324f35caf029c549fc952656f44d3fbe31d44e0
# Parent  feee1c5f54cb765436019adcc09f678946d8214b
hg_branch: creating a high level function for mercurial branch command

Patch

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -571,6 +571,48 @@ 
 	return bbuf;
 }
 
+/* The high level branch command for hglib API. */
+char *hg_branch(hg_handle *handle, int(*callback)(const char *msg, size_t len),
+							char *argument[])
+{
+	int exitcode;
+	int i;
+	int name_set = 0;
+	char **command = cmdbuilder("branch", argument, NULL);
+
+	if(hg_rawcommand(handle, command) < 0){
+		return NULL;
+	}
+	free(command);
+
+	exitcode = hg_runcommand(handle, callback, NULL);
+	if(exitcode)
+		return NULL;
+	char *out = get_output_data(handle);
+	char *position;
+
+	if(argument != NULL)
+	for(i = 0; argument[i] != NULL; ++i){
+		/* len('reset working directory to branch ') == 34*/
+		if(strcmp(argument[i], "--clean") == 0 || 
+				strcmp(argument[i], "-C") == 0)
+			return out + 34;
+		/* I must know if a name is set in this call*/
+		if(argument[i][0] != '-')
+			name_set = 1;
+	}
+
+	/* retrun the set name
+	 * len('marked working directory as branch ') == 35*/
+	if(name_set){
+		position = strstr(out, "\n");
+		out[position - out] = '\0';
+		return out + 35;
+	}
+
+	return out;
+}
+
 /* 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
@@ -649,7 +649,39 @@ 
  * */
 hg_linestream_buffer *hg_bookmarks(hg_handle *handle, int (*callback)
 					(const char *msg, size_t len));
-					
+
+/**
+ * \brief hg_branch command for hglib API.
+ *
+ * With no argument, show the current branch name. With one argument, set the
+ * working directory branch name (the branch will not exist in the repository
+ * until the next commit). Standard practice recommends that primary development
+ * take place on the 'default' branch.
+ *
+ * Unless -f/--force is specified, branch will not let you set a branch name
+ * that already exists, even if it's inactive.
+ *
+ * Use -C/--clean to reset the working directory branch to that of the parent of
+ * the working directory, negating a previous branch change.
+ *
+ * Options/Argument list option:
+ *
+ *	-f, --force	set branch name even if it shadows an existing branch
+ *	-C, --clean	reset branch name to parent branch name
+ *
+ * \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 branch_name  Will return the branch name.
+ * \retval NULL To indicate an error.
+ *
+ * errno can be:
+ *      - hg_rawcommand errors
+ * */
+char *hg_branch(hg_handle *handle, int (*callback)(const char *msg, size_t len),
+							char *argument[]);
+		
 /**
  * \brief The yield mechanism that will get the next entry.
  *