Home Explore Blog CI



nushell

1st chunk of `cookbook/parsing_git_log.md`
92d78b09d2a9bbd1f6e93e53b36dc28dcf854b78897692d900000001000013eb
---
title: Parsing Git Log
---

# Parsing Git Log

# Let's parse git log

This `git log` command is interesting but you can't do a lot with it like this.

```nu
git log
```

Let's make it more parsable

```nu
git log --pretty="%h|%s|%aN|%aE|%aD" -n 25
```

This will work but I've been burnt by this in the past when a pipe `|` gets injected in the commits.

So, let's try again with something that most likely won't show up in commits, `»¦«`. Also, since we're not using a pipe now we don't have to use quotes around the pretty format string. Notice that the output is just a bunch of strings.

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5
# => 42f1874a»¦«Update some examples and docs (#4682)»¦«Justin Ma»¦«hustcer@outlook.com»¦«Tue, 1 Mar 2022 21:05:29 +0800
# => 2a89936b»¦«Move to latest stable crossterm, with fix (#4684)»¦«Sophia»¦«547158+sophiajt@users.noreply.github.com»¦«Tue, 1 Mar 2022 07:05:46 -0500
# => ece5e7db»¦«dataframe list command (#4681)»¦«Fernando Herrera»¦«fernando.j.herrera@gmail.com»¦«Tue, 1 Mar 2022 11:41:13 +0000
# => a6a96b29»¦«Add binary literals (#4680)»¦«Sophia»¦«547158+sophiajt@users.noreply.github.com»¦«Mon, 28 Feb 2022 18:31:53 -0500
# => e3100e6a»¦«Fix alias in `docs/sample_config/config.toml` (#4669)»¦«Luca Trevisani»¦«lucatrv@hotmail.com»¦«Mon, 28 Feb 2022 22:47:14 +0100
```

Ahh, much better. Now that we have the raw data, let's try to parse it with nu.

First we need to get it in lines or rows. Notice that the output is now in a table format.

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines
# => ───┬─────────────────────────────────────────────────────────────────────────────────────────────────
# =>  0 │ 42f1874a»¦«Update some examples and docs (#4682)»¦«Justin Ma»¦«hustcer@outlook.com»¦«Tue, 1 Mar
# =>    │ 2022 21:05:29 +0800
# =>  1 │ 2a89936b»¦«Move to latest stable crossterm, with fix
# =>    │ (#4684)»¦«Sophia»¦«547158+sophiajt@users.noreply.github.com»¦«Tue, 1 Mar 2022 07:05:46 -0500
# =>  2 │ ece5e7db»¦«dataframe list command (#4681)»¦«Fernando
# =>    │ Herrera»¦«fernando.j.herrera@gmail.com»¦«Tue, 1 Mar 2022 11:41:13 +0000
# =>  3 │ a6a96b29»¦«Add binary literals (#4680)»¦«Sophia»¦«547158+sophiajt@users.noreply.github.com»¦«Mon, 28
# =>    │ Feb 2022 18:31:53 -0500
# =>  4 │ e3100e6a»¦«Fix alias in `docs/sample_config/config.toml` (#4669)»¦«Luca
# =>    │ Trevisani»¦«lucatrv@hotmail.com»¦«Mon, 28 Feb 2022 22:47:14 +0100
# => ───┴─────────────────────────────────────────────────────────────────────────────────────────────────
```

That's more like nushell, but it would be nice to have some columns.

We used the delimiter `»¦«` specifically so we can create columns so let's use it like this.

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«"
# => ───┬──────────┬──────────────────────┬──────────────────┬────────────────────────┬──────────────────
# =>  # │ column1  │       column2        │     column3      │       column4          │     column5
# => ───┼──────────┼──────────────────────┼──────────────────┼────────────────────────┼──────────────────
# =>  0 │ 42f1874a │ Update some examples │ Justin Ma        │ hustcer@outlook.com    │ Tue, 1 Mar 2022
# =>    │          │ and docs (#4682)     │                  │                        │ 21:05:29 +0800
# =>  1 │ 2a89936b │ Move to latest       │ Sophia           │ 547158+sophiajt@users. │ Tue, 1 Mar 2022
# =>    │          │ stable crossterm,    │                  │ noreply.github.com     │ 07:05:46 -0500
# =>    │          │ with fix (#4684)     │                  │                        │
# =>  2 │ ece5e7db │ dataframe list       │ Fernando Herrera │ fernando.j.herrera@g   │ Tue, 1 Mar 2022
# =>    │          │ command (#4681)      │                  │ mail.com               │ 11:41:13 +0000
# =>  3 │ a6a96b29 │ Add binary literals  │ Sophia           │ 547158+sophiajt@users. │ Mon, 28 Feb 2022
# =>    │          │ (#4680)              │                  │ noreply.github.com     │ 18:31:53 -0500

Title: Parsing Git Log in Nushell
Summary
This section demonstrates how to parse the output of `git log` command in Nushell. It starts by showing how to format the `git log` output using a custom delimiter to avoid conflicts with commit messages. Then, it shows how to use `lines` and `split column` commands to convert the output into a table format that is easier to work with in Nushell.