jj jj jj jj jj

jj jj jj jj jj

In a similar vein to git git git git git, if you’re using jj and find you’ve accidentally typed one jj too many in the command line, it errors at you. In this case we’re trying to pull the id of the current changeset we’re editing: 与 git git git git git 的情况类似,如果你在使用 jj 时不小心在命令行多打了一个 jj,它就会报错。例如,当我们尝试获取当前正在编辑的变更集(changeset)ID 时:

$ jj jj show -T ‘change_id.short()’ error: unrecognized subcommand ‘jj’ Usage: jj [OPTIONS] For more information, try ‘—help’.

How annoying. Now we could solve this with a shell alias, but jj also allows us to define alias’ in the configuration (much like git!), and combined with jj util exec we should be able to have it run the rest of the line nay bother. 真烦人。虽然我们可以通过 shell 别名来解决这个问题,但 jj 也允许我们在配置文件中定义别名(就像 git 一样!),结合 jj util exec,我们应该能够让它直接运行剩余的命令而无需担心。

Thinking it through, the initial jj will be exec’d by the shell, so that’s gone already. The second jj is then evaluated as the alias, so that’s gone too. The remaining arguments passed to the alias are [“show”, “-T”, “change_id.short()”] so if we just exec those it’ll error trying to run show as a binary. We can avoid that by having it exec jj then the arguments from the alias. 仔细思考一下,第一个 jj 会由 shell 执行,所以它已经被消耗掉了。第二个 jj 会被解析为别名,所以它也被消耗掉了。传递给别名的剩余参数是 ["show", "-T", "change_id.short()"],如果我们直接执行这些参数,它会因为试图将 show 当作二进制文件运行而报错。我们可以通过让它先执行 jj,然后再执行别名中的参数来避免这种情况。

Drop the alias in a jj config file (jj config edit is super useful for editing these, or there’s jj config set to update it from the shell) and then we can test it works. 将别名放入 jj 配置文件中(jj config edit 对于编辑这些配置非常有用,或者也可以使用 jj config set 从 shell 更新),然后我们就可以测试它是否生效了。

[aliases]

jj all the way down

jj = [“util”, “exec”, “jj”]

Lets try our original nested command again and see if we get to see our sweet sweet change id: 让我们再次尝试最初的嵌套命令,看看能否看到我们心心念念的变更 ID:

$ jj jj show -T ‘change_id.short()’ error: unexpected argument ‘-T’ found tip: to pass ‘-T’ as a value, use ’— -T’ Usage: jj util exec [OPTIONS] [ARGS]… For more information, try ‘—help’.

Oh. Our -T argument is being interpreted by jj util exec, not by the jj command we’re running. Classic nested argument parsing when you’re trying to invoke another command. Luckily shell/exec has a solution to this, passing — as an argument stops parsing all remaining arguments so they are passed along verbatim. 噢。我们的 -T 参数被 jj util exec 解析了,而不是被我们正在运行的 jj 命令解析。这是在尝试调用另一个命令时常见的嵌套参数解析问题。幸运的是,shell/exec 有一个解决方案:将 -- 作为参数传递,可以停止对所有剩余参数的解析,从而使它们被原样传递。

With that in mind we can amend our alias to run jj util exec and then stop interpreting any further arguments. 考虑到这一点,我们可以修改别名,让它运行 jj util exec,然后停止解析任何后续参数。

[aliases]

jj all the way down

jj = [“util”, “exec”, ”—”, “jj”]

Now we should be able to see our change id without any issues, no matter how many nested jjs there are in the command! 现在,无论命令中有多少个嵌套的 jj,我们都应该能够顺利看到我们的变更 ID 了!

$ jj show -T ‘change_id.short()’ upvqxuzzvxtx $ jj jj show -T ‘change_id.short()’ upvqxuzzvxtx $ jj jj jj jj jj show -T ‘change_id.short()’ upvqxuzzvxtx $ jj jj jj jj jj jj jj jj jj jj jj jj jj jj jj jj jj show -T ‘change_id.short()’ upvqxuzzvxtx

Happy committing. 提交愉快。