make reflog filename independent from struct ref_lock
PATCH 1/7 make reflog filename independent from struct ref_lock
This allows for ref_log_write() to be used in a more flexible way, and is needed for future changes.
This is only code reorg with no behavior change.
--- builtin-reflog.c | 10 ++++++---- refs.c | 44 ++++++++++++++++++++++---------------------- refs.h | 1 - 3 files changed, 28 insertions(+), 27 deletions(-)
builtin-reflog.c +6 -4
@@ -242,7 +242,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,struct cmd_reflog_expire_cb *cmd = cb_data;struct expire_reflog_cb cb;struct ref_lock *lock; -char *newlog_path =NULL ; +char *log_file, *newlog_path =NULL ;int status =0 ;if (strncmp (ref,"refs/" ,5 ))
@@ -255,7 +255,8 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, lock =lock_ref_sha1 (ref +5 , sha1);if (!lock)return error ("cannot lock ref '%s'" , ref); -if (!file_exists (lock->log_file)) + log_file =xstrdup (git_path ("logs/%s" , ref)); +if (!file_exists (log_file)) goto finish;if (!cmd->dry_run) { newlog_path =xstrdup (git_path ("logs/%s.lock" , ref));
@@ -271,13 +272,14 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,if (fclose (cb.newlog)) status |=error ("%s: %s" ,strerror (errno), newlog_path); -if (rename (newlog_path, lock->log_file)) { +if (rename (newlog_path, log_file)) { status |=error ("cannot rename %s to %s" , - newlog_path, lock->log_file); + newlog_path, log_file);unlink (newlog_path); } }free (newlog_path); +free (log_file);unlock_ref (lock);return status; }
refs.c +22 -22
115 lines not on this page · raw message
refs.h +0 -1
@@ -3,7 +3,6 @@struct ref_lock {char *ref_name; -char *log_file;struct lock_file *lk;unsigned char old_sha1[20 ];int lock_fd;
Signature · 1 line
1.5.0.rc2.g6e2c
PATCH 2/7 lock_ref_sha1_basic(): remember the original name of a ref when resolving it
A ref might be pointing to another ref but only the name of the last ref is remembered. Let's remember about the first name as well.
--- refs.c | 2 ++ refs.h | 1 + 2 files changed, 3 insertions(+), 0 deletions(-)
refs.c +2 -0
@@ -676,6 +676,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char lock->lk =xcalloc (1 ,sizeof (struct lock_file )); lock->ref_name =xstrdup (ref); + lock->orig_ref_name =xstrdup (orig_ref); ref_file =git_path ("%s" , ref); lock->force_write =lstat (ref_file, &st) && errno ==ENOENT ;
@@ -915,6 +916,7 @@ void unlock_ref(struct ref_lock *lock)rollback_lock_file (lock->lk); }free (lock->ref_name); +free (lock->orig_ref_name);free (lock); }
refs.h +1 -0
@@ -3,6 +3,7 @@struct ref_lock {char *ref_name; +char *orig_ref_name;struct lock_file *lk;unsigned char old_sha1[20 ];int lock_fd;
Signature · 1 line
1.5.0.rc2.g6e2c
PATCH 3/7 enable separate reflog for HEAD
If HEAD is tied to a branch then both logs/HEAD and logs/heads/<branch> are updated. This is also true for any symbolic refs in general, but only HEAD will see its reflog created automatically.
--- refs.c | 7 +++++-- 1 files changed, 5 insertions(+), 2 deletions(-)
refs.c +5 -2
@@ -935,7 +935,8 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,if (log_all_ref_updates && (!strncmp (ref_name,"refs/heads/" ,11 ) || - !strncmp (ref_name,"refs/remotes/" ,13 ))) { + !strncmp (ref_name,"refs/remotes/" ,13 ) || + !strcmp (ref_name,"HEAD" ))) {if (safe_create_leading_directories (log_file) <0 )return error ("unable to create directory for %s" , log_file);
@@ -1005,7 +1006,9 @@ int write_ref_sha1(struct ref_lock *lock,return -1 ; }invalidate_cached_refs (); -if (log_ref_write (lock->ref_name, lock->old_sha1, sha1, logmsg) <0 ) { +if (log_ref_write (lock->ref_name, lock->old_sha1, sha1, logmsg) <0 || + (strcmp (lock->ref_name, lock->orig_ref_name) && +log_ref_write (lock->orig_ref_name, lock->old_sha1, sha1, logmsg) <0 )) {unlock_ref (lock);return -1 ; }
Signature · 1 line
1.5.0.rc2.g6e2c
PATCH 4/7 add reflog entries for HEAD when detached
--- git-checkout.sh | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
git-checkout.sh +3 -2
@@ -259,8 +259,9 @@ if [ "$?" -eq 0 ]; then # git update-ref --detach HEAD $new # or something like that... # - echo "$detached" >"$GIT_DIR/HEAD.new" && - mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" || + git-rev-parse HEAD >"$GIT_DIR/HEAD.new" && + mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" && + git-update-ref -m "checkout: moving to $arg" HEAD "$detached" || die "Cannot detach HEAD" if test -n "$detach_warn" then
Signature · 1 line
1.5.0.rc2.g6e2c
PATCH 5/7 move create_symref() past log_ref_write()
This doesn't change the code at all. It is done to makes the next patch more obvious.
--- refs.c | 86 ++++++++++++++++++++++++++++++++-------------------------------- 1 files changed, 43 insertions(+), 43 deletions(-)
refs.c +43 -43
@@ -309,49 +309,6 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *return ref; } -int create_symref (const char *ref_target,const char *refs_heads_master) -{ -const char *lockpath; -char ref[1000 ]; -int fd, len, written; -const char *git_HEAD =git_path ("%s" , ref_target); - -#ifndef NO_SYMLINK_HEAD -if (prefer_symlink_refs) { -unlink (git_HEAD); -if (!symlink (refs_heads_master, git_HEAD)) -return 0 ; -fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); - } -#endif - - len =snprintf (ref,sizeof (ref),"ref: %s\n" , refs_heads_master); -if (sizeof (ref) <= len) { -error ("refname too long: %s" , refs_heads_master); -return -1 ; - } - lockpath =mkpath ("%s.lock" , git_HEAD); - fd =open (lockpath,O_CREAT |O_EXCL |O_WRONLY ,0666 ); - written =write_in_full (fd, ref, len); -close (fd); -if (written != len) { -unlink (lockpath); -error ("Unable to write to %s" , lockpath); -return -2 ; - } -if (rename (lockpath, git_HEAD) <0 ) { -unlink (lockpath); -error ("Unable to create %s" , git_HEAD); -return -3 ; - } -if (adjust_shared_perm (git_HEAD)) { -unlink (lockpath); -error ("Unable to fix permissions on %s" , lockpath); -return -4 ; - } -return 0 ; -} -int read_ref (const char *ref,unsigned char *sha1) {if (resolve_ref (ref, sha1,1 ,NULL ))
@@ -1022,6 +979,49 @@ int write_ref_sha1(struct ref_lock *lock,return 0 ; } +int create_symref (const char *ref_target,const char *refs_heads_master) +{ +const char *lockpath; +char ref[1000 ]; +int fd, len, written; +const char *git_HEAD =git_path ("%s" , ref_target); + +#ifndef NO_SYMLINK_HEAD +if (prefer_symlink_refs) { +unlink (git_HEAD); +if (!symlink (refs_heads_master, git_HEAD)) +return 0 ; +fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); + } +#endif + + len =snprintf (ref,sizeof (ref),"ref: %s\n" , refs_heads_master); +if (sizeof (ref) <= len) { +error ("refname too long: %s" , refs_heads_master); +return -1 ; + } + lockpath =mkpath ("%s.lock" , git_HEAD); + fd =open (lockpath,O_CREAT |O_EXCL |O_WRONLY ,0666 ); + written =write_in_full (fd, ref, len); +close (fd); +if (written != len) { +unlink (lockpath); +error ("Unable to write to %s" , lockpath); +return -2 ; + } +if (rename (lockpath, git_HEAD) <0 ) { +unlink (lockpath); +error ("Unable to create %s" , git_HEAD); +return -3 ; + } +if (adjust_shared_perm (git_HEAD)) { +unlink (lockpath); +error ("Unable to fix permissions on %s" , lockpath); +return -4 ; + } +return 0 ; +} +static char *ref_msg (const char *line,const char *endp) {const char *ep;
Signature · 1 line
1.5.0.rc2.g6e2c
PATCH 6/7 add logref support to git-symbolic-ref
--- Documentation/git-symbolic-ref.txt | 6 +++++- builtin-branch.c | 3 ++- builtin-init-db.c | 2 +- builtin-symbolic-ref.c | 16 ++++++++++++++-- cache.h | 2 +- refs.c | 14 ++++++++++++-- 6 files changed, 35 insertions(+), 8 deletions(-)
Documentation/git-symbolic-ref.txt +5 -1
@@ -7,7 +7,7 @@ git-symbolic-ref - Read and modify symbolic refs SYNOPSIS -------- -'git-symbolic-ref' [-q] <name> [<ref>] +'git-symbolic-ref' [-q] [-m <reason>] <name> [<ref>] DESCRIPTION -----------
@@ -31,6 +31,10 @@ OPTIONS symbolic ref but a detached HEAD; instead exit with non-zero status silently. +-m:: + Update the reflog for <name> with <reason>. This is valid only + when creating or updating a symbolic ref. + NOTES ----- In the past, `.git/HEAD` was a symbolic link pointing at
builtin-branch.c +2 -1
@@ -381,7 +381,8 @@ static void rename_branch(const char *oldname, const char *newname, int force)if (rename_ref (oldref, newref, logmsg))die ("Branch rename failed" ); -if (!strcmp (oldname, head) &&create_symref ("HEAD" , newref)) +/* no need to pass logmsg here as HEAD didn't really move */ +if (!strcmp (oldname, head) &&create_symref ("HEAD" , newref,NULL ))die ("Branch renamed to %s, but HEAD is not updated!" , newname); }
builtin-init-db.c +1 -1
@@ -231,7 +231,7 @@ static int create_default_files(const char *git_dir, const char *template_path)strcpy (path + len,"HEAD" ); reinit = !read_ref ("HEAD" , sha1);if (!reinit) { -if (create_symref ("HEAD" ,"refs/heads/master" ) <0 ) +if (create_symref ("HEAD" ,"refs/heads/master" ,NULL ) <0 )exit (1 ); }
builtin-symbolic-ref.c +14 -2
@@ -3,7 +3,7 @@#include "refs.h" static const char git_symbolic_ref_usage[] = -"git-symbolic-ref [-q] name [ref]" ; +"git-symbolic-ref [-q] [-m <reason>] name [ref]" ;static void check_symref (const char *HEAD ,int quiet) {
@@ -25,6 +25,7 @@ static void check_symref(const char *HEAD, int quiet)int cmd_symbolic_ref (int argc,const char **argv,const char *prefix) {int quiet =0 ; +const char *msg =NULL ;git_config (git_default_config);
@@ -34,6 +35,17 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)break ; else if (!strcmp("-q", arg)) quiet =1 ; + else if (!strcmp("-m", arg)) { + argc--; + argv++; +if (argc <=1 ) +break ; + msg = argv[1 ]; +if (!*msg) +die ("Refusing to perform update with empty message" ); +if (strchr (msg,'\n' )) +die ("Refusing to perform update with \\n in message" ); + } else if (!strcmp("--", arg)) { argc--; argv++;
@@ -50,7 +62,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)check_symref (argv[1 ], quiet);break ;case 3 : -create_symref (argv[1 ], argv[2 ]); +create_symref (argv[1 ], argv[2 ], msg);break ;default :usage (git_symbolic_ref_usage);
cache.h +1 -1
@@ -302,7 +302,7 @@ extern int read_ref(const char *filename, unsigned char *sha1);extern const char *resolve_ref (const char *path,unsigned char *sha1,int ,int *);extern int dwim_ref (const char *str,int len,unsigned char *sha1,char **ref); -extern int create_symref (const char *ref,const char *refs_heads_master); +extern int create_symref (const char *ref,const char *refs_heads_master,const char *logmsg);extern int validate_headref (const char *ref);extern int base_name_compare (const char *name1,int len1,int mode1,const char *name2,int len2,int mode2);
refs.c +12 -2
@@ -979,18 +979,23 @@ int write_ref_sha1(struct ref_lock *lock,return 0 ; } -int create_symref (const char *ref_target,const char *refs_heads_master) +int create_symref (const char *ref_target,const char *refs_heads_master, +const char *logmsg) {const char *lockpath;char ref[1000 ];int fd, len, written;const char *git_HEAD =git_path ("%s" , ref_target); +unsigned char old_sha1[20 ], new_sha1[20 ]; + +if (logmsg &&read_ref (ref_target, old_sha1)) +hashclr (old_sha1);#ifndef NO_SYMLINK_HEAD if (prefer_symlink_refs) {unlink (git_HEAD);if (!symlink (refs_heads_master, git_HEAD)) -return 0 ; + goto done;fprintf (stderr,"no symlink - falling back to symbolic ref\n" ); }#endif
@@ -1019,6 +1024,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master)error ("Unable to fix permissions on %s" , lockpath);return -4 ; } + + done: +if (logmsg && !read_ref (refs_heads_master, new_sha1)) +log_ref_write (ref_target, old_sha1, new_sha1, logmsg); +return 0 ; }
Signature · 1 line
1.5.0.rc2.g6e2c
PATCH 7/7 add reflog when moving HEAD to a new branch
--- git-checkout.sh | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
git-checkout.sh +1 -1
@@ -250,7 +250,7 @@ if [ "$?" -eq 0 ]; then fi if test -n "$branch" then - GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch" + GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch" elif test -n "$detached" then # NEEDSWORK: we would want a command to detach the HEAD
Signature · 1 line
1.5.0.rc2.g6e2c
PATCH 2/3 create_symref(): do not assume pathname from git_path() persists long enough
In reply to Nicolas PitreBeing lazy to rely on the cycling N buffers mkpath() and friends return is nice in general, but it makes it too easy to introduce new bugs that are "mysterious".
Introduction of read_ref() in create_symref() after calling git_path() to get the git_HEAD value (i.e. the path to create a new symref at) consumed more than the available buffers and broke a later call to mkpath() that derives lockpath from it.
---
* t7201-co.sh caught three failures. The problem this patch is fixing overwrote and gave ".lock" in lockpath, then open() on that path failed and gave fd = -1 (because I scratched my head and re-run the test without cleaning, and O_EXCL triggered), which threw write_in_full() into infinite loop, which will be fixed with the next patch.
refs.c | 20 +++++++++++--------- 1 files changed, 11 insertions(+), 9 deletions(-)
refs.c +11 -9
@@ -992,7 +992,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,const char *lockpath;char ref[1000 ];int fd, len, written; -const char *git_HEAD =git_path ("%s" , ref_target); +char *git_HEAD =xstrdup (git_path ("%s" , ref_target));unsigned char old_sha1[20 ], new_sha1[20 ];if (logmsg &&read_ref (ref_target, old_sha1))
@@ -1010,36 +1010,38 @@ int create_symref(const char *ref_target, const char *refs_heads_master, len =snprintf (ref,sizeof (ref),"ref: %s\n" , refs_heads_master);if (sizeof (ref) <= len) {error ("refname too long: %s" , refs_heads_master); -return -1 ; + goto error_free_return; } lockpath =mkpath ("%s.lock" , git_HEAD); fd =open (lockpath,O_CREAT |O_EXCL |O_WRONLY ,0666 );if (fd <0 ) {error ("Unable to open %s for writing" , lockpath); -return -5 ; + goto error_free_return; } written =write_in_full (fd, ref, len);close (fd);if (written != len) { -unlink (lockpath);error ("Unable to write to %s" , lockpath); -return -2 ; + goto error_unlink_return; }if (rename (lockpath, git_HEAD) <0 ) { -unlink (lockpath);error ("Unable to create %s" , git_HEAD); -return -3 ; + goto error_unlink_return; }if (adjust_shared_perm (git_HEAD)) { -unlink (lockpath);error ("Unable to fix permissions on %s" , lockpath); -return -4 ; + error_unlink_return: +unlink (lockpath); + error_free_return: +free (git_HEAD); +return -1 ; } done:if (logmsg && !read_ref (refs_heads_master, new_sha1))log_ref_write (ref_target, old_sha1, new_sha1, logmsg); +free (git_HEAD);return 0 ; }
Signature · 1 line
1.5.0.rc2.g8ac6-dirty