+static void safe_create_dir(const char *dir)
+{
+ if (mkdir(dir, 0755) < 0) {
+ if (errno != EEXIST) {
+ perror(dir);
+ exit(1);
+ }
+ }
+}
+
+static void create_default_files(const char *git_dir)
+{
+ unsigned len = strlen(git_dir);
+ static char path[PATH_MAX];
+
+ 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}
+ */
+ strcpy(path + len, "refs");
+ safe_create_dir(path);
+ strcpy(path + len, "refs/heads");
+ safe_create_dir(path);
+ strcpy(path + len, "refs/tags");
+ safe_create_dir(path);
+
+ /*
+ * Create the default symlink from ".git/HEAD" to the "master"
+ * branch
+ */
+ strcpy(path + len, "HEAD");
+ if (symlink("refs/heads/master", path) < 0) {
+ if (errno != EEXIST) {
+ perror(path);
+ exit(1);
+ }
+ }
+}
+