From 176d58bad44d5cc3b286c5744c4cae1c283cb00d Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 09:17:12 +0200 Subject: [PATCH 001/142] examples: general: use tabs instead of spaces --- examples/general.c | 914 ++++++++++++++++++++++----------------------- 1 file changed, 457 insertions(+), 457 deletions(-) diff --git a/examples/general.c b/examples/general.c index 32fdaf407..036cd9e7d 100644 --- a/examples/general.c +++ b/examples/general.c @@ -52,479 +52,479 @@ static void check_error(int error_code, const char *action) return; printf("Error %d %s - %s\n", error_code, action, - (error && error->message) ? error->message : "???"); + (error && error->message) ? error->message : "???"); exit(1); } int main (int argc, char** argv) { - // Initialize the library, this will set up any global state which libgit2 needs - // including threading and crypto - git_libgit2_init(); + // Initialize the library, this will set up any global state which libgit2 needs + // including threading and crypto + git_libgit2_init(); - // ### Opening the Repository + // ### Opening the Repository - // There are a couple of methods for opening a repository, this being the - // simplest. There are also [methods][me] for specifying the index file - // and work tree locations, here we assume they are in the normal places. + // There are a couple of methods for opening a repository, this being the + // simplest. There are also [methods][me] for specifying the index file + // and work tree locations, here we assume they are in the normal places. // // (Try running this program against tests/resources/testrepo.git.) - // - // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository - int error; - const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git"; - git_repository *repo; - - error = git_repository_open(&repo, repo_path); - check_error(error, "opening repository"); - - // ### SHA-1 Value Conversions - - // For our first example, we will convert a 40 character hex value to the - // 20 byte raw SHA1 value. - printf("*Hex to Raw*\n"); - char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"; - - // The `git_oid` is the structure that keeps the SHA value. We will use - // this throughout the example for storing the value of the current SHA - // key we're working with. - git_oid oid; - git_oid_fromstr(&oid, hex); - - // Once we've converted the string into the oid value, we can get the raw - // value of the SHA by accessing `oid.id` - - // Next we will convert the 20 byte raw SHA1 value to a human readable 40 - // char hex value. - printf("\n*Raw to Hex*\n"); - char out[GIT_OID_HEXSZ+1]; - out[GIT_OID_HEXSZ] = '\0'; - - // If you have a oid, you can easily get the hex value of the SHA as well. - git_oid_fmt(out, &oid); - printf("SHA hex string: %s\n", out); - - // ### Working with the Object Database - - // **libgit2** provides [direct access][odb] to the object database. The - // object database is where the actual objects are stored in Git. For - // working with raw objects, we'll need to get this structure from the - // repository. - // - // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb - git_odb *odb; - git_repository_odb(&odb, repo); - - // #### Raw Object Reading - - printf("\n*Raw Object Read*\n"); - git_odb_object *obj; - git_otype otype; - const unsigned char *data; - const char *str_type; - - // We can read raw objects directly from the object database if we have - // the oid (SHA) of the object. This allows us to access objects without - // knowing their type and inspect the raw bytes unparsed. - error = git_odb_read(&obj, odb, &oid); - check_error(error, "finding object in repository"); - - // A raw object only has three properties - the type (commit, blob, tree - // or tag), the size of the raw data and the raw, unparsed data itself. - // For a commit or tag, that raw data is human readable plain ASCII - // text. For a blob it is just file contents, so it could be text or - // binary data. For a tree it is a special binary format, so it's unlikely - // to be hugely helpful as a raw object. - data = (const unsigned char *)git_odb_object_data(obj); - otype = git_odb_object_type(obj); - - // We provide methods to convert from the object type which is an enum, to - // a string representation of that value (and vice-versa). - str_type = git_object_type2string(otype); - printf("object length and type: %d, %s\n", - (int)git_odb_object_size(obj), - str_type); - - // For proper memory management, close the object when you are done with - // it or it will leak memory. - git_odb_object_free(obj); - - // #### Raw Object Writing - - printf("\n*Raw Object Write*\n"); - - // You can also write raw object data to Git. This is pretty cool because - // it gives you direct access to the key/value properties of Git. Here - // we'll write a new blob object that just contains a simple string. - // Notice that we have to specify the object type as the `git_otype` enum. - git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB); - - // Now that we've written the object, we can check out what SHA1 was - // generated when the object was written to our database. - git_oid_fmt(out, &oid); - printf("Written Object: %s\n", out); - - // ### Object Parsing - - // libgit2 has methods to parse every object type in Git so you don't have - // to work directly with the raw data. This is much faster and simpler - // than trying to deal with the raw data yourself. - - // #### Commit Parsing - - // [Parsing commit objects][pco] is simple and gives you access to all the - // data in the commit - the author (name, email, datetime), committer - // (same), tree, message, encoding and parent(s). - // - // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit - - printf("\n*Commit Parsing*\n"); - - git_commit *commit; - git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"); - - error = git_commit_lookup(&commit, repo, &oid); - check_error(error, "looking up commit"); - - const git_signature *author, *cmtter; - const char *message; - time_t ctime; - unsigned int parents, p; - - // Each of the properties of the commit object are accessible via methods, - // including commonly needed variations, such as `git_commit_time` which - // returns the author time and `git_commit_message` which gives you the - // commit message (as a NUL-terminated string). - message = git_commit_message(commit); - author = git_commit_author(commit); - cmtter = git_commit_committer(commit); - ctime = git_commit_time(commit); - - // The author and committer methods return [git_signature] structures, - // which give you name, email and `when`, which is a `git_time` structure, - // giving you a timestamp and timezone offset. - printf("Author: %s (%s)\n", author->name, author->email); - - // Commits can have zero or more parents. The first (root) commit will - // have no parents, most commits will have one (i.e. the commit it was - // based on) and merge commits will have two or more. Commits can - // technically have any number, though it's rare to have more than two. - parents = git_commit_parentcount(commit); - for (p = 0;p < parents;p++) { - git_commit *parent; - git_commit_parent(&parent, commit, p); - git_oid_fmt(out, git_commit_id(parent)); - printf("Parent: %s\n", out); - git_commit_free(parent); - } - - // Don't forget to close the object to prevent memory leaks. You will have - // to do this for all the objects you open and parse. - git_commit_free(commit); - - // #### Writing Commits - - // libgit2 provides a couple of methods to create commit objects easily as - // well. There are four different create signatures, we'll just show one - // of them here. You can read about the other ones in the [commit API - // docs][cd]. - // - // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit - - printf("\n*Commit Writing*\n"); - git_oid tree_id, parent_id, commit_id; - git_tree *tree; - git_commit *parent; - - // Creating signatures for an authoring identity and time is simple. You - // will need to do this to specify who created a commit and when. Default - // values for the name and email should be found in the `user.name` and - // `user.email` configuration options. See the `config` section of this - // example file to see how to access config values. - git_signature_new((git_signature **)&author, - "Scott Chacon", "schacon@gmail.com", 123456789, 60); - git_signature_new((git_signature **)&cmtter, - "Scott A Chacon", "scott@github.com", 987654321, 90); - - // Commit objects need a tree to point to and optionally one or more - // parents. Here we're creating oid objects to create the commit with, - // but you can also use - git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); - git_tree_lookup(&tree, repo, &tree_id); - git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - git_commit_lookup(&parent, repo, &parent_id); - - // Here we actually create the commit object with a single call with all - // the values we need to create the commit. The SHA key is written to the - // `commit_id` variable here. - git_commit_create_v( - &commit_id, /* out id */ - repo, - NULL, /* do not update the HEAD */ - author, - cmtter, - NULL, /* use default message encoding */ - "example commit", - tree, - 1, parent); - - // Now we can take a look at the commit SHA we've generated. - git_oid_fmt(out, &commit_id); - printf("New Commit: %s\n", out); - - // #### Tag Parsing - - // You can parse and create tags with the [tag management API][tm], which - // functions very similarly to the commit lookup, parsing and creation - // methods, since the objects themselves are very similar. - // - // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag - printf("\n*Tag Parsing*\n"); - git_tag *tag; - const char *tmessage, *tname; - git_otype ttype; - - // We create an oid for the tag object if we know the SHA and look it up - // the same way that we would a commit (or any other object). - git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); - - error = git_tag_lookup(&tag, repo, &oid); - check_error(error, "looking up tag"); - - // Now that we have the tag object, we can extract the information it - // generally contains: the target (usually a commit object), the type of - // the target object (usually 'commit'), the name ('v1.0'), the tagger (a - // git_signature - name, email, timestamp), and the tag message. - git_tag_target((git_object **)&commit, tag); - tname = git_tag_name(tag); // "test" - ttype = git_tag_target_type(tag); // GIT_OBJ_COMMIT (otype enum) - tmessage = git_tag_message(tag); // "tag message\n" - printf("Tag Message: %s\n", tmessage); - - git_commit_free(commit); - - // #### Tree Parsing - - // [Tree parsing][tp] is a bit different than the other objects, in that - // we have a subtype which is the tree entry. This is not an actual - // object type in Git, but a useful structure for parsing and traversing - // tree entries. - // - // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree - printf("\n*Tree Parsing*\n"); - - const git_tree_entry *entry; - git_object *objt; - - // Create the oid and lookup the tree object just like the other objects. - git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5"); - git_tree_lookup(&tree, repo, &oid); - - // Getting the count of entries in the tree so you can iterate over them - // if you want to. - size_t cnt = git_tree_entrycount(tree); // 3 - printf("tree entries: %d\n", (int)cnt); - - entry = git_tree_entry_byindex(tree, 0); - printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c" - - // You can also access tree entries by name if you know the name of the - // entry you're looking for. - entry = git_tree_entry_byname(tree, "README"); - git_tree_entry_name(entry); // "hello.c" - - // Once you have the entry object, you can access the content or subtree - // (or commit, in the case of submodules) that it points to. You can also - // get the mode if you want. - git_tree_entry_to_object(&objt, repo, entry); // blob - - // Remember to close the looked-up object once you are done using it - git_object_free(objt); - - // #### Blob Parsing - - // The last object type is the simplest and requires the least parsing - // help. Blobs are just file contents and can contain anything, there is - // no structure to it. The main advantage to using the [simple blob - // api][ba] is that when you're creating blobs you don't have to calculate - // the size of the content. There is also a helper for reading a file - // from disk and writing it to the db and getting the oid back so you - // don't have to do all those steps yourself. - // - // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob - - printf("\n*Blob Parsing*\n"); - git_blob *blob; - - git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08"); - git_blob_lookup(&blob, repo, &oid); - - // You can access a buffer with the raw contents of the blob directly. - // Note that this buffer may not be contain ASCII data for certain blobs - // (e.g. binary files): do not consider the buffer a NULL-terminated - // string, and use the `git_blob_rawsize` attribute to find out its exact - // size in bytes - printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); // 8 - git_blob_rawcontent(blob); // "content" - - // ### Revwalking - - // The libgit2 [revision walking api][rw] provides methods to traverse the - // directed graph created by the parent pointers of the commit objects. - // Since all commits point back to the commit that came directly before - // them, you can walk this parentage as a graph and find all the commits - // that were ancestors of (reachable from) a given starting point. This - // can allow you to create `git log` type functionality. - // - // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk - - printf("\n*Revwalking*\n"); - git_revwalk *walk; - git_commit *wcommit; - - git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - - // To use the revwalker, create a new walker, tell it how you want to sort - // the output and then push one or more starting points onto the walker. - // If you want to emulate the output of `git log` you would push the SHA - // of the commit that HEAD points to into the walker and then start - // traversing them. You can also 'hide' commits that you want to stop at - // or not see any of their ancestors. So if you want to emulate `git log - // branch1..branch2`, you would push the oid of `branch2` and hide the oid - // of `branch1`. - git_revwalk_new(&walk, repo); - git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); - git_revwalk_push(walk, &oid); - - const git_signature *cauth; - const char *cmsg; - - // Now that we have the starting point pushed onto the walker, we start - // asking for ancestors. It will return them in the sorting order we asked - // for as commit oids. We can then lookup and parse the committed pointed - // at by the returned OID; note that this operation is specially fast - // since the raw contents of the commit object will be cached in memory - while ((git_revwalk_next(&oid, walk)) == 0) { - error = git_commit_lookup(&wcommit, repo, &oid); - check_error(error, "looking up commit during revwalk"); - - cmsg = git_commit_message(wcommit); - cauth = git_commit_author(wcommit); - printf("%s (%s)\n", cmsg, cauth->email); - - git_commit_free(wcommit); - } - - // Like the other objects, be sure to free the revwalker when you're done - // to prevent memory leaks. Also, make sure that the repository being - // walked it not deallocated while the walk is in progress, or it will - // result in undefined behavior - git_revwalk_free(walk); - - // ### Index File Manipulation - - // The [index file API][gi] allows you to read, traverse, update and write - // the Git index file (sometimes thought of as the staging area). - // - // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index - - printf("\n*Index Walking*\n"); - - git_index *index; - unsigned int i, ecount; - - // You can either open the index from the standard location in an open - // repository, as we're doing here, or you can open and manipulate any - // index file with `git_index_open_bare()`. The index for the repository - // will be located and loaded from disk. - git_repository_index(&index, repo); - - // For each entry in the index, you can get a bunch of information - // including the SHA (oid), path and mode which map to the tree objects - // that are written out. It also has filesystem properties to help - // determine what to inspect for changes (ctime, mtime, dev, ino, uid, - // gid, file_size and flags) All these properties are exported publicly in - // the `git_index_entry` struct - ecount = git_index_entrycount(index); - for (i = 0; i < ecount; ++i) { - const git_index_entry *e = git_index_get_byindex(index, i); - - printf("path: %s\n", e->path); - printf("mtime: %d\n", (int)e->mtime.seconds); - printf("fs: %d\n", (int)e->file_size); - } - - git_index_free(index); - - // ### References - - // The [reference API][ref] allows you to list, resolve, create and update - // references such as branches, tags and remote references (everything in - // the .git/refs directory). - // - // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference - - printf("\n*Reference Listing*\n"); - - // Here we will implement something like `git for-each-ref` simply listing - // out all available references and the object SHA they resolve to. - git_strarray ref_list; - git_reference_list(&ref_list, repo); - - const char *refname; - git_reference *ref; - - // Now that we have the list of reference names, we can lookup each ref - // one at a time and resolve them to the SHA, then print both values out. - for (i = 0; i < ref_list.count; ++i) { - refname = ref_list.strings[i]; - git_reference_lookup(&ref, repo, refname); - - switch (git_reference_type(ref)) { - case GIT_REF_OID: - git_oid_fmt(out, git_reference_target(ref)); - printf("%s [%s]\n", refname, out); - break; - - case GIT_REF_SYMBOLIC: - printf("%s => %s\n", refname, git_reference_symbolic_target(ref)); - break; - default: - fprintf(stderr, "Unexpected reference type\n"); - exit(1); - } - } - - git_strarray_free(&ref_list); - - // ### Config Files - - // The [config API][config] allows you to list and updatee config values - // in any of the accessible config file locations (system, global, local). - // - // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config + // + // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository + int error; + const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git"; + git_repository *repo; + + error = git_repository_open(&repo, repo_path); + check_error(error, "opening repository"); + + // ### SHA-1 Value Conversions + + // For our first example, we will convert a 40 character hex value to the + // 20 byte raw SHA1 value. + printf("*Hex to Raw*\n"); + char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"; + + // The `git_oid` is the structure that keeps the SHA value. We will use + // this throughout the example for storing the value of the current SHA + // key we're working with. + git_oid oid; + git_oid_fromstr(&oid, hex); + + // Once we've converted the string into the oid value, we can get the raw + // value of the SHA by accessing `oid.id` + + // Next we will convert the 20 byte raw SHA1 value to a human readable 40 + // char hex value. + printf("\n*Raw to Hex*\n"); + char out[GIT_OID_HEXSZ+1]; + out[GIT_OID_HEXSZ] = '\0'; + + // If you have a oid, you can easily get the hex value of the SHA as well. + git_oid_fmt(out, &oid); + printf("SHA hex string: %s\n", out); + + // ### Working with the Object Database + + // **libgit2** provides [direct access][odb] to the object database. The + // object database is where the actual objects are stored in Git. For + // working with raw objects, we'll need to get this structure from the + // repository. + // + // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb + git_odb *odb; + git_repository_odb(&odb, repo); + + // #### Raw Object Reading + + printf("\n*Raw Object Read*\n"); + git_odb_object *obj; + git_otype otype; + const unsigned char *data; + const char *str_type; + + // We can read raw objects directly from the object database if we have + // the oid (SHA) of the object. This allows us to access objects without + // knowing their type and inspect the raw bytes unparsed. + error = git_odb_read(&obj, odb, &oid); + check_error(error, "finding object in repository"); + + // A raw object only has three properties - the type (commit, blob, tree + // or tag), the size of the raw data and the raw, unparsed data itself. + // For a commit or tag, that raw data is human readable plain ASCII + // text. For a blob it is just file contents, so it could be text or + // binary data. For a tree it is a special binary format, so it's unlikely + // to be hugely helpful as a raw object. + data = (const unsigned char *)git_odb_object_data(obj); + otype = git_odb_object_type(obj); + + // We provide methods to convert from the object type which is an enum, to + // a string representation of that value (and vice-versa). + str_type = git_object_type2string(otype); + printf("object length and type: %d, %s\n", + (int)git_odb_object_size(obj), + str_type); + + // For proper memory management, close the object when you are done with + // it or it will leak memory. + git_odb_object_free(obj); + + // #### Raw Object Writing + + printf("\n*Raw Object Write*\n"); + + // You can also write raw object data to Git. This is pretty cool because + // it gives you direct access to the key/value properties of Git. Here + // we'll write a new blob object that just contains a simple string. + // Notice that we have to specify the object type as the `git_otype` enum. + git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB); + + // Now that we've written the object, we can check out what SHA1 was + // generated when the object was written to our database. + git_oid_fmt(out, &oid); + printf("Written Object: %s\n", out); + + // ### Object Parsing + + // libgit2 has methods to parse every object type in Git so you don't have + // to work directly with the raw data. This is much faster and simpler + // than trying to deal with the raw data yourself. + + // #### Commit Parsing + + // [Parsing commit objects][pco] is simple and gives you access to all the + // data in the commit - the author (name, email, datetime), committer + // (same), tree, message, encoding and parent(s). + // + // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit + + printf("\n*Commit Parsing*\n"); + + git_commit *commit; + git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"); + + error = git_commit_lookup(&commit, repo, &oid); + check_error(error, "looking up commit"); + + const git_signature *author, *cmtter; + const char *message; + time_t ctime; + unsigned int parents, p; + + // Each of the properties of the commit object are accessible via methods, + // including commonly needed variations, such as `git_commit_time` which + // returns the author time and `git_commit_message` which gives you the + // commit message (as a NUL-terminated string). + message = git_commit_message(commit); + author = git_commit_author(commit); + cmtter = git_commit_committer(commit); + ctime = git_commit_time(commit); + + // The author and committer methods return [git_signature] structures, + // which give you name, email and `when`, which is a `git_time` structure, + // giving you a timestamp and timezone offset. + printf("Author: %s (%s)\n", author->name, author->email); + + // Commits can have zero or more parents. The first (root) commit will + // have no parents, most commits will have one (i.e. the commit it was + // based on) and merge commits will have two or more. Commits can + // technically have any number, though it's rare to have more than two. + parents = git_commit_parentcount(commit); + for (p = 0;p < parents;p++) { + git_commit *parent; + git_commit_parent(&parent, commit, p); + git_oid_fmt(out, git_commit_id(parent)); + printf("Parent: %s\n", out); + git_commit_free(parent); + } + + // Don't forget to close the object to prevent memory leaks. You will have + // to do this for all the objects you open and parse. + git_commit_free(commit); + + // #### Writing Commits + + // libgit2 provides a couple of methods to create commit objects easily as + // well. There are four different create signatures, we'll just show one + // of them here. You can read about the other ones in the [commit API + // docs][cd]. + // + // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit + + printf("\n*Commit Writing*\n"); + git_oid tree_id, parent_id, commit_id; + git_tree *tree; + git_commit *parent; + + // Creating signatures for an authoring identity and time is simple. You + // will need to do this to specify who created a commit and when. Default + // values for the name and email should be found in the `user.name` and + // `user.email` configuration options. See the `config` section of this + // example file to see how to access config values. + git_signature_new((git_signature **)&author, + "Scott Chacon", "schacon@gmail.com", 123456789, 60); + git_signature_new((git_signature **)&cmtter, + "Scott A Chacon", "scott@github.com", 987654321, 90); + + // Commit objects need a tree to point to and optionally one or more + // parents. Here we're creating oid objects to create the commit with, + // but you can also use + git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); + git_tree_lookup(&tree, repo, &tree_id); + git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + git_commit_lookup(&parent, repo, &parent_id); + + // Here we actually create the commit object with a single call with all + // the values we need to create the commit. The SHA key is written to the + // `commit_id` variable here. + git_commit_create_v( + &commit_id, /* out id */ + repo, + NULL, /* do not update the HEAD */ + author, + cmtter, + NULL, /* use default message encoding */ + "example commit", + tree, + 1, parent); + + // Now we can take a look at the commit SHA we've generated. + git_oid_fmt(out, &commit_id); + printf("New Commit: %s\n", out); + + // #### Tag Parsing + + // You can parse and create tags with the [tag management API][tm], which + // functions very similarly to the commit lookup, parsing and creation + // methods, since the objects themselves are very similar. + // + // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag + printf("\n*Tag Parsing*\n"); + git_tag *tag; + const char *tmessage, *tname; + git_otype ttype; + + // We create an oid for the tag object if we know the SHA and look it up + // the same way that we would a commit (or any other object). + git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + + error = git_tag_lookup(&tag, repo, &oid); + check_error(error, "looking up tag"); + + // Now that we have the tag object, we can extract the information it + // generally contains: the target (usually a commit object), the type of + // the target object (usually 'commit'), the name ('v1.0'), the tagger (a + // git_signature - name, email, timestamp), and the tag message. + git_tag_target((git_object **)&commit, tag); + tname = git_tag_name(tag); // "test" + ttype = git_tag_target_type(tag); // GIT_OBJ_COMMIT (otype enum) + tmessage = git_tag_message(tag); // "tag message\n" + printf("Tag Message: %s\n", tmessage); + + git_commit_free(commit); + + // #### Tree Parsing + + // [Tree parsing][tp] is a bit different than the other objects, in that + // we have a subtype which is the tree entry. This is not an actual + // object type in Git, but a useful structure for parsing and traversing + // tree entries. + // + // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree + printf("\n*Tree Parsing*\n"); + + const git_tree_entry *entry; + git_object *objt; + + // Create the oid and lookup the tree object just like the other objects. + git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5"); + git_tree_lookup(&tree, repo, &oid); + + // Getting the count of entries in the tree so you can iterate over them + // if you want to. + size_t cnt = git_tree_entrycount(tree); // 3 + printf("tree entries: %d\n", (int)cnt); + + entry = git_tree_entry_byindex(tree, 0); + printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c" + + // You can also access tree entries by name if you know the name of the + // entry you're looking for. + entry = git_tree_entry_byname(tree, "README"); + git_tree_entry_name(entry); // "hello.c" + + // Once you have the entry object, you can access the content or subtree + // (or commit, in the case of submodules) that it points to. You can also + // get the mode if you want. + git_tree_entry_to_object(&objt, repo, entry); // blob + + // Remember to close the looked-up object once you are done using it + git_object_free(objt); + + // #### Blob Parsing + + // The last object type is the simplest and requires the least parsing + // help. Blobs are just file contents and can contain anything, there is + // no structure to it. The main advantage to using the [simple blob + // api][ba] is that when you're creating blobs you don't have to calculate + // the size of the content. There is also a helper for reading a file + // from disk and writing it to the db and getting the oid back so you + // don't have to do all those steps yourself. + // + // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob + + printf("\n*Blob Parsing*\n"); + git_blob *blob; + + git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08"); + git_blob_lookup(&blob, repo, &oid); + + // You can access a buffer with the raw contents of the blob directly. + // Note that this buffer may not be contain ASCII data for certain blobs + // (e.g. binary files): do not consider the buffer a NULL-terminated + // string, and use the `git_blob_rawsize` attribute to find out its exact + // size in bytes + printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); // 8 + git_blob_rawcontent(blob); // "content" + + // ### Revwalking + + // The libgit2 [revision walking api][rw] provides methods to traverse the + // directed graph created by the parent pointers of the commit objects. + // Since all commits point back to the commit that came directly before + // them, you can walk this parentage as a graph and find all the commits + // that were ancestors of (reachable from) a given starting point. This + // can allow you to create `git log` type functionality. + // + // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk + + printf("\n*Revwalking*\n"); + git_revwalk *walk; + git_commit *wcommit; + + git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + + // To use the revwalker, create a new walker, tell it how you want to sort + // the output and then push one or more starting points onto the walker. + // If you want to emulate the output of `git log` you would push the SHA + // of the commit that HEAD points to into the walker and then start + // traversing them. You can also 'hide' commits that you want to stop at + // or not see any of their ancestors. So if you want to emulate `git log + // branch1..branch2`, you would push the oid of `branch2` and hide the oid + // of `branch1`. + git_revwalk_new(&walk, repo); + git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); + git_revwalk_push(walk, &oid); + + const git_signature *cauth; + const char *cmsg; + + // Now that we have the starting point pushed onto the walker, we start + // asking for ancestors. It will return them in the sorting order we asked + // for as commit oids. We can then lookup and parse the committed pointed + // at by the returned OID; note that this operation is specially fast + // since the raw contents of the commit object will be cached in memory + while ((git_revwalk_next(&oid, walk)) == 0) { + error = git_commit_lookup(&wcommit, repo, &oid); + check_error(error, "looking up commit during revwalk"); + + cmsg = git_commit_message(wcommit); + cauth = git_commit_author(wcommit); + printf("%s (%s)\n", cmsg, cauth->email); + + git_commit_free(wcommit); + } + + // Like the other objects, be sure to free the revwalker when you're done + // to prevent memory leaks. Also, make sure that the repository being + // walked it not deallocated while the walk is in progress, or it will + // result in undefined behavior + git_revwalk_free(walk); + + // ### Index File Manipulation + + // The [index file API][gi] allows you to read, traverse, update and write + // the Git index file (sometimes thought of as the staging area). + // + // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index + + printf("\n*Index Walking*\n"); + + git_index *index; + unsigned int i, ecount; + + // You can either open the index from the standard location in an open + // repository, as we're doing here, or you can open and manipulate any + // index file with `git_index_open_bare()`. The index for the repository + // will be located and loaded from disk. + git_repository_index(&index, repo); + + // For each entry in the index, you can get a bunch of information + // including the SHA (oid), path and mode which map to the tree objects + // that are written out. It also has filesystem properties to help + // determine what to inspect for changes (ctime, mtime, dev, ino, uid, + // gid, file_size and flags) All these properties are exported publicly in + // the `git_index_entry` struct + ecount = git_index_entrycount(index); + for (i = 0; i < ecount; ++i) { + const git_index_entry *e = git_index_get_byindex(index, i); + + printf("path: %s\n", e->path); + printf("mtime: %d\n", (int)e->mtime.seconds); + printf("fs: %d\n", (int)e->file_size); + } + + git_index_free(index); + + // ### References + + // The [reference API][ref] allows you to list, resolve, create and update + // references such as branches, tags and remote references (everything in + // the .git/refs directory). + // + // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference + + printf("\n*Reference Listing*\n"); + + // Here we will implement something like `git for-each-ref` simply listing + // out all available references and the object SHA they resolve to. + git_strarray ref_list; + git_reference_list(&ref_list, repo); + + const char *refname; + git_reference *ref; + + // Now that we have the list of reference names, we can lookup each ref + // one at a time and resolve them to the SHA, then print both values out. + for (i = 0; i < ref_list.count; ++i) { + refname = ref_list.strings[i]; + git_reference_lookup(&ref, repo, refname); + + switch (git_reference_type(ref)) { + case GIT_REF_OID: + git_oid_fmt(out, git_reference_target(ref)); + printf("%s [%s]\n", refname, out); + break; + + case GIT_REF_SYMBOLIC: + printf("%s => %s\n", refname, git_reference_symbolic_target(ref)); + break; + default: + fprintf(stderr, "Unexpected reference type\n"); + exit(1); + } + } + + git_strarray_free(&ref_list); + + // ### Config Files + + // The [config API][config] allows you to list and updatee config values + // in any of the accessible config file locations (system, global, local). + // + // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config - printf("\n*Config Listing*\n"); + printf("\n*Config Listing*\n"); - const char *email; - int32_t j; + const char *email; + int32_t j; - git_config *cfg; + git_config *cfg; - // Open a config object so we can read global values from it. - char config_path[256]; - sprintf(config_path, "%s/config", repo_path); - check_error(git_config_open_ondisk(&cfg, config_path), "opening config"); + // Open a config object so we can read global values from it. + char config_path[256]; + sprintf(config_path, "%s/config", repo_path); + check_error(git_config_open_ondisk(&cfg, config_path), "opening config"); - git_config_get_int32(&j, cfg, "help.autocorrect"); - printf("Autocorrect: %d\n", j); + git_config_get_int32(&j, cfg, "help.autocorrect"); + printf("Autocorrect: %d\n", j); - git_config_get_string(&email, cfg, "user.email"); - printf("Email: %s\n", email); + git_config_get_string(&email, cfg, "user.email"); + printf("Email: %s\n", email); - // Finally, when you're done with the repository, you can free it as well. - git_repository_free(repo); + // Finally, when you're done with the repository, you can free it as well. + git_repository_free(repo); - return 0; + return 0; } From 986913f45bfe2ae6f7d55cb3ccdc8db1f293d3bc Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 09:25:06 +0200 Subject: [PATCH 002/142] examples: general: extract function demonstrating config files --- examples/general.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/examples/general.c b/examples/general.c index 036cd9e7d..26914a211 100644 --- a/examples/general.c +++ b/examples/general.c @@ -42,6 +42,8 @@ #include #include +static void config_files(const char *repo_path); + // Almost all libgit2 functions return 0 on success or negative on error. // This is not production quality error checking, but should be sufficient // as an example. @@ -498,22 +500,34 @@ int main (int argc, char** argv) git_strarray_free(&ref_list); - // ### Config Files + config_files(repo_path); - // The [config API][config] allows you to list and updatee config values - // in any of the accessible config file locations (system, global, local). - // - // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config + // Finally, when you're done with the repository, you can free it as well. + git_repository_free(repo); + + return 0; +} + +/** + * ### Config Files + * + * The [config API][config] allows you to list and updatee config values + * in any of the accessible config file locations (system, global, local). + * + * [config]: http://libgit2.github.com/libgit2/#HEAD/group/config + */ +static void config_files(const char *repo_path) +{ + const char *email; + char config_path[256]; + int32_t j; + git_config *cfg; printf("\n*Config Listing*\n"); - const char *email; - int32_t j; - - git_config *cfg; - - // Open a config object so we can read global values from it. - char config_path[256]; + /** + * Open a config object so we can read global values from it. + */ sprintf(config_path, "%s/config", repo_path); check_error(git_config_open_ondisk(&cfg, config_path), "opening config"); @@ -522,9 +536,4 @@ int main (int argc, char** argv) git_config_get_string(&email, cfg, "user.email"); printf("Email: %s\n", email); - - // Finally, when you're done with the repository, you can free it as well. - git_repository_free(repo); - - return 0; } From f9a7973dd9a07829f2071fc151529b7accb8164c Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 09:29:14 +0200 Subject: [PATCH 003/142] examples: general: extract function demonstrating reference listings --- examples/general.c | 60 ++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/examples/general.c b/examples/general.c index 26914a211..ea000157a 100644 --- a/examples/general.c +++ b/examples/general.c @@ -41,7 +41,9 @@ // to compile properly and get all the libgit2 API. #include #include +#include +static void reference_listing(git_repository *repo); static void config_files(const char *repo_path); // Almost all libgit2 functions return 0 on success or negative on error. @@ -459,34 +461,53 @@ int main (int argc, char** argv) git_index_free(index); - // ### References + reference_listing(repo); + config_files(repo_path); - // The [reference API][ref] allows you to list, resolve, create and update - // references such as branches, tags and remote references (everything in - // the .git/refs directory). - // - // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference + // Finally, when you're done with the repository, you can free it as well. + git_repository_free(repo); + + return 0; +} + +/** + * ### References + * + * The [reference API][ref] allows you to list, resolve, create and update + * references such as branches, tags and remote references (everything in + * the .git/refs directory). + * + * [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference + */ +static void reference_listing(git_repository *repo) +{ + git_strarray ref_list; + const char *refname; + git_reference *ref; + unsigned i; + char oid_hex[GIT_OID_HEXSZ+1]; printf("\n*Reference Listing*\n"); - // Here we will implement something like `git for-each-ref` simply listing - // out all available references and the object SHA they resolve to. - git_strarray ref_list; + /** + * Here we will implement something like `git for-each-ref` simply listing + * out all available references and the object SHA they resolve to. + * + * Now that we have the list of reference names, we can lookup each ref + * one at a time and resolve them to the SHA, then print both values out. + */ + git_reference_list(&ref_list, repo); - const char *refname; - git_reference *ref; - - // Now that we have the list of reference names, we can lookup each ref - // one at a time and resolve them to the SHA, then print both values out. for (i = 0; i < ref_list.count; ++i) { + memset(oid_hex, 0, sizeof(oid_hex)); refname = ref_list.strings[i]; git_reference_lookup(&ref, repo, refname); switch (git_reference_type(ref)) { case GIT_REF_OID: - git_oid_fmt(out, git_reference_target(ref)); - printf("%s [%s]\n", refname, out); + git_oid_fmt(oid_hex, git_reference_target(ref)); + printf("%s [%s]\n", refname, oid_hex); break; case GIT_REF_SYMBOLIC: @@ -499,13 +520,6 @@ int main (int argc, char** argv) } git_strarray_free(&ref_list); - - config_files(repo_path); - - // Finally, when you're done with the repository, you can free it as well. - git_repository_free(repo); - - return 0; } /** From c079e3c84739564620161d9f445bed8c26a8f95f Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 09:32:15 +0200 Subject: [PATCH 004/142] examples: general: extract function demonstrating index walking --- examples/general.c | 57 +++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/examples/general.c b/examples/general.c index ea000157a..2ea40d393 100644 --- a/examples/general.c +++ b/examples/general.c @@ -43,6 +43,7 @@ #include #include +static void index_walking(git_repository *repo); static void reference_listing(git_repository *repo); static void config_files(const char *repo_path); @@ -426,30 +427,46 @@ int main (int argc, char** argv) // result in undefined behavior git_revwalk_free(walk); - // ### Index File Manipulation + index_walking(repo); + reference_listing(repo); + config_files(repo_path); - // The [index file API][gi] allows you to read, traverse, update and write - // the Git index file (sometimes thought of as the staging area). - // - // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index + // Finally, when you're done with the repository, you can free it as well. + git_repository_free(repo); - printf("\n*Index Walking*\n"); + return 0; +} +/** + * ### Index File Manipulation * + * The [index file API][gi] allows you to read, traverse, update and write + * the Git index file (sometimes thought of as the staging area). + * + * [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index + */ +static void index_walking(git_repository *repo) +{ git_index *index; unsigned int i, ecount; - // You can either open the index from the standard location in an open - // repository, as we're doing here, or you can open and manipulate any - // index file with `git_index_open_bare()`. The index for the repository - // will be located and loaded from disk. + printf("\n*Index Walking*\n"); + + /** + * You can either open the index from the standard location in an open + * repository, as we're doing here, or you can open and manipulate any + * index file with `git_index_open_bare()`. The index for the repository + * will be located and loaded from disk. + */ git_repository_index(&index, repo); - // For each entry in the index, you can get a bunch of information - // including the SHA (oid), path and mode which map to the tree objects - // that are written out. It also has filesystem properties to help - // determine what to inspect for changes (ctime, mtime, dev, ino, uid, - // gid, file_size and flags) All these properties are exported publicly in - // the `git_index_entry` struct + /** + * For each entry in the index, you can get a bunch of information + * including the SHA (oid), path and mode which map to the tree objects + * that are written out. It also has filesystem properties to help + * determine what to inspect for changes (ctime, mtime, dev, ino, uid, + * gid, file_size and flags) All these properties are exported publicly in + * the `git_index_entry` struct + */ ecount = git_index_entrycount(index); for (i = 0; i < ecount; ++i) { const git_index_entry *e = git_index_get_byindex(index, i); @@ -460,14 +477,6 @@ int main (int argc, char** argv) } git_index_free(index); - - reference_listing(repo); - config_files(repo_path); - - // Finally, when you're done with the repository, you can free it as well. - git_repository_free(repo); - - return 0; } /** From 8b93ccdf08b88c861e37c9db5c7274da3886bf77 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 09:35:08 +0200 Subject: [PATCH 005/142] examples: general: extract function demonstrating revwalking --- examples/general.c | 92 ++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/examples/general.c b/examples/general.c index 2ea40d393..016d3e2b6 100644 --- a/examples/general.c +++ b/examples/general.c @@ -43,6 +43,7 @@ #include #include +static void revwalking(git_repository *repo); static void index_walking(git_repository *repo); static void reference_listing(git_repository *repo); static void config_files(const char *repo_path); @@ -373,43 +374,63 @@ int main (int argc, char** argv) printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); // 8 git_blob_rawcontent(blob); // "content" - // ### Revwalking + revwalking(repo); + index_walking(repo); + reference_listing(repo); + config_files(repo_path); - // The libgit2 [revision walking api][rw] provides methods to traverse the - // directed graph created by the parent pointers of the commit objects. - // Since all commits point back to the commit that came directly before - // them, you can walk this parentage as a graph and find all the commits - // that were ancestors of (reachable from) a given starting point. This - // can allow you to create `git log` type functionality. - // - // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk + // Finally, when you're done with the repository, you can free it as well. + git_repository_free(repo); - printf("\n*Revwalking*\n"); + return 0; +} + +/** + * ### Revwalking + * + * The libgit2 [revision walking api][rw] provides methods to traverse the + * directed graph created by the parent pointers of the commit objects. + * Since all commits point back to the commit that came directly before + * them, you can walk this parentage as a graph and find all the commits + * that were ancestors of (reachable from) a given starting point. This + * can allow you to create `git log` type functionality. + * + * [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk + */ +static void revwalking(git_repository *repo) +{ + const git_signature *cauth; + const char *cmsg; + int error; git_revwalk *walk; git_commit *wcommit; + git_oid oid; + + printf("\n*Revwalking*\n"); git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - // To use the revwalker, create a new walker, tell it how you want to sort - // the output and then push one or more starting points onto the walker. - // If you want to emulate the output of `git log` you would push the SHA - // of the commit that HEAD points to into the walker and then start - // traversing them. You can also 'hide' commits that you want to stop at - // or not see any of their ancestors. So if you want to emulate `git log - // branch1..branch2`, you would push the oid of `branch2` and hide the oid - // of `branch1`. + /** + * To use the revwalker, create a new walker, tell it how you want to sort + * the output and then push one or more starting points onto the walker. + * If you want to emulate the output of `git log` you would push the SHA + * of the commit that HEAD points to into the walker and then start + * traversing them. You can also 'hide' commits that you want to stop at + * or not see any of their ancestors. So if you want to emulate `git log + * branch1..branch2`, you would push the oid of `branch2` and hide the oid + * of `branch1`. + */ git_revwalk_new(&walk, repo); git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); git_revwalk_push(walk, &oid); - const git_signature *cauth; - const char *cmsg; - - // Now that we have the starting point pushed onto the walker, we start - // asking for ancestors. It will return them in the sorting order we asked - // for as commit oids. We can then lookup and parse the committed pointed - // at by the returned OID; note that this operation is specially fast - // since the raw contents of the commit object will be cached in memory + /** + * Now that we have the starting point pushed onto the walker, we start + * asking for ancestors. It will return them in the sorting order we asked + * for as commit oids. We can then lookup and parse the committed pointed + * at by the returned OID; note that this operation is specially fast + * since the raw contents of the commit object will be cached in memory + */ while ((git_revwalk_next(&oid, walk)) == 0) { error = git_commit_lookup(&wcommit, repo, &oid); check_error(error, "looking up commit during revwalk"); @@ -421,20 +442,13 @@ int main (int argc, char** argv) git_commit_free(wcommit); } - // Like the other objects, be sure to free the revwalker when you're done - // to prevent memory leaks. Also, make sure that the repository being - // walked it not deallocated while the walk is in progress, or it will - // result in undefined behavior + /** + * Like the other objects, be sure to free the revwalker when you're done + * to prevent memory leaks. Also, make sure that the repository being + * walked it not deallocated while the walk is in progress, or it will + * result in undefined behavior + */ git_revwalk_free(walk); - - index_walking(repo); - reference_listing(repo); - config_files(repo_path); - - // Finally, when you're done with the repository, you can free it as well. - git_repository_free(repo); - - return 0; } /** From 15960454c58ebe538632efac551f71c9efee8eb5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 09:36:31 +0200 Subject: [PATCH 006/142] examples: general: extract functions demonstrating object parsing --- examples/general.c | 367 ++++++++++++++++++++++++++------------------- 1 file changed, 215 insertions(+), 152 deletions(-) diff --git a/examples/general.c b/examples/general.c index 016d3e2b6..2b6dd47d6 100644 --- a/examples/general.c +++ b/examples/general.c @@ -43,6 +43,10 @@ #include #include +static void commit_parsing(git_repository *repo); +static void tag_parsing(git_repository *repo); +static void tree_parsing(git_repository *repo); +static void blob_parsing(git_repository *repo); static void revwalking(git_repository *repo); static void index_walking(git_repository *repo); static void reference_listing(git_repository *repo); @@ -171,63 +175,6 @@ int main (int argc, char** argv) git_oid_fmt(out, &oid); printf("Written Object: %s\n", out); - // ### Object Parsing - - // libgit2 has methods to parse every object type in Git so you don't have - // to work directly with the raw data. This is much faster and simpler - // than trying to deal with the raw data yourself. - - // #### Commit Parsing - - // [Parsing commit objects][pco] is simple and gives you access to all the - // data in the commit - the author (name, email, datetime), committer - // (same), tree, message, encoding and parent(s). - // - // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit - - printf("\n*Commit Parsing*\n"); - - git_commit *commit; - git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"); - - error = git_commit_lookup(&commit, repo, &oid); - check_error(error, "looking up commit"); - - const git_signature *author, *cmtter; - const char *message; - time_t ctime; - unsigned int parents, p; - - // Each of the properties of the commit object are accessible via methods, - // including commonly needed variations, such as `git_commit_time` which - // returns the author time and `git_commit_message` which gives you the - // commit message (as a NUL-terminated string). - message = git_commit_message(commit); - author = git_commit_author(commit); - cmtter = git_commit_committer(commit); - ctime = git_commit_time(commit); - - // The author and committer methods return [git_signature] structures, - // which give you name, email and `when`, which is a `git_time` structure, - // giving you a timestamp and timezone offset. - printf("Author: %s (%s)\n", author->name, author->email); - - // Commits can have zero or more parents. The first (root) commit will - // have no parents, most commits will have one (i.e. the commit it was - // based on) and merge commits will have two or more. Commits can - // technically have any number, though it's rare to have more than two. - parents = git_commit_parentcount(commit); - for (p = 0;p < parents;p++) { - git_commit *parent; - git_commit_parent(&parent, commit, p); - git_oid_fmt(out, git_commit_id(parent)); - printf("Parent: %s\n", out); - git_commit_free(parent); - } - - // Don't forget to close the object to prevent memory leaks. You will have - // to do this for all the objects you open and parse. - git_commit_free(commit); // #### Writing Commits @@ -242,6 +189,7 @@ int main (int argc, char** argv) git_oid tree_id, parent_id, commit_id; git_tree *tree; git_commit *parent; + const git_signature *author, *cmtter; // Creating signatures for an authoring identity and time is simple. You // will need to do this to specify who created a commit and when. Default @@ -279,101 +227,10 @@ int main (int argc, char** argv) git_oid_fmt(out, &commit_id); printf("New Commit: %s\n", out); - // #### Tag Parsing - - // You can parse and create tags with the [tag management API][tm], which - // functions very similarly to the commit lookup, parsing and creation - // methods, since the objects themselves are very similar. - // - // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag - printf("\n*Tag Parsing*\n"); - git_tag *tag; - const char *tmessage, *tname; - git_otype ttype; - - // We create an oid for the tag object if we know the SHA and look it up - // the same way that we would a commit (or any other object). - git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); - - error = git_tag_lookup(&tag, repo, &oid); - check_error(error, "looking up tag"); - - // Now that we have the tag object, we can extract the information it - // generally contains: the target (usually a commit object), the type of - // the target object (usually 'commit'), the name ('v1.0'), the tagger (a - // git_signature - name, email, timestamp), and the tag message. - git_tag_target((git_object **)&commit, tag); - tname = git_tag_name(tag); // "test" - ttype = git_tag_target_type(tag); // GIT_OBJ_COMMIT (otype enum) - tmessage = git_tag_message(tag); // "tag message\n" - printf("Tag Message: %s\n", tmessage); - - git_commit_free(commit); - - // #### Tree Parsing - - // [Tree parsing][tp] is a bit different than the other objects, in that - // we have a subtype which is the tree entry. This is not an actual - // object type in Git, but a useful structure for parsing and traversing - // tree entries. - // - // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree - printf("\n*Tree Parsing*\n"); - - const git_tree_entry *entry; - git_object *objt; - - // Create the oid and lookup the tree object just like the other objects. - git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5"); - git_tree_lookup(&tree, repo, &oid); - - // Getting the count of entries in the tree so you can iterate over them - // if you want to. - size_t cnt = git_tree_entrycount(tree); // 3 - printf("tree entries: %d\n", (int)cnt); - - entry = git_tree_entry_byindex(tree, 0); - printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c" - - // You can also access tree entries by name if you know the name of the - // entry you're looking for. - entry = git_tree_entry_byname(tree, "README"); - git_tree_entry_name(entry); // "hello.c" - - // Once you have the entry object, you can access the content or subtree - // (or commit, in the case of submodules) that it points to. You can also - // get the mode if you want. - git_tree_entry_to_object(&objt, repo, entry); // blob - - // Remember to close the looked-up object once you are done using it - git_object_free(objt); - - // #### Blob Parsing - - // The last object type is the simplest and requires the least parsing - // help. Blobs are just file contents and can contain anything, there is - // no structure to it. The main advantage to using the [simple blob - // api][ba] is that when you're creating blobs you don't have to calculate - // the size of the content. There is also a helper for reading a file - // from disk and writing it to the db and getting the oid back so you - // don't have to do all those steps yourself. - // - // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob - - printf("\n*Blob Parsing*\n"); - git_blob *blob; - - git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08"); - git_blob_lookup(&blob, repo, &oid); - - // You can access a buffer with the raw contents of the blob directly. - // Note that this buffer may not be contain ASCII data for certain blobs - // (e.g. binary files): do not consider the buffer a NULL-terminated - // string, and use the `git_blob_rawsize` attribute to find out its exact - // size in bytes - printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); // 8 - git_blob_rawcontent(blob); // "content" - + commit_parsing(repo); + tag_parsing(repo); + tree_parsing(repo); + blob_parsing(repo); revwalking(repo); index_walking(repo); reference_listing(repo); @@ -385,6 +242,212 @@ int main (int argc, char** argv) return 0; } +/** + * ### Object Parsing + * + * libgit2 has methods to parse every object type in Git so you don't have + * to work directly with the raw data. This is much faster and simpler + * than trying to deal with the raw data yourself. + */ + +/** + * #### Commit Parsing + * + * [Parsing commit objects][pco] is simple and gives you access to all the + * data in the commit - the author (name, email, datetime), committer + * (same), tree, message, encoding and parent(s). + * + * [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit + */ +static void commit_parsing(git_repository *repo) +{ + const git_signature *author, *cmtter; + git_commit *commit, *parent; + git_oid oid; + char oid_hex[GIT_OID_HEXSZ+1]; + const char *message; + unsigned int parents, p; + int error; + time_t ctime; + + printf("\n*Commit Parsing*\n"); + + git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"); + + error = git_commit_lookup(&commit, repo, &oid); + check_error(error, "looking up commit"); + + /** + * Each of the properties of the commit object are accessible via methods, + * including commonly needed variations, such as `git_commit_time` which + * returns the author time and `git_commit_message` which gives you the + * commit message (as a NUL-terminated string). + */ + message = git_commit_message(commit); + author = git_commit_author(commit); + cmtter = git_commit_committer(commit); + ctime = git_commit_time(commit); + + /** + * The author and committer methods return [git_signature] structures, + * which give you name, email and `when`, which is a `git_time` structure, + * giving you a timestamp and timezone offset. + */ + printf("Author: %s (%s)\n", author->name, author->email); + + /** + * Commits can have zero or more parents. The first (root) commit will + * have no parents, most commits will have one (i.e. the commit it was + * based on) and merge commits will have two or more. Commits can + * technically have any number, though it's rare to have more than two. + */ + parents = git_commit_parentcount(commit); + for (p = 0;p < parents;p++) { + memset(oid_hex, 0, sizeof(oid_hex)); + + git_commit_parent(&parent, commit, p); + git_oid_fmt(oid_hex, git_commit_id(parent)); + printf("Parent: %s\n", oid_hex); + git_commit_free(parent); + } + + git_commit_free(commit); +} + +/** + * #### Tag Parsing + * + * You can parse and create tags with the [tag management API][tm], which + * functions very similarly to the commit lookup, parsing and creation + * methods, since the objects themselves are very similar. + * + * [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag + */ +static void tag_parsing(git_repository *repo) +{ + git_commit *commit; + git_otype type; + git_tag *tag; + git_oid oid; + const char *name, *message; + int error; + + printf("\n*Tag Parsing*\n"); + + /** + * We create an oid for the tag object if we know the SHA and look it up + * the same way that we would a commit (or any other object). + */ + git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + + error = git_tag_lookup(&tag, repo, &oid); + check_error(error, "looking up tag"); + + /** + * Now that we have the tag object, we can extract the information it + * generally contains: the target (usually a commit object), the type of + * the target object (usually 'commit'), the name ('v1.0'), the tagger (a + * git_signature - name, email, timestamp), and the tag message. + */ + git_tag_target((git_object **)&commit, tag); + name = git_tag_name(tag); /* "test" */ + type = git_tag_target_type(tag); /* GIT_OBJ_COMMIT (otype enum) */ + message = git_tag_message(tag); /* "tag message\n" */ + printf("Tag Message: %s\n", message); + + git_commit_free(commit); +} + +/** + * #### Tree Parsing + * + * [Tree parsing][tp] is a bit different than the other objects, in that + * we have a subtype which is the tree entry. This is not an actual + * object type in Git, but a useful structure for parsing and traversing + * tree entries. + * + * [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree + */ +static void tree_parsing(git_repository *repo) +{ + const git_tree_entry *entry; + size_t cnt; + git_object *obj; + git_tree *tree; + git_oid oid; + + printf("\n*Tree Parsing*\n"); + + /** + * Create the oid and lookup the tree object just like the other objects. + */ + git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); + git_tree_lookup(&tree, repo, &oid); + + /** + * Getting the count of entries in the tree so you can iterate over them + * if you want to. + */ + cnt = git_tree_entrycount(tree); /* 2 */ + printf("tree entries: %d\n", (int) cnt); + + entry = git_tree_entry_byindex(tree, 0); + printf("Entry name: %s\n", git_tree_entry_name(entry)); /* "README" */ + + /** + * You can also access tree entries by name if you know the name of the + * entry you're looking for. + */ + entry = git_tree_entry_byname(tree, "README"); + git_tree_entry_name(entry); /* "README" */ + + /** + * Once you have the entry object, you can access the content or subtree + * (or commit, in the case of submodules) that it points to. You can also + * get the mode if you want. + */ + git_tree_entry_to_object(&obj, repo, entry); /* blob */ + + /** + * Remember to close the looked-up object once you are done using it + */ + git_object_free(obj); +} + +/** + * #### Blob Parsing + * + * The last object type is the simplest and requires the least parsing + * help. Blobs are just file contents and can contain anything, there is + * no structure to it. The main advantage to using the [simple blob + * api][ba] is that when you're creating blobs you don't have to calculate + * the size of the content. There is also a helper for reading a file + * from disk and writing it to the db and getting the oid back so you + * don't have to do all those steps yourself. + * + * [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob + */ +static void blob_parsing(git_repository *repo) +{ + git_blob *blob; + git_oid oid; + + printf("\n*Blob Parsing*\n"); + + git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08"); + git_blob_lookup(&blob, repo, &oid); + + /** + * You can access a buffer with the raw contents of the blob directly. + * Note that this buffer may not be contain ASCII data for certain blobs + * (e.g. binary files): do not consider the buffer a NULL-terminated + * string, and use the `git_blob_rawsize` attribute to find out its exact + * size in bytes + * */ + printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); /* 8 */ + git_blob_rawcontent(blob); /* "content" */ +} + /** * ### Revwalking * From b009adad3506ba634611bb17ab332b1226bcb116 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 09:59:28 +0200 Subject: [PATCH 007/142] examples: general: extract function demonstrating commit writing --- examples/general.c | 119 +++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 52 deletions(-) diff --git a/examples/general.c b/examples/general.c index 2b6dd47d6..56fa11bb2 100644 --- a/examples/general.c +++ b/examples/general.c @@ -43,6 +43,7 @@ #include #include +static void commit_writing(git_repository *repo); static void commit_parsing(git_repository *repo); static void tag_parsing(git_repository *repo); static void tree_parsing(git_repository *repo); @@ -175,58 +176,7 @@ int main (int argc, char** argv) git_oid_fmt(out, &oid); printf("Written Object: %s\n", out); - - // #### Writing Commits - - // libgit2 provides a couple of methods to create commit objects easily as - // well. There are four different create signatures, we'll just show one - // of them here. You can read about the other ones in the [commit API - // docs][cd]. - // - // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit - - printf("\n*Commit Writing*\n"); - git_oid tree_id, parent_id, commit_id; - git_tree *tree; - git_commit *parent; - const git_signature *author, *cmtter; - - // Creating signatures for an authoring identity and time is simple. You - // will need to do this to specify who created a commit and when. Default - // values for the name and email should be found in the `user.name` and - // `user.email` configuration options. See the `config` section of this - // example file to see how to access config values. - git_signature_new((git_signature **)&author, - "Scott Chacon", "schacon@gmail.com", 123456789, 60); - git_signature_new((git_signature **)&cmtter, - "Scott A Chacon", "scott@github.com", 987654321, 90); - - // Commit objects need a tree to point to and optionally one or more - // parents. Here we're creating oid objects to create the commit with, - // but you can also use - git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); - git_tree_lookup(&tree, repo, &tree_id); - git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - git_commit_lookup(&parent, repo, &parent_id); - - // Here we actually create the commit object with a single call with all - // the values we need to create the commit. The SHA key is written to the - // `commit_id` variable here. - git_commit_create_v( - &commit_id, /* out id */ - repo, - NULL, /* do not update the HEAD */ - author, - cmtter, - NULL, /* use default message encoding */ - "example commit", - tree, - 1, parent); - - // Now we can take a look at the commit SHA we've generated. - git_oid_fmt(out, &commit_id); - printf("New Commit: %s\n", out); - + commit_writing(repo); commit_parsing(repo); tag_parsing(repo); tree_parsing(repo); @@ -242,6 +192,71 @@ int main (int argc, char** argv) return 0; } +/** + * #### Writing Commits + * + * libgit2 provides a couple of methods to create commit objects easily as + * well. There are four different create signatures, we'll just show one + * of them here. You can read about the other ones in the [commit API + * docs][cd]. + * + * [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit + */ +static void commit_writing(git_repository *repo) +{ + git_oid tree_id, parent_id, commit_id; + git_tree *tree; + git_commit *parent; + const git_signature *author, *cmtter; + char oid_hex[GIT_OID_HEXSZ+1] = { 0 }; + + printf("\n*Commit Writing*\n"); + + /** + * Creating signatures for an authoring identity and time is simple. You + * will need to do this to specify who created a commit and when. Default + * values for the name and email should be found in the `user.name` and + * `user.email` configuration options. See the `config` section of this + * example file to see how to access config values. + */ + git_signature_new((git_signature **)&author, + "Scott Chacon", "schacon@gmail.com", 123456789, 60); + git_signature_new((git_signature **)&cmtter, + "Scott A Chacon", "scott@github.com", 987654321, 90); + + /** + * Commit objects need a tree to point to and optionally one or more + * parents. Here we're creating oid objects to create the commit with, + * but you can also use + */ + git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1"); + git_tree_lookup(&tree, repo, &tree_id); + git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + git_commit_lookup(&parent, repo, &parent_id); + + /** + * Here we actually create the commit object with a single call with all + * the values we need to create the commit. The SHA key is written to the + * `commit_id` variable here. + */ + git_commit_create_v( + &commit_id, /* out id */ + repo, + NULL, /* do not update the HEAD */ + author, + cmtter, + NULL, /* use default message encoding */ + "example commit", + tree, + 1, parent); + + /** + * Now we can take a look at the commit SHA we've generated. + */ + git_oid_fmt(oid_hex, &commit_id); + printf("New Commit: %s\n", oid_hex); +} + /** * ### Object Parsing * From 29d9afc0fb2d673d41bf27f57ac205299c393aeb Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 10:06:17 +0200 Subject: [PATCH 008/142] examples: general: extract function demonstrating ODB --- examples/general.c | 146 ++++++++++++++++++++++++++------------------- 1 file changed, 86 insertions(+), 60 deletions(-) diff --git a/examples/general.c b/examples/general.c index 56fa11bb2..685dce01c 100644 --- a/examples/general.c +++ b/examples/general.c @@ -43,6 +43,7 @@ #include #include +static void object_database(git_repository *repo, git_oid *oid); static void commit_writing(git_repository *repo); static void commit_parsing(git_repository *repo); static void tag_parsing(git_repository *repo); @@ -116,66 +117,7 @@ int main (int argc, char** argv) git_oid_fmt(out, &oid); printf("SHA hex string: %s\n", out); - // ### Working with the Object Database - - // **libgit2** provides [direct access][odb] to the object database. The - // object database is where the actual objects are stored in Git. For - // working with raw objects, we'll need to get this structure from the - // repository. - // - // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb - git_odb *odb; - git_repository_odb(&odb, repo); - - // #### Raw Object Reading - - printf("\n*Raw Object Read*\n"); - git_odb_object *obj; - git_otype otype; - const unsigned char *data; - const char *str_type; - - // We can read raw objects directly from the object database if we have - // the oid (SHA) of the object. This allows us to access objects without - // knowing their type and inspect the raw bytes unparsed. - error = git_odb_read(&obj, odb, &oid); - check_error(error, "finding object in repository"); - - // A raw object only has three properties - the type (commit, blob, tree - // or tag), the size of the raw data and the raw, unparsed data itself. - // For a commit or tag, that raw data is human readable plain ASCII - // text. For a blob it is just file contents, so it could be text or - // binary data. For a tree it is a special binary format, so it's unlikely - // to be hugely helpful as a raw object. - data = (const unsigned char *)git_odb_object_data(obj); - otype = git_odb_object_type(obj); - - // We provide methods to convert from the object type which is an enum, to - // a string representation of that value (and vice-versa). - str_type = git_object_type2string(otype); - printf("object length and type: %d, %s\n", - (int)git_odb_object_size(obj), - str_type); - - // For proper memory management, close the object when you are done with - // it or it will leak memory. - git_odb_object_free(obj); - - // #### Raw Object Writing - - printf("\n*Raw Object Write*\n"); - - // You can also write raw object data to Git. This is pretty cool because - // it gives you direct access to the key/value properties of Git. Here - // we'll write a new blob object that just contains a simple string. - // Notice that we have to specify the object type as the `git_otype` enum. - git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB); - - // Now that we've written the object, we can check out what SHA1 was - // generated when the object was written to our database. - git_oid_fmt(out, &oid); - printf("Written Object: %s\n", out); - + object_database(repo, &oid); commit_writing(repo); commit_parsing(repo); tag_parsing(repo); @@ -192,6 +134,90 @@ int main (int argc, char** argv) return 0; } +/** + * ### Working with the Object Database + * + * **libgit2** provides [direct access][odb] to the object database. The + * object database is where the actual objects are stored in Git. For + * working with raw objects, we'll need to get this structure from the + * repository. + * + * [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb + */ +static void object_database(git_repository *repo, git_oid *oid) +{ + char oid_hex[GIT_OID_HEXSZ+1] = { 0 }; + const unsigned char *data; + const char *str_type; + int error; + git_odb_object *obj; + git_odb *odb; + git_otype otype; + + git_repository_odb(&odb, repo); + + /** + * #### Raw Object Reading + */ + + printf("\n*Raw Object Read*\n"); + + /** + * We can read raw objects directly from the object database if we have + * the oid (SHA) of the object. This allows us to access objects without + * knowing their type and inspect the raw bytes unparsed. + */ + error = git_odb_read(&obj, odb, oid); + check_error(error, "finding object in repository"); + + /** + * A raw object only has three properties - the type (commit, blob, tree + * or tag), the size of the raw data and the raw, unparsed data itself. + * For a commit or tag, that raw data is human readable plain ASCII + * text. For a blob it is just file contents, so it could be text or + * binary data. For a tree it is a special binary format, so it's unlikely + * to be hugely helpful as a raw object. + */ + data = (const unsigned char *)git_odb_object_data(obj); + otype = git_odb_object_type(obj); + + /** + * We provide methods to convert from the object type which is an enum, to + * a string representation of that value (and vice-versa). + */ + str_type = git_object_type2string(otype); + printf("object length and type: %d, %s\n", + (int)git_odb_object_size(obj), + str_type); + + /** + * For proper memory management, close the object when you are done with + * it or it will leak memory. + */ + git_odb_object_free(obj); + + /** + * #### Raw Object Writing + */ + + printf("\n*Raw Object Write*\n"); + + /** + * You can also write raw object data to Git. This is pretty cool because + * it gives you direct access to the key/value properties of Git. Here + * we'll write a new blob object that just contains a simple string. + * Notice that we have to specify the object type as the `git_otype` enum. + */ + git_odb_write(oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB); + + /** + * Now that we've written the object, we can check out what SHA1 was + * generated when the object was written to our database. + */ + git_oid_fmt(oid_hex, oid); + printf("Written Object: %s\n", oid_hex); +} + /** * #### Writing Commits * From c313e3d98635c958df947c20fe902122a7b601f6 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 1 Sep 2016 12:44:08 +0200 Subject: [PATCH 009/142] examples: general: extract function demonstrating OID parsing --- examples/general.c | 70 +++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/examples/general.c b/examples/general.c index 685dce01c..d58e93fa2 100644 --- a/examples/general.c +++ b/examples/general.c @@ -43,6 +43,7 @@ #include #include +static void oid_parsing(git_oid *out); static void object_database(git_repository *repo, git_oid *oid); static void commit_writing(git_repository *repo); static void commit_parsing(git_repository *repo); @@ -71,6 +72,8 @@ static void check_error(int error_code, const char *action) int main (int argc, char** argv) { + git_oid oid; + // Initialize the library, this will set up any global state which libgit2 needs // including threading and crypto git_libgit2_init(); @@ -91,32 +94,7 @@ int main (int argc, char** argv) error = git_repository_open(&repo, repo_path); check_error(error, "opening repository"); - // ### SHA-1 Value Conversions - - // For our first example, we will convert a 40 character hex value to the - // 20 byte raw SHA1 value. - printf("*Hex to Raw*\n"); - char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"; - - // The `git_oid` is the structure that keeps the SHA value. We will use - // this throughout the example for storing the value of the current SHA - // key we're working with. - git_oid oid; - git_oid_fromstr(&oid, hex); - - // Once we've converted the string into the oid value, we can get the raw - // value of the SHA by accessing `oid.id` - - // Next we will convert the 20 byte raw SHA1 value to a human readable 40 - // char hex value. - printf("\n*Raw to Hex*\n"); - char out[GIT_OID_HEXSZ+1]; - out[GIT_OID_HEXSZ] = '\0'; - - // If you have a oid, you can easily get the hex value of the SHA as well. - git_oid_fmt(out, &oid); - printf("SHA hex string: %s\n", out); - + oid_parsing(&oid); object_database(repo, &oid); commit_writing(repo); commit_parsing(repo); @@ -134,6 +112,46 @@ int main (int argc, char** argv) return 0; } +/** + * ### SHA-1 Value Conversions + */ +static void oid_parsing(git_oid *oid) +{ + char out[GIT_OID_HEXSZ+1]; + char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"; + + printf("*Hex to Raw*\n"); + + /** + * For our first example, we will convert a 40 character hex value to the + * 20 byte raw SHA1 value. + * + * The `git_oid` is the structure that keeps the SHA value. We will use + * this throughout the example for storing the value of the current SHA + * key we're working with. + */ + git_oid_fromstr(oid, hex); + + // Once we've converted the string into the oid value, we can get the raw + // value of the SHA by accessing `oid.id` + + // Next we will convert the 20 byte raw SHA1 value to a human readable 40 + // char hex value. + printf("\n*Raw to Hex*\n"); + out[GIT_OID_HEXSZ] = '\0'; + + /** + * If you have a oid, you can easily get the hex value of the SHA as well. + */ + git_oid_fmt(out, oid); + + /** + * If you have a oid, you can easily get the hex value of the SHA as well. + */ + git_oid_fmt(out, oid); + printf("SHA hex string: %s\n", out); +} + /** * ### Working with the Object Database * From 662eee154192e938ffa9402b5e97a93db1b86b8f Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 10:09:52 +0200 Subject: [PATCH 010/142] examples: general: convert C99 comments to C90 comments --- examples/general.c | 94 ++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/examples/general.c b/examples/general.c index d58e93fa2..8f4f74ac9 100644 --- a/examples/general.c +++ b/examples/general.c @@ -12,33 +12,37 @@ * . */ -// [**libgit2**][lg] is a portable, pure C implementation of the Git core -// methods provided as a re-entrant linkable library with a solid API, -// allowing you to write native speed custom Git applications in any -// language which supports C bindings. -// -// This file is an example of using that API in a real, compilable C file. -// As the API is updated, this file will be updated to demonstrate the new -// functionality. -// -// If you're trying to write something in C using [libgit2][lg], you should -// also check out the generated [API documentation][ap]. We try to link to -// the relevant sections of the API docs in each section in this file. -// -// **libgit2** (for the most part) only implements the core plumbing -// functions, not really the higher level porcelain stuff. For a primer on -// Git Internals that you will need to know to work with Git at this level, -// check out [Chapter 10][pg] of the Pro Git book. -// -// [lg]: http://libgit2.github.com -// [ap]: http://libgit2.github.com/libgit2 -// [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain +/** + * [**libgit2**][lg] is a portable, pure C implementation of the Git core + * methods provided as a re-entrant linkable library with a solid API, + * allowing you to write native speed custom Git applications in any + * language which supports C bindings. + * + * This file is an example of using that API in a real, compilable C file. + * As the API is updated, this file will be updated to demonstrate the new + * functionality. + * + * If you're trying to write something in C using [libgit2][lg], you should + * also check out the generated [API documentation][ap]. We try to link to + * the relevant sections of the API docs in each section in this file. + * + * **libgit2** (for the most part) only implements the core plumbing + * functions, not really the higher level porcelain stuff. For a primer on + * Git Internals that you will need to know to work with Git at this level, + * check out [Chapter 10][pg] of the Pro Git book. + * + * [lg]: http://libgit2.github.com + * [ap]: http://libgit2.github.com/libgit2 + * [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain + */ -// ### Includes - -// Including the `git2.h` header will include all the other libgit2 headers -// that you need. It should be the only thing you need to include in order -// to compile properly and get all the libgit2 API. +/** + * ### Includes + * + * Including the `git2.h` header will include all the other libgit2 headers + * that you need. It should be the only thing you need to include in order + * to compile properly and get all the libgit2 API. + */ #include #include #include @@ -55,9 +59,11 @@ static void index_walking(git_repository *repo); static void reference_listing(git_repository *repo); static void config_files(const char *repo_path); -// Almost all libgit2 functions return 0 on success or negative on error. -// This is not production quality error checking, but should be sufficient -// as an example. +/** + * Almost all libgit2 functions return 0 on success or negative on error. + * This is not production quality error checking, but should be sufficient + * as an example. + */ static void check_error(int error_code, const char *action) { const git_error *error = giterr_last(); @@ -74,19 +80,23 @@ int main (int argc, char** argv) { git_oid oid; - // Initialize the library, this will set up any global state which libgit2 needs - // including threading and crypto + /** + * Initialize the library, this will set up any global state which libgit2 needs + * including threading and crypto + */ git_libgit2_init(); - // ### Opening the Repository - - // There are a couple of methods for opening a repository, this being the - // simplest. There are also [methods][me] for specifying the index file - // and work tree locations, here we assume they are in the normal places. - // - // (Try running this program against tests/resources/testrepo.git.) - // - // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository + /** + * ### Opening the Repository + * + * There are a couple of methods for opening a repository, this being the + * simplest. There are also [methods][me] for specifying the index file + * and work tree locations, here we assume they are in the normal places. + * + * (Try running this program against tests/resources/testrepo.git.) + * + * [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository + */ int error; const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git"; git_repository *repo; @@ -106,7 +116,9 @@ int main (int argc, char** argv) reference_listing(repo); config_files(repo_path); - // Finally, when you're done with the repository, you can free it as well. + /** + * Finally, when you're done with the repository, you can free it as well. + */ git_repository_free(repo); return 0; From e2d1b7ecbf2896b23459bee4b8c3dfe94c091ad5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 10:46:35 +0200 Subject: [PATCH 011/142] examples: general: fix remaining warnings --- examples/general.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/general.c b/examples/general.c index 8f4f74ac9..7c05d8712 100644 --- a/examples/general.c +++ b/examples/general.c @@ -78,7 +78,10 @@ static void check_error(int error_code, const char *action) int main (int argc, char** argv) { + int error; git_oid oid; + char *repo_path; + git_repository *repo; /** * Initialize the library, this will set up any global state which libgit2 needs @@ -97,9 +100,7 @@ int main (int argc, char** argv) * * [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository */ - int error; - const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git"; - git_repository *repo; + repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git"; error = git_repository_open(&repo, repo_path); check_error(error, "opening repository"); @@ -216,9 +217,9 @@ static void object_database(git_repository *repo, git_oid *oid) * a string representation of that value (and vice-versa). */ str_type = git_object_type2string(otype); - printf("object length and type: %d, %s\n", + printf("object length and type: %d, %s\nobject data: %s\n", (int)git_odb_object_size(obj), - str_type); + str_type, data); /** * For proper memory management, close the object when you are done with @@ -339,7 +340,7 @@ static void commit_parsing(git_repository *repo) const char *message; unsigned int parents, p; int error; - time_t ctime; + time_t time; printf("\n*Commit Parsing*\n"); @@ -357,14 +358,17 @@ static void commit_parsing(git_repository *repo) message = git_commit_message(commit); author = git_commit_author(commit); cmtter = git_commit_committer(commit); - ctime = git_commit_time(commit); + time = git_commit_time(commit); /** * The author and committer methods return [git_signature] structures, * which give you name, email and `when`, which is a `git_time` structure, * giving you a timestamp and timezone offset. */ - printf("Author: %s (%s)\n", author->name, author->email); + printf("Author: %s (%s)\nCommitter: %s (%s)\nDate: %s\nMessage: %s\n", + author->name, author->email, + cmtter->name, cmtter->email, + ctime(&time), message); /** * Commits can have zero or more parents. The first (root) commit will @@ -424,7 +428,8 @@ static void tag_parsing(git_repository *repo) name = git_tag_name(tag); /* "test" */ type = git_tag_target_type(tag); /* GIT_OBJ_COMMIT (otype enum) */ message = git_tag_message(tag); /* "tag message\n" */ - printf("Tag Message: %s\n", message); + printf("Tag Name: %s\nTag Type: %s\nTag Message: %s\n", + name, git_object_type2string(type), message); git_commit_free(commit); } From 7314da105532306dad21843c8997b746b02fa3ed Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 10:55:28 +0200 Subject: [PATCH 012/142] examples: fix warnings in network/fetch.c --- examples/network/fetch.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/network/fetch.c b/examples/network/fetch.c index 177359b88..10974a9f1 100644 --- a/examples/network/fetch.c +++ b/examples/network/fetch.c @@ -55,6 +55,8 @@ static int update_cb(const char *refname, const git_oid *a, const git_oid *b, vo */ static int transfer_progress_cb(const git_transfer_progress *stats, void *payload) { + (void)payload; + if (stats->received_objects == stats->total_objects) { printf("Resolving deltas %d/%d\r", stats->indexed_deltas, stats->total_deltas); @@ -71,7 +73,6 @@ int fetch(git_repository *repo, int argc, char **argv) { git_remote *remote = NULL; const git_transfer_progress *stats; - struct dl_data data; git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT; if (argc < 2) { @@ -79,14 +80,13 @@ int fetch(git_repository *repo, int argc, char **argv) return EXIT_FAILURE; } - // Figure out whether it's a named remote or a URL + /* Figure out whether it's a named remote or a URL */ printf("Fetching %s for repo %p\n", argv[1], repo); - if (git_remote_lookup(&remote, repo, argv[1]) < 0) { + if (git_remote_lookup(&remote, repo, argv[1]) < 0) if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0) - return -1; - } + goto on_error; - // Set up the callbacks (only update_tips for now) + /* Set up the callbacks (only update_tips for now) */ fetch_opts.callbacks.update_tips = &update_cb; fetch_opts.callbacks.sideband_progress = &progress_cb; fetch_opts.callbacks.transfer_progress = transfer_progress_cb; @@ -98,7 +98,7 @@ int fetch(git_repository *repo, int argc, char **argv) * "fetch". */ if (git_remote_fetch(remote, NULL, &fetch_opts, "fetch") < 0) - return -1; + goto on_error; /** * If there are local objects (we got a thin pack), then tell From 5c2a8361d779477b4d3474c660157bd62e750fe2 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 11:01:09 +0200 Subject: [PATCH 013/142] examples: diff: parse correct types for line-diffopts --- examples/common.c | 19 +++++++++++++++++++ examples/common.h | 9 +++++++++ examples/diff.c | 6 +++--- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/examples/common.c b/examples/common.c index 0f25f3787..96f5eaa8e 100644 --- a/examples/common.c +++ b/examples/common.c @@ -146,6 +146,25 @@ int match_uint16_arg( return 1; } +int match_uint32_arg( + uint32_t *out, struct args_info *args, const char *opt) +{ + const char *found = match_numeric_arg(args, opt); + uint16_t val; + char *endptr = NULL; + + if (!found) + return 0; + + val = (uint32_t)strtoul(found, &endptr, 0); + if (!endptr || *endptr != '\0') + fatal("expected number after argument", opt); + + if (out) + *out = val; + return 1; +} + static int match_int_internal( int *out, const char *str, int allow_negative, const char *opt) { diff --git a/examples/common.h b/examples/common.h index b9fa37ce9..adea0d318 100644 --- a/examples/common.h +++ b/examples/common.h @@ -72,6 +72,15 @@ extern int match_str_arg( extern int match_uint16_arg( uint16_t *out, struct args_info *args, const char *opt); +/** + * Check current `args` entry against `opt` string parsing as uint32. If + * `opt` matches exactly, take the next arg as a uint16_t value; if `opt` + * is a prefix (equal sign optional), take the remainder of the arg as a + * uint32_t value; otherwise return 0. + */ +extern int match_uint32_arg( + uint32_t *out, struct args_info *args, const char *opt); + /** * Check current `args` entry against `opt` string parsing as int. If * `opt` matches exactly, take the next arg as an int value; if it matches diff --git a/examples/diff.c b/examples/diff.c index b69cb2218..9a4f7a59f 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -293,11 +293,11 @@ static void parse_opts(struct opts *o, int argc, char *argv[]) else if (is_prefixed(a, "-B") || is_prefixed(a, "--break-rewrites")) /* TODO: parse thresholds */ o->findopts.flags |= GIT_DIFF_FIND_REWRITES; - else if (!match_uint16_arg( + else if (!match_uint32_arg( &o->diffopts.context_lines, &args, "-U") && - !match_uint16_arg( + !match_uint32_arg( &o->diffopts.context_lines, &args, "--unified") && - !match_uint16_arg( + !match_uint32_arg( &o->diffopts.interhunk_lines, &args, "--inter-hunk-context") && !match_uint16_arg( &o->diffopts.id_abbrev, &args, "--abbrev") && From fc29391950df72222be29cb6b510cc119d65e150 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 11:43:10 +0200 Subject: [PATCH 014/142] examples: add: fix type casting warning --- examples/add.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/add.c b/examples/add.c index 0101ab9ae..e5849892e 100644 --- a/examples/add.c +++ b/examples/add.c @@ -75,15 +75,14 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo { struct print_payload p = *(struct print_payload*)(payload); int ret; - git_status_t status; + unsigned status; (void)matched_pathspec; - if (git_status_file((unsigned int*)(&status), p.repo, path)) { - return -1; //abort + if (git_status_file(&status, p.repo, path)) { + return -1; } - if (status & GIT_STATUS_WT_MODIFIED || - status & GIT_STATUS_WT_NEW) { + if (status & GIT_STATUS_WT_MODIFIED || status & GIT_STATUS_WT_NEW) { printf("add '%s'\n", path); ret = 0; } else { From ec3f5a9c20f77ceafe78d104fe195e4f14a1769b Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 16 Aug 2016 11:02:47 +0200 Subject: [PATCH 015/142] script: cibuild: build examples --- script/cibuild.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/cibuild.sh b/script/cibuild.sh index b13ad88c4..403df223e 100755 --- a/script/cibuild.sh +++ b/script/cibuild.sh @@ -20,7 +20,7 @@ java -jar poxyproxy.jar -d --port 8080 --credentials foo:bar & mkdir _build cd _build # shellcheck disable=SC2086 -cmake .. -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS || exit $? +cmake .. -DBUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS || exit $? make -j2 install || exit $? # If this platform doesn't support test execution, bail out now From feb330d50d0fc10aceec6309131e912e152a1027 Mon Sep 17 00:00:00 2001 From: Igor Gnatenko Date: Wed, 12 Oct 2016 12:41:36 +0200 Subject: [PATCH 016/142] add support for OpenSSL 1.1.0 for BIO filter Closes: https://github.com/libgit2/libgit2/issues/3959 Signed-off-by: Igor Gnatenko --- src/openssl_stream.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/openssl_stream.c b/src/openssl_stream.c index b8ab21fef..9c5e0b1e8 100644 --- a/src/openssl_stream.c +++ b/src/openssl_stream.c @@ -156,10 +156,14 @@ int git_openssl_set_locking(void) static int bio_create(BIO *b) { +#if OPENSSL_VERSION_NUMBER < 0x10100000L b->init = 1; b->num = 0; b->ptr = NULL; b->flags = 0; +#else + BIO_set_init(b, 1); +#endif return 1; } @@ -169,23 +173,36 @@ static int bio_destroy(BIO *b) if (!b) return 0; +#if OPENSSL_VERSION_NUMBER < 0x10100000L b->init = 0; b->num = 0; b->ptr = NULL; b->flags = 0; +#else + BIO_set_init(b, 0); + BIO_set_data(b, NULL); +#endif return 1; } static int bio_read(BIO *b, char *buf, int len) { +#if OPENSSL_VERSION_NUMBER < 0x10100000L git_stream *io = (git_stream *) b->ptr; +#else + git_stream *io = (git_stream *) BIO_get_data(b); +#endif return (int) git_stream_read(io, buf, len); } static int bio_write(BIO *b, const char *buf, int len) { +#if OPENSSL_VERSION_NUMBER < 0x10100000L git_stream *io = (git_stream *) b->ptr; +#else + git_stream *io = (git_stream *) BIO_get_data(b); +#endif return (int) git_stream_write(io, buf, len, 0); } @@ -214,6 +231,7 @@ static int bio_puts(BIO *b, const char *str) return bio_write(b, str, strlen(str)); } +#if OPENSSL_VERSION_NUMBER < 0x10100000L static BIO_METHOD git_stream_bio_method = { BIO_TYPE_SOURCE_SINK, "git_stream", @@ -225,6 +243,9 @@ static BIO_METHOD git_stream_bio_method = { bio_create, bio_destroy }; +#else +static BIO_METHOD *git_stream_bio_method = NULL; +#endif static int ssl_set_error(SSL *ssl, int error) { @@ -445,9 +466,25 @@ int openssl_connect(git_stream *stream) st->connected = true; +#if OPENSSL_VERSION_NUMBER < 0x10100000L bio = BIO_new(&git_stream_bio_method); +#else + git_stream_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "git_stream"); + BIO_meth_set_write(git_stream_bio_method, bio_write); + BIO_meth_set_read(git_stream_bio_method, bio_read); + BIO_meth_set_puts(git_stream_bio_method, bio_puts); + BIO_meth_set_gets(git_stream_bio_method, bio_gets); + BIO_meth_set_ctrl(git_stream_bio_method, bio_ctrl); + BIO_meth_set_create(git_stream_bio_method, bio_create); + BIO_meth_set_destroy(git_stream_bio_method, bio_destroy); + bio = BIO_new(git_stream_bio_method); +#endif GITERR_CHECK_ALLOC(bio); +#if OPENSSL_VERSION_NUMBER < 0x10100000L bio->ptr = st->io; +#else + BIO_set_data(bio, st->io); +#endif SSL_set_bio(st->ssl, bio, bio); /* specify the host in case SNI is needed */ From 6d8ecf087c35fa4f129c6e15017aba55ef3b4986 Mon Sep 17 00:00:00 2001 From: Davide Coppola Date: Sun, 16 Oct 2016 00:43:27 +0100 Subject: [PATCH 017/142] patch: minor documentation fix. Fix @return description of git_patch_num_lines_in_hunk. --- include/git2/patch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/git2/patch.h b/include/git2/patch.h index 790cb74fc..4eb9f0263 100644 --- a/include/git2/patch.h +++ b/include/git2/patch.h @@ -191,7 +191,7 @@ GIT_EXTERN(int) git_patch_get_hunk( * * @param patch The git_patch object * @param hunk_idx Index of the hunk - * @return Number of lines in hunk or -1 if invalid hunk index + * @return Number of lines in hunk or GIT_ENOTFOUND if invalid hunk index */ GIT_EXTERN(int) git_patch_num_lines_in_hunk( const git_patch *patch, From 61ad9bcd3893df47e9856892d1fd38c1da514dd7 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 27 Oct 2016 11:26:52 +0200 Subject: [PATCH 018/142] tests: vector: fix memory leak --- tests/core/vector.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/core/vector.c b/tests/core/vector.c index 336254cce..c2e5d3f34 100644 --- a/tests/core/vector.c +++ b/tests/core/vector.c @@ -404,4 +404,6 @@ void test_core_vector__reverse(void) for (i = 0; i < 5; i++) cl_assert_equal_p(out2[i], git_vector_get(&v, i)); + + git_vector_free(&v); } From 30a876cda6800289d206f823d93aae0d2847a467 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 27 Oct 2016 11:29:15 +0200 Subject: [PATCH 019/142] tests: fetchhead: fix memory leak --- tests/online/fetchhead.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/online/fetchhead.c b/tests/online/fetchhead.c index 9aaad253c..c1ac06dbe 100644 --- a/tests/online/fetchhead.c +++ b/tests/online/fetchhead.c @@ -126,6 +126,8 @@ void test_online_fetchhead__explicit_dst_refspec_creates_branch(void) cl_git_pass(git_branch_lookup(&ref, g_repo, "explicit-refspec", GIT_BRANCH_ALL)); cl_assert_equal_i(refs + 1, count_references()); + + git_reference_free(ref); } void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void) From 6c4d2d3ea208bfd0d38d7c5071ea0b398e71c5c8 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 28 Oct 2016 14:45:55 +0200 Subject: [PATCH 020/142] coverity: fix download URL --- script/coverity.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/script/coverity.sh b/script/coverity.sh index 7fe9eb4c7..229a9c370 100755 --- a/script/coverity.sh +++ b/script/coverity.sh @@ -12,12 +12,11 @@ then exit 0 fi -COV_VERSION=6.6.1 case $(uname -m) in i?86) BITS=32 ;; amd64|x86_64) BITS=64 ;; esac -SCAN_TOOL=https://scan.coverity.com/download/linux-${BITS} +SCAN_TOOL=https://scan.coverity.com/download/cxx/linux${BITS} TOOL_BASE=$(pwd)/_coverity-scan # Install coverity tools From 561276eed6be2c763af23ae3b034a1f194b03d0b Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 28 Oct 2016 14:48:30 +0200 Subject: [PATCH 021/142] coverity: only analyze the master branch of the main repository We used to only execute Coverity analysis on the 'development' branch before commit 998f001 (Refine build limitation, 2014-01-15), which refined Coverity build limitations. While we do not really use the 'development' branch anymore, it does still make sense to only analyze a single branch, as otherwise Coverity might get confused. Re-establish the restriction such that we only analyze libgit2's 'master' branch. Also fix the message announcing why we do not actually analyze a certain build. --- script/coverity.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/script/coverity.sh b/script/coverity.sh index 229a9c370..9021b9e7c 100755 --- a/script/coverity.sh +++ b/script/coverity.sh @@ -5,10 +5,10 @@ set -e [ -z "$COVERITY_TOKEN" ] && echo "Need to set a coverity token" && exit 1 # Only run this on our branches -echo "Pull request: $TRAVIS_PULL_REQUEST | Slug: $TRAVIS_REPO_SLUG" -if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_REPO_SLUG" != "libgit2/libgit2" ]; +echo "Branch: $TRAVIS_BRANCH | Pull request: $TRAVIS_PULL_REQUEST | Slug: $TRAVIS_REPO_SLUG" +if [ "$TRAVIS_BRANCH" != "master" -o "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_REPO_SLUG" != "libgit2/libgit2" ]; then - echo "Only analyzing 'development' on the main repo." + echo "Only analyzing the 'master' brach of the main repository." exit 0 fi From 95fa38802f12b930b3acf3fe842408bb33eb1d18 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 28 Oct 2016 16:07:40 +0200 Subject: [PATCH 022/142] pqueue: resolve possible NULL pointer dereference The `git_pqueue` struct allows being fixed in its total number of entries. In this case, we simply throw away items that are inserted into the priority queue by examining wether the new item to be inserted has a higher priority than the previous smallest one. This feature somewhat contradicts our pqueue implementation in that it is allowed to not have a comparison function. In fact, we also fail to check if the comparison function is actually set in the case where we add a new item into a fully filled fixed-size pqueue. As we cannot determine which item is the smallest item in absence of a comparison function, we fix the `NULL` pointer dereference by simply dropping all new items which are about to be inserted into a full fixed-size pqueue. --- src/pqueue.c | 5 +++-- tests/core/pqueue.c | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/pqueue.c b/src/pqueue.c index 8cfc4390f..9341d1af3 100644 --- a/src/pqueue.c +++ b/src/pqueue.c @@ -86,8 +86,9 @@ int git_pqueue_insert(git_pqueue *pq, void *item) if ((pq->flags & GIT_PQUEUE_FIXED_SIZE) != 0 && pq->length >= pq->_alloc_size) { - /* skip this item if below min item in heap */ - if (pq->_cmp(item, git_vector_get(pq, 0)) <= 0) + /* skip this item if below min item in heap or if + * we do not have a comparison function */ + if (!pq->_cmp || pq->_cmp(item, git_vector_get(pq, 0)) <= 0) return 0; /* otherwise remove the min item before inserting new */ (void)git_pqueue_pop(pq); diff --git a/tests/core/pqueue.c b/tests/core/pqueue.c index bcd4eea9f..2b90f4172 100644 --- a/tests/core/pqueue.c +++ b/tests/core/pqueue.c @@ -93,7 +93,29 @@ void test_core_pqueue__max_heap_size(void) cl_assert_equal_i(0, git_pqueue_size(&pq)); git_pqueue_free(&pq); +} +void test_core_pqueue__max_heap_size_without_comparison(void) +{ + git_pqueue pq; + int i, vals[100] = { 0 }; + + cl_git_pass(git_pqueue_init(&pq, GIT_PQUEUE_FIXED_SIZE, 50, NULL)); + + for (i = 0; i < 100; ++i) + cl_git_pass(git_pqueue_insert(&pq, &vals[i])); + + cl_assert_equal_i(50, git_pqueue_size(&pq)); + + /* As we have no comparison function, we cannot make any + * actual assumptions about which entries are part of the + * pqueue */ + for (i = 0; i < 50; ++i) + cl_assert(git_pqueue_pop(&pq)); + + cl_assert_equal_i(0, git_pqueue_size(&pq)); + + git_pqueue_free(&pq); } static int cmp_ints_like_commit_time(const void *a, const void *b) From ea9ea6ac4a894ff76e22cb531d340b14ec62c56b Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 31 Oct 2016 13:49:52 +0100 Subject: [PATCH 023/142] Documentation: fix small typos --- PROJECTS.md | 2 +- THREADING.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/PROJECTS.md b/PROJECTS.md index 87ce78f02..a6885e38b 100644 --- a/PROJECTS.md +++ b/PROJECTS.md @@ -5,7 +5,7 @@ So, you want to start helping out with `libgit2`? That's fantastic! We welcome contributions and we promise we'll try to be nice. This is a list of libgit2 related projects that new contributors can take -on. It includes a number of good starter projects and well as some larger +on. It includes a number of good starter projects as well as some larger ideas that no one is actively working on. ## Before You Start diff --git a/THREADING.md b/THREADING.md index 0b9e50286..e9be8b99f 100644 --- a/THREADING.md +++ b/THREADING.md @@ -88,7 +88,7 @@ of libssh2 as described above, `git_openssl_set_locking()` is a no-op. If your programming language offers a package/bindings for OpenSSL, you should very strongly prefer to use that in order to set up -locking, as they provide a level of coördination which is impossible +locking, as they provide a level of coordination which is impossible when using this function. See the @@ -102,7 +102,7 @@ if there are alternatives provided by the system. libssh2 may be linked against OpenSSL or libgcrypt. If it uses OpenSSL, see the above paragraphs. If it uses libgcrypt, then you need to set up its locking before using it multi-threaded. libgit2 has no -direct connection to libgcrypt and thus has not convenience functions for +direct connection to libgcrypt and thus has no convenience functions for it (but libgcrypt has macros). Read libgcrypt's [threading documentation for more information](http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html) From 59665db3b389bf32e76b4ef070428d5400aa0d07 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 31 Oct 2016 13:50:13 +0100 Subject: [PATCH 024/142] PROJECTS: consistently quote directories --- PROJECTS.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PROJECTS.md b/PROJECTS.md index a6885e38b..f53a2e120 100644 --- a/PROJECTS.md +++ b/PROJECTS.md @@ -70,11 +70,11 @@ some incremental steps listed towards the larger goal. Those steps might make good smaller projects by themselves. * Port part of the Git test suite to run against the command line emulation - in examples/ - * Pick a Git command that is emulated in our examples/ area + in `examples/` + * Pick a Git command that is emulated in our `examples/` area * Extract the Git tests that exercise that command * Convert the tests to call our emulation - * These tests could go in examples/tests/... + * These tests could go in `examples/tests/`... * Add hooks API to enumerate and manage hooks (not run them at this point) * Enumeration of available hooks * Lookup API to see which hooks have a script and get the script From dc98cb28db852da5cc2820ff502f86fd69977b6b Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 31 Oct 2016 13:50:23 +0100 Subject: [PATCH 025/142] openssl_stream: fix typo --- src/openssl_stream.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openssl_stream.c b/src/openssl_stream.c index b8ab21fef..cab7d9457 100644 --- a/src/openssl_stream.c +++ b/src/openssl_stream.c @@ -148,7 +148,7 @@ int git_openssl_set_locking(void) git__on_shutdown(shutdown_ssl_locking); return 0; #else - giterr_set(GITERR_THREAD, "libgit2 as not built with threads"); + giterr_set(GITERR_THREAD, "libgit2 was not built with threads"); return -1; #endif } From 18c18e3df86850f794dd2fe562ea03a2beab244e Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 31 Oct 2016 15:55:46 +0100 Subject: [PATCH 026/142] coverity: check for Coverity token only if necessary When running a Coverity build, we have to provide an authentication token in order to proof that we are actually allowed to run analysis in the name of a certain project. As this token should be secret, it is only set on the main repository, so when we were requested to run the Coverity script on another repository we do error out. But in fact we do also error out if the Coverity analysis should _not_ be run if there is no authentication token provided. Fix the issue by only checking for the authentication token after determining if analysis is indeed requested. --- script/coverity.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/script/coverity.sh b/script/coverity.sh index 9021b9e7c..5fe16c031 100755 --- a/script/coverity.sh +++ b/script/coverity.sh @@ -1,9 +1,6 @@ #!/bin/bash set -e -# Environment check -[ -z "$COVERITY_TOKEN" ] && echo "Need to set a coverity token" && exit 1 - # Only run this on our branches echo "Branch: $TRAVIS_BRANCH | Pull request: $TRAVIS_PULL_REQUEST | Slug: $TRAVIS_REPO_SLUG" if [ "$TRAVIS_BRANCH" != "master" -o "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_REPO_SLUG" != "libgit2/libgit2" ]; @@ -12,6 +9,9 @@ then exit 0 fi +# Environment check +[ -z "$COVERITY_TOKEN" ] && echo "Need to set a coverity token" && exit 1 + case $(uname -m) in i?86) BITS=32 ;; amd64|x86_64) BITS=64 ;; From 0334bf4b24401c73cb9c9e2211bc4858e61b5934 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 28 Oct 2016 14:57:54 +0200 Subject: [PATCH 027/142] travis: do not allow valgrind failures Our valgrind jobs haven't been failing for several builds by now. This indicates that our tests are sufficiently stable when running under valgrind. As such, any failures reported by valgrind become interesting to us and shouldn't be ignored when causing a build to fail. Remove the valgrind job from the list of allowed failures. --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index bfc0fac48..af38252ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,9 +47,6 @@ matrix: os: linux allow_failures: - env: COVERITY=1 - - env: - - VALGRIND=1 - OPTIONS="-DBUILD_CLAR=ON -DBUILD_EXAMPLES=OFF -DDEBUG_POOL=ON -DCMAKE_BUILD_TYPE=Debug" install: - if [ "$TRAVIS_OS_NAME" = "osx" ]; then ./script/install-deps-${TRAVIS_OS_NAME}.sh; fi From 59c6c2860a573521a96a402f8232127ceb27b0f6 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 27 Oct 2016 12:31:17 +0200 Subject: [PATCH 028/142] global: synchronize initialization and shutdown with pthreads When trying to initialize and tear down global data structures from different threads at once with `git_libgit2_init` and `git_libgit2_shutdown`, we race around initializing data. While we use `pthread_once` to assert that we only initilize data a single time, we actually reset the `pthread_once_t` on the last call to `git_libgit2_shutdown`. As resetting this variable is not synchronized with other threads trying to access it, this is actually racy when one thread tries to do a complete shutdown of libgit2 while another thread tries to initialize it. Fix the issue by creating a mutex which synchronizes `init_once` and the library shutdown. --- src/global.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/global.c b/src/global.c index 45b1ab8f6..de722c7e8 100644 --- a/src/global.c +++ b/src/global.c @@ -247,6 +247,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved) #elif defined(GIT_THREADS) && defined(_POSIX_THREADS) static pthread_key_t _tls_key; +static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_once_t _once_init = PTHREAD_ONCE_INIT; int init_error = 0; @@ -268,12 +269,19 @@ static void init_once(void) int git_libgit2_init(void) { - int ret; + int ret, err; ret = git_atomic_inc(&git__n_inits); - pthread_once(&_once_init, init_once); - return init_error ? init_error : ret; + if ((err = pthread_mutex_lock(&_init_mutex)) != 0) + return err; + err = pthread_once(&_once_init, init_once); + err |= pthread_mutex_unlock(&_init_mutex); + + if (err || init_error) + return err | init_error; + + return ret; } int git_libgit2_shutdown(void) @@ -285,6 +293,9 @@ int git_libgit2_shutdown(void) if ((ret = git_atomic_dec(&git__n_inits)) != 0) return ret; + if ((ret = pthread_mutex_lock(&_init_mutex)) != 0) + return ret; + /* Shut down any subsystems that have global state */ shutdown_common(); @@ -298,6 +309,9 @@ int git_libgit2_shutdown(void) git_mutex_free(&git__mwindow_mutex); _once_init = new_once; + if ((ret = pthread_mutex_unlock(&_init_mutex)) != 0) + return ret; + return 0; } From 6502398f9643442f28a91ecad0a3695bb9ea5ec0 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 1 Nov 2016 16:55:16 +0100 Subject: [PATCH 029/142] proxy: fix typo in documentation --- include/git2/proxy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/git2/proxy.h b/include/git2/proxy.h index dcd615633..194cbb651 100644 --- a/include/git2/proxy.h +++ b/include/git2/proxy.h @@ -19,7 +19,7 @@ typedef enum { /** * Do not attempt to connect through a proxy * - * If built against lbicurl, it itself may attempt to connect + * If built against libcurl, it itself may attempt to connect * to a proxy if the environment variables specify it. */ GIT_PROXY_NONE, From 8d2784d57069ddc49f99b0cde1fc3a7f83532fd9 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Tue, 1 Nov 2016 17:46:20 +0100 Subject: [PATCH 030/142] describe: fix documentation --- include/git2/describe.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/git2/describe.h b/include/git2/describe.h index 3044d9165..971eb350c 100644 --- a/include/git2/describe.h +++ b/include/git2/describe.h @@ -44,8 +44,8 @@ typedef enum { typedef struct git_describe_options { unsigned int version; - unsigned int max_candidates_tags; /** default: 10 */ - unsigned int describe_strategy; /** default: GIT_DESCRIBE_DEFAULT */ + unsigned int max_candidates_tags; /**< default: 10 */ + unsigned int describe_strategy; /**< default: GIT_DESCRIBE_DEFAULT */ const char *pattern; /** * When calculating the distance from the matching tag or @@ -105,6 +105,9 @@ typedef struct { GIT_EXTERN(int) git_describe_init_format_options(git_describe_format_options *opts, unsigned int version); +/** + * A struct that stores the result of a describe operation. + */ typedef struct git_describe_result git_describe_result; /** From 2adac91008047359b6238237a4795275a40fb6ed Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Tue, 1 Nov 2016 17:46:37 +0100 Subject: [PATCH 031/142] remote: unused function typedef --- include/git2/remote.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/git2/remote.h b/include/git2/remote.h index c459f42cc..c77288b1c 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -26,8 +26,6 @@ */ GIT_BEGIN_DECL -typedef int (*git_remote_rename_problem_cb)(const char *problematic_refspec, void *payload); - /** * Add a remote with the default fetch refspec to the repository's configuration. * From 788fcdb8e3067bfcd1f860db96cb9d5003680df3 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Tue, 1 Nov 2016 17:46:57 +0100 Subject: [PATCH 032/142] remote: fix documentation and indent --- include/git2/remote.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/git2/remote.h b/include/git2/remote.h index c77288b1c..370abf117 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -358,6 +358,8 @@ typedef struct { } git_push_update; /** + * Callback used to inform of upcoming updates. + * * @param updates an array containing the updates which will be sent * as commands to the destination. * @param len number of elements in `updates` @@ -401,7 +403,7 @@ struct git_remote_callbacks { * connection to proceed. Returns 1 to allow the connection, 0 * to disallow it or a negative value to indicate an error. */ - git_transport_certificate_check_cb certificate_check; + git_transport_certificate_check_cb certificate_check; /** * During the download of new data, this will be regularly From 8d400c096ad059c2704e2804e202f6f9e69e5d51 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Tue, 1 Nov 2016 17:49:07 +0100 Subject: [PATCH 033/142] transaction: fix documentation --- include/git2/transaction.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/git2/transaction.h b/include/git2/transaction.h index 64abb0c69..00ca13993 100644 --- a/include/git2/transaction.h +++ b/include/git2/transaction.h @@ -8,6 +8,14 @@ #define INCLUDE_git_transaction_h__ #include "common.h" + +/** + * @file git2/transaction.h + * @brief Git transactional reference routines + * @defgroup git_transaction Git transactional reference routines + * @ingroup Git + * @{ + */ GIT_BEGIN_DECL /** @@ -107,5 +115,6 @@ GIT_EXTERN(int) git_transaction_commit(git_transaction *tx); */ GIT_EXTERN(void) git_transaction_free(git_transaction *tx); +/** @} */ GIT_END_DECL #endif From 038f0e1b4cf6333d9835776b72e6bf8fe4975de5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Wed, 2 Nov 2016 08:49:24 +0100 Subject: [PATCH 034/142] global: reset global state on shutdown without threading When threading is not enabled for libgit2, we keep global state in a simple static variable. When libgit2 is shut down, we clean up the global state by freeing the global state's dynamically allocated memory. When libgit2 is built with threading, we additionally free the thread-local storage and thus completely remove the global state. In a non-threaded build, though, we simply leave the global state as-is, which may result in an error upon reinitializing libgit2. Fix the issue by zeroing out the variable on a shutdown, thus returning it to its initial state. --- src/global.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/global.c b/src/global.c index de722c7e8..e2ad8fe71 100644 --- a/src/global.c +++ b/src/global.c @@ -341,7 +341,7 @@ int git_libgit2_init(void) { int ret; - /* Only init SSL the first time */ + /* Only init subsystems the first time */ if ((ret = git_atomic_inc(&git__n_inits)) != 1) return ret; @@ -359,6 +359,7 @@ int git_libgit2_shutdown(void) if ((ret = git_atomic_dec(&git__n_inits)) == 0) { shutdown_common(); git__global_state_cleanup(&__state); + memset(&__state, 0, sizeof(__state)); } return ret; From 1c33ecc4456186f4cc6570876ed5c47031b7ef1b Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 1 Nov 2016 14:30:38 +0100 Subject: [PATCH 035/142] tests: core: test deinitialization and concurrent initialization Exercise the logic surrounding deinitialization of the libgit2 library as well as repeated concurrent de- and reinitialization. This tries to catch races and makes sure that it is possible to reinitialize libgit2 multiple times. After deinitializing libgit2, we have to make sure to setup options required for testing. Currently, this only includes setting up the configuration search path again. Before, this has been set up once in `tests/main.c`. --- tests/core/init.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/core/init.c b/tests/core/init.c index e17b7845f..cd90b378d 100644 --- a/tests/core/init.c +++ b/tests/core/init.c @@ -12,3 +12,43 @@ void test_core_init__returns_count(void) cl_assert_equal_i(1, git_libgit2_shutdown()); } +void test_core_init__reinit_succeeds(void) +{ + cl_assert_equal_i(0, git_libgit2_shutdown()); + cl_assert_equal_i(1, git_libgit2_init()); + cl_sandbox_set_search_path_defaults(); +} + +#ifdef GIT_THREADS +static void *reinit(void *unused) +{ + unsigned i; + + for (i = 0; i < 20; i++) { + cl_assert(git_libgit2_init() > 0); + cl_assert(git_libgit2_shutdown() >= 0); + } + + return unused; +} +#endif + +void test_core_init__concurrent_init_succeeds(void) +{ +#ifdef GIT_THREADS + git_thread threads[10]; + unsigned i; + + cl_assert_equal_i(0, git_libgit2_shutdown()); + + for (i = 0; i < ARRAY_SIZE(threads); i++) + git_thread_create(&threads[i], reinit, NULL); + for (i = 0; i < ARRAY_SIZE(threads); i++) + git_thread_join(&threads[i], NULL); + + cl_assert_equal_i(1, git_libgit2_init()); + cl_sandbox_set_search_path_defaults(); +#else + cl_skip(); +#endif +} From 61530c497dc23f6140557059ca9a55805c21b5fc Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 1 Nov 2016 16:56:07 +0100 Subject: [PATCH 036/142] transports: smart: abort ref announcement on early end of stream When reading a server's reference announcements via the smart protocol, we expect the server to send multiple flushes before the protocol is finished. If we fail to receive new data from the socket, we will only return an end of stream error if we have not seen any flush yet. This logic is flawed in that we may run into an infinite loop when receiving a server's reference announcement with a bogus flush packet. E.g. assume the last flushing package is changed to not be '0000' but instead any other value. In this case, we will still await one more flush package and ignore the fact that we are not receiving any data from the socket, causing an infinite loop. Fix the issue by always returning `GIT_EEOF` if the socket indicates an end of stream. --- src/transports/smart_protocol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c index 3448fa7fb..5db4dda9a 100644 --- a/src/transports/smart_protocol.c +++ b/src/transports/smart_protocol.c @@ -50,7 +50,7 @@ int git_smart__store_refs(transport_smart *t, int flushes) if ((recvd = gitno_recv(buf)) < 0) return recvd; - if (recvd == 0 && !flush) { + if (recvd == 0) { giterr_set(GITERR_NET, "early EOF"); return GIT_EEOF; } From 62494bf234919e04a6e145d59942d2a05c96ae0d Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Wed, 2 Nov 2016 09:38:40 +0100 Subject: [PATCH 037/142] transports: smart: abort receiving packets on end of stream When trying to receive packets from the remote, we loop until either an error distinct to `GIT_EBUFS` occurs or until we successfully parsed the packet. This does not honor the case where we are looping over an already closed socket which has no more data, leaving us in an infinite loop if we got a bogus packet size or if the remote hang up. Fix the issue by returning `GIT_EEOF` when we cannot read data from the socket anymore. --- src/transports/smart_protocol.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c index 5db4dda9a..c1e412436 100644 --- a/src/transports/smart_protocol.c +++ b/src/transports/smart_protocol.c @@ -222,8 +222,12 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf) if (error < 0 && error != GIT_EBUFS) return error; - if ((ret = gitno_recv(buf)) < 0) + if ((ret = gitno_recv(buf)) < 0) { return ret; + } else if (ret == 0) { + giterr_set(GITERR_NET, "early EOF"); + return GIT_EEOF; + } } while (error); gitno_consume(buf, line_end); From 0cf15e39f3d435c6f8d8f52daed2b98c375266a8 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Wed, 2 Nov 2016 12:23:12 +0100 Subject: [PATCH 038/142] pack: fix race in pack_entry_find_offset In `pack_entry_find_offset`, we try to find the offset of a certain object in the pack file. To do so, we first assert if the packfile has already been opened and open it if not. Opening the packfile is guarded with a mutex, so concurrent access to this is in fact safe. What is not thread-safe though is our calculation of offsets inside the packfile. Assume two threads calling `pack_entry_find_offset` at the same time. We first calculate the offset and index location and only then determine if the pack has already been opened. If so, we re-calculate the offset and index address. Now the case for two threads: thread 1 first calculates the addresses and is subsequently suspended. The second thread will now call `pack_index_open` and initialize the pack file, calculating its addresses correctly. When the first thread is resumed now, he'll see that the pack file has already been initialized and will happily proceed with the addresses it has already calculated before the check. As the pack file was not initialized before, these addresses are bogus. Fix the issue by only calculating the addresses after having checked if the pack file is open. --- src/pack.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pack.c b/src/pack.c index 310f00fa3..2ee0c60e4 100644 --- a/src/pack.c +++ b/src/pack.c @@ -1268,8 +1268,8 @@ static int pack_entry_find_offset( const git_oid *short_oid, size_t len) { - const uint32_t *level1_ofs = p->index_map.data; - const unsigned char *index = p->index_map.data; + const uint32_t *level1_ofs; + const unsigned char *index; unsigned hi, lo, stride; int pos, found = 0; git_off_t offset; @@ -1283,11 +1283,11 @@ static int pack_entry_find_offset( if ((error = pack_index_open(p)) < 0) return error; assert(p->index_map.data); - - index = p->index_map.data; - level1_ofs = p->index_map.data; } + index = p->index_map.data; + level1_ofs = p->index_map.data; + if (p->index_version > 1) { level1_ofs += 2; index += 8; From f15eedb3a390dcbe441cd77231c3449ff941d189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Nov 2016 12:28:25 +0100 Subject: [PATCH 039/142] openssl: recreate the OpenSSL 1.1 BIO interface for older versions We want to program against the interface, so recreate it when we compile against pre-1.1 versions. --- src/openssl_stream.c | 92 +++++++++++++++++------------------------ src/openssl_stream.h | 97 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 55 deletions(-) diff --git a/src/openssl_stream.c b/src/openssl_stream.c index 9c5e0b1e8..80c6b9828 100644 --- a/src/openssl_stream.c +++ b/src/openssl_stream.c @@ -13,6 +13,7 @@ #include "posix.h" #include "stream.h" #include "socket_stream.h" +#include "openssl_stream.h" #include "netops.h" #include "git2/transport.h" #include "git2/sys/openssl.h" @@ -71,12 +72,20 @@ static void shutdown_ssl_locking(void) #endif /* GIT_THREADS */ +static BIO_METHOD *git_stream_bio_method; +static int init_bio_method(void); + /** * This function aims to clean-up the SSL context which * we allocated. */ static void shutdown_ssl(void) { + if (git_stream_bio_method) { + BIO_meth_free(git_stream_bio_method); + git_stream_bio_method = NULL; + } + if (git__ssl_ctx) { SSL_CTX_free(git__ssl_ctx); git__ssl_ctx = NULL; @@ -121,6 +130,13 @@ int git_openssl_stream_global_init(void) git__ssl_ctx = NULL; return -1; } + + if (init_bio_method() < 0) { + SSL_CTX_free(git__ssl_ctx); + git__ssl_ctx = NULL; + return -1; + } + #endif git__on_shutdown(shutdown_ssl); @@ -156,14 +172,8 @@ int git_openssl_set_locking(void) static int bio_create(BIO *b) { -#if OPENSSL_VERSION_NUMBER < 0x10100000L - b->init = 1; - b->num = 0; - b->ptr = NULL; - b->flags = 0; -#else BIO_set_init(b, 1); -#endif + BIO_set_data(b, NULL); return 1; } @@ -173,36 +183,22 @@ static int bio_destroy(BIO *b) if (!b) return 0; -#if OPENSSL_VERSION_NUMBER < 0x10100000L - b->init = 0; - b->num = 0; - b->ptr = NULL; - b->flags = 0; -#else - BIO_set_init(b, 0); BIO_set_data(b, NULL); -#endif return 1; } static int bio_read(BIO *b, char *buf, int len) { -#if OPENSSL_VERSION_NUMBER < 0x10100000L - git_stream *io = (git_stream *) b->ptr; -#else git_stream *io = (git_stream *) BIO_get_data(b); -#endif + return (int) git_stream_read(io, buf, len); } static int bio_write(BIO *b, const char *buf, int len) { -#if OPENSSL_VERSION_NUMBER < 0x10100000L - git_stream *io = (git_stream *) b->ptr; -#else git_stream *io = (git_stream *) BIO_get_data(b); -#endif + return (int) git_stream_write(io, buf, len, 0); } @@ -231,21 +227,22 @@ static int bio_puts(BIO *b, const char *str) return bio_write(b, str, strlen(str)); } -#if OPENSSL_VERSION_NUMBER < 0x10100000L -static BIO_METHOD git_stream_bio_method = { - BIO_TYPE_SOURCE_SINK, - "git_stream", - bio_write, - bio_read, - bio_puts, - bio_gets, - bio_ctrl, - bio_create, - bio_destroy -}; -#else -static BIO_METHOD *git_stream_bio_method = NULL; -#endif +static int init_bio_method(void) +{ + /* Set up the BIO_METHOD we use for wrapping our own stream implementations */ + git_stream_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "git_stream"); + GITERR_CHECK_ALLOC(git_stream_bio_method); + + BIO_meth_set_write(git_stream_bio_method, bio_write); + BIO_meth_set_read(git_stream_bio_method, bio_read); + BIO_meth_set_puts(git_stream_bio_method, bio_puts); + BIO_meth_set_gets(git_stream_bio_method, bio_gets); + BIO_meth_set_ctrl(git_stream_bio_method, bio_ctrl); + BIO_meth_set_create(git_stream_bio_method, bio_create); + BIO_meth_set_destroy(git_stream_bio_method, bio_destroy); + + return 0; +} static int ssl_set_error(SSL *ssl, int error) { @@ -466,27 +463,12 @@ int openssl_connect(git_stream *stream) st->connected = true; -#if OPENSSL_VERSION_NUMBER < 0x10100000L - bio = BIO_new(&git_stream_bio_method); -#else - git_stream_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "git_stream"); - BIO_meth_set_write(git_stream_bio_method, bio_write); - BIO_meth_set_read(git_stream_bio_method, bio_read); - BIO_meth_set_puts(git_stream_bio_method, bio_puts); - BIO_meth_set_gets(git_stream_bio_method, bio_gets); - BIO_meth_set_ctrl(git_stream_bio_method, bio_ctrl); - BIO_meth_set_create(git_stream_bio_method, bio_create); - BIO_meth_set_destroy(git_stream_bio_method, bio_destroy); bio = BIO_new(git_stream_bio_method); -#endif GITERR_CHECK_ALLOC(bio); -#if OPENSSL_VERSION_NUMBER < 0x10100000L - bio->ptr = st->io; -#else - BIO_set_data(bio, st->io); -#endif + BIO_set_data(bio, st->io); SSL_set_bio(st->ssl, bio, bio); + /* specify the host in case SNI is needed */ #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME SSL_set_tlsext_host_name(st->ssl, st->host); diff --git a/src/openssl_stream.h b/src/openssl_stream.h index 82b5110c4..509e40428 100644 --- a/src/openssl_stream.h +++ b/src/openssl_stream.h @@ -7,10 +7,107 @@ #ifndef INCLUDE_openssl_stream_h__ #define INCLUDE_openssl_stream_h__ +#include +#include +#include +#include + #include "git2/sys/stream.h" extern int git_openssl_stream_global_init(void); extern int git_openssl_stream_new(git_stream **out, const char *host, const char *port); +/* + * OpenSSL 1.1 made BIO opaque so we have to use functions to interact with it + * which do not exist in previous versions. We define these inline functions so + * we can program against the interface instead of littering the implementation + * with ifdefs. + */ +# if OPENSSL_VERSION_NUMBER < 0x10100000L + +GIT_INLINE(BIO_METHOD*) BIO_meth_new(int type, const char *name) +{ + BIO_METHOD *meth = git__calloc(1, sizeof(BIO_METHOD)); + if (!meth) { + return NULL; + } + + meth->type = type; + meth->name = name; + + return meth; +} + +GIT_INLINE(void) BIO_meth_free(BIO_METHOD *biom) +{ + git__free(biom); +} + +GIT_INLINE(int) BIO_meth_set_write(BIO_METHOD *biom, int (*write) (BIO *, const char *, int)) +{ + biom->bwrite = write; + return 1; +} + +GIT_INLINE(int) BIO_meth_set_read(BIO_METHOD *biom, int (*read) (BIO *, char *, int)) +{ + biom->bread = read; + return 1; +} + +GIT_INLINE(int) BIO_meth_set_puts(BIO_METHOD *biom, int (*puts) (BIO *, const char *)) +{ + biom->bputs = puts; + return 1; +} + +GIT_INLINE(int) BIO_meth_set_gets(BIO_METHOD *biom, int (*gets) (BIO *, char *, int)) + +{ + biom->bgets = gets; + return 1; +} + +GIT_INLINE(int) BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *)) +{ + biom->ctrl = ctrl; + return 1; +} + +GIT_INLINE(int) BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *)) +{ + biom->create = create; + return 1; +} + +GIT_INLINE(int) BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *)) +{ + biom->destroy = destroy; + return 1; +} + +GIT_INLINE(int) BIO_get_new_index(void) +{ + /* This exists as of 1.1 so before we'd just have 0 */ + return 0; +} + +GIT_INLINE(void) BIO_set_init(BIO *b, int init) +{ + b->init = init; +} + +GIT_INLINE(void) BIO_set_data(BIO *a, void *ptr) +{ + a->ptr = ptr; +} + +GIT_INLINE(void*) BIO_get_data(BIO *a) +{ + return a->ptr; +} + +# endif + #endif From 2f3adf9513b1579ae17489d45d032b962bba885a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Nov 2016 12:35:46 +0100 Subject: [PATCH 040/142] openssl: use ASN1_STRING_get0_data when compiling against 1.1 For older versions we can fall back on the deprecated ASN1_STRING_data. --- src/openssl_stream.c | 4 ++-- src/openssl_stream.h | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/openssl_stream.c b/src/openssl_stream.c index 80c6b9828..826c0a537 100644 --- a/src/openssl_stream.c +++ b/src/openssl_stream.c @@ -357,7 +357,7 @@ static int verify_server_cert(SSL *ssl, const char *host) num = sk_GENERAL_NAME_num(alts); for (i = 0; i < num && matched != 1; i++) { const GENERAL_NAME *gn = sk_GENERAL_NAME_value(alts, i); - const char *name = (char *) ASN1_STRING_data(gn->d.ia5); + const char *name = (char *) ASN1_STRING_get0_data(gn->d.ia5); size_t namelen = (size_t) ASN1_STRING_length(gn->d.ia5); /* Skip any names of a type we're not looking for */ @@ -412,7 +412,7 @@ static int verify_server_cert(SSL *ssl, const char *host) if (size > 0) { peer_cn = OPENSSL_malloc(size + 1); GITERR_CHECK_ALLOC(peer_cn); - memcpy(peer_cn, ASN1_STRING_data(str), size); + memcpy(peer_cn, ASN1_STRING_get0_data(str), size); peer_cn[size] = '\0'; } else { goto cert_fail_name; diff --git a/src/openssl_stream.h b/src/openssl_stream.h index 509e40428..e8ce5d91b 100644 --- a/src/openssl_stream.h +++ b/src/openssl_stream.h @@ -108,6 +108,11 @@ GIT_INLINE(void*) BIO_get_data(BIO *a) return a->ptr; } +GIT_INLINE(const unsigned char *) ASN1_STRING_get0_data(const ASN1_STRING *x) +{ + return ASN1_STRING_data((ASN1_STRING *)x); +} + # endif #endif From 3b832a085b8c5dc304dd803979894b9bae05df6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Nov 2016 13:11:31 +0100 Subject: [PATCH 041/142] openssl: include OpenSSL headers only when we're buliding against it We need to include the initialisation and construction functions in all backend, so we include this header when building against SecureTransport and WinHTTP as well. --- src/openssl_stream.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/openssl_stream.h b/src/openssl_stream.h index e8ce5d91b..b769437ae 100644 --- a/src/openssl_stream.h +++ b/src/openssl_stream.h @@ -7,11 +7,6 @@ #ifndef INCLUDE_openssl_stream_h__ #define INCLUDE_openssl_stream_h__ -#include -#include -#include -#include - #include "git2/sys/stream.h" extern int git_openssl_stream_global_init(void); @@ -24,6 +19,14 @@ extern int git_openssl_stream_new(git_stream **out, const char *host, const char * we can program against the interface instead of littering the implementation * with ifdefs. */ +#ifdef GIT_OPENSSL +# include +# include +# include +# include + + + # if OPENSSL_VERSION_NUMBER < 0x10100000L GIT_INLINE(BIO_METHOD*) BIO_meth_new(int type, const char *name) @@ -113,6 +116,7 @@ GIT_INLINE(const unsigned char *) ASN1_STRING_get0_data(const ASN1_STRING *x) return ASN1_STRING_data((ASN1_STRING *)x); } -# endif +# endif // OpenSSL < 1.1 +#endif // GIT_OPENSSL #endif From 86bbaaa1a6754d4f9e1c6eb4e17887ae189aa825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Nov 2016 17:06:15 +0100 Subject: [PATCH 042/142] THREADING: update cURL thread safety link --- THREADING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/THREADING.md b/THREADING.md index e9be8b99f..77160a479 100644 --- a/THREADING.md +++ b/THREADING.md @@ -63,7 +63,7 @@ General Case ------------ By default we use libcurl, which has its own ![recommendations for -thread safety](http://curl.haxx.se/libcurl/c/libcurl-tutorial.html#Multi-threading). +thread safety](https://curl.haxx.se/libcurl/c/threadsafe.html). If libcurl was not found or was disabled, libgit2 uses OpenSSL to be able to use HTTPS as a transport. This library is made to be From f7d316ed7a9663b92d6b70ae632d1ec6e1ca2e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Nov 2016 17:22:30 +0100 Subject: [PATCH 043/142] THREADING: OpenSSL 1.1 is thead-safe, rework some paragraphs --- THREADING.md | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/THREADING.md b/THREADING.md index 77160a479..430bca858 100644 --- a/THREADING.md +++ b/THREADING.md @@ -62,29 +62,34 @@ general case still affects you if you use ssh. General Case ------------ -By default we use libcurl, which has its own ![recommendations for -thread safety](https://curl.haxx.se/libcurl/c/threadsafe.html). +If it's available, by default we use libcurl to provide HTTP tunneling support, +which may be linked against a number of cryptographic libraries and has its +own +[recommendations for thread safety](https://curl.haxx.se/libcurl/c/threadsafe.html). -If libcurl was not found or was disabled, libgit2 uses OpenSSL to be -able to use HTTPS as a transport. This library is made to be -thread-implementation agnostic, and the users of the library must set -which locking function it should use. This means that libgit2 cannot -know what to set as the user of libgit2 may use OpenSSL independently -and the locking settings must survive libgit2 shutting down. +If there are no alternative TLS implementations (currently only +SecureTransport), libgit2 uses OpenSSL in order to use HTTPS as a transport. +OpenSSL is thread-safe starting at version 1.1.0. If your copy of libgit2 is +linked against that version, you do not need to take any further steps. -Even if libgit2 doesn't use OpenSSL directly, OpenSSL can still be used -by libssh2 depending on the configuration. If OpenSSL is used both by -libgit2 and libssh2, you only need to set up threading for OpenSSL once. +Older versions of OpenSSL are made to be thread-implementation agnostic, and the +users of the library must set which locking function it should use. libgit2 +cannot know what to set as the user of libgit2 may also be using OpenSSL independently and +the locking settings must then live outside the lifetime of libgit2. -libgit2 does provide a last-resort convenience function +Even if libgit2 doesn't use OpenSSL directly, OpenSSL can still be used by +libssh2 or libcurl depending on the configuration. If OpenSSL is used by +more than one library, you only need to set up threading for OpenSSL once. + +If libgit2 is linked against OpenSSL, it provides a last-resort convenience function `git_openssl_set_locking()` (available in `sys/openssl.h`) to use the -platform-native mutex mechanisms to perform the locking, which you may -rely on if you do not want to use OpenSSL outside of libgit2, or you -know that libgit2 will outlive the rest of the operations. It is not +platform-native mutex mechanisms to perform the locking, which you can use +if you do not want to use OpenSSL outside of libgit2, or you +know that libgit2 will outlive the rest of the operations. It is then not safe to use OpenSSL multi-threaded after libgit2's shutdown function has been called. Note `git_openssl_set_locking()` only works if libgit2 uses OpenSSL directly - if OpenSSL is only used as a dependency -of libssh2 as described above, `git_openssl_set_locking()` is a no-op. +of libssh2 or libcurl as described above, `git_openssl_set_locking()` is a no-op. If your programming language offers a package/bindings for OpenSSL, you should very strongly prefer to use that in order to set up @@ -96,9 +101,6 @@ See the on threading for more details, and http://trac.libssh2.org/wiki/MultiThreading for a specific example of providing the threading callbacks. -Be also aware that libgit2 does not always link against OpenSSL -if there are alternatives provided by the system. - libssh2 may be linked against OpenSSL or libgcrypt. If it uses OpenSSL, see the above paragraphs. If it uses libgcrypt, then you need to set up its locking before using it multi-threaded. libgit2 has no From f9793884a3fc895a17a2a70709d32cbc2214377b Mon Sep 17 00:00:00 2001 From: John Fultz Date: Fri, 28 Oct 2016 14:32:01 -0500 Subject: [PATCH 044/142] branch: fix forced branch creation on HEAD of a bare repo The code correctly detects that forced creation of a branch on a nonbare repo should not be able to overwrite a branch which is the HEAD reference. But there's no reason to prevent this on a bare repo, and in fact, git allows this. I.e., git branch -f master new_sha works on a bare repo with HEAD set to master. This change fixes that problem, and updates tests so that, for this case, both the bare and nonbare cases are checked for correct behavior. --- src/branch.c | 9 +++++---- tests/refs/branches/create.c | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/branch.c b/src/branch.c index 51c35d7ff..8d1ed6577 100644 --- a/src/branch.c +++ b/src/branch.c @@ -58,16 +58,17 @@ static int create_branch( const char *from, int force) { - int is_head = 0; + int is_unmovable_head = 0; git_reference *branch = NULL; git_buf canonical_branch_name = GIT_BUF_INIT, log_message = GIT_BUF_INIT; int error = -1; + int bare = git_repository_is_bare(repository); assert(branch_name && commit && ref_out); assert(git_object_owner((const git_object *)commit) == repository); - if (force && git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) { + if (force && !bare && git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) { error = git_branch_is_head(branch); git_reference_free(branch); branch = NULL; @@ -75,10 +76,10 @@ static int create_branch( if (error < 0) goto cleanup; - is_head = error; + is_unmovable_head = error; } - if (is_head && force) { + if (is_unmovable_head && force) { giterr_set(GITERR_REFERENCE, "Cannot force update branch '%s' as it is " "the current HEAD of the repository.", branch_name); error = -1; diff --git a/tests/refs/branches/create.c b/tests/refs/branches/create.c index 31dec0678..69488e6c7 100644 --- a/tests/refs/branches/create.c +++ b/tests/refs/branches/create.c @@ -65,10 +65,14 @@ void test_refs_branches_create__can_force_create_over_an_existing_branch(void) cl_assert_equal_s("refs/heads/br2", git_reference_name(branch)); } -void test_refs_branches_create__cannot_force_create_over_current_branch(void) +void test_refs_branches_create__cannot_force_create_over_current_branch_in_nonbare_repo(void) { const git_oid *oid; git_reference *branch2; + + /* Default repo for these tests is a bare repo, but this test requires a non-bare one */ + cl_git_sandbox_cleanup(); + repo = cl_git_sandbox_init("testrepo"); retrieve_known_commit(&target, repo); cl_git_pass(git_branch_lookup(&branch2, repo, "master", GIT_BRANCH_LOCAL)); @@ -84,6 +88,26 @@ void test_refs_branches_create__cannot_force_create_over_current_branch(void) git_reference_free(branch2); } +void test_refs_branches_create__can_force_create_over_current_branch_in_bare_repo(void) +{ + const git_oid *oid; + git_reference *branch2; + retrieve_known_commit(&target, repo); + + cl_git_pass(git_branch_lookup(&branch2, repo, "master", GIT_BRANCH_LOCAL)); + cl_assert_equal_s("refs/heads/master", git_reference_name(branch2)); + cl_assert_equal_i(true, git_branch_is_head(branch2)); + oid = git_commit_id(target); + + cl_git_pass(git_branch_create(&branch, repo, "master", target, 1)); + git_reference_free(branch); + branch = NULL; + cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL)); + cl_assert_equal_s("refs/heads/master", git_reference_name(branch)); + cl_git_pass(git_oid_cmp(git_reference_target(branch), oid)); + git_reference_free(branch2); +} + void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void) { retrieve_known_commit(&target, repo); From 5ca75fd52c36f63fe9b1218e79953411d4435a9d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 10 Nov 2016 08:00:22 -0800 Subject: [PATCH 045/142] curl_stream: check for -1 after CURLINFO_LASTSOCKET We're recently trying to upgrade to the current master of libgit2 in Cargo but we're unfortunately hitting a segfault in one of our tests. This particular test is just a small smoke test that https works (e.g. it's configured in libgit2). It attempts to clone from a URL which simply immediately drops connections after they're accepted (e.g. terminate abnormally). We expect to see a standard error from libgit2 but unfortunately we're seeing a segfault. This segfault is happening inside of the `wait_for` function of `curl_stream.c` at the line `FD_SET(fd, &errfd)` because `fd` is -1. This ends up doing an out-of-bounds array access that faults the program. I tracked back to where this -1 came from to the line here (returned by `CURLINFO_LASTSOCKET`) and added a check to return an error. --- src/curl_stream.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/curl_stream.c b/src/curl_stream.c index 98de187dd..3a3f364b2 100644 --- a/src/curl_stream.c +++ b/src/curl_stream.c @@ -121,6 +121,11 @@ static int curls_connect(git_stream *stream) return seterr_curl(s); } + if (sockextr == -1) { + giterr_set(GITERR_NET, "curl socket is no longer valid"); + return -1; + } + s->socket = sockextr; if (s->parent.encrypted && failed_cert) @@ -198,6 +203,7 @@ static int wait_for(curl_socket_t fd, bool reading) FD_ZERO(&outfd); FD_ZERO(&errfd); + assert(fd >= 0); FD_SET(fd, &errfd); if (reading) FD_SET(fd, &infd); From c9e967a1b48c7a0b484fd3a4860e9c7a0aa4a319 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Thu, 10 Nov 2016 03:51:12 -0800 Subject: [PATCH 046/142] git_repository_open_ext: fix handling of $GIT_NAMESPACE The existing code would set a namespace of "" (empty string) with GIT_NAMESPACE unset. In a repository where refs/heads/namespaces/ exists, that can produce incorrect results. Detect that case and avoid setting the namespace at all. Since that makes the last assignment to error conditional, and the previous assignment can potentially get GIT_ENOTFOUND, set error to 0 explicitly to prevent the call from incorrectly failing with GIT_ENOTFOUND. --- src/repository.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/repository.c b/src/repository.c index cf3d18a2d..7bdcefd40 100644 --- a/src/repository.c +++ b/src/repository.c @@ -613,9 +613,10 @@ static int _git_repository_open_ext_from_env( git_repository_set_odb(repo, odb); error = git__getenv(&alts_buf, "GIT_ALTERNATE_OBJECT_DIRECTORIES"); - if (error == GIT_ENOTFOUND) + if (error == GIT_ENOTFOUND) { giterr_clear(); - else if (error < 0) + error = 0; + } else if (error < 0) goto error; else { const char *end; @@ -638,9 +639,11 @@ static int _git_repository_open_ext_from_env( } } - error = git_repository_set_namespace(repo, git_buf_cstr(&namespace_buf)); - if (error < 0) - goto error; + if (git_buf_len(&namespace_buf)) { + error = git_repository_set_namespace(repo, git_buf_cstr(&namespace_buf)); + if (error < 0) + goto error; + } git_repository_set_index(repo, index); From 7b3f49f0c9bfeb83c2ce55dabbb0ec3e6e876d69 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 14 Nov 2016 09:27:15 +0100 Subject: [PATCH 047/142] fileops: fix typos in `git_futils_creat_locked{,with_path}` --- src/fileops.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fileops.h b/src/fileops.h index 54e3bd4e4..65c96a6f5 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -45,12 +45,12 @@ extern int git_futils_writebuffer( extern int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode); /** - * Create an open a process-locked file + * Create and open a process-locked file */ extern int git_futils_creat_locked(const char *path, const mode_t mode); /** - * Create an open a process-locked file, while + * Create and open a process-locked file, while * also creating all the folders in its path */ extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode); From 8effd26f598f8d2d24f023290f6f7e87be20aac5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 14 Nov 2016 09:54:08 +0100 Subject: [PATCH 048/142] common: mark printf-style formatting for `giterr_set` --- src/common.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common.h b/src/common.h index 51fb9186e..f12cc98cf 100644 --- a/src/common.h +++ b/src/common.h @@ -103,7 +103,8 @@ /** * Set the error message for this thread, formatting as needed. */ -void giterr_set(int error_class, const char *string, ...); + +void giterr_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3); /** * Set the error message for a regex failure, using the internal regex From c77a55a917c60bc052d8702d4c3e170b0b5fe964 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 14 Nov 2016 10:05:31 +0100 Subject: [PATCH 049/142] common: use PRIuZ for size_t in `giterr_set` calls --- src/apply.c | 2 +- src/fetchhead.c | 16 +++++++-------- src/iterator.c | 2 +- src/merge.c | 2 +- src/patch_parse.c | 50 +++++++++++++++++++++++------------------------ 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/apply.c b/src/apply.c index f70172469..635934299 100644 --- a/src/apply.c +++ b/src/apply.c @@ -173,7 +173,7 @@ static int apply_hunk( git_diff_line *line = git_array_get(patch->lines, linenum); if (!line) { - error = apply_err("Preimage does not contain line %d", linenum); + error = apply_err("Preimage does not contain line %"PRIuZ, linenum); goto done; } diff --git a/src/fetchhead.c b/src/fetchhead.c index a95ea4ca4..3d16c2166 100644 --- a/src/fetchhead.c +++ b/src/fetchhead.c @@ -149,7 +149,7 @@ static int fetchhead_ref_parse( if (!*line) { giterr_set(GITERR_FETCHHEAD, - "Empty line in FETCH_HEAD line %d", line_num); + "Empty line in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -163,7 +163,7 @@ static int fetchhead_ref_parse( if (strlen(oid_str) != GIT_OID_HEXSZ) { giterr_set(GITERR_FETCHHEAD, - "Invalid object ID in FETCH_HEAD line %d", line_num); + "Invalid object ID in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -171,7 +171,7 @@ static int fetchhead_ref_parse( const git_error *oid_err = giterr_last(); const char *err_msg = oid_err ? oid_err->message : "Invalid object ID"; - giterr_set(GITERR_FETCHHEAD, "%s in FETCH_HEAD line %d", + giterr_set(GITERR_FETCHHEAD, "%s in FETCH_HEAD line %"PRIuZ, err_msg, line_num); return -1; } @@ -180,7 +180,7 @@ static int fetchhead_ref_parse( if (*line) { if ((is_merge_str = git__strsep(&line, "\t")) == NULL) { giterr_set(GITERR_FETCHHEAD, - "Invalid description data in FETCH_HEAD line %d", line_num); + "Invalid description data in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -190,13 +190,13 @@ static int fetchhead_ref_parse( *is_merge = 0; else { giterr_set(GITERR_FETCHHEAD, - "Invalid for-merge entry in FETCH_HEAD line %d", line_num); + "Invalid for-merge entry in FETCH_HEAD line %"PRIuZ, line_num); return -1; } if ((desc = line) == NULL) { giterr_set(GITERR_FETCHHEAD, - "Invalid description in FETCH_HEAD line %d", line_num); + "Invalid description in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -213,7 +213,7 @@ static int fetchhead_ref_parse( if ((desc = strstr(name, "' ")) == NULL || git__prefixcmp(desc, "' of ") != 0) { giterr_set(GITERR_FETCHHEAD, - "Invalid description in FETCH_HEAD line %d", line_num); + "Invalid description in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -277,7 +277,7 @@ int git_repository_fetchhead_foreach(git_repository *repo, } if (*buffer) { - giterr_set(GITERR_FETCHHEAD, "No EOL at line %d", line_num+1); + giterr_set(GITERR_FETCHHEAD, "No EOL at line %"PRIuZ, line_num+1); error = -1; goto done; } diff --git a/src/iterator.c b/src/iterator.c index 598c69c83..8fc62c01c 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -1311,7 +1311,7 @@ static int filesystem_iterator_frame_push( if (iter->frames.size == FILESYSTEM_MAX_DEPTH) { giterr_set(GITERR_REPOSITORY, - "directory nesting too deep (%d)", iter->frames.size); + "directory nesting too deep (%"PRIuZ")", iter->frames.size); return -1; } diff --git a/src/merge.c b/src/merge.c index 6934aa731..1142917bd 100644 --- a/src/merge.c +++ b/src/merge.c @@ -591,7 +591,7 @@ int git_repository_mergehead_foreach( } if (*buffer) { - giterr_set(GITERR_MERGE, "No EOL at line %d", line_num); + giterr_set(GITERR_MERGE, "No EOL at line %"PRIuZ, line_num); error = -1; goto cleanup; } diff --git a/src/patch_parse.c b/src/patch_parse.c index 5ee09ee27..7a4fe9f1a 100644 --- a/src/patch_parse.c +++ b/src/patch_parse.c @@ -176,7 +176,7 @@ static int parse_header_mode(uint16_t *mode, git_patch_parse_ctx *ctx) int ret; if (ctx->line_len < 1 || !git__isdigit(ctx->line[0])) - return parse_err("invalid file mode at line %d", ctx->line_num); + return parse_err("invalid file mode at line %"PRIuZ, ctx->line_num); if ((ret = git__strntol32(&m, ctx->line, ctx->line_len, &end, 8)) < 0) return ret; @@ -205,7 +205,7 @@ static int parse_header_oid( if (len < GIT_OID_MINPREFIXLEN || len > GIT_OID_HEXSZ || git_oid_fromstrn(oid, ctx->line, len) < 0) - return parse_err("invalid hex formatted object id at line %d", + return parse_err("invalid hex formatted object id at line %"PRIuZ, ctx->line_num); parse_advance_chars(ctx, len); @@ -350,7 +350,7 @@ static int parse_header_similarity( git_patch_parsed *patch, git_patch_parse_ctx *ctx) { if (parse_header_percent(&patch->base.delta->similarity, ctx) < 0) - return parse_err("invalid similarity percentage at line %d", + return parse_err("invalid similarity percentage at line %"PRIuZ, ctx->line_num); return 0; @@ -362,7 +362,7 @@ static int parse_header_dissimilarity( uint16_t dissimilarity; if (parse_header_percent(&dissimilarity, ctx) < 0) - return parse_err("invalid similarity percentage at line %d", + return parse_err("invalid similarity percentage at line %"PRIuZ, ctx->line_num); patch->base.delta->similarity = 100 - dissimilarity; @@ -406,15 +406,15 @@ static int parse_header_git( /* Parse the diff --git line */ if (parse_advance_expected_str(ctx, "diff --git ") < 0) - return parse_err("corrupt git diff header at line %d", ctx->line_num); + return parse_err("corrupt git diff header at line %"PRIuZ, ctx->line_num); if (parse_header_path(&patch->header_old_path, ctx) < 0) - return parse_err("corrupt old path in git diff header at line %d", + return parse_err("corrupt old path in git diff header at line %"PRIuZ, ctx->line_num); if (parse_advance_ws(ctx) < 0 || parse_header_path(&patch->header_new_path, ctx) < 0) - return parse_err("corrupt new path in git diff header at line %d", + return parse_err("corrupt new path in git diff header at line %"PRIuZ, ctx->line_num); /* Parse remaining header lines */ @@ -447,7 +447,7 @@ static int parse_header_git( parse_advance_expected_str(ctx, "\n"); if (ctx->line_len > 0) { - error = parse_err("trailing data at line %d", ctx->line_num); + error = parse_err("trailing data at line %"PRIuZ, ctx->line_num); goto done; } @@ -456,7 +456,7 @@ static int parse_header_git( } if (!found) { - error = parse_err("invalid patch header at line %d", + error = parse_err("invalid patch header at line %"PRIuZ, ctx->line_num); goto done; } @@ -536,7 +536,7 @@ static int parse_hunk_header( hunk->hunk.header_len = ctx->line - header_start; if (hunk->hunk.header_len > (GIT_DIFF_HUNK_HEADER_SIZE - 1)) - return parse_err("oversized patch hunk header at line %d", + return parse_err("oversized patch hunk header at line %"PRIuZ, ctx->line_num); memcpy(hunk->hunk.header, header_start, hunk->hunk.header_len); @@ -545,7 +545,7 @@ static int parse_hunk_header( return 0; fail: - giterr_set(GITERR_PATCH, "invalid patch hunk header at line %d", + giterr_set(GITERR_PATCH, "invalid patch hunk header at line %"PRIuZ, ctx->line_num); return -1; } @@ -570,7 +570,7 @@ static int parse_hunk_body( int prefix = 1; if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n') { - error = parse_err("invalid patch instruction at line %d", + error = parse_err("invalid patch instruction at line %"PRIuZ, ctx->line_num); goto done; } @@ -596,7 +596,7 @@ static int parse_hunk_body( break; default: - error = parse_err("invalid patch hunk at line %d", ctx->line_num); + error = parse_err("invalid patch hunk at line %"PRIuZ, ctx->line_num); goto done; } @@ -672,7 +672,7 @@ static int parse_patch_header( continue; } - error = parse_err("invalid hunk header outside patch at line %d", + error = parse_err("invalid hunk header outside patch at line %"PRIuZ, line_num); goto done; } @@ -715,12 +715,12 @@ static int parse_patch_binary_side( parse_advance_chars(ctx, 6); } else { error = parse_err( - "unknown binary delta type at line %d", ctx->line_num); + "unknown binary delta type at line %"PRIuZ, ctx->line_num); goto done; } if (parse_number(&len, ctx) < 0 || parse_advance_nl(ctx) < 0 || len < 0) { - error = parse_err("invalid binary size at line %d", ctx->line_num); + error = parse_err("invalid binary size at line %"PRIuZ, ctx->line_num); goto done; } @@ -736,7 +736,7 @@ static int parse_patch_binary_side( decoded_len = c - 'a' + (('z' - 'a') + 1) + 1; if (!decoded_len) { - error = parse_err("invalid binary length at line %d", ctx->line_num); + error = parse_err("invalid binary length at line %"PRIuZ, ctx->line_num); goto done; } @@ -745,7 +745,7 @@ static int parse_patch_binary_side( encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5; if (encoded_len > ctx->line_len - 1) { - error = parse_err("truncated binary data at line %d", ctx->line_num); + error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num); goto done; } @@ -754,14 +754,14 @@ static int parse_patch_binary_side( goto done; if (decoded.size - decoded_orig != decoded_len) { - error = parse_err("truncated binary data at line %d", ctx->line_num); + error = parse_err("truncated binary data at line %"PRIuZ, ctx->line_num); goto done; } parse_advance_chars(ctx, encoded_len); if (parse_advance_nl(ctx) < 0) { - error = parse_err("trailing data at line %d", ctx->line_num); + error = parse_err("trailing data at line %"PRIuZ, ctx->line_num); goto done; } } @@ -785,7 +785,7 @@ static int parse_patch_binary( if (parse_advance_expected_str(ctx, "GIT binary patch") < 0 || parse_advance_nl(ctx) < 0) - return parse_err("corrupt git binary header at line %d", ctx->line_num); + return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num); /* parse old->new binary diff */ if ((error = parse_patch_binary_side( @@ -793,7 +793,7 @@ static int parse_patch_binary( return error; if (parse_advance_nl(ctx) < 0) - return parse_err("corrupt git binary separator at line %d", + return parse_err("corrupt git binary separator at line %"PRIuZ, ctx->line_num); /* parse new->old binary diff */ @@ -802,7 +802,7 @@ static int parse_patch_binary( return error; if (parse_advance_nl(ctx) < 0) - return parse_err("corrupt git binary patch separator at line %d", + return parse_err("corrupt git binary patch separator at line %"PRIuZ, ctx->line_num); patch->base.binary.contains_data = 1; @@ -820,7 +820,7 @@ static int parse_patch_binary_nodata( parse_advance_expected_str(ctx, patch->header_new_path) < 0 || parse_advance_expected_str(ctx, " differ") < 0 || parse_advance_nl(ctx) < 0) - return parse_err("corrupt git binary header at line %d", ctx->line_num); + return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num); patch->base.binary.contains_data = 0; patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY; @@ -912,7 +912,7 @@ static int check_prefix( if (remain_len || !*path) return parse_err( - "header filename does not contain %d path components", + "header filename does not contain %"PRIuZ" path components", prefix_len); done: From 901434b00fc878cf7167dc923e60dd59be101c4f Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 14 Nov 2016 10:07:37 +0100 Subject: [PATCH 050/142] common: cast precision specifiers to int --- src/odb.c | 2 +- src/tree.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/odb.c b/src/odb.c index acf4dea71..7b194c70f 100644 --- a/src/odb.c +++ b/src/odb.c @@ -1400,7 +1400,7 @@ int git_odb__error_notfound( char oid_str[GIT_OID_HEXSZ + 1]; git_oid_tostr(oid_str, oid_len+1, oid); giterr_set(GITERR_ODB, "Object not found - %s (%.*s)", - message, oid_len, oid_str); + message, (int) oid_len, oid_str); } else giterr_set(GITERR_ODB, "Object not found - %s", message); diff --git a/src/tree.c b/src/tree.c index 6008a9544..e338acca0 100644 --- a/src/tree.c +++ b/src/tree.c @@ -917,7 +917,7 @@ int git_tree_entry_bypath( if (entry == NULL) { giterr_set(GITERR_TREE, - "the path '%.*s' does not exist in the given tree", filename_len, path); + "the path '%.*s' does not exist in the given tree", (int) filename_len, path); return GIT_ENOTFOUND; } @@ -927,7 +927,7 @@ int git_tree_entry_bypath( * then this entry *must* be a tree */ if (!git_tree_entry__is_tree(entry)) { giterr_set(GITERR_TREE, - "the path '%.*s' exists but is not a tree", filename_len, path); + "the path '%.*s' exists but is not a tree", (int) filename_len, path); return GIT_ENOTFOUND; } From 90a934a5210b60db3d746df3bcb0d6935768ffc1 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 14 Nov 2016 10:06:17 +0100 Subject: [PATCH 051/142] checkout: pass string instead of git_buf to `giterr_set` --- src/checkout.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checkout.c b/src/checkout.c index b3427fb7c..62950913e 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -1966,7 +1966,7 @@ static int checkout_path_suffixed(git_buf *path, const char *suffix) if (i == INT_MAX) { git_buf_truncate(path, path_len); - giterr_set(GITERR_CHECKOUT, "Could not write '%s': working directory file exists", path); + giterr_set(GITERR_CHECKOUT, "Could not write '%s': working directory file exists", path->ptr); return GIT_EEXISTS; } @@ -2469,7 +2469,7 @@ static int checkout_data_init( data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3; else { giterr_set(GITERR_CHECKOUT, "unknown style '%s' given for 'merge.conflictstyle'", - conflict_style); + conflict_style->value); error = -1; git_config_entry_free(conflict_style); goto cleanup; From b81fe7c96da2c9b0b6e4f344bbabaef033fc2ac2 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 14 Nov 2016 10:07:13 +0100 Subject: [PATCH 052/142] path: pass string instead of git_buf to giterr_set --- src/path.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/path.c b/src/path.c index e5f04a56a..f91e42242 100644 --- a/src/path.c +++ b/src/path.c @@ -1347,7 +1347,7 @@ int git_path_diriter_next(git_path_diriter *diriter) return GIT_ITEROVER; giterr_set(GITERR_OS, - "Could not read directory '%s'", diriter->path); + "Could not read directory '%s'", diriter->path.ptr); return -1; } } while (skip_dot && git_path_is_dot_or_dotdot(de->d_name)); From 2382b0f87775691742d0a53092ba0af6b73c2ba6 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 11 Nov 2016 15:50:14 +0100 Subject: [PATCH 053/142] test: discover: move layout creation into test initializer --- tests/repo/discover.c | 57 ++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/tests/repo/discover.c b/tests/repo/discover.c index 358daee9f..14963cbbf 100644 --- a/tests/repo/discover.c +++ b/tests/repo/discover.c @@ -68,42 +68,23 @@ static void append_ceiling_dir(git_buf *ceiling_dirs, const char *path) cl_assert(git_buf_oom(ceiling_dirs) == 0); } -void test_repo_discover__0(void) +void test_repo_discover__initialize(void) { - // test discover git_repository *repo; - git_buf ceiling_dirs_buf = GIT_BUF_INIT, repository_path = GIT_BUF_INIT, - sub_repository_path = GIT_BUF_INIT, found_path = GIT_BUF_INIT; - const char *ceiling_dirs; const mode_t mode = 0777; - git_futils_mkdir_r(DISCOVER_FOLDER, mode); - append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER); - ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); - - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs)); cl_git_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1)); - cl_git_pass(git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs)); git_repository_free(repo); cl_git_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0)); cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode)); - cl_git_pass(git_repository_discover(&sub_repository_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); - cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode)); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, &sub_repository_path); cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, mode)); write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../"); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path); cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, mode)); write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:"); @@ -113,6 +94,40 @@ void test_repo_discover__0(void) write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n"); cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, mode)); write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist"); + + git_repository_free(repo); +} + +void test_repo_discover__cleanup(void) +{ + cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, NULL, GIT_RMDIR_REMOVE_FILES)); +} + +void test_repo_discover__0(void) +{ + // test discover + git_buf ceiling_dirs_buf = GIT_BUF_INIT, repository_path = GIT_BUF_INIT, + sub_repository_path = GIT_BUF_INIT, found_path = GIT_BUF_INIT; + const char *ceiling_dirs; + + append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER); + ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); + + /* Nonexistent */ + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&repository_path, DISCOVER_FOLDER "-nonexistent", 0, ceiling_dirs)); + + cl_git_pass(git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs)); + cl_git_pass(git_repository_discover(&sub_repository_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); + + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, &sub_repository_path); + + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path); + cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs)); cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs)); cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs)); @@ -144,8 +159,6 @@ void test_repo_discover__0(void) ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path); - cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, NULL, GIT_RMDIR_REMOVE_FILES)); - git_repository_free(repo); git_buf_free(&ceiling_dirs_buf); git_buf_free(&repository_path); git_buf_free(&sub_repository_path); From 07afeb23aeb22e9c6bbf2f1f8095f706b5a752a7 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 11 Nov 2016 16:16:34 +0100 Subject: [PATCH 054/142] test: discover: pass constants to ensure_repository_discover --- tests/repo/discover.c | 45 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/tests/repo/discover.c b/tests/repo/discover.c index 14963cbbf..c02009ae4 100644 --- a/tests/repo/discover.c +++ b/tests/repo/discover.c @@ -9,6 +9,7 @@ #define SUB_REPOSITORY_FOLDER_NAME "sub_repo" #define SUB_REPOSITORY_FOLDER DISCOVER_FOLDER "/" SUB_REPOSITORY_FOLDER_NAME +#define SUB_REPOSITORY_GITDIR SUB_REPOSITORY_FOLDER "/.git" #define SUB_REPOSITORY_FOLDER_SUB SUB_REPOSITORY_FOLDER "/sub" #define SUB_REPOSITORY_FOLDER_SUB_SUB SUB_REPOSITORY_FOLDER_SUB "/subsub" #define SUB_REPOSITORY_FOLDER_SUB_SUB_SUB SUB_REPOSITORY_FOLDER_SUB_SUB "/subsubsub" @@ -24,13 +25,19 @@ #define ALTERNATE_NOT_FOUND_FOLDER DISCOVER_FOLDER "/alternate_not_found_repo" static void ensure_repository_discover(const char *start_path, - const char *ceiling_dirs, - git_buf *expected_path) + const char *ceiling_dirs, + const char *expected_path) { - git_buf found_path = GIT_BUF_INIT; + git_buf found_path = GIT_BUF_INIT, resolved = GIT_BUF_INIT; + + git_buf_attach(&resolved, p_realpath(expected_path, NULL), 0); + cl_assert(resolved.size > 0); + cl_git_pass(git_path_to_dir(&resolved)); cl_git_pass(git_repository_discover(&found_path, start_path, 0, ceiling_dirs)); - //across_fs is always 0 as we can't automate the filesystem change tests - cl_assert_equal_s(found_path.ptr, expected_path->ptr); + + cl_assert_equal_s(found_path.ptr, resolved.ptr); + + git_buf_free(&resolved); git_buf_free(&found_path); } @@ -119,14 +126,14 @@ void test_repo_discover__0(void) cl_git_pass(git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs)); cl_git_pass(git_repository_discover(&sub_repository_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, DISCOVER_FOLDER); cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs)); cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs)); @@ -138,8 +145,8 @@ void test_repo_discover__0(void) /* this must pass as ceiling_directories cannot prevent the current * working directory to be checked */ - ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); @@ -148,16 +155,16 @@ void test_repo_discover__0(void) //this must pass as ceiling_directories cannot predent the current //working directory to be checked - ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs)); cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); //.gitfile redirection should not be affected by ceiling directories - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, DISCOVER_FOLDER); git_buf_free(&ceiling_dirs_buf); git_buf_free(&repository_path); From 5242c42488cda4b6834dd9f5a607042d5d18b996 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 11 Nov 2016 16:43:37 +0100 Subject: [PATCH 055/142] test: discover: split up monolithic test into smaller ones --- tests/repo/discover.c | 116 ++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 49 deletions(-) diff --git a/tests/repo/discover.c b/tests/repo/discover.c index c02009ae4..a3df30ca9 100644 --- a/tests/repo/discover.c +++ b/tests/repo/discover.c @@ -75,12 +75,19 @@ static void append_ceiling_dir(git_buf *ceiling_dirs, const char *path) cl_assert(git_buf_oom(ceiling_dirs) == 0); } +static git_buf discovered; +static git_buf ceiling_dirs; + void test_repo_discover__initialize(void) { git_repository *repo; const mode_t mode = 0777; git_futils_mkdir_r(DISCOVER_FOLDER, mode); + git_buf_init(&discovered, 0); + git_buf_init(&ceiling_dirs, 0); + append_ceiling_dir(&ceiling_dirs, TEMP_REPO_FOLDER); + cl_git_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1)); git_repository_free(repo); @@ -107,67 +114,78 @@ void test_repo_discover__initialize(void) void test_repo_discover__cleanup(void) { + git_buf_free(&discovered); + git_buf_free(&ceiling_dirs); cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, NULL, GIT_RMDIR_REMOVE_FILES)); } -void test_repo_discover__0(void) +void test_repo_discover__discovering_repo_with_exact_path_succeeds(void) { - // test discover - git_buf ceiling_dirs_buf = GIT_BUF_INIT, repository_path = GIT_BUF_INIT, - sub_repository_path = GIT_BUF_INIT, found_path = GIT_BUF_INIT; - const char *ceiling_dirs; + cl_git_pass(git_repository_discover(&discovered, DISCOVER_FOLDER, 0, ceiling_dirs.ptr)); + cl_git_pass(git_repository_discover(&discovered, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs.ptr)); +} - append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER); - ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); +void test_repo_discover__discovering_nonexistent_dir_fails(void) +{ + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&discovered, DISCOVER_FOLDER "-nonexistent", 0, NULL)); +} - /* Nonexistent */ - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&repository_path, DISCOVER_FOLDER "-nonexistent", 0, ceiling_dirs)); +void test_repo_discover__discovering_repo_with_subdirectory_succeeds(void) +{ + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); +} - cl_git_pass(git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs)); - cl_git_pass(git_repository_discover(&sub_repository_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); +void test_repo_discover__discovering_repository_with_alternative_gitdir_succeeds(void) +{ + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs.ptr, DISCOVER_FOLDER); +} - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); +void test_repo_discover__discovering_repository_with_malformed_alternative_gitdir_fails(void) +{ + cl_git_fail(git_repository_discover(&discovered, ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs.ptr)); + cl_git_fail(git_repository_discover(&discovered, ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs.ptr)); + cl_git_fail(git_repository_discover(&discovered, ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs.ptr)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&discovered, ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs.ptr)); +} - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, DISCOVER_FOLDER); - - cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs)); - cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs)); - cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs)); - - append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER_SUB); - ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); +void test_repo_discover__discovering_repository_with_ceiling(void) +{ + append_ceiling_dir(&ceiling_dirs, SUB_REPOSITORY_FOLDER_SUB); /* this must pass as ceiling_directories cannot prevent the current * working directory to be checked */ - ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); + ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); - append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER); - ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); - - //this must pass as ceiling_directories cannot predent the current - //working directory to be checked - ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); - - //.gitfile redirection should not be affected by ceiling directories - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, SUB_REPOSITORY_GITDIR); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, DISCOVER_FOLDER); - - git_buf_free(&ceiling_dirs_buf); - git_buf_free(&repository_path); - git_buf_free(&sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&discovered, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs.ptr)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&discovered, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs.ptr)); } +void test_repo_discover__other_ceiling(void) +{ + append_ceiling_dir(&ceiling_dirs, SUB_REPOSITORY_FOLDER); + + /* this must pass as ceiling_directories cannot predent the current + * working directory to be checked */ + ensure_repository_discover(SUB_REPOSITORY_FOLDER, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&discovered, SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs.ptr)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&discovered, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs.ptr)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&discovered, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs.ptr)); +} + +void test_repo_discover__ceiling_should_not_affect_gitdir_redirection(void) +{ + append_ceiling_dir(&ceiling_dirs, SUB_REPOSITORY_FOLDER); + + /* gitfile redirection should not be affected by ceiling directories */ + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs.ptr, DISCOVER_FOLDER); +} From 4dbaf3cd6208e7ce1f0d6be714c1a7cfa646259c Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 14 Nov 2016 10:52:37 +0100 Subject: [PATCH 056/142] test: discover: fix indentation --- tests/repo/discover.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/repo/discover.c b/tests/repo/discover.c index a3df30ca9..ca916e341 100644 --- a/tests/repo/discover.c +++ b/tests/repo/discover.c @@ -44,7 +44,7 @@ static void ensure_repository_discover(const char *start_path, static void write_file(const char *path, const char *content) { git_file file; - int error; + int error; if (git_path_exists(path)) { cl_git_pass(p_unlink(path)); From 0f316096115513b5a07eb4df3883ba45ada28a07 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 11 Nov 2016 16:55:33 +0100 Subject: [PATCH 057/142] repository: do not interpret all files as gitlinks in discovery When trying to find a discovery, we walk up the directory structure checking if there is a ".git" file or directory and, if so, check its validity. But in the case that we've got a ".git" file, we do not want to unconditionally assume that the file is in fact a ".git" file and treat it as such, as we would error out if it is not. Fix the issue by only treating a file as a gitlink file if it ends with "/.git". This allows users of the function to discover a repository by handing in any path contained inside of a git repository. --- src/repository.c | 2 +- tests/repo/discover.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/repository.c b/src/repository.c index 7bdcefd40..5c4442360 100644 --- a/src/repository.c +++ b/src/repository.c @@ -410,7 +410,7 @@ static int find_repo( break; } } - else if (S_ISREG(st.st_mode)) { + else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) { error = read_gitfile(&repo_link, path.ptr); if (error < 0) break; diff --git a/tests/repo/discover.c b/tests/repo/discover.c index ca916e341..48aa27581 100644 --- a/tests/repo/discover.c +++ b/tests/repo/discover.c @@ -189,3 +189,13 @@ void test_repo_discover__ceiling_should_not_affect_gitdir_redirection(void) ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs.ptr, DISCOVER_FOLDER); } + +void test_repo_discover__discovery_starting_at_file_succeeds(void) +{ + int fd; + + cl_assert((fd = p_creat(SUB_REPOSITORY_FOLDER "/file", 0600)) >= 0); + cl_assert(p_close(fd) == 0); + + ensure_repository_discover(SUB_REPOSITORY_FOLDER "/file", ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR); +} From 7da4c429ea3d21e0fca0755e927f19b93e81a5c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 12:37:41 +0000 Subject: [PATCH 058/142] refdb: adjust the threading tests to what we promise We say it's going to work if you use a different repository in each thread. Let's do precisely that in our code instead of hoping re-using the refdb is going to work. This test does fail currently, surfacing existing bugs. --- tests/threads/refdb.c | 120 +++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 77 deletions(-) diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c index f869bcb44..9b1b37592 100644 --- a/tests/threads/refdb.c +++ b/tests/threads/refdb.c @@ -19,13 +19,21 @@ void test_threads_refdb__cleanup(void) #define REPEAT 20 #define THREADS 20 +struct th_data { + int id; + const char *path; +}; + static void *iterate_refs(void *arg) { + struct th_data *data = (struct th_data *) arg; git_reference_iterator *i; git_reference *ref; int count = 0; + git_repository *repo; - cl_git_pass(git_reference_iterator_new(&i, g_repo)); + cl_git_pass(git_repository_open(&repo, data->path)); + cl_git_pass(git_reference_iterator_new(&i, repo)); for (count = 0; !git_reference_next(&ref, i); ++count) { cl_assert(ref != NULL); @@ -37,77 +45,31 @@ static void *iterate_refs(void *arg) git_reference_iterator_free(i); + git_repository_free(repo); giterr_clear(); return arg; } -void test_threads_refdb__iterator(void) -{ - int r, t; - git_thread th[THREADS]; - int id[THREADS]; - git_oid head; - git_reference *ref; - char name[128]; - git_refdb *refdb; - - g_repo = cl_git_sandbox_init("testrepo2"); - - cl_git_pass(git_reference_name_to_id(&head, g_repo, "HEAD")); - - /* make a bunch of references */ - - for (r = 0; r < 200; ++r) { - p_snprintf(name, sizeof(name), "refs/heads/direct-%03d", r); - cl_git_pass(git_reference_create(&ref, g_repo, name, &head, 0, NULL)); - git_reference_free(ref); - } - - cl_git_pass(git_repository_refdb(&refdb, g_repo)); - cl_git_pass(git_refdb_compress(refdb)); - git_refdb_free(refdb); - - g_expected = 206; - - for (r = 0; r < REPEAT; ++r) { - g_repo = cl_git_sandbox_reopen(); /* reopen to flush caches */ - - for (t = 0; t < THREADS; ++t) { - id[t] = t; -#ifdef GIT_THREADS - cl_git_pass(git_thread_create(&th[t], iterate_refs, &id[t])); -#else - th[t] = t; - iterate_refs(&id[t]); -#endif - } - -#ifdef GIT_THREADS - for (t = 0; t < THREADS; ++t) { - cl_git_pass(git_thread_join(&th[t], NULL)); - } -#endif - - memset(th, 0, sizeof(th)); - } -} - static void *create_refs(void *arg) { - int *id = arg, i; + int i; + struct th_data *data = (struct th_data *) arg; git_oid head; char name[128]; git_reference *ref[10]; + git_repository *repo; - cl_git_pass(git_reference_name_to_id(&head, g_repo, "HEAD")); + cl_git_pass(git_repository_open(&repo, data->path)); + + cl_git_pass(git_reference_name_to_id(&head, repo, "HEAD")); for (i = 0; i < 10; ++i) { - p_snprintf(name, sizeof(name), "refs/heads/thread-%03d-%02d", *id, i); - cl_git_pass(git_reference_create(&ref[i], g_repo, name, &head, 0, NULL)); + p_snprintf(name, sizeof(name), "refs/heads/thread-%03d-%02d", data->id, i); + cl_git_pass(git_reference_create(&ref[i], repo, name, &head, 0, NULL)); if (i == 5) { git_refdb *refdb; - cl_git_pass(git_repository_refdb(&refdb, g_repo)); + cl_git_pass(git_repository_refdb(&refdb, repo)); cl_git_pass(git_refdb_compress(refdb)); git_refdb_free(refdb); } @@ -116,33 +78,40 @@ static void *create_refs(void *arg) for (i = 0; i < 10; ++i) git_reference_free(ref[i]); + git_repository_free(repo); + giterr_clear(); return arg; } static void *delete_refs(void *arg) { - int *id = arg, i; + int i; + struct th_data *data = (struct th_data *) arg; git_reference *ref; char name[128]; + git_repository *repo; + + cl_git_pass(git_repository_open(&repo, data->path)); for (i = 0; i < 10; ++i) { p_snprintf( - name, sizeof(name), "refs/heads/thread-%03d-%02d", (*id) & ~0x3, i); + name, sizeof(name), "refs/heads/thread-%03d-%02d", (data->id) & ~0x3, i); - if (!git_reference_lookup(&ref, g_repo, name)) { + if (!git_reference_lookup(&ref, repo, name)) { cl_git_pass(git_reference_delete(ref)); git_reference_free(ref); } if (i == 5) { git_refdb *refdb; - cl_git_pass(git_repository_refdb(&refdb, g_repo)); + cl_git_pass(git_repository_refdb(&refdb, repo)); cl_git_pass(git_refdb_compress(refdb)); git_refdb_free(refdb); } } + git_repository_free(repo); giterr_clear(); return arg; } @@ -150,7 +119,7 @@ static void *delete_refs(void *arg) void test_threads_refdb__edit_while_iterate(void) { int r, t; - int id[THREADS]; + struct th_data th_data[THREADS]; git_oid head; git_reference *ref; char name[128]; @@ -189,29 +158,26 @@ void test_threads_refdb__edit_while_iterate(void) default: fn = iterate_refs; break; } - id[t] = t; + th_data[t].id = t; + th_data[t].path = git_repository_path(g_repo); - /* It appears with all reflog writing changes, etc., that this - * test has started to fail quite frequently, so let's disable it - * for now by just running on a single thread... - */ -/* #ifdef GIT_THREADS */ -/* cl_git_pass(git_thread_create(&th[t], fn, &id[t])); */ -/* #else */ - fn(&id[t]); -/* #endif */ +#ifdef GIT_THREADS + cl_git_pass(git_thread_create(&th[t], fn, &th_data[t])); +#else + fn(&th_data[t]); +#endif } #ifdef GIT_THREADS -/* for (t = 0; t < THREADS; ++t) { */ -/* cl_git_pass(git_thread_join(th[t], NULL)); */ -/* } */ + for (t = 0; t < THREADS; ++t) { + cl_git_pass(git_thread_join(&th[t], NULL)); + } memset(th, 0, sizeof(th)); for (t = 0; t < THREADS; ++t) { - id[t] = t; - cl_git_pass(git_thread_create(&th[t], iterate_refs, &id[t])); + th_data[t].id = t; + cl_git_pass(git_thread_create(&th[t], iterate_refs, &th_data[t])); } for (t = 0; t < THREADS; ++t) { From 9914efec2a0c32477b25897c98cbf78742eb2f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 14:00:48 +0000 Subject: [PATCH 059/142] refdb: bubble up errors We can get useful information like GIT_ELOCKED out of this instead of just -1. --- src/refdb_fs.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index f978038e6..6faf6cca7 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -944,41 +944,42 @@ static int packed_write(refdb_fs_backend *backend) { git_sortedcache *refcache = backend->refcache; git_filebuf pack_file = GIT_FILEBUF_INIT; + int error; size_t i; /* lock the cache to updates while we do this */ - if (git_sortedcache_wlock(refcache) < 0) - return -1; + if ((error = git_sortedcache_wlock(refcache)) < 0) + return error; /* Open the file! */ - if (git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE) < 0) + if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE)) < 0) goto fail; /* Packfiles have a header... apparently * This is in fact not required, but we might as well print it * just for kicks */ - if (git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER) < 0) + if ((error = git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER)) < 0) goto fail; for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) { struct packref *ref = git_sortedcache_entry(refcache, i); assert(ref); - if (packed_find_peel(backend, ref) < 0) + if ((error = packed_find_peel(backend, ref)) < 0) goto fail; - if (packed_write_ref(ref, &pack_file) < 0) + if ((error = packed_write_ref(ref, &pack_file)) < 0) goto fail; } /* if we've written all the references properly, we can commit * the packfile to make the changes effective */ - if (git_filebuf_commit(&pack_file) < 0) + if ((error = git_filebuf_commit(&pack_file)) < 0) goto fail; /* when and only when the packfile has been properly written, * we can go ahead and remove the loose refs */ - if (packed_remove_loose(backend) < 0) + if ((error = packed_remove_loose(backend)) < 0) goto fail; git_sortedcache_updated(refcache); @@ -991,7 +992,7 @@ fail: git_filebuf_cleanup(&pack_file); git_sortedcache_wunlock(refcache); - return -1; + return error; } static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *author, const char *message); From 2d9aec99fb6a6a456aecbc354443c0c87e8a34e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 14:01:38 +0000 Subject: [PATCH 060/142] refdb: make ref deletion after pack safer In order not to undo concurrent modifications to references, we must make sure that we only delete a loose reference if it still has the same value as when we packed it. This means we need to lock it and then compare the value with the one we put in the packed file. --- src/refdb_fs.c | 51 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 6faf6cca7..2e92911ae 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -901,30 +901,68 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file) static int packed_remove_loose(refdb_fs_backend *backend) { size_t i; - git_buf full_path = GIT_BUF_INIT; - int failed = 0; + git_buf ref_content = GIT_BUF_INIT; + int failed = 0, error = 0; /* backend->refcache is already locked when this is called */ for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { struct packref *ref = git_sortedcache_entry(backend->refcache, i); + git_filebuf lock = GIT_FILEBUF_INIT; + git_oid current_id; if (!ref || !(ref->flags & PACKREF_WAS_LOOSE)) continue; - if (git_buf_joinpath(&full_path, backend->path, ref->name) < 0) - return -1; /* critical; do not try to recover on oom */ + /* We need to stop anybody from updating the ref while we try to do a safe delete */ + error = loose_lock(&lock, backend, ref->name); + /* If someone else is updating it, let them do it */ + if (error == GIT_EEXISTS) + continue; - if (git_path_exists(full_path.ptr) && p_unlink(full_path.ptr) < 0) { + if (error < 0) { + failed = 1; + continue; + } + + error = git_futils_readbuffer(&ref_content, lock.path_original); + /* Someone else beat us to cleaning up the ref, let's simply continue */ + if (error == GIT_ENOTFOUND) { + git_filebuf_cleanup(&lock); + continue; + } + + /* This became a symref between us packing and trying to delete it, so ignore it */ + if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF)) { + git_filebuf_cleanup(&lock); + continue; + } + + /* Figure out the current id; if we fail record it but don't fail the whole operation */ + if ((error = loose_parse_oid(¤t_id, lock.path_original, &ref_content)) < 0) { + failed = 1; + git_filebuf_cleanup(&lock); + continue; + } + + /* If the ref moved since we packed it, we must not delete it */ + if (!git_oid_equal(¤t_id, &ref->oid)) { + git_filebuf_cleanup(&lock); + continue; + } + + if (p_unlink(lock.path_original) < 0) { if (failed) continue; giterr_set(GITERR_REFERENCE, "Failed to remove loose reference '%s' after packing: %s", - full_path.ptr, strerror(errno)); + lock.path_original, strerror(errno)); failed = 1; } + git_filebuf_cleanup(&lock); + /* * if we fail to remove a single file, this is *not* good, * but we should keep going and remove as many as possible. @@ -933,7 +971,6 @@ static int packed_remove_loose(refdb_fs_backend *backend) */ } - git_buf_free(&full_path); return failed ? -1 : 0; } From f94825c10c1c8f003e80859530cce8ceea1bd314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 17:21:51 +0000 Subject: [PATCH 061/142] fileops: save errno and report file existence We need to save the errno, lest we clobber it in the giterr_set() call. Also add code for reporting that a path component is missing, which is a distinct failure mode. --- src/fileops.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/fileops.c b/src/fileops.c index fcc0301f9..a82202c98 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -72,8 +72,16 @@ int git_futils_creat_locked(const char *path, const mode_t mode) O_EXCL | O_BINARY | O_CLOEXEC, mode); if (fd < 0) { + int error = errno; giterr_set(GITERR_OS, "Failed to create locked file '%s'", path); - return errno == EEXIST ? GIT_ELOCKED : -1; + switch (error) { + case EEXIST: + return GIT_ELOCKED; + case ENOENT: + return GIT_ENOTFOUND; + default: + return -1; + } } return fd; From 7ea4710ae3529f6c7ddccf70f76bdd8a757b000e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 17:30:24 +0000 Subject: [PATCH 062/142] refdb: don't report failure for expected errors There might be a few threads or processes working with references concurrently, so fortify the code to ignore errors which come from concurrent access which do not stop us from continuing the work. This includes ignoring an unlinking error. Either someone else removed it or we leave the file around. In the former case the job is done, and in the latter case, the ref is still in a valid state. --- src/refdb_fs.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 2e92911ae..17d025edd 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -902,7 +902,7 @@ static int packed_remove_loose(refdb_fs_backend *backend) { size_t i; git_buf ref_content = GIT_BUF_INIT; - int failed = 0, error = 0; + int error = 0; /* backend->refcache is already locked when this is called */ @@ -917,12 +917,12 @@ static int packed_remove_loose(refdb_fs_backend *backend) /* We need to stop anybody from updating the ref while we try to do a safe delete */ error = loose_lock(&lock, backend, ref->name); /* If someone else is updating it, let them do it */ - if (error == GIT_EEXISTS) + if (error == GIT_EEXISTS || error == GIT_ENOTFOUND) continue; if (error < 0) { - failed = 1; - continue; + giterr_set(GITERR_REFERENCE, "failed to lock loose reference '%s'", ref->name); + return error; } error = git_futils_readbuffer(&ref_content, lock.path_original); @@ -940,7 +940,6 @@ static int packed_remove_loose(refdb_fs_backend *backend) /* Figure out the current id; if we fail record it but don't fail the whole operation */ if ((error = loose_parse_oid(¤t_id, lock.path_original, &ref_content)) < 0) { - failed = 1; git_filebuf_cleanup(&lock); continue; } @@ -951,27 +950,17 @@ static int packed_remove_loose(refdb_fs_backend *backend) continue; } - if (p_unlink(lock.path_original) < 0) { - if (failed) - continue; - - giterr_set(GITERR_REFERENCE, - "Failed to remove loose reference '%s' after packing: %s", - lock.path_original, strerror(errno)); - failed = 1; - } - - git_filebuf_cleanup(&lock); - /* * if we fail to remove a single file, this is *not* good, * but we should keep going and remove as many as possible. - * After we've removed as many files as possible, we return - * the error code anyway. + * If we fail to remove, the ref is still in the old state, so + * we haven't lost information. */ + p_unlink(lock.path_original); + git_filebuf_cleanup(&lock); } - return failed ? -1 : 0; + return 0; } /* From dd1ca6f15a85ba1e812be044bc12cdab9b3134dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 17:38:41 +0000 Subject: [PATCH 063/142] refdb: refactor the lockfile cleanup We can reduce the duplication by cleaning up at the beginning of the loop, since it's something we want to do every time we continue. --- src/refdb_fs.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 17d025edd..20cc08fc4 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -901,6 +901,7 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file) static int packed_remove_loose(refdb_fs_backend *backend) { size_t i; + git_filebuf lock = GIT_FILEBUF_INIT; git_buf ref_content = GIT_BUF_INIT; int error = 0; @@ -908,12 +909,13 @@ static int packed_remove_loose(refdb_fs_backend *backend) for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { struct packref *ref = git_sortedcache_entry(backend->refcache, i); - git_filebuf lock = GIT_FILEBUF_INIT; git_oid current_id; if (!ref || !(ref->flags & PACKREF_WAS_LOOSE)) continue; + git_filebuf_cleanup(&lock); + /* We need to stop anybody from updating the ref while we try to do a safe delete */ error = loose_lock(&lock, backend, ref->name); /* If someone else is updating it, let them do it */ @@ -927,28 +929,20 @@ static int packed_remove_loose(refdb_fs_backend *backend) error = git_futils_readbuffer(&ref_content, lock.path_original); /* Someone else beat us to cleaning up the ref, let's simply continue */ - if (error == GIT_ENOTFOUND) { - git_filebuf_cleanup(&lock); + if (error == GIT_ENOTFOUND) continue; - } /* This became a symref between us packing and trying to delete it, so ignore it */ - if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF)) { - git_filebuf_cleanup(&lock); + if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF)) continue; - } - /* Figure out the current id; if we fail record it but don't fail the whole operation */ - if ((error = loose_parse_oid(¤t_id, lock.path_original, &ref_content)) < 0) { - git_filebuf_cleanup(&lock); + /* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */ + if (loose_parse_oid(¤t_id, lock.path_original, &ref_content) < 0) continue; - } /* If the ref moved since we packed it, we must not delete it */ - if (!git_oid_equal(¤t_id, &ref->oid)) { - git_filebuf_cleanup(&lock); + if (!git_oid_equal(¤t_id, &ref->oid)) continue; - } /* * if we fail to remove a single file, this is *not* good, @@ -957,9 +951,9 @@ static int packed_remove_loose(refdb_fs_backend *backend) * we haven't lost information. */ p_unlink(lock.path_original); - git_filebuf_cleanup(&lock); } + git_filebuf_cleanup(&lock); return 0; } From 2e09106e7a8711e3a4a70ef304643d28f2763c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 17:49:49 +0000 Subject: [PATCH 064/142] refdb: bubble up the error code when compressing the db This allows the caller to know the errors was e.g. due to the packed-refs file being already locked and they can try again later. --- src/refdb_fs.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 20cc08fc4..6b55960e1 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -1383,14 +1383,15 @@ static int refdb_fs_backend__rename( static int refdb_fs_backend__compress(git_refdb_backend *_backend) { + int error; refdb_fs_backend *backend = (refdb_fs_backend *)_backend; assert(backend); - if (packed_reload(backend) < 0 || /* load the existing packfile */ - packed_loadloose(backend) < 0 || /* add all the loose refs */ - packed_write(backend) < 0) /* write back to disk */ - return -1; + if ((error = packed_reload(backend)) < 0 || /* load the existing packfile */ + (error = packed_loadloose(backend)) < 0 || /* add all the loose refs */ + (error = packed_write(backend)) < 0) /* write back to disk */ + return error; return 0; } From 26416f6d20044d693ddfb57d719ee5183c065fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 24 Dec 2015 17:51:19 +0000 Subject: [PATCH 065/142] refdb: add retry logic to the threaded tests The logic simply consists of retrying for as long as the library says the data is locked, but it eventually gets through. --- tests/threads/refdb.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c index 9b1b37592..0c5cd2be6 100644 --- a/tests/threads/refdb.c +++ b/tests/threads/refdb.c @@ -52,7 +52,7 @@ static void *iterate_refs(void *arg) static void *create_refs(void *arg) { - int i; + int i, error; struct th_data *data = (struct th_data *) arg; git_oid head; char name[128]; @@ -70,7 +70,9 @@ static void *create_refs(void *arg) if (i == 5) { git_refdb *refdb; cl_git_pass(git_repository_refdb(&refdb, repo)); - cl_git_pass(git_refdb_compress(refdb)); + do { + error = git_refdb_compress(refdb); + } while (error == GIT_ELOCKED); git_refdb_free(refdb); } } @@ -86,7 +88,7 @@ static void *create_refs(void *arg) static void *delete_refs(void *arg) { - int i; + int i, error; struct th_data *data = (struct th_data *) arg; git_reference *ref; char name[128]; @@ -99,14 +101,20 @@ static void *delete_refs(void *arg) name, sizeof(name), "refs/heads/thread-%03d-%02d", (data->id) & ~0x3, i); if (!git_reference_lookup(&ref, repo, name)) { - cl_git_pass(git_reference_delete(ref)); + do { + error = git_reference_delete(ref); + } while (error == GIT_ELOCKED); + cl_git_pass(error); git_reference_free(ref); } if (i == 5) { git_refdb *refdb; cl_git_pass(git_repository_refdb(&refdb, repo)); - cl_git_pass(git_refdb_compress(refdb)); + do { + error = git_refdb_compress(refdb); + } while (error == GIT_ELOCKED); + cl_git_pass(error); git_refdb_free(refdb); } } From 40ffa07f4ff3616acb814ff3ef93df7206f5b5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 31 Dec 2015 14:51:42 +0000 Subject: [PATCH 066/142] sortedcache: check file size after opening the file Checking the size before we open the file descriptor can lead to the file being replaced from under us when renames aren't quite atomic, so we can end up reading too little of the file, leading to us thinking the file is corrupted. --- src/sortedcache.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/sortedcache.c b/src/sortedcache.c index 5c2a167a7..ed4199b71 100644 --- a/src/sortedcache.c +++ b/src/sortedcache.c @@ -200,6 +200,7 @@ void git_sortedcache_runlock(git_sortedcache *sc) int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf) { int error, fd; + struct stat st; if ((error = git_sortedcache_wlock(sc)) < 0) return error; @@ -207,19 +208,26 @@ int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf) if ((error = git_futils_filestamp_check(&sc->stamp, sc->path)) <= 0) goto unlock; - if (!git__is_sizet(sc->stamp.size)) { - giterr_set(GITERR_INVALID, "Unable to load file larger than size_t"); - error = -1; - goto unlock; - } - if ((fd = git_futils_open_ro(sc->path)) < 0) { error = fd; goto unlock; } + if (p_fstat(fd, &st) < 0) { + giterr_set(GITERR_OS, "failed to stat file"); + error = -1; + goto unlock; + } + + if (!git__is_sizet(st.st_size)) { + giterr_set(GITERR_INVALID, "Unable to load file larger than size_t"); + error = -1; + (void)p_close(fd); + goto unlock; + } + if (buf) - error = git_futils_readbuffer_fd(buf, fd, (size_t)sc->stamp.size); + error = git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size); (void)p_close(fd); From 33248b9edb74a6bac4223873402efe68a76a30a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 10 Mar 2016 12:22:34 +0100 Subject: [PATCH 067/142] refdb: remove a check-delete race when removing a loose ref It does not help us to check whether the file exists before trying to unlink it since it might be gone by the time unlink is called. Instead try to remove it and handle the resulting error if it did not exist. --- src/refdb_fs.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 6b55960e1..ab309c841 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -1281,15 +1281,14 @@ static int refdb_fs_backend__delete_tail( if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0) return -1; - if (git_path_isfile(loose_path.ptr)) { - error = p_unlink(loose_path.ptr); - loose_deleted = 1; - } - git_buf_free(&loose_path); - - if (error != 0) + error = p_unlink(loose_path.ptr); + if (error < 0 && errno == ENOENT) + error = 0; + else if (error < 0) goto cleanup; + else if (error == 0) + loose_deleted = 1; if ((error = packed_reload(backend)) < 0) goto cleanup; @@ -1312,6 +1311,7 @@ static int refdb_fs_backend__delete_tail( error = packed_write(backend); cleanup: + git_buf_free(&loose_path); git_filebuf_cleanup(file); return error; From 7c32d874503ee43206cb5d2a8e65dbe23f18eaca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 10 Mar 2016 12:27:07 +0100 Subject: [PATCH 068/142] refdb: expect threaded test deletes to race At times we may try to delete a reference which a different thread has already taken care of. --- tests/threads/refdb.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c index 0c5cd2be6..d8dc77ba5 100644 --- a/tests/threads/refdb.c +++ b/tests/threads/refdb.c @@ -104,6 +104,10 @@ static void *delete_refs(void *arg) do { error = git_reference_delete(ref); } while (error == GIT_ELOCKED); + /* Sometimes we race with other deleter threads */ + if (error == GIT_ENOTFOUND) + error = 0; + cl_git_pass(error); git_reference_free(ref); } From ce5553d48b8ba3510dd5032dfbfd161fb801cd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 10 Mar 2016 22:01:09 +0100 Subject: [PATCH 069/142] refdb: bubble up locked files on the read side On Windows we can find locked files even when reading a reference or the packed-refs file. Bubble up the error in this case as well to allow callers on Windows to retry more intelligently. --- src/path.c | 4 ++++ src/refdb.c | 6 ++++-- src/refdb_fs.c | 38 +++++++++++++++++++++----------------- tests/threads/refdb.c | 17 +++++++++++++---- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/path.c b/src/path.c index f91e42242..2b1a9622e 100644 --- a/src/path.c +++ b/src/path.c @@ -644,6 +644,10 @@ int git_path_set_error(int errno_value, const char *path, const char *action) giterr_set(GITERR_OS, "Failed %s - '%s' already exists", action, path); return GIT_EEXISTS; + case EACCES: + giterr_set(GITERR_OS, "Failed %s - '%s' is locked", action, path); + return GIT_ELOCKED; + default: giterr_set(GITERR_OS, "Could not %s '%s'", action, path); return -1; diff --git a/src/refdb.c b/src/refdb.c index debba1276..85c84892b 100644 --- a/src/refdb.c +++ b/src/refdb.c @@ -125,13 +125,15 @@ int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name) int git_refdb_iterator(git_reference_iterator **out, git_refdb *db, const char *glob) { + int error; + if (!db->backend || !db->backend->iterator) { giterr_set(GITERR_REFERENCE, "This backend doesn't support iterators"); return -1; } - if (db->backend->iterator(out, db->backend, glob) < 0) - return -1; + if ((error = db->backend->iterator(out, db->backend, glob)) < 0) + return error; GIT_REFCOUNT_INC(db); (*out)->db = db; diff --git a/src/refdb_fs.c b/src/refdb_fs.c index ab309c841..7601aa0ac 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -326,12 +326,13 @@ static int refdb_fs_backend__exists( { refdb_fs_backend *backend = (refdb_fs_backend *)_backend; git_buf ref_path = GIT_BUF_INIT; + int error; assert(backend); - if (packed_reload(backend) < 0 || - git_buf_joinpath(&ref_path, backend->path, ref_name) < 0) - return -1; + if ((error = packed_reload(backend)) < 0 || + (error = git_buf_joinpath(&ref_path, backend->path, ref_name)) < 0) + return error; *exists = git_path_isfile(ref_path.ptr) || (git_sortedcache_lookup(backend->refcache, ref_name) != NULL); @@ -409,8 +410,8 @@ static int packed_lookup( int error = 0; struct packref *entry; - if (packed_reload(backend) < 0) - return -1; + if ((error = packed_reload(backend)) < 0) + return error; if (git_sortedcache_rlock(backend->refcache) < 0) return -1; @@ -615,13 +616,14 @@ static int refdb_fs_backend__iterator_next_name( static int refdb_fs_backend__iterator( git_reference_iterator **out, git_refdb_backend *_backend, const char *glob) { + int error; refdb_fs_iter *iter; refdb_fs_backend *backend = (refdb_fs_backend *)_backend; assert(backend); - if (packed_reload(backend) < 0) - return -1; + if ((error = packed_reload(backend)) < 0) + return error; iter = git__calloc(1, sizeof(refdb_fs_iter)); GITERR_CHECK_ALLOC(iter); @@ -674,16 +676,18 @@ static int reference_path_available( int force) { size_t i; + int error; - if (packed_reload(backend) < 0) - return -1; + if ((error = packed_reload(backend)) < 0) + return error; if (!force) { int exists; - if (refdb_fs_backend__exists( - &exists, (git_refdb_backend *)backend, new_ref) < 0) - return -1; + if ((error = refdb_fs_backend__exists( + &exists, (git_refdb_backend *)backend, new_ref)) < 0) { + return error; + } if (exists) { giterr_set(GITERR_REFERENCE, @@ -1164,8 +1168,7 @@ static int refdb_fs_backend__write( assert(backend); - error = reference_path_available(backend, ref->name, NULL, force); - if (error < 0) + if ((error = reference_path_available(backend, ref->name, NULL, force)) < 0) return error; /* We need to perform the reflog append and old value check under the ref's lock */ @@ -1811,9 +1814,10 @@ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, co * there maybe an obsolete/unused directory (or directory hierarchy) in the way. */ if (git_path_isdir(git_buf_cstr(&path))) { - if ((git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) - error = -1; - else if (git_path_isdir(git_buf_cstr(&path))) { + if ((error = git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY)) < 0) { + if (error == GIT_ENOTFOUND) + error = 0; + } else if (git_path_isdir(git_buf_cstr(&path))) { giterr_set(GITERR_REFERENCE, "cannot create reflog at '%s', there are reflogs beneath that folder", ref->name); error = GIT_EDIRECTORY; diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c index d8dc77ba5..ae1a935de 100644 --- a/tests/threads/refdb.c +++ b/tests/threads/refdb.c @@ -29,11 +29,14 @@ static void *iterate_refs(void *arg) struct th_data *data = (struct th_data *) arg; git_reference_iterator *i; git_reference *ref; - int count = 0; + int count = 0, error; git_repository *repo; cl_git_pass(git_repository_open(&repo, data->path)); - cl_git_pass(git_reference_iterator_new(&i, repo)); + do { + error = git_reference_iterator_new(&i, repo); + } while (error == GIT_ELOCKED); + cl_git_pass(error); for (count = 0; !git_reference_next(&ref, i); ++count) { cl_assert(ref != NULL); @@ -61,11 +64,17 @@ static void *create_refs(void *arg) cl_git_pass(git_repository_open(&repo, data->path)); - cl_git_pass(git_reference_name_to_id(&head, repo, "HEAD")); + do { + error = git_reference_name_to_id(&head, repo, "HEAD"); + } while (error == GIT_ELOCKED); + cl_git_pass(error); for (i = 0; i < 10; ++i) { p_snprintf(name, sizeof(name), "refs/heads/thread-%03d-%02d", data->id, i); - cl_git_pass(git_reference_create(&ref[i], repo, name, &head, 0, NULL)); + do { + error = git_reference_create(&ref[i], repo, name, &head, 0, NULL); + } while (error == GIT_ELOCKED); + cl_git_pass(error); if (i == 5) { git_refdb *refdb; From aef54a466ac5403690e7ff6c72fc1252dabf5899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 14 Nov 2016 11:29:40 +0100 Subject: [PATCH 070/142] refdb: use a constant for the number of per-thread creations/deletes --- tests/threads/refdb.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c index ae1a935de..5484b71d6 100644 --- a/tests/threads/refdb.c +++ b/tests/threads/refdb.c @@ -18,6 +18,8 @@ void test_threads_refdb__cleanup(void) #define REPEAT 20 #define THREADS 20 +/* Number of references to create or delete in each thread */ +#define NREFS 10 struct th_data { int id; @@ -59,7 +61,7 @@ static void *create_refs(void *arg) struct th_data *data = (struct th_data *) arg; git_oid head; char name[128]; - git_reference *ref[10]; + git_reference *ref[NREFS]; git_repository *repo; cl_git_pass(git_repository_open(&repo, data->path)); @@ -69,14 +71,14 @@ static void *create_refs(void *arg) } while (error == GIT_ELOCKED); cl_git_pass(error); - for (i = 0; i < 10; ++i) { + for (i = 0; i < NREFS; ++i) { p_snprintf(name, sizeof(name), "refs/heads/thread-%03d-%02d", data->id, i); do { error = git_reference_create(&ref[i], repo, name, &head, 0, NULL); } while (error == GIT_ELOCKED); cl_git_pass(error); - if (i == 5) { + if (i == NREFS/2) { git_refdb *refdb; cl_git_pass(git_repository_refdb(&refdb, repo)); do { @@ -86,7 +88,7 @@ static void *create_refs(void *arg) } } - for (i = 0; i < 10; ++i) + for (i = 0; i < NREFS; ++i) git_reference_free(ref[i]); git_repository_free(repo); @@ -105,7 +107,7 @@ static void *delete_refs(void *arg) cl_git_pass(git_repository_open(&repo, data->path)); - for (i = 0; i < 10; ++i) { + for (i = 0; i < NREFS; ++i) { p_snprintf( name, sizeof(name), "refs/heads/thread-%03d-%02d", (data->id) & ~0x3, i); @@ -121,7 +123,7 @@ static void *delete_refs(void *arg) git_reference_free(ref); } - if (i == 5) { + if (i == NREFS/2) { git_refdb *refdb; cl_git_pass(git_repository_refdb(&refdb, repo)); do { From 1d41b86cd00eda52155ddddfed65dfcc41aa906a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 14 Nov 2016 12:22:20 +0100 Subject: [PATCH 071/142] tree: add a failing test for unsorted input We do not currently use the sorted version of this input in the function, which means we produce bad results. --- tests/object/tree/update.c | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/object/tree/update.c b/tests/object/tree/update.c index 54c4335f5..b76e8612a 100644 --- a/tests/object/tree/update.c +++ b/tests/object/tree/update.c @@ -196,6 +196,63 @@ void test_object_tree_update__add_blobs(void) git_tree_free(base_tree); } +void test_object_tree_update__add_blobs_unsorted(void) +{ + git_oid tree_index_id, tree_updater_id, base_id; + git_tree *base_tree; + git_index *idx; + git_index_entry entry = { {0} }; + int i; + const char *paths[] = { + "some/deep/path", + "a/path/elsewhere", + "some/other/path", + }; + + git_tree_update updates[] = { + { GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[0]}, + { GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[1]}, + { GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[2]}, + }; + + cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b")); + + entry.mode = GIT_FILEMODE_BLOB; + cl_git_pass(git_oid_fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92")); + + for (i = 0; i < 3; i++) { + cl_git_pass(git_oid_fromstr(&updates[i].id, "fa49b077972391ad58037050f2a75f74e3671e92")); + } + + for (i = 0; i < 2; i++) { + int j; + + /* Create it with an index */ + cl_git_pass(git_index_new(&idx)); + + base_tree = NULL; + if (i == 1) { + cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id)); + cl_git_pass(git_index_read_tree(idx, base_tree)); + } + + for (j = 0; j < 3; j++) { + entry.path = paths[j]; + cl_git_pass(git_index_add(idx, &entry)); + } + + cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo)); + git_index_free(idx); + + /* Perform the same operations via the tree updater */ + cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 3, updates)); + + cl_assert_equal_oid(&tree_index_id, &tree_updater_id); + } + + git_tree_free(base_tree); +} + void test_object_tree_update__add_conflict(void) { int i; From b85929c523d7e61ba76715199831ec05089dc844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 14 Nov 2016 12:44:01 +0100 Subject: [PATCH 072/142] tree: use the sorted update list in our loop The loop is made with the assumption that the inputs are sorted and not using it leads to bad outputs. --- src/tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tree.c b/src/tree.c index e338acca0..b67b16249 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1164,8 +1164,8 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli goto cleanup; for (i = 0; i < nupdates; i++) { - const git_tree_update *last_update = i == 0 ? NULL : &updates[i-1]; - const git_tree_update *update = &updates[i]; + const git_tree_update *last_update = i == 0 ? NULL : git_vector_get(&entries, i-1); + const git_tree_update *update = git_vector_get(&entries, i); size_t common_prefix = 0, steps_up, j; const char *path; From 8977658519835805af5327e51f5a84354417839d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 14 Nov 2016 12:44:52 +0100 Subject: [PATCH 073/142] tree: look for conflicts in the new tree when updating We look at whether we're trying to replace a blob with a tree during the update phase, but we fail to look at whether we've just inserted a blob where we're now trying to insert a tree. Update the check to look at both places. The test for this was previously succeeding due to the bu where we did not look at the sorted output. --- src/tree.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tree.c b/src/tree.c index b67b16249..9655ad739 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1200,6 +1200,9 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli last = git_array_last(stack); entry = last->tree ? git_tree_entry_byname(last->tree, component.ptr) : NULL; + if (!entry) + entry = treebuilder_get(last->bld, component.ptr); + if (entry && git_tree_entry_type(entry) != GIT_OBJ_TREE) { giterr_set(GITERR_TREE, "D/F conflict when updating tree"); error = -1; From 21e0fc32abe6e73a5efc5c4401da69e2c133b099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 14 Nov 2016 17:55:49 +0100 Subject: [PATCH 074/142] Plug a leak in the refs compressor --- src/refdb_fs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 7601aa0ac..558d06094 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -927,6 +927,7 @@ static int packed_remove_loose(refdb_fs_backend *backend) continue; if (error < 0) { + git_buf_free(&ref_content); giterr_set(GITERR_REFERENCE, "failed to lock loose reference '%s'", ref->name); return error; } @@ -957,6 +958,7 @@ static int packed_remove_loose(refdb_fs_backend *backend) p_unlink(lock.path_original); } + git_buf_free(&ref_content); git_filebuf_cleanup(&lock); return 0; } From 06de4e759a456861c0c402d7f5522f18d1b2f160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 14 Nov 2016 14:12:13 +0100 Subject: [PATCH 075/142] CHANGELOG: fill in some updates we missed --- CHANGELOG.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dae86de4a..630736501 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,14 +17,37 @@ v0.24 + 1 * Improve the performance of the revwalk and bring us closer to git's code. +* The reference db has improved support for concurrency and returns `GIT_ELOCKED` + when an operation could not be performed due to locking. + +* Nanosecond resolution is now activated by default, following git's change to + do this. + +* We now restrict the set of ciphers we let OpenSSL use by default. + +* Users can now register their own merge drivers for use with `.gitattributes`. + The library also gained built-in support for the union merge driver. + +* The default for creating references is now to validate that the object does + exist. + +* Add `git_proxy_options` which is used by the different networking + implementations to let the caller specify the proxy settings instead of + relying on the environment variables. + ### API additions * You can now get the user-agent used by libgit2 using the `GIT_OPT_GET_USER_AGENT` option with `git_libgit2_opts()`. It is the counterpart to `GIT_OPT_SET_USER_AGENT`. +* The `GIT_OPT_SET_SSL_CIPHERS` option for `git_libgit2_opts()` lets you specify + a custom list of ciphers to use for OpenSSL. + * `git_commit_create_buffer()` creates a commit and writes it into a - user-provided buffer instead of writing it into the object db. + user-provided buffer instead of writing it into the object db. Combine it with + `git_commit_create_with_signature()` in order to create a commit with a + cryptographic signature. * `git_blob_create_fromstream()` and `git_blob_create_fromstream_commit()` allow you to create a blob by @@ -50,12 +73,48 @@ v0.24 + 1 `git_repository_open_ext` with this flag will error out if either `$GIT_WORK_TREE` or `$GIT_COMMON_DIR` is set. -* `git_diff_from_buffer` can create a `git_diff` object from the contents +* `git_diff_from_buffer()` can create a `git_diff` object from the contents of a git-style patch file. * `git_index_version()` and `git_index_set_version()` to get and set the index version +* `git_odb_expand_ids()` lets you check for the existence of multiple + objects at once. + +* The new `git_blob_dup()`, `git_commit_dup()`, `git_tag_dup()` and + `git_tree_dup()` functions provide type-specific wrappers for + `git_object_dup()` to reduce noise and increase type safety for callers. + +* `git_reference_dup()` lets you duplicate a reference to aid in ownership + management and cleanup. + +* `git_signature_from_buffer()` lets you create a signature from a string in the + format that appear in objects. + +* `git_tree_create_updated()` lets you create a tree based on another one + together with a list of updates. For the covered update cases, it's more + efficient than the `git_index` route. + +* `git_apply_patch()` applies hunks from a `git_patch` to a buffer. + +* `git_diff_to_buf()` lets you print an entire diff directory to a buffer, + similar to how `git_patch_to_buf()` works. + +* `git_proxy_init_options()` is added to initialize a `git_proxy_options` + structure at run-time. + +* `git_merge_driver_register()`, `git_merge_driver_unregister()` let you + register and unregister a custom merge driver to be used when `.gitattributes` + specifies it. + +* `git_merge_driver_lookup()` can be used to look up a merge driver by name. + +* `git_merge_driver_source_repo()`, `git_merge_driver_source_ancestor()`, + `git_merge_driver_source_ours()`, `git_merge_driver_source_theirs()`, + `git_merge_driver_source_file_options()` added as accessors to + `git_merge_driver_source`. + ### API removals * `git_blob_create_fromchunks()` has been removed in favour of @@ -80,6 +139,8 @@ v0.24 + 1 If this is `NULL`, then it will not be called and the `exists` function will be used instead. +* `git_remote_connect()` now accepts proxy options. + v0.24 ------- From 8c984fea02578da9a8f0ed2fb5d45d5a243f0eab Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 13 Oct 2016 16:40:43 +0100 Subject: [PATCH 076/142] Introduce a GitHub Issue Template --- .github/ISSUE_TEMPLATE | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE diff --git a/.github/ISSUE_TEMPLATE b/.github/ISSUE_TEMPLATE new file mode 100644 index 000000000..1e432aeef --- /dev/null +++ b/.github/ISSUE_TEMPLATE @@ -0,0 +1,14 @@ +You are opening a _bug report_ against the libgit2 project. If you have a +question about an API or usage, please ask on StackOverflow: +http://stackoverflow.com/questions/tagged/libgit2. Please fill out the +reproduction steps (below) and delete this introductory paragraph. Thanks! + +### Reproduction steps + +### Expected behavior + +### Actual behavior + +### Version of libgit2 (release number or SHA1) + +### Operating system(s) tested From 5cbd52607c7488d7b9680e743887b08926a24710 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 11 Nov 2016 11:37:00 +0100 Subject: [PATCH 077/142] curl_stream: use CURLINFO_ACTIVESOCKET if curl is recent enough The `CURLINFO_LASTSOCKET` information has been deprecated since curl version 7.45.0 as it may result in an overflow in the returned socket on certain systems, most importantly on 64 bit Windows. Instead, a new call `CURLINFO_ACTIVESOCKET` has been added which instead returns a `curl_socket_t`, which is always sufficiently long to store a socket. As we need to provide backwards compatibility with curl versions smaller than 7.45.0, alias CURLINFO_ACTIVESOCKET to CURLINFO_LASTSOCKET on platforms without CURLINFO_ACTIVESOCKET. --- src/curl_stream.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/curl_stream.c b/src/curl_stream.c index 3a3f364b2..4e0455cca 100644 --- a/src/curl_stream.c +++ b/src/curl_stream.c @@ -15,6 +15,16 @@ #include "vector.h" #include "proxy.h" +/* This is for backwards compatibility with curl<7.45.0. */ +#ifndef CURLINFO_ACTIVESOCKET +# define CURLINFO_ACTIVESOCKET CURLINFO_LASTSOCKET +# define GIT_CURL_BADSOCKET -1 +# define git_activesocket_t long +#else +# define GIT_CURL_BADSOCKET CURL_SOCKET_BAD +# define git_activesocket_t curl_socket_t +#endif + typedef struct { git_stream parent; CURL *handle; @@ -87,7 +97,8 @@ static int ask_and_apply_proxy_creds(curl_stream *s) static int curls_connect(git_stream *stream) { curl_stream *s = (curl_stream *) stream; - long sockextr, connect_last = 0; + git_activesocket_t sockextr; + long connect_last = 0; int failed_cert = 0, error; bool retry_connect; CURLcode res; @@ -117,11 +128,11 @@ static int curls_connect(git_stream *stream) if (res == CURLE_PEER_FAILED_VERIFICATION) failed_cert = 1; - if ((res = curl_easy_getinfo(s->handle, CURLINFO_LASTSOCKET, &sockextr)) != CURLE_OK) { + if ((res = curl_easy_getinfo(s->handle, CURLINFO_ACTIVESOCKET, &sockextr)) != CURLE_OK) { return seterr_curl(s); } - if (sockextr == -1) { + if (sockextr == GIT_CURL_BADSOCKET) { giterr_set(GITERR_NET, "curl socket is no longer valid"); return -1; } From 5569778a520a1216d2dc15ea6e73ae2c779c2feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 15 Nov 2016 11:15:40 +0100 Subject: [PATCH 078/142] Bump version number to v0.25 --- CHANGELOG.md | 13 ++++++++++++- include/git2/version.h | 6 +++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 630736501..8544ac451 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,15 @@ -v0.24 + 1 +v0.25 + 1 +------- + +### Changes or improvements + +### API additions + +### API removals + +### Breaking API changes + +v0.25 ------- ### Changes or improvements diff --git a/include/git2/version.h b/include/git2/version.h index 66a6623cd..0df191f4f 100644 --- a/include/git2/version.h +++ b/include/git2/version.h @@ -7,12 +7,12 @@ #ifndef INCLUDE_git_version_h__ #define INCLUDE_git_version_h__ -#define LIBGIT2_VERSION "0.24.0" +#define LIBGIT2_VERSION "0.25.0" #define LIBGIT2_VER_MAJOR 0 -#define LIBGIT2_VER_MINOR 24 +#define LIBGIT2_VER_MINOR 25 #define LIBGIT2_VER_REVISION 0 #define LIBGIT2_VER_PATCH 0 -#define LIBGIT2_SOVERSION 24 +#define LIBGIT2_SOVERSION 25 #endif From 24b2182c5a77945bedbc5de0f1b4a9b6f5d27284 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 15 Nov 2016 12:53:53 +0100 Subject: [PATCH 079/142] sortedcache: plug leaked file descriptor --- src/sortedcache.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sortedcache.c b/src/sortedcache.c index ed4199b71..5bd989a9f 100644 --- a/src/sortedcache.c +++ b/src/sortedcache.c @@ -216,6 +216,7 @@ int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf) if (p_fstat(fd, &st) < 0) { giterr_set(GITERR_OS, "failed to stat file"); error = -1; + (void)p_close(fd); goto unlock; } From 613381fc1461514fdbb1518799bcee0345fdece6 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 15 Nov 2016 13:33:05 +0100 Subject: [PATCH 080/142] patch_parse: fix memory leak --- src/patch_parse.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/patch_parse.c b/src/patch_parse.c index 7a4fe9f1a..f5275947d 100644 --- a/src/patch_parse.c +++ b/src/patch_parse.c @@ -1014,8 +1014,10 @@ git_patch_parse_ctx *git_patch_parse_ctx_init( return NULL; if (content_len) { - if ((ctx->content = git__malloc(content_len)) == NULL) + if ((ctx->content = git__malloc(content_len)) == NULL) { + git__free(ctx); return NULL; + } memcpy((char *)ctx->content, content, content_len); } From 65b78ea3013c3e1a5fd70e4dee6e08996cdcaa62 Mon Sep 17 00:00:00 2001 From: Pranit Bauva Date: Thu, 17 Nov 2016 01:08:49 +0530 Subject: [PATCH 081/142] use `giterr_set_str()` wherever possible `giterr_set()` is used when it is required to format a string, and since we don't really require it for this case, it is better to stick to `giterr_set_str()`. This also suppresses a warning(-Wformat-security) raised by the compiler. Signed-off-by: Pranit Bauva --- src/index.c | 2 +- src/zstream.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.c b/src/index.c index bc15959a8..42579f19a 100644 --- a/src/index.c +++ b/src/index.c @@ -552,7 +552,7 @@ int git_index_clear(git_index *index) static int create_index_error(int error, const char *msg) { - giterr_set(GITERR_INDEX, msg); + giterr_set_str(GITERR_INDEX, msg); return error; } diff --git a/src/zstream.c b/src/zstream.c index d9ad4ca89..d949aa81a 100644 --- a/src/zstream.c +++ b/src/zstream.c @@ -21,7 +21,7 @@ static int zstream_seterr(git_zstream *zs) if (zs->zerr == Z_MEM_ERROR) giterr_set_oom(); else if (zs->z.msg) - giterr_set(GITERR_ZLIB, zs->z.msg); + giterr_set_str(GITERR_ZLIB, zs->z.msg); else giterr_set(GITERR_ZLIB, "Unknown compression error"); From 82f15896deb06d396d76d706f6c0146197d14b2c Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Nov 2016 07:19:22 -0500 Subject: [PATCH 082/142] threads: introduce `git_thread_exit` Introduce `git_thread_exit`, which will allow threads to terminate at an arbitrary time, returning a `void *`. On Windows, this means that we need to store the current `git_thread` in TLS, so that we can set its `return` value when terminating. We cannot simply use `ExitThread`, since Win32 returns `DWORD`s from threads; we return `void *`. --- src/global.h | 6 ++++++ src/unix/pthread.h | 2 ++ src/win32/thread.c | 18 ++++++++++++++++++ src/win32/thread.h | 2 ++ tests/threads/basic.c | 27 +++++++++++++++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/src/global.h b/src/global.h index 219951525..88f40aad1 100644 --- a/src/global.h +++ b/src/global.h @@ -16,6 +16,12 @@ typedef struct { git_error error_t; git_buf error_buf; char oid_fmt[GIT_OID_HEXSZ+1]; + + /* On Windows, this is the current child thread that was started by + * `git_thread_create`. This is used to set the thread's exit code + * when terminated by `git_thread_exit`. It is unused on POSIX. + */ + git_thread *current_thread; } git_global_st; #ifdef GIT_OPENSSL diff --git a/src/unix/pthread.h b/src/unix/pthread.h index 0f3f17927..3f23d10d5 100644 --- a/src/unix/pthread.h +++ b/src/unix/pthread.h @@ -17,6 +17,8 @@ typedef struct { pthread_create(&(git_thread_ptr)->thread, NULL, start_routine, arg) #define git_thread_join(git_thread_ptr, status) \ pthread_join((git_thread_ptr)->thread, status) +#define git_thread_currentid() ((size_t)(pthread_self())) +#define git_thread_exit(retval) pthread_exit(retval) /* Git Mutex */ #define git_mutex pthread_mutex_t diff --git a/src/win32/thread.c b/src/win32/thread.c index 80d56ce5d..87318c9d3 100644 --- a/src/win32/thread.c +++ b/src/win32/thread.c @@ -26,6 +26,9 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter) { git_thread *thread = lpParameter; + /* Set the current thread for `git_thread_exit` */ + GIT_GLOBAL->current_thread = thread; + thread->result = thread->proc(thread->param); git__free_tls_data(); @@ -95,6 +98,21 @@ int git_thread_join( return 0; } +void git_thread_exit(void *value) +{ + assert(GIT_GLOBAL->current_thread); + GIT_GLOBAL->current_thread->result = value; + + git__free_tls_data(); + + ExitThread(CLEAN_THREAD_EXIT); +} + +size_t git_thread_currentid(void) +{ + return GetCurrentThreadId(); +} + int git_mutex_init(git_mutex *GIT_RESTRICT mutex) { InitializeCriticalSection(mutex); diff --git a/src/win32/thread.h b/src/win32/thread.h index 0d01822a6..7f4a2170f 100644 --- a/src/win32/thread.h +++ b/src/win32/thread.h @@ -41,6 +41,8 @@ int git_thread_create(git_thread *GIT_RESTRICT, void *(*) (void *), void *GIT_RESTRICT); int git_thread_join(git_thread *, void **); +size_t git_thread_currentid(void); +void git_thread_exit(void *); int git_mutex_init(git_mutex *GIT_RESTRICT mutex); int git_mutex_free(git_mutex *); diff --git a/tests/threads/basic.c b/tests/threads/basic.c index 9c342bc42..685452d44 100644 --- a/tests/threads/basic.c +++ b/tests/threads/basic.c @@ -48,3 +48,30 @@ void test_threads_basic__set_error(void) { run_in_parallel(1, 4, set_error, NULL, NULL); } + +static void *return_normally(void *param) +{ + return param; +} + +static void *exit_abruptly(void *param) +{ + git_thread_exit(param); + return NULL; +} + +void test_threads_basic__exit(void) +{ + git_thread thread; + void *result; + + /* Ensure that the return value of the threadproc is returned. */ + cl_git_pass(git_thread_create(&thread, return_normally, (void *)424242)); + cl_git_pass(git_thread_join(&thread, &result)); + cl_assert_equal_sz(424242, (size_t)result); + + /* Ensure that the return value of `git_thread_exit` is returned. */ + cl_git_pass(git_thread_create(&thread, return_normally, (void *)232323)); + cl_git_pass(git_thread_join(&thread, &result)); + cl_assert_equal_sz(232323, (size_t)result); +} \ No newline at end of file From 99479062dbb738ebc5892e163b701680cea91980 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Nov 2016 16:50:34 +0000 Subject: [PATCH 083/142] core::init tests: reverse init/shutdown We want a predictable number of initializations in our multithreaded init test, but we also want to make sure that we have _actually_ initialized `git_libgit2_init` before calling `git_thread_create` (since it now has a sanity check that `git_libgit2_init` has been called). Since `git_thread_create` is internal-only, keep this sanity check. Flip the invocation so that we `git_libgit2_init` before our thread tests and `git_libgit2_shutdown` again after. --- tests/core/init.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/core/init.c b/tests/core/init.c index cd90b378d..a8cbd930b 100644 --- a/tests/core/init.c +++ b/tests/core/init.c @@ -39,14 +39,14 @@ void test_core_init__concurrent_init_succeeds(void) git_thread threads[10]; unsigned i; - cl_assert_equal_i(0, git_libgit2_shutdown()); + cl_assert_equal_i(2, git_libgit2_init()); for (i = 0; i < ARRAY_SIZE(threads); i++) git_thread_create(&threads[i], reinit, NULL); for (i = 0; i < ARRAY_SIZE(threads); i++) git_thread_join(&threads[i], NULL); - cl_assert_equal_i(1, git_libgit2_init()); + cl_assert_equal_i(1, git_libgit2_shutdown()); cl_sandbox_set_search_path_defaults(); #else cl_skip(); From bbf22f8229718571059cd579945b134772f9a73d Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Nov 2016 07:34:20 -0500 Subject: [PATCH 084/142] clar: Introduce assertion helpers for threads Don't `cl_git_pass` in a child thread. When the assertion fails, clar will `longjmp` to its error handler, but: > The effect of a call to longjmp() where initialization of the jmp_buf > structure was not performed in the calling thread is undefined. Instead, set up an error context that threads can populate, and the caller can check. --- tests/clar_libgit2.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h index d7e635302..1d8d4a50b 100644 --- a/tests/clar_libgit2.h +++ b/tests/clar_libgit2.h @@ -41,6 +41,47 @@ } \ } while(0) +/** + * Thread safe assertions; you cannot use `cl_git_report_failure` from a + * child thread since it will try to `longjmp` to abort and "the effect of + * a call to longjmp() where initialization of the jmp_buf structure was + * not performed in the calling thread is undefined." + * + * Instead, callers can provide a clar thread error context to a thread, + * which will populate and return it on failure. Callers can check the + * status with `cl_git_thread_check`. + */ +typedef struct { + int error; + const char *file; + int line; + const char *expr; + char error_msg[4096]; +} cl_git_thread_err; + +#define cl_git_thread_pass(threaderr, expr) cl_git_thread_pass_(threaderr, (expr), __FILE__, __LINE__) + +#define cl_git_thread_pass_(__threaderr, __expr, __file, __line) do { \ + giterr_clear(); \ + if ((((cl_git_thread_err *)__threaderr)->error = (__expr)) != 0) { \ + const git_error *_last = giterr_last(); \ + ((cl_git_thread_err *)__threaderr)->file = __file; \ + ((cl_git_thread_err *)__threaderr)->line = __line; \ + ((cl_git_thread_err *)__threaderr)->expr = "Function call failed: " #__expr; \ + p_snprintf(((cl_git_thread_err *)__threaderr)->error_msg, 4096, "thread 0x%" PRIxZ " - error %d - %s", \ + git_thread_currentid(), ((cl_git_thread_err *)__threaderr)->error, \ + _last ? _last->message : ""); \ + git_thread_exit(__threaderr); \ + } \ + } while (0) + +static void cl_git_thread_check(void *data) +{ + cl_git_thread_err *threaderr = (cl_git_thread_err *)data; + if (threaderr->error != 0) + clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1); +} + void cl_git_report_failure(int, const char *, int, const char *); #define cl_assert_at_line(expr,file,line) \ From 6a05c7a0efd60dce3e99673c3256bf60516693ed Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Nov 2016 07:37:47 -0500 Subject: [PATCH 085/142] threads::refdb tests: use new threaded clar assert --- tests/threads/refdb.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c index 5484b71d6..e2f7563c7 100644 --- a/tests/threads/refdb.c +++ b/tests/threads/refdb.c @@ -22,6 +22,7 @@ void test_threads_refdb__cleanup(void) #define NREFS 10 struct th_data { + cl_git_thread_err error; int id; const char *path; }; @@ -34,11 +35,11 @@ static void *iterate_refs(void *arg) int count = 0, error; git_repository *repo; - cl_git_pass(git_repository_open(&repo, data->path)); + cl_git_thread_pass(data, git_repository_open(&repo, data->path)); do { error = git_reference_iterator_new(&i, repo); } while (error == GIT_ELOCKED); - cl_git_pass(error); + cl_git_thread_pass(data, error); for (count = 0; !git_reference_next(&ref, i); ++count) { cl_assert(ref != NULL); @@ -64,26 +65,27 @@ static void *create_refs(void *arg) git_reference *ref[NREFS]; git_repository *repo; - cl_git_pass(git_repository_open(&repo, data->path)); + cl_git_thread_pass(data, git_repository_open(&repo, data->path)); do { error = git_reference_name_to_id(&head, repo, "HEAD"); } while (error == GIT_ELOCKED); - cl_git_pass(error); + cl_git_thread_pass(data, error); for (i = 0; i < NREFS; ++i) { p_snprintf(name, sizeof(name), "refs/heads/thread-%03d-%02d", data->id, i); do { error = git_reference_create(&ref[i], repo, name, &head, 0, NULL); } while (error == GIT_ELOCKED); - cl_git_pass(error); + cl_git_thread_pass(data, error); if (i == NREFS/2) { git_refdb *refdb; - cl_git_pass(git_repository_refdb(&refdb, repo)); + cl_git_thread_pass(data, git_repository_refdb(&refdb, repo)); do { error = git_refdb_compress(refdb); } while (error == GIT_ELOCKED); + cl_git_thread_pass(data, error); git_refdb_free(refdb); } } @@ -105,7 +107,7 @@ static void *delete_refs(void *arg) char name[128]; git_repository *repo; - cl_git_pass(git_repository_open(&repo, data->path)); + cl_git_thread_pass(data, git_repository_open(&repo, data->path)); for (i = 0; i < NREFS; ++i) { p_snprintf( @@ -119,17 +121,17 @@ static void *delete_refs(void *arg) if (error == GIT_ENOTFOUND) error = 0; - cl_git_pass(error); + cl_git_thread_pass(data, error); git_reference_free(ref); } if (i == NREFS/2) { git_refdb *refdb; - cl_git_pass(git_repository_refdb(&refdb, repo)); + cl_git_thread_pass(data, git_repository_refdb(&refdb, repo)); do { error = git_refdb_compress(refdb); } while (error == GIT_ELOCKED); - cl_git_pass(error); + cl_git_thread_pass(data, error); git_refdb_free(refdb); } } @@ -194,6 +196,7 @@ void test_threads_refdb__edit_while_iterate(void) #ifdef GIT_THREADS for (t = 0; t < THREADS; ++t) { cl_git_pass(git_thread_join(&th[t], NULL)); + cl_git_thread_check(&th_data[t]); } memset(th, 0, sizeof(th)); @@ -205,6 +208,7 @@ void test_threads_refdb__edit_while_iterate(void) for (t = 0; t < THREADS; ++t) { cl_git_pass(git_thread_join(&th[t], NULL)); + cl_git_thread_check(&th_data[t]); } #endif } From 6367c58cd482288e5cd476bd48d0d4406e3bac7b Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 18 Nov 2016 18:30:20 +0000 Subject: [PATCH 086/142] tests: handle life without threads --- tests/clar_libgit2.h | 6 +++++- tests/threads/basic.c | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h index 1d8d4a50b..663d1362a 100644 --- a/tests/clar_libgit2.h +++ b/tests/clar_libgit2.h @@ -59,7 +59,11 @@ typedef struct { char error_msg[4096]; } cl_git_thread_err; -#define cl_git_thread_pass(threaderr, expr) cl_git_thread_pass_(threaderr, (expr), __FILE__, __LINE__) +#ifdef GIT_THREADS +# define cl_git_thread_pass(threaderr, expr) cl_git_thread_pass_(threaderr, (expr), __FILE__, __LINE__) +#else +# define cl_git_thread_pass(threaderr, expr) cl_git_pass(expr) +#endif #define cl_git_thread_pass_(__threaderr, __expr, __file, __line) do { \ giterr_clear(); \ diff --git a/tests/threads/basic.c b/tests/threads/basic.c index 685452d44..a9310bbd4 100644 --- a/tests/threads/basic.c +++ b/tests/threads/basic.c @@ -49,6 +49,7 @@ void test_threads_basic__set_error(void) run_in_parallel(1, 4, set_error, NULL, NULL); } +#ifdef GIT_THREADS static void *return_normally(void *param) { return param; @@ -59,9 +60,13 @@ static void *exit_abruptly(void *param) git_thread_exit(param); return NULL; } +#endif void test_threads_basic__exit(void) { +#ifndef GIT_THREADS + clar__skip(); +#else git_thread thread; void *result; @@ -74,4 +79,5 @@ void test_threads_basic__exit(void) cl_git_pass(git_thread_create(&thread, return_normally, (void *)232323)); cl_git_pass(git_thread_join(&thread, &result)); cl_assert_equal_sz(232323, (size_t)result); -} \ No newline at end of file +#endif +} From 86364af99590b3df7e7f647eb92a9449b29cee57 Mon Sep 17 00:00:00 2001 From: Boris Barbulovski Date: Sun, 20 Nov 2016 11:30:45 +0100 Subject: [PATCH 087/142] Properly pass `wchar *` type to giterr_set --- src/win32/w32_util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/win32/w32_util.h b/src/win32/w32_util.h index 2e475e5e9..66987cc6b 100644 --- a/src/win32/w32_util.h +++ b/src/win32/w32_util.h @@ -174,7 +174,7 @@ GIT_INLINE(int) git_win32__file_attribute_to_stat( /* st_size gets the UTF-8 length of the target name, in bytes, * not counting the NULL terminator */ if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) { - giterr_set(GITERR_OS, "Could not convert reparse point name for '%s'", path); + giterr_set(GITERR_OS, "Could not convert reparse point name for '%S'", path); return -1; } } From 236a690ce4cbcbaf243dda8853832de8485bac3f Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Sun, 20 Nov 2016 22:46:01 -0800 Subject: [PATCH 088/142] Allow Windows with WinHTTP to use external http-parser --- CMakeLists.txt | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f26f46879..8cc43c6ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -235,10 +235,21 @@ IF (WIN32 AND EMBED_SSH_PATH) ADD_DEFINITIONS(-DGIT_SSH) ENDIF() -IF (WIN32 AND WINHTTP) - ADD_DEFINITIONS(-DGIT_WINHTTP) + +FIND_PACKAGE(HTTP_Parser) +IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2) + INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS}) + LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES}) + LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser") +ELSE() + MESSAGE(STATUS "http-parser was not found or is too old; using bundled 3rd-party sources.") INCLUDE_DIRECTORIES(deps/http-parser) FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h) +ENDIF() + + +IF (WIN32 AND WINHTTP) + ADD_DEFINITIONS(-DGIT_WINHTTP) # Since MinGW does not come with headers or an import library for winhttp, # we have to include a private header and generate our own import library @@ -291,17 +302,6 @@ ELSE () LINK_LIBRARIES(${CURL_LIBRARIES}) LIST(APPEND LIBGIT2_PC_LIBS ${CURL_LDFLAGS}) ENDIF() - - FIND_PACKAGE(HTTP_Parser) - IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2) - INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS}) - LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES}) - LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser") - ELSE() - MESSAGE(STATUS "http-parser was not found or is too old; using bundled 3rd-party sources.") - INCLUDE_DIRECTORIES(deps/http-parser) - FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h) - ENDIF() ENDIF() # Specify sha1 implementation From 4db1fc7e5ead5c29ffb6594b229b84f4392b40f1 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 1 Dec 2016 23:06:41 -0500 Subject: [PATCH 089/142] git_rebase_init: correctly handle detached HEAD git_rebase_finish relies on head_detached being set, but rebase_init_merge was only setting it when branch->ref_name was unset. But branch->ref_name would be set to "HEAD" in the case of detached HEAD being either implicitly (NULL) or explicitly passed to git_rebase_init. --- src/rebase.c | 2 +- tests/rebase/merge.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/rebase.c b/src/rebase.c index e86312e7e..0af2b3cf4 100644 --- a/src/rebase.c +++ b/src/rebase.c @@ -630,7 +630,7 @@ static int rebase_init_merge( rebase->state_path = git_buf_detach(&state_path); GITERR_CHECK_ALLOC(rebase->state_path); - if (branch->ref_name) { + if (branch->ref_name && strcmp(branch->ref_name, "HEAD")) { rebase->orig_head_name = git__strdup(branch->ref_name); GITERR_CHECK_ALLOC(rebase->orig_head_name); } else { diff --git a/tests/rebase/merge.c b/tests/rebase/merge.c index 0f06ed153..8492c638f 100644 --- a/tests/rebase/merge.c +++ b/tests/rebase/merge.c @@ -1,4 +1,5 @@ #include "clar_libgit2.h" +#include "git2/checkout.h" #include "git2/rebase.h" #include "posix.h" #include "signature.h" @@ -475,6 +476,58 @@ void test_rebase_merge__finish(void) git_rebase_free(rebase); } +void test_rebase_merge__detached_finish(void) +{ + git_rebase *rebase; + git_reference *branch_ref, *upstream_ref, *head_ref; + git_annotated_commit *branch_head, *upstream_head; + git_rebase_operation *rebase_operation; + git_oid commit_id; + git_reflog *reflog; + const git_reflog_entry *reflog_entry; + git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; + int error; + + cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy")); + cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal")); + + cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref)); + cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref)); + + cl_git_pass(git_repository_set_head_detached_from_annotated(repo, branch_head)); + opts.checkout_strategy = GIT_CHECKOUT_FORCE; + git_checkout_head(repo, &opts); + + cl_git_pass(git_rebase_init(&rebase, repo, NULL, upstream_head, NULL, NULL)); + + cl_git_pass(git_rebase_next(&rebase_operation, rebase)); + cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, + NULL, NULL)); + + cl_git_fail(error = git_rebase_next(&rebase_operation, rebase)); + cl_assert_equal_i(GIT_ITEROVER, error); + + cl_git_pass(git_rebase_finish(rebase, signature)); + + cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo)); + + cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD")); + cl_assert_equal_i(GIT_REF_OID, git_reference_type(head_ref)); + + /* Make sure the reflogs are updated appropriately */ + cl_git_pass(git_reflog_read(&reflog, repo, "HEAD")); + cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0)); + cl_assert_equal_oid(git_annotated_commit_id(upstream_head), git_reflog_entry_id_old(reflog_entry)); + cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry)); + + git_reflog_free(reflog); + git_annotated_commit_free(branch_head); + git_annotated_commit_free(upstream_head); + git_reference_free(branch_ref); + git_reference_free(upstream_ref); + git_rebase_free(rebase); +} + void test_rebase_merge__finish_with_ids(void) { git_rebase *rebase; From 72cee168901cb384d7ceed4492362dc7cf31a6d3 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 2 Dec 2016 16:14:47 -0800 Subject: [PATCH 090/142] remote: fix typo in git_fetch_init_options docs --- include/git2/remote.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/git2/remote.h b/include/git2/remote.h index c459f42cc..53c4aee46 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -569,7 +569,7 @@ typedef struct { * Initializes a `git_fetch_options` with default values. Equivalent to * creating an instance with GIT_FETCH_OPTIONS_INIT. * - * @param opts the `git_push_options` instance to initialize. + * @param opts the `git_fetch_options` instance to initialize. * @param version the version of the struct; you should pass * `GIT_FETCH_OPTIONS_VERSION` here. * @return Zero on success; -1 on failure. From 9af59f5dcd7ae7ae8210811bbe5458934a55e112 Mon Sep 17 00:00:00 2001 From: Boris Barbulovski Date: Tue, 6 Dec 2016 03:08:52 +0100 Subject: [PATCH 091/142] Properly pass `wchar *` type to giterr_set --- src/win32/w32_util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/win32/w32_util.h b/src/win32/w32_util.h index 66987cc6b..784a7a04c 100644 --- a/src/win32/w32_util.h +++ b/src/win32/w32_util.h @@ -174,7 +174,7 @@ GIT_INLINE(int) git_win32__file_attribute_to_stat( /* st_size gets the UTF-8 length of the target name, in bytes, * not counting the NULL terminator */ if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) { - giterr_set(GITERR_OS, "Could not convert reparse point name for '%S'", path); + giterr_set(GITERR_OS, "Could not convert reparse point name for '%ls'", path); return -1; } } From ab0cc5a0598ecbfa2cffe7248deb5256993eb86e Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 25 Nov 2016 14:58:16 +0100 Subject: [PATCH 092/142] clar: mark `cl_git_thread_check()` as inline The function `cl_git_thread_check()` is defined as static. As the function is defined in a header file which is included by our tests, this can result in warnings for every test file where `cl_git_thread_check` is never used. Fix the issue by marking it as inline instead. --- tests/clar_libgit2.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h index 663d1362a..fc08bbf1f 100644 --- a/tests/clar_libgit2.h +++ b/tests/clar_libgit2.h @@ -79,7 +79,7 @@ typedef struct { } \ } while (0) -static void cl_git_thread_check(void *data) +GIT_INLINE(void) cl_git_thread_check(void *data) { cl_git_thread_err *threaderr = (cl_git_thread_err *)data; if (threaderr->error != 0) From 8468a4406775ddecaebb4f896e74632c76b12411 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 25 Nov 2016 15:00:20 +0100 Subject: [PATCH 093/142] odb_mempack: mark zero-length array as GIT_FLEX_ARRAY --- src/odb_mempack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/odb_mempack.c b/src/odb_mempack.c index 594a2784c..68db3bc32 100644 --- a/src/odb_mempack.c +++ b/src/odb_mempack.c @@ -24,7 +24,7 @@ struct memobject { git_oid oid; size_t len; git_otype type; - char data[]; + char data[GIT_FLEX_ARRAY]; }; struct memory_packer_db { From 013ecb4f2a7f2acacec9220448e93911edb1faf9 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 25 Nov 2016 15:00:50 +0100 Subject: [PATCH 094/142] revwalk: do not re-declare `commit` variable --- src/revwalk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/revwalk.c b/src/revwalk.c index 0ada5870a..f5502a72e 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -290,7 +290,7 @@ static void mark_parents_uninteresting(git_commit_list_node *commit) while (parents) { - git_commit_list_node *commit = git_commit_list_pop(&parents); + commit = git_commit_list_pop(&parents); while (commit) { if (commit->uninteresting) From 6cf575b1ad989fbb8a239dd6acc26d72286eb4cb Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 25 Nov 2016 15:01:04 +0100 Subject: [PATCH 095/142] path: remove unused local variable --- src/path.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/path.c b/src/path.c index 2b1a9622e..767552778 100644 --- a/src/path.c +++ b/src/path.c @@ -1145,7 +1145,6 @@ int git_path_diriter_init( unsigned int flags) { git_win32_path path_filter; - git_buf hack = {0}; static int is_win7_or_later = -1; if (is_win7_or_later < 0) From 482d17484e2c35593c67c96a06e96c06d16bb1f7 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 25 Nov 2016 15:01:35 +0100 Subject: [PATCH 096/142] transports: smart: do not redeclare loop counters --- src/transports/smart_protocol.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c index c1e412436..c41de4e23 100644 --- a/src/transports/smart_protocol.c +++ b/src/transports/smart_protocol.c @@ -412,12 +412,12 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c if (i % 20 == 0 && t->rpc) { git_pkt_ack *pkt; - unsigned int i; + unsigned int j; if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0) goto on_error; - git_vector_foreach(&t->common, i, pkt) { + git_vector_foreach(&t->common, j, pkt) { if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0) goto on_error; } @@ -432,12 +432,12 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c /* Tell the other end that we're done negotiating */ if (t->rpc && t->common.length > 0) { git_pkt_ack *pkt; - unsigned int i; + unsigned int j; if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0) goto on_error; - git_vector_foreach(&t->common, i, pkt) { + git_vector_foreach(&t->common, j, pkt) { if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0) goto on_error; } From e781a0c52f2816ea5e9ba83f58b9808c0e42024f Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 25 Nov 2016 15:02:07 +0100 Subject: [PATCH 097/142] graph: flag fields should be declared as unsigned --- src/graph.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graph.c b/src/graph.c index 8accd808c..948f7d306 100644 --- a/src/graph.c +++ b/src/graph.c @@ -59,7 +59,7 @@ static int mark_parents(git_revwalk *walk, git_commit_list_node *one, /* as long as there are non-STALE commits */ while (interesting(&list, roots)) { git_commit_list_node *commit = git_pqueue_pop(&list); - int flags; + unsigned int flags; if (commit == NULL) break; From 34b320535bde5505429f1349fc7d5cc423857a20 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 25 Nov 2016 15:02:34 +0100 Subject: [PATCH 098/142] Fix potential use of uninitialized values --- src/pack.c | 4 +++- src/patch_generate.c | 2 +- src/transports/smart_protocol.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pack.c b/src/pack.c index 2ee0c60e4..56de64d57 100644 --- a/src/pack.c +++ b/src/pack.c @@ -509,8 +509,10 @@ int git_packfile_resolve_header( git_packfile_stream_free(&stream); if (error < 0) return error; - } else + } else { *size_p = size; + base_offset = 0; + } while (type == GIT_OBJ_OFS_DELTA || type == GIT_OBJ_REF_DELTA) { curpos = base_offset; diff --git a/src/patch_generate.c b/src/patch_generate.c index a13f2ff5d..0e5d1db31 100644 --- a/src/patch_generate.c +++ b/src/patch_generate.c @@ -284,7 +284,7 @@ static int create_binary( size_t b_datalen) { git_buf deflate = GIT_BUF_INIT, delta = GIT_BUF_INIT; - size_t delta_data_len; + size_t delta_data_len = 0; int error; /* The git_delta function accepts unsigned long only */ diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c index c41de4e23..53c0b089e 100644 --- a/src/transports/smart_protocol.c +++ b/src/transports/smart_protocol.c @@ -728,7 +728,7 @@ static int add_push_report_pkt(git_push *push, git_pkt *pkt) static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt, git_buf *data_pkt_buf) { git_pkt *pkt; - const char *line, *line_end; + const char *line, *line_end = NULL; size_t line_len; int error; int reading_from_buf = data_pkt_buf->size > 0; From ff5eea06a98182697f16d1335ee331cd8cd1b90c Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 12 Dec 2016 09:36:15 +0100 Subject: [PATCH 099/142] pack: dereference cached pack entry on error When trying to uncompress deltas in a packfile's delta chain, we try to add object bases to the packfile cache, subsequently decrementing its reference count if it has been added successfully. This may lead to a mismatched reference count in the case where we exit the loop early due to an encountered error. Fix the issue by decrementing the reference count in error cleanup. --- src/pack.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/pack.c b/src/pack.c index 2ee0c60e4..16e82a92b 100644 --- a/src/pack.c +++ b/src/pack.c @@ -757,8 +757,11 @@ int git_packfile_unpack( } cleanup: - if (error < 0) + if (error < 0) { git__free(obj->data); + if (cached) + git_atomic_dec(&cached->refcount); + } if (elem) *obj_offset = curpos; From b31283a658eb6fdbfaf7256d5303f1ce06de404f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 12 Dec 2016 17:09:12 +0000 Subject: [PATCH 100/142] refdb: disable concurrent compress in the threading tests on Windows This is far from an ideal situation, but this causes issues on Windows which make it harder to develop anything, as these tests hit issues which relate specifically to the Windows filesystem like permission errors for files we should be able to access. There is an issue likely related to the ordering of the repack, but there's enough noise that it does not currently help us to run this aspect of the test in CI. --- tests/threads/refdb.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/threads/refdb.c b/tests/threads/refdb.c index e2f7563c7..94c5f5057 100644 --- a/tests/threads/refdb.c +++ b/tests/threads/refdb.c @@ -5,6 +5,12 @@ static git_repository *g_repo; static int g_expected = 0; +#ifdef GIT_WIN32 +static bool concurrent_compress = false; +#else +static bool concurrent_compress = true; +#endif + void test_threads_refdb__initialize(void) { g_repo = NULL; @@ -79,7 +85,7 @@ static void *create_refs(void *arg) } while (error == GIT_ELOCKED); cl_git_thread_pass(data, error); - if (i == NREFS/2) { + if (concurrent_compress && i == NREFS/2) { git_refdb *refdb; cl_git_thread_pass(data, git_repository_refdb(&refdb, repo)); do { @@ -125,7 +131,7 @@ static void *delete_refs(void *arg) git_reference_free(ref); } - if (i == NREFS/2) { + if (concurrent_compress && i == NREFS/2) { git_refdb *refdb; cl_git_thread_pass(data, git_repository_refdb(&refdb, repo)); do { From 6ab65b80b47cac90a8ded74e35ad2d4cc1a34cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 11 Dec 2016 17:56:38 +0000 Subject: [PATCH 101/142] refdb: bubble up recursive rm when locking a ref Failure to bubble up this error means some locking errors do not get reported as such on Windows. --- src/refdb_fs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 558d06094..8739d5b89 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -729,8 +729,8 @@ static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char * /* Remove a possibly existing empty directory hierarchy * which name would collide with the reference name */ - if (git_futils_rmdir_r(name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0) - return -1; + if ((error = git_futils_rmdir_r(name, backend->path, GIT_RMDIR_SKIP_NONEMPTY)) < 0) + return error; if (git_buf_joinpath(&ref_path, backend->path, name) < 0) return -1; From f7dcd58cc722d501665bc8c5b53945859b8b0615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 17 Dec 2016 00:55:06 +0000 Subject: [PATCH 102/142] rebase: plug a leak in the tests --- tests/rebase/merge.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/rebase/merge.c b/tests/rebase/merge.c index 8492c638f..7b2d6876c 100644 --- a/tests/rebase/merge.c +++ b/tests/rebase/merge.c @@ -523,6 +523,7 @@ void test_rebase_merge__detached_finish(void) git_reflog_free(reflog); git_annotated_commit_free(branch_head); git_annotated_commit_free(upstream_head); + git_reference_free(head_ref); git_reference_free(branch_ref); git_reference_free(upstream_ref); git_rebase_free(rebase); From 061a0ad1f9a33aab07421b51db4194c5fdce2357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 17 Dec 2016 14:23:35 +0000 Subject: [PATCH 103/142] settings: don't hard-code HTTPS capability This partially reverts bdec62dce1c17465b7330100ea2f71e63fc411dd which activates the transport code-paths which allow you to use a custom TLS implementation without having to have one at build-time. However the capabilities describe how libgit2 was built, not what it could potentially support, bring back the ifdefs so we only say we support HTTPS if libgit2 was itself built with a TLS implementation. --- src/settings.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/settings.c b/src/settings.c index cb2317f74..4a6e0f353 100644 --- a/src/settings.c +++ b/src/settings.c @@ -29,7 +29,9 @@ int git_libgit2_features(void) #ifdef GIT_THREADS | GIT_FEATURE_THREADS #endif +#if defined(GIT_OPENSSL) || defined(GIT_WINHTTP) || defined(GIT_SECURE_TRANSPORT) | GIT_FEATURE_HTTPS +#endif #if defined(GIT_SSH) | GIT_FEATURE_SSH #endif From 77e4623257dbdabb98e7ed5812ae4a8614c815b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 17 Dec 2016 14:31:36 +0000 Subject: [PATCH 104/142] settings: clarify what each value means Most importantly, clarify what it means for HTTPS and SSH to be supported. --- include/git2/common.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/git2/common.h b/include/git2/common.h index a8d698fa4..99c99812b 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -109,9 +109,27 @@ GIT_EXTERN(void) git_libgit2_version(int *major, int *minor, int *rev); * was compiled */ typedef enum { + /** + * If set, libgit2 was built thread-aware and can be safely used from multiple + * threads. + */ GIT_FEATURE_THREADS = (1 << 0), + /** + * If set, libgit2 was built with and linked against a TLS implementation. + * Custom TLS streams may still be added by the user to support HTTPS + * regardless of this. + */ GIT_FEATURE_HTTPS = (1 << 1), + /** + * If set, libgit2 was built with and linked against libssh2. A custom + * transport may still be added by the user to support libssh2 regardless of + * this. + */ GIT_FEATURE_SSH = (1 << 2), + /** + * If set, libgit2 was built with support for sub-second resolution in file + * modification times. + */ GIT_FEATURE_NSEC = (1 << 3), } git_feature_t; From 23c9ff8632d8ae90d211601d3254ab7f4d35e853 Mon Sep 17 00:00:00 2001 From: Andreas Henriksson Date: Sat, 17 Dec 2016 17:33:13 +0100 Subject: [PATCH 105/142] Fix off-by-one problems in git_signature__parse Etc/GMT-14 aka UTC+14:00 is a thing.... https://en.wikipedia.org/wiki/UTC%2B14:00 Also allow offsets on the last minute (59). Addresses: https://bugs.debian.org/841532 Fixes: #3970 --- src/signature.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/signature.c b/src/signature.c index dcc379717..22cba7ef3 100644 --- a/src/signature.c +++ b/src/signature.c @@ -251,7 +251,7 @@ int git_signature__parse(git_signature *sig, const char **buffer_out, * only store timezone if it's not overflowing; * see http://www.worldtimezone.com/faq.html */ - if (hours < 14 && mins < 59) { + if (hours <= 14 && mins <= 59) { sig->when.offset = (hours * 60) + mins; if (tz_start[0] == '-') sig->when.offset = -sig->when.offset; From 410855fc4b55d9f6d75e2be44393f039f1708935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 17 Dec 2016 18:18:30 +0000 Subject: [PATCH 106/142] sysdir: add failing test for variable substitution When given $PATH as part of a search path, we guess again instead of substituting what the user already set. --- tests/core/env.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/core/env.c b/tests/core/env.c index ee08258a6..1af0e6e5d 100644 --- a/tests/core/env.c +++ b/tests/core/env.c @@ -298,3 +298,24 @@ void test_core_env__2(void) git_buf_free(&path); git_buf_free(&found); } + +void test_core_env__substitution(void) +{ + git_buf buf = GIT_BUF_INIT, expected = GIT_BUF_INIT; + + /* Set it to something non-default so we have controllable values */ + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, "/tmp/a")); + cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf)); + cl_assert_equal_s("/tmp/a", buf.ptr); + + git_buf_clear(&buf); + cl_git_pass(git_buf_join(&buf, GIT_PATH_LIST_SEPARATOR, "$PATH", "/tmp/b")); + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, buf.ptr)); + cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf)); + + cl_git_pass(git_buf_join(&expected, GIT_PATH_LIST_SEPARATOR, "/tmp/a", "/tmp/b")); + cl_assert_equal_s(expected.ptr, buf.ptr); + + git_buf_free(&expected); + git_buf_free(&buf); +} From 9f09f290bbe49865f57cadb68883af0cd3b2cb71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 17 Dec 2016 18:20:29 +0000 Subject: [PATCH 107/142] sysdir: don't guess the paths again when $PATH is specified We should replace it with whatever the user set, not start again. --- src/sysdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sysdir.c b/src/sysdir.c index 29e53e239..e89db7697 100644 --- a/src/sysdir.c +++ b/src/sysdir.c @@ -171,7 +171,7 @@ int git_sysdir_set(git_sysdir_t which, const char *search_path) expand_path = strstr(search_path, PATH_MAGIC); /* reset the default if this path has been cleared */ - if (!search_path || expand_path) + if (!search_path) git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf); /* if $PATH is not referenced, then just set the path */ From 8f064000dd85b72b8eb4d1baf41b1e6f1dc2f9ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 19 Dec 2016 13:54:55 +0000 Subject: [PATCH 108/142] README: be more explicit in the goals and scope Make it clearer from the get-go that we do not aim to implement user-facing commands from the git tool. --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e656a460c..9e2eaf47f 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,17 @@ We ask that you not open a GitHub Issue for help, only for bug reports. What It Can Do ============== -`libgit2` is already very usable and is being used in production for many -applications including the GitHub.com site, in Plastic SCM and also powering -Microsoft's Visual Studio tools for Git. The library provides: +The goal of this library is to allow its users the ability to handle Git data in +their applications from their programming language of choice, as is used in +production for many applications including the GitHub.com site, in Plastic SCM +and also powering Microsoft's Visual Studio tools for Git. + +It does not aim to replace the git tool or its user-facing commands. Some APIs +resemble the plumbing commands as those align closely with the concepts of the +Git system, but most commands a user would type are out of scope for this +library to implement directly. + +The library provides: * SHA conversions, formatting and shortening * abstracted ODB backend system From 87faeaecdadb0d3d0081d2f2aa71a56aad55fac0 Mon Sep 17 00:00:00 2001 From: Lucas Derraugh Date: Mon, 19 Dec 2016 09:09:34 -0800 Subject: [PATCH 109/142] Gift deprecated in favor of SwiftGit2 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e656a460c..ca081ede1 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,7 @@ Here are the bindings to libgit2 that are currently available: * Rust * git2-rs * Swift - * Gift + * SwiftGit2 * Vala * libgit2.vapi From fafafb1f37d21ebf4b0bbf02651b910d842cfe27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 20 Dec 2016 16:19:30 +0000 Subject: [PATCH 110/142] http: bump the pretend git version in the User-Agent We want to keep the git UA in order for services to recognise that we're a Git client and not a browser. But in order to stop dumb HTTP some services have blocked UAs that claim to be pre-1.6.6 git. Thread these needles by using the "git/2.0" prefix which is still close enough to git's yet distinct enough that you can tell it's us. --- src/transports/http.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transports/http.c b/src/transports/http.c index ca1f5042f..ad28c5889 100644 --- a/src/transports/http.c +++ b/src/transports/http.c @@ -208,7 +208,7 @@ static int gen_request( git_buf_printf(buf, "%s %s%s HTTP/1.1\r\n", s->verb, path, s->service_url); - git_buf_printf(buf, "User-Agent: git/1.0 (%s)\r\n", user_agent()); + git_buf_printf(buf, "User-Agent: git/2.0 (%s)\r\n", user_agent()); git_buf_printf(buf, "Host: %s\r\n", t->connection_data.host); if (s->chunked || content_length > 0) { From 567b83de5434abcc40eeeabb8167979240f27a8f Mon Sep 17 00:00:00 2001 From: Nurahmadie Date: Sat, 24 Dec 2016 17:43:08 +0700 Subject: [PATCH 111/142] Fix BIO_* functions method linking when compiled with libressl. ref: https://github.com/gentoo/libressl/blob/672ac74ce7b7cb2e4799b2d66bc0b1b1efa3454e/media-video/ffmpeg/files/ffmpeg-3.2-libressl.patch --- src/openssl_stream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openssl_stream.h b/src/openssl_stream.h index b769437ae..f5e59dab1 100644 --- a/src/openssl_stream.h +++ b/src/openssl_stream.h @@ -27,7 +27,7 @@ extern int git_openssl_stream_new(git_stream **out, const char *host, const char -# if OPENSSL_VERSION_NUMBER < 0x10100000L +# if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) GIT_INLINE(BIO_METHOD*) BIO_meth_new(int type, const char *name) { From 6a8127d71d10d5b2d7adfda3da1182568e19afde Mon Sep 17 00:00:00 2001 From: Jacques Germishuys Date: Sun, 25 Dec 2016 22:13:48 +0200 Subject: [PATCH 112/142] mempack: set the odb backend version --- src/odb_mempack.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/odb_mempack.c b/src/odb_mempack.c index 68db3bc32..84ed9c104 100644 --- a/src/odb_mempack.c +++ b/src/odb_mempack.c @@ -177,6 +177,7 @@ int git_mempack_new(git_odb_backend **out) db->objects = git_oidmap_alloc(); + db->parent.version = GIT_ODB_BACKEND_VERSION; db->parent.read = &impl__read; db->parent.write = &impl__write; db->parent.read_header = &impl__read_header; From f928c69a07168b1236be13b61a6dcb3ecb7a0871 Mon Sep 17 00:00:00 2001 From: Jacques Germishuys Date: Thu, 29 Dec 2016 12:54:26 +0200 Subject: [PATCH 113/142] rebase: check the result code of rebase_init_merge --- src/rebase.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rebase.c b/src/rebase.c index 0af2b3cf4..ef044692a 100644 --- a/src/rebase.c +++ b/src/rebase.c @@ -731,7 +731,7 @@ int git_rebase_init( if (inmemory) error = rebase_init_inmemory(rebase, repo, branch, upstream, onto); else - rebase_init_merge(rebase, repo, branch ,upstream, onto); + error = rebase_init_merge(rebase, repo, branch ,upstream, onto); if (error == 0) *out = rebase; From 909d5494368a00809bc42f4780e86f4dd66e4422 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 29 Dec 2016 12:25:15 +0000 Subject: [PATCH 114/142] giterr_set: consistent error messages Error messages should be sentence fragments, and therefore: 1. Should not begin with a capital letter, 2. Should not conclude with punctuation, and 3. Should not end a sentence and begin a new one --- src/apply.c | 4 +- src/attr_file.c | 10 +-- src/attrcache.c | 6 +- src/blob.c | 6 +- src/branch.c | 20 +++--- src/buffer.c | 8 +-- src/cache.c | 2 +- src/checkout.c | 50 +++++++-------- src/cherrypick.c | 4 +- src/commit.c | 4 +- src/commit_list.c | 4 +- src/common.h | 2 +- src/config.c | 24 +++---- src/config_file.c | 18 +++--- src/crlf.c | 2 +- src/delta.c | 8 +-- src/describe.c | 22 +++---- src/diff_driver.c | 2 +- src/diff_file.c | 2 +- src/diff_generate.c | 4 +- src/diff_print.c | 10 +-- src/diff_tform.c | 2 +- src/diff_xdiff.c | 4 +- src/fetch.c | 2 +- src/fetchhead.c | 16 ++--- src/filebuf.c | 18 +++--- src/fileops.c | 54 ++++++++-------- src/filter.c | 8 +-- src/hashsig.c | 4 +- src/ignore.c | 2 +- src/index.c | 30 ++++----- src/indexer.c | 6 +- src/merge.c | 16 ++--- src/merge_file.c | 2 +- src/mwindow.c | 2 +- src/netops.c | 8 +-- src/notes.c | 6 +- src/object.c | 10 +-- src/odb.c | 24 +++---- src/odb_loose.c | 12 ++-- src/oid.c | 10 +-- src/openssl_stream.c | 6 +- src/pack-objects.c | 10 +-- src/pack.c | 12 ++-- src/patch_generate.c | 4 +- src/path.c | 30 ++++----- src/pathspec.c | 4 +- src/posix.c | 2 +- src/push.c | 12 ++-- src/rebase.c | 24 +++---- src/refdb.c | 2 +- src/refdb_fs.c | 24 +++---- src/reflog.c | 4 +- src/refs.c | 22 +++---- src/remote.c | 6 +- src/repository.c | 26 ++++---- src/repository.h | 2 +- src/reset.c | 2 +- src/revert.c | 4 +- src/revparse.c | 10 +-- src/revwalk.c | 6 +- src/settings.c | 2 +- src/sha1_lookup.c | 2 +- src/signature.c | 2 +- src/socket_stream.c | 10 +-- src/sortedcache.c | 10 +-- src/stash.c | 12 ++-- src/status.c | 8 +-- src/submodule.c | 28 ++++----- src/sysdir.c | 4 +- src/tag.c | 28 ++++----- src/trace.c | 2 +- src/transport.c | 2 +- src/transports/auth_negotiate.c | 8 +-- src/transports/cred.c | 2 +- src/transports/git.c | 6 +- src/transports/http.c | 12 ++-- src/transports/local.c | 4 +- src/transports/smart.c | 10 +-- src/transports/smart_pkt.c | 8 +-- src/transports/smart_protocol.c | 2 +- src/transports/ssh.c | 18 +++--- src/transports/winhttp.c | 100 +++++++++++++++--------------- src/tree-cache.c | 4 +- src/tree.c | 10 +-- src/unix/map.c | 2 +- src/util.c | 6 +- src/win32/dir.c | 6 +- src/win32/findfile.c | 2 +- src/win32/map.c | 12 ++-- src/win32/w32_crtdbg_stacktrace.c | 4 +- src/win32/w32_util.c | 2 +- src/win32/w32_util.h | 2 +- src/zstream.c | 2 +- 94 files changed, 496 insertions(+), 496 deletions(-) diff --git a/src/apply.c b/src/apply.c index 635934299..595f5f300 100644 --- a/src/apply.c +++ b/src/apply.c @@ -173,7 +173,7 @@ static int apply_hunk( git_diff_line *line = git_array_get(patch->lines, linenum); if (!line) { - error = apply_err("Preimage does not contain line %"PRIuZ, linenum); + error = apply_err("preimage does not contain line %"PRIuZ, linenum); goto done; } @@ -193,7 +193,7 @@ static int apply_hunk( line_num = hunk->hunk.new_start ? hunk->hunk.new_start - 1 : 0; if (!find_hunk_linenum(&line_num, image, &preimage, line_num)) { - error = apply_err("Hunk at line %d did not apply", + error = apply_err("hunk at line %d did not apply", hunk->hunk.new_start); goto done; } diff --git a/src/attr_file.c b/src/attr_file.c index 11d149358..0bb761d04 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -30,7 +30,7 @@ int git_attr_file__new( GITERR_CHECK_ALLOC(attrs); if (git_mutex_init(&attrs->lock) < 0) { - giterr_set(GITERR_OS, "Failed to initialize lock"); + giterr_set(GITERR_OS, "failed to initialize lock"); git__free(attrs); return -1; } @@ -49,7 +49,7 @@ int git_attr_file__clear_rules(git_attr_file *file, bool need_lock) git_attr_rule *rule; if (need_lock && git_mutex_lock(&file->lock) < 0) { - giterr_set(GITERR_OS, "Failed to lock attribute file"); + giterr_set(GITERR_OS, "failed to lock attribute file"); return -1; } @@ -140,7 +140,7 @@ int git_attr_file__load( break; } default: - giterr_set(GITERR_INVALID, "Unknown file source %d", source); + giterr_set(GITERR_INVALID, "unknown file source %d", source); return -1; } @@ -212,7 +212,7 @@ int git_attr_file__out_of_date( } default: - giterr_set(GITERR_INVALID, "Invalid file type %d", file->source); + giterr_set(GITERR_INVALID, "invalid file type %d", file->source); return -1; } } @@ -238,7 +238,7 @@ int git_attr_file__parse_buffer( context = attrs->entry->path; if (git_mutex_lock(&attrs->lock) < 0) { - giterr_set(GITERR_OS, "Failed to lock attribute file"); + giterr_set(GITERR_OS, "failed to lock attribute file"); return -1; } diff --git a/src/attrcache.c b/src/attrcache.c index a57110684..0ade38c7c 100644 --- a/src/attrcache.c +++ b/src/attrcache.c @@ -12,7 +12,7 @@ GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache) GIT_UNUSED(cache); /* avoid warning if threading is off */ if (git_mutex_lock(&cache->lock) < 0) { - giterr_set(GITERR_OS, "Unable to get attr cache lock"); + giterr_set(GITERR_OS, "unable to get attr cache lock"); return -1; } return 0; @@ -365,7 +365,7 @@ int git_attr_cache__do_init(git_repository *repo) /* set up lock */ if (git_mutex_init(&cache->lock) < 0) { - giterr_set(GITERR_OS, "Unable to initialize lock for attr cache"); + giterr_set(GITERR_OS, "unable to initialize lock for attr cache"); git__free(cache); return -1; } @@ -430,7 +430,7 @@ int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro) return 0; if (git_mutex_lock(&cache->lock) < 0) { - giterr_set(GITERR_OS, "Unable to get attr cache lock"); + giterr_set(GITERR_OS, "unable to get attr cache lock"); error = -1; } else { git_strmap_insert(macros, macro->match.pattern, macro, error); diff --git a/src/blob.c b/src/blob.c index 1926c9e58..cd5df3537 100644 --- a/src/blob.c +++ b/src/blob.c @@ -96,7 +96,7 @@ static int write_file_stream( p_close(fd); if (written != file_size || read_len < 0) { - giterr_set(GITERR_OS, "Failed to read file into stream"); + giterr_set(GITERR_OS, "failed to read file into stream"); error = -1; } @@ -142,7 +142,7 @@ static int write_symlink( read_len = p_readlink(path, link_data, link_size); if (read_len != (ssize_t)link_size) { - giterr_set(GITERR_OS, "Failed to create blob. Can't read symlink '%s'", path); + giterr_set(GITERR_OS, "failed to create blob: cannot read symlink '%s'", path); git__free(link_data); return -1; } @@ -186,7 +186,7 @@ int git_blob__create_from_paths( goto done; if (S_ISDIR(st.st_mode)) { - giterr_set(GITERR_ODB, "cannot create blob from '%s'; it is a directory", content_path); + giterr_set(GITERR_ODB, "cannot create blob from '%s': it is a directory", content_path); error = GIT_EDIRECTORY; goto done; } diff --git a/src/branch.c b/src/branch.c index 8d1ed6577..7ddcb3da7 100644 --- a/src/branch.c +++ b/src/branch.c @@ -33,7 +33,7 @@ static int retrieve_branch_reference( /* OOM */; else if ((error = git_reference_lookup(&branch, repo, ref_name.ptr)) < 0) giterr_set( - GITERR_REFERENCE, "Cannot locate %s branch '%s'", + GITERR_REFERENCE, "cannot locate %s branch '%s'", is_remote ? "remote-tracking" : "local", branch_name); *branch_reference_out = branch; /* will be NULL on error */ @@ -46,7 +46,7 @@ static int not_a_local_branch(const char *reference_name) { giterr_set( GITERR_INVALID, - "Reference '%s' is not a local branch.", reference_name); + "reference '%s' is not a local branch.", reference_name); return -1; } @@ -80,7 +80,7 @@ static int create_branch( } if (is_unmovable_head && force) { - giterr_set(GITERR_REFERENCE, "Cannot force update branch '%s' as it is " + giterr_set(GITERR_REFERENCE, "cannot force update branch '%s' as it is " "the current HEAD of the repository.", branch_name); error = -1; goto cleanup; @@ -135,7 +135,7 @@ int git_branch_delete(git_reference *branch) assert(branch); if (!git_reference_is_branch(branch) && !git_reference_is_remote(branch)) { - giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.", + giterr_set(GITERR_INVALID, "reference '%s' is not a valid branch.", git_reference_name(branch)); return GIT_ENOTFOUND; } @@ -144,7 +144,7 @@ int git_branch_delete(git_reference *branch) return is_head; if (is_head) { - giterr_set(GITERR_REFERENCE, "Cannot delete branch '%s' as it is " + giterr_set(GITERR_REFERENCE, "cannot delete branch '%s' as it is " "the current HEAD of the repository.", git_reference_name(branch)); return -1; } @@ -306,7 +306,7 @@ int git_branch_name( branch_name += strlen(GIT_REFS_REMOTES_DIR); } else { giterr_set(GITERR_INVALID, - "Reference '%s' is neither a local nor a remote branch.", ref->name); + "reference '%s' is neither a local nor a remote branch.", ref->name); return -1; } *out = branch_name; @@ -436,7 +436,7 @@ int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refna /* Verify that this is a remote branch */ if (!git_reference__is_remote(refname)) { - giterr_set(GITERR_INVALID, "Reference '%s' is not a remote branch.", + giterr_set(GITERR_INVALID, "reference '%s' is not a remote branch.", refname); error = GIT_ERROR; goto cleanup; @@ -463,7 +463,7 @@ int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refna git_remote_free(remote); giterr_set(GITERR_REFERENCE, - "Reference '%s' is ambiguous", refname); + "reference '%s' is ambiguous", refname); error = GIT_EAMBIGUOUS; goto cleanup; } @@ -477,7 +477,7 @@ int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refna error = git_buf_puts(buf, remote_name); } else { giterr_set(GITERR_REFERENCE, - "Could not determine remote for '%s'", refname); + "could not determine remote for '%s'", refname); error = GIT_ENOTFOUND; } @@ -566,7 +566,7 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name) local = 0; else { giterr_set(GITERR_REFERENCE, - "Cannot set upstream for branch '%s'", shortname); + "cannot set upstream for branch '%s'", shortname); return GIT_ENOTFOUND; } diff --git a/src/buffer.c b/src/buffer.c index d135ebe4a..fdb732d9e 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -962,14 +962,14 @@ int git_buf_unquote(git_buf *buf) case '0': case '1': case '2': case '3': if (j == buf->size-3) { giterr_set(GITERR_INVALID, - "Truncated quoted character \\%c", ch); + "truncated quoted character \\%c", ch); return -1; } if (buf->ptr[j+1] < '0' || buf->ptr[j+1] > '7' || buf->ptr[j+2] < '0' || buf->ptr[j+2] > '7') { giterr_set(GITERR_INVALID, - "Truncated quoted character \\%c%c%c", + "truncated quoted character \\%c%c%c", buf->ptr[j], buf->ptr[j+1], buf->ptr[j+2]); return -1; } @@ -981,7 +981,7 @@ int git_buf_unquote(git_buf *buf) break; default: - giterr_set(GITERR_INVALID, "Invalid quoted character \\%c", ch); + giterr_set(GITERR_INVALID, "invalid quoted character \\%c", ch); return -1; } } @@ -995,6 +995,6 @@ int git_buf_unquote(git_buf *buf) return 0; invalid: - giterr_set(GITERR_INVALID, "Invalid quoted line"); + giterr_set(GITERR_INVALID, "invalid quoted line"); return -1; } diff --git a/src/cache.c b/src/cache.c index ca5173c0d..16ae9b397 100644 --- a/src/cache.c +++ b/src/cache.c @@ -70,7 +70,7 @@ int git_cache_init(git_cache *cache) cache->map = git_oidmap_alloc(); GITERR_CHECK_ALLOC(cache->map); if (git_rwlock_init(&cache->lock)) { - giterr_set(GITERR_OS, "Failed to initialize cache rwlock"); + giterr_set(GITERR_OS, "failed to initialize cache rwlock"); return -1; } return 0; diff --git a/src/checkout.c b/src/checkout.c index 62950913e..0cc29054d 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -1021,13 +1021,13 @@ static int checkout_conflicts_load_byname_entry( *theirs_out = NULL; if (!name_entry->ancestor) { - giterr_set(GITERR_INDEX, "A NAME entry exists without an ancestor"); + giterr_set(GITERR_INDEX, "a NAME entry exists without an ancestor"); error = -1; goto done; } if (!name_entry->ours && !name_entry->theirs) { - giterr_set(GITERR_INDEX, "A NAME entry exists without an ours or theirs"); + giterr_set(GITERR_INDEX, "a NAME entry exists without an ours or theirs"); error = -1; goto done; } @@ -1035,7 +1035,7 @@ static int checkout_conflicts_load_byname_entry( if ((ancestor = checkout_conflicts_search_ancestor(data, name_entry->ancestor)) == NULL) { giterr_set(GITERR_INDEX, - "A NAME entry referenced ancestor entry '%s' which does not exist in the main index", + "a NAME entry referenced ancestor entry '%s' which does not exist in the main index", name_entry->ancestor); error = -1; goto done; @@ -1047,7 +1047,7 @@ static int checkout_conflicts_load_byname_entry( else if ((ours = checkout_conflicts_search_branch(data, name_entry->ours)) == NULL || ours->ours == NULL) { giterr_set(GITERR_INDEX, - "A NAME entry referenced our entry '%s' which does not exist in the main index", + "a NAME entry referenced our entry '%s' which does not exist in the main index", name_entry->ours); error = -1; goto done; @@ -1062,7 +1062,7 @@ static int checkout_conflicts_load_byname_entry( else if ((theirs = checkout_conflicts_search_branch(data, name_entry->theirs)) == NULL || theirs->theirs == NULL) { giterr_set(GITERR_INDEX, - "A NAME entry referenced their entry '%s' which does not exist in the main index", + "a NAME entry referenced their entry '%s' which does not exist in the main index", name_entry->theirs); error = -1; goto done; @@ -1161,7 +1161,7 @@ static int checkout_conflicts_mark_directoryfile( if ((error = git_index_find(&j, index, path)) < 0) { if (error == GIT_ENOTFOUND) giterr_set(GITERR_INDEX, - "Index inconsistency, could not find entry for expected conflict '%s'", path); + "index inconsistency, could not find entry for expected conflict '%s'", path); goto done; } @@ -1169,7 +1169,7 @@ static int checkout_conflicts_mark_directoryfile( for (; j < len; j++) { if ((entry = git_index_get_byindex(index, j)) == NULL) { giterr_set(GITERR_INDEX, - "Index inconsistency, truncated index while loading expected conflict '%s'", path); + "index inconsistency, truncated index while loading expected conflict '%s'", path); error = -1; goto done; } @@ -1254,14 +1254,14 @@ static int checkout_verify_paths( if (action & CHECKOUT_ACTION__REMOVE) { if (!git_path_isvalid(repo, delta->old_file.path, flags)) { - giterr_set(GITERR_CHECKOUT, "Cannot remove invalid path '%s'", delta->old_file.path); + giterr_set(GITERR_CHECKOUT, "cannot remove invalid path '%s'", delta->old_file.path); return -1; } } if (action & ~CHECKOUT_ACTION__REMOVE) { if (!git_path_isvalid(repo, delta->new_file.path, flags)) { - giterr_set(GITERR_CHECKOUT, "Cannot checkout to invalid path '%s'", delta->new_file.path); + giterr_set(GITERR_CHECKOUT, "cannot checkout to invalid path '%s'", delta->new_file.path); return -1; } } @@ -1430,7 +1430,7 @@ static int mkpath2file( */ error = git_futils_rmdir_r(path, NULL, GIT_RMDIR_REMOVE_FILES); } else if (errno != ENOENT) { - giterr_set(GITERR_OS, "Failed to stat file '%s'", path); + giterr_set(GITERR_OS, "failed to stat '%s'", path); return GIT_EEXISTS; } else { giterr_clear(); @@ -1454,7 +1454,7 @@ static int checkout_stream_write( int ret; if ((ret = p_write(stream->fd, buffer, len)) < 0) - giterr_set(GITERR_OS, "Could not write to '%s'", stream->path); + giterr_set(GITERR_OS, "could not write to '%s'", stream->path); return ret; } @@ -1503,7 +1503,7 @@ static int blob_content_to_file( mode = GIT_FILEMODE_BLOB; if ((fd = p_open(path, flags, mode)) < 0) { - giterr_set(GITERR_OS, "Could not open '%s' for writing", path); + giterr_set(GITERR_OS, "could not open '%s' for writing", path); return fd; } @@ -1540,7 +1540,7 @@ static int blob_content_to_file( data->perfdata.stat_calls++; if ((error = p_stat(path, st)) < 0) { - giterr_set(GITERR_OS, "Error statting '%s'", path); + giterr_set(GITERR_OS, "failed to stat '%s'", path); return error; } @@ -1567,7 +1567,7 @@ static int blob_content_to_link( if (data->can_symlink) { if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0) - giterr_set(GITERR_OS, "Could not create symlink %s", path); + giterr_set(GITERR_OS, "could not create symlink %s", path); } else { error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path); } @@ -1576,7 +1576,7 @@ static int blob_content_to_link( data->perfdata.stat_calls++; if ((error = p_lstat(path, st)) < 0) - giterr_set(GITERR_CHECKOUT, "Could not stat symlink %s", path); + giterr_set(GITERR_CHECKOUT, "could not stat symlink %s", path); st->st_mode = GIT_FILEMODE_LINK; } @@ -1621,7 +1621,7 @@ static int checkout_submodule_update_index( data->perfdata.stat_calls++; if (p_stat(fullpath->ptr, &st) < 0) { giterr_set( - GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path); + GITERR_CHECKOUT, "could not stat submodule %s\n", file->path); return GIT_ENOTFOUND; } @@ -1694,7 +1694,7 @@ static int checkout_safe_for_update_only( return 0; /* otherwise, stat error and no update */ - giterr_set(GITERR_OS, "Failed to stat file '%s'", path); + giterr_set(GITERR_OS, "failed to stat '%s'", path); return -1; } @@ -1966,7 +1966,7 @@ static int checkout_path_suffixed(git_buf *path, const char *suffix) if (i == INT_MAX) { git_buf_truncate(path, path_len); - giterr_set(GITERR_CHECKOUT, "Could not write '%s': working directory file exists", path->ptr); + giterr_set(GITERR_CHECKOUT, "could not write '%s': working directory file exists", path->ptr); return GIT_EEXISTS; } @@ -2097,7 +2097,7 @@ static int checkout_write_merge( goto done; if (result.path == NULL || result.mode == 0) { - giterr_set(GITERR_CHECKOUT, "Could not merge contents of file"); + giterr_set(GITERR_CHECKOUT, "could not merge contents of file"); error = GIT_ECONFLICT; goto done; } @@ -2345,7 +2345,7 @@ static int checkout_data_init( memset(data, 0, sizeof(*data)); if (!repo) { - giterr_set(GITERR_CHECKOUT, "Cannot checkout nothing"); + giterr_set(GITERR_CHECKOUT, "cannot checkout nothing"); return -1; } @@ -2647,7 +2647,7 @@ int git_checkout_index( if (!index && !repo) { giterr_set(GITERR_CHECKOUT, - "Must provide either repository or index to checkout"); + "must provide either repository or index to checkout"); return -1; } @@ -2655,7 +2655,7 @@ int git_checkout_index( git_index_owner(index) && git_index_owner(index) != repo) { giterr_set(GITERR_CHECKOUT, - "Index to checkout does not match repository"); + "index to checkout does not match repository"); return -1; } else if(index && repo && !git_index_owner(index)) { GIT_REFCOUNT_OWN(index, repo); @@ -2694,12 +2694,12 @@ int git_checkout_tree( if (!treeish && !repo) { giterr_set(GITERR_CHECKOUT, - "Must provide either repository or tree to checkout"); + "must provide either repository or tree to checkout"); return -1; } if (treeish && repo && git_object_owner(treeish) != repo) { giterr_set(GITERR_CHECKOUT, - "Object to checkout does not match repository"); + "object to checkout does not match repository"); return -1; } @@ -2709,7 +2709,7 @@ int git_checkout_tree( if (treeish) { if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) { giterr_set( - GITERR_CHECKOUT, "Provided object cannot be peeled to a tree"); + GITERR_CHECKOUT, "provided object cannot be peeled to a tree"); return -1; } } diff --git a/src/cherrypick.c b/src/cherrypick.c index c92975194..ab067339e 100644 --- a/src/cherrypick.c +++ b/src/cherrypick.c @@ -130,13 +130,13 @@ int git_cherrypick_commit( if (git_commit_parentcount(cherrypick_commit) > 1) { if (!mainline) return cherrypick_seterr(cherrypick_commit, - "Mainline branch is not specified but %s is a merge commit"); + "mainline branch is not specified but %s is a merge commit"); parent = mainline; } else { if (mainline) return cherrypick_seterr(cherrypick_commit, - "Mainline branch specified but %s is not a merge commit"); + "mainline branch specified but %s is not a merge commit"); parent = git_commit_parentcount(cherrypick_commit); } diff --git a/src/commit.c b/src/commit.c index 76e6dcbc9..87ab2ab60 100644 --- a/src/commit.c +++ b/src/commit.c @@ -468,7 +468,7 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj) return 0; bad_buffer: - giterr_set(GITERR_OBJECT, "Failed to parse bad commit object"); + giterr_set(GITERR_OBJECT, "failed to parse bad commit object"); return -1; } @@ -598,7 +598,7 @@ int git_commit_parent( parent_id = git_commit_parent_id(commit, n); if (parent_id == NULL) { - giterr_set(GITERR_INVALID, "Parent %u does not exist", n); + giterr_set(GITERR_INVALID, "parent %u does not exist", n); return GIT_ENOTFOUND; } diff --git a/src/commit_list.c b/src/commit_list.c index a1681ffae..3bba58c27 100644 --- a/src/commit_list.c +++ b/src/commit_list.c @@ -61,7 +61,7 @@ static int commit_error(git_commit_list_node *commit, const char *msg) git_oid_fmt(commit_oid, &commit->oid); commit_oid[GIT_OID_HEXSZ] = '\0'; - giterr_set(GITERR_ODB, "Failed to parse commit %s - %s", commit_oid, msg); + giterr_set(GITERR_ODB, "failed to parse commit %s - %s", commit_oid, msg); return -1; } @@ -191,7 +191,7 @@ int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) return error; if (obj->cached.type != GIT_OBJ_COMMIT) { - giterr_set(GITERR_INVALID, "Object is no commit object"); + giterr_set(GITERR_INVALID, "object is no commit object"); error = -1; } else error = commit_quick_parse( diff --git a/src/common.h b/src/common.h index f12cc98cf..e566aeabd 100644 --- a/src/common.h +++ b/src/common.h @@ -188,7 +188,7 @@ GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expect if (actual > 0 && actual <= expected_max) return 0; - giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name); + giterr_set(GITERR_INVALID, "invalid version %d on %s", actual, name); return -1; } #define GITERR_CHECK_VERSION(S,V,N) if (giterr__check_version(S,V,N) < 0) return -1 diff --git a/src/config.c b/src/config.c index 403b7090d..0d73ad2cc 100644 --- a/src/config.c +++ b/src/config.c @@ -109,7 +109,7 @@ int git_config_add_file_ondisk( res = p_stat(path, &st); if (res < 0 && errno != ENOENT) { - giterr_set(GITERR_CONFIG, "Error stat'ing config file '%s'", path); + giterr_set(GITERR_CONFIG, "failed to stat '%s'", path); return -1; } @@ -203,7 +203,7 @@ static int find_internal_file_by_level( if (pos == -1) { giterr_set(GITERR_CONFIG, - "No config file exists for the given level '%i'", (int)level); + "no config file exists for the given level '%i'", (int)level); return GIT_ENOTFOUND; } @@ -218,7 +218,7 @@ static int duplicate_level(void **old_raw, void *new_raw) GIT_UNUSED(new_raw); - giterr_set(GITERR_CONFIG, "A file with the same level (%i) has already been added to the config", (int)(*old)->level); + giterr_set(GITERR_CONFIG, "a file with the same level (%i) has already been added to the config", (int)(*old)->level); return GIT_EEXISTS; } @@ -579,7 +579,7 @@ int git_config_foreach_match( static int config_error_nofiles(const char *name) { giterr_set(GITERR_CONFIG, - "Cannot set value for '%s' when no config files exist", name); + "cannot set value for '%s' when no config files exist", name); return GIT_ENOTFOUND; } @@ -620,7 +620,7 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value) file_internal *internal; if (!value) { - giterr_set(GITERR_CONFIG, "The value to set cannot be NULL"); + giterr_set(GITERR_CONFIG, "the value to set cannot be NULL"); return -1; } @@ -674,7 +674,7 @@ int git_config__update_entry( static int config_error_notfound(const char *name) { - giterr_set(GITERR_CONFIG, "Config value '%s' was not found", name); + giterr_set(GITERR_CONFIG, "config value '%s' was not found", name); return GIT_ENOTFOUND; } @@ -1236,7 +1236,7 @@ int git_config_lookup_map_value( } fail_parse: - giterr_set(GITERR_CONFIG, "Failed to map '%s'", value); + giterr_set(GITERR_CONFIG, "failed to map '%s'", value); return -1; } @@ -1270,7 +1270,7 @@ int git_config_parse_bool(int *out, const char *value) return 0; } - giterr_set(GITERR_CONFIG, "Failed to parse '%s' as a boolean value", value); + giterr_set(GITERR_CONFIG, "failed to parse '%s' as a boolean value", value); return -1; } @@ -1313,7 +1313,7 @@ int git_config_parse_int64(int64_t *out, const char *value) } fail_parse: - giterr_set(GITERR_CONFIG, "Failed to parse '%s' as an integer", value ? value : "(null)"); + giterr_set(GITERR_CONFIG, "failed to parse '%s' as an integer", value ? value : "(null)"); return -1; } @@ -1333,7 +1333,7 @@ int git_config_parse_int32(int32_t *out, const char *value) return 0; fail_parse: - giterr_set(GITERR_CONFIG, "Failed to parse '%s' as a 32-bit integer", value ? value : "(null)"); + giterr_set(GITERR_CONFIG, "failed to parse '%s' as a 32-bit integer", value ? value : "(null)"); return -1; } @@ -1398,7 +1398,7 @@ int git_config__normalize_name(const char *in, char **out) invalid: git__free(name); - giterr_set(GITERR_CONFIG, "Invalid config item name '%s'", in); + giterr_set(GITERR_CONFIG, "invalid config item name '%s'", in); return GIT_EINVALIDSPEC; } @@ -1461,7 +1461,7 @@ int git_config_rename_section( replace.ptr, strchr(replace.ptr, '.'))) < 0) { giterr_set( - GITERR_CONFIG, "Invalid config section '%s'", new_section_name); + GITERR_CONFIG, "invalid config section '%s'", new_section_name); goto cleanup; } diff --git a/src/config_file.c b/src/config_file.c index 9ff021e7e..2e3d568bb 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -126,7 +126,7 @@ static int config_snapshot(git_config_backend **out, git_config_backend *in); static void set_parse_error(struct reader *reader, int col, const char *error_str) { - giterr_set(GITERR_CONFIG, "Failed to parse config file: %s (in %s:%d, column %d)", + giterr_set(GITERR_CONFIG, "failed to parse config file: %s (in %s:%d, column %d)", error_str, reader->file_path, reader->line_number, col); } @@ -233,7 +233,7 @@ static refcounted_strmap *refcounted_strmap_take(diskfile_header *h) refcounted_strmap *map; if (git_mutex_lock(&h->values_mutex) < 0) { - giterr_set(GITERR_OS, "Failed to lock config backend"); + giterr_set(GITERR_OS, "failed to lock config backend"); return NULL; } @@ -322,7 +322,7 @@ static int config__refresh(git_config_backend *cfg) goto out; if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) { - giterr_set(GITERR_OS, "Failed to lock config backend"); + giterr_set(GITERR_OS, "failed to lock config backend"); goto out; } @@ -479,7 +479,7 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val cvar_t *existing = git_strmap_value_at(values, pos); if (existing->next != NULL) { - giterr_set(GITERR_CONFIG, "Multivar incompatible with simple set"); + giterr_set(GITERR_CONFIG, "multivar incompatible with simple set"); ret = -1; goto out; } @@ -611,7 +611,7 @@ static int config_delete(git_config_backend *cfg, const char *name) if (!git_strmap_valid_index(values, pos)) { refcounted_strmap_free(map); - giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name); + giterr_set(GITERR_CONFIG, "could not find key '%s' to delete", name); return GIT_ENOTFOUND; } @@ -619,7 +619,7 @@ static int config_delete(git_config_backend *cfg, const char *name) refcounted_strmap_free(map); if (var->next != NULL) { - giterr_set(GITERR_CONFIG, "Cannot delete multivar with a single delete"); + giterr_set(GITERR_CONFIG, "cannot delete multivar with a single delete"); return -1; } @@ -651,7 +651,7 @@ static int config_delete_multivar(git_config_backend *cfg, const char *name, con if (!git_strmap_valid_index(values, pos)) { refcounted_strmap_free(map); git__free(key); - giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name); + giterr_set(GITERR_CONFIG, "could not find key '%s' to delete", name); return GIT_ENOTFOUND; } @@ -1325,7 +1325,7 @@ static int unescape_line( *fixed++ = escaped[esc - escapes]; } else { git__free(str); - giterr_set(GITERR_CONFIG, "Invalid escape at %s", ptr); + giterr_set(GITERR_CONFIG, "invalid escape at %s", ptr); return -1; } } @@ -1639,7 +1639,7 @@ static int config_read(git_strmap *values, diskfile_backend *cfg_file, struct re struct parse_data parse_data; if (depth >= MAX_INCLUDE_DEPTH) { - giterr_set(GITERR_CONFIG, "Maximum config include depth reached"); + giterr_set(GITERR_CONFIG, "maximum config include depth reached"); return -1; } diff --git a/src/crlf.c b/src/crlf.c index 11895b19f..b8ae5cda1 100644 --- a/src/crlf.c +++ b/src/crlf.c @@ -218,7 +218,7 @@ static const char *line_ending(struct crlf_attrs *ca) return "\r\n"; line_ending_error: - giterr_set(GITERR_INVALID, "Invalid input to line ending filter"); + giterr_set(GITERR_INVALID, "invalid input to line ending filter"); return NULL; } diff --git a/src/delta.c b/src/delta.c index dc45697b6..073cba7c6 100644 --- a/src/delta.c +++ b/src/delta.c @@ -131,7 +131,7 @@ static int lookup_index_alloc( GITERR_CHECK_ALLOC_ADD(&index_len, index_len, hash_len); if (!git__is_ulong(index_len)) { - giterr_set(GITERR_NOMEMORY, "Overly large delta"); + giterr_set(GITERR_NOMEMORY, "overly large delta"); return -1; } @@ -544,12 +544,12 @@ int git_delta_apply( * base object, resulting in data corruption or segfault. */ if ((hdr_sz(&base_sz, &delta, delta_end) < 0) || (base_sz != base_len)) { - giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data"); + giterr_set(GITERR_INVALID, "failed to apply delta: base size does not match given data"); return -1; } if (hdr_sz(&res_sz, &delta, delta_end) < 0) { - giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data"); + giterr_set(GITERR_INVALID, "failed to apply delta: base size does not match given data"); return -1; } @@ -614,6 +614,6 @@ fail: *out = NULL; *out_len = 0; - giterr_set(GITERR_INVALID, "Failed to apply delta"); + giterr_set(GITERR_INVALID, "failed to apply delta"); return -1; } diff --git a/src/describe.c b/src/describe.c index fc48fbde4..16e195542 100644 --- a/src/describe.c +++ b/src/describe.c @@ -335,14 +335,14 @@ static int display_name(git_buf *buf, git_repository *repo, struct commit_name * { if (n->prio == 2 && !n->tag) { if (git_tag_lookup(&n->tag, repo, &n->sha1) < 0) { - giterr_set(GITERR_TAG, "Annotated tag '%s' not available", n->path); + giterr_set(GITERR_TAG, "annotated tag '%s' not available", n->path); return -1; } } if (n->tag && !n->name_checked) { if (!git_tag_name(n->tag)) { - giterr_set(GITERR_TAG, "Annotated tag '%s' has no embedded name", n->path); + giterr_set(GITERR_TAG, "annotated tag '%s' has no embedded name", n->path); return -1; } @@ -471,7 +471,7 @@ static int describe( if (!data->opts->max_candidates_tags) { error = describe_not_found( git_commit_id(commit), - "Cannot describe - no tag exactly matches '%s'"); + "cannot describe - no tag exactly matches '%s'"); goto cleanup; } @@ -564,15 +564,15 @@ static int describe( } if (unannotated_cnt) { error = describe_not_found(git_commit_id(commit), - "Cannot describe - " - "No annotated tags can describe '%s'." - "However, there were unannotated tags."); + "cannot describe - " + "no annotated tags can describe '%s'; " + "however, there were unannotated tags."); goto cleanup; } else { error = describe_not_found(git_commit_id(commit), - "Cannot describe - " - "No tags can describe '%s'."); + "cannot describe - " + "no tags can describe '%s'."); goto cleanup; } } @@ -695,8 +695,8 @@ int git_describe_commit( goto cleanup; if (git_oidmap_size(data.names) == 0 && !opts->show_commit_oid_as_fallback) { - giterr_set(GITERR_DESCRIBE, "Cannot describe - " - "No reference found, cannot describe anything."); + giterr_set(GITERR_DESCRIBE, "cannot describe - " + "no reference found, cannot describe anything."); error = -1; goto cleanup; } @@ -793,7 +793,7 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g if (opts.always_use_long_format && opts.abbreviated_size == 0) { - giterr_set(GITERR_DESCRIBE, "Cannot describe - " + giterr_set(GITERR_DESCRIBE, "cannot describe - " "'always_use_long_format' is incompatible with a zero" "'abbreviated_size'"); return -1; diff --git a/src/diff_driver.c b/src/diff_driver.c index 1a7f09a44..0adf704fb 100644 --- a/src/diff_driver.c +++ b/src/diff_driver.c @@ -151,7 +151,7 @@ static git_diff_driver_registry *git_repository_driver_registry( } if (!repo->diff_drivers) - giterr_set(GITERR_REPOSITORY, "Unable to create diff driver registry"); + giterr_set(GITERR_REPOSITORY, "unable to create diff driver registry"); return repo->diff_drivers; } diff --git a/src/diff_file.c b/src/diff_file.c index cc1029038..d5fc5e940 100644 --- a/src/diff_file.c +++ b/src/diff_file.c @@ -304,7 +304,7 @@ static int diff_file_content_load_workdir_symlink( read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len); if (read_len < 0) { - giterr_set(GITERR_OS, "Failed to read symlink '%s'", fc->file->path); + giterr_set(GITERR_OS, "failed to read symlink '%s'", fc->file->path); return -1; } diff --git a/src/diff_generate.c b/src/diff_generate.c index 06f9b19c7..f6cc04fed 100644 --- a/src/diff_generate.c +++ b/src/diff_generate.c @@ -624,7 +624,7 @@ int git_diff__oid_for_entry( error = git_odb__hashlink(out, full_path.ptr); diff->base.perf.oid_calculations++; } else if (!git__is_sizet(entry.file_size)) { - giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'", + giterr_set(GITERR_OS, "file size overflow (for 32-bits) on '%s'", entry.path); error = -1; } else if (!(error = git_filter_list_load(&fl, @@ -1587,7 +1587,7 @@ int git_diff__commit( char commit_oidstr[GIT_OID_HEXSZ + 1]; error = -1; - giterr_set(GITERR_INVALID, "Commit %s is a merge commit", + giterr_set(GITERR_INVALID, "commit %s is a merge commit", git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit))); goto on_error; } diff --git a/src/diff_print.c b/src/diff_print.c index fd1a186c1..5aa8a37e6 100644 --- a/src/diff_print.c +++ b/src/diff_print.c @@ -222,7 +222,7 @@ static int diff_print_one_raw( if (pi->id_strlen > id_abbrev) { giterr_set(GITERR_PATCH, - "The patch input contains %d id characters (cannot print %d)", + "the patch input contains %d id characters (cannot print %d)", id_abbrev, pi->id_strlen); return -1; } @@ -273,7 +273,7 @@ static int diff_print_oid_range( if (delta->old_file.mode && id_strlen > delta->old_file.id_abbrev) { giterr_set(GITERR_PATCH, - "The patch input contains %d id characters (cannot print %d)", + "the patch input contains %d id characters (cannot print %d)", delta->old_file.id_abbrev, id_strlen); return -1; } @@ -281,7 +281,7 @@ static int diff_print_oid_range( if ((delta->new_file.mode && id_strlen > delta->new_file.id_abbrev)) { giterr_set(GITERR_PATCH, - "The patch input contains %d id characters (cannot print %d)", + "the patch input contains %d id characters (cannot print %d)", delta->new_file.id_abbrev, id_strlen); return -1; } @@ -680,7 +680,7 @@ int git_diff_print( print_file = diff_print_one_name_status; break; default: - giterr_set(GITERR_INVALID, "Unknown diff output format (%d)", format); + giterr_set(GITERR_INVALID, "unknown diff output format (%d)", format); return -1; } @@ -708,7 +708,7 @@ int git_diff_print_callback__to_buf( GIT_UNUSED(delta); GIT_UNUSED(hunk); if (!output) { - giterr_set(GITERR_INVALID, "Buffer pointer must be provided"); + giterr_set(GITERR_INVALID, "buffer pointer must be provided"); return -1; } diff --git a/src/diff_tform.c b/src/diff_tform.c index e8848bd45..f2ff147d1 100644 --- a/src/diff_tform.c +++ b/src/diff_tform.c @@ -131,7 +131,7 @@ int git_diff__merge( if (ignore_case != ((from->opts.flags & GIT_DIFF_IGNORE_CASE) != 0) || reversed != ((from->opts.flags & GIT_DIFF_REVERSE) != 0)) { giterr_set(GITERR_INVALID, - "Attempt to merge diffs created with conflicting options"); + "attempt to merge diffs created with conflicting options"); return -1; } diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c index 5bd6381b5..60c4d85cb 100644 --- a/src/diff_xdiff.c +++ b/src/diff_xdiff.c @@ -50,7 +50,7 @@ static int git_xdiff_parse_hunk(git_diff_hunk *hunk, const char *header) return 0; fail: - giterr_set(GITERR_INVALID, "Malformed hunk header from xdiff"); + giterr_set(GITERR_INVALID, "malformed hunk header from xdiff"); return -1; } @@ -99,7 +99,7 @@ static int diff_update_lines( info->new_lineno += (int)line->num_lines; break; default: - giterr_set(GITERR_INVALID, "Unknown diff line origin %02x", + giterr_set(GITERR_INVALID, "unknown diff line origin %02x", (unsigned int)line->origin); return -1; } diff --git a/src/fetch.c b/src/fetch.c index 4d895752c..f408a5174 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -113,7 +113,7 @@ int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts) remote->need_pack = 0; if (filter_wants(remote, opts) < 0) { - giterr_set(GITERR_NET, "Failed to filter the reference list for wants"); + giterr_set(GITERR_NET, "failed to filter the reference list for wants"); return -1; } diff --git a/src/fetchhead.c b/src/fetchhead.c index 3d16c2166..0d9ab2c25 100644 --- a/src/fetchhead.c +++ b/src/fetchhead.c @@ -149,7 +149,7 @@ static int fetchhead_ref_parse( if (!*line) { giterr_set(GITERR_FETCHHEAD, - "Empty line in FETCH_HEAD line %"PRIuZ, line_num); + "empty line in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -163,13 +163,13 @@ static int fetchhead_ref_parse( if (strlen(oid_str) != GIT_OID_HEXSZ) { giterr_set(GITERR_FETCHHEAD, - "Invalid object ID in FETCH_HEAD line %"PRIuZ, line_num); + "invalid object ID in FETCH_HEAD line %"PRIuZ, line_num); return -1; } if (git_oid_fromstr(oid, oid_str) < 0) { const git_error *oid_err = giterr_last(); - const char *err_msg = oid_err ? oid_err->message : "Invalid object ID"; + const char *err_msg = oid_err ? oid_err->message : "invalid object ID"; giterr_set(GITERR_FETCHHEAD, "%s in FETCH_HEAD line %"PRIuZ, err_msg, line_num); @@ -180,7 +180,7 @@ static int fetchhead_ref_parse( if (*line) { if ((is_merge_str = git__strsep(&line, "\t")) == NULL) { giterr_set(GITERR_FETCHHEAD, - "Invalid description data in FETCH_HEAD line %"PRIuZ, line_num); + "invalid description data in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -190,13 +190,13 @@ static int fetchhead_ref_parse( *is_merge = 0; else { giterr_set(GITERR_FETCHHEAD, - "Invalid for-merge entry in FETCH_HEAD line %"PRIuZ, line_num); + "invalid for-merge entry in FETCH_HEAD line %"PRIuZ, line_num); return -1; } if ((desc = line) == NULL) { giterr_set(GITERR_FETCHHEAD, - "Invalid description in FETCH_HEAD line %"PRIuZ, line_num); + "invalid description in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -213,7 +213,7 @@ static int fetchhead_ref_parse( if ((desc = strstr(name, "' ")) == NULL || git__prefixcmp(desc, "' of ") != 0) { giterr_set(GITERR_FETCHHEAD, - "Invalid description in FETCH_HEAD line %"PRIuZ, line_num); + "invalid description in FETCH_HEAD line %"PRIuZ, line_num); return -1; } @@ -277,7 +277,7 @@ int git_repository_fetchhead_foreach(git_repository *repo, } if (*buffer) { - giterr_set(GITERR_FETCHHEAD, "No EOL at line %"PRIuZ, line_num+1); + giterr_set(GITERR_FETCHHEAD, "no EOL at line %"PRIuZ, line_num+1); error = -1; goto done; } diff --git a/src/filebuf.c b/src/filebuf.c index 582399470..ef68b16f4 100644 --- a/src/filebuf.c +++ b/src/filebuf.c @@ -23,7 +23,7 @@ static int verify_last_error(git_filebuf *file) { switch (file->last_error) { case BUFERR_WRITE: - giterr_set(GITERR_OS, "Failed to write out file"); + giterr_set(GITERR_OS, "failed to write out file"); return -1; case BUFERR_MEM: @@ -48,7 +48,7 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode) else { giterr_clear(); /* actual OS error code just confuses */ giterr_set(GITERR_OS, - "Failed to lock file '%s' for writing", file->path_lock); + "failed to lock file '%s' for writing", file->path_lock); return GIT_ELOCKED; } } @@ -75,7 +75,7 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode) source = p_open(file->path_original, O_RDONLY); if (source < 0) { giterr_set(GITERR_OS, - "Failed to open file '%s' for reading", + "failed to open file '%s' for reading", file->path_original); return -1; } @@ -90,10 +90,10 @@ static int lock_file(git_filebuf *file, int flags, mode_t mode) p_close(source); if (read_bytes < 0) { - giterr_set(GITERR_OS, "Failed to read file '%s'", file->path_original); + giterr_set(GITERR_OS, "failed to read file '%s'", file->path_original); return -1; } else if (error < 0) { - giterr_set(GITERR_OS, "Failed to write file '%s'", file->path_lock); + giterr_set(GITERR_OS, "failed to write file '%s'", file->path_lock); return -1; } } @@ -316,7 +316,7 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo if (compression != 0) { /* Initialize the ZLib stream */ if (deflateInit(&file->zs, compression) != Z_OK) { - giterr_set(GITERR_ZLIB, "Failed to initialize zlib"); + giterr_set(GITERR_ZLIB, "failed to initialize zlib"); goto cleanup; } @@ -426,14 +426,14 @@ int git_filebuf_commit(git_filebuf *file) file->fd_is_open = false; if (p_close(file->fd) < 0) { - giterr_set(GITERR_OS, "Failed to close file at '%s'", file->path_lock); + giterr_set(GITERR_OS, "failed to close file at '%s'", file->path_lock); goto on_error; } file->fd = -1; if (p_rename(file->path_lock, file->path_original) < 0) { - giterr_set(GITERR_OS, "Failed to rename lockfile to '%s'", file->path_original); + giterr_set(GITERR_OS, "failed to rename lockfile to '%s'", file->path_original); goto on_error; } @@ -571,7 +571,7 @@ int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file) res = p_stat(file->path_original, &st); if (res < 0) { - giterr_set(GITERR_OS, "Could not get stat info for '%s'", + giterr_set(GITERR_OS, "could not get stat info for '%s'", file->path_original); return res; } diff --git a/src/fileops.c b/src/fileops.c index a82202c98..7a8733209 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -37,13 +37,13 @@ int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode) if ((fd = p_mkstemp(path_out->ptr)) < 0) { giterr_set(GITERR_OS, - "Failed to create temporary file '%s'", path_out->ptr); + "failed to create temporary file '%s'", path_out->ptr); return -1; } if (p_chmod(path_out->ptr, (mode & ~mask))) { giterr_set(GITERR_OS, - "Failed to set permissions on file '%s'", path_out->ptr); + "failed to set permissions on file '%s'", path_out->ptr); return -1; } @@ -59,7 +59,7 @@ int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode fd = p_creat(path, mode); if (fd < 0) { - giterr_set(GITERR_OS, "Failed to create file '%s'", path); + giterr_set(GITERR_OS, "failed to create file '%s'", path); return -1; } @@ -73,7 +73,7 @@ int git_futils_creat_locked(const char *path, const mode_t mode) if (fd < 0) { int error = errno; - giterr_set(GITERR_OS, "Failed to create locked file '%s'", path); + giterr_set(GITERR_OS, "failed to create locked file '%s'", path); switch (error) { case EEXIST: return GIT_ELOCKED; @@ -108,7 +108,7 @@ git_off_t git_futils_filesize(git_file fd) struct stat sb; if (p_fstat(fd, &sb)) { - giterr_set(GITERR_OS, "Failed to stat file descriptor"); + giterr_set(GITERR_OS, "failed to stat file descriptor"); return -1; } @@ -137,7 +137,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len) git_buf_clear(buf); if (!git__is_ssizet(len)) { - giterr_set(GITERR_INVALID, "Read too large."); + giterr_set(GITERR_INVALID, "read too large"); return -1; } @@ -149,7 +149,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len) read_size = p_read(fd, buf->ptr, len); if (read_size != (ssize_t)len) { - giterr_set(GITERR_OS, "Failed to read descriptor"); + giterr_set(GITERR_OS, "failed to read descriptor"); git_buf_free(buf); return -1; } @@ -184,7 +184,7 @@ int git_futils_readbuffer_updated( } if (!git__is_sizet(st.st_size+1)) { - giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path); + giterr_set(GITERR_OS, "invalid regular file stat for '%s'", path); return -1; } @@ -245,18 +245,18 @@ int git_futils_writebuffer( mode = GIT_FILEMODE_BLOB; if ((fd = p_open(path, flags, mode)) < 0) { - giterr_set(GITERR_OS, "Could not open '%s' for writing", path); + giterr_set(GITERR_OS, "could not open '%s' for writing", path); return fd; } if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) { - giterr_set(GITERR_OS, "Could not write to '%s'", path); + giterr_set(GITERR_OS, "could not write to '%s'", path); (void)p_close(fd); return error; } if ((error = p_close(fd)) < 0) - giterr_set(GITERR_OS, "Error while closing '%s'", path); + giterr_set(GITERR_OS, "error while closing '%s'", path); return error; } @@ -267,7 +267,7 @@ int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmod return -1; if (p_rename(from, to) < 0) { - giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to); + giterr_set(GITERR_OS, "failed to rename '%s' to '%s'", from, to); return -1; } @@ -290,7 +290,7 @@ int git_futils_mmap_ro_file(git_map *out, const char *path) len = git_futils_filesize(fd); if (!git__is_sizet(len)) { - giterr_set(GITERR_OS, "File `%s` too large to mmap", path); + giterr_set(GITERR_OS, "file `%s` too large to mmap", path); return -1; } @@ -314,14 +314,14 @@ GIT_INLINE(int) mkdir_validate_dir( /* with exclusive create, existing dir is an error */ if ((flags & GIT_MKDIR_EXCL) != 0) { giterr_set(GITERR_FILESYSTEM, - "Failed to make directory '%s': directory exists", path); + "failed to make directory '%s': directory exists", path); return GIT_EEXISTS; } if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) || (S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) { if (p_unlink(path) < 0) { - giterr_set(GITERR_OS, "Failed to remove %s '%s'", + giterr_set(GITERR_OS, "failed to remove %s '%s'", S_ISLNK(st->st_mode) ? "symlink" : "file", path); return GIT_EEXISTS; } @@ -329,7 +329,7 @@ GIT_INLINE(int) mkdir_validate_dir( opts->perfdata.mkdir_calls++; if (p_mkdir(path, mode) < 0) { - giterr_set(GITERR_OS, "Failed to make directory '%s'", path); + giterr_set(GITERR_OS, "failed to make directory '%s'", path); return GIT_EEXISTS; } } @@ -339,14 +339,14 @@ GIT_INLINE(int) mkdir_validate_dir( opts->perfdata.stat_calls++; if (p_stat(path, st) < 0) { - giterr_set(GITERR_OS, "Failed to make directory '%s'", path); + giterr_set(GITERR_OS, "failed to make directory '%s'", path); return GIT_EEXISTS; } } else if (!S_ISDIR(st->st_mode)) { giterr_set(GITERR_FILESYSTEM, - "Failed to make directory '%s': directory exists", path); + "failed to make directory '%s': directory exists", path); return GIT_EEXISTS; } @@ -569,7 +569,7 @@ int git_futils_mkdir_relative( retry_lstat: if (p_lstat(make_path.ptr, &st) < 0) { if (mkdir_attempted || errno != ENOENT) { - giterr_set(GITERR_OS, "Cannot access component in path '%s'", make_path.ptr); + giterr_set(GITERR_OS, "cannot access component in path '%s'", make_path.ptr); error = -1; goto done; } @@ -580,7 +580,7 @@ retry_lstat: if (p_mkdir(make_path.ptr, mode) < 0) { if (errno == EEXIST) goto retry_lstat; - giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr); + giterr_set(GITERR_OS, "failed to make directory '%s'", make_path.ptr); error = -1; goto done; } @@ -621,7 +621,7 @@ retry_lstat: opts->perfdata.stat_calls++; if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) { - giterr_set(GITERR_OS, "Path is not a directory '%s'", + giterr_set(GITERR_OS, "path is not a directory '%s'", make_path.ptr); error = GIT_ENOTFOUND; } @@ -644,10 +644,10 @@ typedef struct { static int futils__error_cannot_rmdir(const char *path, const char *filemsg) { if (filemsg) - giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s", + giterr_set(GITERR_OS, "could not remove directory '%s': %s", path, filemsg); else - giterr_set(GITERR_OS, "Could not remove directory '%s'", path); + giterr_set(GITERR_OS, "could not remove directory '%s'", path); return -1; } @@ -815,7 +815,7 @@ static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done) error = p_write(ofd, buffer, len); if (len < 0) { - giterr_set(GITERR_OS, "Read error while copying file"); + giterr_set(GITERR_OS, "read error while copying file"); error = (int)len; } @@ -871,14 +871,14 @@ static int cp_link(const char *from, const char *to, size_t link_size) read_len = p_readlink(from, link_data, link_size); if (read_len != (ssize_t)link_size) { - giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from); + giterr_set(GITERR_OS, "failed to read symlink data for '%s'", from); error = -1; } else { link_data[read_len] = '\0'; if (p_symlink(link_data, to) < 0) { - giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'", + giterr_set(GITERR_OS, "could not symlink '%s' as '%s'", link_data, to); error = -1; } @@ -974,7 +974,7 @@ static int _cp_r_callback(void *ref, git_buf *from) return 0; if (p_unlink(info->to.ptr) < 0) { - giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'", + giterr_set(GITERR_OS, "cannot overwrite existing file '%s'", info->to.ptr); return GIT_EEXISTS; } diff --git a/src/filter.c b/src/filter.c index a0628d779..0d8831e0c 100644 --- a/src/filter.c +++ b/src/filter.c @@ -296,7 +296,7 @@ int git_filter_unregister(const char *name) /* cannot unregister default filters */ if (!strcmp(GIT_FILTER_CRLF, name) || !strcmp(GIT_FILTER_IDENT, name)) { - giterr_set(GITERR_FILTER, "Cannot unregister filter '%s'", name); + giterr_set(GITERR_FILTER, "cannot unregister filter '%s'", name); return -1; } @@ -306,7 +306,7 @@ int git_filter_unregister(const char *name) } if ((fdef = filter_registry_lookup(&pos, name)) == NULL) { - giterr_set(GITERR_FILTER, "Cannot find filter '%s' to unregister", name); + giterr_set(GITERR_FILTER, "cannot find filter '%s' to unregister", name); error = GIT_ENOTFOUND; goto done; } @@ -645,7 +645,7 @@ int git_filter_list_push( git_rwlock_rdunlock(&filter_registry.lock); if (fdef == NULL) { - giterr_set(GITERR_FILTER, "Cannot use an unregistered filter"); + giterr_set(GITERR_FILTER, "cannot use an unregistered filter"); return -1; } @@ -758,7 +758,7 @@ static int buf_from_blob(git_buf *out, git_blob *blob) git_off_t rawsize = git_blob_rawsize(blob); if (!git__is_sizet(rawsize)) { - giterr_set(GITERR_OS, "Blob is too large to filter"); + giterr_set(GITERR_OS, "blob is too large to filter"); return -1; } diff --git a/src/hashsig.c b/src/hashsig.c index e99637d8b..bea538349 100644 --- a/src/hashsig.c +++ b/src/hashsig.c @@ -214,7 +214,7 @@ static int hashsig_finalize_hashes(git_hashsig *sig) if (sig->mins.size < HASHSIG_HEAP_MIN_SIZE && !(sig->opt & GIT_HASHSIG_ALLOW_SMALL_FILES)) { giterr_set(GITERR_INVALID, - "File too small for similarity signature calculation"); + "file too small for similarity signature calculation"); return GIT_EBUFS; } @@ -286,7 +286,7 @@ int git_hashsig_create_fromfile( if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) { if ((error = (int)buflen) < 0) giterr_set(GITERR_OS, - "Read error on '%s' calculating similarity hashes", path); + "read error on '%s' calculating similarity hashes", path); break; } diff --git a/src/ignore.c b/src/ignore.c index dcbd5c1ca..d1a6c5550 100644 --- a/src/ignore.c +++ b/src/ignore.c @@ -175,7 +175,7 @@ static int parse_ignore_file( context = attrs->entry->path; if (git_mutex_lock(&attrs->lock) < 0) { - giterr_set(GITERR_OS, "Failed to lock ignore file"); + giterr_set(GITERR_OS, "failed to lock ignore file"); return -1; } diff --git a/src/index.c b/src/index.c index 42579f19a..f27fa16d2 100644 --- a/src/index.c +++ b/src/index.c @@ -570,7 +570,7 @@ int git_index_set_caps(git_index *index, int caps) if (!repo) return create_index_error( - -1, "Cannot access repository to set index caps"); + -1, "cannot access repository to set index caps"); if (!git_repository__cvar(&val, repo, GIT_CVAR_IGNORECASE)) index->ignore_case = (val != 0); @@ -639,7 +639,7 @@ int git_index_read(git_index *index, int force) if (!index->index_file_path) return create_index_error(-1, - "Failed to read index: The index is in-memory only"); + "failed to read index: The index is in-memory only"); index->on_disk = git_path_exists(index->index_file_path); @@ -653,7 +653,7 @@ int git_index_read(git_index *index, int force) ((updated = compare_checksum(index)) < 0)) { giterr_set( GITERR_INDEX, - "Failed to read index: '%s' no longer exists", + "failed to read index: '%s' no longer exists", index->index_file_path); return updated; } @@ -765,7 +765,7 @@ int git_index_set_version(git_index *index, unsigned int version) if (version < INDEX_VERSION_NUMBER_LB || version > INDEX_VERSION_NUMBER_UB) { - giterr_set(GITERR_INDEX, "Invalid version number"); + giterr_set(GITERR_INDEX, "invalid version number"); return -1; } @@ -805,7 +805,7 @@ int git_index_write_tree(git_oid *oid, git_index *index) if (repo == NULL) return create_index_error(-1, "Failed to write tree. " - "The index file is not backed up by an existing repository"); + "the index file is not backed up by an existing repository"); return git_tree__write_index(oid, index, repo); } @@ -847,7 +847,7 @@ const git_index_entry *git_index_get_bypath( if (git_idxmap_valid_index(index->entries_map, pos)) return git_idxmap_value_at(index->entries_map, pos); - giterr_set(GITERR_INDEX, "Index does not contain %s", path); + giterr_set(GITERR_INDEX, "index does not contain '%s'", path); return NULL; } @@ -934,7 +934,7 @@ static int index_entry_init( if (INDEX_OWNER(index) == NULL) return create_index_error(-1, - "Could not initialize index entry. " + "could not initialize index entry. " "Index is not backed up by an existing repository."); if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, true) < 0) @@ -1423,7 +1423,7 @@ int git_index_add_frombuffer( if (INDEX_OWNER(index) == NULL) return create_index_error(-1, - "Could not initialize index entry. " + "could not initialize index entry. " "Index is not backed up by an existing repository."); if (!valid_filemode(source_entry->mode)) { @@ -1637,7 +1637,7 @@ int git_index_remove(git_index *index, const char *path, int stage) if (index_find(&position, index, path, 0, stage) < 0) { giterr_set( - GITERR_INDEX, "Index does not contain %s at stage %d", path, stage); + GITERR_INDEX, "index does not contain %s at stage %d", path, stage); error = GIT_ENOTFOUND; } else { error = index_remove_entry(index, position); @@ -1709,7 +1709,7 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path) if (git_vector_bsearch2( &pos, &index->entries, index->entries_search_path, path) < 0) { - giterr_set(GITERR_INDEX, "Index does not contain %s", path); + giterr_set(GITERR_INDEX, "index does not contain %s", path); return GIT_ENOTFOUND; } @@ -2153,7 +2153,7 @@ void git_index_reuc_clear(git_index *index) static int index_error_invalid(const char *message) { - giterr_set(GITERR_INDEX, "Invalid data in index - %s", message); + giterr_set(GITERR_INDEX, "invalid data in index - %s", message); return -1; } @@ -3390,7 +3390,7 @@ static int index_apply_to_all( i--; /* back up foreach if we removed this */ break; default: - giterr_set(GITERR_INVALID, "Unknown index action %d", action); + giterr_set(GITERR_INVALID, "unknown index action %d", action); error = -1; break; } @@ -3475,13 +3475,13 @@ int git_indexwriter_init( if (!index->index_file_path) return create_index_error(-1, - "Failed to write index: The index is in-memory only"); + "failed to write index: The index is in-memory only"); if ((error = git_filebuf_open( &writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) { if (error == GIT_ELOCKED) - giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process"); + giterr_set(GITERR_INDEX, "the index is locked; this might be due to a concurrent or crashed process"); return error; } @@ -3530,7 +3530,7 @@ int git_indexwriter_commit(git_indexwriter *writer) if ((error = git_futils_filestamp_check( &writer->index->stamp, writer->index->index_file_path)) < 0) { - giterr_set(GITERR_OS, "Could not read index timestamp"); + giterr_set(GITERR_OS, "could not read index timestamp"); return -1; } diff --git a/src/indexer.c b/src/indexer.c index a3a866989..4e8919adb 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -83,12 +83,12 @@ static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack) /* Verify we recognize this pack file format. */ if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) { - giterr_set(GITERR_INDEXER, "Wrong pack signature"); + giterr_set(GITERR_INDEXER, "wrong pack signature"); return -1; } if (!pack_version_ok(hdr->hdr_version)) { - giterr_set(GITERR_INDEXER, "Wrong pack version"); + giterr_set(GITERR_INDEXER, "wrong pack version"); return -1; } @@ -376,7 +376,7 @@ static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_star GITERR_CHECK_ALLOC(entry); if (git_odb__hashobj(&oid, obj) < 0) { - giterr_set(GITERR_INDEXER, "Failed to hash object"); + giterr_set(GITERR_INDEXER, "failed to hash object"); goto on_error; } diff --git a/src/merge.c b/src/merge.c index 1142917bd..4d812da50 100644 --- a/src/merge.c +++ b/src/merge.c @@ -78,7 +78,7 @@ int merge_bases_many(git_commit_list **out, git_revwalk **walk_out, git_reposito unsigned int i; if (length < 2) { - giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %" PRIuZ ".", length); + giterr_set(GITERR_INVALID, "at least two commits are required to find an ancestor"); return -1; } @@ -104,7 +104,7 @@ int merge_bases_many(git_commit_list **out, git_revwalk **walk_out, git_reposito goto on_error; if (!result) { - giterr_set(GITERR_MERGE, "No merge base found"); + giterr_set(GITERR_MERGE, "no merge base found"); error = GIT_ENOTFOUND; goto on_error; } @@ -184,7 +184,7 @@ int git_merge_base_octopus(git_oid *out, git_repository *repo, size_t length, co assert(out && repo && input_array); if (length < 2) { - giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %" PRIuZ ".", length); + giterr_set(GITERR_INVALID, "at least two commits are required to find an ancestor"); return -1; } @@ -230,7 +230,7 @@ static int merge_bases(git_commit_list **out, git_revwalk **walk_out, git_reposi if (!result) { git_revwalk_free(walk); - giterr_set(GITERR_MERGE, "No merge base found"); + giterr_set(GITERR_MERGE, "no merge base found"); return GIT_ENOTFOUND; } @@ -574,7 +574,7 @@ int git_repository_mergehead_foreach( while ((line = git__strsep(&buffer, "\n")) != NULL) { if (strlen(line) != GIT_OID_HEXSZ) { - giterr_set(GITERR_INVALID, "Unable to parse OID - invalid length"); + giterr_set(GITERR_INVALID, "unable to parse OID - invalid length"); error = -1; goto cleanup; } @@ -591,7 +591,7 @@ int git_repository_mergehead_foreach( } if (*buffer) { - giterr_set(GITERR_MERGE, "No EOL at line %"PRIuZ, line_num); + giterr_set(GITERR_MERGE, "no EOL at line %"PRIuZ, line_num); error = -1; goto cleanup; } @@ -3043,7 +3043,7 @@ int git_merge_analysis( assert(analysis_out && preference_out && repo && their_heads); if (their_heads_len != 1) { - giterr_set(GITERR_MERGE, "Can only merge a single branch"); + giterr_set(GITERR_MERGE, "can only merge a single branch"); error = -1; goto done; } @@ -3099,7 +3099,7 @@ int git_merge( assert(repo && their_heads); if (their_heads_len != 1) { - giterr_set(GITERR_MERGE, "Can only merge a single branch"); + giterr_set(GITERR_MERGE, "can only merge a single branch"); return -1; } diff --git a/src/merge_file.c b/src/merge_file.c index 3f14a4f63..5ecd8f4d0 100644 --- a/src/merge_file.c +++ b/src/merge_file.c @@ -127,7 +127,7 @@ static int merge_file__xdiff( if ((xdl_result = xdl_merge(&ancestor_mmfile, &our_mmfile, &their_mmfile, &xmparam, &mmbuffer)) < 0) { - giterr_set(GITERR_MERGE, "Failed to merge files."); + giterr_set(GITERR_MERGE, "failed to merge files"); error = -1; goto done; } diff --git a/src/mwindow.c b/src/mwindow.c index 8a5b5caee..520e7685f 100644 --- a/src/mwindow.c +++ b/src/mwindow.c @@ -231,7 +231,7 @@ static int git_mwindow_close_lru(git_mwindow_file *mwf) } if (!lru_w) { - giterr_set(GITERR_OS, "Failed to close memory window. Couldn't find LRU"); + giterr_set(GITERR_OS, "failed to close memory window; couldn't find LRU"); return -1; } diff --git a/src/netops.c b/src/netops.c index 90326ea59..4b73baa0e 100644 --- a/src/netops.c +++ b/src/netops.c @@ -144,7 +144,7 @@ int gitno_connection_data_from_url( default_port = "80"; if (data->use_ssl) { - giterr_set(GITERR_NET, "Redirect from HTTPS to HTTP is not allowed"); + giterr_set(GITERR_NET, "redirect from HTTPS to HTTP is not allowed"); goto cleanup; } } else if (!git__prefixcmp(url, prefix_https)) { @@ -155,7 +155,7 @@ int gitno_connection_data_from_url( default_port = data->use_ssl ? "443" : "80"; if (!default_port) { - giterr_set(GITERR_NET, "Unrecognized URL prefix"); + giterr_set(GITERR_NET, "unrecognized URL prefix"); goto cleanup; } @@ -187,7 +187,7 @@ int gitno_connection_data_from_url( /* Check for errors in the resulting data */ if (original_host && url[0] != '/' && strcmp(original_host, data->host)) { - giterr_set(GITERR_NET, "Cross host redirect not allowed"); + giterr_set(GITERR_NET, "cross host redirect not allowed"); error = -1; } } @@ -237,7 +237,7 @@ int gitno_extract_url_parts( const char *_host, *_port, *_path, *_userinfo; if (http_parser_parse_url(url, strlen(url), false, &u)) { - giterr_set(GITERR_NET, "Malformed URL '%s'", url); + giterr_set(GITERR_NET, "malformed URL '%s'", url); return GIT_EINVALIDSPEC; } diff --git a/src/notes.c b/src/notes.c index fe8d2164f..75108b9c9 100644 --- a/src/notes.c +++ b/src/notes.c @@ -15,7 +15,7 @@ static int note_error_notfound(void) { - giterr_set(GITERR_INVALID, "Note could not be found"); + giterr_set(GITERR_INVALID, "note could not be found"); return GIT_ENOTFOUND; } @@ -226,7 +226,7 @@ static int remove_note_in_tree_enotfound_cb( GIT_UNUSED(note_oid); GIT_UNUSED(fanout); - giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha); + giterr_set(GITERR_REPOSITORY, "object '%s' has no note", annotated_object_sha); return current_error; } @@ -244,7 +244,7 @@ static int insert_note_in_tree_eexists_cb(git_tree **out, GIT_UNUSED(note_oid); GIT_UNUSED(fanout); - giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha); + giterr_set(GITERR_REPOSITORY, "note for '%s' exists already", annotated_object_sha); return current_error; } diff --git a/src/object.c b/src/object.c index 1d45f9f1b..2da36a2ee 100644 --- a/src/object.c +++ b/src/object.c @@ -66,12 +66,12 @@ int git_object__from_odb_object( /* Validate type match */ if (type != GIT_OBJ_ANY && type != odb_obj->cached.type) { giterr_set(GITERR_INVALID, - "The requested type does not match the type in the ODB"); + "the requested type does not match the type in the ODB"); return GIT_ENOTFOUND; } if ((object_size = git_object__size(odb_obj->cached.type)) == 0) { - giterr_set(GITERR_INVALID, "The requested type is invalid"); + giterr_set(GITERR_INVALID, "the requested type is invalid"); return GIT_ENOTFOUND; } @@ -122,7 +122,7 @@ int git_object_lookup_prefix( assert(repo && object_out && id); if (len < GIT_OID_MINPREFIXLEN) { - giterr_set(GITERR_OBJECT, "Ambiguous lookup - OID prefix is too short"); + giterr_set(GITERR_OBJECT, "ambiguous lookup - OID prefix is too short"); return GIT_EAMBIGUOUS; } @@ -147,7 +147,7 @@ int git_object_lookup_prefix( if (type != GIT_OBJ_ANY && type != object->cached.type) { git_object_free(object); giterr_set(GITERR_INVALID, - "The requested type does not match the type in ODB"); + "the requested type does not match the type in ODB"); return GIT_ENOTFOUND; } @@ -292,7 +292,7 @@ static int peel_error(int error, const git_oid *oid, git_otype type) git_oid_fmt(hex_oid, oid); hex_oid[GIT_OID_HEXSZ] = '\0'; - giterr_set(GITERR_OBJECT, "The git_object of id '%s' can not be " + giterr_set(GITERR_OBJECT, "the git_object of id '%s' can not be " "successfully peeled into a %s (git_otype=%i).", hex_oid, type_name, type); return error; diff --git a/src/odb.c b/src/odb.c index 7b194c70f..dc98a6ff7 100644 --- a/src/odb.c +++ b/src/odb.c @@ -176,7 +176,7 @@ int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type) int error = 0; if (!git_object_typeisloose(type)) { - giterr_set(GITERR_INVALID, "Invalid object type for hash"); + giterr_set(GITERR_INVALID, "invalid object type for hash"); return -1; } @@ -199,7 +199,7 @@ int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type) * If size is not zero, the file was truncated after we originally * stat'd it, so we consider this a read failure too */ if (read_len < 0 || size > 0) { - giterr_set(GITERR_OS, "Error reading file for hashing"); + giterr_set(GITERR_OS, "error reading file for hashing"); error = -1; goto done; @@ -251,7 +251,7 @@ int git_odb__hashlink(git_oid *out, const char *path) return -1; if (!git__is_int(st.st_size) || (int)st.st_size < 0) { - giterr_set(GITERR_FILESYSTEM, "File size overflow for 32-bit systems"); + giterr_set(GITERR_FILESYSTEM, "file size overflow for 32-bit systems"); return -1; } @@ -269,7 +269,7 @@ int git_odb__hashlink(git_oid *out, const char *path) read_len = p_readlink(path, link_data, size); link_data[size] = '\0'; if (read_len != size) { - giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", path); + giterr_set(GITERR_OS, "failed to read symlink data for '%s'", path); git__free(link_data); return -1; } @@ -295,7 +295,7 @@ int git_odb_hashfile(git_oid *out, const char *path, git_otype type) return fd; if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) { - giterr_set(GITERR_OS, "File size overflow for 32-bit systems"); + giterr_set(GITERR_OS, "file size overflow for 32-bit systems"); p_close(fd); return -1; } @@ -475,7 +475,7 @@ size_t git_odb_num_backends(git_odb *odb) static int git_odb__error_unsupported_in_backend(const char *action) { giterr_set(GITERR_ODB, - "Cannot %s - unsupported in the loaded odb backends", action); + "cannot %s - unsupported in the loaded odb backends", action); return -1; } @@ -492,7 +492,7 @@ int git_odb_get_backend(git_odb_backend **out, git_odb *odb, size_t pos) return 0; } - giterr_set(GITERR_ODB, "No ODB backend loaded at index %" PRIuZ, pos); + giterr_set(GITERR_ODB, "no ODB backend loaded at index %" PRIuZ, pos); return GIT_ENOTFOUND; } @@ -517,7 +517,7 @@ static int add_default_backends( if (as_alternates) return 0; - giterr_set(GITERR_ODB, "Failed to load object database in '%s'", objects_dir); + giterr_set(GITERR_ODB, "failed to load object database in '%s'", objects_dir); return -1; } @@ -1264,7 +1264,7 @@ static int git_odb_stream__invalid_length( const char *action) { giterr_set(GITERR_ODB, - "Cannot %s - " + "cannot %s - " "Invalid length. %"PRIuZ" was expected. The " "total size of the received chunks amounts to %"PRIuZ".", action, stream->declared_size, stream->received_bytes); @@ -1399,17 +1399,17 @@ int git_odb__error_notfound( if (oid != NULL) { char oid_str[GIT_OID_HEXSZ + 1]; git_oid_tostr(oid_str, oid_len+1, oid); - giterr_set(GITERR_ODB, "Object not found - %s (%.*s)", + giterr_set(GITERR_ODB, "object not found - %s (%.*s)", message, (int) oid_len, oid_str); } else - giterr_set(GITERR_ODB, "Object not found - %s", message); + giterr_set(GITERR_ODB, "object not found - %s", message); return GIT_ENOTFOUND; } int git_odb__error_ambiguous(const char *message) { - giterr_set(GITERR_ODB, "Ambiguous SHA1 prefix - %s", message); + giterr_set(GITERR_ODB, "ambiguous SHA1 prefix - %s", message); return GIT_EAMBIGUOUS; } diff --git a/src/odb_loose.c b/src/odb_loose.c index f312b9c9c..b2e2f1fb7 100644 --- a/src/odb_loose.c +++ b/src/odb_loose.c @@ -214,7 +214,7 @@ static int finish_inflate(z_stream *s) inflateEnd(s); if ((status != Z_STREAM_END) || (s->avail_in != 0)) { - giterr_set(GITERR_ZLIB, "Failed to finish ZLib inflation. Stream aborted prematurely"); + giterr_set(GITERR_ZLIB, "failed to finish zlib inflation; stream aborted prematurely"); return -1; } @@ -243,7 +243,7 @@ static int inflate_buffer(void *in, size_t inlen, void *out, size_t outlen) zs.avail_in = (uInt)inlen; if (inflateInit(&zs) < Z_OK) { - giterr_set(GITERR_ZLIB, "Failed to inflate buffer"); + giterr_set(GITERR_ZLIB, "failed to inflate buffer"); return -1; } @@ -255,7 +255,7 @@ static int inflate_buffer(void *in, size_t inlen, void *out, size_t outlen) if (status != Z_STREAM_END /* || zs.avail_in != 0 */ || zs.total_out != outlen) { - giterr_set(GITERR_ZLIB, "Failed to inflate buffer. Stream aborted prematurely"); + giterr_set(GITERR_ZLIB, "failed to inflate buffer; stream aborted prematurely"); return -1; } @@ -319,7 +319,7 @@ static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_buf *obj) */ if ((used = get_binary_object_header(&hdr, obj)) == 0 || !git_object_typeisloose(hdr.type)) { - giterr_set(GITERR_ODB, "Failed to inflate loose object."); + giterr_set(GITERR_ODB, "failed to inflate loose object"); return -1; } @@ -366,7 +366,7 @@ static int inflate_disk_obj(git_rawobj *out, git_buf *obj) (used = get_object_header(&hdr, head)) == 0 || !git_object_typeisloose(hdr.type)) { - giterr_set(GITERR_ODB, "Failed to inflate disk object."); + giterr_set(GITERR_ODB, "failed to inflate disk object"); return -1; } @@ -455,7 +455,7 @@ static int read_header_loose(git_rawobj *out, git_buf *loc) || get_object_header(&header_obj, inflated_buffer) == 0 || git_object_typeisloose(header_obj.type) == 0) { - giterr_set(GITERR_ZLIB, "Failed to read loose object header"); + giterr_set(GITERR_ZLIB, "failed to read loose object header"); error = -1; } else { out->len = header_obj.size; diff --git a/src/oid.c b/src/oid.c index 9fe2ebb65..9dc719194 100644 --- a/src/oid.c +++ b/src/oid.c @@ -16,7 +16,7 @@ static char to_hex[] = "0123456789abcdef"; static int oid_error_invalid(const char *msg) { - giterr_set(GITERR_INVALID, "Unable to parse OID - %s", msg); + giterr_set(GITERR_INVALID, "unable to parse OID - %s", msg); return -1; } @@ -380,7 +380,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) node_index idx; if (os->full) { - giterr_set(GITERR_INVALID, "Unable to shorten OID - OID set full"); + giterr_set(GITERR_INVALID, "unable to shorten OID - OID set full"); return -1; } @@ -395,7 +395,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) trie_node *node; if (c == -1) { - giterr_set(GITERR_INVALID, "Unable to shorten OID - invalid hex value"); + giterr_set(GITERR_INVALID, "unable to shorten OID - invalid hex value"); return -1; } @@ -410,7 +410,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]); if (node == NULL) { if (os->full) - giterr_set(GITERR_INVALID, "Unable to shorten OID - OID set full"); + giterr_set(GITERR_INVALID, "unable to shorten OID - OID set full"); return -1; } } @@ -418,7 +418,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid) if (node->children[c] == 0) { if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL) { if (os->full) - giterr_set(GITERR_INVALID, "Unable to shorten OID - OID set full"); + giterr_set(GITERR_INVALID, "unable to shorten OID - OID set full"); return -1; } break; diff --git a/src/openssl_stream.c b/src/openssl_stream.c index ddb9c4ab9..bb9b32c67 100644 --- a/src/openssl_stream.c +++ b/src/openssl_stream.c @@ -257,10 +257,10 @@ static int ssl_set_error(SSL *ssl, int error) switch (err) { case SSL_ERROR_WANT_CONNECT: case SSL_ERROR_WANT_ACCEPT: - giterr_set(GITERR_NET, "SSL error: connection failure\n"); + giterr_set(GITERR_NET, "SSL error: connection failure"); break; case SSL_ERROR_WANT_X509_LOOKUP: - giterr_set(GITERR_NET, "SSL error: x509 error\n"); + giterr_set(GITERR_NET, "SSL error: x509 error"); break; case SSL_ERROR_SYSCALL: e = ERR_get_error(); @@ -327,7 +327,7 @@ static int verify_server_cert(SSL *ssl, const char *host) int i = -1,j; if (SSL_get_verify_result(ssl) != X509_V_OK) { - giterr_set(GITERR_SSL, "The SSL certificate is invalid"); + giterr_set(GITERR_SSL, "the SSL certificate is invalid"); return GIT_ECERTIFICATE; } diff --git a/src/pack-objects.c b/src/pack-objects.c index 9f62322f7..6dbb6cf6c 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -162,7 +162,7 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo) git_mutex_init(&pb->progress_mutex) || git_cond_init(&pb->progress_cond)) { - giterr_set(GITERR_OS, "Failed to initialize packbuilder mutex"); + giterr_set(GITERR_OS, "failed to initialize packbuilder mutex"); goto on_error; } @@ -225,7 +225,7 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid, GITERR_CHECK_ALLOC_MULTIPLY(&newsize, newsize, 3 / 2); if (!git__is_uint32(newsize)) { - giterr_set(GITERR_NOMEMORY, "Packfile too large to fit in memory."); + giterr_set(GITERR_NOMEMORY, "packfile too large to fit in memory."); return -1; } @@ -298,7 +298,7 @@ static int get_delta(void **out, git_odb *odb, git_pobject *po) goto on_error; if (error == GIT_EBUFS || delta_size != po->delta_size) { - giterr_set(GITERR_INVALID, "Delta size changed"); + giterr_set(GITERR_INVALID, "delta size changed"); goto on_error; } @@ -808,7 +808,7 @@ static int try_delta(git_packbuilder *pb, struct unpacked *trg, if (sz != trg_size) { giterr_set(GITERR_INVALID, - "Inconsistent target object length"); + "inconsistent target object length"); return -1; } @@ -830,7 +830,7 @@ static int try_delta(git_packbuilder *pb, struct unpacked *trg, if (sz != src_size) { giterr_set(GITERR_INVALID, - "Inconsistent source object length"); + "inconsistent source object length"); return -1; } diff --git a/src/pack.c b/src/pack.c index 2e19b3b62..345ff5259 100644 --- a/src/pack.c +++ b/src/pack.c @@ -45,7 +45,7 @@ static int pack_entry_find_offset( static int packfile_error(const char *message) { - giterr_set(GITERR_ODB, "Invalid pack file - %s", message); + giterr_set(GITERR_ODB, "invalid pack file - %s", message); return -1; } @@ -99,7 +99,7 @@ static int cache_init(git_pack_cache *cache) cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT; if (git_mutex_init(&cache->lock)) { - giterr_set(GITERR_OS, "Failed to initialize pack cache mutex"); + giterr_set(GITERR_OS, "failed to initialize pack cache mutex"); git__free(cache->entries); cache->entries = NULL; @@ -226,7 +226,7 @@ static int pack_index_check(const char *path, struct git_pack_file *p) if (p_fstat(fd, &st) < 0) { p_close(fd); - giterr_set(GITERR_OS, "Unable to stat pack index '%s'", path); + giterr_set(GITERR_OS, "unable to stat pack index '%s'", path); return -1; } @@ -235,7 +235,7 @@ static int pack_index_check(const char *path, struct git_pack_file *p) (idx_size = (size_t)st.st_size) < 4 * 256 + 20 + 20) { p_close(fd); - giterr_set(GITERR_ODB, "Invalid pack index '%s'", path); + giterr_set(GITERR_ODB, "invalid pack index '%s'", path); return -1; } @@ -1082,7 +1082,7 @@ static int packfile_open(struct git_pack_file *p) return 0; cleanup: - giterr_set(GITERR_OS, "Invalid packfile '%s'", p->pack_name); + giterr_set(GITERR_OS, "invalid packfile '%s'", p->pack_name); if (p->mwf.fd >= 0) p_close(p->mwf.fd); @@ -1158,7 +1158,7 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path) p->index_version = -1; if (git_mutex_init(&p->lock)) { - giterr_set(GITERR_OS, "Failed to initialize packfile mutex"); + giterr_set(GITERR_OS, "failed to initialize packfile mutex"); git__free(p); return -1; } diff --git a/src/patch_generate.c b/src/patch_generate.c index 0e5d1db31..3559cc2b8 100644 --- a/src/patch_generate.c +++ b/src/patch_generate.c @@ -417,7 +417,7 @@ static int diff_required(git_diff *diff, const char *action) { if (diff) return 0; - giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action); + giterr_set(GITERR_INVALID, "must provide valid diff to %s", action); return -1; } @@ -776,7 +776,7 @@ int git_patch_generated_from_diff( delta = git_vector_get(&diff->deltas, idx); if (!delta) { - giterr_set(GITERR_INVALID, "Index out of range for delta in diff"); + giterr_set(GITERR_INVALID, "index out of range for delta in diff"); return GIT_ENOTFOUND; } diff --git a/src/path.c b/src/path.c index 767552778..bffde930b 100644 --- a/src/path.c +++ b/src/path.c @@ -341,7 +341,7 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base) if (p_realpath(path, buf) == NULL) { /* giterr_set resets the errno when dealing with a GITERR_OS kind of error */ int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1; - giterr_set(GITERR_OS, "Failed to resolve path '%s'", path); + giterr_set(GITERR_OS, "failed to resolve path '%s'", path); git_buf_clear(path_out); @@ -632,24 +632,24 @@ int git_path_set_error(int errno_value, const char *path, const char *action) switch (errno_value) { case ENOENT: case ENOTDIR: - giterr_set(GITERR_OS, "Could not find '%s' to %s", path, action); + giterr_set(GITERR_OS, "could not find '%s' to %s", path, action); return GIT_ENOTFOUND; case EINVAL: case ENAMETOOLONG: - giterr_set(GITERR_OS, "Invalid path for filesystem '%s'", path); + giterr_set(GITERR_OS, "invalid path for filesystem '%s'", path); return GIT_EINVALIDSPEC; case EEXIST: - giterr_set(GITERR_OS, "Failed %s - '%s' already exists", action, path); + giterr_set(GITERR_OS, "failed %s - '%s' already exists", action, path); return GIT_EEXISTS; case EACCES: - giterr_set(GITERR_OS, "Failed %s - '%s' is locked", action, path); + giterr_set(GITERR_OS, "failed %s - '%s' is locked", action, path); return GIT_ELOCKED; default: - giterr_set(GITERR_OS, "Could not %s '%s'", action, path); + giterr_set(GITERR_OS, "could not %s '%s'", action, path); return -1; } } @@ -758,7 +758,7 @@ int git_path_resolve_relative(git_buf *path, size_t ceiling) /* error out if trying to up one from a hard base */ if (to == base && ceiling != 0) { giterr_set(GITERR_INVALID, - "Cannot strip root component off url"); + "cannot strip root component off url"); return -1; } @@ -987,7 +987,7 @@ int git_path_iconv(git_path_iconv_t *ic, const char **in, size_t *inlen) return 0; fail: - giterr_set(GITERR_OS, "Unable to convert unicode path data"); + giterr_set(GITERR_OS, "unable to convert unicode path data"); return -1; } @@ -1080,7 +1080,7 @@ int git_path_direach( wd_len = git_buf_len(path); if ((dir = opendir(path->ptr)) == NULL) { - giterr_set(GITERR_OS, "Failed to open directory '%s'", path->ptr); + giterr_set(GITERR_OS, "failed to open directory '%s'", path->ptr); if (errno == ENOENT) return GIT_ENOTFOUND; @@ -1161,13 +1161,13 @@ int git_path_diriter_init( git_path_trim_slashes(&diriter->path_utf8); if (diriter->path_utf8.size == 0) { - giterr_set(GITERR_FILESYSTEM, "Could not open directory '%s'", path); + giterr_set(GITERR_FILESYSTEM, "could not open directory '%s'", path); return -1; } if ((diriter->parent_len = git_win32_path_from_utf8(diriter->path, diriter->path_utf8.ptr)) < 0 || !git_win32__findfirstfile_filter(path_filter, diriter->path_utf8.ptr)) { - giterr_set(GITERR_OS, "Could not parse the directory path '%s'", path); + giterr_set(GITERR_OS, "could not parse the directory path '%s'", path); return -1; } @@ -1180,7 +1180,7 @@ int git_path_diriter_init( is_win7_or_later ? FIND_FIRST_EX_LARGE_FETCH : 0); if (diriter->handle == INVALID_HANDLE_VALUE) { - giterr_set(GITERR_OS, "Could not open directory '%s'", path); + giterr_set(GITERR_OS, "could not open directory '%s'", path); return -1; } @@ -1310,14 +1310,14 @@ int git_path_diriter_init( git_path_trim_slashes(&diriter->path); if (diriter->path.size == 0) { - giterr_set(GITERR_FILESYSTEM, "Could not open directory '%s'", path); + giterr_set(GITERR_FILESYSTEM, "could not open directory '%s'", path); return -1; } if ((diriter->dir = opendir(diriter->path.ptr)) == NULL) { git_buf_free(&diriter->path); - giterr_set(GITERR_OS, "Failed to open directory '%s'", path); + giterr_set(GITERR_OS, "failed to open directory '%s'", path); return -1; } @@ -1350,7 +1350,7 @@ int git_path_diriter_next(git_path_diriter *diriter) return GIT_ITEROVER; giterr_set(GITERR_OS, - "Could not read directory '%s'", diriter->path.ptr); + "could not read directory '%s'", diriter->path.ptr); return -1; } } while (skip_dot && git_path_is_dot_or_dotdot(de->d_name)); diff --git a/src/pathspec.c b/src/pathspec.c index 361b398b8..00dba4f6b 100644 --- a/src/pathspec.c +++ b/src/pathspec.c @@ -487,7 +487,7 @@ static int pathspec_match_from_iterator( /* if every pattern failed to match, then we have failed */ if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_files) { - giterr_set(GITERR_INVALID, "No matching files were found"); + giterr_set(GITERR_INVALID, "no matching files were found"); error = GIT_ENOTFOUND; } @@ -658,7 +658,7 @@ int git_pathspec_match_diff( /* if every pattern failed to match, then we have failed */ if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_deltas) { - giterr_set(GITERR_INVALID, "No matching deltas were found"); + giterr_set(GITERR_INVALID, "no matching deltas were found"); error = GIT_ENOTFOUND; } diff --git a/src/posix.c b/src/posix.c index b3f1a1cd3..e68f324f6 100644 --- a/src/posix.c +++ b/src/posix.c @@ -240,7 +240,7 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs out->len = 0; if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) { - giterr_set(GITERR_OS, "Trying to map shared-writeable"); + giterr_set(GITERR_OS, "trying to map shared-writeable"); return -1; } diff --git a/src/push.c b/src/push.c index b4901388b..09c234034 100644 --- a/src/push.c +++ b/src/push.c @@ -90,7 +90,7 @@ static void free_refspec(push_spec *spec) static int check_rref(char *ref) { if (git__prefixcmp(ref, "refs/")) { - giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref); + giterr_set(GITERR_INVALID, "not a valid reference '%s'", ref); return -1; } @@ -111,7 +111,7 @@ static int check_lref(git_push *push, char *ref) giterr_set(GITERR_REFERENCE, "src refspec '%s' does not match any existing object", ref); else - giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref); + giterr_set(GITERR_INVALID, "not a valid reference '%s'", ref); return -1; } @@ -321,7 +321,7 @@ static int revwalk(git_vector *commits, git_push *push) if (!git_odb_exists(push->repo->_odb, &spec->roid)) { giterr_set(GITERR_REFERENCE, - "Cannot push because a reference that you are trying to update on the remote contains commits that are not present locally."); + "cannot push because a reference that you are trying to update on the remote contains commits that are not present locally."); error = GIT_ENONFASTFORWARD; goto on_error; } @@ -332,7 +332,7 @@ static int revwalk(git_vector *commits, git_push *push) if (error == GIT_ENOTFOUND || (!error && !git_oid_equal(&base, &spec->roid))) { giterr_set(GITERR_REFERENCE, - "Cannot push non-fastforwardable reference"); + "cannot push non-fastforwardable reference"); error = GIT_ENONFASTFORWARD; goto on_error; } @@ -553,7 +553,7 @@ static int calculate_work(git_push *push) /* This is a create or update. Local ref must exist. */ if (git_reference_name_to_id( &spec->loid, push->repo, spec->refspec.src) < 0) { - giterr_set(GITERR_REFERENCE, "No such reference '%s'", spec->refspec.src); + giterr_set(GITERR_REFERENCE, "no such reference '%s'", spec->refspec.src); return -1; } } @@ -579,7 +579,7 @@ static int do_push(git_push *push, const git_remote_callbacks *callbacks) git_transport *transport = push->remote->transport; if (!transport->push) { - giterr_set(GITERR_NET, "Remote transport doesn't support push"); + giterr_set(GITERR_NET, "remote transport doesn't support push"); error = -1; goto on_error; } diff --git a/src/rebase.c b/src/rebase.c index ef044692a..b2024a439 100644 --- a/src/rebase.c +++ b/src/rebase.c @@ -152,7 +152,7 @@ GIT_INLINE(int) rebase_readint( return error; if (git__strtol32(&num, asc_out->ptr, &eol, 10) < 0 || num < 0 || *eol) { - giterr_set(GITERR_REBASE, "The file '%s' contains an invalid numeric value", filename); + giterr_set(GITERR_REBASE, "the file '%s' contains an invalid numeric value", filename); return -1; } @@ -170,7 +170,7 @@ GIT_INLINE(int) rebase_readoid( return error; if (str_out->size != GIT_OID_HEXSZ || git_oid_fromstr(out, str_out->ptr) < 0) { - giterr_set(GITERR_REBASE, "The file '%s' contains an invalid object ID", filename); + giterr_set(GITERR_REBASE, "the file '%s' contains an invalid object ID", filename); return -1; } @@ -316,7 +316,7 @@ int git_rebase_open( goto done; if (rebase->type == GIT_REBASE_TYPE_NONE) { - giterr_set(GITERR_REBASE, "There is no rebase in progress"); + giterr_set(GITERR_REBASE, "there is no rebase in progress"); error = GIT_ENOTFOUND; goto done; } @@ -372,14 +372,14 @@ int git_rebase_open( switch (rebase->type) { case GIT_REBASE_TYPE_INTERACTIVE: - giterr_set(GITERR_REBASE, "Interactive rebase is not supported"); + giterr_set(GITERR_REBASE, "interactive rebase is not supported"); error = -1; break; case GIT_REBASE_TYPE_MERGE: error = rebase_open_merge(rebase); break; case GIT_REBASE_TYPE_APPLY: - giterr_set(GITERR_REBASE, "Patch application rebase is not supported"); + giterr_set(GITERR_REBASE, "patch application rebase is not supported"); error = -1; break; default: @@ -478,7 +478,7 @@ static int rebase_setupfiles(git_rebase *rebase) git_oid_fmt(orig_head, &rebase->orig_head_id); if (p_mkdir(rebase->state_path, REBASE_DIR_MODE) < 0) { - giterr_set(GITERR_OS, "Failed to create rebase directory '%s'", rebase->state_path); + giterr_set(GITERR_OS, "failed to create rebase directory '%s'", rebase->state_path); return -1; } @@ -511,7 +511,7 @@ static int rebase_ensure_not_in_progress(git_repository *repo) return error; if (type != GIT_REBASE_TYPE_NONE) { - giterr_set(GITERR_REBASE, "There is an existing rebase in progress"); + giterr_set(GITERR_REBASE, "there is an existing rebase in progress"); return -1; } @@ -536,7 +536,7 @@ static int rebase_ensure_not_dirty( goto done; if (git_diff_num_deltas(diff) > 0) { - giterr_set(GITERR_REBASE, "Uncommitted changes exist in index"); + giterr_set(GITERR_REBASE, "uncommitted changes exist in index"); error = fail_with; goto done; } @@ -550,7 +550,7 @@ static int rebase_ensure_not_dirty( goto done; if (git_diff_num_deltas(diff) > 0) { - giterr_set(GITERR_REBASE, "Unstaged changes exist in workdir"); + giterr_set(GITERR_REBASE, "unstaged changes exist in workdir"); error = fail_with; goto done; } @@ -807,7 +807,7 @@ static int rebase_next_merge( goto done; if ((parent_count = git_commit_parentcount(current_commit)) > 1) { - giterr_set(GITERR_REBASE, "Cannot rebase a merge commit"); + giterr_set(GITERR_REBASE, "cannot rebase a merge commit"); error = -1; goto done; } else if (parent_count) { @@ -864,7 +864,7 @@ static int rebase_next_inmemory( goto done; if ((parent_count = git_commit_parentcount(current_commit)) > 1) { - giterr_set(GITERR_REBASE, "Cannot rebase a merge commit"); + giterr_set(GITERR_REBASE, "cannot rebase a merge commit"); error = -1; goto done; } else if (parent_count) { @@ -1259,7 +1259,7 @@ static int rebase_copy_notes( goto done; on_error: - giterr_set(GITERR_REBASE, "Invalid rewritten file at line %d", linenum); + giterr_set(GITERR_REBASE, "invalid rewritten file at line %d", linenum); error = -1; done: diff --git a/src/refdb.c b/src/refdb.c index 85c84892b..1ee0efb31 100644 --- a/src/refdb.c +++ b/src/refdb.c @@ -128,7 +128,7 @@ int git_refdb_iterator(git_reference_iterator **out, git_refdb *db, const char * int error; if (!db->backend || !db->backend->iterator) { - giterr_set(GITERR_REFERENCE, "This backend doesn't support iterators"); + giterr_set(GITERR_REFERENCE, "this backend doesn't support iterators"); return -1; } diff --git a/src/refdb_fs.c b/src/refdb_fs.c index 8739d5b89..e40f48bd5 100644 --- a/src/refdb_fs.c +++ b/src/refdb_fs.c @@ -185,7 +185,7 @@ static int packed_reload(refdb_fs_backend *backend) return 0; parse_failed: - giterr_set(GITERR_REFERENCE, "Corrupted packed references file"); + giterr_set(GITERR_REFERENCE, "corrupted packed references file"); git_sortedcache_clear(backend->refcache, false); git_sortedcache_wunlock(backend->refcache); @@ -212,7 +212,7 @@ static int loose_parse_oid( return 0; corrupted: - giterr_set(GITERR_REFERENCE, "Corrupted loose reference file: %s", filename); + giterr_set(GITERR_REFERENCE, "corrupted loose reference file: %s", filename); return -1; } @@ -349,7 +349,7 @@ static const char *loose_parse_symbolic(git_buf *file_content) refname_start = (const char *)file_content->ptr; if (git_buf_len(file_content) < header_len + 1) { - giterr_set(GITERR_REFERENCE, "Corrupted loose reference file"); + giterr_set(GITERR_REFERENCE, "corrupted loose reference file"); return NULL; } @@ -398,7 +398,7 @@ static int loose_lookup( static int ref_error_notfound(const char *name) { - giterr_set(GITERR_REFERENCE, "Reference '%s' not found", name); + giterr_set(GITERR_REFERENCE, "reference '%s' not found", name); return GIT_ENOTFOUND; } @@ -691,7 +691,7 @@ static int reference_path_available( if (exists) { giterr_set(GITERR_REFERENCE, - "Failed to write reference '%s': a reference with " + "failed to write reference '%s': a reference with " "that name already exists.", new_ref); return GIT_EEXISTS; } @@ -705,7 +705,7 @@ static int reference_path_available( if (ref && !ref_is_available(old_ref, new_ref, ref->name)) { git_sortedcache_runlock(backend->refcache); giterr_set(GITERR_REFERENCE, - "Path to reference '%s' collides with existing one", new_ref); + "path to reference '%s' collides with existing one", new_ref); return -1; } } @@ -722,7 +722,7 @@ static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char * assert(file && backend && name); if (!git_path_isvalid(backend->repo, name, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS)) { - giterr_set(GITERR_INVALID, "Invalid reference name '%s'.", name); + giterr_set(GITERR_INVALID, "invalid reference name '%s'", name); return GIT_EINVALIDSPEC; } @@ -1484,7 +1484,7 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) #define seek_forward(_increase) do { \ if (_increase >= buf_size) { \ - giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \ + giterr_set(GITERR_INVALID, "ran out of data while parsing reflog"); \ goto fail; \ } \ buf += _increase; \ @@ -1700,7 +1700,7 @@ static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char repo = backend->repo; if (!git_path_isvalid(backend->repo, refname, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS)) { - giterr_set(GITERR_INVALID, "Invalid reference name '%s'.", refname); + giterr_set(GITERR_INVALID, "invalid reference name '%s'", refname); return GIT_EINVALIDSPEC; } @@ -1709,7 +1709,7 @@ static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char if (!git_path_isfile(git_buf_cstr(&log_path))) { giterr_set(GITERR_INVALID, - "Log file for reference '%s' doesn't exist.", refname); + "log file for reference '%s' doesn't exist", refname); error = -1; goto cleanup; } @@ -1889,7 +1889,7 @@ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_ p_close(fd); if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) { - giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); + giterr_set(GITERR_OS, "failed to rename reflog for %s", new_name); error = -1; goto cleanup; } @@ -1906,7 +1906,7 @@ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_ } if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) { - giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); + giterr_set(GITERR_OS, "failed to rename reflog for %s", new_name); error = -1; } diff --git a/src/reflog.c b/src/reflog.c index 9ce9aee6f..98ef1b669 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -93,7 +93,7 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_sign if (newline) { if (newline[1] != '\0') { - giterr_set(GITERR_INVALID, "Reflog message cannot contain newline"); + giterr_set(GITERR_INVALID, "reflog message cannot contain newline"); goto cleanup; } @@ -193,7 +193,7 @@ int git_reflog_drop(git_reflog *reflog, size_t idx, int rewrite_previous_entry) entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); if (entry == NULL) { - giterr_set(GITERR_REFERENCE, "No reflog entry at index %"PRIuZ, idx); + giterr_set(GITERR_REFERENCE, "no reflog entry at index %"PRIuZ, idx); return GIT_ENOTFOUND; } diff --git a/src/refs.c b/src/refs.c index bff443ac9..dbc7e5e8e 100644 --- a/src/refs.c +++ b/src/refs.c @@ -236,7 +236,7 @@ int git_reference_lookup_resolved( if (scan_type != GIT_REF_OID && max_nesting != 0) { giterr_set(GITERR_REFERENCE, - "Cannot resolve reference (>%u levels deep)", max_nesting); + "cannot resolve reference (>%u levels deep)", max_nesting); git_reference_free(ref); return -1; } @@ -298,7 +298,7 @@ cleanup: if (error && !foundvalid) { /* never found a valid reference name */ giterr_set(GITERR_REFERENCE, - "Could not use '%s' as valid reference name", git_buf_cstr(&name)); + "could not use '%s' as valid reference name", git_buf_cstr(&name)); } if (error == GIT_ENOTFOUND) @@ -396,7 +396,7 @@ static int reference__create( if (!git_object__is_valid(repo, oid, GIT_OBJ_ANY)) { giterr_set(GITERR_REFERENCE, - "Target OID for the reference doesn't exist on the repository"); + "target OID for the reference doesn't exist on the repository"); return -1; } @@ -524,7 +524,7 @@ static int ensure_is_an_updatable_direct_reference(git_reference *ref) if (ref->type == GIT_REF_OID) return 0; - giterr_set(GITERR_REFERENCE, "Cannot set OID on symbolic reference"); + giterr_set(GITERR_REFERENCE, "cannot set OID on symbolic reference"); return -1; } @@ -552,7 +552,7 @@ static int ensure_is_an_updatable_symbolic_reference(git_reference *ref) if (ref->type == GIT_REF_SYMBOLIC) return 0; - giterr_set(GITERR_REFERENCE, "Cannot set symbolic target on a direct reference"); + giterr_set(GITERR_REFERENCE, "cannot set symbolic target on a direct reference"); return -1; } @@ -599,7 +599,7 @@ static int reference__rename(git_reference **out, git_reference *ref, const char /* Update HEAD it was pointing to the reference being renamed */ if (should_head_be_updated && (error = git_repository_set_head(ref->db->repo, normalized)) < 0) { - giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference"); + giterr_set(GITERR_REFERENCE, "failed to update HEAD after renaming reference"); return error; } @@ -636,7 +636,7 @@ int git_reference_resolve(git_reference **ref_out, const git_reference *ref) return git_reference_lookup_resolved(ref_out, ref->db->repo, ref->target.symbolic, -1); default: - giterr_set(GITERR_REFERENCE, "Invalid reference"); + giterr_set(GITERR_REFERENCE, "invalid reference"); return -1; } } @@ -973,7 +973,7 @@ cleanup: if (error == GIT_EINVALIDSPEC) giterr_set( GITERR_REFERENCE, - "The given reference name '%s' is not valid", name); + "the given reference name '%s' is not valid", name); if (error && normalize) git_buf_free(buf); @@ -1000,7 +1000,7 @@ int git_reference_normalize_name( if (git_buf_len(&buf) > buffer_size - 1) { giterr_set( GITERR_REFERENCE, - "The provided buffer is too short to hold the normalization of '%s'", name); + "the provided buffer is too short to hold the normalization of '%s'", name); error = GIT_EBUFS; goto cleanup; } @@ -1046,7 +1046,7 @@ static int get_terminal(git_reference **out, git_repository *repo, const char *r int error = 0; if (nesting > MAX_NESTING_LEVEL) { - giterr_set(GITERR_REFERENCE, "Reference chain too deep (%d)", nesting); + giterr_set(GITERR_REFERENCE, "reference chain too deep (%d)", nesting); return GIT_ENOTFOUND; } @@ -1229,7 +1229,7 @@ static int peel_error(int error, git_reference *ref, const char* msg) { giterr_set( GITERR_INVALID, - "The reference '%s' cannot be peeled - %s", git_reference_name(ref), msg); + "the reference '%s' cannot be peeled - %s", git_reference_name(ref), msg); return error; } diff --git a/src/remote.c b/src/remote.c index c1d7d59ea..8da7346fb 100644 --- a/src/remote.c +++ b/src/remote.c @@ -283,7 +283,7 @@ static int ensure_remote_doesnot_exist(git_repository *repo, const char *name) giterr_set( GITERR_CONFIG, - "Remote '%s' already exists.", name); + "remote '%s' already exists", name); return GIT_EEXISTS; } @@ -476,7 +476,7 @@ int git_remote_lookup(git_remote **out, git_repository *repo, const char *name) if (!optional_setting_found) { error = GIT_ENOTFOUND; - giterr_set(GITERR_CONFIG, "Remote '%s' does not exist.", name); + giterr_set(GITERR_CONFIG, "remote '%s' does not exist", name); goto cleanup; } @@ -1718,7 +1718,7 @@ int git_remote_set_autotag(git_repository *repo, const char *remote, git_remote_ error = 0; break; default: - giterr_set(GITERR_INVALID, "Invalid value for the tagopt setting"); + giterr_set(GITERR_INVALID, "invalid value for the tagopt setting"); error = -1; } diff --git a/src/repository.c b/src/repository.c index 5c4442360..60299193f 100644 --- a/src/repository.c +++ b/src/repository.c @@ -332,7 +332,7 @@ static int read_gitfile(git_buf *path_out, const char *file_path) memcmp(git_buf_cstr(&file), GIT_FILE_CONTENT_PREFIX, prefix_len) != 0) { giterr_set(GITERR_REPOSITORY, - "The `.git` file at '%s' is malformed", file_path); + "the `.git` file at '%s' is malformed", file_path); error = -1; } else if ((error = git_path_dirname_r(path_out, file_path)) >= 0) { @@ -459,7 +459,7 @@ static int find_repo( * to report, report that. */ if (!git_buf_len(repo_path) && !error) { giterr_set(GITERR_REPOSITORY, - "Could not find repository from '%s'", start_path); + "could not find repository from '%s'", start_path); error = GIT_ENOTFOUND; } @@ -481,7 +481,7 @@ int git_repository_open_bare( if (!valid_repository_path(&path)) { git_buf_free(&path); - giterr_set(GITERR_REPOSITORY, "Path is not a repository: %s", bare_path); + giterr_set(GITERR_REPOSITORY, "path is not a repository: %s", bare_path); return GIT_ENOTFOUND; } @@ -1180,7 +1180,7 @@ static int check_repositoryformatversion(git_config *config) if (GIT_REPO_VERSION < version) { giterr_set(GITERR_REPOSITORY, - "Unsupported repository version %d. Only versions up to %d are supported.", + "unsupported repository version %d. Only versions up to %d are supported.", version, GIT_REPO_VERSION); return -1; } @@ -1274,12 +1274,12 @@ static int create_empty_file(const char *path, mode_t mode) int fd; if ((fd = p_creat(path, mode)) < 0) { - giterr_set(GITERR_OS, "Error while creating '%s'", path); + giterr_set(GITERR_OS, "error while creating '%s'", path); return -1; } if (p_close(fd) < 0) { - giterr_set(GITERR_OS, "Error while closing '%s'", path); + giterr_set(GITERR_OS, "error while closing '%s'", path); return -1; } @@ -1508,7 +1508,7 @@ static int repo_write_template( if (error) giterr_set(GITERR_OS, - "Failed to initialize repository with template '%s'", file); + "failed to initialize repository with template '%s'", file); return error; } @@ -1539,7 +1539,7 @@ static int repo_write_gitlink( if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) { giterr_set(GITERR_REPOSITORY, - "Cannot overwrite gitlink file into path '%s'", in_dir); + "cannot overwrite gitlink file into path '%s'", in_dir); error = GIT_EEXISTS; goto cleanup; } @@ -1593,7 +1593,7 @@ static int repo_init_structure( if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) { if (git_win32__set_hidden(repo_dir, true) < 0) { giterr_set(GITERR_OS, - "Failed to mark Git repository folder as hidden"); + "failed to mark Git repository folder as hidden"); return -1; } } @@ -1747,7 +1747,7 @@ static int repo_init_directories( if (git_path_dirname_r(wd_path, repo_path->ptr) < 0) return -1; } else { - giterr_set(GITERR_REPOSITORY, "Cannot pick working directory" + giterr_set(GITERR_REPOSITORY, "cannot pick working directory" " for non-bare repository that isn't a '.git' directory"); return -1; } @@ -1867,7 +1867,7 @@ int git_repository_init_ext( if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) { giterr_set(GITERR_REPOSITORY, - "Attempt to reinitialize '%s'", given_repo); + "attempt to reinitialize '%s'", given_repo); error = GIT_EEXISTS; goto cleanup; } @@ -2149,7 +2149,7 @@ int git_repository_message(git_buf *out, git_repository *repo) if ((error = p_stat(git_buf_cstr(&path), &st)) < 0) { if (errno == ENOENT) error = GIT_ENOTFOUND; - giterr_set(GITERR_OS, "Could not access message file"); + giterr_set(GITERR_OS, "could not access message file"); } else { error = git_futils_readbuffer(out, git_buf_cstr(&path)); } @@ -2227,7 +2227,7 @@ int git_repository_hashfile( } if (!git__is_sizet(len)) { - giterr_set(GITERR_OS, "File size overflow for 32-bit systems"); + giterr_set(GITERR_OS, "file size overflow for 32-bit systems"); error = -1; goto cleanup; } diff --git a/src/repository.h b/src/repository.h index fd679b483..b259bea3b 100644 --- a/src/repository.h +++ b/src/repository.h @@ -182,7 +182,7 @@ GIT_INLINE(int) git_repository__ensure_not_bare( giterr_set( GITERR_REPOSITORY, - "Cannot %s. This operation is not allowed against bare repositories.", + "cannot %s. This operation is not allowed against bare repositories.", operation_name); return GIT_EBAREREPO; diff --git a/src/reset.c b/src/reset.c index db0bfb373..066b5dbda 100644 --- a/src/reset.c +++ b/src/reset.c @@ -137,7 +137,7 @@ static int reset( (git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE || git_index_has_conflicts(index))) { - giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge.", ERROR_MSG); + giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge", ERROR_MSG); error = GIT_EUNMERGED; goto cleanup; } diff --git a/src/revert.c b/src/revert.c index c481e7dea..b255245bf 100644 --- a/src/revert.c +++ b/src/revert.c @@ -133,13 +133,13 @@ int git_revert_commit( if (git_commit_parentcount(revert_commit) > 1) { if (!mainline) return revert_seterr(revert_commit, - "Mainline branch is not specified but %s is a merge commit"); + "mainline branch is not specified but %s is a merge commit"); parent = mainline; } else { if (mainline) return revert_seterr(revert_commit, - "Mainline branch specified but %s is not a merge commit"); + "mainline branch specified but %s is not a merge commit"); parent = git_commit_parentcount(revert_commit); } diff --git a/src/revparse.c b/src/revparse.c index aa7e0bd98..d5511b47b 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -46,7 +46,7 @@ static int build_regex(regex_t *regex, const char *pattern) int error; if (*pattern == '\0') { - giterr_set(GITERR_REGEX, "Empty pattern"); + giterr_set(GITERR_REGEX, "empty pattern"); return GIT_EINVALIDSPEC; } @@ -118,7 +118,7 @@ static int revparse_lookup_object( if ((error = maybe_describe(object_out, repo, spec)) != GIT_ENOTFOUND) return error; - giterr_set(GITERR_REFERENCE, "Revspec '%s' not found.", spec); + giterr_set(GITERR_REFERENCE, "revspec '%s' not found", spec); return GIT_ENOTFOUND; } @@ -245,7 +245,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t ide notfound: giterr_set( GITERR_REFERENCE, - "Reflog for '%s' has only %"PRIuZ" entries, asked for %"PRIuZ, + "reflog for '%s' has only %"PRIuZ" entries, asked for %"PRIuZ, git_reference_name(ref), numentries, identifier); git_reflog_free(reflog); @@ -757,7 +757,7 @@ int revparse__ext( * TODO: support merge-stage path lookup (":2:Makefile") * and plain index blob lookup (:i-am/a/blob) */ - giterr_set(GITERR_INVALID, "Unimplemented"); + giterr_set(GITERR_INVALID, "unimplemented"); error = GIT_ERROR; goto cleanup; } @@ -816,7 +816,7 @@ cleanup: if (error) { if (error == GIT_EINVALIDSPEC) giterr_set(GITERR_INVALID, - "Failed to parse revision specifier - Invalid pattern '%s'", spec); + "failed to parse revision specifier - Invalid pattern '%s'", spec); git_object_free(base_rev); git_reference_free(reference); diff --git a/src/revwalk.c b/src/revwalk.c index f5502a72e..8c370bcc8 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -61,7 +61,7 @@ static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting, if (from_glob) return 0; - giterr_set(GITERR_INVALID, "Object is not a committish"); + giterr_set(GITERR_INVALID, "object is not a committish"); return -1; } if (error < 0) @@ -198,7 +198,7 @@ int git_revwalk_push_range(git_revwalk *walk, const char *range) if (revspec.flags & GIT_REVPARSE_MERGE_BASE) { /* TODO: support "..." */ - giterr_set(GITERR_INVALID, "Symmetric differences not implemented in revwalk"); + giterr_set(GITERR_INVALID, "symmetric differences not implemented in revwalk"); return GIT_EINVALIDSPEC; } @@ -733,7 +733,7 @@ int git_revwalk_add_hide_cb( if (walk->hide_cb) { /* There is already a callback added */ - giterr_set(GITERR_INVALID, "There is already a callback added to hide commits in revision walker."); + giterr_set(GITERR_INVALID, "there is already a callback added to hide commits in revwalk"); return -1; } diff --git a/src/settings.c b/src/settings.c index 4a6e0f353..980233d88 100644 --- a/src/settings.c +++ b/src/settings.c @@ -64,7 +64,7 @@ static int config_level_to_sysdir(int config_level) break; default: giterr_set( - GITERR_INVALID, "Invalid config path selector %d", config_level); + GITERR_INVALID, "invalid config path selector %d", config_level); } return val; diff --git a/src/sha1_lookup.c b/src/sha1_lookup.c index c6b561340..ead26de06 100644 --- a/src/sha1_lookup.c +++ b/src/sha1_lookup.c @@ -206,7 +206,7 @@ int sha1_entry_pos(const void *table, #endif if (!(lo <= mi && mi < hi)) { - giterr_set(GITERR_INVALID, "Assertion failure. Binary search invariant is false"); + giterr_set(GITERR_INVALID, "assertion failure: binary search invariant is false"); return -1; } diff --git a/src/signature.c b/src/signature.c index 22cba7ef3..e792a52f8 100644 --- a/src/signature.c +++ b/src/signature.c @@ -25,7 +25,7 @@ void git_signature_free(git_signature *sig) static int signature_error(const char *msg) { - giterr_set(GITERR_INVALID, "Failed to parse signature - %s", msg); + giterr_set(GITERR_INVALID, "failed to parse signature - %s", msg); return -1; } diff --git a/src/socket_stream.c b/src/socket_stream.c index 71f49118e..fca411717 100644 --- a/src/socket_stream.c +++ b/src/socket_stream.c @@ -57,7 +57,7 @@ static int close_socket(GIT_SOCKET s) return -1; if (0 != WSACleanup()) { - giterr_set(GITERR_OS, "Winsock cleanup failed"); + giterr_set(GITERR_OS, "winsock cleanup failed"); return -1; } @@ -82,13 +82,13 @@ int socket_connect(git_stream *stream) WSADATA wsd; if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { - giterr_set(GITERR_OS, "Winsock init failed"); + giterr_set(GITERR_OS, "winsock init failed"); return -1; } if (LOBYTE(wsd.wVersion) != 2 || HIBYTE(wsd.wVersion) != 2) { WSACleanup(); - giterr_set(GITERR_OS, "Winsock init failed"); + giterr_set(GITERR_OS, "winsock init failed"); return -1; } #endif @@ -99,7 +99,7 @@ int socket_connect(git_stream *stream) if ((ret = p_getaddrinfo(st->host, st->port, &hints, &info)) != 0) { giterr_set(GITERR_NET, - "Failed to resolve address for %s: %s", st->host, p_gai_strerror(ret)); + "failed to resolve address for %s: %s", st->host, p_gai_strerror(ret)); return -1; } @@ -121,7 +121,7 @@ int socket_connect(git_stream *stream) /* Oops, we couldn't connect to any address */ if (s == INVALID_SOCKET && p == NULL) { - giterr_set(GITERR_OS, "Failed to connect to %s", st->host); + giterr_set(GITERR_OS, "failed to connect to %s", st->host); p_freeaddrinfo(info); return -1; } diff --git a/src/sortedcache.c b/src/sortedcache.c index 5bd989a9f..c5e338f75 100644 --- a/src/sortedcache.c +++ b/src/sortedcache.c @@ -27,7 +27,7 @@ int git_sortedcache_new( goto fail; if (git_rwlock_init(&sc->lock)) { - giterr_set(GITERR_OS, "Failed to initialize lock"); + giterr_set(GITERR_OS, "failed to initialize lock"); goto fail; } @@ -162,7 +162,7 @@ int git_sortedcache_wlock(git_sortedcache *sc) GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */ if (git_rwlock_wrlock(&sc->lock) < 0) { - giterr_set(GITERR_OS, "Unable to acquire write lock on cache"); + giterr_set(GITERR_OS, "unable to acquire write lock on cache"); return -1; } return 0; @@ -181,7 +181,7 @@ int git_sortedcache_rlock(git_sortedcache *sc) GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */ if (git_rwlock_rdlock(&sc->lock) < 0) { - giterr_set(GITERR_OS, "Unable to acquire read lock on cache"); + giterr_set(GITERR_OS, "unable to acquire read lock on cache"); return -1; } return 0; @@ -221,7 +221,7 @@ int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf) } if (!git__is_sizet(st.st_size)) { - giterr_set(GITERR_INVALID, "Unable to load file larger than size_t"); + giterr_set(GITERR_INVALID, "unable to load file larger than size_t"); error = -1; (void)p_close(fd); goto unlock; @@ -373,7 +373,7 @@ int git_sortedcache_remove(git_sortedcache *sc, size_t pos) */ if ((item = git_vector_get(&sc->items, pos)) == NULL) { - giterr_set(GITERR_INVALID, "Removing item out of range"); + giterr_set(GITERR_INVALID, "removing item out of range"); return GIT_ENOTFOUND; } diff --git a/src/stash.c b/src/stash.c index f5f4f36bf..d13220cdd 100644 --- a/src/stash.c +++ b/src/stash.c @@ -27,7 +27,7 @@ static int create_error(int error, const char *msg) { - giterr_set(GITERR_STASH, "Cannot stash changes - %s", msg); + giterr_set(GITERR_STASH, "cannot stash changes - %s", msg); return error; } @@ -36,7 +36,7 @@ static int retrieve_head(git_reference **out, git_repository *repo) int error = git_repository_head(out, repo); if (error == GIT_EUNBORNBRANCH) - return create_error(error, "You do not have the initial commit yet."); + return create_error(error, "you do not have the initial commit yet."); return error; } @@ -198,7 +198,7 @@ static int stash_update_index_from_diff( /* Unimplemented */ giterr_set( GITERR_INVALID, - "Cannot update index. Unimplemented status (%d)", + "cannot update index. Unimplemented status (%d)", delta->status); return -1; } @@ -479,7 +479,7 @@ static int ensure_there_are_changes_to_stash( return 0; if (!error) - return create_error(GIT_ENOTFOUND, "There is nothing to stash."); + return create_error(GIT_ENOTFOUND, "there is nothing to stash."); return error; } @@ -593,7 +593,7 @@ static int retrieve_stash_commit( max = git_reflog_entrycount(reflog); if (!max || index > max - 1) { error = GIT_ENOTFOUND; - giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index); + giterr_set(GITERR_STASH, "no stashed state at position %" PRIuZ, index); goto cleanup; } @@ -1036,7 +1036,7 @@ int git_stash_drop( if (!max || index > max - 1) { error = GIT_ENOTFOUND; - giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index); + giterr_set(GITERR_STASH, "no stashed state at position %" PRIuZ, index); goto cleanup; } diff --git a/src/status.c b/src/status.c index e610f5fe1..6752b5625 100644 --- a/src/status.c +++ b/src/status.c @@ -243,13 +243,13 @@ static int status_validate_options(const git_status_options *opts) GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options"); if (opts->show > GIT_STATUS_SHOW_WORKDIR_ONLY) { - giterr_set(GITERR_INVALID, "Unknown status 'show' option"); + giterr_set(GITERR_INVALID, "unknown status 'show' option"); return -1; } if ((opts->flags & GIT_STATUS_OPT_NO_REFRESH) != 0 && (opts->flags & GIT_STATUS_OPT_UPDATE_INDEX) != 0) { - giterr_set(GITERR_INVALID, "Updating index from status " + giterr_set(GITERR_INVALID, "updating index from status " "is not allowed when index refresh is disabled"); return -1; } @@ -510,13 +510,13 @@ int git_status_file( if (error < 0 && sfi.ambiguous) { giterr_set(GITERR_INVALID, - "Ambiguous path '%s' given to git_status_file", sfi.expected); + "ambiguous path '%s' given to git_status_file", sfi.expected); error = GIT_EAMBIGUOUS; } if (!error && !sfi.count) { giterr_set(GITERR_INVALID, - "Attempt to get status of nonexistent file '%s'", path); + "attempt to get status of nonexistent file '%s'", path); error = GIT_ENOTFOUND; } diff --git a/src/submodule.c b/src/submodule.c index 86ad53be0..6d6b314d6 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -124,8 +124,8 @@ static void submodule_set_lookup_error(int error, const char *name) return; giterr_set(GITERR_SUBMODULE, (error == GIT_ENOTFOUND) ? - "No submodule named '%s'" : - "Submodule '%s' has not been added yet", name); + "no submodule named '%s'" : + "submodule '%s' has not been added yet", name); } typedef struct { @@ -618,7 +618,7 @@ int git_submodule_add_setup( giterr_clear(); else { giterr_set(GITERR_SUBMODULE, - "Attempt to add submodule '%s' that already exists", path); + "attempt to add submodule '%s' that already exists", path); return GIT_EEXISTS; } @@ -628,7 +628,7 @@ int git_submodule_add_setup( path += strlen(git_repository_workdir(repo)); if (git_path_root(path) >= 0) { - giterr_set(GITERR_SUBMODULE, "Submodule path must be a relative path"); + giterr_set(GITERR_SUBMODULE, "submodule path must be a relative path"); error = -1; goto cleanup; } @@ -637,7 +637,7 @@ int git_submodule_add_setup( if (!(mods = open_gitmodules(repo, GITMODULES_CREATE))) { giterr_set(GITERR_SUBMODULE, - "Adding submodules to a bare repository is not supported"); + "adding submodules to a bare repository is not supported"); return -1; } @@ -758,7 +758,7 @@ int git_submodule_add_to_index(git_submodule *sm, int write_index) /* read stat information for submodule working directory */ if (p_stat(path.ptr, &st) < 0) { giterr_set(GITERR_SUBMODULE, - "Cannot add submodule without working directory"); + "cannot add submodule without working directory"); error = -1; goto cleanup; } @@ -771,7 +771,7 @@ int git_submodule_add_to_index(git_submodule *sm, int write_index) /* calling git_submodule_open will have set sm->wd_oid if possible */ if ((sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) == 0) { giterr_set(GITERR_SUBMODULE, - "Cannot add submodule without HEAD to index"); + "cannot add submodule without HEAD to index"); error = -1; goto cleanup; } @@ -861,7 +861,7 @@ int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *ur } else if (strchr(url, ':') != NULL || url[0] == '/') { error = git_buf_sets(out, url); } else { - giterr_set(GITERR_SUBMODULE, "Invalid format for submodule URL"); + giterr_set(GITERR_SUBMODULE, "invalid format for submodule URL"); error = -1; } @@ -1133,7 +1133,7 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio goto done; if (!init) { - giterr_set(GITERR_SUBMODULE, "Submodule is not initialized."); + giterr_set(GITERR_SUBMODULE, "submodule is not initialized"); error = GIT_ERROR; goto done; } @@ -1215,7 +1215,7 @@ int git_submodule_init(git_submodule *sm, int overwrite) if (!sm->url) { giterr_set(GITERR_SUBMODULE, - "No URL configured for submodule '%s'", sm->name); + "no URL configured for submodule '%s'", sm->name); return -1; } @@ -1259,7 +1259,7 @@ int git_submodule_sync(git_submodule *sm) if (!sm->url) { giterr_set(GITERR_SUBMODULE, - "No URL configured for submodule '%s'", sm->name); + "no URL configured for submodule '%s'", sm->name); return -1; } @@ -1578,7 +1578,7 @@ static int submodule_alloc( git_submodule *sm; if (!name || !(namelen = strlen(name))) { - giterr_set(GITERR_SUBMODULE, "Invalid submodule name"); + giterr_set(GITERR_SUBMODULE, "invalid submodule name"); return -1; } @@ -1630,7 +1630,7 @@ void git_submodule_free(git_submodule *sm) static int submodule_config_error(const char *property, const char *value) { giterr_set(GITERR_INVALID, - "Invalid value for submodule '%s' property: '%s'", property, value); + "invalid value for submodule '%s' property: '%s'", property, value); return -1; } @@ -1968,7 +1968,7 @@ static int lookup_default_remote(git_remote **remote, git_repository *repo) if (error == GIT_ENOTFOUND) giterr_set( GITERR_SUBMODULE, - "Cannot get default remote for submodule - no local tracking " + "cannot get default remote for submodule - no local tracking " "branch for HEAD and origin does not exist"); return error; diff --git a/src/sysdir.c b/src/sysdir.c index e89db7697..ed11221a3 100644 --- a/src/sysdir.c +++ b/src/sysdir.c @@ -150,7 +150,7 @@ int git_sysdir_get_str( GITERR_CHECK_ERROR(git_sysdir_get(&path, which)); if (!out || path->size >= outlen) { - giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); + giterr_set(GITERR_NOMEMORY, "buffer is too short for the path"); return GIT_EBUFS; } @@ -241,7 +241,7 @@ static int git_sysdir_find_in_dirlist( done: git_buf_free(path); - giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name); + giterr_set(GITERR_OS, "the %s file '%s' doesn't exist", label, name); return GIT_ENOTFOUND; } diff --git a/src/tag.c b/src/tag.c index fe840fe82..2bf23fc3c 100644 --- a/src/tag.c +++ b/src/tag.c @@ -61,7 +61,7 @@ const char *git_tag_message(const git_tag *t) static int tag_error(const char *str) { - giterr_set(GITERR_TAG, "Failed to parse tag. %s", str); + giterr_set(GITERR_TAG, "failed to parse tag: %s", str); return -1; } @@ -76,13 +76,13 @@ static int tag_parse(git_tag *tag, const char *buffer, const char *buffer_end) char *search; if (git_oid__parse(&tag->target, &buffer, buffer_end, "object ") < 0) - return tag_error("Object field invalid"); + return tag_error("object field invalid"); if (buffer + 5 >= buffer_end) - return tag_error("Object too short"); + return tag_error("object too short"); if (memcmp(buffer, "type ", 5) != 0) - return tag_error("Type field not found"); + return tag_error("type field not found"); buffer += 5; tag->type = GIT_OBJ_BAD; @@ -91,7 +91,7 @@ static int tag_parse(git_tag *tag, const char *buffer, const char *buffer_end) size_t type_length = strlen(tag_types[i]); if (buffer + type_length >= buffer_end) - return tag_error("Object too short"); + return tag_error("object too short"); if (memcmp(buffer, tag_types[i], type_length) == 0) { tag->type = i; @@ -101,19 +101,19 @@ static int tag_parse(git_tag *tag, const char *buffer, const char *buffer_end) } if (tag->type == GIT_OBJ_BAD) - return tag_error("Invalid object type"); + return tag_error("invalid object type"); if (buffer + 4 >= buffer_end) - return tag_error("Object too short"); + return tag_error("object too short"); if (memcmp(buffer, "tag ", 4) != 0) - return tag_error("Tag field not found"); + return tag_error("tag field not found"); buffer += 4; search = memchr(buffer, '\n', buffer_end - buffer); if (search == NULL) - return tag_error("Object too short"); + return tag_error("object too short"); text_len = search - buffer; @@ -234,7 +234,7 @@ static int write_tag_annotation( on_error: git_buf_free(&tag); - giterr_set(GITERR_OBJECT, "Failed to create tag annotation."); + giterr_set(GITERR_OBJECT, "failed to create tag annotation"); return -1; } @@ -257,7 +257,7 @@ static int git_tag_create__internal( assert(!create_tag_annotation || (tagger && message)); if (git_object_owner(target) != repo) { - giterr_set(GITERR_INVALID, "The given target does not belong to this repository"); + giterr_set(GITERR_INVALID, "the given target does not belong to this repository"); return -1; } @@ -269,7 +269,7 @@ static int git_tag_create__internal( * reference unless overwriting has explicitly been requested **/ if (error == 0 && !allow_ref_overwrite) { git_buf_free(&ref_name); - giterr_set(GITERR_TAG, "Tag already exists"); + giterr_set(GITERR_TAG, "tag already exists"); return GIT_EEXISTS; } @@ -349,7 +349,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu goto on_error; if (tag.type != target_obj->cached.type) { - giterr_set(GITERR_TAG, "The type for the given target is invalid"); + giterr_set(GITERR_TAG, "the type for the given target is invalid"); goto on_error; } @@ -366,7 +366,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu /** Ensure the tag name doesn't conflict with an already existing * reference unless overwriting has explicitly been requested **/ if (error == 0 && !allow_ref_overwrite) { - giterr_set(GITERR_TAG, "Tag already exists"); + giterr_set(GITERR_TAG, "tag already exists"); return GIT_EEXISTS; } diff --git a/src/trace.c b/src/trace.c index ee5039f56..0f2142861 100644 --- a/src/trace.c +++ b/src/trace.c @@ -32,7 +32,7 @@ int git_trace_set(git_trace_level_t level, git_trace_callback callback) GIT_UNUSED(callback); giterr_set(GITERR_INVALID, - "This version of libgit2 was not built with tracing."); + "this version of libgit2 was not built with tracing."); return -1; #endif } diff --git a/src/transport.c b/src/transport.c index f08d2dc68..b66165332 100644 --- a/src/transport.c +++ b/src/transport.c @@ -121,7 +121,7 @@ int git_transport_new(git_transport **out, git_remote *owner, const char *url) int error; if ((error = transport_find_fn(&fn, url, ¶m)) == GIT_ENOTFOUND) { - giterr_set(GITERR_NET, "Unsupported URL protocol"); + giterr_set(GITERR_NET, "unsupported URL protocol"); return -1; } else if (error < 0) return error; diff --git a/src/transports/auth_negotiate.c b/src/transports/auth_negotiate.c index 8b99fc735..7c868c9fd 100644 --- a/src/transports/auth_negotiate.c +++ b/src/transports/auth_negotiate.c @@ -107,13 +107,13 @@ static int negotiate_next_token( challenge_len = ctx->challenge ? strlen(ctx->challenge) : 0; if (challenge_len < 9) { - giterr_set(GITERR_NET, "No negotiate challenge sent from server"); + giterr_set(GITERR_NET, "no negotiate challenge sent from server"); error = -1; goto done; } else if (challenge_len > 9) { if (git_buf_decode_base64(&input_buf, ctx->challenge + 10, challenge_len - 10) < 0) { - giterr_set(GITERR_NET, "Invalid negotiate challenge from server"); + giterr_set(GITERR_NET, "invalid negotiate challenge from server"); error = -1; goto done; } @@ -122,7 +122,7 @@ static int negotiate_next_token( input_token.length = input_buf.size; input_token_ptr = &input_token; } else if (ctx->gss_context != GSS_C_NO_CONTEXT) { - giterr_set(GITERR_NET, "Could not restart authentication"); + giterr_set(GITERR_NET, "could not restart authentication"); error = -1; goto done; } @@ -228,7 +228,7 @@ static int negotiate_init_context( gss_release_oid_set(&status_minor, &mechanism_list); if (!ctx->oid) { - giterr_set(GITERR_NET, "Negotiate authentication is not supported"); + giterr_set(GITERR_NET, "negotiate authentication is not supported"); return -1; } diff --git a/src/transports/cred.c b/src/transports/cred.c index 49ede48bf..8e3f64435 100644 --- a/src/transports/cred.c +++ b/src/transports/cred.c @@ -216,7 +216,7 @@ int git_cred_ssh_key_memory_new( GIT_UNUSED(passphrase); giterr_set(GITERR_INVALID, - "This version of libgit2 was not built with ssh memory credentials."); + "this version of libgit2 was not built with ssh memory credentials."); return -1; #endif } diff --git a/src/transports/git.c b/src/transports/git.c index 6c6acf9c5..01edfdc49 100644 --- a/src/transports/git.c +++ b/src/transports/git.c @@ -45,7 +45,7 @@ static int gen_proto(git_buf *request, const char *cmd, const char *url) delim = strchr(url, '/'); if (delim == NULL) { - giterr_set(GITERR_NET, "Malformed URL"); + giterr_set(GITERR_NET, "malformed URL"); return -1; } @@ -240,7 +240,7 @@ static int _git_uploadpack( return 0; } - giterr_set(GITERR_NET, "Must call UPLOADPACK_LS before UPLOADPACK"); + giterr_set(GITERR_NET, "must call UPLOADPACK_LS before UPLOADPACK"); return -1; } @@ -296,7 +296,7 @@ static int _git_receivepack( return 0; } - giterr_set(GITERR_NET, "Must call RECEIVEPACK_LS before RECEIVEPACK"); + giterr_set(GITERR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK"); return -1; } diff --git a/src/transports/http.c b/src/transports/http.c index ad28c5889..9edbee551 100644 --- a/src/transports/http.c +++ b/src/transports/http.c @@ -403,7 +403,7 @@ static int on_headers_complete(http_parser *parser) t->location) { if (s->redirect_count >= 7) { - giterr_set(GITERR_NET, "Too many redirects"); + giterr_set(GITERR_NET, "too many redirects"); return t->parse_error = PARSE_ERROR_GENERIC; } @@ -428,14 +428,14 @@ static int on_headers_complete(http_parser *parser) /* Check for a 200 HTTP status code. */ if (parser->status_code != 200) { giterr_set(GITERR_NET, - "Unexpected HTTP status code: %d", + "unexpected HTTP status code: %d", parser->status_code); return t->parse_error = PARSE_ERROR_GENERIC; } /* The response must contain a Content-Type header. */ if (!t->content_type) { - giterr_set(GITERR_NET, "No Content-Type header in response"); + giterr_set(GITERR_NET, "no Content-Type header in response"); return t->parse_error = PARSE_ERROR_GENERIC; } @@ -455,7 +455,7 @@ static int on_headers_complete(http_parser *parser) if (strcmp(t->content_type, git_buf_cstr(&buf))) { git_buf_free(&buf); giterr_set(GITERR_NET, - "Invalid Content-Type: %s", + "invalid Content-Type: %s", t->content_type); return t->parse_error = PARSE_ERROR_GENERIC; } @@ -488,7 +488,7 @@ static int on_body_fill_buffer(http_parser *parser, const char *str, size_t len) return 0; if (ctx->buf_size < len) { - giterr_set(GITERR_NET, "Can't fit data in the buffer"); + giterr_set(GITERR_NET, "can't fit data in the buffer"); return t->parse_error = PARSE_ERROR_GENERIC; } @@ -857,7 +857,7 @@ static int http_stream_write_single( assert(t->connected); if (s->sent_request) { - giterr_set(GITERR_NET, "Subtransport configured for only one write"); + giterr_set(GITERR_NET, "subtransport configured for only one write"); return -1; } diff --git a/src/transports/local.c b/src/transports/local.c index 4eae9dead..87745add5 100644 --- a/src/transports/local.c +++ b/src/transports/local.c @@ -252,7 +252,7 @@ static int local_ls(const git_remote_head ***out, size_t *size, git_transport *t transport_local *t = (transport_local *)transport; if (!t->have_refs) { - giterr_set(GITERR_NET, "The transport has not yet loaded the refs"); + giterr_set(GITERR_NET, "the transport has not yet loaded the refs"); return -1; } @@ -371,7 +371,7 @@ static int local_push( but we forbid all pushes just in case */ if (!remote_repo->is_bare) { error = GIT_EBAREREPO; - giterr_set(GITERR_INVALID, "Local push doesn't (yet) support pushing to non-bare repos."); + giterr_set(GITERR_INVALID, "local push doesn't (yet) support pushing to non-bare repos."); goto on_error; } diff --git a/src/transports/smart.c b/src/transports/smart.c index 7a35c39d8..e4aa26d86 100644 --- a/src/transports/smart.c +++ b/src/transports/smart.c @@ -231,7 +231,7 @@ static int git_smart__connect( else if (GIT_DIRECTION_PUSH == t->direction) service = GIT_SERVICE_RECEIVEPACK_LS; else { - giterr_set(GITERR_NET, "Invalid direction"); + giterr_set(GITERR_NET, "invalid direction"); return -1; } @@ -252,7 +252,7 @@ static int git_smart__connect( pkt = (git_pkt *)git_vector_get(&t->refs, 0); if (!pkt || GIT_PKT_COMMENT != pkt->type) { - giterr_set(GITERR_NET, "Invalid response"); + giterr_set(GITERR_NET, "invalid response"); return -1; } else { /* Remove the comment pkt from the list */ @@ -299,7 +299,7 @@ static int git_smart__ls(const git_remote_head ***out, size_t *size, git_transpo transport_smart *t = (transport_smart *)transport; if (!t->have_refs) { - giterr_set(GITERR_NET, "The transport has not yet loaded the refs"); + giterr_set(GITERR_NET, "the transport has not yet loaded the refs"); return -1; } @@ -319,7 +319,7 @@ int git_smart__negotiation_step(git_transport *transport, void *data, size_t len return -1; if (GIT_DIRECTION_FETCH != t->direction) { - giterr_set(GITERR_NET, "This operation is only valid for fetch"); + giterr_set(GITERR_NET, "this operation is only valid for fetch"); return -1; } @@ -348,7 +348,7 @@ int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream return -1; if (GIT_DIRECTION_PUSH != t->direction) { - giterr_set(GITERR_NET, "This operation is only valid for push"); + giterr_set(GITERR_NET, "this operation is only valid for push"); return -1; } diff --git a/src/transports/smart_pkt.c b/src/transports/smart_pkt.c index 2297cc94f..c0672e25a 100644 --- a/src/transports/smart_pkt.c +++ b/src/transports/smart_pkt.c @@ -226,7 +226,7 @@ static int ref_pkt(git_pkt **out, const char *line, size_t len) /* Check for a bit of consistency */ if (line[GIT_OID_HEXSZ] != ' ') { - giterr_set(GITERR_NET, "Error parsing pkt-line"); + giterr_set(GITERR_NET, "error parsing pkt-line"); error = -1; goto error_out; } @@ -270,7 +270,7 @@ static int ok_pkt(git_pkt **out, const char *line, size_t len) line += 3; /* skip "ok " */ if (!(ptr = strchr(line, '\n'))) { - giterr_set(GITERR_NET, "Invalid packet line"); + giterr_set(GITERR_NET, "invalid packet line"); git__free(pkt); return -1; } @@ -327,7 +327,7 @@ static int ng_pkt(git_pkt **out, const char *line, size_t len) return 0; out_err: - giterr_set(GITERR_NET, "Invalid packet line"); + giterr_set(GITERR_NET, "invalid packet line"); git__free(pkt->ref); git__free(pkt); return -1; @@ -535,7 +535,7 @@ static int buffer_want_with_caps(const git_remote_head *head, transport_smart_ca if (len > 0xffff) { giterr_set(GITERR_NET, - "Tried to produce packet with invalid length %" PRIuZ, len); + "tried to produce packet with invalid length %" PRIuZ, len); return -1; } diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c index 53c0b089e..b29818d3f 100644 --- a/src/transports/smart_protocol.c +++ b/src/transports/smart_protocol.c @@ -60,7 +60,7 @@ int git_smart__store_refs(transport_smart *t, int flushes) gitno_consume(buf, line_end); if (pkt->type == GIT_PKT_ERR) { - giterr_set(GITERR_NET, "Remote error: %s", ((git_pkt_err *)pkt)->error); + giterr_set(GITERR_NET, "remote error: %s", ((git_pkt_err *)pkt)->error); git__free(pkt); return -1; } diff --git a/src/transports/ssh.c b/src/transports/ssh.c index cfd573665..44d02e522 100644 --- a/src/transports/ssh.c +++ b/src/transports/ssh.c @@ -83,7 +83,7 @@ static int gen_proto(git_buf *request, const char *cmd, const char *url) done: if (!repo) { - giterr_set(GITERR_NET, "Malformed git protocol URL"); + giterr_set(GITERR_NET, "malformed git protocol URL"); return -1; } @@ -274,7 +274,7 @@ static int git_ssh_extract_url_parts( } if (colon == NULL || (colon < start)) { - giterr_set(GITERR_NET, "Malformed URL"); + giterr_set(GITERR_NET, "malformed URL"); return -1; } @@ -445,7 +445,7 @@ static int request_creds(git_cred **out, ssh_subtransport *t, const char *user, else if (error < 0) return error; else if (!cred) { - giterr_set(GITERR_SSH, "Callback failed to initialize SSH credentials"); + giterr_set(GITERR_SSH, "callback failed to initialize SSH credentials"); return -1; } } @@ -478,7 +478,7 @@ static int _git_ssh_session_create( s = libssh2_session_init(); if (!s) { - giterr_set(GITERR_NET, "Failed to initialize SSH session"); + giterr_set(GITERR_NET, "failed to initialize SSH session"); return -1; } @@ -487,7 +487,7 @@ static int _git_ssh_session_create( } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc); if (rc != LIBSSH2_ERROR_NONE) { - ssh_error(s, "Failed to start SSH session"); + ssh_error(s, "failed to start SSH session"); libssh2_session_free(s); return -1; } @@ -685,7 +685,7 @@ static int ssh_uploadpack( return 0; } - giterr_set(GITERR_NET, "Must call UPLOADPACK_LS before UPLOADPACK"); + giterr_set(GITERR_NET, "must call UPLOADPACK_LS before UPLOADPACK"); return -1; } @@ -712,7 +712,7 @@ static int ssh_receivepack( return 0; } - giterr_set(GITERR_NET, "Must call RECEIVEPACK_LS before RECEIVEPACK"); + giterr_set(GITERR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK"); return -1; } @@ -844,7 +844,7 @@ int git_smart_subtransport_ssh( assert(out); *out = NULL; - giterr_set(GITERR_INVALID, "Cannot create SSH transport. Library was built without SSH support"); + giterr_set(GITERR_INVALID, "cannot create SSH transport. Library was built without SSH support"); return -1; #endif } @@ -888,7 +888,7 @@ int git_transport_ssh_with_paths(git_transport **out, git_remote *owner, void *p assert(out); *out = NULL; - giterr_set(GITERR_INVALID, "Cannot create SSH transport. Library was built without SSH support"); + giterr_set(GITERR_INVALID, "cannot create SSH transport. Library was built without SSH support"); return -1; #endif } diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c index 78e42cf3b..051340d87 100644 --- a/src/transports/winhttp.c +++ b/src/transports/winhttp.c @@ -139,12 +139,12 @@ static int apply_basic_credential(HINTERNET request, git_cred *cred) goto on_error; if ((wide_len = git__utf8_to_16_alloc(&wide, git_buf_cstr(&buf))) < 0) { - giterr_set(GITERR_OS, "Failed to convert string to wide form"); + giterr_set(GITERR_OS, "failed to convert string to wide form"); goto on_error; } if (!WinHttpAddRequestHeaders(request, wide, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); + giterr_set(GITERR_OS, "failed to add a header to the request"); goto on_error; } @@ -202,7 +202,7 @@ static int fallback_cred_acquire_cb( /* Convert URL to wide characters */ if (git__utf8_to_16_alloc(&wide_url, url) < 0) { - giterr_set(GITERR_OS, "Failed to convert string to wide form"); + giterr_set(GITERR_OS, "failed to convert string to wide form"); return -1; } @@ -351,7 +351,7 @@ static int winhttp_stream_connect(winhttp_stream *s) /* Convert URL to wide characters */ if (git__utf8_to_16_alloc(&s->request_uri, git_buf_cstr(&buf)) < 0) { - giterr_set(GITERR_OS, "Failed to convert string to wide form"); + giterr_set(GITERR_OS, "failed to convert string to wide form"); goto on_error; } @@ -366,12 +366,12 @@ static int winhttp_stream_connect(winhttp_stream *s) t->connection_data.use_ssl ? WINHTTP_FLAG_SECURE : 0); if (!s->request) { - giterr_set(GITERR_OS, "Failed to open request"); + giterr_set(GITERR_OS, "failed to open request"); goto on_error; } if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) { - giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP"); + giterr_set(GITERR_OS, "failed to set timeouts for WinHTTP"); goto on_error; } @@ -444,7 +444,7 @@ static int winhttp_stream_connect(winhttp_stream *s) WINHTTP_OPTION_PROXY, &proxy_info, sizeof(WINHTTP_PROXY_INFO))) { - giterr_set(GITERR_OS, "Failed to set proxy"); + giterr_set(GITERR_OS, "failed to set proxy"); git__free(proxy_wide); goto on_error; } @@ -467,7 +467,7 @@ static int winhttp_stream_connect(winhttp_stream *s) WINHTTP_OPTION_DISABLE_FEATURE, &disable_redirects, sizeof(disable_redirects))) { - giterr_set(GITERR_OS, "Failed to disable redirects"); + giterr_set(GITERR_OS, "failed to disable redirects"); goto on_error; } @@ -481,7 +481,7 @@ static int winhttp_stream_connect(winhttp_stream *s) /* Send Pragma: no-cache header */ if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); + giterr_set(GITERR_OS, "failed to add a header to the request"); goto on_error; } @@ -494,13 +494,13 @@ static int winhttp_stream_connect(winhttp_stream *s) goto on_error; if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) { - giterr_set(GITERR_OS, "Failed to convert content-type to wide characters"); + giterr_set(GITERR_OS, "failed to convert content-type to wide characters"); goto on_error; } if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); + giterr_set(GITERR_OS, "failed to add a header to the request"); goto on_error; } @@ -511,13 +511,13 @@ static int winhttp_stream_connect(winhttp_stream *s) goto on_error; if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) { - giterr_set(GITERR_OS, "Failed to convert accept header to wide characters"); + giterr_set(GITERR_OS, "failed to convert accept header to wide characters"); goto on_error; } if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); + giterr_set(GITERR_OS, "failed to add a header to the request"); goto on_error; } } @@ -527,13 +527,13 @@ static int winhttp_stream_connect(winhttp_stream *s) git_buf_clear(&buf); git_buf_puts(&buf, t->owner->custom_headers.strings[i]); if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) { - giterr_set(GITERR_OS, "Failed to convert custom header to wide characters"); + giterr_set(GITERR_OS, "failed to convert custom header to wide characters"); goto on_error; } if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); + giterr_set(GITERR_OS, "failed to add a header to the request"); goto on_error; } } @@ -596,7 +596,7 @@ static int parse_unauthorized_response( * We can assume this was already done, since we know we are unauthorized. */ if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) { - giterr_set(GITERR_OS, "Failed to parse supported auth schemes"); + giterr_set(GITERR_OS, "failed to parse supported auth schemes"); return -1; } @@ -629,7 +629,7 @@ static int write_chunk(HINTERNET request, const char *buffer, size_t len) git_buf_cstr(&buf), (DWORD)git_buf_len(&buf), &bytes_written)) { git_buf_free(&buf); - giterr_set(GITERR_OS, "Failed to write chunk header"); + giterr_set(GITERR_OS, "failed to write chunk header"); return -1; } @@ -639,7 +639,7 @@ static int write_chunk(HINTERNET request, const char *buffer, size_t len) if (!WinHttpWriteData(request, buffer, (DWORD)len, &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write chunk"); + giterr_set(GITERR_OS, "failed to write chunk"); return -1; } @@ -647,7 +647,7 @@ static int write_chunk(HINTERNET request, const char *buffer, size_t len) if (!WinHttpWriteData(request, "\r\n", 2, &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write chunk footer"); + giterr_set(GITERR_OS, "failed to write chunk footer"); return -1; } @@ -660,7 +660,7 @@ static int winhttp_close_connection(winhttp_subtransport *t) if (t->connection) { if (!WinHttpCloseHandle(t->connection)) { - giterr_set(GITERR_OS, "Unable to close connection"); + giterr_set(GITERR_OS, "unable to close connection"); ret = -1; } @@ -669,7 +669,7 @@ static int winhttp_close_connection(winhttp_subtransport *t) if (t->session) { if (!WinHttpCloseHandle(t->session)) { - giterr_set(GITERR_OS, "Unable to close session"); + giterr_set(GITERR_OS, "unable to close session"); ret = -1; } @@ -714,7 +714,7 @@ static int winhttp_connect( /* Prepare host */ if (git__utf8_to_16_alloc(&wide_host, t->connection_data.host) < 0) { - giterr_set(GITERR_OS, "Unable to convert host to wide characters"); + giterr_set(GITERR_OS, "unable to convert host to wide characters"); return -1; } @@ -724,7 +724,7 @@ static int winhttp_connect( } if (git__utf8_to_16_alloc(&wide_ua, git_buf_cstr(&ua)) < 0) { - giterr_set(GITERR_OS, "Unable to convert host to wide characters"); + giterr_set(GITERR_OS, "unable to convert host to wide characters"); git__free(wide_host); git_buf_free(&ua); return -1; @@ -741,12 +741,12 @@ static int winhttp_connect( 0); if (!t->session) { - giterr_set(GITERR_OS, "Failed to init WinHTTP"); + giterr_set(GITERR_OS, "failed to init WinHTTP"); goto on_error; } if (!WinHttpSetTimeouts(t->session, default_timeout, default_connect_timeout, default_timeout, default_timeout)) { - giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP"); + giterr_set(GITERR_OS, "failed to set timeouts for WinHTTP"); goto on_error; } @@ -759,7 +759,7 @@ static int winhttp_connect( 0); if (!t->connection) { - giterr_set(GITERR_OS, "Failed to connect to host"); + giterr_set(GITERR_OS, "failed to connect to host"); goto on_error; } @@ -853,7 +853,7 @@ static int winhttp_stream_read( replay: /* Enforce a reasonable cap on the number of replays */ if (++replay_count >= 7) { - giterr_set(GITERR_NET, "Too many redirects or authentication replays"); + giterr_set(GITERR_NET, "too many redirects or authentication replays"); return -1; } @@ -888,7 +888,7 @@ replay: if (!WinHttpWriteData(s->request, "0\r\n\r\n", 5, &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write final chunk"); + giterr_set(GITERR_OS, "failed to write final chunk"); return -1; } } @@ -899,7 +899,7 @@ replay: if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body, 0, 0, FILE_BEGIN) && NO_ERROR != GetLastError()) { - giterr_set(GITERR_OS, "Failed to reset file pointer"); + giterr_set(GITERR_OS, "failed to reset file pointer"); return -1; } @@ -913,14 +913,14 @@ replay: &bytes_read, NULL) || !bytes_read) { git__free(buffer); - giterr_set(GITERR_OS, "Failed to read from temp file"); + giterr_set(GITERR_OS, "failed to read from temp file"); return -1; } if (!WinHttpWriteData(s->request, buffer, bytes_read, &bytes_written)) { git__free(buffer); - giterr_set(GITERR_OS, "Failed to write data"); + giterr_set(GITERR_OS, "failed to write data"); return -1; } @@ -936,7 +936,7 @@ replay: } if (!WinHttpReceiveResponse(s->request, 0)) { - giterr_set(GITERR_OS, "Failed to receive response"); + giterr_set(GITERR_OS, "failed to receive response"); return -1; } @@ -948,7 +948,7 @@ replay: WINHTTP_HEADER_NAME_BY_INDEX, &status_code, &status_code_length, WINHTTP_NO_HEADER_INDEX)) { - giterr_set(GITERR_OS, "Failed to retrieve status code"); + giterr_set(GITERR_OS, "failed to retrieve status code"); return -1; } @@ -978,7 +978,7 @@ replay: &location_length, WINHTTP_NO_HEADER_INDEX) || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { - giterr_set(GITERR_OS, "Failed to read Location header"); + giterr_set(GITERR_OS, "failed to read Location header"); return -1; } @@ -991,14 +991,14 @@ replay: location, &location_length, WINHTTP_NO_HEADER_INDEX)) { - giterr_set(GITERR_OS, "Failed to read Location header"); + giterr_set(GITERR_OS, "failed to read Location header"); git__free(location); return -1; } /* Convert the Location header to UTF-8 */ if (git__utf16_to_8_alloc(&location8, location) < 0) { - giterr_set(GITERR_OS, "Failed to convert Location header to UTF-8"); + giterr_set(GITERR_OS, "failed to convert Location header to UTF-8"); git__free(location); return -1; } @@ -1090,7 +1090,7 @@ replay: } if (HTTP_STATUS_OK != status_code) { - giterr_set(GITERR_NET, "Request failed with status code: %d", status_code); + giterr_set(GITERR_NET, "request failed with status code: %d", status_code); return -1; } @@ -1101,7 +1101,7 @@ replay: p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service); if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) { - giterr_set(GITERR_OS, "Failed to convert expected content-type to wide characters"); + giterr_set(GITERR_OS, "failed to convert expected content-type to wide characters"); return -1; } @@ -1112,12 +1112,12 @@ replay: WINHTTP_HEADER_NAME_BY_INDEX, &content_type, &content_type_length, WINHTTP_NO_HEADER_INDEX)) { - giterr_set(GITERR_OS, "Failed to retrieve response content-type"); + giterr_set(GITERR_OS, "failed to retrieve response content-type"); return -1; } if (wcscmp(expected_content_type, content_type)) { - giterr_set(GITERR_NET, "Received unexpected content-type"); + giterr_set(GITERR_NET, "received unexpected content-type"); return -1; } @@ -1129,7 +1129,7 @@ replay: (DWORD)buf_size, &dw_bytes_read)) { - giterr_set(GITERR_OS, "Failed to read data"); + giterr_set(GITERR_OS, "failed to read data"); return -1; } @@ -1152,7 +1152,7 @@ static int winhttp_stream_write_single( /* This implementation of write permits only a single call. */ if (s->sent_request) { - giterr_set(GITERR_NET, "Subtransport configured for only one write"); + giterr_set(GITERR_NET, "subtransport configured for only one write"); return -1; } @@ -1165,7 +1165,7 @@ static int winhttp_stream_write_single( (LPCVOID)buffer, (DWORD)len, &bytes_written)) { - giterr_set(GITERR_OS, "Failed to write data"); + giterr_set(GITERR_OS, "failed to write data"); return -1; } @@ -1183,12 +1183,12 @@ static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch) if (RPC_S_OK != status && RPC_S_UUID_LOCAL_ONLY != status && RPC_S_UUID_NO_ADDRESS != status) { - giterr_set(GITERR_NET, "Unable to generate name for temp file"); + giterr_set(GITERR_NET, "unable to generate name for temp file"); return -1; } if (buffer_len_cch < UUID_LENGTH_CCH + 1) { - giterr_set(GITERR_NET, "Buffer too small for name of temp file"); + giterr_set(GITERR_NET, "buffer too small for name of temp file"); return -1; } @@ -1203,7 +1203,7 @@ static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch) uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]); if (result < UUID_LENGTH_CCH) { - giterr_set(GITERR_OS, "Unable to generate name for temp file"); + giterr_set(GITERR_OS, "unable to generate name for temp file"); return -1; } @@ -1215,7 +1215,7 @@ static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch) size_t len; if (!GetTempPathW(buffer_len_cch, buffer)) { - giterr_set(GITERR_OS, "Failed to get temp path"); + giterr_set(GITERR_OS, "failed to get temp path"); return -1; } @@ -1258,13 +1258,13 @@ static int winhttp_stream_write_buffered( if (INVALID_HANDLE_VALUE == s->post_body) { s->post_body = NULL; - giterr_set(GITERR_OS, "Failed to create temporary file"); + giterr_set(GITERR_OS, "failed to create temporary file"); return -1; } } if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) { - giterr_set(GITERR_OS, "Failed to write to temporary file"); + giterr_set(GITERR_OS, "failed to write to temporary file"); return -1; } @@ -1291,7 +1291,7 @@ static int winhttp_stream_write_chunked( if (!WinHttpAddRequestHeaders(s->request, transfer_encoding, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "Failed to add a header to the request"); + giterr_set(GITERR_OS, "failed to add a header to the request"); return -1; } diff --git a/src/tree-cache.c b/src/tree-cache.c index b37be0f0d..548054136 100644 --- a/src/tree-cache.c +++ b/src/tree-cache.c @@ -137,7 +137,7 @@ static int read_tree_internal(git_tree_cache **out, return 0; corrupted: - giterr_set(GITERR_INDEX, "Corrupted TREE extension in index"); + giterr_set(GITERR_INDEX, "corrupted TREE extension in index"); return -1; } @@ -149,7 +149,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer return -1; if (buffer < buffer_end) { - giterr_set(GITERR_INDEX, "Corrupted TREE extension in index (unexpected trailing data)"); + giterr_set(GITERR_INDEX, "corrupted TREE extension in index (unexpected trailing data)"); return -1; } diff --git a/src/tree.c b/src/tree.c index 9655ad739..783dca41f 100644 --- a/src/tree.c +++ b/src/tree.c @@ -627,7 +627,7 @@ int git_tree__write_index( if (git_index_has_conflicts(index)) { giterr_set(GITERR_INDEX, - "Cannot create a tree from a not fully merged index."); + "cannot create a tree from a not fully merged index."); return GIT_EUNMERGED; } @@ -909,7 +909,7 @@ int git_tree_entry_bypath( filename_len = subpath_len(path); if (filename_len == 0) { - giterr_set(GITERR_TREE, "Invalid tree path given"); + giterr_set(GITERR_TREE, "invalid tree path given"); return GIT_ENOTFOUND; } @@ -1027,7 +1027,7 @@ int git_tree_walk( git_buf root_path = GIT_BUF_INIT; if (mode != GIT_TREEWALK_POST && mode != GIT_TREEWALK_PRE) { - giterr_set(GITERR_INVALID, "Invalid walking mode for tree walk"); + giterr_set(GITERR_INVALID, "invalid walking mode for tree walk"); return -1; } @@ -1237,7 +1237,7 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli const git_tree_entry *e = git_treebuilder_get(last->bld, basename); if (e && git_tree_entry_type(e) != git_object__type_from_filemode(update->filemode)) { git__free(basename); - giterr_set(GITERR_TREE, "Cannot replace '%s' with '%s' at '%s'", + giterr_set(GITERR_TREE, "cannot replace '%s' with '%s' at '%s'", git_object_type2string(git_tree_entry_type(e)), git_object_type2string(git_object__type_from_filemode(update->filemode)), update->path); @@ -1257,7 +1257,7 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli break; } default: - giterr_set(GITERR_TREE, "unkown action for update"); + giterr_set(GITERR_TREE, "unknown action for update"); error = -1; goto cleanup; } diff --git a/src/unix/map.c b/src/unix/map.c index c55ad1aa7..9d9b1fe38 100644 --- a/src/unix/map.c +++ b/src/unix/map.c @@ -52,7 +52,7 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs out->data = mmap(NULL, len, mprot, mflag, fd, offset); if (!out->data || out->data == MAP_FAILED) { - giterr_set(GITERR_OS, "Failed to mmap. Could not write data"); + giterr_set(GITERR_OS, "failed to mmap. Could not write data"); return -1; } diff --git a/src/util.c b/src/util.c index 76ca711b2..a44f4c9ac 100644 --- a/src/util.c +++ b/src/util.c @@ -136,7 +136,7 @@ int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const cha Return: if (ndig == 0) { - giterr_set(GITERR_INVALID, "Failed to convert string to long. Not a number"); + giterr_set(GITERR_INVALID, "failed to convert string to long: not a number"); return -1; } @@ -144,7 +144,7 @@ Return: *endptr = p; if (ovfl) { - giterr_set(GITERR_INVALID, "Failed to convert string to long. Overflow error"); + giterr_set(GITERR_INVALID, "failed to convert string to long: overflow error"); return -1; } @@ -169,7 +169,7 @@ int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const cha tmp_int = tmp_long & 0xFFFFFFFF; if (tmp_int != tmp_long) { - giterr_set(GITERR_INVALID, "Failed to convert. '%s' is too large", nptr); + giterr_set(GITERR_INVALID, "failed to convert: '%s' is too large", nptr); return -1; } diff --git a/src/win32/dir.c b/src/win32/dir.c index c15757085..8a724a4e9 100644 --- a/src/win32/dir.c +++ b/src/win32/dir.c @@ -28,7 +28,7 @@ git__DIR *git__opendir(const char *dir) new->h = FindFirstFileW(filter_w, &new->f); if (new->h == INVALID_HANDLE_VALUE) { - giterr_set(GITERR_OS, "Could not open directory '%s'", dir); + giterr_set(GITERR_OS, "could not open directory '%s'", dir); git__free(new); return NULL; } @@ -53,7 +53,7 @@ int git__readdir_ext( else if (!FindNextFileW(d->h, &d->f)) { if (GetLastError() == ERROR_NO_MORE_FILES) return 0; - giterr_set(GITERR_OS, "Could not read from directory '%s'", d->dir); + giterr_set(GITERR_OS, "could not read from directory '%s'", d->dir); return -1; } @@ -98,7 +98,7 @@ void git__rewinddir(git__DIR *d) d->h = FindFirstFileW(filter_w, &d->f); if (d->h == INVALID_HANDLE_VALUE) - giterr_set(GITERR_OS, "Could not open directory '%s'", d->dir); + giterr_set(GITERR_OS, "could not open directory '%s'", d->dir); else d->first = 1; } diff --git a/src/win32/findfile.c b/src/win32/findfile.c index 58c22279e..1c768f7f4 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -38,7 +38,7 @@ static int win32_path_to_8(git_buf *dest, const wchar_t *src) git_win32_utf8_path utf8_path; if (git_win32_path_to_utf8(utf8_path, src) < 0) { - giterr_set(GITERR_OS, "Unable to convert path to UTF-8"); + giterr_set(GITERR_OS, "unable to convert path to UTF-8"); return -1; } diff --git a/src/win32/map.c b/src/win32/map.c index 03a3646a6..5fcc1085b 100644 --- a/src/win32/map.c +++ b/src/win32/map.c @@ -67,7 +67,7 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs if (fh == INVALID_HANDLE_VALUE) { errno = EBADF; - giterr_set(GITERR_OS, "Failed to mmap. Invalid handle value"); + giterr_set(GITERR_OS, "failed to mmap. Invalid handle value"); return -1; } @@ -86,13 +86,13 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs if (page_offset != 0) { /* offset must be multiple of the allocation granularity */ errno = EINVAL; - giterr_set(GITERR_OS, "Failed to mmap. Offset must be multiple of allocation granularity"); + giterr_set(GITERR_OS, "failed to mmap. Offset must be multiple of allocation granularity"); return -1; } out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL); if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) { - giterr_set(GITERR_OS, "Failed to mmap. Invalid handle value"); + giterr_set(GITERR_OS, "failed to mmap. Invalid handle value"); out->fmh = NULL; return -1; } @@ -103,7 +103,7 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs off_hi = (DWORD)(page_start >> 32); out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len); if (!out->data) { - giterr_set(GITERR_OS, "Failed to mmap. No data written"); + giterr_set(GITERR_OS, "failed to mmap. No data written"); CloseHandle(out->fmh); out->fmh = NULL; return -1; @@ -121,7 +121,7 @@ int p_munmap(git_map *map) if (map->data) { if (!UnmapViewOfFile(map->data)) { - giterr_set(GITERR_OS, "Failed to munmap. Could not unmap view of file"); + giterr_set(GITERR_OS, "failed to munmap. Could not unmap view of file"); error = -1; } map->data = NULL; @@ -129,7 +129,7 @@ int p_munmap(git_map *map) if (map->fmh) { if (!CloseHandle(map->fmh)) { - giterr_set(GITERR_OS, "Failed to munmap. Could not close handle"); + giterr_set(GITERR_OS, "failed to munmap. Could not close handle"); error = -1; } map->fmh = NULL; diff --git a/src/win32/w32_crtdbg_stacktrace.c b/src/win32/w32_crtdbg_stacktrace.c index a778f4164..2dbdaf45b 100644 --- a/src/win32/w32_crtdbg_stacktrace.c +++ b/src/win32/w32_crtdbg_stacktrace.c @@ -253,11 +253,11 @@ int git_win32__crtdbg_stacktrace__dump( bool b_quiet = IS_BIT_SET(opt, GIT_WIN32__CRTDBG_STACKTRACE__QUIET); if (b_leaks_since_mark && b_leaks_total) { - giterr_set(GITERR_INVALID, "Cannot combine LEAKS_SINCE_MARK and LEAKS_TOTAL."); + giterr_set(GITERR_INVALID, "cannot combine LEAKS_SINCE_MARK and LEAKS_TOTAL."); return GIT_ERROR; } if (!b_set_mark && !b_leaks_since_mark && !b_leaks_total) { - giterr_set(GITERR_INVALID, "Nothing to do."); + giterr_set(GITERR_INVALID, "nothing to do."); return GIT_ERROR; } diff --git a/src/win32/w32_util.c b/src/win32/w32_util.c index 60311bb50..b7b1ffa10 100644 --- a/src/win32/w32_util.c +++ b/src/win32/w32_util.c @@ -68,7 +68,7 @@ int git_win32__set_hidden(const char *path, bool hidden) newattrs = attrs & ~FILE_ATTRIBUTE_HIDDEN; if (attrs != newattrs && !SetFileAttributesW(buf, newattrs)) { - giterr_set(GITERR_OS, "Failed to %s hidden bit for '%s'", + giterr_set(GITERR_OS, "failed to %s hidden bit for '%s'", hidden ? "set" : "unset", path); return -1; } diff --git a/src/win32/w32_util.h b/src/win32/w32_util.h index 784a7a04c..77973b502 100644 --- a/src/win32/w32_util.h +++ b/src/win32/w32_util.h @@ -174,7 +174,7 @@ GIT_INLINE(int) git_win32__file_attribute_to_stat( /* st_size gets the UTF-8 length of the target name, in bytes, * not counting the NULL terminator */ if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) { - giterr_set(GITERR_OS, "Could not convert reparse point name for '%ls'", path); + giterr_set(GITERR_OS, "could not convert reparse point name for '%ls'", path); return -1; } } diff --git a/src/zstream.c b/src/zstream.c index d949aa81a..141b49b27 100644 --- a/src/zstream.c +++ b/src/zstream.c @@ -23,7 +23,7 @@ static int zstream_seterr(git_zstream *zs) else if (zs->z.msg) giterr_set_str(GITERR_ZLIB, zs->z.msg); else - giterr_set(GITERR_ZLIB, "Unknown compression error"); + giterr_set(GITERR_ZLIB, "unknown compression error"); return -1; } From 8f0d5cdef9e2cb53c2f455d0a449f25c87647811 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 29 Dec 2016 12:55:49 +0000 Subject: [PATCH 115/142] tests: update error message checking --- tests/fetchhead/nonetwork.c | 4 ++-- tests/odb/backend/nobackend.c | 2 +- tests/refs/branches/upstream.c | 2 +- tests/refs/reflog/reflog.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/fetchhead/nonetwork.c b/tests/fetchhead/nonetwork.c index 3b750af5e..ea4b70e4a 100644 --- a/tests/fetchhead/nonetwork.c +++ b/tests/fetchhead/nonetwork.c @@ -293,7 +293,7 @@ void test_fetchhead_nonetwork__invalid_for_merge(void) cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tinvalid-merge\t\n"); cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL)); - cl_assert(git__prefixcmp(giterr_last()->message, "Invalid for-merge") == 0); + cl_assert(git__prefixcmp(giterr_last()->message, "invalid for-merge") == 0); } void test_fetchhead_nonetwork__invalid_description(void) @@ -304,7 +304,7 @@ void test_fetchhead_nonetwork__invalid_description(void) cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\n"); cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL)); - cl_assert(git__prefixcmp(giterr_last()->message, "Invalid description") == 0); + cl_assert(git__prefixcmp(giterr_last()->message, "invalid description") == 0); } static int assert_master_for_merge(const char *ref, const char *url, const git_oid *id, unsigned int is_merge, void *data) diff --git a/tests/odb/backend/nobackend.c b/tests/odb/backend/nobackend.c index 783641e8f..3c4f344b1 100644 --- a/tests/odb/backend/nobackend.c +++ b/tests/odb/backend/nobackend.c @@ -40,7 +40,7 @@ void test_odb_backend_nobackend__write_fails_gracefully(void) cl_git_fail(git_odb_write(&id, odb, "Hello world!\n", 13, GIT_OBJ_BLOB)); err = giterr_last(); - cl_assert_equal_s(err->message, "Cannot write object - unsupported in the loaded odb backends"); + cl_assert_equal_s(err->message, "cannot write object - unsupported in the loaded odb backends"); git_odb_free(odb); } diff --git a/tests/refs/branches/upstream.c b/tests/refs/branches/upstream.c index 8f2e7a2ca..82f5665d8 100644 --- a/tests/refs/branches/upstream.c +++ b/tests/refs/branches/upstream.c @@ -175,7 +175,7 @@ void test_refs_branches_upstream__no_fetch_refspec(void) cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/test")); cl_git_pass(git_reference_create(&ref, repo, "refs/remotes/matching/master", git_reference_target(branch), 1, "fetch")); cl_git_fail(git_branch_set_upstream(branch, "matching/master")); - cl_assert_equal_s("Could not determine remote for 'refs/remotes/matching/master'", + cl_assert_equal_s("could not determine remote for 'refs/remotes/matching/master'", giterr_last()->message); /* we can't set it automatically, so let's test the user setting it by hand */ diff --git a/tests/refs/reflog/reflog.c b/tests/refs/reflog/reflog.c index fdb15502c..9e46c8ae9 100644 --- a/tests/refs/reflog/reflog.c +++ b/tests/refs/reflog/reflog.c @@ -261,7 +261,7 @@ void test_refs_reflog_reflog__reading_a_reflog_with_invalid_format_returns_error error = giterr_last(); cl_assert(error != NULL); - cl_assert_equal_s("Unable to parse OID - contains invalid characters", error->message); + cl_assert_equal_s("unable to parse OID - contains invalid characters", error->message); git_reference_free(ref); git_buf_free(&logpath); From 832278bf36a382d51d8617d5575164a62ca26e6b Mon Sep 17 00:00:00 2001 From: Douglas Swanson Date: Thu, 29 Dec 2016 07:43:03 -0700 Subject: [PATCH 116/142] Fix issue #4046 Seg fault in config_files() --- examples/general.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/general.c b/examples/general.c index 7c05d8712..cc0cf1b42 100644 --- a/examples/general.c +++ b/examples/general.c @@ -57,7 +57,7 @@ static void blob_parsing(git_repository *repo); static void revwalking(git_repository *repo); static void index_walking(git_repository *repo); static void reference_listing(git_repository *repo); -static void config_files(const char *repo_path); +static void config_files(const char *repo_path, git_repository *repo); /** * Almost all libgit2 functions return 0 on success or negative on error. @@ -115,7 +115,7 @@ int main (int argc, char** argv) revwalking(repo); index_walking(repo); reference_listing(repo); - config_files(repo_path); + config_files(repo_path, repo); /** * Finally, when you're done with the repository, you can free it as well. @@ -692,12 +692,13 @@ static void reference_listing(git_repository *repo) * * [config]: http://libgit2.github.com/libgit2/#HEAD/group/config */ -static void config_files(const char *repo_path) +static void config_files(const char *repo_path, git_repository* repo) { const char *email; char config_path[256]; int32_t j; git_config *cfg; + git_config *snap_cfg; printf("\n*Config Listing*\n"); @@ -710,6 +711,7 @@ static void config_files(const char *repo_path) git_config_get_int32(&j, cfg, "help.autocorrect"); printf("Autocorrect: %d\n", j); - git_config_get_string(&email, cfg, "user.email"); + check_error(git_repository_config_snapshot(&snap_cfg, repo), "config snapshot"); + git_config_get_string(&email, snap_cfg, "user.email"); printf("Email: %s\n", email); } From 5f959dca0d7d7d921ccf2948e0745659f6055148 Mon Sep 17 00:00:00 2001 From: John Fultz Date: Thu, 29 Dec 2016 19:26:50 -0600 Subject: [PATCH 117/142] Fix handling of GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH flag. git_checkout_tree() sets up its working directory iterator to respect the pathlist if GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH is present, which is great. What's not so great is that this iterator is then used side-by-side with an iterator created by git_checkout_iterator(), which did not set up its pathlist appropriately (although the iterator mirrors all other iterator options). This could cause git_checkout_tree() to delete working tree files which were not specified in the pathlist when GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH was used, as the unsynchronized iterators causes git_checkout_tree() to think that files have been deleted between the two trees. Oops. And added a test which fails without this fix (specifically, the final check for "testrepo/README" to still be present fails). --- src/checkout.c | 4 ++++ tests/checkout/tree.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/checkout.c b/src/checkout.c index 62950913e..578695a37 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -2553,6 +2553,10 @@ int git_checkout_iterator( GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE; baseline_opts.start = data.pfx; baseline_opts.end = data.pfx; + if (opts && (opts->checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)) { + baseline_opts.pathlist.count = opts->paths.count; + baseline_opts.pathlist.strings = opts->paths.strings; + } if (data.opts.baseline_index) { if ((error = git_iterator_for_index( diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c index 4a0314a9e..c9581fdc5 100644 --- a/tests/checkout/tree.c +++ b/tests/checkout/tree.c @@ -422,6 +422,44 @@ void test_checkout_tree__can_checkout_with_pattern(void) cl_assert(git_path_exists("testrepo/new.txt")); } +void test_checkout_tree__pathlist_checkout_ignores_non_matches(void) +{ + char *entries[] = { "branch_file.txt", "link_to_new.txt" }; + + /* reset to beginning of history (i.e. just a README file) */ + + g_opts.checkout_strategy = + GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED; + + cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master")); + + cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); + cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master")); + + cl_assert(git_path_exists("testrepo/README")); + cl_assert(git_path_exists("testrepo/branch_file.txt")); + cl_assert(git_path_exists("testrepo/link_to_new.txt")); + cl_assert(git_path_exists("testrepo/new.txt")); + + cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479")); + + g_opts.checkout_strategy = + GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH; + g_opts.paths.strings = entries; + g_opts.paths.count = 2; + + cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts)); + + cl_assert(git_path_exists("testrepo/README")); + cl_assert(!git_path_exists("testrepo/branch_file.txt")); + cl_assert(!git_path_exists("testrepo/link_to_new.txt")); + cl_assert(git_path_exists("testrepo/new.txt")); + + git_object_free(g_object); + g_object = NULL; + +} + void test_checkout_tree__can_disable_pattern_match(void) { char *entries[] = { "b*.txt" }; From e56e4c76b930e600419af24a3433f1517e1a6e0f Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Fri, 30 Dec 2016 17:59:47 -0800 Subject: [PATCH 118/142] CMakeLists: Move `http-parser` block down by `zlib`, update error message --- CMakeLists.txt | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cc43c6ea..4bfd1b499 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -235,19 +235,6 @@ IF (WIN32 AND EMBED_SSH_PATH) ADD_DEFINITIONS(-DGIT_SSH) ENDIF() - -FIND_PACKAGE(HTTP_Parser) -IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2) - INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS}) - LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES}) - LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser") -ELSE() - MESSAGE(STATUS "http-parser was not found or is too old; using bundled 3rd-party sources.") - INCLUDE_DIRECTORIES(deps/http-parser) - FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h) -ENDIF() - - IF (WIN32 AND WINHTTP) ADD_DEFINITIONS(-DGIT_WINHTTP) @@ -332,6 +319,18 @@ IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") SET(SRC_REGEX deps/regex/regex.c) ENDIF() +# Optional external dependency: http-parser +FIND_PACKAGE(HTTP_Parser) +IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2) + INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS}) + LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES}) + LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser") +ELSE() + MESSAGE(STATUS "http-parser version 2 was not found; using bundled 3rd-party sources.") + INCLUDE_DIRECTORIES(deps/http-parser) + FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h) +ENDIF() + # Optional external dependency: zlib FIND_PACKAGE(ZLIB) IF (ZLIB_FOUND) From db535d0a7dfa3e5c2ded4a408234623b0241de00 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 1 Jan 2017 12:45:02 -0800 Subject: [PATCH 119/142] Delete temporary packfile in indexer This change deletes the temporary packfile that the indexer creates to avoid littering the pack/ directory with garbage. --- src/indexer.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/indexer.c b/src/indexer.c index a3a866989..27f49473a 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -151,6 +151,12 @@ cleanup: if (fd != -1) p_close(fd); + if (git_buf_is_allocated(&tmp_path)) + (void)p_unlink(git_buf_cstr(&tmp_path)); + + if (idx->pack != NULL) + (void)p_unlink(idx->pack->pack_name); + git_buf_free(&path); git_buf_free(&tmp_path); git__free(idx); @@ -1084,6 +1090,9 @@ void git_indexer_free(git_indexer *idx) git_vector_free_deep(&idx->deltas); + /* Try to delete the temporary file in case it was not committed. */ + (void)p_unlink(idx->pack->pack_name); + if (!git_mutex_lock(&git__mwindow_mutex)) { git_packfile_free(idx->pack); git_mutex_unlock(&git__mwindow_mutex); From 19ed4d0ca3fdd2f46e1391597aa2b70e4f330c6f Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sun, 1 Jan 2017 22:19:23 +0000 Subject: [PATCH 120/142] merge: set default rename threshold When `GIT_MERGE_FIND_RENAMES` is set, provide a default for `rename_threshold` when it is unset. --- include/git2/merge.h | 3 ++- src/merge.c | 10 +++++----- tests/merge/trees/renames.c | 2 ++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/include/git2/merge.h b/include/git2/merge.h index c6f6cba6c..94ac8b5c5 100644 --- a/include/git2/merge.h +++ b/include/git2/merge.h @@ -290,7 +290,8 @@ typedef struct { } git_merge_options; #define GIT_MERGE_OPTIONS_VERSION 1 -#define GIT_MERGE_OPTIONS_INIT {GIT_MERGE_OPTIONS_VERSION} +#define GIT_MERGE_OPTIONS_INIT { \ + GIT_MERGE_OPTIONS_VERSION, GIT_MERGE_FIND_RENAMES } /** * Initializes a `git_merge_options` with default values. Equivalent to diff --git a/src/merge.c b/src/merge.c index 1142917bd..c75cab635 100644 --- a/src/merge.c +++ b/src/merge.c @@ -1713,16 +1713,16 @@ static int merge_normalize_opts( if ((error = git_repository_config__weakptr(&cfg, repo)) < 0) return error; - if (given != NULL) + if (given != NULL) { memcpy(opts, given, sizeof(git_merge_options)); - else { + } else { git_merge_options init = GIT_MERGE_OPTIONS_INIT; memcpy(opts, &init, sizeof(init)); - - opts->flags = GIT_MERGE_FIND_RENAMES; - opts->rename_threshold = GIT_MERGE_DEFAULT_RENAME_THRESHOLD; } + if ((opts->flags & GIT_MERGE_FIND_RENAMES) && !opts->rename_threshold) + opts->rename_threshold = GIT_MERGE_DEFAULT_RENAME_THRESHOLD; + if (given && given->default_driver) { opts->default_driver = git__strdup(given->default_driver); GITERR_CHECK_ALLOC(opts->default_driver); diff --git a/tests/merge/trees/renames.c b/tests/merge/trees/renames.c index d7721c894..6f5c36b0a 100644 --- a/tests/merge/trees/renames.c +++ b/tests/merge/trees/renames.c @@ -242,6 +242,8 @@ void test_merge_trees_renames__no_rename_index(void) { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "7-both-renamed.txt" }, }; + opts.flags &= ~GIT_MERGE_FIND_RENAMES; + cl_git_pass(merge_trees_from_branches(&index, repo, BRANCH_RENAME_OURS, BRANCH_RENAME_THEIRS, &opts)); From def644e48a3cd4afd0cee975d3214eeeb3671c99 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 1 Jan 2017 17:35:29 -0800 Subject: [PATCH 121/142] Add a test --- tests/pack/indexer.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/pack/indexer.c b/tests/pack/indexer.c index 49a106d98..15a9017f1 100644 --- a/tests/pack/indexer.c +++ b/tests/pack/indexer.c @@ -125,3 +125,43 @@ void test_pack_indexer__fix_thin(void) git_indexer_free(idx); } } + +static int find_tmp_file_recurs(void *opaque, git_buf *path) +{ + int error = 0; + git_buf *first_tmp_file = opaque; + struct stat st; + + if ((error = p_lstat_posixly(path->ptr, &st)) < 0) + return error; + + if (S_ISDIR(st.st_mode)) + return git_path_direach(path, 0, find_tmp_file_recurs, opaque); + + /* This is the template that's used in git_futils_mktmp. */ + if (strstr(git_buf_cstr(path), "_git2_") != NULL) + return git_buf_sets(first_tmp_file, git_buf_cstr(path)); + + return 0; +} + +void test_pack_indexer__no_tmp_files(void) +{ + git_indexer *idx = NULL; + git_buf path = GIT_BUF_INIT; + git_buf first_tmp_file = GIT_BUF_INIT; + + /* Precondition: there are no temporary files. */ + cl_git_pass(git_buf_sets(&path, clar_sandbox_path())); + cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); + if (git_buf_is_allocated(&first_tmp_file)) { + cl_warning("Found a temporary file before running the test"); + cl_skip(); + } + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL)); + git_indexer_free(idx); + + cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); + cl_check(!git_buf_is_allocated(&first_tmp_file)); +} From 31c7829954c376b419f5ab318c4a00628a0002d3 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 3 Jan 2017 10:11:52 +0000 Subject: [PATCH 122/142] mention field addition in breaking API changes --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8544ac451..69ebd3a2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -150,7 +150,8 @@ v0.25 If this is `NULL`, then it will not be called and the `exists` function will be used instead. -* `git_remote_connect()` now accepts proxy options. +* `git_remote_connect()` now accepts `git_proxy_options` argument, and + `git_fetch_options` and `git_push_options` each have a `proxy_opts` field. v0.24 ------- From d8f984bc4980a491ffc61dcfdd57bed27c34ff0d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 3 Jan 2017 10:54:24 +0000 Subject: [PATCH 123/142] move git_merge_options changes to correct location --- CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69ebd3a2b..3a8db068f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -153,6 +153,10 @@ v0.25 * `git_remote_connect()` now accepts `git_proxy_options` argument, and `git_fetch_options` and `git_push_options` each have a `proxy_opts` field. +* `git_merge_options` now provides a `default_driver` that can be used + to provide the name of a merge driver to be used to handle files changed + during a merge. + v0.24 ------- @@ -228,10 +232,6 @@ v0.24 ### Breaking API changes -* `git_merge_options` now provides a `default_driver` that can be used - to provide the name of a merge driver to be used to handle files changed - during a merge. - * The `git_merge_tree_flag_t` is now `git_merge_flag_t`. Subsequently, its members are no longer prefixed with `GIT_MERGE_TREE_FLAG` but are now prefixed with `GIT_MERGE_FLAG`, and the `tree_flags` field of the From a7ff6e5e5e2715ecccdb217f2018dc2567e1eebd Mon Sep 17 00:00:00 2001 From: lhchavez Date: Tue, 3 Jan 2017 18:24:51 -0800 Subject: [PATCH 124/142] Fix the memory leak --- tests/pack/indexer.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/pack/indexer.c b/tests/pack/indexer.c index 15a9017f1..2c9724605 100644 --- a/tests/pack/indexer.c +++ b/tests/pack/indexer.c @@ -154,7 +154,9 @@ void test_pack_indexer__no_tmp_files(void) /* Precondition: there are no temporary files. */ cl_git_pass(git_buf_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); + git_buf_free(&path); if (git_buf_is_allocated(&first_tmp_file)) { + git_buf_free(&first_tmp_file); cl_warning("Found a temporary file before running the test"); cl_skip(); } @@ -162,6 +164,12 @@ void test_pack_indexer__no_tmp_files(void) cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL)); git_indexer_free(idx); + cl_git_pass(git_buf_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); - cl_check(!git_buf_is_allocated(&first_tmp_file)); + git_buf_free(&path); + if (git_buf_is_allocated(&first_tmp_file)) { + cl_warning(git_buf_cstr(&first_tmp_file)); + git_buf_free(&first_tmp_file); + cl_fail("Found a temporary file"); + } } From 96df833b63f37c21240094aa740f05600ef9c2b9 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Tue, 3 Jan 2017 19:15:09 -0800 Subject: [PATCH 125/142] Close the file before unlinking I forgot that Windows chokes while trying to delete open files. --- src/indexer.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/indexer.c b/src/indexer.c index 27f49473a..93153621a 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -1091,6 +1091,12 @@ void git_indexer_free(git_indexer *idx) git_vector_free_deep(&idx->deltas); /* Try to delete the temporary file in case it was not committed. */ + git_mwindow_free_all(&idx->pack->mwf); + /* We need to close the descriptor here so Windows doesn't choke on unlink */ + if (idx->pack->mwf.fd != -1) { + (void)p_close(idx->pack->mwf.fd); + idx->pack->mwf.fd = -1; + } (void)p_unlink(idx->pack->pack_name); if (!git_mutex_lock(&git__mwindow_mutex)) { From 9a64e62f0f20c9cf9b2e1609f037060eb2d8eb22 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Wed, 21 Dec 2016 21:24:33 +0100 Subject: [PATCH 126/142] http: check certificate validity before clobbering the error variable --- src/transports/http.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/transports/http.c b/src/transports/http.c index ad28c5889..155fd7b30 100644 --- a/src/transports/http.c +++ b/src/transports/http.c @@ -624,13 +624,12 @@ static int http_connect(http_subtransport *t) if ((!error || error == GIT_ECERTIFICATE) && t->owner->certificate_check_cb != NULL && git_stream_is_encrypted(t->io)) { git_cert *cert; - int is_valid; + int is_valid = (error == GIT_OK); if ((error = git_stream_certificate(&cert, t->io)) < 0) return error; giterr_clear(); - is_valid = error != GIT_ECERTIFICATE; error = t->owner->certificate_check_cb(cert, is_valid, t->connection_data.host, t->owner->message_cb_payload); if (error < 0) { From 98d66240ecb7765e191da19b535c75c92ccc90fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 6 Jan 2017 10:51:31 +0000 Subject: [PATCH 127/142] http: perform 'badssl' check also via certificate callback Make sure that the callbacks do also get a 'valid' value of zero when the certificate we're looking at is in valid and assert that within the test. --- tests/online/badssl.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/online/badssl.c b/tests/online/badssl.c index 66b090df4..f3470f6d7 100644 --- a/tests/online/badssl.c +++ b/tests/online/badssl.c @@ -10,37 +10,66 @@ static bool g_has_ssl = true; static bool g_has_ssl = false; #endif +static int cert_check_assert_invalid(git_cert *cert, int valid, const char* host, void *payload) +{ + GIT_UNUSED(cert); GIT_UNUSED(host); GIT_UNUSED(payload); + + cl_assert_equal_i(0, valid); + + return GIT_ECERTIFICATE; +} + void test_online_badssl__expired(void) { + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + opts.fetch_opts.callbacks.certificate_check = cert_check_assert_invalid; + if (!g_has_ssl) cl_skip(); cl_git_fail_with(GIT_ECERTIFICATE, git_clone(&g_repo, "https://expired.badssl.com/fake.git", "./fake", NULL)); + + cl_git_fail_with(GIT_ECERTIFICATE, + git_clone(&g_repo, "https://expired.badssl.com/fake.git", "./fake", &opts)); } void test_online_badssl__wrong_host(void) { + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + opts.fetch_opts.callbacks.certificate_check = cert_check_assert_invalid; + if (!g_has_ssl) cl_skip(); cl_git_fail_with(GIT_ECERTIFICATE, git_clone(&g_repo, "https://wrong.host.badssl.com/fake.git", "./fake", NULL)); + cl_git_fail_with(GIT_ECERTIFICATE, + git_clone(&g_repo, "https://wrong.host.badssl.com/fake.git", "./fake", &opts)); } void test_online_badssl__self_signed(void) { + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + opts.fetch_opts.callbacks.certificate_check = cert_check_assert_invalid; + if (!g_has_ssl) cl_skip(); cl_git_fail_with(GIT_ECERTIFICATE, git_clone(&g_repo, "https://self-signed.badssl.com/fake.git", "./fake", NULL)); + cl_git_fail_with(GIT_ECERTIFICATE, + git_clone(&g_repo, "https://self-signed.badssl.com/fake.git", "./fake", &opts)); } void test_online_badssl__old_cipher(void) { + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + opts.fetch_opts.callbacks.certificate_check = cert_check_assert_invalid; + if (!g_has_ssl) cl_skip(); cl_git_fail(git_clone(&g_repo, "https://rc4.badssl.com/fake.git", "./fake", NULL)); + cl_git_fail(git_clone(&g_repo, "https://rc4.badssl.com/fake.git", "./fake", &opts)); } From 66e3774d279672ee51c3b54545a79d20d1ada834 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 15 Nov 2016 11:36:27 +0100 Subject: [PATCH 128/142] smart_pkt: verify packet length exceeds PKT_LEN_SIZE Each packet line in the Git protocol is prefixed by a four-byte length of how much data will follow, which we parse in `git_pkt_parse_line`. The transmitted length can either be equal to zero in case of a flush packet or has to be at least of length four, as it also includes the encoded length itself. Not checking this may result in a buffer overflow as we directly pass the length to functions which accept a `size_t` length as parameter. Fix the issue by verifying that non-flush packets have at least a length of `PKT_LEN_SIZE`. --- src/transports/smart_pkt.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/transports/smart_pkt.c b/src/transports/smart_pkt.c index 2297cc94f..6fe53b931 100644 --- a/src/transports/smart_pkt.c +++ b/src/transports/smart_pkt.c @@ -427,6 +427,14 @@ int git_pkt_parse_line( if (bufflen > 0 && bufflen < (size_t)len) return GIT_EBUFS; + /* + * The length has to be exactly 0 in case of a flush + * packet or greater than PKT_LEN_SIZE, as the decoded + * length includes its own encoded length of four bytes. + */ + if (len != 0 && len < PKT_LEN_SIZE) + return GIT_ERROR; + line += PKT_LEN_SIZE; /* * TODO: How do we deal with empty lines? Try again? with the next From 2fdef641fd0dd2828bd948234ae86de75221a11a Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 15 Nov 2016 11:44:51 +0100 Subject: [PATCH 129/142] smart_pkt: treat empty packet lines as error The Git protocol does not specify what should happen in the case of an empty packet line (that is a packet line "0004"). We currently indicate success, but do not return a packet in the case where we hit an empty line. The smart protocol was not prepared to handle such packets in all cases, though, resulting in a `NULL` pointer dereference. Fix the issue by returning an error instead. As such kind of packets is not even specified by upstream, this is the right thing to do. --- src/transports/smart_pkt.c | 10 +++++----- src/transports/smart_protocol.c | 11 ----------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/transports/smart_pkt.c b/src/transports/smart_pkt.c index 6fe53b931..e05196cd8 100644 --- a/src/transports/smart_pkt.c +++ b/src/transports/smart_pkt.c @@ -437,13 +437,13 @@ int git_pkt_parse_line( line += PKT_LEN_SIZE; /* - * TODO: How do we deal with empty lines? Try again? with the next - * line? + * The Git protocol does not specify empty lines as part + * of the protocol. Not knowing what to do with an empty + * line, we should return an error upon hitting one. */ if (len == PKT_LEN_SIZE) { - *head = NULL; - *out = line; - return 0; + giterr_set_str(GITERR_NET, "Invalid empty packet"); + return GIT_ERROR; } if (len == 0) { /* Flush pkt */ diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c index 53c0b089e..db6a8b9c8 100644 --- a/src/transports/smart_protocol.c +++ b/src/transports/smart_protocol.c @@ -763,14 +763,6 @@ static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt, line_len -= (line_end - line); line = line_end; - /* When a valid packet with no content has been - * read, git_pkt_parse_line does not report an - * error, but the pkt pointer has not been set. - * Handle this by skipping over empty packets. - */ - if (pkt == NULL) - continue; - error = add_push_report_pkt(push, pkt); git_pkt_free(pkt); @@ -825,9 +817,6 @@ static int parse_report(transport_smart *transport, git_push *push) error = 0; - if (pkt == NULL) - continue; - switch (pkt->type) { case GIT_PKT_DATA: /* This is a sideband packet which contains other packets */ From a5cf255b471ad7113247d552d5695db0cb720882 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 6 Jan 2017 17:15:53 +0000 Subject: [PATCH 130/142] Bump version to 0.25.1 --- include/git2/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/git2/version.h b/include/git2/version.h index 0df191f4f..d190893fe 100644 --- a/include/git2/version.h +++ b/include/git2/version.h @@ -7,10 +7,10 @@ #ifndef INCLUDE_git_version_h__ #define INCLUDE_git_version_h__ -#define LIBGIT2_VERSION "0.25.0" +#define LIBGIT2_VERSION "0.25.1" #define LIBGIT2_VER_MAJOR 0 #define LIBGIT2_VER_MINOR 25 -#define LIBGIT2_VER_REVISION 0 +#define LIBGIT2_VER_REVISION 1 #define LIBGIT2_VER_PATCH 0 #define LIBGIT2_SOVERSION 25 From 3fdba15c2f3fd8577dff6cbc61073b723136af4d Mon Sep 17 00:00:00 2001 From: "Force.Charlie-I" Date: Mon, 9 Jan 2017 14:09:57 +0800 Subject: [PATCH 131/142] fix examples/network/clone.c: heap-buffer-overflow Format of a length of string to the correct format is:%.*s --- examples/network/clone.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/network/clone.c b/examples/network/clone.c index caf41cca8..540000bfd 100644 --- a/examples/network/clone.c +++ b/examples/network/clone.c @@ -50,7 +50,7 @@ static int sideband_progress(const char *str, int len, void *payload) { (void)payload; // unused - printf("remote: %*s", len, str); + printf("remote: %.*s", len, str); fflush(stdout); return 0; } From 3829ba2e710553893faf6336cc6b2f3fc17a293e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 9 Jan 2017 17:50:17 +0000 Subject: [PATCH 132/142] http: correct the expected error for RC4 We must make sure that we're getting a certificate error from the library so we know that we're testing the right thing. --- tests/online/badssl.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/online/badssl.c b/tests/online/badssl.c index f3470f6d7..6dacc18b6 100644 --- a/tests/online/badssl.c +++ b/tests/online/badssl.c @@ -70,6 +70,8 @@ void test_online_badssl__old_cipher(void) if (!g_has_ssl) cl_skip(); - cl_git_fail(git_clone(&g_repo, "https://rc4.badssl.com/fake.git", "./fake", NULL)); - cl_git_fail(git_clone(&g_repo, "https://rc4.badssl.com/fake.git", "./fake", &opts)); + cl_git_fail_with(GIT_ECERTIFICATE, + git_clone(&g_repo, "https://rc4.badssl.com/fake.git", "./fake", NULL)); + cl_git_fail_with(GIT_ECERTIFICATE, + git_clone(&g_repo, "https://rc4.badssl.com/fake.git", "./fake", &opts)); } From 2ac57aa89bde788173b54bd153430369deec64c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 9 Jan 2017 17:53:21 +0000 Subject: [PATCH 133/142] https: don't test that RC4 is invalid None of our crypto backends actually reject RC4 as a cipher so don't test for it and instead keep it as something we'd like to do. --- tests/online/badssl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/online/badssl.c b/tests/online/badssl.c index 6dacc18b6..aa4c24d9c 100644 --- a/tests/online/badssl.c +++ b/tests/online/badssl.c @@ -67,6 +67,9 @@ void test_online_badssl__old_cipher(void) git_clone_options opts = GIT_CLONE_OPTIONS_INIT; opts.fetch_opts.callbacks.certificate_check = cert_check_assert_invalid; + /* FIXME: we don't actually reject RC4 anywhere, figure out what to tweak */ + cl_skip(); + if (!g_has_ssl) cl_skip(); From 07bb8078cbea77e3d85bf087e2510c2e23d8fa29 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 14 Jan 2017 16:01:53 +0000 Subject: [PATCH 134/142] CHANGELOG: update `GIT_MERGE_OPTIONS_INIT` changes --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8544ac451..9e60424ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ v0.25 + 1 ### Changes or improvements +* `GIT_MERGE_OPTIONS_INIT` now includes a setting to perform rename detection. + This aligns this structure with the default by `git_merge` and + `git_merge_trees` when `NULL` was provided for the options. + ### API additions ### API removals From f5586f5c73ac162393df10feec0117f59bbd1409 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 14 Jan 2017 16:37:00 +0000 Subject: [PATCH 135/142] Addressed review feedback --- src/indexer.c | 10 +++++----- tests/pack/indexer.c | 13 +++---------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/indexer.c b/src/indexer.c index 93153621a..3d1c81c86 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -151,11 +151,11 @@ cleanup: if (fd != -1) p_close(fd); - if (git_buf_is_allocated(&tmp_path)) - (void)p_unlink(git_buf_cstr(&tmp_path)); + if (git_buf_len(&tmp_path) > 0) + p_unlink(git_buf_cstr(&tmp_path)); if (idx->pack != NULL) - (void)p_unlink(idx->pack->pack_name); + p_unlink(idx->pack->pack_name); git_buf_free(&path); git_buf_free(&tmp_path); @@ -1094,10 +1094,10 @@ void git_indexer_free(git_indexer *idx) git_mwindow_free_all(&idx->pack->mwf); /* We need to close the descriptor here so Windows doesn't choke on unlink */ if (idx->pack->mwf.fd != -1) { - (void)p_close(idx->pack->mwf.fd); + p_close(idx->pack->mwf.fd); idx->pack->mwf.fd = -1; } - (void)p_unlink(idx->pack->pack_name); + p_unlink(idx->pack->pack_name); if (!git_mutex_lock(&git__mwindow_mutex)) { git_packfile_free(idx->pack); diff --git a/tests/pack/indexer.c b/tests/pack/indexer.c index 2c9724605..1e514b2d6 100644 --- a/tests/pack/indexer.c +++ b/tests/pack/indexer.c @@ -155,11 +155,7 @@ void test_pack_indexer__no_tmp_files(void) cl_git_pass(git_buf_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); git_buf_free(&path); - if (git_buf_is_allocated(&first_tmp_file)) { - git_buf_free(&first_tmp_file); - cl_warning("Found a temporary file before running the test"); - cl_skip(); - } + cl_assert(git_buf_len(&first_tmp_file) == 0); cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL)); git_indexer_free(idx); @@ -167,9 +163,6 @@ void test_pack_indexer__no_tmp_files(void) cl_git_pass(git_buf_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); git_buf_free(&path); - if (git_buf_is_allocated(&first_tmp_file)) { - cl_warning(git_buf_cstr(&first_tmp_file)); - git_buf_free(&first_tmp_file); - cl_fail("Found a temporary file"); - } + cl_assert(git_buf_len(&first_tmp_file) == 0); + git_buf_free(&first_tmp_file); } From 4e4a1460ebf6e2ed53c23ddf641307346aaa6db2 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 30 Dec 2016 12:13:34 -0500 Subject: [PATCH 136/142] WinHTTP: support best auth mechanism For username/password credentials, support NTLM or Basic (in that order of priority). Use the WinHTTP built-in authentication support for both, and maintain a bitfield of the supported mechanisms from the response. --- src/transports/winhttp.c | 131 +++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 67 deletions(-) diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c index 051340d87..79b3ac6b4 100644 --- a/src/transports/winhttp.c +++ b/src/transports/winhttp.c @@ -68,7 +68,8 @@ static const IID IID_IInternetSecurityManager_mingw = typedef enum { GIT_WINHTTP_AUTH_BASIC = 1, - GIT_WINHTTP_AUTH_NEGOTIATE = 2, + GIT_WINHTTP_AUTH_NTLM = 2, + GIT_WINHTTP_AUTH_NEGOTIATE = 4, } winhttp_authmechanism_t; typedef struct { @@ -95,79 +96,66 @@ typedef struct { git_cred *cred; git_cred *url_cred; git_cred *proxy_cred; - int auth_mechanism; + int auth_mechanisms; HINTERNET session; HINTERNET connection; } winhttp_subtransport; -static int apply_basic_credential_proxy(HINTERNET request, git_cred *cred) +static int _apply_userpass_credential(HINTERNET request, DWORD target, DWORD scheme, git_cred *cred) { git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred; wchar_t *user, *pass; - int error; + int user_len = 0, pass_len = 0, error = 0; - if ((error = git__utf8_to_16_alloc(&user, c->username)) < 0) - return error; + if ((error = user_len = git__utf8_to_16_alloc(&user, c->username)) < 0) + goto done; - if ((error = git__utf8_to_16_alloc(&pass, c->password)) < 0) - return error; + if ((error = pass_len = git__utf8_to_16_alloc(&pass, c->password)) < 0) + goto done; - if (!WinHttpSetCredentials(request, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC, - user, pass, NULL)) { - giterr_set(GITERR_OS, "failed to set proxy auth"); + if (!WinHttpSetCredentials(request, target, scheme, user, pass, NULL)) { + giterr_set(GITERR_OS, "failed to set credentials"); error = -1; } +done: + if (user_len > 0) + git__memzero(user, user_len * sizeof(wchar_t)); + + if (pass_len > 0) + git__memzero(pass, pass_len * sizeof(wchar_t)); + git__free(user); git__free(pass); return error; } -static int apply_basic_credential(HINTERNET request, git_cred *cred) +static int apply_userpass_credential_proxy(HINTERNET request, git_cred *cred) { - git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred; - git_buf buf = GIT_BUF_INIT, raw = GIT_BUF_INIT; - wchar_t *wide = NULL; - int error = -1, wide_len; - - git_buf_printf(&raw, "%s:%s", c->username, c->password); - - if (git_buf_oom(&raw) || - git_buf_puts(&buf, "Authorization: Basic ") < 0 || - git_buf_encode_base64(&buf, git_buf_cstr(&raw), raw.size) < 0) - goto on_error; - - if ((wide_len = git__utf8_to_16_alloc(&wide, git_buf_cstr(&buf))) < 0) { - giterr_set(GITERR_OS, "failed to convert string to wide form"); - goto on_error; - } - - if (!WinHttpAddRequestHeaders(request, wide, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) { - giterr_set(GITERR_OS, "failed to add a header to the request"); - goto on_error; - } - - error = 0; - -on_error: - /* We were dealing with plaintext passwords, so clean up after ourselves a bit. */ - if (wide) - memset(wide, 0x0, wide_len * sizeof(wchar_t)); - - if (buf.size) - memset(buf.ptr, 0x0, buf.size); - - if (raw.size) - memset(raw.ptr, 0x0, raw.size); - - git__free(wide); - git_buf_free(&buf); - git_buf_free(&raw); - return error; + return _apply_userpass_credential(request, WINHTTP_AUTH_TARGET_PROXY, + WINHTTP_AUTH_SCHEME_BASIC, cred); } -static int apply_default_credentials(HINTERNET request) +static int apply_userpass_credential(HINTERNET request, int mechanisms, git_cred *cred) +{ + DWORD native_scheme; + + if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) || + (mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE)) { + native_scheme = WINHTTP_AUTH_SCHEME_NTLM; + } else if (mechanisms & GIT_WINHTTP_AUTH_BASIC) { + native_scheme = WINHTTP_AUTH_SCHEME_BASIC; + } else { + giterr_set(GITERR_NET, "invalid authentication scheme"); + return -1; + } + + return _apply_userpass_credential(request, WINHTTP_AUTH_TARGET_SERVER, + native_scheme, cred); +} + +static int apply_default_credentials(HINTERNET request, int mechanisms) { /* Either the caller explicitly requested that default credentials be passed, * or our fallback credential callback was invoked and checked that the target @@ -177,6 +165,12 @@ static int apply_default_credentials(HINTERNET request) * to Internet Explorer security zones, but in fact does not. */ DWORD data = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW; + if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) == 0 && + (mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) == 0) { + giterr_set(GITERR_NET, "invalid authentication scheme"); + return -1; + } + if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &data, sizeof(DWORD))) return -1; @@ -453,7 +447,7 @@ static int winhttp_stream_connect(winhttp_stream *s) if (t->proxy_cred) { if (t->proxy_cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT) { - if ((error = apply_basic_credential_proxy(s->request, t->proxy_cred)) < 0) + if ((error = apply_userpass_credential_proxy(s->request, t->proxy_cred)) < 0) goto on_error; } } @@ -550,13 +544,11 @@ static int winhttp_stream_connect(winhttp_stream *s) /* If we have a credential on the subtransport, apply it to the request */ if (t->cred && t->cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT && - t->auth_mechanism == GIT_WINHTTP_AUTH_BASIC && - apply_basic_credential(s->request, t->cred) < 0) + apply_userpass_credential(s->request, t->auth_mechanisms, t->cred) < 0) goto on_error; else if (t->cred && t->cred->credtype == GIT_CREDTYPE_DEFAULT && - t->auth_mechanism == GIT_WINHTTP_AUTH_NEGOTIATE && - apply_default_credentials(s->request) < 0) + apply_default_credentials(s->request, t->auth_mechanisms) < 0) goto on_error; /* If no other credentials have been applied and the URL has username and @@ -565,7 +557,7 @@ static int winhttp_stream_connect(winhttp_stream *s) if (!t->url_cred && git_cred_userpass_plaintext_new(&t->url_cred, t->connection_data.user, t->connection_data.pass) < 0) goto on_error; - if (apply_basic_credential(s->request, t->url_cred) < 0) + if (apply_userpass_credential(s->request, GIT_WINHTTP_AUTH_BASIC, t->url_cred) < 0) goto on_error; } @@ -585,12 +577,12 @@ on_error: static int parse_unauthorized_response( HINTERNET request, int *allowed_types, - int *auth_mechanism) + int *allowed_mechanisms) { DWORD supported, first, target; *allowed_types = 0; - *auth_mechanism = 0; + *allowed_mechanisms = 0; /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes(). * We can assume this was already done, since we know we are unauthorized. @@ -600,15 +592,20 @@ static int parse_unauthorized_response( return -1; } - if (WINHTTP_AUTH_SCHEME_BASIC & supported) { + if (WINHTTP_AUTH_SCHEME_NTLM & supported) { *allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT; - *auth_mechanism = GIT_WINHTTP_AUTH_BASIC; + *allowed_types |= GIT_CREDTYPE_DEFAULT; + *allowed_mechanisms = GIT_WINHTTP_AUTH_NEGOTIATE; } - if ((WINHTTP_AUTH_SCHEME_NTLM & supported) || - (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported)) { + if (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported) { *allowed_types |= GIT_CREDTYPE_DEFAULT; - *auth_mechanism = GIT_WINHTTP_AUTH_NEGOTIATE; + *allowed_mechanisms = GIT_WINHTTP_AUTH_NEGOTIATE; + } + + if (WINHTTP_AUTH_SCHEME_BASIC & supported) { + *allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT; + *allowed_mechanisms |= GIT_WINHTTP_AUTH_BASIC; } return 0; @@ -1029,7 +1026,7 @@ replay: if (status_code == HTTP_STATUS_PROXY_AUTH_REQ) { int allowed_types; - if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0) + if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanisms) < 0) return -1; /* TODO: extract the username from the url, no payload? */ @@ -1049,7 +1046,7 @@ replay: if (HTTP_STATUS_DENIED == status_code && get_verb == s->verb) { int allowed_types; - if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0) + if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanisms) < 0) return -1; if (allowed_types) { From ca05857e71f8d11582b1ad82f63c6a61e96fe20e Mon Sep 17 00:00:00 2001 From: David Turner Date: Wed, 23 Nov 2016 18:26:19 -0500 Subject: [PATCH 137/142] Fix formatting Signed-off-by: David Turner --- src/submodule.c | 126 ++++++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/src/submodule.c b/src/submodule.c index 6d6b314d6..46462f165 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -324,89 +324,89 @@ done: static int submodules_from_index(git_strmap *map, git_index *idx, git_config *cfg) { - int error; - git_iterator *i; - const git_index_entry *entry; - git_buf name = GIT_BUF_INIT; + int error; + git_iterator *i; + const git_index_entry *entry; + git_buf name = GIT_BUF_INIT; - if ((error = git_iterator_for_index(&i, git_index_owner(idx), idx, NULL)) < 0) - return error; + if ((error = git_iterator_for_index(&i, git_index_owner(idx), idx, NULL)) < 0) + return error; - while (!(error = git_iterator_advance(&entry, i))) { - khiter_t pos = git_strmap_lookup_index(map, entry->path); - git_submodule *sm; + while (!(error = git_iterator_advance(&entry, i))) { + khiter_t pos = git_strmap_lookup_index(map, entry->path); + git_submodule *sm; - git_buf_clear(&name); - if (!name_from_path(&name, cfg, entry->path)) { - git_strmap_lookup_index(map, name.ptr); - } + git_buf_clear(&name); + if (!name_from_path(&name, cfg, entry->path)) { + git_strmap_lookup_index(map, name.ptr); + } - if (git_strmap_valid_index(map, pos)) { - sm = git_strmap_value_at(map, pos); + if (git_strmap_valid_index(map, pos)) { + sm = git_strmap_value_at(map, pos); - if (S_ISGITLINK(entry->mode)) - submodule_update_from_index_entry(sm, entry); - else - sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; - } else if (S_ISGITLINK(entry->mode)) { - if (!submodule_get_or_create(&sm, git_index_owner(idx), map, name.ptr ? name.ptr : entry->path)) { - submodule_update_from_index_entry(sm, entry); - git_submodule_free(sm); - } - } - } + if (S_ISGITLINK(entry->mode)) + submodule_update_from_index_entry(sm, entry); + else + sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; + } else if (S_ISGITLINK(entry->mode)) { + if (!submodule_get_or_create(&sm, git_index_owner(idx), map, name.ptr ? name.ptr : entry->path)) { + submodule_update_from_index_entry(sm, entry); + git_submodule_free(sm); + } + } + } - if (error == GIT_ITEROVER) - error = 0; + if (error == GIT_ITEROVER) + error = 0; - git_buf_free(&name); - git_iterator_free(i); + git_buf_free(&name); + git_iterator_free(i); - return error; + return error; } static int submodules_from_head(git_strmap *map, git_tree *head, git_config *cfg) { - int error; - git_iterator *i; - const git_index_entry *entry; - git_buf name = GIT_BUF_INIT; + int error; + git_iterator *i; + const git_index_entry *entry; + git_buf name = GIT_BUF_INIT; - if ((error = git_iterator_for_tree(&i, head, NULL)) < 0) - return error; + if ((error = git_iterator_for_tree(&i, head, NULL)) < 0) + return error; - while (!(error = git_iterator_advance(&entry, i))) { - khiter_t pos = git_strmap_lookup_index(map, entry->path); - git_submodule *sm; + while (!(error = git_iterator_advance(&entry, i))) { + khiter_t pos = git_strmap_lookup_index(map, entry->path); + git_submodule *sm; - git_buf_clear(&name); - if (!name_from_path(&name, cfg, entry->path)) { - git_strmap_lookup_index(map, name.ptr); - } + git_buf_clear(&name); + if (!name_from_path(&name, cfg, entry->path)) { + git_strmap_lookup_index(map, name.ptr); + } - if (git_strmap_valid_index(map, pos)) { - sm = git_strmap_value_at(map, pos); + if (git_strmap_valid_index(map, pos)) { + sm = git_strmap_value_at(map, pos); - if (S_ISGITLINK(entry->mode)) - submodule_update_from_head_data(sm, entry->mode, &entry->id); - else - sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE; - } else if (S_ISGITLINK(entry->mode)) { - if (!submodule_get_or_create(&sm, git_tree_owner(head), map, name.ptr ? name.ptr : entry->path)) { - submodule_update_from_head_data( - sm, entry->mode, &entry->id); - git_submodule_free(sm); - } - } - } + if (S_ISGITLINK(entry->mode)) + submodule_update_from_head_data(sm, entry->mode, &entry->id); + else + sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE; + } else if (S_ISGITLINK(entry->mode)) { + if (!submodule_get_or_create(&sm, git_tree_owner(head), map, name.ptr ? name.ptr : entry->path)) { + submodule_update_from_head_data( + sm, entry->mode, &entry->id); + git_submodule_free(sm); + } + } + } - if (error == GIT_ITEROVER) - error = 0; + if (error == GIT_ITEROVER) + error = 0; - git_buf_free(&name); - git_iterator_free(i); + git_buf_free(&name); + git_iterator_free(i); - return error; + return error; } /* If have_sm is true, sm is populated, otherwise map an repo are. */ From 4d99c4cfc604bb141fd4e1423e934ebd3fb7e2a7 Mon Sep 17 00:00:00 2001 From: Brock Peabody Date: Wed, 23 Nov 2016 18:32:48 -0500 Subject: [PATCH 138/142] Allow for caching of submodules. Added `git_repository_submodule_cache_all` to initialze a cache of submodules on the repository so that operations looking up N submodules are O(N) and not O(N^2). Added a `git_repository_submodule_cache_clear` function to remove the cache. Also optimized the function that loads all submodules as it was itself O(N^2) w.r.t the number of submodules, having to loop through the `.gitmodules` file once per submodule. I changed it to process the `.gitmodules` file once, into a map. Signed-off-by: David Turner --- include/git2/sys/repository.h | 29 +++++++++ src/repository.c | 33 ++++++++++ src/repository.h | 1 + src/submodule.c | 119 +++++++++++++++++++++++----------- src/submodule.h | 3 + tests/submodule/lookup.c | 25 +++++++ 6 files changed, 171 insertions(+), 39 deletions(-) diff --git a/include/git2/sys/repository.h b/include/git2/sys/repository.h index 800396c86..0c9142143 100644 --- a/include/git2/sys/repository.h +++ b/include/git2/sys/repository.h @@ -135,6 +135,35 @@ GIT_EXTERN(void) git_repository_set_index(git_repository *repo, git_index *index */ GIT_EXTERN(int) git_repository_set_bare(git_repository *repo); +/** + * Load and cache all submodules. + * + * Because the `.gitmodules` file is unstructured, loading submodules is an + * O(N) operation. Any operation (such as `git_rebase_init`) that requires + * accessing all submodules is O(N^2) in the number of submodules, if it + * has to look each one up individually. This function loads all submodules + * and caches them so that subsequent calls to `git_submodule_lookup` are O(1). + * + * @param repo the repository whose submodules will be cached. + */ +GIT_EXTERN(int) git_repository_submodule_cache_all( + git_repository *repo); + +/** + * Clear the submodule cache. + * + * Clear the submodule cache populated by `git_repository_submodule_cache_all`. + * If there is no cache, do nothing. + * + * The cache incorporates data from the repository's configuration, as well + * as the state of the working tree, the index, and HEAD. So any time any + * of these has changed, the cache might become invalid. + * + * @param repo the repository whose submodule cache will be cleared + */ +GIT_EXTERN(int) git_repository_submodule_cache_clear( + git_repository *repo); + /** @} */ GIT_END_DECL #endif diff --git a/src/repository.c b/src/repository.c index 60299193f..2185632bf 100644 --- a/src/repository.c +++ b/src/repository.c @@ -27,6 +27,10 @@ #include "merge.h" #include "diff_driver.h" #include "annotated_commit.h" +#include "submodule.h" + +GIT__USE_STRMAP +#include "strmap.h" #ifdef GIT_WIN32 # include "win32/w32_util.h" @@ -109,6 +113,7 @@ void git_repository__cleanup(git_repository *repo) { assert(repo); + git_repository_submodule_cache_clear(repo); git_cache_clear(&repo->objects); git_attr_cache_flush(repo); @@ -2541,3 +2546,31 @@ int git_repository_set_ident(git_repository *repo, const char *name, const char return 0; } + +int git_repository_submodule_cache_all(git_repository *repo) +{ + int error; + + assert(repo); + + if ((error = git_strmap_alloc(&repo->submodule_cache))) + return error; + + error = git_submodule__map(repo, repo->submodule_cache); + return error; +} + +int git_repository_submodule_cache_clear(git_repository *repo) +{ + git_submodule *sm; + assert(repo); + if (repo->submodule_cache == NULL) { + return 0; + } + git_strmap_foreach_value(repo->submodule_cache, sm, { + git_submodule_free(sm); + }); + git_strmap_free(repo->submodule_cache); + repo->submodule_cache = 0; + return 0; +} diff --git a/src/repository.h b/src/repository.h index b259bea3b..9d276f376 100644 --- a/src/repository.h +++ b/src/repository.h @@ -143,6 +143,7 @@ struct git_repository { git_atomic attr_session_key; git_cvar_value cvar_cache[GIT_CVAR_CACHE_MAX]; + git_strmap *submodule_cache; }; GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo) diff --git a/src/submodule.c b/src/submodule.c index 46462f165..6996006dc 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -149,40 +149,53 @@ static int find_by_path(const git_config_entry *entry, void *payload) } /** - * Find out the name of a submodule from its path + * Release the name map returned by 'load_submodule_names'. */ -static int name_from_path(git_buf *out, git_config *cfg, const char *path) +static void free_submodule_names(git_strmap *names) +{ + git_buf *name = 0; + if (names == NULL) + return; + git_strmap_foreach_value(names, name, { + git__free(name); + }); + git_strmap_free(names); + return; +} + +/** + * Map submodule paths to names. + * TODO: for some use-cases, this might need case-folding on a + * case-insensitive filesystem + */ +static int load_submodule_names(git_strmap *out, git_config *cfg) { const char *key = "submodule\\..*\\.path"; git_config_iterator *iter; git_config_entry *entry; - int error; + git_buf buf = GIT_BUF_INIT; + int rval; + int error = 0; if ((error = git_config_iterator_glob_new(&iter, cfg, key)) < 0) return error; - while ((error = git_config_next(&entry, iter)) == 0) { + while (git_config_next(&entry, iter) == 0) { const char *fdot, *ldot; - /* TODO: this should maybe be strcasecmp on a case-insensitive fs */ - if (strcmp(path, entry->value) != 0) - continue; - fdot = strchr(entry->name, '.'); ldot = strrchr(entry->name, '.'); - git_buf_clear(out); - git_buf_put(out, fdot + 1, ldot - fdot - 1); - goto cleanup; + git_buf_put(&buf, fdot + 1, ldot - fdot - 1); + git_strmap_insert(out, entry->value, git_buf_detach(&buf), rval); + if (rval < 0) { + giterr_set(GITERR_NOMEMORY, "Error inserting submodule into hash table"); + free_submodule_names(out); + return -1; + } } - if (error == GIT_ITEROVER) { - giterr_set(GITERR_SUBMODULE, "could not find a submodule name for '%s'", path); - error = GIT_ENOTFOUND; - } - -cleanup: git_config_iterator_free(iter); - return error; + return 0; } int git_submodule_lookup( @@ -196,6 +209,17 @@ int git_submodule_lookup( assert(repo && name); + if (repo->submodule_cache != NULL) { + khiter_t pos = git_strmap_lookup_index(repo->submodule_cache, name); + if (git_strmap_valid_index(repo->submodule_cache, pos)) { + if (out) { + *out = git_strmap_value_at(repo->submodule_cache, pos); + GIT_REFCOUNT_INC(*out); + } + return 0; + } + } + if ((error = submodule_alloc(&sm, repo, name)) < 0) return error; @@ -327,20 +351,18 @@ static int submodules_from_index(git_strmap *map, git_index *idx, git_config *cf int error; git_iterator *i; const git_index_entry *entry; - git_buf name = GIT_BUF_INIT; + git_strmap *names = 0; + git_strmap_alloc(&names); + if ((error = load_submodule_names(names, cfg))) + goto done; if ((error = git_iterator_for_index(&i, git_index_owner(idx), idx, NULL)) < 0) - return error; + goto done; while (!(error = git_iterator_advance(&entry, i))) { khiter_t pos = git_strmap_lookup_index(map, entry->path); git_submodule *sm; - git_buf_clear(&name); - if (!name_from_path(&name, cfg, entry->path)) { - git_strmap_lookup_index(map, name.ptr); - } - if (git_strmap_valid_index(map, pos)) { sm = git_strmap_value_at(map, pos); @@ -349,7 +371,17 @@ static int submodules_from_index(git_strmap *map, git_index *idx, git_config *cf else sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; } else if (S_ISGITLINK(entry->mode)) { - if (!submodule_get_or_create(&sm, git_index_owner(idx), map, name.ptr ? name.ptr : entry->path)) { + khiter_t name_pos; + const char *name; + + name_pos = git_strmap_lookup_index(names, entry->path); + if (git_strmap_valid_index(names, name_pos)) { + name = git_strmap_value_at(names, name_pos); + } else { + name = entry->path; + } + + if (!submodule_get_or_create(&sm, git_index_owner(idx), map, name)) { submodule_update_from_index_entry(sm, entry); git_submodule_free(sm); } @@ -359,8 +391,9 @@ static int submodules_from_index(git_strmap *map, git_index *idx, git_config *cf if (error == GIT_ITEROVER) error = 0; - git_buf_free(&name); +done: git_iterator_free(i); + free_submodule_names(names); return error; } @@ -370,20 +403,18 @@ static int submodules_from_head(git_strmap *map, git_tree *head, git_config *cfg int error; git_iterator *i; const git_index_entry *entry; - git_buf name = GIT_BUF_INIT; + git_strmap *names = 0; + git_strmap_alloc(&names); + if ((error = load_submodule_names(names, cfg))) + goto done; if ((error = git_iterator_for_tree(&i, head, NULL)) < 0) - return error; + goto done; while (!(error = git_iterator_advance(&entry, i))) { khiter_t pos = git_strmap_lookup_index(map, entry->path); git_submodule *sm; - git_buf_clear(&name); - if (!name_from_path(&name, cfg, entry->path)) { - git_strmap_lookup_index(map, name.ptr); - } - if (git_strmap_valid_index(map, pos)) { sm = git_strmap_value_at(map, pos); @@ -392,7 +423,17 @@ static int submodules_from_head(git_strmap *map, git_tree *head, git_config *cfg else sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE; } else if (S_ISGITLINK(entry->mode)) { - if (!submodule_get_or_create(&sm, git_tree_owner(head), map, name.ptr ? name.ptr : entry->path)) { + khiter_t name_pos; + const char *name; + + name_pos = git_strmap_lookup_index(names, entry->path); + if (git_strmap_valid_index(names, name_pos)) { + name = git_strmap_value_at(names, name_pos); + } else { + name = entry->path; + } + + if (!submodule_get_or_create(&sm, git_tree_owner(head), map, name)) { submodule_update_from_head_data( sm, entry->mode, &entry->id); git_submodule_free(sm); @@ -403,8 +444,9 @@ static int submodules_from_head(git_strmap *map, git_tree *head, git_config *cfg if (error == GIT_ITEROVER) error = 0; - git_buf_free(&name); +done: git_iterator_free(i); + free_submodule_names(names); return error; } @@ -416,7 +458,7 @@ typedef struct { git_repository *repo; } lfc_data; -static int all_submodules(git_repository *repo, git_strmap *map) +int git_submodule__map(git_repository *repo, git_strmap *map) { int error = 0; git_index *idx = NULL; @@ -509,7 +551,7 @@ int git_submodule_foreach( if ((error = git_strmap_alloc(&submodules)) < 0) return error; - if ((error = all_submodules(repo, submodules)) < 0) + if ((error = git_submodule__map(repo, submodules)) < 0) goto done; if (!(error = git_vector_init( @@ -1566,7 +1608,6 @@ int git_submodule_location(unsigned int *location, git_submodule *sm) location, NULL, NULL, NULL, sm, GIT_SUBMODULE_IGNORE_ALL); } - /* * INTERNAL FUNCTIONS */ diff --git a/src/submodule.h b/src/submodule.h index 2ef2031b3..456a93979 100644 --- a/src/submodule.h +++ b/src/submodule.h @@ -143,4 +143,7 @@ extern int git_submodule_parse_ignore( extern int git_submodule_parse_update( git_submodule_update_t *out, const char *value); +extern int git_submodule__map( + git_repository *repo, + git_strmap *map); #endif diff --git a/tests/submodule/lookup.c b/tests/submodule/lookup.c index 148f9273e..e36fc44e0 100644 --- a/tests/submodule/lookup.c +++ b/tests/submodule/lookup.c @@ -388,3 +388,28 @@ void test_submodule_lookup__renamed(void) cl_git_pass(git_submodule_foreach(g_repo, sm_lookup_cb, &data)); cl_assert_equal_i(8, data.count); } + +void test_submodule_lookup_cached(void) { + git_submodule *sm; + git_submodule *sm2; + /* See that the simple tests still pass. */ + + git_repository_submodule_cache_all(g_repo); + test_submodule_lookup__simple_lookup(); + git_repository_submodule_cache_clear(g_repo); + test_submodule_lookup__simple_lookup(); + + /* Check that subsequent calls return different objects when cached. */ + git_repository_submodule_cache_all(g_repo); + cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); + cl_git_pass(git_submodule_lookup(&sm2, g_repo, "sm_unchanged")); + cl_assert_equal_p(sm, sm2); + git_submodule_free(sm2); + + /* and that we get new objects again after clearing the cache. */ + git_repository_submodule_cache_clear(g_repo); + cl_git_pass(git_submodule_lookup(&sm2, g_repo, "sm_unchanged")); + cl_assert(sm != sm2); + git_submodule_free(sm); + git_submodule_free(sm2); +} From 673dff88a2c78766609a7023167acb5a882fb84d Mon Sep 17 00:00:00 2001 From: Brock Peabody Date: Wed, 23 Nov 2016 18:32:55 -0500 Subject: [PATCH 139/142] Skip submodule head/index update when caching. `git_submodule_status` is very slow, bottlenecked on `git_repository_head_tree`, which it uses through `submodule_update_head`. If the user has requested submodule caching, assume that they want this status cached too and skip it. Signed-off-by: David Turner --- src/submodule.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/submodule.c b/src/submodule.c index 6996006dc..3099db6cf 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -1544,13 +1544,22 @@ int git_submodule__status( return 0; } - /* refresh the index OID */ - if (submodule_update_index(sm) < 0) - return -1; + /* If the user has requested caching submodule state, performing these + * expensive operations (especially `submodule_update_head`, which is + * bottlenecked on `git_repository_head_tree`) eliminates much of the + * advantage. We will, therefore, interpret the request for caching to + * apply here to and skip them. + */ - /* refresh the HEAD OID */ - if (submodule_update_head(sm) < 0) - return -1; + if (sm->repo->submodule_cache == NULL) { + /* refresh the index OID */ + if (submodule_update_index(sm) < 0) + return -1; + + /* refresh the HEAD OID */ + if (submodule_update_head(sm) < 0) + return -1; + } /* for ignore == dirty, don't scan the working directory */ if (ign == GIT_SUBMODULE_IGNORE_DIRTY) { From d030bba9fa64c4363368f9e3e0ed3f115dc06dc2 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 21 Jan 2017 17:15:33 +0000 Subject: [PATCH 140/142] indexer: only delete temp file if it was unused Only try to `unlink` our temp file when we know that we didn't copy it into its permanent location. --- src/indexer.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/indexer.c b/src/indexer.c index 3d1c81c86..aa5646023 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -33,7 +33,7 @@ struct entry { struct git_indexer { unsigned int parsed_header :1, - opened_pack :1, + pack_committed :1, have_stream :1, have_delta :1; struct git_pack_header hdr; @@ -1060,6 +1060,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats) /* And don't forget to rename the packfile to its new place. */ p_rename(idx->pack->pack_name, git_buf_cstr(&filename)); + idx->pack_committed = 1; git_buf_free(&filename); git_hash_ctx_cleanup(&ctx); @@ -1092,12 +1093,13 @@ void git_indexer_free(git_indexer *idx) /* Try to delete the temporary file in case it was not committed. */ git_mwindow_free_all(&idx->pack->mwf); + /* We need to close the descriptor here so Windows doesn't choke on unlink */ - if (idx->pack->mwf.fd != -1) { + if (idx->pack->mwf.fd != -1) p_close(idx->pack->mwf.fd); - idx->pack->mwf.fd = -1; - } - p_unlink(idx->pack->pack_name); + + if (!idx->pack_committed) + p_unlink(idx->pack->pack_name); if (!git_mutex_lock(&git__mwindow_mutex)) { git_packfile_free(idx->pack); From bf339ab0ef64754dcc8e7506dd0d23629815a65a Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 21 Jan 2017 14:51:31 -0500 Subject: [PATCH 141/142] indexer: introduce `git_packfile_close` Encapsulation! --- src/indexer.c | 13 +++---------- src/pack.c | 17 +++++++++++++---- src/pack.h | 1 + 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/indexer.c b/src/indexer.c index 05a02f646..805c36ea4 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -1091,17 +1091,10 @@ void git_indexer_free(git_indexer *idx) git_vector_free_deep(&idx->deltas); - /* Try to delete the temporary file in case it was not committed. */ - git_mwindow_free_all(&idx->pack->mwf); - - /* We need to close the descriptor here so Windows doesn't choke on unlink */ - if (idx->pack->mwf.fd != -1) - p_close(idx->pack->mwf.fd); - - if (!idx->pack_committed) - p_unlink(idx->pack->pack_name); - if (!git_mutex_lock(&git__mwindow_mutex)) { + if (!idx->pack_committed) + git_packfile_close(idx->pack, true); + git_packfile_free(idx->pack); git_mutex_unlock(&git__mwindow_mutex); } diff --git a/src/pack.c b/src/pack.c index 345ff5259..243719d9c 100644 --- a/src/pack.c +++ b/src/pack.c @@ -991,6 +991,18 @@ git_off_t get_delta_base( * ***********************************************************/ +void git_packfile_close(struct git_pack_file *p, bool unlink_packfile) +{ + if (p->mwf.fd >= 0) { + git_mwindow_free_all_locked(&p->mwf); + p_close(p->mwf.fd); + p->mwf.fd = -1; + } + + if (unlink_packfile) + p_unlink(p->pack_name); +} + void git_packfile_free(struct git_pack_file *p) { if (!p) @@ -998,10 +1010,7 @@ void git_packfile_free(struct git_pack_file *p) cache_free(&p->bases); - if (p->mwf.fd >= 0) { - git_mwindow_free_all_locked(&p->mwf); - p_close(p->mwf.fd); - } + git_packfile_close(p, false); pack_index_free(p); diff --git a/src/pack.h b/src/pack.h index 5302db5b7..e2bf165f4 100644 --- a/src/pack.h +++ b/src/pack.h @@ -149,6 +149,7 @@ git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs, git_off_t *curpos, git_otype type, git_off_t delta_obj_offset); +void git_packfile_close(struct git_pack_file *p, bool unlink_packfile); void git_packfile_free(struct git_pack_file *p); int git_packfile_alloc(struct git_pack_file **pack_out, const char *path); From 87b7a7051b8aa50bddc806a46240e7b15e3e23cc Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 21 Jan 2017 15:44:57 -0500 Subject: [PATCH 142/142] indexer: avoid warning about `idx->pack` It must be non-NULL to have a valid `git_indexer`. --- src/indexer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/indexer.c b/src/indexer.c index 805c36ea4..951d7179c 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -1081,7 +1081,7 @@ void git_indexer_free(git_indexer *idx) git_vector_free_deep(&idx->objects); - if (idx->pack && idx->pack->idx_cache) { + if (idx->pack->idx_cache) { struct git_pack_entry *pentry; kh_foreach_value( idx->pack->idx_cache, pentry, { git__free(pentry); });