make git-pull a builtin
The git-pull command is currently implemented by the shell script git-pull.sh. However, git-pull.sh suffers from some problems:
* Inconsistent command line interface: since git-pull.sh does not use git's internal parse-options API, it's command line interface is inconsistent with other git builtins. For example, the option --st should throw an ambigious option error as it could mean --stat or --strategy, but git-pull.sh is hardcoded to assume --st resolves to --strategy. It also does not list the command line options in its usage help.
* Inefficient: shell scripts can only access git functionality through spawning git processes, which has a relatively high overhead on certain operating systems such as Windows. Furthermore, the repeated starting up of git means that commonly-used files such config files, the index file and often accessed objects/packfiles are repeatedly read, which can cause poor performance on filesystems or operating systems with slow IO operations.
* Makes it difficult to port git: git-pull.sh requires a posix conformant shell and utilities such as sed and tr. This makes it harder to port git to non-posix environments.
By making git-pull a builtin, it has access to git's internal API and thus solves the above problems:
* Usage of parse-options API means greater consistency of the command-line interface with other git commands.
* No spawning of external git processes: internal API and builtins are called directly. This also means that they use the same underlying object/config cache, which reduces needless IO/parsing operations.
* Low-level control over code execution and thus greater efficiency. For example commit SHA1s now only need to be parsed from a string once (when resolving branch refs), while as a shell script they would have to be stored as strings and parsed over and over again.
Furthermore, this reduces git's dependence on posix shells and utilities, which would be very helpful in porting git to non-posix environments such as Windows.
---
Hi, I would like to share this very rough prototype with everyone. All the relevant tests (t5520-t5572) pass.
I did a quick benchmark on my Linux and Windows systems, which have nearly the same hardware, by running 'git pull' 100 times on a repo and its clone with 1 commit. On Linux, there is negligible performance difference, but on Windows the runtime fell from 8m 25s to 1m 3s. The timings should not be affected by any msys2 emulation as the git processes were spawned from a native executable. I started this as a just-for-fun exercise to learn about the git internal API, but seeing as the results are very encouraging, I'm tempted to take on this project for GSoC.
Of course, it would be easy to make any shell script a builtin by using the run_command* functions, but I feel that that would not take advantage of the potential benefits in using git's internal API directly. So, I set myself a few requirements for the ideal end result:
* zero spawning of processes so that the internal object, index and config cache can be taken advantage of. Some operating systems (like Windows) also have a relatively large overhead when spawning processes[1].
* avoid needless parsing/string conversions by directly accessing the C data structures. Massaging of strings cause too much code churn, so functions that take the richer C data structures as input should be preferred over functions that work by parsing string arguments (e.g. prefer using the run_diff_files() function instead of calling cmd_diff_files or spawning a git diff-files process)
* use the internal API as much as possible: share code between the builtins (e.g. builtin fmt-merge-msg, exposed in fmt-merge-msg.h) in order to reduce code complexity. For example, the fork_point() code in this patch should be shared with the merge-base builtin.
[1] http://stackoverflow.com/questions/10710912
However, there were some problems encountered when trying to fulfill the requirements above:
* Many builtins only provide their main entry point to access their functionality -- this means that argument options are parsed, put back together, and then re-parsed again, which is quite inefficient and can create a lot of code complexity. A better solution would be to expose the functionality of these builtins as a separate function without argument parsing, similar to how fmt_merge_msg is implemented.
* Some buitins do not expect to be called by their cmd_* functions through code paths other than git.c. For example, fetch.c actually frees the strings it pulls from argv. This patch works around these functions by not attempting to free memory passed to these functions, but a more comprehensive solution would be to investigate and fix these functions. (Or expose their functionality in as a separate function)
* Finally, there is the possibility that in the process of conversion bugs or incompatible behavioral changes may be introduced which are not caught by the test suite. Ideally, I think the solution is to improve the test suite and make it as comprehensive as possible, but writing a comprehensive test suite may be too time consuming. A simpler, but less perfect strategy might be to just convert the shell scripts directly statement by statement to C, using the run_command*() functions as Duy Nguyen[2] suggested, before changing the code to use the internal API.
[2] http://thread.gmane.org/gmane.comp.version-control.git/265575/focus=265602
This patch is the result of trying to fulfill the above requirements, and temporarily working around the above problems until I can find their source/come up with a patch. (There may also be some bugs in the git-pull.sh code related to not taking into account the fact that "$@" can contain arguments for git-fetch. However, I have not found a way to trigger them yet in practice.) I would like some feedback on whether the above requirements are sane, and whether there may be disadvantages that I'm missing out on. As stated above, there are big performance wins for Windows, but I think the biggest win of all is to reduce git's dependence on a scripting language, improving portability and reducing code duplication between the languages, thus making git a better universal cross-platform solution for version control :)
Regards,
Paul
Makefile | 2 +- builtin.h | 1 + builtin/pull.c | 938 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ git.c | 1 + 4 files changed, 941 insertions(+), 1 deletion(-) create mode 100644 builtin/pull.c
Makefile +1 -1
@@ -470,7 +470,6 @@ SCRIPT_SH += git-merge-octopus.sh SCRIPT_SH += git-merge-one-file.sh SCRIPT_SH += git-merge-resolve.sh SCRIPT_SH += git-mergetool.sh -SCRIPT_SH += git-pull.sh SCRIPT_SH += git-quiltimport.sh SCRIPT_SH += git-rebase.sh SCRIPT_SH += git-remote-testgit.sh
@@ -872,6 +871,7 @@ BUILTIN_OBJS += builtin/pack-refs.o BUILTIN_OBJS += builtin/patch-id.o BUILTIN_OBJS += builtin/prune-packed.o BUILTIN_OBJS += builtin/prune.o +BUILTIN_OBJS += builtin/pull.o BUILTIN_OBJS += builtin/push.o BUILTIN_OBJS += builtin/read-tree.o BUILTIN_OBJS += builtin/receive-pack.o
builtin.h +1 -0
@@ -98,6 +98,7 @@ extern int cmd_pack_redundant(int argc, const char **argv, const char *prefix);extern int cmd_patch_id (int argc,const char **argv,const char *prefix);extern int cmd_prune (int argc,const char **argv,const char *prefix);extern int cmd_prune_packed (int argc,const char **argv,const char *prefix); +extern int cmd_pull (int argc,const char **argv,const char *prefix);extern int cmd_push (int argc,const char **argv,const char *prefix);extern int cmd_read_tree (int argc,const char **argv,const char *prefix);extern int cmd_receive_pack (int argc,const char **argv,const char *prefix);
builtin/pull.c new file +938 -0
938 lines not on this page · raw message
git.c +1 -0
@@ -443,6 +443,7 @@ static struct cmd_struct commands[] = { { "pickaxe", cmd_blame, RUN_SETUP }, { "prune", cmd_prune, RUN_SETUP }, { "prune-packed", cmd_prune_packed, RUN_SETUP }, + { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE}, { "push", cmd_push, RUN_SETUP }, { "read-tree", cmd_read_tree, RUN_SETUP }, { "receive-pack", cmd_receive_pack },
Signature · 1 line
2.1.4
Quoted 3 lines
Paul Tan writes:
I would like to share this very rough prototype with everyone.
...
Quoted 1 line
I started this as a just-for-fun exercise to learn about the git internal
API
I started to rewrite git-pull for similar reasons a couple of months ago, but I haven't had time to complete it. It looks like my version has less work remaining than yours, would you like me to share it?
Quoted 1 line
Finally, there is the possibility that in the process of conversion bugs
or incompatible behavioral changes may be introduced which are not caught by the test suite. Ideally, I think the solution is to improve the test suite and make it as comprehensive as possible, but writing a comprehensive test suite may be too time consuming.
This is why I haven't had time to finish and submit my patch. While the c code is pretty much complete, I felt the test suite needed some extension before I could be confident the new code is correct.
Hi Stephen,
Quoted 9 lines · Stephen Robin
Quoted 2 lines
Paul Tan writes:
I would like to share this very rough prototype with everyone....
Quoted 1 line
I started this as a just-for-fun exercise to learn about the git internal
API
I started to rewrite git-pull for similar reasons a couple of months ago, but I haven't had time to complete it. It looks like my version has less work remaining than yours, would you like me to share it?
Hmm. It always results in an unnecessary round trip if you ask people to ask you to share it.
At this point, because Paul already made his work public, I would say that we should continue with his version. Please understand this as an encouragement for the future to share your code pro-actively, just like Git's own source code has been shared with you without the need for you to request it explicitly.
For the record, Duy also already shared his C code to implement git pull in C publicly. I think that Paul decided to start again in order to understand every detail of the process of converting a shell script into a builtin; If that was the rationale, I would agree that it is a wise one.
Ciao,
Johannes
Hi Paul,
thank you for this very detailed mail. It was a real pleasure to read this well-researched document.
In the following, I will pick out only parts from the mail, in the interest of both of our time. Please assume that I agree with everything that I do not quote below (and even the with quoted parts I agree mostly ;-)).
Quoted 1 line · Paul Tan
on Windows the runtime fell from 8m 25s to 1m 3s.
This is *exactly* the type of benefit that makes this project so important! Nice one.
Quoted 4 lines
A simpler, but less perfect strategy might be to just convert the shell scripts directly statement by statement to C, using the run_command*() functions as Duy Nguyen[2] suggested, before changing the code to use the internal API.
Yeah, the idea is to have a straight-forward strategy to convert the scripts in as efficient manner as possible. It also makes reviewing easier if the first step is an almost one-to-one translation to run_command*()-based builtins.
Plus, it is rewarding to have concise steps that can be completed in a timely manner.
builtin/pull.c line 36
Quoted 3 lines
builtin/pull.c new file +3
-0+/* NOTE: git-pull.sh only supports --log and --no-log, as opposed to what +* man git-pull says. */ +static int opt_shortlog_len;
This comment might want to live in the commit message instead... It is prone to get stale in the code while it will always be correct (and easier to spot) in the commit message.
Quoted 12 lines
builtin/pull.c new file +12
-0+/** +* Returns default rebase option value +*/ +static int rebase_config_default (void ) +{ +struct strbuf name =STRBUF_INIT ; +const char *value =NULL ; +int boolval; + +strbuf_addf (&name,"branch.%s.rebase" , head_name_short); +if (git_config_get_value (name.buf, &value)) +git_config_get_value ("pull.rebase" , &value);
builtin/pull.c line 97
Quoted 10 lines
builtin/pull.c new file +10
-0+strbuf_release (&name); +if (!value) +return REBASE_FALSE ; + boolval =git_config_maybe_bool ("pull.rebase" , value); +if (boolval >=0 ) +return boolval ?REBASE_TRUE :REBASE_FALSE ; +else if (value && !strcmp (value,"preserve" )) +return REBASE_PRESERVE ; +die (_ ("invalid value for branch.%s.rebase \"%s\"" ), head_name_short, value); +}
Personally, I would use a couple of empty lines for enhanced structure. Conceptually, there are four parts: the variable declarations, querying the config, parsing the value, and die()ing in case of error. There is already an empty line between the first two parts, and I would imagine that readability would be improved further by having an empty line after the strbuf_release() and the return REBASE_PRESERVE statement, respectively.
builtin/pull.c line 106
Quoted 9 lines
+static int
parse_opt_rebase(const struct option *opt, const char *arg, int unset)builtin/pull.c new file +7
-0+{ +if (arg) + *(int *)opt->value =parse_rebase (arg); +else + *(int *)opt->value = unset ?REBASE_FALSE :REBASE_TRUE ; +return (*(int *)opt->value) >=0 ?0 :-1 ; +}
In this function (and also in other places below), there is this pattern that a struct option pointer is passed to the function, but then only *(int *)opt->value is written to. Therefore, I would suggest to change the signature of the function and pass (int *)opt->value as function parameter.
Quoted 1 line
+static int
has_unstaged_changes(void)
Yeah, this function, as well as the ones below it, look as if they are so common that they *should* be already somewhere in libgit.a. But I did not find them, either...
Of course it *would* be nice to identify places where essentially the same code is needed, and refactor accordingly. But I think that is outside the scope of this project.
The rest looks pretty good (and you realize that my comments above are really more nit picks than anything else).
The FIXMEs should be relatively easy to address. It would be my pleasure to work with you.
Ciao,
Johannes
Hi,
First of all, thanks a lot for working on this. I'm rather impressed to see a working proof of concept so soon! And impressed by the quality for a "first draft".
A few minor remaks below after a very quick look.
Quoted 3 lines · Paul Tan
Ideally, I think the solution is to
improve the test suite and make it as comprehensive as possible, but writing a comprehensive test suite may be too time consuming.
time-consuming, but also very beneficial since the code would end up being better tested. For sure, a rewrite is a good way to break stuff, but anything untested can also be broken by mistake rather easily at any time.
I'd suggest doing a bit of manual mutation testing: take your C code, comment-out a few lines of code, see if the tests still pass, and if they do, add a failing test that passes again once you uncomment the code.
Makefile SCRIPT_SH += git-merge-octopus.sh · line 473
Quoted 9 lines
Makefile +0
-1@@ -470,7 +470,6 @@ SCRIPT_SH += git-merge-octopus.sh SCRIPT_SH += git-merge-one-file.sh SCRIPT_SH += git-merge-resolve.sh SCRIPT_SH += git-mergetool.sh -SCRIPT_SH += git-pull.sh
When converting a script into a builtin, we usually move the old script to contrib/examples/.
Makefile SCRIPT_SH += git-merge-octopus.sh · line 474
Quoted 2 lines
Makefile +2
-0+static const char * const builtin_pull_usage[] = { + N_("git pull [-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff|--ff-only] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>..."),
I know we have many instances of very long lines for usage string, but it would be better IMHO to wrap it both in the code and in the output of "git pull -h".
Makefile SCRIPT_SH += git-merge-octopus.sh
Quoted 2 lines
Makefile +2
-0+/* NOTE: git-pull.sh only supports --log and --no-log, as opposed to what + * man git-pull says. */
We usually write multi-line comments
/*
* like
* this
*/
Quoted 1 line
+/* Global vars since they are used often */
Being use often does not count as an excuse for being global IMHO. Having global variables means you share the same instance in several functions, and you have to be careful with things like
void g()
{
glob = bar;
}void f()
{
glob = foo;
g();
bar = glob;
}As a reader, I'd rather not have to be careful about this to keep my neurons free for other things.
Quoted 1 line
+static char
*head_name;
Actually, this one is used only in one function, so "often" is not even true ;-).
Quoted 1 line
+static struct option builtin_pull_options[] = {
You may also declare this as local in cmd_pull().
Quoted 2 lines
+/**
+ * Returns remote for branch
Here and elsewhere: use imperative (return, not return_s_). The comment asks the function to return a value.
Makefile
Quoted 3 lines
Makefile +3
-0+ strbuf_addf(&key, "branch.%s.remote", branch); + git_config_get_value(key.buf, &remote); + strbuf_release(&key);
This config API is beautiful :-).
(Before last year's GSoC, you'd have needed ~10 lines of code to do the same thing)
Makefile
Quoted 1 line
Makefile +1
-0+ return error("Ambiguous refname: '%s'", ref);
Here and elsewhere: don't forget to mark strings for translation.
Makefile
Quoted 9 lines
Makefile +9
-0+/** + * Appends FETCH_HEAD's merge heads into arr. Returns number of merge heads, + * or -1 on error. + */ +static int sha1_array_append_fetch_merge_heads(struct sha1_array *arr) +{ + int num_heads = 0; + char *filename = git_path("FETCH_HEAD"); + FILE *fp = fopen(filename, "r");
I guess this is one instance where we could avoid writting (fetch) and then parsing (here) if we had a better internal API.
But that can come after, of course.
Makefile
Quoted 4 lines
Makefile +4
-0+} + + +static void argv_array_push_merge_args(struct argv_array *arr,
Bikeshed: you sometimes have two blank lines between functions, sometimes one. Not sure it's intended.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Quoted 16 lines · Paul Tan
builtin/pull.c new file +16
-0+/* Global vars since they are used often */ +static char *head_name; +static const char *head_name_short; +static unsigned char head_sha1[20 ]; +static int head_flags; + +enum rebase_type { +REBASE_FALSE =0 , +REBASE_TRUE =1 , +REBASE_PRESERVE =2 +}; + +/** +* Parse rebase config/option value and return corresponding int +*/ +static int parse_rebase (const char *arg)
builtin/pull.c line 74
Quoted 10 lines
builtin/pull.c new file +10
-0+{ +if (!strcmp (arg,"true" )) +return REBASE_TRUE ; +else if (!strcmp (arg,"false" )) +return REBASE_FALSE ; +else if (!strcmp (arg,"preserve" )) +return REBASE_PRESERVE ; +else +return -1 ;/* Invalid value */ +}
Even though the original does not use bool-or-string-config, we would want to do the same by doing something like
case (config_maybe_bool()) {
case 0:
return REBASE_FALSE;
case 1:
return REBASE_TRUE;
default:
if (!strcmp(arg, "preserve"))
return REBASE_PRESERVE;
return -1;
}and then use that in rebase_config_default().
builtin/pull.c line 87
Quoted 13 lines
builtin/pull.c new file +13
-0+ +/** +* Returns default rebase option value +*/ +static int rebase_config_default (void ) +{ +struct strbuf name =STRBUF_INIT ; +const char *value =NULL ; +int boolval; + +strbuf_addf (&name,"branch.%s.rebase" , head_name_short); +if (git_config_get_value (name.buf, &value)) +git_config_get_value ("pull.rebase" , &value);
What happens when neither is defined?
builtin/pull.c line 90
Quoted 3 lines
builtin/pull.c new file +3
-0+strbuf_release (&name); +if (!value) +return REBASE_FALSE ;
Hmph, are you sure about this? Isn't this "[pull] rebase" that does not have "= value", in which case pull.rebase is "true"?
You cannot use NULL as the sentinel value to tell that you did not find either branch.*.rebase nor pull.rebase (in which case you want to default to 'false'). Either of them can be spelled as an equal-less true, which you will see as value==NULL, and you want to take that as 'true'.
const char *value = "false";
...
if (get_value(..., &value))
get_value(..., &value));
strbuf_release(&name);
if (!value)
return REBASE_TRUE;
return parse_rebase(value);or something along that line, perhaps?
builtin/pull.c line 95
Quoted 5 lines
builtin/pull.c new file +5
-0+ boolval =git_config_maybe_bool ("pull.rebase" , value); +if (boolval >=0 ) +return boolval ?REBASE_TRUE :REBASE_FALSE ; +else if (value && !strcmp (value,"preserve" )) +return REBASE_PRESERVE ;
Is value something you need to free before returning from this function?
builtin/pull.c line 149
Quoted 13 lines
builtin/pull.c new file +13
-0+static int parse_opt_recurse_submodules (const struct option *opt,const char *arg,int unset) +{ +if (!arg) + *(int *)opt->value = unset ?RS_NO :RS_YES ; +else if (!strcmp (arg,"no" )) + *(int *)opt->value =RS_NO ; +else if (!strcmp (arg,"yes" )) + *(int *)opt->value =RS_YES ; +else if (!strcmp (arg,"on-demand" )) + *(int *)opt->value =RS_ON_DEMAND ; +else +return -1 ; +return 0 ;
I suspect that maybe-bool-or-string comment applies equally here for the UI consistency.
I'll stop here for now. Thanks.
Hi,
Thanks for your suggestions, I agree with most of them :).
Quoted 11 lines · Johannes Schindelin
builtin/pull.c line 106
Quoted 9 lines
+static int
parse_opt_rebase(const struct option *opt, const char *arg, int unset)builtin/pull.c new file +7
-0+{ +if (arg) + *(int *)opt->value =parse_rebase (arg); +else + *(int *)opt->value = unset ?REBASE_FALSE :REBASE_TRUE ; +return (*(int *)opt->value) >=0 ?0 :-1 ; +}In this function (and also in other places below), there is this pattern that a
struct optionpointer is passed to the function, but then only*(int *)opt->valueis written to. Therefore, I would suggest to change the signature of the function and pass(int *)opt->valueas function parameter.
It's used as a callback for the argument parser though, so the callback signature is required.
Quoted 6 lines
Quoted 2 lines
+static int
has_unstaged_changes(void)Yeah, this function, as well as the ones below it, look as if they are so common that they *should* be already somewhere in libgit.a. But I did not find them, either...
Of course it *would* be nice to identify places where essentially the same code is needed, and refactor accordingly. But I think that is outside the scope of this project.
Actually, I think that identifying the places where code can be trivially shared (without requiring major refactoring) should be part of the project, otherwise lots of code may be duplicated and cause code bloat. The obvious one would be fork_point() in this patch, which is copied from the merge-base builtin and not accessible because it has static linkage. The project should, at the very least, allow the function to be shared between git-pull and git-merge-base, as well as to modify the function so that it can fail without die()-ing.
Regards,
Paul
Hi Paul,
Quoted 16 lines · Paul Tan
Thanks for your suggestions, I agree with most of them :).
Quoted 11 lines · Johannes Schindelin
builtin/pull.c line 106
Quoted 9 lines
+static int
parse_opt_rebase(const struct option *opt, const char *arg, int unset)builtin/pull.c new file +7
-0+{ +if (arg) + *(int *)opt->value =parse_rebase (arg); +else + *(int *)opt->value = unset ?REBASE_FALSE :REBASE_TRUE ; +return (*(int *)opt->value) >=0 ?0 :-1 ; +}In this function (and also in other places below), there is this pattern that a
struct optionpointer is passed to the function, but then only*(int *)opt->valueis written to. Therefore, I would suggest to change the signature of the function and pass(int *)opt->valueas function parameter.
Quoted 2 lines
It's used as a callback for the argument parser though, so the callback signature is required.
Good point ;-)
For readability, I would then suggest to declare int *result = (int *)opt->value; on the top of the function and then use *result = ... later.
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
+static int
has_unstaged_changes(void)Yeah, this function, as well as the ones below it, look as if they are so common that they *should* be already somewhere in libgit.a. But I did not find them, either...
Of course it *would* be nice to identify places where essentially the same code is needed, and refactor accordingly. But I think that is outside the scope of this project.
Quoted 8 lines
Actually, I think that identifying the places where code can be trivially shared (without requiring major refactoring) should be part of the project, otherwise lots of code may be duplicated and cause code bloat. The obvious one would be
fork_point()in this patch, which is copied from the merge-base builtin and not accessible because it has static linkage. The project should, at the very least, allow the function to be shared between git-pull and git-merge-base, as well as to modify the function so that it can fail without die()-ing.
Yes, I agree wholeheartedly. But I like your choice to strike a balance for the sake of a proof-of-concept.
Ciao,
Johannes
Hi,
Thanks for the review, though I would like to work on the proposal now before the deadline passes :)
Quoted 11 lines · Matthieu Moy
Quoted 3 lines · Paul Tan
Ideally, I think the solution is to
improve the test suite and make it as comprehensive as possible, but writing a comprehensive test suite may be too time consuming.time-consuming, but also very beneficial since the code would end up being better tested. For sure, a rewrite is a good way to break stuff, but anything untested can also be broken by mistake rather easily at any time.
Quoted 4 lines
I'd suggest doing a bit of manual mutation testing: take your C code, comment-out a few lines of code, see if the tests still pass, and if they do, add a failing test that passes again once you uncomment the code.
Maybe code coverage tools could help here so we only need to focus on the code paths that are untested by the test suite. At the minimum, all of the non-trivial code paths in both the shell script and the converted builtin must be covered by tests. This should help to eliminate most sources of breakages. Anything further than that would require an experienced understanding of all the possible important inputs to be tested, which I personally feel would make the project quite tedious.
I see git already has gcov support. For shell scripts, maybe kcov[1] could be used. With some slight code changes, I managed to generate a report for the git-pull tests[2] which should at least provide a good starting point for how the tests can be improved.
[1] http://simonkagstrom.github.io/kcov/
[2] <http://www.googledrive.com/host/0B4O2AiYulllpfmJlTW4xT050OVVicnNWWS02dm52aTJ2TFIwQ2QwdWh0VHotSkU4eUNNWjg>
Regards,
Paul
Hi Paul,
Quoted 2 lines · Paul Tan
Thanks for the review, though I would like to work on the proposal now before the deadline passes :)
That makes sense.
Quoted 27 lines
Quoted 11 lines · Matthieu Moy
Quoted 3 lines · Paul Tan
Ideally, I think the solution is to
improve the test suite and make it as comprehensive as possible, but writing a comprehensive test suite may be too time consuming.time-consuming, but also very beneficial since the code would end up being better tested. For sure, a rewrite is a good way to break stuff, but anything untested can also be broken by mistake rather easily at any time.
Quoted 4 lines
I'd suggest doing a bit of manual mutation testing: take your C code, comment-out a few lines of code, see if the tests still pass, and if they do, add a failing test that passes again once you uncomment the code.
Maybe code coverage tools could help here so we only need to focus on the code paths that are untested by the test suite. At the minimum, all of the non-trivial code paths in both the shell script and the converted builtin must be covered by tests. This should help to eliminate most sources of breakages. Anything further than that would require an experienced understanding of all the possible important inputs to be tested, which I personally feel would make the project quite tedious.
Quoted 4 lines
I see git already has gcov support. For shell scripts, maybe kcov[1] could be used. With some slight code changes, I managed to generate a report for the git-pull tests[2] which should at least provide a good starting point for how the tests can be improved.
While it is often a tempting idea to make test suites as thorough as possible, there lies a true danger herein. True war story: in one of the projects I was involved in, the test suite grew to a size that one complete run lasted two weeks. Yes, that is fourteen days. Needless to say: this test suite was run rarely. How useful is a test suite that is run rarely? More useful than a non-existent one, to be sure, but it is still more of a burden than a boon.
Now, on Windows the test suite takes almost three hours to run. This really, really slows down development.
So while we are not yet at the "too large to be useful state", I would caution against trying to get there.
Instead, I would really like to focus on the *usage*. Calling git grep "git pull" t/ should give you an idea what usage of git pull is already tested. It should be pretty easy to come up with a list of *common* use cases, and if any of them are not covered, adding tests for them is simple and straight-forward, too.
Ciao,
Johannes
Hi,
Quoted 43 lines · Junio C Hamano
Quoted 16 lines · Paul Tan
builtin/pull.c new file +16
-0+/* Global vars since they are used often */ +static char *head_name; +static const char *head_name_short; +static unsigned char head_sha1[20 ]; +static int head_flags; + +enum rebase_type { +REBASE_FALSE =0 , +REBASE_TRUE =1 , +REBASE_PRESERVE =2 +}; + +/** +* Parse rebase config/option value and return corresponding int +*/ +static int parse_rebase (const char *arg)builtin/pull.c line 74
Quoted 10 lines
builtin/pull.c new file +10
-0+{ +if (!strcmp (arg,"true" )) +return REBASE_TRUE ; +else if (!strcmp (arg,"false" )) +return REBASE_FALSE ; +else if (!strcmp (arg,"preserve" )) +return REBASE_PRESERVE ; +else +return -1 ;/* Invalid value */ +}Even though the original does not use bool-or-string-config, we would want to do the same by doing something like
case (config_maybe_bool()) { case 0: return REBASE_FALSE; case 1: return REBASE_TRUE; default: if (!strcmp(arg, "preserve")) return REBASE_PRESERVE; return -1; }
Quoted 1 line
and then use that in
rebase_config_default().
If you mean letting "yes", "on", "no", "off" be accepted on the command line as well, then yes I guess it will be a good idea.
Quoted 39 lines
Quoted 4 lines
builtin/pull.c new file +3
-0+ +/** +* Returns default rebase option value builtin/pull.c line 87
Quoted 10 lines
builtin/pull.c new file +10
-0+*/ +static int rebase_config_default (void ) +{ +struct strbuf name =STRBUF_INIT ; +const char *value =NULL ; +int boolval; + +strbuf_addf (&name,"branch.%s.rebase" , head_name_short); +if (git_config_get_value (name.buf, &value)) +git_config_get_value ("pull.rebase" , &value);What happens when neither is defined?
builtin/pull.c line 90
Quoted 3 lines
builtin/pull.c new file +3
-0+strbuf_release (&name); +if (!value) +return REBASE_FALSE ;Hmph, are you sure about this? Isn't this "[pull] rebase" that does not have "= value", in which case pull.rebase is "true"?
You cannot use NULL as the sentinel value to tell that you did not find either branch.*.rebase nor pull.rebase (in which case you want to default to 'false'). Either of them can be spelled as an equal-less true, which you will see as value==NULL, and you want to take that as 'true'.
const char *value = "false"; ... if (get_value(..., &value)) get_value(..., &value)); strbuf_release(&name); if (!value) return REBASE_TRUE; return parse_rebase(value);
Quoted 1 line
or something along that line, perhaps?
Whoops, didn't take into account the possibility that the config value could be NULL. Thanks.
Quoted 11 lines
builtin/pull.c line 95
Quoted 6 lines
builtin/pull.c new file +5
-0+ boolval =git_config_maybe_bool ("pull.rebase" , value); +if (boolval >=0 ) +return boolval ?REBASE_TRUE :REBASE_FALSE ; +else if (value && !strcmp (value,"preserve" )) +return REBASE_PRESERVE ;Is value something you need to free before returning from this function?
From my reading if
config.c, the memory of value comes from the
config_set the_config_set (the config cache), so there is no need to free it.
Quoted 17 lines
Quoted 4 lines
builtin/pull.c new file +3
-0+static int parse_opt_recurse_submodules (const struct option *opt,const char *arg,int unset) +{ +if (!arg)builtin/pull.c line 149
Quoted 10 lines
builtin/pull.c new file +10
-0+ *(int *)opt->value = unset ?RS_NO :RS_YES ; + else if (!strcmp(arg, "no")) + *(int *)opt->value =RS_NO ; + else if (!strcmp(arg, "yes")) + *(int *)opt->value =RS_YES ; + else if (!strcmp(arg, "on-demand")) + *(int *)opt->value =RS_ON_DEMAND ; + else + return -1; +return 0 ;I suspect that maybe-bool-or-string comment applies equally here for the UI consistency.
Yup, I'll keep that in mind.
Quoted 1 line
I'll stop here for now. Thanks.
Thanks.
Hi,
Quoted 14 lines · Johannes Schindelin
Quoted 9 lines
Maybe code coverage tools could help here so we only need to focus on the code paths that are untested by the test suite. At the minimum, all of the non-trivial code paths in both the shell script and the converted builtin must be covered by tests. This should help to eliminate most sources of breakages. Anything further than that would require an experienced understanding of all the possible important inputs to be tested, which I personally feel would make the project quite tedious.
Quoted 4 lines
I see git already has gcov support. For shell scripts, maybe kcov[1] could be used. With some slight code changes, I managed to generate a report for the git-pull tests[2] which should at least provide a good starting point for how the tests can be improved.
Quoted 7 lines
While it is often a tempting idea to make test suites as thorough as possible, there lies a true danger herein. True war story: in one of the projects I was involved in, the test suite grew to a size that one complete run lasted two weeks. Yes, that is fourteen days. Needless to say: this test suite was run rarely. How useful is a test suite that is run rarely? More useful than a non-existent one, to be sure, but it is still more of a burden than a boon.
Now, on Windows the test suite takes almost three hours to run. This really, really slows down development.
So while we are not yet at the "too large to be useful state", I would caution against trying to get there.
Instead, I would really like to focus on the *usage*. Calling
git grep "git pull" t/should give you an idea what usage ofgit pullis already tested. It should be pretty easy to come up with a list of *common* use cases, and if any of them are not covered, adding tests for them is simple and straight-forward, too.
The code coverage tools can help here as well. The kcov output clearly shows which options of git-pull are currently not being tested. But yes, I agree that the test suite shouldn't be relied too much on compared to code inspection and review.
On another important topic, though, along with git-pull.sh, I'm looking for another script to convert in parallel with git-pull.sh so that there will be no blocks due to patch review. Generally, I think rewriting scripts that are called frequently by users, or spawn a lot of processes due to loops, would be most desirable because the runtime gains would be much higher. A quick review of the scripts shows that git-am.sh, git-rebase--interactive.sh and git-quiltimport.sh have pretty heavy loops with lots of process spawning that grows with input.
I'm currently leaning with git-am because not only is it a frequently used command, git-rebase--am.sh (for non-interactive rebase) calls it as well. In fact, quick tests show that it takes up 98% of git-rebase's execution time on Windows, so if git-am's performance improves it would be a huge win on many fronts. git-am's code also seems to be manageable for a 3-month project.
Anyway, I would like to know if you (or anyone else) have any scripts in mind.
(I also think that just 2 scripts would be enough to fill the 3 months, but that might be me just being too conservative)
Regards,
Paul
Hi Paul,
Quoted 4 lines · Paul Tan
The code coverage tools can help here as well. The kcov output clearly shows which options of git-pull are currently not being tested. But yes, I agree that the test suite shouldn't be relied too much on compared to code inspection and review.
Fully agree.
Quoted 10 lines
On another important topic, though, along with
git-pull.sh, I'm looking for another script to convert in parallel withgit-pull.shso that there will be no blocks due to patch review. Generally, I think rewriting scripts that are called frequently by users, or spawn a lot of processes due to loops, would be most desirable because the runtime gains would be much higher. A quick review of the scripts shows thatgit-am.sh,git-rebase--interactive.shandgit-quiltimport.shhave pretty heavy loops with lots of process spawning that grows with input.
Quoted 6 lines
I'm currently leaning with git-am because not only is it a frequently used command,
git-rebase--am.sh(for non-interactive rebase) calls it as well. In fact, quick tests show that it takes up 98% of git-rebase's execution time on Windows, so if git-am's performance improves it would be a huge win on many fronts. git-am's code also seems to be manageable for a 3-month project.
Yeah, git am is definitely a good pick.
Thanks!
Johannes
Quoted 4 lines · Paul Tan
I see git already has gcov support. For shell scripts, maybe kcov[1] could be used. With some slight code changes, I managed to generate a report for the git-pull tests[2] which should at least provide a good starting point for how the tests can be improved.
Could you share these changes? I'm just wondering if we can add kcov support to the test suite.
Quoted 2 lines
[1] http://simonkagstrom.github.io/kcov/
[2] <http://www.googledrive.com/host/0B4O2AiYulllpfmJlTW4xT050OVVicnNWWS02dm52aTJ2TFIwQ2QwdWh0VHotSkU4eUNNWjg>
At least we can see [3] that not many options in git-pull is executed. Cool reports.
Signature · 1 line
Duy
Quoted 2 lines · Duy Nguyen
Could you share these changes? I'm just wondering if we can add kcov support to the test suite.
In this case it's more of an embarrassing hack, as I just needed a way to make git run "kcov outdir git-pull.sh" whenever git pull is called since kcov will not instrument any spawned subprocesses. I modified execv_dashed_external() in git.c to prepend kcov to argv (diff probably munged by gmail):
git.c +7 -1
@@ -536,6 +536,8 @@ static void execv_dashed_external(const char **argv)struct strbuf cmd =STRBUF_INIT ;const char *tmp;int status; +struct argv_array args =ARGV_ARRAY_INIT ; +int i;if (use_pager ==-1 ) use_pager =check_pager_config (argv[0 ]);
@@ -551,6 +553,11 @@ static void execv_dashed_external(const char **argv)*/ tmp = argv[0 ]; argv[0 ] = cmd.buf; +argv_array_push (&args,"kcov" ); +argv_array_push (&args,"/home/pyokagan/pyk/git/kcov" ); +argv_array_push (&args, cmd.buf); +for (i =1 ; argv[i]; i++) +argv_array_push (&args, argv[i]);trace_argv_printf (argv,"trace: exec:" );
@@ -558,7 +565,7 @@ static void execv_dashed_external(const char **argv)* if we fail because the command is not found, it is * OK to return. Otherwise, we just pass along the status code. */ - status = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE |
RUN_CLEAN_ON_EXIT);
+ status = run_command_v_opt(args.argv, RUN_SILENT_EXEC_FAILURE
| RUN_CLEAN_ON_EXIT);
if (status >= 0 || errno != ENOENT)
exit(status);I'm guessing a real solution is to follow what the test suite does for the --valgrind option, though I haven't looked into it in detail.