jcs
/amend
/amendments
/7
repo: Show diff header with full log message
Add a utility function to append text to a TE without redrawing it
jcs made amendment 7 over 3 years ago
--- repo.c Mon Oct 18 17:12:17 2021
+++ repo.c Tue Oct 19 17:17:40 2021
@@ -326,29 +326,58 @@ repo_file_with_id(struct repo *repo, short id)
void
repo_show_diff_text(struct repo_commit *commit, TEHandle te)
{
+ char buf[512];
Handle diffh;
- unsigned long len, blen;
- char buf[100];
+ struct tm *ttm = NULL;
+ unsigned long diff_len;
+ short header_len, i, blen;
diffh = Get1Resource(REPO_DIFF_RTYPE, commit->id);
if (diffh == NULL)
err(1, "failed finding DIFF %d", commit->id);
HLock(diffh);
+ HLock(commit->log);
- len = GetHandleSize(diffh);
- if (len > 32767L) {
- len = 32767L - strlen(REPO_DIFF_TOO_BIG) - 20;
- blen = sprintf(buf, REPO_DIFF_TOO_BIG,
- GetHandleSize(diffh) - len);
- TESetText(*diffh, len, te);
- TESetSelect(32767L, 32767L, te);
- TEInsert(buf, blen, te);
- } else {
- TESetText(*diffh, len, te);
+ diff_len = GetHandleSize(diffh);
+ if (diff_len == 0)
+ LoadResource(diffh);
+
+ ttm = localtime(&commit->date);
+ header_len = sprintf(buf,
+ "Author: %s\r"
+ "Date: %04d-%02d-%02d %02d:%02d:%02d\r"
+ "\r ",
+ commit->author,
+ ttm->tm_year + 1900, ttm->tm_mon + 1, ttm->tm_mday,
+ ttm->tm_hour, ttm->tm_min, ttm->tm_sec);
+
+ for (i = 0; i < commit->log_len; i++) {
+ buf[header_len++] = (*(commit->log))[i];
+
+ if ((*(commit->log))[i] == '\r' && i < commit->log_len - 1) {
+ buf[header_len++] = ' ';
+ buf[header_len++] = ' ';
}
+ }
+ buf[header_len++] = '\r';
+ buf[header_len++] = '\r';
+ TESetText("", 0, te);
+ TEAppendFast(buf, header_len, te);
+
+ if (diff_len + header_len > 32767L) {
+ diff_len = 32767L - header_len - strlen(REPO_DIFF_TOO_BIG) - 20;
+ blen = sprintf(buf, REPO_DIFF_TOO_BIG,
+ GetHandleSize(diffh) - diff_len);
+ TEAppendFast(*diffh, diff_len, te);
+ TEAppendFast(buf, blen, te);
+ } else
+ TEAppendFast(*diffh, diff_len, te);
+
ReleaseResource(diffh);
+
+ TECalText(te);
}
struct repo_file *
@@ -698,10 +727,14 @@ repo_commit(struct repo *repo, short *files, short nfi
/* store diff */
AddResource(diff, REPO_DIFF_RTYPE, commit_id, NULL);
+ if (ResError())
+ err(1, "Failed storing diff in repo file (%d)", ResError());
WriteResource(diff);
/* store commit */
AddResource(commith, REPO_COMMIT_RTYPE, commit_id, NULL);
+ if (ResError())
+ err(1, "Failed storing commit in repo file (%d)", ResError());
WriteResource(commith);
/* store new versions of each file */
@@ -710,11 +743,13 @@ repo_commit(struct repo *repo, short *files, short nfi
if (file == NULL)
err(1, "Bogus file %d in commit", files[i]);
+ SetResLoad(false);
texth = Get1Resource(REPO_TEXT_RTYPE, file->id);
if (texth != NULL) {
RmveResource(texth);
ReleaseResource(texth);
}
+ SetResLoad(true);
len = strlen(file->filename);
memcpy(tfilename, file->filename, len);
--- util.c Mon Oct 18 13:14:58 2021
+++ util.c Tue Oct 19 17:08:21 2021
@@ -575,3 +575,31 @@ TECopyRemovingFakeTabs(TEHandle teh)
HUnlock(te->hText);
HUnlock(teh);
}
+
+void
+TEAppendFast(char *str, size_t len, TEHandle te)
+{
+ unsigned long cur_size;
+
+ HLock(te);
+
+ cur_size = GetHandleSize((*te)->hText);
+ if ((*te)->teLength + len >= cur_size) {
+ SetHandleSize((*te)->hText, cur_size + len);
+ if (MemError())
+ err(1, "Out of memory! Can't expand diff TE beyond %lu bytes.",
+ cur_size);
+ }
+
+ HLock((*te)->hText);
+ memcpy(*((*te)->hText) + (*te)->teLength, str, len);
+ HUnlock((*te)->hText);
+
+ if ((long)((*te)->teLength) + len < 32767L)
+ (*te)->teLength += len;
+ else
+ (*te)->teLength = 32767L;
+
+ HUnlock(te);
+ /* be sure to call TECalText() when done appending */
+}
--- util.h Mon Oct 18 13:09:59 2021
+++ util.h Tue Oct 19 16:30:38 2021
@@ -85,8 +85,6 @@ void SetTrackControlTE(TEHandle te);
pascal void TrackMouseDownInControl(ControlHandle control, short part);
void TECopyRemovingFakeTabs(TEHandle teh);
short TECanAddLine(TEHandle teh, size_t addition);
-
-Handle SizedTENew(Rect viewRect, unsigned long size);
-
+void TEAppendFast(char *str, size_t len, TEHandle te);
#endif