Debian Codegraph @

internal/job/job.go

raw symbols SCIP
1 package job
2
3 import (
4 "github.com/isacikgoz/gitbatch/internal/command"
5 "github.com/isacikgoz/gitbatch/internal/git"
6 )
7
8 // Job relates the type of the operation and the entity
9 type Job struct {
10 // JobType is to select operation type that will be applied to repository
11 JobType Type
12 // Repository points to the repository that will be used for operation
13 Repository *git.Repository
14 // Options is a placeholder for operation options
15 Options interface{}
16 }
17
18 // Type is the a git operation supported
19 type Type string
20
21 const (
22 // FetchJob is wrapper of git fetch command
23 FetchJob Type = "fetch"
24
25 // PullJob is wrapper of git pull command
26 PullJob Type = "pull"
27
28 // MergeJob is wrapper of git merge command
29 MergeJob Type = "merge"
30
31 // CheckoutJob is wrapper of git merge command
32 CheckoutJob Type = "checkout"
33 )
34
35 // starts the job
36 func (j *Job) start() error {
37 j.Repository.SetWorkStatus(git.Working)
38 // TODO: Better implementation required
39 switch mode := j.JobType; mode {
40 case FetchJob:
41 j.Repository.State.Message = "fetching.."
42 var opts *command.FetchOptions
43 if j.Options != nil {
44 opts = j.Options.(*command.FetchOptions)
45 } else {
46 opts = &command.FetchOptions{
47 RemoteName: j.Repository.State.Remote.Name,
48 CommandMode: command.ModeNative,
49 }
50 }
51 if err := command.Fetch(j.Repository, opts); err != nil {
52 j.Repository.SetWorkStatus(git.Fail)
53 j.Repository.State.Message = err.Error()
54 return err
55 }
56 case PullJob:
57 j.Repository.State.Message = "pulling.."
58 var opts *command.PullOptions
59 if j.Repository.State.Branch.Upstream == nil {
60 j.Repository.SetWorkStatus(git.Fail)
61 j.Repository.State.Message = "upstream not set"
62 return nil
63 }
64 if j.Options != nil {
65 opts = j.Options.(*command.PullOptions)
66 } else {
67 opts = &command.PullOptions{
68 RemoteName: j.Repository.State.Remote.Name,
69 CommandMode: command.ModeNative,
70 }
71 }
72 if err := command.Pull(j.Repository, opts); err != nil {
73 j.Repository.SetWorkStatus(git.Fail)
74 j.Repository.State.Message = err.Error()
75 return err
76 }
77 case MergeJob:
78 j.Repository.State.Message = "merging.."
79 if j.Repository.State.Branch.Upstream == nil {
80 j.Repository.SetWorkStatus(git.Fail)
81 j.Repository.State.Message = "upstream not set"
82 return nil
83 }
84 if err := command.Merge(j.Repository, &command.MergeOptions{
85 BranchName: j.Repository.State.Branch.Upstream.Name,
86 }); err != nil {
87 j.Repository.SetWorkStatus(git.Fail)
88 j.Repository.State.Message = err.Error()
89 return err
90 }
91 case CheckoutJob:
92 j.Repository.State.Message = "switching to.."
93 var opts *command.CheckoutOptions
94 if j.Options != nil {
95 opts = j.Options.(*command.CheckoutOptions)
96 } else {
97 opts = &command.CheckoutOptions{
98 TargetRef: "master",
99 CommandMode: command.ModeNative,
100 }
101 }
102 if err := command.Checkout(j.Repository, opts); err != nil {
103 j.Repository.SetWorkStatus(git.Fail)
104 j.Repository.State.Message = err.Error()
105 return err
106 }
107 default:
108 j.Repository.SetWorkStatus(git.Available)
109 return nil
110 }
111 return nil
112 }