perf/run: add get_var_from_env_or_config()

Add get_var_from_env_or_config() to easily set variables from a config file if they are defined there and not already set. This can also set them to a default value if one is provided. As an example, use this function to set GIT_PERF_REPEAT_COUNT from the perf.repeatCount config option or from the default value. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Sep 23, 2017 at 19:55 UTC e6b71539dedc13a5737ab97cf54b9f41f94cac24
2 files changed +21 -3
t/perf/perf-lib.sh
-3
@@ -59,9 +59,6 @@ perf_results_dir=$TEST_OUTPUT_DIRECTORY/test-results
59 mkdir -p "$perf_results_dir"
60 rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
61
62 -if test -z "$GIT_PERF_REPEAT_COUNT"; then
63 - GIT_PERF_REPEAT_COUNT=3
64 -fi
62 die_if_build_dir_not_repo () {
63 if ! ( cd "$TEST_DIRECTORY/.." &&
64 git rev-parse --build-dir >/dev/null 2>&1 ); then
t/perf/run
+21
@@ -34,6 +34,7 @@ unpack_git_rev () {
34 (cd "$(git rev-parse --show-cdup)" && git archive --format=tar $rev) |
35 (cd build/$rev && tar x)
36 }
37 +
38 build_git_rev () {
39 rev=$1
40 for config in config.mak config.mak.autogen config.status
@@ -92,6 +93,26 @@ run_dirs () {
93 done
94 }
95
96 +get_var_from_env_or_config () {
97 + env_var="$1"
98 + conf_var="$2"
99 + # $3 can be set to a default value
100 +
101 + # Do nothing if the env variable is already set
102 + eval "test -z \"\${$env_var+x}\"" || return
103 +
104 + # Check if the variable is in the config file
105 + test -n "$GIT_PERF_CONFIG_FILE" &&
106 + conf_value=$(git config -f "$GIT_PERF_CONFIG_FILE" "$conf_var") &&
107 + eval "$env_var=\"$conf_value\"" || {
108 + test -n "${3+x}" &&
109 + eval "$env_var=\"$3\""
110 + }
111 +}
112 +
113 +get_var_from_env_or_config "GIT_PERF_REPEAT_COUNT" "perf.repeatCount" 3
114 +export GIT_PERF_REPEAT_COUNT
115 +
116 GIT_PERF_AGGREGATING_LATER=t
117 export GIT_PERF_AGGREGATING_LATER
118