config.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. * Copyright (C) Johannes Schindelin, 2005
  6. *
  7. */
  8. #include "cache.h"
  9. #include "branch.h"
  10. #include "config.h"
  11. #include "repository.h"
  12. #include "lockfile.h"
  13. #include "exec-cmd.h"
  14. #include "strbuf.h"
  15. #include "quote.h"
  16. #include "hashmap.h"
  17. #include "string-list.h"
  18. #include "object-store.h"
  19. #include "utf8.h"
  20. #include "dir.h"
  21. #include "color.h"
  22. #include "refs.h"
  23. struct config_source {
  24. struct config_source *prev;
  25. union {
  26. FILE *file;
  27. struct config_buf {
  28. const char *buf;
  29. size_t len;
  30. size_t pos;
  31. } buf;
  32. } u;
  33. enum config_origin_type origin_type;
  34. const char *name;
  35. const char *path;
  36. enum config_error_action default_error_action;
  37. int linenr;
  38. int eof;
  39. size_t total_len;
  40. struct strbuf value;
  41. struct strbuf var;
  42. unsigned subsection_case_sensitive : 1;
  43. int (*do_fgetc)(struct config_source *c);
  44. int (*do_ungetc)(int c, struct config_source *conf);
  45. long (*do_ftell)(struct config_source *c);
  46. };
  47. /*
  48. * These variables record the "current" config source, which
  49. * can be accessed by parsing callbacks.
  50. *
  51. * The "cf" variable will be non-NULL only when we are actually parsing a real
  52. * config source (file, blob, cmdline, etc).
  53. *
  54. * The "current_config_kvi" variable will be non-NULL only when we are feeding
  55. * cached config from a configset into a callback.
  56. *
  57. * They should generally never be non-NULL at the same time. If they are both
  58. * NULL, then we aren't parsing anything (and depending on the function looking
  59. * at the variables, it's either a bug for it to be called in the first place,
  60. * or it's a function which can be reused for non-config purposes, and should
  61. * fall back to some sane behavior).
  62. */
  63. static struct config_source *cf;
  64. static struct key_value_info *current_config_kvi;
  65. /*
  66. * Similar to the variables above, this gives access to the "scope" of the
  67. * current value (repo, global, etc). For cached values, it can be found via
  68. * the current_config_kvi as above. During parsing, the current value can be
  69. * found in this variable. It's not part of "cf" because it transcends a single
  70. * file (i.e., a file included from .git/config is still in "repo" scope).
  71. */
  72. static enum config_scope current_parsing_scope;
  73. static int core_compression_seen;
  74. static int pack_compression_seen;
  75. static int zlib_compression_seen;
  76. static int config_file_fgetc(struct config_source *conf)
  77. {
  78. return getc_unlocked(conf->u.file);
  79. }
  80. static int config_file_ungetc(int c, struct config_source *conf)
  81. {
  82. return ungetc(c, conf->u.file);
  83. }
  84. static long config_file_ftell(struct config_source *conf)
  85. {
  86. return ftell(conf->u.file);
  87. }
  88. static int config_buf_fgetc(struct config_source *conf)
  89. {
  90. if (conf->u.buf.pos < conf->u.buf.len)
  91. return conf->u.buf.buf[conf->u.buf.pos++];
  92. return EOF;
  93. }
  94. static int config_buf_ungetc(int c, struct config_source *conf)
  95. {
  96. if (conf->u.buf.pos > 0) {
  97. conf->u.buf.pos--;
  98. if (conf->u.buf.buf[conf->u.buf.pos] != c)
  99. BUG("config_buf can only ungetc the same character");
  100. return c;
  101. }
  102. return EOF;
  103. }
  104. static long config_buf_ftell(struct config_source *conf)
  105. {
  106. return conf->u.buf.pos;
  107. }
  108. #define MAX_INCLUDE_DEPTH 10
  109. static const char include_depth_advice[] = N_(
  110. "exceeded maximum include depth (%d) while including\n"
  111. " %s\n"
  112. "from\n"
  113. " %s\n"
  114. "This might be due to circular includes.");
  115. static int handle_path_include(const char *path, struct config_include_data *inc)
  116. {
  117. int ret = 0;
  118. struct strbuf buf = STRBUF_INIT;
  119. char *expanded;
  120. if (!path)
  121. return config_error_nonbool("include.path");
  122. expanded = expand_user_path(path, 0);
  123. if (!expanded)
  124. return error(_("could not expand include path '%s'"), path);
  125. path = expanded;
  126. /*
  127. * Use an absolute path as-is, but interpret relative paths
  128. * based on the including config file.
  129. */
  130. if (!is_absolute_path(path)) {
  131. char *slash;
  132. if (!cf || !cf->path)
  133. return error(_("relative config includes must come from files"));
  134. slash = find_last_dir_sep(cf->path);
  135. if (slash)
  136. strbuf_add(&buf, cf->path, slash - cf->path + 1);
  137. strbuf_addstr(&buf, path);
  138. path = buf.buf;
  139. }
  140. if (!access_or_die(path, R_OK, 0)) {
  141. if (++inc->depth > MAX_INCLUDE_DEPTH)
  142. die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path,
  143. !cf ? "<unknown>" :
  144. cf->name ? cf->name :
  145. "the command line");
  146. ret = git_config_from_file(git_config_include, path, inc);
  147. inc->depth--;
  148. }
  149. strbuf_release(&buf);
  150. free(expanded);
  151. return ret;
  152. }
  153. static void add_trailing_starstar_for_dir(struct strbuf *pat)
  154. {
  155. if (pat->len && is_dir_sep(pat->buf[pat->len - 1]))
  156. strbuf_addstr(pat, "**");
  157. }
  158. static int prepare_include_condition_pattern(struct strbuf *pat)
  159. {
  160. struct strbuf path = STRBUF_INIT;
  161. char *expanded;
  162. int prefix = 0;
  163. expanded = expand_user_path(pat->buf, 1);
  164. if (expanded) {
  165. strbuf_reset(pat);
  166. strbuf_addstr(pat, expanded);
  167. free(expanded);
  168. }
  169. if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) {
  170. const char *slash;
  171. if (!cf || !cf->path)
  172. return error(_("relative config include "
  173. "conditionals must come from files"));
  174. strbuf_realpath(&path, cf->path, 1);
  175. slash = find_last_dir_sep(path.buf);
  176. if (!slash)
  177. BUG("how is this possible?");
  178. strbuf_splice(pat, 0, 1, path.buf, slash - path.buf);
  179. prefix = slash - path.buf + 1 /* slash */;
  180. } else if (!is_absolute_path(pat->buf))
  181. strbuf_insertstr(pat, 0, "**/");
  182. add_trailing_starstar_for_dir(pat);
  183. strbuf_release(&path);
  184. return prefix;
  185. }
  186. static int include_by_gitdir(const struct config_options *opts,
  187. const char *cond, size_t cond_len, int icase)
  188. {
  189. struct strbuf text = STRBUF_INIT;
  190. struct strbuf pattern = STRBUF_INIT;
  191. int ret = 0, prefix;
  192. const char *git_dir;
  193. int already_tried_absolute = 0;
  194. if (opts->git_dir)
  195. git_dir = opts->git_dir;
  196. else
  197. goto done;
  198. strbuf_realpath(&text, git_dir, 1);
  199. strbuf_add(&pattern, cond, cond_len);
  200. prefix = prepare_include_condition_pattern(&pattern);
  201. again:
  202. if (prefix < 0)
  203. goto done;
  204. if (prefix > 0) {
  205. /*
  206. * perform literal matching on the prefix part so that
  207. * any wildcard character in it can't create side effects.
  208. */
  209. if (text.len < prefix)
  210. goto done;
  211. if (!icase && strncmp(pattern.buf, text.buf, prefix))
  212. goto done;
  213. if (icase && strncasecmp(pattern.buf, text.buf, prefix))
  214. goto done;
  215. }
  216. ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
  217. WM_PATHNAME | (icase ? WM_CASEFOLD : 0));
  218. if (!ret && !already_tried_absolute) {
  219. /*
  220. * We've tried e.g. matching gitdir:~/work, but if
  221. * ~/work is a symlink to /mnt/storage/work
  222. * strbuf_realpath() will expand it, so the rule won't
  223. * match. Let's match against a
  224. * strbuf_add_absolute_path() version of the path,
  225. * which'll do the right thing
  226. */
  227. strbuf_reset(&text);
  228. strbuf_add_absolute_path(&text, git_dir);
  229. already_tried_absolute = 1;
  230. goto again;
  231. }
  232. done:
  233. strbuf_release(&pattern);
  234. strbuf_release(&text);
  235. return ret;
  236. }
  237. static int include_by_branch(const char *cond, size_t cond_len)
  238. {
  239. int flags;
  240. int ret;
  241. struct strbuf pattern = STRBUF_INIT;
  242. const char *refname = !the_repository->gitdir ?
  243. NULL : resolve_ref_unsafe("HEAD", 0, NULL, &flags);
  244. const char *shortname;
  245. if (!refname || !(flags & REF_ISSYMREF) ||
  246. !skip_prefix(refname, "refs/heads/", &shortname))
  247. return 0;
  248. strbuf_add(&pattern, cond, cond_len);
  249. add_trailing_starstar_for_dir(&pattern);
  250. ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME);
  251. strbuf_release(&pattern);
  252. return ret;
  253. }
  254. static int include_condition_is_true(const struct config_options *opts,
  255. const char *cond, size_t cond_len)
  256. {
  257. if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
  258. return include_by_gitdir(opts, cond, cond_len, 0);
  259. else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
  260. return include_by_gitdir(opts, cond, cond_len, 1);
  261. else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
  262. return include_by_branch(cond, cond_len);
  263. /* unknown conditionals are always false */
  264. return 0;
  265. }
  266. int git_config_include(const char *var, const char *value, void *data)
  267. {
  268. struct config_include_data *inc = data;
  269. const char *cond, *key;
  270. size_t cond_len;
  271. int ret;
  272. /*
  273. * Pass along all values, including "include" directives; this makes it
  274. * possible to query information on the includes themselves.
  275. */
  276. ret = inc->fn(var, value, inc->data);
  277. if (ret < 0)
  278. return ret;
  279. if (!strcmp(var, "include.path"))
  280. ret = handle_path_include(value, inc);
  281. if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
  282. (cond && include_condition_is_true(inc->opts, cond, cond_len)) &&
  283. !strcmp(key, "path"))
  284. ret = handle_path_include(value, inc);
  285. return ret;
  286. }
  287. void git_config_push_parameter(const char *text)
  288. {
  289. struct strbuf env = STRBUF_INIT;
  290. const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
  291. if (old && *old) {
  292. strbuf_addstr(&env, old);
  293. strbuf_addch(&env, ' ');
  294. }
  295. sq_quote_buf(&env, text);
  296. setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
  297. strbuf_release(&env);
  298. }
  299. static inline int iskeychar(int c)
  300. {
  301. return isalnum(c) || c == '-';
  302. }
  303. /*
  304. * Auxiliary function to sanity-check and split the key into the section
  305. * identifier and variable name.
  306. *
  307. * Returns 0 on success, -1 when there is an invalid character in the key and
  308. * -2 if there is no section name in the key.
  309. *
  310. * store_key - pointer to char* which will hold a copy of the key with
  311. * lowercase section and variable name
  312. * baselen - pointer to size_t which will hold the length of the
  313. * section + subsection part, can be NULL
  314. */
  315. static int git_config_parse_key_1(const char *key, char **store_key, size_t *baselen_, int quiet)
  316. {
  317. size_t i, baselen;
  318. int dot;
  319. const char *last_dot = strrchr(key, '.');
  320. /*
  321. * Since "key" actually contains the section name and the real
  322. * key name separated by a dot, we have to know where the dot is.
  323. */
  324. if (last_dot == NULL || last_dot == key) {
  325. if (!quiet)
  326. error(_("key does not contain a section: %s"), key);
  327. return -CONFIG_NO_SECTION_OR_NAME;
  328. }
  329. if (!last_dot[1]) {
  330. if (!quiet)
  331. error(_("key does not contain variable name: %s"), key);
  332. return -CONFIG_NO_SECTION_OR_NAME;
  333. }
  334. baselen = last_dot - key;
  335. if (baselen_)
  336. *baselen_ = baselen;
  337. /*
  338. * Validate the key and while at it, lower case it for matching.
  339. */
  340. if (store_key)
  341. *store_key = xmallocz(strlen(key));
  342. dot = 0;
  343. for (i = 0; key[i]; i++) {
  344. unsigned char c = key[i];
  345. if (c == '.')
  346. dot = 1;
  347. /* Leave the extended basename untouched.. */
  348. if (!dot || i > baselen) {
  349. if (!iskeychar(c) ||
  350. (i == baselen + 1 && !isalpha(c))) {
  351. if (!quiet)
  352. error(_("invalid key: %s"), key);
  353. goto out_free_ret_1;
  354. }
  355. c = tolower(c);
  356. } else if (c == '\n') {
  357. if (!quiet)
  358. error(_("invalid key (newline): %s"), key);
  359. goto out_free_ret_1;
  360. }
  361. if (store_key)
  362. (*store_key)[i] = c;
  363. }
  364. return 0;
  365. out_free_ret_1:
  366. if (store_key) {
  367. FREE_AND_NULL(*store_key);
  368. }
  369. return -CONFIG_INVALID_KEY;
  370. }
  371. int git_config_parse_key(const char *key, char **store_key, size_t *baselen)
  372. {
  373. return git_config_parse_key_1(key, store_key, baselen, 0);
  374. }
  375. int git_config_key_is_valid(const char *key)
  376. {
  377. return !git_config_parse_key_1(key, NULL, NULL, 1);
  378. }
  379. int git_config_parse_parameter(const char *text,
  380. config_fn_t fn, void *data)
  381. {
  382. const char *value;
  383. char *canonical_name;
  384. struct strbuf **pair;
  385. int ret;
  386. pair = strbuf_split_str(text, '=', 2);
  387. if (!pair[0])
  388. return error(_("bogus config parameter: %s"), text);
  389. if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
  390. strbuf_setlen(pair[0], pair[0]->len - 1);
  391. value = pair[1] ? pair[1]->buf : "";
  392. } else {
  393. value = NULL;
  394. }
  395. strbuf_trim(pair[0]);
  396. if (!pair[0]->len) {
  397. strbuf_list_free(pair);
  398. return error(_("bogus config parameter: %s"), text);
  399. }
  400. if (git_config_parse_key(pair[0]->buf, &canonical_name, NULL)) {
  401. ret = -1;
  402. } else {
  403. ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
  404. free(canonical_name);
  405. }
  406. strbuf_list_free(pair);
  407. return ret;
  408. }
  409. int git_config_from_parameters(config_fn_t fn, void *data)
  410. {
  411. const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
  412. int ret = 0;
  413. char *envw;
  414. const char **argv = NULL;
  415. int nr = 0, alloc = 0;
  416. int i;
  417. struct config_source source;
  418. if (!env)
  419. return 0;
  420. memset(&source, 0, sizeof(source));
  421. source.prev = cf;
  422. source.origin_type = CONFIG_ORIGIN_CMDLINE;
  423. cf = &source;
  424. /* sq_dequote will write over it */
  425. envw = xstrdup(env);
  426. if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
  427. ret = error(_("bogus format in %s"), CONFIG_DATA_ENVIRONMENT);
  428. goto out;
  429. }
  430. for (i = 0; i < nr; i++) {
  431. if (git_config_parse_parameter(argv[i], fn, data) < 0) {
  432. ret = -1;
  433. goto out;
  434. }
  435. }
  436. out:
  437. free(argv);
  438. free(envw);
  439. cf = source.prev;
  440. return ret;
  441. }
  442. static int get_next_char(void)
  443. {
  444. int c = cf->do_fgetc(cf);
  445. if (c == '\r') {
  446. /* DOS like systems */
  447. c = cf->do_fgetc(cf);
  448. if (c != '\n') {
  449. if (c != EOF)
  450. cf->do_ungetc(c, cf);
  451. c = '\r';
  452. }
  453. }
  454. if (c != EOF && ++cf->total_len > INT_MAX) {
  455. /*
  456. * This is an absurdly long config file; refuse to parse
  457. * further in order to protect downstream code from integer
  458. * overflows. Note that we can't return an error specifically,
  459. * but we can mark EOF and put trash in the return value,
  460. * which will trigger a parse error.
  461. */
  462. cf->eof = 1;
  463. return 0;
  464. }
  465. if (c == '\n')
  466. cf->linenr++;
  467. if (c == EOF) {
  468. cf->eof = 1;
  469. cf->linenr++;
  470. c = '\n';
  471. }
  472. return c;
  473. }
  474. static char *parse_value(void)
  475. {
  476. int quote = 0, comment = 0, space = 0;
  477. strbuf_reset(&cf->value);
  478. for (;;) {
  479. int c = get_next_char();
  480. if (c == '\n') {
  481. if (quote) {
  482. cf->linenr--;
  483. return NULL;
  484. }
  485. return cf->value.buf;
  486. }
  487. if (comment)
  488. continue;
  489. if (isspace(c) && !quote) {
  490. if (cf->value.len)
  491. space++;
  492. continue;
  493. }
  494. if (!quote) {
  495. if (c == ';' || c == '#') {
  496. comment = 1;
  497. continue;
  498. }
  499. }
  500. for (; space; space--)
  501. strbuf_addch(&cf->value, ' ');
  502. if (c == '\\') {
  503. c = get_next_char();
  504. switch (c) {
  505. case '\n':
  506. continue;
  507. case 't':
  508. c = '\t';
  509. break;
  510. case 'b':
  511. c = '\b';
  512. break;
  513. case 'n':
  514. c = '\n';
  515. break;
  516. /* Some characters escape as themselves */
  517. case '\\': case '"':
  518. break;
  519. /* Reject unknown escape sequences */
  520. default:
  521. return NULL;
  522. }
  523. strbuf_addch(&cf->value, c);
  524. continue;
  525. }
  526. if (c == '"') {
  527. quote = 1-quote;
  528. continue;
  529. }
  530. strbuf_addch(&cf->value, c);
  531. }
  532. }
  533. static int get_value(config_fn_t fn, void *data, struct strbuf *name)
  534. {
  535. int c;
  536. char *value;
  537. int ret;
  538. /* Get the full name */
  539. for (;;) {
  540. c = get_next_char();
  541. if (cf->eof)
  542. break;
  543. if (!iskeychar(c))
  544. break;
  545. strbuf_addch(name, tolower(c));
  546. }
  547. while (c == ' ' || c == '\t')
  548. c = get_next_char();
  549. value = NULL;
  550. if (c != '\n') {
  551. if (c != '=')
  552. return -1;
  553. value = parse_value();
  554. if (!value)
  555. return -1;
  556. }
  557. /*
  558. * We already consumed the \n, but we need linenr to point to
  559. * the line we just parsed during the call to fn to get
  560. * accurate line number in error messages.
  561. */
  562. cf->linenr--;
  563. ret = fn(name->buf, value, data);
  564. if (ret >= 0)
  565. cf->linenr++;
  566. return ret;
  567. }
  568. static int get_extended_base_var(struct strbuf *name, int c)
  569. {
  570. cf->subsection_case_sensitive = 0;
  571. do {
  572. if (c == '\n')
  573. goto error_incomplete_line;
  574. c = get_next_char();
  575. } while (isspace(c));
  576. /* We require the format to be '[base "extension"]' */
  577. if (c != '"')
  578. return -1;
  579. strbuf_addch(name, '.');
  580. for (;;) {
  581. int c = get_next_char();
  582. if (c == '\n')
  583. goto error_incomplete_line;
  584. if (c == '"')
  585. break;
  586. if (c == '\\') {
  587. c = get_next_char();
  588. if (c == '\n')
  589. goto error_incomplete_line;
  590. }
  591. strbuf_addch(name, c);
  592. }
  593. /* Final ']' */
  594. if (get_next_char() != ']')
  595. return -1;
  596. return 0;
  597. error_incomplete_line:
  598. cf->linenr--;
  599. return -1;
  600. }
  601. static int get_base_var(struct strbuf *name)
  602. {
  603. cf->subsection_case_sensitive = 1;
  604. for (;;) {
  605. int c = get_next_char();
  606. if (cf->eof)
  607. return -1;
  608. if (c == ']')
  609. return 0;
  610. if (isspace(c))
  611. return get_extended_base_var(name, c);
  612. if (!iskeychar(c) && c != '.')
  613. return -1;
  614. strbuf_addch(name, tolower(c));
  615. }
  616. }
  617. struct parse_event_data {
  618. enum config_event_t previous_type;
  619. size_t previous_offset;
  620. const struct config_options *opts;
  621. };
  622. static int do_event(enum config_event_t type, struct parse_event_data *data)
  623. {
  624. size_t offset;
  625. if (!data->opts || !data->opts->event_fn)
  626. return 0;
  627. if (type == CONFIG_EVENT_WHITESPACE &&
  628. data->previous_type == type)
  629. return 0;
  630. offset = cf->do_ftell(cf);
  631. /*
  632. * At EOF, the parser always "inserts" an extra '\n', therefore
  633. * the end offset of the event is the current file position, otherwise
  634. * we will already have advanced to the next event.
  635. */
  636. if (type != CONFIG_EVENT_EOF)
  637. offset--;
  638. if (data->previous_type != CONFIG_EVENT_EOF &&
  639. data->opts->event_fn(data->previous_type, data->previous_offset,
  640. offset, data->opts->event_fn_data) < 0)
  641. return -1;
  642. data->previous_type = type;
  643. data->previous_offset = offset;
  644. return 0;
  645. }
  646. static int git_parse_source(config_fn_t fn, void *data,
  647. const struct config_options *opts)
  648. {
  649. int comment = 0;
  650. size_t baselen = 0;
  651. struct strbuf *var = &cf->var;
  652. int error_return = 0;
  653. char *error_msg = NULL;
  654. /* U+FEFF Byte Order Mark in UTF8 */
  655. const char *bomptr = utf8_bom;
  656. /* For the parser event callback */
  657. struct parse_event_data event_data = {
  658. CONFIG_EVENT_EOF, 0, opts
  659. };
  660. for (;;) {
  661. int c;
  662. c = get_next_char();
  663. if (bomptr && *bomptr) {
  664. /* We are at the file beginning; skip UTF8-encoded BOM
  665. * if present. Sane editors won't put this in on their
  666. * own, but e.g. Windows Notepad will do it happily. */
  667. if (c == (*bomptr & 0377)) {
  668. bomptr++;
  669. continue;
  670. } else {
  671. /* Do not tolerate partial BOM. */
  672. if (bomptr != utf8_bom)
  673. break;
  674. /* No BOM at file beginning. Cool. */
  675. bomptr = NULL;
  676. }
  677. }
  678. if (c == '\n') {
  679. if (cf->eof) {
  680. if (do_event(CONFIG_EVENT_EOF, &event_data) < 0)
  681. return -1;
  682. return 0;
  683. }
  684. if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0)
  685. return -1;
  686. comment = 0;
  687. continue;
  688. }
  689. if (comment)
  690. continue;
  691. if (isspace(c)) {
  692. if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0)
  693. return -1;
  694. continue;
  695. }
  696. if (c == '#' || c == ';') {
  697. if (do_event(CONFIG_EVENT_COMMENT, &event_data) < 0)
  698. return -1;
  699. comment = 1;
  700. continue;
  701. }
  702. if (c == '[') {
  703. if (do_event(CONFIG_EVENT_SECTION, &event_data) < 0)
  704. return -1;
  705. /* Reset prior to determining a new stem */
  706. strbuf_reset(var);
  707. if (get_base_var(var) < 0 || var->len < 1)
  708. break;
  709. strbuf_addch(var, '.');
  710. baselen = var->len;
  711. continue;
  712. }
  713. if (!isalpha(c))
  714. break;
  715. if (do_event(CONFIG_EVENT_ENTRY, &event_data) < 0)
  716. return -1;
  717. /*
  718. * Truncate the var name back to the section header
  719. * stem prior to grabbing the suffix part of the name
  720. * and the value.
  721. */
  722. strbuf_setlen(var, baselen);
  723. strbuf_addch(var, tolower(c));
  724. if (get_value(fn, data, var) < 0)
  725. break;
  726. }
  727. if (do_event(CONFIG_EVENT_ERROR, &event_data) < 0)
  728. return -1;
  729. switch (cf->origin_type) {
  730. case CONFIG_ORIGIN_BLOB:
  731. error_msg = xstrfmt(_("bad config line %d in blob %s"),
  732. cf->linenr, cf->name);
  733. break;
  734. case CONFIG_ORIGIN_FILE:
  735. error_msg = xstrfmt(_("bad config line %d in file %s"),
  736. cf->linenr, cf->name);
  737. break;
  738. case CONFIG_ORIGIN_STDIN:
  739. error_msg = xstrfmt(_("bad config line %d in standard input"),
  740. cf->linenr);
  741. break;
  742. case CONFIG_ORIGIN_SUBMODULE_BLOB:
  743. error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"),
  744. cf->linenr, cf->name);
  745. break;
  746. case CONFIG_ORIGIN_CMDLINE:
  747. error_msg = xstrfmt(_("bad config line %d in command line %s"),
  748. cf->linenr, cf->name);
  749. break;
  750. default:
  751. error_msg = xstrfmt(_("bad config line %d in %s"),
  752. cf->linenr, cf->name);
  753. }
  754. switch (opts && opts->error_action ?
  755. opts->error_action :
  756. cf->default_error_action) {
  757. case CONFIG_ERROR_DIE:
  758. die("%s", error_msg);
  759. break;
  760. case CONFIG_ERROR_ERROR:
  761. error_return = error("%s", error_msg);
  762. break;
  763. case CONFIG_ERROR_SILENT:
  764. error_return = -1;
  765. break;
  766. case CONFIG_ERROR_UNSET:
  767. BUG("config error action unset");
  768. }
  769. free(error_msg);
  770. return error_return;
  771. }
  772. static uintmax_t get_unit_factor(const char *end)
  773. {
  774. if (!*end)
  775. return 1;
  776. else if (!strcasecmp(end, "k"))
  777. return 1024;
  778. else if (!strcasecmp(end, "m"))
  779. return 1024 * 1024;
  780. else if (!strcasecmp(end, "g"))
  781. return 1024 * 1024 * 1024;
  782. return 0;
  783. }
  784. static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
  785. {
  786. if (value && *value) {
  787. char *end;
  788. intmax_t val;
  789. uintmax_t uval;
  790. uintmax_t factor;
  791. errno = 0;
  792. val = strtoimax(value, &end, 0);
  793. if (errno == ERANGE)
  794. return 0;
  795. factor = get_unit_factor(end);
  796. if (!factor) {
  797. errno = EINVAL;
  798. return 0;
  799. }
  800. uval = val < 0 ? -val : val;
  801. if (unsigned_mult_overflows(factor, uval) ||
  802. factor * uval > max) {
  803. errno = ERANGE;
  804. return 0;
  805. }
  806. val *= factor;
  807. *ret = val;
  808. return 1;
  809. }
  810. errno = EINVAL;
  811. return 0;
  812. }
  813. static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
  814. {
  815. if (value && *value) {
  816. char *end;
  817. uintmax_t val;
  818. uintmax_t factor;
  819. errno = 0;
  820. val = strtoumax(value, &end, 0);
  821. if (errno == ERANGE)
  822. return 0;
  823. factor = get_unit_factor(end);
  824. if (!factor) {
  825. errno = EINVAL;
  826. return 0;
  827. }
  828. if (unsigned_mult_overflows(factor, val) ||
  829. factor * val > max) {
  830. errno = ERANGE;
  831. return 0;
  832. }
  833. val *= factor;
  834. *ret = val;
  835. return 1;
  836. }
  837. errno = EINVAL;
  838. return 0;
  839. }
  840. static int git_parse_int(const char *value, int *ret)
  841. {
  842. intmax_t tmp;
  843. if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
  844. return 0;
  845. *ret = tmp;
  846. return 1;
  847. }
  848. static int git_parse_int64(const char *value, int64_t *ret)
  849. {
  850. intmax_t tmp;
  851. if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
  852. return 0;
  853. *ret = tmp;
  854. return 1;
  855. }
  856. int git_parse_ulong(const char *value, unsigned long *ret)
  857. {
  858. uintmax_t tmp;
  859. if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
  860. return 0;
  861. *ret = tmp;
  862. return 1;
  863. }
  864. int git_parse_ssize_t(const char *value, ssize_t *ret)
  865. {
  866. intmax_t tmp;
  867. if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t)))
  868. return 0;
  869. *ret = tmp;
  870. return 1;
  871. }
  872. NORETURN
  873. static void die_bad_number(const char *name, const char *value)
  874. {
  875. const char *error_type = (errno == ERANGE) ?
  876. N_("out of range") : N_("invalid unit");
  877. const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s");
  878. if (!value)
  879. value = "";
  880. if (!strcmp(name, "GIT_TEST_GETTEXT_POISON"))
  881. /*
  882. * We explicitly *don't* use _() here since it would
  883. * cause an infinite loop with _() needing to call
  884. * use_gettext_poison(). This is why marked up
  885. * translations with N_() above.
  886. */
  887. die(bad_numeric, value, name, error_type);
  888. if (!(cf && cf->name))
  889. die(_(bad_numeric), value, name, _(error_type));
  890. switch (cf->origin_type) {
  891. case CONFIG_ORIGIN_BLOB:
  892. die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
  893. value, name, cf->name, _(error_type));
  894. case CONFIG_ORIGIN_FILE:
  895. die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
  896. value, name, cf->name, _(error_type));
  897. case CONFIG_ORIGIN_STDIN:
  898. die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
  899. value, name, _(error_type));
  900. case CONFIG_ORIGIN_SUBMODULE_BLOB:
  901. die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
  902. value, name, cf->name, _(error_type));
  903. case CONFIG_ORIGIN_CMDLINE:
  904. die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
  905. value, name, cf->name, _(error_type));
  906. default:
  907. die(_("bad numeric config value '%s' for '%s' in %s: %s"),
  908. value, name, cf->name, _(error_type));
  909. }
  910. }
  911. int git_config_int(const char *name, const char *value)
  912. {
  913. int ret;
  914. if (!git_parse_int(value, &ret))
  915. die_bad_number(name, value);
  916. return ret;
  917. }
  918. int64_t git_config_int64(const char *name, const char *value)
  919. {
  920. int64_t ret;
  921. if (!git_parse_int64(value, &ret))
  922. die_bad_number(name, value);
  923. return ret;
  924. }
  925. unsigned long git_config_ulong(const char *name, const char *value)
  926. {
  927. unsigned long ret;
  928. if (!git_parse_ulong(value, &ret))
  929. die_bad_number(name, value);
  930. return ret;
  931. }
  932. ssize_t git_config_ssize_t(const char *name, const char *value)
  933. {
  934. ssize_t ret;
  935. if (!git_parse_ssize_t(value, &ret))
  936. die_bad_number(name, value);
  937. return ret;
  938. }
  939. static int git_parse_maybe_bool_text(const char *value)
  940. {
  941. if (!value)
  942. return 1;
  943. if (!*value)
  944. return 0;
  945. if (!strcasecmp(value, "true")
  946. || !strcasecmp(value, "yes")
  947. || !strcasecmp(value, "on"))
  948. return 1;
  949. if (!strcasecmp(value, "false")
  950. || !strcasecmp(value, "no")
  951. || !strcasecmp(value, "off"))
  952. return 0;
  953. return -1;
  954. }
  955. int git_parse_maybe_bool(const char *value)
  956. {
  957. int v = git_parse_maybe_bool_text(value);
  958. if (0 <= v)
  959. return v;
  960. if (git_parse_int(value, &v))
  961. return !!v;
  962. return -1;
  963. }
  964. int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
  965. {
  966. int v = git_parse_maybe_bool_text(value);
  967. if (0 <= v) {
  968. *is_bool = 1;
  969. return v;
  970. }
  971. *is_bool = 0;
  972. return git_config_int(name, value);
  973. }
  974. int git_config_bool(const char *name, const char *value)
  975. {
  976. int discard;
  977. return !!git_config_bool_or_int(name, value, &discard);
  978. }
  979. int git_config_string(const char **dest, const char *var, const char *value)
  980. {
  981. if (!value)
  982. return config_error_nonbool(var);
  983. *dest = xstrdup(value);
  984. return 0;
  985. }
  986. int git_config_pathname(const char **dest, const char *var, const char *value)
  987. {
  988. if (!value)
  989. return config_error_nonbool(var);
  990. *dest = expand_user_path(value, 0);
  991. if (!*dest)
  992. die(_("failed to expand user dir in: '%s'"), value);
  993. return 0;
  994. }
  995. int git_config_expiry_date(timestamp_t *timestamp, const char *var, const char *value)
  996. {
  997. if (!value)
  998. return config_error_nonbool(var);
  999. if (parse_expiry_date(value, timestamp))
  1000. return error(_("'%s' for '%s' is not a valid timestamp"),
  1001. value, var);
  1002. return 0;
  1003. }
  1004. int git_config_color(char *dest, const char *var, const char *value)
  1005. {
  1006. if (!value)
  1007. return config_error_nonbool(var);
  1008. if (color_parse(value, dest) < 0)
  1009. return -1;
  1010. return 0;
  1011. }
  1012. static int git_default_core_config(const char *var, const char *value, void *cb)
  1013. {
  1014. /* This needs a better name */
  1015. if (!strcmp(var, "core.filemode")) {
  1016. trust_executable_bit = git_config_bool(var, value);
  1017. return 0;
  1018. }
  1019. if (!strcmp(var, "core.trustctime")) {
  1020. trust_ctime = git_config_bool(var, value);
  1021. return 0;
  1022. }
  1023. if (!strcmp(var, "core.checkstat")) {
  1024. if (!strcasecmp(value, "default"))
  1025. check_stat = 1;
  1026. else if (!strcasecmp(value, "minimal"))
  1027. check_stat = 0;
  1028. }
  1029. if (!strcmp(var, "core.quotepath")) {
  1030. quote_path_fully = git_config_bool(var, value);
  1031. return 0;
  1032. }
  1033. if (!strcmp(var, "core.symlinks")) {
  1034. has_symlinks = git_config_bool(var, value);
  1035. return 0;
  1036. }
  1037. if (!strcmp(var, "core.ignorecase")) {
  1038. ignore_case = git_config_bool(var, value);
  1039. return 0;
  1040. }
  1041. if (!strcmp(var, "core.attributesfile"))
  1042. return git_config_pathname(&git_attributes_file, var, value);
  1043. if (!strcmp(var, "core.hookspath"))
  1044. return git_config_pathname(&git_hooks_path, var, value);
  1045. if (!strcmp(var, "core.bare")) {
  1046. is_bare_repository_cfg = git_config_bool(var, value);
  1047. return 0;
  1048. }
  1049. if (!strcmp(var, "core.ignorestat")) {
  1050. assume_unchanged = git_config_bool(var, value);
  1051. return 0;
  1052. }
  1053. if (!strcmp(var, "core.prefersymlinkrefs")) {
  1054. prefer_symlink_refs = git_config_bool(var, value);
  1055. return 0;
  1056. }
  1057. if (!strcmp(var, "core.logallrefupdates")) {
  1058. if (value && !strcasecmp(value, "always"))
  1059. log_all_ref_updates = LOG_REFS_ALWAYS;
  1060. else if (git_config_bool(var, value))
  1061. log_all_ref_updates = LOG_REFS_NORMAL;
  1062. else
  1063. log_all_ref_updates = LOG_REFS_NONE;
  1064. return 0;
  1065. }
  1066. if (!strcmp(var, "core.warnambiguousrefs")) {
  1067. warn_ambiguous_refs = git_config_bool(var, value);
  1068. return 0;
  1069. }
  1070. if (!strcmp(var, "core.abbrev")) {
  1071. if (!value)
  1072. return config_error_nonbool(var);
  1073. if (!strcasecmp(value, "auto"))
  1074. default_abbrev = -1;
  1075. else {
  1076. int abbrev = git_config_int(var, value);
  1077. if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz)
  1078. return error(_("abbrev length out of range: %d"), abbrev);
  1079. default_abbrev = abbrev;
  1080. }
  1081. return 0;
  1082. }
  1083. if (!strcmp(var, "core.disambiguate"))
  1084. return set_disambiguate_hint_config(var, value);
  1085. if (!strcmp(var, "core.loosecompression")) {
  1086. int level = git_config_int(var, value);
  1087. if (level == -1)
  1088. level = Z_DEFAULT_COMPRESSION;
  1089. else if (level < 0 || level > Z_BEST_COMPRESSION)
  1090. die(_("bad zlib compression level %d"), level);
  1091. zlib_compression_level = level;
  1092. zlib_compression_seen = 1;
  1093. return 0;
  1094. }
  1095. if (!strcmp(var, "core.compression")) {
  1096. int level = git_config_int(var, value);
  1097. if (level == -1)
  1098. level = Z_DEFAULT_COMPRESSION;
  1099. else if (level < 0 || level > Z_BEST_COMPRESSION)
  1100. die(_("bad zlib compression level %d"), level);
  1101. core_compression_level = level;
  1102. core_compression_seen = 1;
  1103. if (!zlib_compression_seen)
  1104. zlib_compression_level = level;
  1105. if (!pack_compression_seen)
  1106. pack_compression_level = level;
  1107. return 0;
  1108. }
  1109. if (!strcmp(var, "core.packedgitwindowsize")) {
  1110. int pgsz_x2 = getpagesize() * 2;
  1111. packed_git_window_size = git_config_ulong(var, value);
  1112. /* This value must be multiple of (pagesize * 2) */
  1113. packed_git_window_size /= pgsz_x2;
  1114. if (packed_git_window_size < 1)
  1115. packed_git_window_size = 1;
  1116. packed_git_window_size *= pgsz_x2;
  1117. return 0;
  1118. }
  1119. if (!strcmp(var, "core.bigfilethreshold")) {
  1120. big_file_threshold = git_config_ulong(var, value);
  1121. return 0;
  1122. }
  1123. if (!strcmp(var, "core.packedgitlimit")) {
  1124. packed_git_limit = git_config_ulong(var, value);
  1125. return 0;
  1126. }
  1127. if (!strcmp(var, "core.deltabasecachelimit")) {
  1128. delta_base_cache_limit = git_config_ulong(var, value);
  1129. return 0;
  1130. }
  1131. if (!strcmp(var, "core.autocrlf")) {
  1132. if (value && !strcasecmp(value, "input")) {
  1133. auto_crlf = AUTO_CRLF_INPUT;
  1134. return 0;
  1135. }
  1136. auto_crlf = git_config_bool(var, value);
  1137. return 0;
  1138. }
  1139. if (!strcmp(var, "core.safecrlf")) {
  1140. int eol_rndtrp_die;
  1141. if (value && !strcasecmp(value, "warn")) {
  1142. global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
  1143. return 0;
  1144. }
  1145. eol_rndtrp_die = git_config_bool(var, value);
  1146. global_conv_flags_eol = eol_rndtrp_die ?
  1147. CONV_EOL_RNDTRP_DIE : 0;
  1148. return 0;
  1149. }
  1150. if (!strcmp(var, "core.eol")) {
  1151. if (value && !strcasecmp(value, "lf"))
  1152. core_eol = EOL_LF;
  1153. else if (value && !strcasecmp(value, "crlf"))
  1154. core_eol = EOL_CRLF;
  1155. else if (value && !strcasecmp(value, "native"))
  1156. core_eol = EOL_NATIVE;
  1157. else
  1158. core_eol = EOL_UNSET;
  1159. return 0;
  1160. }
  1161. if (!strcmp(var, "core.checkroundtripencoding")) {
  1162. check_roundtrip_encoding = xstrdup(value);
  1163. return 0;
  1164. }
  1165. if (!strcmp(var, "core.notesref")) {
  1166. notes_ref_name = xstrdup(value);
  1167. return 0;
  1168. }
  1169. if (!strcmp(var, "core.editor"))
  1170. return git_config_string(&editor_program, var, value);
  1171. if (!strcmp(var, "core.commentchar")) {
  1172. if (!value)
  1173. return config_error_nonbool(var);
  1174. else if (!strcasecmp(value, "auto"))
  1175. auto_comment_line_char = 1;
  1176. else if (value[0] && !value[1]) {
  1177. comment_line_char = value[0];
  1178. auto_comment_line_char = 0;
  1179. } else
  1180. return error(_("core.commentChar should only be one character"));
  1181. return 0;
  1182. }
  1183. if (!strcmp(var, "core.askpass"))
  1184. return git_config_string(&askpass_program, var, value);
  1185. if (!strcmp(var, "core.excludesfile"))
  1186. return git_config_pathname(&excludes_file, var, value);
  1187. if (!strcmp(var, "core.whitespace")) {
  1188. if (!value)
  1189. return config_error_nonbool(var);
  1190. whitespace_rule_cfg = parse_whitespace_rule(value);
  1191. return 0;
  1192. }
  1193. if (!strcmp(var, "core.fsyncobjectfiles")) {
  1194. fsync_object_files = git_config_bool(var, value);
  1195. return 0;
  1196. }
  1197. if (!strcmp(var, "core.preloadindex")) {
  1198. core_preload_index = git_config_bool(var, value);
  1199. return 0;
  1200. }
  1201. if (!strcmp(var, "core.createobject")) {
  1202. if (!strcmp(value, "rename"))
  1203. object_creation_mode = OBJECT_CREATION_USES_RENAMES;
  1204. else if (!strcmp(value, "link"))
  1205. object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
  1206. else
  1207. die(_("invalid mode for object creation: %s"), value);
  1208. return 0;
  1209. }
  1210. if (!strcmp(var, "core.sparsecheckout")) {
  1211. core_apply_sparse_checkout = git_config_bool(var, value);
  1212. return 0;
  1213. }
  1214. if (!strcmp(var, "core.sparsecheckoutcone")) {
  1215. core_sparse_checkout_cone = git_config_bool(var, value);
  1216. return 0;
  1217. }
  1218. if (!strcmp(var, "core.precomposeunicode")) {
  1219. precomposed_unicode = git_config_bool(var, value);
  1220. return 0;
  1221. }
  1222. if (!strcmp(var, "core.protecthfs")) {
  1223. protect_hfs = git_config_bool(var, value);
  1224. return 0;
  1225. }
  1226. if (!strcmp(var, "core.protectntfs")) {
  1227. protect_ntfs = git_config_bool(var, value);
  1228. return 0;
  1229. }
  1230. if (!strcmp(var, "core.usereplacerefs")) {
  1231. read_replace_refs = git_config_bool(var, value);
  1232. return 0;
  1233. }
  1234. /* Add other config variables here and to Documentation/config.txt. */
  1235. return platform_core_config(var, value, cb);
  1236. }
  1237. static int git_default_i18n_config(const char *var, const char *value)
  1238. {
  1239. if (!strcmp(var, "i18n.commitencoding"))
  1240. return git_config_string(&git_commit_encoding, var, value);
  1241. if (!strcmp(var, "i18n.logoutputencoding"))
  1242. return git_config_string(&git_log_output_encoding, var, value);
  1243. /* Add other config variables here and to Documentation/config.txt. */
  1244. return 0;
  1245. }
  1246. static int git_default_branch_config(const char *var, const char *value)
  1247. {
  1248. if (!strcmp(var, "branch.autosetupmerge")) {
  1249. if (value && !strcasecmp(value, "always")) {
  1250. git_branch_track = BRANCH_TRACK_ALWAYS;
  1251. return 0;
  1252. }
  1253. git_branch_track = git_config_bool(var, value);
  1254. return 0;
  1255. }
  1256. if (!strcmp(var, "branch.autosetuprebase")) {
  1257. if (!value)
  1258. return config_error_nonbool(var);
  1259. else if (!strcmp(value, "never"))
  1260. autorebase = AUTOREBASE_NEVER;
  1261. else if (!strcmp(value, "local"))
  1262. autorebase = AUTOREBASE_LOCAL;
  1263. else if (!strcmp(value, "remote"))
  1264. autorebase = AUTOREBASE_REMOTE;
  1265. else if (!strcmp(value, "always"))
  1266. autorebase = AUTOREBASE_ALWAYS;
  1267. else
  1268. return error(_("malformed value for %s"), var);
  1269. return 0;
  1270. }
  1271. /* Add other config variables here and to Documentation/config.txt. */
  1272. return 0;
  1273. }
  1274. static int git_default_push_config(const char *var, const char *value)
  1275. {
  1276. if (!strcmp(var, "push.default")) {
  1277. if (!value)
  1278. return config_error_nonbool(var);
  1279. else if (!strcmp(value, "nothing"))
  1280. push_default = PUSH_DEFAULT_NOTHING;
  1281. else if (!strcmp(value, "matching"))
  1282. push_default = PUSH_DEFAULT_MATCHING;
  1283. else if (!strcmp(value, "simple"))
  1284. push_default = PUSH_DEFAULT_SIMPLE;
  1285. else if (!strcmp(value, "upstream"))
  1286. push_default = PUSH_DEFAULT_UPSTREAM;
  1287. else if (!strcmp(value, "tracking")) /* deprecated */
  1288. push_default = PUSH_DEFAULT_UPSTREAM;
  1289. else if (!strcmp(value, "current"))
  1290. push_default = PUSH_DEFAULT_CURRENT;
  1291. else {
  1292. error(_("malformed value for %s: %s"), var, value);
  1293. return error(_("must be one of nothing, matching, simple, "
  1294. "upstream or current"));
  1295. }
  1296. return 0;
  1297. }
  1298. /* Add other config variables here and to Documentation/config.txt. */
  1299. return 0;
  1300. }
  1301. static int git_default_mailmap_config(const char *var, const char *value)
  1302. {
  1303. if (!strcmp(var, "mailmap.file"))
  1304. return git_config_pathname(&git_mailmap_file, var, value);
  1305. if (!strcmp(var, "mailmap.blob"))
  1306. return git_config_string(&git_mailmap_blob, var, value);
  1307. /* Add other config variables here and to Documentation/config.txt. */
  1308. return 0;
  1309. }
  1310. int git_default_config(const char *var, const char *value, void *cb)
  1311. {
  1312. if (starts_with(var, "core."))
  1313. return git_default_core_config(var, value, cb);
  1314. if (starts_with(var, "user.") ||
  1315. starts_with(var, "author.") ||
  1316. starts_with(var, "committer."))
  1317. return git_ident_config(var, value, cb);
  1318. if (starts_with(var, "i18n."))
  1319. return git_default_i18n_config(var, value);
  1320. if (starts_with(var, "branch."))
  1321. return git_default_branch_config(var, value);
  1322. if (starts_with(var, "push."))
  1323. return git_default_push_config(var, value);
  1324. if (starts_with(var, "mailmap."))
  1325. return git_default_mailmap_config(var, value);
  1326. if (starts_with(var, "advice.") || starts_with(var, "color.advice"))
  1327. return git_default_advice_config(var, value);
  1328. if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
  1329. pager_use_color = git_config_bool(var,value);
  1330. return 0;
  1331. }
  1332. if (!strcmp(var, "pack.packsizelimit")) {
  1333. pack_size_limit_cfg = git_config_ulong(var, value);
  1334. return 0;
  1335. }
  1336. if (!strcmp(var, "pack.compression")) {
  1337. int level = git_config_int(var, value);
  1338. if (level == -1)
  1339. level = Z_DEFAULT_COMPRESSION;
  1340. else if (level < 0 || level > Z_BEST_COMPRESSION)
  1341. die(_("bad pack compression level %d"), level);
  1342. pack_compression_level = level;
  1343. pack_compression_seen = 1;
  1344. return 0;
  1345. }
  1346. /* Add other config variables here and to Documentation/config.txt. */
  1347. return 0;
  1348. }
  1349. /*
  1350. * All source specific fields in the union, die_on_error, name and the callbacks
  1351. * fgetc, ungetc, ftell of top need to be initialized before calling
  1352. * this function.
  1353. */
  1354. static int do_config_from(struct config_source *top, config_fn_t fn, void *data,
  1355. const struct config_options *opts)
  1356. {
  1357. int ret;
  1358. /* push config-file parsing state stack */
  1359. top->prev = cf;
  1360. top->linenr = 1;
  1361. top->eof = 0;
  1362. top->total_len = 0;
  1363. strbuf_init(&top->value, 1024);
  1364. strbuf_init(&top->var, 1024);
  1365. cf = top;
  1366. ret = git_parse_source(fn, data, opts);
  1367. /* pop config-file parsing state stack */
  1368. strbuf_release(&top->value);
  1369. strbuf_release(&top->var);
  1370. cf = top->prev;
  1371. return ret;
  1372. }
  1373. static int do_config_from_file(config_fn_t fn,
  1374. const enum config_origin_type origin_type,
  1375. const char *name, const char *path, FILE *f,
  1376. void *data, const struct config_options *opts)
  1377. {
  1378. struct config_source top;
  1379. int ret;
  1380. top.u.file = f;
  1381. top.origin_type = origin_type;
  1382. top.name = name;
  1383. top.path = path;
  1384. top.default_error_action = CONFIG_ERROR_DIE;
  1385. top.do_fgetc = config_file_fgetc;
  1386. top.do_ungetc = config_file_ungetc;
  1387. top.do_ftell = config_file_ftell;
  1388. flockfile(f);
  1389. ret = do_config_from(&top, fn, data, opts);
  1390. funlockfile(f);
  1391. return ret;
  1392. }
  1393. static int git_config_from_stdin(config_fn_t fn, void *data)
  1394. {
  1395. return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin,
  1396. data, NULL);
  1397. }
  1398. int git_config_from_file_with_options(config_fn_t fn, const char *filename,
  1399. void *data,
  1400. const struct config_options *opts)
  1401. {
  1402. int ret = -1;
  1403. FILE *f;
  1404. f = fopen_or_warn(filename, "r");
  1405. if (f) {
  1406. ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
  1407. filename, f, data, opts);
  1408. fclose(f);
  1409. }
  1410. return ret;
  1411. }
  1412. int git_config_from_file(config_fn_t fn, const char *filename, void *data)
  1413. {
  1414. return git_config_from_file_with_options(fn, filename, data, NULL);
  1415. }
  1416. int git_config_from_mem(config_fn_t fn,
  1417. const enum config_origin_type origin_type,
  1418. const char *name, const char *buf, size_t len,
  1419. void *data, const struct config_options *opts)
  1420. {
  1421. struct config_source top;
  1422. top.u.buf.buf = buf;
  1423. top.u.buf.len = len;
  1424. top.u.buf.pos = 0;
  1425. top.origin_type = origin_type;
  1426. top.name = name;
  1427. top.path = NULL;
  1428. top.default_error_action = CONFIG_ERROR_ERROR;
  1429. top.do_fgetc = config_buf_fgetc;
  1430. top.do_ungetc = config_buf_ungetc;
  1431. top.do_ftell = config_buf_ftell;
  1432. return do_config_from(&top, fn, data, opts);
  1433. }
  1434. int git_config_from_blob_oid(config_fn_t fn,
  1435. const char *name,
  1436. const struct object_id *oid,
  1437. void *data)
  1438. {
  1439. enum object_type type;
  1440. char *buf;
  1441. unsigned long size;
  1442. int ret;
  1443. buf = read_object_file(oid, &type, &size);
  1444. if (!buf)
  1445. return error(_("unable to load config blob object '%s'"), name);
  1446. if (type != OBJ_BLOB) {
  1447. free(buf);
  1448. return error(_("reference '%s' does not point to a blob"), name);
  1449. }
  1450. ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size,
  1451. data, NULL);
  1452. free(buf);
  1453. return ret;
  1454. }
  1455. static int git_config_from_blob_ref(config_fn_t fn,
  1456. const char *name,
  1457. void *data)
  1458. {
  1459. struct object_id oid;
  1460. if (get_oid(name, &oid) < 0)
  1461. return error(_("unable to resolve config blob '%s'"), name);
  1462. return git_config_from_blob_oid(fn, name, &oid, data);
  1463. }
  1464. const char *git_etc_gitconfig(void)
  1465. {
  1466. static const char *system_wide;
  1467. if (!system_wide)
  1468. system_wide = system_path(ETC_GITCONFIG);
  1469. return system_wide;
  1470. }
  1471. /*
  1472. * Parse environment variable 'k' as a boolean (in various
  1473. * possible spellings); if missing, use the default value 'def'.
  1474. */
  1475. int git_env_bool(const char *k, int def)
  1476. {
  1477. const char *v = getenv(k);
  1478. return v ? git_config_bool(k, v) : def;
  1479. }
  1480. /*
  1481. * Parse environment variable 'k' as ulong with possibly a unit
  1482. * suffix; if missing, use the default value 'val'.
  1483. */
  1484. unsigned long git_env_ulong(const char *k, unsigned long val)
  1485. {
  1486. const char *v = getenv(k);
  1487. if (v && !git_parse_ulong(v, &val))
  1488. die(_("failed to parse %s"), k);
  1489. return val;
  1490. }
  1491. int git_config_system(void)
  1492. {
  1493. return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
  1494. }
  1495. static int do_git_config_sequence(const struct config_options *opts,
  1496. config_fn_t fn, void *data)
  1497. {
  1498. int ret = 0;
  1499. char *xdg_config = xdg_config_home("config");
  1500. char *user_config = expand_user_path("~/.gitconfig", 0);
  1501. char *repo_config;
  1502. enum config_scope prev_parsing_scope = current_parsing_scope;
  1503. if (opts->commondir)
  1504. repo_config = mkpathdup("%s/config", opts->commondir);
  1505. else if (opts->git_dir)
  1506. BUG("git_dir without commondir");
  1507. else
  1508. repo_config = NULL;
  1509. current_parsing_scope = CONFIG_SCOPE_SYSTEM;
  1510. if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK,
  1511. opts->system_gently ?
  1512. ACCESS_EACCES_OK : 0))
  1513. ret += git_config_from_file(fn, git_etc_gitconfig(),
  1514. data);
  1515. current_parsing_scope = CONFIG_SCOPE_GLOBAL;
  1516. if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
  1517. ret += git_config_from_file(fn, xdg_config, data);
  1518. if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
  1519. ret += git_config_from_file(fn, user_config, data);
  1520. current_parsing_scope = CONFIG_SCOPE_LOCAL;
  1521. if (!opts->ignore_repo && repo_config &&
  1522. !access_or_die(repo_config, R_OK, 0))
  1523. ret += git_config_from_file(fn, repo_config, data);
  1524. current_parsing_scope = CONFIG_SCOPE_WORKTREE;
  1525. if (!opts->ignore_worktree && repository_format_worktree_config) {
  1526. char *path = git_pathdup("config.worktree");
  1527. if (!access_or_die(path, R_OK, 0))
  1528. ret += git_config_from_file(fn, path, data);
  1529. free(path);
  1530. }
  1531. current_parsing_scope = CONFIG_SCOPE_COMMAND;
  1532. if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
  1533. die(_("unable to parse command-line config"));
  1534. current_parsing_scope = prev_parsing_scope;
  1535. free(xdg_config);
  1536. free(user_config);
  1537. free(repo_config);
  1538. return ret;
  1539. }
  1540. int config_with_options(config_fn_t fn, void *data,
  1541. struct git_config_source *config_source,
  1542. const struct config_options *opts)
  1543. {
  1544. struct config_include_data inc = CONFIG_INCLUDE_INIT;
  1545. if (opts->respect_includes) {
  1546. inc.fn = fn;
  1547. inc.data = data;
  1548. inc.opts = opts;
  1549. fn = git_config_include;
  1550. data = &inc;
  1551. }
  1552. if (config_source)
  1553. current_parsing_scope = config_source->scope;
  1554. /*
  1555. * If we have a specific filename, use it. Otherwise, follow the
  1556. * regular lookup sequence.
  1557. */
  1558. if (config_source && config_source->use_stdin)
  1559. return git_config_from_stdin(fn, data);
  1560. else if (config_source && config_source->file)
  1561. return git_config_from_file(fn, config_source->file, data);
  1562. else if (config_source && config_source->blob)
  1563. return git_config_from_blob_ref(fn, config_source->blob, data);
  1564. return do_git_config_sequence(opts, fn, data);
  1565. }
  1566. static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
  1567. {
  1568. int i, value_index;
  1569. struct string_list *values;
  1570. struct config_set_element *entry;
  1571. struct configset_list *list = &cs->list;
  1572. for (i = 0; i < list->nr; i++) {
  1573. entry = list->items[i].e;
  1574. value_index = list->items[i].value_index;
  1575. values = &entry->value_list;
  1576. current_config_kvi = values->items[value_index].util;
  1577. if (fn(entry->key, values->items[value_index].string, data) < 0)
  1578. git_die_config_linenr(entry->key,
  1579. current_config_kvi->filename,
  1580. current_config_kvi->linenr);
  1581. current_config_kvi = NULL;
  1582. }
  1583. }
  1584. void read_early_config(config_fn_t cb, void *data)
  1585. {
  1586. struct config_options opts = {0};
  1587. struct strbuf commondir = STRBUF_INIT;
  1588. struct strbuf gitdir = STRBUF_INIT;
  1589. opts.respect_includes = 1;
  1590. if (have_git_dir()) {
  1591. opts.commondir = get_git_common_dir();
  1592. opts.git_dir = get_git_dir();
  1593. /*
  1594. * When setup_git_directory() was not yet asked to discover the
  1595. * GIT_DIR, we ask discover_git_directory() to figure out whether there
  1596. * is any repository config we should use (but unlike
  1597. * setup_git_directory_gently(), no global state is changed, most
  1598. * notably, the current working directory is still the same after the
  1599. * call).
  1600. */
  1601. } else if (!discover_git_directory(&commondir, &gitdir)) {
  1602. opts.commondir = commondir.buf;
  1603. opts.git_dir = gitdir.buf;
  1604. }
  1605. config_with_options(cb, data, NULL, &opts);
  1606. strbuf_release(&commondir);
  1607. strbuf_release(&gitdir);
  1608. }
  1609. /*
  1610. * Read config but only enumerate system and global settings.
  1611. * Omit any repo-local, worktree-local, or command-line settings.
  1612. */
  1613. void read_very_early_config(config_fn_t cb, void *data)
  1614. {
  1615. struct config_options opts = { 0 };
  1616. opts.respect_includes = 1;
  1617. opts.ignore_repo = 1;
  1618. opts.ignore_worktree = 1;
  1619. opts.ignore_cmdline = 1;
  1620. opts.system_gently = 1;
  1621. config_with_options(cb, data, NULL, &opts);
  1622. }
  1623. static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
  1624. {
  1625. struct config_set_element k;
  1626. struct config_set_element *found_entry;
  1627. char *normalized_key;
  1628. /*
  1629. * `key` may come from the user, so normalize it before using it
  1630. * for querying entries from the hashmap.
  1631. */
  1632. if (git_config_parse_key(key, &normalized_key, NULL))
  1633. return NULL;
  1634. hashmap_entry_init(&k.ent, strhash(normalized_key));
  1635. k.key = normalized_key;
  1636. found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL);
  1637. free(normalized_key);
  1638. return found_entry;
  1639. }
  1640. static int configset_add_value(struct config_set *cs, const char *key, const char *value)
  1641. {
  1642. struct config_set_element *e;
  1643. struct string_list_item *si;
  1644. struct configset_list_item *l_item;
  1645. struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
  1646. e = configset_find_element(cs, key);
  1647. /*
  1648. * Since the keys are being fed by git_config*() callback mechanism, they
  1649. * are already normalized. So simply add them without any further munging.
  1650. */
  1651. if (!e) {
  1652. e = xmalloc(sizeof(*e));
  1653. hashmap_entry_init(&e->ent, strhash(key));
  1654. e->key = xstrdup(key);
  1655. string_list_init(&e->value_list, 1);
  1656. hashmap_add(&cs->config_hash, &e->ent);
  1657. }
  1658. si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
  1659. ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
  1660. l_item = &cs->list.items[cs->list.nr++];
  1661. l_item->e = e;
  1662. l_item->value_index = e->value_list.nr - 1;
  1663. if (!cf)
  1664. BUG("configset_add_value has no source");
  1665. if (cf->name) {
  1666. kv_info->filename = strintern(cf->name);
  1667. kv_info->linenr = cf->linenr;
  1668. kv_info->origin_type = cf->origin_type;
  1669. } else {
  1670. /* for values read from `git_config_from_parameters()` */
  1671. kv_info->filename = NULL;
  1672. kv_info->linenr = -1;
  1673. kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
  1674. }
  1675. kv_info->scope = current_parsing_scope;
  1676. si->util = kv_info;
  1677. return 0;
  1678. }
  1679. static int config_set_element_cmp(const void *unused_cmp_data,
  1680. const struct hashmap_entry *eptr,
  1681. const struct hashmap_entry *entry_or_key,
  1682. const void *unused_keydata)
  1683. {
  1684. const struct config_set_element *e1, *e2;
  1685. e1 = container_of(eptr, const struct config_set_element, ent);
  1686. e2 = container_of(entry_or_key, const struct config_set_element, ent);
  1687. return strcmp(e1->key, e2->key);
  1688. }
  1689. void git_configset_init(struct config_set *cs)
  1690. {
  1691. hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0);
  1692. cs->hash_initialized = 1;
  1693. cs->list.nr = 0;
  1694. cs->list.alloc = 0;
  1695. cs->list.items = NULL;
  1696. }
  1697. void git_configset_clear(struct config_set *cs)
  1698. {
  1699. struct config_set_element *entry;
  1700. struct hashmap_iter iter;
  1701. if (!cs->hash_initialized)
  1702. return;
  1703. hashmap_for_each_entry(&cs->config_hash, &iter, entry,
  1704. ent /* member name */) {
  1705. free(entry->key);
  1706. string_list_clear(&entry->value_list, 1);
  1707. }
  1708. hashmap_free_entries(&cs->config_hash, struct config_set_element, ent);
  1709. cs->hash_initialized = 0;
  1710. free(cs->list.items);
  1711. cs->list.nr = 0;
  1712. cs->list.alloc = 0;
  1713. cs->list.items = NULL;
  1714. }
  1715. static int config_set_callback(const char *key, const char *value, void *cb)
  1716. {
  1717. struct config_set *cs = cb;
  1718. configset_add_value(cs, key, value);
  1719. return 0;
  1720. }
  1721. int git_configset_add_file(struct config_set *cs, const char *filename)
  1722. {
  1723. return git_config_from_file(config_set_callback, filename, cs);
  1724. }
  1725. int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
  1726. {
  1727. const struct string_list *values = NULL;
  1728. /*
  1729. * Follows "last one wins" semantic, i.e., if there are multiple matches for the
  1730. * queried key in the files of the configset, the value returned will be the last
  1731. * value in the value list for that key.
  1732. */
  1733. values = git_configset_get_value_multi(cs, key);
  1734. if (!values)
  1735. return 1;
  1736. assert(values->nr > 0);
  1737. *value = values->items[values->nr - 1].string;
  1738. return 0;
  1739. }
  1740. const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
  1741. {
  1742. struct config_set_element *e = configset_find_element(cs, key);
  1743. return e ? &e->value_list : NULL;
  1744. }
  1745. int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
  1746. {
  1747. const char *value;
  1748. if (!git_configset_get_value(cs, key, &value))
  1749. return git_config_string((const char **)dest, key, value);
  1750. else
  1751. return 1;
  1752. }
  1753. int git_configset_get_string_tmp(struct config_set *cs, const char *key,
  1754. const char **dest)
  1755. {
  1756. const char *value;
  1757. if (!git_configset_get_value(cs, key, &value)) {
  1758. if (!value)
  1759. return config_error_nonbool(key);
  1760. *dest = value;
  1761. return 0;
  1762. } else {
  1763. return 1;
  1764. }
  1765. }
  1766. int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
  1767. {
  1768. const char *value;
  1769. if (!git_configset_get_value(cs, key, &value)) {
  1770. *dest = git_config_int(key, value);
  1771. return 0;
  1772. } else
  1773. return 1;
  1774. }
  1775. int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
  1776. {
  1777. const char *value;
  1778. if (!git_configset_get_value(cs, key, &value)) {
  1779. *dest = git_config_ulong(key, value);
  1780. return 0;
  1781. } else
  1782. return 1;
  1783. }
  1784. int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
  1785. {
  1786. const char *value;
  1787. if (!git_configset_get_value(cs, key, &value)) {
  1788. *dest = git_config_bool(key, value);
  1789. return 0;
  1790. } else
  1791. return 1;
  1792. }
  1793. int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
  1794. int *is_bool, int *dest)
  1795. {
  1796. const char *value;
  1797. if (!git_configset_get_value(cs, key, &value)) {
  1798. *dest = git_config_bool_or_int(key, value, is_bool);
  1799. return 0;
  1800. } else
  1801. return 1;
  1802. }
  1803. int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
  1804. {
  1805. const char *value;
  1806. if (!git_configset_get_value(cs, key, &value)) {
  1807. *dest = git_parse_maybe_bool(value);
  1808. if (*dest == -1)
  1809. return -1;
  1810. return 0;
  1811. } else
  1812. return 1;
  1813. }
  1814. int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
  1815. {
  1816. const char *value;
  1817. if (!git_configset_get_value(cs, key, &value))
  1818. return git_config_pathname(dest, key, value);
  1819. else
  1820. return 1;
  1821. }
  1822. /* Functions use to read configuration from a repository */
  1823. static void repo_read_config(struct repository *repo)
  1824. {
  1825. struct config_options opts = { 0 };
  1826. opts.respect_includes = 1;
  1827. opts.commondir = repo->commondir;
  1828. opts.git_dir = repo->gitdir;
  1829. if (!repo->config)
  1830. repo->config = xcalloc(1, sizeof(struct config_set));
  1831. else
  1832. git_configset_clear(repo->config);
  1833. git_configset_init(repo->config);
  1834. if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
  1835. /*
  1836. * config_with_options() normally returns only
  1837. * zero, as most errors are fatal, and
  1838. * non-fatal potential errors are guarded by "if"
  1839. * statements that are entered only when no error is
  1840. * possible.
  1841. *
  1842. * If we ever encounter a non-fatal error, it means
  1843. * something went really wrong and we should stop
  1844. * immediately.
  1845. */
  1846. die(_("unknown error occurred while reading the configuration files"));
  1847. }
  1848. static void git_config_check_init(struct repository *repo)
  1849. {
  1850. if (repo->config && repo->config->hash_initialized)
  1851. return;
  1852. repo_read_config(repo);
  1853. }
  1854. static void repo_config_clear(struct repository *repo)
  1855. {
  1856. if (!repo->config || !repo->config->hash_initialized)
  1857. return;
  1858. git_configset_clear(repo->config);
  1859. }
  1860. void repo_config(struct repository *repo, config_fn_t fn, void *data)
  1861. {
  1862. git_config_check_init(repo);
  1863. configset_iter(repo->config, fn, data);
  1864. }
  1865. int repo_config_get_value(struct repository *repo,
  1866. const char *key, const char **value)
  1867. {
  1868. git_config_check_init(repo);
  1869. return git_configset_get_value(repo->config, key, value);
  1870. }
  1871. const struct string_list *repo_config_get_value_multi(struct repository *repo,
  1872. const char *key)
  1873. {
  1874. git_config_check_init(repo);
  1875. return git_configset_get_value_multi(repo->config, key);
  1876. }
  1877. int repo_config_get_string(struct repository *repo,
  1878. const char *key, char **dest)
  1879. {
  1880. int ret;
  1881. git_config_check_init(repo);
  1882. ret = git_configset_get_string(repo->config, key, dest);
  1883. if (ret < 0)
  1884. git_die_config(key, NULL);
  1885. return ret;
  1886. }
  1887. int repo_config_get_string_tmp(struct repository *repo,
  1888. const char *key, const char **dest)
  1889. {
  1890. int ret;
  1891. git_config_check_init(repo);
  1892. ret = git_configset_get_string_tmp(repo->config, key, dest);
  1893. if (ret < 0)
  1894. git_die_config(key, NULL);
  1895. return ret;
  1896. }
  1897. int repo_config_get_int(struct repository *repo,
  1898. const char *key, int *dest)
  1899. {
  1900. git_config_check_init(repo);
  1901. return git_configset_get_int(repo->config, key, dest);
  1902. }
  1903. int repo_config_get_ulong(struct repository *repo,
  1904. const char *key, unsigned long *dest)
  1905. {
  1906. git_config_check_init(repo);
  1907. return git_configset_get_ulong(repo->config, key, dest);
  1908. }
  1909. int repo_config_get_bool(struct repository *repo,
  1910. const char *key, int *dest)
  1911. {
  1912. git_config_check_init(repo);
  1913. return git_configset_get_bool(repo->config, key, dest);
  1914. }
  1915. int repo_config_get_bool_or_int(struct repository *repo,
  1916. const char *key, int *is_bool, int *dest)
  1917. {
  1918. git_config_check_init(repo);
  1919. return git_configset_get_bool_or_int(repo->config, key, is_bool, dest);
  1920. }
  1921. int repo_config_get_maybe_bool(struct repository *repo,
  1922. const char *key, int *dest)
  1923. {
  1924. git_config_check_init(repo);
  1925. return git_configset_get_maybe_bool(repo->config, key, dest);
  1926. }
  1927. int repo_config_get_pathname(struct repository *repo,
  1928. const char *key, const char **dest)
  1929. {
  1930. int ret;
  1931. git_config_check_init(repo);
  1932. ret = git_configset_get_pathname(repo->config, key, dest);
  1933. if (ret < 0)
  1934. git_die_config(key, NULL);
  1935. return ret;
  1936. }
  1937. /* Functions used historically to read configuration from 'the_repository' */
  1938. void git_config(config_fn_t fn, void *data)
  1939. {
  1940. repo_config(the_repository, fn, data);
  1941. }
  1942. void git_config_clear(void)
  1943. {
  1944. repo_config_clear(the_repository);
  1945. }
  1946. int git_config_get_value(const char *key, const char **value)
  1947. {
  1948. return repo_config_get_value(the_repository, key, value);
  1949. }
  1950. const struct string_list *git_config_get_value_multi(const char *key)
  1951. {
  1952. return repo_config_get_value_multi(the_repository, key);
  1953. }
  1954. int git_config_get_string(const char *key, char **dest)
  1955. {
  1956. return repo_config_get_string(the_repository, key, dest);
  1957. }
  1958. int git_config_get_string_tmp(const char *key, const char **dest)
  1959. {
  1960. return repo_config_get_string_tmp(the_repository, key, dest);
  1961. }
  1962. int git_config_get_int(const char *key, int *dest)
  1963. {
  1964. return repo_config_get_int(the_repository, key, dest);
  1965. }
  1966. int git_config_get_ulong(const char *key, unsigned long *dest)
  1967. {
  1968. return repo_config_get_ulong(the_repository, key, dest);
  1969. }
  1970. int git_config_get_bool(const char *key, int *dest)
  1971. {
  1972. return repo_config_get_bool(the_repository, key, dest);
  1973. }
  1974. int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
  1975. {
  1976. return repo_config_get_bool_or_int(the_repository, key, is_bool, dest);
  1977. }
  1978. int git_config_get_maybe_bool(const char *key, int *dest)
  1979. {
  1980. return repo_config_get_maybe_bool(the_repository, key, dest);
  1981. }
  1982. int git_config_get_pathname(const char *key, const char **dest)
  1983. {
  1984. return repo_config_get_pathname(the_repository, key, dest);
  1985. }
  1986. int git_config_get_expiry(const char *key, const char **output)
  1987. {
  1988. int ret = git_config_get_string(key, (char **)output);
  1989. if (ret)
  1990. return ret;
  1991. if (strcmp(*output, "now")) {
  1992. timestamp_t now = approxidate("now");
  1993. if (approxidate(*output) >= now)
  1994. git_die_config(key, _("Invalid %s: '%s'"), key, *output);
  1995. }
  1996. return ret;
  1997. }
  1998. int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestamp_t now)
  1999. {
  2000. const char *expiry_string;
  2001. intmax_t days;
  2002. timestamp_t when;
  2003. if (git_config_get_string_tmp(key, &expiry_string))
  2004. return 1; /* no such thing */
  2005. if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) {
  2006. const int scale = 86400;
  2007. *expiry = now - days * scale;
  2008. return 0;
  2009. }
  2010. if (!parse_expiry_date(expiry_string, &when)) {
  2011. *expiry = when;
  2012. return 0;
  2013. }
  2014. return -1; /* thing exists but cannot be parsed */
  2015. }
  2016. int git_config_get_split_index(void)
  2017. {
  2018. int val;
  2019. if (!git_config_get_maybe_bool("core.splitindex", &val))
  2020. return val;
  2021. return -1; /* default value */
  2022. }
  2023. int git_config_get_max_percent_split_change(void)
  2024. {
  2025. int val = -1;
  2026. if (!git_config_get_int("splitindex.maxpercentchange", &val)) {
  2027. if (0 <= val && val <= 100)
  2028. return val;
  2029. return error(_("splitIndex.maxPercentChange value '%d' "
  2030. "should be between 0 and 100"), val);
  2031. }
  2032. return -1; /* default value */
  2033. }
  2034. int git_config_get_fsmonitor(void)
  2035. {
  2036. if (git_config_get_pathname("core.fsmonitor", &core_fsmonitor))
  2037. core_fsmonitor = getenv("GIT_TEST_FSMONITOR");
  2038. if (core_fsmonitor && !*core_fsmonitor)
  2039. core_fsmonitor = NULL;
  2040. if (core_fsmonitor)
  2041. return 1;
  2042. return 0;
  2043. }
  2044. int git_config_get_index_threads(int *dest)
  2045. {
  2046. int is_bool, val;
  2047. val = git_env_ulong("GIT_TEST_INDEX_THREADS", 0);
  2048. if (val) {
  2049. *dest = val;
  2050. return 0;
  2051. }
  2052. if (!git_config_get_bool_or_int("index.threads", &is_bool, &val)) {
  2053. if (is_bool)
  2054. *dest = val ? 0 : 1;
  2055. else
  2056. *dest = val;
  2057. return 0;
  2058. }
  2059. return 1;
  2060. }
  2061. NORETURN
  2062. void git_die_config_linenr(const char *key, const char *filename, int linenr)
  2063. {
  2064. if (!filename)
  2065. die(_("unable to parse '%s' from command-line config"), key);
  2066. else
  2067. die(_("bad config variable '%s' in file '%s' at line %d"),
  2068. key, filename, linenr);
  2069. }
  2070. NORETURN __attribute__((format(printf, 2, 3)))
  2071. void git_die_config(const char *key, const char *err, ...)
  2072. {
  2073. const struct string_list *values;
  2074. struct key_value_info *kv_info;
  2075. if (err) {
  2076. va_list params;
  2077. va_start(params, err);
  2078. vreportf("error: ", err, params);
  2079. va_end(params);
  2080. }
  2081. values = git_config_get_value_multi(key);
  2082. kv_info = values->items[values->nr - 1].util;
  2083. git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
  2084. }
  2085. /*
  2086. * Find all the stuff for git_config_set() below.
  2087. */
  2088. struct config_store_data {
  2089. size_t baselen;
  2090. char *key;
  2091. int do_not_match;
  2092. regex_t *value_regex;
  2093. int multi_replace;
  2094. struct {
  2095. size_t begin, end;
  2096. enum config_event_t type;
  2097. int is_keys_section;
  2098. } *parsed;
  2099. unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc;
  2100. unsigned int key_seen:1, section_seen:1, is_keys_section:1;
  2101. };
  2102. static void config_store_data_clear(struct config_store_data *store)
  2103. {
  2104. free(store->key);
  2105. if (store->value_regex != NULL &&
  2106. store->value_regex != CONFIG_REGEX_NONE) {
  2107. regfree(store->value_regex);
  2108. free(store->value_regex);
  2109. }
  2110. free(store->parsed);
  2111. free(store->seen);
  2112. memset(store, 0, sizeof(*store));
  2113. }
  2114. static int matches(const char *key, const char *value,
  2115. const struct config_store_data *store)
  2116. {
  2117. if (strcmp(key, store->key))
  2118. return 0; /* not ours */
  2119. if (!store->value_regex)
  2120. return 1; /* always matches */
  2121. if (store->value_regex == CONFIG_REGEX_NONE)
  2122. return 0; /* never matches */
  2123. return store->do_not_match ^
  2124. (value && !regexec(store->value_regex, value, 0, NULL, 0));
  2125. }
  2126. static int store_aux_event(enum config_event_t type,
  2127. size_t begin, size_t end, void *data)
  2128. {
  2129. struct config_store_data *store = data;
  2130. ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
  2131. store->parsed[store->parsed_nr].begin = begin;
  2132. store->parsed[store->parsed_nr].end = end;
  2133. store->parsed[store->parsed_nr].type = type;
  2134. if (type == CONFIG_EVENT_SECTION) {
  2135. int (*cmpfn)(const char *, const char *, size_t);
  2136. if (cf->var.len < 2 || cf->var.buf[cf->var.len - 1] != '.')
  2137. return error(_("invalid section name '%s'"), cf->var.buf);
  2138. if (cf->subsection_case_sensitive)
  2139. cmpfn = strncasecmp;
  2140. else
  2141. cmpfn = strncmp;
  2142. /* Is this the section we were looking for? */
  2143. store->is_keys_section =
  2144. store->parsed[store->parsed_nr].is_keys_section =
  2145. cf->var.len - 1 == store->baselen &&
  2146. !cmpfn(cf->var.buf, store->key, store->baselen);
  2147. if (store->is_keys_section) {
  2148. store->section_seen = 1;
  2149. ALLOC_GROW(store->seen, store->seen_nr + 1,
  2150. store->seen_alloc);
  2151. store->seen[store->seen_nr] = store->parsed_nr;
  2152. }
  2153. }
  2154. store->parsed_nr++;
  2155. return 0;
  2156. }
  2157. static int store_aux(const char *key, const char *value, void *cb)
  2158. {
  2159. struct config_store_data *store = cb;
  2160. if (store->key_seen) {
  2161. if (matches(key, value, store)) {
  2162. if (store->seen_nr == 1 && store->multi_replace == 0) {
  2163. warning(_("%s has multiple values"), key);
  2164. }
  2165. ALLOC_GROW(store->seen, store->seen_nr + 1,
  2166. store->seen_alloc);
  2167. store->seen[store->seen_nr] = store->parsed_nr;
  2168. store->seen_nr++;
  2169. }
  2170. } else if (store->is_keys_section) {
  2171. /*
  2172. * Do not increment matches yet: this may not be a match, but we
  2173. * are in the desired section.
  2174. */
  2175. ALLOC_GROW(store->seen, store->seen_nr + 1, store->seen_alloc);
  2176. store->seen[store->seen_nr] = store->parsed_nr;
  2177. store->section_seen = 1;
  2178. if (matches(key, value, store)) {
  2179. store->seen_nr++;
  2180. store->key_seen = 1;
  2181. }
  2182. }
  2183. return 0;
  2184. }
  2185. static int write_error(const char *filename)
  2186. {
  2187. error(_("failed to write new configuration file %s"), filename);
  2188. /* Same error code as "failed to rename". */
  2189. return 4;
  2190. }
  2191. static struct strbuf store_create_section(const char *key,
  2192. const struct config_store_data *store)
  2193. {
  2194. const char *dot;
  2195. size_t i;
  2196. struct strbuf sb = STRBUF_INIT;
  2197. dot = memchr(key, '.', store->baselen);
  2198. if (dot) {
  2199. strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
  2200. for (i = dot - key + 1; i < store->baselen; i++) {
  2201. if (key[i] == '"' || key[i] == '\\')
  2202. strbuf_addch(&sb, '\\');
  2203. strbuf_addch(&sb, key[i]);
  2204. }
  2205. strbuf_addstr(&sb, "\"]\n");
  2206. } else {
  2207. strbuf_addch(&sb, '[');
  2208. strbuf_add(&sb, key, store->baselen);
  2209. strbuf_addstr(&sb, "]\n");
  2210. }
  2211. return sb;
  2212. }
  2213. static ssize_t write_section(int fd, const char *key,
  2214. const struct config_store_data *store)
  2215. {
  2216. struct strbuf sb = store_create_section(key, store);
  2217. ssize_t ret;
  2218. ret = write_in_full(fd, sb.buf, sb.len);
  2219. strbuf_release(&sb);
  2220. return ret;
  2221. }
  2222. static ssize_t write_pair(int fd, const char *key, const char *value,
  2223. const struct config_store_data *store)
  2224. {
  2225. int i;
  2226. ssize_t ret;
  2227. const char *quote = "";
  2228. struct strbuf sb = STRBUF_INIT;
  2229. /*
  2230. * Check to see if the value needs to be surrounded with a dq pair.
  2231. * Note that problematic characters are always backslash-quoted; this
  2232. * check is about not losing leading or trailing SP and strings that
  2233. * follow beginning-of-comment characters (i.e. ';' and '#') by the
  2234. * configuration parser.
  2235. */
  2236. if (value[0] == ' ')
  2237. quote = "\"";
  2238. for (i = 0; value[i]; i++)
  2239. if (value[i] == ';' || value[i] == '#')
  2240. quote = "\"";
  2241. if (i && value[i - 1] == ' ')
  2242. quote = "\"";
  2243. strbuf_addf(&sb, "\t%s = %s", key + store->baselen + 1, quote);
  2244. for (i = 0; value[i]; i++)
  2245. switch (value[i]) {
  2246. case '\n':
  2247. strbuf_addstr(&sb, "\\n");
  2248. break;
  2249. case '\t':
  2250. strbuf_addstr(&sb, "\\t");
  2251. break;
  2252. case '"':
  2253. case '\\':
  2254. strbuf_addch(&sb, '\\');
  2255. /* fallthrough */
  2256. default:
  2257. strbuf_addch(&sb, value[i]);
  2258. break;
  2259. }
  2260. strbuf_addf(&sb, "%s\n", quote);
  2261. ret = write_in_full(fd, sb.buf, sb.len);
  2262. strbuf_release(&sb);
  2263. return ret;
  2264. }
  2265. /*
  2266. * If we are about to unset the last key(s) in a section, and if there are
  2267. * no comments surrounding (or included in) the section, we will want to
  2268. * extend begin/end to remove the entire section.
  2269. *
  2270. * Note: the parameter `seen_ptr` points to the index into the store.seen
  2271. * array. * This index may be incremented if a section has more than one
  2272. * entry (which all are to be removed).
  2273. */
  2274. static void maybe_remove_section(struct config_store_data *store,
  2275. size_t *begin_offset, size_t *end_offset,
  2276. int *seen_ptr)
  2277. {
  2278. size_t begin;
  2279. int i, seen, section_seen = 0;
  2280. /*
  2281. * First, ensure that this is the first key, and that there are no
  2282. * comments before the entry nor before the section header.
  2283. */
  2284. seen = *seen_ptr;
  2285. for (i = store->seen[seen]; i > 0; i--) {
  2286. enum config_event_t type = store->parsed[i - 1].type;
  2287. if (type == CONFIG_EVENT_COMMENT)
  2288. /* There is a comment before this entry or section */
  2289. return;
  2290. if (type == CONFIG_EVENT_ENTRY) {
  2291. if (!section_seen)
  2292. /* This is not the section's first entry. */
  2293. return;
  2294. /* We encountered no comment before the section. */
  2295. break;
  2296. }
  2297. if (type == CONFIG_EVENT_SECTION) {
  2298. if (!store->parsed[i - 1].is_keys_section)
  2299. break;
  2300. section_seen = 1;
  2301. }
  2302. }
  2303. begin = store->parsed[i].begin;
  2304. /*
  2305. * Next, make sure that we are removing he last key(s) in the section,
  2306. * and that there are no comments that are possibly about the current
  2307. * section.
  2308. */
  2309. for (i = store->seen[seen] + 1; i < store->parsed_nr; i++) {
  2310. enum config_event_t type = store->parsed[i].type;
  2311. if (type == CONFIG_EVENT_COMMENT)
  2312. return;
  2313. if (type == CONFIG_EVENT_SECTION) {
  2314. if (store->parsed[i].is_keys_section)
  2315. continue;
  2316. break;
  2317. }
  2318. if (type == CONFIG_EVENT_ENTRY) {
  2319. if (++seen < store->seen_nr &&
  2320. i == store->seen[seen])
  2321. /* We want to remove this entry, too */
  2322. continue;
  2323. /* There is another entry in this section. */
  2324. return;
  2325. }
  2326. }
  2327. /*
  2328. * We are really removing the last entry/entries from this section, and
  2329. * there are no enclosed or surrounding comments. Remove the entire,
  2330. * now-empty section.
  2331. */
  2332. *seen_ptr = seen;
  2333. *begin_offset = begin;
  2334. if (i < store->parsed_nr)
  2335. *end_offset = store->parsed[i].begin;
  2336. else
  2337. *end_offset = store->parsed[store->parsed_nr - 1].end;
  2338. }
  2339. int git_config_set_in_file_gently(const char *config_filename,
  2340. const char *key, const char *value)
  2341. {
  2342. return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0);
  2343. }
  2344. void git_config_set_in_file(const char *config_filename,
  2345. const char *key, const char *value)
  2346. {
  2347. git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
  2348. }
  2349. int git_config_set_gently(const char *key, const char *value)
  2350. {
  2351. return git_config_set_multivar_gently(key, value, NULL, 0);
  2352. }
  2353. void git_config_set(const char *key, const char *value)
  2354. {
  2355. git_config_set_multivar(key, value, NULL, 0);
  2356. trace2_cmd_set_config(key, value);
  2357. }
  2358. /*
  2359. * If value==NULL, unset in (remove from) config,
  2360. * if value_regex!=NULL, disregard key/value pairs where value does not match.
  2361. * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
  2362. * (only add a new one)
  2363. * if multi_replace==0, nothing, or only one matching key/value is replaced,
  2364. * else all matching key/values (regardless how many) are removed,
  2365. * before the new pair is written.
  2366. *
  2367. * Returns 0 on success.
  2368. *
  2369. * This function does this:
  2370. *
  2371. * - it locks the config file by creating ".git/config.lock"
  2372. *
  2373. * - it then parses the config using store_aux() as validator to find
  2374. * the position on the key/value pair to replace. If it is to be unset,
  2375. * it must be found exactly once.
  2376. *
  2377. * - the config file is mmap()ed and the part before the match (if any) is
  2378. * written to the lock file, then the changed part and the rest.
  2379. *
  2380. * - the config file is removed and the lock file rename()d to it.
  2381. *
  2382. */
  2383. int git_config_set_multivar_in_file_gently(const char *config_filename,
  2384. const char *key, const char *value,
  2385. const char *value_regex,
  2386. int multi_replace)
  2387. {
  2388. int fd = -1, in_fd = -1;
  2389. int ret;
  2390. struct lock_file lock = LOCK_INIT;
  2391. char *filename_buf = NULL;
  2392. char *contents = NULL;
  2393. size_t contents_sz;
  2394. struct config_store_data store;
  2395. memset(&store, 0, sizeof(store));
  2396. /* parse-key returns negative; flip the sign to feed exit(3) */
  2397. ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
  2398. if (ret)
  2399. goto out_free;
  2400. store.multi_replace = multi_replace;
  2401. if (!config_filename)
  2402. config_filename = filename_buf = git_pathdup("config");
  2403. /*
  2404. * The lock serves a purpose in addition to locking: the new
  2405. * contents of .git/config will be written into it.
  2406. */
  2407. fd = hold_lock_file_for_update(&lock, config_filename, 0);
  2408. if (fd < 0) {
  2409. error_errno(_("could not lock config file %s"), config_filename);
  2410. ret = CONFIG_NO_LOCK;
  2411. goto out_free;
  2412. }
  2413. /*
  2414. * If .git/config does not exist yet, write a minimal version.
  2415. */
  2416. in_fd = open(config_filename, O_RDONLY);
  2417. if ( in_fd < 0 ) {
  2418. if ( ENOENT != errno ) {
  2419. error_errno(_("opening %s"), config_filename);
  2420. ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
  2421. goto out_free;
  2422. }
  2423. /* if nothing to unset, error out */
  2424. if (value == NULL) {
  2425. ret = CONFIG_NOTHING_SET;
  2426. goto out_free;
  2427. }
  2428. free(store.key);
  2429. store.key = xstrdup(key);
  2430. if (write_section(fd, key, &store) < 0 ||
  2431. write_pair(fd, key, value, &store) < 0)
  2432. goto write_err_out;
  2433. } else {
  2434. struct stat st;
  2435. size_t copy_begin, copy_end;
  2436. int i, new_line = 0;
  2437. struct config_options opts;
  2438. if (value_regex == NULL)
  2439. store.value_regex = NULL;
  2440. else if (value_regex == CONFIG_REGEX_NONE)
  2441. store.value_regex = CONFIG_REGEX_NONE;
  2442. else {
  2443. if (value_regex[0] == '!') {
  2444. store.do_not_match = 1;
  2445. value_regex++;
  2446. } else
  2447. store.do_not_match = 0;
  2448. store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
  2449. if (regcomp(store.value_regex, value_regex,
  2450. REG_EXTENDED)) {
  2451. error(_("invalid pattern: %s"), value_regex);
  2452. FREE_AND_NULL(store.value_regex);
  2453. ret = CONFIG_INVALID_PATTERN;
  2454. goto out_free;
  2455. }
  2456. }
  2457. ALLOC_GROW(store.parsed, 1, store.parsed_alloc);
  2458. store.parsed[0].end = 0;
  2459. memset(&opts, 0, sizeof(opts));
  2460. opts.event_fn = store_aux_event;
  2461. opts.event_fn_data = &store;
  2462. /*
  2463. * After this, store.parsed will contain offsets of all the
  2464. * parsed elements, and store.seen will contain a list of
  2465. * matches, as indices into store.parsed.
  2466. *
  2467. * As a side effect, we make sure to transform only a valid
  2468. * existing config file.
  2469. */
  2470. if (git_config_from_file_with_options(store_aux,
  2471. config_filename,
  2472. &store, &opts)) {
  2473. error(_("invalid config file %s"), config_filename);
  2474. ret = CONFIG_INVALID_FILE;
  2475. goto out_free;
  2476. }
  2477. /* if nothing to unset, or too many matches, error out */
  2478. if ((store.seen_nr == 0 && value == NULL) ||
  2479. (store.seen_nr > 1 && multi_replace == 0)) {
  2480. ret = CONFIG_NOTHING_SET;
  2481. goto out_free;
  2482. }
  2483. if (fstat(in_fd, &st) == -1) {
  2484. error_errno(_("fstat on %s failed"), config_filename);
  2485. ret = CONFIG_INVALID_FILE;
  2486. goto out_free;
  2487. }
  2488. contents_sz = xsize_t(st.st_size);
  2489. contents = xmmap_gently(NULL, contents_sz, PROT_READ,
  2490. MAP_PRIVATE, in_fd, 0);
  2491. if (contents == MAP_FAILED) {
  2492. if (errno == ENODEV && S_ISDIR(st.st_mode))
  2493. errno = EISDIR;
  2494. error_errno(_("unable to mmap '%s'"), config_filename);
  2495. ret = CONFIG_INVALID_FILE;
  2496. contents = NULL;
  2497. goto out_free;
  2498. }
  2499. close(in_fd);
  2500. in_fd = -1;
  2501. if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
  2502. error_errno(_("chmod on %s failed"), get_lock_file_path(&lock));
  2503. ret = CONFIG_NO_WRITE;
  2504. goto out_free;
  2505. }
  2506. if (store.seen_nr == 0) {
  2507. if (!store.seen_alloc) {
  2508. /* Did not see key nor section */
  2509. ALLOC_GROW(store.seen, 1, store.seen_alloc);
  2510. store.seen[0] = store.parsed_nr
  2511. - !!store.parsed_nr;
  2512. }
  2513. store.seen_nr = 1;
  2514. }
  2515. for (i = 0, copy_begin = 0; i < store.seen_nr; i++) {
  2516. size_t replace_end;
  2517. int j = store.seen[i];
  2518. new_line = 0;
  2519. if (!store.key_seen) {
  2520. copy_end = store.parsed[j].end;
  2521. /* include '\n' when copying section header */
  2522. if (copy_end > 0 && copy_end < contents_sz &&
  2523. contents[copy_end - 1] != '\n' &&
  2524. contents[copy_end] == '\n')
  2525. copy_end++;
  2526. replace_end = copy_end;
  2527. } else {
  2528. replace_end = store.parsed[j].end;
  2529. copy_end = store.parsed[j].begin;
  2530. if (!value)
  2531. maybe_remove_section(&store,
  2532. &copy_end,
  2533. &replace_end, &i);
  2534. /*
  2535. * Swallow preceding white-space on the same
  2536. * line.
  2537. */
  2538. while (copy_end > 0 ) {
  2539. char c = contents[copy_end - 1];
  2540. if (isspace(c) && c != '\n')
  2541. copy_end--;
  2542. else
  2543. break;
  2544. }
  2545. }
  2546. if (copy_end > 0 && contents[copy_end-1] != '\n')
  2547. new_line = 1;
  2548. /* write the first part of the config */
  2549. if (copy_end > copy_begin) {
  2550. if (write_in_full(fd, contents + copy_begin,
  2551. copy_end - copy_begin) < 0)
  2552. goto write_err_out;
  2553. if (new_line &&
  2554. write_str_in_full(fd, "\n") < 0)
  2555. goto write_err_out;
  2556. }
  2557. copy_begin = replace_end;
  2558. }
  2559. /* write the pair (value == NULL means unset) */
  2560. if (value != NULL) {
  2561. if (!store.section_seen) {
  2562. if (write_section(fd, key, &store) < 0)
  2563. goto write_err_out;
  2564. }
  2565. if (write_pair(fd, key, value, &store) < 0)
  2566. goto write_err_out;
  2567. }
  2568. /* write the rest of the config */
  2569. if (copy_begin < contents_sz)
  2570. if (write_in_full(fd, contents + copy_begin,
  2571. contents_sz - copy_begin) < 0)
  2572. goto write_err_out;
  2573. munmap(contents, contents_sz);
  2574. contents = NULL;
  2575. }
  2576. if (commit_lock_file(&lock) < 0) {
  2577. error_errno(_("could not write config file %s"), config_filename);
  2578. ret = CONFIG_NO_WRITE;
  2579. goto out_free;
  2580. }
  2581. ret = 0;
  2582. /* Invalidate the config cache */
  2583. git_config_clear();
  2584. out_free:
  2585. rollback_lock_file(&lock);
  2586. free(filename_buf);
  2587. if (contents)
  2588. munmap(contents, contents_sz);
  2589. if (in_fd >= 0)
  2590. close(in_fd);
  2591. config_store_data_clear(&store);
  2592. return ret;
  2593. write_err_out:
  2594. ret = write_error(get_lock_file_path(&lock));
  2595. goto out_free;
  2596. }
  2597. void git_config_set_multivar_in_file(const char *config_filename,
  2598. const char *key, const char *value,
  2599. const char *value_regex, int multi_replace)
  2600. {
  2601. if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
  2602. value_regex, multi_replace))
  2603. return;
  2604. if (value)
  2605. die(_("could not set '%s' to '%s'"), key, value);
  2606. else
  2607. die(_("could not unset '%s'"), key);
  2608. }
  2609. int git_config_set_multivar_gently(const char *key, const char *value,
  2610. const char *value_regex, int multi_replace)
  2611. {
  2612. return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
  2613. multi_replace);
  2614. }
  2615. void git_config_set_multivar(const char *key, const char *value,
  2616. const char *value_regex, int multi_replace)
  2617. {
  2618. git_config_set_multivar_in_file(NULL, key, value, value_regex,
  2619. multi_replace);
  2620. }
  2621. static int section_name_match (const char *buf, const char *name)
  2622. {
  2623. int i = 0, j = 0, dot = 0;
  2624. if (buf[i] != '[')
  2625. return 0;
  2626. for (i = 1; buf[i] && buf[i] != ']'; i++) {
  2627. if (!dot && isspace(buf[i])) {
  2628. dot = 1;
  2629. if (name[j++] != '.')
  2630. break;
  2631. for (i++; isspace(buf[i]); i++)
  2632. ; /* do nothing */
  2633. if (buf[i] != '"')
  2634. break;
  2635. continue;
  2636. }
  2637. if (buf[i] == '\\' && dot)
  2638. i++;
  2639. else if (buf[i] == '"' && dot) {
  2640. for (i++; isspace(buf[i]); i++)
  2641. ; /* do_nothing */
  2642. break;
  2643. }
  2644. if (buf[i] != name[j++])
  2645. break;
  2646. }
  2647. if (buf[i] == ']' && name[j] == 0) {
  2648. /*
  2649. * We match, now just find the right length offset by
  2650. * gobbling up any whitespace after it, as well
  2651. */
  2652. i++;
  2653. for (; buf[i] && isspace(buf[i]); i++)
  2654. ; /* do nothing */
  2655. return i;
  2656. }
  2657. return 0;
  2658. }
  2659. static int section_name_is_ok(const char *name)
  2660. {
  2661. /* Empty section names are bogus. */
  2662. if (!*name)
  2663. return 0;
  2664. /*
  2665. * Before a dot, we must be alphanumeric or dash. After the first dot,
  2666. * anything goes, so we can stop checking.
  2667. */
  2668. for (; *name && *name != '.'; name++)
  2669. if (*name != '-' && !isalnum(*name))
  2670. return 0;
  2671. return 1;
  2672. }
  2673. /* if new_name == NULL, the section is removed instead */
  2674. static int git_config_copy_or_rename_section_in_file(const char *config_filename,
  2675. const char *old_name,
  2676. const char *new_name, int copy)
  2677. {
  2678. int ret = 0, remove = 0;
  2679. char *filename_buf = NULL;
  2680. struct lock_file lock = LOCK_INIT;
  2681. int out_fd;
  2682. char buf[1024];
  2683. FILE *config_file = NULL;
  2684. struct stat st;
  2685. struct strbuf copystr = STRBUF_INIT;
  2686. struct config_store_data store;
  2687. memset(&store, 0, sizeof(store));
  2688. if (new_name && !section_name_is_ok(new_name)) {
  2689. ret = error(_("invalid section name: %s"), new_name);
  2690. goto out_no_rollback;
  2691. }
  2692. if (!config_filename)
  2693. config_filename = filename_buf = git_pathdup("config");
  2694. out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
  2695. if (out_fd < 0) {
  2696. ret = error(_("could not lock config file %s"), config_filename);
  2697. goto out;
  2698. }
  2699. if (!(config_file = fopen(config_filename, "rb"))) {
  2700. ret = warn_on_fopen_errors(config_filename);
  2701. if (ret)
  2702. goto out;
  2703. /* no config file means nothing to rename, no error */
  2704. goto commit_and_out;
  2705. }
  2706. if (fstat(fileno(config_file), &st) == -1) {
  2707. ret = error_errno(_("fstat on %s failed"), config_filename);
  2708. goto out;
  2709. }
  2710. if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
  2711. ret = error_errno(_("chmod on %s failed"),
  2712. get_lock_file_path(&lock));
  2713. goto out;
  2714. }
  2715. while (fgets(buf, sizeof(buf), config_file)) {
  2716. unsigned i;
  2717. int length;
  2718. int is_section = 0;
  2719. char *output = buf;
  2720. for (i = 0; buf[i] && isspace(buf[i]); i++)
  2721. ; /* do nothing */
  2722. if (buf[i] == '[') {
  2723. /* it's a section */
  2724. int offset;
  2725. is_section = 1;
  2726. /*
  2727. * When encountering a new section under -c we
  2728. * need to flush out any section we're already
  2729. * coping and begin anew. There might be
  2730. * multiple [branch "$name"] sections.
  2731. */
  2732. if (copystr.len > 0) {
  2733. if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
  2734. ret = write_error(get_lock_file_path(&lock));
  2735. goto out;
  2736. }
  2737. strbuf_reset(&copystr);
  2738. }
  2739. offset = section_name_match(&buf[i], old_name);
  2740. if (offset > 0) {
  2741. ret++;
  2742. if (new_name == NULL) {
  2743. remove = 1;
  2744. continue;
  2745. }
  2746. store.baselen = strlen(new_name);
  2747. if (!copy) {
  2748. if (write_section(out_fd, new_name, &store) < 0) {
  2749. ret = write_error(get_lock_file_path(&lock));
  2750. goto out;
  2751. }
  2752. /*
  2753. * We wrote out the new section, with
  2754. * a newline, now skip the old
  2755. * section's length
  2756. */
  2757. output += offset + i;
  2758. if (strlen(output) > 0) {
  2759. /*
  2760. * More content means there's
  2761. * a declaration to put on the
  2762. * next line; indent with a
  2763. * tab
  2764. */
  2765. output -= 1;
  2766. output[0] = '\t';
  2767. }
  2768. } else {
  2769. copystr = store_create_section(new_name, &store);
  2770. }
  2771. }
  2772. remove = 0;
  2773. }
  2774. if (remove)
  2775. continue;
  2776. length = strlen(output);
  2777. if (!is_section && copystr.len > 0) {
  2778. strbuf_add(&copystr, output, length);
  2779. }
  2780. if (write_in_full(out_fd, output, length) < 0) {
  2781. ret = write_error(get_lock_file_path(&lock));
  2782. goto out;
  2783. }
  2784. }
  2785. /*
  2786. * Copy a trailing section at the end of the config, won't be
  2787. * flushed by the usual "flush because we have a new section
  2788. * logic in the loop above.
  2789. */
  2790. if (copystr.len > 0) {
  2791. if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
  2792. ret = write_error(get_lock_file_path(&lock));
  2793. goto out;
  2794. }
  2795. strbuf_reset(&copystr);
  2796. }
  2797. fclose(config_file);
  2798. config_file = NULL;
  2799. commit_and_out:
  2800. if (commit_lock_file(&lock) < 0)
  2801. ret = error_errno(_("could not write config file %s"),
  2802. config_filename);
  2803. out:
  2804. if (config_file)
  2805. fclose(config_file);
  2806. rollback_lock_file(&lock);
  2807. out_no_rollback:
  2808. free(filename_buf);
  2809. config_store_data_clear(&store);
  2810. return ret;
  2811. }
  2812. int git_config_rename_section_in_file(const char *config_filename,
  2813. const char *old_name, const char *new_name)
  2814. {
  2815. return git_config_copy_or_rename_section_in_file(config_filename,
  2816. old_name, new_name, 0);
  2817. }
  2818. int git_config_rename_section(const char *old_name, const char *new_name)
  2819. {
  2820. return git_config_rename_section_in_file(NULL, old_name, new_name);
  2821. }
  2822. int git_config_copy_section_in_file(const char *config_filename,
  2823. const char *old_name, const char *new_name)
  2824. {
  2825. return git_config_copy_or_rename_section_in_file(config_filename,
  2826. old_name, new_name, 1);
  2827. }
  2828. int git_config_copy_section(const char *old_name, const char *new_name)
  2829. {
  2830. return git_config_copy_section_in_file(NULL, old_name, new_name);
  2831. }
  2832. /*
  2833. * Call this to report error for your variable that should not
  2834. * get a boolean value (i.e. "[my] var" means "true").
  2835. */
  2836. #undef config_error_nonbool
  2837. int config_error_nonbool(const char *var)
  2838. {
  2839. return error(_("missing value for '%s'"), var);
  2840. }
  2841. int parse_config_key(const char *var,
  2842. const char *section,
  2843. const char **subsection, size_t *subsection_len,
  2844. const char **key)
  2845. {
  2846. const char *dot;
  2847. /* Does it start with "section." ? */
  2848. if (!skip_prefix(var, section, &var) || *var != '.')
  2849. return -1;
  2850. /*
  2851. * Find the key; we don't know yet if we have a subsection, but we must
  2852. * parse backwards from the end, since the subsection may have dots in
  2853. * it, too.
  2854. */
  2855. dot = strrchr(var, '.');
  2856. *key = dot + 1;
  2857. /* Did we have a subsection at all? */
  2858. if (dot == var) {
  2859. if (subsection) {
  2860. *subsection = NULL;
  2861. *subsection_len = 0;
  2862. }
  2863. }
  2864. else {
  2865. if (!subsection)
  2866. return -1;
  2867. *subsection = var + 1;
  2868. *subsection_len = dot - *subsection;
  2869. }
  2870. return 0;
  2871. }
  2872. const char *current_config_origin_type(void)
  2873. {
  2874. int type;
  2875. if (current_config_kvi)
  2876. type = current_config_kvi->origin_type;
  2877. else if(cf)
  2878. type = cf->origin_type;
  2879. else
  2880. BUG("current_config_origin_type called outside config callback");
  2881. switch (type) {
  2882. case CONFIG_ORIGIN_BLOB:
  2883. return "blob";
  2884. case CONFIG_ORIGIN_FILE:
  2885. return "file";
  2886. case CONFIG_ORIGIN_STDIN:
  2887. return "standard input";
  2888. case CONFIG_ORIGIN_SUBMODULE_BLOB:
  2889. return "submodule-blob";
  2890. case CONFIG_ORIGIN_CMDLINE:
  2891. return "command line";
  2892. default:
  2893. BUG("unknown config origin type");
  2894. }
  2895. }
  2896. const char *config_scope_name(enum config_scope scope)
  2897. {
  2898. switch (scope) {
  2899. case CONFIG_SCOPE_SYSTEM:
  2900. return "system";
  2901. case CONFIG_SCOPE_GLOBAL:
  2902. return "global";
  2903. case CONFIG_SCOPE_LOCAL:
  2904. return "local";
  2905. case CONFIG_SCOPE_WORKTREE:
  2906. return "worktree";
  2907. case CONFIG_SCOPE_COMMAND:
  2908. return "command";
  2909. case CONFIG_SCOPE_SUBMODULE:
  2910. return "submodule";
  2911. default:
  2912. return "unknown";
  2913. }
  2914. }
  2915. const char *current_config_name(void)
  2916. {
  2917. const char *name;
  2918. if (current_config_kvi)
  2919. name = current_config_kvi->filename;
  2920. else if (cf)
  2921. name = cf->name;
  2922. else
  2923. BUG("current_config_name called outside config callback");
  2924. return name ? name : "";
  2925. }
  2926. enum config_scope current_config_scope(void)
  2927. {
  2928. if (current_config_kvi)
  2929. return current_config_kvi->scope;
  2930. else
  2931. return current_parsing_scope;
  2932. }
  2933. int current_config_line(void)
  2934. {
  2935. if (current_config_kvi)
  2936. return current_config_kvi->linenr;
  2937. else
  2938. return cf->linenr;
  2939. }
  2940. int lookup_config(const char **mapping, int nr_mapping, const char *var)
  2941. {
  2942. int i;
  2943. for (i = 0; i < nr_mapping; i++) {
  2944. const char *name = mapping[i];
  2945. if (name && !strcasecmp(var, name))
  2946. return i;
  2947. }
  2948. return -1;
  2949. }