diff --git a/README.md b/README.md index 9bb36b7..3b31fa3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1850 TILs and counting..._ +_1851 TILs and counting..._ See some of the other learning resources I work on: @@ -402,6 +402,7 @@ If you've learned something here, support my efforts writing daily TILs by - [List All Files Added During Span Of Time](git/list-all-files-added-during-span-of-time.md) - [List All Files Changed Between Two Branches](git/list-all-files-changed-between-two-branches.md) - [List All Git Aliases From gitconfig](git/list-all-git-aliases-from-gitconfig.md) +- [List And Count All Posts In TIL Repo](git/list-and-count-all-posts-in-til-repo.md) - [List Branches That Contain A Commit](git/list-branches-that-contain-a-commit.md) - [List Commits On A Branch](git/list-commits-on-a-branch.md) - [List Different Commits Between Two Branches](git/list-different-commits-between-two-branches.md) diff --git a/git/list-and-count-all-posts-in-til-repo.md b/git/list-and-count-all-posts-in-til-repo.md new file mode 100644 index 0000000..8561719 --- /dev/null +++ b/git/list-and-count-all-posts-in-til-repo.md @@ -0,0 +1,39 @@ +# List And Count All Posts In TIL Repo + +I want to be able to reliably list and count all posts in [my TIL +repo](https://github.com/jbranchaud/til). I do this to check that the count in +the README is accurate and in [the workflow +script](https://github.com/jbranchaud/jbranchaud/blob/71cba39dffb2bff68bf16d8895e435065e400250/scripts/update_tils.py#L101) +that powers [my GitHub Profile +README](https://github.com/jbranchaud/jbranchaud). In the past, I've used +pattern matching on the listing of all TILs in the +[README.md](https://github.com/jbranchaud/til/blob/master/README.md). That is +error prone and has required me to use two different markdown list styles. + +A better approach is to ask `git` how many posts it currently has under version +control. I use a consistent directory structure where each TIL post is a +markdown file that is nested within a single category directory. + +```bash +❯ git ls-files -- */*.md +ack/ack-bar.md +ack/case-insensitive-search.md +ack/list-available-file-types.md +... +``` + +Using `git ls-files` has the added benefit of only listing files that are +currently checked in to the project. So if I run this locally, it won't pick up +a draft post that hasn't been committed yet. + +I can then pipe this to `wc -l` (count of all lines) to produce the count: + +```bash +❯ git ls-files -- */*.md | wc -l | xargs +1850 +``` + +Note: the empty `xargs` is a trick to trim the whitespace padding that `wc` +introduces. + +See `man git-ls-files` for more details.