[PATCH 2 of 5 V3 c-hglib] examples: merge command example, implemented using level 0

Giovanni Gherdovich g.gherdovich at gmail.com
Fri Feb 7 16:25:58 CST 2014


# HG changeset patch
# User Iulian Stana and Giovanni Gherdovich <g.gherdovich at gmail.com>
# Date 1380288770 -10800
#      Fri Sep 27 16:32:50 2013 +0300
# Node ID eb78cae73f00e1bd60011ef34a354ec9b8e4a5fa
# Parent  50d143796f41c2478a844dccd84e679a544a0371
examples: merge command example, implemented using level 0

This is a illustative implementation for merge command. The implementation is
using just level0 functions.

diff -r 50d143796f41 -r eb78cae73f00 README
--- a/README	Fri Sep 27 15:56:01 2013 +0300
+++ b/README	Fri Sep 27 16:32:50 2013 +0300
@@ -134,6 +134,42 @@
   > 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 examples
+
+This action will create an executable file named merge_level0.
+
+If c-hglib has not been installed system-wide via `make install`, the
+environment variable LD_LIBRARY_PATH has to be augmented with the path of
+libhg.so, which is likely to be the path to c-hglib sources (it's the case if
+only `make build` has been run).
+
+  e.g: LD_LIBRARY_PATH=/path/to/c-hglib:$LD_LIBRARY_PATH \
+       ./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
+  > touch bar ; hg add bar ; hg commit -m bar
+  > hg up 0
+  > touch baz ; hg add baz ; hg commit -m baz
+
+  `Repository that creates two heads with conflicts`
+  > hg init tmp
+  > cd tmp
+  > touch foo ; hg add foo ; hg commit -m foo
+  > echo bar > foo ; hg commit -m 'bar text'
+  > hg up 0
+  > echo baz > foo ; hg commit -m 'baz text'
 
 Contact
 """"""""
diff -r 50d143796f41 -r eb78cae73f00 examples/merge.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/merge.c	Fri Sep 27 16:32:50 2013 +0300
@@ -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 enum 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