2811
return 0;
2812
}
2813
2814
-int create_symref(const char *ref_target, const char *refs_heads_master,
2815
- const char *logmsg)
2814
+int create_symref(const char *refname, const char *target, const char *logmsg)
2815
{
2816
char *lockpath = NULL;
2818
- char ref[1000];
2817
+ char buf[1000];
2818
int fd, len, written;
2820
- char *git_HEAD = git_pathdup("%s", ref_target);
2819
+ char *ref_path = git_pathdup("%s", refname);
2820
unsigned char old_sha1[20], new_sha1[20];
2821
struct strbuf err = STRBUF_INIT;
2822
2824
- if (logmsg && read_ref(ref_target, old_sha1))
2823
+ if (logmsg && read_ref(refname, old_sha1))
2824
hashclr(old_sha1);
2825
2827
- if (safe_create_leading_directories(git_HEAD) < 0)
2828
- return error("unable to create directory for %s", git_HEAD);
2826
+ if (safe_create_leading_directories(ref_path) < 0)
2827
+ return error("unable to create directory for %s", ref_path);
2828
2829
#ifndef NO_SYMLINK_HEAD
2830
if (prefer_symlink_refs) {
2832
- unlink(git_HEAD);
2833
- if (!symlink(refs_heads_master, git_HEAD))
2831
+ unlink(ref_path);
2832
+ if (!symlink(target, ref_path))
2833
goto done;
2834
fprintf(stderr, "no symlink - falling back to symbolic ref\n");
2835
}
2836
#endif
2837
2839
- len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
2840
- if (sizeof(ref) <= len) {
2841
- error("refname too long: %s", refs_heads_master);
2838
+ len = snprintf(buf, sizeof(buf), "ref: %s\n", target);
2839
+ if (sizeof(buf) <= len) {
2840
+ error("refname too long: %s", target);
2841
goto error_free_return;
2842
}
2844
- lockpath = mkpathdup("%s.lock", git_HEAD);
2843
+ lockpath = mkpathdup("%s.lock", ref_path);
2844
fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
2845
if (fd < 0) {
2846
error("Unable to open %s for writing", lockpath);
2847
goto error_free_return;
2848
}
2850
- written = write_in_full(fd, ref, len);
2849
+ written = write_in_full(fd, buf, len);
2850
if (close(fd) != 0 || written != len) {
2851
error("Unable to write to %s", lockpath);
2852
goto error_unlink_return;
2853
}
2855
- if (rename(lockpath, git_HEAD) < 0) {
2856
- error("Unable to create %s", git_HEAD);
2854
+ if (rename(lockpath, ref_path) < 0) {
2855
+ error("Unable to create %s", ref_path);
2856
goto error_unlink_return;
2857
}
2859
- if (adjust_shared_perm(git_HEAD)) {
2858
+ if (adjust_shared_perm(ref_path)) {
2859
error("Unable to fix permissions on %s", lockpath);
2860
error_unlink_return:
2861
unlink_or_warn(lockpath);
2862
error_free_return:
2863
free(lockpath);
2865
- free(git_HEAD);
2864
+ free(ref_path);
2865
return -1;
2866
}
2867
free(lockpath);
2869
#ifndef NO_SYMLINK_HEAD
2870
done:
2871
#endif
2873
- if (logmsg && !read_ref(refs_heads_master, new_sha1) &&
2874
- log_ref_write(ref_target, old_sha1, new_sha1, logmsg, 0, &err)) {
2872
+ if (logmsg && !read_ref(target, new_sha1) &&
2873
+ log_ref_write(refname, old_sha1, new_sha1, logmsg, 0, &err)) {
2874
error("%s", err.buf);
2875
strbuf_release(&err);
2876
}
2877
2879
- free(git_HEAD);
2878
+ free(ref_path);
2879
return 0;
2880
}
2881