wt-status: make the require_clean_work_tree() function reusable
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 7 Oct 2016 16:08:38 +0000 (18:08 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 7 Oct 2016 16:29:31 +0000 (09:29 -0700)
The function used by "git pull" to stop the user when the working
tree has changes is useful in other places.

Let's move it into a more prominent (and into an actually reusable)
spot: wt-status.[ch].

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/pull.c
wt-status.c
wt-status.h
index 58fc176db681298bc6daf14b4cc09752d3b9bccc..01b64654c5c017dd15dd98a05f750b1e723065cc 100644 (file)
@@ -17,6 +17,7 @@
 #include "revision.h"
 #include "tempfile.h"
 #include "lockfile.h"
+#include "wt-status.h"
 
 enum rebase_type {
        REBASE_INVALID = -1,
@@ -325,82 +326,6 @@ static int git_pull_config(const char *var, const char *value, void *cb)
        return git_default_config(var, value, cb);
 }
 
-/**
- * Returns 1 if there are unstaged changes, 0 otherwise.
- */
-static int has_unstaged_changes(void)
-{
-       struct rev_info rev_info;
-       int result;
-
-       init_revisions(&rev_info, NULL);
-       DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
-       DIFF_OPT_SET(&rev_info.diffopt, QUICK);
-       diff_setup_done(&rev_info.diffopt);
-       result = run_diff_files(&rev_info, 0);
-       return diff_result_code(&rev_info.diffopt, result);
-}
-
-/**
- * Returns 1 if there are uncommitted changes, 0 otherwise.
- */
-static int has_uncommitted_changes(void)
-{
-       struct rev_info rev_info;
-       int result;
-
-       if (is_cache_unborn())
-               return 0;
-
-       init_revisions(&rev_info, NULL);
-       DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
-       DIFF_OPT_SET(&rev_info.diffopt, QUICK);
-       add_head_to_pending(&rev_info);
-       diff_setup_done(&rev_info.diffopt);
-       result = run_diff_index(&rev_info, 1);
-       return diff_result_code(&rev_info.diffopt, result);
-}
-
-/**
- * If the work tree has unstaged or uncommitted changes, dies with the
- * appropriate message.
- */
-static int require_clean_work_tree(const char *action, const char *hint,
-               int gently)
-{
-       struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
-       int err = 0;
-
-       hold_locked_index(lock_file, 0);
-       refresh_cache(REFRESH_QUIET);
-       update_index_if_able(&the_index, lock_file);
-       rollback_lock_file(lock_file);
-
-       if (has_unstaged_changes()) {
-               /* TRANSLATORS: the action is e.g. "pull with rebase" */
-               error(_("Cannot %s: You have unstaged changes."), _(action));
-               err = 1;
-       }
-
-       if (has_uncommitted_changes()) {
-               if (err)
-                       error(_("Additionally, your index contains uncommitted changes."));
-               else
-                       error(_("Cannot %s: Your index contains uncommitted changes."),
-                             _(action));
-               err = 1;
-       }
-
-       if (err) {
-               if (hint)
-                       error("%s", hint);
-               if (!gently)
-                       exit(128);
-       }
-
-       return err;
-}
-
 /**
  * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
  * into merge_heads.
index 539aac15a37adcce6429c4d82f401a7db17fac28..4f9b5130df91b34707efaa2c3d61392332a82d51 100644 (file)
@@ -16,6 +16,7 @@
 #include "strbuf.h"
 #include "utf8.h"
 #include "worktree.h"
+#include "lockfile.h"
 
 static const char cut_line[] =
 "------------------------ >8 ------------------------\n";
@@ -2209,3 +2210,78 @@ void wt_status_print(struct wt_status *s)
                break;
        }
 }
+
+/**
+ * Returns 1 if there are unstaged changes, 0 otherwise.
+ */
+static int has_unstaged_changes(void)
+{
+       struct rev_info rev_info;
+       int result;
+
+       init_revisions(&rev_info, NULL);
+       DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
+       DIFF_OPT_SET(&rev_info.diffopt, QUICK);
+       diff_setup_done(&rev_info.diffopt);
+       result = run_diff_files(&rev_info, 0);
+       return diff_result_code(&rev_info.diffopt, result);
+}
+
+/**
+ * Returns 1 if there are uncommitted changes, 0 otherwise.
+ */
+static int has_uncommitted_changes(void)
+{
+       struct rev_info rev_info;
+       int result;
+
+       if (is_cache_unborn())
+               return 0;
+
+       init_revisions(&rev_info, NULL);
+       DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
+       DIFF_OPT_SET(&rev_info.diffopt, QUICK);
+       add_head_to_pending(&rev_info);
+       diff_setup_done(&rev_info.diffopt);
+       result = run_diff_index(&rev_info, 1);
+       return diff_result_code(&rev_info.diffopt, result);
+}
+
+/**
+ * If the work tree has unstaged or uncommitted changes, dies with the
+ * appropriate message.
+ */
+int require_clean_work_tree(const char *action, const char *hint, int gently)
+{
+       struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
+       int err = 0;
+
+       hold_locked_index(lock_file, 0);
+       refresh_cache(REFRESH_QUIET);
+       update_index_if_able(&the_index, lock_file);
+       rollback_lock_file(lock_file);
+
+       if (has_unstaged_changes()) {
+               /* TRANSLATORS: the action is e.g. "pull with rebase" */
+               error(_("Cannot %s: You have unstaged changes."), _(action));
+               err = 1;
+       }
+
+       if (has_uncommitted_changes()) {
+               if (err)
+                       error(_("Additionally, your index contains uncommitted changes."));
+               else
+                       error(_("Cannot %s: Your index contains uncommitted changes."),
+                             _(action));
+               err = 1;
+       }
+
+       if (err) {
+               if (hint)
+                       error("%s", hint);
+               if (!gently)
+                       exit(128);
+       }
+
+       return err;
+}
index e40183770700cb593d4e4f2bf914a5b6eff0bb09..68b4709835c825222f08775107c74a131cfc9dcf 100644 (file)
@@ -128,4 +128,7 @@ void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, .
 __attribute__((format (printf, 3, 4)))
 void status_printf(struct wt_status *s, const char *color, const char *fmt, ...);
 
+/* The following function expects that the caller took care of reading the index. */
+int require_clean_work_tree(const char *action, const char *hint, int gently);
+
 #endif /* STATUS_H */