die("too long a refname '%s'", list->name);
write_or_die(fd, line, len);
}
+ close(fd);
return commit_lock_file(&packlock);
}
free(lock);
}
+/*
+ * copy the reflog message msg to buf, which has been allocated sufficiently
+ * large, while cleaning up the whitespaces. Especially, convert LF to space,
+ * because reflog file is one line per entry.
+ */
+static int copy_msg(char *buf, const char *msg)
+{
+ char *cp = buf;
+ char c;
+ int wasspace = 1;
+
+ *cp++ = '\t';
+ while ((c = *msg++)) {
+ if (wasspace && isspace(c))
+ continue;
+ wasspace = isspace(c);
+ if (wasspace)
+ c = ' ';
+ *cp++ = c;
+ }
+ while (buf < cp && isspace(cp[-1]))
+ cp--;
+ *cp++ = '\n';
+ return cp - buf;
+}
+
static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
const unsigned char *new_sha1, const char *msg)
{
adjust_shared_perm(log_file);
- msglen = 0;
- if (msg) {
- /* clean up the message and make sure it is a single line */
- for ( ; *msg; msg++)
- if (!isspace(*msg))
- break;
- if (*msg) {
- const char *ep = strchr(msg, '\n');
- if (ep)
- msglen = ep - msg;
- else
- msglen = strlen(msg);
- }
- }
-
+ msglen = msg ? strlen(msg) : 0;
committer = git_committer_info(-1);
maxlen = strlen(committer) + msglen + 100;
logrec = xmalloc(maxlen);
sha1_to_hex(new_sha1),
committer);
if (msglen)
- len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
+ len += copy_msg(logrec + len - 1, msg) - 1;
written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
free(logrec);
- close(logfd);
- if (written != len)
+ if (close(logfd) != 0 || written != len)
return error("Unable to append to %s", log_file);
return 0;
}
goto error_free_return;
}
written = write_in_full(fd, ref, len);
- close(fd);
- if (written != len) {
+ if (close(fd) != 0 || written != len) {
error("Unable to write to %s", lockpath);
goto error_unlink_return;
}
static char *ref_msg(const char *line, const char *endp)
{
const char *ep;
- char *msg;
-
line += 82;
- for (ep = line; ep < endp && *ep != '\n'; ep++)
- ;
- msg = xmalloc(ep - line + 1);
- memcpy(msg, line, ep - line);
- msg[ep - line] = 0;
- return msg;
+ ep = memchr(line, '\n', endp - line);
+ if (!ep)
+ ep = endp;
+ return xmemdupz(line, ep - line);
}
int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
if (hashcmp(logged_sha1, sha1)) {
fprintf(stderr,
"warning: Log %s has gap after %s.\n",
- logfile, show_rfc2822_date(date, tz));
+ logfile, show_date(date, tz, DATE_RFC2822));
}
}
else if (date == at_time) {
if (hashcmp(logged_sha1, sha1)) {
fprintf(stderr,
"warning: Log %s unexpectedly ended on %s.\n",
- logfile, show_rfc2822_date(date, tz));
+ logfile, show_date(date, tz, DATE_RFC2822));
}
}
munmap(log_mapped, mapsz);
{
return do_for_each_reflog("", fn, cb_data);
}
+
+int update_ref(const char *action, const char *refname,
+ const unsigned char *sha1, const unsigned char *oldval,
+ int flags, enum action_on_err onerr)
+{
+ static struct ref_lock *lock;
+ lock = lock_any_ref_for_update(refname, oldval, flags);
+ if (!lock) {
+ const char *str = "Cannot lock the ref '%s'.";
+ switch (onerr) {
+ case MSG_ON_ERR: error(str, refname); break;
+ case DIE_ON_ERR: die(str, refname); break;
+ case QUIET_ON_ERR: break;
+ }
+ return 1;
+ }
+ if (write_ref_sha1(lock, sha1, action) < 0) {
+ const char *str = "Cannot update the ref '%s'.";
+ switch (onerr) {
+ case MSG_ON_ERR: error(str, refname); break;
+ case DIE_ON_ERR: die(str, refname); break;
+ case QUIET_ON_ERR: break;
+ }
+ return 1;
+ }
+ return 0;
+}