Skip to content

Commit a3c0efe

Browse files
committed
Merge branch 'ew/config-protect-mode'
* ew/config-protect-mode: config: preserve config file permissions on edits
2 parents d6850db + daa22c6 commit a3c0efe

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

config.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,13 @@ int git_config_set_multivar_in_file(const char *config_filename,
16361636
MAP_PRIVATE, in_fd, 0);
16371637
close(in_fd);
16381638

1639+
if (fchmod(fd, st.st_mode & 07777) < 0) {
1640+
error("fchmod on %s failed: %s",
1641+
lock->filename, strerror(errno));
1642+
ret = CONFIG_NO_WRITE;
1643+
goto out_free;
1644+
}
1645+
16391646
if (store.seen == 0)
16401647
store.seen = 1;
16411648

@@ -1784,6 +1791,7 @@ int git_config_rename_section_in_file(const char *config_filename,
17841791
int out_fd;
17851792
char buf[1024];
17861793
FILE *config_file;
1794+
struct stat st;
17871795

17881796
if (new_name && !section_name_is_ok(new_name)) {
17891797
ret = error("invalid section name: %s", new_name);
@@ -1805,6 +1813,14 @@ int git_config_rename_section_in_file(const char *config_filename,
18051813
goto unlock_and_out;
18061814
}
18071815

1816+
fstat(fileno(config_file), &st);
1817+
1818+
if (fchmod(out_fd, st.st_mode & 07777) < 0) {
1819+
ret = error("fchmod on %s failed: %s",
1820+
lock->filename, strerror(errno));
1821+
goto out;
1822+
}
1823+
18081824
while (fgets(buf, sizeof(buf), config_file)) {
18091825
int i;
18101826
int length;

t/t1300-repo-config.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,4 +1158,14 @@ test_expect_failure 'adding a key into an empty section reuses header' '
11581158
test_cmp expect .git/config
11591159
'
11601160

1161+
test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
1162+
chmod 0600 .git/config &&
1163+
git config imap.pass Hunter2 &&
1164+
perl -e \
1165+
"die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" &&
1166+
git config --rename-section imap pop &&
1167+
perl -e \
1168+
"die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
1169+
'
1170+
11611171
test_done

0 commit comments

Comments
 (0)