is_empty_file() can help to refactor a lot of code. This will be very
helpful in porting "git bisect" to C.

Suggested-by: Torsten Bögershausen <tbo...@web.de>
Mentored-by: Lars Schneider <larsxschnei...@gmail.com>
Mentored-by: Christian Couder <chrisc...@tuxfamily.org>
Signed-off-by: Pranit Bauva <pranit.ba...@gmail.com>
---
 builtin/am.c | 20 ++------------------
 cache.h      |  3 +++
 wrapper.c    | 13 +++++++++++++
 3 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 739b34d..9e1e9d6 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -30,22 +30,6 @@
 #include "mailinfo.h"
 
 /**
- * Returns 1 if the file is empty or does not exist, 0 otherwise.
- */
-static int is_empty_file(const char *filename)
-{
-       struct stat st;
-
-       if (stat(filename, &st) < 0) {
-               if (errno == ENOENT)
-                       return 1;
-               die_errno(_("could not stat %s"), filename);
-       }
-
-       return !st.st_size;
-}
-
-/**
  * Returns the length of the first line of msg.
  */
 static int linelen(const char *msg)
@@ -1324,7 +1308,7 @@ static int parse_mail(struct am_state *state, const char 
*mail)
                goto finish;
        }
 
-       if (is_empty_file(am_path(state, "patch"))) {
+       if (is_empty_or_missing_file(am_path(state, "patch"))) {
                printf_ln(_("Patch is empty. Was it split wrong?"));
                die_user_resolve(state);
        }
@@ -1896,7 +1880,7 @@ static void am_run(struct am_state *state, int resume)
                resume = 0;
        }
 
-       if (!is_empty_file(am_path(state, "rewritten"))) {
+       if (!is_empty_or_missing_file(am_path(state, "rewritten"))) {
                assert(state->rebasing);
                copy_notes_for_rebase(state);
                run_post_rewrite_hook(state);
diff --git a/cache.h b/cache.h
index b780a91..49f214b 100644
--- a/cache.h
+++ b/cache.h
@@ -1916,4 +1916,7 @@ void sleep_millisec(int millisec);
  */
 void safe_create_dir(const char *dir, int share);
 
+/* Return 1 if the file is empty or does not exists, 0 otherwise. */
+extern int is_empty_or_missing_file(const char *filename);
+
 #endif /* CACHE_H */
diff --git a/wrapper.c b/wrapper.c
index e7f1979..78f6431 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -679,3 +679,16 @@ void sleep_millisec(int millisec)
 {
        poll(NULL, 0, millisec);
 }
+
+int is_empty_or_missing_file(const char *filename)
+{
+       struct stat st;
+
+       if (stat(filename, &st) < 0) {
+               if (errno == ENOENT)
+                       return 1;
+               die_errno(_("could not stat %s"), filename);
+       }
+
+       return !st.st_size;
+}

--
https://github.com/git/git/pull/287

Reply via email to