Skip to content

Commit 9e08092

Browse files
committed
advice.c: generate "struct advice_setting" from advice.txt
Have generate-advicelist.sh generate the enum label to configuration key mapping used in advice.c, instead of needing to manually maintain the *.txt and *.c versions in sync with one another. As preceding and subsequent commits adding missing documentation show hoping that we can maintain a 1=1 mapping between the two hasn't been going well, by generating the file we ensure that we won't make those mistakes again. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
1 parent 1bab03a commit 9e08092

File tree

5 files changed

+34
-42
lines changed

5 files changed

+34
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
/gitweb/gitweb.cgi
191191
/gitweb/static/gitweb.js
192192
/gitweb/static/gitweb.min.*
193+
/advice-config.h
193194
/advice-type.h
194195
/config-list.h
195196
/command-list.h

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ REFTABLE_TEST_LIB = reftable/libreftable_test.a
932932
# Generated headers. The "COMMON_GENERATED_H" will be added as
933933
# implicit dependencies on fresh builds (without existing dep/*.mak
934934
# files)
935+
GENERATED_ADVICE_H += advice-config.h
935936
GENERATED_ADVICE_H += advice-type.h
936937

937938
COMMON_GENERATED_H += $(GENERATED_ADVICE_H)

advice.c

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "cache.h"
2+
#include "advice.h"
3+
#include "advice-config.h"
24
#include "config.h"
35
#include "color.h"
46
#include "help.h"
@@ -31,48 +33,6 @@ static const char *advise_get_color(enum color_advice ix)
3133
return "";
3234
}
3335

34-
static struct {
35-
const char *key;
36-
int enabled;
37-
} advice_setting[] = {
38-
[ADVICE_ADD_EMBEDDED_REPO] = { "addEmbeddedRepo", 1 },
39-
[ADVICE_ADD_EMPTY_PATHSPEC] = { "addEmptyPathspec", 1 },
40-
[ADVICE_ADD_IGNORED_FILE] = { "addIgnoredFile", 1 },
41-
[ADVICE_AM_WORK_DIR] = { "amWorkDir", 1 },
42-
[ADVICE_AMBIGUOUS_FETCH_REFSPEC] = { "ambiguousFetchRefspec", 1 },
43-
[ADVICE_BRANCH_SET_UPSTREAM_FAILURE] = { "branchSetUpstreamFailure", 1 },
44-
[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName", 1 },
45-
[ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge", 1 },
46-
[ADVICE_DETACHED_HEAD] = { "detachedHead", 1 },
47-
[ADVICE_SUGGEST_DETACHING_HEAD] = { "suggestDetachingHead", 1 },
48-
[ADVICE_FETCH_SHOW_FORCED_UPDATES] = { "fetchShowForcedUpdates", 1 },
49-
[ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated", 1 },
50-
[ADVICE_IGNORED_HOOK] = { "ignoredHook", 1 },
51-
[ADVICE_IMPLICIT_IDENTITY] = { "implicitIdentity", 1 },
52-
[ADVICE_NESTED_TAG] = { "nestedTag", 1 },
53-
[ADVICE_OBJECT_NAME_WARNING] = { "objectNameWarning", 1 },
54-
[ADVICE_PUSH_ALREADY_EXISTS] = { "pushAlreadyExists", 1 },
55-
[ADVICE_PUSH_FETCH_FIRST] = { "pushFetchFirst", 1 },
56-
[ADVICE_PUSH_NEEDS_FORCE] = { "pushNeedsForce", 1 },
57-
[ADVICE_PUSH_REF_NEEDS_UPDATE] = { "pushRefNeedsUpdate", 1 },
58-
[ADVICE_PUSH_NON_FF_CURRENT] = { "pushNonFFCurrent", 1 },
59-
[ADVICE_PUSH_NON_FF_MATCHING] = { "pushNonFFMatching", 1 },
60-
[ADVICE_PUSH_UNQUALIFIED_REFNAME] = { "pushUnqualifiedRefName", 1 },
61-
[ADVICE_PUSH_UPDATE_REJECTED] = { "pushUpdateRejected", 1 },
62-
[ADVICE_RESET_NO_REFRESH] = { "resetNoRefresh", 1 },
63-
[ADVICE_RESOLVE_CONFLICT] = { "resolveConflict", 1 },
64-
[ADVICE_RM_HINTS] = { "rmHints", 1 },
65-
[ADVICE_SEQUENCER_IN_USE] = { "sequencerInUse", 1 },
66-
[ADVICE_SKIPPED_CHERRY_PICKS] = { "skippedCherryPicks", 1 },
67-
[ADVICE_STATUS_AHEAD_BEHIND] = { "statusAheadBehind", 1 },
68-
[ADVICE_STATUS_HINTS] = { "statusHints", 1 },
69-
[ADVICE_STATUS_UOPTION] = { "statusUoption", 1 },
70-
[ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 },
71-
[ADVICE_SUBMODULES_NOT_UPDATED] = { "submodulesNotUpdated", 1 },
72-
[ADVICE_UPDATE_SPARSE_PATH] = { "updateSparsePath", 1 },
73-
[ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 },
74-
};
75-
7636
static const char turn_off_instructions[] =
7737
N_("\n"
7838
"Disable this message with \"git config advice.%s false\"");

contrib/buildsystems/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,13 @@ if(NOT EXISTS ${CMAKE_BINARY_DIR}/advice-type.h)
678678
OUTPUT_FILE ${CMAKE_BINARY_DIR}/advice-type.h)
679679
endif()
680680

681+
if(NOT EXISTS ${CMAKE_BINARY_DIR}/advice-config.h)
682+
message("Generating advice-config.h")
683+
execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-advice.sh advice-config.h
684+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
685+
OUTPUT_FILE ${CMAKE_BINARY_DIR}/advice-config.h)
686+
endif()
687+
681688
include_directories(${CMAKE_BINARY_DIR})
682689

683690
#build

generate-advice.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ advice_labels () {
2525
txt2label
2626
}
2727

28+
advice_labels_to_config () {
29+
advice_list |
30+
while read line
31+
do
32+
label=$(echo "$line" | txt2label)
33+
printf "[%s] = { \"%s\", 1 }\n" "$label" "$line"
34+
done
35+
}
36+
2837
listify () {
2938
sed \
3039
-e "2,\$s/^/$HT/" \
@@ -35,6 +44,7 @@ case "$#" in
3544
1) ;;
3645
*)
3746
echo "usage: $0 advice-type.h >advice-type.h"
47+
echo " or: $0 advice-config.h >advice-config.h"
3848
exit 1
3949
esac
4050

@@ -47,6 +57,19 @@ enum advice_type {
4757
/* Auto-generated from Documentation/config/advice.txt */
4858
$(advice_labels | listify)
4959
};
60+
EOF
61+
;;
62+
advice-config.h)
63+
cat <<EOF
64+
/* Automatically generated by generate-advice.sh */
65+
struct advice_entry {
66+
const char *key;
67+
int enabled;
68+
};
69+
70+
static struct advice_entry advice_setting[] = {
71+
$(advice_labels_to_config | listify)
72+
};
5073
EOF
5174
;;
5275
*)

0 commit comments

Comments
 (0)