jcs
/amend
/amendments
/11
browser: Implement file checkout/revert of last stored version
jcs made amendment 11 over 3 years ago
--- amend.h Mon Oct 18 13:09:14 2021
+++ amend.h Wed Oct 20 08:49:30 2021
@@ -32,6 +32,8 @@
#define REPO_MENU_ID 131
#define REPO_MENU_ADD_FILE_ID 1
+#define REPO_MENU_REVERT_FILE_ID 2
+#define REPO_MENU_APPLY_DIFF_ID 3
#define COMMIT_LDEF_ID 128
--- browser.c Mon Oct 18 13:09:06 2021
+++ browser.c Wed Oct 20 09:18:33 2021
@@ -27,6 +27,7 @@
void browser_add_files(struct browser *browser);
void browser_filter_commits(struct browser *browser);
+void browser_revert_file(struct browser *browser);
Pattern fill_pattern;
@@ -62,6 +63,9 @@ browser_idle(struct browser *browser)
if (browser->committer)
committer_idle(browser->committer);
break;
+ case BROWSER_STATE_REVERT_FILE:
+ browser_revert_file(browser);
+ browser->state = BROWSER_STATE_IDLE;
}
}
@@ -325,6 +329,37 @@ browser_show_commit(struct browser *browser, struct re
InvalRect(&(*(browser->diff_te))->viewRect);
UpdateScrollbarForTE(browser->diff_scroller, browser->diff_te, true);
+}
+
+void
+browser_revert_file(struct browser *browser)
+{
+ char buf[256], filename[256];
+ Point pt = { 75, 100 };
+ struct repo_file *file;
+ short *selected = NULL;
+ short nselected = 0, i, error;
+ SFReply reply;
+
+ nselected = browser_selected_file_ids(browser, &selected);
+ for (i = 0; i < nselected; i++) {
+ file = repo_file_with_id(browser->repo, selected[i]);
+
+ sprintf(buf, "Save %s:", file->filename);
+ CtoPstr(buf);
+
+ strcpy(filename, file->filename);
+ CtoPstr(filename);
+
+ SFPutFile(pt, buf, filename, NULL, &reply);
+ if (!reply.good)
+ break;
+
+ error = repo_checkout_file(browser->repo, file, reply.vRefNum,
+ reply.fName);
+ if (error)
+ break;
+ }
}
void
--- browser.h Mon Oct 18 13:09:23 2021
+++ browser.h Wed Oct 20 08:50:59 2021
@@ -29,7 +29,8 @@ enum {
BROWSER_STATE_UPDATE_COMMIT_LIST,
BROWSER_STATE_OPEN_COMMITTER,
BROWSER_STATE_COMMITTER_DO_DIFF,
- BROWSER_STATE_WAITING_FOR_COMMITTER
+ BROWSER_STATE_WAITING_FOR_COMMITTER,
+ BROWSER_STATE_REVERT_FILE
};
struct browser {
--- main.c Mon Oct 18 09:22:19 2021
+++ main.c Wed Oct 20 08:50:31 2021
@@ -251,6 +251,12 @@ handle_menu(long menu_id)
if (cur_browser)
cur_browser->state = BROWSER_STATE_ADD_FILE;
break;
+ case REPO_MENU_REVERT_FILE_ID:
+ if (cur_browser)
+ cur_browser->state = BROWSER_STATE_REVERT_FILE;
+ break;
+ case REPO_MENU_APPLY_DIFF_ID:
+ break;
}
break;
}
--- repo.c Mon Oct 18 17:12:17 2021
+++ repo.c Wed Oct 20 09:31:09 2021
@@ -226,9 +226,9 @@ repo_parse_file(Handle hfile)
data += (data[0] + 1);
/* type fourcc, creator fourcc */
- memcpy(file->type, data, 4);
+ memcpy(&file->type, data, 4);
data += 4;
- memcpy(file->creator, data, 4);
+ memcpy(&file->creator, data, 4);
data += 4;
/* creation date, long */
@@ -541,6 +541,48 @@ repo_get_file_attrs(struct repo *repo, Str255 filename
return 0;
}
+short
+repo_checkout_file(struct repo *repo, struct repo_file *file,
+ short vrefnum, Str255 filename)
+{
+ Handle texth;
+ size_t len;
+ short error, frefnum;
+
+ texth = Get1Resource(REPO_TEXT_RTYPE, file->id);
+ if (texth == NULL) {
+ warn("No copy of file %s exists in repo", file->filename);
+ return -1;
+ }
+
+ error = Create(filename, vrefnum, file->creator, file->type);
+ if (error && error != dupFNErr) {
+ warn("Failed to create file %s: %d", PtoCstr(filename),
+ error);
+ return -1;
+ }
+
+ error = FSOpen(filename, vrefnum, &frefnum);
+ if (error)
+ err(1, "Failed to open file %s: %d", PtoCstr(filename), error);
+
+ error = SetEOF(frefnum, 0);
+ if (error)
+ err(1, "Failed to truncate file %s: %d", PtoCstr(filename),
+ error);
+
+ len = GetHandleSize(texth);
+ HLock(texth);
+ error = FSWrite(frefnum, &len, *texth);
+ if (error)
+ err(1, "Failed to write file to %s: %d", PtoCstr(filename), error);
+ ReleaseResource(texth);
+
+ FSClose(frefnum);
+
+ return 0;
+}
+
void
repo_sort_files(struct repo *repo)
{
@@ -593,8 +635,7 @@ repo_diff_file(struct repo *repo, struct repo_file *fi
PROGRAM_NAME);
CtoPstr(fromfilename);
- error = Create(fromfilename, repo->vrefnum, (OSType)file->creator,
- (OSType)file->type);
+ error = Create(fromfilename, repo->vrefnum, file->creator, file->type);
if (error && error != dupFNErr)
err(1, "Failed to create file %s: %d", PtoCstr(fromfilename),
error);
--- repo.h Mon Oct 18 15:08:25 2021
+++ repo.h Wed Oct 20 09:26:07 2021
@@ -32,8 +32,8 @@
struct repo_file {
short id;
char filename[256];
- char type[4];
- char creator[4];
+ OSType type;
+ OSType creator;
unsigned long ctime;
unsigned long mtime;
};
@@ -79,6 +79,8 @@ struct repo_file *repo_file_with_id(struct repo *repo,
void repo_show_diff_text(struct repo_commit *commit, TEHandle te);
struct repo_file *repo_add_file(struct repo *repo);
short repo_diff_file(struct repo *repo, struct repo_file *file);
+short repo_checkout_file(struct repo *repo, struct repo_file *file,
+ short vrefnum, Str255 filename);
void repo_commit(struct repo *repo, short *files, short nfiles, short adds,
short subs, Handle log, short loglen, Handle diff, unsigned long difflen);