Skip to content

Commit 654f1a6

Browse files
committed
Use Checkwd() to detect multiple issues
Can return one of three errors: - NotRepositor: Like the original use of the function, returned when the working directory is not a repository - NotAnnex: When there is no annex information. This usually results in a warning and suggests running 'gin init' to (re)initialise the repository. - UpgradeRequired: If a v5 (or < v7) repository is detected. This is used to warn the user that they should upgrade the annex.
1 parent 4d705dc commit 654f1a6

21 files changed

+151
-53
lines changed

ginclient/repos.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ func (gincl *Client) CloneRepo(repopath string, clonechan chan<- git.RepoFileSta
454454
// If a new commit is created and a default remote exists, the new commit is pushed to initialise the remote as well.
455455
// Returns 'true' if (and only if) a commit was created.
456456
func CommitIfNew() (bool, error) {
457-
if !git.Checkwd() {
457+
if git.Checkwd() == git.NotRepository {
458+
// Other errors allowed
458459
return false, fmt.Errorf("not a repository")
459460
}
460461
_, err := git.RevParse("HEAD")
@@ -635,7 +636,7 @@ func CheckoutFileCopies(commithash string, paths []string, outpath string, suffi
635636
// Optionally initialised as a bare repository (for annex directory remotes).
636637
func (gincl *Client) InitDir(bare bool) error {
637638
initerr := ginerror{Origin: "InitDir", Description: "Error initialising local directory"}
638-
if !git.Checkwd() {
639+
if git.Checkwd() == git.NotRepository {
639640
err := git.Init(bare)
640641
if err != nil {
641642
initerr.UError = err.Error()

gincmd/addremotecmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func defaultRemoteIfUnset(name string) {
152152
}
153153

154154
func addRemote(cmd *cobra.Command, args []string) {
155-
if !git.Checkwd() {
155+
if git.Checkwd() == git.NotRepository {
156156
Die(ginerrors.NotInRepo)
157157
}
158158
flags := cmd.Flags()

gincmd/commitcmd.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import (
1414

1515
func commit(cmd *cobra.Command, args []string) {
1616
prStyle := determinePrintStyle(cmd)
17-
if !git.Checkwd() {
17+
switch git.Checkwd() {
18+
case git.NotRepository:
1819
Die(ginerrors.NotInRepo)
20+
case git.NotAnnex:
21+
Warn(ginerrors.MissingAnnex)
22+
case git.UpgradeRequired:
23+
annexVersionNotice()
1924
}
2025

2126
commitmsg, _ := cmd.Flags().GetString("message")

gincmd/common.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ func Die(msg interface{}) {
7272
os.Exit(1)
7373
}
7474

75+
// Warn prints a warning message to stderr, logs it, and returns without interruption.
76+
func Warn(msg string) {
77+
log.Write("Showing warning: %q", msg)
78+
fmt.Fprintf(color.Error, "%s %s\n", yellow("[warning]"), msg)
79+
}
80+
7581
// Exit prints a message to stdout and exits the program with status 0.
7682
func Exit(msg string) {
7783
if len(msg) > 0 {
@@ -113,6 +119,22 @@ func requirelogin(cmd *cobra.Command, gincl *ginclient.Client, prompt bool) {
113119
gincl.LoadToken()
114120
}
115121

122+
func annexVersionNotice() {
123+
msg := `The current repository is using an old layout for annexed data. It is recommended that you upgrade to the newest version. You may still use it as is for now, but in the future the upgrade will happen automatically. This message will continue to appear for affected git-annex operations until you upgrade.
124+
125+
Run 'gin annex upgrade' followed by 'gin init' to upgrade this repository now. The operation should only take a few seconds.
126+
127+
Visit the following page for a description of how this change may affect your workflow:`
128+
w := termwidth()
129+
if w > 80 {
130+
w = 80
131+
}
132+
msg = wrap.Wrap(msg, w)
133+
// append URL after wrapping to avoid breaking the URL with spaces
134+
msg += "https://gin.g-node.org/G-Node/gin-cli-releases/src/master/v1.7#changes"
135+
fmt.Println(msg)
136+
}
137+
116138
func usageDie(cmd *cobra.Command) {
117139
cmd.Help()
118140
// exit without message

gincmd/downloadcmd.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ func download(cmd *cobra.Command, args []string) {
1717
// TODO: no client necessary? Just use remotes
1818
conf := config.Read()
1919
gincl := ginclient.New(conf.DefaultServer)
20-
if !git.Checkwd() {
20+
switch git.Checkwd() {
21+
case git.NotRepository:
2122
Die(ginerrors.NotInRepo)
23+
case git.NotAnnex:
24+
Warn(ginerrors.MissingAnnex)
25+
case git.UpgradeRequired:
26+
annexVersionNotice()
2227
}
2328
remote, err := ginclient.DefaultRemote()
2429
if err != nil { // TODO && len(remotes) == 0 {

gincmd/getcontentcmd.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ func getContent(cmd *cobra.Command, args []string) {
1616
// TODO: no need for client; use remotes (and all keys?)
1717
gincl := ginclient.New(conf.DefaultServer)
1818
requirelogin(cmd, gincl, prStyle != psJSON)
19-
if !git.Checkwd() {
19+
switch git.Checkwd() {
20+
case git.NotRepository:
2021
Die(ginerrors.NotInRepo)
22+
case git.NotAnnex:
23+
Warn(ginerrors.MissingAnnex)
24+
case git.UpgradeRequired:
25+
annexVersionNotice()
2126
}
2227

2328
if prStyle == psDefault {

gincmd/ginerrors/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ const (
1515

1616
// MissingGitUser is returned when a string is assumed to be a git configuration but does not contain a user (missing @)
1717
MissingGitUser = "could not determine git username (no @)"
18+
19+
// MissingAnnex is returned when a repository doesn't have annex initialised (can also be used as a warning)
20+
MissingAnnex = "no annex information found: run 'gin init' to initialise annex"
1821
)

gincmd/lockcmd.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ import (
1212

1313
func lock(cmd *cobra.Command, args []string) {
1414
prStyle := determinePrintStyle(cmd)
15-
if !git.Checkwd() {
15+
switch git.Checkwd() {
16+
case git.NotRepository:
1617
Die(ginerrors.NotInRepo)
18+
case git.NotAnnex:
19+
Warn(ginerrors.MissingAnnex)
20+
case git.UpgradeRequired:
21+
annexVersionNotice()
1722
}
1823
if prStyle != psJSON {
1924
fmt.Println(":: Locking files")

gincmd/lscmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import (
1515
)
1616

1717
func lsRepo(cmd *cobra.Command, args []string) {
18-
if !git.Checkwd() {
18+
switch git.Checkwd() {
19+
case git.NotRepository:
1920
Die(ginerrors.NotInRepo)
21+
case git.NotAnnex:
22+
Warn(ginerrors.MissingAnnex)
2023
}
2124

2225
flags := cmd.Flags()

gincmd/remotescmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ func printremotes(cmd *cobra.Command, args []string) {
1515
flags := cmd.Flags()
1616
jsonout, _ := flags.GetBool("json")
1717

18-
if !git.Checkwd() {
18+
switch git.Checkwd() {
19+
case git.NotRepository:
1920
Die(ginerrors.NotInRepo)
21+
case git.NotAnnex:
22+
Warn(ginerrors.MissingAnnex)
2023
}
2124

2225
remotes, err := git.RemoteShow()

0 commit comments

Comments
 (0)