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()
This commit is contained in:
Junio C Hamano
2016-10-26 13:14:44 -07:00
3 changed files with 87 additions and 68 deletions

View File

@ -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;
}