* [PATCH] builtin-clone: Implement git clone as a builtin command.
@ 2007-12-11 19:57 Kristian Høgsberg
2007-12-11 20:59 ` Daniel Barkalow
0 siblings, 1 reply; 13+ messages in thread
From: Kristian Høgsberg @ 2007-12-11 19:57 UTC (permalink / raw)
To: gister; +Cc: git
Still work-in-progress, local clones and --reference not fully functional.
---
Ok, don't flame me, I know this isn't appropriate at the moment with
stabilization for 1.5.4 going on, but I just wanted to post a heads up
on this work to avoid duplicate effort. It's one big patch at this point
and I haven't even run the test suite yet, but that will change.
cheers,
Kristian
Makefile | 2 +-
builtin-clone.c | 504 +++++++++++++++++++++++++
builtin-init-db.c | 119 +++----
builtin-rerere.c | 19 +-
builtin.h | 1 +
cache.h | 5 +
git-clone.sh => contrib/examples/git-clone.sh | 0
copy.c | 21 +
diff.c | 8 +-
git.c | 1 +
unpack-trees.c | 3 +-
unpack-trees.h | 1 +
12 files changed, 595 insertions(+), 89 deletions(-)
create mode 100644 builtin-clone.c
rename git-clone.sh => contrib/examples/git-clone.sh (100%)
diff --git a/Makefile b/Makefile
index cb1cbb1..ca42ed1 100644
--- a/Makefile
+++ b/Makefile
@@ -213,7 +213,6 @@ BASIC_LDFLAGS =
SCRIPT_SH = \
git-bisect.sh git-checkout.sh \
- git-clone.sh \
git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
git-pull.sh git-rebase.sh git-rebase--interactive.sh \
git-repack.sh git-request-pull.sh \
@@ -327,6 +326,7 @@ BUILTIN_OBJS = \
builtin-checkout-index.o \
builtin-check-ref-format.o \
builtin-clean.o \
+ builtin-clone.o \
builtin-commit.o \
builtin-commit-tree.o \
builtin-count-objects.o \
diff --git a/builtin-clone.c b/builtin-clone.c
new file mode 100644
index 0000000..acd7beb
--- /dev/null
+++ b/builtin-clone.c
@@ -0,0 +1,504 @@
+/*
+ * Builtin "git clone"
+ *
+ * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
+ * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
+ *
+ * Clone a repository into a different directory that does not yet exist.
+ */
+
+#include "cache.h"
+#include "parse-options.h"
+#include "fetch-pack.h"
+#include "refs.h"
+#include "tree.h"
+#include "tree-walk.h"
+#include "unpack-trees.h"
+
+/*
+ * Overall FIXMEs:
+ * - respect DB_ENVIRONMENT for .git/objects.
+ * - error path cleanup of dirs+files.
+ *
+ * Implementation notes:
+ * - dropping use-separate-remote and no-separate-remote compatibility
+ *
+ */
+static const char * const builtin_clone_usage[] = {
+ "git-clone [options] [--] <repo> [<dir>]",
+ NULL
+};
+
+static int option_quiet, option_no_checkout, option_bare;
+static int option_local, option_no_hardlinks, option_shared, option_depth;
+static char *option_template, *option_reference;
+static char *option_origin = "origin";
+static char *option_upload_pack = "git-upload-pack";
+
+static struct option builtin_clone_options[] = {
+ OPT__QUIET(&option_quiet),
+ OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
+ "don't create a checkout"),
+ OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
+ OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
+ OPT_BOOLEAN('l', "local", &option_local,
+ "to clone from a local repository"),
+ OPT_BOOLEAN(0, "no-hardlinks", &option_no_hardlinks,
+ "don't use local hardlinks, always copy"),
+ OPT_BOOLEAN('s', "shared", &option_shared,
+ "setup as shared repository"),
+ OPT_STRING(0, "template", &option_template, "path",
+ "path the template repository"),
+ OPT_STRING(0, "reference", &option_reference, "repo",
+ "reference repository"),
+ OPT_STRING('o', "origin", &option_origin, "branch",
+ "use <branch> instead or 'origin' to track upstream"),
+ OPT_STRING('u', "upload-pack", &option_upload_pack, "path",
+ "path to git-upload-pack on the remote"),
+ OPT_INTEGER(0, "depth", &option_depth,
+ "create a shallow clone of that depth"),
+
+ OPT_END()
+};
+
+static char *get_repo_path(const char *repo)
+{
+ const char *path;
+ struct stat buf;
+
+ path = mkpath("%s/.git", repo);
+ if (!stat(path, &buf) && S_ISDIR(buf.st_mode))
+ return xstrdup(make_absolute_path(path));
+
+ path = mkpath("%s.git", repo);
+ if (!stat(path, &buf) && S_ISDIR(buf.st_mode))
+ return xstrdup(make_absolute_path(path));
+
+ if (!stat(repo, &buf) && S_ISDIR(buf.st_mode))
+ return xstrdup(make_absolute_path(repo));
+
+ return NULL;
+}
+
+static char *guess_dir_name(const char *repo)
+{
+ const char *p, *start, *end, *limit;
+ int after_slash_or_colon;
+
+ /* Guess dir name from repository: strip trailing '/',
+ * strip trailing '[:/]*git', strip leading '.*[/:]'. */
+
+ after_slash_or_colon = 1;
+ limit = repo + strlen(repo);
+ start = repo;
+ end = limit;
+ for (p = repo; p < limit; p++) {
+ if (!prefixcmp(p, ".git")) {
+ if (!after_slash_or_colon)
+ end = p;
+ p += 3;
+ } else if (*p == '/' || *p == ':') {
+ if (end == limit)
+ end = p;
+ after_slash_or_colon = 1;
+ } else if (after_slash_or_colon) {
+ start = p;
+ end = limit;
+ after_slash_or_colon = 0;
+ }
+ }
+
+ return xstrndup(start, end - start);
+}
+
+static void
+write_alternates_file(const char *repo, const char *reference)
+{
+ char *file;
+ char *alternates;
+ int fd;
+
+ file = mkpath("%s/objects/info/alternates", repo);
+ fd = open(file, O_CREAT | O_WRONLY, 0666);
+ if (fd < 0)
+ die("failed to create %s", file);
+ alternates = mkpath("%s/objects\n", reference);
+ write_or_die(fd, alternates, strlen(alternates));
+ if (close(fd))
+ die("could not close %s", file);
+}
+
+static int
+setup_tmp_ref(const char *refname,
+ const unsigned char *sha1, int flags, void *cb_data)
+{
+ const char *ref_temp = cb_data;
+ char *path;
+ struct lock_file lk;
+ struct ref_lock *rl;
+
+ /*
+
+ echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
+ (
+ GIT_DIR="$ref_git" git for-each-ref \
+ --format='%(objectname) %(*objectname)'
+ ) |
+ while read a b
+ do
+ test -z "$a" ||
+ git update-ref "refs/reference-tmp/$a" "$a"
+ test -z "$b" ||
+ git update-ref "refs/reference-tmp/$b" "$b"
+ done
+
+ */
+
+ /* We go a bit out of way to use write_ref_sha1() here. We
+ * could just write the ref file directly, since neither
+ * locking or reflog really matters here. However, let's use
+ * the standard interface for writing refs as much as is
+ * possible given that get_git_dir() != the repo we're writing
+ * the refs in. */
+
+ printf("%s -> %s/%s\n",
+ sha1_to_hex(sha1), ref_temp, sha1_to_hex(sha1));
+
+ path = mkpath("%s/%s", ref_temp, sha1_to_hex(sha1));
+ rl = xmalloc(sizeof *rl);
+ rl->force_write = 1;
+ rl->lk = &lk;
+ rl->ref_name = xstrdup(sha1_to_hex(sha1));
+ rl->orig_ref_name = xstrdup(rl->ref_name);
+ rl->lock_fd = hold_lock_file_for_update(rl->lk, path, 1);
+ if (write_ref_sha1(rl, sha1, NULL) < 0)
+ die("failed to write temporary ref %s", lk.filename);
+
+ return 0;
+}
+
+static char *
+setup_reference(const char *repo)
+{
+ struct stat buf;
+ const char *ref_git;
+ char *ref_temp;
+
+ if (!option_reference)
+ return NULL;
+
+ ref_git = make_absolute_path(option_reference);
+
+ if (!stat(mkpath("%s/.git/objects", ref_git), &buf) &&
+ S_ISDIR(buf.st_mode))
+ ref_git = mkpath("%s/.git", ref_git);
+ else if (stat(mkpath("%s/objects", ref_git), &buf) ||
+ !S_ISDIR(buf.st_mode))
+ die("reference repository '%s' is not a local directory.",
+ option_reference);
+
+ set_git_dir(ref_git);
+
+ write_alternates_file(repo, ref_git);
+
+ ref_temp = xstrdup(mkpath("%s/refs/reference-tmp", repo));
+ if (mkdir(ref_temp, 0777))
+ die("could not create directory %s", ref_temp);
+ for_each_ref(setup_tmp_ref, (void *) ref_temp);
+
+ return ref_temp;
+}
+
+static void
+cleanup_reference(char *ref_temp)
+{
+ struct dirent *de;
+ DIR *dir;
+
+ if (!ref_temp)
+ return;
+ dir = opendir(ref_temp);
+ if (!dir) {
+ if (errno == ENOENT)
+ return;
+ die("failed to open directory %s", ref_temp);
+ }
+
+ while ((de = readdir(dir)) != NULL) {
+ if (de->d_name[0] == '.')
+ continue;
+ unlink(mkpath("%s/%s", ref_temp, de->d_name));
+ }
+
+ unlink(ref_temp);
+ free(ref_temp);
+}
+
+static void
+walk_objects(char *src, char *dest)
+{
+ struct dirent *de;
+ struct stat buf;
+ int src_len, dest_len;
+ DIR *dir;
+
+ dir = opendir(src);
+ if (!dir)
+ die("failed to open %s\n", src);
+
+ if (mkdir(dest, 0777)) {
+ if (errno != EEXIST)
+ die("failed to create directory %s\n", dest);
+ else if (stat(dest, &buf))
+ die("failed to stat %s\n", dest);
+ else if (!S_ISDIR(buf.st_mode))
+ die("%s exists and is not a directory\n", dest);
+ }
+
+ src_len = strlen(src);
+ src[src_len] = '/';
+ dest_len = strlen(dest);
+ dest[dest_len] = '/';
+
+ while ((de = readdir(dir)) != NULL) {
+ strcpy(src + src_len + 1, de->d_name);
+ strcpy(dest + dest_len + 1, de->d_name);
+ if (stat(src, &buf)) {
+ fprintf(stderr, "failed to stat %s, ignoring\n", src);
+ continue;
+ }
+ if (S_ISDIR(buf.st_mode)) {
+ if (de->d_name[0] != '.')
+ walk_objects(src, dest);
+ continue;
+ }
+
+ if (unlink(dest) && errno != ENOENT)
+ die("failed to unlink %s\n", dest);
+ if (option_no_hardlinks) {
+ if (copy_file(dest, src, 0666))
+ die("failed to copy file to %s\n", dest);
+ } else {
+ if (link(src, dest))
+ die("failed to create link %s\n", dest);
+ }
+ }
+}
+
+static struct ref *
+clone_local(const char *src_repo, const char *dest_repo)
+{
+ char src[PATH_MAX];
+ char dest[PATH_MAX];
+
+ if (option_shared) {
+ write_alternates_file(dest_repo, src_repo);
+ } else {
+ snprintf(src, PATH_MAX, "%s/objects", src_repo);
+ snprintf(dest, PATH_MAX, "%s/objects", dest_repo);
+ walk_objects(src, dest);
+ }
+
+ /* FIXME: Return list of refs for src repo. */
+
+ return NULL;
+}
+
+int cmd_clone(int argc, const char **argv, const char *prefix)
+{
+ int use_local_hardlinks = 1;
+ int use_separate_remote = 1;
+ struct stat buf;
+ const char *repo, *work_tree, *git_dir;
+ char *path, *dir, *head, *ref_temp;
+ struct ref *refs, *r, *remote_head, *head_points_at;
+ char branch_top[256], key[256], refname[256], value[256];
+
+ argc = parse_options(argc, argv, builtin_clone_options,
+ builtin_clone_usage, 0);
+
+ if (argc == 0)
+ die("You must specify a repository to clone.");
+
+ if (option_no_hardlinks)
+ use_local_hardlinks = 0;
+
+ if (option_bare) {
+ if (option_origin)
+ die("--bare and --origin %s options are incompatible.",
+ option_origin);
+ option_no_checkout = 1;
+ use_separate_remote = 0;
+ }
+
+ repo = argv[0];
+ path = get_repo_path(repo);
+
+ if (argc == 2) {
+ dir = xstrdup(argv[1]);
+ } else {
+ dir = guess_dir_name(repo);
+ }
+
+ if (!stat(dir, &buf))
+ die("destination directory '%s' already exists.", dir);
+
+ if (option_bare)
+ work_tree = NULL;
+ else {
+ work_tree = getenv("GIT_WORK_TREE");
+ if (work_tree && !stat(work_tree, &buf))
+ die("working tree '%s' already exists.", work_tree);
+ }
+
+ if (mkdir(dir, 0755))
+ die("could not create repository dir '%s'.", dir);
+ if (work_tree && mkdir(work_tree, 0755))
+ die("could not create work tree dir '%s'.", work_tree);
+
+ if (option_bare || work_tree)
+ git_dir = xstrdup(dir);
+ else
+ git_dir = xstrdup(mkpath("%s/.git", dir));
+
+ init_db(git_dir, option_template, option_quiet ? INIT_DB_QUIET : 0);
+
+ /* This calls set_git_dir for the reference repo so we can get
+ * the refs there. Thus, call this before calling
+ * set_git_dir() on the repo we're setting up. */
+ ref_temp = setup_reference(git_dir);
+
+ set_git_dir(make_absolute_path(git_dir));
+
+ if (option_bare)
+ git_config_set("core.bare", "true");
+
+ if (path != NULL) {
+ refs = clone_local(path, git_dir);
+ } else {
+ struct fetch_pack_args args;
+
+ args.uploadpack = option_upload_pack;
+ args.quiet = option_quiet;
+ args.fetch_all = 1;
+ args.lock_pack = 0;
+ args.keep_pack = 1;
+ args.depth = option_depth;
+ args.no_progress = 1;
+
+ refs = fetch_pack(&args, argv[0], 0, NULL, NULL);
+ }
+
+ cleanup_reference(ref_temp);
+
+ if (option_bare)
+ strcpy(branch_top, "heads");
+ else
+ snprintf(branch_top, sizeof branch_top,
+ "refs/remotes/%s", option_origin);
+
+ remote_head = NULL;
+ for (r = refs; r; r = r->next) {
+ if (!strcmp(r->name, "HEAD")) {
+ remote_head = r;
+ continue;
+ }
+
+ if (!prefixcmp(r->name, "refs/heads/"))
+ snprintf(refname, sizeof refname,
+ "%s/%s", branch_top, r->name + 11);
+ else if (!prefixcmp(r->name, "refs/tags/"))
+ snprintf(refname, sizeof refname,
+ "refs/tags/%s", r->name + 10);
+ else
+ continue;
+
+ update_ref("clone from $repo",
+ refname, r->old_sha1, NULL, 0, DIE_ON_ERR);
+ }
+
+ if (option_bare)
+ return 0;
+
+ /* Is HEAD always first? If so, we could do this in the loop above. */
+ head_points_at = NULL;
+ for (r = refs; r; r = r->next) {
+ if (r != remote_head &&
+ !hashcmp(r->old_sha1, remote_head->old_sha1)) {
+ head_points_at = r;
+ printf("head points at %s\n", r->name);
+ break;
+ }
+ }
+
+ if (strrchr(head_points_at->name, '/'))
+ head = strrchr(head_points_at->name, '/') + 1;
+ else
+ head = head_points_at->name;
+
+ /* FIXME: What about the "Uh-oh, the remote told us..." case? */
+
+ snprintf(key, sizeof key, "remote.%s.remote", option_origin);
+ git_config_set(key, repo);
+ snprintf(key, sizeof key, "remote.%s.fetch", option_origin);
+ snprintf(value, sizeof value, "+refs/heads/*:%s/*", branch_top);
+
+ git_config_set_multivar(key, value, "^$", 0);
+
+ if (head_points_at) {
+ /* Local default branch */
+ create_symref("HEAD", head_points_at->name, NULL);
+
+ /* Tracking branch for the primary branch at the remote. */
+ update_ref(NULL, "HEAD", head_points_at->old_sha1,
+ NULL, 0, DIE_ON_ERR);
+ /*
+ rm -f "refs/remotes/$origin/HEAD"
+ git symbolic-ref "refs/remotes/$origin/HEAD" \
+ "refs/remotes/$origin/$head_points_at" &&
+ */
+
+ snprintf(key, sizeof key, "branch.%s.remote", head);
+ git_config_set(key, option_origin);
+ snprintf(key, sizeof key, "branch.%s.merge", head);
+ git_config_set(key, head_points_at->name);
+ } else {
+ /* Source had detached HEAD pointing nowhere. */
+ update_ref("clone from $repo", "HEAD", remote_head->old_sha1,
+ NULL, REF_NODEREF, DIE_ON_ERR);
+ }
+
+ if (!option_no_checkout) {
+ char base_dir[PATH_MAX];
+ struct lock_file lock_file;
+ struct unpack_trees_options opts;
+ struct tree *tree;
+ struct tree_desc t[2];
+ int fd;
+
+ fd = hold_locked_index(&lock_file, 1);
+
+ memset(&opts, 0, sizeof opts);
+ opts.update = 1;
+ opts.verbose_update = !option_quiet;
+ opts.merge = 1;
+ opts.fn = twoway_merge;
+
+ /* FIXME: Handle basedir ends in '/' */
+ snprintf(base_dir, sizeof base_dir, "%s/",
+ work_tree ? work_tree : dir);
+ opts.base_dir = base_dir;
+
+ tree = parse_tree_indirect(remote_head->old_sha1);
+ parse_tree(tree);
+ init_tree_desc(&t[0], tree->buffer, tree->size);
+ init_tree_desc(&t[1], tree->buffer, tree->size);
+ unpack_trees(2, t, &opts);
+
+ if (write_cache(fd, active_cache, active_nr) ||
+ close(fd) || commit_locked_index(&lock_file))
+ die("unable to write new index file");
+ }
+
+ return 0;
+}
diff --git a/builtin-init-db.c b/builtin-init-db.c
index e1393b8..18abc43 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -29,27 +29,6 @@ static void safe_create_dir(const char *dir, int share)
die("Could not make %s writable by group\n", dir);
}
-static int copy_file(const char *dst, const char *src, int mode)
-{
- int fdi, fdo, status;
-
- mode = (mode & 0111) ? 0777 : 0666;
- if ((fdi = open(src, O_RDONLY)) < 0)
- return fdi;
- if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
- close(fdi);
- return fdo;
- }
- status = copy_fd(fdi, fdo);
- if (close(fdo) != 0)
- return error("%s: write error: %s", dst, strerror(errno));
-
- if (!status && adjust_shared_perm(dst))
- return -1;
-
- return status;
-}
-
static void copy_templates_1(char *path, int baselen,
char *template, int template_baselen,
DIR *dir)
@@ -330,49 +309,11 @@ static void guess_repository_type(const char *git_dir)
return;
}
-static const char init_db_usage[] =
-"git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
-
-/*
- * 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)
+int init_db(const char *git_dir, const char *template_dir, unsigned int flags)
{
- const char *git_dir;
const char *sha1_dir;
- const char *template_dir = NULL;
char *path;
- int len, i, reinit;
- int quiet = 0;
-
- for (i = 1; i < argc; i++, argv++) {
- const char *arg = argv[1];
- if (!prefixcmp(arg, "--template="))
- template_dir = arg+11;
- else if (!strcmp(arg, "--shared"))
- shared_repository = PERM_GROUP;
- else if (!prefixcmp(arg, "--shared="))
- shared_repository = git_config_perm("arg", arg+9);
- else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
- quiet = 1;
- else
- usage(init_db_usage);
- }
-
- /*
- * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
- * without --bare. Catch the error early.
- */
- git_dir = getenv(GIT_DIR_ENVIRONMENT);
- if ((!git_dir || is_bare_repository_cfg == 1)
- && getenv(GIT_WORK_TREE_ENVIRONMENT))
- die("%s (or --work-tree=<directory>) not allowed without "
- "specifying %s (or --git-dir=<directory>)",
- GIT_WORK_TREE_ENVIRONMENT,
- GIT_DIR_ENVIRONMENT);
+ int len, reinit;
guess_repository_type(git_dir);
@@ -388,7 +329,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
/*
* Set up the default .git directory contents
*/
- git_dir = getenv(GIT_DIR_ENVIRONMENT);
if (!git_dir)
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
safe_create_dir(git_dir, 0);
@@ -403,9 +343,13 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
reinit = create_default_files(git_dir, template_dir);
/*
- * And set up the object store.
+ * And set up the object store. Don't use
+ * get_object_directory() here, since we're initializing
+ * relative to git_dir, not $GIT_DIR.
*/
- sha1_dir = get_object_directory();
+ sha1_dir = getenv(DB_ENVIRONMENT);
+ if (!sha1_dir)
+ sha1_dir = mkpath("%s/objects", git_dir);
len = strlen(sha1_dir);
path = xmalloc(len + 40);
memcpy(path, sha1_dir, len);
@@ -427,7 +371,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
git_config_set("receive.denyNonFastforwards", "true");
}
- if (!quiet)
+ if (!(flags & INIT_DB_QUIET))
printf("%s%s Git repository in %s/\n",
reinit ? "Reinitialized existing" : "Initialized empty",
shared_repository ? " shared" : "",
@@ -435,3 +379,48 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
return 0;
}
+
+static const char init_db_usage[] =
+"git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
+
+/*
+ * 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 *template_dir = NULL;
+ unsigned int flags = 0;
+ int i;
+
+ for (i = 1; i < argc; i++, argv++) {
+ const char *arg = argv[1];
+ if (!prefixcmp(arg, "--template="))
+ template_dir = arg+11;
+ else if (!strcmp(arg, "--shared"))
+ shared_repository = PERM_GROUP;
+ else if (!prefixcmp(arg, "--shared="))
+ shared_repository = git_config_perm("arg", arg+9);
+ else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
+ flags |= INIT_DB_QUIET;
+ else
+ usage(init_db_usage);
+ }
+
+ /*
+ * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
+ * without --bare. Catch the error early.
+ */
+ git_dir = getenv(GIT_DIR_ENVIRONMENT);
+ if ((!git_dir || is_bare_repository_cfg == 1)
+ && getenv(GIT_WORK_TREE_ENVIRONMENT))
+ die("%s (or --work-tree=<directory>) not allowed without "
+ "specifying %s (or --git-dir=<directory>)",
+ GIT_WORK_TREE_ENVIRONMENT,
+ GIT_DIR_ENVIRONMENT);
+
+ return init_db(git_dir, template_dir, flags);
+}
diff --git a/builtin-rerere.c b/builtin-rerere.c
index 7449323..2d83524 100644
--- a/builtin-rerere.c
+++ b/builtin-rerere.c
@@ -267,23 +267,6 @@ static int diff_two(const char *file1, const char *label1,
return 0;
}
-static int copy_file(const char *src, const char *dest)
-{
- FILE *in, *out;
- char buffer[32768];
- int count;
-
- if (!(in = fopen(src, "r")))
- return error("Could not open %s", src);
- if (!(out = fopen(dest, "w")))
- return error("Could not open %s", dest);
- while ((count = fread(buffer, 1, sizeof(buffer), in)))
- fwrite(buffer, 1, count, out);
- fclose(in);
- fclose(out);
- return 0;
-}
-
static int do_plain_rerere(struct path_list *rr, int fd)
{
struct path_list conflict = { NULL, 0, 0, 1 };
@@ -343,7 +326,7 @@ static int do_plain_rerere(struct path_list *rr, int fd)
continue;
fprintf(stderr, "Recorded resolution for '%s'.\n", path);
- copy_file(path, rr_path(name, "postimage"));
+ copy_file(path, rr_path(name, "postimage"), 0666);
tail_optimization:
if (i < rr->nr - 1)
memmove(rr->items + i,
diff --git a/builtin.h b/builtin.h
index cb675c4..1b9da64 100644
--- a/builtin.h
+++ b/builtin.h
@@ -24,6 +24,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
extern int cmd_cherry(int argc, const char **argv, const char *prefix);
extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
+extern int cmd_clone(int argc, const char **argv, const char *prefix);
extern int cmd_clean(int argc, const char **argv, const char *prefix);
extern int cmd_commit(int argc, const char **argv, const char *prefix);
extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
diff --git a/cache.h b/cache.h
index 4e59646..1e29e70 100644
--- a/cache.h
+++ b/cache.h
@@ -230,6 +230,10 @@ extern const char *prefix_filename(const char *prefix, int len, const char *path
extern void verify_filename(const char *prefix, const char *name);
extern void verify_non_filename(const char *prefix, const char *name);
+#define INIT_DB_QUIET 0x0001
+
+extern int init_db(const char *git_dir, const char *template_dir, unsigned int flags);
+
#define alloc_nr(x) (((x)+16)*3/2)
/*
@@ -588,6 +592,7 @@ extern const char *git_log_output_encoding;
/* IO helper functions */
extern void maybe_flush_or_die(FILE *, const char *);
extern int copy_fd(int ifd, int ofd);
+extern int copy_file(const char *dst, const char *src, int mode);
extern int read_in_full(int fd, void *buf, size_t count);
extern int write_in_full(int fd, const void *buf, size_t count);
extern void write_or_die(int fd, const void *buf, size_t count);
diff --git a/git-clone.sh b/contrib/examples/git-clone.sh
similarity index 100%
rename from git-clone.sh
rename to contrib/examples/git-clone.sh
diff --git a/copy.c b/copy.c
index c225d1b..afc4fbf 100644
--- a/copy.c
+++ b/copy.c
@@ -34,3 +34,24 @@ int copy_fd(int ifd, int ofd)
close(ifd);
return 0;
}
+
+int copy_file(const char *dst, const char *src, int mode)
+{
+ int fdi, fdo, status;
+
+ mode = (mode & 0111) ? 0777 : 0666;
+ if ((fdi = open(src, O_RDONLY)) < 0)
+ return fdi;
+ if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
+ close(fdi);
+ return fdo;
+ }
+ status = copy_fd(fdi, fdo);
+ if (close(fdo) != 0)
+ return error("%s: write error: %s", dst, strerror(errno));
+
+ if (!status && adjust_shared_perm(dst))
+ return -1;
+
+ return status;
+}
diff --git a/diff.c b/diff.c
index 5175950..0af5b81 100644
--- a/diff.c
+++ b/diff.c
@@ -258,8 +258,8 @@ static void print_line_count(int count)
}
}
-static void copy_file(int prefix, const char *data, int size,
- const char *set, const char *reset)
+static void copy_file_with_prefix(int prefix, const char *data, int size,
+ const char *set, const char *reset)
{
int ch, nl_just_seen = 1;
while (0 < size--) {
@@ -310,9 +310,9 @@ static void emit_rewrite_diff(const char *name_a,
print_line_count(lc_b);
printf(" @@%s\n", reset);
if (lc_a)
- copy_file('-', one->data, one->size, old, reset);
+ copy_file_with_prefix('-', one->data, one->size, old, reset);
if (lc_b)
- copy_file('+', two->data, two->size, new, reset);
+ copy_file_with_prefix('+', two->data, two->size, new, reset);
}
static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
diff --git a/git.c b/git.c
index f406c4b..c8dfb6d 100644
--- a/git.c
+++ b/git.c
@@ -302,6 +302,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
{ "cherry", cmd_cherry, RUN_SETUP },
{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+ { "clone", cmd_clone },
{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
{ "commit-tree", cmd_commit_tree, RUN_SETUP },
diff --git a/unpack-trees.c b/unpack-trees.c
index e9eb795..752278a 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -338,7 +338,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
memset(&df_conflict_list, 0, sizeof(df_conflict_list));
df_conflict_list.next = &df_conflict_list;
memset(&state, 0, sizeof(state));
- state.base_dir = "";
+ state.base_dir = o->base_dir ? o->base_dir : "";
+ state.base_dir_len = strlen(state.base_dir);
state.force = 1;
state.quiet = 1;
state.refresh_cache = 1;
diff --git a/unpack-trees.h b/unpack-trees.h
index 5517faa..15b2ed9 100644
--- a/unpack-trees.h
+++ b/unpack-trees.h
@@ -17,6 +17,7 @@ struct unpack_trees_options {
int verbose_update;
int aggressive;
const char *prefix;
+ const char *base_dir;
int pos;
struct dir_struct *dir;
merge_fn_t fn;
--
1.5.3.4
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-11 19:57 [PATCH] builtin-clone: Implement git clone as a builtin command Kristian Høgsberg
@ 2007-12-11 20:59 ` Daniel Barkalow
2007-12-11 23:38 ` Kristian Høgsberg
0 siblings, 1 reply; 13+ messages in thread
From: Daniel Barkalow @ 2007-12-11 20:59 UTC (permalink / raw)
To: Kristian Høgsberg; +Cc: git
[-- Attachment #1: Type: TEXT/PLAIN, Size: 897 bytes --]
On Tue, 11 Dec 2007, Kristian Høgsberg wrote:
> Ok, don't flame me, I know this isn't appropriate at the moment with
> stabilization for 1.5.4 going on, but I just wanted to post a heads up
> on this work to avoid duplicate effort. It's one big patch at this point
> and I haven't even run the test suite yet, but that will change.
Is that why you misspelled Junio's email address? :)
Also as a heads-up, I've got a builtin-checkout that I've got passing all
the tests (plus a few to test stuff I originally hadn't implemented). This
mostly involved correcting the "interesting" states that unpack_trees()
can leave the index in memory when it returns and figuring out how the
merge code works. I can send it off for review and testing to people who
are interested and don't have other things they should be doing instead.
-Daniel
*This .sig left intentionally blank*
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-11 20:59 ` Daniel Barkalow
@ 2007-12-11 23:38 ` Kristian Høgsberg
2007-12-12 3:12 ` Junio C Hamano
0 siblings, 1 reply; 13+ messages in thread
From: Kristian Høgsberg @ 2007-12-11 23:38 UTC (permalink / raw)
To: Daniel Barkalow; +Cc: git
On Tue, 2007-12-11 at 15:59 -0500, Daniel Barkalow wrote:
> On Tue, 11 Dec 2007, Kristian Høgsberg wrote:
>
> > Ok, don't flame me, I know this isn't appropriate at the moment with
> > stabilization for 1.5.4 going on, but I just wanted to post a heads up
> > on this work to avoid duplicate effort. It's one big patch at this point
> > and I haven't even run the test suite yet, but that will change.
>
> Is that why you misspelled Junio's email address? :)
Hehe, yeah, do not mess with maintainers in release mode :)
> Also as a heads-up, I've got a builtin-checkout that I've got passing all
> the tests (plus a few to test stuff I originally hadn't implemented). This
> mostly involved correcting the "interesting" states that unpack_trees()
> can leave the index in memory when it returns and figuring out how the
> merge code works. I can send it off for review and testing to people who
> are interested and don't have other things they should be doing instead.
Thanks, that's useful, I was already considering what to do next. I
wouldn't mind having a look, but maybe it's better to not discuss new
features on the list at this point. I appreciate the heads up though.
cheers,
Kristian
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-11 23:38 ` Kristian Høgsberg
@ 2007-12-12 3:12 ` Junio C Hamano
2007-12-12 3:20 ` Junio C Hamano
` (3 more replies)
0 siblings, 4 replies; 13+ messages in thread
From: Junio C Hamano @ 2007-12-12 3:12 UTC (permalink / raw)
To: Kristian Høgsberg; +Cc: Daniel Barkalow, git
Kristian Høgsberg <krh@redhat.com> writes:
> On Tue, 2007-12-11 at 15:59 -0500, Daniel Barkalow wrote:
>> On Tue, 11 Dec 2007, Kristian Høgsberg wrote:
>>
>> > Ok, don't flame me, I know this isn't appropriate at the moment with
>> > stabilization for 1.5.4 going on, but I just wanted to post a heads up
>> > on this work to avoid duplicate effort. It's one big patch at this point
>> > and I haven't even run the test suite yet, but that will change.
>>
>> Is that why you misspelled Junio's email address? :)
>
> Hehe, yeah, do not mess with maintainers in release mode :)
Actually this is a bit unfortunate, regardless of everybody being in
release and bugfix only mode.
I was hoping that the evolution path for clone would be to first make it
a very thin wrapper around:
git init
git remote add -f
git checkout
sequence. Currently, the "origin" repository is not quite equal to
other remotes added with "git remote add", but if we enhance "git remote
add" a bit, we should be able to make this happen. This would hopefully
lose a lot of code from git-clone. And then after we are done with
that, rewrite the remaining thin wrapper in C.
There are a handful issues in that approach with the current git-remote,
and that was why I also thought recent "git remote in C" by Dscho a bit
unfortunate, as enhancements and interface fixes (both user and machine)
tend to be much easier in scripted version.
What the current "git clone" does that are not naturally expressed by
the above sequence are:
* HEAD discovery
The code can be lifted from the scripted version and transplanted to
git-remote. And to make "origin" and other remotes added by "git
remote add", this logic needs to be moved to "git remote".
However, before rewriting the "git remote" to C, it would be really
nice if we can update the native protocol so that we can reliably
find out which branch HEAD points at. The current code guesses, only
because the native protocol does not carry that information [*1*].
Worse yet, even though the current code _knows_ this information when
going over dumb protocols, it discards it to use the same guessing
logic as used by the native protocol.
* --shared optimization
This is a very easy addition to "git remote add". You make sure that
the added remote repository is on a local machine, and set up
alternates to point at its object store.
* --reference optimization
This is a bit more involved than --shared. Half the power of this
optimization is coming from setting up alternates to point at another
local repository, which allows you not to have to _store_ duplicated
objects yourself, but the other half is coming from being able to lie
to the repository being cloned from that you have branches and tags
that reference repository has, even though they are not your branches
and tags, which allows you not to have to _download_ the objects to
begin with.
I think this can be added to "git remote add" by making --reference
also imply -f. Then while "git remote add" sets up the new remote,
it can stash the borrowed refs somewhere, just like git-clone does,
run the git-fetch, and then remove the borrowed refs once done.
* local optimization (the "cpio" thing)
I think this part needs to stay in git-clone even after we move the
above to "git remote add".
[Footnote]
*1* Here is a demonstration of the necessary protocol extension.
-- >8 --
Implement show-symref protocol extension.
This updates the git native "upload-pack" protocol to carry extra
information to show which branch HEAD symref points at. As is the other
protocol extension, this is enabled only when both ends of the exchange
supports it.
The receiving end currently does not do anything, and the logic needs to
go to peek-remote more than it needs to go to fetch-pack, but one has to
start from somewhere.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
builtin-fetch-pack.c | 86 +++++++++++++++++++++++++++++++++----------------
upload-pack.c | 25 +++++++++++++-
2 files changed, 81 insertions(+), 30 deletions(-)
diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c
index 807fa93..e9f86d6 100644
--- a/builtin-fetch-pack.c
+++ b/builtin-fetch-pack.c
@@ -32,7 +32,7 @@ static const char fetch_pack_usage[] =
#define MAX_IN_VAIN 256
static struct commit_list *rev_list;
-static int non_common_revs, multi_ack, use_sideband;
+static int non_common_revs, multi_ack, use_sideband, show_symref;
static void rev_list_push(struct commit *commit, int mark)
{
@@ -141,6 +141,51 @@ static const unsigned char* get_rev(void)
return commit->object.sha1;
}
+static void handle_shallow(int fd[2])
+{
+ char line[1024];
+ unsigned char sha1[20];
+ int len;
+
+ while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
+ if (!prefixcmp(line, "shallow ")) {
+ if (get_sha1_hex(line + 8, sha1))
+ die("invalid shallow line: %s", line);
+ register_shallow(sha1);
+ continue;
+ }
+ if (!prefixcmp(line, "unshallow ")) {
+ if (get_sha1_hex(line + 10, sha1))
+ die("invalid unshallow line: %s", line);
+ if (!lookup_object(sha1))
+ die("object not found: %s", line);
+ /* make sure that it is parsed as shallow */
+ parse_object(sha1);
+ if (unregister_shallow(sha1))
+ die("no shallow found: %s", line);
+ continue;
+ }
+ die("expected shallow/unshallow, got %s", line);
+ }
+}
+
+static void handle_symref(int fd[2], struct ref *refs)
+{
+ char line[1024];
+ int len;
+
+ while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
+ if (!prefixcmp(line, "symref ")) {
+ /*
+ * Here you would remember what symbolic ref
+ * pointed at what real ref to use that
+ * information later.
+ */
+ fputs(line, stderr);
+ }
+ }
+}
+
static int find_common(int fd[2], unsigned char *result_sha1,
struct ref *refs)
{
@@ -173,8 +218,9 @@ static int find_common(int fd[2], unsigned char *result_sha1,
}
if (!fetching)
- packet_write(fd[1], "want %s%s%s%s%s%s%s\n",
+ packet_write(fd[1], "want %s%s%s%s%s%s%s%s\n",
sha1_to_hex(remote),
+ (show_symref ? " show-symref" : ""),
(multi_ack ? " multi_ack" : ""),
(use_sideband == 2 ? " side-band-64k" : ""),
(use_sideband == 1 ? " side-band" : ""),
@@ -193,32 +239,11 @@ static int find_common(int fd[2], unsigned char *result_sha1,
if (!fetching)
return 1;
- if (args.depth > 0) {
- char line[1024];
- unsigned char sha1[20];
- int len;
-
- while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
- if (!prefixcmp(line, "shallow ")) {
- if (get_sha1_hex(line + 8, sha1))
- die("invalid shallow line: %s", line);
- register_shallow(sha1);
- continue;
- }
- if (!prefixcmp(line, "unshallow ")) {
- if (get_sha1_hex(line + 10, sha1))
- die("invalid unshallow line: %s", line);
- if (!lookup_object(sha1))
- die("object not found: %s", line);
- /* make sure that it is parsed as shallow */
- parse_object(sha1);
- if (unregister_shallow(sha1))
- die("no shallow found: %s", line);
- continue;
- }
- die("expected shallow/unshallow, got %s", line);
- }
- }
+ if (args.depth > 0)
+ handle_shallow(fd);
+
+ if (show_symref)
+ handle_symref(fd, refs);
flushes = 0;
retval = -1;
@@ -558,6 +583,11 @@ static struct ref *do_fetch_pack(int fd[2],
get_remote_heads(fd[0], &ref, 0, NULL, 0);
if (is_repository_shallow() && !server_supports("shallow"))
die("Server does not support shallow clients");
+ if (server_supports("show-symref")) {
+ if (args.verbose)
+ fprintf(stderr, "Server supports show-symref\n");
+ show_symref = 1;
+ }
if (server_supports("multi_ack")) {
if (args.verbose)
fprintf(stderr, "Server supports multi_ack\n");
diff --git a/upload-pack.c b/upload-pack.c
index 7e04311..351d501 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -27,7 +27,7 @@ static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=n
static unsigned long oldest_have;
static int multi_ack, nr_our_refs;
-static int use_thin_pack, use_ofs_delta, no_progress;
+static int use_thin_pack, use_ofs_delta, no_progress, show_symref;
static struct object_array have_obj;
static struct object_array want_obj;
static unsigned int timeout;
@@ -477,6 +477,10 @@ static void receive_needs(void)
get_sha1_hex(line+5, sha1_buf))
die("git-upload-pack: protocol error, "
"expected to get sha, not '%s'", line);
+
+ /* Protocol extensions */
+ if (strstr(line+45, "show-symref"))
+ show_symref = 1;
if (strstr(line+45, "multi_ack"))
multi_ack = 1;
if (strstr(line+45, "thin-pack"))
@@ -557,7 +561,7 @@ static void receive_needs(void)
static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
static const char *capabilities = "multi_ack thin-pack side-band"
- " side-band-64k ofs-delta shallow no-progress";
+ " side-band-64k ofs-delta shallow no-progress show-symref";
struct object *o = parse_object(sha1);
if (!o)
@@ -580,6 +584,18 @@ static int send_ref(const char *refname, const unsigned char *sha1, int flag, vo
return 0;
}
+static int send_symref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+{
+ unsigned char object_name[20];
+ const char *symref;
+ int what;
+
+ symref = resolve_ref(refname, object_name, 1, &what);
+ if (symref && (what & REF_ISSYMREF))
+ packet_write(1, "symref %s %s\n", refname, symref);
+ return 0;
+}
+
static void upload_pack(void)
{
reset_timeout();
@@ -587,6 +603,11 @@ static void upload_pack(void)
for_each_ref(send_ref, NULL);
packet_flush(1);
receive_needs();
+ if (show_symref) {
+ send_symref("HEAD", NULL, 0, NULL);
+ for_each_ref(send_symref, NULL);
+ packet_flush(1);
+ }
if (want_obj.nr) {
get_common_commits();
create_pack_file();
--
1.5.3.7-1157-gbf82a
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 3:12 ` Junio C Hamano
@ 2007-12-12 3:20 ` Junio C Hamano
2007-12-12 11:12 ` Johannes Schindelin
` (2 subsequent siblings)
3 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2007-12-12 3:20 UTC (permalink / raw)
To: Kristian Høgsberg; +Cc: Daniel Barkalow, git
Junio C Hamano <gitster@pobox.com> writes:
> What the current "git clone" does that are not naturally expressed by
> the above sequence are:
>
> * HEAD discovery
>
> The code can be lifted from the scripted version and transplanted to
> git-remote. And to make "origin" and other remotes added by "git
> remote add", this logic needs to be moved to "git remote".
s/remote add", this/remote add" more equal, this/;
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 3:12 ` Junio C Hamano
2007-12-12 3:20 ` Junio C Hamano
@ 2007-12-12 11:12 ` Johannes Schindelin
2007-12-12 15:04 ` Kristian Høgsberg
2007-12-12 15:24 ` Kristian Høgsberg
2007-12-24 1:52 ` J. Bruce Fields
3 siblings, 1 reply; 13+ messages in thread
From: Johannes Schindelin @ 2007-12-12 11:12 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Kristian Høgsberg, Daniel Barkalow, git
[-- Attachment #1: Type: TEXT/PLAIN, Size: 3184 bytes --]
Hi,
On Tue, 11 Dec 2007, Junio C Hamano wrote:
> Kristian Høgsberg <krh@redhat.com> writes:
>
> > On Tue, 2007-12-11 at 15:59 -0500, Daniel Barkalow wrote:
> >> On Tue, 11 Dec 2007, Kristian Høgsberg wrote:
> >>
> >> > Ok, don't flame me, I know this isn't appropriate at the moment
> >> > with stabilization for 1.5.4 going on, but I just wanted to post a
> >> > heads up on this work to avoid duplicate effort. It's one big
> >> > patch at this point and I haven't even run the test suite yet, but
> >> > that will change.
> >>
> >> Is that why you misspelled Junio's email address? :)
> >
> > Hehe, yeah, do not mess with maintainers in release mode :)
>
> Actually this is a bit unfortunate, regardless of everybody being in
> release and bugfix only mode.
I can understand that feeling, but I have to say that I am actually quite
pleased with the progress in direction of having most of git as builtins.
> I was hoping that the evolution path for clone would be to first make it
> a very thin wrapper around:
>
> git init
> git remote add -f
> git checkout
>
> sequence.
Yeah, I thought so too, but I'll also gladly take the builtin first.
> There are a handful issues in that approach with the current git-remote,
> and that was why I also thought recent "git remote in C" by Dscho a bit
> unfortunate, as enhancements and interface fixes (both user and machine)
> tend to be much easier in scripted version.
And here I have to disagree strongly. I _wasted_ a _week_ on trying to
fix that stupid "add --mirror && prune" bug in the scripted version. It
was absolutely horrible. And I felt like a moron after that week.
In contrast, it was easy as chocolate cake to fix it in the builtin
remote.
Now, if you not only hinted in some mail that something is wrong with
builtin-remote, but gave me some input, I could fix that in the builtin,
too.
> What the current "git clone" does that are not naturally expressed by
> the above sequence are:
>
> * HEAD discovery
>
> The code can be lifted from the scripted version and transplanted to
> git-remote. And to make "origin" and other remotes added by "git
> remote add", this logic needs to be moved to "git remote".
>
> However, before rewriting the "git remote" to C, it would be really
> nice if we can update the native protocol so that we can reliably
> find out which branch HEAD points at. The current code guesses, only
> because the native protocol does not carry that information [*1*].
> Worse yet, even though the current code _knows_ this information when
> going over dumb protocols, it discards it to use the same guessing
> logic as used by the native protocol.
I wonder why this should be easier with git remote in Perl. IMHO it is
easier with git remote in C.
> * --shared optimization
>
> This is a very easy addition to "git remote add". You make sure that
> the added remote repository is on a local machine, and set up
> alternates to point at its object store.
Concur.
Since I want to lose that dependency on cpio on Windows (which we fake by
using tar), I'll implement this in C anyway.
Ciao,
Dscho
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 11:12 ` Johannes Schindelin
@ 2007-12-12 15:04 ` Kristian Høgsberg
2007-12-12 18:24 ` Johannes Schindelin
0 siblings, 1 reply; 13+ messages in thread
From: Kristian Høgsberg @ 2007-12-12 15:04 UTC (permalink / raw)
To: Johannes Schindelin; +Cc: Junio C Hamano, Daniel Barkalow, git
On Wed, 2007-12-12 at 11:12 +0000, Johannes Schindelin wrote:
> Hi,
>
> On Tue, 11 Dec 2007, Junio C Hamano wrote:
...
> > * --shared optimization
> >
> > This is a very easy addition to "git remote add". You make sure that
> > the added remote repository is on a local machine, and set up
> > alternates to point at its object store.
>
> Concur.
>
> Since I want to lose that dependency on cpio on Windows (which we fake by
> using tar), I'll implement this in C anyway.
It's not used for --shared (which is just writing an alternates file),
it's used for -l, hardlinking locally cloned repos. The code to replace
cpio is already in the patch I sent, look for clone_local().
cheers,
Kristian
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 15:04 ` Kristian Høgsberg
@ 2007-12-12 18:24 ` Johannes Schindelin
0 siblings, 0 replies; 13+ messages in thread
From: Johannes Schindelin @ 2007-12-12 18:24 UTC (permalink / raw)
To: Kristian Høgsberg; +Cc: Junio C Hamano, Daniel Barkalow, git
Hi,
On Wed, 12 Dec 2007, Kristian H?gsberg wrote:
> On Wed, 2007-12-12 at 11:12 +0000, Johannes Schindelin wrote:
>
> > On Tue, 11 Dec 2007, Junio C Hamano wrote:
> ...
> > > * --shared optimization
> > >
> > > This is a very easy addition to "git remote add". You make sure
> > > that the added remote repository is on a local machine, and set
> > > up alternates to point at its object store.
> >
> > Concur.
> >
> > Since I want to lose that dependency on cpio on Windows (which we fake
> > by using tar), I'll implement this in C anyway.
>
> It's not used for --shared (which is just writing an alternates file),
> it's used for -l, hardlinking locally cloned repos. The code to replace
> cpio is already in the patch I sent, look for clone_local().
Sorry, that comment should have gone after another part of the original
message.
My only two excuses are that I am ill, and am overloaded with work.
Ciao,
Dscho
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 3:12 ` Junio C Hamano
2007-12-12 3:20 ` Junio C Hamano
2007-12-12 11:12 ` Johannes Schindelin
@ 2007-12-12 15:24 ` Kristian Høgsberg
2007-12-12 18:00 ` Daniel Barkalow
2007-12-24 1:52 ` J. Bruce Fields
3 siblings, 1 reply; 13+ messages in thread
From: Kristian Høgsberg @ 2007-12-12 15:24 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Daniel Barkalow, git
On Tue, 2007-12-11 at 19:12 -0800, Junio C Hamano wrote:
> Kristian Høgsberg <krh@redhat.com> writes:
>
> > On Tue, 2007-12-11 at 15:59 -0500, Daniel Barkalow wrote:
> >> On Tue, 11 Dec 2007, Kristian Høgsberg wrote:
> >>
> >> > Ok, don't flame me, I know this isn't appropriate at the moment with
> >> > stabilization for 1.5.4 going on, but I just wanted to post a heads up
> >> > on this work to avoid duplicate effort. It's one big patch at this point
> >> > and I haven't even run the test suite yet, but that will change.
> >>
> >> Is that why you misspelled Junio's email address? :)
> >
> > Hehe, yeah, do not mess with maintainers in release mode :)
>
> Actually this is a bit unfortunate, regardless of everybody being in
> release and bugfix only mode.
Well, let's just pick up the discussion in January, I have a lot of
other stuff I'm trying to do anyway :)
> I was hoping that the evolution path for clone would be to first make it
> a very thin wrapper around:
>
> git init
> git remote add -f
> git checkout
>
> sequence.
However, let me just say that the patch I sent is almost just that.
Part of the patch refactors init-db to be useful from clone, part of the
code is option parsing and figuring out the git dir, work tree. Also,
the part of the patch that does 'git checkout' is approximately 20 lines
that end up calling unpack_tre() and then write_cache(). The bulk of
the work here is really just builtin boilerplate code, option parsing
and the builtin-clone tasks you describe below (HEAD discovery, --shared
and --reference optimizations and the local hardlink optimization - all
these are in the 500 line builtin-clone.c I sent).
And maybe it makes sense to use builtin-remote for the remote add -f
part, but the fetch part of the patch is 10 lines to set up for
fetch_pack(). So while I do agree that it makes sense to keep remotes
handling in one place, doing the fetch_pack() in builtin-clone.c doesn't
seem like a big duplication of code. And either way, I agree with
Dscho, once we have either builtin-clone or builtin-fetch it's easier to
share code and refactor, and there is not a strong reason to do one or
the other first.
cheers,
Kristian
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 15:24 ` Kristian Høgsberg
@ 2007-12-12 18:00 ` Daniel Barkalow
2007-12-12 18:25 ` Kristian Høgsberg
0 siblings, 1 reply; 13+ messages in thread
From: Daniel Barkalow @ 2007-12-12 18:00 UTC (permalink / raw)
To: Kristian Høgsberg; +Cc: Junio C Hamano, git
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: TEXT/PLAIN; charset=utf-8, Size: 1769 bytes --]
On Wed, 12 Dec 2007, Kristian Høgsberg wrote:
> However, let me just say that the patch I sent is almost just that.
> Part of the patch refactors init-db to be useful from clone, part of the
> code is option parsing and figuring out the git dir, work tree. Also,
> the part of the patch that does 'git checkout' is approximately 20 lines
> that end up calling unpack_tre() and then write_cache(). The bulk of
> the work here is really just builtin boilerplate code, option parsing
> and the builtin-clone tasks you describe below (HEAD discovery, --shared
> and --reference optimizations and the local hardlink optimization - all
> these are in the 500 line builtin-clone.c I sent).
>
> And maybe it makes sense to use builtin-remote for the remote add -f
> part, but the fetch part of the patch is 10 lines to set up for
> fetch_pack(). So while I do agree that it makes sense to keep remotes
> handling in one place, doing the fetch_pack() in builtin-clone.c doesn't
> seem like a big duplication of code. And either way, I agree with
> Dscho, once we have either builtin-clone or builtin-fetch it's easier to
> share code and refactor, and there is not a strong reason to do one or
> the other first.
Er, we have builtin-fetch. We just don't have a way of calling it with all
of the option parsing done, but that should be easy. I was expecting that
step to get done when clone got converted, or maybe remote...
I agree that the checkout special case when the code knows in advance that
you don't have anything checked out beforehand is particularly trivial,
and it's probably just as easy to call unpack_trees() and write_cache() as
to use an actual checkout implementation.
-Daniel
*This .sig left intentionally blank*
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 18:00 ` Daniel Barkalow
@ 2007-12-12 18:25 ` Kristian Høgsberg
2007-12-12 18:40 ` Daniel Barkalow
0 siblings, 1 reply; 13+ messages in thread
From: Kristian Høgsberg @ 2007-12-12 18:25 UTC (permalink / raw)
To: Daniel Barkalow; +Cc: Junio C Hamano, git
On Wed, 2007-12-12 at 13:00 -0500, Daniel Barkalow wrote:
> On Wed, 12 Dec 2007, Kristian Hgsberg wrote:
>
> > However, let me just say that the patch I sent is almost just that.
> > Part of the patch refactors init-db to be useful from clone, part of the
> > code is option parsing and figuring out the git dir, work tree. Also,
> > the part of the patch that does 'git checkout' is approximately 20 lines
> > that end up calling unpack_tre() and then write_cache(). The bulk of
> > the work here is really just builtin boilerplate code, option parsing
> > and the builtin-clone tasks you describe below (HEAD discovery, --shared
> > and --reference optimizations and the local hardlink optimization - all
> > these are in the 500 line builtin-clone.c I sent).
> >
> > And maybe it makes sense to use builtin-remote for the remote add -f
> > part, but the fetch part of the patch is 10 lines to set up for
> > fetch_pack(). So while I do agree that it makes sense to keep remotes
> > handling in one place, doing the fetch_pack() in builtin-clone.c doesn't
> > seem like a big duplication of code. And either way, I agree with
> > Dscho, once we have either builtin-clone or builtin-fetch it's easier to
> > share code and refactor, and there is not a strong reason to do one or
> > the other first.
>
> Er, we have builtin-fetch. We just don't have a way of calling it with all
> of the option parsing done, but that should be easy. I was expecting that
> step to get done when clone got converted, or maybe remote...
Ugh, I meant builtin-remote there, sorry. I use fetch_pack() like the shell
script does, and it seem a lot easier that trying to call fetch:
struct fetch_pack_args args;
args.uploadpack = option_upload_pack;
args.quiet = option_quiet;
args.fetch_all = 1;
args.lock_pack = 0;
args.keep_pack = 1;
args.depth = option_depth;
args.no_progress = 1;
refs = fetch_pack(&args, argv[0], 0, NULL, NULL);
Kristian
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 18:25 ` Kristian Høgsberg
@ 2007-12-12 18:40 ` Daniel Barkalow
0 siblings, 0 replies; 13+ messages in thread
From: Daniel Barkalow @ 2007-12-12 18:40 UTC (permalink / raw)
To: Kristian Høgsberg; +Cc: Junio C Hamano, git
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: TEXT/PLAIN; charset=X-UNKNOWN, Size: 767 bytes --]
On Wed, 12 Dec 2007, Kristian Høgsberg wrote:
> Ugh, I meant builtin-remote there, sorry. I use fetch_pack() like the shell
> script does, and it seem a lot easier that trying to call fetch:
>
> struct fetch_pack_args args;
>
> args.uploadpack = option_upload_pack;
> args.quiet = option_quiet;
> args.fetch_all = 1;
> args.lock_pack = 0;
> args.keep_pack = 1;
> args.depth = option_depth;
> args.no_progress = 1;
>
> refs = fetch_pack(&args, argv[0], 0, NULL, NULL);
Ah, but that only works for git native protocol remote repositories.
Calling fetch instead would mean that other protocols also work without
any fuss.
-Daniel
*This .sig left intentionally blank*
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] builtin-clone: Implement git clone as a builtin command.
2007-12-12 3:12 ` Junio C Hamano
` (2 preceding siblings ...)
2007-12-12 15:24 ` Kristian Høgsberg
@ 2007-12-24 1:52 ` J. Bruce Fields
3 siblings, 0 replies; 13+ messages in thread
From: J. Bruce Fields @ 2007-12-24 1:52 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Kristian Høgsberg, Daniel Barkalow, git
On Tue, Dec 11, 2007 at 07:12:54PM -0800, Junio C Hamano wrote:
> * HEAD discovery
>
> The code can be lifted from the scripted version and transplanted to
> git-remote. And to make "origin" and other remotes added by "git
> remote add", this logic needs to be moved to "git remote".
A rough first attempt appended.
Cleaning up in "remote rm" is a bit of a pain once a remote can contain
symbolic-refs. Would it make sense to add something like a "git
update-ref -D <refname>" that deletes anything at that path with no
checking?
Thanks for outlining these remote improvements, by the way, I really
look forward to them. I'll do what I can, but will probably be much too
slow....
--b.
commit fecdb5c0d118767c216302e2c91950cca04f9a26
Author: J. Bruce Fields <bfields@citi.umich.edu>
Date: Fri Dec 21 19:55:09 2007 -0500
git-remote: make add -f guess HEAD, as clone does
Has a few ugly bits.
Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
diff --git a/git-remote.perl b/git-remote.perl
index d13e4c1..3299029 100755
--- a/git-remote.perl
+++ b/git-remote.perl
@@ -270,12 +270,57 @@ sub show_remote {
return 0;
}
+sub guess_head {
+ my ($subdir, $head_sha1) = @_;
+ my $found = 0;
+
+ for ($git->command('for-each-ref', "$subdir")) {
+ chomp;
+ m|^([0-9a-f]{40})\s[a-z]+\s$subdir/(.*)$| || die();
+ my ($sha1, $ref) = ($1, $2);
+
+ if ($sha1 eq $head_sha1) {
+ $found = $ref;
+ # prefer "master" if it matches:
+ if ($ref eq "master") {
+ last;
+ }
+ }
+ }
+ return $found
+}
+
+sub fix_head {
+ my ($remote) = @_;
+
+ my $subdir = "refs/remotes/$remote";
+ my $head_sha1 = $git->command(qw(rev-parse --verify), "$subdir/HEAD");
+ chomp($head_sha1);
+
+ unlink($git->repo_path."/$subdir/HEAD");
+ my $found = guess_head($subdir, $head_sha1);
+ if (!$found) {
+ # Just leave it as a a bare sha1
+ $git->command("update-ref", "$subdir/HEAD", "$head_sha1");
+ return;
+ }
+ $git->command("symbolic-ref", "$subdir/HEAD", "$subdir/$found");
+ $git->command("config", "branch.$found.remote", "$remote");
+ $git->command("config", "branch.$found.merge", "refs/heads/$found");
+}
+
sub add_remote {
my ($name, $url, $opts) = @_;
if (exists $remote->{$name}) {
print STDERR "remote $name already exists.\n";
exit(1);
}
+
+ # Tricky!: magic HEAD setup should only be done when
+ # "mirror", "master", and "track" options aren't given.
+ # And for orthagonality perhaps we should also provide a
+ # "track head" optio., compatible with "track" but not
+ # the other two....
$git->command('config', "remote.$name.url", $url);
my $track = $opts->{'track'} || ["*"];
@@ -286,7 +331,17 @@ sub add_remote {
"+refs/heads/$_:refs/remotes/$name/$_");
}
if ($opts->{'fetch'}) {
- $git->command('fetch', $name);
+ if (!$opts->{'mirror'} && !$opts->{'master'}
+ && !$opts->{'track'}) {
+ my $refspec = "+refs/heads/*:refs/remotes/$name/*";
+
+ # XXX: can there be a remote refs/heads/HEAD??
+ $git->command('fetch', $name, $refspec,
+ "HEAD:refs/remotes/$name/HEAD");
+ fix_head($name);
+ } else {
+ $git->command('fetch', $name);
+ }
}
if (exists $opts->{'master'}) {
$git->command('symbolic-ref', "refs/remotes/$name/HEAD",
@@ -338,11 +393,11 @@ sub rm_remote {
}
};
- my @refs = $git->command('for-each-ref',
- '--format=%(refname) %(objectname)', "refs/remotes/$name");
- for (@refs) {
- ($ref, $object) = split;
- $git->command(qw(update-ref -d), $ref, $object);
+ # Ugh: update-ref doesn't work on symref (as "HEAD" may be),
+ # but the following won't work on packed refs, for example.
+ for ($git->command('for-each-ref',
+ '--format=%(refname)', "refs/remotes/$name")) {
+ unlink($git->repo_path."/$_");
}
return 0;
}
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 636aec2..5576f2a 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -69,7 +69,7 @@ test_expect_success 'add another remote' '
tokens_match "origin second" "$(git remote)" &&
check_remote_track origin master side &&
check_remote_track second master side another &&
- check_tracking_branch second master side another &&
+ check_tracking_branch second HEAD master side another &&
git for-each-ref "--format=%(refname)" refs/remotes |
sed -e "/^refs\/remotes\/origin\//d" \
-e "/^refs\/remotes\/second\//d" >actual &&
^ permalink raw reply related [flat|nested] 13+ messages in thread
end of thread, other threads:[~2007-12-24 1:53 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-11 19:57 [PATCH] builtin-clone: Implement git clone as a builtin command Kristian Høgsberg
2007-12-11 20:59 ` Daniel Barkalow
2007-12-11 23:38 ` Kristian Høgsberg
2007-12-12 3:12 ` Junio C Hamano
2007-12-12 3:20 ` Junio C Hamano
2007-12-12 11:12 ` Johannes Schindelin
2007-12-12 15:04 ` Kristian Høgsberg
2007-12-12 18:24 ` Johannes Schindelin
2007-12-12 15:24 ` Kristian Høgsberg
2007-12-12 18:00 ` Daniel Barkalow
2007-12-12 18:25 ` Kristian Høgsberg
2007-12-12 18:40 ` Daniel Barkalow
2007-12-24 1:52 ` J. Bruce Fields
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).