Merge branch 'jk/ambiguous-short-object-names'
[gitweb.git] / wt-status.c
index 99d1b0a8180f34bb928ad92612b6a11922907e27..0bd2781225a2a9fa3e7a553aaf49112745743244 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";
@@ -1110,7 +1111,6 @@ static void abbrev_sha1_in_line(struct strbuf *line)
        split = strbuf_split_max(line, ' ', 3);
        if (split[0] && split[1]) {
                unsigned char sha1[20];
-               const char *abbrev;
 
                /*
                 * strbuf_split_max left a space. Trim it and re-add
@@ -1118,9 +1118,10 @@ static void abbrev_sha1_in_line(struct strbuf *line)
                 */
                strbuf_trim(split[1]);
                if (!get_sha1(split[1]->buf, sha1)) {
-                       abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
                        strbuf_reset(split[1]);
-                       strbuf_addf(split[1], "%s ", abbrev);
+                       strbuf_add_unique_abbrev(split[1], sha1,
+                                                DEFAULT_ABBREV);
+                       strbuf_addch(split[1], ' ');
                        strbuf_reset(line);
                        for (i = 0; split[i]; i++)
                                strbuf_addbuf(line, split[i]);
@@ -1343,10 +1344,8 @@ static char *get_branch(const struct worktree *wt, const char *path)
        else if (starts_with(sb.buf, "refs/"))
                ;
        else if (!get_sha1_hex(sb.buf, sha1)) {
-               const char *abbrev;
-               abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
                strbuf_reset(&sb);
-               strbuf_addstr(&sb, abbrev);
+               strbuf_add_unique_abbrev(&sb, sha1, DEFAULT_ABBREV);
        } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
                goto got_nothing;
        else                    /* bisect */
@@ -2208,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;
+}