Skip to content

Commit

Permalink
commit: allow editing the commit message even in shared repos
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Johannes Schindelin authored and Junio C Hamano committed Jan 7, 2016
1 parent 833e482 commit 79d7582
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
hook_arg2 = "";
}

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

Expand Down
1 change: 1 addition & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ extern int xmkstemp_mode(char *template, int mode);
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
extern char *xgetcwd(void);
extern FILE *fopen_for_writing(const char *path);

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

Expand Down
13 changes: 13 additions & 0 deletions wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@ FILE *xfdopen(int fd, const char *mode)
return stream;
}

FILE *fopen_for_writing(const char *path)
{
FILE *ret = fopen(path, "w");

if (!ret && errno == EPERM) {
if (!unlink(path))
ret = fopen(path, "w");
else
errno = EPERM;
}
return ret;
}

int xmkstemp(char *template)
{
int fd;
Expand Down

0 comments on commit 79d7582

Please sign in to comment.