Home Explore Blog CI



nushell

13th chunk of `cookbook/parsing_git_log.md`
b9eeaa67c1471c0538a3846f9ef045207a684dc8242f27930000000100001288
# =>     │          │ (#4650)                  │                   │ l.com                     │
# =>  23 │ ed46f0ea │ fix: add missing         │ Jae-Heon Ji       │ 32578710+jaeheonji@user   │ 3 days ago
# =>     │          │ metadata for `ls_colors` │                   │ s.noreply.github.com      │
# =>     │          │ (#4603)                  │                   │                           │
# =>  24 │ 0c3ea636 │ Add support for stderr   │ Sophia            │ 547158+sophiajt@users.nor │ 4 days ago
# =>     │          │ and exit code (#4647)    │                   │ eply.github.com           │
# => ────┴──────────┴──────────────────────────┴───────────────────┴───────────────────────────┴──────────────
```

Now, with the 365 day slice of data, let's `group-by` name where the commits are less than a year old. This table has a lot of columns so it's unreadable. However, if we `group-by` name and `transpose` the table things will look much cleaner. `Pivot` takes rows and turns them into columns or turns columns into rows.

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose
# => ─────┬─────────────────────────────────┬──────────────────
# =>   #  │             column0             │     column1
# => ─────┼─────────────────────────────────┼──────────────────
# =>    0 │ Justin Ma                       │ [table 21 rows]
# =>    1 │ Sophia                          │ [table 851 rows]
# =>    2 │ Fernando Herrera                │ [table 176 rows]
# =>    3 │ Luca Trevisani                  │ [table 1 row]
# =>    4 │ Stefan Holderbach               │ [table 19 rows]
# =>    5 │ Jonathan Moore                  │ [table 2 rows]
# =>    6 │ Darren Schroeder                │ [table 242 rows]
# =>    7 │ LordMZTE                        │ [table 1 row]
# =>    8 │ Jae-Heon Ji                     │ [table 10 rows]
# =>    9 │ zkldi                           │ [table 1 row]
# =>   10 │ Michael Angerman                │ [table 61 rows]
# => ...
```

Side note: If you happen to get errors, pay attention to the error message. For instance, this error means that the data being returned from `git log` is somehow incomplete. Specifically, there is a missing date column. I've seen git commands work perfectly on Windows and not work at all on Linux or Mac. I'm not sure why. If you run into this issue, one easy way to temporarily avoid it is to limit `git log` results to a certain number like `git log -n 100`.

```
error: Unknown column
  ┌─ shell:1:124
  │
1 │ git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day))
  │                                                                                                                              ^^^^
  │                                                                                                                              │
  │                                                                                                                              There isn't a column named 'date'
  │                                                                                                                              Perhaps you meant 'commit'? Columns available: commit, subject
```

Here's one tip for dealing with this error. We have a `do` command that has an `--ignore_errors` parameter. This is how you'd use it in the above example, if it were giving errors.

```nu
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | do -i { split column "»¦«" commit subject name email date } | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose
```

Now, back to parsing.
What if we throw in the `sort-by` and `reverse` commands for good measure? Also, while we're in there, let's get rid of the `[table 21 rows]` thing too. We do that by using the `length` command on each row of column1.

Title: Analyzing Git Commit Data with Nushell: Grouping, Transposing, and Error Handling
Summary
This section demonstrates how to analyze Git commit data using Nushell. It covers grouping commits by author name, transposing the resulting table for readability, and removing the '[table rows]' notation by calculating the length of each grouped table. It also addresses potential errors during data parsing and suggests using the `do -i` command with `--ignore_errors` to handle incomplete data from `git log`.