Skip to content

Commit e251a7b

Browse files
committed
Merge branch 'mh/status-optionally-refresh'
* mh/status-optionally-refresh: t7508: add a test for "git status" in a read-only repository git status: refresh the index if possible t7508: add test for "git status" refreshing the index
2 parents c58c512 + b2f6fd9 commit e251a7b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

builtin/commit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
10171017
int cmd_status(int argc, const char **argv, const char *prefix)
10181018
{
10191019
struct wt_status s;
1020+
int fd;
10201021
unsigned char sha1[20];
10211022
static struct option builtin_status_options[] = {
10221023
OPT__VERBOSE(&verbose),
@@ -1050,6 +1051,14 @@ int cmd_status(int argc, const char **argv, const char *prefix)
10501051

10511052
read_cache_preload(s.pathspec);
10521053
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
1054+
1055+
fd = hold_locked_index(&index_lock, 0);
1056+
if (0 <= fd) {
1057+
if (!write_cache(fd, active_cache, active_nr))
1058+
commit_locked_index(&index_lock);
1059+
rollback_lock_file(&index_lock);
1060+
}
1061+
10531062
s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
10541063
s.in_merge = in_merge;
10551064
wt_status_collect(&s);

t/t7508-status.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,16 @@ test_expect_success 'dry-run of partial commit excluding new file in index' '
496496
test_cmp expect output
497497
'
498498

499+
cat >expect <<EOF
500+
:100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M dir1/modified
501+
EOF
502+
test_expect_success 'status refreshes the index' '
503+
touch dir2/added &&
504+
git status &&
505+
git diff-files >output &&
506+
test_cmp expect output
507+
'
508+
499509
test_expect_success 'setup status submodule summary' '
500510
test_create_repo sm && (
501511
cd sm &&
@@ -693,4 +703,19 @@ test_expect_success 'commit --dry-run submodule summary (--amend)' '
693703
test_cmp expect output
694704
'
695705

706+
test_expect_success POSIXPERM 'status succeeds in a read-only repository' '
707+
(
708+
chmod a-w .git &&
709+
# make dir1/tracked stat-dirty
710+
>dir1/tracked1 && mv -f dir1/tracked1 dir1/tracked &&
711+
git status -s >output &&
712+
! grep dir1/tracked output &&
713+
# make sure "status" succeeded without writing index out
714+
git diff-files | grep dir1/tracked
715+
)
716+
status=$?
717+
chmod 775 .git
718+
(exit $status)
719+
'
720+
696721
test_done

0 commit comments

Comments
 (0)