jenkinsci/jenkins — pull request #986 by ikedam (2013-10-26 19:21:49+00:00)
Title: [JENKINS-20280] Improved variable overriding.

This is an implementation for [JENKINS-20280] (https://issues.jenkins-ci.org/browse/JENKINS-20280).

When defining environment variables (as in system configuration, node configuration and job configuration), it is expected to support following features:
1. expansion: A=foo, B=bar, C=${A}${B}
2. extension: PATH=/some/other/path:${PATH}

The former one was not fully supported till fix in [JENKINS-19488] (https://issues.jenkins-ci.org/browse/JENKINS-20280), 3eea39648943583a530f47f69f93737266537d9d. The root cause was that, it depended on expansion order: when processed in the order of A, C, B, ${B} is not able to be expanded evaluating C.

The latter one was broken with fix for [JENKINS-19488] (https://issues.jenkins-ci.org/browse/JENKINS-20280), and fixed in [JENKINS-19926] (https://issues.jenkins-ci.org/browse/JENKINS-19926), b95f2694529d22523701e8fe20f4f20b2f4d9fc4.

But these fixes seem done in a very ad-hoc way.

This request fixes these problems in a more general way, calculating a proper order to override variables.
For example, if there are following overriding variables:
* A=foobar
* B=${C}
* C=${A}
this patch overrides variables in the following order, which allows proper variable expansions:
* A=foobar
* C=${A}
* B=${C}

This is done by scanning and counting referring variables. With the above example, it is evaluated like this:

|Variable|Referring variables|number of referring variables|
|:-------|:------------------|:----------------------------|
|A |- |0 |
|B |A, C |2 |
|C |A |1 |

then sort them in an ascending order of the number of referring variables, and apply overriding in that order.