fix bug in concurrent commit check and add another commit test
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
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 50c0ae7..6986c3a 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -73,9 +73,8 @@ const struct got_error *got_worktree_match_path_prefix(int *,
/*
* Get the name of a work tree's HEAD reference.
- * The caller must dispose of it with free(3).
*/
-char *got_worktree_get_head_ref_name(struct got_worktree *);
+const char *got_worktree_get_head_ref_name(struct got_worktree *);
/*
* Get the work tree's HEAD reference.
diff --git a/lib/worktree.c b/lib/worktree.c
index 4523727..10ad383 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -512,10 +512,10 @@ got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
return NULL;
}
-char *
+const char *
got_worktree_get_head_ref_name(struct got_worktree *worktree)
{
- return got_ref_to_str(worktree->head_ref);
+ return got_ref_get_name(worktree->head_ref);
}
struct got_reference *
@@ -2738,7 +2738,8 @@ got_worktree_commit(struct got_object_id **new_commit_id,
struct collect_commitables_arg cc_arg;
struct got_pathlist_head commitable_paths;
struct got_pathlist_entry *pe;
- char *relpath = NULL, *head_ref_str = NULL;
+ char *relpath = NULL;
+ const char *head_ref_name = NULL;
struct got_commit_object *base_commit = NULL;
struct got_object_id *head_commit_id = NULL;
struct got_reference *head_ref2 = NULL;
@@ -2827,12 +2828,12 @@ got_worktree_commit(struct got_object_id **new_commit_id,
/* Check if a concurrent commit to our branch has occurred. */
/* XXX ideally we'd lock the reference file here to avoid a race */
- head_ref_str = got_ref_to_str(worktree->head_ref);
- if (head_ref_str == NULL) {
+ head_ref_name = got_worktree_get_head_ref_name(worktree);
+ if (head_ref_name == NULL) {
err = got_error_from_errno();
goto done;
}
- err = got_ref_open(&head_ref2, repo, head_ref_str);
+ err = got_ref_open(&head_ref2, repo, head_ref_name);
if (err)
goto done;
err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
@@ -2879,7 +2880,6 @@ done:
free(relpath);
free(head_commit_id);
free(head_commit_id2);
- free(head_ref_str);
if (head_ref2)
got_ref_close(head_ref2);
return err;
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index e378888..30da797 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -82,5 +82,34 @@ function test_commit_new_subdir {
test_done "$testroot" "$ret"
}
+function test_commit_subdir {
+ local testroot=`test_init commit_subdir`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified alpha" > $testroot/wt/alpha
+ echo "modified zeta" > $testroot/wt/epsilon/zeta
+
+ (cd $testroot/wt && \
+ got commit -m 'test commit_subdir' epsilon > $testroot/stdout)
+
+ local head_rev=`git_show_head $testroot/repo`
+ echo "M epsilon/zeta" >> $testroot/stdout.expected
+ echo "created commit $head_rev" >> $testroot/stdout.expected
+
+ cmp $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_commit_basic
run_test test_commit_new_subdir
+run_test test_commit_subdir