fix out-of-date check regression; only commit staged files after 'got stage'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
diff --git a/include/got_error.h b/include/got_error.h
index 0a2e9fc..cbf9d06 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -118,6 +118,7 @@
#define GOT_ERR_STAGE_NO_CHANGE 102
#define GOT_ERR_STAGE_CONFLICT 103
#define GOT_ERR_STAGE_OUT_OF_DATE 104
+#define GOT_ERR_FILE_NOT_STAGED 105
static const struct got_error {
int code;
@@ -239,6 +240,7 @@ static const struct got_error {
{ GOT_ERR_STAGE_CONFLICT, "cannot stage file in conflicted status" },
{ GOT_ERR_STAGE_OUT_OF_DATE, "work tree must be updated before "
"changes can be staged" },
+ { GOT_ERR_FILE_NOT_STAGED, "file is not staged" },
};
/*
diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index f7db624..c45a958 100644
--- a/lib/got_lib_worktree.h
+++ b/lib/got_lib_worktree.h
@@ -40,8 +40,10 @@ struct got_commitable {
char *in_repo_path;
char *ondisk_path;
unsigned char status;
+ unsigned char staged_status;
struct got_object_id *blob_id;
struct got_object_id *base_blob_id;
+ struct got_object_id *staged_blob_id;
struct got_object_id *base_commit_id;
mode_t mode;
int flags;
diff --git a/lib/worktree.c b/lib/worktree.c
index 0115f06..0f55d72 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2843,6 +2843,7 @@ free_commitable(struct got_commitable *ct)
free(ct->ondisk_path);
free(ct->blob_id);
free(ct->base_blob_id);
+ free(ct->staged_blob_id);
free(ct->base_commit_id);
free(ct);
}
@@ -2929,6 +2930,15 @@ collect_commitables(void *arg, unsigned char status,
goto done;
}
}
+ ct->staged_status = staged_status;
+ if (ct->staged_status == GOT_STATUS_ADD ||
+ ct->staged_status == GOT_STATUS_MODIFY) {
+ ct->staged_blob_id = got_object_id_dup(staged_blob_id);
+ if (ct->staged_blob_id == NULL) {
+ err = got_error_from_errno("got_object_id_dup");
+ goto done;
+ }
+ }
ct->path = strdup(path);
if (ct->path == NULL) {
err = got_error_from_errno("strdup");
@@ -3414,20 +3424,6 @@ done:
return err;
}
-static const struct got_error *
-check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
- struct got_object_id *head_commit_id)
-{
- const char *ct_path = ct->in_repo_path;
-
- while (ct_path[0] == '/')
- ct_path++;
-
- return check_out_of_date(ct_path, ct->status, ct->base_commit_id,
- ct->base_commit_id, head_commit_id, repo,
- GOT_ERR_COMMIT_OUT_OF_DATE);
-}
-
const struct got_error *
commit_worktree(struct got_object_id **new_commit_id,
struct got_pathlist_head *commitable_paths,
@@ -3582,6 +3578,19 @@ check_path_is_commitable(const char *path,
return NULL;
}
+static const struct got_error *
+check_staged_file(void *arg, struct got_fileindex_entry *ie)
+{
+ int *have_staged_files = arg;
+
+ if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
+ *have_staged_files = 1;
+ return got_error(GOT_ERR_CANCELLED);
+ }
+
+ return NULL;
+}
+
const struct got_error *
got_worktree_commit(struct got_object_id **new_commit_id,
struct got_worktree *worktree, struct got_pathlist_head *paths,
@@ -3598,6 +3607,7 @@ got_worktree_commit(struct got_object_id **new_commit_id,
struct got_pathlist_entry *pe;
struct got_reference *head_ref = NULL;
struct got_object_id *head_commit_id = NULL;
+ int have_staged_files = 0;
*new_commit_id = NULL;
@@ -3640,11 +3650,28 @@ got_worktree_commit(struct got_object_id **new_commit_id,
goto done;
}
+ err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
+ &have_staged_files);
+ if (err && err->code != GOT_ERR_CANCELLED)
+ goto done;
+
TAILQ_FOREACH(pe, &commitable_paths, entry) {
struct got_commitable *ct = pe->data;
- err = check_ct_out_of_date(ct, repo, head_commit_id);
+ const char *ct_path = ct->in_repo_path;
+
+ while (ct_path[0] == '/')
+ ct_path++;
+ err = check_out_of_date(ct_path, ct->status,
+ ct->base_blob_id, ct->base_commit_id, head_commit_id,
+ repo, GOT_ERR_COMMIT_OUT_OF_DATE);
if (err)
goto done;
+ if (have_staged_files &&
+ ct->staged_status == GOT_STATUS_NO_CHANGE) {
+ err = got_error_path(ct_path, GOT_ERR_FILE_NOT_STAGED);
+ goto done;
+ }
+
}
err = commit_worktree(new_commit_id, &commitable_paths,
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index 55bad57..385f2ae 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -137,6 +137,7 @@ function test_commit_single_file {
function test_commit_out_of_date {
local testroot=`test_init commit_out_of_date`
+ local first_commit=`git_show_head $testroot/repo`
got checkout $testroot/repo $testroot/wt > /dev/null
ret="$?"
@@ -153,7 +154,6 @@ function test_commit_out_of_date {
(cd $testroot/wt && got commit -m 'test commit_out_of_date' \
> $testroot/stdout 2> $testroot/stderr)
- local head_rev=`git_show_head $testroot/repo`
echo -n > $testroot/stdout.expected
echo "got: work tree must be updated before these" \
"changes can be committed" > $testroot/stderr.expected
@@ -170,6 +170,31 @@ function test_commit_out_of_date {
ret="$?"
if [ "$ret" != "0" ]; then
diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "alpha" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "reset alpha contents"
+ (cd $testroot/wt && got update -c $first_commit > /dev/null)
+
+ echo "modified alpha" > $testroot/wt/alpha
+
+ (cd $testroot/wt && got commit -m 'changed alpha ' > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "commit failed unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ local head_rev=`git_show_head $testroot/repo`
+ echo "M alpha" > $testroot/stdout.expected
+ echo "Created commit $head_rev" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
fi
test_done "$testroot" "$ret"
}
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 4d057d7..61e10ca 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -895,6 +895,50 @@ function test_stage_update {
test_done "$testroot" "$ret"
}
+function test_stage_commit_non_staged {
+ local testroot=`test_init stage_commit_non_staged`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified file" > $testroot/wt/alpha
+ (cd $testroot/wt && got rm beta > /dev/null)
+ echo "new file" > $testroot/wt/foo
+ (cd $testroot/wt && got add foo > /dev/null)
+ (cd $testroot/wt && got stage alpha beta foo > /dev/null)
+
+ echo "modified file" > $testroot/wt/gamma/delta
+ (cd $testroot/wt && got commit -m "change delta" gamma/delta \
+ > $testroot/stdout 2> $testroot/stderr)
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got commit command succeeded unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo -n > $testroot/stdout.expected
+ echo "got: gamma/delta: file is not staged" > $testroot/stderr.expected
+
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_stage_basic
run_test test_stage_conflict
run_test test_stage_out_of_date
@@ -907,3 +951,4 @@ run_test test_stage_diff
run_test test_stage_histedit
run_test test_stage_rebase
run_test test_stage_update
+run_test test_stage_commit_non_staged