rerere: Expose an API corresponding to 'clear' functionality
PATCH 1/1 rerere: Expose an API corresponding to 'clear' functionality
Expose a new function called rerere_clear, and rework 'builtin/rerere.c' to use this when the corresponding command-line argument is passed.
---
Ref: http://thread.gmane.org/gmane.comp.version-control.git/171255/focus=171273
builtin/rerere.c | 19 +++---------------- rerere.c | 24 ++++++++++++++++++++++++ rerere.h | 2 ++ 3 files changed, 29 insertions(+), 16 deletions(-)
builtin/rerere.c +3 -16
@@ -28,14 +28,6 @@ static time_t rerere_last_used_at(const char *name)return stat (rerere_path (name,"postimage" ), &st) ? (time_t )0 : st.st_mtime; } -static void unlink_rr_item (const char *name) -{ -unlink (rerere_path (name,"thisimage" )); -unlink (rerere_path (name,"preimage" )); -unlink (rerere_path (name,"postimage" )); -rmdir (git_path ("rr-cache/%s" , name)); -} -static int git_rerere_gc_config (const char *var,const char *value,void *cb) {if (!strcmp (var,"gc.rerereresolved" ))
@@ -142,19 +134,14 @@ int cmd_rerere(int argc, const char **argv, const char *prefix) pathspec =get_pathspec (prefix, argv +1 );return rerere_forget (pathspec); } +if (!strcmp (argv[0 ],"clear" )) +return rerere_clear (); fd =setup_rerere (&merge_rr, flags);if (fd <0 )return 0 ; -if (!strcmp (argv[0 ],"clear" )) { -for (i =0 ; i < merge_rr.nr; i++) { -const char *name = (const char *)merge_rr.items[i].util; -if (!has_rerere_resolution (name)) -unlink_rr_item (name); - } -unlink_or_warn (git_path ("MERGE_RR" )); - }else if (!strcmp (argv[0 ],"gc" )) +if (!strcmp (argv[0 ],"gc" ))garbage_collect (&merge_rr);else if (!strcmp (argv[0 ],"status" ))for (i =0 ; i < merge_rr.nr; i++)
rerere.c +24 -0
@@ -25,6 +25,14 @@ const char *rerere_path(const char *hex, const char *file)return git_path ("rr-cache/%s/%s" , hex, file); } +void unlink_rr_item (const char *name) +{ +unlink (rerere_path (name,"thisimage" )); +unlink (rerere_path (name,"preimage" )); +unlink (rerere_path (name,"postimage" )); +rmdir (git_path ("rr-cache/%s" , name)); +} +int has_rerere_resolution (const char *hex) {struct stat st;
@@ -671,3 +679,19 @@ int rerere_forget(const char **pathspec) }return write_rr (&merge_rr, fd); } + +int rerere_clear (void ) +{ +int i, fd; +struct string_list merge_rr =STRING_LIST_INIT_DUP ; + + fd =setup_rerere (&merge_rr,RERERE_NOAUTOUPDATE ); +for (i =0 ; i < merge_rr.nr; i++) { +const char *name = (const char *)merge_rr.items[i].util; +if (!has_rerere_resolution (name)) +unlink_rr_item (name); + } +string_list_clear (&merge_rr,1 ); +unlink_or_warn (git_path ("MERGE_RR" )); +return 0 ; +}
rerere.h +2 -0
@@ -16,8 +16,10 @@ extern void *RERERE_RESOLVED;extern int setup_rerere (struct string_list *,int );extern int rerere (int );extern const char *rerere_path (const char *hex,const char *file); +extern void unlink_rr_item (const char *name);extern int has_rerere_resolution (const char *hex);extern int rerere_forget (const char **); +extern int rerere_clear (void );extern int rerere_remaining (struct string_list *);#define OPT_RERERE_AUTOUPDATE (v) OPT_UYN(0, "rerere-autoupdate", (v), \
Signature · 1 line
1.7.4.rc1.7.g2cf08.dirty
Quoted 13 lines · Ramkumar Ramachandra
Expose a new function called
rerere_clear, and rework 'builtin/rerere.c' to use this when the corresponding command-line argument is passed.builtin/rerere.c +2
-0@@ -142,19 +134,14 @@ int cmd_rerere(int argc, const char **argv, const char *prefix) pathspec =get_pathspec (prefix, argv +1 );return rerere_forget (pathspec); } +if (!strcmp (argv[0 ],"clear" )) +return rerere_clear (); fd =setup_rerere (&merge_rr, flags);if (fd <0 )
builtin/rerere.c int cmd_rerere(int argc, const char **argv, const char *prefix) · line 157
Quoted 10 lines
builtin/rerere.c +0
-8return 0 ; -if (!strcmp (argv[0 ],"clear" )) { -for (i =0 ; i < merge_rr.nr; i++) { -const char *name = (const char *)merge_rr.items[i].util; -if (!has_rerere_resolution (name)) -unlink_rr_item (name); - } -unlink_or_warn (git_path ("MERGE_RR" )); - }else if (!strcmp (argv[0 ],"gc" ))
In your version, the call of rerere_clear() is not protected by a check that used to return early in a repository that is configured not to use rerere. I don't see there is a corresponding early return in the new function rerere_clear() you created in rerere.c, either.
Would that cause a user-visible regression?
rerere.h extern void *RERERE_RESOLVED; · line 19
Quoted 9 lines
rerere.h +1
-0@@ -16,8 +16,10 @@ extern void *RERERE_RESOLVED;extern int setup_rerere (struct string_list *,int );extern int rerere (int );extern const char *rerere_path (const char *hex,const char *file); +extern void unlink_rr_item (const char *name);
The name was perfectly fine while it was a file-scope static, but is unacceptable as a public function. At least please spell "rerere" out somewhere to ensure that even first-time readers can tell what area of the system the function is about.
PATCH v2 1/1 rerere: Expose an API corresponding to 'clear' functionality
Expose a new function called rerere_clear, and make 'builtin/rerere.c' use this when the corresponding command-line argument is passed. As a side-effect, also expose unlink_rr_item as unlink_rerere_item.
---
Junio: Thanks for the review. Is this version alright?
builtin/rerere.c | 21 ++++----------------- rerere.c | 27 +++++++++++++++++++++++++++ rerere.h | 2 ++ 3 files changed, 33 insertions(+), 17 deletions(-)
builtin/rerere.c +4 -17
@@ -28,14 +28,6 @@ static time_t rerere_last_used_at(const char *name)return stat (rerere_path (name,"postimage" ), &st) ? (time_t )0 : st.st_mtime; } -static void unlink_rr_item (const char *name) -{ -unlink (rerere_path (name,"thisimage" )); -unlink (rerere_path (name,"preimage" )); -unlink (rerere_path (name,"postimage" )); -rmdir (git_path ("rr-cache/%s" , name)); -} -static int git_rerere_gc_config (const char *var,const char *value,void *cb) {if (!strcmp (var,"gc.rerereresolved" ))
@@ -76,7 +68,7 @@ static void garbage_collect(struct string_list *rr)string_list_append (&to_remove, e->d_name); }for (i =0 ; i < to_remove.nr; i++) -unlink_rr_item (to_remove.items[i].string); +unlink_rerere_item (to_remove.items[i].string);string_list_clear (&to_remove,0 ); }
@@ -142,19 +134,14 @@ int cmd_rerere(int argc, const char **argv, const char *prefix) pathspec =get_pathspec (prefix, argv +1 );return rerere_forget (pathspec); } +if (!strcmp (argv[0 ],"clear" )) +return rerere_clear (); fd =setup_rerere (&merge_rr, flags);if (fd <0 )return 0 ; -if (!strcmp (argv[0 ],"clear" )) { -for (i =0 ; i < merge_rr.nr; i++) { -const char *name = (const char *)merge_rr.items[i].util; -if (!has_rerere_resolution (name)) -unlink_rr_item (name); - } -unlink_or_warn (git_path ("MERGE_RR" )); - }else if (!strcmp (argv[0 ],"gc" )) +if (!strcmp (argv[0 ],"gc" ))garbage_collect (&merge_rr);else if (!strcmp (argv[0 ],"status" ))for (i =0 ; i < merge_rr.nr; i++)
rerere.c +27 -0
@@ -25,6 +25,14 @@ const char *rerere_path(const char *hex, const char *file)return git_path ("rr-cache/%s/%s" , hex, file); } +void unlink_rerere_item (const char *name) +{ +unlink (rerere_path (name,"thisimage" )); +unlink (rerere_path (name,"preimage" )); +unlink (rerere_path (name,"postimage" )); +rmdir (git_path ("rr-cache/%s" , name)); +} +int has_rerere_resolution (const char *hex) {struct stat st;
@@ -671,3 +679,22 @@ int rerere_forget(const char **pathspec) }return write_rr (&merge_rr, fd); } + +int rerere_clear (void ) +{ +int i, fd; +struct string_list merge_rr =STRING_LIST_INIT_DUP ; + + fd =setup_rerere (&merge_rr,RERERE_NOAUTOUPDATE ); +if (fd <0 ) +return 0 ; + +for (i =0 ; i < merge_rr.nr; i++) { +const char *name = (const char *)merge_rr.items[i].util; +if (!has_rerere_resolution (name)) +unlink_rerere_item (name); + } +string_list_clear (&merge_rr,1 ); +unlink_or_warn (git_path ("MERGE_RR" )); +return 0 ; +}
rerere.h +2 -0
@@ -16,8 +16,10 @@ extern void *RERERE_RESOLVED;extern int setup_rerere (struct string_list *,int );extern int rerere (int );extern const char *rerere_path (const char *hex,const char *file); +extern void unlink_rerere_item (const char *name);extern int has_rerere_resolution (const char *hex);extern int rerere_forget (const char **); +extern int rerere_clear (void );extern int rerere_remaining (struct string_list *);#define OPT_RERERE_AUTOUPDATE (v) OPT_UYN(0, "rerere-autoupdate", (v), \
Signature · 1 line
1.7.4.rc1.7.g2cf08.dirty
Hi,
Quoted 1 line · Ramkumar Ramachandra
Expose a new function called
rerere_clear,
The commit message is a good chance to quickly explain the API. What does the function do? When would one call it? Is it equivalent to running "git rerere clear"? Any gotchas?
Quoted 2 lines
and make '
builtin/rerere.c'
use this when the corresponding command-line argument is passed.
Didn't "git rerere" already use the functionality of this function? I'm not sure what this part means.
Quoted 2 lines
As a
side-effect, also exposeunlink_rr_itemasunlink_rerere_item.
This is not a side-effect; you did it directly.
I think the reason for this is that rerere_gc is not being exposed at the same time, right? I suppose if I were doing it, I would have moved that to rerere.c, too and kept unlink_rr_item static, but there is also appeal in a minimal patch. It would be clearer to say something to the effect that we
Also export unlink_rr_item as unlink_rerere_item so rerere_clear and the un-libified "git rerere gc" can both use it.
[...]
Quoted 9 lines
+++
b/builtin/rerere.cbuiltin/rerere.c +2
-0@@ -142,19 +134,14 @@ int cmd_rerere(int argc, const char **argv, const char *prefix) pathspec =get_pathspec (prefix, argv +1 );return rerere_forget (pathspec); } +if (!strcmp (argv[0 ],"clear" )) +return rerere_clear (); fd =setup_rerere (&merge_rr, flags);
[...]
Quoted 2 lines
+++
b/rerere.c(quoted diff) +0
-0@@ -671,3 +679,22 @@ int rerere_clear(void)
[...]
rerere.c int rerere_forget(const char **pathspec) · line 690
Quoted 3 lines
rerere.c +3
-0+ fd =setup_rerere (&merge_rr,RERERE_NOAUTOUPDATE ); +if (fd <0 ) +return 0 ;
Why RERERE_NOAUTOUPDATE instead of 0? (Of course it doesn't matter in
practice. Maybe "0" would convey that more clearly?)
rerere.c int rerere_forget(const char **pathspec) · line 700
Quoted 10 lines
rerere.c +10
-0+ +for (i =0 ; i < merge_rr.nr; i++) { +const char *name = (const char *)merge_rr.items[i].util; +if (!has_rerere_resolution (name)) +unlink_rerere_item (name); + } +string_list_clear (&merge_rr,1 ); +unlink_or_warn (git_path ("MERGE_RR" )); +return 0 ; +}
The write_lock is never rolled back. "git rerere" won't care since it exits moments later and the atexit handler is called, but others might mind that they can't perform any more rerere operations afterwards. :)
Thanks and hope that helps.
Jonathan
PATCH v3 1/1 rerere: Expose an API corresponding to 'clear' functionality
Libify the "rerere clear" into a simple function called rerere_clear that takes no arguments, and returns the exit status. Also export unlink_rr_item as unlink_rerere_item so rerere_clear and the un-libified "git rerere gc" can both use it.
---
What changed since v2: Jonathan's review.
builtin/rerere.c | 21 ++++----------------- rerere.c | 27 +++++++++++++++++++++++++++ rerere.h | 2 ++ 3 files changed, 33 insertions(+), 17 deletions(-)
builtin/rerere.c +4 -17
@@ -28,14 +28,6 @@ static time_t rerere_last_used_at(const char *name)return stat (rerere_path (name,"postimage" ), &st) ? (time_t )0 : st.st_mtime; } -static void unlink_rr_item (const char *name) -{ -unlink (rerere_path (name,"thisimage" )); -unlink (rerere_path (name,"preimage" )); -unlink (rerere_path (name,"postimage" )); -rmdir (git_path ("rr-cache/%s" , name)); -} -static int git_rerere_gc_config (const char *var,const char *value,void *cb) {if (!strcmp (var,"gc.rerereresolved" ))
@@ -76,7 +68,7 @@ static void garbage_collect(struct string_list *rr)string_list_append (&to_remove, e->d_name); }for (i =0 ; i < to_remove.nr; i++) -unlink_rr_item (to_remove.items[i].string); +unlink_rerere_item (to_remove.items[i].string);string_list_clear (&to_remove,0 ); }
@@ -142,19 +134,14 @@ int cmd_rerere(int argc, const char **argv, const char *prefix) pathspec =get_pathspec (prefix, argv +1 );return rerere_forget (pathspec); } +if (!strcmp (argv[0 ],"clear" )) +return rerere_clear (); fd =setup_rerere (&merge_rr, flags);if (fd <0 )return 0 ; -if (!strcmp (argv[0 ],"clear" )) { -for (i =0 ; i < merge_rr.nr; i++) { -const char *name = (const char *)merge_rr.items[i].util; -if (!has_rerere_resolution (name)) -unlink_rr_item (name); - } -unlink_or_warn (git_path ("MERGE_RR" )); - }else if (!strcmp (argv[0 ],"gc" )) +if (!strcmp (argv[0 ],"gc" ))garbage_collect (&merge_rr);else if (!strcmp (argv[0 ],"status" ))for (i =0 ; i < merge_rr.nr; i++)
rerere.c +27 -0
@@ -25,6 +25,14 @@ const char *rerere_path(const char *hex, const char *file)return git_path ("rr-cache/%s/%s" , hex, file); } +void unlink_rerere_item (const char *name) +{ +unlink (rerere_path (name,"thisimage" )); +unlink (rerere_path (name,"preimage" )); +unlink (rerere_path (name,"postimage" )); +rmdir (git_path ("rr-cache/%s" , name)); +} +int has_rerere_resolution (const char *hex) {struct stat st;
@@ -671,3 +679,22 @@ int rerere_forget(const char **pathspec) }return write_rr (&merge_rr, fd); } + +int rerere_clear (void ) +{ +int i, fd; +struct string_list merge_rr =STRING_LIST_INIT_DUP ; + + fd =setup_rerere (&merge_rr,0 ); +if (fd <0 ) +return -1 ; +for (i =0 ; i < merge_rr.nr; i++) { +const char *name = (const char *)merge_rr.items[i].util; +if (!has_rerere_resolution (name)) +unlink_rerere_item (name); + } +string_list_clear (&merge_rr,1 ); +unlink_or_warn (git_path ("MERGE_RR" )); +rollback_lock_file (&write_lock); +return 0 ; +}
rerere.h +2 -0
@@ -16,8 +16,10 @@ extern void *RERERE_RESOLVED;extern int setup_rerere (struct string_list *,int );extern int rerere (int );extern const char *rerere_path (const char *hex,const char *file); +extern void unlink_rerere_item (const char *name);extern int has_rerere_resolution (const char *hex);extern int rerere_forget (const char **); +extern int rerere_clear (void );extern int rerere_remaining (struct string_list *);#define OPT_RERERE_AUTOUPDATE (v) OPT_UYN(0, "rerere-autoupdate", (v), \
Signature · 1 line
1.7.5.GIT
Quoted 9 lines · Ramkumar Ramachandra
Libify the "rerere clear" into a simple function called
rerere_clearthat takes no arguments, and returns the exit status. Also exportunlink_rr_itemasunlink_rerere_itemsorerere_clearand the un-libified "git rerere gc" can both use it.Helped-by Jonathan Nieder <[email protected]>Signed-off-by Ramkumar Ramachandra <[email protected]>---
What changed since v2: Jonathan's review.
Are you sure this is the version you wanted to send?
You now return -1 from rerere_clear() when setup_rerere() says that the feature is not enabled, and this is propagated back to cmd_rerere(), causing the whole command to report a failure in its exit status, which seems to me a grave regression. Your previous round got this part right, but it is broken in this round.
Also I seem to recall that Jonathan suggested that you do not have to expose unlink_rr_item() as an external symbol if you moved the garbage collection part from builtin/rerere.c to rerere.c but I do not see such a change in this patch. I think the gc interface is a lot more reasonable API to expose to external callers ("git gc" may want to make an internal call to rerere_gc() moved to rerere.c, instead of spawning "git rerere gc" as an external command) than unlink_rerere_item() that is only useful for callers that are deep inside rerere specific codepath.
What is going on?
v4 cover Libify rerere: clear and gc
Hi,
This is an alternate version of the v3 patch: Instead of exposing unlink_rr_item, I have chosen to libify "rerere gc" in order to libify "rerere clear", and it may be argued that this is a pleasant side-effect. I've also included another patch to replace the "die_errno" call with a new "error_errno" call, and I hope this will be used in other places as well. Note one more subtle change: I've chosen to pass "flags" as an argument to both functions for the sake of consistency; if and when rerere grows more command-line options, "flags" can be replaced by a struct containing those parsed options.
Cons: This patch is much larger than v3.
Thanks to: Junio and Jonathan.
Ramkumar Ramachandra (2):
usage: Introduce error_errno corresponding to die_errno
rerere: Libify "rerere clear" and "rerere gc"
builtin/rerere.c | 81 ++----------------------------------------- git-compat-util.h | 1 + rerere.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ rerere.h | 2 + usage.c | 10 +++++ 5 files changed, 116 insertions(+), 77 deletions(-)
Signature · 1 line
1.7.5.GIT
PATCH v4 1/2 usage: Introduce error_errno corresponding to die_errno
The error function always returns the constant value -1, and this does not convey enough information about the nature of the error. This patch introduces a new function error_errno that functions exactly like the error function, except that it returns errorno instead of a constant value.
---
Is "-errno" correct? This will determine how error-handling will be in a more libified version of Git, and I think it's important to get this right.
git-compat-util.h | 1 + usage.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 0 deletions(-)
git-compat-util.h +1 -0
@@ -241,6 +241,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 error_errno (const char *err, ...) __attribute__((format (printf,1 ,2 )));extern void warning (const char *err, ...) __attribute__((format (printf,1 ,2 ))); extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
usage.c +10 -0
@@ -107,6 +107,16 @@ int error(const char *err, ...)return -1 ; } +int error_errno (const char *err, ...) +{ +va_list params; + +va_start (params, err); +error_routine (err, params); +va_end (params); +return -errno; +} +void warning (const char *warn, ...) {va_list params;
Signature · 1 line
1.7.5.GIT
Re: [PATCH v4 1/2] usage: Introduce error_errno corresponding to die_errno
Hi,
Quoted 5 lines · Ramkumar Ramachandra
The error function always returns the constant value -1, and this does not convey enough information about the nature of the error. This patch introduces a new function
error_errnothat functions exactly like the error function, except that it returnserrornoinstead of a constant value.
An extra note: this could have been implemented differently, as seen in my sequencer series [1]. However, after some thought, I think this is how we want error handling to work. The name may be a little troubling because it doesn't behave like die_errno: does anyone have suggestions for a different name?
[1]: http://article.gmane.org/gmane.comp.version-control.git/171262
-- Ram
Re: [PATCH v4 1/2] usage: Introduce error_errno corresponding to die_errno
In reply to Ramkumar Ramachandragit-compat-util.h extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, · line 245
Quoted 10 lines · Ramkumar Ramachandra
git-compat-util.h +1
-0@@ -241,6 +241,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 error_errno (const char *err, ...) __attribute__((format (printf,1 ,2 )));extern void warning (const char *err, ...) __attribute__((format (printf,1 ,2 )));
Anybody remotely familiar with die_errno() would at least expect that the new and improved error_errno() function would give the errno neatly formatted via strerror() in the message, but that is not what the function does.
Quoted 7 lines
usage.c +0
-0@@ -107,6 +107,16 @@ int error(const char *err, ...)return -1 ; }
usage.c int error(const char *err, ...) · line 118
Quoted 10 lines
usage.c +9
-0+int error_errno (const char *err, ...) +{ +va_list params; + +va_start (params, err); +error_routine (err, params); +va_end (params); +return -errno; +}
Can error_routine() do something to cause errno to change?
For that matter, I suspect that the caller who would _care_ about what kind of error it got would need to save it away itself anyway. For example, it is not entirely clear if the callsite of this function in your other patch is correct. Can rollback_lock_file() cause errno to change?
git_config(git_rerere_gc_config, NULL);
dir = opendir(git_path("rr-cache"));
if (!dir) {+ int err = errno;
- rollback_lock_file(&write_lock);
- return error_errno("Unable to open rr-cache directory");
+ error("Untable to open rr-cache directory: %s", strerror(err));
+ return -err; }
I would prefer to do without introducing a confusingly named API function whose first and only usecase does not even suggest it would be useful.
PATCH v4 2/2 rerere: Libify "rerere clear" and "rerere gc"
In reply to Ramkumar RamachandraLibify "rerere clear" and "rerere gc" into simple functions called rerere_clear and rerere_garbage_collect; both functions take flags as a lone argument and return the exit status.
---
Have I cleaned up all the locks correctly? The tests should probably be updated to catch unclean exists- I'll probably work on this shortly.
builtin/rerere.c | 81 ++------------------------------------------ rerere.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ rerere.h | 2 + 3 files changed, 105 insertions(+), 77 deletions(-)
builtin/rerere.c +4 -77
@@ -12,74 +12,6 @@ static const char * const rerere_usage[] = { NULL, }; -/* these values are days */ -static int cutoff_noresolve =15 ; -static int cutoff_resolve =60 ; - -static time_t rerere_created_at (const char *name) -{ -struct stat st; -return stat (rerere_path (name,"preimage" ), &st) ? (time_t )0 : st.st_mtime; -} - -static time_t rerere_last_used_at (const char *name) -{ -struct stat st; -return stat (rerere_path (name,"postimage" ), &st) ? (time_t )0 : st.st_mtime; -} - -static void unlink_rr_item (const char *name) -{ -unlink (rerere_path (name,"thisimage" )); -unlink (rerere_path (name,"preimage" )); -unlink (rerere_path (name,"postimage" )); -rmdir (git_path ("rr-cache/%s" , name)); -} - -static int git_rerere_gc_config (const char *var,const char *value,void *cb) -{ -if (!strcmp (var,"gc.rerereresolved" )) - cutoff_resolve =git_config_int (var, value); -else if (!strcmp (var,"gc.rerereunresolved" )) - cutoff_noresolve =git_config_int (var, value); -else -return git_default_config (var, value, cb); -return 0 ; -} - -static void garbage_collect (struct string_list *rr) -{ -struct string_list to_remove =STRING_LIST_INIT_DUP ; -DIR *dir; -struct dirent *e; -int i, cutoff; -time_t now =time (NULL ), then; - -git_config (git_rerere_gc_config,NULL ); - dir =opendir (git_path ("rr-cache" )); -if (!dir) -die_errno ("unable to open rr-cache directory" ); -while ((e =readdir (dir))) { -if (is_dot_or_dotdot (e->d_name)) -continue ; - - then =rerere_last_used_at (e->d_name); -if (then) { - cutoff = cutoff_resolve; - }else { - then =rerere_created_at (e->d_name); -if (!then) -continue ; - cutoff = cutoff_noresolve; - } -if (then < now - cutoff *86400 ) -string_list_append (&to_remove, e->d_name); - } -for (i =0 ; i < to_remove.nr; i++) -unlink_rr_item (to_remove.items[i].string); -string_list_clear (&to_remove,0 ); -} -static int outf (void *dummy,mmbuffer_t *ptr,int nbuf) {int i;
@@ -142,20 +74,15 @@ int cmd_rerere(int argc, const char **argv, const char *prefix) pathspec =get_pathspec (prefix, argv +1 );return rerere_forget (pathspec); } +else if (!strcmp (argv[0 ],"clear" )) +return rerere_clear (flags); +else if (!strcmp (argv[0 ],"gc" )) +return rerere_garbage_collect (flags); fd =setup_rerere (&merge_rr, flags);if (fd <0 )return 0 ; -if (!strcmp (argv[0 ],"clear" )) { -for (i =0 ; i < merge_rr.nr; i++) { -const char *name = (const char *)merge_rr.items[i].util; -if (!has_rerere_resolution (name)) -unlink_rr_item (name); - } -unlink_or_warn (git_path ("MERGE_RR" )); - }else if (!strcmp (argv[0 ],"gc" )) -garbage_collect (&merge_rr);else if (!strcmp (argv[0 ],"status" ))for (i =0 ; i < merge_rr.nr; i++)printf ("%s\n" , merge_rr.items[i].string);
rerere.c +99 -0
120 lines not on this page · raw message
rerere.h +2 -0
@@ -15,10 +15,12 @@ extern void *RERERE_RESOLVED;extern int setup_rerere (struct string_list *,int );extern int rerere (int ); +extern int rerere_garbage_collect (int );extern const char *rerere_path (const char *hex,const char *file);extern int has_rerere_resolution (const char *hex);extern int rerere_forget (const char **);extern int rerere_remaining (struct string_list *); +extern int rerere_clear (int );#define OPT_RERERE_AUTOUPDATE (v) OPT_UYN(0, "rerere-autoupdate", (v), \ "update the index with reused conflict resolution if possible")
Signature · 1 line
1.7.5.GIT
Re: [PATCH v4 2/2] rerere: Libify "rerere clear" and "rerere gc"
Quoted 11 lines · Ramkumar Ramachandra
rerere.c +3
-0@@ -623,6 +658,51 @@ int rerere(int flags)return do_plain_rerere (&merge_rr, fd); } +int rerere_garbage_collect (int flags) +{ +struct string_list merge_rr =STRING_LIST_INIT_DUP ;
rerere.c int rerere(int flags) · line 673
Quoted 10 lines
rerere.c +10
-0+struct string_list to_remove =STRING_LIST_INIT_DUP ; + +DIR *dir; +struct dirent *e; +int i, fd, cutoff; +time_t now =time (NULL ), then; + + fd =setup_rerere (&merge_rr, flags); +if (fd <0 ) +return 0 ;
Not a good taste in API design, I would have to say. The callers may already want to have interacted with the rerere mechanism before calling into this function and that is exactly why setup_rerere() is a public API from the beginning. I would prefer to see you make it the caller's responsibility just like the original code you moved from builtin/rerere.c did.
rerere.c int rerere(int flags) · line 679
Quoted 6 lines
rerere.c +6
-0+git_config (git_rerere_gc_config,NULL ); + dir =opendir (git_path ("rr-cache" )); +if (!dir) { +rollback_lock_file (&write_lock); +return error_errno ("Unable to open rr-cache directory" ); + }
I have already commented on this part and showed an alternative.
Whatever you do, how about starting from just a simple code movement, without any change in behaviour?
That not only makes it easier to review your subsequent changes on top of the result, but the original "large code movement" patch infinitely easier to review, as "blame rerere.c" can tell us that almost all the lines came from builtin/rerere.c without any new bug slipping in.
-- >8 --
Subject: [PATCH] rerere: libify rerere_clear() and rerere_gc()
This moves the two features from builtin/rerere.c to a more library-ish portion of the codebase. No behaviour change.
--- builtin/rerere.c | 77 +------------------------------------------------ rerere.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ rerere.h | 2 + 3 files changed, 88 insertions(+), 75 deletions(-)
builtin/rerere.c +2 -75
@@ -12,74 +12,6 @@ static const char * const rerere_usage[] = { NULL, }; -/* these values are days */ -static int cutoff_noresolve =15 ; -static int cutoff_resolve =60 ; - -static time_t rerere_created_at (const char *name) -{ -struct stat st; -return stat (rerere_path (name,"preimage" ), &st) ? (time_t )0 : st.st_mtime; -} - -static time_t rerere_last_used_at (const char *name) -{ -struct stat st; -return stat (rerere_path (name,"postimage" ), &st) ? (time_t )0 : st.st_mtime; -} - -static void unlink_rr_item (const char *name) -{ -unlink (rerere_path (name,"thisimage" )); -unlink (rerere_path (name,"preimage" )); -unlink (rerere_path (name,"postimage" )); -rmdir (git_path ("rr-cache/%s" , name)); -} - -static int git_rerere_gc_config (const char *var,const char *value,void *cb) -{ -if (!strcmp (var,"gc.rerereresolved" )) - cutoff_resolve =git_config_int (var, value); -else if (!strcmp (var,"gc.rerereunresolved" )) - cutoff_noresolve =git_config_int (var, value); -else -return git_default_config (var, value, cb); -return 0 ; -} - -static void garbage_collect (struct string_list *rr) -{ -struct string_list to_remove =STRING_LIST_INIT_DUP ; -DIR *dir; -struct dirent *e; -int i, cutoff; -time_t now =time (NULL ), then; - -git_config (git_rerere_gc_config,NULL ); - dir =opendir (git_path ("rr-cache" )); -if (!dir) -die_errno ("unable to open rr-cache directory" ); -while ((e =readdir (dir))) { -if (is_dot_or_dotdot (e->d_name)) -continue ; - - then =rerere_last_used_at (e->d_name); -if (then) { - cutoff = cutoff_resolve; - }else { - then =rerere_created_at (e->d_name); -if (!then) -continue ; - cutoff = cutoff_noresolve; - } -if (then < now - cutoff *86400 ) -string_list_append (&to_remove, e->d_name); - } -for (i =0 ; i < to_remove.nr; i++) -unlink_rr_item (to_remove.items[i].string); -string_list_clear (&to_remove,0 ); -} -static int outf (void *dummy,mmbuffer_t *ptr,int nbuf) {int i;
@@ -148,14 +80,9 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)return 0 ;if (!strcmp (argv[0 ],"clear" )) { -for (i =0 ; i < merge_rr.nr; i++) { -const char *name = (const char *)merge_rr.items[i].util; -if (!has_rerere_resolution (name)) -unlink_rr_item (name); - } -unlink_or_warn (git_path ("MERGE_RR" )); +rerere_clear (&merge_rr); }else if (!strcmp (argv[0 ],"gc" )) -garbage_collect (&merge_rr); +rerere_gc (&merge_rr);else if (!strcmp (argv[0 ],"status" ))for (i =0 ; i < merge_rr.nr; i++)printf ("%s\n" , merge_rr.items[i].string);
rerere.c +84 -0
87 lines not on this page · raw message
rerere.h +2 -0
@@ -19,6 +19,8 @@ extern const char *rerere_path(const char *hex, const char *file);extern int has_rerere_resolution (const char *hex);extern int rerere_forget (const char **);extern int rerere_remaining (struct string_list *); +extern void rerere_clear (struct string_list *); +extern void rerere_gc (struct string_list *);#define OPT_RERERE_AUTOUPDATE (v) OPT_UYN(0, "rerere-autoupdate", (v), \ "update the index with reused conflict resolution if possible")
Signature · 1 line
1.7.5.1.268.gce5bd
Hi Junio,
Quoted 12 lines · Junio C Hamano
Quoted 9 lines · Ramkumar Ramachandra
Libify the "rerere clear" into a simple function called
rerere_clearthat takes no arguments, and returns the exit status. Also exportunlink_rr_itemasunlink_rerere_itemsorerere_clearand the un-libified "git rerere gc" can both use it.Helped-by Jonathan Nieder <[email protected]>Signed-off-by Ramkumar Ramachandra <[email protected]>---
What changed since v2: Jonathan's review.
Quoted 7 lines
Are you sure this is the version you wanted to send?
You now return -1 from
rerere_clear()whensetup_rerere()says that the feature is not enabled, and this is propagated back tocmd_rerere(), causing the whole command to report a failure in its exit status, which seems to me a grave regression. Your previous round got this part right, but it is broken in this round.
Ugh, I'm not sure how this change crept in- sorry :| Could you please squash this diff into the patch?
rerere.c +1 -1
@@ -687,7 +687,7 @@ int rerere_clear(void) fd =setup_rerere (&merge_rr,0 );if (fd <0 ) -return -1 ; +return 0 ;for (i =0 ; i < merge_rr.nr; i++) {const char *name = (const char *)merge_rr.items[i].util;if (!has_rerere_resolution (name))
Quoted 8 lines
Also I seem to recall that Jonathan suggested that you do not have to expose
unlink_rr_item()as an external symbol if you moved the garbage collection part frombuiltin/rerere.ctorerere.cbut I do not see such a change in this patch. I think the gc interface is a lot more reasonable API to expose to external callers ("git gc" may want to make an internal call torerere_gc()moved torerere.c, instead of spawning "git rerere gc" as an external command) thanunlink_rerere_item()that is only useful for callers that are deep inside rerere specific codepath.
I'll quote Jonathan from the previous review:
"
I think the reason for this is that rerere_gc is not being exposed at the same time, right? I suppose if I were doing it, I would have moved that to rerere.c, too and kept unlink_rr_item static, but there is also appeal in a minimal patch. It would be clearer to say something to the effect that we
Also export unlink_rr_item as unlink_rerere_item so rerere_clear and the un-libified "git rerere gc" can both use it.
"
To the first part of the question: yes, that's the reason for exposing unlink_rr_item as unlink_rerere_item. Yet, I followed the latter approach for the appeal of the minimal patch -- I should have said this explicitly. Anyway, I plan to post another patch cleaning up and libifying rerere shortly.
Junio: If you feel that garbage_collect should be exposed in this patch, I'll post an alternative version now, and you can pick the one you like :)
-- Ram