@matheusbsbs / matheus-bagliano / commits / c847f53712
Fork 0

Detached HEAD (experimental)

This allows "git checkout v1.4.3" to dissociate the HEAD of repository from any branch. After this point, "git branch" starts reporting that you are not on any branch. You can go back to an existing branch by saying "git checkout master", for example. This is still experimental. While I think it makes sense to allow commits on top of detached HEAD, it is rather dangerous unless you are careful in the current form. Next "git checkout master" will obviously lose what you have done, so we might want to require "git checkout -f" out of a detached HEAD if we find that the HEAD commit is not an ancestor of any other branches. There is no such safety valve implemented right now. On the other hand, the reason the user did not start the ad-hoc work on a new branch with "git checkout -b" was probably because the work was of a throw-away nature, so the convenience of not having that safety valve might be even better. The user, after accumulating some commits on top of a detached HEAD, can always create a new branch with "git checkout -b" not to lose useful work done while the HEAD was detached. We'll see. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jan 1, 2007 at 23:31 UTC c847f537125ceab3425205721fdaaa834e6d8a83
5 files changed +50 -23
builtin-branch.c
+17 -7
@@ -308,7 +308,8 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
308 free_ref_list(&ref_list);
309 }
310
311 -static void create_branch(const char *name, const char *start,
311 +static void create_branch(const char *name, const char *start_name,
312 + unsigned char *start_sha1,
313 int force, int reflog)
314 {
315 struct ref_lock *lock;
@@ -327,9 +328,14 @@ static void create_branch(const char *name, const char *start,
328 die("Cannot force update the current branch.");
329 }
330
330 - if (get_sha1(start, sha1) ||
331 - (commit = lookup_commit_reference(sha1)) == NULL)
332 - die("Not a valid branch point: '%s'.", start);
331 + if (start_sha1)
332 + /* detached HEAD */
333 + hashcpy(sha1, start_sha1);
334 + else if (get_sha1(start_name, sha1))
335 + die("Not a valid object name: '%s'.", start_name);
336 +
337 + if ((commit = lookup_commit_reference(sha1)) == NULL)
338 + die("Not a valid branch point: '%s'.", start_name);
339 hashcpy(sha1, commit->object.sha1);
340
341 lock = lock_any_ref_for_update(ref, NULL);
@@ -338,7 +344,8 @@ static void create_branch(const char *name, const char *start,
344
345 if (reflog) {
346 log_all_ref_updates = 1;
341 - snprintf(msg, sizeof msg, "branch: Created from %s", start);
347 + snprintf(msg, sizeof msg, "branch: Created from %s",
348 + start_name);
349 }
350
351 if (write_ref_sha1(lock, sha1, msg) < 0)
@@ -350,6 +357,9 @@ static void rename_branch(const char *oldname, const char *newname, int force)
357 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
358 unsigned char sha1[20];
359
360 + if (!oldname)
361 + die("cannot rename the curren branch while not on any.");
362 +
363 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
364 die("Old branchname too long");
365
@@ -474,9 +484,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
484 else if (rename && (i == argc - 2))
485 rename_branch(argv[i], argv[i + 1], force_rename);
486 else if (i == argc - 1)
477 - create_branch(argv[i], head, force_create, reflog);
487 + create_branch(argv[i], head, head_sha1, force_create, reflog);
488 else if (i == argc - 2)
479 - create_branch(argv[i], argv[i + 1], force_create, reflog);
489 + create_branch(argv[i], argv[i+1], NULL, force_create, reflog);
490 else
491 usage(builtin_branch_usage);
492
cache.h
+1 -1
@@ -299,7 +299,7 @@ extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
299 extern int read_ref(const char *filename, unsigned char *sha1);
300 extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
301 extern int create_symref(const char *ref, const char *refs_heads_master);
302 -extern int validate_symref(const char *ref);
302 +extern int validate_headref(const char *ref);
303
304 extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
305 extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);
git-checkout.sh
+11 -5
@@ -144,13 +144,19 @@ fi
144 # are switching to, then we'd better just be checking out
145 # what we already had
146
147 -[ -z "$branch$newbranch" ] &&
148 - [ "$new" != "$old" ] &&
149 - die "git checkout: provided reference cannot be checked out directly
150 -
151 - You need -b to associate a new branch with the wanted checkout. Example:
147 +if test -z "$branch$newbranch" && test "$new" != "$old"
148 +then
149 + # NEEDSWORK: we would want to have this command here
150 + # that allows us to detach the HEAD atomically.
151 + # git update-ref --detach HEAD "$new"
152 + rm -f "$GIT_DIR/HEAD"
153 + echo "$new" >"$GIT_DIR/HEAD"
154 + echo >&2 "WARNING: you are not on ANY branch anymore.
155 +If you meant to create a new branch from the commit, you need -b to
156 +associate a new branch with the wanted checkout. Example:
157 git checkout -b <new_branch_name> $arg
158 "
159 +fi
160
161 if [ "X$old" = X ]
162 then
path.c
+18 -8
@@ -90,10 +90,11 @@ int git_mkstemp(char *path, size_t len, const char *template)
90 }
91
92
93 -int validate_symref(const char *path)
93 +int validate_headref(const char *path)
94 {
95 struct stat st;
96 char *buf, buffer[256];
97 + unsigned char sha1[20];
98 int len, fd;
99
100 if (lstat(path, &st) < 0)
@@ -119,14 +120,23 @@ int validate_symref(const char *path)
120 /*
121 * Is it a symbolic ref?
122 */
122 - if (len < 4 || memcmp("ref:", buffer, 4))
123 + if (len < 4)
124 return -1;
124 - buf = buffer + 4;
125 - len -= 4;
126 - while (len && isspace(*buf))
127 - buf++, len--;
128 - if (len >= 5 && !memcmp("refs/", buf, 5))
125 + if (!memcmp("ref:", buffer, 4)) {
126 + buf = buffer + 4;
127 + len -= 4;
128 + while (len && isspace(*buf))
129 + buf++, len--;
130 + if (len >= 5 && !memcmp("refs/", buf, 5))
131 + return 0;
132 + }
133 +
134 + /*
135 + * Is this a detached HEAD?
136 + */
137 + if (!get_sha1_hex(buffer, sha1))
138 return 0;
139 +
140 return -1;
141 }
142
@@ -241,7 +251,7 @@ char *enter_repo(char *path, int strict)
251 return NULL;
252
253 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
244 - validate_symref("HEAD") == 0) {
254 + validate_headref("HEAD") == 0) {
255 putenv("GIT_DIR=.");
256 check_repository_format();
257 return path;
setup.c
+3 -2
@@ -138,7 +138,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
138 * GIT_OBJECT_DIRECTORY environment variable
139 * - a refs/ directory
140 * - either a HEAD symlink or a HEAD file that is formatted as
141 - * a proper "ref:".
141 + * a proper "ref:", or a regular file HEAD that has a properly
142 + * formatted sha1 object name.
143 */
144 static int is_git_directory(const char *suspect)
145 {
@@ -161,7 +162,7 @@ static int is_git_directory(const char *suspect)
162 return 0;
163
164 strcpy(path + len, "/HEAD");
164 - if (validate_symref(path))
165 + if (validate_headref(path))
166 return 0;
167
168 return 1;