Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/var.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / var.c` 403 lines ยท 7989 bytes Raw Blame History 1 /* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Eric Biederman, 2005 5 */ 6 7 #define USE_THE_REPOSITORY_VARIABLE 8 9 #include "builtin.h" 10 11 #include "attr.h" 12 #include "config.h" 13 #include "editor.h" 14 #include "environment.h" 15 #include "ident.h" 16 #include "pager.h" 17 #include "parse-options.h" 18 #include "path.h" 19 #include "refs.h" 20 #include "run-command.h" 21 #include "strbuf.h" 22 #include "string-list.h" 23 24 static const char * const var_usage[] = { 25 N_("git var [-z] -l"), 26 N_("git var [-z] ..."), 27 NULL 28 }; 29 30 enum ident_part { 31 IDENT_NAME, 32 IDENT_MAIL, 33 IDENT_DATE, 34 }; 35 36 static char *ident_part(const char *ident, enum ident_part part) 37 { 38 struct ident_split split; 39 40 if (!ident) 41 return NULL; 42 if (split_ident_line(&split, ident, strlen(ident))) 43 return NULL; 44 45 switch (part) { 46 case IDENT_NAME: 47 if (!split.name_begin || !split.name_end) 48 BUG("split_ident_line() gave NULL names???"); 49 return xmemdupz(split.name_begin, 50 split.name_end - split.name_begin); 51 case IDENT_MAIL: 52 if (!split.mail_begin || !split.mail_end) 53 BUG("split_ident_line() gave NULL mail???"); 54 return xmemdupz(split.mail_begin, 55 split.mail_end - split.mail_begin); 56 case IDENT_DATE: 57 if (!split.date_begin || !split.tz_end) 58 BUG("split_ident_line() gave NULL date/tz???"); 59 return xmemdupz(split.date_begin, 60 split.tz_end - split.date_begin); 61 default: 62 BUG("unknown ident_part %d", part); 63 } 64 } 65 66 static char *committer(int ident_flag) 67 { 68 return xstrdup_or_null(git_committer_info(ident_flag)); 69 } 70 71 static char *committer_name(int ident_flag) 72 { 73 return ident_part(git_committer_info(ident_flag), IDENT_NAME); 74 } 75 76 static char *committer_email(int ident_flag) 77 { 78 return ident_part(git_committer_info(ident_flag), IDENT_MAIL); 79 } 80 81 static char *committer_date(int ident_flag) 82 { 83 return ident_part(git_committer_info(ident_flag), IDENT_DATE); 84 } 85 86 static char *author(int ident_flag) 87 { 88 return xstrdup_or_null(git_author_info(ident_flag)); 89 } 90 91 static char *author_name(int ident_flag) 92 { 93 return ident_part(git_author_info(ident_flag), IDENT_NAME); 94 } 95 96 static char *author_email(int ident_flag) 97 { 98 return ident_part(git_author_info(ident_flag), IDENT_MAIL); 99 } 100 101 static char *author_date(int ident_flag) 102 { 103 return ident_part(git_author_info(ident_flag), IDENT_DATE); 104 } 105 106 static char *editor(int ident_flag UNUSED) 107 { 108 return xstrdup_or_null(git_editor()); 109 } 110 111 static char *sequence_editor(int ident_flag UNUSED) 112 { 113 return xstrdup_or_null(git_sequence_editor()); 114 } 115 116 static char *pager(int ident_flag UNUSED) 117 { 118 const char *pgm = git_pager(the_repository, 1); 119 120 if (!pgm) 121 pgm = "cat"; 122 return xstrdup(pgm); 123 } 124 125 static char *default_branch(int ident_flag UNUSED) 126 { 127 return repo_default_branch_name(the_repository, 1); 128 } 129 130 static char *shell_path(int ident_flag UNUSED) 131 { 132 return git_shell_path(); 133 } 134 135 static char *git_attr_val_system(int ident_flag UNUSED) 136 { 137 if (git_attr_system_is_enabled()) { 138 char *file = xstrdup(git_attr_system_file()); 139 normalize_path_copy(file, file); 140 return file; 141 } 142 return NULL; 143 } 144 145 static char *git_attr_val_global(int ident_flag UNUSED) 146 { 147 char *file = xstrdup_or_null(git_attr_global_file()); 148 if (file) { 149 normalize_path_copy(file, file); 150 return file; 151 } 152 return NULL; 153 } 154 155 static char *git_config_val_system(int ident_flag UNUSED) 156 { 157 if (git_config_system()) { 158 char *file = git_system_config(); 159 normalize_path_copy(file, file); 160 return file; 161 } 162 return NULL; 163 } 164 165 static void git_config_val_global(struct string_list *list) 166 { 167 char *user, *xdg; 168 169 git_global_config_paths(&user, &xdg); 170 if (xdg && *xdg) { 171 normalize_path_copy(xdg, xdg); 172 string_list_append(list, xdg); 173 } 174 if (user && *user) { 175 normalize_path_copy(user, user); 176 string_list_append(list, user); 177 } 178 free(xdg); 179 free(user); 180 } 181 182 static char *hostname(int ident_flag UNUSED) 183 { 184 char buf[HOST_NAME_MAX + 1]; 185 186 if (xgethostname(buf, sizeof(buf))) 187 return NULL; 188 return xstrdup(buf); 189 } 190 191 struct git_var { 192 const char *name; 193 char *(*read)(int); 194 void (*multiread)(struct string_list *); 195 }; 196 static struct git_var git_vars[] = { 197 { 198 .name = "GIT_COMMITTER_IDENT", 199 .read = committer, 200 }, 201 { 202 .name = "GIT_COMMITTER_NAME", 203 .read = committer_name, 204 }, 205 { 206 .name = "GIT_COMMITTER_EMAIL", 207 .read = committer_email, 208 }, 209 { 210 .name = "GIT_COMMITTER_DATE", 211 .read = committer_date, 212 }, 213 { 214 .name = "GIT_AUTHOR_IDENT", 215 .read = author, 216 }, 217 { 218 .name = "GIT_AUTHOR_NAME", 219 .read = author_name, 220 }, 221 { 222 .name = "GIT_AUTHOR_EMAIL", 223 .read = author_email, 224 }, 225 { 226 .name = "GIT_AUTHOR_DATE", 227 .read = author_date, 228 }, 229 { 230 .name = "GIT_EDITOR", 231 .read = editor, 232 }, 233 { 234 .name = "GIT_SEQUENCE_EDITOR", 235 .read = sequence_editor, 236 }, 237 { 238 .name = "GIT_PAGER", 239 .read = pager, 240 }, 241 { 242 .name = "GIT_DEFAULT_BRANCH", 243 .read = default_branch, 244 }, 245 { 246 .name = "GIT_SHELL_PATH", 247 .read = shell_path, 248 }, 249 { 250 .name = "GIT_ATTR_SYSTEM", 251 .read = git_attr_val_system, 252 }, 253 { 254 .name = "GIT_ATTR_GLOBAL", 255 .read = git_attr_val_global, 256 }, 257 { 258 .name = "GIT_CONFIG_SYSTEM", 259 .read = git_config_val_system, 260 }, 261 { 262 .name = "GIT_CONFIG_GLOBAL", 263 .multiread = git_config_val_global, 264 }, 265 { 266 .name = "GIT_HOSTNAME", 267 .read = hostname, 268 }, 269 { 270 .name = "", 271 .read = NULL, 272 }, 273 }; 274 275 static void list_vars(int nul_term) 276 { 277 struct git_var *ptr; 278 char delim = nul_term ? '\n' : '='; 279 char term = nul_term ? '\0' : '\n'; 280 281 for (ptr = git_vars; ptr->read || ptr->multiread; ptr++) { 282 if (ptr->read) { 283 char *val = ptr->read(0); 284 285 if (val) { 286 printf("%s%c%s%c", ptr->name, delim, val, term); 287 free(val); 288 } 289 } else { 290 struct string_list list = STRING_LIST_INIT_DUP; 291 292 ptr->multiread(&list); 293 for (size_t i = 0; i < list.nr; i++) 294 printf("%s%c%s%c", ptr->name, delim, 295 list.items[i].string, term); 296 string_list_clear(&list, 0); 297 } 298 } 299 } 300 301 static const struct git_var *get_git_var(const char *var) 302 { 303 struct git_var *ptr; 304 for (ptr = git_vars; ptr->read || ptr->multiread; ptr++) { 305 if (strcmp(var, ptr->name) == 0) { 306 return ptr; 307 } 308 } 309 return NULL; 310 } 311 312 static int show_config(const char *var, const char *value, 313 const struct config_context *ctx, void *cb) 314 { 315 int *nul_term = cb; 316 char delim = *nul_term ? '\n' : '='; 317 char term = *nul_term ? '\0' : '\n'; 318 319 if (value) 320 printf("%s%c%s%c", var, delim, value, term); 321 else 322 printf("%s%c", var, term); 323 return git_default_config(var, value, ctx, cb); 324 } 325 326 int cmd_var(int argc, 327 const char **argv, 328 const char *prefix, 329 struct repository *repo UNUSED) 330 { 331 int list = 0; 332 int nul_term = 0; 333 char delim; 334 char term; 335 struct option options[] = { 336 OPT_BOOL('l', NULL, &list, 337 N_("list all variables")), 338 OPT_BOOL('z', NULL, &nul_term, 339 N_("terminate entries with NUL")), 340 OPT_END(), 341 }; 342 343 argc = parse_options(argc, argv, prefix, options, 344 var_usage, PARSE_OPT_STOP_AT_NON_OPTION); 345 346 if (list) { 347 if (argc) 348 usage_with_options(var_usage, options); 349 repo_config(the_repository, show_config, &nul_term); 350 list_vars(nul_term); 351 return 0; 352 } 353 354 if (!argc) 355 usage_with_options(var_usage, options); 356 357 repo_config(the_repository, git_default_config, NULL); 358 359 delim = nul_term ? '\n' : '='; 360 term = nul_term ? '\0' : '\n'; 361 362 for (int i = 0; i < argc; i++) { 363 const struct git_var *git_var = get_git_var(argv[i]); 364 365 if (!git_var) 366 usage_with_options(var_usage, options); 367 368 if (git_var->read) { 369 char *val = git_var->read(IDENT_STRICT); 370 371 if (!val) { 372 if (argc == 1) 373 return 1; 374 continue; 375 } 376 if (argc == 1) 377 printf("%s%c", val, term); 378 else 379 printf("%s%c%s%c", git_var->name, delim, 380 val, term); 381 free(val); 382 } else { 383 struct string_list list = STRING_LIST_INIT_DUP; 384 385 git_var->multiread(&list); 386 if (argc == 1 && !list.nr) { 387 string_list_clear(&list, 0); 388 return 1; 389 } 390 for (size_t j = 0; j < list.nr; j++) { 391 if (argc == 1) 392 printf("%s%c", list.items[j].string, 393 term); 394 else 395 printf("%s%c%s%c", git_var->name, delim, 396 list.items[j].string, term); 397 } 398 string_list_clear(&list, 0); 399 } 400 } 401 402 return 0; 403 }