Skip to content

Add on-load hook#2010

Merged
joelim-work merged 1 commit into
gokcehan:masterfrom
CatsDeservePets:feat/on-load
Jun 20, 2025
Merged

Add on-load hook#2010
joelim-work merged 1 commit into
gokcehan:masterfrom
CatsDeservePets:feat/on-load

Conversation

@CatsDeservePets

Copy link
Copy Markdown
Collaborator

This pull request adds the on-load hook command as discussed in #1994.

I will provide examples later in combination with addcustominfo.

@CatsDeservePets CatsDeservePets force-pushed the feat/on-load branch 2 times, most recently from f99ee20 to 1e87771 Compare June 15, 2025 03:50
Comment thread app.go Outdated
Comment thread doc.md Outdated
Comment thread eval.go Outdated
Comment thread lf.1
@CatsDeservePets

Copy link
Copy Markdown
Collaborator Author

@joelim-work Please take a look again.

@joelim-work joelim-work left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for the changes

@joelim-work joelim-work merged commit ff0c7f1 into gokcehan:master Jun 20, 2025
4 checks passed
@joelim-work joelim-work added this to the r36 milestone Jun 20, 2025
@joelim-work joelim-work added the new Pull requests that add new behavior label Jun 20, 2025
@joelim-work

joelim-work commented Jun 20, 2025

Copy link
Copy Markdown
Collaborator

Hi @CatsDeservePets I was testing this with the watch setting enabled. Apparently calling git status creates an index.lock file in the .git directory, causing it to be reloaded, thus triggering the on-init hook command again and causing an infinite loop.

Creating the index.lock file will trigger two reloads:

  • /path/to/repo/.git will be reloaded as index.lock is now contained in that directory
  • /path/to/repo will also be updated as the .git file in it has been modified

To handle this, I think the example on-load hook will need to have the following changes:

  • Exit if the directory that is being loaded is a .git directory
  • Skip the .git directory when looping through files to call git status --porcelain on

EDIT: I ended up doing something like this:

set info custom:size
set watch true

cmd on-load &{{
    dir="$(dirname "$1")"
    [ "$(basename "$dir")" != .git ] || exit 0

    cd "$dir" || exit 1
    git rev-parse >/dev/null 2>&1 || exit 0

    cmds=""

    for file in "$@"; do
        [ "$(basename "$file")" != .git ] || continue

        status=$(git status --porcelain --ignored -- "$file" | cut -c1-2 | head -n1)

        if [ -n "$status" ]; then
            cmds="${cmds}addcustominfo ${file} \"$status\"; "
        else
            cmds="${cmds}addcustominfo ${file} ''; "
        fi
    done

    lf -remote "send $id :$cmds"
}}

@CatsDeservePets

Copy link
Copy Markdown
Collaborator Author

@joelim-work
This is my approach. I wanted to make sure that also sub-directories of .git get ignored.

cmd on-load &{{
	cd "$(dirname "$1")" || exit 1
	[ "$(git rev-parse --is-inside-git-dir 2>/dev/null)" = false ] || exit 0

	cmds=""
	for file in "$@"; do
		case "$file" in
			*/.git|*/.git/*) continue;;
		esac
		status=$(git status --porcelain --ignored -- "$file" | cut -c1-2 | head -n1)
		if [ -n "$status" ]; then
			cmds="${cmds}addcustominfo ${file} \"$status\"; "
		else
			cmds="${cmds}addcustominfo ${file} ''; "
		fi
	done

	lf -remote "send $id :$cmds"
}}

@joelim-work

Copy link
Copy Markdown
Collaborator

@CatsDeservePets Nice, I didn't know about --is-inside-git-dir, it looks very useful.

Anyway I've added your changes in #2016, thanks once again.

@CatsDeservePets CatsDeservePets deleted the feat/on-load branch June 30, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new Pull requests that add new behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants