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

login
register
mail settings
Submitter Iulian Stana
Date Dec. 6, 2013, 7:08 p.m.
Message ID <e5852ac5e274541d312d.1386356892@doppler>
Download mbox | patch
Permalink /patch/3207/
State Superseded
Headers show

Comments

Iulian Stana - Dec. 6, 2013, 7:08 p.m.
# HG changeset patch
# User Iulian Stana <julian.stana@gmail.com>
# Date 1380288770 -10800
# Node ID e5852ac5e274541d312dc80e7da7db6828ec6fc0
# 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.
Iulian Stana - Dec. 6, 2013, 7:09 p.m.
Sorry I forgot the enum ...


2013/12/6 <julian.stana@gmail.com>

> # HG changeset patch
> # User Iulian Stana <julian.stana@gmail.com>
> # Date 1380288770 -10800
> # Node ID e5852ac5e274541d312dc80e7da7db6828ec6fc0
> # 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 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;
> +}
>
Dave S - Dec. 6, 2013, 8:58 p.m.
Copying to the list

/dps

---------- Forwarded message ----------
From: Dave S <snidely.too@gmail.com>
Date: Fri, Dec 6, 2013 at 12:17 PM
Subject: Re: [PATCH 2 of 5 c-hglib V2] examples: merge command example,
implemented using level 0
To: julian.stana@gmail.com


On Fri, Dec 6, 2013 at 11:08 AM,  <julian.stana@gmail.com> wrote:

> # HG changeset patch
> # User Iulian Stana <julian.stana@gmail.com>
> # Date 1380288770 -10800
> # Node ID e5852ac5e274541d312dc80e7da7db6828ec6fc0
> # 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
> +
>

In each of the posts of the V1 series and again in this V2 post, you write
"where you want to import the patch",
but only one of the posts is an example of hg import.

Is this a block-copy error, or am I misunderstanding what you mean by
"patch" here?

/dps
Giovanni Gherdovich - Dec. 7, 2013, 11:52 a.m.
thanks Dave,

::::
:::: Is this a block-copy error, or am I misunderstanding what you mean by
"patch" here?

the former.

I prepared this series together with Iulian, actually this whole bunch of
mis-pasting sneaked into it.

Iulian:
I suggest amending as follow. Can you resend?

:::: 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 be replaced by

"To run this executable, give as argument the path of a repository wich has
two heads.

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, wich is likely to be the path to c-hglib sources
(it's the case if only `make install` 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
:::: +  > 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'

the above example can be simplified into

> 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

with the added benefit of being conformant to RFC 3092,
http://www.ietf.org/rfc/rfc3092.txt
"boo", "baloo" and "voodoo" are non-standard variable names,
and should be avoided when possible.

:::: +
:::: +  `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;
:::: +}

Patch

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 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;
+}