1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-05 08:08:02 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
jbranchaud
fdd2461b75 Add Check If A File Has Changed In A Script as a Git TIL 2025-10-28 12:25:43 -05:00
jbranchaud
e8c2e01d6f Add better status check for notes:push 2025-10-28 08:29:15 -05:00
jbranchaud
ed9cedc870 Add Run A Task If It Meets Criteria as a Taskfile TIL 2025-10-28 08:26:39 -05:00
4 changed files with 77 additions and 2 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1670 TILs and counting..._
_1672 TILs and counting..._
See some of the other learning resources I work on:
@@ -83,6 +83,7 @@ If you've learned something here, support my efforts writing daily TILs by
* [SQLite](#sqlite)
* [Streaming](#streaming)
* [Tailwind CSS](#tailwind-css)
* [Taskfile](#taskfile)
* [tmux](#tmux)
* [TypeScript](#typescript)
* [Unix](#unix)
@@ -320,6 +321,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Caching Credentials](git/caching-credentials.md)
- [Change The Start Point Of A Branch](git/change-the-start-point-of-a-branch.md)
- [Check How A File Is Being Ignored](git/check-how-a-file-is-being-ignored.md)
- [Check If A File Has Changed In A Script](git/check-if-a-file-has-changed-in-a-script.md)
- [Checking Commit Ancestry](git/checking-commit-ancestry.md)
- [Checkout Old Version Of A File](git/checkout-old-version-of-a-file.md)
- [Checkout Previous Branch](git/checkout-previous-branch.md)
@@ -1494,6 +1496,10 @@ If you've learned something here, support my efforts writing daily TILs by
- [Specify Paths For Purging Unused CSS](tailwind/specify-paths-for-purging-unused-css.md)
- [Use Tailwind Typography Prose In Dark Mode](tailwind/use-tailwind-typography-prose-in-dark-mode.md)
### Taskfile
- [Run A Task If It Meets Criteria](taskfile/run-a-task-if-it-meets-criteria.md)
### tmux
- [Access Past Copy Buffer History](tmux/access-past-copy-buffer-history.md)

View File

@@ -51,7 +51,7 @@ tasks:
- git commit -m "Update notes - $(date '+%Y-%m-%d %H:%M')"
- git push
status:
- git add NOTES.md && git diff --cached --quiet
- git diff --exit-code NOTES.md
silent: false
notes:status:

View File

@@ -0,0 +1,38 @@
# Check If A File Has Changed In A Script
If I'm at the command line and I want to check if a file has changed, I can run
`git diff` and see what has changed. If I want to be more specific, I can run
`git diff README.md` to see if there are changes to that specific file.
If I'm trying to do this check in a script though, I want the command to clearly
tell the script _Yes_ or _No_. Usually a script looks for an exit code to
determine what path to take. But as long as `git diff` runs successfully,
regardless of whether or not their are changes, it is going to have an
affirmative exit code of `0`.
This is why `git diff` offers the `--exit-code` flag.
> Make the program exit with codes similar to diff(1). That is, it exits with 1
> if there were differences and 0 means no differences.
With that in mind, we can wire up a script with `git diff` that takes different
paths depending on whether or not there are changes.
```bash
if ! git diff --exit-code README.md; then
echo "README.md has changes"
else
echo "README.md is clean"
fi
```
We can take this a step further and instead use the `--quiet` flag.
> Disable all output of the program. Implies --exit-code. Disables execution of
> external diff helpers whose exit code is not trusted
This exhibits the same behavior as `--exit-code` and goes the additional step of
silencing diff output and disabling execution of external diff helpers like
`delta`.
See `man git-diff` for more details.

View File

@@ -0,0 +1,31 @@
# Run A Task If It Meets Criteria
The [Taskfile `status`
directive](https://taskfile.dev/docs/guide#limiting-when-tasks-run) can be used
to tell a task when it needs to run. If it doesn't need to run, it can be
skipped over. The idea being that we're making a status check to see if we're
up-to-date or need to run the task.
For instance, here is a `status` check that determines if there are changes to
commit and push. If there are changes to `NOTES.md`, then we are out-of-date and
need to run the `cmds` that make up the task.
```yaml
notes:push:
desc: Commit and push changes to notes submodule
dir: '{{.NOTES_DIR}}'
cmds:
- git add NOTES.md
- git commit -m "Update notes - $(date '+%Y-%m-%d %H:%M')"
- git push
status:
- git diff --exit-code NOTES.md
silent: false
```
This is useful because I don't want the `git add`, `git commit`, and `git push`
commands to run when there is nothing to do.
Note: this is different from the `preconditions` directive. Instead of
short-circuiting a sequence of tasks, this will either run or skip the task and
move on to the next one.