Home Explore Blog CI



git

5th chunk of `Documentation/MyFirstContribution.adoc`
778c885bd63c4290274ac8905df56f9b14095f9474973b6c0000000100000fac

----

Build and try it. As you may expect, there's pretty much just whatever we give
on the command line, including the name of our command. (If `prefix` is empty
for you, try `cd Documentation/ && ../bin-wrappers/git psuh`). That's not so
helpful. So what other context can we get?

Add a line to `#include "config.h"` and `#include "repository.h"`.
Then, add the following bits to the function body:
function body:

----
	const char *cfg_name;

...

	repo_config(repo, git_default_config, NULL);
	if (repo_config_get_string_tmp(repo, "user.name", &cfg_name))
		printf(_("No name is found in config\n"));
	else
		printf(_("Your name: %s\n"), cfg_name);
----

`repo_config()` will grab the configuration from config files known to Git and
apply standard precedence rules. `repo_config_get_string_tmp()` will look up
a specific key ("user.name") and give you the value. There are a number of
single-key lookup functions like this one; you can see them all (and more info
about how to use `repo_config()`) in `Documentation/technical/api-config.adoc`.

You should see that the name printed matches the one you see when you run:

----
$ git config --get user.name
----

Great! Now we know how to check for values in the Git config. Let's commit this
too, so we don't lose our progress.

----
$ git add builtin/psuh.c
$ git commit -sm "psuh: show parameters & config opts"
----

NOTE: Again, the above is for sake of brevity in this tutorial. In a real change
you should not use `-m` but instead use the editor to write a meaningful
message.

Still, it'd be nice to know what the user's working context is like. Let's see
if we can print the name of the user's current branch. We can mimic the
`git status` implementation; the printer is located in `wt-status.c` and we can
see that the branch is held in a `struct wt_status`.

`wt_status_print()` gets invoked by `cmd_status()` in `builtin/commit.c`.
Looking at that implementation we see the status config being populated like so:

----
status_init_config(&s, git_status_config);
----

But as we drill down, we can find that `status_init_config()` wraps a call
to `repo_config()`. Let's modify the code we wrote in the previous commit.

Be sure to include the header to allow you to use `struct wt_status`:

----
#include "wt-status.h"
----

Then modify your `cmd_psuh` implementation to declare your `struct wt_status`,
prepare it, and print its contents:

----
	struct wt_status status;

...

	wt_status_prepare(repo, &status);
	repo_config(repo, git_default_config, &status);

...

	printf(_("Your current branch: %s\n"), status.branch);
----

Run it again. Check it out - here's the (verbose) name of your current branch!

Let's commit this as well.

----
$ git add builtin/psuh.c
$ git commit -sm "psuh: print the current branch"
----

Now let's see if we can get some info about a specific commit.

Luckily, there are some helpers for us here. `commit.h` has a function called
`lookup_commit_reference_by_name` to which we can simply provide a hardcoded
string; `pretty.h` has an extremely handy `pp_commit_easy()` call which doesn't
require a full format object to be passed.

Add the following includes:

----
#include "commit.h"
#include "pretty.h"
----

Then, add the following lines within your implementation of `cmd_psuh()` near
the declarations and the logic, respectively.

----
	struct commit *c = NULL;
	struct strbuf commitline = STRBUF_INIT;

...

	c = lookup_commit_reference_by_name("origin/master");

	if (c != NULL) {
		pp_commit_easy(CMIT_FMT_ONELINE, c, &commitline);
		printf(_("Current commit: %s\n"), commitline.buf);
	}
----

The `struct strbuf` provides some safety belts to your basic `char*`, one of
which is a length member to prevent buffer overruns. It needs to be initialized
nicely with `STRBUF_INIT`. Keep it in mind when you need to pass around `char*`.

`lookup_commit_reference_by_name` resolves the name you pass it, so you can play
with the value there and see what kind of things you can come up with.

`pp_commit_easy`

Title: Enhancing the Git Command with Config and Commit Information
Summary
This section of the tutorial explains how to enhance the new Git command by retrieving and printing additional information, including the user's name from the Git config, the current branch, and details about a specific commit, using various Git functions and structures such as `repo_config()`, `struct wt_status`, `lookup_commit_reference_by_name()`, and `pp_commit_easy()`.