Skip to content

Commit

Permalink
Merge branch 'Add-tag-with-hash' into w
Browse files Browse the repository at this point in the history
  • Loading branch information
he committed Feb 18, 2024
2 parents 2ce07a6 + 996f4ed commit 3fcc06f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,15 +750,22 @@ export class DataSource extends Disposable {
* @param force Force add the tag, replacing an existing tag with the same name (if it exists).
* @returns The ErrorInfo from the executed command.
*/
public addTag(repo: string, tagName: string, commitHash: string, type: TagType, message: string, force: boolean) {
public addTag(repo: string, tagName: string, commitHash: string, type: TagType, message: string, force: boolean, withHash: boolean) {
let tmpTagName = tagName;

if (withHash) {
// msg.tagName 拼接
tmpTagName = tmpTagName + '.' + commitHash.substring(0, 8);
}

const args = ['tag'];
if (force) {
args.push('-f');
}
if (type === TagType.Lightweight) {
args.push(tagName);
args.push(tmpTagName);
} else {
args.push(getConfig().signTags ? '-s' : '-a', tagName, '-m', message);
args.push(getConfig().signTags ? '-s' : '-a', tmpTagName, '-m', message);
}
args.push(commitHash);
return this.runGitCommand(args, repo);
Expand Down
2 changes: 1 addition & 1 deletion src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class GitGraphView extends Disposable {
});
break;
case 'addTag':
errorInfos = [await this.dataSource.addTag(msg.repo, msg.tagName, msg.commitHash, msg.type, msg.message, msg.force)];
errorInfos = [await this.dataSource.addTag(msg.repo, msg.tagName, msg.commitHash, msg.type, msg.message, msg.force, msg.withHash)];
if (errorInfos[0] === null && msg.pushToRemote !== null) {
errorInfos.push(...await this.dataSource.pushTag(msg.repo, msg.tagName, [msg.pushToRemote], msg.commitHash, msg.pushSkipRemoteCheck));
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ export interface RequestAddTag extends RepoRequest {
readonly pushToRemote: string | null; // string => name of the remote to push the tag to, null => don't push to a remote
readonly pushSkipRemoteCheck: boolean;
readonly force: boolean;
readonly withHash: boolean;
}
export interface ResponseAddTag extends ResponseWithMultiErrorInfo {
readonly command: 'addTag';
Expand Down
19 changes: 12 additions & 7 deletions web/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ class GitGraphView {

/* Actions */

private addTagAction(hash: string, initialName: string, initialType: GG.TagType, initialMessage: string, initialPushToRemote: string | null, target: DialogTarget & CommitTarget, isInitialLoad: boolean = true) {
private addTagAction(hash: string, initialName: string, initialType: GG.TagType, initialMessage: string, initialPushToRemote: string | null, target: DialogTarget & CommitTarget, isInitialLoad: boolean = true, withHash: boolean = true) {
let mostRecentTagsIndex = -1;
for (let i = 0; i < this.commits.length; i++) {
if (this.commits[i].tags.length > 0 && (mostRecentTagsIndex === -1 || this.commits[i].date > this.commits[mostRecentTagsIndex].date)) {
Expand All @@ -1616,7 +1616,8 @@ class GitGraphView {
const inputs: DialogInput[] = [
{ type: DialogInputType.TextRef, name: 'Name', default: initialName, info: mostRecentTags.length > 0 ? 'The most recent tag' + (mostRecentTags.length > 1 ? 's' : '') + ' in the loaded commits ' + (mostRecentTags.length > 1 ? 'are' : 'is') + ' ' + formatCommaSeparatedList(mostRecentTags) + '.' : undefined },
{ type: DialogInputType.Select, name: 'Type', default: initialType === GG.TagType.Annotated ? 'annotated' : 'lightweight', options: [{ name: 'Annotated', value: 'annotated' }, { name: 'Lightweight', value: 'lightweight' }] },
{ type: DialogInputType.Text, name: 'Message', default: initialMessage, placeholder: 'Optional', info: 'A message can only be added to an annotated tag.' }
{ type: DialogInputType.Text, name: 'Message', default: initialMessage, placeholder: 'Optional', info: 'A message can only be added to an annotated tag.' },
{ type: DialogInputType.Checkbox, name: 'WithHash', value: withHash, info: 'Include the commit hash in the end of the tag name.' }
];
if (this.gitRemotes.length > 1) {
const options = [{ name: 'Don\'t push', value: '-1' }];
Expand All @@ -1636,11 +1637,14 @@ class GitGraphView {
const tagName = <string>values[0];
const type = <string>values[1] === 'annotated' ? GG.TagType.Annotated : GG.TagType.Lightweight;
const message = <string>values[2];
const pushToRemote = this.gitRemotes.length > 1 && <string>values[3] !== '-1'
? this.gitRemotes[parseInt(<string>values[3])]
: this.gitRemotes.length === 1 && <boolean>values[3]
const pushToRemote = this.gitRemotes.length > 1 && <string>values[4] !== '-1'
? this.gitRemotes[parseInt(<string>values[4])]
: this.gitRemotes.length === 1 && <boolean>values[4]
? this.gitRemotes[0]
: null;
const withHash = <boolean>values[3];



const runAddTagAction = (force: boolean) => {
runAction({
Expand All @@ -1652,15 +1656,16 @@ class GitGraphView {
message: message,
pushToRemote: pushToRemote,
pushSkipRemoteCheck: globalState.pushTagSkipRemoteCheck,
force: force
force: force,
withHash: withHash
}, 'Adding Tag');
};

if (this.gitTags.includes(tagName)) {
dialog.showTwoButtons('A tag named <b><i>' + escapeHtml(tagName) + '</i></b> already exists, do you want to replace it with this new tag?', 'Yes, replace the existing tag', () => {
runAddTagAction(true);
}, 'No, choose another tag name', () => {
this.addTagAction(hash, tagName, type, message, pushToRemote, target, false);
this.addTagAction(hash, tagName, type, message, pushToRemote, target, true);
}, target);
} else {
runAddTagAction(false);
Expand Down

0 comments on commit 3fcc06f

Please sign in to comment.