Skip to content

examples: additions and fixes#5421

Merged
pks-t merged 1 commit into
libgit2:masterfrom
petersalomonsen:examples-fixes-and-additions
Apr 4, 2020
Merged

examples: additions and fixes#5421
pks-t merged 1 commit into
libgit2:masterfrom
petersalomonsen:examples-fixes-and-additions

Conversation

@petersalomonsen
Copy link
Copy Markdown
Contributor

here's the example fixes / additions from my emscripten PR as requested in #5345 (comment)

  • add example for git commit
  • fix example for git add
  • add example for git push

@pks-t
Copy link
Copy Markdown
Member

pks-t commented Feb 24, 2020

I will review this after v1.0 is released. Please feel free to ping me after the release if I forget.

Copy link
Copy Markdown
Member

@pks-t pks-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for these examples, I'm always happy to extend what we have. There's a few comments I'd like to address, but I'm happy to merge this in the future.

Comment thread examples/commit.c Outdated
const char *comment = argv[2];
int error;

git_oid commit_oid,tree_oid;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is indented with spaces

Comment thread examples/commit.c Outdated
git_revparse_ext(&parent, &ref, repo, "HEAD");
git_repository_index(&index, repo);
git_index_write_tree(&tree_oid, index);
git_index_write(index);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the above commands should be checked for errors.

Comment thread examples/commit.c Outdated
(void)repo; /* unused */

/* Validate args */
if (argc < 3 || strcmp(opt, "-m")!=0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space before and after !=

Comment thread examples/commit.c Outdated
const git_error *err = git_error_last();
if (err) printf("ERROR %d: %s\n", err->klass, err->message);
else printf("ERROR %d: no detailed info\n", error);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use check_lg2 in general for any kind of error checking.

Comment thread examples/push.c
const git_strarray refspecs = {
&refspec,
1,
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some formatting errors above with regards to whitespace. We also always use /* */ even for single-line comments, same below.

Comment thread examples/push.c Outdated
const git_error *err = git_error_last();
if (err) printf("ERROR %d: %s\n", err->klass, err->message);
else printf("ERROR %d: no detailed info\n", error);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use check_lg2, then you don't need this block. so please also use it fo r git_remote_lookup and git_push_options_init.

@petersalomonsen petersalomonsen force-pushed the examples-fixes-and-additions branch 4 times, most recently from 8642b2c to c73eeed Compare April 1, 2020 18:57
@petersalomonsen
Copy link
Copy Markdown
Contributor Author

Thanks for reviewing @pks-t . I believe the issues are resolved now. For commit I had to handle GIT_ENOTFOUND differently ( empty repositories have no commits ), but elsewhere I've used check_lg2 for error handling. Please let me know if there are still issues.

Copy link
Copy Markdown
Member

@pks-t pks-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be nitpicky, but there's some more style issues I'd like to have fixed before merging. Looks fine to me otherwise :)

Comment thread examples/commit.c Outdated
git_reference *ref = NULL;
git_signature *signature;

(void)repo; /* unused */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed, you use the repo ;)

Comment thread examples/commit.c Outdated
}

error = git_revparse_ext(&parent, &ref, repo, "HEAD");
if ( error == GIT_ENOTFOUND) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a stray space here in between ( error

Comment thread examples/commit.c Outdated

check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "Error looking up tree", NULL);

git_signature_default(&signature, repo);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also requires error checking.

Comment thread examples/push.c Outdated

check_lg2(git_push_options_init( &options, GIT_PUSH_OPTIONS_VERSION ), "Error initializing push", NULL);

check_lg2(git_remote_push( remote, &refspecs, &options), "Error pushing", NULL);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few more stray spaces in these commands :)

Comment thread examples/commit.c Outdated
check_lg2(git_index_write_tree(&tree_oid, index), "Could not write tree", NULL);;
check_lg2(git_index_write(index), "Could not write index", NULL);;

git_index_free(index);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this line before the return error to not disturb the program's business logic flowing and make it easier to understand

add example for git commit
fix example for git add
add example for git push
@petersalomonsen petersalomonsen force-pushed the examples-fixes-and-additions branch from c73eeed to dc2beb7 Compare April 2, 2020 16:39
@petersalomonsen
Copy link
Copy Markdown
Contributor Author

yeah, no problem being nitpicky :-) quality is important, and I feel a bit novice on the code conventions here - so very happy for your guidance :-) Think I got the issues resolved now, but let me know if there are more :-)

Copy link
Copy Markdown
Member

@pks-t pks-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks a lot for these examples!

@pks-t
Copy link
Copy Markdown
Member

pks-t commented Apr 4, 2020

yeah, no problem being nitpicky :-) quality is important, and I feel a bit novice on the code conventions here - so very happy for your guidance :-) Think I got the issues resolved now, but let me know if there are more :-)

That's on us, though. We don't have a style guide anywhere, so newcomers are left guessing. We should definitely have one, but I've been saying that for a few years now and never just sat down and wrote one.

@pks-t pks-t merged commit 7d9b1f0 into libgit2:master Apr 4, 2020
@petersalomonsen petersalomonsen deleted the examples-fixes-and-additions branch July 19, 2020 05:45
@aaronfranke
Copy link
Copy Markdown
Contributor

aaronfranke commented Apr 17, 2021

The push example always fails for me. I tested pushing to remotes on GitHub, GitLab, and a local repo on my desktop. I always get -1 returned from git_remote_push when push to a remote on server, and -8 when pushing to the repo on my desktop. I tried the refspec as refs/heads/master and refs/heads/master:refs/remotes/myremote/master, both fail. The git_remote_lookup and git_push_options_init methods always succeed and return 0.

After doing some testing with pushing to a local remote on my desktop, I've isolated the failure: transport->push fails inside of do_push inside of git_push_finish inside of git_remote_upload inside of git_remote_push.

With pushing to both my GitHub and GitLab remotes, it fails in t->connect in git_remote__connect in git_remote_connect in git_remote_push.

For pushing to both my local desktop copy and to GitLab, the error message is: "the transport has not yet loaded the refs"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants