1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#include"cache.h" 7 8static voidsafe_create_dir(const char*dir) 9{ 10if(mkdir(dir,0777) <0) { 11if(errno != EEXIST) { 12perror(dir); 13exit(1); 14} 15} 16} 17 18static voidcreate_default_files(const char*git_dir) 19{ 20unsigned len =strlen(git_dir); 21static char path[PATH_MAX]; 22 23if(len >sizeof(path)-50) 24die("insane git directory%s", git_dir); 25memcpy(path, git_dir, len); 26 27if(len && path[len-1] !='/') 28 path[len++] ='/'; 29 30/* 31 * Create .git/refs/{heads,tags} 32 */ 33strcpy(path + len,"refs"); 34safe_create_dir(path); 35strcpy(path + len,"refs/heads"); 36safe_create_dir(path); 37strcpy(path + len,"refs/tags"); 38safe_create_dir(path); 39 40/* 41 * Create the default symlink from ".git/HEAD" to the "master" 42 * branch 43 */ 44strcpy(path + len,"HEAD"); 45if(symlink("refs/heads/master", path) <0) { 46if(errno != EEXIST) { 47perror(path); 48exit(1); 49} 50} 51} 52 53/* 54 * If you want to, you can share the DB area with any number of branches. 55 * That has advantages: you can save space by sharing all the SHA1 objects. 56 * On the other hand, it might just make lookup slower and messier. You 57 * be the judge. The default case is to have one DB per managed directory. 58 */ 59intmain(int argc,char**argv) 60{ 61const char*git_dir; 62const char*sha1_dir; 63char*path; 64int len, i; 65 66/* 67 * Set up the default .git directory contents 68 */ 69 git_dir =gitenv(GIT_DIR_ENVIRONMENT); 70if(!git_dir) { 71 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; 72fprintf(stderr,"defaulting to local storage area\n"); 73} 74safe_create_dir(git_dir); 75create_default_files(git_dir); 76 77/* 78 * And set up the object store. 79 */ 80 sha1_dir =get_object_directory(); 81 len =strlen(sha1_dir); 82 path =xmalloc(len +40); 83memcpy(path, sha1_dir, len); 84 85safe_create_dir(sha1_dir); 86for(i =0; i <256; i++) { 87sprintf(path+len,"/%02x", i); 88safe_create_dir(path); 89} 90strcpy(path+len,"/pack"); 91safe_create_dir(path); 92return0; 93}