5 ms·
Test && (commit || revert)? Or (test && commit) || revert?
by boffinism 8y ago
Test && (commit || revert)?
Or (test && commit) || revert?
- CGamesPlay 8y agoAssuming it's shell, they are always processed in left-to-right order, so it's (A && B) || C, or (A || B) && C, with no proper order of operations. In this case, it's closer to test ? commit : revert though since the commit can't fail (but that's not a bash operation)
- Spivak 8y agotest && commit || revert is grouped like test × commit + revert so it would be the latter.
- kazinator 8y agoIn C and derived languages, this is true; not in the POSIX shell, though, where these are on the same precedence level, and associate left to right. $ echo 4 || echo 5 4 $ echo 4 || false && echo 5 4 5 This is because the grouping is (echo 4 || false) && echo 5.
- adrianmonk 8y agoI'm thinking they mean the Bourne(-style) shell, where this syntax means that test is run, either commit or revert is run. That is, it is a shorthand for: if test then commit else revert fi Presumably the point is to encourage you to keep your code changes and tests so small that you are OK with losing work.
- mikelward 8y agoIt's actually equivalent to if test; then commit || revert else revert fi Or in another language: try { test commit } catch { revert } This is because test && commit is evaluated, and the resulting value is the left side of expr || revert Not sure if that was intentional, but it's a very nice one liner assuming either your commit never fails, or your commit runs extra tests, and you want to revert if those fail.
- mikelward 8y agoThe latter. Including reverting if commit fails. I'm not sure if they meant that literally, but it could actually be useful to revert if commit fails, since commit might have hooks that run extra tests.