Skip to content

Commit

Permalink
fix: Correct branch management (#4)
Browse files Browse the repository at this point in the history
Checking out latest tracked ref was not working correctly due to invalid
`git` commands usage.
  • Loading branch information
nieomylnieja authored Jul 30, 2024
1 parent af0b01e commit fd01e5e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ The config file is a JSON file which describes the synchronization process.
"name": "go-libyear",
// Required. URL used to clone the repository.
"url": "https://github.com/nieomylnieja/go-libyear.git",
// Optional. Default: "main".
// Optional. Default: "origin/main".
"ref": "dev-branch",
// Optional, merged with global 'ignore' section.
// Follows the same format and rules but applies ONLY to the specified repository.
Expand Down
6 changes: 4 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/nieomylnieja/gitsync/internal/diff"
)

const defaultRef = "origin/main"

type Config struct {
StorePath string `json:"storePath,omitempty"`
Root *RepositoryConfig `json:"root"`
Expand Down Expand Up @@ -117,15 +119,15 @@ func (c *Config) setDefaults() error {
for _, repo := range c.Repositories {
repo.path = filepath.Join(c.GetStorePath(), repo.Name)
if repo.Ref == "" {
repo.defaultRef = "main"
repo.defaultRef = defaultRef
}
}
c.Root.path = filepath.Join(c.GetStorePath(), c.Root.Name)
if c.Root.Ignore != nil {
return errors.New("root repository cannot have ignore rules")
}
if c.Root.Ref == "" {
c.Root.defaultRef = "main"
c.Root.defaultRef = defaultRef
}
return nil
}
Expand Down
19 changes: 15 additions & 4 deletions internal/gitsync/gitsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ func Run(conf *config.Config, command Command) error {
if err := updateTrackedRef(repo); err != nil {
return errors.Wrapf(err, "failed to update repository %s", repo.Name)
}
if err := checkoutSyncBranch(repo); err != nil {
return err
if command == CommandSync {
if err := checkoutSyncBranch(repo); err != nil {
return err
}
}
}
updatedFiles := make(map[*config.RepositoryConfig][]string, len(conf.Repositories))
Expand Down Expand Up @@ -350,7 +352,16 @@ func updateTrackedRef(repo *config.RepositoryConfig) error {
"--force",
"--all",
); err != nil {
return errors.Wrap(err, "failed to clone repository")
return errors.Wrap(err, "failed to fetch repository objects and refs")
}
if _, err := execCmd(
"git",
"-C", path,
"checkout",
"--force",
ref,
); err != nil {
return errors.Wrapf(err, "failed to force checkout %s ref", ref)
}
if _, err := execCmd(
"git",
Expand All @@ -359,7 +370,7 @@ func updateTrackedRef(repo *config.RepositoryConfig) error {
"--hard",
ref,
); err != nil {
return errors.Wrapf(err, "failed to reset repository to %s ref", ref)
return errors.Wrapf(err, "failed to hard reset repository to %s ref", ref)
}
return nil
}
Expand Down

0 comments on commit fd01e5e

Please sign in to comment.