Skip to content

Commit 79d7582

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 833e482 commit 79d7582

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
761761
hook_arg2 = "";
762762
}
763763

764-
s->fp = fopen(git_path(commit_editmsg), "w");
764+
s->fp = fopen_for_writing(git_path(commit_editmsg));
765765
if (s->fp == NULL)
766766
die_errno(_("could not open '%s'"), git_path(commit_editmsg));
767767

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ extern int xmkstemp_mode(char *template, int mode);
733733
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
734734
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
735735
extern char *xgetcwd(void);
736+
extern FILE *fopen_for_writing(const char *path);
736737

737738
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
738739

wrapper.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,19 @@ FILE *xfdopen(int fd, const char *mode)
375375
return stream;
376376
}
377377

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+
378391
int xmkstemp(char *template)
379392
{
380393
int fd;

0 commit comments

Comments
 (0)