vscode: only overwrite C/C++ settings
[git.git] / contrib / vscode / init.sh
blobba946922681e62814f30ad814a76acfea978f424
1 #!/bin/sh
3 die () {
4 echo "$*" >&2
5 exit 1
8 cd "$(dirname "$0")"/../.. ||
9 die "Could not cd to top-level directory"
11 mkdir -p .vscode ||
12 die "Could not create .vscode/"
14 # General settings
16 cat >.vscode/settings.json.new <<\EOF ||
18 "C_Cpp.intelliSenseEngine": "Default",
19 "C_Cpp.intelliSenseEngineFallback": "Disabled",
20 "files.associations": {
21 "*.h": "c",
22 "*.c": "c"
25 EOF
26 die "Could not write settings.json"
28 # Infer some setup-specific locations/names
30 GCCPATH="$(which gcc)"
31 GDBPATH="$(which gdb)"
32 MAKECOMMAND="make -j5 DEVELOPER=1"
33 OSNAME=
35 case "$(uname -s)" in
36 MINGW*)
37 GCCPATH="$(cygpath -am "$GCCPATH")"
38 GDBPATH="$(cygpath -am "$GDBPATH")"
39 MAKE_BASH="$(cygpath -am /git-cmd.exe) --command=usr\\\\bin\\\\bash.exe"
40 MAKECOMMAND="$MAKE_BASH -lc \\\"$MAKECOMMAND\\\""
41 OSNAME=Win32
42 X=.exe
44 Linux)
45 OSNAME=Linux
47 Darwin)
48 OSNAME=macOS
50 esac
52 # Default build task
54 cat >.vscode/tasks.json.new <<EOF ||
56 // See https://go.microsoft.com/fwlink/?LinkId=733558
57 // for the documentation about the tasks.json format
58 "version": "2.0.0",
59 "tasks": [
61 "label": "make",
62 "type": "shell",
63 "command": "$MAKECOMMAND",
64 "group": {
65 "kind": "build",
66 "isDefault": true
71 EOF
72 die "Could not install default build task"
74 # Debugger settings
76 cat >.vscode/launch.json.new <<EOF ||
78 // Use IntelliSense to learn about possible attributes.
79 // Hover to view descriptions of existing attributes.
80 // For more information, visit:
81 // https://go.microsoft.com/fwlink/?linkid=830387
82 "version": "0.2.0",
83 "configurations": [
85 "name": "(gdb) Launch",
86 "type": "cppdbg",
87 "request": "launch",
88 "program": "\${workspaceFolder}/git$X",
89 "args": [],
90 "stopAtEntry": false,
91 "cwd": "\${workspaceFolder}",
92 "environment": [],
93 "externalConsole": true,
94 "MIMode": "gdb",
95 "miDebuggerPath": "$GDBPATH",
96 "setupCommands": [
98 "description": "Enable pretty-printing for gdb",
99 "text": "-enable-pretty-printing",
100 "ignoreFailures": true
107 die "Could not write launch configuration"
109 # C/C++ extension settings
111 make -f - OSNAME=$OSNAME GCCPATH="$GCCPATH" vscode-init \
112 >.vscode/c_cpp_properties.json <<\EOF ||
113 include Makefile
115 vscode-init:
116 @mkdir -p .vscode && \
117 incs= && defs= && \
118 for e in $(ALL_CFLAGS) \
119 '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
120 '-DGIT_LOCALE_PATH="$(localedir_relative_SQ)"' \
121 '-DBINDIR="$(bindir_relative_SQ)"' \
122 '-DFALLBACK_RUNTIME_PREFIX="$(prefix_SQ)"' \
123 '-DDEFAULT_GIT_TEMPLATE_DIR="$(template_dir_SQ)"' \
124 '-DETC_GITCONFIG="$(ETC_GITCONFIG_SQ)"' \
125 '-DETC_GITATTRIBUTES="$(ETC_GITATTRIBUTES_SQ)"' \
126 '-DGIT_LOCALE_PATH="$(localedir_relative_SQ)"' \
127 '-DCURL_DISABLE_TYPECHECK', \
128 '-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \
129 '-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
130 '-DGIT_INFO_PATH="$(infodir_relative_SQ)"'; do \
131 case "$$e" in \
132 -I.) \
133 incs="$$(printf '% 16s"$${workspaceRoot}",\n%s' \
134 "" "$$incs")" \
135 ;; \
136 -I/*) \
137 incs="$$(printf '% 16s"%s",\n%s' \
138 "" "$${e#-I}" "$$incs")" \
139 ;; \
140 -I*) \
141 incs="$$(printf '% 16s"$${workspaceRoot}/%s",\n%s' \
142 "" "$${e#-I}" "$$incs")" \
143 ;; \
144 -D*) \
145 defs="$$(printf '% 16s"%s",\n%s' \
146 "" "$$(echo "$${e#-D}" | sed 's/"/\\&/g')" \
147 "$$defs")" \
148 ;; \
149 esac; \
150 done && \
151 echo '{' && \
152 echo ' "configurations": [' && \
153 echo ' {' && \
154 echo ' "name": "$(OSNAME)",' && \
155 echo ' "intelliSenseMode": "clang-x64",' && \
156 echo ' "includePath": [' && \
157 echo "$$incs" | sort | sed '$$s/,$$//' && \
158 echo ' ],' && \
159 echo ' "defines": [' && \
160 echo "$$defs" | sort | sed '$$s/,$$//' && \
161 echo ' ],' && \
162 echo ' "browse": {' && \
163 echo ' "limitSymbolsToIncludedHeaders": true,' && \
164 echo ' "databaseFilename": "",' && \
165 echo ' "path": [' && \
166 echo ' "$${workspaceRoot}"' && \
167 echo ' ]' && \
168 echo ' },' && \
169 echo ' "cStandard": "c11",' && \
170 echo ' "cppStandard": "c++17",' && \
171 echo ' "compilerPath": "$(GCCPATH)"' && \
172 echo ' }' && \
173 echo ' ],' && \
174 echo ' "version": 4' && \
175 echo '}'
177 die "Could not write settings for the C/C++ extension"
179 for file in .vscode/settings.json .vscode/tasks.json .vscode/launch.json
181 if test -f $file
182 then
183 if git diff --no-index --quiet --exit-code $file $file.new
184 then
185 rm $file.new
186 else
187 printf "The file $file.new has these changes:\n\n"
188 git --no-pager diff --no-index $file $file.new
189 printf "\n\nMaybe \`mv $file.new $file\`?\n\n"
191 else
192 mv $file.new $file
194 done