-
Notifications
You must be signed in to change notification settings - Fork 34
Refactoring #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amitsaha
wants to merge
8
commits into
master
Choose a base branch
from
refactor_1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactoring #108
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d646ef3
refactor repositories related code
amitsaha d3e6938
Refactor the client creation code
amitsaha 46aa73d
wip client refactor
amitsaha 9368e7d
fix tests and remove Delete* function as it is unused
amitsaha e788ba5
refactor newClient
amitsaha 4f8976c
More client code refactor
amitsaha 5161af6
client refactor
amitsaha 9fe0994
refactor backup
amitsaha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -17,59 +17,61 @@ var appFS = afero.NewOsFs() | |
var gitCommand = "git" | ||
var gethomeDir = homedir.Dir | ||
|
||
func updateExistingClone(repoDir string, bare bool, repo *Repository) ([]byte, error) { | ||
log.Printf("%s exists, updating. \n", repo.Name) | ||
var cmd *exec.Cmd | ||
if bare { | ||
cmd = execCommand(gitCommand, "-C", repoDir, "remote", "update", "--prune") | ||
} else { | ||
cmd = execCommand(gitCommand, "-C", repoDir, "pull") | ||
} | ||
return cmd.CombinedOutput() | ||
} | ||
|
||
func newClone(repoDir string, bare bool, repo *Repository, useHTTPSClone *bool) ([]byte, error) { | ||
|
||
log.Printf("Cloning %s\n", repo.Name) | ||
log.Printf("%#v\n", repo) | ||
|
||
if repo.Private && ignorePrivate != nil && *ignorePrivate { | ||
log.Printf("Skipping %s as it is a private repo.\n", repo.Name) | ||
return nil, nil | ||
} | ||
|
||
if useHTTPSClone != nil && *useHTTPSClone { | ||
// Add username and token to the clone URL | ||
// https://gitlab.com/amitsaha/testproject1 => https://amitsaha:[email protected]/amitsaha/testproject1 | ||
u, err := url.Parse(repo.CloneURL) | ||
if err != nil { | ||
log.Fatalf("Invalid clone URL: %v\n", err) | ||
} | ||
repo.CloneURL = u.Scheme + "://" + gitHostUsername + ":" + gitHostToken + "@" + u.Host + u.Path | ||
} | ||
|
||
var cmd *exec.Cmd | ||
if bare { | ||
cmd = execCommand(gitCommand, "clone", "--mirror", repo.CloneURL, repoDir) | ||
} else { | ||
cmd = execCommand(gitCommand, "clone", repo.CloneURL, repoDir) | ||
} | ||
return cmd.CombinedOutput() | ||
} | ||
|
||
// Check if we have a copy of the repo already, if | ||
// we do, we update the repo, else we do a fresh clone | ||
func backUp(backupDir string, repo *Repository, bare bool, wg *sync.WaitGroup) ([]byte, error) { | ||
defer wg.Done() | ||
|
||
var dirName string | ||
dirName := repo.Name | ||
if bare { | ||
dirName = repo.Name + ".git" | ||
} else { | ||
dirName = repo.Name | ||
dirName += ".git" | ||
} | ||
repoDir := path.Join(backupDir, repo.Namespace, dirName) | ||
|
||
_, err := appFS.Stat(repoDir) | ||
|
||
var stdoutStderr []byte | ||
if err == nil { | ||
log.Printf("%s exists, updating. \n", repo.Name) | ||
var cmd *exec.Cmd | ||
if bare { | ||
cmd = execCommand(gitCommand, "-C", repoDir, "remote", "update", "--prune") | ||
} else { | ||
cmd = execCommand(gitCommand, "-C", repoDir, "pull") | ||
} | ||
stdoutStderr, err = cmd.CombinedOutput() | ||
} else { | ||
log.Printf("Cloning %s\n", repo.Name) | ||
log.Printf("%#v\n", repo) | ||
|
||
if repo.Private && ignorePrivate != nil && *ignorePrivate { | ||
log.Printf("Skipping %s as it is a private repo.\n", repo.Name) | ||
return stdoutStderr, nil | ||
} | ||
|
||
if useHTTPSClone != nil && *useHTTPSClone { | ||
// Add username and token to the clone URL | ||
// https://gitlab.com/amitsaha/testproject1 => https://amitsaha:[email protected]/amitsaha/testproject1 | ||
u, err := url.Parse(repo.CloneURL) | ||
if err != nil { | ||
log.Fatalf("Invalid clone URL: %v\n", err) | ||
} | ||
repo.CloneURL = u.Scheme + "://" + gitHostUsername + ":" + gitHostToken + "@" + u.Host + u.Path | ||
} | ||
|
||
var cmd *exec.Cmd | ||
if bare { | ||
cmd = execCommand(gitCommand, "clone", "--mirror", repo.CloneURL, repoDir) | ||
} else { | ||
cmd = execCommand(gitCommand, "clone", repo.CloneURL, repoDir) | ||
} | ||
stdoutStderr, err = cmd.CombinedOutput() | ||
return updateExistingClone(repoDir, bare, repo) | ||
} | ||
return stdoutStderr, err | ||
return newClone(repoDir, bare, repo, useHTTPSClone) | ||
} | ||
|
||
func setupBackupDir(backupDir, service, githostURL *string) string { | ||
|
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information