make 'got fetch' update our copy of the remote HEAD in case it has changed
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
diff --git a/got/got.c b/got/got.c
index b02072b..96e6346 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1502,6 +1502,41 @@ done:
return err;
}
+static const struct got_error *
+update_symref(const char *refname, struct got_reference *target_ref,
+ int verbosity, struct got_repository *repo)
+{
+ const struct got_error *err = NULL, *unlock_err;
+ struct got_reference *symref;
+
+ err = got_ref_open(&symref, repo, refname, 1);
+ if (err)
+ return err;
+
+ if (strcmp(got_ref_get_symref_target(symref),
+ got_ref_get_name(target_ref)) == 0)
+ goto done;
+
+ err = got_ref_change_symref(symref, got_ref_get_name(target_ref));
+ if (err)
+ goto done;
+
+ err = got_ref_write(symref, repo);
+ if (err)
+ goto done;
+
+ if (verbosity >= 0)
+ printf("Updated reference %s: %s\n", got_ref_get_name(symref),
+ got_ref_get_symref_target(symref));
+done:
+ unlock_err = got_ref_unlock(symref);
+ if (unlock_err && err == NULL)
+ err = unlock_err;
+ got_ref_close(symref);
+ return err;
+ return NULL;
+}
+
__dead static void
usage_fetch(void)
{
@@ -1895,8 +1930,58 @@ cmd_fetch(int argc, char *argv[])
}
}
}
- if (delete_refs)
+ if (delete_refs) {
error = delete_missing_refs(&refs, verbosity, repo);
+ if (error)
+ goto done;
+ }
+
+ if (!remote->mirror_references) {
+ /* Update remote HEAD reference if the server provided one. */
+ TAILQ_FOREACH(pe, &symrefs, entry) {
+ struct got_reference *target_ref;
+ const char *refname = pe->path;
+ const char *target = pe->data;
+ char *remote_refname = NULL, *remote_target = NULL;
+
+ if (strcmp(refname, GOT_REF_HEAD) != 0)
+ continue;
+
+ if (strncmp("refs/heads/", target, 11) != 0)
+ continue;
+
+ if (asprintf(&remote_refname, "refs/remotes/%s/%s",
+ remote->name, refname) == -1) {
+ error = got_error_from_errno("asprintf");
+ goto done;
+ }
+ if (asprintf(&remote_target, "refs/remotes/%s/%s",
+ remote->name, target + 11) == -1) {
+ error = got_error_from_errno("asprintf");
+ free(remote_refname);
+ goto done;
+ }
+
+ error = got_ref_open(&target_ref, repo, remote_target,
+ 0);
+ if (error) {
+ free(remote_refname);
+ free(remote_target);
+ if (error->code == GOT_ERR_NOT_REF) {
+ error = NULL;
+ continue;
+ }
+ goto done;
+ }
+ error = update_symref(remote_refname, target_ref,
+ verbosity, repo);
+ free(remote_refname);
+ free(remote_target);
+ got_ref_close(target_ref);
+ if (error)
+ goto done;
+ }
+ }
done:
if (fetchpid > 0) {
if (kill(fetchpid, SIGTERM) == -1)
diff --git a/regress/cmdline/fetch.sh b/regress/cmdline/fetch.sh
index 404c994..e4a422f 100755
--- a/regress/cmdline/fetch.sh
+++ b/regress/cmdline/fetch.sh
@@ -207,7 +207,7 @@ function test_fetch_branch {
echo "HEAD: refs/heads/master" > $testroot/stdout.expected
echo "refs/heads/foo: $commit_id3" >> $testroot/stdout.expected
echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
- echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+ echo "refs/remotes/origin/HEAD: refs/remotes/origin/foo" \
>> $testroot/stdout.expected
echo "refs/remotes/origin/foo: $commit_id3" >> $testroot/stdout.expected
# refs/remotes/origin/master is umodified because it wasn't fetched
@@ -247,7 +247,7 @@ function test_fetch_branch {
echo "HEAD: refs/heads/master" > $testroot/stdout.expected
echo "refs/heads/foo: $commit_id3" >> $testroot/stdout.expected
echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
- echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+ echo "refs/remotes/origin/HEAD: refs/remotes/origin/foo" \
>> $testroot/stdout.expected
echo "refs/remotes/origin/foo: $commit_id3" >> $testroot/stdout.expected
echo "refs/remotes/origin/master: $commit_id2" \
@@ -823,6 +823,75 @@ function test_fetch_replace_symref {
}
+function test_fetch_update_headref {
+ local testroot=`test_init fetch_update_headref`
+ local testurl=ssh://127.0.0.1/$testroot
+ local commit_id=`git_show_head $testroot/repo`
+
+ got clone -q $testurl/repo $testroot/repo-clone
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got clone command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/master: $commit_id" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -r $testroot/repo -c refs/heads/master refs/heads/foo
+ got ref -r $testroot/repo -s refs/heads/foo HEAD
+ got ref -l -r $testroot/repo > $testroot/stdout
+
+ echo "HEAD: refs/heads/foo" > $testroot/stdout.expected
+ echo "refs/heads/foo: $commit_id" >> $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got fetch -q -r $testroot/repo-clone
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/foo: $commit_id" >> $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/HEAD: refs/remotes/origin/foo" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/foo: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/master: $commit_id" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+
+}
+
run_test test_fetch_basic
run_test test_fetch_list
run_test test_fetch_branch
@@ -832,3 +901,4 @@ run_test test_fetch_delete_branch
run_test test_fetch_update_tag
run_test test_fetch_reference
run_test test_fetch_replace_symref
+run_test test_fetch_update_headref