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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 10, 2015 at 08:26 UTC
54b418f6985568bcb71c354ada204efe103b870e
1 file changed
+15
-23
refs.c
+15
-23
@@ -3150,46 +3150,42 @@ static int should_autocreate_reflog(const char *refname)
3150
* should_autocreate_reflog returns non-zero. Otherwise, create it
3151
* regardless of the ref name. Fill in *err and return -1 on failure.
3152
*/
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)
3154
{
3155
int logfd, oflags = O_APPEND | O_WRONLY;
3156
- char *logfile;
3156
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);
3158
if (force_create || should_autocreate_reflog(refname)) {
3163
- if (safe_create_leading_directories(logfile) < 0) {
3159
+ if (safe_create_leading_directories(logfile->buf) < 0) {
3160
strbuf_addf(err, "unable to create directory for %s: "
3165
- "%s", logfile, strerror(errno));
3161
+ "%s", logfile->buf, strerror(errno));
3162
return -1;
3163
}
3164
oflags |= O_CREAT;
3165
}
3166
3171
- logfd = open(logfile, oflags, 0666);
3167
+ logfd = open(logfile->buf, oflags, 0666);
3168
if (logfd < 0) {
3169
if (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR))
3170
return 0;
3171
3172
if (errno == EISDIR) {
3177
- if (remove_empty_directories(logfile)) {
3173
+ if (remove_empty_directories(logfile->buf)) {
3174
strbuf_addf(err, "There are still logs under "
3179
- "'%s'", logfile);
3175
+ "'%s'", logfile->buf);
3176
return -1;
3177
}
3182
- logfd = open(logfile, oflags, 0666);
3178
+ logfd = open(logfile->buf, oflags, 0666);
3179
}
3180
3181
if (logfd < 0) {
3182
strbuf_addf(err, "unable to append to %s: %s",
3187
- logfile, strerror(errno));
3183
+ logfile->buf, strerror(errno));
3184
return -1;
3185
}
3186
}
3187
3192
- adjust_shared_perm(logfile);
3188
+ adjust_shared_perm(logfile->buf);
3189
close(logfd);
3190
return 0;
3191
}
@@ -3233,36 +3229,32 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
3229
3230
static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
3231
const unsigned char *new_sha1, const char *msg,
3236
- struct strbuf *sb_log_file, int flags,
3232
+ struct strbuf *logfile, int flags,
3233
struct strbuf *err)
3234
{
3235
int logfd, result, oflags = O_APPEND | O_WRONLY;
3240
- char *log_file;
3236
3237
if (log_all_ref_updates < 0)
3238
log_all_ref_updates = !is_bare_repository();
3239
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);
3241
3242
if (result)
3243
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;
3244
3253
- logfd = open(log_file, oflags);
3245
+ logfd = open(logfile->buf, oflags);
3246
if (logfd < 0)
3247
return 0;
3248
result = log_ref_write_fd(logfd, old_sha1, new_sha1,
3249
git_committer_info(0), msg);
3250
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,
3252
strerror(errno));
3253
close(logfd);
3254
return -1;
3255
}
3256
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,
3258
strerror(errno));
3259
return -1;
3260
}