Skip to content

Commit 6f12736

Browse files
committed
Make the require_clean_work_tree() function truly reusable
It is remarkable that libgit.a did not sport this function yet... 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>
1 parent f8094a2 commit 6f12736

3 files changed

Lines changed: 77 additions & 74 deletions

File tree

builtin/pull.c

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "revision.h"
1818
#include "tempfile.h"
1919
#include "lockfile.h"
20+
#include "wt-status.h"
2021

2122
enum rebase_type {
2223
REBASE_INVALID = -1,
@@ -305,80 +306,6 @@ static enum rebase_type config_get_rebase(void)
305306
return REBASE_FALSE;
306307
}
307308

308-
/**
309-
* Returns 1 if there are unstaged changes, 0 otherwise.
310-
*/
311-
static int has_unstaged_changes()
312-
{
313-
struct rev_info rev_info;
314-
int result;
315-
316-
init_revisions(&rev_info, NULL);
317-
DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
318-
DIFF_OPT_SET(&rev_info.diffopt, QUICK);
319-
diff_setup_done(&rev_info.diffopt);
320-
result = run_diff_files(&rev_info, 0);
321-
return diff_result_code(&rev_info.diffopt, result);
322-
}
323-
324-
/**
325-
* Returns 1 if there are uncommitted changes, 0 otherwise.
326-
*/
327-
static int has_uncommitted_changes()
328-
{
329-
struct rev_info rev_info;
330-
int result;
331-
332-
if (is_cache_unborn())
333-
return 0;
334-
335-
init_revisions(&rev_info, NULL);
336-
DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
337-
DIFF_OPT_SET(&rev_info.diffopt, QUICK);
338-
add_head_to_pending(&rev_info);
339-
diff_setup_done(&rev_info.diffopt);
340-
result = run_diff_index(&rev_info, 1);
341-
return diff_result_code(&rev_info.diffopt, result);
342-
}
343-
344-
/**
345-
* If the work tree has unstaged or uncommitted changes, dies with the
346-
* appropriate message.
347-
*/
348-
static int require_clean_work_tree(const char *action, const char *hint,
349-
int gently)
350-
{
351-
struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
352-
int err = 0;
353-
354-
hold_locked_index(lock_file, 0);
355-
refresh_cache(REFRESH_QUIET);
356-
update_index_if_able(&the_index, lock_file);
357-
rollback_lock_file(lock_file);
358-
359-
if (has_unstaged_changes()) {
360-
error(_("Cannot %s: You have unstaged changes."), action);
361-
err = 1;
362-
}
363-
364-
if (has_uncommitted_changes()) {
365-
if (err)
366-
error(_("Additionally, your index contains uncommitted changes."));
367-
else
368-
error(_("Cannot %s: Your index contains uncommitted changes."), action);
369-
err = 1;
370-
}
371-
372-
if (err) {
373-
if (hint)
374-
error("%s", hint);
375-
if (!gently)
376-
exit(err);
377-
}
378-
379-
return err;
380-
}
381-
382309
/**
383310
* Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
384311
* into merge_heads.

wt-status.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "column.h"
1616
#include "strbuf.h"
1717
#include "utf8.h"
18+
#include "lockfile.h"
1819

1920
static const char cut_line[] =
2021
"------------------------ >8 ------------------------\n";
@@ -1731,3 +1732,76 @@ void wt_porcelain_print(struct wt_status *s)
17311732
s->no_gettext = 1;
17321733
wt_shortstatus_print(s);
17331734
}
1735+
1736+
/**
1737+
* Returns 1 if there are unstaged changes, 0 otherwise.
1738+
*/
1739+
static int has_unstaged_changes()
1740+
{
1741+
struct rev_info rev_info;
1742+
int result;
1743+
1744+
init_revisions(&rev_info, NULL);
1745+
DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
1746+
DIFF_OPT_SET(&rev_info.diffopt, QUICK);
1747+
diff_setup_done(&rev_info.diffopt);
1748+
result = run_diff_files(&rev_info, 0);
1749+
return diff_result_code(&rev_info.diffopt, result);
1750+
}
1751+
1752+
/**
1753+
* Returns 1 if there are uncommitted changes, 0 otherwise.
1754+
*/
1755+
static int has_uncommitted_changes()
1756+
{
1757+
struct rev_info rev_info;
1758+
int result;
1759+
1760+
if (is_cache_unborn())
1761+
return 0;
1762+
1763+
init_revisions(&rev_info, NULL);
1764+
DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
1765+
DIFF_OPT_SET(&rev_info.diffopt, QUICK);
1766+
add_head_to_pending(&rev_info);
1767+
diff_setup_done(&rev_info.diffopt);
1768+
result = run_diff_index(&rev_info, 1);
1769+
return diff_result_code(&rev_info.diffopt, result);
1770+
}
1771+
1772+
/**
1773+
* If the work tree has unstaged or uncommitted changes, dies with the
1774+
* appropriate message.
1775+
*/
1776+
int require_clean_work_tree(const char *action, const char *hint, int gently)
1777+
{
1778+
struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
1779+
int err = 0;
1780+
1781+
hold_locked_index(lock_file, 0);
1782+
refresh_cache(REFRESH_QUIET);
1783+
update_index_if_able(&the_index, lock_file);
1784+
rollback_lock_file(lock_file);
1785+
1786+
if (has_unstaged_changes()) {
1787+
error(_("Cannot %s: You have unstaged changes."), action);
1788+
err = 1;
1789+
}
1790+
1791+
if (has_uncommitted_changes()) {
1792+
if (err)
1793+
error(_("Additionally, your index contains uncommitted changes."));
1794+
else
1795+
error(_("Cannot %s: Your index contains uncommitted changes."), action);
1796+
err = 1;
1797+
}
1798+
1799+
if (err) {
1800+
if (hint)
1801+
error("%s", hint);
1802+
if (!gently)
1803+
exit(err);
1804+
}
1805+
1806+
return err;
1807+
}

wt-status.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, .
109109
__attribute__((format (printf, 3, 4)))
110110
void status_printf(struct wt_status *s, const char *color, const char *fmt, ...);
111111

112+
int require_clean_work_tree(const char *action, const char *hint, int gently);
113+
112114
#endif /* STATUS_H */

0 commit comments

Comments
 (0)