worktree lock, move, remove and unlock
cover worktree lock, move, remove and unlock
This is basically a resend from last time, which happened during rc time. It adds 4 more commands, basically cleaning up the "TODO" list in git-worktree.txt.
So far I've only actually used move and remove (and maybe unlock once because worktree-add failed on me and I had to unlock it manually). And I don't get to move worktrees a lot either so not really extensive testing.
[01/25] usage.c: move format processing out of die_errno() [02/25] usage.c: add sys_error() that prints strerror() automatically [03/25] copy.c: import copy_file() from busybox
[04/25] copy.c: delete unused code in copy_file()
[05/25] copy.c: convert bb_(p)error_msg to (sys_)error [06/25] copy.c: style fix
[07/25] copy.c: convert copy_file() to copy_dir_recursively() [08/25] completion: support git-worktree
[09/25] git-worktree.txt: keep subcommand listing in alphabetical order [10/25] path.c: add git_common_path() and strbuf_git_common_path() [11/25] worktree.c: use is_dot_or_dotdot()
[12/25] worktree.c: store "id" instead of "git_dir"
[13/25] worktree.c: add clear_worktree()
[14/25] worktree.c: add find_worktree_by_path()
[15/25] worktree.c: add is_main_worktree()
[16/25] worktree.c: add validate_worktree()
[17/25] worktree.c: add update_worktree_location()
[18/25] worktree.c: add is_worktree_locked()
[19/25] worktree: avoid 0{40}, too many zeroes, hard to read [20/25] worktree: simplify prefixing paths
[21/25] worktree: add "lock" command
[22/25] worktree: add "unlock" command
[23/25] worktree: add "move" commmand
[24/25] worktree move: accept destination as directory [25/25] worktree: add "remove" command
Total 11 files changed, 1028 insertions(+), 48 deletions(-)
PATCH 1/25 usage.c: move format processing out of die_errno()
--- usage.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
usage.c +15 -10
@@ -109,19 +109,12 @@ void NORETURN die(const char *err, ...)va_end (params); } -void NORETURN die_errno(const char *fmt, ...) +static const char *fmt_with_err (const char *fmt) { -va_list params; -char fmt_with_err[1024 ]; +static char fmt_with_err[1024 ];char str_error[256 ], *err;int i, j; -if (die_is_recursing ()) { -fputs ("fatal: recursion detected in die_errno handler\n" , - stderr); -exit (128 ); - } - err =strerror (errno);for (i = j =0 ; err[i] && j <sizeof (str_error) -1 ; ) {if ((str_error[j++] = err[i++]) !='%' )
@@ -137,9 +130,21 @@ void NORETURN die_errno(const char *fmt, ...) } str_error[j] =0 ;snprintf (fmt_with_err,sizeof (fmt_with_err),"%s: %s" , fmt, str_error); +return fmt_with_err; +} + +void NORETURN die_errno(const char *fmt, ...) +{ +va_list params; + +if (die_is_recursing ()) { +fputs ("fatal: recursion detected in die_errno handler\n" , + stderr); +exit (128 ); + }va_start (params, fmt); -die_routine (fmt_with_err, params); +die_routine (fmt_with_err (fmt), params);va_end (params); }
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 2/25 usage.c: add sys_error() that prints strerror() automatically
In reply to Nguyễn Thái Ngọc Duy--- git-compat-util.h | 1 + usage.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
git-compat-util.h +1 -0
@@ -412,6 +412,7 @@ extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); extern NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));extern int error (const char *err, ...) __attribute__((format (printf,1 ,2 ))); +extern int sys_error (const char *err, ...) __attribute__((format (printf,1 ,2 )));extern void warning (const char *err, ...) __attribute__((format (printf,1 ,2 )));#ifndef NO_OPENSSL
usage.c +10 -0
@@ -148,6 +148,16 @@ void NORETURN die_errno(const char *fmt, ...)va_end (params); } +int sys_error (const char *fmt, ...) +{ +va_list params; + +va_start (params, fmt); +error_routine (fmt_with_err (fmt), params); +va_end (params); +return -1 ; +} +#undef errorint error (const char *err, ...) {
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 3/25 copy.c: import copy_file() from busybox
In reply to Nguyễn Thái Ngọc DuyThis is busybox's unmodified copy_file() in libbb/copy_file.c from the GPL2+ commit f2c043acfcf9dad9fd3d65821b81f89986bbe54e (busybox: fix uninitialized memory when displaying IPv6 addresses - 2016-01-18)
--- copy.c | 331 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 331 insertions(+)
copy.c +331 -0
@@ -65,3 +65,334 @@ int copy_file_with_time(const char *dst, const char *src, int mode)return copy_times (dst, src);return status; } + +#if 0 +/* Return: +* -1 error, copy not made +* 0 copy is made or user answered "no" in interactive mode +* (failures to preserve mode/owner/times are not reported in exit code) +*/ +int FAST_FUNC copy_file(const char *source, const char *dest, int flags) +{ +/* This is a recursive function, try to minimize stack usage */ +/* NB: each struct stat is ~100 bytes */ +struct stat source_stat; +struct stat dest_stat; +smallint retval =0 ; +smallint dest_exists =0 ; +smallint ovr; + +/* Inverse of cp -d ("cp without -d") */ +#define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0)) + +if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) <0 ) { +/* This may be a dangling symlink. +* Making [sym]links to dangling symlinks works, so... */ +if (flags & (FILEUTILS_MAKE_SOFTLINK |FILEUTILS_MAKE_HARDLINK )) + goto make_links; +bb_perror_msg ("can't stat '%s'" , source); +return -1 ; + } + +if (lstat (dest, &dest_stat) <0 ) { +if (errno !=ENOENT ) { +bb_perror_msg ("can't stat '%s'" , dest); +return -1 ; + } + }else { +if (source_stat.st_dev == dest_stat.st_dev + && source_stat.st_ino == dest_stat.st_ino + ) { +bb_error_msg ("'%s' and '%s' are the same file" , source, dest); +return -1 ; + } + dest_exists =1 ; + } + +#if ENABLE_SELINUX +if ((flags &FILEUTILS_PRESERVE_SECURITY_CONTEXT ) &&is_selinux_enabled () >0 ) { +security_context_t con; +if (lgetfilecon (source, &con) >=0 ) { +if (setfscreatecon (con) <0 ) { +bb_perror_msg ("can't set setfscreatecon %s" , con); +freecon (con); +return -1 ; + } + }else if (errno ==ENOTSUP || errno ==ENODATA ) { +setfscreatecon_or_die (NULL ); + }else { +bb_perror_msg ("can't lgetfilecon %s" , source); +return -1 ; + } + } +#endif + +if (S_ISDIR (source_stat.st_mode)) { +DIR *dp; +const char *tp; +struct dirent *d; +mode_t saved_umask =0 ; + +if (!(flags &FILEUTILS_RECUR )) { +bb_error_msg ("omitting directory '%s'" , source); +return -1 ; + } + +/* Did we ever create source ourself before? */ + tp =is_in_ino_dev_hashtable (&source_stat); +if (tp) { +/* We did! it's a recursion! man the lifeboats... */ +bb_error_msg ("recursion detected, omitting directory '%s'" , + source); +return -1 ; + } + +if (dest_exists) { +if (!S_ISDIR (dest_stat.st_mode)) { +bb_error_msg ("target '%s' is not a directory" , dest); +return -1 ; + } +/* race here: user can substitute a symlink between +* this check and actual creation of files inside dest */ + }else { +/* Create DEST */ +mode_t mode; + saved_umask =umask (0 ); + + mode = source_stat.st_mode; +if (!(flags &FILEUTILS_PRESERVE_STATUS )) + mode = source_stat.st_mode & ~saved_umask; +/* Allow owner to access new dir (at least for now) */ + mode |=S_IRWXU ; +if (mkdir (dest, mode) <0 ) { +umask (saved_umask); +bb_perror_msg ("can't create directory '%s'" , dest); +return -1 ; + } +umask (saved_umask); +/* need stat info for add_to_ino_dev_hashtable */ +if (lstat (dest, &dest_stat) <0 ) { +bb_perror_msg ("can't stat '%s'" , dest); +return -1 ; + } + } +/* remember (dev,inode) of each created dir. +* NULL: name is not remembered */ +add_to_ino_dev_hashtable (&dest_stat,NULL ); + +/* Recursively copy files in SOURCE */ + dp =opendir (source); +if (dp ==NULL ) { + retval =-1 ; + goto preserve_mode_ugid_time; + } + +while ((d =readdir (dp)) !=NULL ) { +char *new_source, *new_dest; + + new_source =concat_subpath_file (source, d->d_name); +if (new_source ==NULL ) +continue ; + new_dest =concat_path_file (dest, d->d_name); +if (copy_file (new_source, new_dest, flags & ~FILEUTILS_DEREFERENCE_L0 ) <0 ) + retval =-1 ; +free (new_source); +free (new_dest); + } +closedir (dp); + +if (!dest_exists + &&chmod (dest, source_stat.st_mode & ~saved_umask) <0 + ) { +bb_perror_msg ("can't preserve %s of '%s'" ,"permissions" , dest); +/* retval = -1; - WRONG! copy *WAS* made */ + } + goto preserve_mode_ugid_time; + } + +if (flags & (FILEUTILS_MAKE_SOFTLINK |FILEUTILS_MAKE_HARDLINK )) { +int (*lf)(const char *oldpath,const char *newpath); + make_links: +/* Hmm... maybe +* if (DEREF && MAKE_SOFTLINK) source = realpath(source) ? +* (but realpath returns NULL on dangling symlinks...) */ + lf = (flags &FILEUTILS_MAKE_SOFTLINK ) ? symlink : link; +if (lf (source, dest) <0 ) { + ovr =ask_and_unlink (dest, flags); +if (ovr <=0 ) +return ovr; +if (lf (source, dest) <0 ) { +bb_perror_msg ("can't create link '%s'" , dest); +return -1 ; + } + } +/* _Not_ jumping to preserve_mode_ugid_time: +* (sym)links don't have those */ +return 0 ; + } + +if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */ + !(flags &FILEUTILS_RECUR ) +/* "cp [-opts] regular_file thing2" */ + ||S_ISREG (source_stat.st_mode) +/* DEREF uses stat, which never returns S_ISLNK() == true. +* So the below is never true: */ +/* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */ + ) { +int src_fd; +int dst_fd; +mode_t new_mode; + +if (!FLAGS_DEREF &&S_ISLNK (source_stat.st_mode)) { +/* "cp -d symlink dst": create a link */ + goto dont_cat; + } + +if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF ) { +const char *link_target; + link_target =is_in_ino_dev_hashtable (&source_stat); +if (link_target) { +if (link (link_target, dest) <0 ) { + ovr =ask_and_unlink (dest, flags); +if (ovr <=0 ) +return ovr; +if (link (link_target, dest) <0 ) { +bb_perror_msg ("can't create link '%s'" , dest); +return -1 ; + } + } +return 0 ; + } +add_to_ino_dev_hashtable (&source_stat, dest); + } + + src_fd =open_or_warn (source,O_RDONLY ); +if (src_fd <0 ) +return -1 ; + +/* Do not try to open with weird mode fields */ + new_mode = source_stat.st_mode; +if (!S_ISREG (source_stat.st_mode)) + new_mode =0666 ; + +// POSIX way is a security problem versus (sym)link attacks +if (!ENABLE_FEATURE_NON_POSIX_CP ) { + dst_fd =open (dest,O_WRONLY |O_CREAT |O_TRUNC , new_mode); + }else {/* safe way: */ + dst_fd =open (dest,O_WRONLY |O_CREAT |O_EXCL , new_mode); + } +if (dst_fd ==-1 ) { + ovr =ask_and_unlink (dest, flags); +if (ovr <=0 ) { +close (src_fd); +return ovr; + } +/* It shouldn't exist. If it exists, do not open (symlink attack?) */ + dst_fd =open3_or_warn (dest,O_WRONLY |O_CREAT |O_EXCL , new_mode); +if (dst_fd <0 ) { +close (src_fd); +return -1 ; + } + } + +#if ENABLE_SELINUX +if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT |FILEUTILS_SET_SECURITY_CONTEXT )) + &&is_selinux_enabled () >0 + ) { +security_context_t con; +if (getfscreatecon (&con) ==-1 ) { +bb_perror_msg ("getfscreatecon" ); +return -1 ; + } +if (con) { +if (setfilecon (dest, con) ==-1 ) { +bb_perror_msg ("setfilecon:%s,%s" , dest, con); +freecon (con); +return -1 ; + } +freecon (con); + } + } +#endif +if (bb_copyfd_eof (src_fd, dst_fd) ==-1 ) + retval =-1 ; +/* Careful with writing... */ +if (close (dst_fd) <0 ) { +bb_perror_msg ("error writing to '%s'" , dest); + retval =-1 ; + } +/* ...but read size is already checked by bb_copyfd_eof */ +close (src_fd); +/* "cp /dev/something new_file" should not +* copy mode of /dev/something */ +if (!S_ISREG (source_stat.st_mode)) +return retval; + goto preserve_mode_ugid_time; + } + dont_cat: + +/* Source is a symlink or a special file */ +/* We are lazy here, a bit lax with races... */ +if (dest_exists) { + errno =EEXIST ; + ovr =ask_and_unlink (dest, flags); +if (ovr <=0 ) +return ovr; + } +if (S_ISLNK (source_stat.st_mode)) { +char *lpath =xmalloc_readlink_or_warn (source); +if (lpath) { +int r =symlink (lpath, dest); +free (lpath); +if (r <0 ) { +bb_perror_msg ("can't create symlink '%s'" , dest); +return -1 ; + } +if (flags &FILEUTILS_PRESERVE_STATUS ) +if (lchown (dest, source_stat.st_uid, source_stat.st_gid) <0 ) +bb_perror_msg ("can't preserve %s of '%s'" ,"ownership" , dest); + } +/* _Not_ jumping to preserve_mode_ugid_time: +* symlinks don't have those */ +return 0 ; + } +if (S_ISBLK (source_stat.st_mode) ||S_ISCHR (source_stat.st_mode) + ||S_ISSOCK (source_stat.st_mode) ||S_ISFIFO (source_stat.st_mode) + ) { +if (mknod (dest, source_stat.st_mode, source_stat.st_rdev) <0 ) { +bb_perror_msg ("can't create '%s'" , dest); +return -1 ; + } + }else { +bb_error_msg ("unrecognized file '%s' with mode %x" , source, source_stat.st_mode); +return -1 ; + } + + preserve_mode_ugid_time: + +if (flags &FILEUTILS_PRESERVE_STATUS +/* Cannot happen: */ +/* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ + ) { +struct timeval times[2 ]; + + times[1 ].tv_sec = times[0 ].tv_sec = source_stat.st_mtime; + times[1 ].tv_usec = times[0 ].tv_usec =0 ; +/* BTW, utimes sets usec-precision time - just FYI */ +if (utimes (dest, times) <0 ) +bb_perror_msg ("can't preserve %s of '%s'" ,"times" , dest); +if (chown (dest, source_stat.st_uid, source_stat.st_gid) <0 ) { + source_stat.st_mode &= ~(S_ISUID |S_ISGID ); +bb_perror_msg ("can't preserve %s of '%s'" ,"ownership" , dest); + } +if (chmod (dest, source_stat.st_mode) <0 ) +bb_perror_msg ("can't preserve %s of '%s'" ,"permissions" , dest); + } + +if (flags &FILEUTILS_VERBOSE ) { +printf ("'%s' -> '%s'\n" , source, dest); + } + +return retval; +} +#endif
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 4/25 copy.c: delete unused code in copy_file()
In reply to Nguyễn Thái Ngọc Duy - selinux preservation code
- make-link code
- delete link dereference code
- non-recursive copy code
- stat no preservation code
- verbose printing code
--- copy.c | 101 +++++------------------------------------------------------------ 1 file changed, 7 insertions(+), 94 deletions(-)
copy.c +7 -94
@@ -82,14 +82,7 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)smallint dest_exists =0 ;smallint ovr; -/* Inverse of cp -d ("cp without -d") */ -#define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0)) - -if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) <0 ) { -/* This may be a dangling symlink. -* Making [sym]links to dangling symlinks works, so... */ -if (flags & (FILEUTILS_MAKE_SOFTLINK |FILEUTILS_MAKE_HARDLINK )) - goto make_links; +if (lstat (source, &source_stat) <0 ) {bb_perror_msg ("can't stat '%s'" , source);return -1 ; }
@@ -109,35 +102,12 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) dest_exists =1 ; } -#if ENABLE_SELINUX -if ((flags &FILEUTILS_PRESERVE_SECURITY_CONTEXT ) &&is_selinux_enabled () >0 ) { -security_context_t con; -if (lgetfilecon (source, &con) >=0 ) { -if (setfscreatecon (con) <0 ) { -bb_perror_msg ("can't set setfscreatecon %s" , con); -freecon (con); -return -1 ; - } - }else if (errno ==ENOTSUP || errno ==ENODATA ) { -setfscreatecon_or_die (NULL ); - }else { -bb_perror_msg ("can't lgetfilecon %s" , source); -return -1 ; - } - } -#endif -if (S_ISDIR (source_stat.st_mode)) {DIR *dp;const char *tp;struct dirent *d;mode_t saved_umask =0 ; -if (!(flags &FILEUTILS_RECUR )) { -bb_error_msg ("omitting directory '%s'" , source); -return -1 ; - } -/* Did we ever create source ourself before? */ tp =is_in_ino_dev_hashtable (&source_stat);if (tp) {
@@ -160,8 +130,6 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) saved_umask =umask (0 ); mode = source_stat.st_mode; -if (!(flags &FILEUTILS_PRESERVE_STATUS )) - mode = source_stat.st_mode & ~saved_umask;/* Allow owner to access new dir (at least for now) */ mode |=S_IRWXU ;if (mkdir (dest, mode) <0 ) {
@@ -210,45 +178,17 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) goto preserve_mode_ugid_time; } -if (flags & (FILEUTILS_MAKE_SOFTLINK |FILEUTILS_MAKE_HARDLINK )) { -int (*lf)(const char *oldpath,const char *newpath); - make_links: -/* Hmm... maybe -* if (DEREF && MAKE_SOFTLINK) source = realpath(source) ? -* (but realpath returns NULL on dangling symlinks...) */ - lf = (flags &FILEUTILS_MAKE_SOFTLINK ) ? symlink : link; -if (lf (source, dest) <0 ) { - ovr =ask_and_unlink (dest, flags); -if (ovr <=0 ) -return ovr; -if (lf (source, dest) <0 ) { -bb_perror_msg ("can't create link '%s'" , dest); -return -1 ; - } - } -/* _Not_ jumping to preserve_mode_ugid_time: -* (sym)links don't have those */ -return 0 ; - } - -if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */ - !(flags &FILEUTILS_RECUR ) -/* "cp [-opts] regular_file thing2" */ - ||S_ISREG (source_stat.st_mode) -/* DEREF uses stat, which never returns S_ISLNK() == true. -* So the below is never true: */ -/* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */ - ) { +if (S_ISREG (source_stat.st_mode) ) {/* "cp [-opts] regular_file thing2" */ int src_fd;int dst_fd;mode_t new_mode; -if (!FLAGS_DEREF &&S_ISLNK (source_stat.st_mode)) { +if (S_ISLNK (source_stat.st_mode)) {/* "cp -d symlink dst": create a link */ goto dont_cat; } -if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF ) { +if (ENABLE_FEATURE_PRESERVE_HARDLINKS ) {const char *link_target; link_target =is_in_ino_dev_hashtable (&source_stat);if (link_target) {
@@ -295,25 +235,6 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) } } -#if ENABLE_SELINUX -if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT |FILEUTILS_SET_SECURITY_CONTEXT )) - &&is_selinux_enabled () >0 - ) { -security_context_t con; -if (getfscreatecon (&con) ==-1 ) { -bb_perror_msg ("getfscreatecon" ); -return -1 ; - } -if (con) { -if (setfilecon (dest, con) ==-1 ) { -bb_perror_msg ("setfilecon:%s,%s" , dest, con); -freecon (con); -return -1 ; - } -freecon (con); - } - } -#endif if (bb_copyfd_eof (src_fd, dst_fd) ==-1 ) retval =-1 ;/* Careful with writing... */
@@ -348,9 +269,8 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)bb_perror_msg ("can't create symlink '%s'" , dest);return -1 ; } -if (flags &FILEUTILS_PRESERVE_STATUS ) -if (lchown (dest, source_stat.st_uid, source_stat.st_gid) <0 ) -bb_perror_msg ("can't preserve %s of '%s'" ,"ownership" , dest); +if (lchown (dest, source_stat.st_uid, source_stat.st_gid) <0 ) +bb_perror_msg ("can't preserve %s of '%s'" ,"ownership" , dest); }/* _Not_ jumping to preserve_mode_ugid_time: * symlinks don't have those */
@@ -370,10 +290,7 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) preserve_mode_ugid_time: -if (flags &FILEUTILS_PRESERVE_STATUS -/* Cannot happen: */ -/* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ - ) { +if (1 /*FILEUTILS_PRESERVE_STATUS*/ ) {struct timeval times[2 ]; times[1 ].tv_sec = times[0 ].tv_sec = source_stat.st_mtime;
@@ -389,10 +306,6 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) bb_perror_msg("can't preserve %s of '%s'", "permissions", dest); } -if (flags &FILEUTILS_VERBOSE ) { -printf ("'%s' -> '%s'\n" , source, dest); - } -return retval; }#endif
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 5/25 copy.c: convert bb_(p)error_msg to (sys_)error
In reply to Nguyễn Thái Ngọc Duy--- copy.c | 85 ++++++++++++++++++++++++------------------------------------------ 1 file changed, 31 insertions(+), 54 deletions(-)
copy.c +31 -54
@@ -82,23 +82,16 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)smallint dest_exists =0 ;smallint ovr; -if (lstat (source, &source_stat) <0 ) { -bb_perror_msg ("can't stat '%s'" , source); -return -1 ; - } +if (lstat (source, &source_stat) <0 ) +return sys_error (_ ("can't stat '%s'" ), source);if (lstat (dest, &dest_stat) <0 ) { -if (errno !=ENOENT ) { -bb_perror_msg ("can't stat '%s'" , dest); -return -1 ; - } +if (errno !=ENOENT ) +return sys_error (_ ("can't stat '%s'" ), dest); }else { -if (source_stat.st_dev == dest_stat.st_dev - && source_stat.st_ino == dest_stat.st_ino - ) { -bb_error_msg ("'%s' and '%s' are the same file" , source, dest); -return -1 ; - } +if (source_stat.st_dev == dest_stat.st_dev && + source_stat.st_ino == dest_stat.st_ino) +return error (_ ("'%s' and '%s' are the same file" ), source, dest); dest_exists =1 ; }
@@ -110,18 +103,14 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)/* Did we ever create source ourself before? */ tp =is_in_ino_dev_hashtable (&source_stat); -if (tp) { +if (tp)/* We did! it's a recursion! man the lifeboats... */ -bb_error_msg ("recursion detected, omitting directory '%s'" , - source); -return -1 ; - } +return error (_ ("recursion detected, omitting directory '%s'" ), + source);if (dest_exists) { -if (!S_ISDIR (dest_stat.st_mode)) { -bb_error_msg ("target '%s' is not a directory" , dest); -return -1 ; - } +if (!S_ISDIR (dest_stat.st_mode)) +return error (_ ("target '%s' is not a directory" ), dest);/* race here: user can substitute a symlink between * this check and actual creation of files inside dest */ }else {
@@ -134,15 +123,12 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) mode |=S_IRWXU ;if (mkdir (dest, mode) <0 ) {umask (saved_umask); -bb_perror_msg ("can't create directory '%s'" , dest); -return -1 ; +return sys_error (_ ("can't create directory '%s'" ), dest); }umask (saved_umask);/* need stat info for add_to_ino_dev_hashtable */ -if (lstat (dest, &dest_stat) <0 ) { -bb_perror_msg ("can't stat '%s'" , dest); -return -1 ; - } +if (lstat (dest, &dest_stat) <0 ) +return sys_error (_ ("can't stat '%s'" ), dest); }/* remember (dev,inode) of each created dir. * NULL: name is not remembered */
@@ -172,7 +158,7 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (!dest_exists &&chmod (dest, source_stat.st_mode & ~saved_umask) <0 ) { -bb_perror_msg ("can't preserve %s of '%s'" ,"permissions" , dest); +sys_error (_ ("can't preserve permissions of '%s'" ), dest);/* retval = -1; - WRONG! copy *WAS* made */ } goto preserve_mode_ugid_time;
@@ -196,10 +182,8 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) ovr =ask_and_unlink (dest, flags);if (ovr <=0 )return ovr; -if (link (link_target, dest) <0 ) { -bb_perror_msg ("can't create link '%s'" , dest); -return -1 ; - } +if (link (link_target, dest) <0 ) +return sys_error (_ ("can't create link '%s'" ), dest); }return 0 ; }
@@ -238,10 +222,8 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (bb_copyfd_eof (src_fd, dst_fd) ==-1 ) retval =-1 ;/* Careful with writing... */ -if (close (dst_fd) <0 ) { -bb_perror_msg ("error writing to '%s'" , dest); - retval =-1 ; - } +if (close (dst_fd) <0 ) + retval =sys_error (_ ("error writing to '%s'" ), dest);/* ...but read size is already checked by bb_copyfd_eof */ close (src_fd);/* "cp /dev/something new_file" should not
@@ -265,12 +247,10 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (lpath) {int r =symlink (lpath, dest);free (lpath); -if (r <0 ) { -bb_perror_msg ("can't create symlink '%s'" , dest); -return -1 ; - } +if (r <0 ) +return sys_error (_ ("can't create symlink '%s'" ), dest);if (lchown (dest, source_stat.st_uid, source_stat.st_gid) <0 ) -bb_perror_msg ("can't preserve %s of '%s'" ,"ownership" , dest); +sys_error (_ ("can't preserve %s of '%s'" ),"ownership" , dest); }/* _Not_ jumping to preserve_mode_ugid_time: * symlinks don't have those */
@@ -279,14 +259,11 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (S_ISBLK (source_stat.st_mode) ||S_ISCHR (source_stat.st_mode) ||S_ISSOCK (source_stat.st_mode) ||S_ISFIFO (source_stat.st_mode) ) { -if (mknod (dest, source_stat.st_mode, source_stat.st_rdev) <0 ) { -bb_perror_msg ("can't create '%s'" , dest); -return -1 ; - } - }else { -bb_error_msg ("unrecognized file '%s' with mode %x" , source, source_stat.st_mode); -return -1 ; - } +if (mknod (dest, source_stat.st_mode, source_stat.st_rdev) <0 ) +return sys_error (_ ("can't create '%s'" ), dest); + }else +return error (_ ("unrecognized file '%s' with mode %x" ), + source, source_stat.st_mode); preserve_mode_ugid_time:
@@ -297,13 +274,13 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) times[1 ].tv_usec = times[0 ].tv_usec =0 ;/* BTW, utimes sets usec-precision time - just FYI */ if (utimes (dest, times) <0 ) -bb_perror_msg ("can't preserve %s of '%s'" ,"times" , dest); +sys_error (_ ("can't preserve %s of '%s'" ),"times" , dest);if (chown (dest, source_stat.st_uid, source_stat.st_gid) <0 ) { source_stat.st_mode &= ~(S_ISUID |S_ISGID ); -bb_perror_msg ("can't preserve %s of '%s'" ,"ownership" , dest); +sys_error (_ ("can't preserve %s of '%s'" ),"ownership" , dest); }if (chmod (dest, source_stat.st_mode) <0 ) -bb_perror_msg ("can't preserve %s of '%s'" ,"permissions" , dest); +sys_error (_ ("can't preserve %s of '%s'" ),"permissions" , dest); }return retval;
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 6/25 copy.c: style fix
In reply to Nguyễn Thái Ngọc Duy--- copy.c | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-)
copy.c +29 -21
@@ -111,8 +111,10 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (dest_exists) {if (!S_ISDIR (dest_stat.st_mode))return error (_ ("target '%s' is not a directory" ), dest); -/* race here: user can substitute a symlink between -* this check and actual creation of files inside dest */ +/* +* race here: user can substitute a symlink between +* this check and actual creation of files inside dest +*/ }else {/* Create DEST */ mode_t mode;
@@ -130,22 +132,24 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (lstat (dest, &dest_stat) <0 )return sys_error (_ ("can't stat '%s'" ), dest); } -/* remember (dev,inode) of each created dir. -* NULL: name is not remembered */ +/* +* remember (dev,inode) of each created dir. name is +* not remembered +*/ add_to_ino_dev_hashtable (&dest_stat,NULL );/* Recursively copy files in SOURCE */ dp =opendir (source); -if (dp ==NULL ) { +if (!dp) { retval =-1 ; goto preserve_mode_ugid_time; } -while ((d =readdir (dp)) !=NULL ) { +while ((d =readdir (dp))) {char *new_source, *new_dest; new_source =concat_subpath_file (source, d->d_name); -if (new_source ==NULL ) +if (!new_source)continue ; new_dest =concat_path_file (dest, d->d_name);if (copy_file (new_source, new_dest, flags & ~FILEUTILS_DEREFERENCE_L0 ) <0 )
@@ -155,16 +159,15 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) }closedir (dp); -if (!dest_exists - &&chmod (dest, source_stat.st_mode & ~saved_umask) <0 - ) { +if (!dest_exists && +chmod (dest, source_stat.st_mode & ~saved_umask) <0 ) {sys_error (_ ("can't preserve permissions of '%s'" ), dest);/* retval = -1; - WRONG! copy *WAS* made */ } goto preserve_mode_ugid_time; } -if (S_ISREG (source_stat.st_mode) ) {/* "cp [-opts] regular_file thing2" */ +if (S_ISREG (source_stat.st_mode)) {/* "cp [-opts] regular_file thing2" */ int src_fd;int dst_fd;mode_t new_mode;
@@ -199,7 +202,7 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (!S_ISREG (source_stat.st_mode)) new_mode =0666 ; -// POSIX way is a security problem versus (sym)link attacks +/* POSIX way is a security problem versus (sym)link attacks */ if (!ENABLE_FEATURE_NON_POSIX_CP ) { dst_fd =open (dest,O_WRONLY |O_CREAT |O_TRUNC , new_mode); }else {/* safe way: */
@@ -226,13 +229,15 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) retval =sys_error (_ ("error writing to '%s'" ), dest);/* ...but read size is already checked by bb_copyfd_eof */ close (src_fd); -/* "cp /dev/something new_file" should not -* copy mode of /dev/something */ +/* +* "cp /dev/something new_file" should not +* copy mode of /dev/something +*/ if (!S_ISREG (source_stat.st_mode))return retval; goto preserve_mode_ugid_time; } - dont_cat: +dont_cat:/* Source is a symlink or a special file */ /* We are lazy here, a bit lax with races... */
@@ -252,20 +257,23 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)if (lchown (dest, source_stat.st_uid, source_stat.st_gid) <0 )sys_error (_ ("can't preserve %s of '%s'" ),"ownership" , dest); } -/* _Not_ jumping to preserve_mode_ugid_time: -* symlinks don't have those */ +/* +* _Not_ jumping to preserve_mode_ugid_time: symlinks +* don't have those +*/ return 0 ; } -if (S_ISBLK (source_stat.st_mode) ||S_ISCHR (source_stat.st_mode) - ||S_ISSOCK (source_stat.st_mode) ||S_ISFIFO (source_stat.st_mode) - ) { +if (S_ISBLK (source_stat.st_mode) || +S_ISCHR (source_stat.st_mode) || +S_ISSOCK (source_stat.st_mode) || +S_ISFIFO (source_stat.st_mode)) {if (mknod (dest, source_stat.st_mode, source_stat.st_rdev) <0 )return sys_error (_ ("can't create '%s'" ), dest); }else return error (_ ("unrecognized file '%s' with mode %x" ), source, source_stat.st_mode); - preserve_mode_ugid_time: +preserve_mode_ugid_time:if (1 /*FILEUTILS_PRESERVE_STATUS*/ ) {struct timeval times[2 ];
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 7/25 copy.c: convert copy_file() to copy_dir_recursively()
In reply to Nguyễn Thái Ngọc DuyThis finally enables busybox's copy_file() code under a new name (because "copy_file" is already taken in Git code base). Because this comes from busybox, POSIXy (or even Linuxy) behavior is expected. More changes may be needed for Windows support.
--- cache.h | 1 + copy.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 179 insertions(+), 38 deletions(-)
cache.h +1 -0
@@ -1677,6 +1677,7 @@ extern void fprintf_or_die(FILE *, const char *fmt, ...);extern int copy_fd (int ifd,int ofd);extern int copy_file (const char *dst,const char *src,int mode);extern int copy_file_with_time (const char *dst,const char *src,int mode); +extern int copy_dir_recursively (const char *source,const char *dest);extern void write_or_die (int fd,const void *buf,size_t count);extern int write_or_whine (int fd,const void *buf,size_t count,const char *msg);
copy.c +178 -38
304 lines not on this page · raw message
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 8/25 completion: support git-worktree
In reply to Nguyễn Thái Ngọc Duy--- contrib/completion/git-completion.bash | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
contrib/completion/git-completion.bash +23 -0
@@ -2594,6 +2594,29 @@ _git_whatchanged () _git_log } +_git_worktree () +{ + local subcommands="add list prune" + local subcommand="$(__git_find_on_cmdline "$subcommands")" + if [ -z "$subcommand" ]; then + __gitcomp "$subcommands" + else + case "$subcommand,$cur" in + add,--*) + __gitcomp "--detach --force" + ;; + list,--*) + __gitcomp "--porcelain" + ;; + prune,--*) + __gitcomp "--dry-run --expire --verbose" + ;; + *) + ;; + esac + fi +} + __git_main () { local i c=1 command __git_dir
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 9/25 git-worktree.txt: keep subcommand listing in alphabetical order
In reply to Nguyễn Thái Ngọc Duy--- Documentation/git-worktree.txt | 10 +++++----- builtin/worktree.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-)
Documentation/git-worktree.txt +5 -5
@@ -10,8 +10,8 @@ SYNOPSIS -------- [verse] 'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>] -'git worktree prune' [-n] [-v] [--expire <expire>] 'git worktree list' [--porcelain] +'git worktree prune' [-n] [-v] [--expire <expire>] DESCRIPTION -----------
@@ -54,10 +54,6 @@ If `<branch>` is omitted and neither `-b` nor `-B` nor `--detached` used, then, as a convenience, a new branch based at HEAD is created automatically, as if `-b $(basename <path>)` was specified. -prune:: - -Prune working tree information in $GIT_DIR/worktrees. - list:: List details of each worktree. The main worktree is listed first, followed by
@@ -65,6 +61,10 @@ each of the linked worktrees. The output details include if the worktree is bare, the revision currently checked out, and the branch currently checked out (or 'detached HEAD' if none). +prune:: + +Prune working tree information in $GIT_DIR/worktrees. + OPTIONS -------
builtin/worktree.c +1 -1
@@ -13,8 +13,8 @@static const char *const worktree_usage[] = {N_ ("git worktree add [<options>] <path> [<branch>]" ), -N_ ("git worktree prune [<options>]" ),N_ ("git worktree list [<options>]" ), +N_ ("git worktree prune [<options>]" ),NULL };
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 10/25 path.c: add git_common_path() and strbuf_git_common_path()
In reply to Nguyễn Thái Ngọc Duy--- cache.h | 3 +++ path.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+)
cache.h +3 -0
@@ -767,11 +767,14 @@ extern int check_repository_format(void);*/ extern const char *mkpath (const char *fmt, ...) __attribute__((format (printf,1 ,2 )));extern const char *git_path (const char *fmt, ...) __attribute__((format (printf,1 ,2 ))); +extern const char *git_common_path (const char *fmt, ...) __attribute__((format (printf,1 ,2 )));extern char *mksnpath (char *buf,size_t n,const char *fmt, ...) __attribute__((format (printf,3 ,4 )));extern void strbuf_git_path (struct strbuf *sb,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +extern void strbuf_git_common_path (struct strbuf *sb,const char *fmt, ...) + __attribute__((format (printf,2 ,3 )));extern char *git_path_buf (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); extern void strbuf_git_path_submodule(struct strbuf *sb, const char *path,
path.c +29 -0
@@ -503,6 +503,35 @@ void strbuf_git_path_submodule(struct strbuf *buf, const char *path,va_end (args); } +static void do_git_common_path (struct strbuf *buf, +const char *fmt, +va_list args) +{ +strbuf_addstr (buf,get_git_common_dir ()); +if (buf->len && !is_dir_sep (buf->buf[buf->len -1 ])) +strbuf_addch (buf,'/' ); +strbuf_vaddf (buf, fmt, args); +strbuf_cleanup_path (buf); +} + +const char *git_common_path (const char *fmt, ...) +{ +struct strbuf *pathname =get_pathname (); +va_list args; +va_start (args, fmt); +do_git_common_path (pathname, fmt, args); +va_end (args); +return pathname->buf; +} + +void strbuf_git_common_path (struct strbuf *sb,const char *fmt, ...) +{ +va_list args; +va_start (args, fmt); +do_git_common_path (sb, fmt, args); +va_end (args); +} +int validate_headref (const char *path) {struct stat st;
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 11/25 worktree.c: use is_dot_or_dotdot()
In reply to Nguyễn Thái Ngọc Duy--- builtin/worktree.c | 2 +- worktree.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
builtin/worktree.c +1 -1
@@ -94,7 +94,7 @@ static void prune_worktrees(void)if (!dir)return ;while ((d =readdir (dir)) !=NULL ) { -if (!strcmp (d->d_name,"." ) || !strcmp (d->d_name,".." )) +if (is_dot_or_dotdot (d->d_name))continue ;strbuf_reset (&reason);if (!prune_worktree (d->d_name, &reason))
worktree.c +2 -1
@@ -2,6 +2,7 @@#include "refs.h" #include "strbuf.h" #include "worktree.h" +#include "dir.h" void free_worktrees (struct worktree **worktrees) {
@@ -173,7 +174,7 @@ struct worktree **get_worktrees(void)if (dir) {while ((d =readdir (dir)) !=NULL ) {struct worktree *linked =NULL ; -if (!strcmp (d->d_name,"." ) || !strcmp (d->d_name,".." )) +if (is_dot_or_dotdot (d->d_name))continue ;if ((linked =get_linked_worktree (d->d_name))) {
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 12/25 worktree.c: store "id" instead of "git_dir"
In reply to Nguyễn Thái Ngọc DuyWe can reconstruct git_dir from id quite easily. It's a bit hackier to do the reverse.
--- worktree.c | 29 ++++++++++++++++------------- worktree.h | 7 ++++++- 2 files changed, 22 insertions(+), 14 deletions(-)
worktree.c +16 -13
@@ -10,7 +10,7 @@ void free_worktrees(struct worktree **worktrees)for (i =0 ; worktrees[i]; i++) {free (worktrees[i]->path); -free (worktrees[i]->git_dir); +free (worktrees[i]->id);free (worktrees[i]->head_ref);free (worktrees[i]); }
@@ -75,13 +75,11 @@ static struct worktree *get_main_worktree(void)struct worktree *worktree =NULL ;struct strbuf path =STRBUF_INIT ;struct strbuf worktree_path =STRBUF_INIT ; -struct strbuf gitdir =STRBUF_INIT ;struct strbuf head_ref =STRBUF_INIT ;int is_bare =0 ;int is_detached =0 ; -strbuf_addf (&gitdir,"%s" ,absolute_path (get_git_common_dir ())); -strbuf_addbuf (&worktree_path, &gitdir); +strbuf_addstr (&worktree_path,absolute_path (get_git_common_dir ())); is_bare = !strbuf_strip_suffix (&worktree_path,"/.git" );if (is_bare)strbuf_strip_suffix (&worktree_path,"/." );
@@ -93,7 +91,7 @@ static struct worktree *get_main_worktree(void) worktree =xmalloc (sizeof (struct worktree )); worktree->path =strbuf_detach (&worktree_path,NULL ); - worktree->git_dir =strbuf_detach (&gitdir,NULL ); + worktree->id =NULL ; worktree->is_bare = is_bare; worktree->head_ref =NULL ; worktree->is_detached = is_detached;
@@ -101,7 +99,6 @@ static struct worktree *get_main_worktree(void) done:strbuf_release (&path); -strbuf_release (&gitdir);strbuf_release (&worktree_path);strbuf_release (&head_ref);return worktree;
@@ -112,16 +109,13 @@ static struct worktree *get_linked_worktree(const char *id)struct worktree *worktree =NULL ;struct strbuf path =STRBUF_INIT ;struct strbuf worktree_path =STRBUF_INIT ; -struct strbuf gitdir =STRBUF_INIT ;struct strbuf head_ref =STRBUF_INIT ;int is_detached =0 ;if (!id)die ("Missing linked worktree name" ); -strbuf_addf (&gitdir,"%s/worktrees/%s" , -absolute_path (get_git_common_dir ()), id); -strbuf_addf (&path,"%s/gitdir" , gitdir.buf); +strbuf_git_common_path (&path,"worktrees/%s/gitdir" , id);if (strbuf_read_file (&worktree_path, path.buf,0 ) <=0 )/* invalid gitdir file */ goto done;
@@ -141,7 +135,7 @@ static struct worktree *get_linked_worktree(const char *id) worktree =xmalloc (sizeof (struct worktree )); worktree->path =strbuf_detach (&worktree_path,NULL ); - worktree->git_dir =strbuf_detach (&gitdir,NULL ); + worktree->id =xstrdup (id); worktree->is_bare =0 ; worktree->head_ref =NULL ; worktree->is_detached = is_detached;
@@ -149,7 +143,6 @@ static struct worktree *get_linked_worktree(const char *id) done:strbuf_release (&path); -strbuf_release (&gitdir);strbuf_release (&worktree_path);strbuf_release (&head_ref);return worktree;
@@ -189,6 +182,14 @@ struct worktree **get_worktrees(void)return list; } +const char *get_worktree_git_dir (const struct worktree *wt) +{ +if (wt->id) +return git_common_path ("worktrees/%s" , wt->id); +else +return get_git_common_dir (); +} +char *find_shared_symref (const char *symref,const char *target) {char *existing =NULL ;
@@ -200,7 +201,9 @@ char *find_shared_symref(const char *symref, const char *target)for (i =0 ; worktrees[i]; i++) {strbuf_reset (&path);strbuf_reset (&sb); -strbuf_addf (&path,"%s/%s" , worktrees[i]->git_dir, symref); +strbuf_addf (&path,"%s/%s" , +get_worktree_git_dir (worktrees[i]), + symref);if (parse_ref (path.buf, &sb,NULL )) {continue ;
worktree.h +6 -1
@@ -3,7 +3,7 @@struct worktree {char *path; -char *git_dir; +char *id;char *head_ref;unsigned char head_sha1[20 ];int is_detached;
@@ -23,6 +23,11 @@ struct worktree {extern struct worktree **get_worktrees (void );/* +* Return git dir of the worktree. Note that the path may be relative. +*/ +extern const char *get_worktree_git_dir (const struct worktree *wt); + +/* * Free up the memory for worktree(s) */ extern void free_worktrees (struct worktree **);
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 13/25 worktree.c: add clear_worktree()
In reply to Nguyễn Thái Ngọc Duy--- worktree.c | 14 +++++++++++--- worktree.h | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-)
worktree.c +11 -3
@@ -4,14 +4,22 @@#include "worktree.h" #include "dir.h" +void clear_worktree (struct worktree *wt) +{ +if (!wt) +return ; +free (wt->path); +free (wt->id); +free (wt->head_ref); +memset (wt,0 ,sizeof (*wt)); +} +void free_worktrees (struct worktree **worktrees) {int i =0 ;for (i =0 ; worktrees[i]; i++) { -free (worktrees[i]->path); -free (worktrees[i]->id); -free (worktrees[i]->head_ref); +clear_worktree (worktrees[i]);free (worktrees[i]); }free (worktrees);
worktree.h +5 -0
@@ -28,6 +28,11 @@ extern struct worktree **get_worktrees(void);extern const char *get_worktree_git_dir (const struct worktree *wt);/* +* Free up the memory for worktree +*/ +extern void clear_worktree (struct worktree *); + +/* * Free up the memory for worktree(s) */ extern void free_worktrees (struct worktree **);
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 14/25 worktree.c: add find_worktree_by_path()
In reply to Nguyễn Thái Ngọc Duy--- worktree.c | 16 ++++++++++++++++ worktree.h | 6 ++++++ 2 files changed, 22 insertions(+)
worktree.c +16 -0
@@ -198,6 +198,22 @@ const char *get_worktree_git_dir(const struct worktree *wt)return get_git_common_dir (); } +struct worktree *find_worktree_by_path (struct worktree **list, +const char *path_) +{ +char *path =xstrdup (real_path (path_)); +struct worktree *wt =NULL ; + +while (*list) { + wt = *list++; +if (!strcmp_icase (path,real_path (wt->path))) +break ; + wt =NULL ; + } +free (path); +return wt; +} +char *find_shared_symref (const char *symref,const char *target) {char *existing =NULL ;
worktree.h +6 -0
@@ -28,6 +28,12 @@ extern struct worktree **get_worktrees(void);extern const char *get_worktree_git_dir (const struct worktree *wt);/* +* Search a worktree by its path. Paths are normalized internally. +*/ +extern struct worktree *find_worktree_by_path (struct worktree **list, +const char *path_); + +/* * Free up the memory for worktree */ extern void clear_worktree (struct worktree *);
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 15/25 worktree.c: add is_main_worktree()
In reply to Nguyễn Thái Ngọc Duy--- worktree.c | 5 +++++ worktree.h | 5 +++++ 2 files changed, 10 insertions(+)
worktree.c +5 -0
@@ -214,6 +214,11 @@ struct worktree *find_worktree_by_path(struct worktree **list,return wt; } +int is_main_worktree (const struct worktree *wt) +{ +return wt && !wt->id; +} +char *find_shared_symref (const char *symref,const char *target) {char *existing =NULL ;
worktree.h +5 -0
@@ -34,6 +34,11 @@ extern struct worktree *find_worktree_by_path(struct worktree **list, const char *path_);/* +* Return true if the given worktree is the main one. +*/ +extern int is_main_worktree (const struct worktree *wt); + +/* * Free up the memory for worktree */ extern void clear_worktree (struct worktree *);
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 16/25 worktree.c: add validate_worktree()
In reply to Nguyễn Thái Ngọc DuyThis function is later used by "worktree move" and "worktree remove" to ensure that we have a good connection between the repository and the worktree.
--- worktree.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ worktree.h | 5 +++++ 2 files changed, 68 insertions(+)
worktree.c +63 -0
@@ -219,6 +219,69 @@ int is_main_worktree(const struct worktree *wt)return wt && !wt->id; } +static int report (int quiet,const char *fmt, ...) +{ +va_list params; + +if (quiet) +return -1 ; + +va_start (params, fmt); +vfprintf (stderr, fmt, params); +fputc ('\n' , stderr); +va_end (params); +return -1 ; +} + +int validate_worktree (const struct worktree *wt,int quiet) +{ +struct strbuf sb =STRBUF_INIT ; +const char *path; +int err; + +if (is_main_worktree (wt)) { +/* +* Main worktree using .git file to point to the +* repository would make it impossible to know where +* the actual worktree is if this function is executed +* from another worktree. No .git file support for now. +*/ +strbuf_addf (&sb,"%s/.git" , wt->path); +if (!is_directory (sb.buf)) { +strbuf_release (&sb); +return report (quiet,_ ("'%s/.git' at main worktree is not the repository directory" ), + wt->path); + } +return 0 ; + } + +/* +* Make sure "gitdir" file points to a real .git file and that +* file points back here. +*/ +if (!is_absolute_path (wt->path)) +return report (quiet,_ ("'%s' file does not contain absolute path to the worktree location" ), +git_common_path ("worktrees/%s/gitdir" , wt->id)); + +strbuf_addf (&sb,"%s/.git" , wt->path); +if (!file_exists (sb.buf)) { +strbuf_release (&sb); +return report (quiet,_ ("'%s/.git' does not exist" ), wt->path); + } + + path =read_gitfile_gently (sb.buf, &err); +strbuf_release (&sb); +if (!path) +return report (quiet,_ ("'%s/.git' is not a .git file, error code %d" ), + wt->path, err); + +if (strcmp_icase (path,real_path (git_common_path ("worktrees/%s" , wt->id)))) +return report (quiet,_ ("'%s' does not point back to" ), + wt->path,git_common_path ("worktrees/%s" , wt->id)); + +return 0 ; +} +char *find_shared_symref (const char *symref,const char *target) {char *existing =NULL ;
worktree.h +5 -0
@@ -39,6 +39,11 @@ extern struct worktree *find_worktree_by_path(struct worktree **list,extern int is_main_worktree (const struct worktree *wt);/* +* Return zero if the worktree is in good condition. +*/ +extern int validate_worktree (const struct worktree *wt,int quiet); + +/* * Free up the memory for worktree */ extern void clear_worktree (struct worktree *);
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 17/25 worktree.c: add update_worktree_location()
In reply to Nguyễn Thái Ngọc Duy--- worktree.c | 25 +++++++++++++++++++++++++ worktree.h | 6 ++++++ 2 files changed, 31 insertions(+)
worktree.c +25 -0
@@ -282,6 +282,31 @@ int validate_worktree(const struct worktree *wt, int quiet)return 0 ; } +int update_worktree_location (struct worktree *wt,const char *path_) +{ +struct strbuf path =STRBUF_INIT ; +int ret =0 ; + +if (is_main_worktree (wt)) +return 0 ; + +strbuf_add_absolute_path (&path, path_); +if (strcmp_icase (wt->path, path.buf)) { +if (!write_file_gently (git_common_path ("worktrees/%s/gitdir" , + wt->id), +"%s/.git" ,real_path (path.buf))) { +free (wt->path); + wt->path =strbuf_detach (&path,NULL ); + ret =0 ; + }else + ret =sys_error (_ ("failed to update '%s'" ), +git_common_path ("worktrees/%s/gitdir" , + wt->id)); + } +strbuf_release (&path); +return ret; +} +char *find_shared_symref (const char *symref,const char *target) {char *existing =NULL ;
worktree.h +6 -0
@@ -44,6 +44,12 @@ extern int is_main_worktree(const struct worktree *wt);extern int validate_worktree (const struct worktree *wt,int quiet);/* +* Update worktrees/xxx/gitdir with the new path. +*/ +extern int update_worktree_location (struct worktree *wt, +const char *path_); + +/* * Free up the memory for worktree */ extern void clear_worktree (struct worktree *);
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 18/25 worktree.c: add is_worktree_locked()
In reply to Nguyễn Thái Ngọc Duy--- worktree.c | 18 ++++++++++++++++++ worktree.h | 6 ++++++ 2 files changed, 24 insertions(+)
worktree.c +18 -0
@@ -219,6 +219,24 @@ int is_main_worktree(const struct worktree *wt)return wt && !wt->id; } +const char *is_worktree_locked (const struct worktree *wt) +{ +static struct strbuf sb =STRBUF_INIT ; + +if (!file_exists (git_common_path ("worktrees/%s/locked" , wt->id))) +return NULL ; + +strbuf_reset (&sb); +if (strbuf_read_file (&sb, +git_common_path ("worktrees/%s/locked" , wt->id), +0 ) <0 ) +die_errno (_ ("failed to read '%s'" ), +git_common_path ("worktrees/%s/locked" , wt->id)); + +strbuf_rtrim (&sb); +return sb.buf; +} +static int report (int quiet,const char *fmt, ...) {va_list params;
worktree.h +6 -0
@@ -39,6 +39,12 @@ extern struct worktree *find_worktree_by_path(struct worktree **list,extern int is_main_worktree (const struct worktree *wt);/* +* Return the reason string if the given worktree is locked. Return +* NULL otherwise. +*/ +extern const char *is_worktree_locked (const struct worktree *wt); + +/* * Return zero if the worktree is in good condition. */ extern int validate_worktree (const struct worktree *wt,int quiet);
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 19/25 worktree: avoid 0{40}, too many zeroes, hard to read
In reply to Nguyễn Thái Ngọc Duy--- builtin/worktree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/worktree.c +1 -1
@@ -261,7 +261,7 @@ static int add_worktree(const char *path, const char *refname,*/ strbuf_reset (&sb);strbuf_addf (&sb,"%s/HEAD" , sb_repo.buf); -write_file (sb.buf,"0000000000000000000000000000000000000000" ); +write_file (sb.buf,sha1_to_hex (null_sha1));strbuf_reset (&sb);strbuf_addf (&sb,"%s/commondir" , sb_repo.buf);write_file (sb.buf,"../.." );
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 20/25 worktree: simplify prefixing paths
In reply to Nguyễn Thái Ngọc Duy--- builtin/worktree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
builtin/worktree.c +3 -1
@@ -330,7 +330,7 @@ static int add(int ac, const char **av, const char *prefix)if (ac <1 || ac >2 )usage_with_options (worktree_usage, options); - path = prefix ?prefix_filename (prefix,strlen (prefix), av[0 ]) : av[0 ]; + path =prefix_filename (prefix,strlen (prefix), av[0 ]); branch = ac <2 ?"HEAD" : av[1 ]; opts.force_new_branch = !!new_branch_force;
@@ -460,6 +460,8 @@ int cmd_worktree(int ac, const char **av, const char *prefix)if (ac <2 )usage_with_options (worktree_usage, options); +if (!prefix) + prefix ="" ;if (!strcmp (av[1 ],"add" ))return add (ac -1 , av +1 , prefix);if (!strcmp (av[1 ],"prune" ))
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 21/25 worktree: add "lock" command
In reply to Nguyễn Thái Ngọc Duy--- Documentation/git-worktree.txt | 12 ++++++++-- builtin/worktree.c | 41 ++++++++++++++++++++++++++++++++++ contrib/completion/git-completion.bash | 5 ++++- t/t2028-worktree-move.sh (new +x) | 34 ++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100755 t/t2028-worktree-move.sh
Documentation/git-worktree.txt +10 -2
@@ -11,6 +11,7 @@ SYNOPSIS [verse] 'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>] 'git worktree list' [--porcelain] +'git worktree lock' [--reason <string>] <path> 'git worktree prune' [-n] [-v] [--expire <expire>] DESCRIPTION
@@ -61,6 +62,12 @@ each of the linked worktrees. The output details include if the worktree is bare, the revision currently checked out, and the branch currently checked out (or 'detached HEAD' if none). +lock:: + +When a worktree is locked, it cannot be pruned, moved or deleted. For +example, if the worktree is on portable device that is not available +when "git worktree <command>" is executed. + prune:: Prune working tree information in $GIT_DIR/worktrees.
@@ -104,6 +111,9 @@ OPTIONS --expire <time>:: With `prune`, only expire unused working trees older than <time>. +--reason <string>: + An explanation why the worktree is locked. + DETAILS ------- Each linked working tree has a private sub-directory in the repository's
@@ -220,8 +230,6 @@ performed manually, such as: - `remove` to remove a linked working tree and its administrative files (and warn if the working tree is dirty) - `mv` to move or rename a working tree and update its administrative files -- `lock` to prevent automatic pruning of administrative files (for instance, - for a working tree on a portable device) GIT ---
builtin/worktree.c +41 -0
58 lines not on this page · raw message
contrib/completion/git-completion.bash +4 -1
@@ -2596,7 +2596,7 @@ _git_whatchanged () _git_worktree () { - local subcommands="add list prune" + local subcommands="add list lock prune" local subcommand="$(__git_find_on_cmdline "$subcommands")" if [ -z "$subcommand" ]; then __gitcomp "$subcommands"
@@ -2608,6 +2608,9 @@ _git_worktree () list,--*) __gitcomp "--porcelain" ;; + lock,--*) + __gitcomp "--reason" + ;; prune,--*) __gitcomp "--dry-run --expire --verbose" ;;
t/t2028-worktree-move.sh new file +34 -0
@@ -0,0 +1,34 @@ +#!/bin/sh + +test_description='test git worktree move, remove, lock and unlock' + +. ./test-lib.sh + +test_expect_success 'setup' ' + test_commit init && + git worktree add source && + git worktree list --porcelain | grep "^worktree" >actual && + cat <<-EOF >expected && + worktree $TRASH_DIRECTORY + worktree $TRASH_DIRECTORY/source + EOF + test_cmp expected actual +' + +test_expect_success 'lock main worktree' ' + test_must_fail git worktree lock . +' + +test_expect_success 'lock linked worktree' ' + git worktree lock --reason hahaha source && + echo hahaha >expected && + test_cmp expected .git/worktrees/source/locked +' + +test_expect_success 'lock worktree twice' ' + test_must_fail git worktree lock source && + echo hahaha >expected && + test_cmp expected .git/worktrees/source/locked +' + +test_done
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 22/25 worktree: add "unlock" command
In reply to Nguyễn Thái Ngọc Duy--- Documentation/git-worktree.txt | 5 +++++ builtin/worktree.c | 31 +++++++++++++++++++++++++++++++ contrib/completion/git-completion.bash | 2 +- t/t2028-worktree-move.sh | 14 ++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-)
Documentation/git-worktree.txt +5 -0
@@ -13,6 +13,7 @@ SYNOPSIS 'git worktree list' [--porcelain] 'git worktree lock' [--reason <string>] <path> 'git worktree prune' [-n] [-v] [--expire <expire>] +'git worktree unlock' <path> DESCRIPTION -----------
@@ -72,6 +73,10 @@ prune:: Prune working tree information in $GIT_DIR/worktrees. +unlock:: + +Unlock a worktree, allowing it to be pruned, moved or deleted. + OPTIONS -------
builtin/worktree.c +31 -0
@@ -16,6 +16,7 @@ static const char * const worktree_usage[] = {N_ ("git worktree list [<options>]" ),N_ ("git worktree lock [<options>] <path>" ),N_ ("git worktree prune [<options>]" ), +N_ ("git worktree unlock <path>" ), NULL };
@@ -491,6 +492,34 @@ static int lock_worktree(int ac, const char **av, const char *prefix)return 0 ; } +static int unlock_worktree (int ac,const char **av,const char *prefix) +{ +struct option options[] = { +OPT_END () + }; +struct worktree **worktrees, *wt; +struct strbuf dst =STRBUF_INIT ; + + ac =parse_options (ac, av, prefix, options, worktree_usage,0 ); +if (ac !=1 ) +usage_with_options (worktree_usage, options); + +strbuf_addstr (&dst,prefix_filename (prefix, +strlen (prefix), + av[0 ])); + + worktrees =get_worktrees (); + wt =find_worktree_by_path (worktrees, dst.buf); +if (!wt) +die (_ ("'%s' is not a working directory" ), av[0 ]); +if (is_main_worktree (wt)) +die (_ ("'%s' is a main working directory" ), av[0 ]); +if (!is_worktree_locked (wt)) +die (_ ("not locked" )); + +return unlink_or_warn (git_common_path ("worktrees/%s/locked" , wt->id)); +} +int cmd_worktree (int ac,const char **av,const char *prefix) {struct option options[] = {
@@ -509,5 +538,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)return list (ac -1 , av +1 , prefix);if (!strcmp (av[1 ],"lock" ))return lock_worktree (ac -1 , av +1 , prefix); +if (!strcmp (av[1 ],"unlock" )) +return unlock_worktree (ac -1 , av +1 , prefix);usage_with_options (worktree_usage, options); }
contrib/completion/git-completion.bash +1 -1
@@ -2596,7 +2596,7 @@ _git_whatchanged () _git_worktree () { - local subcommands="add list lock prune" + local subcommands="add list lock prune unlock" local subcommand="$(__git_find_on_cmdline "$subcommands")" if [ -z "$subcommand" ]; then __gitcomp "$subcommands"
t/t2028-worktree-move.sh +14 -0
@@ -31,4 +31,18 @@ test_expect_success 'lock worktree twice' ' test_cmp expected .git/worktrees/source/locked ' +test_expect_success 'unlock main worktree' ' + test_must_fail git worktree unlock . +' + +test_expect_success 'unlock linked worktree' ' + git worktree unlock source && + test_path_is_missing .git/worktrees/source/locked +' + +test_expect_success 'unlock worktree twice' ' + test_must_fail git worktree unlock source && + test_path_is_missing .git/worktrees/source/locked +' + test_done
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 23/25 worktree: add "move" commmand
In reply to Nguyễn Thái Ngọc Duy--- Documentation/git-worktree.txt | 6 +++- builtin/worktree.c | 60 ++++++++++++++++++++++++++++++++++ contrib/completion/git-completion.bash | 2 +- t/t2028-worktree-move.sh | 29 ++++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-)
Documentation/git-worktree.txt +5 -1
@@ -12,6 +12,7 @@ SYNOPSIS 'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>] 'git worktree list' [--porcelain] 'git worktree lock' [--reason <string>] <path> +'git worktree move' <path> <new-path> 'git worktree prune' [-n] [-v] [--expire <expire>] 'git worktree unlock' <path>
@@ -69,6 +70,10 @@ When a worktree is locked, it cannot be pruned, moved or deleted. For example, if the worktree is on portable device that is not available when "git worktree <command>" is executed. +move:: + +Move a worktree to a new location. Note that the main worktree cannot be moved. + prune:: Prune working tree information in $GIT_DIR/worktrees.
@@ -234,7 +239,6 @@ performed manually, such as: - `remove` to remove a linked working tree and its administrative files (and warn if the working tree is dirty) -- `mv` to move or rename a working tree and update its administrative files GIT ---
builtin/worktree.c +60 -0
77 lines not on this page · raw message
contrib/completion/git-completion.bash +1 -1
@@ -2596,7 +2596,7 @@ _git_whatchanged () _git_worktree () { - local subcommands="add list lock prune unlock" + local subcommands="add list lock move prune unlock" local subcommand="$(__git_find_on_cmdline "$subcommands")" if [ -z "$subcommand" ]; then __gitcomp "$subcommands"
t/t2028-worktree-move.sh +29 -0
@@ -45,4 +45,33 @@ test_expect_success 'unlock worktree twice' ' test_path_is_missing .git/worktrees/source/locked ' +test_expect_success 'move non-worktree' ' + mkdir abc && + test_must_fail git worktree move abc def +' + +test_expect_success 'move locked worktree' ' + git worktree lock source && + test_must_fail git worktree move source destination && + git worktree unlock source +' + +test_expect_success 'move worktree' ' + git worktree move source destination && + test_path_is_missing source && + git worktree list --porcelain | grep "^worktree" >actual && + cat <<-EOF >expected && + worktree $TRASH_DIRECTORY + worktree $TRASH_DIRECTORY/destination + EOF + test_cmp expected actual && + git -C destination log --format=%s >actual2 && + echo init >expected2 && + test_cmp expected2 actual2 +' + +test_expect_success 'move main worktree' ' + test_must_fail git worktree move . def +' + test_done
Signature · 1 line
2.8.0.rc0.210.gd302cd2
PATCH 24/25 worktree move: accept destination as directory
In reply to Nguyễn Thái Ngọc DuySimilar to "mv a b/", which is actually "mv a b/a", we extract basename of source worktree and create a directory of the same name at destination if dst path is a directory.
--- builtin/worktree.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
builtin/worktree.c +18 -1
@@ -538,7 +538,13 @@ static int move_worktree(int ac, const char **av, const char *prefix)strbuf_addstr (&dst,prefix_filename (prefix,strlen (prefix), av[1 ])); -if (file_exists (dst.buf)) +if (is_directory (dst.buf)) +/* +* keep going, dst will be appended after we get the +* source's absolute path +*/ + ; +else if (file_exists (dst.buf))die (_ ("target '%s' already exists" ), av[1 ]); worktrees =get_worktrees ();
@@ -558,6 +564,17 @@ static int move_worktree(int ac, const char **av, const char *prefix)if (validate_worktree (wt,0 ))return -1 ; +if (is_directory (dst.buf)) { +const char *sep =strrchr (wt->path,'/' ); + +if (!sep) +die (_ ("could not figure out destination name from '%s'" ), + wt->path); +strbuf_addstr (&dst, sep); +if (file_exists (dst.buf)) +die (_ ("target '%s' already exists" ), dst.buf); + } +/* * First try. Atomically move, and probably cheaper, if both * source and target are on the same file system.
Signature · 1 line
2.8.0.rc0.210.gd302cd2
Re: [PATCH 24/25] worktree move: accept destination as directory
Quoted 18 lines · Nguyễn Thái Ngọc Duy
Similar to "mv a b/", which is actually "mv a b/a", we extract basename of source worktree and create a directory of the same name at destination if dst path is a directory.
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>---
builtin/worktree.c +7
-1@@ -538,7 +538,13 @@ static int move_worktree(int ac, const char **av, const char *prefix) -if (file_exists (dst.buf)) +if (is_directory (dst.buf)) +/* +* keep going, dst will be appended after we get the +* source's absolute path +*/ + ; +else if (file_exists (dst.buf))die (_ ("target '%s' already exists" ), av[1 ]);
builtin/worktree.c static int move_worktree(int ac, const char **av, const char *prefix) · line 565
Quoted 2 lines
builtin/worktree.c +2
-0@@ -558,6 +564,17 @@ static int move_worktree(int ac, const char **av, const char *prefix) +if (is_directory (dst.buf)) { +const char *sep =strrchr (wt->path,'/' );
Does this need to take Windows into account? Perhaps git_find_last_dir_sep()?
Quoted 8 lines
builtin/worktree.c +8
-0+ +if (!sep) +die (_ ("could not figure out destination name from '%s'" ), + wt->path); +strbuf_addstr (&dst, sep); +if (file_exists (dst.buf)) +die (_ ("target '%s' already exists" ), dst.buf); + }
Re: [PATCH 24/25] worktree move: accept destination as directory
Quoted 15 lines · Eric Sunshine
Quoted 14 lines · Nguyễn Thái Ngọc Duy
Similar to "mv a b/", which is actually "mv a b/a", we extract basename of source worktree and create a directory of the same name at destination if dst path is a directory.
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>---
builtin/worktree.c +5
-1@@ -538,7 +538,13 @@ static int move_worktree(int ac, const char **av, const char *prefix) -if (file_exists (dst.buf)) +if (is_directory (dst.buf)) +/* +* keep going, dst will be appended after we get the +* source's absolute path +*/
Quoted 8 lines
builtin/worktree.c static int move_worktree(int ac, const char **av, const char *prefix) · line 565
Quoted 6 lines
builtin/worktree.c +4
-0+ ; + else if (file_exists(dst.buf)) die(_("target '%s' already exists"), av[1]);@@ -558,6 +564,17 @@ static int move_worktree(int ac, const char **av, const char *prefix) +if (is_directory (dst.buf)) { +const char *sep =strrchr (wt->path,'/' );Does this need to take Windows into account?
wt->path comes from $GIT_DIR/worktrees/xxx/gitdir, which normally uses forward slashes, so we should be safe. We already rely on forward slashes in get_linked_worktree()
Quoted 1 line
Perhaps
git_find_last_dir_sep()?
But this is probably a good thing to do anyway, to be more robust in future. But it could confuse the reader later on why it's necessary when backward slashes can't exist in wt->path. I don't know. Maybe just have a comment that backward slashes can't never appear here?
There is also a potential problem with find_worktree_by_path(). I was counting on real_path() to normalize paths and could simply do strcmp_icase (or its new name, fspathcmp). But real_path() does not seem to convert unify slashes. I will need to have a closer look at this. Hopefully prefix_filename() already makes sure everything uses forward slashes. Or maybe we could improve fspathcmp to see '/' and '\' the same thing on Windows.
Signature · 1 line
Duy
Re: [PATCH 24/25] worktree move: accept destination as directory
Quoted 11 lines · Duy Nguyen
Quoted 5 lines · Eric Sunshine
builtin/worktree.c static int move_worktree(int ac, const char **av, const char *prefix) · line 568
Quoted 2 lines · Nguyễn Thái Ngọc Duy
builtin/worktree.c +2
-0+if (is_directory (dst.buf)) { +const char *sep =strrchr (wt->path,'/' );Does this need to take Windows into account?
wt->pathcomes from $GIT_DIR/worktrees/xxx/gitdir, which normally uses forward slashes, so we should be safe. We already rely on forward slashes inget_linked_worktree()
Quoted 6 lines
Quoted 1 line
Perhaps
git_find_last_dir_sep()?But this is probably a good thing to do anyway, to be more robust in future. But it could confuse the reader later on why it's necessary when backward slashes can't exist in
wt->path. I don't know. Maybe just have a comment that backward slashes can't never appear here?
As this path is read from a file git itself creates, and if we know that it will always contain forward slashes, then I agree that it could be potentially confusing to later readers to see git_find_last_dir_sep(). So, keeping it as-is seems correct.
Not sure if it needs a comment. I reviewed this rather quickly since (I think) you plan on re-rolling it and I'm far behind on my reviews. Consequently, I didn't check the existing code, and reviewed only within the context of the patch itself. If the end result is that it's clear from reading the code that it will always contain forward slashes, then a comment would be redundant. You could perhaps mention in the commit message that the slash will always be forward, which should satisfy future reviewers and readers of the code once its in the tree.
Quoted 7 lines
There is also a potential problem with
find_worktree_by_path(). I was counting onreal_path()to normalize paths and could simply dostrcmp_icase(or its new name, fspathcmp). Butreal_path()does not seem to convert unify slashes. I will need to have a closer look at this. Hopefullyprefix_filename()already makes sure everything uses forward slashes. Or maybe we could improve fspathcmp to see '/' and '\' the same thing on Windows.
If we look at fspathcmp() as a function which performs whatever magic is needed to make comparisons work on a platform/filesystem, then it might indeed be reasonable to enhance it to recognize '/' and '\' as equivalent (with possible caveats for Windows corner cases).
Re: [PATCH 24/25] worktree move: accept destination as directory
Am 11.05.2016 um 19:32 schrieb Eric Sunshine:
Quoted 19 lines · Duy Nguyen
Quoted 11 lines · Eric Sunshine
Quoted 5 lines · Nguyễn Thái Ngọc Duy
builtin/worktree.c static int move_worktree(int ac, const char **av, const char *prefix) · line 568
Quoted 2 lines
builtin/worktree.c +2
-0+if (is_directory (dst.buf)) { +const char *sep =strrchr (wt->path,'/' );Does this need to take Windows into account?
wt->pathcomes from $GIT_DIR/worktrees/xxx/gitdir, which normally uses forward slashes, so we should be safe. We already rely on forward slashes inget_linked_worktree()Quoted 6 lines
Quoted 1 line
Perhaps
git_find_last_dir_sep()?But this is probably a good thing to do anyway, to be more robust in future. But it could confuse the reader later on why it's necessary when backward slashes can't exist in
wt->path. I don't know. Maybe just have a comment that backward slashes can't never appear here?
Quoted 4 lines
As this path is read from a file git itself creates, and if we know that it will always contain forward slashes, then I agree that it could be potentially confusing to later readers to see
git_find_last_dir_sep(). So, keeping it as-is seems correct.
Please allow me to disagree. There should not be any assumption that a path uses forward slashes as directory separator, except when the path is
- a pathspec
- a ref
- a path found in the object database including the index
In particular, everything concerning paths in the file system (including paths pointing to Git's own files) must not assume forward slashes.
We do convert backslashes to forward slashes in a number of places, but this is only for cosmetic reasons, not to maintain an invariant.
Quoted 4 lines
If we look at
fspathcmp()as a function which performs whatever magic is needed to make comparisons work on a platform/filesystem, then it might indeed be reasonable to enhance it to recognize '/' and '\' as equivalent (with possible caveats for Windows corner cases).
That sounds reasonable.
-- Hannes
Re: [PATCH 24/25] worktree move: accept destination as directory
Quoted 5 lines · Johannes Sixt
Quoted 4 lines
As this path is read from a file git itself creates, and if we know that it will always contain forward slashes, then I agree that it could be potentially confusing to later readers to see
git_find_last_dir_sep(). So, keeping it as-is seems correct.
Quoted 7 lines
Please allow me to disagree. There should not be any assumption that a path uses forward slashes as directory separator, except when the path is
- a pathspec
- a ref
- a path found in the object database including the index
I think standardising on one way for what we write out would give less hassle to users. The human end users should not be opening these files in their editors and futzing with their contents, but there are third-party tools and reimplementations of Git. Forcing them to be prepared for input with slashes and backslashes does not make much sense to me.
Is there an upside for us to accept both slashes in this file?
Re: [PATCH 24/25] worktree move: accept destination as directory
Am 11.05.2016 um 23:34 schrieb Junio C Hamano:
Quoted 15 lines
Quoted 5 lines · Johannes Sixt
Quoted 4 lines
As this path is read from a file git itself creates, and if we know that it will always contain forward slashes, then I agree that it could be potentially confusing to later readers to see
git_find_last_dir_sep(). So, keeping it as-is seems correct.Quoted 7 lines
Please allow me to disagree. There should not be any assumption that a path uses forward slashes as directory separator, except when the path is
- a pathspec
- a ref
- a path found in the object database including the index
Quoted 6 lines
I think standardising on one way for what we write out would give less hassle to users. The human end users should not be opening these files in their editors and futzing with their contents, but there are third-party tools and reimplementations of Git. Forcing them to be prepared for input with slashes and backslashes does not make much sense to me.
It is the opposite: We would force other tools to write slashes even though on Windows both types of slashes are allowed as directory separators.
Quoted 1 line
Is there an upside for us to accept both slashes in this file?
Obviously, yes: We can accept what third-party tools write.
BTW, we also have to accept absolute paths in the file, no? Then we cannot assume that the path begins with a slash on Windows; absolute paths would come in drive letter style on Windows.
-- Hannes
PATCH 25/25 worktree: add "remove" command
In reply to Nguyễn Thái Ngọc Duy--- Documentation/git-worktree.txt | 22 +++++---- builtin/worktree.c | 81 ++++++++++++++++++++++++++++++++++ contrib/completion/git-completion.bash | 5 ++- t/t2028-worktree-move.sh | 26 +++++++++++ 4 files changed, 124 insertions(+), 10 deletions(-)
Documentation/git-worktree.txt +13 -9
@@ -14,6 +14,7 @@ SYNOPSIS 'git worktree lock' [--reason <string>] <path> 'git worktree move' <path> <new-path> 'git worktree prune' [-n] [-v] [--expire <expire>] +'git worktree remove' [--force] <path> 'git worktree unlock' <path> DESCRIPTION
@@ -78,6 +79,14 @@ prune:: Prune working tree information in $GIT_DIR/worktrees. +remove:: + +Remove a worktree. Only clean worktrees, no untracked files and no +modification in tracked files, can be removed. Unclean worktrees can +be removed with `--force`. Main worktree cannot be removed. It needs +to be converted to a linked worktree first by moving the repository +away. + unlock:: Unlock a worktree, allowing it to be pruned, moved or deleted.
@@ -87,9 +96,10 @@ OPTIONS -f:: --force:: - By default, `add` refuses to create a new working tree when `<branch>` - is already checked out by another working tree. This option overrides - that safeguard. + By default, `add` refuses to create a new working tree when + `<branch>` is already checked out by another working tree and + `remove` refuses to remove an unclean worktree. This option + overrides that safeguard. -b <new-branch>:: -B <new-branch>::
@@ -234,12 +244,6 @@ Multiple checkout in general is still experimental, and the support for submodules is incomplete. It is NOT recommended to make multiple checkouts of a superproject. -git-worktree could provide more automation for tasks currently -performed manually, such as: - -- `remove` to remove a linked working tree and its administrative files (and - warn if the working tree is dirty) - GIT --- Part of the linkgit:git[1] suite
builtin/worktree.c +81 -0
98 lines not on this page · raw message
contrib/completion/git-completion.bash +4 -1
@@ -2596,7 +2596,7 @@ _git_whatchanged () _git_worktree () { - local subcommands="add list lock move prune unlock" + local subcommands="add list lock move prune remove unlock" local subcommand="$(__git_find_on_cmdline "$subcommands")" if [ -z "$subcommand" ]; then __gitcomp "$subcommands"
@@ -2614,6 +2614,9 @@ _git_worktree () prune,--*) __gitcomp "--dry-run --expire --verbose" ;; + remove,--*) + __gitcomp "--force" + ;; *) ;; esac
t/t2028-worktree-move.sh +26 -0
@@ -74,4 +74,30 @@ test_expect_success 'move main worktree' ' test_must_fail git worktree move . def ' +test_expect_success 'remove main worktree' ' + test_must_fail git worktree remove . +' + +test_expect_success 'remove locked worktree' ' + git worktree lock destination && + test_must_fail git worktree remove destination && + git worktree unlock destination +' + +test_expect_success 'remove worktree with dirty tracked file' ' + echo dirty >>destination/init.t && + test_must_fail git worktree remove destination +' + +test_expect_success 'remove worktree with untracked file' ' + git -C destination checkout init.t && + : >destination/untracked && + test_must_fail git worktree remove destination +' + +test_expect_success 'force remove worktree with untracked file' ' + git worktree remove --force destination && + test_path_is_missing destination +' + test_done
Signature · 1 line
2.8.0.rc0.210.gd302cd2
Quoted 2 lines · Nguyễn Thái Ngọc Duy
This is basically a resend from last time, which happened during rc time.
It would have made them a much more pleasant read if you re-read them during that time and added the missing "why" to many of the commit log message.
Quoted 10 lines
It adds 4 more commands, basically cleaning up the "TODO" list in git-worktree.txt.
So far I've only actually used move and remove (and maybe unlock once because worktree-add failed on me and I had to unlock it manually). And I don't get to move worktrees a lot either so not really extensive testing.
[01/25]
usage.c: move format processing out ofdie_errno()[02/25]usage.c: addsys_error()that printsstrerror()automatically
This looks parallel to die_errno(); isn't error_errno() a better name?
Quoted 5 lines
[03/25]
copy.c: importcopy_file()from busybox [04/25]copy.c: delete unused code incopy_file()[05/25]copy.c: convert bb_(p)error_msg to (sys_)error [06/25]copy.c: style fix
[07/25]copy.c: convertcopy_file()tocopy_dir_recursively()
Somewhere among these, there needs to be a single overview of why we want "cp" implementation of busybox, e.g. what part of "cp" we want? the whole thing? or "because this is to be used from this and that codepaths to make copy of these things, we only need these parts and can remove other features like this and that?"
Quoted 2 lines
[08/25] completion: support git-worktree
[09/25] git-worktree.txt: keep subcommand listing in alphabetical order
I'd defer doing this immediately before 21 if I were doing this series.
Offhand, I think it makes it easier to look things up in an alphabetical list in the description section, but it probably is easier to get an overview if the synopsis part groups things along concepts and/or lists things along the order in typical workflows (e.g. "create, list, rename, remove" would be a list along lifecycle), not alphabetical.
But such judgement is better done when we know what are the final elements that are to be listed, i.e. closer to where new things are introduced. This is especially true, as the log messages of patches leading to 21 are all sketchy and do not give the readers a good birds-view picture.
Quoted 1 line
[10/25]
path.c: addgit_common_path()andstrbuf_git_common_path()
Write "what for" when adding a new API function. "Wanting to learn X is very common and there are many existing code or new code that repeats sequence A, B and C to compute it. Give a helper function to do so to refactor the existing codepaths" or something like that?
Move some part of [12/25] that is not about "store 'id'" but is about this refactoring to this commit.
Quoted 2 lines
[11/25]
worktree.c: useis_dot_or_dotdot()[12/25]worktree.c: store "id" instead of "git_dir"
It is better to have these (and other conceptually "small and obvious" ones) as preliminary clean-up to make the main body of the series that may need to go through iterations smaller.
Quoted 15 lines
[13/25]
worktree.c: addclear_worktree()
[14/25]worktree.c: addfind_worktree_by_path()[15/25]worktree.c: addis_main_worktree()
[16/25]worktree.c: addvalidate_worktree()[17/25]worktree.c: addupdate_worktree_location()[18/25]worktree.c: addis_worktree_locked()[19/25] worktree: avoid 0{40}, too many zeroes, hard to read [20/25] worktree: simplify prefixing paths
[21/25] worktree: add "lock" command
[22/25] worktree: add "unlock" command
[23/25] worktree: add "move" commmand
[24/25] worktree move: accept destination as directory [25/25] worktree: add "remove" commandTotal 11 files changed, 1028 insertions(+), 48 deletions(-)
Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Nguyễn Thái Ngọc Duy
This is basically a resend from last time, which happened during rc time.
It would have made them a much more pleasant read if you re-read them during that time and added the missing "why" to many of the commit log message.
Hmm... I thought I didn't receive any comments last time.
Quoted 8 lines
Quoted 8 lines
It adds 4 more commands, basically cleaning up the "TODO" list in git-worktree.txt.
So far I've only actually used move and remove (and maybe unlock once because worktree-add failed on me and I had to unlock it manually). And I don't get to move worktrees a lot either so not really extensive testing.
Quoted 4 lines
Quoted 2 lines
[01/25]
usage.c: move format processing out ofdie_errno()[02/25]usage.c: addsys_error()that printsstrerror()automaticallyThis looks parallel to
die_errno(); isn'terror_errno()a better name?
To me, no. Duplicating the "err" looks weird. error_no() does not look good either. Though there's a couple of warning(..., strerror()), which could become warning_errno(). Then maybe error_errno() makes more sense because all three follow the same naming convention.
Quoted 11 lines
Quoted 5 lines
[03/25]
copy.c: importcopy_file()from busybox [04/25]copy.c: delete unused code incopy_file()[05/25]copy.c: convert bb_(p)error_msg to (sys_)error [06/25]copy.c: style fix
[07/25]copy.c: convertcopy_file()tocopy_dir_recursively()Somewhere among these, there needs to be a single overview of why we want "cp" implementation of busybox, e.g. what part of "cp" we want? the whole thing? or "because this is to be used from this and that codepaths to make copy of these things, we only need these parts and can remove other features like this and that?"
We need directory move functionality. In the worst case when rename(<dir>, <dir>) wouldn't do the job, we have to fall back to copying the whole directory over, preserving metadata as much as possible, then delete the old directory. I don't want to write new code for this because I think it shows in busybox code that there are pitfalls in dealing with filesystems. And I don't want to fall back to /bin/cp either. Windows won't have one (long term I hope we won't need msys) and *nix is not famous for consistent command line behavior. Plus, if we want to clean up a failed cp operation, it's easier to do it in core by keeping track of what files have been copied.
Quoted 5 lines
Quoted 2 lines
[08/25] completion: support git-worktree
[09/25] git-worktree.txt: keep subcommand listing in alphabetical orderI'd defer doing this immediately before 21 if I were doing this series.
Will do.
Quoted 7 lines
Offhand, I think it makes it easier to look things up in an alphabetical list in the description section, but it probably is easier to get an overview if the synopsis part groups things along concepts and/or lists things along the order in typical workflows (e.g. "create, list, rename, remove" would be a list along lifecycle), not alphabetical.
Quoted 5 lines
But such judgement is better done when we know what are the final elements that are to be listed, i.e. closer to where new things are introduced. This is especially true, as the log messages of patches leading to 21 are all sketchy and do not give the readers a good birds-view picture.
Well. I think all the commands are there now at the end of this series. So we have add, list, prune, move, remove, lock and unlock. I guess we can group list/add/move/remove together and the rest as support commands. I might add "git worktree migrate" for converting between worktree v0 and v1. But it's not clear yet.
Quoted 6 lines
Quoted 1 line
[10/25]
path.c: addgit_common_path()andstrbuf_git_common_path()Write "what for" when adding a new API function. "Wanting to learn X is very common and there are many existing code or new code that repeats sequence A, B and C to compute it. Give a helper function to do so to refactor the existing codepaths" or something like that?
Pretty much for convenience. Will add some more in commit message.
Quoted 9 lines
Move some part of [12/25] that is not about "store 'id'" but is about this refactoring to this commit.
Quoted 2 lines
[11/25]
worktree.c: useis_dot_or_dotdot()[12/25]worktree.c: store "id" instead of "git_dir"It is better to have these (and other conceptually "small and obvious" ones) as preliminary clean-up to make the main body of the series that may need to go through iterations smaller.
Sure.
Signature · 1 line
Duy
Quoted 11 lines · Duy Nguyen
Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Nguyễn Thái Ngọc Duy
This is basically a resend from last time, which happened during rc time.
It would have made them a much more pleasant read if you re-read them during that time and added the missing "why" to many of the commit log message.
Hmm... I thought I didn't receive any comments last time.
I think you've been here long enough to know that absense of comments does not mean anything more than that: lack of interest at that moment in time.
Quoted 6 lines
Quoted 1 line
This looks parallel to
die_errno(); isn'terror_errno()a better name?To me, no. Duplicating the "err" looks weird.
error_no()does not look good either. Though there's a couple ofwarning(..., strerror()), which could becomewarning_errno(). Then maybeerror_errno()makes more sense because all three follow the same naming convention.
So in the end error_errno() would be a better name to you after all ;-) I agree the stuttering sound coming from repeating error twice feels somewhat odd, but warning_errno() would be so natural and obvious future direction, so...
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
[03/25]
copy.c: importcopy_file()from busybox [04/25]copy.c: delete unused code incopy_file()[05/25]copy.c: convert bb_(p)error_msg to (sys_)error [06/25]copy.c: style fix
[07/25]copy.c: convertcopy_file()tocopy_dir_recursively()
Quoted 7 lines
Quoted 5 lines
Somewhere among these, there needs to be a single overview of why we want "cp" implementation of busybox, e.g. what part of "cp" we want? the whole thing? or "because this is to be used from this and that codepaths to make copy of these things, we only need these parts and can remove other features like this and that?"
We need directory move functionality.
Yeah, I know all that but you do not want to explain that to me only when I asked in a mailing list response. You want to get into the habit of having that in the log message to help reviewers, not just me, before they ask such a question.
Quoted 11 lines
Quoted 5 lines
But such judgement is better done when we know what are the final elements that are to be listed, i.e. closer to where new things are introduced. This is especially true, as the log messages of patches leading to 21 are all sketchy and do not give the readers a good birds-view picture.
Well. I think all the commands are there now at the end of this series. So we have add, list, prune, move, remove, lock and unlock. I guess we can group list/add/move/remove together and the rest as support commands. I might add "git worktree migrate" for converting between worktree v0 and v1. But it's not clear yet.
Just so that there is no confusion, I am not opposed to reordering. I was just saying that the decision on what the right ordering is can be easier to make when the readers more or less know what the final set of things in the set are, and because that becomes only clear when they start reading 21, and because the log messages of patches leading to 21 do not show the direction clearly enough, it is hard to make that judgment at this point so early in the series.