This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synchronize git projects in the workspace FS with projects list defin…
…ed in the current workspace definition (#38) Fix eclipse-che/che#12006 Signed-off-by: Sun Seng David TAN <sutan@redhat.com>
- Loading branch information
Showing
11 changed files
with
446 additions
and
600 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
const { spawn } = require('child_process'); | ||
|
||
export interface GitUpstreamBranch { | ||
remote: string; | ||
branch: string; | ||
remoteURL?: string; | ||
} | ||
|
||
export async function getUpstreamBranch(projectPath: string): Promise<GitUpstreamBranch | undefined> { | ||
const remoteBranchRef = await execGit(projectPath, 'rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}'); | ||
if (!remoteBranchRef) { | ||
return; | ||
} | ||
const gitUpstreamBranch = parseGitUpstreamBranch(remoteBranchRef); | ||
if (gitUpstreamBranch) { | ||
gitUpstreamBranch.remoteURL = await getRemoteURL(gitUpstreamBranch.remote, projectPath); | ||
} | ||
return gitUpstreamBranch; | ||
} | ||
|
||
export function getRemoteURL(remote: string, projectPath: string): Promise<string | undefined> { | ||
return execGit(projectPath, 'config', '--get', `remote.${remote}.url`); | ||
} | ||
|
||
export function parseGitUpstreamBranch(gitBranchvvOutput: string): GitUpstreamBranch | undefined { | ||
|
||
const branchOrRemote = '[^\\s^/]+'; | ||
const regexp = new RegExp( | ||
`(${branchOrRemote})\\/(${branchOrRemote})` | ||
); | ||
|
||
const result: RegExpMatchArray | null = gitBranchvvOutput.match(regexp); | ||
|
||
if (!result) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
remote: result[1], branch: result[2] | ||
}; | ||
|
||
} | ||
|
||
export function getGitRootFolder(uri: string): string { | ||
return uri.substring(0, uri.lastIndexOf('.git/')); | ||
} | ||
|
||
export async function execGit(directory: string, ...args: string[]): Promise<string | undefined> { | ||
return new Promise<string | undefined>((resolve, reject) => { | ||
|
||
const gitCommand = spawn('git', args, { cwd: directory }); | ||
let result = ''; | ||
gitCommand.stdout.on('data', (data: string) => { | ||
result += data.toString(); | ||
}); | ||
gitCommand.on('close', () => { | ||
result = result.trim(); | ||
resolve(result); | ||
}); | ||
gitCommand.on('error', () => { resolve(undefined); }); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
import { che as cheApi } from '@eclipse-che/api'; | ||
|
||
export function updateOrCreateGitProject( | ||
projects: cheApi.workspace.ProjectConfig[], projectPath: string, projectGitLocation: string, projectGitRemoteBranch: string): cheApi.workspace.ProjectConfig[] { | ||
|
||
const filteredProject = projects.filter(project => project.path === projectPath); | ||
|
||
if (filteredProject.length === 0) { | ||
const projectName = projectPath.split('/').pop(); | ||
|
||
// create a new one | ||
projects.push({ | ||
"name": projectName ? projectName : "new-project", | ||
"attributes": {}, | ||
"source": { | ||
"location": projectGitLocation, | ||
"type": "git", | ||
"parameters": { | ||
"branch": projectGitRemoteBranch | ||
} | ||
}, | ||
"path": projectPath, | ||
"description": "", | ||
"mixins": [] | ||
}); | ||
return projects; | ||
} | ||
|
||
filteredProject.forEach(project => { | ||
if (!project.source) { | ||
project.source = { | ||
type: 'git', | ||
location: '', | ||
parameters: {} | ||
}; | ||
} | ||
project.source.location = projectGitLocation; | ||
if (!project.source.parameters) { | ||
project.source.parameters = {}; | ||
} | ||
project.source.parameters['branch'] = projectGitRemoteBranch; | ||
}); | ||
|
||
return projects; | ||
} | ||
|
||
export function deleteGitProject( | ||
projects: cheApi.workspace.ProjectConfig[], | ||
projectPath: string): cheApi.workspace.ProjectConfig[] { | ||
|
||
projects | ||
.filter(project => project.path === projectPath) | ||
.forEach(project => { | ||
projects.splice(projects.indexOf(project)); | ||
}); | ||
|
||
return projects; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.