From feb98d134289527eeb9d1fd11d8c284819a0aa1d Mon Sep 17 00:00:00 2001 From: Erick Mattos Date: Fri, 21 May 2010 21:28:35 -0300 Subject: Documentation: alter checkout --orphan description The present text is a try to enhance description accuracy. It is a merge of the rewritten text made by native english speaker Chris Johnsen and further changes of Junio. It came from the last thread messages of --orphan patch. Signed-off-by: Erick Mattos Signed-off-by: Junio C Hamano --- Documentation/git-checkout.txt | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index 4505eb6d84..b84ec26276 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -91,22 +91,29 @@ explicitly give a name with '-b' in such a case. details. --orphan:: - Create a new branch named , unparented to any other - branch. The new branch you switch to does not have any commit - and after the first one it will become the root of a new history - completely unconnected from all the other branches. + Create a new 'orphan' branch, named , started from + and switch to it. The first commit made on this + new branch will have no parents and it will be the root of a new + history totally disconnected from all the other branches and + commits. + -When you use "--orphan", the index and the working tree are kept intact. -This allows you to start a new history that records set of paths similar -to that of the start-point commit, which is useful when you want to keep -different branches for different audiences you are working to like when -you have an open source and commercial versions of a software, for example. +The index and the working tree are adjusted as if you had previously run +"git checkout ". This allows you to start a new history +that records a set of paths similar to by easily running +"git commit -a" to make the root commit. + -If you want to start a disconnected history that records set of paths -totally different from the original branch, you may want to first clear -the index and the working tree, by running "git rm -rf ." from the -top-level of the working tree, before preparing your files (by copying -from elsewhere, extracting a tarball, etc.) in the working tree. +This can be useful when you want to publish the tree from a commit +without exposing its full history. You might want to do this to publish +an open source branch of a project whose current tree is "clean", but +whose full history contains proprietary or otherwise encumbered bits of +code. ++ +If you want to start a disconnected history that records a set of paths +that is totally different from the one of , then you should +clear the index and the working tree right after creating the orphan +branch by running "git rm -rf ." from the top level of the working tree. +Afterwards you will be ready to prepare your new files, repopulating the +working tree, by copying them from elsewhere, extracting a tarball, etc. -m:: --merge:: -- cgit v1.2.3 From 859c30175f7d8f64f61968cb3ece7aca9218b314 Mon Sep 17 00:00:00 2001 From: Erick Mattos Date: Fri, 21 May 2010 21:28:36 -0300 Subject: refs: split log_ref_write logic into log_ref_setup Separation of the logic for testing and preparing the reflogs from function log_ref_write to a new non static new function: log_ref_setup. This allows to be performed from outside the first all reasonable checks and procedures for writing reflogs. Signed-off-by: Erick Mattos Signed-off-by: Junio C Hamano --- refs.c | 57 ++++++++++++++++++++++++++++++++++++--------------------- refs.h | 3 +++ 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/refs.c b/refs.c index d3db15a76c..1161c2d0d9 100644 --- a/refs.c +++ b/refs.c @@ -1258,52 +1258,67 @@ static int copy_msg(char *buf, const char *msg) return cp - buf; } -static int log_ref_write(const char *ref_name, const unsigned char *old_sha1, - const unsigned char *new_sha1, const char *msg) +int log_ref_setup(const char *ref_name, char **log_file) { - int logfd, written, oflags = O_APPEND | O_WRONLY; - unsigned maxlen, len; - int msglen; - char log_file[PATH_MAX]; - char *logrec; - const char *committer; - - if (log_all_ref_updates < 0) - log_all_ref_updates = !is_bare_repository(); - - git_snpath(log_file, sizeof(log_file), "logs/%s", ref_name); + int logfd, oflags = O_APPEND | O_WRONLY; + char logfile[PATH_MAX]; + git_snpath(logfile, sizeof(logfile), "logs/%s", ref_name); + *log_file = logfile; if (log_all_ref_updates && (!prefixcmp(ref_name, "refs/heads/") || !prefixcmp(ref_name, "refs/remotes/") || !prefixcmp(ref_name, "refs/notes/") || !strcmp(ref_name, "HEAD"))) { - if (safe_create_leading_directories(log_file) < 0) + if (safe_create_leading_directories(*log_file) < 0) return error("unable to create directory for %s", - log_file); + *log_file); oflags |= O_CREAT; } - logfd = open(log_file, oflags, 0666); + logfd = open(*log_file, oflags, 0666); if (logfd < 0) { if (!(oflags & O_CREAT) && errno == ENOENT) return 0; if ((oflags & O_CREAT) && errno == EISDIR) { - if (remove_empty_directories(log_file)) { + if (remove_empty_directories(*log_file)) { return error("There are still logs under '%s'", - log_file); + *log_file); } - logfd = open(log_file, oflags, 0666); + logfd = open(*log_file, oflags, 0666); } if (logfd < 0) return error("Unable to append to %s: %s", - log_file, strerror(errno)); + *log_file, strerror(errno)); } - adjust_shared_perm(log_file); + adjust_shared_perm(*log_file); + close(logfd); + return 0; +} +static int log_ref_write(const char *ref_name, const unsigned char *old_sha1, + const unsigned char *new_sha1, const char *msg) +{ + int logfd, result, written, oflags = O_APPEND | O_WRONLY; + unsigned maxlen, len; + int msglen; + char *log_file; + char *logrec; + const char *committer; + + if (log_all_ref_updates < 0) + log_all_ref_updates = !is_bare_repository(); + + result = log_ref_setup(ref_name, &log_file); + if (result) + return result; + + logfd = open(log_file, oflags); + if (logfd < 0) + return 0; msglen = msg ? strlen(msg) : 0; committer = git_committer_info(0); maxlen = strlen(committer) + msglen + 100; diff --git a/refs.h b/refs.h index 4a18b083f5..594c9d97fd 100644 --- a/refs.h +++ b/refs.h @@ -68,6 +68,9 @@ extern void unlock_ref(struct ref_lock *lock); /** Writes sha1 into the ref specified by the lock. **/ extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg); +/** Setup reflog before using. **/ +int log_ref_setup(const char *ref_name, char **log_file); + /** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt); -- cgit v1.2.3 From 3631bf77f75bc34e810216f3ec6abc8203e2d16a Mon Sep 17 00:00:00 2001 From: Erick Mattos Date: Fri, 21 May 2010 21:28:37 -0300 Subject: checkout --orphan: respect -l option always Added changes to satisfy a corner case: creating reflogs by using -l when core.logAllRefUpdates is set to false. Signed-off-by: Erick Mattos Signed-off-by: Junio C Hamano --- builtin/checkout.c | 31 ++++++++++++++++-- t/t2017-checkout-orphan.sh | 78 +++++++++++++++++++++++++++++++++------------- 2 files changed, 85 insertions(+), 24 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index c3825219c1..9d618c21d2 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -493,7 +493,24 @@ static void update_refs_for_switch(struct checkout_opts *opts, struct strbuf msg = STRBUF_INIT; const char *old_desc; if (opts->new_branch) { - if (!opts->new_orphan_branch) + if (opts->new_orphan_branch) { + if (opts->new_branch_log && !log_all_ref_updates) { + int temp; + char *log_file; + char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch); + + temp = log_all_ref_updates; + log_all_ref_updates = 1; + if (log_ref_setup(ref_name, &log_file)) { + fprintf(stderr, "Can not do reflog for '%s'\n", + opts->new_orphan_branch); + log_all_ref_updates = temp; + return; + } + log_all_ref_updates = temp; + } + } + else create_branch(old->name, opts->new_branch, new->name, 0, opts->new_branch_log, opts->track); new->name = opts->new_branch; @@ -517,6 +534,14 @@ static void update_refs_for_switch(struct checkout_opts *opts, opts->new_branch ? " a new" : "", new->name); } + if (old->path && old->name) { + char log_file[PATH_MAX], ref_file[PATH_MAX]; + + git_snpath(log_file, sizeof(log_file), "logs/%s", old->path); + git_snpath(ref_file, sizeof(ref_file), "%s", old->path); + if (!file_exists(ref_file) && file_exists(log_file)) + remove_path(log_file); + } } else if (strcmp(new->name, "HEAD")) { update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL, REF_NODEREF, DIE_ON_ERR); @@ -684,8 +709,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) if (opts.new_orphan_branch) { if (opts.new_branch) die("--orphan and -b are mutually exclusive"); - if (opts.track > 0 || opts.new_branch_log) - die("--orphan cannot be used with -t or -l"); + if (opts.track > 0) + die("--orphan cannot be used with -t"); opts.new_branch = opts.new_orphan_branch; } diff --git a/t/t2017-checkout-orphan.sh b/t/t2017-checkout-orphan.sh index a8297c61bd..be88d4b5ee 100755 --- a/t/t2017-checkout-orphan.sh +++ b/t/t2017-checkout-orphan.sh @@ -49,6 +49,62 @@ test_expect_success '--orphan must be rejected with -b' ' test refs/heads/master = "$(git symbolic-ref HEAD)" ' +test_expect_success '--orphan must be rejected with -t' ' + git checkout master && + test_must_fail git checkout --orphan new -t master && + test refs/heads/master = "$(git symbolic-ref HEAD)" +' + +test_expect_success '--orphan ignores branch.autosetupmerge' ' + git checkout master && + git config branch.autosetupmerge always && + git checkout --orphan gamma && + test -z "$(git config branch.gamma.merge)" && + test refs/heads/gamma = "$(git symbolic-ref HEAD)" && + test_must_fail git rev-parse --verify HEAD^ +' + +test_expect_success '--orphan makes reflog by default' ' + git checkout master && + git config --unset core.logAllRefUpdates && + git checkout --orphan delta && + ! test -f .git/logs/refs/heads/delta && + test_must_fail PAGER= git reflog show delta && + git commit -m Delta && + test -f .git/logs/refs/heads/delta && + PAGER= git reflog show delta +' + +test_expect_success '--orphan does not make reflog when core.logAllRefUpdates = false' ' + git checkout master && + git config core.logAllRefUpdates false && + git checkout --orphan epsilon && + ! test -f .git/logs/refs/heads/epsilon && + test_must_fail PAGER= git reflog show epsilon && + git commit -m Epsilon && + ! test -f .git/logs/refs/heads/epsilon && + test_must_fail PAGER= git reflog show epsilon +' + +test_expect_success '--orphan with -l makes reflog when core.logAllRefUpdates = false' ' + git checkout master && + git checkout -l --orphan zeta && + test -f .git/logs/refs/heads/zeta && + test_must_fail PAGER= git reflog show zeta && + git commit -m Zeta && + PAGER= git reflog show zeta +' + +test_expect_success 'giving up --orphan not committed when -l and core.logAllRefUpdates = false deletes reflog' ' + git checkout master && + git checkout -l --orphan eta && + test -f .git/logs/refs/heads/eta && + test_must_fail PAGER= git reflog show eta && + git checkout master && + ! test -f .git/logs/refs/heads/eta && + test_must_fail PAGER= git reflog show eta +' + test_expect_success '--orphan is rejected with an existing name' ' git checkout master && test_must_fail git checkout --orphan master && @@ -60,31 +116,11 @@ test_expect_success '--orphan refuses to switch if a merge is needed' ' git reset --hard && echo local >>"$TEST_FILE" && cat "$TEST_FILE" >"$TEST_FILE.saved" && - test_must_fail git checkout --orphan gamma master^ && + test_must_fail git checkout --orphan new master^ && test refs/heads/master = "$(git symbolic-ref HEAD)" && test_cmp "$TEST_FILE" "$TEST_FILE.saved" && git diff-index --quiet --cached HEAD && git reset --hard ' -test_expect_success '--orphan does not mix well with -t' ' - git checkout master && - test_must_fail git checkout -t master --orphan gamma && - test refs/heads/master = "$(git symbolic-ref HEAD)" -' - -test_expect_success '--orphan ignores branch.autosetupmerge' ' - git checkout -f master && - git config branch.autosetupmerge always && - git checkout --orphan delta && - test -z "$(git config branch.delta.merge)" && - test refs/heads/delta = "$(git symbolic-ref HEAD)" && - test_must_fail git rev-parse --verify HEAD^ -' - -test_expect_success '--orphan does not mix well with -l' ' - git checkout -f master && - test_must_fail git checkout -l --orphan gamma -' - test_done -- cgit v1.2.3 From b2099957752071fec70502c81206364ac986073a Mon Sep 17 00:00:00 2001 From: Erick Mattos Date: Fri, 21 May 2010 21:28:38 -0300 Subject: t3200: test -l with core.logAllRefUpdates options By default reflogs are always created for new local branches by "checkout -b". But by setting core.logAllRefUpdates to false this will not be true anymore. In that case you only create the reflogs when you use -l switch with "checkout -b". Added missing tests to check expected behaviors. Signed-off-by: Erick Mattos Signed-off-by: Junio C Hamano --- t/t3200-branch.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index e0b760513c..9d2c06ea69 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -224,6 +224,30 @@ test_expect_success \ test -f .git/logs/refs/heads/g/h/i && diff expect .git/logs/refs/heads/g/h/i' +test_expect_success 'checkout -b makes reflog by default' ' + git checkout master && + git config --unset core.logAllRefUpdates && + git checkout -b alpha && + test -f .git/logs/refs/heads/alpha && + PAGER= git reflog show alpha +' + +test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' ' + git checkout master && + git config core.logAllRefUpdates false && + git checkout -b beta && + ! test -f .git/logs/refs/heads/beta && + test_must_fail PAGER= git reflog show beta +' + +test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' ' + git checkout master && + git checkout -lb gamma && + git config --unset core.logAllRefUpdates && + test -f .git/logs/refs/heads/gamma && + PAGER= git reflog show gamma +' + test_expect_success 'avoid ambiguous track' ' git config branch.autosetupmerge true && git config remote.ambi1.url lalala && -- cgit v1.2.3 From 86e8e7a566c5546e58f56566d0181a08d60f919f Mon Sep 17 00:00:00 2001 From: Erick Mattos Date: Fri, 21 May 2010 21:43:52 -0300 Subject: bash completion: add --orphan to 'git checkout' Update git-completion.bash with new --orphan option to 'git checkout'. Signed-off-by: Erick Mattos Signed-off-by: Junio C Hamano --- contrib/completion/git-completion.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 545bd4b383..b70cca13fd 100755 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -841,7 +841,7 @@ _git_checkout () --*) __gitcomp " --quiet --ours --theirs --track --no-track --merge - --conflict= --patch + --conflict= --orphan --patch " ;; *) -- cgit v1.2.3 From 157aaea5fff7dbf2fc39829d827b35df647562a4 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Thu, 10 Jun 2010 14:54:03 +0200 Subject: log_ref_setup: don't return stack-allocated array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 859c301 (refs: split log_ref_write logic into log_ref_setup, 2010-05-21) refactors the stack allocation of the log_file array into the new log_ref_setup() function, but passes it back to the caller. Since the original intent seems to have been to split the work between log_ref_setup and log_ref_write, make it the caller's responsibility to allocate the buffer. Signed-off-by: Thomas Rast Reported-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- builtin/checkout.c | 4 ++-- refs.c | 26 ++++++++++++-------------- refs.h | 2 +- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index 9d618c21d2..72e4fbc729 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -496,12 +496,12 @@ static void update_refs_for_switch(struct checkout_opts *opts, if (opts->new_orphan_branch) { if (opts->new_branch_log && !log_all_ref_updates) { int temp; - char *log_file; + char log_file[PATH_MAX]; char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch); temp = log_all_ref_updates; log_all_ref_updates = 1; - if (log_ref_setup(ref_name, &log_file)) { + if (log_ref_setup(ref_name, log_file, sizeof(log_file))) { fprintf(stderr, "Can not do reflog for '%s'\n", opts->new_orphan_branch); log_all_ref_updates = temp; diff --git a/refs.c b/refs.c index 1161c2d0d9..10abda7d0d 100644 --- a/refs.c +++ b/refs.c @@ -1258,43 +1258,41 @@ static int copy_msg(char *buf, const char *msg) return cp - buf; } -int log_ref_setup(const char *ref_name, char **log_file) +int log_ref_setup(const char *ref_name, char *logfile, int bufsize) { int logfd, oflags = O_APPEND | O_WRONLY; - char logfile[PATH_MAX]; - git_snpath(logfile, sizeof(logfile), "logs/%s", ref_name); - *log_file = logfile; + git_snpath(logfile, bufsize, "logs/%s", ref_name); if (log_all_ref_updates && (!prefixcmp(ref_name, "refs/heads/") || !prefixcmp(ref_name, "refs/remotes/") || !prefixcmp(ref_name, "refs/notes/") || !strcmp(ref_name, "HEAD"))) { - if (safe_create_leading_directories(*log_file) < 0) + if (safe_create_leading_directories(logfile) < 0) return error("unable to create directory for %s", - *log_file); + logfile); oflags |= O_CREAT; } - logfd = open(*log_file, oflags, 0666); + logfd = open(logfile, oflags, 0666); if (logfd < 0) { if (!(oflags & O_CREAT) && errno == ENOENT) return 0; if ((oflags & O_CREAT) && errno == EISDIR) { - if (remove_empty_directories(*log_file)) { + if (remove_empty_directories(logfile)) { return error("There are still logs under '%s'", - *log_file); + logfile); } - logfd = open(*log_file, oflags, 0666); + logfd = open(logfile, oflags, 0666); } if (logfd < 0) return error("Unable to append to %s: %s", - *log_file, strerror(errno)); + logfile, strerror(errno)); } - adjust_shared_perm(*log_file); + adjust_shared_perm(logfile); close(logfd); return 0; } @@ -1305,14 +1303,14 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1, int logfd, result, written, oflags = O_APPEND | O_WRONLY; unsigned maxlen, len; int msglen; - char *log_file; + char log_file[PATH_MAX]; char *logrec; const char *committer; if (log_all_ref_updates < 0) log_all_ref_updates = !is_bare_repository(); - result = log_ref_setup(ref_name, &log_file); + result = log_ref_setup(ref_name, log_file, sizeof(log_file)); if (result) return result; diff --git a/refs.h b/refs.h index 594c9d97fd..762ce504b5 100644 --- a/refs.h +++ b/refs.h @@ -69,7 +69,7 @@ extern void unlock_ref(struct ref_lock *lock); extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg); /** Setup reflog before using. **/ -int log_ref_setup(const char *ref_name, char **log_file); +int log_ref_setup(const char *ref_name, char *logfile, int bufsize); /** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt); -- cgit v1.2.3