jj icon indicating copy to clipboard operation
jj copied to clipboard

FR: Make `jj bookmark list <name>` return an error code when nothing is returned

Open hintron opened this issue 5 months ago • 4 comments
trafficstars

Right now, in my jj scripting, I have to do the following to see if an exact branch exists:

            while [ "$(jj bookmark list --all "${BOOKMARK}" | wc -l)" -gt 0 ]; do
                ...

It would be nice if bookmark list returned an error code, so I could just do this:

            while jj bookmark list --all "${BOOKMARK}"; do
                ...

hintron avatar Jun 18 '25 21:06 hintron

It gets a little weird when you use matching: I wouldn't expect jj bookmark list 'glob:feat/*' to fail even if it doesn't match anything. Special-casing might be too magical?

As a hack, you could do

while jj bookmark move "${BOOKMARK}" --from "${BOOKMARK}" --to "${BOOKMARK}"; do
    ....

maybe? (Though I can't guess why you might be looping until a bookmark disappears.)

hotsphink avatar Jun 18 '25 22:06 hotsphink

I'm trying to script finding the next available "checkpoint" branch name I can use when creating a new branch. Here's the full code (that works):

        # Automatically increment bookmark version if needed
        local CHK_PREFIX_BOOKMARK="${CHK_PREFIX}${BOOKMARK_ROOT_NAME}"
        local CHK_BOOKMARK="${CHK_PREFIX_BOOKMARK}_v1" # Start at v1
        # If bookmark exists, increment version
        if [ "$(jj bookmark list --all "glob:${CHK_PREFIX_BOOKMARK}*" | wc -l)" -gt 0 ]; then
            local i=1
            while [ "$(jj bookmark list --all "${CHK_BOOKMARK}" | wc -l)" -gt 0 ]; do
                i=$((i+1))
                CHK_BOOKMARK="${CHK_PREFIX_BOOKMARK}_v${i}"
            done
        fi

My point is just that perhaps jj bookmark list can act more like grep does - if something is found, error code is 0, if something is not found, error code is 1. The I don't need to do the whole subshell with wc -l and then check if it's > 0.

hintron avatar Jun 19 '25 04:06 hintron

jj show <rev> fails with an error code if <rev> doesn't exist. Why shouldn't jj bookmark list <bookmark> also fail when <bookmark> doesn't exist?

hintron avatar Jun 20 '25 19:06 hintron

These commands have different design. jj bookmark list NAME is closer to jj log -r 'bookmarks(NAME)' in a way that both list and log commands show multiple matching entries (including zero entries), and the NAME is evaluated as a pattern.

jj bookmark list NAME could emit a warning if no matching bookmark exists, but I don't think it should exit with non-zero status code.

yuja avatar Jun 21 '25 01:06 yuja