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

Giovanni Gherdovich g.gherdovich at gmail.com
Sat Dec 7 05:52:14 CST 2013


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;
:::: +}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20131207/264e5d4c/attachment.html>


More information about the Mercurial-devel mailing list