[PATCH 18 of 55 RFC c-hglib:level1] hg_commit: creating a high level function for mercurial commit command

Iulian Stana julian.stana at gmail.com
Fri Sep 13 19:35:30 CDT 2013


# HG changeset patch
# User Iulian Stana <julian.stana at gmail.com>
# Date 1379111655 -10800
#      Sat Sep 14 01:34:15 2013 +0300
# Node ID 1c5cade9911e847792db151f4adb35fbb9e0daa2
# Parent  0eee111297a5551f532f261612d6c96b810cedef
hg_commit: creating a high level function for mercurial commit command

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -670,6 +670,39 @@
 	return cbuf;
 }
 
+/* The high level copy command for hglib API. */
+int hg_commit(hg_handle *handle, int(*callback)(const char *msg, size_t len),
+							char *argument[])
+{
+	int exitcode, commit_msg = 0, i;
+
+	if(argument == NULL)
+		return -1;
+
+	for(i = 0; argument[i] != NULL; ++i){
+		if(commit_message(argument[i]) && argument[i + 1] != NULL)
+			commit_msg = 1;
+	}
+
+	if(!commit_msg){
+		char *err_msg = "must provide at least a message or a logfile";
+		callback(err_msg, strlen(err_msg));
+		return -1;
+	}
+
+	char **command = cmdbuilder("commit", argument, "--debug", NULL);
+
+
+	if(hg_rawcommand(handle, command) < 0){
+		return -1;
+	}
+	free(command);
+
+	exitcode = hg_runcommand(handle, callback, NULL);
+
+	return exitcode;
+}
+
 /* 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
@@ -798,6 +798,43 @@
  * */
 hg_linestream_buffer *hg_cat(hg_handle *handle, int (*callback)
 			(const char *msg, size_t len), char *argument[]);
+
+/**
+ * \brief hg_commit command for hglib API.
+ *
+ * Commit changes to the given files into the repository. Unlike a centralized
+ * SCM, this operation is a local operation. See hg push for a way to actively
+ * distribute your changes.
+ *
+ * If a list of files is omitted, all changes reported by hg status will be
+ * committed.
+ *
+ * Options/Argument list option:
+ *
+ *	-A, --addremove	mark new/missing files as added/removed before 
+ *				committing
+ *	--close-branch	mark a branch as closed, hiding it from the branch list
+ *	--amend		amend the parent of the working dir
+ *	-s, --secret	use the secret phase for committing
+ *	-I, --include	include names matching the given patterns
+ *	-X, --exclude	exclude names matching the given patterns
+ *	-m, --message	use text as commit message
+ *	-l, --logfile	read commit message from file
+ *	-d, --date		record the specified date as commit date
+ *	-u, --user		record the specified user as committer
+ *	-S, --subrepos	recurse into subrepositories
+ *
+ * \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 exitcode  To indicate the end of commit_command.
+ *
+ * errno can be:
+ *      - hg_rawcommand errors
+ * */
+int hg_commit(hg_handle *handle, int (*callback)(const char *msg, size_t len),
+							char *argument[]);
 	
 /**
  * \brief The yield mechanism that will get the next entry.
diff --git a/utils.c b/utils.c
--- a/utils.c
+++ b/utils.c
@@ -73,6 +73,21 @@
 	return new_cmd;
 }
 
+/*
+ * This function test if the string is a commit option.
+ * */
+int commit_message(char *string)
+{
+	char *message_option[] = {"-m", "-l", "--message", "--logfile"};
+	int size = 4, i;
+	for(i = 0; i < size; ++i){
+		if(strcmp(string, message_option[i]) == 0)
+			return 1;
+	}
+	return 0;
+}
+
+
 /* 
  * Adding to the destination pointer the source pointer. 
  * */
diff --git a/utils.h b/utils.h
--- a/utils.h
+++ b/utils.h
@@ -39,6 +39,21 @@
 char **cmdbuilder(char *command, char *options[], ...);
 
 /**
+ * \brief This function test if the string is a commit option.
+ *
+ * This function test if the sting is a option that will provide a message
+ * for the commit action. Is one of the following option: "-m", "-l", 
+ * "--message", "--logfile".
+ * 
+ * Used by commit command.
+ *
+ * \param string The option that will be test.
+ * \retval 0 If the string is not a option to provide commit message.
+ * \retval 1 If the string is a option to provide commit message.
+ * */
+int commit_message(char *string);
+
+/**
  * \brief Append a pointer to a destination pointer.
  *
  * Create space and concatenate the dest pointer with source pointer.


More information about the Mercurial-devel mailing list