aboutsummaryrefslogtreecommitdiff
path: root/wt-status.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2016-10-26 13:14:44 -0700
committerJunio C Hamano <gitster@pobox.com>2016-10-26 13:14:44 -0700
commit2bee56be7e241db75774fc6a7a235b8125936943 (patch)
tree091eaf666d5e2cb8c363af949ca7c5389c823f1f /wt-status.c
parente5272d304af3528163cd5faa822f88086448ae57 (diff)
parent4777e175de439b7ea0c435de4bb0de9ab1663c38 (diff)
downloadgit-2bee56be7e241db75774fc6a7a235b8125936943.tar.gz
git-2bee56be7e241db75774fc6a7a235b8125936943.tar.xz
Merge branch 'js/libify-require-clean-work-tree'
The require_clean_work_tree() helper was recreated in C when "git pull" was rewritten from shell; the helper is now made available to other callers in preparation for upcoming "rebase -i" work. * js/libify-require-clean-work-tree: wt-status: begin error messages with lower-case wt-status: teach has_{unstaged,uncommitted}_changes() about submodules wt-status: export also the has_un{staged,committed}_changes() functions wt-status: make the require_clean_work_tree() function reusable pull: make code more similar to the shell script again pull: drop confusing prefix parameter of die_on_unclean_work_tree()
Diffstat (limited to 'wt-status.c')
-rw-r--r--wt-status.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/wt-status.c b/wt-status.c
index ca5c45f94..0bd278122 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -16,6 +16,7 @@
#include "strbuf.h"
#include "utf8.h"
#include "worktree.h"
+#include "lockfile.h"
static const char cut_line[] =
"------------------------ >8 ------------------------\n";
@@ -2206,3 +2207,80 @@ void wt_status_print(struct wt_status *s)
break;
}
}
+
+/**
+ * Returns 1 if there are unstaged changes, 0 otherwise.
+ */
+int has_unstaged_changes(int ignore_submodules)
+{
+ struct rev_info rev_info;
+ int result;
+
+ init_revisions(&rev_info, NULL);
+ if (ignore_submodules)
+ 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.
+ */
+int has_uncommitted_changes(int ignore_submodules)
+{
+ struct rev_info rev_info;
+ int result;
+
+ if (is_cache_unborn())
+ return 0;
+
+ init_revisions(&rev_info, NULL);
+ if (ignore_submodules)
+ 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 ignore_submodules, 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(ignore_submodules)) {
+ /* TRANSLATORS: the action is e.g. "pull with rebase" */
+ error(_("cannot %s: You have unstaged changes."), _(action));
+ err = 1;
+ }
+
+ if (has_uncommitted_changes(ignore_submodules)) {
+ 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;
+}