Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
git
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
f3bb8b4
Documentation
block-sha1
builtin
add.c
annotate.c
apply.c
archive.c
bisect--helper.c
blame.c
branch.c
bundle.c
cat-file.c
check-attr.c
check-ref-format.c
checkout-index.c
checkout.c
clean.c
clone.c
commit-tree.c
commit.c
config.c
count-objects.c
describe.c
diff-files.c
diff-index.c
diff-tree.c
diff.c
fast-export.c
fetch-pack.c
fetch.c
fmt-merge-msg.c
for-each-ref.c
fsck.c
gc.c
grep.c
hash-object.c
help.c
index-pack.c
init-db.c
log.c
ls-files.c
ls-remote.c
ls-tree.c
mailinfo.c
mailsplit.c
merge-base.c
merge-file.c
merge-index.c
merge-ours.c
merge-recursive.c
merge-tree.c
merge.c
mktag.c
mktree.c
mv.c
name-rev.c
notes.c
pack-objects.c
pack-redundant.c
pack-refs.c
patch-id.c
prune-packed.c
prune.c
push.c
read-tree.c
receive-pack.c
reflog.c
remote-ext.c
remote-fd.c
remote.c
replace.c
rerere.c
reset.c
rev-list.c
rev-parse.c
revert.c
rm.c
send-pack.c
shortlog.c
show-branch.c
show-ref.c
stripspace.c
symbolic-ref.c
tag.c
tar-tree.c
unpack-file.c
unpack-objects.c
update-index.c
update-ref.c
update-server-info.c
upload-archive.c
var.c
verify-pack.c
verify-tag.c
write-tree.c
compat
contrib
git-gui
git_remote_helpers
gitk-git
gitweb
perl
ppc
t
templates
vcs-svn
xdiff
.gitattributes
.gitignore
.mailmap
COPYING
GIT-VERSION-GEN
INSTALL
Makefile
README
RelNotes
abspath.c
aclocal.m4
advice.c
advice.h
alias.c
alloc.c
archive-tar.c
archive-zip.c
archive.c
archive.h
attr.c
attr.h
base85.c
bisect.c
bisect.h
blob.c
blob.h
branch.c
branch.h
builtin.h
bundle.c
bundle.h
cache-tree.c
cache-tree.h
cache.h
check-builtins.sh
check-racy.c
check_bindir
color.c
color.h
combine-diff.c
command-list.txt
commit.c
commit.h
config.c
config.mak.in
configure.ac
connect.c
convert.c
copy.c
csum-file.c
csum-file.h
ctype.c
daemon.c
date.c
decorate.c
decorate.h
delta.h
diff-delta.c
diff-lib.c
diff-no-index.c
diff.c
diff.h
diffcore-break.c
diffcore-delta.c
diffcore-order.c
diffcore-pickaxe.c
diffcore-rename.c
diffcore.h
dir.c
dir.h
editor.c
entry.c
environment.c
exec_cmd.c
exec_cmd.h
fast-import.c
fetch-pack.h
fixup-builtins
fsck.c
fsck.h
generate-cmdlist.sh
git-add--interactive.perl
git-am.sh
git-archimport.perl
git-bisect.sh
git-compat-util.h
git-cvsexportcommit.perl
git-cvsimport.perl
git-cvsserver.perl
git-difftool--helper.sh
git-difftool.perl
git-filter-branch.sh
git-instaweb.sh
git-lost-found.sh
git-merge-octopus.sh
git-merge-one-file.sh
git-merge-resolve.sh
git-mergetool--lib.sh
git-mergetool.sh
git-parse-remote.sh
git-pull.sh
git-quiltimport.sh
git-rebase--interactive.sh
git-rebase.sh
git-relink.perl
git-remote-testgit.py
git-repack.sh
git-request-pull.sh
git-send-email.perl
git-sh-setup.sh
git-stash.sh
git-submodule.sh
git-svn.perl
git-web--browse.sh
git.c
git.spec.in
graph.c
graph.h
grep.c
grep.h
hash.c
hash.h
help.c
help.h
hex.c
http-backend.c
http-fetch.c
http-push.c
http-walker.c
http.c
http.h
ident.c
imap-send.c
levenshtein.c
levenshtein.h
list-objects.c
list-objects.h
ll-merge.c
ll-merge.h
lockfile.c
log-tree.c
log-tree.h
mailmap.c
mailmap.h
match-trees.c
merge-file.c
merge-recursive.c
merge-recursive.h
name-hash.c
notes-cache.c
notes-cache.h
notes-merge.c
notes-merge.h
notes.c
notes.h
object.c
object.h
pack-check.c
pack-refs.c
pack-refs.h
pack-revindex.c
pack-revindex.h
pack-write.c
pack.h
pager.c
parse-options.c
parse-options.h
patch-delta.c
patch-ids.c
patch-ids.h
path.c
pkt-line.c
pkt-line.h
preload-index.c
pretty.c
progress.c
progress.h
quote.c
quote.h
reachable.c
reachable.h
read-cache.c
reflog-walk.c
reflog-walk.h
refs.c
refs.h
remote-curl.c
remote.c
remote.h
replace_object.c
rerere.c
rerere.h
resolve-undo.c
resolve-undo.h
revision.c
revision.h
run-command.c
run-command.h
send-pack.h
server-info.c
setup.c
sha1-lookup.c
sha1-lookup.h
sha1_file.c
sha1_name.c
shallow.c
shell.c
shortlog.h
show-index.c
sideband.c
sideband.h
sigchain.c
sigchain.h
strbuf.c
strbuf.h
string-list.c
string-list.h
submodule.c
submodule.h
symlinks.c
tag.c
tag.h
tar.h
test-chmtime.c
test-ctype.c
test-date.c
test-delta.c
test-dump-cache-tree.c
test-genrandom.c
test-index-version.c
test-line-buffer.c
test-match-trees.c
test-obj-pool.c
test-parse-options.c
test-path-utils.c
test-run-command.c
test-sha1.c
test-sha1.sh
test-sigchain.c
test-string-pool.c
test-subprocess.c
test-svn-fe.c
test-treap.c
thread-utils.c
thread-utils.h
trace.c
transport-helper.c
transport.c
transport.h
tree-diff.c
tree-walk.c
tree-walk.h
tree.c
tree.h
unimplemented.sh
unpack-trees.c
unpack-trees.h
upload-pack.c
url.c
url.h
usage.c
userdiff.c
userdiff.h
utf8.c
utf8.h
walker.c
walker.h
wrap-for-bin.sh
wrapper.c
write_or_die.c
ws.c
wt-status.c
wt-status.h
xdiff-interface.c
xdiff-interface.h
zlib.c
Breadcrumbs
git
/
builtin
/
init-db.c
Blame
Blame
Latest commit
History
History
530 lines (468 loc) · 14.2 KB
Breadcrumbs
git
/
builtin
/
init-db.c
Top
File metadata and controls
Code
Blame
530 lines (468 loc) · 14.2 KB
Raw
/* * GIT - The information manager from hell * * Copyright (C) Linus Torvalds, 2005 */ #include "cache.h" #include "builtin.h" #include "exec_cmd.h" #include "parse-options.h" #ifndef DEFAULT_GIT_TEMPLATE_DIR #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" #endif #ifdef NO_TRUSTABLE_FILEMODE #define TEST_FILEMODE 0 #else #define TEST_FILEMODE 1 #endif static int init_is_bare_repository = 0; static int init_shared_repository = -1; static const char *init_db_template_dir; static void safe_create_dir(const char *dir, int share) { if (mkdir(dir, 0777) < 0) { if (errno != EEXIST) { perror(dir); exit(1); } } else if (share && adjust_shared_perm(dir)) die("Could not make %s writable by group", dir); } static void copy_templates_1(char *path, int baselen, char *template, int template_baselen, DIR *dir) { struct dirent *de; /* Note: if ".git/hooks" file exists in the repository being * re-initialized, /etc/core-git/templates/hooks/update would * cause "git init" to fail here. I think this is sane but * it means that the set of templates we ship by default, along * with the way the namespace under .git/ is organized, should * be really carefully chosen. */ safe_create_dir(path, 1); while ((de = readdir(dir)) != NULL) { struct stat st_git, st_template; int namelen; int exists = 0; if (de->d_name[0] == '.') continue; namelen = strlen(de->d_name); if ((PATH_MAX <= baselen + namelen) || (PATH_MAX <= template_baselen + namelen)) die("insanely long template name %s", de->d_name); memcpy(path + baselen, de->d_name, namelen+1); memcpy(template + template_baselen, de->d_name, namelen+1); if (lstat(path, &st_git)) { if (errno != ENOENT) die_errno("cannot stat '%s'", path); } else exists = 1; if (lstat(template, &st_template)) die_errno("cannot stat template '%s'", template); if (S_ISDIR(st_template.st_mode)) { DIR *subdir = opendir(template); int baselen_sub = baselen + namelen; int template_baselen_sub = template_baselen + namelen; if (!subdir) die_errno("cannot opendir '%s'", template); path[baselen_sub++] = template[template_baselen_sub++] = '/'; path[baselen_sub] = template[template_baselen_sub] = 0; copy_templates_1(path, baselen_sub, template, template_baselen_sub, subdir); closedir(subdir); } else if (exists) continue; else if (S_ISLNK(st_template.st_mode)) { char lnk[256]; int len; len = readlink(template, lnk, sizeof(lnk)); if (len < 0) die_errno("cannot readlink '%s'", template); if (sizeof(lnk) <= len) die("insanely long symlink %s", template); lnk[len] = 0; if (symlink(lnk, path)) die_errno("cannot symlink '%s' '%s'", lnk, path); } else if (S_ISREG(st_template.st_mode)) { if (copy_file(path, template, st_template.st_mode)) die_errno("cannot copy '%s' to '%s'", template, path); } else error("ignoring template %s", template); } } static void copy_templates(const char *template_dir) { char path[PATH_MAX]; char template_path[PATH_MAX]; int template_len; DIR *dir; const char *git_dir = get_git_dir(); int len = strlen(git_dir); if (!template_dir) template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); if (!template_dir) template_dir = init_db_template_dir; if (!template_dir) template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); if (!template_dir[0]) return; template_len = strlen(template_dir); if (PATH_MAX <= (template_len+strlen("/config"))) die("insanely long template path %s", template_dir); strcpy(template_path, template_dir); if (template_path[template_len-1] != '/') { template_path[template_len++] = '/'; template_path[template_len] = 0; } dir = opendir(template_path); if (!dir) { warning("templates not found %s", template_dir); return; } /* Make sure that template is from the correct vintage */ strcpy(template_path + template_len, "config"); repository_format_version = 0; git_config_from_file(check_repository_format_version, template_path, NULL); template_path[template_len] = 0; if (repository_format_version && repository_format_version != GIT_REPO_VERSION) { warning("not copying templates of " "a wrong format version %d from '%s'", repository_format_version, template_dir); closedir(dir); return; } memcpy(path, git_dir, len); if (len && path[len - 1] != '/') path[len++] = '/'; path[len] = 0; copy_templates_1(path, len, template_path, template_len, dir); closedir(dir); } static int git_init_db_config(const char *k, const char *v, void *cb) { if (!strcmp(k, "init.templatedir")) return git_config_pathname(&init_db_template_dir, k, v); return 0; } static int create_default_files(const char *template_path) { const char *git_dir = get_git_dir(); unsigned len = strlen(git_dir); static char path[PATH_MAX]; struct stat st1; char repo_version_string[10]; char junk[2]; int reinit; int filemode; if (len > sizeof(path)-50) die("insane git directory %s", git_dir); memcpy(path, git_dir, len); if (len && path[len-1] != '/') path[len++] = '/'; /* * Create .git/refs/{heads,tags} */ safe_create_dir(git_path("refs"), 1); safe_create_dir(git_path("refs/heads"), 1); safe_create_dir(git_path("refs/tags"), 1); /* Just look for `init.templatedir` */ git_config(git_init_db_config, NULL); /* First copy the templates -- we might have the default * config file there, in which case we would want to read * from it after installing. */ copy_templates(template_path); git_config(git_default_config, NULL); is_bare_repository_cfg = init_is_bare_repository; /* reading existing config may have overwrote it */ if (init_shared_repository != -1) shared_repository = init_shared_repository; /* * We would have created the above under user's umask -- under * shared-repository settings, we would need to fix them up. */ if (shared_repository) { adjust_shared_perm(get_git_dir()); adjust_shared_perm(git_path("refs")); adjust_shared_perm(git_path("refs/heads")); adjust_shared_perm(git_path("refs/tags")); } /* * Create the default symlink from ".git/HEAD" to the "master" * branch, if it does not exist yet. */ strcpy(path + len, "HEAD"); reinit = (!access(path, R_OK) || readlink(path, junk, sizeof(junk)-1) != -1); if (!reinit) { if (create_symref("HEAD", "refs/heads/master", NULL) < 0) exit(1); } /* This forces creation of new config file */ sprintf(repo_version_string, "%d", GIT_REPO_VERSION); git_config_set("core.repositoryformatversion", repo_version_string); path[len] = 0; strcpy(path + len, "config"); /* Check filemode trustability */ filemode = TEST_FILEMODE; if (TEST_FILEMODE && !lstat(path, &st1)) { struct stat st2; filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && !lstat(path, &st2) && st1.st_mode != st2.st_mode); } git_config_set("core.filemode", filemode ? "true" : "false"); if (is_bare_repository()) git_config_set("core.bare", "true"); else { const char *work_tree = get_git_work_tree(); git_config_set("core.bare", "false"); /* allow template config file to override the default */ if (log_all_ref_updates == -1) git_config_set("core.logallrefupdates", "true"); if (prefixcmp(git_dir, work_tree) || strcmp(git_dir + strlen(work_tree), "/.git")) { git_config_set("core.worktree", work_tree); } } if (!reinit) { /* Check if symlink is supported in the work tree */ path[len] = 0; strcpy(path + len, "tXXXXXX"); if (!close(xmkstemp(path)) && !unlink(path) && !symlink("testing", path) && !lstat(path, &st1) && S_ISLNK(st1.st_mode)) unlink(path); /* good */ else git_config_set("core.symlinks", "false"); /* Check if the filesystem is case-insensitive */ path[len] = 0; strcpy(path + len, "CoNfIg"); if (!access(path, F_OK)) git_config_set("core.ignorecase", "true"); } return reinit; } static void create_object_directory(void) { const char *object_directory = get_object_directory(); int len = strlen(object_directory); char *path = xmalloc(len + 40); memcpy(path, object_directory, len); safe_create_dir(object_directory, 1); strcpy(path+len, "/pack"); safe_create_dir(path, 1); strcpy(path+len, "/info"); safe_create_dir(path, 1); free(path); } int init_db(const char *template_dir, unsigned int flags) { int reinit; safe_create_dir(get_git_dir(), 0); init_is_bare_repository = is_bare_repository(); /* Check to see if the repository version is right. * Note that a newly created repository does not have * config file, so this will not fail. What we are catching * is an attempt to reinitialize new repository with an old tool. */ check_repository_format(); reinit = create_default_files(template_dir); create_object_directory(); if (shared_repository) { char buf[10]; /* We do not spell "group" and such, so that * the configuration can be read by older version * of git. Note, we use octal numbers for new share modes, * and compatibility values for PERM_GROUP and * PERM_EVERYBODY. */ if (shared_repository < 0) /* force to the mode value */ sprintf(buf, "0%o", -shared_repository); else if (shared_repository == PERM_GROUP) sprintf(buf, "%d", OLD_PERM_GROUP); else if (shared_repository == PERM_EVERYBODY) sprintf(buf, "%d", OLD_PERM_EVERYBODY); else die("oops"); git_config_set("core.sharedrepository", buf); git_config_set("receive.denyNonFastforwards", "true"); } if (!(flags & INIT_DB_QUIET)) { const char *git_dir = get_git_dir(); int len = strlen(git_dir); printf("%s%s Git repository in %s%s\n", reinit ? "Reinitialized existing" : "Initialized empty", shared_repository ? " shared" : "", git_dir, len && git_dir[len-1] != '/' ? "/" : ""); } return 0; } static int guess_repository_type(const char *git_dir) { char cwd[PATH_MAX]; const char *slash; /* * "GIT_DIR=. git init" is always bare. * "GIT_DIR=`pwd` git init" too. */ if (!strcmp(".", git_dir)) return 1; if (!getcwd(cwd, sizeof(cwd))) die_errno("cannot tell cwd"); if (!strcmp(git_dir, cwd)) return 1; /* * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. */ if (!strcmp(git_dir, ".git")) return 0; slash = strrchr(git_dir, '/'); if (slash && !strcmp(slash, "/.git")) return 0; /* * Otherwise it is often bare. At this point * we are just guessing. */ return 1; } static int shared_callback(const struct option *opt, const char *arg, int unset) { *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; return 0; } static const char *const init_db_usage[] = { "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]", NULL }; /* * If you want to, you can share the DB area with any number of branches. * That has advantages: you can save space by sharing all the SHA1 objects. * On the other hand, it might just make lookup slower and messier. You * be the judge. The default case is to have one DB per managed directory. */ int cmd_init_db(int argc, const char **argv, const char *prefix) { const char *git_dir; const char *work_tree; const char *template_dir = NULL; unsigned int flags = 0; const struct option init_db_options[] = { OPT_STRING(0, "template", &template_dir, "template-directory", "provide the directory from which templates will be used"), OPT_SET_INT(0, "bare", &is_bare_repository_cfg, "create a bare repository", 1), { OPTION_CALLBACK, 0, "shared", &init_shared_repository, "permissions", "specify that the git repository is to be shared amongst several users", PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET), OPT_END() }; argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); if (argc == 1) { int mkdir_tried = 0; retry: if (chdir(argv[0]) < 0) { if (!mkdir_tried) { int saved; /* * At this point we haven't read any configuration, * and we know shared_repository should always be 0; * but just in case we play safe. */ saved = shared_repository; shared_repository = 0; switch (safe_create_leading_directories_const(argv[0])) { case -3: errno = EEXIST; /* fallthru */ case -1: die_errno("cannot mkdir %s", argv[0]); break; default: break; } shared_repository = saved; if (mkdir(argv[0], 0777) < 0) die_errno("cannot mkdir %s", argv[0]); mkdir_tried = 1; goto retry; } die_errno("cannot chdir to %s", argv[0]); } } else if (0 < argc) { usage(init_db_usage[0]); } if (is_bare_repository_cfg == 1) { static char git_dir[PATH_MAX+1]; setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), argc > 0); } if (init_shared_repository != -1) shared_repository = init_shared_repository; /* * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR * without --bare. Catch the error early. */ git_dir = getenv(GIT_DIR_ENVIRONMENT); work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) die("%s (or --work-tree=<directory>) not allowed without " "specifying %s (or --git-dir=<directory>)", GIT_WORK_TREE_ENVIRONMENT, GIT_DIR_ENVIRONMENT); /* * Set up the default .git directory contents */ if (!git_dir) git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; if (is_bare_repository_cfg < 0) is_bare_repository_cfg = guess_repository_type(git_dir); if (!is_bare_repository_cfg) { if (git_dir) { const char *git_dir_parent = strrchr(git_dir, '/'); if (git_dir_parent) { char *rel = xstrndup(git_dir, git_dir_parent - git_dir); git_work_tree_cfg = xstrdup(make_absolute_path(rel)); free(rel); } } if (!git_work_tree_cfg) { git_work_tree_cfg = xcalloc(PATH_MAX, 1); if (!getcwd(git_work_tree_cfg, PATH_MAX)) die_errno ("Cannot access current working directory"); } if (work_tree) set_git_work_tree(make_absolute_path(work_tree)); else set_git_work_tree(git_work_tree_cfg); if (access(get_git_work_tree(), X_OK)) die_errno ("Cannot access work tree '%s'", get_git_work_tree()); } else { if (work_tree) set_git_work_tree(make_absolute_path(work_tree)); } set_git_dir(make_absolute_path(git_dir)); return init_db(template_dir, flags); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
You can’t perform that action at this time.