Skip to content

Commit

Permalink
Refactor bbinfo file to be an advanced feature not enabled by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxyu1115 committed Apr 2, 2024
1 parent 39caadd commit 79b5bc6
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 127 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## [0.4.0] - 2024-?

### Changed
- Refactored and made the BranchInfo file an opt-in feature. For the few fans of this feature, you'll need to manually enable it after the update.

### Fixed
- Removed BranchInfo file caching to avoid bugs related to non-gbs modifications to the file


## [0.3.0] - 2023-08-13

### Changed
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ Git Backup & Sync currently supports the following commands:
## Extension Settings
This extension contributes the following settings:

* `git-backup-sync.branchInfoPath`: Points to the location of the branch info file. This defaults to `.branchinfo`.
* `git-backup-sync.defaultBackupUpstreamName`: Name of the default upstream name. This defaults to `origin`.
* `git-backup-sync.defaultAutoBackupBranches`: When creating new backup branches, the new backup branch starts off with `autobackup = defaultAutoBackupBranches`. This defaults to `false`.
* `git-backup-sync.shouldCommitBranchInfoFile`: When creating a new Backup Branch, do you want to commit the branch info file change. (This is useful for sharing across devices). This defaults to `false`.
* `git-backup-sync.backupBranchNamePrefix`: The default prefix of the backup branch name. This defaults to `"gbs-backup-"`.
* `git-backup-sync.defaultAutoBackupBranches`: Whether to auto backup upon detecting changes in VSCode. This defaults to `false`.
* `git-backup-sync.defaultBackupUpstreamName`: Name of the default upstream name. This defaults to `origin`.

And for more customizations using the "branch info" file, there are the following settings:
* `git-backup-sync.branchInfo.enable`: Whether to enable the branch info file. Enabling this file allows for more granular customization per branch, at the cost of needing to manage an additional config file. This defaults to `false`.
* `git-backup-sync.branchInfo.path`: Points to the location of the branch info file. This defaults to `.bbinfo`.
* `git-backup-sync.branchInfo.shouldCommitBranchInfoFile`: When creating a new Backup Branch, do you want to commit the branch info file change. (This is useful for sharing across devices). This defaults to `false`.

These settings don't need to be shared and can be different across clones, even including `branchInfoPath`. (But if one wants to sync changes across devices, they need to use the same backup branch. )

Expand All @@ -43,4 +46,8 @@ The code for this extension is available on github at: https://github.com/maxyu1


## Want to Contribute?
I have already created a few issues. Feel free to grab one, work on it, and make a pull request. If you have other ideas, feel free to start a thread in discussions.
I have already created a few issues. Feel free to grab one, work on it, and make a pull request. If you have other ideas, feel free to start a thread in discussions.

To setup your dev environment, you need `npm` as well as vscode. After cloning, run `npm install` to setup, and F5 to build and run the extension locally.

And after making your changes, please document a short summary in the `CHANGELOG.md`, following the current format.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
"type": "object",
"title": "Git Backup & Sync",
"properties": {
"git-backup-sync.branchInfoPath": {
"type": "string",
"default": ".bbinfo",
"description": "Path pointing to the Backup Branch Info file. This file stores backup branch information necessary to the extension. "
},
"git-backup-sync.defaultAutoBackupBranches": {
"type": "boolean",
"default": false,
Expand All @@ -61,15 +56,25 @@
"default": "origin",
"description": "The upstream name we backup to. This defaults to \"origin\""
},
"git-backup-sync.shouldCommitBranchInfoFile": {
"type": "boolean",
"default": false,
"description": "When creating a new Backup Branch, do you want to commit the branch info file change. (This is useful for sharing across devices)"
},
"git-backup-sync.backupBranchNamePrefix": {
"type": "string",
"default": "gbs-backup-",
"description": "The prefix of backup branch name. This defaults to 'gbs-backup-' "
},
"git-backup-sync.branchInfo.enabled": {
"type": "boolean",
"default": false,
"description": "Whether to enable the branch info file. Enabling this file allows for more granular customization per branch, at the cost of needing to manage an additional config file."
},
"git-backup-sync.branchInfo.path": {
"type": "string",
"default": ".bbinfo",
"markdownDescription": "**This only is effective when branchInfo file is enabled**. Path pointing to the Backup Branch Info file. "
},
"git-backup-sync.branchInfo.shouldCommitBranchInfoFile": {
"type": "boolean",
"default": false,
"markdownDescription": "**This only is effective when branchInfo file is enabled**. When creating a new Backup Branch, do you want to commit the branch info file change. (This is useful for sharing across devices)"
}
}
}
Expand Down
27 changes: 10 additions & 17 deletions src/branchinfo.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { TextDecoder, TextEncoder } from 'util';
import * as vscode from 'vscode';

interface BranchInfo {
export interface BranchInfo {
autoBackup: boolean,
backupBranchName: string,
}


export class BranchInfoManager {
private workspaceUri: vscode.Uri;
private lastPath?: string;
private branchInfoMap: Map<string, BranchInfo> = new Map();

private encoder = new TextEncoder();
private decoder = new TextDecoder();

constructor(workspaceUri: vscode.Uri) {
this.workspaceUri = workspaceUri;
this.lastPath = undefined;
}

private async readBranchInfoFile() {
let branchInfoUri = this.workspaceUri.with({path: this.workspaceUri.path + "/" + this.lastPath});
private async readBranchInfoFile(branchInfoPath: string) {
let branchInfoUri = this.workspaceUri.with({path: this.workspaceUri.path + "/" + branchInfoPath});
return vscode.workspace.fs.readFile(branchInfoUri).then(content => {
this.branchInfoMap = new Map();
const jsonObject = JSON.parse(this.decoder.decode(content));
Expand All @@ -34,8 +32,8 @@ export class BranchInfoManager {
});
}

private async updateBranchInfoFile() {
let branchInfoUri = this.workspaceUri.with({path: this.workspaceUri.path + "/" + this.lastPath});
private async updateBranchInfoFile(branchInfoPath: string) {
let branchInfoUri = this.workspaceUri.with({path: this.workspaceUri.path + "/" + branchInfoPath});
return vscode.workspace.fs.writeFile(branchInfoUri,
this.encoder.encode(
JSON.stringify(Object.fromEntries(this.branchInfoMap), null, 4)
Expand All @@ -44,12 +42,7 @@ export class BranchInfoManager {
}

public async getMap(branchInfoPath: string): Promise<Map<string, BranchInfo>> {
if (this.lastPath === branchInfoPath) {
return this.branchInfoMap;
}
this.lastPath = branchInfoPath;
// only read from fs when cache is invalidated
return this.readBranchInfoFile().then(() => this.branchInfoMap);
return this.readBranchInfoFile(branchInfoPath).then(() => this.branchInfoMap);
}

public async get(branchInfoPath: string, branchName: string): Promise<BranchInfo | undefined> {
Expand All @@ -64,21 +57,21 @@ export class BranchInfoManager {
// this makes sure the branch info map is up to date
await this.getMap(branchInfoPath);
this.branchInfoMap.set(branchName, branchInfo);
return this.updateBranchInfoFile();
return this.updateBranchInfoFile(branchInfoPath);
}

public async delete(branchInfoPath: string, branchName: string) {
// this makes sure the branch info map is up to date
await this.getMap(branchInfoPath);
this.branchInfoMap.delete(branchName);
return this.updateBranchInfoFile();
return this.updateBranchInfoFile(branchInfoPath);
}

public async updateAutoBackup(branchInfoPath: string, defaultAutoBackupBranches:boolean) {
await this.getMap(branchInfoPath);
this.branchInfoMap.forEach( (_branchInfo,branchName) => {
this.branchInfoMap.forEach( (_branchInfo, branchName) => {
_branchInfo.autoBackup = defaultAutoBackupBranches;
});
return this.updateBranchInfoFile();
return this.updateBranchInfoFile(branchInfoPath);
}
}
Loading

0 comments on commit 79b5bc6

Please sign in to comment.