Skip to content

Commit 54b418f

Browse files
peffgitster
authored andcommitted
refs.c: simplify strbufs in reflog setup and writing
Commit 1a83c24 (git_snpath(): retire and replace with strbuf_git_path(), 2014-11-30) taught log_ref_setup and log_ref_write_1 to take a strbuf parameter, rather than a bare string. It then makes an alias to the strbuf's "buf" field under the original name. This made the original diff much shorter, but the resulting code is more complicated that it needs to be. Since we've aliased the pointer, we drop our reference to the strbuf to ensure we don't accidentally change it. But if we simply drop our alias and use "logfile.buf" directly, we do not have to worry about this aliasing. It's a larger diff, but the resulting code is simpler. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 07e3070 commit 54b418f

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

refs.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,46 +3150,42 @@ static int should_autocreate_reflog(const char *refname)
31503150
* should_autocreate_reflog returns non-zero. Otherwise, create it
31513151
* regardless of the ref name. Fill in *err and return -1 on failure.
31523152
*/
3153-
static int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err, int force_create)
3153+
static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)
31543154
{
31553155
int logfd, oflags = O_APPEND | O_WRONLY;
3156-
char *logfile;
31573156

3158-
strbuf_git_path(sb_logfile, "logs/%s", refname);
3159-
logfile = sb_logfile->buf;
3160-
/* make sure the rest of the function can't change "logfile" */
3161-
sb_logfile = NULL;
3157+
strbuf_git_path(logfile, "logs/%s", refname);
31623158
if (force_create || should_autocreate_reflog(refname)) {
3163-
if (safe_create_leading_directories(logfile) < 0) {
3159+
if (safe_create_leading_directories(logfile->buf) < 0) {
31643160
strbuf_addf(err, "unable to create directory for %s: "
3165-
"%s", logfile, strerror(errno));
3161+
"%s", logfile->buf, strerror(errno));
31663162
return -1;
31673163
}
31683164
oflags |= O_CREAT;
31693165
}
31703166

3171-
logfd = open(logfile, oflags, 0666);
3167+
logfd = open(logfile->buf, oflags, 0666);
31723168
if (logfd < 0) {
31733169
if (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR))
31743170
return 0;
31753171

31763172
if (errno == EISDIR) {
3177-
if (remove_empty_directories(logfile)) {
3173+
if (remove_empty_directories(logfile->buf)) {
31783174
strbuf_addf(err, "There are still logs under "
3179-
"'%s'", logfile);
3175+
"'%s'", logfile->buf);
31803176
return -1;
31813177
}
3182-
logfd = open(logfile, oflags, 0666);
3178+
logfd = open(logfile->buf, oflags, 0666);
31833179
}
31843180

31853181
if (logfd < 0) {
31863182
strbuf_addf(err, "unable to append to %s: %s",
3187-
logfile, strerror(errno));
3183+
logfile->buf, strerror(errno));
31883184
return -1;
31893185
}
31903186
}
31913187

3192-
adjust_shared_perm(logfile);
3188+
adjust_shared_perm(logfile->buf);
31933189
close(logfd);
31943190
return 0;
31953191
}
@@ -3233,36 +3229,32 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
32333229

32343230
static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
32353231
const unsigned char *new_sha1, const char *msg,
3236-
struct strbuf *sb_log_file, int flags,
3232+
struct strbuf *logfile, int flags,
32373233
struct strbuf *err)
32383234
{
32393235
int logfd, result, oflags = O_APPEND | O_WRONLY;
3240-
char *log_file;
32413236

32423237
if (log_all_ref_updates < 0)
32433238
log_all_ref_updates = !is_bare_repository();
32443239

3245-
result = log_ref_setup(refname, sb_log_file, err, flags & REF_FORCE_CREATE_REFLOG);
3240+
result = log_ref_setup(refname, logfile, err, flags & REF_FORCE_CREATE_REFLOG);
32463241

32473242
if (result)
32483243
return result;
3249-
log_file = sb_log_file->buf;
3250-
/* make sure the rest of the function can't change "log_file" */
3251-
sb_log_file = NULL;
32523244

3253-
logfd = open(log_file, oflags);
3245+
logfd = open(logfile->buf, oflags);
32543246
if (logfd < 0)
32553247
return 0;
32563248
result = log_ref_write_fd(logfd, old_sha1, new_sha1,
32573249
git_committer_info(0), msg);
32583250
if (result) {
3259-
strbuf_addf(err, "unable to append to %s: %s", log_file,
3251+
strbuf_addf(err, "unable to append to %s: %s", logfile->buf,
32603252
strerror(errno));
32613253
close(logfd);
32623254
return -1;
32633255
}
32643256
if (close(logfd)) {
3265-
strbuf_addf(err, "unable to append to %s: %s", log_file,
3257+
strbuf_addf(err, "unable to append to %s: %s", logfile->buf,
32663258
strerror(errno));
32673259
return -1;
32683260
}

0 commit comments

Comments
 (0)