[PATCH 2 of 5 c-hglib] examples: merge command example, implemented using level 0
Iulian Stana
julian.stana at gmail.com
Mon Dec 2 13:10:12 CST 2013
# HG changeset patch
# User Iulian Stana <julian.stana at gmail.com>
# Date 1380288770 -10800
# Node ID d18cd4d134b3664eb6389b858bdc76bd9cec7a4e
# Parent 4770f8c809b5b2b2ba23f0a49bc1213310b69e82
examples: merge command example, implemented using level 0
This is a illustative implementation for merge command. The implementation is
using just level0 functions.
diff --git a/README b/README
--- a/README
+++ b/README
@@ -126,6 +126,39 @@
> touch voodoo ; hg add voodoo ; hg commit -m voodoo
> echo voodoo > voodoo ; hg commit -m 'voodoo text'
+* Merge example:
+
+The merge example will use the level 0 implementation and will merge the heads
+for a given repository.
+To compile the binary file you can use the make tool with "examples" target.
+ > make example
+
+This action will create an executable file named merge_level0.
+
+To run this executable, the first argument must be a path to the repository
+where you want to import the patch, second argument.
+ e.g: ./merge_level0 repository_path
+
+To run this example, you can use an existing mercurial repository,
+or create a throw-away one just for the example. Here is how
+you can create an ad-hoc repository
+ e.g:
+ `Repository that creates two heads without conflicts`
+ > hg init tmp
+ > cd tmp
+ > touch foo ; hg add foo ; hg commit -m foo
+ > echo baloo > foo ; hg commit -m 'baloo text'
+ > hg up 0
+ > touch boo ; hg add boo ; hg commit -m boo
+ > echo voodoo > boo ; hg commit -m 'voodoo text'
+
+ `Repository that creates two heads with conflicts`
+ > hg init tmp
+ > cd tmp
+ > touch foo ; hg add foo ; hg commit -m foo
+ > echo baloo > foo ; hg commit -m 'baloo text'
+ > hg up 0
+ > echo voodoo > foo ; hg commit -m 'voodoo text'
Contact
""""""""
diff --git a/examples/merge.c b/examples/merge.c
new file mode 100644
--- /dev/null
+++ b/examples/merge.c
@@ -0,0 +1,93 @@
+/* For more details please check the README file from the root directory.*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "client.h"
+#include "utils.h"
+
+#define BUFF_SIZE 4096
+
+enum prompt_callback_state{
+ scan_input = 0,
+ terminate = 1
+};
+
+char *prompt_function(char *output, int size)
+{
+ static prompt_callback_state state = scan_input;
+ char *option;
+ if (state == scan_input) {
+ state = terminate;
+ option = malloc(sizeof(char));
+ scanf("%s", option);
+ return option;
+ }
+ state = scan_input;
+ return "";
+}
+
+/**
+ * \brief The merge command example.
+ *
+ * The merge function gets a prompt function that deals prompts.
+ *
+ * The prompt function will read the option from standard input. The purpose
+ * is to show the mechanism(it's not a level 0 issue)
+ *
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \param prompt a pointer to a function.
+ * \retval exitcode
+ * */
+int hg_merge_by_hand(hg_handle *handle, char *(*prompt)(char *buff, int size))
+{
+ char *comm[] = {"merge", "--tool=internal:prompt", NULL};
+ char buff[BUFF_SIZE];
+ char *prompt_msg;
+ int exitcode = 0;
+ int ns;
+
+ hg_rawcommand(handle, comm);
+
+ hg_header *head;
+ while (head = hg_read_header(handle), head != NULL &&
+ head->channel != r) {
+ if (head->channel == o) {
+ if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+ printf("%s", buff);
+ }
+ } else if (head->channel == e) {
+ if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+ printf("error data: %s", buff);
+ }
+ } else if (head->channel == L || head->channel == I) {
+ prompt_msg = prompt(buff, strlen(buff));
+ hg_rawwrite(handle, prompt_msg, strlen(prompt_msg));
+ }
+ }
+
+ exitcode = hg_exitcode(handle);
+ printf("exitcode = %d\n", exitcode);
+
+ return exitcode;
+}
+
+/**
+ * \brief The main function
+ * */
+int main(int argc, char **argv)
+{
+ hg_handle *handle;
+ int exitcode;
+
+ if (argc != 2) {
+ printf("Usage: %s repository_path\n", argv[0]);
+ return 1;
+ }
+
+ handle = hg_open(argv[1], NULL);
+ exitcode = hg_merge_by_hand(handle, prompt_function);
+ hg_close(&handle);
+
+ return exitcode;
+}
More information about the Mercurial-devel
mailing list