commit: allow editing the commit message even in shared repos
It was pointed out by Yaroslav Halchenko that the file containing the commit message is writable only by the owner, which means that we have to rewrite it from scratch in a shared repository. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jan 6, 2016 at 14:09 UTC
79d7582e32ca84eacf032298c8ae3d26816f48d0
3 files changed
+15
-1
builtin/commit.c
+1
-1
@@ -761,7 +761,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
761
hook_arg2 = "";
762
}
763
764
- s->fp = fopen(git_path(commit_editmsg), "w");
764
+ s->fp = fopen_for_writing(git_path(commit_editmsg));
765
if (s->fp == NULL)
766
die_errno(_("could not open '%s'"), git_path(commit_editmsg));
767
git-compat-util.h
+1
@@ -733,6 +733,7 @@ extern int xmkstemp_mode(char *template, int mode);
733
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
734
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
735
extern char *xgetcwd(void);
736
+extern FILE *fopen_for_writing(const char *path);
737
738
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
739
wrapper.c
+13
@@ -375,6 +375,19 @@ FILE *xfdopen(int fd, const char *mode)
375
return stream;
376
}
377
378
+FILE *fopen_for_writing(const char *path)
379
+{
380
+ FILE *ret = fopen(path, "w");
381
+
382
+ if (!ret && errno == EPERM) {
383
+ if (!unlink(path))
384
+ ret = fopen(path, "w");
385
+ else
386
+ errno = EPERM;
387
+ }
388
+ return ret;
389
+}
390
+
391
int xmkstemp(char *template)
392
{
393
int fd;